From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91C57CF042A for ; Tue, 8 Oct 2024 22:38:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 176E76B0096; Tue, 8 Oct 2024 18:38:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1252A6B0098; Tue, 8 Oct 2024 18:38:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE02F6B0099; Tue, 8 Oct 2024 18:38:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CC8436B0096 for ; Tue, 8 Oct 2024 18:38:01 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 162AB40C4D for ; Tue, 8 Oct 2024 22:38:00 +0000 (UTC) X-FDA: 82651899162.10.1DAE537 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf25.hostedemail.com (Postfix) with ESMTP id 9EE50A0004 for ; Tue, 8 Oct 2024 22:37:59 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="A/66Wa7l"; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728427053; a=rsa-sha256; cv=none; b=JZLHvJ4E+FKoGn6vF0feVQheI1cKBov9vaJMVnkAQD8Uv/d2hZbkkk7Zj9ghiXV1T9O2Mt BLK3SVrkB01PuLsa+jnqcLU6LwDFpRj++4VVJqNkKNH9E0B/8sarcIy0lHREyAhtstW62R qEs7mLmcU8L2vAPwCOE32YzOj4FvAZk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="A/66Wa7l"; dmarc=none; spf=pass (imf25.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728427053; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jmcYgz7XnLLJto60RpG9fxf/3nbG0BDRV+8az326H18=; b=YhZqeXpK5IQbygPaGaEs5pkv7c2u0hlO4WeJVzPCvEV+plMazVu2Yvp8YEcla3AY3BB8qY NIZ3+XzdpmH0htwHQkZjSF4wEPEzi8i4c9SO9n5B/UgMGRXLcD4bm1M6iON5DGIj3DgNzA Z4lV8cRieXK9j38d+4R4D+5eTPOUkDI= Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71e0c3e85c5so1751395b3a.2 for ; Tue, 08 Oct 2024 15:37:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1728427078; x=1729031878; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=jmcYgz7XnLLJto60RpG9fxf/3nbG0BDRV+8az326H18=; b=A/66Wa7lmXkfBvN0HB124EyRr7ccdKMYaN+7JMfP5P8fEfnQK26RJ2x9R4jX9Uc0MP QWzE2BuD1BXibp1vio3t+aP6oelOySat5jJFkbuZnZ3h37lNekN2UWjJ8g2vaTYT7Nq0 QgudR0ZZ9zDgf8sy0Vn3OXMIIpHDPVUT6e8biESOKCdj9ZiPTBDPiKZM5cn8VIJ1eYgy fU4af8erGjmGlCPFLkaAZv0P2n/jx+7oP8iUGOJSepuRIxodGod56m7yAjt78zhyEfe6 yXFb7itPs1bsTchSKHsPCgU+ARZTD7Bu12FfKAHy0+SKvf7+ccJmvyhaqAeGNa1m7pbg oGyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728427078; x=1729031878; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jmcYgz7XnLLJto60RpG9fxf/3nbG0BDRV+8az326H18=; b=ibuum/boXidCv4RzhZ4bzNtKY+36uyAcP0j/qrJ4bZimNX6p3y5H7AmM28Xzvz2txo tCzkBXJ94PkWC+eCwV2ZQjzk2OpC8DBN5Q+pSn2tokE2d4cZAiYzAwlZA5rMdcSM/Go6 DzR7jihkFFQYJFJtBuQw/5DXfB2sJ3MOyr7Xe+GrGAyn3jQ45dEdBSis13ZzVXK+//Ev FMaQpFm89kSlzYa1XKGkI4LbzDUc1NVunhuoEVtbOr0AQ5QNt5TVKZIeY03sGu77OwaC xFwnq+ktf81YnSR3412ic3ilO9VO6UjNJKjsCFZJIEQvExSB+TAoqZW5o6lryQPcWnIk AjeQ== X-Forwarded-Encrypted: i=1; AJvYcCU1CPp4dO1oz+rxgmJRTqJLAv6SqoHLQrmCCqxPRw71fdBNf5PXEy3+8z5HnxEaHJEkw/Kcq/cnyg==@kvack.org X-Gm-Message-State: AOJu0YzcaJNpyzKNAhKHy7ZeBDG2owgk9niS0jG9pswnQEW9sOXo8TPc aLcER83hBvyOQ9nhcN/XpeWY71dwTzEyyAWBn+Xm7MVrs6lF2MCTnd2f6flZw7I= X-Google-Smtp-Source: AGHT+IErKskUpIBiFKlwdYnNS9we5iBG5psWxaDz48JOAw5L1P7wN1gzBxCeW201xTOm4LwBlfF9lQ== X-Received: by 2002:a05:6a00:816:b0:71e:19d0:2988 with SMTP id d2e1a72fcca58-71e1db85912mr671554b3a.14.1728427078379; Tue, 08 Oct 2024 15:37:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccc4b2sm6591270b3a.45.2024.10.08.15.37.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 15:37:58 -0700 (PDT) From: Deepak Gupta Date: Tue, 08 Oct 2024 15:36:46 -0700 Subject: [PATCH v6 04/33] riscv: Add support for per-thread envcfg CSR values MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20241008-v5_user_cfi_series-v6-4-60d9fe073f37@rivosinc.com> References: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> In-Reply-To: <20241008-v5_user_cfi_series-v6-0-60d9fe073f37@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta , Samuel Holland , Andrew Jones X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: h3ibn9im8cjuuw5usttrkutb75uy4qtp X-Rspamd-Queue-Id: 9EE50A0004 X-Rspamd-Server: rspam02 X-HE-Tag: 1728427079-440007 X-HE-Meta: U2FsdGVkX1+jHo5G95qb37+ASlaKk1lF2KfhQHTRcMI+SQiofBcsIgr1T5ob1dPYVuSpEJpgI2mOh8L8rfCKtF6OPUAnBUk7zgoW6LHt5mL5kd1L69FU2ko01bL4C0A9iuKqCEPEya/QWpSDoreg0w3z0tZp239raXHMBRYkZ2Ouof3t5kKNAib7ej0L7LBpdvz+emV27hjIno2QzwtZ/EvfkL27Y1o41P6Vpj07WfBQYrudw4tHOe7xMeHYzePZicG7NNKiwVOIlGD4IIyxo2p6ksiKuwSGrxiZmijIPf7rL0bUsCC1xLl4J0trqaP9pFkUGhDevqu51nEuQLTPfhPPOYrJBJsxJ/JNo7LTf1wijPi2fhIcPfW6FNfqAdIlRA1aSz5R2dB6Rt3Fg5YQpFxNfhz16/Gs/pPmhPWSTcJAbO+EFHZF1t4qRj3f5q05E+fKMRLsbpNvAuXbrRNCaWpz+3rc2ZwTwmkvieLLvRGVRQUbbiT10e7thYcULgIm49d4h4ctCC5mv7lOsiGiYtXD1BxGFCNKSRFk3GuU78INYc/brLE8tpWMeAextM3D6Mzd3aHSStYPg5jVx0niTWecmiJ3mbmbUagMwNqf/UQSvRyUx5ooejTRI9tayJhJsbQaRBHgZA/OUv5UCf6fxZheXwbpeMF4hkGcgEShNHIErMnA0oqU6bc2EQ871ucGulh67p3cX2aHrvWDal44HAx4fs9Ukj0M4NGg6VPf1bHSVRlW0a3YZPyBPwHmhxNPi8dupo21KGDrYGga0Aw6DGLFAu8SyU3yzCAkZDsGJkPtmd9Vs+I72MGZ7obnGeokhx5Cur4upziqTdfZM9zJAvKgFdESLaeLrQqm63JYiCD49HvK3Nyh2KKcJHfR4EimyTHv15nkkREy/xnKaXIG93i+fB/8lh55kGg4Kbr3wohaP6CSorDkq4oJyCId2frVfCxgWe4Eou6bOhJO5j2 wNXKfdt6 YhRs87ClVaFrE47J7nDdrXU9FMpdznks1tQfaumep2VZb0E3m4OKTP4l/De9/Y0SQb8DHRxwxRfJ91b40CE6rfgPv7PBYgNP6k3piAJja4FJz17phLeAOH8CG6j+24Fm5v1OnhTjK1fjz5ZKEHHdDkqDc0m51yFxrL3FhHuklAngpY0n2YBypFgpdBI66m+AEKSAUR9qa1GLM0hylmbCtMxzbDC3ZKvXR6waK1g8+aJkn4scRFNCuHjEAKkQ8p8SnOQeU/dEriXVdvy1tossADK45piAAoc8luePaqv8ss5Ovl/RFxRs3LFxln8tC3HpKMAvDFyXpppv8WgKCzCtENvfAaLNVGPnl4fkJtXZmNglBEQQQsGTAMA7FRKg1nC+bZZCh7AgG2ngJGi4aNB0Sid0uyb4P4aC972TOxv7CirdMCMprFaEy9lxPnMG399w+dUD17/mNlVOVIGqy3SkMQQ0Yo5Baq8n35iKbDI3KZP3TYdqWlTn5f/CDOiA8nUf7saWjj8oRAu7n5P9o4pCNQczy2+GrSZOFAH3k9mGhr/9QB4Dw7sbeuzjMWW2Rq4A3bOw4W6RZ7NZVIus= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Samuel Holland Some bits in the [ms]envcfg CSR, such as the CFI state and pointer masking mode, need to be controlled on a per-thread basis. Support this by keeping a copy of the CSR value in struct thread_struct and writing it during context switches. It is safe to discard the old CSR value during the context switch because the CSR is modified only by software, so the CSR will remain in sync with the copy in thread_struct. Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to minimize branchiness in the context switching code. Since thread_struct is copied during fork(), setting the value for the init task sets the default value for all other threads. Reviewed-by: Andrew Jones Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 8 ++++++++ arch/riscv/kernel/cpufeature.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index efa1b3519b23..c1a492508835 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -102,6 +102,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + unsigned long envcfg; u32 riscv_v_flags; u32 vstate_ctrl; struct __riscv_v_ext_state vstate; diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7594df37cc9f..9685cd85e57c 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +static inline void __switch_to_envcfg(struct task_struct *next) +{ + asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", + 0, RISCV_ISA_EXT_XLINUXENVCFG, 1) + :: "r" (next->thread.envcfg) : "memory"); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -103,6 +110,7 @@ do { \ __switch_to_vector(__prev, __next); \ if (switch_to_should_flush_icache(__next)) \ local_flush_icache_all(); \ + __switch_to_envcfg(__next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index e560a253e99b..27bafc5dd62d 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -923,7 +923,7 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) - csr_set(CSR_ENVCFG, ENVCFG_CBZE); + current->thread.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } -- 2.45.0