Skip to content

Commit 744f0f6

Browse files
marcanherrnst
authored andcommitted
prctl: Introduce PR_{SET,GET}_MEM_MODEL
On some architectures, it is possible to query and/or change the CPU memory model. This allows userspace to switch to a stricter memory model for performance reasons, such as when emulating code for another architecture where that model is the default. Introduce two prctls to allow userspace to query and set the memory model for a thread. Two models are initially defined: - PR_SET_MEM_MODEL_DEFAULT requests the default memory model for the architecture. - PR_SET_MEM_MODEL_TSO requests the x86 TSO memory model. PR_SET_MEM_MODEL is allowed to set a stricter memory model than requested if available, in which case it will return successfully. If the requested memory model cannot be fulfilled, it will return an error. The memory model that was actually set can be queried by a subsequent call to PR_GET_MEM_MODEL. Examples: - On a CPU with not support for a memory model at least as strong as TSO, PR_SET_MEM_MODEL(PR_SET_MEM_MODEL_TSO) fails. - On a CPU with runtime-configurable TSO support, PR_SET_MEM_MODEL can toggle the memory model between DEFAULT and TSO at will. - On a CPU where the only memory model is at least as strict as TSO, PR_GET_MEM_MODEL will return PR_SET_MEM_MODEL_DEFAULT, and PR_SET_MEM_MODEL(PR_SET_MEM_MODEL_TSO) will return success but leave the memory model at PR_SET_MEM_MODEL_DEFAULT. This implies that the default is in fact at least as strict as TSO. Signed-off-by: Hector Martin <[email protected]>
1 parent 5872a51 commit 744f0f6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

include/uapi/linux/prctl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,10 @@ struct prctl_mm_map {
294294

295295
#define PR_SET_MEMORY_MERGE 67
296296
#define PR_GET_MEMORY_MERGE 68
297+
298+
#define PR_GET_MEM_MODEL 0x6d4d444c
299+
#define PR_SET_MEM_MODEL 0x4d4d444c
300+
# define PR_SET_MEM_MODEL_DEFAULT 0
301+
# define PR_SET_MEM_MODEL_TSO 1
302+
297303
#endif /* _LINUX_PRCTL_H */

kernel/sys.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,16 @@ static int prctl_get_auxv(void __user *addr, unsigned long len)
23992399
return sizeof(mm->saved_auxv);
24002400
}
24012401

2402+
int __weak arch_prctl_mem_model_get(struct task_struct *t)
2403+
{
2404+
return -EINVAL;
2405+
}
2406+
2407+
int __weak arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
2408+
{
2409+
return -EINVAL;
2410+
}
2411+
24022412
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
24032413
unsigned long, arg4, unsigned long, arg5)
24042414
{
@@ -2708,6 +2718,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
27082718
error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
27092719
break;
27102720
#endif
2721+
case PR_GET_MEM_MODEL:
2722+
if (arg2 || arg3 || arg4 || arg5)
2723+
return -EINVAL;
2724+
error = arch_prctl_mem_model_get(me);
2725+
break;
2726+
case PR_SET_MEM_MODEL:
2727+
if (arg3 || arg4 || arg5)
2728+
return -EINVAL;
2729+
error = arch_prctl_mem_model_set(me, arg2);
2730+
break;
27112731
default:
27122732
error = -EINVAL;
27132733
break;

0 commit comments

Comments
 (0)