From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABE7BC021B8 for ; Tue, 4 Mar 2025 09:18:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 325BD6B0088; Tue, 4 Mar 2025 04:18:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D4F76B0089; Tue, 4 Mar 2025 04:18:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19D016B008A; Tue, 4 Mar 2025 04:18:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EDDAA6B0088 for ; Tue, 4 Mar 2025 04:18:02 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9E43A161A0A for ; Tue, 4 Mar 2025 09:18:02 +0000 (UTC) X-FDA: 83183316804.05.676FADC Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf14.hostedemail.com (Postfix) with ESMTP id 773DD10000F for ; Tue, 4 Mar 2025 09:18:00 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=G9TAC2cP; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=WVjRosd2; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=G9TAC2cP; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=WVjRosd2; spf=pass (imf14.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1741079880; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VZ+zMoNnPWznQOdy8eGcx7wdCBztUYeaRVbXyyuev0Q=; b=WIILrYSo3VJudo5LzJtGbeuB5VeU6XaaXTtR9rAJDnafceAEHyYBM1oAQHyHmzoDaIDdYa 46i+yERX+SdvXG0s7A8DdjRtr7Qsw3sESwf/xA9ChBoKXQ1b0N4SBnHsAykekzzhMoByxP nZG6gGzRsXRvtyXDWkfHDevEt/OCz1s= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=G9TAC2cP; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=WVjRosd2; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=G9TAC2cP; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=WVjRosd2; spf=pass (imf14.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741079880; a=rsa-sha256; cv=none; b=ua4lQZla5HVSwlUKQSCBOp/HAXOvcKb7D2J000+Om94LMnnZU7s/GerzEmFFXUtD4yJay5 vmDkYQJaWDpIed2ZT7fZTIGgJXi03qmdStVJehal/f+C+rcKNUXzxIOBH9mtmrw7nPlpv8 B/nUtPsVJfxFobrcTXBn195VQJ9Apbg= Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id CBA8A2118F; Tue, 4 Mar 2025 09:17:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1741079878; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VZ+zMoNnPWznQOdy8eGcx7wdCBztUYeaRVbXyyuev0Q=; b=G9TAC2cPulrjS53UqxjY7EBbS3rMubidLTmA9LMQgLr87w/M9pKMOZs6onGjcbypHwOLD6 DNlPlDBXze1z/h4JVp/k4CXHV+j17i1uIq6+BPsSHzRXZ6pzD3lzOMXvVwMNn86UqECJGV SD3gy/9kC8lAoKDSJqoJT2r1oSHPbF8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1741079878; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VZ+zMoNnPWznQOdy8eGcx7wdCBztUYeaRVbXyyuev0Q=; b=WVjRosd2bIKgA5bpXiKhWM8h3dZ4evUHX/2hXVjnHKgJ5Peh2/FQLCkSitc6TRtf9ejFIw C3IBV/h+cATEpqBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1741079878; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VZ+zMoNnPWznQOdy8eGcx7wdCBztUYeaRVbXyyuev0Q=; b=G9TAC2cPulrjS53UqxjY7EBbS3rMubidLTmA9LMQgLr87w/M9pKMOZs6onGjcbypHwOLD6 DNlPlDBXze1z/h4JVp/k4CXHV+j17i1uIq6+BPsSHzRXZ6pzD3lzOMXvVwMNn86UqECJGV SD3gy/9kC8lAoKDSJqoJT2r1oSHPbF8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1741079878; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VZ+zMoNnPWznQOdy8eGcx7wdCBztUYeaRVbXyyuev0Q=; b=WVjRosd2bIKgA5bpXiKhWM8h3dZ4evUHX/2hXVjnHKgJ5Peh2/FQLCkSitc6TRtf9ejFIw C3IBV/h+cATEpqBg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 48E2C13967; Tue, 4 Mar 2025 09:17:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id mmiCDkbFxmfWSgAAD6G6ig (envelope-from ); Tue, 04 Mar 2025 09:17:58 +0000 Date: Tue, 4 Mar 2025 10:17:56 +0100 From: Oscar Salvador To: Liu Shixin Cc: Muchun Song , Andrew Morton , David Hildenbrand , Barry Song , Kefeng Wang , Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm/hugetlb: update nr_huge_pages and surplus_huge_pages together Message-ID: References: <20250304083841.283159-1-liushixin2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250304083841.283159-1-liushixin2@huawei.com> X-Stat-Signature: uemipwsgwer9rboruspne7wd8o94pqr9 X-Rspamd-Queue-Id: 773DD10000F X-Rspamd-Server: rspam06 X-Rspam-User: X-HE-Tag: 1741079880-270701 X-HE-Meta: U2FsdGVkX18Cjj/ZhJiWqpq2SiPfajhtBYjuCmFiEEbWvOY+kt5JZ2GVWO4TefRJtIzcD8EtABW0aaWh+bZbo/X5RcZ/fwXojE+cwUcL2c4wOfFhq3ePm2tqIVXwSNXs2wKP2hj/3+I7rYJGh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boo4VtGirmiGqJkGTIi2wHVd1qqZd1tHp8xLej3jvXlVjOBwk5+UJ23R9MIov3xaC8cHVDslRsFpC/JjDMcktpI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, Mar 04, 2025 at 04:38:41PM +0800, Liu Shixin wrote: > In alloc_surplus_hugetlb_folio(), we increase nr_huge_pages and > surplus_huge_pages separately. In the middle window, if we set > nr_hugepages to smaller and satisfy count < persistent_huge_pages(h), > the surplus_huge_pages will be increased by adjust_pool_surplus(). > > After adding delay in the middle window, we can reproduce the problem > easily by following step: > > 1. echo 3 > /proc/sys/vm/nr_overcommit_hugepages > 2. mmap two hugepages. When nr_huge_pages=2 and surplus_huge_pages=1, > goto step 3. > 3. echo 0 > /proc/sys/vm/nr_huge_pages > > Finally, nr_huge_pages is less than surplus_huge_pages. > > To fix the problem, call only_alloc_fresh_hugetlb_folio() instead and > move down __prep_account_new_huge_page() into the hugetlb_lock. > > Fixes: 0c397daea1d4 ("mm, hugetlb: further simplify hugetlb allocation API") > Signed-off-by: Liu Shixin > --- > mm/hugetlb.c | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 9faa1034704ff..0b02ea1c39e63 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -2253,11 +2253,19 @@ static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h, > goto out_unlock; > spin_unlock_irq(&hugetlb_lock); > > - folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask); > + folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL); > if (!folio) > return NULL; > > + hugetlb_vmemmap_optimize_folio(h, folio); > + > spin_lock_irq(&hugetlb_lock); > + /* > + * Update nr_huge_pages and surplus_huge_pages together, > + * otherwise it might confuse persistent_huge_pages() momentarily. > + */ > + __prep_account_new_huge_page(h, nid); It would be great if we could pair up this one with the actual increase of surplus pages, but then free_huge_folio() will decrease the nr_huge_pages if the pool changed. Also, that comment makes me think that__prep_account_new_huge_page() will adjust both counters, so maybe just go with something like "nr_huge_pages needs to be adjusted within the same lock cycle as surplus_pages, otherwise..." -- Oscar Salvador SUSE Labs