From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51BD1606A31A for ; Tue, 1 Dec 2026 15:08:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=tUYI+amZ; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=kPjndx/7 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=dTaB/lfnKvDQA9HdPQaWb4quIkvC3s2jxOaKawbYZ3w=; b=tUYI+amZySUM70D0TZ4Octw4ER mrIH7UEXrpcwKj18y1ZaAgyUp+LbUR8c6OSYGwzcOlJhz1U7woDQV6hlXloowgICiXDbT2k+fYNfR mDHKiPtFuJWhkxXN9gtESqbWDpnLeqE6M8wImyNm1Q1xL2HvSyR98ar5Y4IaOykdyfrcU5aGQip9Y GkSt/39pvGawW4Kqo0nrNfPKsLv/4QoLnt25ErRI4FUXwmqBPOGeLJxr3FhV1HyYG4oMRSGtprwNX 7wQMIK8/aOKGKz74Z+r366W4icvqixOG4ea1z1l0Sn79svW4picdSNItHaAIONBA660Iq2RCJrj8s NxZgJ9Tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPSp-00000008U55-1Pzn; Tue, 01 Dec 2026 15:08:15 +0000 Received: from mail-pl1-x645.google.com ([2607:f8b0:4864:20::645]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYPSn-00000008U1z-0Tpg for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 15:08:14 +0000 Received: by mail-pl1-x645.google.com with SMTP id d9443c01a7336-2f04561ee04so68980255ad.1 for ; Tue, 01 Dec 2026 07:08:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137692; x=1796742492; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=dTaB/lfnKvDQA9HdPQaWb4quIkvC3s2jxOaKawbYZ3w=; b=kPjndx/7NvyjO1gErPbnxVZxGBUMfHxiYhTiGESxwdGCXZKcWKsLl5OUl8wxojkBac /E0P9Ndo2Y9wuOXrmN2vHSSN79qfFEUPzaFywd26LoOK1tSQB6VDyQ+yfAkwhAW/kNiX 8DXlUmMkKeioOCrzeiBcNSVQUPssZAXTCHkrYKVT3inLeeNMJev7IDqllfE3IW0V6rPP Nh3sCrpN23UtErw970rluPZZGt4dcXYwj1A3WoSkXJnp36E+6FvQGwa8/pxi43I1kUPO 3EF8V4CN+OiRfCN6ZoZ1JbvaxLV1Y+yEck6MT8kkIVsuUoYRwQZYmcCkAlCh0O6cJtSH hRvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137692; x=1796742492; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=dTaB/lfnKvDQA9HdPQaWb4quIkvC3s2jxOaKawbYZ3w=; b=iPrAQcg4nC3YgHll1lNM43xXUTXYxh67X14h4OnTZgyTqt3jdtUTZsXf8WMSC3UVGI kRBYCSClFvUlVVTA4vO+Y0Qn5y84u8FA5dMza83XxSxz7WBNQ5epZMNDFDUYZCIIu0V+ 6M4w2hQ10BFIRn+vpcPq9AImq02zq8ac+SxWNnebNJFh1fDiJbFP7rQP/yFOsnsJVeSJ m1z2O7Vpfl8Ru6THUxyliQso3KC6RZkVsE2sHul6uFBFNLEGKbiuMtuhBW6nMNu2P3/P ShkYNDKLQxmKrRfExXo7WR8y3QVLwVeOyA1aplTwG8ssxef4i65DzawApaN04cwW+WAH 7eWQ== X-Forwarded-Encrypted: i=1; AL+hErF6NGFTBoe+Bb9sV6FjcJOh6o2sRfpFCmWm6ZdWd6yePMsO+wMN9byz8vcvkzaLgrUMWrVOgf9y9jyoZQG9cAJ3@lists.infradead.org X-Gm-Message-State: APuU4Z+rPsgRlXtwbPcuxOJdePWc8N7wDklUD790RhAPJDlq+IycaNPn eQ9P+jNzzBCYDxUq5TIP1LVOLbAB6qDW9kCNOcK7SDyEDUGH72jFR23bk53hK1Io4NztXrw/lus poqkJjVUaydrMVq5c2c0s+g== X-Received: from plho3.prod.google.com ([2002:a17:903:23c3:b0:2f0:3f10:25fd]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3910:b0:2f1:2d37:6e93 with SMTP id d9443c01a7336-2f12d376fd6mr120263225ad.45.1796137691472; Tue, 01 Dec 2026 07:08:11 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:46 +0000 In-Reply-To: <20261201150801.2239894-1-jthoughton@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261201150801.2239894-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201150801.2239894-5-jthoughton@google.com> Subject: [PATCH v4 04/18] hugetlb_vmemmap: Use vmemmap_update_pte to update in-use PTEs From: James Houghton To: Will Deacon , Catalin Marinas , Muchun Song , Oscar Salvador , Andrew Morton Cc: Kefeng Wang , Nikos Nikoleris , Linu Cherian , Mark Rutland , David Hildenbrand , Ryan Roberts , Nanyong Sun , Yu Zhao , Frank van der Linden , David Rientjes , James Houghton , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_070813_180234_7C1999B3 X-CRM114-Status: GOOD ( 23.33 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable 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 set_pte_at() cannot be used to replace in-use vmemmap PTEs on arm64, so replace it with a more specific routine, vmemmap_update_pte(). vmemmap_update_pte() is used for modifying page table entries such that there is a guarantee that no fault will be taken. The generic implementation still uses set_pte_at(). Architectures that cannot safely update in-use vmemmap PTEs with it must override vmemmap_update_pte(). Signed-off-by: James Houghton --- include/linux/pgtable.h | 23 +++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 35 +++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index bc1353f03b08..13edae56c7e0 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -458,6 +458,29 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr, #endif #define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) +#ifndef vmemmap_update_pte +/* + * vmemmap_update_pte - Remap PTEs used by the vmemmap. + * @addr: Base address of the remapped PTE. + * @ptep: Page table pointer to be overwritten. + * @pte: Page table entry to write. + * + * This function is only to be used to update PTEs that map the vmemmap. The + * only valid transitions supported by this function are: leaf-level + * (PAGE_SIZE), valid-to-valid. The pfn and prot bits may be changed. + * + * Implementations of this function must ensure that, while the update is taking + * place, CPUs will not fault on the remapped virtual address. Architectures on + * which set_pte_at() does not guarantee this must override this function. + */ +static inline int vmemmap_update_pte(unsigned long addr, pte_t *ptep, + pte_t pte) +{ + set_pte_at(&init_mm, addr, ptep, pte); + return 0; +} +#endif + #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep, diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index deafbe9b8141..8ec80f450123 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -220,15 +220,19 @@ static int alloc_vmemmap_page_list(struct list_head *head, return -ENOMEM; } +/* + * Copy @src into the first page on @vmemmap_pages and return it. The page is + * left on the list; the caller must remove it only once it has successfully + * been installed in the vmemmap. + */ static struct page *vmemmap_dup_page_from_list(const struct page *src, struct list_head *vmemmap_pages) { struct page *dst = list_first_entry(vmemmap_pages, struct page, lru); - list_del(&dst->lru); copy_page(page_address(dst), page_address(src)); - /* Ensure updates to @dst page are visible before set_pte_at(). */ + /* Ensure updates to @dst page are visible before vmemmap_update_pte(). */ smp_wmb(); return dst; @@ -250,6 +254,8 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr, const struct page *page = (const struct page *)addr; const struct folio *folio = page_folio(page); struct page *src = pte_page(ptep_get(pte)), *dst; + bool dup = false; + int ret; if (likely(vmemmap_optimizable_pfn(page_to_pfn(page), folio_order(folio)))) { dst = folio_vmemmap_shared_tail_page(folio); @@ -257,16 +263,27 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr, return -ENOMEM; } else { dst = vmemmap_dup_page_from_list(src, walk->vmemmap_pages); + dup = true; } - list_add(&src->lru, walk->vmemmap_pages); /* * Map the shared tail page writable intentionally. A lockless get that * read a non-zero refcount before the remap can still do its cmpxchg * after it. The refcount is zero, so the cmpxchg fails and leaves the * page unchanged. */ - set_pte_at(&init_mm, addr, pte, mk_pte(dst, PAGE_KERNEL)); + ret = vmemmap_update_pte(addr, pte, mk_pte(dst, PAGE_KERNEL)); + if (ret) + return ret; + + /* + * We successfully overwrote the vmemmap PTE, so the page that was just + * unmapped can be freed, and if we mapped a duplicated page, remove it + * from the list so that it doesn't get freed later. + */ + if (dup) + list_del(&dst->lru); + list_add(&src->lru, walk->vmemmap_pages); return 0; } @@ -277,6 +294,7 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr, const struct page *page = (const struct page *)addr; const struct folio *folio = page_folio(page); struct page *src = pte_page(ptep_get(pte)), *dst, *tail; + int ret; if (unlikely(!vmemmap_optimizable_pfn(page_to_pfn(page), folio_order(folio)))) return 0; @@ -293,7 +311,11 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr, return 0; dst = vmemmap_dup_page_from_list(src, walk->vmemmap_pages); - set_pte_at(&init_mm, addr, pte, mk_pte(dst, PAGE_KERNEL)); + ret = vmemmap_update_pte(addr, pte, mk_pte(dst, PAGE_KERNEL)); + if (ret) + return ret; + + list_del(&dst->lru); return 0; } @@ -499,7 +521,8 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h, list_move(&folio->lru, non_hvo_folios); } - if (restored) + /* A failed restore may have remapped part of the folio. */ + if (restored || ret) flush_tlb_all(); if (!ret) ret = restored; -- 2.56.1.839.g22a8290db9-goog