From patchwork Fri Nov 24 07:21:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christoph_M=C3=BCllner?= X-Patchwork-Id: 13467077 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D610CC636BD for ; Fri, 24 Nov 2023 07:22:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IcJRlQbtwFDvPzU3yPDAouAzKQWfFyrSinpwM3JhS2g=; b=CaJbXTTp8rDlF/ lVlcOz+yuufUkeGTIP33HbtC4Zq06mJ88X3OZAycRxIKBVppA87RVuLGJmbn8aF4uS1Ur5BxDGJyC TLA1D88XqZHCgIMiuzCmoWQTtGu9iANm5a7nX6WQRm2X/YBzpqUKQB3v5mUKTH0fZFuAc5fj+vN7m VTDbIsMoiQk+a05FxNgDBtOo4bmOEI1mGHO5qnfrWr38E1yZwuzl8/yb4Z90Pm8SViiwnAqVrS+d7 kn2fCrCZurmXEZ27sd8h/26UiQ+d5VkngXGXFOouL0f3mr6cmVXK2RPrUMUP0OKzS3pu1QYvj4QDD ak0qY3sS0S7flxSMwb7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r6QW3-006NPR-2b; Fri, 24 Nov 2023 07:22:19 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r6QVx-006NK2-2g for linux-riscv@lists.infradead.org; Fri, 24 Nov 2023 07:22:15 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a02ba1f500fso228074566b.0 for ; Thu, 23 Nov 2023 23:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1700810530; x=1701415330; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FdW6KLqAappiJz7t43GP8yDkVw3RQUH/PCIQaxhT8io=; b=bi1NBLP/85G9uEMMwZk9UmoT5CN69idPqwD+DDi30xpJTv6T0Rnz5ASXOLh2eV+LaF f3iBL+vDl5eHKxLVTbfrKp/4oMXZxzsM1N80L2QvRe0Pkn3qHALM2OQ5byUsxJPidmvG 0oTuicSSpGs+MH76U0afRaXcMuBI7jZC7VVT9kpdXVWowP1ZY0QRWoZxnOQ30txSM/Hq oFEb+rFR6bGLUARShMRDpY5+zON3gL0y7CEo0ww1OaG9jNnDNwxIxdJW/hWPdJRtj1xf NY/6A4QSSgkIBpURnPODZz6y5giXQuqIMSLtVBDfKZ47KNu0OMGLwXohdrFPOf//lYXB YsRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700810530; x=1701415330; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FdW6KLqAappiJz7t43GP8yDkVw3RQUH/PCIQaxhT8io=; b=d0UDQs5xLhRdn/IFkEENmAGgOrzwKQjsW4zawAq/l/hCizh4DQDfGIm99OuMf90Ja/ 4HisX8F41qLJTURMTvWDFzCNjPhbIc/HHVyjQtQoKgPTKn1MvmM/qHZ0nng2yMTu4OHG 4zRYBj/+F6kPE1kjrOkZlhoGGUit2Q837XKgwNzoTvcOb57eD9uoog0pl19SaJZyiHtX z7PrTuxaw8QzUDIpEVOr5ZeQ9HAdFyJKvOKZ1/ca5FaNatxx+iMwSWk+ngdbi4kewsv2 9viS+FZ+E0uv7wy3rztfj+YMYASAAU8P4HBHKlc2UBv+2uCmW1WjFJnY/bv6cA+Oep5K iCEw== X-Gm-Message-State: AOJu0YyiyOi3aiF91mSuRniRZW/yPKTH1e2I7EBGFrPwJHLkIzquReXU NwlQ/b6tofRdztL5SO1OrfFaXd0tt5nozqveUmP0QuMr X-Google-Smtp-Source: AGHT+IH6YJTwKcWKKY/43rAcT0UuuNs5BImrDdkrAupQGRjsaG9PttQVyjsPB2Eshk3SwrOXEogoeQ== X-Received: by 2002:a17:906:220c:b0:a09:e716:5ea8 with SMTP id s12-20020a170906220c00b00a09e7165ea8mr121667ejs.18.1700810530557; Thu, 23 Nov 2023 23:22:10 -0800 (PST) Received: from beast.fritz.box (62-178-148-172.cable.dynamic.surfer.at. [62.178.148.172]) by smtp.gmail.com with ESMTPSA id q18-20020a1709060e5200b00992f2befcbcsm1709930eji.180.2023.11.23.23.22.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 23:22:10 -0800 (PST) From: Christoph Muellner To: linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Palmer Dabbelt , Paul Walmsley , Albert Ou , Andrew Morton , Shuah Khan , Jonathan Corbet , Anup Patel , Philipp Tomsich , Andrew Jones , Guo Ren , Daniel Henrique Barboza , Conor Dooley , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Alan Stern , Andrea Parri , Will Deacon , Daniel Lustig , Peter Zijlstra Cc: =?utf-8?q?Christoph_M=C3=BCllner?= Subject: [RFC PATCH 3/5] uapi: prctl: Add new prctl call to set/get the memory consistency model Date: Fri, 24 Nov 2023 08:21:40 +0100 Message-ID: <20231124072142.2786653-4-christoph.muellner@vrull.eu> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231124072142.2786653-1-christoph.muellner@vrull.eu> References: <20231124072142.2786653-1-christoph.muellner@vrull.eu> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231123_232213_870035_BA574554 X-CRM114-Status: GOOD ( 24.31 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Christoph Müllner Some ISAs have a weak default memory consistency model and allow to switch to a more strict model at runtime. This patch adds calls to the prctl interface which allow to get and set the current memory consistency model. 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 --- .../mm/dynamic-memory-consistency-model.rst | 58 +++++++++++++++++++ include/uapi/linux/prctl.h | 3 + kernel/sys.c | 12 ++++ 3 files changed, 73 insertions(+) create mode 100644 Documentation/mm/dynamic-memory-consistency-model.rst diff --git a/Documentation/mm/dynamic-memory-consistency-model.rst b/Documentation/mm/dynamic-memory-consistency-model.rst new file mode 100644 index 000000000000..21675b41ec84 --- /dev/null +++ b/Documentation/mm/dynamic-memory-consistency-model.rst @@ -0,0 +1,58 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================ +Dynamic memory consistency model +================================ + +This document gives an overview of the userspace interface to change memory +consistency model at run-time. + + +What is a memory consistency model? +=================================== + +The memory consistency model is a set of guarantees a CPU architecture +provides about (re-)ordering memory accesses. Each architecture defines +its own model and set of rules within that, which are carefully specified. +The provided guarantees have consequences for the microarchitectures (e.g., +some memory consistency models allow reordering stores after loads) and +the software executed within this model (memory consistency models that +allow reordering memory accesses provide memory barrier instructions +to enforce additional guarantees when needed explicitly). + +Details about the architecture-independent memory consistency model abstraction +in the Linux kernel and the use of the different types of memory barriers +can be found here: + + Documentation/memory-barriers.txt + +Two models can be in a weaker/stronger relation. I.e., a consistency +model A is weaker/stronger than another model B if A provides a subset/superset +of the constraints that B provides. + +Some architectures define more than one memory consistency model. +On such architectures, switching the memory consistency model at run-time +to a stronger one is possible because software written for the weaker model is +compatible with the constraints of the stronger model. + +If two models are not in a weaker/stronger relation, switching between +them will violate the consistency assumptions that the software was +written under (i.e., causing subtle bugs that are very hard to debug). + +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, then + -1 is returned, and errno is set to EINVAL. 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..a8a217a10767 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 (-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;