From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 659D4606A326 for ; Thu, 26 Nov 2026 11:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=M0F1dvXo Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4jZvC00yyMz2yNj; Thu, 26 Nov 2026 22:17:00 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2600:3c0a:e001:78e:0:1991:8:25" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1795691820; cv=none; b=Y4owM8zoc5IRaf1D0KGIqPMSW/C8AYL8rCokFGwIYiAVf30ywPfVRHBeYRXM9taAMYhG3hy6wKvSmJPPi5bfwNvbJM/gX78Fm/6iTBmdbroPgAalM+aDGgYeM2KLIBoITbqBjNKQoI9aWxbI7aPYQDo+e4PsGM5q6QWPP480nXCzHzz13l48xIeFaTyKbTHN6fOyyceE7Dm/vae8FRdYAwssYeA6J06sG2yOOzzwGKSPrgjd4PL/Uq83PW2dGSA1iAd2QgPhhQYmZabxz2nzpZVg5rHCM9S8Erl2oZ43Gzfb+ExjTZ9/qGI1whrDUDgxzsIsGq9jTnV5g1pnZzs2fg== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1795691820; c=relaxed/relaxed; bh=/joh9LYYtFAk508MPTeDmWNF1bv5NoqV23DPZyaFhMw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=A7TnM//1HSZs2VZrDhNCPtJ+eRHVNKfQHwjNAqo2jU2mCITQ2jaqvXhbESFH0z9vIMeE0VtlMsjxbxp8X0ik0fSt0nHHNBiQUHfLiBX/RstR19NVW34i/7QV1dg3JskIaBIWr1ypLzJzY0P55uP7py2JRIt9FwUu042vfToyM1CAOtnHOjF2/lTjZbFyLvTxURRNPoZtseCfbT13AidDAe4HN1HyhwE09ufFstDfg+2/6vHVrjCPfQiZQPo8cr0hDar60roB/m64MjgFMRjjuHVc54h+NNEUbfZbN8QPN0jDFPZ/yE136xWQUJwu3BF/YFaIRYeLkOJBkV9O3rqYjw== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=M0F1dvXo; dkim-atps=neutral; spf=pass (client-ip=2600:3c0a:e001:78e:0:1991:8:25; helo=sea.source.kernel.org; envelope-from=rppt@kernel.org; receiver=lists.ozlabs.org) smtp.mailfrom=kernel.org Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=M0F1dvXo; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=2600:3c0a:e001:78e:0:1991:8:25; helo=sea.source.kernel.org; envelope-from=rppt@kernel.org; receiver=lists.ozlabs.org) Received: from sea.source.kernel.org (sea.source.kernel.org [IPv6:2600:3c0a:e001:78e:0:1991:8:25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4jZvBz2KCPz2yMn for ; Thu, 26 Nov 2026 22:16:59 +1100 (AEDT) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 640EA4437B; Thu, 26 Nov 2026 11:16:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 446AF1F00A03; Thu, 26 Nov 2026 11:16:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795691816; bh=/joh9LYYtFAk508MPTeDmWNF1bv5NoqV23DPZyaFhMw=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=M0F1dvXoUExr4F9nBSfhMX1FI+ggjPL2FV636MxhU5/uw+s8CVJmcZrapiJ08aojR A9p9cNfVVScZf3tX+EhfPkX69q9GEC7HAXM7a2jKMPL4gxPJeDexttYgMtp5wVOCpH Ee+FNPGtArX7IUdZBaHnkcwkVuiqbwKol7/gqk7wdWS9iRplveTGVe0Ek+QZx3P3I6 QEilAO3kIEM3eKXEve6hi0oSYd/CyS938BDYh2HoedWMWjDCUhOE31ZFcyaV/jMfMT OV5FKVPdeZmXAygPKHVm+bGAD2WJxWCsWEjnXUGYRfqg0WhmqZmsG9jQZrhlBDHHWe rFoUqe6s11ckQ== From: "Mike Rapoport (Microsoft)" Date: Thu, 26 Nov 2026 13:14:28 +0200 Subject: [PATCH RFC v2 15/54] x86/mm/pat: look up the effective NX/RW bits in verify_rwx() X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , Precedence: list X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261126-generic-set-memory-v0-1-v2-15-723f1e33c3d8@kernel.org> References: <20261126-generic-set-memory-v0-1-v2-0-723f1e33c3d8@kernel.org> In-Reply-To: <20261126-generic-set-memory-v0-1-v2-0-723f1e33c3d8@kernel.org> To: linux-mm@kvack.org Cc: =?utf-8?q?Adrian_Barna=C5=9B?= , Alexander Gordeev , Andrew Morton , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Brendan Jackman , Catalin Marinas , Dave Hansen , David Hildenbrand , Gerald Schaefer , Heiko Carstens , Huacai Chen , Ingo Molnar , Kevin Brodsky , "Liam R. Howlett" , Lorenzo Stoakes , Madhavan Srinivasan , Michal Hocko , Mike Rapoport , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Russell King , Suren Baghdasaryan , Thomas Gleixner , Vasily Gorbik , Vlastimil Babka , Will Deacon , linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, x86@kernel.org X-Mailer: b4 0.16.1-dev X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Every CPA lookup collects the effective NX and RW bits of the upper page table levels. Only verify_rwx() needs them, and only when a mapping is about to become both writable and executable. Walk the page tables in verify_rwx() after its early exits and let the CPA lookups return just the entry and its level. Add cpa_pgd() that picks the page table of the request, to avoid code duplication. Such a lookup does not depend on the x86 page table format. It will move to the generic code together with the rest of CPA core logic. Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/mm/pat/set_memory.c | 51 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 9dfe69d608b8..8d481e9edf75 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -307,6 +307,14 @@ static inline unsigned long fix_addr(unsigned long addr) #endif } +static pgd_t *cpa_pgd(struct cpa_data *cpa, unsigned long address) +{ + if (!cpa->pgd) + return pgd_offset_k(address); + + return cpa->pgd + pgd_index(address); +} + static unsigned long __cpa_addr(struct cpa_data *cpa, unsigned long idx) { return cpa->addr + idx * PAGE_SIZE; @@ -688,11 +696,14 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long start, /* * Validate strict W^X semantics. */ -static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long start, - unsigned long pfn, unsigned long npg, - bool nx, bool rw) +static inline pgprot_t verify_rwx(struct cpa_data *cpa, pgprot_t old, + pgprot_t new, unsigned long start, + unsigned long pfn, unsigned long npg) { + unsigned int level; unsigned long end; + bool nx, rw; + pgd_t *pgd; /* * 32-bit has some unfixable W+X issues, like EFI code @@ -712,6 +723,13 @@ static inline pgprot_t verify_rwx(pgprot_t old, pgprot_t new, unsigned long star if ((pgprot_val(new) & (_PAGE_RW | _PAGE_NX)) != _PAGE_RW) return new; + /* + * Only a mapping about to become W+X pays for the second walk that + * collects the effective NX and RW bits of the upper levels. + */ + pgd = cpa_pgd(cpa, start); + lookup_address_in_pgd_attr(pgd, start, &level, &nx, &rw); + /* Non-leaf translation entries can disable writing or execution. */ if (!rw || nx) return new; @@ -825,16 +843,11 @@ pte_t *lookup_address(unsigned long address, unsigned int *level) EXPORT_SYMBOL_GPL(lookup_address); static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address, - unsigned int *level, bool *nx, bool *rw) + unsigned int *level) { - pgd_t *pgd; + pgd_t *pgd = cpa_pgd(cpa, address); - if (!cpa->pgd) - pgd = pgd_offset_k(address); - else - pgd = cpa->pgd + pgd_index(address); - - return lookup_address_in_pgd_attr(pgd, address, level, nx, rw); + return lookup_address_in_pgd(pgd, address, level); } /* @@ -966,7 +979,6 @@ static int should_split_large_page(pte_t *kpte, unsigned long address, pgprot_t old_prot, new_prot, req_prot, chk_prot; pte_t new_pte, *tmp; enum pg_level level; - bool nx, rw; if (cpa->flags & CPA_REQ_SPLIT) return 1; @@ -975,7 +987,7 @@ static int should_split_large_page(pte_t *kpte, unsigned long address, * Check for races, another CPU might have split this page * up already: */ - tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw); + tmp = _lookup_address_cpa(cpa, address, &level); if (tmp != kpte) return 1; @@ -1086,8 +1098,8 @@ static int should_split_large_page(pte_t *kpte, unsigned long address, new_prot = static_protections(req_prot, lpaddr, old_pfn, numpages, psize, CPA_DETECT); - new_prot = verify_rwx(old_prot, new_prot, lpaddr, old_pfn, numpages, - nx, rw); + new_prot = verify_rwx(cpa, old_prot, new_prot, lpaddr, old_pfn, + numpages); /* * If there is a conflict, split the large page. @@ -1153,14 +1165,13 @@ __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address, struct page *base = virt_to_page(pbase); unsigned int i, level; pgprot_t ref_prot; - bool nx, rw; pte_t *tmp; /* * Check for races, another CPU might have split this page * up for us already: */ - tmp = _lookup_address_cpa(cpa, address, &level, &nx, &rw); + tmp = _lookup_address_cpa(cpa, address, &level); if (tmp != kpte) return 1; @@ -1866,11 +1877,10 @@ static int __change_page_attr(struct cpa_data *cpa) int do_split, err; unsigned int level; pte_t *kpte, old_pte; - bool nx, rw; address = __cpa_addr(cpa, cpa->curpage); repeat: - kpte = _lookup_address_cpa(cpa, address, &level, &nx, &rw); + kpte = _lookup_address_cpa(cpa, address, &level); if (!kpte) return __cpa_process_fault(cpa, address); @@ -1892,8 +1902,7 @@ static int __change_page_attr(struct cpa_data *cpa) new_prot = static_protections(new_prot, address, pfn, 1, 0, CPA_PROTECT); - new_prot = verify_rwx(old_prot, new_prot, address, pfn, 1, - nx, rw); + new_prot = verify_rwx(cpa, old_prot, new_prot, address, pfn, 1); new_prot = pgprot_clear_protnone_bits(new_prot); -- 2.53.0