From: Hector Martin <marcan@marcan.st> To: Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will@kernel.org>, Marc Zyngier <maz@kernel.org>, Mark Rutland <mark.rutland@arm.com> Cc: Zayd Qumsieh <zayd_qumsieh@apple.com>, Justin Lu <ih_justin@apple.com>, Ryan Houdek <Houdek.Ryan@fex-emu.org>, Mark Brown <broonie@kernel.org>, Ard Biesheuvel <ardb@kernel.org>, Mateusz Guzik <mjguzik@gmail.com>, Anshuman Khandual <anshuman.khandual@arm.com>, Oliver Upton <oliver.upton@linux.dev>, Miguel Luis <miguel.luis@oracle.com>, Joey Gouly <joey.gouly@arm.com>, Christoph Paasch <cpaasch@apple.com>, Kees Cook <keescook@chromium.org>, Sami Tolvanen <samitolvanen@google.com>, Baoquan He <bhe@redhat.com>, Joel Granados <j.granados@samsung.com>, Dawei Li <dawei.li@shingroup.cn>, Andrew Morton <akpm@linux-foundation.org>, Florent Revest <revest@chromium.org>, David Hildenbrand <david@redhat.com>, Stefan Roesch <shr@devkernel.io>, Andy Chiu <andy.chiu@sifive.com>, Josh Triplett <josh@joshtriplett.org>, Oleg Nesterov <oleg@redhat.com>, Helge Deller <deller@gmx.de>, Zev Weiss <zev@bewilderbeest.net>, Ondrej Mosnacek <omosnace@redhat.com>, Miguel Ojeda <ojeda@kernel.org>, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Asahi Linux <asahi@lists.linux.dev>, Hector Martin <marcan@marcan.st> Subject: [PATCH 1/4] prctl: Introduce PR_{SET,GET}_MEM_MODEL Date: Thu, 11 Apr 2024 09:51:20 +0900 [thread overview] Message-ID: <20240411-tso-v1-1-754f11abfbff@marcan.st> (raw) In-Reply-To: <20240411-tso-v1-0-754f11abfbff@marcan.st> 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> --- include/linux/memory_ordering_model.h | 11 +++++++++++ include/uapi/linux/prctl.h | 5 +++++ kernel/sys.c | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/linux/memory_ordering_model.h b/include/linux/memory_ordering_model.h new file mode 100644 index 000000000000..267a12ca6630 --- /dev/null +++ b/include/linux/memory_ordering_model.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MEMORY_ORDERING_MODEL_H +#define __ASM_MEMORY_ORDERING_MODEL_H + +/* Arch hooks to implement the PR_{GET_SET}_MEM_MODEL prctls */ + +struct task_struct; +int arch_prctl_mem_model_get(struct task_struct *t); +int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val); + +#endif diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..961216093f11 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,9 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f +#define PR_GET_MEM_MODEL 0x6d4d444c +#define PR_SET_MEM_MODEL 0x4d4d444c +# define PR_SET_MEM_MODEL_DEFAULT 0 +# define PR_SET_MEM_MODEL_TSO 1 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index f8e543f1e38a..6af659a9f826 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -45,6 +45,7 @@ #include <linux/version.h> #include <linux/ctype.h> #include <linux/syscall_user_dispatch.h> +#include <linux/memory_ordering_model.h> #include <linux/compat.h> #include <linux/syscalls.h> @@ -2442,6 +2443,16 @@ static int prctl_get_auxv(void __user *addr, unsigned long len) return sizeof(mm->saved_auxv); } +int __weak arch_prctl_mem_model_get(struct task_struct *t) +{ + return -EINVAL; +} + +int __weak arch_prctl_mem_model_set(struct task_struct *t, unsigned long val) +{ + return -EINVAL; +} + SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5) { @@ -2757,6 +2768,16 @@ 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_GET_MEM_MODEL: + if (arg2 || arg3 || arg4 || arg5) + return -EINVAL; + error = arch_prctl_mem_model_get(me); + break; + case PR_SET_MEM_MODEL: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_prctl_mem_model_set(me, arg2); + break; default: error = -EINVAL; break; -- 2.44.0
WARNING: multiple messages have this Message-ID (diff)
From: Hector Martin <marcan@marcan.st> To: Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will@kernel.org>, Marc Zyngier <maz@kernel.org>, Mark Rutland <mark.rutland@arm.com> Cc: Zayd Qumsieh <zayd_qumsieh@apple.com>, Justin Lu <ih_justin@apple.com>, Ryan Houdek <Houdek.Ryan@fex-emu.org>, Mark Brown <broonie@kernel.org>, Ard Biesheuvel <ardb@kernel.org>, Mateusz Guzik <mjguzik@gmail.com>, Anshuman Khandual <anshuman.khandual@arm.com>, Oliver Upton <oliver.upton@linux.dev>, Miguel Luis <miguel.luis@oracle.com>, Joey Gouly <joey.gouly@arm.com>, Christoph Paasch <cpaasch@apple.com>, Kees Cook <keescook@chromium.org>, Sami Tolvanen <samitolvanen@google.com>, Baoquan He <bhe@redhat.com>, Joel Granados <j.granados@samsung.com>, Dawei Li <dawei.li@shingroup.cn>, Andrew Morton <akpm@linux-foundation.org>, Florent Revest <revest@chromium.org>, David Hildenbrand <david@redhat.com>, Stefan Roesch <shr@devkernel.io>, Andy Chiu <andy.chiu@sifive.com>, Josh Triplett <josh@joshtriplett.org>, Oleg Nesterov <oleg@redhat.com>, Helge Deller <deller@gmx.de>, Zev Weiss <zev@bewilderbeest.net>, Ondrej Mosnacek <omosnace@redhat.com>, Miguel Ojeda <ojeda@kernel.org>, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Asahi Linux <asahi@lists.linux.dev>, Hector Martin <marcan@marcan.st> Subject: [PATCH 1/4] prctl: Introduce PR_{SET,GET}_MEM_MODEL Date: Thu, 11 Apr 2024 09:51:20 +0900 [thread overview] Message-ID: <20240411-tso-v1-1-754f11abfbff@marcan.st> (raw) In-Reply-To: <20240411-tso-v1-0-754f11abfbff@marcan.st> 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> --- include/linux/memory_ordering_model.h | 11 +++++++++++ include/uapi/linux/prctl.h | 5 +++++ kernel/sys.c | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/linux/memory_ordering_model.h b/include/linux/memory_ordering_model.h new file mode 100644 index 000000000000..267a12ca6630 --- /dev/null +++ b/include/linux/memory_ordering_model.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MEMORY_ORDERING_MODEL_H +#define __ASM_MEMORY_ORDERING_MODEL_H + +/* Arch hooks to implement the PR_{GET_SET}_MEM_MODEL prctls */ + +struct task_struct; +int arch_prctl_mem_model_get(struct task_struct *t); +int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val); + +#endif diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..961216093f11 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,9 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f +#define PR_GET_MEM_MODEL 0x6d4d444c +#define PR_SET_MEM_MODEL 0x4d4d444c +# define PR_SET_MEM_MODEL_DEFAULT 0 +# define PR_SET_MEM_MODEL_TSO 1 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index f8e543f1e38a..6af659a9f826 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -45,6 +45,7 @@ #include <linux/version.h> #include <linux/ctype.h> #include <linux/syscall_user_dispatch.h> +#include <linux/memory_ordering_model.h> #include <linux/compat.h> #include <linux/syscalls.h> @@ -2442,6 +2443,16 @@ static int prctl_get_auxv(void __user *addr, unsigned long len) return sizeof(mm->saved_auxv); } +int __weak arch_prctl_mem_model_get(struct task_struct *t) +{ + return -EINVAL; +} + +int __weak arch_prctl_mem_model_set(struct task_struct *t, unsigned long val) +{ + return -EINVAL; +} + SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5) { @@ -2757,6 +2768,16 @@ 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_GET_MEM_MODEL: + if (arg2 || arg3 || arg4 || arg5) + return -EINVAL; + error = arch_prctl_mem_model_get(me); + break; + case PR_SET_MEM_MODEL: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_prctl_mem_model_set(me, arg2); + break; default: error = -EINVAL; break; -- 2.44.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2024-04-11 0:51 UTC|newest] Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-04-11 0:51 [PATCH 0/4] arm64: Support the TSO memory model Hector Martin 2024-04-11 0:51 ` Hector Martin 2024-04-11 0:51 ` Hector Martin [this message] 2024-04-11 0:51 ` [PATCH 1/4] prctl: Introduce PR_{SET,GET}_MEM_MODEL Hector Martin 2024-04-11 0:51 ` [PATCH 2/4] arm64: Implement PR_{GET,SET}_MEM_MODEL for always-TSO CPUs Hector Martin 2024-04-11 0:51 ` Hector Martin 2024-04-11 0:51 ` [PATCH 3/4] arm64: Introduce scaffolding to add ACTLR_EL1 to thread state Hector Martin 2024-04-11 0:51 ` Hector Martin 2024-04-11 0:51 ` [PATCH 4/4] arm64: Implement Apple IMPDEF TSO memory model control Hector Martin 2024-04-11 0:51 ` Hector Martin 2024-04-11 1:37 ` [PATCH 0/4] arm64: Support the TSO memory model Neal Gompa 2024-04-11 1:37 ` Neal Gompa 2024-04-11 13:28 ` Will Deacon 2024-04-11 13:28 ` Will Deacon 2024-04-11 14:19 ` Hector Martin 2024-04-11 14:19 ` Hector Martin 2024-04-11 18:43 ` Hector Martin 2024-04-11 18:43 ` Hector Martin 2024-04-16 2:22 ` Zayd Qumsieh 2024-04-16 2:22 ` Zayd Qumsieh 2024-04-19 16:58 ` Will Deacon 2024-04-19 16:58 ` Will Deacon 2024-04-19 18:05 ` Catalin Marinas 2024-04-19 18:05 ` Catalin Marinas 2024-04-19 16:58 ` Will Deacon 2024-04-19 16:58 ` Will Deacon 2024-04-20 11:37 ` Marc Zyngier 2024-04-20 11:37 ` Marc Zyngier 2024-05-02 0:10 ` Zayd Qumsieh 2024-05-02 0:10 ` Zayd Qumsieh 2024-05-02 13:25 ` Marc Zyngier 2024-05-02 13:25 ` Marc Zyngier 2024-05-06 8:20 ` Jonas Oberhauser 2024-05-06 8:20 ` Jonas Oberhauser 2024-04-20 12:13 ` Eric Curtin 2024-04-20 12:13 ` Eric Curtin 2024-04-20 12:15 ` Eric Curtin 2024-04-20 12:15 ` Eric Curtin 2024-05-06 11:21 ` Sergio Lopez Pascual 2024-05-06 11:21 ` Sergio Lopez Pascual 2024-05-06 16:12 ` Marc Zyngier 2024-05-06 16:12 ` Marc Zyngier 2024-05-06 16:20 ` Eric Curtin 2024-05-06 16:20 ` Eric Curtin 2024-05-06 22:04 ` Sergio Lopez Pascual 2024-05-06 22:04 ` Sergio Lopez Pascual 2024-05-02 0:16 ` Zayd Qumsieh 2024-05-02 0:16 ` Zayd Qumsieh 2024-05-07 10:24 ` Alex Bennée 2024-05-07 10:24 ` Alex Bennée 2024-05-07 14:52 ` Ard Biesheuvel 2024-05-07 14:52 ` Ard Biesheuvel 2024-05-09 11:13 ` Catalin Marinas 2024-05-09 11:13 ` Catalin Marinas 2024-05-09 12:31 ` Neal Gompa 2024-05-09 12:31 ` Neal Gompa 2024-05-09 12:56 ` Catalin Marinas 2024-05-09 12:56 ` Catalin Marinas 2024-04-16 2:11 ` Zayd Qumsieh 2024-04-16 2:11 ` Zayd Qumsieh
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240411-tso-v1-1-754f11abfbff@marcan.st \ --to=marcan@marcan.st \ --cc=Houdek.Ryan@fex-emu.org \ --cc=akpm@linux-foundation.org \ --cc=andy.chiu@sifive.com \ --cc=anshuman.khandual@arm.com \ --cc=ardb@kernel.org \ --cc=asahi@lists.linux.dev \ --cc=bhe@redhat.com \ --cc=broonie@kernel.org \ --cc=catalin.marinas@arm.com \ --cc=cpaasch@apple.com \ --cc=david@redhat.com \ --cc=dawei.li@shingroup.cn \ --cc=deller@gmx.de \ --cc=ih_justin@apple.com \ --cc=j.granados@samsung.com \ --cc=joey.gouly@arm.com \ --cc=josh@joshtriplett.org \ --cc=keescook@chromium.org \ --cc=linux-arm-kernel@lists.infradead.org \ --cc=linux-kernel@vger.kernel.org \ --cc=mark.rutland@arm.com \ --cc=maz@kernel.org \ --cc=miguel.luis@oracle.com \ --cc=mjguzik@gmail.com \ --cc=ojeda@kernel.org \ --cc=oleg@redhat.com \ --cc=oliver.upton@linux.dev \ --cc=omosnace@redhat.com \ --cc=revest@chromium.org \ --cc=samitolvanen@google.com \ --cc=shr@devkernel.io \ --cc=will@kernel.org \ --cc=zayd_qumsieh@apple.com \ --cc=zev@bewilderbeest.net \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.