From patchwork Tue Feb 6 03:08:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 197151 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1291798dyb; Mon, 5 Feb 2024 19:08:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHa+c2Ac8wz+sonHY8MZHa9wT5Bm6A81uD+02C9xdKclwjqvSrx5g0pXq4GjiXJjt66pnN4 X-Received: by 2002:a05:620a:468b:b0:783:e114:2a62 with SMTP id bq11-20020a05620a468b00b00783e1142a62mr1572821qkb.16.1707188934497; Mon, 05 Feb 2024 19:08:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707188934; cv=pass; d=google.com; s=arc-20160816; b=smO7TwyNtb4IpId5LSw/UdXlfOhdk2hZ0dSGz5mrZTKEDDsKo2aE8pPJ0GU7sYS8Kb KQzZlCCDZTDnUbAQQgS3LVajWXvly5wzwc/C8w3Unv63yYvySJeisAm3G9ckrGMB1qMl Ma74ypGtUWxyNDF21xGDbFL8xNhV6Ot7BF+zuuobPokc2MS2DF/3G1m/P9fJHq0jzTx2 Er9IPkoBl3JF8gwOV1vNIR3cLmOIyZGv50cVhPhPo1vc/r4KBfe8+O+Pf2f9uB/wbwh8 Aw9kCnI1OBNQTSez/tXufHqClxWqhgv0OcEhux5nYDHC8jEXyico+PAdhKQyjkrFr/rj thaQ== 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=lsjZbfVW8/WKO3D8nyVLEixZPG0VzeutC5Yw93QCPIQ=; fh=zZSeUIopHvTxpLb4VP3WW/f85qpSlv7S5i+WC/qCtxU=; b=hZO/S0cKUswTmt46usDMR/ARnkWFihfiOVR50NWUWNvLqu2HYw0oMHGGEM35LfLEpP tACDnRJTPiguLWmy+SMhSt3qosRDcR+BxTHB7+xfygqZiic0c3WmNcOlTwJtAP1HkeQu pXDlsJeUdbij5hCMjboFhLuoRfTPhoWjS6DCsrNnM68oXzW2daCckhSXnXIC5bwHNWlX k6UO1maAWsp7k6A58RqW9LbAXBWBB2RtpYsOqfmk9iR283sZ3NRBHRq57GoQzrcHSMEx HJb0HGDW8b4OGxKopt1dVPdOxFMNYneo11suMI1bUalf1Ha4uS3GfW9I580tpHQHLyXI VNLw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=CKPkvmNm; 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-54238-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54238-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com X-Forwarded-Encrypted: i=1; AJvYcCXK/ksbTP5tgGfOag4q8w2LE/Vk/F4oByFoWt9w4uohxSRwUHGgR5mIbcl3WsKtEvg1lexUgTAB8Uzbbf/inJKbg+CJYw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a10-20020a05620a066a00b0078400d7f282si1293657qkh.725.2024.02.05.19.08.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 19:08:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54238-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=CKPkvmNm; 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-54238-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54238-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 3F4281C22A0C for ; Tue, 6 Feb 2024 03:08:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8479D745EF; Tue, 6 Feb 2024 03:08:28 +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="CKPkvmNm" Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) (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 5483B4BAAA for ; Tue, 6 Feb 2024 03:08:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.113 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707188905; cv=none; b=dLSylD2ZtHuaXt19LFxiK7X9pnVGSKD8DyHCU4/+xi1yJ69qnY24cs73VANxhMkHZf8w81zYWU4EQbpDUiKelm5Kk+qF+W5Cpw3OPgqOz0pdih8JyM0hwG7kGAmOjJ1haBzK+rHjhJg5U3kox/VuY21I65gSc6kMhOXSkieXngE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707188905; c=relaxed/simple; bh=W9VSracklWdFWQ06kTb4tXZEKnLldZkwFebfVEFrakA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=LsdcuQ1LtqT9F/TONgXgtdwnHauaoKR2w5yM68fsrZ9CRiZoHSRUDl7r6HnubYqB1282uuBQsZ0fFK56ipMU0oLXVWBEPRmN8aQgYW9RS02NDtx4ZLug7FlnOfW6rrIgtTPwHdUUNTFzH1iodJqoaFbrvZxbg/bs/J7FjMfDAps= 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=CKPkvmNm; arc=none smtp.client-ip=115.124.30.113 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=1707188900; h=From:To:Subject:Date:Message-Id:MIME-Version:Content-Type; bh=lsjZbfVW8/WKO3D8nyVLEixZPG0VzeutC5Yw93QCPIQ=; b=CKPkvmNm/pKI/lLVh59JkKEXSXh1mMbKh1voRCLIf3M+kwG4MXYlDZRZ56sWdQtNojWWkrhzqdkJD1ZbVHDf6P223ZKIN+CmWkZJqDPS3oTo6aVtTR5oE8e7h6YGttV8IYYQ0jECf2ibWJpbcX48dMeu6rOxigIdR/mozLKd3Iw= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045176;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0W0CACps_1707188899; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W0CACps_1707188899) by smtp.aliyun-inc.com; Tue, 06 Feb 2024 11:08:19 +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 v3] mm: hugetlb: improve the handling of hugetlb allocation failure for freed or in-use hugetlb Date: Tue, 6 Feb 2024 11:08:11 +0800 Message-Id: <62890fd60b1ecd5bf1cdc476c973f60fe37aa0cb.1707181934.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: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790063748483585555 X-GMAIL-MSGID: 1790117344222573172 alloc_and_dissolve_hugetlb_folio() preallocates a new hugetlb page before it takes hugetlb_lock. In 3 out of 4 cases the page is not really used and therefore the newly allocated page is just freed right away. This is wasteful and it might cause pre-mature failures in those cases. Address that by moving the allocation down to the only case (hugetlb page is really in the free pages pool). We need to drop hugetlb_lock to do so and therefore need to recheck the page state after regaining it. The patch is more of a cleanup than an actual fix to an existing problem. There are no known reports about pre-mature failures. Signed-off-by: Baolin Wang Acked-by: Michal Hocko Reviewed-by: Muchun Song --- Changes from v2; - Update the commit message suggested by Michal. - Remove unnecessary comments. Changes from v1: - Update the suject line per Muchun. - Move the allocation into the free hugetlb handling branch per Michal. --- mm/hugetlb.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9d996fe4ecd9..a05507a2143f 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,16 @@ 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 @@ -3102,9 +3100,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; }