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 F14DDC25B10 for ; Fri, 10 May 2024 23:29:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79FAA6B00D1; Fri, 10 May 2024 19:29:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74F9B6B010A; Fri, 10 May 2024 19:29:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 618356B00EB; Fri, 10 May 2024 19:29:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 43EF26B0142 for ; Fri, 10 May 2024 19:29:30 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E55E41C206F for ; Fri, 10 May 2024 23:29:29 +0000 (UTC) X-FDA: 82104080058.20.E627AA4 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf01.hostedemail.com (Postfix) with ESMTP id 1F35840005 for ; Fri, 10 May 2024 23:29:26 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=KVmUBz51; spf=pass (imf01.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715383767; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3Vz2Ea5vDvTd7NZaHdWli7Zi0tzSRPfvayQLC9Zmwn4=; b=R/RWr2YKxzAm4oG2WiHYJcKgUV40KHdTlw+hwWxo7TIC/MsqpZ5+lqa+C7w2yMFNuviRdY rtIUKy7oZDYFtxqAgDnhFrzjOLsdCNaDjPyLbNd9gkYNLPqlCzCFOwigCFMc6pUmmzyxxu ETmgyj2Ry8gyU43fX18SwWDFEj4bjjs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715383767; a=rsa-sha256; cv=none; b=1XDgPaYOttqdU57zdklCBor1MlgLKinQX9JplMMc1IPEiAhg15aCKe18TsTmT5Ff/RquYw uTP4agi1vBn6XTRttu6GWIEcFAJxjpuA47wsaUHSSqCKe/pTS+FnNAf7KJzunsJOsupNko VMffdB7+xLxb3y3RiCUIDsA90vDPlGM= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=KVmUBz51; spf=pass (imf01.hostedemail.com: domain of charlie@rivosinc.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=charlie@rivosinc.com; dmarc=none Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5e42b4bbfa4so1503740a12.1 for ; Fri, 10 May 2024 16:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1715383766; x=1715988566; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=3Vz2Ea5vDvTd7NZaHdWli7Zi0tzSRPfvayQLC9Zmwn4=; b=KVmUBz51oxM/JBJ725sZo38Qe98AwMXzSj2n99eZ/CQh20kJ9vFFEMQNRbwG2TkBG+ VN5oWVgKqlPz0ZjdZ0krqNK5Z6gFqFBZgxO/wb7RKS3wF8i9XADbIQ4S74DzoECoVwIu 8Lb+86HHHro4MwcMMdtxw9fxhWKaDas3KUYggz0F2hCfzXPY6HP3l6/qXPlsa+YRwNGJ xxDAV24QJvjiex0FCamUUVsdzsyoijXXFFxa+eS+vLDyS4uT/1dP74T1sOMcB1qSKyw2 QyD/OAumwRfjkiZXwmJJJqV8cc/4Ai/6W0LafLDNyd+yVmyTmgg8iQlIG0Vqpf7j81NS 291g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715383766; x=1715988566; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=3Vz2Ea5vDvTd7NZaHdWli7Zi0tzSRPfvayQLC9Zmwn4=; b=UcWsQTmdjW1HAxVEF5L1VAO6ibwgeihrfpk+PBnzek6v5X3eWk4jtxlp8UyuvD92R0 6cBpfGRnCeyvW1JM6GVC1v8ryKxOH27VDK0Il14yTM8wJ+whmEDcAaiq0hIFaawlJBUq BRX/sTNvk6vn6NPaAZhEBtH3ks61AF78IgH/WjSwM3hEcmzYP+/pL7ycyU6GmiTUT3LP CKKXaW9Guy3ebALQuipEBh4ysHWJnuM+bYO7AXx9K3CaIaeIpKEEPd45QqR1gzGtdpWH y0kmcHNm+4FsypP2Uq7lzY7/Djbmfshi5UIGDqgl1aOgWNtoghtTJuSAWcg3WIpxCcNL p/VA== X-Forwarded-Encrypted: i=1; AJvYcCU4utbWDezClFi7mi9S0sa/rGAocGtar+KSsVqtohgcUgsWi0ciUFUCXW3fvjxmyt860Uu1K1eVMgH/ZqVJ99jTf4A= X-Gm-Message-State: AOJu0YxSooQYNfRcJhZRy+SbB8aD0745LVVi1l/nXUFXkYN0OHE37rfB f4e+Ocs/gSTPGOBvjhZ4XKIyZdx0zTGiFSPJUKoOqIQRSsftjjZ5vIvIm25NpiGP/4Vnh0c85K1 E X-Google-Smtp-Source: AGHT+IFtRGEgJNgaXpQi4rnaLmSdVV1xvKctjw8cDKjjnX0+o8uhHRNsHVhKbF+1oYccjQdTEuSLLg== X-Received: by 2002:a05:6a20:96d5:b0:1af:5d8b:87f3 with SMTP id adf61e73a8af0-1afde0d5537mr4236824637.23.1715383765853; Fri, 10 May 2024 16:29:25 -0700 (PDT) Received: from ghost ([2601:647:5700:6860:629e:3f2:f321:6c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2b67158c3b4sm3755016a91.45.2024.05.10.16.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 16:29:25 -0700 (PDT) Date: Fri, 10 May 2024 16:29:19 -0700 From: Charlie Jenkins To: Deepak Gupta Cc: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: Re: [PATCH v3 17/29] prctl: arch-agnostic prctl for indirect branch tracking Message-ID: References: <20240403234054.2020347-1-debug@rivosinc.com> <20240403234054.2020347-18-debug@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240403234054.2020347-18-debug@rivosinc.com> X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1F35840005 X-Rspam-User: X-Stat-Signature: 58pmex6fs8rx8rq5y1551y5hoekxcupx X-HE-Tag: 1715383766-723026 X-HE-Meta: U2FsdGVkX1/OGuuVnSy4lthLNxA/k8F23W5+1RJln6yasI21VVGArsN4M0ccAwgRYnU0jvzBNTfmO/yx3/utJ9+IeRhZVi6wbmHhiQZRPPwHDy+dbqHCYPH1ziKfaWefWpK7ywwveIqT9iAWxkh1NdwAetN/uBBLrRw3nLA0yiA+KWJtTu6eiFCytoDubf0Y58W6NScCOvfZpJ7ByWa8THO5HN4ZLKTv0RNj+bC7DLIG3KuGjFZptQjbz82j42CoQdxFabFHX+gSM1zTK7ETnaFc7Z1V9At19JRmcuP1mYv8r/T7ygOCMYFefPW1hPjCjqiXB2pnYgFQkrH490by7AmJP+BnPxTYCmGj1B8olaBeBfITJV8qcN79w5qA81hE/3YKW6drWdwHPkE3tE5WQ1kvCm5jJrgWgCfnXbZLLiUzAOoK3YWlHB6XfGlAbKnbMlQ2PG9YlKXtEYp3rCYXYTli3boJfl0Gpsc5y+BKTEQpOgIH8bd2Usa1ALZ6TnJ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ity=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: On Wed, Apr 03, 2024 at 04:35:05PM -0700, Deepak Gupta wrote: > 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 242e9f147791..c770060c3f06 100644 > --- a/kernel/sys.c > +++ b/kernel/sys.c > @@ -2330,6 +2330,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; > +} > + These weak references each cause a warning: kernel/sys.c:2333:12: warning: no previous prototype for 'arch_get_indir_br_lp_status' [-Wmissing-prototypes] 2333 | int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ kernel/sys.c:2338:12: warning: no previous prototype for 'arch_set_indir_br_lp_status' [-Wmissing-prototypes] 2338 | int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ kernel/sys.c:2343:12: warning: no previous prototype for 'arch_lock_indir_br_lp_status' [-Wmissing-prototypes] 2343 | int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) Can the definitions be added to include/linux/mm.h alongside the *_shadow_stack_status() definitions? - Charlie > #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) > > #ifdef CONFIG_ANON_VMA_NAME > @@ -2787,6 +2802,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; > -- > 2.43.2 >