Message ID | b2e6ce111400670d8021baf4d7ac524ae78a40d5.1707105047.git.baolin.wang@linux.alibaba.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-52020-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp654043dyb; Sun, 4 Feb 2024 19:54:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFm5U4U+7g+QE2LtvGEy7ryg+vYTTMUbHm1yw+aovEd7Dgtb2pzsEG9lVtz6pIPiOhM+hcb X-Received: by 2002:a05:6870:440e:b0:219:432f:be6f with SMTP id u14-20020a056870440e00b00219432fbe6fmr6659299oah.1.1707105289002; Sun, 04 Feb 2024 19:54:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707105288; cv=pass; d=google.com; s=arc-20160816; b=wBUkxgARzWKQg93q3yu3OKNgemwAF47RFNvAy1i6bBay8YQcx1JfcDl53Ojji9yJtc 4n3AwzFG2w4ZyadHM8CleJ9+EEaVbLpf3g0RAwptusUs0SQuuoiuePLoXxhG22DB7Eol WXVF0kQLZ4y4Sbj6a/g6mw6PzdbDbt/EjR4T5NKtfV54UPaGrWwtdWr5I5qTGKsl8GEf soJeDGny+IJT7HPIBY9ojEIBeFei8EBcSqE9555+/mWogKFsbvYmFEvBGQ1NEnpEQnjP e3nuSvIkezRg0mpH2Jkk7sP8zuQQr3uA+ilX6tPf8Nqrbx9K3BvJ646BeNI+W0rXgTy+ mgrg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=vSiJJMfX52mmBCYj2MFQe+6cAYIwJlgaq+HlVSo63jQ=; fh=C7ziOJzsqAOukVSiLcvHiSdd0K+Vj6IcRfPLOz0U4rg=; b=wj1w09eIA4vrWbeEEQccbw0nHIbZ1KcOWJ2m+5TQ4V1zxt17CU65Uar925R4gb+Ote z2aehc6+fWdK6jl5Y8cNnY/3qrAj/M9G4gG4SKTAcI7aFQdDJU/EC/AA3I1wnzhqA5BI 1a9misjY/6rpdWA96wdJorVf0hqa6uuyrUCUbqdgKFtHnjp/G1xvabIhH4ltZgGdK/iO UjnIafPaufAxwJSl/4qUHAeH/vyUHbMNMbicWTGv4gwZ8rw2vaGK7FjRxl24/UFQY00Z 4+eSWHd0YPEjdpyGorSMP8reBavtUQER1JfILYFj/gtz++GsCHZOWnOJMedtn7KCPuPn mdKw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=gnz7E3ZI; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-52020-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52020-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com X-Forwarded-Encrypted: i=1; AJvYcCUDAgIN6lekdYnL3dFeW5L22AS50HKtQQv/iGFQLmlQI9Igbl2/klG5x64oTVIXyRhKynzWghnFMDVDgkLY+XULUKQqdA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d11-20020a630e0b000000b005d8b6ebfb13si5379493pgl.659.2024.02.04.19.54.48 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 19:54:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52020-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=gnz7E3ZI; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-52020-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52020-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id AE1E7281EB6 for <ouuuleilei@gmail.com>; Mon, 5 Feb 2024 03:54:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0BC7B666; Mon, 5 Feb 2024 03:54:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="gnz7E3ZI" Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) (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 4ADAF9474 for <linux-kernel@vger.kernel.org>; Mon, 5 Feb 2024 03:54:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707105274; cv=none; b=I9VeX0ZieQP6mhKz+iwRm3PTlM1v5eTrEOjz9AFIKlOaaaxqL5nwmTmXFUCqxmkvfmscEHOyOjWt7opRvKCXeuFq9MYgBfMBze4dPnJzr4X6ROmJa6obrV9XaU9Xt8CvsXvrjAct1CiMt+RHPMz8U7kTOEzbkHefuZ14lZsfOmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707105274; c=relaxed/simple; bh=5UZD8O3S7MyGqYNX0Pw4AhJoeza+Svr9UOPoihuLHms=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=NE9gEBJ7yAmdzcjbVyJcJsnpEiqgho7drT5B+dWJrM1q+xDn43yXLdBGnCz3gOPFk5Qkwe15KeKKhSNt66Zp0RaD07WSwwrJjcirOBrt8H1GCWdjn7FmuXIb/cIVBvy0RUSgje5TFmNxMfo+928pSwXaB99XxnOys5yJ/KtBUyY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=gnz7E3ZI; arc=none smtp.client-ip=115.124.30.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1707105263; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=vSiJJMfX52mmBCYj2MFQe+6cAYIwJlgaq+HlVSo63jQ=; b=gnz7E3ZIzsp8TWYxHWpumdj1vQjFmS9Bu/RKi03bOKep7ZU///DhGjg02RAxzKqDJw6oFmpEHMB5XYJcun2G2C4Bz9BobDhXnBmVALWPUdvZYgaLhltkfJhfTxBVTk3FCpwmloOnofnKD8flRZRQegYwE8D8Ws0L3I3CV0nWZxU= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046056;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0W02KW8o_1707105262; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W02KW8o_1707105262) by smtp.aliyun-inc.com; Mon, 05 Feb 2024 11:54:23 +0800 From: Baolin Wang <baolin.wang@linux.alibaba.com> To: akpm@linux-foundation.org, muchun.song@linux.dev Cc: osalvador@suse.de, david@redhat.com, mhocko@kernel.org, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: hugetlb: fix hugetlb allocation failure when handling freed or in-use hugetlb Date: Mon, 5 Feb 2024 11:54:17 +0800 Message-Id: <b2e6ce111400670d8021baf4d7ac524ae78a40d5.1707105047.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790029635464784403 X-GMAIL-MSGID: 1790029635464784403 |
Series |
mm: hugetlb: fix hugetlb allocation failure when handling freed or in-use hugetlb
|
|
Commit Message
Baolin Wang
Feb. 5, 2024, 3:54 a.m. UTC
When handling the freed hugetlb or in-use hugetlb, we should ignore the
failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully,
since we did not use the new allocated hugetlb in this 2 cases.
Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
---
mm/hugetlb.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
Comments
> On Feb 5, 2024, at 11:54, Baolin Wang <baolin.wang@linux.alibaba.com> wrote: > > When handling the freed hugetlb or in-use hugetlb, we should ignore the > failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, > since we did not use the new allocated hugetlb in this 2 cases. > > Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com> OK. It is not a fix (I see a fix keyword in subject) but an optimization for unnecessary-allocation cases. Thanks. Reviewed-by: Muchun Song <muchun.song@linux.dev>
On 2/5/2024 2:56 PM, Muchun Song wrote: > > >> On Feb 5, 2024, at 11:54, Baolin Wang <baolin.wang@linux.alibaba.com> wrote: >> >> When handling the freed hugetlb or in-use hugetlb, we should ignore the >> failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, >> since we did not use the new allocated hugetlb in this 2 cases. >> >> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com> > > OK. It is not a fix (I see a fix keyword in subject) but an > optimization for unnecessary-allocation cases. Thanks. Yes, better to change the subject to 'mm: hugetlb: improve the handling of hugetlb allocation failure for freed or in-use hugetlb' Andrew, could you help to change the subject line when you apply it? (or you want a new version, please let me know) Thanks. > Reviewed-by: Muchun Song <muchun.song@linux.dev> Thanks for reviewing.
On Mon 05-02-24 11:54:17, Baolin Wang wrote: > When handling the freed hugetlb or in-use hugetlb, we should ignore the > failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, > since we did not use the new allocated hugetlb in this 2 cases. > > Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com> > --- > mm/hugetlb.c | 18 ++++++++++++------ > 1 file changed, 12 insertions(+), 6 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 9d996fe4ecd9..212ab331d355 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -3042,9 +3042,8 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > * under the lock. > */ > new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); > - if (!new_folio) > - return -ENOMEM; > - __prep_new_hugetlb_folio(h, new_folio); > + if (new_folio) > + __prep_new_hugetlb_folio(h, new_folio); Is there any reason why you haven't moved the allocation to the only branch that actually needs it? I know that we hold hugetlb lock but you could have easily dropped the lock, allocate a page and then goto retry. This would actually save an allocation. Something like this: diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ed1581b670d4..db5f72b94422 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3029,21 +3029,9 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, { gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; int nid = folio_nid(old_folio); - struct folio *new_folio; + struct folio *new_folio = NULL; int ret = 0; - /* - * Before dissolving the folio, we need to allocate a new one for the - * pool to remain stable. Here, we allocate the folio and 'prep' it - * by doing everything but actually updating counters and adding to - * the pool. This simplifies and let us do most of the processing - * under the lock. - */ - new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); - if (!new_folio) - return -ENOMEM; - __prep_new_hugetlb_folio(h, new_folio); - retry: spin_lock_irq(&hugetlb_lock); if (!folio_test_hugetlb(old_folio)) { @@ -3073,6 +3061,15 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, cond_resched(); goto retry; } else { + + if (!new_folio) { + spin_unlock_irq(&hugetlb_lock); + new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); + if (!new_folio) + return -ENOMEM; + __prep_new_hugetlb_folio(h, new_folio); + goto retry; + } /* * Ok, old_folio is still a genuine free hugepage. Remove it from * the freelist and decrease the counters. These will be @@ -3100,9 +3097,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, free_new: spin_unlock_irq(&hugetlb_lock); - /* Folio has a zero ref count, but needs a ref to be freed */ - folio_ref_unfreeze(new_folio, 1); - update_and_free_hugetlb_folio(h, new_folio, false); + if (new_folio) { + /* Folio has a zero ref count, but needs a ref to be freed */ + folio_ref_unfreeze(new_folio, 1); + update_and_free_hugetlb_folio(h, new_folio, false); + } return ret; }
On 2/5/2024 5:31 PM, Michal Hocko wrote: > On Mon 05-02-24 11:54:17, Baolin Wang wrote: >> When handling the freed hugetlb or in-use hugetlb, we should ignore the >> failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, >> since we did not use the new allocated hugetlb in this 2 cases. >> >> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com> >> --- >> mm/hugetlb.c | 18 ++++++++++++------ >> 1 file changed, 12 insertions(+), 6 deletions(-) >> >> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >> index 9d996fe4ecd9..212ab331d355 100644 >> --- a/mm/hugetlb.c >> +++ b/mm/hugetlb.c >> @@ -3042,9 +3042,8 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, >> * under the lock. >> */ >> new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); >> - if (!new_folio) >> - return -ENOMEM; >> - __prep_new_hugetlb_folio(h, new_folio); >> + if (new_folio) >> + __prep_new_hugetlb_folio(h, new_folio); > > Is there any reason why you haven't moved the allocation to the only > branch that actually needs it? I know that we hold hugetlb lock but you Nope, just did a simple patch to ignore the allocation failure. > could have easily dropped the lock, allocate a page and then goto retry. > This would actually save an allocation. Yes, will do. Thanks. > Something like this: > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index ed1581b670d4..db5f72b94422 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -3029,21 +3029,9 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > { > gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; > int nid = folio_nid(old_folio); > - struct folio *new_folio; > + struct folio *new_folio = NULL; > int ret = 0; > > - /* > - * Before dissolving the folio, we need to allocate a new one for the > - * pool to remain stable. Here, we allocate the folio and 'prep' it > - * by doing everything but actually updating counters and adding to > - * the pool. This simplifies and let us do most of the processing > - * under the lock. > - */ > - new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); > - if (!new_folio) > - return -ENOMEM; > - __prep_new_hugetlb_folio(h, new_folio); > - > retry: > spin_lock_irq(&hugetlb_lock); > if (!folio_test_hugetlb(old_folio)) { > @@ -3073,6 +3061,15 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > cond_resched(); > goto retry; > } else { > + > + if (!new_folio) { > + spin_unlock_irq(&hugetlb_lock); > + new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); > + if (!new_folio) > + return -ENOMEM; > + __prep_new_hugetlb_folio(h, new_folio); > + goto retry; > + } > /* > * Ok, old_folio is still a genuine free hugepage. Remove it from > * the freelist and decrease the counters. These will be > @@ -3100,9 +3097,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, > > free_new: > spin_unlock_irq(&hugetlb_lock); > - /* Folio has a zero ref count, but needs a ref to be freed */ > - folio_ref_unfreeze(new_folio, 1); > - update_and_free_hugetlb_folio(h, new_folio, false); > + if (new_folio) { > + /* Folio has a zero ref count, but needs a ref to be freed */ > + folio_ref_unfreeze(new_folio, 1); > + update_and_free_hugetlb_folio(h, new_folio, false); > + } > > return ret; > }
diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9d996fe4ecd9..212ab331d355 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3042,9 +3042,8 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, * under the lock. */ new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); - if (!new_folio) - return -ENOMEM; - __prep_new_hugetlb_folio(h, new_folio); + if (new_folio) + __prep_new_hugetlb_folio(h, new_folio); retry: spin_lock_irq(&hugetlb_lock); @@ -3075,6 +3074,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, cond_resched(); goto retry; } else { + if (!new_folio) { + ret = -ENOMEM; + goto free_new; + } + /* * Ok, old_folio is still a genuine free hugepage. Remove it from * the freelist and decrease the counters. These will be @@ -3102,9 +3106,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, free_new: spin_unlock_irq(&hugetlb_lock); - /* Folio has a zero ref count, but needs a ref to be freed */ - folio_ref_unfreeze(new_folio, 1); - update_and_free_hugetlb_folio(h, new_folio, false); + if (new_folio) { + /* Folio has a zero ref count, but needs a ref to be freed */ + folio_ref_unfreeze(new_folio, 1); + update_and_free_hugetlb_folio(h, new_folio, false); + } return ret; }