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 8FC1DC3ABAC for ; Fri, 2 May 2025 23:31:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A4C26B00B5; Fri, 2 May 2025 19:31:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 803B26B00B6; Fri, 2 May 2025 19:31:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 630026B00B7; Fri, 2 May 2025 19:31:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 430FA6B00B5 for ; Fri, 2 May 2025 19:31:22 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2AAA7160995 for ; Fri, 2 May 2025 23:31:23 +0000 (UTC) X-FDA: 83399566446.09.19CE888 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf24.hostedemail.com (Postfix) with ESMTP id 3D6B7180013 for ; Fri, 2 May 2025 23:31:21 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZnWjxsqC; dmarc=none; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746228681; a=rsa-sha256; cv=none; b=LdyqZw9/yloiqRdhSL6rb1PHZWt7Ux20qJVIzd90EHdtvy0UNgGb1gxFeZepbiLjd6JaUX xi4bRaj61iEtOcDK8FjJe7CQT7KHMiO67j1pl8EDUlWilaGa31fsEc+ooJosixcQo7dFnm Vs/+APcFIFAxS+c+lB/Ay8rauy62QEk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZnWjxsqC; dmarc=none; spf=pass (imf24.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 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=1746228681; 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=Wqa+EnEmqD/WcxsFXdACBzGsJGhuUJWdKCVGNJwyc08=; b=FosZ1T7Yn3VYS/02QegR1NhAQ+FaTkBBFu49YLKaAfktGYyUH1YaB/CL7TtSx6Vi+TPbm6 aF2KfO73z//Tiet4iu1n6N/c+y8c6AfG4uhgFcMUIh1ApCzspiiPob5vHzSahh58kwWfTa UkrJjj5WY8M+jYruL+BkQcPUQW7pS5w= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2279915e06eso29956375ad.1 for ; Fri, 02 May 2025 16:31:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1746228680; x=1746833480; 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=Wqa+EnEmqD/WcxsFXdACBzGsJGhuUJWdKCVGNJwyc08=; b=ZnWjxsqCnSh4FfGVvYQwzbayMqOEOJY+gOBflXi98MpcxFdr87s+srhrhm2HTfbQIf P2oaotN0PuPcIEsbDr3LGVuhjU7AYwO3uIF5+Pa0/JG96cnJ/gbaA2O8wJZNy743trAR k6ASdl/98TEmtokeesG3uvB+9zJv8tTwalezAjKEwLAxMdjX4mfXAJey/dXnJfW8ZZaM yHu02qvbbR1vop3iMxZkH7WpRtJqimQcZqOL16M/FawH0sRBlpSUdFZ4E3LR7FU+eqcx JYNjx6kJYCi5lTGmzEg2p5UUVKbLi54eOfc9K8bFKX1IgPImI3oDHZXXSK6euUB4z9rY x5ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746228680; x=1746833480; 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=Wqa+EnEmqD/WcxsFXdACBzGsJGhuUJWdKCVGNJwyc08=; b=iRPZQ78p8Sec9bQHLoxJImv+1JpIL2ghovBzvhepRkgdwouGQUvd1BeyddksyXP6Ic dw5XiEiW+XRADhV8ltRKRqVMq0yrC4wNAHWLEXl65KQxVeAQWG5U0TlDTMdgzVmNrycK Oge6ckMI3dNJqo7UxokCQtbLH73pRaV6J7sszpEzoZFWqPoYlZ0dC0GbWLX1aGIiPAuF /Uv/C/ync+4RKWfxM7RfV/DzI9UTKuwGDGkAUB36OAe07Ce1B/TA2mm2zypj5lUxl+YZ 9cla/jQneqTwO1RNbqjBJA9riWkDsDcvcDGi5+cfi7rG4O3H5wnwIMSMF0FDLR/FErI/ vgQA== X-Forwarded-Encrypted: i=1; AJvYcCVs6g9MESL9wZtTMlLhe9LMYpkwY90whx7xMZdmJB6x6+7rEdeE/HFK3mwxQ865YUzFe/W8UU4WmA==@kvack.org X-Gm-Message-State: AOJu0Yzqaagi4uSqHBeojCxqfePtQGqK351psoG0C6/RVEIl3pQ05fpS MTQ1dlXbPJa762VE60sFgoM+dNB/3p+uqmAf3IMr/7FujHuXgCK2w+GUeyY4HLg= X-Gm-Gg: ASbGnctFWX0/DjkNEcN6td/bNeF6zqTACgYW6Qy+N1ZEgYT2jOiVPViDM1BQH8FlsrO ID1Yr1ooIkEsokIuqa71rdr+GErz13fOnGnoR4LLkmElvD8jP9/6R4u7jYKHj+ImCVkv592CHKF tLtSlqBYEZpsa1BeeiFPbayc9LrYUosRoUqzndczib24FuT2L0Oyof4SsOETsloIJtTLeOjt7SM rof24t4wQvYfWGRl9/Sr5eo60zNVvuH2xBDXyK8nkMPO/ITMHgfcEIZ0LlFBFqy/gIuAQXgORgs N5cyoEBNY45PI5fKJs5bOg4i9R0tjAmR5lNaLJ6q7jWks41fLPo= X-Google-Smtp-Source: AGHT+IHvXL5pWhrUbiSVOochGhIxFNup+4MO6YwBLUVqWrs7X/12jWeixSydoIhUkoMUZk+BhO60Fg== X-Received: by 2002:a17:903:1a6b:b0:21f:61a9:be7d with SMTP id d9443c01a7336-22e103ecbd7mr73331515ad.49.1746228679984; Fri, 02 May 2025 16:31:19 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22e15228ff2sm13367055ad.180.2025.05.02.16.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 16:31:19 -0700 (PDT) From: Deepak Gupta Date: Fri, 02 May 2025 16:30:44 -0700 Subject: [PATCH v15 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: <20250502-v5_user_cfi_series-v15-13-914966471885@rivosinc.com> References: <20250502-v5_user_cfi_series-v15-0-914966471885@rivosinc.com> In-Reply-To: <20250502-v5_user_cfi_series-v15-0-914966471885@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?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross 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: 3D6B7180013 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: ff5jsqst5dtk3nbcsra8e74ht6p5dpeg X-HE-Tag: 1746228681-883192 X-HE-Meta: U2FsdGVkX1/1nq07IJHGQoM+4HWvsiPQ0ueL+ORmtxOqzizJcnWOg5U4MheV8q14bQ8aMeoW+wsIkmRZFWLb64Tei7hjUyMkak7eo71QgP+Jz83CdQyDf7Ei2rzDZ+jUyvjE9TZZtmzKVjS9C4g6Dt3qacMWDdMNHeF5lckayDOPqx2mCszYLpKI6f7bz6693txKN8X5qlhUvDAAp2Hg3A8+98Z9wN3mBQ+vQIHUJROVRhU1UrPacyUG6T8RDsR1+tLfrbd702cI7ybnkasTecCKJfuVVdf60ZrbhN3ZWsIvUnT/hXB7Hk9dKe4B15lUIjedwf+nLYRQovRZBN2RNpXe3Fgwln7ANq+u5LhcnxAtSUIUzjzoO+YZmv/Efl3ErymG4xLUs+6ecd2a3dmOU4IS4c38u/fPqPW63fRIhye6LnPLv9zGD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ar 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 e3049543008b..ea8b351b5bc5 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -204,4 +204,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 15c18ef4eb11..2e09b19317a3 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -364,4 +364,31 @@ struct prctl_mm_map { # define PR_TIMER_CREATE_RESTORE_IDS_ON 1 # define PR_TIMER_CREATE_RESTORE_IDS_GET 2 +/* + * 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 78 + +/* + * 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 79 +# 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 80 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index c434968e9f5d..91a1dc093c2a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2340,6 +2340,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 @@ -2820,6 +2835,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = posixtimer_create_prctl(arg2); 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