From patchwork Mon Feb 5 12:50:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13545545 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 0D727C4828D for ; Mon, 5 Feb 2024 12:51:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B34E6B00A9; Mon, 5 Feb 2024 07:51:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 863336B00AA; Mon, 5 Feb 2024 07:51:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72B456B00AB; Mon, 5 Feb 2024 07:51:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 60A646B00A9 for ; Mon, 5 Feb 2024 07:51:06 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 32FEF80A34 for ; Mon, 5 Feb 2024 12:51:06 +0000 (UTC) X-FDA: 81757735332.24.C09D42D Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) by imf22.hostedemail.com (Postfix) with ESMTP id DE72BC0012 for ; Mon, 5 Feb 2024 12:51:02 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=LEE5vTzE; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf22.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707137464; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=s/Pp95J1WK6GTpmalhMVjkVH/7wA3cd9KPuozZdyxCQ=; b=Y8LvyF7RV/COMIs78Tzw7+GyUUMDpshfMlD1h/XD3Tuu4SS7pDUFyUkeiBpZSay008cH/F mbuo9bHT8xk9ty1Pf8LEGX1Obeln21sXOpI3w9qWgqWclObBK4wSxA9kZrFrJyfKwSTWxU jkpitkci80wuP+BYyvTcxGk9PwAFWok= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=LEE5vTzE; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf22.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707137464; a=rsa-sha256; cv=none; b=c4NqhdEzssjMFVQHLg3JOPZ0g+8nf1L3dx8Y2SmrdUBFeLyPhrwwMjXaw7j4/1+T0fLde8 w1G1rJx9etc/CC/H53dK09R/C0vhEE9Q24G3jtXbX0xuVo+UJ7TXaNVtt8H+9JJp08Gk7L 68khu8dRHXjEUByGwnqVRDoeVPivCaQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1707137459; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=s/Pp95J1WK6GTpmalhMVjkVH/7wA3cd9KPuozZdyxCQ=; b=LEE5vTzElrI4TMZU1TZ5xU/oPSx81vChYf8A5DeD1HJ7cDLJXfg1KtCwLaYeSS/X2318IslOwbK2cWR01Nw7CnwDsBEwSHFBv+VHBGcjOjQ69ILllfSEntPrmsUt/s9KnKanO0DNp+RKwBS5USaYOzKDQ48F1Uu5zFlXyEaZbXA= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R351e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0W0AV717_1707137458; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W0AV717_1707137458) by smtp.aliyun-inc.com; Mon, 05 Feb 2024 20:50:59 +0800 From: Baolin Wang 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 v2] mm: hugetlb: improve the handling of hugetlb allocation failure for freed or in-use hugetlb Date: Mon, 5 Feb 2024 20:50:51 +0800 Message-Id: <23814ccce5dd3cd30fd67aa692fd0bf3514b0166.1707137359.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspamd-Queue-Id: DE72BC0012 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 4dnmu8myf4jgtyytieoqbjicqny68dt1 X-HE-Tag: 1707137462-877057 X-HE-Meta: U2FsdGVkX18QQTngOs6/cr/sy8KtcMG8T0IWuKVEwa1GYX81CAexC5gKIkQOTZY1lY4aAT2st5RPNLkLbwAoWb4QIXs14c4jowHyAhguZQPuZpxWzv6mPEHxSZOiThhEKt9VO+IiiVC3Jfh7ODdvPMYCbmQOuYSmOqRVNYit+ZGdANSu4iUIFmA8qveSBsf64mWjO9JMLDhM+k71G7k3lepUATjIearBxbPB2Cz4wE0ipZ70Q5YlS5sax5vvIA/nifnWYAuNkt15Yyq8kRXzzEWHJ5Tm3bEhi7TZSKx2/3Hxq0s/ARFcWaJ3VCcY2FSwIKbUbDzfSt+0SXcxAL8g7dr/paboFF0Qunmynw9K97S++jLYAngdQjU7AIWDLW+ezW0qwgJ9u2m0uO4DIuxB2b8amf+gUauXUlAcSOoEbFsWwDMRHlYftb7dYs/WLrTlQBma8l8dL97p5WNuBsLc3pVmTwCHGAqN9ZciM+OPs9WVXoWLgdHAAjI6kyA6bJdr9I5dLI6kDIprtQIXYovUQyDzmBXila8tSay3x5SiDDdnEA7pheHqzswCZrtbLpxqsVZPsifqP2rukcU1/M9+J36cUh+qd3hB04kn61TNwM22A08J1DNPfxljhfRcsGt/wv0CzbmaldNI9su17jP6L+ZMOkdkc8HAxGvcj/kkYp4juzm1Nk2EJ0fSzk0sJhRhYqHr0ZF2qkb/mzTYSiFastexfPCqsoe5YpumfY3lSExzMrY5TygzqCXqTYLohm2SbpNPyTrMorj8L8o0RDmEhGbB8/GF5L6g6tcyM8XWiAJV4saofmFDwa8J+DJl41iDZz68g1ZoX0U9ofQXciJT9ZCoO8ut2BfYstlRNp2iUrru4awNcFmHI0r5r5FZ2XQh2fe1HwXTawkePyaNM1GgZahqxoKO81wUXwEBLcY5YbIk7lVQuvmOfKTjg8zhKRaHIQSAEgIr8n5960QvgqD A9P75UGR RuydgTukQI+KFkgcSAhFuhHDOPFqe1flUD1PXc0evCqHo1et3Rydl2Gt2Y4COFs7OG3H7gqnhs7M3gndPFT8m9zXoMOKAakmZQDzgKbY5I2BkByuIIXQhscE1JL5lACCQFeIbr7LL8W84O+L53DNFb7l0yPGSgBgA0aMzITZfX1/FBDtkSwMeh2TYTg== 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: 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. Moreover, moving the allocation into the free hugetlb handling branch. Signed-off-by: Baolin Wang --- Changes from v1: - Update the suject line per Muchun. - Move the allocation into the free hugetlb handling branch per Michal. --- mm/hugetlb.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9d996fe4ecd9..4899167d3652 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3031,21 +3031,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)) { @@ -3075,6 +3063,24 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, cond_resched(); goto retry; } else { + if (!new_folio) { + spin_unlock_irq(&hugetlb_lock); + /* + * Before dissolving the free hugetlb, 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); + goto retry; + } + /* * Ok, old_folio is still a genuine free hugepage. Remove it from * the freelist and decrease the counters. These will be @@ -3102,9 +3108,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; }