From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 719223E0080 for ; Tue, 26 May 2026 11:17:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779794251; cv=none; b=P7XPhsD7yWHbvyh8H7Y0chmpP8dNXawWYSjIWQQQjI/EPrFlqbE29ZKM6TF/GNjX7o8p8uP9hrGJR8jJjEkXy4dHwjMqYhBshEdI4mtMiTDD9jpaGDUU1TAcDY8sW3oxc7F4rGjVoWl6hpxJdPWyqTb173yRfsLrNZnBRzwNhmo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779794251; c=relaxed/simple; bh=t4PPHi7X24sfazkG9K2nWc4u6Ed6AjMWJ5H3R6VytlA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DGWyt4yhClCsrUspLiLXI6AjBLnGZON0KpIkL8KqESu6igFgpybYCnLH7OD0bjCx1jvy2jRpFBGiJg8eRNWkJR1VJESGcfFWQmL7WqLFKmwGA3HfZxqO3tAeLapKyoD78XA+q5KMlQmOUETfFs5yAkNlmh7H7lvXEIjzS8hGYIQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=raDh0PaK; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="raDh0PaK" 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 B43E116F2; Tue, 26 May 2026 04:17:23 -0700 (PDT) Received: from localhost.localdomain (e123572-lin.cambridge.arm.com [10.1.194.54]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E9CBA3F7D8; Tue, 26 May 2026 04:17:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1779794248; bh=t4PPHi7X24sfazkG9K2nWc4u6Ed6AjMWJ5H3R6VytlA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=raDh0PaK4/EH8L2R5toVA8l98hDbyGY6KysUl2a7vLw3yjxEusYRl42dGl/5DCm4H fdku1nNJa57rCCpKeiAvxFEqCwgOIsXfdnErKRIEYrZnYwzijYHrUQ8ouIxTNzYdXE +Q3mA9YtAx1g81mbLtYnqcOkRFtTmSt80SG5XMQo= From: Kevin Brodsky Date: Tue, 26 May 2026 12:15:55 +0100 Subject: [PATCH RFC v8 06/24] arm64: set_memory: Implement set_memory_pkey() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260526-kpkeys-v8-6-eaaacdacc67c@arm.com> References: <20260526-kpkeys-v8-0-eaaacdacc67c@arm.com> In-Reply-To: <20260526-kpkeys-v8-0-eaaacdacc67c@arm.com> To: linux-hardening@vger.kernel.org Cc: Kevin Brodsky , Andrew Morton , Andy Lutomirski , Catalin Marinas , Dave Hansen , "David Hildenbrand (Arm)" , Ira Weiny , Jann Horn , Jeff Xu , Joey Gouly , Kees Cook , Linus Walleij , Marc Zyngier , Mark Brown , Matthew Wilcox , Maxwell Bland , "Mike Rapoport (IBM)" , Peter Zijlstra , Pierre Langlois , Quentin Perret , Rick Edgecombe , Ryan Roberts , Vlastimil Babka , Will Deacon , Yang Shi , Yeoreum Yun , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, x86@kernel.org, Lorenzo Stoakes , Thomas Gleixner X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779794212; l=2094; i=kevin.brodsky@arm.com; s=20260427; h=from:subject:message-id; bh=t4PPHi7X24sfazkG9K2nWc4u6Ed6AjMWJ5H3R6VytlA=; b=e5pMjC0jaX+BcDIZo23dX28PhZonT653oh1aJ8TdP8ymaQgAXPkO7sJ4E+xvbGOq/yGNefzZc d2SEKZeN7WTD4oFUBpohE39uolgaJteMWSvQWN5dEbMUwzsx/pbOYA1 X-Developer-Key: i=kevin.brodsky@arm.com; a=ed25519; pk=N2QG+eJKrvkNovwhhwJhnJ4+ScVfsGCHldmqLfcMTFs= Implement set_memory_pkey() using POE. Signed-off-by: Kevin Brodsky --- arch/arm64/include/asm/set_memory.h | 4 ++++ arch/arm64/mm/pageattr.c | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/arch/arm64/include/asm/set_memory.h b/arch/arm64/include/asm/set_memory.h index 90f61b17275e..b6cd6de34abf 100644 --- a/arch/arm64/include/asm/set_memory.h +++ b/arch/arm64/include/asm/set_memory.h @@ -19,4 +19,8 @@ bool kernel_page_present(struct page *page); int set_memory_encrypted(unsigned long addr, int numpages); int set_memory_decrypted(unsigned long addr, int numpages); +#ifdef CONFIG_ARCH_HAS_KPKEYS +int set_memory_pkey(unsigned long addr, int numpages, int pkey); +#endif + #endif /* _ASM_ARM64_SET_MEMORY_H */ diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index ce035e1b4eaf..8f3519400bd1 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include @@ -365,6 +367,30 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) return set_memory_valid(addr, nr, valid); } +#ifdef CONFIG_ARCH_HAS_KPKEYS +int set_memory_pkey(unsigned long addr, int numpages, int pkey) +{ + unsigned long set_prot = 0; + + if (!kpkeys_enabled()) + return 0; + + if (!__is_lm_address(kasan_reset_tag((void *)addr))) + return -EINVAL; + + if (pkey < 0 || pkey >= arch_max_pkey()) + return -EINVAL; + + set_prot |= pkey & BIT(0) ? PTE_PO_IDX_0 : 0; + set_prot |= pkey & BIT(1) ? PTE_PO_IDX_1 : 0; + set_prot |= pkey & BIT(2) ? PTE_PO_IDX_2 : 0; + + return __change_memory_common(addr, PAGE_SIZE * numpages, + __pgprot(set_prot), + __pgprot(PTE_PO_IDX_MASK)); +} +#endif + #ifdef CONFIG_DEBUG_PAGEALLOC /* * This is - apart from the return value - doing the same -- 2.51.2