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 C9BD6C54ED1 for ; Tue, 27 May 2025 03:37:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4EDD46B0088; Mon, 26 May 2025 23:37:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 49E736B008A; Mon, 26 May 2025 23:37:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B48B6B008C; Mon, 26 May 2025 23:37:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1C7EC6B0088 for ; Mon, 26 May 2025 23:37:03 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B54E914148B for ; Tue, 27 May 2025 03:37:02 +0000 (UTC) X-FDA: 83487276684.26.7459936 Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.7]) by imf18.hostedemail.com (Postfix) with ESMTP id 2ED051C0005 for ; Tue, 27 May 2025 03:36:59 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=SvuZOpOV; spf=pass (imf18.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.7 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1748317021; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=OxLrHwRKd6VmF2kibj1apKILaD+MlHJ+P625Hvp6Sc0=; b=h2cNpemGtVHxNXRO7IRnL9Occ1bcknDqAOzMcia7MpVUjYKif33Dg0rMNpvDlnUbaQWfGP MEnUyQ4Cy+jDWpmdBcTu5FWNM84j0lAb/S237Bpels3aPfV4fZ0i5099nDGNenuYlLJryo X1T3vkdlrTYF098SaxupwPY3snXJUY0= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=SvuZOpOV; spf=pass (imf18.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.7 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748317021; a=rsa-sha256; cv=none; b=wGGYGwS5BFRKvVbu0Q2c+BChsuPCJJuIv2QA8kDwpQVGosIMKuqX/oEQPdYeSAsIDKSWG3 mfBFWlTP/++7OuIzGqQTVplrYccEhFN+aEVA+BxnLEouR4Ejx7yk+0p69FrrTE4PF313Nb rOJ+CIuvgYpTH7fiTdwL4BF/Z41vfpw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=OxLrHwRKd6VmF2k ibj1apKILaD+MlHJ+P625Hvp6Sc0=; b=SvuZOpOVsEQ8//kWcUaNf4TU/T4nYdm kQ6FVkrknEe2Rj2MKcbEa9v2zCHpl01YLYfbgVKf5PUowtS7GIjyTwzuLaxpK0gZ fNZZKN/UZKMInWMzE5cT2BijNYHfTSchldy//0Uf9IW8W3ASrhEx6y5quVR2YpFz eNUAkrw6Ay0s= Received: from hg-OptiPlex-7040.hygon.cn (unknown []) by gzga-smtp-mtada-g0-0 (Coremail) with SMTP id _____wDnz2RTMzVoDBO5Ag--.12961S2; Tue, 27 May 2025 11:36:52 +0800 (CST) From: yangge1116@126.com To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, 21cnbao@gmail.com, david@redhat.com, baolin.wang@linux.alibaba.com, muchun.song@linux.dev, osalvador@suse.de, liuzixing@hygon.cn, Ge Yang Subject: [PATCH V2] mm/hugetlb: remove unnecessary holding of hugetlb_lock Date: Tue, 27 May 2025 11:36:50 +0800 Message-Id: <1748317010-16272-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID:_____wDnz2RTMzVoDBO5Ag--.12961S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3Ary5Xw18Kr48GFykuw45Jrb_yoW7XFW8pF W3GrsxtrWkXa4Dur4xAr4kGrn8K34qqryjyFWxKrW3ZF43tw1UKFnrZw18XayrCr9rJFWf ZFWjqFWv9F4jy3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRoKZXUUUUU= X-Originating-IP: [112.64.138.194] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOhZaG2g1Me8rCgAAsP X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 2ED051C0005 X-Stat-Signature: ttz1yh8xzao4mrs1gamj1cgehuqjo6no X-Rspam-User: X-HE-Tag: 1748317019-631657 X-HE-Meta: U2FsdGVkX1/OOdn2ASJgU8OK/F5MBUxhw/B2vflj7BAoh0lywNHWEPZ/P8jLL76FUwldYhyDrC2XtV2Efnuz3JTQr/q44z+yiaVqdM0oAqjnfcVPG909mBlL3WoXMIWlIjwDBhy6+ZfEeFb56+W1b64hEqw3nNIsjlnd7PpWthxpbzsmcdYqPHFsJyXkmKR+ZzXEJ5q09aqUpRPipxZ3Az8Ra2OpmBXsLR1qzg4xG9gfHPN9hU8zO4F7DAcShOGcYW7G4Rus2nytIQWGOD8JQpxcq+citcJRbfo71ewyGYuJrP9GWDTiJqsVoFW786aC+sl/ll9nIDS8j8OuHi2NsaPCogfNprgm1JU7Vz0Gp0NtyGwHTy9V5LLv2KeF+IoUSMzTOKTGqtTKSzfvm32qsBlCiSDcoxz9vj2h0pkD/izr+SjW3Y68Fu7W5E6b13Ykz4trcNT44FqeKYxnPrPtQlRs8i0B9OQbP01dEF+3rpZTfKW6NZ1BFsB0yLD/PaW1SeKBLEPSkDLaqdo2tSnGCZw5fDCEF73nAWG4C9aqCJDjRCsWdSBLgWq2U3sWuhkxsRw34HsHplZ7kTuWqzhv/6L3QjOrbr6hqKzSjK7jowiHs1An1u32lS+inprCzeX5yEBHeDWibJlpQAKev44hIIwDdPJb9WPc5QZqAuph5ia+udxeprxYsO1tWD1bE0ZVx9sniBeLthLEmi+ADjHSLFKYBbWbfxJWpdLufQF209azzOfjloFNRu+5ia596N2QVVEHoZ/qIrEr2K7grjFkPRdWK7Yr7bDcUXsHuMSIvl3JIVVcfXzADDmY+/DGkGOPcgm+8UxbppzRadQNJ0vPRSJZNdzJpQWJCrGtz9/dcjzJij/I0uaxI/dRIzB9xubsIM1ppq6xW1yMqhoZ5/+I3vBM0bNK4rzAMHyCC1biIsiZ2POPsrIUmNtlgolUUi1FjCMN3wJgCFKdI33i+H1 t4uQKl8Q a3ApDvx02JRgIIocEygF1Ic1RVWo4QHkVVD+3J79YxcZmeXzo8B+Bcden78+o1fNR2qvRU1s44TqDuowSd8774Zu1cL5P3OQ0k8jyJIk58/lisOsfOLfxbNieetqamRn3p8hIR3e4t/cabtere8thck7n4zB4JWaUrt1jPqduKZUWZ7swhuS0A/RzOdNp8iOj4G83xK7YOxjp0Gfze3/Kr5tRQ5PXwLwjTdodGR0ZFE33w/db545RSL31V90b6Bsxn1EzM3XN+0BL901flOxW+tMf8mgV8M5c6zZNVFNpB6XrkVa2/BkkJWItOvm4tmW/Mjj9zREzEgbWXRRuScNO4Gf2G0L19vZQIprixJMwWh+gwLV+YJkI3tKAUA== 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: From: Ge Yang In the isolate_or_dissolve_huge_folio() function, after acquiring the hugetlb_lock, it is only for the purpose of obtaining the correct hstate, which is then passed to the alloc_and_dissolve_hugetlb_folio() function. The alloc_and_dissolve_hugetlb_folio() function itself also acquires the hugetlb_lock. We can have the alloc_and_dissolve_hugetlb_folio() function obtain the hstate by itself, so that the isolate_or_dissolve_huge_folio() function no longer needs to acquire the hugetlb_lock. In addition, we keep the folio_test_hugetlb() check within the isolate_or_dissolve_huge_folio() function. By doing so, we can avoid disrupting the normal path by vainly holding the hugetlb_lock. The replace_free_hugepage_folios() function has the same issue, and we should address it as well. Suggested-by: Oscar Salvador Signed-off-by: Ge Yang --- V2: - Add early folio_test_hugetlb() suggested by David - Change the subject from "mm/hugetlb: remove redundant folio_test_hugetlb" to "mm/hugetlb: reduce unnecessary holding of hugetlb_lock" mm/hugetlb.c | 54 +++++++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f0b1d53..b4bc2bd 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2787,20 +2787,24 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, /* * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve * the old one - * @h: struct hstate old page belongs to * @old_folio: Old folio to dissolve * @list: List to isolate the page in case we need to * Returns 0 on success, otherwise negated error. */ -static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, - struct folio *old_folio, struct list_head *list) +static int alloc_and_dissolve_hugetlb_folio(struct folio *old_folio, + struct list_head *list) { - gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; + gfp_t gfp_mask; + struct hstate *h; int nid = folio_nid(old_folio); struct folio *new_folio = NULL; int ret = 0; retry: + /* + * The old_folio might have been dissolved from under our feet, so make sure + * to carefully check the state under the lock. + */ spin_lock_irq(&hugetlb_lock); if (!folio_test_hugetlb(old_folio)) { /* @@ -2829,8 +2833,10 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, cond_resched(); goto retry; } else { + h = folio_hstate(old_folio); if (!new_folio) { spin_unlock_irq(&hugetlb_lock); + gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL); if (!new_folio) @@ -2874,35 +2880,24 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) { - struct hstate *h; int ret = -EBUSY; - /* - * The page might have been dissolved from under our feet, so make sure - * to carefully check the state under the lock. - * Return success when racing as if we dissolved the page ourselves. - */ - spin_lock_irq(&hugetlb_lock); - if (folio_test_hugetlb(folio)) { - h = folio_hstate(folio); - } else { - spin_unlock_irq(&hugetlb_lock); + /* Not to disrupt normal path by vainly holding hugetlb_lock */ + if (!folio_test_hugetlb(folio)) return 0; - } - spin_unlock_irq(&hugetlb_lock); /* * Fence off gigantic pages as there is a cyclic dependency between * alloc_contig_range and them. Return -ENOMEM as this has the effect * of bailing out right away without further retrying. */ - if (hstate_is_gigantic(h)) + if (folio_order(folio) > MAX_PAGE_ORDER) return -ENOMEM; if (folio_ref_count(folio) && folio_isolate_hugetlb(folio, list)) ret = 0; else if (!folio_ref_count(folio)) - ret = alloc_and_dissolve_hugetlb_folio(h, folio, list); + ret = alloc_and_dissolve_hugetlb_folio(folio, list); return ret; } @@ -2916,7 +2911,6 @@ int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list) */ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) { - struct hstate *h; struct folio *folio; int ret = 0; @@ -2925,23 +2919,9 @@ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) while (start_pfn < end_pfn) { folio = pfn_folio(start_pfn); - /* - * The folio might have been dissolved from under our feet, so make sure - * to carefully check the state under the lock. - */ - spin_lock_irq(&hugetlb_lock); - if (folio_test_hugetlb(folio)) { - h = folio_hstate(folio); - } else { - spin_unlock_irq(&hugetlb_lock); - start_pfn++; - continue; - } - spin_unlock_irq(&hugetlb_lock); - - if (!folio_ref_count(folio)) { - ret = alloc_and_dissolve_hugetlb_folio(h, folio, - &isolate_list); + /* Not to disrupt normal path by vainly holding hugetlb_lock */ + if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) { + ret = alloc_and_dissolve_hugetlb_folio(folio, &isolate_list); if (ret) break; -- 2.7.4