From patchwork Mon Dec 9 09:50:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Brodsky X-Patchwork-Id: 13899194 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 A829EE7717D for ; Mon, 9 Dec 2024 09:51:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F3CB8D0040; Mon, 9 Dec 2024 04:51:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 67B868D0029; Mon, 9 Dec 2024 04:51:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F4438D0040; Mon, 9 Dec 2024 04:51:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 24CA18D0029 for ; Mon, 9 Dec 2024 04:51:07 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D3E7E14027A for ; Mon, 9 Dec 2024 09:51:06 +0000 (UTC) X-FDA: 82874951754.16.2D44DDF Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 7077E40014 for ; Mon, 9 Dec 2024 09:50:46 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf11.hostedemail.com: domain of kevin.brodsky@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=kevin.brodsky@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733737856; 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; bh=Ikxvul1YEZ1JZMqMyeWN6QrFnKi6/EKYx8Mcx81qqDY=; b=2yNpop7aYFpzYyY7+4DnmbYpaa8gD0ce678pPSScOU6MEL5Y9D7/T1dd1tTPIEDKQRkOQ/ bJY6fEUimTqwbPq4SGeWrqufuFu4nahLTelX40ruiHP+pU4mzaXx0uRycDc85waRE7Nopm Y8HfxHTT8oPdqk/rTB2QHEECE6sDGZQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733737856; a=rsa-sha256; cv=none; b=jAqSQQo5kLiLWecZGhAV0iqv8vNmpt0Hs/7ahgxlsouHtvG6ldfkUNtLv13wOosuc66Q+e nLgppdemrg/4v/p94aV6DfWm/h15e3KJBEOprMklZUx22Fe7kZK7Stcjj5w/4brMfikESS 0vnMVhOlMyxDQhGvbG4dtkl6t8aetnc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf11.hostedemail.com: domain of kevin.brodsky@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=kevin.brodsky@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A82F6143D; Mon, 9 Dec 2024 01:51:32 -0800 (PST) Received: from e123572-lin.arm.com (e123572-lin.cambridge.arm.com [10.1.194.54]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9E7263F720; Mon, 9 Dec 2024 01:51:02 -0800 (PST) From: Kevin Brodsky To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Kevin Brodsky , akpm@linux-foundation.org, aruna.ramakrishna@oracle.com, catalin.marinas@arm.com, dave.hansen@linux.intel.com, joey.gouly@arm.com, keith.lucas@oracle.com, ryan.roberts@arm.com, shuah@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, x86@kernel.org Subject: [PATCH 11/14] selftests/mm: Use sys_pkey helpers consistently Date: Mon, 9 Dec 2024 09:50:16 +0000 Message-ID: <20241209095019.1732120-12-kevin.brodsky@arm.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241209095019.1732120-1-kevin.brodsky@arm.com> References: <20241209095019.1732120-1-kevin.brodsky@arm.com> MIME-Version: 1.0 X-Stat-Signature: eshu9hmcjc6816u8a519o8dgdq4nefbn X-Rspamd-Queue-Id: 7077E40014 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1733737846-282044 X-HE-Meta: U2FsdGVkX1+Ic0zHOxsSh0IdNZEZcsxYUduAiJzds/FFhOi7apQVEIGKbd0NwA3XEfiMJtHvG1Sfciuc6N3qM5hAGK696PVUIBoJUOofMur7T31PbEvLwz5J5JL/nm9kcq6W463J0HEYE+U24Sgo6XON1yBFppaoJjuOWPqpHiOeVoNzrrMbRpmjSkbMFo/k9iROwz0gyWI6O//KV0EDp4dsiIbcope/vNRWmBCbg+w5wrZlTavuUE4nHuDbcZENyJ8PiiXMDoDpbDhXsLGK/vRjTx3cYEEflYqVqbB+/l4VeCW6w9PD2d20N0FSMsRwsgAFk9dZTO4MKR0hlkw1ibyI+x34aYEPNpBagnqurXbp9UUKs4QN4821gjKqB4eGcdcWhD+J1+HYYVyg6bd+U8NNoJMNKZ3aB5hxsUqdx/AgVDpYaz0yj/w5VWj6sUW2XUYiG/dU+P8sozWBbpUbHAII9a/pflmOyuc+0yFrEh+rkcYND8CkUkJqXqgqACc5UbzHvRaVTcFgFeHOtJCt+jDpXyDWU28lKc6eBxD1qdUIiAvJDbsuES67gddzr1wibt4pkq/NGgYs1S3qGtwaCp35W/G/j4KVo089afOr+JpdC7DiQ528pUDtEP+1U965ovILiJVqYrIdmDakCXa78u/X+wuqBLCsD6UnCDgDl8W90y/Os9svgI4BJlSerk/P3WuYHF/LoNsvfDydyw3egQ0RobrZixPEGMrYgbrUloiW8uPiER5fDqPvMmBpNm01kouI6xFU63GUpb9Hk+LBDeOjYKZDcx3tfXDCu5UvN5iJxKef0bfSFlhUilVTQOOWDg4CKwhG9feGQhUkwf1GVcEz04ob3vyEycbsGU1M69DzJbrUocSMmObS4FUvb0zZXNIrSP1Ws54ZFc+sEavD15QMw+ArEFgPfqHZYthwKH+GTqXLMoYwnBqY8k8cIMllovnhe7DC9/DBg0gcYa5 xfuvZj6D 7nhSGK3vcOVCJhuoOmTe5uAX5xWP3DSOqU1G/cn/2ueE9U5uEj97eBmn33wXQ7PZp7MpLCIJxjL0bjVi2myMVPvd13TdFQC5d6/epsPdIkzz7feRFIDLTy4ptcbYwP8qm0K3RO5yLO3DsHI2hRaFKM3gmZd7Tetmq4XcKamzZRRLz12HD9Ne4yIEQKthlkphlZTYuEQei66J9492EQcen9vZ3IYqp53QnR5PZ3HYlhDqJK+CG4r6F7w5Nsc8wcV1h1N+eYFOYl9MuJmrHOLJLI/5e5jZlpvlidVsKIwEdqB4SSwLUUQp1yF6trISTW5SilGA3dzffgp12r0E= 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: sys_pkey_alloc, sys_pkey_free and sys_mprotect_pkey are currently used in protections_keys.c, while pkey_sighandler_tests.c calls the libc wrappers directly (e.g. pkey_mprotect()). This is probably ok when using glibc (those symbols appeared a while ago), but Musl does not currently provide them. The logging in the helpers from pkey-helpers.h can also come in handy. Make things more consistent by using the sys_pkey helpers in pkey_sighandler_tests.c too. To that end their implementation is moved to a common .c file (pkey_util.c). This also enables calling is_pkeys_supported() outside of protections_keys.c, since it relies on sys_pkey_{alloc,free}. Signed-off-by: Kevin Brodsky --- tools/testing/selftests/mm/Makefile | 4 +- tools/testing/selftests/mm/pkey-helpers.h | 2 + .../selftests/mm/pkey_sighandler_tests.c | 8 ++-- tools/testing/selftests/mm/pkey_util.c | 40 +++++++++++++++++++ tools/testing/selftests/mm/protection_keys.c | 35 ---------------- 5 files changed, 48 insertions(+), 41 deletions(-) create mode 100644 tools/testing/selftests/mm/pkey_util.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 814b17a43385..1f0743d9459d 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -147,8 +147,8 @@ TEST_FILES += write_hugetlb_memory.sh include ../lib.mk -$(TEST_GEN_PROGS): vm_util.c thp_settings.c -$(TEST_GEN_FILES): vm_util.c thp_settings.c +$(TEST_GEN_PROGS): vm_util.c thp_settings.c pkey_util.c +$(TEST_GEN_FILES): vm_util.c thp_settings.c pkey_util.c $(OUTPUT)/uffd-stress: uffd-common.c $(OUTPUT)/uffd-unit-tests: uffd-common.c diff --git a/tools/testing/selftests/mm/pkey-helpers.h b/tools/testing/selftests/mm/pkey-helpers.h index 6f0ab7b42738..f080e97b39be 100644 --- a/tools/testing/selftests/mm/pkey-helpers.h +++ b/tools/testing/selftests/mm/pkey-helpers.h @@ -89,6 +89,8 @@ extern void abort_hooks(void); int sys_pkey_alloc(unsigned long flags, unsigned long init_val); int sys_pkey_free(unsigned long pkey); +int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot, + unsigned long pkey); /* For functions called from protection_keys.c only */ noinline int read_ptr(int *ptr); diff --git a/tools/testing/selftests/mm/pkey_sighandler_tests.c b/tools/testing/selftests/mm/pkey_sighandler_tests.c index e1aaeb65cfae..c73cee192b88 100644 --- a/tools/testing/selftests/mm/pkey_sighandler_tests.c +++ b/tools/testing/selftests/mm/pkey_sighandler_tests.c @@ -311,8 +311,8 @@ static void test_sigsegv_handler_with_different_pkey_for_stack(void) __write_pkey_reg(pkey_reg); /* Protect the new stack with MPK 1 */ - pkey = pkey_alloc(0, 0); - pkey_mprotect(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey); + pkey = sys_pkey_alloc(0, 0); + sys_mprotect_pkey(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey); /* Set up alternate signal stack that will use the default MPK */ sigstack.ss_sp = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, @@ -484,8 +484,8 @@ static void test_pkru_sigreturn(void) __write_pkey_reg(pkey_reg); /* Protect the stack with MPK 2 */ - pkey = pkey_alloc(0, 0); - pkey_mprotect(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey); + pkey = sys_pkey_alloc(0, 0); + sys_mprotect_pkey(stack, STACK_SIZE, PROT_READ | PROT_WRITE, pkey); /* Set up alternate signal stack that will use the default MPK */ sigstack.ss_sp = mmap(0, STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, diff --git a/tools/testing/selftests/mm/pkey_util.c b/tools/testing/selftests/mm/pkey_util.c new file mode 100644 index 000000000000..ca4ad0d44ab2 --- /dev/null +++ b/tools/testing/selftests/mm/pkey_util.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +#include "pkey-helpers.h" + +int sys_pkey_alloc(unsigned long flags, unsigned long init_val) +{ + int ret = syscall(SYS_pkey_alloc, flags, init_val); + dprintf1("%s(flags=%lx, init_val=%lx) syscall ret: %d errno: %d\n", + __func__, flags, init_val, ret, errno); + return ret; +} + +int sys_pkey_free(unsigned long pkey) +{ + int ret = syscall(SYS_pkey_free, pkey); + dprintf1("%s(pkey=%ld) syscall ret: %d\n", __func__, pkey, ret); + return ret; +} + +int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot, + unsigned long pkey) +{ + int sret; + + dprintf2("%s(0x%p, %zx, prot=%lx, pkey=%lx)\n", __func__, + ptr, size, orig_prot, pkey); + + errno = 0; + sret = syscall(__NR_pkey_mprotect, ptr, size, orig_prot, pkey); + if (errno) { + dprintf2("SYS_mprotect_key sret: %d\n", sret); + dprintf2("SYS_mprotect_key prot: 0x%lx\n", orig_prot); + dprintf2("SYS_mprotect_key failed, errno: %d\n", errno); + if (DEBUG_LEVEL >= 2) + perror("SYS_mprotect_pkey"); + } + return sret; +} diff --git a/tools/testing/selftests/mm/protection_keys.c b/tools/testing/selftests/mm/protection_keys.c index f43cf3b75d8e..3688571e6b39 100644 --- a/tools/testing/selftests/mm/protection_keys.c +++ b/tools/testing/selftests/mm/protection_keys.c @@ -460,34 +460,6 @@ static pid_t fork_lazy_child(void) return forkret; } -int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot, - unsigned long pkey) -{ - int sret; - - dprintf2("%s(0x%p, %zx, prot=%lx, pkey=%lx)\n", __func__, - ptr, size, orig_prot, pkey); - - errno = 0; - sret = syscall(__NR_pkey_mprotect, ptr, size, orig_prot, pkey); - if (errno) { - dprintf2("SYS_mprotect_key sret: %d\n", sret); - dprintf2("SYS_mprotect_key prot: 0x%lx\n", orig_prot); - dprintf2("SYS_mprotect_key failed, errno: %d\n", errno); - if (DEBUG_LEVEL >= 2) - perror("SYS_mprotect_pkey"); - } - return sret; -} - -int sys_pkey_alloc(unsigned long flags, unsigned long init_val) -{ - int ret = syscall(SYS_pkey_alloc, flags, init_val); - dprintf1("%s(flags=%lx, init_val=%lx) syscall ret: %d errno: %d\n", - __func__, flags, init_val, ret, errno); - return ret; -} - static int alloc_pkey(void) { int ret; @@ -534,13 +506,6 @@ static int alloc_pkey(void) return ret; } -int sys_pkey_free(unsigned long pkey) -{ - int ret = syscall(SYS_pkey_free, pkey); - dprintf1("%s(pkey=%ld) syscall ret: %d\n", __func__, pkey, ret); - return ret; -} - /* * I had a bug where pkey bits could be set by mprotect() but * not cleared. This ensures we get lots of random bit sets