From patchwork Thu Jan 18 12:39:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gang Li X-Patchwork-Id: 13522811 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 BE2D3C4707B for ; Thu, 18 Jan 2024 12:39:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51E876B009B; Thu, 18 Jan 2024 07:39:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 47FCF6B009C; Thu, 18 Jan 2024 07:39:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 235886B009D; Thu, 18 Jan 2024 07:39:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0DC276B009B for ; Thu, 18 Jan 2024 07:39:43 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D090F1C16C3 for ; Thu, 18 Jan 2024 12:39:42 +0000 (UTC) X-FDA: 81692388204.29.CD2FF7B Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf18.hostedemail.com (Postfix) with ESMTP id 0402C1C0005 for ; Thu, 18 Jan 2024 12:39:40 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ur3J0rdF; spf=pass (imf18.hostedemail.com: domain of gang.li@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=gang.li@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705581581; 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=ASpAHuGdRPUV+HgZTqugmZdcOurDfxWM74nOvo7+sGU=; b=0YJCvGBvodacgGxQzNYZwbMh0PuDVVtt/YTgl/lw4B59TuttJmbgu2i2KLUnpjIwcr9qHh 6gsoCexD8xhyf9CAD1cxfYH4wnTYAVmZ58WwON6J7NIBbKkqjXpmc77f8iqhXafUfOFqGt E2KDr1A6icaQGkqRXrLr1/+yDCw92WU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705581581; a=rsa-sha256; cv=none; b=Yckbq8U+mpdUdFVC+sNGNCSLQd/n3Ugwokc7F1+M0TE3Go/vfVVOOA9sPS4nN4hY72TRLh LFS95grGmDWXc+WZ9ivn2EUf0u+rGGEcriXfcFSvYCkAYNZLt1gRW7iITnxWwv3RM5Q+9G mkpVS96zLEDiH4bCWanABZNSm2Nb+N4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ur3J0rdF; spf=pass (imf18.hostedemail.com: domain of gang.li@linux.dev designates 95.215.58.175 as permitted sender) smtp.mailfrom=gang.li@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1705581579; 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=ASpAHuGdRPUV+HgZTqugmZdcOurDfxWM74nOvo7+sGU=; b=ur3J0rdFoKrBHOlkNFrybggpwIpG8n2CJi1qMK84hfgA7E/TaiI1w9l1BBbJDIM1msiwBL gh9ANKc6TXOhjrRqRFpEcDA4BexqtUJi0cSm6TSIfI5jMiX4hdqupg3XRmcKAxMIh5GVCp t+89Jvp6g6U0aik5bbwsy7+HbeHAzPc= 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 v4 7/7] hugetlb: parallelize 1G hugetlb initialization Date: Thu, 18 Jan 2024 20:39:11 +0800 Message-Id: <20240118123911.88833-8-gang.li@linux.dev> In-Reply-To: <20240118123911.88833-1-gang.li@linux.dev> References: <20240118123911.88833-1-gang.li@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 0402C1C0005 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: tufsenso6qw8s4cdwyzurgzcc4pwcbjd X-HE-Tag: 1705581580-38376 X-HE-Meta: U2FsdGVkX1/aqiqPCxcG9EZduCoJTyaUIguU+U/WoQ208q10A6laAoDOEd3gsuSRShSOp/V1HC7xsrmbhYqdX69T2JBISxXzVUEvgqZjXBAR60hPaAj39mh33qFoCvcBu5tUsAITNt+r6L6+EOwVl2k/f4D6rZvQPiHcPX8r6cGJ/ocgcGtCZqTph+jeGsMGfnVCAzVcfzB2k9zFSJ3veunkz9+HbKe/NAEGPPGdMAkTW+fE96xzIXTm/95Enn8IdAfoRgSI1SyJ/LvhfDzi0LcFScLcWzujFrkzJkd+qxtm7VtHrulSx5eXGy7NMiP5IEh0MLuHxEH4ujE4JzNXBb/kO3GWJ70ALBv4INQT/T5kXIaLYPsFDqenLQviUME9rhea9ObocF5lrVqyFNSDVBNrYA8YRa1ZYrBbDBU0h/DE4+dF9GHvqBvBqs6q27x3qyacoyOj1grm9u/SG2u+4EIIFC5sibVYOBK4ULl/knyoV6wBtEsl8g4LcQbcL4FEvohQ21abcruRlvn8ivFyIoQvuMPJ5dMPaAo54LlGBfxHybWCEstpOrqSoaSOTD7OJ8CIxuphbfAOzIwAWgA6A5NcczkM/xzMItj4Ud13WRtizCyX2VGidhTIeP9U1lFObDynr2fZafpm9IiNVhzM5CaS4GJ53ixj6O1a0l4toz0rzRe0jDndcFCHo/dMn7nbw26MaFn5tP972I9fh1wiNiddYKxQ3jIf2Xt6/wINFBoJ9RO1RTuRNUdc9+OkixrhDESwV2/Y00l++uyAFpZvDPGLjiSZJtMvYqtqcO1fEYly92Z+5E+QsyWnyktfjNyXQe34YQOTOZmDPRsj1eqcboLKfX1KQvZ24+P9ukcvhiamvZa2UjWTz9QHR4Vl/jzUKnr8PawNy2RKMlnZIR4inWwd7k56X1j09jgv5xbgBjhdJNI3IrrpcaHV+Uw25uFvLw63dXITqApovm82kkQ qisT+nLd ib2tY3NsWaNSywWO1uhhFy1+ia+XDnz9ZhH1CtgiRjyQ4ZEg6Qv0Kjiv45Urz5TTYrcLLfZAFIyFBCgQc6rc+kMXxmbd8BGEOlR2m 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: Optimizing the initialization speed of 1G huge pages through parallelization. 1G hugetlbs are allocated from bootmem, a process that is already very fast and does not currently require optimization. Therefore, we focus on parallelizing only the initialization phase in `gather_bootmem_prealloc`. Here are some test results: test no patch(ms) patched(ms) saved ------------------- -------------- ------------- -------- 256c2t(4 node) 1G 4745 2024 57.34% 128c1t(2 node) 1G 3358 1712 49.02% 12t 1G 77000 18300 76.23% Signed-off-by: Gang Li Tested-by: David Rientjes --- include/linux/hugetlb.h | 2 +- mm/hugetlb.c | 42 +++++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index c1ee640d87b1..77b30a8c6076 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -178,7 +178,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage); extern int sysctl_hugetlb_shm_group; -extern struct list_head huge_boot_pages; +extern struct list_head huge_boot_pages[MAX_NUMNODES]; /* arch callbacks */ diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9b348ba418f5..2f4b77630ada 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -69,7 +69,7 @@ static bool hugetlb_cma_folio(struct folio *folio, unsigned int order) #endif static unsigned long hugetlb_cma_size __initdata; -__initdata LIST_HEAD(huge_boot_pages); +__initdata struct list_head huge_boot_pages[MAX_NUMNODES]; /* for command line parsing */ static struct hstate * __initdata parsed_hstate; @@ -3301,7 +3301,7 @@ int alloc_bootmem_huge_page(struct hstate *h, int nid) int __alloc_bootmem_huge_page(struct hstate *h, int nid) { struct huge_bootmem_page *m = NULL; /* initialize for clang */ - int nr_nodes, node; + int nr_nodes, node = nid; /* do node specific alloc */ if (nid != NUMA_NO_NODE) { @@ -3339,7 +3339,7 @@ int __alloc_bootmem_huge_page(struct hstate *h, int nid) huge_page_size(h) - PAGE_SIZE); /* Put them into a private list first because mem_map is not up yet */ INIT_LIST_HEAD(&m->list); - list_add(&m->list, &huge_boot_pages); + list_add(&m->list, &huge_boot_pages[node]); m->hstate = h; return 1; } @@ -3390,8 +3390,6 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h, /* Send list for bulk vmemmap optimization processing */ hugetlb_vmemmap_optimize_folios(h, folio_list); - /* Add all new pool pages to free lists in one lock cycle */ - spin_lock_irqsave(&hugetlb_lock, flags); list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { if (!folio_test_hugetlb_vmemmap_optimized(folio)) { /* @@ -3404,23 +3402,27 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h, HUGETLB_VMEMMAP_RESERVE_PAGES, pages_per_huge_page(h)); } + /* Subdivide locks to achieve better parallel performance */ + 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); } - spin_unlock_irqrestore(&hugetlb_lock, flags); } /* * Put bootmem huge pages into the standard lists after mem_map is up. * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages. */ -static void __init gather_bootmem_prealloc(void) +static void __init __gather_bootmem_prealloc(unsigned long start, unsigned long end, void *arg) + { + int nid = start; LIST_HEAD(folio_list); struct huge_bootmem_page *m; struct hstate *h = NULL, *prev_h = NULL; - list_for_each_entry(m, &huge_boot_pages, list) { + list_for_each_entry(m, &huge_boot_pages[nid], list) { struct page *page = virt_to_page(m); struct folio *folio = (void *)page; @@ -3453,6 +3455,22 @@ static void __init gather_bootmem_prealloc(void) prep_and_add_bootmem_folios(h, &folio_list); } +static void __init gather_bootmem_prealloc(void) +{ + struct padata_mt_job job = { + .thread_fn = __gather_bootmem_prealloc, + .fn_arg = NULL, + .start = 0, + .size = num_node_state(N_MEMORY), + .align = 1, + .min_chunk = 1, + .max_threads = num_node_state(N_MEMORY), + .numa_aware = true, + }; + + padata_do_multithreaded(&job); +} + static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) { unsigned long i; @@ -3602,6 +3620,14 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) return; } + /* hugetlb_hstate_alloc_pages will be called many times, init huge_boot_pages once*/ + if (huge_boot_pages[0].next == NULL) { + int i = 0; + + for (i = 0; i < MAX_NUMNODES; i++) + INIT_LIST_HEAD(&huge_boot_pages[i]); + } + /* do node specific alloc */ if (hugetlb_hstate_alloc_pages_specific_nodes(h)) return;