From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5D4C433BB8 for ; Tue, 7 Jul 2026 18:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783448966; cv=none; b=Et/XmB5Xdi+brLYn3qVR9TF6O/kNkMqX0zqD1BYk2+4PH9YFdtt5skTKjLjinCr+F8QCJrFWywMbR48HUlUae/+5ZL0kT7su1jffb7818jZpSk5fRXuP46r7PsfweNRzUtncNKbvInhSUV7bLMM3vi6U58AydvX6Z58l6h93x3A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783448966; c=relaxed/simple; bh=ehag85Usb7/5RjVArHQkA9A6/UL7Uw2h5bC34P3yZvw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WqPRxcOCULv4axON8YKGJxmuSAgOzaUYNJECs0glPGMROQC9sxHEQ+n+pNmrmLvWPGu1ypjDZ4N60K3PgVxqS+o4hG7NDdLGdqON0HhbkMbU5q6lS+BpwtS33Okjwbul6Kf9WeHo9nnnS3N/zE1IsphJIc7yQc6wtWLksFjnFOU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=W6qGxlCg; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=bcHG1qA5; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W6qGxlCg"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="bcHG1qA5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1783448963; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9HRvV/BdSZtB6LDhoeM5qhtklSBvZEZTbpGHK8giblA=; b=W6qGxlCgspYLRY5ydzPk0DCv3pgFU5HXbKuIoXsfOiJnUXS5Php5LgdjMWTpir/MjcnfWc HxqS3NrCH+jjxqMyuxlo2l9oRybuJzXg/Ie7qdR6vtIbjHZiCeoidIaESOH7VMbgyxKV6Q gTTF5x1PveuI8rAoT4C7q5gLhBYkJAE= Received: from mail-oa1-f70.google.com (mail-oa1-f70.google.com [209.85.160.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-636-oTiC9EmeMWSS5VxYgq_ySg-1; Tue, 07 Jul 2026 14:29:22 -0400 X-MC-Unique: oTiC9EmeMWSS5VxYgq_ySg-1 X-Mimecast-MFC-AGG-ID: oTiC9EmeMWSS5VxYgq_ySg_1783448962 Received: by mail-oa1-f70.google.com with SMTP id 586e51a60fabf-44ce7e98e27so1395229fac.0 for ; Tue, 07 Jul 2026 11:29:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1783448962; x=1784053762; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9HRvV/BdSZtB6LDhoeM5qhtklSBvZEZTbpGHK8giblA=; b=bcHG1qA5ebj6KK96RFUItNfkeC6YZNTRlx+jfidVluALJtKoysaS4S8QeJ20Dhfm8J LQAol1L+f7LtFTvSX4OKFrpd20MiKXFlaNW3vs/n+Q5mQFfaEdipYvFomR/gXRMCKWLD FetCzzuqWm0fIGeIj//8cI1rGtt8fHrSyccLgBifJpiZPthl0vtSq0LudhdiXUMuKrJK +dWVTv2AkcY6enms0DaBKi1TLNDGVFt0we2yJbTSCk1+Q9UlUc51Doe5M2pdb1/UsYUT DhpUiZVlayNF/fKxHYkqwOMgDUvV6WrdGA388S3qrklJTb97fSHrhGoal8j3cmmh9iMi apRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783448962; x=1784053762; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=9HRvV/BdSZtB6LDhoeM5qhtklSBvZEZTbpGHK8giblA=; b=NHdQR3hFS9q6UBfo7UgiS/9floJmpDgqF7UjCXdSHa3wiGELcmt0NLtzbn+cIAEuA0 hb3+rxNWL/VznEDVDJIsmGM2b8xbDlXz1wQ/nKbRCaTxOr3mS0jWPFHjmVeRkGY8tWpH X5uJAzsMeQRelwoCFLvLCdnnT1xbHe1S5leJkZnFdic7TCCA6Lf7hKNeMu3L/DtAtDC4 9jlioaau1Rn2Z8wqJ4VS3lFSstjj053/DM1Ua5ksWj1lJR66V3JvadM9TgfWROJqAqhQ W9tTAzJQHbTSwmZvs15E9Z+Oqlb2tnr4VWoNcrg0hIXqcCRxEVA/p57hRWB+TR+MubQA BriQ== X-Forwarded-Encrypted: i=1; AHgh+RoGNzJH+SR/4UxQ6nuj6r/wQOrwo0ro5ngJXy7K0f3hd0V5rEAXGfPdH47TtGo416lCiirSC1rvZhYzbyc=@vger.kernel.org X-Gm-Message-State: AOJu0Yzvr+y2ZT6c1Ic/1snQ1PPa0FxIQGkUx25KdxhPHvgMhLlmqxHM dh+fKa/BL9IJVoaxXixbaCVFqp5blz9AYc9DuLiKYOc6VTFRI/JKmPnQnm5yXHGyrpVOzA01G+g qylP++Ms6iHgdc5FR15gMABeDPeL9WsP5faWkk9TreCwps+RP/MVXzRPZC0919bAtsA== X-Gm-Gg: AfdE7cktfvPNsTPHWcEfgb/k5t8wh5FAjU8Dh39TEil7dEG4neRUYBTjauxbf/H3waQ D/6UucXEdqo3VyG0TTk1xWAVn8LkF2OE8upPhLwFLbpmIEQJNH3/HAM8Z+b2wmAW7nGBxWWAZPS INLkHsXOWZ/Kon9Y8JKlcAsYg+X+VGXGJNgdQV6LpgxBSBdwsTNdZH4ydpof18bbJhuSa4DrQci 1cB1JBZf7XFlXXJZ4F0fcTk9kbU++qxo+SkMOhgHmRBUwdjSBpjojIcMU6DfK3jx+Grba+Mm9AN CtuSz1lGG+hlxiOUmnIcGo2uehY+MhHmPmI5qnJFH3h7JL52sBVS22l77/EBKPr7pUTB8wZsFGS 0T2MuUvbfxNfHU9eWaNp4v3sTSkS75/IYxTKjyXbLiiz0icW3bN0buh8pBlczACpRf26Lsfb/6V 5ftaCGDQ== X-Received: by 2002:a05:6870:b61b:b0:448:aa9e:b051 with SMTP id 586e51a60fabf-45118c70621mr2232778fac.14.1783448961802; Tue, 07 Jul 2026 11:29:21 -0700 (PDT) X-Received: by 2002:a05:6870:b61b:b0:448:aa9e:b051 with SMTP id 586e51a60fabf-45118c70621mr2232761fac.14.1783448961294; Tue, 07 Jul 2026 11:29:21 -0700 (PDT) Received: from chaithco-thinkpadp16vgen1.rmtusco.csb.cloud (184-96-191-56.hlrn.qwest.net. [184.96.191.56]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-44cfb55907dsm13550509fac.11.2026.07.07.11.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2026 11:29:20 -0700 (PDT) From: Charles Haithcock To: muchun.song@linux.dev, osalvador@suse.de, akpm@linux-foundation.org Cc: Charles Haithcock , david@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, arozansk@redhat.com, Joshua Hahn Subject: [PATCH 1/2] mm/hugetlb: Prefer mempolicy nodes for calcuating surplus hugepage reservation Date: Tue, 7 Jul 2026 12:28:55 -0600 Message-ID: <20260707182900.483757-2-chaithco@redhat.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260707182900.483757-1-chaithco@redhat.com> References: <20260707182900.483757-1-chaithco@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Currently, reserving surplus hugetlb pages in a mempolicy still use global counters for calculating how many hugetlb pages to reserve. Modify this to consider the nodes in the mempolicy first but fall back to all possible nodes. Suggested-by: Joshua Hahn Signed-off-by: Charles Haithcock --- - v1: Modified `needed` calculation to use `allowed_mems_nr(h)` in order to consider free hugetlb pages in our mempolicy. - v2: Folded in Joshua Hahn's recommendation [1] to further modify `needed` calculation to take the max of either the available hugetlb pages in the mempolicy or the globally available hugetlb pages. Allows allocations to prioritize nodes in the mempolicy but can still fall back to offnode allocations. Also added selftests to check only for the edgecase which caused this to initially be reported and sanity checks. - v3: Split the change and tests into separate patches and clean up code to better align with kernel style guidelines. Likewise, fold in Usama Arif's correction [2]. [1] https://lore.kernel.org/all/20260602152022.2673803-1-joshua.hahnjy@gmail.com/ [2] https://lore.kernel.org/linux-mm/20260624144600.8159-1-usama.arif@linux.dev/ mm/hugetlb.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f24bf49be0..f5e678e997 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2255,6 +2255,23 @@ static nodemask_t *policy_mbind_nodemask(gfp_t gfp) return NULL; } +static unsigned int allowed_mems_nr(struct hstate *h) +{ + int node; + unsigned int nr = 0; + nodemask_t *mbind_nodemask; + unsigned int *array = h->free_huge_pages_node; + gfp_t gfp_mask = htlb_alloc_mask(h); + + mbind_nodemask = policy_mbind_nodemask(gfp_mask); + for_each_node_mask(node, cpuset_current_mems_allowed) { + if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) + nr += array[node]; + } + + return nr; +} + /* * Increase the hugetlb pool such that it can accommodate a reservation * of size 'delta'. @@ -2277,7 +2294,8 @@ static int gather_surplus_pages(struct hstate *h, long delta) alloc_nodemask = cpuset_current_mems_allowed; lockdep_assert_held(&hugetlb_lock); - needed = (h->resv_huge_pages + delta) - h->free_huge_pages; + needed = max_t(long, (delta - allowed_mems_nr(h)), + ((h->resv_huge_pages + delta) - h->free_huge_pages)); if (needed <= 0) { h->resv_huge_pages += delta; return 0; @@ -2311,8 +2329,9 @@ static int gather_surplus_pages(struct hstate *h, long delta) * because either resv_huge_pages or free_huge_pages may have changed. */ spin_lock_irq(&hugetlb_lock); - needed = (h->resv_huge_pages + delta) - - (h->free_huge_pages + allocated); + needed = max_t(long, ((delta - allowed_mems_nr(h)) - allocated), + ((h->resv_huge_pages + delta) - + (h->free_huge_pages + allocated))); if (needed > 0) { if (alloc_ok) goto retry; @@ -4513,23 +4532,6 @@ static int __init hugepage_alloc_threads_setup(char *s) } __setup("hugepage_alloc_threads=", hugepage_alloc_threads_setup); -static unsigned int allowed_mems_nr(struct hstate *h) -{ - int node; - unsigned int nr = 0; - nodemask_t *mbind_nodemask; - unsigned int *array = h->free_huge_pages_node; - gfp_t gfp_mask = htlb_alloc_mask(h); - - mbind_nodemask = policy_mbind_nodemask(gfp_mask); - for_each_node_mask(node, cpuset_current_mems_allowed) { - if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) - nr += array[node]; - } - - return nr; -} - void hugetlb_report_meminfo(struct seq_file *m) { struct hstate *h; -- 2.55.0