Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: sashiko-reviews@lists.linux.dev,
	Muchun Song <muchun.song@linux.dev>,
	Oscar Salvador <osalvador@suse.de>,
	Naoya Horiguchi <nao.horiguchi@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Mel Gorman <mgorman@suse.de>, Andy Whitcroft <apw@canonical.com>,
	Miaohe Lin <linmiaohe@huawei.com>, Michal Hocko <mhocko@suse.com>,
	Rik van Riel <riel@surriel.com>, Hugh Dickins <hughd@google.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Vivek Kasireddy <vivek.kasireddy@intel.com>,
	Dave Airlie <airlied@redhat.com>, Jason Gunthorpe <jgg@ziepe.ca>,
	Lorenzo Stoakes <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Peter Xu <peterx@redhat.com>, Breno Leitao <leitao@debian.org>,
	Will Deacon <will@kernel.org>,
	"Aneesh Kumar K.V" <aneesh.kumar@kernel.org>,
	Nick Piggin <npiggin@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Arnd Bergmann <arnd@arndb.de>
Cc: linux-mm@kvack.org, Gerd Hoffmann <kraxel@redhat.com>,
	linux-s390@vger.kernel.org, Josef Bacik <josef@toxicpanda.com>
Subject: Re: [PATCH 14/15] RFC: mm/hugetlb: account restored reservations before freeing folios
Date: Mon, 2 Nov 2026 14:42:44 +0100	[thread overview]
Message-ID: <12d9eb7a-a9eb-4e3a-b5fe-abc49f67f212@kernel.org> (raw)
In-Reply-To: <sashiko-outbox-189914@kernel.org>

On 10/29/26 21:16, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] mm/hugetlb: gather_surplus_pages() over-allocates and retains surplus folios
> --
> 
> commit d5256483029a06972dd519de19de57831de33af4
> Author: David Hildenbrand (Arm) <david@kernel.org>
> 
> RFC: mm/hugetlb: account restored reservations before freeing folios
> 
> This commit updates anonymous hugetlb folio unmapping to increment
> resv_huge_pages immediately when restoring the VMA reservation rather than
> waiting for the folio to be freed. Because resv_huge_pages can now
> temporarily exceed free_huge_pages while unmapped folios still have
> elevated reference counts, available_huge_pages() is updated to clamp at
> zero.
> 
>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
>> index 69a5d2c73ee1d..06ef8ba41df7c 100644
>> --- a/mm/hugetlb.c
>> +++ b/mm/hugetlb.c
> [ ... ]
>> @@ -5313,6 +5309,45 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,
>>  	return len + old_addr - old_end;
>>  }
>>  
>> +/* Must be called after rmap removal. */
>> +static void hugetlb_folio_restore_rsv_on_unmap(struct vm_area_struct *vma,
>> +		struct folio *folio)
>> +{
>> +	struct hstate *h = hstate_vma(vma);
>> +	unsigned long address;
>> +	long rc;
>> +
>> +	/*
>> +	 * Restore only on the last unmap, otherwise the owner could reuse
>> +	 * the reservation while the folio is still mapped by a child.
>> +	 *
>> +	 * TODO: Handle migration temporarily dropping the mapcount to zero.
>> +	 * TODO: Prevent reservation reuse before the folio is freed.
>> +	 */
>> +	if (!folio_test_anon(folio) || !__vma_private_lock(vma) ||
>> +	    folio_mapped(folio))
>> +		return;
>> +
>> +	address = vma_anon_address(vma, folio->index, 1);
>> +	rc = vma_needs_reservation(h, vma, address);
>> +	if (!rc)
>> +		vma_end_reservation(h, vma, address);
>> +	if (rc <= 0)
>> +		return;
>> +
>> +	rc = vma_add_reservation(h, vma, address);
>> +	if (rc < 0)
>> +		return;
>> +
>> +	/* The restored reservation owns the subpool charge. */
>> +	hugetlb_set_folio_subpool(folio, NULL);
>> +
>> +	/* Account before the VMA can close or fault again. */
>> +	spin_lock_irq(&hugetlb_lock);
>> +	h->resv_huge_pages++;
>> +	spin_unlock_irq(&hugetlb_lock);
>> +}
> 
> [Severity: High]
> With h->resv_huge_pages now able to exceed h->free_huge_pages while an
> unmapped folio is still referenced (for example, via vmsplice() or GUP),
> can a concurrent hugetlb_acct_memory() -> gather_surplus_pages() over-allocate
> surplus folios and retain them in the pool?
> 
> In surplus_pages_needed():
> 
> 	static long surplus_pages_needed(struct hstate *h, long delta, long allocated)
> 	{
> 		long global_free = (long)h->free_huge_pages + allocated;
> 		long allowed_free = (long)allowed_mems_nr(h) + allocated;
> 
> 		return max((long)h->resv_huge_pages + delta - global_free,
> 			   delta - allowed_free);
> 	}
> 
> When h->resv_huge_pages > h->free_huge_pages, (long)h->resv_huge_pages +
> delta - global_free includes the deficit from the in-flight unmapped
> folios on top of delta.
> 
> If gather_surplus_pages() succeeds, needed += allocated equals delta +
> (h->resv_huge_pages - h->free_huge_pages), so it allocates and enqueues
> extra surplus folios beyond delta (or fails with -ENOMEM if
> nr_overcommit_huge_pages only had room for delta).
> 
> Furthermore, when gather_surplus_pages() cleans up unused or aborted
> folios at the free label:
> 
> 	free:
> 		spin_unlock_irq(&hugetlb_lock);
> 
> 		/*
> 		 * Free unnecessary surplus pages to the buddy allocator.
> 		 * Pages have no ref count, call free_huge_folio directly.
> 		 */
> 		list_for_each_entry_safe(folio, tmp, &surplus_list, lru)
> 			free_huge_folio(folio);
> 
> free_huge_folio() checks:
> 
> 	} else if (h->surplus_huge_pages_node[nid] &&
> 		   h->free_huge_pages >= h->resv_huge_pages) {
> 		/* Free this surplus folio that is not required globally. */
> 		remove_hugetlb_folio(h, folio, true);
> 		spin_unlock_irqrestore(&hugetlb_lock, flags);
> 		update_and_free_hugetlb_folio(h, folio, true);
> 	} else {
> 		arch_clear_hugetlb_flags(folio);
> 		enqueue_hugetlb_folio(h, folio);
> 		spin_unlock_irqrestore(&hugetlb_lock, flags);
> 	}
> 
> While h->free_huge_pages < h->resv_huge_pages, free_huge_folio() will
> enqueue the aborted surplus folios into the hugetlb pool instead of
> returning them to the buddy allocator. If the in-flight unmapped folio is
> a persistent folio on node A (where surplus_huge_pages_node[nid_A] == 0)
> and the surplus folio was allocated on node B, dropping the last reference
> to the persistent folio on node A will not free the surplus folio on
> node B either, leaving the surplus folio stranded in the pool.
> 

Damn that's confusing. What an overengineered mess :)

Surplus pages ... another thing that should have never been merged.

I guess we'd have to clamp also in surplus_pages_needed:

@@ -2317,11 +2312,13 @@ static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
  */
 static long surplus_pages_needed(struct hstate *h, long delta, long allocated)
 {
-	long global_free = (long)h->free_huge_pages + allocated;
-	long allowed_free = (long)allowed_mems_nr(h) + allocated;
+	long available = min(available_huge_pages(h), allowed_mems_nr(h));

-	return max((long)h->resv_huge_pages + delta - global_free,
-		   delta - allowed_free);
+	return delta - available - allocated;
 }


And in gather_surplus_pages() we'd then have to be more careful about which
surplus folios we're actually freeing.

-- 
Cheers,

David


  reply	other threads:[~2026-11-02 13:42 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 20:13 [PATCH 00/15] mm/hugetlb: various fixes David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 01/15] hugetlbfs: fix integer overflow in fallocate() David Hildenbrand (Arm)
2026-10-29  3:37   ` Muchun Song
2026-10-29  9:05   ` Oscar Salvador (SUSE)
2026-10-29 20:53   ` Ackerley Tng
2026-10-28 20:13 ` [PATCH 02/15] mm/hugetlb: compare against persistent count when freeing lowmem folios David Hildenbrand (Arm)
2026-10-29  3:39   ` Muchun Song
2026-11-02 12:54     ` David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 03/15] mm/hugetlb: preserve reservations when shrinking on HIGHMEM David Hildenbrand (Arm)
2026-10-29  3:41   ` Muchun Song
2026-10-28 20:13 ` [PATCH 04/15] mm/hugetlb: account temporary folios separately David Hildenbrand (Arm)
2026-10-30  9:07   ` Muchun Song
2026-11-02 12:56   ` David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 05/15] mm/hugetlb: keep surplus folios when the pool shrinks David Hildenbrand (Arm)
2026-10-29  3:51   ` Muchun Song
2026-10-28 20:13 ` [PATCH 06/15] mm/hugetlb: fix mremap() racing with page migration David Hildenbrand (Arm)
2026-10-30  9:20   ` Muchun Song
2026-10-28 20:13 ` [PATCH 07/15] hugetlbfs: fix unmapping folios from private VMAs David Hildenbrand (Arm)
2026-10-30  9:39   ` Muchun Song
2026-10-28 20:13 ` [PATCH 08/15] mm/hugetlb: serialize page-cache allocations with truncation David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 09/15] mm/memfd: recheck the page cache before allocating a hugetlb folio David Hildenbrand (Arm)
2026-11-04  9:24   ` Muchun Song
2026-10-28 20:13 ` [PATCH 10/15] mm/hugetlb: count again if the reserve map shrank David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 11/15] hugetlbfs: do not grow a sealed file in mmap() David Hildenbrand (Arm)
2026-11-04  9:55   ` Muchun Song
2026-10-28 20:13 ` [PATCH 12/15] mm/hugetlb: keep surplus folios that are reserved David Hildenbrand (Arm)
2026-11-02  8:08   ` Muchun Song
2026-10-28 20:13 ` [PATCH 13/15] mm/hugetlb: fix reservation leak on min_size= mounts David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 14/15] RFC: mm/hugetlb: account restored reservations before freeing folios David Hildenbrand (Arm)
2026-10-29 20:16   ` sashiko-bot
2026-11-02 13:42     ` David Hildenbrand (Arm) [this message]
2026-11-05 10:09   ` David Hildenbrand (Arm)
2026-10-28 20:13 ` [PATCH 15/15] RFC: mm/hugetlb: delay anon folio rmap removal until after the TLB flush David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12d9eb7a-a9eb-4e3a-b5fe-abc49f67f212@kernel.org \
    --to=david@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=airlied@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=aneesh.kumar@kernel.org \
    --cc=apw@canonical.com \
    --cc=arnd@arndb.de \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=corbet@lwn.net \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=hughd@google.com \
    --cc=jgg@ziepe.ca \
    --cc=josef@toxicpanda.com \
    --cc=kraxel@redhat.com \
    --cc=leitao@debian.org \
    --cc=liam@infradead.org \
    --cc=linmiaohe@huawei.com \
    --cc=linux-mm@kvack.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nao.horiguchi@gmail.com \
    --cc=npiggin@gmail.com \
    --cc=osalvador@suse.de \
    --cc=peterx@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=riel@surriel.com \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=svens@linux.ibm.com \
    --cc=vbabka@kernel.org \
    --cc=vivek.kasireddy@intel.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox