Skip to content

Commit b5c6de4

Browse files
committed
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 1d9d900 commit b5c6de4

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

include/uapi/linux/prctl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,9 @@ struct prctl_mm_map {
290290
#define PR_SET_VMA 0x53564d41
291291
# define PR_SET_VMA_ANON_NAME 0
292292

293+
#define PR_GET_MEM_MODEL 0x6d4d444c
294+
#define PR_SET_MEM_MODEL 0x4d4d444c
295+
# define PR_SET_MEM_MODEL_DEFAULT 0
296+
# define PR_SET_MEM_MODEL_TSO 1
297+
293298
#endif /* _LINUX_PRCTL_H */

kernel/sys.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,16 @@ static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
23882388
PR_MDWE_REFUSE_EXEC_GAIN : 0;
23892389
}
23902390

2391+
int __weak arch_prctl_mem_model_get(struct task_struct *t)
2392+
{
2393+
return -EINVAL;
2394+
}
2395+
2396+
int __weak arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
2397+
{
2398+
return -EINVAL;
2399+
}
2400+
23912401
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
23922402
unsigned long, arg4, unsigned long, arg5)
23932403
{
@@ -2672,6 +2682,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
26722682
case PR_SET_VMA:
26732683
error = prctl_set_vma(arg2, arg3, arg4, arg5);
26742684
break;
2685+
case PR_GET_MEM_MODEL:
2686+
if (arg2 || arg3 || arg4 || arg5)
2687+
return -EINVAL;
2688+
error = arch_prctl_mem_model_get(me);
2689+
break;
2690+
case PR_SET_MEM_MODEL:
2691+
if (arg3 || arg4 || arg5)
2692+
return -EINVAL;
2693+
error = arch_prctl_mem_model_set(me, arg2);
2694+
break;
26752695
default:
26762696
error = -EINVAL;
26772697
break;

0 commit comments

Comments
 (0)