From patchwork Fri Dec 8 02:52:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 13484696 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 BF588C4167B for ; Fri, 8 Dec 2023 02:53:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5643D6B0093; Thu, 7 Dec 2023 21:53:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C5966B0098; Thu, 7 Dec 2023 21:53:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 365896B009B; Thu, 7 Dec 2023 21:53:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 196AE6B0093 for ; Thu, 7 Dec 2023 21:53:20 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0009F403E1 for ; Fri, 8 Dec 2023 02:53:19 +0000 (UTC) X-FDA: 81542129718.20.0C726A5 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) by imf04.hostedemail.com (Postfix) with ESMTP id 55A9440002 for ; Fri, 8 Dec 2023 02:53:18 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=chBueub5; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of gang.li@linux.dev designates 91.218.175.173 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=1702003998; 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=kGZs/UomIOv18ew/FrkoM04DuS4bMFxPVmfT4OFKf7o=; b=29O5BhKWUOsul6ZLx2jVrIVJ1tdLHvbNRIZIyZlRLc/HYTTnQT2K0tj8apw9kGPOc2Fluo q+9w55Bn3JxOPcybLS7xG9mrWxQpxMw4wvRSSujuSnyVNwGmpXO950OTxPmw6xPwtEkJ0v AJGsJbaSF4V+ZmDIB3U+5dpAYrO6t60= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=chBueub5; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of gang.li@linux.dev designates 91.218.175.173 as permitted sender) smtp.mailfrom=gang.li@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702003998; a=rsa-sha256; cv=none; b=CERbSWYR6pJ0opMl4C0Twbd3nsGCeJsRu4eUeZD4V8L8sx3DLS6fsupq5sqOCq+5AnBCbV g9p250rDvE1dACu+DIiD+fqNr3VanBGNTfF9h+qr2C9k/Cv6cn7ldipKWgcStqt8apjG6m kQIQHlUjISMQlmI5htwz4MoZX2J+B3A= 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=1702003997; 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=kGZs/UomIOv18ew/FrkoM04DuS4bMFxPVmfT4OFKf7o=; b=chBueub5MeUxcphaWjUQxqFROsevyxpVPZfmiPXveRFoaHIrVUEHPmnvrGUbaR4eTRBiWA f94Dvmb/dAYuSx8K1f3+h/VIZx1F4a/NHLYb+wLTgBl21NY0lhI2iPxvxgQs9MN7KjUjor XiwWvmtR111DRnYq7EyqfBimXVLu1UU= From: Gang Li To: David Hildenbrand , David Rientjes , Mike Kravetz , Muchun Song , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, ligang.bdlg@bytedance.com, Gang Li Subject: [RFC PATCH v2 4/5] hugetlb: parallelize 2M hugetlb allocation and initialization Date: Fri, 8 Dec 2023 10:52:39 +0800 Message-Id: <20231208025240.4744-5-gang.li@linux.dev> In-Reply-To: <20231208025240.4744-1-gang.li@linux.dev> References: <20231208025240.4744-1-gang.li@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 55A9440002 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: hrgqx1xxpjtdtqnfbtjzixwdhm8s68dx X-HE-Tag: 1702003998-357542 X-HE-Meta: U2FsdGVkX18QRnk2gcrmTPwJnGAW0ypqGlj/D9NSQLQa840TNe7U1qHUsCwsx49Z2yWs6XwBJcVixyStyOx7MgMO+XsajsRD8Bb/QRbKMWlfGhKbgsvb3z+beecVsUNLXlFP7/nSGvIf6VcEjPEh7ZxYnR7IdBxn0DGpqC+/4CPvvXDtHshstUAKderdFtK2pdNyDG+1y2RlXCpH37ufFU8Wh83h17WTjHUSv6FCWqT5RXfAjt19HWe6fm64pAe5Jau3flLpuIoazk1bm+IND6P/fX0VmjmP75fwoHQ0BX1xdk/bNmqqEPgaFFWOGhamsjRwrooGu8YNnIddxqst7Y5OLc1HZM/Ro/4yMGYBPpgLIdjPMCCGFP8jZ2/UqMWfFZ9AFMBTXhqSj5v+bvvxvRW15W8qwhE/ymg88lKo/Bt6d139ieOGqheF/Pmf9eG8IYDvMDhRqNVRsEVptFxpczVzlROJ9LhNljEh64/JA4/fxolGqFXkzl9M9jbcNQPbNoUfVP5AYJ3nlapkd30Eh1H7QrpcrrO+Aajt9fTU7hjDPH01K4386snXJEvDbpIXVwJgLmMSMSnkEN1UQvGdPl9XuSyz/AtJesCipPwj1D3BZQZCkVj83LpjOCXka/hjWX1ZXE3V6TiSbhJ1XWXetMmVTeAhF8TwwnuQz5rTffqi9RdrYNWbF54d7d+2aPsQn+L0VWB5au/28+wgDT/seXBg6bVPI2o4g8GaPiwVHw9yGRjfBSvHqwzrymtXmel2ELUKXmFH9WtMFvcVby3hlLEFmDI46sY/1A5BPblh/t7okoaYkaLpb3RCPdF9DmJjAQmHdO59m3Z02aXYsYm9l1s3nBuyz2o0E3dYXDBB6UXmUiVYeBKEVH3lyEH0DXPZ+84sCLY8YtK45jzvH3lISCY9HeSIgBoEtnLPO52YZDdtxkfkNLVIdXt0UfLBJwiVgkOnxNPiPkjeFtdWgCC 6wc3Ppjw w+nyKOiKVVoeNtg56FmUkU61oJ98PitwqWA//qaLi1+bnXpupWGIbLwqgKJL0FnwnzYAEyzcX1WBep7E= 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. This patch can achieve 60% improvement in performance. test no patch(ms) patched(ms) saved ------------------- -------------- ------------- -------- 256c2t(4 node) 2M 2624 956 63.57% 128c1t(2 node) 2M 1788 684 61.74% Signed-off-by: Gang Li --- mm/hugetlb.c | 71 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8de1653fc4c4f..033e359fdb86b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -3502,6 +3503,37 @@ 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; + + /* 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); + 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; @@ -3521,26 +3553,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; } /*