From patchwork Tue Jan 2 13:12:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 13509017 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 D2434C47074 for ; Tue, 2 Jan 2024 13:13:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62EEF6B0196; Tue, 2 Jan 2024 08:13:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5DC9D6B0198; Tue, 2 Jan 2024 08:13:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 455D56B0199; Tue, 2 Jan 2024 08:13:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 28C436B0196 for ; Tue, 2 Jan 2024 08:13:45 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0BBCEA07A0 for ; Tue, 2 Jan 2024 13:13:45 +0000 (UTC) X-FDA: 81634413210.03.D46DDB6 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) by imf26.hostedemail.com (Postfix) with ESMTP id 539B3140006 for ; Tue, 2 Jan 2024 13:13:43 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="rOSB7/ml"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of gang.li@linux.dev designates 95.215.58.171 as permitted sender) smtp.mailfrom=gang.li@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704201223; 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:in-reply-to:references:references:dkim-signature; bh=lq6odAt2f+wReMUr3LVecCPvhipoB8fbQDkgCZGAfFs=; b=ACh3XKV4s7vnCWuF6Uyw2WSIHKA0AY1FEb5smHNj8JjQaWGGQfqgvzvUK4h8UWHzIEBCpe gFcllNBGYCgMabTMb0ehvg4Tj1LGkBFscAWyEqmigzLKN+1dMTzTHoXMe2pRbv2jFYIoCl mUj6PXF58tM+aE0WFFhtBmv6acfc7qQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="rOSB7/ml"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of gang.li@linux.dev designates 95.215.58.171 as permitted sender) smtp.mailfrom=gang.li@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704201223; a=rsa-sha256; cv=none; b=Fx2Cp7GObodCin4chW+lJKTTee0UAAtjfjOYP5bW8QXvCgpkPo6JItCyheeqrw+KgN6rYI b61RVctGDJHuN8jrJenQXe9QXXKZtaRoplfjHG0xvRNohEeurGZHFYoCDiQKPA/+aP1qyZ RCAb05Km1WVF/iFBHFhayZFxSMEvxdw= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1704201222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lq6odAt2f+wReMUr3LVecCPvhipoB8fbQDkgCZGAfFs=; b=rOSB7/mlWJwm7Sgj4858g08l3Mah9AIYX19x1bg4TM3sAFEv9Sl0kgizs7/gcRTPswrfp0 vOOopJqL+bE11QDfDg2MV8rTX8iV8fJMNIn3ouKYEcKaZ82/9tLxyXxN5xbIYQpLmzlMAU 1lf390N2ueiFxZGQLNGz7dsM80AvN8Q= From: Gang Li To: David Hildenbrand , David Rientjes , Mike Kravetz , Muchun Song , Andrew Morton , Tim Chen Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, ligang.bdlg@bytedance.com, Gang Li Subject: [PATCH v3 6/7] hugetlb: parallelize 2M hugetlb allocation and initialization Date: Tue, 2 Jan 2024 21:12:48 +0800 Message-Id: <20240102131249.76622-7-gang.li@linux.dev> In-Reply-To: <20240102131249.76622-1-gang.li@linux.dev> References: <20240102131249.76622-1-gang.li@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 539B3140006 X-Stat-Signature: qbhuxhnu9u36q8thyaeiqdc9k57uy79y X-HE-Tag: 1704201223-582091 X-HE-Meta: U2FsdGVkX1+cDj+M4L6LRbRMmvxcVba06t2Yc5Fz1gok+XXiHZthxmXRwEMbpF7/sE8uFIqfjI73DobQcyGP7mYCYIbqC6jXFBP4hACbZ2LJHNY5UwHCad4zkCiD0j/7wsyuHan9p5h+knjDMI8ocwTpkesIZvvL7SrZmZZ9KzczL9dyMlV97RRM/4LqQ0HhMw1u2Vkor7XwEX2LTm5TNCFs31RtrICgzFdWpcv8A7LWvOZQOH4S+e0Zno4yTMtmNo0sADT6JnJAKn6JkgNzDr5MHGBH1sC2Gg8itIcJUVbRysymaQM+9Ww+t2tqOOUCeEtHLr2YDnNB8E0jBVDfFPoi41W/wvc97lft3VlcWfTDmBekwCVqfM7E3WEUhkgY0A0m+3DkXa/yFZjlU84lsWIECyssFHry0Z+l7Cu8PKMu/OlRdthhLzlo1+/2WyIoH6FRUx64vJ6fPPMlp1CvCAH85d08xnfV7yXf5BSFcOYK4yCX3pdmK2LgPThht1r85dMcnvKY6iIF8hXTbSNmUS5p8dnPVCB9jabaSBzz3MiDQaTruWJAOdLb2uzwGyWPRFaMtikgFRy1hiq5wccJQ/JEwCjFJqUxaJSPt5iNo50VPiiTkBzyyLBxbz/m/3P/svAkA8h+M1H44B+hGWiYAFvfWl9cr+1QVAetQmxXY72APhsjWHdUFRDPSlNJplmhvYNMSIJ/CoSVZ68KqMs1TLYqngxHJUaDVgMqGyEID96cosiY28zrO+4VPGuVcBSP4ONShKRkNwZOjhtFSl8jbuWFPyjIVSHXU7K9VubbBIrYnv8InBs4ubDQWY0vgL6FkdBNTPvAaoX1qT5epDRR+anMWEXODPIkJiOVKXmCi0S0Ti5+mZ2+Ck9IMP5jKUPhyigbHlBFcOfv8GORCuCHk5HU0aAW59y4ML7uoobbRPW+32BcQFe7kgD8zmN8Vdn0D9K2PuwmOxnL4R3pnQz tHEEUuNF rPF7ThDoJ5+lfG99Dm6dcCoU030Mw+9DYwevZ3k87ueqsn2UYurpuI/fypenBOnurhR+V 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: By distributing both the allocation and the initialization tasks across multiple threads, the initialization of 2M hugetlb will be faster, thereby improving the boot speed. Here are some test results: test no patch(ms) patched(ms) saved ------------------- -------------- ------------- -------- 256c2t(4 node) 2M 3336 1051 68.52% 128c1t(2 node) 2M 1943 716 63.15% Signed-off-by: Gang Li --- mm/hugetlb.c | 72 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a71bc1622b53b..d1629df5f399f 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -3510,6 +3511,38 @@ static void __init hugetlb_hstate_alloc_pages_report(unsigned long allocated, st } } +static void __init hugetlb_alloc_node(unsigned long start, unsigned long end, void *arg) +{ + struct hstate *h = (struct hstate *)arg; + int i, num = end - start; + nodemask_t node_alloc_noretry; + unsigned long flags; + int next_nid_to_alloc = 0; + + /* Bit mask controlling how hard we retry per-node allocations.*/ + nodes_clear(node_alloc_noretry); + + for (i = 0; i < num; ++i) { + struct folio *folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY], + &node_alloc_noretry, &next_nid_to_alloc); + if (!folio) + break; + spin_lock_irqsave(&hugetlb_lock, flags); + __prep_account_new_huge_page(h, folio_nid(folio)); + enqueue_hugetlb_folio(h, folio); + spin_unlock_irqrestore(&hugetlb_lock, flags); + cond_resched(); + } +} + +static void __init hugetlb_vmemmap_optimize_node(unsigned long start, unsigned long end, void *arg) +{ + struct hstate *h = (struct hstate *)arg; + int nid = start; + + hugetlb_vmemmap_optimize_folios(h, &h->hugepage_freelists[nid]); +} + static unsigned long __init hugetlb_hstate_alloc_pages_gigantic(struct hstate *h) { unsigned long i; @@ -3529,26 +3562,27 @@ static unsigned long __init hugetlb_hstate_alloc_pages_gigantic(struct hstate *h static unsigned long __init hugetlb_hstate_alloc_pages_non_gigantic(struct hstate *h) { - unsigned long i; - struct folio *folio; - LIST_HEAD(folio_list); - nodemask_t node_alloc_noretry; - - /* Bit mask controlling how hard we retry per-node allocations.*/ - nodes_clear(node_alloc_noretry); - - for (i = 0; i < h->max_huge_pages; ++i) { - folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY], - &node_alloc_noretry); - if (!folio) - break; - list_add(&folio->lru, &folio_list); - cond_resched(); - } - - prep_and_add_allocated_folios(h, &folio_list); + struct padata_mt_job job = { + .fn_arg = h, + .align = 1, + .numa_aware = true + }; - return i; + job.thread_fn = hugetlb_alloc_node; + job.start = 0; + job.size = h->max_huge_pages; + job.min_chunk = h->max_huge_pages / num_node_state(N_MEMORY) / 2; + job.max_threads = num_node_state(N_MEMORY) * 2; + padata_do_multithreaded(&job); + + job.thread_fn = hugetlb_vmemmap_optimize_node; + job.start = 0; + job.size = num_node_state(N_MEMORY); + job.min_chunk = 1; + job.max_threads = num_node_state(N_MEMORY); + padata_do_multithreaded(&job); + + return h->nr_huge_pages; } /*