From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137692; b=L6yOyvsiMH5PqckxqGQnTJnSJ2VYs9sFHCgx8B6YXN4rMwEJpdzUr7vSwIdZzyxN/0EY KCwgTTl1PreHyO9y5xLa0G2MKmnminP5uzaB9GRthkyMF8aIR3r+yUobsKDNxlisrlMNB L1RJJvzJUzYYkZ62KiyV5+dnb/w3imQhC/MBcYFKx9f6PxKXOQL5lCORuZsj8llBfOO6u +8InjuALfC2VRqs6ruQbqTgcsmJAIFzDLlNc9+6OJL0IIxa/LB+2Zl3qSXOuX7U/8RvVC UyKq2z38W39i1uL/4tpSCaHZ++T0g/QO4JXQeCffeM9CIhWTKrUbIIglQwuSykXu4rg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137692; 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=gj+jozYrTzzF5qs/vUxwXgYeGkeqmEb0VemseM0j9zA=; b=ILrXiSRuWykcrwHccPljRzqY6JVREE32kaZhWzvHNHm9cp6KgGc+zdhMsFU66h6BKzsA xUnSVU9Vm2FnUKBYvkSaL//4Coe1xSkVKEoLznsP9S0CQzV1iRAho4fJXio3amkoGjFev +ZuJo/+HohHjcx5NZ4XC4yKnYZhAqqnLWBYclyjSVK3nJ4lJosA3TibssmT5oC7pLGojn yAy5zjiYohewPJaAE+o+6QiavcNJBsNhZm6lzSmqYNaEJoTkLCy6HvDTMFVc9u84KJ7yi t7nfQr/1FAo3WJS3zWJwOqnivAC2hpiWQzAZ8Iw0O6+HpizyaVFGgySeuBejRsfX6fw== 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=ofZwcSXJ; arc=none smtp.remote-ip=209.85.215.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 BE2C6606A33E for ; Tue, 1 Dec 2026 15:08:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BE2C6606A33E 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=ofZwcSXJ Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cdd9fa2a558so9498957a12.0 for ; Tue, 01 Dec 2026 07:08:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137691; x=1796742491; 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=gj+jozYrTzzF5qs/vUxwXgYeGkeqmEb0VemseM0j9zA=; b=ofZwcSXJYgSqC/tVuDFpFdAeUR/EMwP4JwbXLLL2IfuSa+yZcIi0Mj5mOiAdHpC3q/ StOiMcp/zbmDLPBetyr8oxapox55o8GoW7O0nWxy/Qgo0GeByUaJp8ZccbkwbWXBJ9dR zKN1RfX5LtbD7RIdyLM4kF31KuO+SsAc/P4qn8RpI45WfS4w0d0EjfdPLCJVSSQQU6be A1WcgEyZZVhNbnRJY4RjIfRVAQszsg+Ww2b+ElRX2TXqPWwuLtZmE0FKlp+Ya3rZPL22 a7/24Hn6FJZgmU2j5EqzVvYbYqmnvAGIdXIV80yqgrW7ZrSrCIwwJVzkP9VOMXa0lYhg YYkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137691; x=1796742491; 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=gj+jozYrTzzF5qs/vUxwXgYeGkeqmEb0VemseM0j9zA=; b=y9/+4Djcs2KeOrE1s1VntiDG4erTBOGvwIGG48MmzOVs+kXKITsOe0EqgpHxc184Lf GhbihqqRg7vJCICJKx6yScur61KHZhESn/r7TpeuGZxDNg80YugKu/+qnCgaGTcmO1+A A4ObVTxxWgnMnfAFir9W8Qa3Qh+ALS13Cr03K+qL3cg9Ide1L8CmKehddmTLzdm/HjuO IzPconGS5IO2Yadw+EWjOf8BWuEc/25nzEH/SGJJyzLAZQFlk4vx2nglXs54qL8cM9Oh ADeywOBZHRdaAAm5AglzpUO43NWXOQsCWgypywPl8kL5MYrajVUwRLI3H6bhslN4DaE8 /WPw== X-Forwarded-Encrypted: i=1; AL+hErFuHjFg/hZQXp252ETbZ7AMMBR6zNAttIzrYJj6U2LoyQTHqLTuCVLg5L1jIfXlldbju/z4ShHOGCnkN0Y=@vger.kernel.org X-Gm-Message-State: APuU4Z9bjcsuMoz9fGttutivE+R1NfVCRCc5Egb4FK+bnu5H2oyPBX7g xb+0YLBRu78zx8v0OBOl7oZgPDXWpjPiN39Q03QdoNguE5JCiS92DQOc4CGz5vqZhuTQZue3yFH qM/bgmkrur0hunqD6SrRzxQ== X-Received: from pgak15.prod.google.com ([2002:a05:6a02:674f:b0:cc7:9d5b:c2ac]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:8210:b0:3e2:6b5a:fea3 with SMTP id adf61e73a8af0-4014d456becmr42486957637.71.1796137690665; Tue, 01 Dec 2026 07:08:10 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:45 +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-4-jthoughton@google.com> Subject: [PATCH v4 03/18] hugetlb_vmemmap: Leave pages partially HVOed upon restore failure 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" When vmemmap_optimize_folio() fails partway through, it rolls back the PTEs it has already remapped with vmemmap_restore_range(). Right now it is assumed that this rollback will not fail; that is, if we fail to fully optimize a folio, it is assumed that restoring the folio's vmemmap is guaranteed to succeed. Although vmemmap restoration may never fail in practice, properly handle such failure cases now. This will leave folios in a partially HVOed state. In a follow-up patch, failure certainly becomes possible. In the event we end up with partially-HVOed folios, make sure to: 1. Leave the HVO page flag in place. vmemmap_optimize_folio() now returns VMEMMAP_REMAP_INCOMPLETE in this case. 2. Free any replacement vmemmap pages that vmemmap_restore_folio() did not consume. Because a folio may now be only partially optimized, vmemmap_restore_pte() only restores PTEs that still map the shared tail page (as returned by folio_vmemmap_shared_tail_page()), instead of warning about PTEs that map anything else. Signed-off-by: James Houghton --- mm/hugetlb_vmemmap.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index a4727558cc57..deafbe9b8141 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -276,12 +276,22 @@ 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; + struct page *src = pte_page(ptep_get(pte)), *dst, *tail; if (unlikely(!vmemmap_optimizable_pfn(page_to_pfn(page), folio_order(folio)))) return 0; - VM_WARN_ON_ONCE(folio_vmemmap_shared_tail_page(folio) != src); + tail = folio_vmemmap_shared_tail_page(folio); + if (WARN_ON_ONCE(!tail)) + return -EINVAL; + + /* + * The folio may be only partially optimized, so restore only PTEs + * currently pointing at the shared tail page. + */ + if (src != tail) + return 0; + dst = vmemmap_dup_page_from_list(src, walk->vmemmap_pages); set_pte_at(&init_mm, addr, pte, mk_pte(dst, PAGE_KERNEL)); @@ -327,6 +337,7 @@ static int vmemmap_pteify_folio(const struct folio *folio, unsigned long flags) return vmemmap_remap_range(start, end, &walk); } +#define VMEMMAP_REMAP_INCOMPLETE 1 /** * vmemmap_optimize_folio - optimize the vmemmap backing of a folio * @folio: folio whose vmemmap pages should be optimized. @@ -342,7 +353,8 @@ static int vmemmap_pteify_folio(const struct folio *folio, unsigned long flags) * Note: The caller must hold vmemmap_remap_rwsem. If TLB flushing is deferred, * the lock must remain held until the final TLB flush. * - * Return: %0 on success, negative error code otherwise. + * Return: %0 on success, VMEMMAP_REMAP_INCOMPLETE if the folio is incompletely + * optimized, negative error code otherwise. */ static int vmemmap_optimize_folio(const struct folio *folio, struct list_head *vmemmap_pages, unsigned long flags) @@ -374,7 +386,8 @@ static int vmemmap_optimize_folio(const struct folio *folio, return ret; end = start + walk.nr_walked * PAGE_SIZE; - vmemmap_restore_range(start, end, vmemmap_pages, 0); + if (vmemmap_restore_range(start, end, vmemmap_pages, 0)) + return VMEMMAP_REMAP_INCOMPLETE; return ret; } @@ -404,7 +417,12 @@ static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags) return ret; guard(rwsem_read)(&vmemmap_remap_rwsem); - return vmemmap_restore_range(start, end, &vmemmap_pages, flags); + ret = vmemmap_restore_range(start, end, &vmemmap_pages, flags); + + /* Not all pages may have been consumed */ + free_vmemmap_page_list(&vmemmap_pages); + + return ret; } static bool vmemmap_optimize_enabled = IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON); @@ -528,7 +546,11 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h, */ folio_set_hugetlb_vmemmap_optimized(folio); ret = vmemmap_optimize_folio(folio, vmemmap_pages, flags); - if (ret) + /* + * If ret == VMEMMAP_REMAP_INCOMPLETE, the folio might be partially + * HVOed. Leave the HVO page folio flag in place. + */ + if (ret < 0) folio_clear_hugetlb_vmemmap_optimized(folio); return ret; -- 2.56.1.839.g22a8290db9-goog