From patchwork Thu Jan 25 06:21:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530088 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 1DFA3C47DDF for ; Thu, 25 Jan 2024 06:30:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBA438D0009; Thu, 25 Jan 2024 01:30:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D3C768D0001; Thu, 25 Jan 2024 01:30:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B19FC8D0009; Thu, 25 Jan 2024 01:30:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9C0BF8D0001 for ; Thu, 25 Jan 2024 01:30:06 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7267C1207BC for ; Thu, 25 Jan 2024 06:30:06 +0000 (UTC) X-FDA: 81716858412.12.8AA3F4A Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf06.hostedemail.com (Postfix) with ESMTP id 6D09318000A for ; Thu, 25 Jan 2024 06:30:04 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NFqPiFZ9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.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=1706164204; 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=ZGW+3ds4X7hyu8QcIwFbpgQ3FZs37tf1ecbtDkVWTR0=; b=M7vXvW5ZlEt978QD7SkIfBzm6Mopqop+d6+vh81VBp94eFI9cjo//kZXvZt45Z1/FcOKLa BapRREwZdwhXL/6UyRF93/OxYM2M5vBzYKqzXPfesRqvld0Ji2JFOt0YfqTPOzVM9oX6ja E1M7YsYXpHerhrd/SRhx7FiW57d95P8= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NFqPiFZ9; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164204; a=rsa-sha256; cv=none; b=ihruMUv9pFFIYHocuZeFQ84wY2f+DRUsaYupNrbNaC80XGbUxHVZhZfbvLUm0+uPqSTts4 CkHfwlCwL2G4hxtfw4NgBI0qSCiu97aPhgcxwFYDuIxXNup8v8q3N5UcnG3WApLlGWsqbG t7tjDo/+9f/FGdy61QfukP6qX7Lc4SE= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6dd853c1f80so290753b3a.1 for ; Wed, 24 Jan 2024 22:30:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164203; x=1706769003; 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=ZGW+3ds4X7hyu8QcIwFbpgQ3FZs37tf1ecbtDkVWTR0=; b=NFqPiFZ9AVGQIChzVwUP1UY4zxi9SZITOxouvsYhK56WPzWyQ21hjPs7WPYsdW301n MNxy8Xbw5Jswjh0n4MdViR5Vo5Hy43kasQoT3SaXoWzOF6PMWg+ix4rAWQ+X8SFSr6J3 Oxdq6jokf0KbLI8QA4/xV5zsZ0K75V1H/L8Y7kRG1Kv2Ns8FcJjK4ZCMSmWm3+aT7w3I 4OFPf+10IV9j5N5tm9xKwwtXNTBPfB1ws+iRDiKa7YbS/kGt1G4K+u6JWmjB+f+uHMl0 BqXYdRhSn3DzPg4DdIqUBqt5Lf9OFMro19ovNVV6HdbM9viVFSQIN2XC4js138rLJEUS wjeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164203; x=1706769003; 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=ZGW+3ds4X7hyu8QcIwFbpgQ3FZs37tf1ecbtDkVWTR0=; b=LhQxT4HFe7pI8QHzVJRt+RDjrq+IdUMBOBnIbdkgHDp5aPONKdIIrbK3O9Td61eBK0 8oj7/ilPrlxpw38b35hpyPjEYu1N37tTqe9t3coZYuYvXKnurV4CRaYJIgG5y/uShndl u6SVnT1PJcG0ysJiiSJ31SjECUbCi8QO757A/dYm4HG8HfH+sY86QjkMbFWN6J3d7Ut5 JPUYk6BSIM++FfU2cVlChMreU4exZchabZg/GEb+CBQPCQxvJZ3aV+8CdVDLosm8epM9 rRlW7btjIss7eEZfqkXEOwVezC+86kv10tasgW3MWvGtX2CLPdeVhRu1btmkMzL2YVZc YxNg== X-Gm-Message-State: AOJu0YxuWZ+Sci57wkzEKdg2SwJl4cVzRfBJ7gmKL7/wo3xfKh5kWpkp c+idDcjLsp4pq8d9gnmyhHzwCOdPoiPfkvnGWHFd+GpUZ/Md6rKwv6PwVVTNHmg= X-Google-Smtp-Source: AGHT+IFX0O2DMMykvx1boq4E3+vew5Q+ucUD8KeW51e5M6H2MI9XnhwlSZDCE7ZMzSEryAMcjaVeiA== X-Received: by 2002:a05:6a00:b87:b0:6dd:c77a:721f with SMTP id g7-20020a056a000b8700b006ddc77a721fmr862372pfj.0.1706164203244; Wed, 24 Jan 2024 22:30:03 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:02 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 18/28] prctl: arch-agnostic prtcl for indirect branch tracking Date: Wed, 24 Jan 2024 22:21:43 -0800 Message-ID: <20240125062739.1339782-19-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6D09318000A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: cxkhorgziwx6516wg7t3u94uicf85d84 X-HE-Tag: 1706164204-851319 X-HE-Meta: U2FsdGVkX19qxztHatCLnJzseUr2NRP2F6RUzbmjl8gyR1hr4VY2FuwaGFRopimdYOD2a5bHo9X5lEUkpqMBQNGIQoCTpw2x9dXkJOihXHZ5SaDWndOOZsgvsDrl07g6e492j9zOkq4SHCIwChbmR+4h0PXhX3/wfrNgNoAdEfX7T5FORC09EOuIgCrvieaQ+w0imaKcIw9GCzYatNr1L5vX7xMriHXtdwmsmUeGkVo60E0dFn0dgELJqV7hh//czwiZ14oplAUaRiXZcsuUhEhS85B9w57xJlcyVXL5fYqzS8aMl1fuqQUuRXFeb3m8rqH4ijShBPYk72A6u6v+yJkqPZ1SvUegNIQPeKGqtCrtURQuHZAnPthc2nnrcyzM14Vjv3l9c7ZfoQq5T888N10zXZk+1QULTWD6LOpMibMZSseC//9LAcCExNKPvzGTj8RzyutHOTu5J8TeyoejvUT9Du/4wUI307GIlkKsCTFinY4K0k+VZ84NraHckZ1O44ICXDUihsDFNlEZXwS9O4gvNMSYq4081Eio8cCfy5jUhX+oO7kOQ/KMHnB5VWPsgfgJZvjeHc61otRIIIAseHrl6sX6Umj8guu2qaSp1Tv25sOcS3VMs7aN6shCgEzU9qMYx1FUdgTPyB2VeqdBFwKby4UDwlchT641OxX/Zf6jeMhiAYuZ/8b/o6vkGDmE0XomCuX8As3EXpXbo0tLIbyeqPFlkQ++MgZshHYU6+PuvXd1uPer4s1i0wAM1Nt6mEE1fP7HFkWd0FsvsfkUXa4HQJjWnepDRzyjVHXackehjkwYaYfe6XMH9wc2da0DLyIwMA1EXoTNcFZzbp7F8XWb9oYCuS23Rr1vhp67HmrAKsDDn+JaC4SM09eLc2+er4eJBmRjTuelALWQcNUwCAJ85+JKyNGpP25AN/N2tT9z/AsCYnznVy5qrFnzUxo8dgE7iCn1/QxxOaETZ1z v6AIg9sm IRioo/JB0dEuLcZMBxXBJg+H6QRiripoT4geL7Hb3ixJ+YWmL7sw/D7bQl9kflWS5pH7Kd1FOY/OqaQ+UC8+QYGuk+WuuM6fhL//eFNmNW7LapB2kozk3DrxUTZO+4O2NgAtUSK8v2Fn/2eU3YZAWEizfjnSsKvuqUAzDuJ/7PjWTvOtG1088OajIJaMg/iStXtGHXVYtPZNkGthzw1ATr3za1JwcPWSOQ9WVQ83pF1MrfHLQocA1y7mejQHm1WRNBs36Uw54F4qy/lkWtcjbFvtAi+2HCrQhxMBSeuX/WtkGqYmcjyGCoQd6cm+4ekBXNW95hkuvw8UiX12DUjme6Q7WStEQiMJN2NOBafcIa5KInIhZqTZJaBgrBqzBIViAuIdUL84np4hh/4F6KLp2r9/PQ+8UXN8p/CY9gDbn/c59QpBB3oihoDJISyBt5RNruroq 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: Deepak Gupta 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` aarch64 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. Signed-off-by: Deepak Gupta --- include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 3c66ed8f46d8..b7a8212a068e 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -328,4 +328,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 73 +/* + * 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 74 + +/* + * 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 75 +# 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 76 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 96e8a6b5993a..9e2ebf9d9859 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2316,6 +2316,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 __user *status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2773,6 +2788,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = arch_lock_shadow_stack_status(me, 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, (unsigned long __user *) arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, (unsigned long __user *) arg2); + break; default: error = -EINVAL; break;