From patchwork Fri Feb 9 06:40:45 2024 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: 13550906 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 46FFAC4828F for ; Fri, 9 Feb 2024 06:41:11 +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=oHW+yK+4GTWgeaZx/F6fKeaLn8shYT4INwo8varqCvk=; b=LitmERCzfQ9Zv/ d/Bkz6j8inOh0p0D5ttI+ownrrRTDO/8b5uAv7W6MVYXIm3hMleo4CTRY6FLr+qVqzIhGTVLFBbX9 779SFMGGgrioex4iwbb7frll78XCGHDjdtRDVEMnfAkG3QJ2cgSF9qDXvfzYxRpTtry7nOJISGYgg sbsGrXTC2WqBgjUuodbJeBOeXYZyfI+Q2fXvbPno1hJX0NcrVd7DaXnLgX9ay3msbvotCdvcZYU6u daIY15VbsWV72z8wO8An4yUOWJy44fca8IBUHnd5lXBQz34D83mwJyuoOEVYGh06Vznf/Vc/eT+A/ dP8J5nVcWBAgQHNCsAhA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZK-0000000GFjc-48yW; Fri, 09 Feb 2024 06:41:02 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZI-0000000GFhK-27mm for linux-riscv@lists.infradead.org; Fri, 09 Feb 2024 06:41:01 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-5114cd44f6aso792870e87.1 for ; Thu, 08 Feb 2024 22:40:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1707460857; x=1708065657; 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=DNljdwCtgXX7yfrlqeBRJqsYJccdvVcfZVPFTIAjXvY=; b=ce1bRbH/DZZmgRiWrtaZHZ/I39JTd0I9AutxKTU2WYsVBar9kNFHuXZ7i7FPPvtmEJ Tc71Ecyn11JGQKjuFM8t8pibSSlFOwFFmfxOdcS9MR4VX+tLVm3uBw/npONZUzQptP4S n86U9G4uJQWzf0jR3M2/PXfRexZGHgmEa51XiasLyBCThzBhHXr0JpNtvaaeyt8BCht8 rLGccMJqbWVVySm7Gb9OdsVCaNR62AylEs5dKF/FsjMyjvrWPsbDqeDzkoGZjW3fqIr7 lsob9Fpc8Af7ggr865HrW4+zsx1nXI0D0+mvbcwJ0AeJ8lDBen074/LBUxu3BUPrmD5i vj7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707460857; x=1708065657; 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=DNljdwCtgXX7yfrlqeBRJqsYJccdvVcfZVPFTIAjXvY=; b=rLg5Xq0/Xpz6iTcZFBX+z78sm4BFruDWyldKZ7bzZZaKndPLVHy+CWKgL+I0wl5s8G belt94PZNEIbW4EaY3gubaYPE8Fm8BOUImx3WIZz+E6dV8AVPBZBfm9a04i2sjR06yPk oN94HEqjuvhVvL1vOVUPBb0WXSCsD5BfPAUMpqOxaFQ9VOxTIMS3k7asZzDynfpvbw5F arbDWTz7aW7AevrbgqPM6oVWMZM7pQ0OJzyj7CxZSzijS47uORaKdBuAjfdExar7Fsll lWK0eb6uSMzg59ea875jsq5dizKMsonZ0qh4HGpqFLE9B6NwM5PyecmI2h8VlhjD8FPg DtkA== X-Gm-Message-State: AOJu0YxMDmw8g8tg0ESnZXxNsVeUoEj3nXyA2YzqJs9y2o2n4/VCC+5h sG+Wcu717P78KlLydKCKb/+nA6CBiJ6LyGbLZi2VS8bxhFxgpFomiY09N41YGHzReYLIm/AO+o8 kiWg= X-Google-Smtp-Source: AGHT+IEDhflFCI5m8yckBlcrHhcrQQ3ldJE+rE+09x08ZsDb/7emc/oMoKtCCmmPX+z/DuDjiayT1g== X-Received: by 2002:a05:6512:3e6:b0:511:6f2e:d23f with SMTP id n6-20020a05651203e600b005116f2ed23fmr303429lfq.43.1707460856741; Thu, 08 Feb 2024 22:40:56 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUaL+yEba5wm8i34ZSQJNQMvgW6J8dd/cBl9IdEWc4gJ7MotgFZlcF2vvfMBwq4KUmx959cSuSlRe3Qlxhu4KfyqJb2tOoRm5+/3LKr8WMyPrWXCd0f7xaUwI4heMRaEmdtkB03hUfP5VdCbRxXOVGwsR0+FM1Q1eZoT/7ZQi1h75nUQBGOq452eMVDPqEQ8vJwybjkXHa2P2956nsp4ell4ZmZvfFkG8AKEkC5PDV4EWdVxBuTYDqOJNmx1zA9X/5CFXLdE444VW797AcovWMNx1QCzp6cwWLS/G62gqSwdVZ7fcJQ43MAnuGCwwog6JYyd6M9ml084zVece5wtDbgMfpl4Sv8asE365tfO4DEl6+vtRyJF4xLh/1HFvY125EiYDehiYJ38vOgbELtcCFGs1wHqol92ZvncDJ8YfqeD45mSFdS2XDr27wJOT5rd7XW3hSmOGw68GuLvIa3T/ZLkytmvs8fxKFIPSkDmohuOir+dLdnL5vTzPWCnggEtTBV8+3Up3TAwJMviLwjRq8UIczfXtPNcmhJcCpHztwzbuA+uoNBNHblC7eiGpy4q6IiD5jrEguUynT7b6FxUPuJSZP2dHgF7k3mj4f4LPHzEr2SG3i88ssWGkRqdrNHQ1ZI5rCrEBFNLjuxg5HalTNGp3XqhWOgEjQbRp0tTDcJBB+XKMkl+LYW0RvJXS3pvfghirr/1o+ONtKmvg7GmB48p+6rxDPAoGvj1ZFLP/6Akd+iMWgHO2//OKQD3DXKwQbHq347V96eRJyHRponH2izjQ8kzxgOZfAdrZFvAV4u8iMvFhUbNVQzAWmHSpTX+SbxrqrlkuyDYJfZ5RTU9vYpeWj1ZGKimEXlKvhwx0IsnA== Received: from beast.fritz.box (62-178-148-172.cable.dynamic.surfer.at. [62.178.148.172]) by smtp.gmail.com with ESMTPSA id vb9-20020a170907d04900b00a370a76d3a0sm441180ejc.123.2024.02.08.22.40.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 22:40:56 -0800 (PST) From: =?utf-8?q?Christoph_M=C3=BCllner?= To: linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Jonas Oberhauser , Peter Zijlstra , 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 , Will Deacon , Daniel Lustig , Brendan Sweeney , Andrew Waterman , Brendan Sweeney , Andrea Parri , Hans Boehm Cc: =?utf-8?q?Christoph_M=C3=BCllner?= Subject: [RFC PATCH v2 1/6] mm: Add dynamic memory consistency model switching Date: Fri, 9 Feb 2024 07:40:45 +0100 Message-ID: <20240209064050.2746540-2-christoph.muellner@vrull.eu> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240209064050.2746540-1-christoph.muellner@vrull.eu> References: <20240209064050.2746540-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-20240208_224100_575149_0753982A X-CRM114-Status: GOOD ( 24.05 ) 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 Some architectures have support to change the memory consistency model at run time. This patch adds a new field 'active_memory_consistency_model' to task_struct that allows architecture code to store the active model as a per-process property. To avoid useless overhead, the mechanism needs to be explicitly enabled in the architecture's Kconfig. Signed-off-by: Christoph Müllner --- .../mm/dynamic-memory-consistency-model.rst | 49 +++++++++++++++++++ Documentation/mm/index.rst | 1 + arch/Kconfig | 14 ++++++ include/linux/sched.h | 5 ++ 4 files changed, 69 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..3117c3d82b2b --- /dev/null +++ b/Documentation/mm/dynamic-memory-consistency-model.rst @@ -0,0 +1,49 @@ +.. SPDX-License-Identifier: GPL-2.0 + +========================================== +Dynamic memory consistency model switching +========================================== + +:Author: Christoph Müllner +:Date: 1 Feb 2024 + +This document gives an overview about dynamic memory consistency model +switching for user mode at run-time. + +Memory consistency models +========================= + +A 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). + +The following restrictions apply for switching the memory consistency model +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. diff --git a/Documentation/mm/index.rst b/Documentation/mm/index.rst index 31d2ac306438..36d40502b421 100644 --- a/Documentation/mm/index.rst +++ b/Documentation/mm/index.rst @@ -43,6 +43,7 @@ above structured documentation, or deleted if it has served its purpose. arch_pgtable_helpers balance damon/index + dynamic-memory-consistency-model free_page_reporting hmm hwpoison diff --git a/arch/Kconfig b/arch/Kconfig index a5af0edd3eb8..89d4e27f9b80 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1479,6 +1479,20 @@ config ARCH_HAS_NONLEAF_PMD_YOUNG address translations. Page table walkers that clear the accessed bit may use this capability to reduce their search space. +config ARCH_HAS_DYNAMIC_MEMORY_CONSISTENCY_MODEL + bool + help + An arch should select this symbol if it supports switching + the memory consistency model at run-time. + +config DYNAMIC_MEMORY_CONSISTENCY_MODEL + bool "Dynamic memory consistency model support" + depends on ARCH_HAS_DYNAMIC_MEMORY_CONSISTENCY_MODEL + default y + help + This option turns on the support to switch the memory consistency + model at runtime on a per-process-base. + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig" diff --git a/include/linux/sched.h b/include/linux/sched.h index ffe8f618ab86..5cbd3a3b80ab 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -979,6 +979,11 @@ struct task_struct { /* Canary value for the -fstack-protector GCC feature: */ unsigned long stack_canary; #endif + +#ifdef CONFIG_DYNAMIC_MEMORY_CONSISTENCY_MODEL + unsigned long memory_consistency_model; +#endif + /* * Pointers to the (original) parent process, youngest child, younger sibling, * older sibling, respectively. (p->father can be replaced with