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=1796298003; b=YrqSy79JahkHCC4iN24aP7/Wo5xSIdhiJnkLbZfKFSumu/GazvFywOcZUZucvcyD8d0d WCy7WcAef+8W30TX+wCs5SUqE1/0QU9h3CJ+wqW79GNbRQXI3lHN3nDZ0I8ohvGK1Ziwg 6MgNijOow5/2bAmPkWwagRiwpgJn6irD4e1w/4nm10jfjx5jqp+VwaElJ/A8pT5MOtQ8G QgAHlK/vrb20O8d9HOJy1ZLTd6JTzCCYJYxmDz78nYZ92spvE31S37tJRrYyriWp3mHr9 AZ3KCC8Y6Uat1ltvg8ueKzgdDASnpD182iCcsxMbS7kzMockKfrwliM5HXXPoMpjZUw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298003; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=bw+y7ZSvyox2jzHgS/6Lzrc5eZPyOaphgJ6T6xGeKxI=; b=e0unmnp9P3DA3Lzve3NZ726UWw5eBTFfgdQ+/I+6NDY4QcTUXJddVdnYHzxzmEEEJyA9 mVLW3jfmrnaLrGFCXYdmVu4lJd8cVMRdGYX9lSv/bUFUn5QMH2gosdbUeKWxqcKzArcyp etE+1l1zI5kjVgeuC9aS1uhjNQ+Skuwk/hOiRGbSYduwbKEFgMFC6fe9/5L9+X04pGkmk ewW7ksRwK3Dgs9898rvVNsuZzvShn9xuwm5Vzox9usFI4MN7dHFEJBcOrummDGaoXrtEe U1VXye8xrwYSmQeg57SLQbecGgJCWJqnxSt1bYkvaX63/rErerjurEbIIwDWgqHbwWA== 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=envci4No; 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) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43D06606A32C; Thu, 3 Dec 2026 11:40:03 +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=envci4No Received: by smtp.kernel.org (Postfix) with ESMTPSA id 124B31F00893; Thu, 3 Dec 2026 11:39:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298003; bh=bw+y7ZSvyox2jzHgS/6Lzrc5eZPyOaphgJ6T6xGeKxI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=envci4NohS4bEq0wM0Pitt1ONhivTuS+Vnbo9KLBHBSkEW5X2dl6ipXh6h11Zpim/ QSylo6D18uvimFYduuQqFpwaFKUbLLGwoe8MGQRY3184RdQZFELHOfw0lkQyA1ku43 fN+K72gZrxWJ7R3jxG6LiUDRXQVAsnCWChTddEBoCgGC/TPrdTDL7JYD9b1Z3fxPG0 qe8wD/GRYQEo4h1l665ij6TaEUb6znTALh06NQjUXH3JDbBfWur1n4zj9aIdY6N+9A R5SYrExiKCfqDbbxGDlUvyvP8U5Sx/fs4Wu4LjvOULlu2m/iUrUUWAXsI6tmpZxXPo sNdhwwjdAh6AQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:32 +0100 Subject: [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-9-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4794; i=david@kernel.org; h=from:subject:message-id; bh=3By+Dsdm8BrJOwdUuKf+LaNuecrVwUNv0VObea66Ot4=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6oYlHfZXE3Z916BW+lv3v0rhwze7jixPjnk43GTs 5Eie/kzO0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE9gQLk4BmIjjMkaGK5XS3fsS5/BYpZ3c KPT7p/Bq4UWlM9zt33UeELOs+OPoyPDPWEvWrePBbMFr2/pYFOf6HNCQf/5tXvu2xvkvlR7kafk yAgA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A Surplus hugetlb folios are accounted per node, but reservations are global. When free_huge_folio() frees a hugetlb folio whose node doesn't have any surplus folios, we currently keep it as a free hugetlb folio. However, we might then have surplus folios on other nodes that are no longer required for the global reservations. This can be reproduced with two NUMA nodes, nr_hugepages=1 on node 0 and nr_overcommit_hugepages=1: (1) Allocate the single hugetlb folio on node 0 from the pool. (2) With MPOL_BIND to node 1, mmap(MAP_SHARED) a hugetlb memfd that needs a single folio. This will reserve + allocate a surplus folio on node 1. (3) Free the folio from (1). (4) With MPOL_BIND to node 0, fault in the folio of the memfd, which consumes the reservation and allocates node 0's free folio, and immediately punch a hole, freeing the folio without restoring the reservation. Node 0 has no surplus folios, so the folio is kept. The pool ends up with a free surplus folio on node 1 that nobody reserved: HugePages_Total: 2 HugePages_Free: 2 HugePages_Rsvd: 0 HugePages_Surp: 1 Let's fix it by freeing a surplus folio of another node when free_huge_folio() keeps a folio whose node has no surplus folios, using the same rule as for the folio's own node: the free folios cover all reservations without it. With this fix, the surplus folio is already freed in (3), as the two free folios then exceed the single reservation, and we correctly end up with a single free folio on node 0 and no surplus folios. Note that the freed surplus folio might have been required by a task that was bound to that node. But we don't care about that: reservations are inherently not NUMA aware, and anybody who needs guarantees on specific nodes has to preallocate, like QEMU does, for example. This has low urgency. Fixes: 7893d1d505d5 ("hugetlb: Try to grow hugetlb pool for MAP_PRIVATE mappings") Cc: stable@vger.kernel.org Reported-by: Chris Mason Tested-by: Hongfu Li Reviewed-by: Hongfu Li Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 662cb87fe740a..5984377160d37 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -142,6 +142,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb, unsigned long end, struct folio *folio, zap_flags_t zap_flags); static struct resv_map *vma_resv_map(struct vm_area_struct *vma); +static struct folio *remove_pool_hugetlb_folio(struct hstate *h, + nodemask_t *nodes_allowed, bool acct_surplus); static inline bool subpool_is_free(struct hugepage_subpool *spool) { @@ -1767,6 +1769,7 @@ void free_huge_folio(struct folio *folio) struct hstate *h = folio_hstate(folio); int nid = folio_nid(folio); struct hugepage_subpool *spool = hugetlb_folio_subpool(folio); + struct folio *surplus_folio = NULL; bool restore_reserve; unsigned long flags; @@ -1815,19 +1818,35 @@ void free_huge_folio(struct folio *folio) folio_clear_hugetlb_temporary(folio); spin_unlock_irqrestore(&hugetlb_lock, flags); update_and_free_hugetlb_folio(h, folio, true); + return; } 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 { - mod_node_page_state(folio_pgdat(folio), NR_HUGETLB, - -(long)pages_per_huge_page(h)); - arch_clear_hugetlb_flags(folio); - enqueue_hugetlb_folio(h, folio); - spin_unlock_irqrestore(&hugetlb_lock, flags); + return; } + + mod_node_page_state(folio_pgdat(folio), NR_HUGETLB, + -(long)pages_per_huge_page(h)); + arch_clear_hugetlb_flags(folio); + enqueue_hugetlb_folio(h, folio); + + /* + * This is not a surplus folio on this node, but we might have surplus + * folios on other nodes that are no longer required, as reservations + * are global and surplus folios are per node. So free another folio + * instead. + */ + if (h->surplus_huge_pages && + h->free_huge_pages > h->resv_huge_pages) + surplus_folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY], + /* acct_surplus= */ true); + spin_unlock_irqrestore(&hugetlb_lock, flags); + /* We might be in atomic context, but freeing might need vmemmap pages. */ + if (surplus_folio) + update_and_free_hugetlb_folio(h, surplus_folio, true); } /* -- 2.56.1