From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796381188; b=Hb8+JJCdbnjplnBm2OrpTeH/nGZEMeQEl2WlZNDAdgiuW1y+wmpCjqnduXyo35OLR1S9 NdNJnspoB8KM4CQQD3Iz9kunXQxvm+LESeIf04vLxN9NgjWJqSYvr0itnVIcX5009bSv1 SJpap2xcUvBYM7wkEXYJ5igWQH0VQqcwLHa+ThoZ5tctH2nxLMESffGe4MmwDA80rqeL+ KblFdy5vE24sRRlY6kB2VZGA5o7lxrvfeWGtT5uoUkKs4g8NMP9zyv4Xlhe0zcDjoy5ZS cLFFkz6g5Gs8lNXYf2Fzkyddb0hMHkdhzSNbmbZtZeRX1GgkazYNolNEqIQL8YLMr8Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381188; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=aP6RHGx/CPiCxWLIgvTPyhQadUZD9Abh/sHODw3QLco=; b=EoLpABKYZKGXPKU/uyS4GpLeH06kXQK180otr0wXCZWYr4k81MdpDpBamr+Cauryl+fz UytvbLSrnu6tYfq2VofYeEsgv/IBsACuvH8I7W/GdUW42/DB/6YTACQXDohMx7CqEdZ1s c/SiDHiwe9nIaReIWkOrXEaUO/TWe677EIZE+dyoSZc61dfc0+szXaK40NmpvCk9Z0bnB htYI4/j4yDmZvn2SCz9qwyAm+8xjkTPNVDceMcRsPOsYDE6nSzdZifFzJpHlZKatJHuH+ dRJ2OtHVdMYYEWJYRz0IW/Kt6WpEuokQYMHGyQdkU3eygBMSHCa80tVP2ZAcgj2RYMQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ppuL7LQR; arc=none smtp.remote-ip=209.85.128.51 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 C7F0F606A32F for ; Fri, 4 Dec 2026 10:46:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C7F0F606A32F 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.a=rsa-sha256 header.s=20260707 header.b=ppuL7LQR Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4bb666304fdso15965735e9.0 for ; Fri, 04 Dec 2026 02:46:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796381186; x=1796985986; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=aP6RHGx/CPiCxWLIgvTPyhQadUZD9Abh/sHODw3QLco=; b=ppuL7LQRLQU4mFwpLUo5iPvW7I547bftAg5HYCVXfW8bBEDmj1z5eEramv5xOLhpqk 9iltC1mo7VhHnMwpSO51JQXKAz8aIKmCTyXjIu419Rw6c09Ute8Lz07NfLUzu5JNSYRl y1W8RbcIl2SO1VCH22TNYDOf7GwyQsKLdDPw5Bj/74fO6kOBnuguRZ6qJ1KEGXOK1U/T mKU1Im7XDuIlGtL1PPbUgTrnC+8mEIARt4PJ5f6PGFgDoA0nctPy+gYJAnTM3ZLMDYgU 9docEn2MiUivLMWA0lQ/hpBtMeU10HSci7AP60b00il/5MfVOB1lKpavRagqnnauKlZ/ OwNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796381186; x=1796985986; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aP6RHGx/CPiCxWLIgvTPyhQadUZD9Abh/sHODw3QLco=; b=Y4mu/O0hIaDJNEYWbR5vIHr//BB4gYtzzGH3mVgTlJx/EuvNaNZEeOKpM3Nf0d68sp B0r1Ue1gC2QlBp1oSAHbUds9GjZhIB5QoEuDuqKKzFnVf0aTlzcyV1/N5p3ralUtQOnH Hq/mWkmkwA8ulDJ2lRhsMg+MnmBFeqmNUaYKSwpAdqcXvytEoZjUWTplNiDKt6+35/4P Pgmp0Ad3zVZ71ON2XDh1KBlJzX91vbEPLZx6eKYm1l3KoT5MyGhZ5ZgkGbA39VrL6Gs9 yQgT+AcEH40fiI4NRwue1pd/p+pDKF/QePZUlu1rP5evvBbkZLVKuceKs2yJv65i9vyb nMYQ== X-Forwarded-Encrypted: i=1; AL+hErElJbrLvWMnfqMcUa+uoznS1oUZLYiibkKzrrDhTtQ2AkwM29ypEDPrprjWNPgVVHTXBdYYop0uCvi6eiE=@vger.kernel.org X-Gm-Message-State: APuU4Z9T/xvh3Ctp02XVgREWCkSBZq28766OpH9dmIb2v8W55LEqbKP7 vpinEjNtHIxbqKIwfcEkoWqPtRwrlI5YmeTVIsSsAvfrRnH2vtikmpHT X-Gm-Gg: AZFdTZe4lERdDmIzBzG/9lPdKr3h5VqSPj+vzuKxTmPzg3sVBZ3LZ99uN2l9j9nAthV R9QKKOY9+T+7UQr3PtpSQZrWeTda32wk0SZlDXf/53nbiUfUcncgZfYCQnnXyFh/damVPhKb57i xupSzU9LzJu0MEFukxL5kigsxrbsF7NgdFIb/dNV7W6zFHGVFZ/acAN8+NfiLl8h/woANyiUHBy pVubx5VLtuniL+uRWAouiIJSpCXTmsmZX/RpiNpkWrhTpFruOd1e53jjxaUz8w0MVmUleRoe9q8 MBbre65/mdwQxLBBfkiUtHaAp6gjPvtQnn7e13hg1gexTOmpv1CZINgJBiFHv0BlxwzoNtmzo6i JzZD13PsPuNYZsZI8F0SCyjOjKqo0My4aDWkumIxUWlHUTfPWR8R3uFuSEaJOyUnBNDlJpFTLHT E1Vedt1QAUr3/DyZQ/bh0Ywgb81eqrBlhUTnRHnOoOCzebFDUuan+JX6z9AT9DyzkJKbg/kOTDN ESsx43szbp3B5hwjPdtnKqdchigzTeb97oQ/hxDztFZ5o8vsipVF+PcjdP52Fdg20lmp0w24fHX irh/TZHWSCOMVmvGAkF2MsnhZYShRZUVeVNOANW8fQBCma/jL0nyXX7q X-Received: by 2002:a05:600c:4743:b0:4a2:1960:6fa with SMTP id 5b1f17b1804b1-4bbeb851326mr27208375e9.4.1796381186066; Fri, 04 Dec 2026 02:46:26 -0800 (PST) Received: from [192.168.1.22] ([83.231.69.9]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbeb6bb90bsm28592495e9.2.2026.12.04.02.46.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 04 Dec 2026 02:46:25 -0800 (PST) Message-ID: <5685aff7-3c37-4316-a3af-fcfe59b6e2bf@gmail.com> Date: Fri, 4 Dec 2026 11:46:22 +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: [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free To: "David Hildenbrand (Arm)" , 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 References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> <20261203-hugetlb_fixes_2-v2-9-9bbeabb555d2@kernel.org> Content-Language: en-US From: "Jose A. Perez de Azpillaga" In-Reply-To: <20261203-hugetlb_fixes_2-v2-9-9bbeabb555d2@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote: > 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); with Chris's hwpoison check folded in here: Reviewed-by: Jose A. Perez de Azpillaga -- cheers, jose a. p-a