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 6D6B7C54ED0 for ; Fri, 23 May 2025 05:32:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B580E6B00A8; Fri, 23 May 2025 01:31:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE1706B00A9; Fri, 23 May 2025 01:31:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9CF556B00AA; Fri, 23 May 2025 01:31:59 -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 74C7B6B00A8 for ; Fri, 23 May 2025 01:31:59 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2942C5FAEA for ; Fri, 23 May 2025 05:31:59 +0000 (UTC) X-FDA: 83473051158.19.BA6070D Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf02.hostedemail.com (Postfix) with ESMTP id 3505380003 for ; Fri, 23 May 2025 05:31:57 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eKw5ogrz; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 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=1747978317; 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=vqrL1nSTJBoslfnw5ibPxgNBEr9QYrjQskvohvWsTXdhQf+/3sSefT2bHWiQVAUkt1DyyG WRBSlngraqOSVLvJ8ogAp1EIbp47DNPdHuQXgp557dBCsvRFMn9S2y3l+KomPwvyqTnZN+ G5COt7lS7aACN3Sy9fJAaKk/U8CnNCc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=eKw5ogrz; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747978317; a=rsa-sha256; cv=none; b=1/owKxLf6ZLH8ZWUiUVUBBHZ8L8HwAhk+mo1ro0cakf5S0FMAPSfJCb+vQ05761/EGC/p5 JdvQc0F2CdcGlqUFYjIUhNmqzRpCRradHaQn4/zAgprE3Pj/34mXVmOezVZbqZebhC3pCd LfI/VoJ/KAwbz7XVdaMNyl3Vu1wcDmk= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-7399838db7fso514643b3a.0 for ; Thu, 22 May 2025 22:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747978316; x=1748583116; 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=eKw5ogrz0XeJDhEPgZrQQj+QKUSBI7l5XcpJ4So8+1GyBItU03ADMB0bsvtNTgbGZf rEuoTuY/Ceop2YDJLAa/PhNdz2OCqt6o0YXODKiEje3DBAJBcm8GhUuEP8R1/QUkrLgT jvW0nTr5Q2n2quNXFEzqHrBys0K7rmgTcZC6hAlLN+1+7oRsEyVBqUz493QCRxCGAI5n 9MP/PmfBOSJ+7aoZ/7O2N9ntttRKGoVBhjZdwfKiVt7wPGpoGQKwo4f2GDtqoChXrpVc /h+rXeDPo27mS1S1zaF/z/u6thBjFElwQQD5UFvq3OdluuMYb4+imPlWddS3/+IfcXPq tdDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747978316; x=1748583116; 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=fFBchwC8UNdlklLdkX//kmUjZt3SS+Fmw9JgKxM8Pm3AlH//4vzJgfNJrBhds73pl/ SpYWGMe2z6AR+qy861509ds2a2RSjymztyjzmM7zP4x51o5iqTLHUNk9SV0lgkN4FjoV LL/2yevigRRQVsv5v+cOYox4r+CR5SKBA+KKKsQgZ9Udf5MDsrdq6I79hOnaq3nxK3qC cXatxPBvUhWBc98vqyYE8430FI+5OHUmGRj+vlbQdoi5nEKcA7NwQkwpy27eRwwvIT1t eCKm3UkFQY+HAejFTcMvZzGgW0RTW4KH54FVejMvm2XfZyz8QCbudj9ON9YEnafRZADo 1UOg== X-Forwarded-Encrypted: i=1; AJvYcCWVlT2APhoEZosIsqlVhH8ggTvxSWPjzvIg9kFOPaJ6gBMgHb9ZbwwIAUlMI3EfYpbS2pvdUPd0zw==@kvack.org X-Gm-Message-State: AOJu0YyVHOMsU+YwvbYAv/p8Jj2JMDdKDomizHq9Av5AHT9iNKYeLSIR /QTBzp31tZfxhKga5HnP7Yif0KRfuJBRZUWb7W5EbVTJ4VFcrVhBbc202q7CPw3qaMg= X-Gm-Gg: ASbGncsk0Zwz/sNa09B7B8jwfnvUhBZKcFHZnFdt51EcOppOhuyr/yJG84dnFfcJAHa zoRZUXmY4rILp39Nc/u9PJayzdb0aw+9ptB6NC1yKT4q+AnSCQtTc7eGLlfiEFaNkKKf2OuZIYD UyrXbk+HUMdpc3/J0ODlxjZ/BK3DhNYrwwL6E7DzmQ2AHE7/bxukw6gOQKId5GpHXP+q6QZroNj kuf93wC3/HT20EPAZ2btAb6L1NH3sPDYFC8iuf5dvvHCjhZ4cDMuHel5WKFodtueWKVHCHbesuq x+7f5ENSSEEXN5EQTs/xq7mq+ZRjQtndj75Ee/XuNQsOnvLYpq9tqr7AR3PSNg== X-Google-Smtp-Source: AGHT+IGQc6IZJp5HAkHQYlyq1dVOD5fmS/7I583lCwYe7yctSptPmxrk/1ttbdxyBKVGijqawjtVpw== X-Received: by 2002:a05:6a00:9294:b0:740:6f6:7338 with SMTP id d2e1a72fcca58-745ecdc83c5mr2853244b3a.3.1747978316020; Thu, 22 May 2025 22:31:56 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742a982a0a4sm12474336b3a.101.2025.05.22.22.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 22:31:55 -0700 (PDT) From: Deepak Gupta Date: Thu, 22 May 2025 22:31:16 -0700 Subject: [PATCH v16 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: <20250522-v5_user_cfi_series-v16-13-64f61a35eee7@rivosinc.com> References: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@rivosinc.com> In-Reply-To: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@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: 3505380003 X-Stat-Signature: ke4unh6jcfhdshepa5t11dacp5mswp8e X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1747978316-300710 X-HE-Meta: U2FsdGVkX1961EDzOxPLT13SDjv2yI8zGojmCjhMLFaP0K0pLQ5p4dbgprv+/TUieRQ8sQi41fmJijIhROKEuHmRkULf51mgKgUswtNLtHn1/fCi+wFuqJ61FX+m3JRlHx/WLshHoKTV6m13stYL0S670eCdsKWil9v66ibyjd56Pbjq/feoKgpUCgzXGUNeTbFFOMKF8LD5FQPg2f0Y9pUFREautfTcilsz8ei4bxQj8PeUrLvFBZE4b8os+iYX3lmw3PVXfMD722MovCzvsNBm+cuK4IY7q+pbQkld4fjUzl8yPJdWqZy0Aac60REPGEGUNZohPjW/iWnubMTJACZk8Tj4TG4T8M13YCkZX4Ttsfcjj/IH4/5zMTlrhKs2GAxv0hjQO1XV/Ih1c92WdhN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NA0TXsQP8emQW1g8A8TPF9ptCZS5aMRfJHlB8zPNEZOAbLrlSH 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