From nobody Mon Feb 9 17:58:21 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6618143C6C for ; Wed, 14 Aug 2024 08:14:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723623288; cv=none; b=YP337lnC8i1wWCjzVwT806psp0YxHwicdBeC26/hHLTNrS9fi8OOvH/2Imrfc+G5MwvVWO2FoESlgPVuBJfguBmsKhrR9fUHx2hA5pEwRCE+uSv+jhen6QzyEwBZYn2lj1TZ/gVAqSZaoa6G115oyXFD+EPT3DDZpvmKKBM4Lqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723623288; c=relaxed/simple; bh=vmkWZtpE1edsNLQZpDe8w0yMp8CPRngRdCPf5sCMI5s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r8dUK+5CwnrKrwpnYbenSpJ71k4F2iZOrtogf7SaAiAjuM4/nMV0sKrUBK71Y5gH+z80kG2bYG+w85r2/rP79DfbPNTma0FLP4OLPVIUcmleQCoa2iYcleZikFLRcA65EO3pOl8XgpGqPGNkNnEn2/ZOT1Oa23dbyXPDOe56bEs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=IVXgA+Ek; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="IVXgA+Ek" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1fd9e70b592so49419355ad.3 for ; Wed, 14 Aug 2024 01:14:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1723623286; x=1724228086; darn=vger.kernel.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=FGFNLFqSjlWe1rFESyvT8Bv10SEVbMi3uLZQLxiHpKM=; b=IVXgA+EkFUOEut8zi5SQ5wsIHZVbgUBL5qhqHR+Ki0YLXObgP8dOIg8cwIY+2nqKdN uRnuRDT/1sjvcmwUQ1XWFke9r2ZJLgAIRHEqaFnlUxBeQdv3UJk+dwPw4m2KOBoihhrv 0n0T0LQqoGlV7rFzv0hmrUJZbCg5MeBB23b8h3zJdNd3LcT/m8JBcN6oHnXXoYRplmJN bNXCuYYu+r9n65I6cFnPzqJW/MVrfy3sPvL24sVStPLugeHTM5o6JqESqEwFjaUe9/Oi Sv/MaER2TvSZBf8YI61XVRSVoBKHvMWSonTFz7OzDDiLzBxRj4Oogd/jT8L1m2vaWglv LX6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723623286; x=1724228086; 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=FGFNLFqSjlWe1rFESyvT8Bv10SEVbMi3uLZQLxiHpKM=; b=mr6l1W7sAx/vjGqOSRzJ8riC0ZYgXmyq4bM4J9Dm9Q9xhCaiOvZCQyL00yxHUS+V2N suWbpDbXxKNz1b3Hel4KptoVvOSqU0xgq0HY65YcWUXzMjNzGHE16bTjQArlNWn537ni 3bggxHmf+w6QsRKc6jcfYPfMfLydETEvu9Q43dbmmRKd2XFTdeDUzjVArxlexUkYOJa8 T9wSFmiMljZUSG8g62+9JRr5X38WADTW0dHC49FeRxYGLY2mlzBIkCIml4qNl0XUr/5l fH3JnL4qp4RnrXtU9lc8N6aPwAffe5XS13UAwidR2zud/0o9tjc/L9yp0vY0IK2uJs1o hkig== X-Forwarded-Encrypted: i=1; AJvYcCXfsbhuH1QCADZZ1TYs+oPwoOlfA0sqLgjUdPhlVhl9k+hlJHahlmWg4MeGoG42zgMvmh+Nl+X3wKdq3f8=@vger.kernel.org X-Gm-Message-State: AOJu0YzlsJ2KAGyANZZdxOKMdCUiUm5G4KGJM5Ty/IoAxvWQeGKCTsj2 qcJhI1DEmr/y/WlkITmeU8S4d2HJ7edZXLgYLXMI59Jc3g/LxjAFcRnlRHKXQL4= X-Google-Smtp-Source: AGHT+IEuCRpXnMypEEg9Ag8jZzl2QUywU8kyMp8G6oY+9ojSV+wDf0uO/E2kB9x+MF9du1eUQrd4eg== X-Received: by 2002:a17:902:e5ce:b0:1fd:a769:fcaf with SMTP id d9443c01a7336-201d6592fcdmr22165255ad.61.1723623285971; Wed, 14 Aug 2024 01:14:45 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201cd147ec4sm24868335ad.85.2024.08.14.01.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 01:14:45 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v3 04/10] riscv: Add support for userspace pointer masking Date: Wed, 14 Aug 2024 01:13:31 -0700 Message-ID: <20240814081437.956855-5-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240814081437.956855-1-samuel.holland@sifive.com> References: <20240814081437.956855-1-samuel.holland@sifive.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" RISC-V supports pointer masking with a variable number of tag bits (which is called "PMLEN" in the specification) and which is configured at the next higher privilege level. Wire up the PR_SET_TAGGED_ADDR_CTRL and PR_GET_TAGGED_ADDR_CTRL prctls so userspace can request a lower bound on the number of tag bits and determine the actual number of tag bits. As with arm64's PR_TAGGED_ADDR_ENABLE, the pointer masking configuration is thread-scoped, inherited on clone() and fork() and cleared on execve(). Signed-off-by: Samuel Holland --- Changes in v3: - Rename CONFIG_RISCV_ISA_POINTER_MASKING to CONFIG_RISCV_ISA_SUPM, since it only controls the userspace part of pointer masking - Use IS_ENABLED instead of #ifdef when possible - Use an enum for the supported PMLEN values - Simplify the logic in set_tagged_addr_ctrl() Changes in v2: - Rebase on riscv/linux.git for-next - Add and use the envcfg_update_bits() helper function - Inline flush_tagged_addr_state() arch/riscv/Kconfig | 11 ++++ arch/riscv/include/asm/processor.h | 8 +++ arch/riscv/include/asm/switch_to.h | 11 ++++ arch/riscv/kernel/process.c | 90 ++++++++++++++++++++++++++++++ include/uapi/linux/prctl.h | 3 + 5 files changed, 123 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 0f3cd7c3a436..817437157138 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -512,6 +512,17 @@ config RISCV_ISA_C =20 If you don't know what to do here, say Y. =20 +config RISCV_ISA_SUPM + bool "Supm extension for userspace pointer masking" + depends on 64BIT + default y + help + Add support for pointer masking in userspace (Supm) when the + underlying hardware extension (Smnpm or Ssnpm) is detected at boot. + + If this option is disabled, userspace will be unable to use + the prctl(PR_{SET,GET}_TAGGED_ADDR_CTRL) API. + config RISCV_ISA_SVNAPOT bool "Svnapot extension support for supervisor mode NAPOT pages" depends on 64BIT && MMU diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/pr= ocessor.h index 586e4ab701c4..5c4d4fb97314 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -200,6 +200,14 @@ extern int set_unalign_ctl(struct task_struct *tsk, un= signed int val); #define RISCV_SET_ICACHE_FLUSH_CTX(arg1, arg2) riscv_set_icache_flush_ctx(= arg1, arg2) extern int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long per= _thread); =20 +#ifdef CONFIG_RISCV_ISA_SUPM +/* PR_{SET,GET}_TAGGED_ADDR_CTRL prctl */ +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg); +long get_tagged_addr_ctrl(struct task_struct *task); +#define SET_TAGGED_ADDR_CTRL(arg) set_tagged_addr_ctrl(current, arg) +#define GET_TAGGED_ADDR_CTRL() get_tagged_addr_ctrl(current) +#endif + #endif /* __ASSEMBLY__ */ =20 #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/sw= itch_to.h index 9685cd85e57c..94e33216b2d9 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -70,6 +70,17 @@ static __always_inline bool has_fpu(void) { return false= ; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif =20 +static inline void envcfg_update_bits(struct task_struct *task, + unsigned long mask, unsigned long val) +{ + unsigned long envcfg; + + envcfg =3D (task->thread.envcfg & ~mask) | val; + task->thread.envcfg =3D envcfg; + if (task =3D=3D current) + csr_write(CSR_ENVCFG, envcfg); +} + static inline void __switch_to_envcfg(struct task_struct *next) { asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e4bc61c4e58a..1280a7c4a412 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -7,6 +7,7 @@ * Copyright (C) 2017 SiFive */ =20 +#include #include #include #include @@ -171,6 +172,9 @@ void flush_thread(void) memset(¤t->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); clear_tsk_thread_flag(current, TIF_RISCV_V_DEFER_RESTORE); #endif + if (IS_ENABLED(CONFIG_RISCV_ISA_SUPM) && + riscv_has_extension_unlikely(RISCV_ISA_EXT_SUPM)) + envcfg_update_bits(current, ENVCFG_PMM, ENVCFG_PMM_PMLEN_0); } =20 void arch_release_task_struct(struct task_struct *tsk) @@ -233,3 +237,89 @@ void __init arch_task_cache_init(void) { riscv_v_setup_ctx_cache(); } + +#ifdef CONFIG_RISCV_ISA_SUPM +enum { + PMLEN_0 =3D 0, + PMLEN_7 =3D 7, + PMLEN_16 =3D 16, +}; + +static bool have_user_pmlen_7; +static bool have_user_pmlen_16; + +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) +{ + unsigned long valid_mask =3D PR_PMLEN_MASK; + struct thread_info *ti =3D task_thread_info(task); + unsigned long pmm; + u8 pmlen; + + if (is_compat_thread(ti)) + return -EINVAL; + + if (arg & ~valid_mask) + return -EINVAL; + + /* + * Prefer the smallest PMLEN that satisfies the user's request, + * in case choosing a larger PMLEN has a performance impact. + */ + pmlen =3D FIELD_GET(PR_PMLEN_MASK, arg); + if (pmlen =3D=3D PMLEN_0) + pmm =3D ENVCFG_PMM_PMLEN_0; + else if (pmlen <=3D PMLEN_7 && have_user_pmlen_7) + pmm =3D ENVCFG_PMM_PMLEN_7; + else if (pmlen <=3D PMLEN_16 && have_user_pmlen_16) + pmm =3D ENVCFG_PMM_PMLEN_16; + else + return -EINVAL; + + envcfg_update_bits(task, ENVCFG_PMM, pmm); + + return 0; +} + +long get_tagged_addr_ctrl(struct task_struct *task) +{ + struct thread_info *ti =3D task_thread_info(task); + long ret =3D 0; + + if (is_compat_thread(ti)) + return -EINVAL; + + switch (task->thread.envcfg & ENVCFG_PMM) { + case ENVCFG_PMM_PMLEN_7: + ret =3D FIELD_PREP(PR_PMLEN_MASK, PMLEN_7); + break; + case ENVCFG_PMM_PMLEN_16: + ret =3D FIELD_PREP(PR_PMLEN_MASK, PMLEN_16); + break; + } + + return ret; +} + +static bool try_to_set_pmm(unsigned long value) +{ + csr_set(CSR_ENVCFG, value); + return (csr_read_clear(CSR_ENVCFG, ENVCFG_PMM) & ENVCFG_PMM) =3D=3D value; +} + +static int __init tagged_addr_init(void) +{ + if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SUPM)) + return 0; + + /* + * envcfg.PMM is a WARL field. Detect which values are supported. + * Assume the supported PMLEN values are the same on all harts. + */ + csr_clear(CSR_ENVCFG, ENVCFG_PMM); + have_user_pmlen_7 =3D try_to_set_pmm(ENVCFG_PMM_PMLEN_7); + have_user_pmlen_16 =3D try_to_set_pmm(ENVCFG_PMM_PMLEN_16); + + return 0; +} +core_initcall(tagged_addr_init); +#endif /* CONFIG_RISCV_ISA_SUPM */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 35791791a879..6e84c827869b 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -244,6 +244,9 @@ struct prctl_mm_map { # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) /* Unused; kept only for source compatibility */ # define PR_MTE_TCF_SHIFT 1 +/* RISC-V pointer masking tag length */ +# define PR_PMLEN_SHIFT 24 +# define PR_PMLEN_MASK (0x7fUL << PR_PMLEN_SHIFT) =20 /* Control reclaim behavior when allocating memory */ #define PR_SET_IO_FLUSHER 57 --=20 2.45.1