From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A445C148 for ; Fri, 19 Dec 2025 00:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766102888; cv=none; b=kO0Om3uw2Lg8voiP4RNQfwtw2T6UgLROv0kDjoxNUKMr9RQrsX34lxHyqQIApsAN8EMCwcvD3Yfv6f72bfSp7ol94C+gTedvex+PefmBGqxYPH9f9TpmJmZ80FDVlNoCdVRsMcwsvHBeYA6Od/J6YWagnh/AbCFJwCOcIcUpC0s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766102888; c=relaxed/simple; bh=eytLIDw9fHkW6lFwRZt5WNAdmEaPb7coYCS5ZDCVoDw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lcaSqq0sl96YcOIivgmIlxDRNjDGYKs6BqqpCf3L2a8IcmeWQub/ioJO6m0dM7laaCdczjRjkNwECJ2a240x0GfBEpGJqWGcel9DhZi6+TSk5Yi/fvhZCB5gJDyqMmDxGhbHlqDiLHJ4p7wc4x3wQ1jb0je9MfRnhRKvvGp7r+U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=c1yDT/Af; arc=none smtp.client-ip=209.85.208.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c1yDT/Af" Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-647a3bca834so1833303a12.2 for ; Thu, 18 Dec 2025 16:08:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766102883; x=1766707683; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=XCAP50irZ/VU2CViej2qFkT1BTEl/kl/JteGqqPW+cs=; b=c1yDT/AfAurbUlUxTVCLwypp+c0R8A2QTnd6kLP3w8DvhAUxBzSR+kpL5fCtgH1U3W stRVGO0F3aLQRWILBxc53jcItgkr/0osY1UbEZVjjoLMl3z6Yv724upe7NYcGkwIfSji jQoFBusWI14jMkVdFvNdou1fs3PMH7NjNCodXBEmehhBdxMYcB3QB6vpWtkdq1z8Gl9B Fw5vbYfXgFidgUMST+sXpQ3xzLXYAa/xsWLJ8bcNXMxqMDTN4NzP4AycwgwN1n/ZOx3z ByR7irwTVUZScudOzwCUOxjF4fdm8s0ex/mZgF/ryel8Y7Wur1Kd1/zhAdqoQs27l1mR FT0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766102883; x=1766707683; h=user-agent:in-reply-to:content-disposition:mime-version:references :reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XCAP50irZ/VU2CViej2qFkT1BTEl/kl/JteGqqPW+cs=; b=hr1gSreWOLbszjhOZb0t6w4FsTAm2pGBw5RO/zDdSULLTl6o7alx1spaipoaz2979L TK/HAs76q+nPmlkEU3lSX+uVFy9T8k3KGOUY8Oc2+L+2Wj2yEG35U5HaDBknHauA1s0v NXjCHCvvpxVpidwJo6unicrrGQ5S0M7jcZW2oLH4AvXIP1N1hHHEFCcyBfHuvQqF0fnV R50ZYRHSykSQtNxeF22PZcqBqWgXUTX89riPSSB4cQBqIDBkoy6+4RnHSv4p9bEGN1FE MNcvfGvY/4tV+J70P2SgMftWvs6wJPT7D5d/jMehw0pojqW7Eto0X9Hc4+0QsV4TNknp 3FDg== X-Forwarded-Encrypted: i=1; AJvYcCW6bH7GncRGVBnPsVDRcxSXzyRICzfRjjVny0IZjTgnBWfCAiLktpmj4FzXkJZwkX2oV4kDUb9CoOZv/2o=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0IfXD6iEtbVjVtMgoO8kr13OCrioh5mTRO7HEAgYeAvDTmPCv huHRG7ZLfhwvZTcXsVBxtkZS7APLeZIceztfoLzg8+YaFImyP+Bwug72 X-Gm-Gg: AY/fxX6cy3LHNNcy6I785nrbMyb6JFCja3VzdviaWwFmvRGceyzBhGCMZl2QEV8w/G8 pE6ozJns4OVZHQzcRlaYh/+6rQ+rnMbF/OjGkAMptQ+yY3RGL4App0/2rqmwxv+ezW9gxBEix7M iwR1Cm/MSPMJAtELnjx/xkMYkrCcGcGufBSwHWPaNNsdbYUsbsq0i8Qd6+BJZFbRvnuNRDU8C00 a3r8IK9BRR5AEDbfpWGgXeivZwH1u3DPdGzd75+FwYc2/8ajq+cgS2k/tM0YSb1Ga74V0i7t6Kj 09+MhnIPwL4y+Xnauj0n+dtofXZ5Z9InrX8G0z1gPfJdnWESjk/XEah46c6KXGst51yslCj1mz1 sYDoswdhlddu0tv19ns72ShVDpZnpUv9QeTx17FPBou+vTBRdA5MhlQVTwWpXTSmwTCzEicUgUK TvHx7engxr0g== X-Google-Smtp-Source: AGHT+IG5XujdJGl2wz4kIxzCrUSWspKCEBh/ORj6MONC6VI1zBsgVlXgDRFf1dkzETmR/VTE4zcmXg== X-Received: by 2002:a17:907:970f:b0:b80:3445:f4be with SMTP id a640c23a62f3a-b80371744d4mr114740266b.38.1766102882298; Thu, 18 Dec 2025 16:08:02 -0800 (PST) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b8037f089fesm71913266b.46.2025.12.18.16.08.01 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 18 Dec 2025 16:08:01 -0800 (PST) Date: Fri, 19 Dec 2025 00:08:00 +0000 From: Wei Yang To: Gregory Price Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, akpm@linux-foundation.org, vbabka@suse.cz, surenb@google.com, mhocko@suse.com, jackmanb@google.com, hannes@cmpxchg.org, ziy@nvidia.com, richard.weiyang@gmail.com, osalvador@suse.de, rientjes@google.com, david@redhat.com, joshua.hahnjy@gmail.com, fvdl@google.com Subject: Re: [PATCH v6] page_alloc: allow migration of smaller hugepages during contig_alloc Message-ID: <20251219000800.tnpqzvcdyeqcwryt@master> Reply-To: Wei Yang References: <20251218233804.1395835-1-gourry@gourry.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251218233804.1395835-1-gourry@gourry.net> User-Agent: NeoMutt/20170113 (1.7.2) On Thu, Dec 18, 2025 at 06:38:04PM -0500, Gregory Price wrote: >We presently skip regions with hugepages entirely when trying to do >contiguous page allocation. This will cause otherwise-movable >2MB HugeTLB pages to be considered unmovable, and makes 1GB gigantic >page allocation less reliable on systems utilizing both. > >Commit 4d73ba5fa710 ("mm: page_alloc: skip regions with hugetlbfs pages >when allocating 1G pages") skipped all HugePage containing regions >because it can cause significant delays in 1G allocation (as HugeTLB >migrations may fail for a number of reasons). > >Instead, if hugepage migration is enabled, consider regions with >hugepages smaller than the target contiguous allocation request >as valid targets for allocation. > >We optimize for the existing behavior by searching for non-hugetlb >regions in a first pass, then retrying the search to include hugetlb >only on failure. This allows the existing fast-path to remain the >default case with a slow-path fallback to increase reliability. > >We only fallback to the slow path if a hugetlb region was detected, >and we do a full re-scan because the zones/blocks may have changed >during the first pass (and it's not worth further complexity). > >isolate_migrate_pages_block() has similar hugetlb filter logic, and >the hugetlb code does a migratable check in folio_isolate_hugetlb() >during isolation. The code servicing the allocation and migration >already supports this exact use case. > >To test, allocate a bunch of 2MB HugeTLB pages (in this case 48GB) >and then attempt to allocate some 1G HugeTLB pages (in this case 4GB) >(Scale to your machine's memory capacity). > >echo 24576 > .../hugepages-2048kB/nr_hugepages >echo 4 > .../hugepages-1048576kB/nr_hugepages > >Prior to this patch, the 1GB page reservation can fail if no contiguous >1GB pages remain. After this patch, the kernel will try to move 2MB >pages and successfully allocate the 1GB pages (assuming overall >sufficient memory is available). Also tested this while a program had >the 2MB reservations mapped, and the 1GB reservation still succeeds. > >folio_alloc_gigantic() is the primary user of alloc_contig_pages(), >other users are debug or init-time allocations and largely unaffected. >- ppc/memtrace is a debugfs interface >- x86/tdx memory allocation occurs once on module-init >- kfence/core happens once on module (late) init >- THP uses it in debug_vm_pgtable_alloc_huge_page at __init time > >Suggested-by: David Hildenbrand >Link: https://lore.kernel.org/linux-mm/6fe3562d-49b2-4975-aa86-e139c535ad00@redhat.com/ >Signed-off-by: Gregory Price >--- > mm/page_alloc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++---- > 1 file changed, 48 insertions(+), 4 deletions(-) > >diff --git a/mm/page_alloc.c b/mm/page_alloc.c >index 822e05f1a964..adf579a0df3e 100644 >--- a/mm/page_alloc.c >+++ b/mm/page_alloc.c >@@ -7083,7 +7083,8 @@ static int __alloc_contig_pages(unsigned long start_pfn, > } > > static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, >- unsigned long nr_pages) >+ unsigned long nr_pages, bool skip_hugetlb, >+ bool *skipped_hugetlb) > { > unsigned long i, end_pfn = start_pfn + nr_pages; > struct page *page; >@@ -7099,8 +7100,35 @@ static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, > if (PageReserved(page)) > return false; > >- if (PageHuge(page)) >- return false; >+ /* >+ * Only consider ranges containing hugepages if those pages are >+ * smaller than the requested contiguous region. e.g.: >+ * Move 2MB pages to free up a 1GB range. >+ * Don't move 1GB pages to free up a 2MB range. >+ * >+ * This makes contiguous allocation more reliable if multiple >+ * hugepage sizes are used without causing needless movement. >+ */ >+ if (PageHuge(page)) { >+ unsigned int order; >+ >+ if (!IS_ENABLED(CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION)) >+ return false; >+ >+ if (skip_hugetlb) { >+ *skipped_hugetlb = true; >+ return false; >+ } >+ >+ page = compound_head(page); >+ order = compound_order(page); The order is get from head page. >+ if ((order >= MAX_FOLIO_ORDER) || >+ (nr_pages <= (1 << order))) >+ return false; >+ >+ /* No need to check the pfns for this page */ >+ i += (1 << order) - 1; So this advance should based on "head page" instead of original page, right? >+ } > } > return true; > } >@@ -7143,7 +7171,10 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, > struct zonelist *zonelist; > struct zone *zone; > struct zoneref *z; >+ bool skip_hugetlb = true; >+ bool skipped_hugetlb = false; > >+retry: > zonelist = node_zonelist(nid, gfp_mask); > for_each_zone_zonelist_nodemask(zone, z, zonelist, > gfp_zone(gfp_mask), nodemask) { >@@ -7151,7 +7182,9 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, > > pfn = ALIGN(zone->zone_start_pfn, nr_pages); > while (zone_spans_last_pfn(zone, pfn, nr_pages)) { >- if (pfn_range_valid_contig(zone, pfn, nr_pages)) { >+ if (pfn_range_valid_contig(zone, pfn, nr_pages, >+ skip_hugetlb, >+ &skipped_hugetlb)) { > /* > * We release the zone lock here because > * alloc_contig_range() will also lock the zone >@@ -7170,6 +7203,17 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, > } > spin_unlock_irqrestore(&zone->lock, flags); > } >+ /* >+ * If we failed, retry the search, but treat regions with HugeTLB pages >+ * as valid targets. This retains fast-allocations on first pass >+ * without trying to migrate HugeTLB pages (which may fail). On the >+ * second pass, we will try moving HugeTLB pages when those pages are >+ * smaller than the requested contiguous region size. >+ */ >+ if (skip_hugetlb && skipped_hugetlb) { >+ skip_hugetlb = false; >+ goto retry; >+ } > return NULL; > } > #endif /* CONFIG_CONTIG_ALLOC */ >-- >2.52.0 -- Wei Yang Help you, Help me