Skip to content

Commit 244735d

Browse files
marcanjannau
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 <marcan@marcan.st> Reviewed-by: Neal Gompa <neal@gompa.dev>
1 parent 7d0a66e commit 244735d

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ASM_MEMORY_ORDERING_MODEL_H
3+
#define __ASM_MEMORY_ORDERING_MODEL_H
4+
5+
/* Arch hooks to implement the PR_{GET_SET}_MEM_MODEL prctls */
6+
7+
struct task_struct;
8+
int arch_prctl_mem_model_get(struct task_struct *t);
9+
int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val);
10+
11+
#endif

include/uapi/linux/prctl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
/* Values to pass as first argument to prctl() */
88

9+
#define PR_GET_MEM_MODEL 0x6d4d444c
10+
#define PR_SET_MEM_MODEL 0x4d4d444c
11+
# define PR_SET_MEM_MODEL_DEFAULT 0
12+
# define PR_SET_MEM_MODEL_TSO 1
13+
914
#define PR_SET_PDEATHSIG 1 /* Second arg is a signal */
1015
#define PR_GET_PDEATHSIG 2 /* Second arg is a ptr to return the signal */
1116

kernel/sys.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <linux/version.h>
4646
#include <linux/ctype.h>
4747
#include <linux/syscall_user_dispatch.h>
48+
#include <linux/memory_ordering_model.h>
4849

4950
#include <linux/compat.h>
5051
#include <linux/syscalls.h>
@@ -2515,6 +2516,16 @@ static int prctl_set_thp_disable(bool thp_disable, unsigned long flags,
25152516
return 0;
25162517
}
25172518

2519+
int __weak arch_prctl_mem_model_get(struct task_struct *t)
2520+
{
2521+
return -EINVAL;
2522+
}
2523+
2524+
int __weak arch_prctl_mem_model_set(struct task_struct *t, unsigned long val)
2525+
{
2526+
return -EINVAL;
2527+
}
2528+
25182529
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
25192530
unsigned long, arg4, unsigned long, arg5)
25202531
{
@@ -2528,6 +2539,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
25282539

25292540
error = 0;
25302541
switch (option) {
2542+
case PR_GET_MEM_MODEL:
2543+
if (arg2 || arg3 || arg4 || arg5)
2544+
return -EINVAL;
2545+
error = arch_prctl_mem_model_get(me);
2546+
break;
2547+
case PR_SET_MEM_MODEL:
2548+
if (arg3 || arg4 || arg5)
2549+
return -EINVAL;
2550+
error = arch_prctl_mem_model_set(me, arg2);
2551+
break;
25312552
case PR_SET_PDEATHSIG:
25322553
if (!valid_signal(arg2)) {
25332554
error = -EINVAL;

0 commit comments

Comments
 (0)