From patchwork Tue Oct 29 23:44:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855720 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 47120D74950 for ; Tue, 29 Oct 2024 23:44:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF48B6B0098; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA3436B0099; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D14A6B009A; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6E2416B0098 for ; Tue, 29 Oct 2024 19:44:29 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 27AE8A07F2 for ; Tue, 29 Oct 2024 23:44:29 +0000 (UTC) X-FDA: 82728270660.26.D1F89AE Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf05.hostedemail.com (Postfix) with ESMTP id BBC2E100006 for ; Tue, 29 Oct 2024 23:43:39 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ycBrjs/a"; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730245412; 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=CCKzg74NjcYHO/B30bowh1owAHwLHbAhOOOeEQpeDk4=; b=yOR0rcMXILG6a5jpNz/zvD7M8yTHc/rQUpvpeFfwoVdVe/t9E8x1RnRZXYBHeJMU7yApQ1 xoQPxF/GzeSAfhxEAaawT16aT4cDZ5sBCNrOdlm5sYuGSWN9pN+Y91rnnIj8kdy6o2Ens2 dhcws5GbKxKZihn0I7Qyp0SxZgHWYuM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ycBrjs/a"; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245412; a=rsa-sha256; cv=none; b=TjcictaoYVrzyDPD2+gxKwWKwm7idw9G2ORjJWCe7ny/aR3g/oHC5BRTK5bCIpPLrbGClY cnXJglfX2S0UdgZKZonI1eYK55sUZfjyOhXwpY3/25bFogtOAZNxRVuMcEH7TjDFVKocTq WU9q2OPQyQQ6Uy+Q2oFvCNiI+GfQBHU= Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7ea7ad1e01fso279378a12.0 for ; Tue, 29 Oct 2024 16:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245466; x=1730850266; 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=CCKzg74NjcYHO/B30bowh1owAHwLHbAhOOOeEQpeDk4=; b=ycBrjs/auYUwCfIJBy7WzB+91VUuwFTmcyzzR72oX+ubgd4SIs8JHGdP3YWvb//f2W S9tgIwqNRnZO6hA+sXCiI9ka1shsJkoh1OPrr4KxnOUg7IVvrzp61FXrG41AjBjCoj00 A5bwWlv0kDkVfIwKbiYTVwczolw5bq8thOSuNwbk/Bd+PGCiyNw33Ijd98Me0MOPgBr0 tI+bAXPPB/jO4XCSootXJGf/bqml8XUY6IEbsKVIB7RQfYBRf/NwRFTbjJzj0WqTMGDg RkLQ53M7TZ3vEYTOgTVyvbnqF24lr+8qxM7ZHakhdEwDvuj+0APOf1Dv5ayOTDVuCJor 4DGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245466; x=1730850266; 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=CCKzg74NjcYHO/B30bowh1owAHwLHbAhOOOeEQpeDk4=; b=giSR6ijMUCHA+yF/1EHWwJJgB7qj0ovVlTm8c2gkiVO4OrM9O2hZrY7D/Pbg26oYkP /uS0ohW+lU05dXEZF9cdhIyPcZKeBYu0dDl/9qYKyn77/55wXDSd8dfDursPyRyUK/Yh 5xNAYrgUS8Ix0WpsBwdkVdulHrJIrJq/v17iNR6srJbsToE1fiYgWaK0A+lLBDgkmb/b 8UHma7HaNmrTP7oy0DqeQriDSgdnewrCz6PBCO0VwptNYiiKsXCfCFb24y1QQCE7RvvY N2YVgE8sCsFEkQR+Hf163skPOo2vaA8AsQLgJXYhsE5G72GDdBZkt7nSIKG93Ua/yFr/ ZdGw== X-Forwarded-Encrypted: i=1; AJvYcCWEiCu2cqQt0n6JU3vZkVxwewaxFd0RGtwM8uDcyqa/MLBaKEg3IV/YPDKHHuM6UeO4nEoXf/tzkA==@kvack.org X-Gm-Message-State: AOJu0Yx8Xt6uiRaYkM2KEbvDjOowVWtgzl7Ls9aRT5FmlvzvnbU/7yYT FQo3kr8Ii/wt4uFxS30PSI/AexxxsECfsejEdTibrkoOzepaJY/eMfbS1vVl++8= X-Google-Smtp-Source: AGHT+IHJh+1ZXsYwxslZQeQFaVaIosIBJ9byAeINs1Cby21LH+tx3YEEJESgsOz7GEIKZ2P3AAXg4A== X-Received: by 2002:a05:6a21:99a0:b0:1d4:f7b7:f20 with SMTP id adf61e73a8af0-1db7ff5e5e7mr749841637.21.1730245465983; Tue, 29 Oct 2024 16:44:25 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.44.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:44:25 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:04 -0700 Subject: [PATCH v7 04/32] riscv: Add support for per-thread envcfg CSR values MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-4-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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, Samuel Holland , Andrew Jones , Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Stat-Signature: cyr731yq4fuxt1xqmiqwdoith9bq5fi6 X-Rspamd-Queue-Id: BBC2E100006 X-Rspamd-Server: rspam11 X-HE-Tag: 1730245419-16177 X-HE-Meta: U2FsdGVkX1/0ZBpWCP1AE3ozN1Nkln4dr4AJqTbUtczgXmfsSW/fCcDxtS74hqualg69G6BRo9qYVY7g690GQQ7koaNNR4qj/5ScJHN5PWb5nmrDkE8nizMZdvJ8ZysgWGC7of2C7y4uE6UsYD9hBvJoT5w6zhJBjpevvOxQTAq5hFA0ffnMv0tnyX0BV/todDliJsyH9d/bbGJvsuWZ+apoTi8VyryzhnIeDRqnmI7npDo948etb15vojmoeNqn2ddlIGeRMKXG3uNQJMf+IxlrcbXHFsp38mR/QHS9YqeoqYXprdv0KLkje28AsQoxp+Vhedd7tppq2lGG+n3FkwvL/01KGw2uBal0ZYE+NN+h1T6spDJRmtmlcxx/zGEDuKIHnTDKAL2iWYuSaWe2GwIEmpi5oBSbWQ2IfSajKHyDyTpX3smxddt5TSIHrg2vesEdgREgUauY8/ztvywdlYjnegCaeR944h4eb+L4vtwTFbaTvpuZANTud+ST468RK4jNA+58hA/O6zr7o8ALloPqDBCRzjEENPyZQv+xz5GsX4dm2ctILerDc6f5NIpaPn1tKRYx/rL4pR/oqvEkW71NZMrqrBl2T8aZK1m3K+8r9DguiA4by1gC2VfAfesd6UpFAozh8teIZdqEu475MCVDulZ6Ox04f70zAn42zIAecCUdW55ZzPk9sDRFqjyD16h490iuMOD2pl6ocgyrCcGG6k3PfwgY4IlgDaJdd9nbLgV+1bpKtgDjrWVhPlBO9FA8E5hlnCpqYehJ/k4c+tTa0mUJWwUOPo2A2YZLk66JMZlYm1ptRAYne2guH+4EG/nIyde3Ha314i7j6bbKGlry+tHCmIquVtK3pAEItY+zwBzj8gx+how2IW7QWMjYugRnTo0HOqLxKVckdLNEwaGPWy+563hQHo9N1RQy7MB+EfVkMly3Jh9asSvaHM3N0j3rz/YlDVPpKxfQQvq 7/fnPHEK JDmpzX/CtujQUAiWF+Rn0zBoYssfppb23tgd/HViIGpvePaJqODAKpvo4ItVsYYK92a0LWvevqFEvTXGiXLRVVsFQQg6xNOeTUMlwD47bXwfofQe21uJS0Ndn7oXvaA0F9MTp54SMBxf+39jZG+kuLtKouTCXHBpxtHSVytGAdCr1gmh5TyUe6xD10u7gFJvHx6kN1rX2mAieJyZdfVl5Z0j/q1JzGOre8Wa6AM68qL291L9G9bARV0b53yEXQHKw+DrF6DLOKtHyxB84lfCjlt+C71V8NFNw4VYlmE4KMtrow21OQnTc/svEP6R41os2GGXsyWfdKMaCYfNlLJi2fQz67C3Jb8R3+DW6wMMHddpeCWUIhKjkCqcFbuy7gVv+dQR020fKOBdlXiU0Zu2mJ0F+TRlrxhH5uNj1ctnEOTUupsUeSHHu62nAQJ8J5ifQfRtj11GAyFUYaJph2GhB2x+THSSR5tB7PbnoRlsjpn8vh5NL8sScShrcB6OYasCw7iK78OE0RfA3YsVvHhz3H1yXnVbgHYV5MhuHoCsKeaOVd3EKy528ZNMFueXOrsIKubX97C8yaQSArZo= 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"); }