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 From patchwork Fri Feb 9 06:40:46 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: 13550909 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 642B0C4828F for ; Fri, 9 Feb 2024 06:41:15 +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=Fn47QmKsS6gTO3wNV6r0Sq9DAxLLLBU7cHm/Rm4Hduk=; b=pK/DiqRR3F/7kS xEE/7IaiUn4MwNPFRe+gR43G2UH5Ly4LSMV2/czmX9EpZLNseQzld6NouHWtcg94WADKCew86xfT5 BFVlLgDUW3UXZvWfY+X8qwDneVkgtSASjSwwJA2YDDoDjulw7TGSIo9CYrbjhe4VOaR82IHW8/u29 9Gu0T67xCqBELkhBb2lMxwxgv9E8owCC2D1mSm7t+9lNAQcb77rM70Ed/FLHVI0IOgqttJf/sGVDR ldXCocHZ9pFqUiPaFPzi+CD7fzRB7esxn1bWCQGRyeOXWO4s6sS+EiNwf7Vu6Xzn630zk3uMYUcYN sB9SSBzJVCZ8HuN9lQrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZL-0000000GFkH-3RU5; Fri, 09 Feb 2024 06:41:03 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZI-0000000GFhg-1uOS for linux-riscv@lists.infradead.org; Fri, 09 Feb 2024 06:41:02 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a3810e975aaso66933966b.3 for ; Thu, 08 Feb 2024 22:41:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1707460859; x=1708065659; 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=ka0BwVIa89CrbsNMJkPh2Vgcr3mcWFHipPKQFS87pYA=; b=lAA5IacUoXP4BPgOemetpFYLjuNP7yzXv7GazoGaSZIm9p2doJI3bZLig/iV72lTmf jAtaBqHaJzlZ6S7Ovor6uWW67PQLbH9OCDgOOtrC8+biC8/YHoOfGqQDGnPP6OHq2NvN pPJFbaZl4/vsRXmqRsVLVPt0mzHLiH/+ZX6UJo664Q+6BOXUvKmvgf1mCa8F88SyhdB9 q3M7aOFR5czeYQoMVusapeYsf3ALoWKURm6C4QpsZAkzAbQjDYbN/2BAcYh2SofTNOtm yfvnx7l598RrnGcFboQAFzm+HksH2F/Dm2OU1nMkZyJN4/TptfSqcal423iqBHT0EJc8 /3Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707460859; x=1708065659; 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=ka0BwVIa89CrbsNMJkPh2Vgcr3mcWFHipPKQFS87pYA=; b=CaVH4qZOv0orDLh5+aBBxPRNz23A7fynCgj5BBwFrUeY8RU9q6hiEcF1Htl8s0C+Ho JpHn4vlE8DPkvPXFvVnZ7NXV4dqs80SpQG0j7JoMo6HI3okeDk3B61nuLHUJPQgEY30v BgBYHyHSeHuqPTtmAxXrBi/BK2FiYXYrTxAoj7m+qG4DJHf7JPZjag70Ks6J4a9KblE8 TOi4iqYezHYVQZ0TeYUQvd00jBh0k/75VrgK1Wxpya35V4cHqeRqBh0Wb41NtNuTjPB5 6JdkFNnY2Q9inuX2lUVWLEieapmQ7QUCbV/+Ja2gRCu6qM5D92YQ52AUC1XRu1vMyPsw o+Gg== X-Gm-Message-State: AOJu0YxfefIRw66+8ZFYuW85gJDIDehR9YVZqUyPWjfeHigp3kj4YqwQ WduYyYd3UcryyYTrdIsmaWpubiO3nnT8kYA16tQWhPma7z3/fzTXw9y851nkoJGXMtIzajCRfWe uDSQ= X-Google-Smtp-Source: AGHT+IFEN03if+6niuHDJ7y6KQ3qH470BeXh3gKz+Sb465eM/2K3LAMO8sCRGXg+1jKiB4FL7oHnjw== X-Received: by 2002:a17:906:2e91:b0:a37:7b4:9538 with SMTP id o17-20020a1709062e9100b00a3707b49538mr530857eji.33.1707460858580; Thu, 08 Feb 2024 22:40:58 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUGzRsAZwmfQ3VpSld4acgsFVB8Z2BpFxm5NYoupbUP/wDHZUcHWD0xshhR8SzfywjNN/6uiHCGHWB3luBB7eAro95MsHdnxJbi/UsaoOWSi/QX7Yt3BGzNKdp5rZMZI4ZCaJrcG3SSADnGciJhN51oKuauuTPK+/oaFDQEq9fFxnkTdCcgWwxBRc2/qEVCRnTSSXWdN9Dhwxos973APpZ8ybYob1XLGih9Ej/b67MnvWZBSQzE/otQIR76P/XvNjg/eVczYytDfWZXVLUrGDTLltumZbxt+67DG9vbcnDKew0X/chAbTQfYrTYpkLRlj0kwsodyv04SaDJ1gad2bOJTPEQtGLPKz2/YsnmOLREsvkoageVxwSbJhiDF30C7qkgV++HU2WIkmb+wOIuU3Ck3GzZIMla0sFstAz8dXyRNm+yDeUoEflVK0q1lKALfz8tkUBEGc6omFxBsljE/KqQwVHtBdygQ8fNqUfMEWoT03c74tkohgGFw1IsWg7MdAVF9WedboaLk5CYiTXW9RAWxMEBDsDVChXyTdHCz2NF7PPk1QYS+zBnkZyypbUI02tKIE7oorhJtzbg0Vkuslqhe7u5XzLFZLe8WE7GD35XxR1jGkVb1zdSYPBxCzO/3LmeKOWXN12YDTlnDMFgGmKCvKNAs5IipyIg22VgBDA/GW0iNeAax58E0aap/Swu9mxIkfNzrAZOSnJRYq6ZR6wASlI8grPksL2Sabilobn98XkAJ1WuIQ5fOHUl+iRQMsuItpiI724RZwSPd/uKOZnbK/M+PBA+0pYuXqXUK9gz+7NrViKr8a+JhLSAFif+ZnT4CZaNekMrga7Hjfyh9xRRxpJzHHYAXrXnPSDDbJWFZw== 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.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 22:40:58 -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 2/6] uapi: prctl: Add new prctl call to set/get the memory consistency model Date: Fri, 9 Feb 2024 07:40:46 +0100 Message-ID: <20240209064050.2746540-3-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_559820_194B34E5 X-CRM114-Status: GOOD ( 17.69 ) 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 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 --- .../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. + + 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; From patchwork Fri Feb 9 06:40:47 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: 13550907 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 CA752C4829D for ; Fri, 9 Feb 2024 06:41:12 +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=CMUHtoWViHB1LzzhibfuPM/lDBdLvoaqVBCDyst5ZCI=; b=Ig6rAgq8xoVAX+ kCbHCigukpMg8yFUqKj8Gx2QZLk6XW0jIMZ45k8mIx75K5P3djRc4NQLbjvTZ1LQIgBZoaBN1kTSx zwfORbr/CDvo0mGIxead3G3/HFRHsfKeAln3/QvQrJihgHHkQOoFAKRxC1FA/ycd7v76QczmL55fi DF1DGAg/BhmtcaNow0nu9x3yEVyPYaepqeYnEd0jNjcv+zQapY1F0kVSlUieW4inB/f55bRaWwQsU qwJnp8ejMP+g0pQPtQCkNAICtyXZtZotpLaiCxeUd/OWyytWiO9vU1tP6paLp647ZeKyB/fOtjL3O uq0trBXlSxmdhXZ5h6sw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZP-0000000GFmo-01ia; Fri, 09 Feb 2024 06:41:07 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZK-0000000GFio-1ayK for linux-riscv@lists.infradead.org; Fri, 09 Feb 2024 06:41:03 +0000 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-558f523c072so917843a12.2 for ; Thu, 08 Feb 2024 22:41:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1707460860; x=1708065660; 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=+Iy+qb8cqxclx2b4u4iSMkolO0A5gDplvFNecdg0ef0=; b=dz7ow+/0JkCKK2xI1D/QJ7G8hYNinPHtUlKg4JnyCCxj33+7kxId2B+0WIXpPRWrJS jhvEywoYpY2LrPAMTzje5PrNmpyBfiyc4EJFMg87yeQL+ADUfbaY2hUfhxhUF984VeBP gDqPuUCZRzVtMog65WB59hzvbV77uG6B2/GTNwrbYYaj9C1MwvFA4JMxqNMpHKLbsu6a OJgLsn67hVNDiaziu9FhkIXpfJapC0rYRhR8ee+CCHoBR1+qlN8NNgWmH+oksP4okUU8 7LPGvrUGG/D5E4xb5H+CEHjwICi3vnFhVun8yGxA4E/bm1dMGsNExyf2Chk1yIgSoDSk SMTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707460860; x=1708065660; 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=+Iy+qb8cqxclx2b4u4iSMkolO0A5gDplvFNecdg0ef0=; b=NfzgSD0/x/9RcqMWeTMV9zFW/XnkNnJMW7eBz40FMeGdMAdsHFiuvi9YGij9GQfUBK hu058BVycyWAJsSVHhStvdYGfjwRoom/h67ukPyqko7irYGYdvZUoiUT9/1F1BCAUlDE FioiiS85azy/qFZ9lBMYmtYLoGhCuZNlxTG5c06vH6uhR9bIQqPwxISwq647ix475Oj3 ccvzx+WsBb7co7wBcTXirE0hO2H09Z7I6LJQyO7W2ZHJ5NC/OxgNwrb7uthI8NkKyhh2 3bV8G2rCziLD0DzolXFN2DnCsiJlwfh0HDFhSQkpkxPPnmHm/B9z/RzbWtTrN25b2XAs 4uSg== X-Gm-Message-State: AOJu0YyLkU6KptgkGbjyCj944McKazCSrhzeGW9VePpNkAex6VE59r3p QCzU+4vv0gJ6wyexIWDy5v+zwvh3xYu1SAdmZdLirGQy8N/rRZvXG3R+3coTVOZz9b8mzXSNrpE RhMM= X-Google-Smtp-Source: AGHT+IGWG9nd+Kfmz7RZ7epqow+tvQ3hOS+FSn8n3iqZKNodfZG8lzKrxgzFpIiKDWC28PJLXwIyaw== X-Received: by 2002:a17:906:4a45:b0:a37:625d:397d with SMTP id a5-20020a1709064a4500b00a37625d397dmr485869ejv.44.1707460860511; Thu, 08 Feb 2024 22:41:00 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCX71W5p9A9pSxDne91E1S+4dp8HZwpU0zSjneWfoUp5Vt++iXRw5L1lGgzZko6taQEC94C0rij3mTasjZ0imQ33SelhsJS8vXg4MFZtDCv3a9Gu1ybl3KXUIVa/k6uPIBveTIa7wX2YUYYpaN/AncLrj6jhN7f4uDuH3v0+HXBHBXVavl0bmm2QP2neV+G7RfmU6WvDDb68V6ZsmTsUexb0+hxKOgQTUyOGKVs/ElNaFdvJpmrYOWVlHO/+fINXm02jH5erk/ApvVYwhuWTS83Z1B2DnGQy50tkRqCxt4mvAZptPoEZCqEd6XOTmHSOSzwz3AsaNcBpM4dGGBcNxAsOghL32JGvy6vXlrPeegNpxjHokJdnMSwPOyx7T8mEdpY0CuITCe+uhB2WiyuEJ3NZEysAY/GF2uFXej92BT6tDCuuFJJ4uEwVNTsD9De0/gTCETvzUMtB0D0KUoCuMGitSAdyiKvG0doTkND9n9FXzJ3Nle8XkRSutC6b6qdobtfR0tbHInlRn9BG9rEjmMisBCVeW4ZHYzTUS3iMwDwLxFTyrHfHtpuzOUWPvLrcMUgHOMqGTbJRM+8NNjzOr/cX6QKD5lg3uGrTc4TWWFgxvJM2dVt9n3YpvTQ2QGbVPL6BvDdbUEfkLaq+86GBL0CjzxWC0Kj9KubMYkgcqf6gmMYCY8MPnSvP3UBJU8na4T4zRLNMQtPrpqC+kX3yyoGzBNXpldu4u9aoCrp+KpWj/EZtCX057UdjBZclhwV+K+RRcmRfBL3EInLWnKOO9xfh9FMp9OsQqlp4hK6GphEw5217o/PLI9FDc6/pJl5Jv8xPfNoOB3RtxRkwBx541UsO373RLlu50htB1kQEZIushg== 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.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 22:40:59 -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 3/6] RISC-V: Enable dynamic memory consistency model support with Ssdtso Date: Fri, 9 Feb 2024 07:40:47 +0100 Message-ID: <20240209064050.2746540-4-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_224102_489272_865F41DA X-CRM114-Status: GOOD ( 28.13 ) 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 This patch implements dynamic memory consistency switching on RISC-V using the Ssdtso ISA extension. Ssdtso is a RISC-V ISA extension, which allows to switch the memory consistency model of less privileged modes from RVWMO to TSO at runtime. The active model is controlled by a DTSO bit in the {m,h,s}envcfg CSRs (per-hart state). TSO is a stronger memory ordering than RVWMO, which means that executing software that was written for RVWMO can also run under TSO without causing memory consistency issues. Since RVWMO is the default model, switching to TSO is safe. The patch introduces Ssdtso basic support: * enable dynamic memory consistency switching if Ssdtso support is enabled in the kernel config * define the relevant envcfg bits * add the relevant code to store/restore the DTSO state * register the the extension in hwcap/cpufeatures * extend task_struct to keep the state across context switches * add a Kconfig symbol to disable Ssdtso support Signed-off-by: Christoph Müllner --- arch/riscv/Kconfig | 11 ++++ arch/riscv/include/asm/csr.h | 1 + arch/riscv/include/asm/dtso.h | 97 ++++++++++++++++++++++++++++++ arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/switch_to.h | 3 + arch/riscv/kernel/asm-offsets.c | 3 + arch/riscv/kernel/cpufeature.c | 1 + 7 files changed, 117 insertions(+) create mode 100644 arch/riscv/include/asm/dtso.h diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index bffbd869a068..1b26797e7cdd 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -23,6 +23,7 @@ config RISCV select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DEBUG_VM_PGTABLE select ARCH_HAS_DEBUG_WX + select ARCH_HAS_DYNAMIC_MEMORY_CONSISTENCY_MODEL if RISCV_ISA_SSDTSO select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_GIGANTIC_PAGE @@ -480,6 +481,16 @@ config RISCV_ISA_C If you don't know what to do here, say Y. +config RISCV_ISA_SSDTSO + bool "Ssdtso extension support for dynamic TSO memory ordering" + default y + help + Adds support to dynamically detect the presence of the Ssdtso + ISA-extension and allows user-space processes to activate/deactivate + the TSO memory ordering model at run-time. + + If you don't know what to do here, say Y. + config RISCV_ISA_SVNAPOT bool "Svnapot extension support for supervisor mode NAPOT pages" depends on 64BIT && MMU diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 510014051f5d..83e5737d720d 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -194,6 +194,7 @@ /* xENVCFG flags */ #define ENVCFG_STCE (_AC(1, ULL) << 63) #define ENVCFG_PBMTE (_AC(1, ULL) << 62) +#define ENVCFG_DTSO (_AC(1, UL) << 8) #define ENVCFG_CBZE (_AC(1, UL) << 7) #define ENVCFG_CBCFE (_AC(1, UL) << 6) #define ENVCFG_CBIE_SHIFT 4 diff --git a/arch/riscv/include/asm/dtso.h b/arch/riscv/include/asm/dtso.h new file mode 100644 index 000000000000..25f9bb30884e --- /dev/null +++ b/arch/riscv/include/asm/dtso.h @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2023 Christoph Muellner + */ + +#ifndef __ASM_RISCV_DTSO_H +#define __ASM_RISCV_DTSO_H + +#define RISCV_MEMORY_CONSISTENCY_MODEL_WMO 0 +#define RISCV_MEMORY_CONSISTENCY_MODEL_TSO 1 + +#ifdef CONFIG_RISCV_ISA_SSDTSO + +#include +#include +#include + +static __always_inline bool has_dtso(void) +{ + return riscv_has_extension_unlikely(RISCV_ISA_EXT_SSDTSO); +} + +static __always_inline bool has_ztso(void) +{ + return riscv_has_extension_unlikely(RISCV_ISA_EXT_ZTSO); +} + +static inline bool dtso_is_enabled(void) +{ + if (has_dtso()) + return csr_read(CSR_SENVCFG) & ENVCFG_DTSO; + return 0; +} + +static inline void dtso_disable(void) +{ + if (has_dtso() && !has_ztso()) + csr_clear(CSR_SENVCFG, ENVCFG_DTSO); +} + +static inline void dtso_enable(void) +{ + if (has_dtso() && !has_ztso()) + csr_set(CSR_SENVCFG, ENVCFG_DTSO); +} + +static inline unsigned long get_memory_consistency_model( + struct task_struct *task) +{ + return task->memory_consistency_model; +} + +static inline void set_memory_consitency_model(struct task_struct *task, + unsigned long model) +{ + task->memory_consistency_model = model; +} + +static inline void dtso_restore(struct task_struct *task) +{ + unsigned long cur_model = get_memory_consistency_model(task); + + if (cur_model == RISCV_MEMORY_CONSISTENCY_MODEL_TSO) + dtso_enable(); + else + dtso_disable(); +} + +static inline void __switch_to_dtso(struct task_struct *prev, + struct task_struct *next) +{ + struct pt_regs *regs; + + regs = task_pt_regs(prev); + + /* + * We don't need to save the DTSO bit, because we don't expect it to + * change. So any mechanism that changes the DTSO bit, needs to take + * care to write to task->memory_consistency_model (and reschedule + * all threads of the process). + */ + + dtso_restore(next); +} + +#else /* ! CONFIG_RISCV_ISA_SSDTSO */ + +static __always_inline bool has_dtso(void) { return false; } +static __always_inline bool dtso_is_enabled(void) { return false; } +#define dtso_disable() do { } while (0) +#define dtso_enable() do { } while (0) +#define dtso_restore(task) do { } while (0) +#define __switch_to_dtso(prev, next) do { } while (0) + +#endif /* CONFIG_RISCV_ISA_SSDTSO */ + +#endif /* ! __ASM_RISCV_DTSO_H */ diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 5340f818746b..88740f419d13 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -80,6 +80,7 @@ #define RISCV_ISA_EXT_ZFA 71 #define RISCV_ISA_EXT_ZTSO 72 #define RISCV_ISA_EXT_ZACAS 73 +#define RISCV_ISA_EXT_SSDTSO 74 #define RISCV_ISA_EXT_MAX 128 #define RISCV_ISA_EXT_INVALID U32_MAX diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7efdb0584d47..bedf7fe12c1d 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,8 @@ do { \ __switch_to_fpu(__prev, __next); \ if (has_vector()) \ __switch_to_vector(__prev, __next); \ + if (has_dtso()) \ + __switch_to_dtso(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index a03129f40c46..b5dc39788c41 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -80,6 +80,9 @@ void asm_offsets(void) #ifdef CONFIG_STACKPROTECTOR OFFSET(TSK_STACK_CANARY, task_struct, stack_canary); #endif +#ifdef CONFIG_DYNAMIC_MEMORY_CONSISTENCY_MODEL + OFFSET(TASK_MEM_CONSISTENCY_MODEL, task_struct, memory_consistency_model); +#endif DEFINE(PT_SIZE, sizeof(struct pt_regs)); OFFSET(PT_EPC, pt_regs, epc); diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 89920f84d0a3..b63d6b699238 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -303,6 +303,7 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN), __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA), __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF), + __RISCV_ISA_EXT_DATA(ssdtso, RISCV_ISA_EXT_SSDTSO), __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC), __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL), __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT), From patchwork Fri Feb 9 06:40:48 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: 13550912 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 68A52C4829F for ; Fri, 9 Feb 2024 06:41:15 +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=OO/K+OtDVrhh2zjDQCbEOFQz62fx1Jh2IehE26PF1zk=; b=EhhvRKDoscJEip AvuA1wi/fPQNnuoM49pFNDKjfRYFYWOBWQA640/v9eWkwq4+fNuilKXRP/51+0lphWvy/HNtHBKWT sycwUsXwMrWEuaLToKOGKTmGojf6nQdjKIACb22vMg12Y53SyTbjfbPZnkX9Hrf//zEpgooT59BL0 aJIzsba2rvRgxLndeGd2KfvbQrXFZBEr/PeIwdzod9oLMB/58l9QZFnvwhO4c70Dd/lsx9g+LYM8A aymmxyzH8cqEC05fNQZRWEROhV9G0t1/BiQfwMqSkaTjdR33W29/YRfEGXm28eFznrvbbNa9UDvMv mayTfYH5ddm19vHtYQyQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZQ-0000000GFoo-3MGp; Fri, 09 Feb 2024 06:41:08 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZN-0000000GFjs-1NyX for linux-riscv@lists.infradead.org; Fri, 09 Feb 2024 06:41:06 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-a370e7e1e02so63018466b.0 for ; Thu, 08 Feb 2024 22:41:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1707460862; x=1708065662; 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=ST8e7AXP20QlvDbKLGY7HjIR6vrJd80LvNJ9Brmy7H4=; b=GN1C7feUg2GirHaPkrN0DvyM++tL/5aCijCh9DTx6E9dHr1Ud0o1dm+vFyvJfhopvw xbUSp9OQo+uuwOGf2H8vG5gq1rfpaLDfkKaLHf2vjqzMI/pMTXugidSwevcjhAxSkKJ/ 5ss+rAro6ptNwhklT4PDov2FwfKMwt7iaUWXNbIs0RjuyDrkIR70c0ugtixKprPmC3uY n8NJ/fdj/ZYG5kINBSTzc3x8awEr7K7H3SguP3CrWiNSG7SKfk4YziF/FkrxHsTh5k6C ed265u53qen6wy3+IddiE783KJDX7V02QireYEEH48uRsW/RX/v2Q5MY69kXMvzWKxXK 2LRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707460862; x=1708065662; 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=ST8e7AXP20QlvDbKLGY7HjIR6vrJd80LvNJ9Brmy7H4=; b=sT9RmafNdsK99zx0HTpk8cRd50lDTsPnEhP64ofpszJ/b/3NIZ7gpkwCvlFzXf5Bbo extzJjfkkY5Tmvns/f4/81PuBioXXyXhCyVcjKD0CkTjOIFlY0WVxRAhGkf2ZKk4QIZZ hmnlEeXZtHCDzVRS2hrgSVSayjLYu+xH9WMnWCKREaY8as+JpGetQensDagxhSQR8hIj jjL29f4XylllAltYGFOoQ9WaDbFmZS73bvqFa2j+gBIv9M82bQgcGVoGXF8SfpGFF98I QJ/HB/HKvPcfTQCOx6oFl58WOMfDj7X3zA1LwFjQ/1s5dOac9QHcTZFbhz5toUizaCsq 53aA== X-Gm-Message-State: AOJu0Yw0yujW1VSBfTE49i0snYLlevHbyCgc1GrJnxll5SYqYWoG8ZXy z/vMq4BGhCg+A2jUPkCjhzfGs1rX9a2DGkjsMlNMSd13m3AEEBlagnHh6u5GOaJg+Rdvbq99JsW CWO4= X-Google-Smtp-Source: AGHT+IHJxpP7wNCT0XP54IBN7nE37NyEnMgUpbvOnsyakzsvJGztY1XpZVbrFW4xhEKC6uRrNdPefA== X-Received: by 2002:a17:906:22cf:b0:a3b:bf5d:1089 with SMTP id q15-20020a17090622cf00b00a3bbf5d1089mr512293eja.20.1707460862356; Thu, 08 Feb 2024 22:41:02 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV5OzmNE8LiMqHmaGw/QPWfvFxV8RhGPBp+rnFG+Na0Q3cHsxEhfqQL2QspBYEjTxncL+jeqmpc6BHhKbUGQZLQZewRKoXY1iAMbJcBSUMim5GOBuHcYcmKRDq5LzkFF6pZijFFDjIK8pAotOIwKg4LdT0fZ+tybBjyb2deghtcEZIiJYGnnjQpRS2cdoLe1GXXpm0bIub3MmnoQfwMvOhFsJ+xC4expH1Xe/5YbvIap8/82wHazrs6ceuEcXYYrVo2IIOEYrUaQ5886DYbYfgRhdO4MVOiVA5XGUcekIznv+7nUHELZh4ciUWoHnNCTVyrY2LtrZJfrq2HFEmD+J25f95+AowlS8ktKhqRDUa3jSiC+GGBlx8KqnuugRTdlEIKFP9UBFx+lCkhTIRI+oo15JlyrlMgkMPkJVmGprlVWHKV8DBzuecYZF1dHRVXC0En4/fvC5F2Lg/LTEoTWh3Bg/sTR8Ody5b/Fk8beJnWo3NKZGIj3O+LrSJCrw6UCUvaLdkwn3ijLipnwx6VJn2gYbJBXsvgsoDCgF6d8C8TDj7YVwANKq3wuS+EAwwIwrOVpXo3AoynYhcchjB1OcU44v5hBT9B8ltomg+oHTA+Aw3TmTg7vsQAdohx2ELbWvwoirRoXWCFh2fAHNKNZCRxTPT8j0p9CAz4YOF/3u7ioq5R9mOoYpRc1oJBrCWWay3i4/NF2QF0I9Ud+Pa8ZrP9d+YZhffxiopRD2lR/p+rQwmx6bpWQTFFlaZfIcQp4EcVo1wrzjEu9pQRlh0XH0Y/fV6vlIV/bDTEUDkm2hoNVaZwOXC7V1JSvdIVLW4Lpi81Oj+Bv3c4InNxAnMWrhiz4N6nwyqjeu5ta/UeHehdMQ== 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.41.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 22:41:01 -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 4/6] RISC-V: Implement prctl call to set/get the memory consistency model Date: Fri, 9 Feb 2024 07:40:48 +0100 Message-ID: <20240209064050.2746540-5-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_224105_403638_B952F954 X-CRM114-Status: GOOD ( 24.64 ) 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 We can use the PR_{S,G}ET_MEMORY_CONSISTENCY_MODEL prctl calls to change the memory consistency model at run-time if we have Ssdtso. This patch registers RISCV_WMO and RISCV_TSO as valid arguments for these prctl calls and implements the glue code to switch between these. Signed-off-by: Christoph Müllner --- .../mm/dynamic-memory-consistency-model.rst | 12 +++- arch/riscv/include/asm/processor.h | 7 ++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/dtso.c | 67 +++++++++++++++++++ include/uapi/linux/prctl.h | 2 + 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/kernel/dtso.c diff --git a/Documentation/mm/dynamic-memory-consistency-model.rst b/Documentation/mm/dynamic-memory-consistency-model.rst index 1fce855a1fad..c8188c174e27 100644 --- a/Documentation/mm/dynamic-memory-consistency-model.rst +++ b/Documentation/mm/dynamic-memory-consistency-model.rst @@ -73,4 +73,14 @@ Supported memory consistency models This section defines the memory consistency models which are supported by the prctl interface. - +RISC-V +------ + +RISC-V uses RVWMO (RISC-V weak memory ordering) as default memory consistency +model. TSO (total store ordering) is another specified model and provides +additional ordering guarantees. Switching user-mode processes from RVWMO to TSO +is possible when the Ssdtso extension is available. + +* :c:macro:`PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO`: RISC-V weak memory ordering (default). + +* :c:macro:`PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO`: RISC-V total store ordering. diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index a8509cc31ab2..05e05fddc94d 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -184,6 +184,13 @@ extern int set_unalign_ctl(struct task_struct *tsk, unsigned int val); #define GET_UNALIGN_CTL(tsk, addr) get_unalign_ctl((tsk), (addr)) #define SET_UNALIGN_CTL(tsk, val) set_unalign_ctl((tsk), (val)) +#ifdef CONFIG_RISCV_ISA_SSDTSO +extern int dtso_set_memory_consistency_model(unsigned long arg); +extern int dtso_get_memory_consistency_model(void); +#define SET_MEMORY_CONSISTENCY_MODEL(arg) dtso_set_memory_consistency_model(arg) +#define GET_MEMORY_CONSISTENCY_MODEL() dtso_get_memory_consistency_model() +#endif /* CONIG_RISCV_ISA_SSDTSO */ + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index f71910718053..85f7291da498 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -65,6 +65,7 @@ obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o +obj-$(CONFIG_RISCV_ISA_SSDTSO) += dtso.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o diff --git a/arch/riscv/kernel/dtso.c b/arch/riscv/kernel/dtso.c new file mode 100644 index 000000000000..591d5f9de0f5 --- /dev/null +++ b/arch/riscv/kernel/dtso.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2024 Christoph Muellner + */ + +#include +#include +#include + +#include +#include + +#include + +int dtso_set_memory_consistency_model(unsigned long arg) +{ + int cpu; + unsigned long cur_model = get_memory_consistency_model(current); + unsigned long new_model; + + switch (arg) { + case PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO: + new_model = RISCV_MEMORY_CONSISTENCY_MODEL_WMO; + break; + case PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO: + new_model = RISCV_MEMORY_CONSISTENCY_MODEL_TSO; + break; + default: + return -EINVAL; + } + + /* No change requested. */ + if (cur_model == new_model) + return 0; + + /* Enabling TSO only works if DTSO is available. */ + if (new_model == PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO && !has_dtso()) + return -EINVAL; + + /* Switching TSO->WMO is not allowed. */ + if (new_model == RISCV_MEMORY_CONSISTENCY_MODEL_WMO) + return -EINVAL; + + /* Set the new model in the task struct. */ + set_memory_consitency_model(current, new_model); + + /* + * We need to reschedule all threads of the current process. + * Let's do this by rescheduling all CPUs. + * This is stricter than necessary, but since this call is + * not expected to happen frequently the impact is low. + */ + for_each_cpu(cpu, cpu_online_mask) + smp_send_reschedule(cpu); + + return 0; +} + +int dtso_get_memory_consistency_model(void) +{ + unsigned long cur_model = get_memory_consistency_model(current); + + if (cur_model == RISCV_MEMORY_CONSISTENCY_MODEL_TSO) + return PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO; + + return PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO; +} diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 579662731eaa..20264bdc3092 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -308,5 +308,7 @@ struct prctl_mm_map { #define PR_SET_MEMORY_CONSISTENCY_MODEL 71 #define PR_GET_MEMORY_CONSISTENCY_MODEL 72 +# define PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO 1 +# define PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO 2 #endif /* _LINUX_PRCTL_H */ From patchwork Fri Feb 9 06:40:49 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: 13550908 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 3D1F1C48297 for ; Fri, 9 Feb 2024 06:41:15 +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=0YTTg0j9lYC7H/f+ktdPF1Jue3kgb6FKbBB9Udzug/c=; b=oALM5EpBknTuLz qmYXs5uCA+XJAsHebWcP9xhCMetSZSROED9JPzP0XPzcWRtimpsxjkJDUzMlySy6sWN5Pe01hC72b Ejdctb9h282CVGkxhTbtW/Mbdlx0d5tcV8PoUnqA4+/8BGLcM8OYsXsp53bPhZNK3HUwc79h/lc7L 5pOcPF/5uG/umckCqGjEkY9nh272sC6pDbWU2w0bHReJcBKlcWBQySLSY5bLUPS/e65yPHb+EZwhQ Mz75Mu0/7MxsWQ0VotNwcDBEK1wZkBu4l3QJkPS9PEcSfxplnh18/JogJ3Ebi1/rK1CovBhbMnwWz QUQ0toZRG/VOH8FD6pdg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZT-0000000GFr9-21po; Fri, 09 Feb 2024 06:41:11 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZP-0000000GFlO-3wfF for linux-riscv@lists.infradead.org; Fri, 09 Feb 2024 06:41:09 +0000 Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a3864258438so110915366b.0 for ; Thu, 08 Feb 2024 22:41:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1707460864; x=1708065664; 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=bqv1kMDueOeDBLAFFzoizkbUOaos7eIuqxmLJzXo0DE=; b=N7e4vF7p8+i7V2gKJKTmM7MfFKKUfMFqSGXw70lW+QcidTOl0WXiuXWWBDd+Fru4/6 xMvdXkEs2YJXtYSi9xi6oYQ4ksX4T6KEa9UC0u+L3vXXWdsNL8KxIUnfIdHgR7bwr3vN 3CXAtuy+Fx2rEgVf9WFq/pqK0tGASqQPyIHfoH08PvTsaCBw6z2EXbokwFRGHZpnYF6N UFR1d1LQZ5LYKQiSPc0+zrKUUdPlv/RNZuyGQWrfumBfXnqPdkmenSF6WVTxg/XOQUY0 VAqiRlpkadzcP8nLVRtNFvjS/1bEyNycEzks4HJwg8l4Zu6qX6hi+5hy2TJBKfGReqyI qYXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707460864; x=1708065664; 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=bqv1kMDueOeDBLAFFzoizkbUOaos7eIuqxmLJzXo0DE=; b=PVVioyTbbjGVtM1hYImYSJy4Xu7R/aUspJZbp1MF7m2bbRpTbeJK+Z/6tsLZx+x6RM fjtoiYHmdgdy+EKuC5e5X130C3kFjKhc6B72hepto5tzQMAwBM1VtR6s+gzGtwZ0RGoS 4vdQQhq+Fyqy6KEP2F3vR6JTbo4t2+E65+Ceed/TppoxWj14/7c6PglOY+3WitqLjp28 VgPx2X0vsDnclhUD+Trvwco1N4vARpQa0d8YBd0wLpD3K6SaI6sHdtZl48fZDuz6DDvt 0TuqXapSJgXX3KPWDds9TQ03G0uHw3y7SS6NLVUExHvvXF6jnf8R4p5ytT/5Oth3Duxd kl1Q== X-Gm-Message-State: AOJu0YwCQUyqmULDktzuw/kuuojIuduUshrUS7iXU6W+3CRxv2uaTqQD B+GOUfG757CfEGWxrCzfWUX0aJiaKmi2YvpjwKZxatUS3SvHryA66ZWvKAZiPs2svfn8CQKmI0G zFv8= X-Google-Smtp-Source: AGHT+IEqYoT9P4yGxxV/Ao8vom6ObPUGe8xNfmYsrj9mQrupH69Vmjj5Fc9hQWs8Ax3BJgQO4kgnZg== X-Received: by 2002:a17:906:5913:b0:a3b:e92b:2a41 with SMTP id h19-20020a170906591300b00a3be92b2a41mr102530ejq.13.1707460864179; Thu, 08 Feb 2024 22:41:04 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVqA02Hms1GnUHhoCeUGdttK/loHEZjbx5QHKXwtPOxxttGYOksuXyK3muEb0xliNnRKF6szoFyOSFwsz9PcaJko6qKNjXTFB2fAnAyUpnMoVD+BQG/lDIsKGVkKuNnMy7wzKsP39NZoAmg06wjeoSHXyfFAN5cEbS/6EnYBTKMlzhMZrDk5pfPc0uIXGdiLw/6BaMIhcMFITP5pb19J+1RzF1urgzUtqlh9B3W/WQuGvzEkbSKup3S6Ti9kxG8XeGvyWsAft1kmMY1VJ6TJbTHHR1wRt2CPbV9ydGIIqh/nQ9xc08W/kJCzxUtTilHW2TunSFgIeJzQ3lp9ayfyWx2182k/ndXiSX8IaP8KBgE8qY5GSVV+bdIJ5yQSQN9sfvgp75UBDTSZI2KE/8h2zApaSfOvP1gqYu/l6w5sOHk5uXmxwNtsDKPg/gvNYpxPQv7s2Gux4RWuG/wgzzPenpKcbPz33H0ukQHd5hP61bQyFOi56bWkPzdKg5iSEHJwmX+hUwCtRf1aFZLCk8jDQBfrf6u7hMgQG4Fzd/mXKTkwTYwYGWEuSXv/GdHqagQcbufcOpMEPv8GUrFRZPXbb+AgJhrcf6yK9tzrRxobSk9dGB+n3QindAud8ArE7rKrG9g6aIUWMuYYYRNaST1o7FvBqxYWk3FP3yx7itHhzpZNOWGtzCqfkGtd8D6UmbZdt4aCSQKBYYs0yt3Y4BwVSmaxyAQ8wD7McQfbqmqIViPqNjTuTbrEnVlJxms4USHpISc/HAc/2iE4gnsIRzEnNdlnrvq5C/hROjT2FFt8cZ7PhawJvwaZWx3U3nXNWMQeSN47hTgcGZa24eiYOELFMolR5ikwa245mlzkn2LoKfrnA== 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.41.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 22:41:03 -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 5/6] RISC-V: Expose Ssdtso via hwprobe API Date: Fri, 9 Feb 2024 07:40:49 +0100 Message-ID: <20240209064050.2746540-6-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_224108_061545_D206AC9E X-CRM114-Status: GOOD ( 12.61 ) 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 This patch adds Ssdtso to the list of extensions which are announced to user-space using te hwprobe API. Signed-off-by: Christoph Müllner --- Documentation/arch/riscv/hwprobe.rst | 3 +++ arch/riscv/include/uapi/asm/hwprobe.h | 1 + arch/riscv/kernel/sys_hwprobe.c | 2 ++ 3 files changed, 6 insertions(+) diff --git a/Documentation/arch/riscv/hwprobe.rst b/Documentation/arch/riscv/hwprobe.rst index b2bcc9eed9aa..d881d56eba37 100644 --- a/Documentation/arch/riscv/hwprobe.rst +++ b/Documentation/arch/riscv/hwprobe.rst @@ -188,6 +188,9 @@ The following keys are defined: manual starting from commit 95cf1f9 ("Add changes requested by Ved during signoff") + * :c:macro:`RISCV_HWPROBE_EXT_SSDTSO`: The Ssdtso extension is supported, as + in version v1.0-draft3 of the corresponding extension. + * :c:macro:`RISCV_HWPROBE_KEY_CPUPERF_0`: A bitmask that contains performance information about the selected set of processors. diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 9f2a8e3ff204..ee6e830abe4d 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -59,6 +59,7 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33) #define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34) #define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35) +#define RISCV_HWPROBE_EXT_SSDTSO (1ULL << 36) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index a7c56b41efd2..9024061e9193 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -132,6 +132,8 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, EXT_KEY(ZFHMIN); EXT_KEY(ZFA); } + + EXT_KEY(SSDTSO); #undef EXT_KEY } From patchwork Fri Feb 9 06:40:50 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: 13550911 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 805EFC4829D for ; Fri, 9 Feb 2024 06:41:16 +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=41WbWy56qoz8AT9a2MkqQnpnibv2RKF+eKZr6gFp3ws=; b=ZxTkm3J6IOAjE7 3YIfYBFk7TqDEa1P6xzrr8RaJrs8sHwxlnZ2GseoXWGIhJa/TF+Cv34rjnlc2dfZ1P5321ryopWzk +wY2igDa+8WzLsTSvr8vudtdQ+QEcyh1tl+Xjd2YT6JN8W+CxkVKL8MpGi0WMlz5t0QmLPx22DjSa CtNv3FBH+tx/YNZfsBYKt7NcX44oYqzKBIgZICngtcqKiF1OzepWtE11CDBuLm2lKQthEn63Ix0pS u8Qu8HaEZAM3h2nKy0LX9OQ07B/ZYfxXPMwLP8uHes0qkINuXvmCo+EiVWE3Fcc4qMfkwe6icjQ3E c+ityXuKMjEWSmCMkPVQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZV-0000000GFsp-41I3; Fri, 09 Feb 2024 06:41:13 +0000 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rYKZR-0000000GFn0-3kFf for linux-riscv@lists.infradead.org; Fri, 09 Feb 2024 06:41:11 +0000 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a389a3b9601so76826466b.1 for ; Thu, 08 Feb 2024 22:41:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1707460866; x=1708065666; 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=L/ygjtZTVjHU12+LM3KMSNgL31cpnfsr1+pYQSNIAAg=; b=Shaq7AgjX4SwmTDQM1CubeVd+AfmEmPVhpcTTiPy0ChHOF9ZTRVIHuZVWeiXa7KeGp hs4EohrVXmquLv3Ud3R3S6I1evbkAGNpjSQyjpl5Zs522B6koB5eWRN2knvrfZSADykZ xBbNA9Bc/z53bOC5Z+QtHljDV0K9JEvdLP/oWJlHvX/o2czK2dVPpbmONxPimfv5v9ja 3ixBYpyEnoAOI1h8esOmq1I0eO6pXqABNH6cMtNLDyNO7ZlK4Dbq8A250XxH2f7cHUos KjT08pJzjE8IsL1E/HNs6bqNt9/ykatjAQ9/5zGmrDodEr0v9O1A8ET0k5mL1AA9JxWF wonQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707460866; x=1708065666; 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=L/ygjtZTVjHU12+LM3KMSNgL31cpnfsr1+pYQSNIAAg=; b=lorvI1r2chIie9FfYyb6P6bEF0YWExUFqcqhCXfe3Uxm79vTEWbbqK4rMaXYr7Jqrl KgDf0Xp687T8jkzvd5dplncAKmb83sSgnsR8tktQ0diqzSiY6QwSD4t2yIW44zk8QLuk VXvIvDBHvneD211mEz9Amd5407ZygJNViBdOBpOxZJD3/Hb0Dl9TJH6D/+415GjzkRBW KrbkVq7r6UX2GyFtya8udK8J0+M9GbaLHarj+Zad15Es7RCY0t39BDixgo9qiK3utcOv uvCahbwHVwRdWAzsnySW59eAksoJTOsW0qhfzn/rlR27uECeVKMpGNDaoyqHh/BU+40r fcQA== X-Gm-Message-State: AOJu0YzRtV4x0EusUF9K1CbpQqSvNlSAJquLSjPQkAYeQ0rWhe+DDk3c KoYkzqWdl6pJ8zR2ZoNO3m1Ew3P0C7EUzgQO6BT/Z/AyK1htVpq19HRGmIZhfz4ioCl9GYl+OqV u/ok= X-Google-Smtp-Source: AGHT+IEIwRiGBGnLEqNZprft3iYD6FyLivJVlJ5DvcYSMjlUi+u/u4Zmw+cdsozGJ8bDIbGbqO9viw== X-Received: by 2002:a17:907:363:b0:a3b:e115:7b69 with SMTP id rs3-20020a170907036300b00a3be1157b69mr404470ejb.69.1707460866164; Thu, 08 Feb 2024 22:41:06 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWVssZFqFbfzrmw5N+m8jAa5zM7KTVmwqDhtj5haZxxQzg6uOIC2be48nkwLf0iu0niHguJ551L7u5EteLMF9srBcTAPbhPmlhN9lnjkUU2tTUIeTzif6I3w0++uwCEmChFy3wD+RbH1zBIgET/4eJv8GvYZpGHrClhravWVN/9G12TzOT6pSxV2Hf5e3nv7flcEKay3n/VtUfCFZG3bPN9ruH01WA4pLxi6TRTHdY1GWH33+HGoG4MpOKRjax3mVebVc09T8VWGX+znRRJccUOB6llHqPneJV3KtYH+dT4o/DHN4NIJfia2h3/sHl4L3kSb/tSovFkIYLWSDk7SfUqW4Chj3MUJXUmE0sf9MQYAdtfAN6/1fja+8Igov9hgoncDu7N6JrTf1HelAqwly0GYbt9mqEuV8OdU8VwuMxP9M/G6jFA4wa/KHnI64loSqYdktUMjvLgZ7rx2qlfJXN6Zd3aOJyvy7H5eCPyX4h6MwZ4bSZg8NP1kggYVgQlsNU1N6PAwAccqf+KBuVmusiVRDgI+H0kqOURQEVwFR6RGGBHW+HSr7CQwAxFTrLx054movAmNnanQ2a79ApvJwsN3r/dgbUoFqDGx06ftfb/L8PW2DBt5m8p9uCyAVppafDCV6fPkCjNoRaVSmiVJ3sH76fTIAlBb876kIN/CSV8gNL/Ffh4rtqvRfEz6leUCCjBpy0vXgkAx4sCA7iptzDDd8TasbVhCvk8WJkPDXMHVafgfSo/lKt24X5klr1yI0qYMQawiJ5TkcuJOiLbzBMsWBu8vz6ANKnF7FSpPfnOyRduMkg26SjeQOmb1c/q857B+B6Y2Rw1lUEIifHLy+pGHEaQaYYnqCNjq+3DZmZd6g== 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.41.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 22:41:05 -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 6/6] RISC-V: selftests: Add DTSO tests Date: Fri, 9 Feb 2024 07:40:50 +0100 Message-ID: <20240209064050.2746540-7-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_224110_048832_47B5ECE6 X-CRM114-Status: GOOD ( 21.98 ) 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 This patch tests the dynamic memory consistency model prctl() behaviour on RISC-V. It does not depend on CONFIG_RISCV_ISA_SSDTSO or the availability of Ssdtso, but will test other aspects if these are not given. Signed-off-by: Christoph Müllner --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/dtso/.gitignore | 1 + tools/testing/selftests/riscv/dtso/Makefile | 11 +++ tools/testing/selftests/riscv/dtso/dtso.c | 82 +++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/dtso/.gitignore create mode 100644 tools/testing/selftests/riscv/dtso/Makefile create mode 100644 tools/testing/selftests/riscv/dtso/dtso.c diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 4a9ff515a3a0..1421c21841f9 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= hwprobe vector mm +RISCV_SUBTARGETS ?= dtso hwprobe vector mm else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/dtso/.gitignore b/tools/testing/selftests/riscv/dtso/.gitignore new file mode 100644 index 000000000000..217d01679115 --- /dev/null +++ b/tools/testing/selftests/riscv/dtso/.gitignore @@ -0,0 +1 @@ +dtso diff --git a/tools/testing/selftests/riscv/dtso/Makefile b/tools/testing/selftests/riscv/dtso/Makefile new file mode 100644 index 000000000000..a1ffbdd3da85 --- /dev/null +++ b/tools/testing/selftests/riscv/dtso/Makefile @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2023 VRULL + +CFLAGS += -I$(top_srcdir)/tools/include + +TEST_GEN_PROGS := dtso + +include ../../lib.mk + +$(OUTPUT)/dtso: dtso.c ../hwprobe/sys_hwprobe.S + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/dtso/dtso.c b/tools/testing/selftests/riscv/dtso/dtso.c new file mode 100644 index 000000000000..c8a7b25adefd --- /dev/null +++ b/tools/testing/selftests/riscv/dtso/dtso.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* dtso - used for functional tests of memory consistency model switching + * at run-time. + * + * Copyright (c) 2023 Christoph Muellner + */ + +#include +#include +#include + +#include "../hwprobe/hwprobe.h" +#include "../../kselftest_harness.h" + +/* + * We have the following cases: + * 1) DTSO support disabed in the kernel config: + * - Ssdtso is not detected + * - {G,S}ET_MEMORY_CONSISTENCY_MODEL fails with EINVAL + * 2) DTSO support enabled and Ssdtso not available: + * - Ssdtso is not detected + * - {G,S}ET_MEMORY_CONSISTENCY_MODEL works for WMO and fails for TSO with EINVAL: + * 3) DTSO support enabled and Ssdtso available + * - Ssdtso is detected + * - {G,S}ET_MEMORY_CONSISTENCY_MODEL works for WMO and TSO + */ + +TEST(dtso) +{ + struct riscv_hwprobe pair; + int ret; + bool ssdtso_configured; + bool ssdtso_available; + + ret = prctl(PR_GET_MEMORY_CONSISTENCY_MODEL); + if (ret < 0) { + ASSERT_EQ(errno, EINVAL); + ssdtso_configured = false; + } else { + ASSERT_TRUE(ret == PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO || + ret == PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO); + ssdtso_configured = true; + } + + pair.key = RISCV_HWPROBE_KEY_IMA_EXT_0; + ret = riscv_hwprobe(&pair, 1, 0, NULL, 0); + ASSERT_GE(ret, 0); + ASSERT_EQ(pair.key, RISCV_HWPROBE_KEY_IMA_EXT_0); + ssdtso_available = !!(pair.value & RISCV_HWPROBE_EXT_SSDTSO); + + if (ssdtso_configured) { + /* Read out current model. */ + ret = prctl(PR_GET_MEMORY_CONSISTENCY_MODEL); + ASSERT_TRUE(ret == PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO || + ret == PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO); + + if (ssdtso_available) { + /* Switch to TSO. */ + ret = prctl(PR_SET_MEMORY_CONSISTENCY_MODEL, + PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO); + ASSERT_EQ(ret, 0); + ret = prctl(PR_GET_MEMORY_CONSISTENCY_MODEL); + ASSERT_TRUE(ret == PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO); + + /* Try switching back to WMO (must fail). */ + ret = prctl(PR_SET_MEMORY_CONSISTENCY_MODEL, + PR_MEMORY_CONSISTENCY_MODEL_RISCV_WMO); + ASSERT_LT(ret, 0); + ret = prctl(PR_GET_MEMORY_CONSISTENCY_MODEL); + ASSERT_TRUE(ret == PR_MEMORY_CONSISTENCY_MODEL_RISCV_TSO); + } else { + /* Set the same model, that's currently active. */ + ret = prctl(PR_SET_MEMORY_CONSISTENCY_MODEL, ret); + ASSERT_EQ(ret, 0); + } + } else { + ASSERT_EQ(ssdtso_available, false); + ksft_test_result_skip("Ssdtso not configured\n"); + } +} + +TEST_HARNESS_MAIN