[RFC PATCH v2 2/6] uapi: prctl: Add new prctl call to set/get the memory consistency model

From: Christoph Müllner
Date: Fri Feb 09 2024 - 01:41:28 EST


This patch defines a prctl uAPI for switching the active memory
consistency model of user-space processes.

The implementation follows the way other prctl calls are implemented by
disabling them unless arch-specific code provides the relevant macros.

Signed-off-by: Christoph Müllner <christoph.muellner@xxxxxxxx>
---
.../mm/dynamic-memory-consistency-model.rst | 27 +++++++++++++++++++
include/uapi/linux/prctl.h | 3 +++
kernel/sys.c | 12 +++++++++
3 files changed, 42 insertions(+)

diff --git a/Documentation/mm/dynamic-memory-consistency-model.rst b/Documentation/mm/dynamic-memory-consistency-model.rst
index 3117c3d82b2b..1fce855a1fad 100644
--- a/Documentation/mm/dynamic-memory-consistency-model.rst
+++ b/Documentation/mm/dynamic-memory-consistency-model.rst
@@ -47,3 +47,30 @@ at run-time:
* Only switching from a weaker to a stronger model is safe.
* The stronger memory model affects all threads of a process, when running in user mode.
* Forked processes derive their active memory model from their parents.
+
+User API via prctl
+==================
+
+Two prctl calls are defined to get/set the active memory consistency model:
+
+* prctl(PR_GET_MEMORY_CONSISTENCY_MODEL)
+
+ Returns the active memory consistency model for the calling process/thread.
+ If the architecture does not support dynamic memory consistency models,
+ then -1 is returned, and errno is set to EINVAL.
+
+* prctl(PR_SET_MEMORY_CONSISTENCY_MODEL, unsigned long new_model)
+
+ Switches the memory consistency model for the calling process/thread
+ to the given model. If the architecture does not support dynamic
+ memory consistency models, or does not support the provided model, or
+ does not allow to switch to the proveided model then -1 is returned,
+ and errno is set to EINVAL.
+
+Supported memory consistency models
+===================================
+
+This section defines the memory consistency models which are supported
+by the prctl interface.
+
+<none>
diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 370ed14b1ae0..579662731eaa 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -306,4 +306,7 @@ struct prctl_mm_map {
# define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc
# define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f

+#define PR_SET_MEMORY_CONSISTENCY_MODEL 71
+#define PR_GET_MEMORY_CONSISTENCY_MODEL 72
+
#endif /* _LINUX_PRCTL_H */
diff --git a/kernel/sys.c b/kernel/sys.c
index e219fcfa112d..a1b92a38f889 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -146,6 +146,12 @@
#ifndef RISCV_V_GET_CONTROL
# define RISCV_V_GET_CONTROL() (-EINVAL)
#endif
+#ifndef SET_MEMORY_CONSISTENCY_MODEL
+# define SET_MEMORY_CONSISTENCY_MODEL(a) (-EINVAL)
+#endif
+#ifndef GET_MEMORY_CONSISTENCY_MODEL
+# define GET_MEMORY_CONSISTENCY_MODEL() (-EINVAL)
+#endif

/*
* this is where the system-wide overflow UID and GID are defined, for
@@ -2743,6 +2749,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
case PR_RISCV_V_GET_CONTROL:
error = RISCV_V_GET_CONTROL();
break;
+ case PR_SET_MEMORY_CONSISTENCY_MODEL:
+ error = SET_MEMORY_CONSISTENCY_MODEL(arg2);
+ break;
+ case PR_GET_MEMORY_CONSISTENCY_MODEL:
+ error = GET_MEMORY_CONSISTENCY_MODEL();
+ break;
default:
error = -EINVAL;
break;
--
2.43.0