From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137693; b=o/IsTOhhdLCk4Y7hYOdFldL0H3OprmceyRzjg/UQYt4IirSvWnJm7H/FuNtDOM6sTBZ6 R+eJb1JJsGSseCL2HsUsiEPERQr3CqZRizj9j7xrOo7zOSrmvxaMTI+uO81t826hITUhj DsdFMcPqROGThsaxyMcxIIPFgfcK7IFN8HJg4rf+901MsWJuWbMnSiTDHYnjKVl5xvsr4 1rSVbStNc4T1VJYEfq6KzTV/OLLP3IIOvmSX3Hi4HrSvEH2p9iwFsp/lK8zfQyWFnui/R +nuBkB85L1jUknZSOg7UkbdJso7UQBErRZY+GRwLn25Ci9cwfhaVIOZAPYl4Y/dgE9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137693; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=dTaB/lfnKvDQA9HdPQaWb4quIkvC3s2jxOaKawbYZ3w=; b=D4JSm2KvPtTCN4VlQ+ZmS8iFa2N7/r5FloCJDSUVfqF8gUCxtKNtyWSO1C0lKKzZzEIc xFh1P3GUXd7YoNgxUaEfzLQwlLAeHaTQmkG+20Yl3JUuGQ5JjzIAGdvM4wq8iK9L8HHKL 2n/CPINplbSPwiunUU4nQ2Gmiq+XbbLu87bOwQgNgAXfiQMMCouMYmzAMui4UoNxRXAAm sDWhve1OrAY3jigxPj84ud5lDfuvTc3vQcB6U+Qw3PYu2hAo9AX35eZdFKHbcBU0Ihewh sd+c74AEiwHVRfrwMLC+AVrsjiRxnILDJc4UQ+cXCQiD+tT+E3OiHDLDjFwO8gD2A8Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=UqLuOo1d; arc=none smtp.remote-ip=209.85.214.197 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95F44606A691 for ; Tue, 1 Dec 2026 15:08:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 95F44606A691 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=UqLuOo1d Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2f074b87307so84369755ad.2 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=vger.kernel.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=UqLuOo1dyLK8qFq+dEn0HS7Y700zmbrl3pUCvxSTrDp6CDTPq8b2/jR6rCR8m2FfBU ISYqhb3wQJ2ww+VkvA+1YchMeSPJKOy1FyDzTiXMyUd0Pf19KxTnK/QrRupKnjhrwPop k5UMyGnI2K1f/YJyna+DZWL0eOfOjDI9qq6x739k+XMJy84zkjVPRoQt8urGQGznU+9N jlvJau3GvMt7gL7eq8KG2AkmAGLvQ3XAofl2XJcS8S26VydwN7YPGnaKAq0wVwG02APA xvvko6LL2Ma2acZxv6F3Hd/FNQx+pkd6wUEtZj6GDUq9TUao1fo2Utqe8kOJaQqey2Mo C9FQ== 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=FqiqoquyqkxwxHiAPdwgIPKFuY3aMUxQESofWft23qswiVsXuIXLXkzjOBCrvS4XvF 2UTuhU9tCwoA58s9XgHpNaXZMM8giFjkcACVSFKduuSPrHjxFx7XgwoTqNmisu4GWnGO QXYfGN7euUwrSIhd+dvIPokxzYG4Gpk2vP5UFSqokcxqevj/Uh5DFvEIRpH3/FXiAXsk CoYLPrRhX7lPu0FXyhgxrabvD00DIIPOuekh5v3VMKbu2pItu0II17fpfAkl0s+QHLp5 eR1EZBqHn3AUUYgrvHk1A0OmpDiUFmzqiD+VLoIUl6rzqq8X05xn2T0FH1yBRfrZh8mW dBbQ== X-Forwarded-Encrypted: i=1; AL+hErGbSTOySCWnZCjAS5EFX3kCUdqpVpXBo9p3l+xmK7IKQ7gxjdUd0zlTsdM8ErUL2d2O7bpyAf1nPkucLU8=@vger.kernel.org X-Gm-Message-State: APuU4Z/PG06VTsDbAH0piyXcUZxcrX72P1PrryToaHJZkJ/sunhTzQCr ttk0lDY+SgXyQ/6iCbeZ4b+lulosGif2ml6Jd9Daf8j6O31uqG4T+vXo8Cmo78HbEp5rGmGiYQp BEnzPNCHUAP2NR6G876UKVg== 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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" 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