From patchwork Thu Sep 12 23:16:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802827 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 831DBEEE270 for ; Thu, 12 Sep 2024 23:17:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 183AB6B0095; Thu, 12 Sep 2024 19:17:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 133C06B0099; Thu, 12 Sep 2024 19:17:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E51886B0098; Thu, 12 Sep 2024 19:17:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C774C6B0095 for ; Thu, 12 Sep 2024 19:17:26 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7E1461609B3 for ; Thu, 12 Sep 2024 23:17:26 +0000 (UTC) X-FDA: 82557649692.22.63A3236 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf29.hostedemail.com (Postfix) with ESMTP id A1583120008 for ; Thu, 12 Sep 2024 23:17:24 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ehlw5uF/"; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182966; a=rsa-sha256; cv=none; b=fjvGRYP1qBM4VV8ACVGPMUo328YJ1a8HwJbGJoUgms+7zQgRugXRorN2n2ikmCqRoq1HTt zzSQJYj5eJy9b2ShulmM2xRxYErQa1laSvIev5ZrrJDnxgFkUdLLPeOQwzVt6RFtsACEVU v51Sw+FBAo0FmXqZgmKojnH0Iua2Msw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ehlw5uF/"; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 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=1726182966; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zDmvOTSrnPg54HHGP9mWRdaXRn5Dr4fSLwQGe7BW+Sc=; b=3Tj7sm7LO76byhVxQfHB9CrJC82tOL7DL6dIkQ2Yk2f3kMNNnsDZTNKGCelirQbJqQ5mAr uuoA26lOP5L0h2TsBrAud0XdLTGC2WYWUw/GBLp+TSJswE1Zfrb4SRbO/1CJ+2nhkYX0Za 9z3oLIh9G2LiGZk140y8qGIv1SSLTtE= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-206bd1c6ccdso2787285ad.3 for ; Thu, 12 Sep 2024 16:17:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183043; x=1726787843; darn=kvack.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=zDmvOTSrnPg54HHGP9mWRdaXRn5Dr4fSLwQGe7BW+Sc=; b=ehlw5uF/b4pP/7ojO5+O7hbyp10NCYDNUd72oiBqEqhCmMUr4Jw+YL50za4Ucg679w RoNk3BmDK/N1uhrK14kHi2jHkgfjUoDrkrGXvIXpHiivE5BBzwE1K3+2m4f61eUdecEA Bq6HCIG0/9oTjgKkePjmF4a7heF14eYbH8SfiRBa7+QXuA8bDUGBDUYBfhKKXu1w/Zpm cZGIra+YVuDVTAMwJxQ7QIv6T7warpBDySZTQhOvySPRgKyKszKQGWNr2DiuNZr4VtEd LvGSbObkb1bspjlzSlEU+K7i1hZGGPm3FwZBrcxu0FA272VwdEJPbLQ7jczp2nURpOEE 98eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183043; x=1726787843; 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=zDmvOTSrnPg54HHGP9mWRdaXRn5Dr4fSLwQGe7BW+Sc=; b=irzhqOmnEz288yCngwI4WbOkpodWz2s+UrF0Yf5cduD9e9xF70G35hhHu+pK0GAB32 ObOVhIozkbLYaUVWFWPHmJOMsEQM3NUZ8/E//LlORWRRA3vKQmGJ6xM32E05AkfI6NUw re59RX7PzyY5H4x9NgaVHmz34ePOo4YcU4gYG6BZ94joHiIslWIAxs9CpWAH6BUvSx6B b7gPuFBNowReIvbQBzdEwk4MbxZytCAuJzAuuAeVONoO/uCUXOYZTKv73xEOEnn12rDj Unw9Xr+Yx0+Q7KKO5gIylW6PMLJnr++xhqsfyVbeCAWAHi9gwwnw8KLWXZkRZvNVXqEG xckg== X-Forwarded-Encrypted: i=1; AJvYcCU9t+6fC9IE+F2vXhabSEubLFqo4wtoB3OUV9X2K/4gv09Au02uEaMTye4/tBbdxBEMEl5pCdfo2g==@kvack.org X-Gm-Message-State: AOJu0YxXY/PQimPTW4hAb+WFP9OAP6+mshHQKw4rFAv2E0N9qIAbqA8z ct0aYYX+Z+8EufIGaLTlqux4hm0U4YGYCKNwYMdSHF+n455v+U5bsaAVwJT9mLk= X-Google-Smtp-Source: AGHT+IFCbH4CWj3cRlGP/tTcFYJu5lqNBzSaDboWQslgR+Qb4odzPou2d9TOn5WUDlzlxhbuOJA+kw== X-Received: by 2002:a17:90a:cf0f:b0:2d8:8ce3:1e9d with SMTP id 98e67ed59e1d1-2dbb9dbda04mr929613a91.3.1726183043273; Thu, 12 Sep 2024 16:17:23 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:22 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 04/30] riscv: Add support for per-thread envcfg CSR values Date: Thu, 12 Sep 2024 16:16:23 -0700 Message-ID: <20240912231650.3740732-5-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A1583120008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: pnzrohutoyp3uz1hj5xn6ekd8nwfy3fe X-HE-Tag: 1726183044-713602 X-HE-Meta: U2FsdGVkX1/dCBwk5NLFbgjpqzEQIlsya8AT6/CDcNiHTBJ9bpJSmwPckCX4QBJNoxvMUtVSuLhdDgLWr7TUamjHS9Zht5pT5o3H/hHusGYOOoxFEzn7pFEkgexco2KRo92ocvRp7asLlM7ksP6oBiaJRnfv2t1e2GoSsfnZ47asSU4FyoxdjHUPZfh6t6vVrzq35PpyVtCWumZ7HWNfK3wbSTkHXw+nS6LpX9KVooscKUfVsgPAFNEnlp1xQKmRpNyvmaVqlS1KEn1b726P1YoI10Z+aEFMcZG9TNKpUHO5HnoWsHth5n/wkmCps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rQd9vV243kj/CVuzzgv9/yIXvlhjyDlZiTr7T5k7IUS8+5lkh6940ojgELkpaFJKVEYfpouw46DV4CuCRWEfDyK9WG3lynbjPd0qewU+FiYw+zZFeAxiwpkIQ5tiUTgf0ZzNKn 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 Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/switch_to.h | 8 ++++++++ arch/riscv/include/asm/thread_info.h | 1 + arch/riscv/kernel/cpufeature.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7594df37cc9f..dd4a36ff4356 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_info.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/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index fca5c6be2b81..c74536194626 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -57,6 +57,7 @@ struct thread_info { long user_sp; /* User stack pointer */ int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ + unsigned long envcfg; #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 0139d4ea8426..f7fcd23d55de 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -922,7 +922,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_info.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); }