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 X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C7CEC433DB for ; Fri, 5 Feb 2021 15:17:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7D544650EA for ; Fri, 5 Feb 2021 15:17:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7D544650EA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DBD5B6B0081; Fri, 5 Feb 2021 10:16:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D76596B0082; Fri, 5 Feb 2021 10:16:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE8256B0085; Fri, 5 Feb 2021 10:16:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id A2D376B0081 for ; Fri, 5 Feb 2021 10:16:54 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 71370181AEF1A for ; Fri, 5 Feb 2021 15:16:54 +0000 (UTC) X-FDA: 77784566748.05.rate48_5800ab2275e5 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 52B901801969E for ; Fri, 5 Feb 2021 15:16:54 +0000 (UTC) X-HE-Tag: rate48_5800ab2275e5 X-Filterd-Recvd-Size: 13552 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 15:16:53 +0000 (UTC) IronPort-SDR: oCv3cfvhXLeYlIz2T6QVPLgmrNf8+WZmWRrvySvgwxMm3UiIwRniVlGhvtT/TIjGX6199zwpuS foJ9mNO7bg9A== X-IronPort-AV: E=McAfee;i="6000,8403,9885"; a="180668943" X-IronPort-AV: E=Sophos;i="5.81,155,1610438400"; d="scan'208";a="180668943" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2021 07:16:45 -0800 IronPort-SDR: 3eANWtqascL8MdqUIK8RZ6Xvx57XDRQlibPw9uDAxt0iOqF2cUfi6kOP4+Qdm1ncQcV/APWJNP L1a3oC5SBZKQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.81,155,1610438400"; d="scan'208";a="397474638" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 05 Feb 2021 07:16:42 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id A6D56108; Fri, 5 Feb 2021 17:16:40 +0200 (EET) From: "Kirill A. Shutemov" To: Dave Hansen , Andy Lutomirski , Peter Zijlstra Cc: x86@kernel.org, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , "H . J . Lu" , Andi Kleen , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [RFC 1/9] mm, arm64: Update PR_SET/GET_TAGGED_ADDR_CTRL interface Date: Fri, 5 Feb 2021 18:16:21 +0300 Message-Id: <20210205151631.43511-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205151631.43511-1-kirill.shutemov@linux.intel.com> References: <20210205151631.43511-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: The interface for enabling tagged addresses is very inflexible. It implies tag size and tag shift implemented by ARM TBI. Rework the interface to accommodate different shifts and tag sizes. PR_SET_TAGGED_ADDR_CTRL now accepts two new arguments: - nr_bits is pointer to int. The caller specifies the tag size it wants. Kernel updates the value of actual tag size that can be larger. - offset is pointer to int. Kernel returns there a shift of tag in the address. The change doesn't break existing users of the interface: if any of these pointers are NULL (as we had before the change), the user expects ARM TBI implementation: nr_bits =3D=3D 8 && offset =3D=3D 56 as it was im= plied before. The initial implementation checked that these argument are NULL and the change wouldn't not break any legacy users. If tagging is enabled, GET_TAGGED_ADDR_CTRL would return size of tags and offset in the additional arguments. If tagging is disable, GET_TAGGED_ADDR_CTRL would return the maximum tag size in nr_bits. The selftest is updated accordingly and moved out of arm64-specific directory as we going to enable the interface on x86. As alternative to this approach we could introduce a totally new API and leave the legacy one as is. But it would slow down adoption: new prctl(2) flag wound need to propogate to the userspace headers. Signed-off-by: Kirill A. Shutemov --- arch/arm64/include/asm/processor.h | 12 ++-- arch/arm64/kernel/process.c | 45 ++++++++++++--- arch/arm64/kernel/ptrace.c | 4 +- kernel/sys.c | 14 +++-- .../testing/selftests/arm64/tags/tags_test.c | 31 ---------- .../selftests/{arm64 =3D> vm}/tags/.gitignore | 0 .../selftests/{arm64 =3D> vm}/tags/Makefile | 0 .../{arm64 =3D> vm}/tags/run_tags_test.sh | 0 tools/testing/selftests/vm/tags/tags_test.c | 57 +++++++++++++++++++ 9 files changed, 113 insertions(+), 50 deletions(-) delete mode 100644 tools/testing/selftests/arm64/tags/tags_test.c rename tools/testing/selftests/{arm64 =3D> vm}/tags/.gitignore (100%) rename tools/testing/selftests/{arm64 =3D> vm}/tags/Makefile (100%) rename tools/testing/selftests/{arm64 =3D> vm}/tags/run_tags_test.sh (10= 0%) create mode 100644 tools/testing/selftests/vm/tags/tags_test.c diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/= processor.h index fce8cbecd6bc..77b91e6d3c85 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -305,10 +305,14 @@ extern void __init minsigstksz_setup(void); =20 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI /* PR_{SET,GET}_TAGGED_ADDR_CTRL prctl */ -long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg); -long get_tagged_addr_ctrl(struct task_struct *task); -#define SET_TAGGED_ADDR_CTRL(arg) set_tagged_addr_ctrl(current, arg) -#define GET_TAGGED_ADDR_CTRL() get_tagged_addr_ctrl(current) +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long flags, + int __user *nr_bits, int __user *offset); +long get_tagged_addr_ctrl(struct task_struct *task, + int __user *nr_bits, int __user *offset); +#define SET_TAGGED_ADDR_CTRL(flags, nr_bits, offset) \ + set_tagged_addr_ctrl(current, flags, nr_bits, offset) +#define GET_TAGGED_ADDR_CTRL(nr_bits, offset) \ + get_tagged_addr_ctrl(current, nr_bits, offset) #endif =20 /* diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index ed919f633ed8..a3007f80e889 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -630,15 +630,21 @@ void arch_setup_new_exec(void) } =20 #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI + +#define TBI_TAG_BITS 8 +#define TBI_TAG_SHIFT 56 + /* * Control the relaxed ABI allowing tagged user addresses into the kerne= l. */ static unsigned int tagged_addr_disabled; =20 -long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long flags, + int __user *nr_bits, int __user *offset) { unsigned long valid_mask =3D PR_TAGGED_ADDR_ENABLE; struct thread_info *ti =3D task_thread_info(task); + int val; =20 if (is_compat_thread(ti)) return -EINVAL; @@ -646,25 +652,41 @@ long set_tagged_addr_ctrl(struct task_struct *task,= unsigned long arg) if (system_supports_mte()) valid_mask |=3D PR_MTE_TCF_MASK | PR_MTE_TAG_MASK; =20 - if (arg & ~valid_mask) + if (flags & ~valid_mask) return -EINVAL; =20 + if (nr_bits) { + if (get_user(val, nr_bits)) + return -EFAULT; + if (val > TBI_TAG_BITS || val < 1) + return -EINVAL; + } + /* * Do not allow the enabling of the tagged address ABI if globally * disabled via sysctl abi.tagged_addr_disabled. */ - if (arg & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) + if (flags & PR_TAGGED_ADDR_ENABLE && tagged_addr_disabled) return -EINVAL; =20 - if (set_mte_ctrl(task, arg) !=3D 0) + if (set_mte_ctrl(task, flags) !=3D 0) return -EINVAL; =20 - update_ti_thread_flag(ti, TIF_TAGGED_ADDR, arg & PR_TAGGED_ADDR_ENABLE)= ; + if (flags & PR_TAGGED_ADDR_ENABLE) { + if (nr_bits && put_user(TBI_TAG_BITS, nr_bits)) + return -EFAULT; + if (offset && put_user(TBI_TAG_SHIFT, offset)) + return -EFAULT; + } + + update_ti_thread_flag(ti, TIF_TAGGED_ADDR, + flags & PR_TAGGED_ADDR_ENABLE); =20 return 0; } =20 -long get_tagged_addr_ctrl(struct task_struct *task) +long get_tagged_addr_ctrl(struct task_struct *task, + int __user *nr_bits, int __user *offset) { long ret =3D 0; struct thread_info *ti =3D task_thread_info(task); @@ -672,8 +694,17 @@ long get_tagged_addr_ctrl(struct task_struct *task) if (is_compat_thread(ti)) return -EINVAL; =20 - if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR)) + if (test_ti_thread_flag(ti, TIF_TAGGED_ADDR)) { ret =3D PR_TAGGED_ADDR_ENABLE; + if (nr_bits && put_user(TBI_TAG_BITS, nr_bits)) + return -EFAULT; + if (offset && put_user(TBI_TAG_SHIFT, offset)) + return -EFAULT; + } else { + /* Report maximum tag size */ + if (nr_bits && put_user(TBI_TAG_BITS, nr_bits)) + return -EFAULT; + } =20 ret |=3D get_mte_ctrl(task); =20 diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index f49b349e16a3..3010db7ef93e 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -1038,7 +1038,7 @@ static int tagged_addr_ctrl_get(struct task_struct = *target, const struct user_regset *regset, struct membuf to) { - long ctrl =3D get_tagged_addr_ctrl(target); + long ctrl =3D get_tagged_addr_ctrl(target, NULL, NULL); =20 if (IS_ERR_VALUE(ctrl)) return ctrl; @@ -1058,7 +1058,7 @@ static int tagged_addr_ctrl_set(struct task_struct = *target, const struct if (ret) return ret; =20 - return set_tagged_addr_ctrl(target, ctrl); + return set_tagged_addr_ctrl(target, ctrl, NULL, NULL); } #endif =20 diff --git a/kernel/sys.c b/kernel/sys.c index a730c03ee607..7e968d8331cc 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -120,10 +120,10 @@ # define PAC_RESET_KEYS(a, b) (-EINVAL) #endif #ifndef SET_TAGGED_ADDR_CTRL -# define SET_TAGGED_ADDR_CTRL(a) (-EINVAL) +# define SET_TAGGED_ADDR_CTRL(a, b, c) (-EINVAL) #endif #ifndef GET_TAGGED_ADDR_CTRL -# define GET_TAGGED_ADDR_CTRL() (-EINVAL) +# define GET_TAGGED_ADDR_CTRL(a, b) (-EINVAL) #endif =20 /* @@ -2498,14 +2498,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long= , arg2, unsigned long, arg3, error =3D PAC_RESET_KEYS(me, arg2); break; case PR_SET_TAGGED_ADDR_CTRL: - if (arg3 || arg4 || arg5) + if (arg5) return -EINVAL; - error =3D SET_TAGGED_ADDR_CTRL(arg2); + error =3D SET_TAGGED_ADDR_CTRL(arg2, (int __user *)arg3, + (int __user *)arg4); break; case PR_GET_TAGGED_ADDR_CTRL: - if (arg2 || arg3 || arg4 || arg5) + if (arg4 || arg5) return -EINVAL; - error =3D GET_TAGGED_ADDR_CTRL(); + error =3D GET_TAGGED_ADDR_CTRL((int __user *)arg2, + (int __user *)arg3); break; case PR_SET_IO_FLUSHER: if (!capable(CAP_SYS_RESOURCE)) diff --git a/tools/testing/selftests/arm64/tags/tags_test.c b/tools/testi= ng/selftests/arm64/tags/tags_test.c deleted file mode 100644 index 5701163460ef..000000000000 --- a/tools/testing/selftests/arm64/tags/tags_test.c +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -#include -#include -#include -#include -#include -#include - -#define SHIFT_TAG(tag) ((uint64_t)(tag) << 56) -#define SET_TAG(ptr, tag) (((uint64_t)(ptr) & ~SHIFT_TAG(0xff)) | \ - SHIFT_TAG(tag)) - -int main(void) -{ - static int tbi_enabled =3D 0; - unsigned long tag =3D 0; - struct utsname *ptr; - int err; - - if (prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0) =3D=3D= 0) - tbi_enabled =3D 1; - ptr =3D (struct utsname *)malloc(sizeof(*ptr)); - if (tbi_enabled) - tag =3D 0x42; - ptr =3D (struct utsname *)SET_TAG(ptr, tag); - err =3D uname(ptr); - free(ptr); - - return err; -} diff --git a/tools/testing/selftests/arm64/tags/.gitignore b/tools/testin= g/selftests/vm/tags/.gitignore similarity index 100% rename from tools/testing/selftests/arm64/tags/.gitignore rename to tools/testing/selftests/vm/tags/.gitignore diff --git a/tools/testing/selftests/arm64/tags/Makefile b/tools/testing/= selftests/vm/tags/Makefile similarity index 100% rename from tools/testing/selftests/arm64/tags/Makefile rename to tools/testing/selftests/vm/tags/Makefile diff --git a/tools/testing/selftests/arm64/tags/run_tags_test.sh b/tools/= testing/selftests/vm/tags/run_tags_test.sh similarity index 100% rename from tools/testing/selftests/arm64/tags/run_tags_test.sh rename to tools/testing/selftests/vm/tags/run_tags_test.sh diff --git a/tools/testing/selftests/vm/tags/tags_test.c b/tools/testing/= selftests/vm/tags/tags_test.c new file mode 100644 index 000000000000..ec10a409388d --- /dev/null +++ b/tools/testing/selftests/vm/tags/tags_test.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include + +static int tag_bits; +static int tag_offset; + +#define SHIFT_TAG(tag) ((uint64_t)(tag) << tag_offset) +#define SET_TAG(ptr, tag) (((uint64_t)(ptr) & ~SHIFT_TAG((1 << tag_bits)= - 1)) | SHIFT_TAG(tag)) + +static int max_tag_bits(void) +{ + int nr; + + if (prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0) < 0) + return 0; + + if (prctl(PR_GET_TAGGED_ADDR_CTRL, &nr, 0, 0) < 0) + return 8; /* Assume ARM TBI */ + + return nr; +} + +int main(void) +{ + static int tags_enabled =3D 0; + unsigned long tag =3D 0; + struct utsname *ptr; + int err; + + tag_bits =3D max_tag_bits(); + + if (tag_bits && !prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, + &tag_bits, &tag_offset, 0)) { + tags_enabled =3D 1; + } else if (tag_bits =3D=3D 8 && !prctl(PR_SET_TAGGED_ADDR_CTRL, + PR_TAGGED_ADDR_ENABLE, 0, 0)) { + /* ARM TBI with legacy interface*/ + tags_enabled =3D 1; + tag_offset =3D 56; + } + + ptr =3D (struct utsname *)malloc(sizeof(*ptr)); + if (tags_enabled) + tag =3D (1UL << tag_bits) - 1; + ptr =3D (struct utsname *)SET_TAG(ptr, tag); + err =3D uname(ptr); + printf("Sysname: %s\n", ptr->sysname); + free(ptr); + + return err; +} --=20 2.26.2