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 BC16DC83F1B for ; Fri, 11 Jul 2025 19:46:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B0B36B00B3; Fri, 11 Jul 2025 15:46:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 561D26B00B4; Fri, 11 Jul 2025 15:46:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DC746B00B5; Fri, 11 Jul 2025 15:46:57 -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 2D4B66B00B3 for ; Fri, 11 Jul 2025 15:46:57 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 01A43BACF3 for ; Fri, 11 Jul 2025 19:46:56 +0000 (UTC) X-FDA: 83653016874.22.3EE0800 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf04.hostedemail.com (Postfix) with ESMTP id 12FAE40007 for ; Fri, 11 Jul 2025 19:46:54 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0k3PstjI; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752263215; a=rsa-sha256; cv=none; b=FJn5mEJQ5FQ9nNczp+cPn6D8WtTT6/nZAB8oLI3ZNvF2MHzRyUj3xJRGd5pZP5+yQb8DGA eOzOitz/yFxk+cYgs+XAKhUsfFx7Dsb0YGw7OTCN8V6WMZ0KS72pUbgDdtYQVPLh6ZNfXM mDZLheu5pI0VX4ElY5lTbDBqWm8+T1I= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0k3PstjI; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.169 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=1752263215; 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=9Zc5n/fmBFqSLzd2InHh+oo1/6vhMJLkVEGctgWUI64=; b=Q27KIQdiHGT9ACr0DTq+oTECV+/55XHCqlQkgYIMemrPYFOiIRVrC054YAGX6VL7rud6LY 2f+t15J9z2G9TkegpHTG66w1RJAOMLo3+CyUYbP45eEnsXETjNZpztCpwZgQvGeGlmifn8 Am2TZRwcVnTao9cqlCqopfI/dNccxtw= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-748d982e97cso2276309b3a.1 for ; Fri, 11 Jul 2025 12:46:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1752263214; x=1752868014; 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=9Zc5n/fmBFqSLzd2InHh+oo1/6vhMJLkVEGctgWUI64=; b=0k3PstjIgYs/FPVZrIAgKOmVue7edkewQBDbCHnESiTYXMqV8hJY4p5yBmEALm2RS3 VIoHeOpNt1U5wq934p6pctHRC1jra9FtdRb7frfYYsxosbXtZhTE/p9bu/UeXXT2G23+ vzJj6lDmXHTOrbzQkebGThji4WGpqgBAJE59Z8bBiYDr47txOrEQKaaptG9yol7nGt+w bFXfdGR4FQg1YkNWr/yl+wfzU1pQNo4xinedUJTzardFt1h7LZLlj2ioNowQgk05AOTo 3IZeNsHjaM7Gh9hQhRQE3DcoF0Uj6hciPBJlXIekrUN/5wS/EOrrvlXh6IVd5lRaZJhq VYzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752263214; x=1752868014; 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=9Zc5n/fmBFqSLzd2InHh+oo1/6vhMJLkVEGctgWUI64=; b=BYBbwXBFCkiKF78+BnUmWu0Ycje/qlXQfD9GU4V4pKI7euv4RDozAxL8E1Hmr2Kouy 9FyUY6kZOvTnWIQWXZBZSA4sxVAir/FZ8EZozRQjCRfJH6NsTe8Ysg/oCMk4i7USZeZ5 s1pFn1S3PNeizLGmoAmGDemU5RqLdjv08pki5lK/cNHG42S02MHZyjbtJtINTGvIK/Ky LonG5RmC/qNBB0Xx01XqH5z8vZ3Hp2MeHvob5UmZadbklhSs+ACa1jIvsQSafQ786aOB LD8lFXqcA4WRUFRn1YHv6iYkak9n8Y3Fq7VXthyITF95s4TzBLmdErLBH7FO4LjpKfca g6rQ== X-Forwarded-Encrypted: i=1; AJvYcCXSztdfSgYhq7BG4h8it+mrQ0ffzy48txxPHH21xvIiUwU7XkpD9bNPWvm7acHhMTM1eki65zsFJw==@kvack.org X-Gm-Message-State: AOJu0Yy/sitcAliUBH/giTyjBJGz6wxJdNtMhJm9c2V3mpy17cpD3Qj1 WEDsZXOz0cH8uIBDPsp9Bx7muxDF90/k4tVBK/Pn1y/eBH0vyhKkJTVLfzSFSiSvI94= X-Gm-Gg: ASbGncs608kXHSyeB8lTmM3ylSC3PQZtiNPDUc5bzXk2/gcBRo9+MXY63oRCMMHWCRw P7GXdQ0ojDXf3GngJerGWubJcZkUB/N92GLema3DgK4Tnedl7L04bxPsMpPCPa7IJEXHhON+3yG v6lYEGUxZvywaSRb1qeUMZHtBLArwLd07FB2CVVBzuDg8UobkSosOvjYDj/mTrGp2WkSeX5+PyJ nRA2KPlZL9/zGAH2GXzZgABoYeRZwRq+6G0yGvihqzTabO7mhANSb2+EMt8epkusB7aoSUXZ1Jo q4YIvvDtaLfhjdOQjOs4RWMg88tcQx2f1XR3+V5K+yUa7C3KfvuHv//RjWLSLGJYVkGzOMaYbPX 3j8bYCgDIDzlSRKSUZcbdbsCnqrf6ZuAa X-Google-Smtp-Source: AGHT+IG9YESao8E3E+NAefGQdBa7e8A7i1d/GjNrpQQsGCEH+wr+b7VKqJz9UJMRxEMoKnBp2VxK8Q== X-Received: by 2002:a05:6a21:689:b0:230:d674:e906 with SMTP id adf61e73a8af0-2311f61b48bmr9002009637.34.1752263213908; Fri, 11 Jul 2025 12:46:53 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-74eb9e06995sm5840977b3a.38.2025.07.11.12.46.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Jul 2025 12:46:53 -0700 (PDT) From: Deepak Gupta Date: Fri, 11 Jul 2025 12:46:18 -0700 Subject: [PATCH v18 13/27] prctl: arch-agnostic prctl for indirect branch tracking MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250711-v5_user_cfi_series-v18-13-a8ee62f9f38e@rivosinc.com> References: <20250711-v5_user_cfi_series-v18-0-a8ee62f9f38e@rivosinc.com> In-Reply-To: <20250711-v5_user_cfi_series-v18-0-a8ee62f9f38e@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 , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Benno Lossin 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, rust-for-linux@vger.kernel.org, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspamd-Queue-Id: 12FAE40007 X-Rspamd-Server: rspam07 X-Stat-Signature: wzwf7sjyqzddide4e7xqfbsn38i5jnqe X-Rspam-User: X-HE-Tag: 1752263214-718608 X-HE-Meta: U2FsdGVkX1+LpXxikqBTkebFHu6eMFLR+IHQhK7o1ERVhlFUL78ox3srHkEDtb5P5cwt0C/0IK7DOTrcobtCXYPiA4LTmsLucB3RPmOPLrGWsGxmQpq9TgCKVFGGkBmav89lkJCmD0d4LaYfqjIRAnRmm5EyXb0wE973mtakNWY0R1KkJSpeffvvjrNMGiHwxrUn9YOEctNC2JPOHsCmgwLg8HGwLjZNp92FmfnDlgiuASgKoUrg91uS7wArmQm1SNw4xINJ0nD1wLPE8R3kpCC4AhLzG6l3gJ8gOCfIh0Ns+Fa2ozSTwaIYHNzA1tibg4RRHg3ufzs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Z4C67DlYlEwTE69sIKtvN37wfWM2XHY7ivCBCyro4sHBlmRZtnuDyjB6f81kFgIkpEJ1FytAhO4CvDm1Xvfe80rVNogZbgVDYX8CSQ5HLrpU7ANOM62/KlHD4cWDOx3ZLoYCXp7V+vQY3ZzNIRd7OZ 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: Three architectures (x86, aarch64, riscv) have support for indirect branch tracking feature in a very similar fashion. On a very high level, indirect branch tracking is a CPU feature where CPU tracks branches which uses memory operand to perform control transfer in program. As part of this tracking on indirect branches, CPU goes in a state where it expects a landing pad instr on target and if not found then CPU raises some fault (architecture dependent) x86 landing pad instr - `ENDBRANCH` arch64 landing pad instr - `BTI` riscv landing instr - `lpad` Given that three major arches have support for indirect branch tracking, This patch makes `prctl` for indirect branch tracking arch agnostic. To allow userspace to enable this feature for itself, following prtcls are defined: - PR_GET_INDIR_BR_LP_STATUS: Gets current configured status for indirect branch tracking. - PR_SET_INDIR_BR_LP_STATUS: Sets a configuration for indirect branch tracking. Following status options are allowed - PR_INDIR_BR_LP_ENABLE: Enables indirect branch tracking on user thread. - PR_INDIR_BR_LP_DISABLE; Disables indirect branch tracking on user thread. - PR_LOCK_INDIR_BR_LP_STATUS: Locks configured status for indirect branch tracking for user thread. Reviewed-by: Mark Brown Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- include/linux/cpu.h | 4 ++++ include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index e6089abc28e2..e94cd69825e2 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -208,4 +208,8 @@ static inline bool cpu_mitigations_auto_nosmt(void) } #endif +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status); +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status); +int arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status); + #endif /* _LINUX_CPU_H_ */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 43dec6eed559..9cbb37afccda 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -371,4 +371,31 @@ struct prctl_mm_map { # define PR_FUTEX_HASH_GET_SLOTS 2 # define PR_FUTEX_HASH_GET_IMMUTABLE 3 +/* + * Get the current indirect branch tracking configuration for the current + * thread, this will be the value configured via PR_SET_INDIR_BR_LP_STATUS. + */ +#define PR_GET_INDIR_BR_LP_STATUS 79 + +/* + * Set the indirect branch tracking configuration. PR_INDIR_BR_LP_ENABLE will + * enable cpu feature for user thread, to track all indirect branches and ensure + * they land on arch defined landing pad instruction. + * x86 - If enabled, an indirect branch must land on `ENDBRANCH` instruction. + * arch64 - If enabled, an indirect branch must land on `BTI` instruction. + * riscv - If enabled, an indirect branch must land on `lpad` instruction. + * PR_INDIR_BR_LP_DISABLE will disable feature for user thread and indirect + * branches will no more be tracked by cpu to land on arch defined landing pad + * instruction. + */ +#define PR_SET_INDIR_BR_LP_STATUS 80 +# define PR_INDIR_BR_LP_ENABLE (1UL << 0) + +/* + * Prevent further changes to the specified indirect branch tracking + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_INDIR_BR_LP_STATUS 81 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index adc0de0aa364..af6c28dc6b71 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2341,6 +2341,21 @@ int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long st return -EINVAL; } +int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2824,6 +2839,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_FUTEX_HASH: error = futex_hash_prctl(arg2, arg3, arg4); break; + case PR_GET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_indir_br_lp_status(me, (unsigned long __user *)arg2); + break; + case PR_SET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_indir_br_lp_status(me, arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, arg2); + break; default: trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5); error = -EINVAL; -- 2.43.0