From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796210569; b=exv+ND7qZEkC+QaA/OSl8rXMg4k/IRwgCfZJ0XM5AlRPrfdyN9nlMNIZu0xR7Y1bBqH6 CTctdDLj7WsE6mXy/+h9w+9jCuFZda9QoBWwmEhzm5CzI1HDXyJ1YTb9afC2gMUm56B0L VJPa2ErOmkVX2wJUxKP6A5icoSpm4HbxYjBmzDBLB03buIokb4djJ3QFAgBb7x8nMzc5L /sxVj7/3pnnyBUNN570xMrrAFQNt9lXqlvMACi81ollg99nGM9xVcGcYPrmdZcbws+TqD KfAZeAb7sMWFct/+cpqkELsKXvLEdlZlfPJ4uBlfvzqrOS/anBKXJjrjqFOdnO8zTJw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796210569; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=uDpd4J/SA69cWHugmYYycEEsXAhygChMfivIJq3oabw=; b=4UxCMUVs7D3UyEQ+XOb1zDLt04jZy7yqWu9O7RrGF8/0RFNDRDUOLeoMXjpim32IbM2w 5+SdFz6EC1gKMalZG3LaSAz5UqFH36goLKy3o8//50tiRsKd0Pit9is0WyBA7fZLOv1xT 6C1PcIEdAAZIrXLTNjtojCKiXnRS0hg9U0pxc6b/leRBabY643ZYEaVl8gZli0eJGAHie rboT8sI1cClGd6HmfOWBYRAA936PTwPUm1t5FdFU7AF031DI9ipGwxwloorv6Tfyso+ba wTJvXGuzyPpmDONNCYY2AIScinbelVoAV105su7vyniWe+VvHHtaIBJ66nhK00nrSkw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="SyHkzc/u"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (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 5569B606A323 for ; Wed, 2 Dec 2026 11:22:49 +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=SyHkzc/u Received: by smtp.kernel.org (Postfix) with ESMTPSA id C766C1F00893; Wed, 2 Dec 2026 11:22:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796210568; bh=uDpd4J/SA69cWHugmYYycEEsXAhygChMfivIJq3oabw=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=SyHkzc/uP7ekscVfrkr+bNggvkZzFhLxCEnU3nUe52BaHKvrAUKxCKgjrpvUMaNVr 2LwBx5UTZ2MtUjYMM2KbioUzF1FVjdIFmcD5Jjqp90LgiLe02Z1m//zg4An9+gFwPR n3rLrKl7TYU9C52tE1L4JXe5oTxVip3zn3fOY2CwZOjnMQf3AtZLK2uwPUKHckyXZK 3UjrGFWYorsBunAYinI08EgteU/7GTqqx8RgP+xECw6LqhMu02u+4K9bQET6+3rq28 1WK3aClH9JcQShvqri965DF2AUVUw94YxdxvhVLYomJo54lRLXej4LSWql16N9q1oL rEW6ovSSOGIHg== Message-ID: Date: Wed, 2 Dec 2026 12:22:39 +0100 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 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED To: Alexandre Ghiti , Andrew Morton , Zi Yan Cc: Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Vlastimil Babka , Mike Rapoport , Michal Hocko , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20261202102818.1317130-1-alex@ghiti.fr> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzS5EYXZpZCBIaWxk ZW5icmFuZCAoQ3VycmVudCkgPGRhdmlkQGtlcm5lbC5vcmc+wsGQBBMBCAA6AhsDBQkmWA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oRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <20261202102818.1317130-1-alex@ghiti.fr> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/2/26 11:28, Alexandre Ghiti wrote: Hi, > MADV_DONTNEED on a range that covers only part of a PMD-mapped THP splits > the PMD and zaps the PTEs of the range. The THP itself is not freed: it > stays allocated and charged as a whole, now partially mapped, until the > deferred split shrinker splits it under memory pressure. Until then the > process has lost the PMD mapping, and the TLB reach that comes with it, for > no memory benefit. Right. User space should avoid doing stupid things. > > Besides, a THP is checked for being underused only once: it is queued on > the deferred split list when it is created, and the shrinker drops it from > the list for good once it has found it not underused. A long-lived THP > whose pages are freed later is never looked at again, so its unused memory > can't be reclaimed. The job of the underused shrinking is to detect over-allocation. Not "user space used it and then partially freed it". > > When the underused THP shrinker can reclaim the range later, zero it in > place instead: it reads as zeroes, as MADV_DONTNEED requires, and the PMD > mapping stays. The THP is queued again on the deferred split list, so the > shrinker looks at it again. > > For an exclusive anonymous THP, MADV_DONTNEED on part of it thus no longer > leaves it partially mapped: the range reads as zeroes at once, and the > memory is given back when reclaim needs it, once the THP holds more than > max_ptes_none zeroed pages. Below that, the THP is not split even under > memory pressure, where the partially mapped THP used to be split: it keeps > up to max_ptes_none zeroed pages, the waste khugepaged already allows when > collapsing. > > The range is zapped as before when the zeroed pages could not be freed, > e.g. with max_ptes_none at 511 (the default), or when anyone else may see > the THP, e.g. after fork or through a GUP pin. > > Results on a large production workload (THP=always, max_ptes_none=0, > allocator purging with MADV_DONTNEED), same allocator configuration before > and after this patch: > > THPs and memory change > ------------------------------------ ------ > THPs partially mapped -97% > memory mapped by THPs (AnonHugePages) +29% > underused THP splits +145% > PMD splits +10% > workload anonymous memory +0.2% > > Performance change > ------------------------------------ ------ > p50 latency -1.7% > p99 latency -2.3% > throughput within noise > dTLB misses per instruction -0.4% > IPC +0.2% > > Signed-off-by: Alexandre Ghiti > --- > Open questions: > > - MADV_DONTNEED now zeroes the range synchronously, up to a whole PMD > minus one page (2M on x86, 512M with 64K base pages on arm64). Should > zeroing be capped, falling back to the split above some size? > > - A pin taken while the range is being zeroed goes unnoticed, as neither > GUP-fast nor GUP takes the folio lock: a device write through it (e.g. an > O_DIRECT read into the range from another thread) can land after > MADV_DONTNEED returns. Only a process doing I/O into memory it is > discarding can see it; pins that exist when the THP is checked fall back > to the zap. Closing the window would take a PMD invalidation and a TLB > flush per call, which this patch avoids, or GUP backing off on a folio > being zeroed. Is the race acceptable? > > - RSS (and so oom_badness()), /proc/pid/pagemap and mincore() keep showing > the range as resident until the THP is split, where the zap made it > disappear at once. memcg usage is unchanged: the THP stayed charged until > its split before too. > > - The cow (THP_RUN_SINGLE_PTE), khugepaged and transhuge-stress selftests > use a partial MADV_DONTNEED to split a PMD, which MADV_DONTNEED never > guaranteed. khugepaged sets max_ptes_none to 511 first, but cow and > transhuge-stress keep the host's value: with a lower max_ptes_none they > still pass, while silently testing a PMD-mapped THP. They should pin > max_ptes_none or split the PMD explicitly (MADV_NOHUGEPAGE, mprotect() or > debugfs). > > PTE-mapped large folios (mTHP) are left for later. > > include/linux/huge_mm.h | 2 ++ > include/linux/mm.h | 2 ++ > mm/huge_memory.c | 69 +++++++++++++++++++++++++++++++++++++++++ > mm/internal.h | 3 ++ > mm/madvise.c | 2 +- > mm/memory.c | 14 ++++++--- > 6 files changed, 87 insertions(+), 5 deletions(-) > > diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h > index 087920c38163..4ca675435856 100644 > --- a/include/linux/huge_mm.h > +++ b/include/linux/huge_mm.h > @@ -28,6 +28,8 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, > pmd_t *pmd, unsigned long addr, unsigned long next); > bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, > unsigned long addr); > +bool zero_huge_pmd_range(struct vm_area_struct *vma, pmd_t *pmd, > + unsigned long addr, unsigned long end); > int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud, > unsigned long addr); > bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 00abd4d250a9..d28ba827f1d5 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -3413,6 +3413,8 @@ struct zap_details { > #define ZAP_FLAG_DROP_MARKER ((__force zap_flags_t) BIT(0)) > /* Set in unmap_vmas() to indicate a final unmap call. Only used by hugetlb */ > #define ZAP_FLAG_UNMAP ((__force zap_flags_t) BIT(1)) > +/* MADV_DONTNEED: zero part of a PMD-mapped THP in place instead of splitting */ > +#define ZAP_FLAG_ZERO_PARTIAL_THP ((__force zap_flags_t) BIT(2)) > > #ifdef CONFIG_MMU > extern bool can_do_mlock(void); > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 4fc888ddfb69..121da9397194 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -2623,6 +2623,75 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, > return true; > } > > +/* > + * MADV_DONTNEED on [addr, end), part of a PMD-mapped THP: zero it in place > + * instead of splitting the PMD, and queue the THP for the deferred split > + * shrinker, which frees the zeroed pages once the THP is underused. Return > + * false if the caller must split the PMD and zap the range instead. > + */ > +bool zero_huge_pmd_range(struct vm_area_struct *vma, pmd_t *pmd, > + unsigned long addr, unsigned long end) > +{ > + unsigned long haddr = addr & HPAGE_PMD_MASK; > + struct folio *folio; > + spinlock_t *ptl; > + pmd_t orig_pmd; > + bool ret; > + > + /* With max_ptes_none at 511, no THP is ever underused. */ > + if (!READ_ONCE(split_underused_thp) || > + khugepaged_max_ptes_none == HPAGE_PMD_NR - 1) > + return false; > + > + if ((vma->vm_flags & VM_MTE) || userfaultfd_armed(vma) || > + mm_forbids_zeropage(vma->vm_mm)) > + return false; > + > + ptl = pmd_trans_huge_lock(pmd, vma); > + if (!ptl) > + return false; > + orig_pmd = pmdp_get(pmd); > + if (is_huge_zero_pmd(orig_pmd)) { > + spin_unlock(ptl); > + return true; > + } > + > + folio = pmd_present(orig_pmd) ? > + vm_normal_folio_pmd(vma, haddr, orig_pmd) : NULL; > + if (!folio || !folio_test_anon(folio) || > + !PageAnonExclusive(&folio->page) || folio_test_mlocked(folio) || > + folio_contain_hwpoisoned_page(folio) || > + folio_ref_count(folio) != folio_expected_ref_count(folio) || > + !folio_trylock(folio)) { > + spin_unlock(ptl); > + return false; > + } > + folio_get(folio); > + spin_unlock(ptl); > + > + flush_cache_range(vma, addr, end); > + clear_contig_highpages(folio_page(folio, (addr - haddr) >> PAGE_SHIFT), > + addr, (end - addr) >> PAGE_SHIFT); > + > + /* > + * Mark the PMD dirty, and soft-dirty, as a write through it would. If > + * the PMD was split meanwhile, let the caller zap the range instead. > + */ > + ptl = pmd_lock(vma->vm_mm, pmd); > + orig_pmd = pmdp_get(pmd); > + ret = pmd_present(orig_pmd) && pmd_trans_huge(orig_pmd) && > + pmd_folio(orig_pmd) == folio; > + if (ret) { > + touch_pmd(vma, haddr, pmd, true); > + deferred_split_folio(folio, false); > + } > + spin_unlock(ptl); > + > + folio_unlock(folio); > + folio_put(folio); > + return ret; > +} There is not a lot I like about this code :) Why can't user space just do reasonable things instead? -- Cheers, David