hugetlb: parallelize hugetlb page init on boot
Subsystems (6)
v6 cover hugetlb: parallelize hugetlb page init on boot
Hi all, hugetlb init parallelization has now been updated to v6.
This version is tested on mm/mm-stable.
Since the release of v5, there have been some scattered discussions, they have primarily centered around two issues. Both of these issues have now been resolved, leading to the release of v6.
Two updates in v6
-----------------
- Fix a Kconfig warning
hugetlb parallelization depends on PADATA, and PADATA depends on SMP. When SMP is not selected, selecting PADATA will cause a warning: "WARNING: unmet direct dependencies detected for PADATA". So HUGETLBFS can only select PADATA when SMP is set.
padata.c will not be compiled if !SMP, but padata_do_multithreaded is still used in this series for hugetlb parallel init. So it is necessary to implement a serial version in padata.h.
- Fix a potential bug in gather_bootmem_prealloc_node padata_do_multithreaded implementation guarantees that each gather_bootmem_prealloc_node task handles one node. However, the API described in padata_do_multithreaded comment indicates that padata_do_multithreaded also can assign multiple nodes to a gather_bootmem_prealloc_node task.
To avoid potential bug from future changes in padata_do_multithreaded, gather_bootmem_prealloc_parallel is introduced to wrap the gather_bootmem_prealloc_node.
More details in: https://lore.kernel.org/r/[email protected]
Introduction
------------
Hugetlb initialization during boot takes up a considerable amount of time. For instance, on a 2TB system, initializing 1,800 1GB huge pages takes 1-2 seconds out of 10 seconds. Initializing 11,776 1GB pages on a 12TB Intel host takes more than 1 minute[1]. This is a noteworthy figure.
Inspired by [2] and [3], hugetlb initialization can also be accelerated through parallelization. Kernel already has infrastructure like padata_do_multithreaded, this patch uses it to achieve effective results by minimal modifications.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
max_threads
-----------
This patch use padata_do_multithreaded like this:
``` job.max_threads = num_node_state(N_MEMORY) * multiplier; padata_do_multithreaded(&job); ```
To fully utilize the CPU, the number of parallel threads needs to be carefully considered. max_threads = num_node_state(N_MEMORY) does not fully utilize the CPU, so we need to multiply it by a multiplier.
Tests below indicate that a multiplier of 2 significantly improves performance, and although larger values also provide improvements, the gains are marginal.
multiplier 1 2 3 4 5
------------ ------- ------- ------- ------- -------
256G 2node 358ms 215ms 157ms 134ms 126ms 2T 4node 979ms 679ms 543ms 489ms 481ms 50G 2node 71ms 44ms 37ms 30ms 31ms
Therefore, choosing 2 as the multiplier strikes a good balance between enhancing parallel processing capabilities and maintaining efficient resource management.
Test result
-----------
test case 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%256c2T(4 node) 2M 3336 1051 68.52% 128c1T(2 node) 2M 1943 716 63.15%
Change log
----------
Changes in v6:
- Fix a Kconfig warning
- Fix a potential bug in gather_bootmem_prealloc_node
Changes in v5:
- https://lore.kernel.org/lkml/[email protected]/
- Use prep_and_add_allocated_folios in 2M hugetlb parallelization
- Update huge_boot_pages in arch/powerpc/mm/hugetlbpage.c
- Revise struct padata_mt_job comment
- Add 'max_threads' section in cover letter
- Collect more Reviewed-by
Changes in v4:
- https://lore.kernel.org/r/[email protected]
- Make padata_do_multithreaded dispatch all jobs with a global iterator
- Revise commit message
- Rename some functions
- Collect Tested-by and Reviewed-by
Changes in v3:
- https://lore.kernel.org/all/[email protected]/
- Select CONFIG_PADATA as we use padata_do_multithreaded
- Fix a race condition in h->next_nid_to_alloc
- Fix local variable initialization issues
- Remove RFC tag
Changes in v2:
- https://lore.kernel.org/all/[email protected]/
- Reduce complexity with padata_do_multithreaded
- Support 1G hugetlb
v1:
- https://lore.kernel.org/all/[email protected]/
- parallelize 2M hugetlb initialization with workqueue
Gang Li (8):
hugetlb: code clean for hugetlb_hstate_alloc_pages
hugetlb: split hugetlb_hstate_alloc_pages
hugetlb: pass *next_nid_to_alloc directly to
for_each_node_mask_to_alloc
padata: dispatch works on different nodes
padata: downgrade padata_do_multithreaded to serial execution for
non-SMP
hugetlb: have CONFIG_HUGETLBFS select CONFIG_PADATA
hugetlb: parallelize 2M hugetlb allocation and initialization
hugetlb: parallelize 1G hugetlb initialization
arch/powerpc/mm/hugetlbpage.c | 2 +- fs/Kconfig | 1 + include/linux/hugetlb.h | 2 +- include/linux/padata.h | 14 +- kernel/padata.c | 14 +- mm/hugetlb.c | 241 +++++++++++++++++++++++----------- mm/mm_init.c | 1 + 7 files changed, 190 insertions(+), 85 deletions(-)
Signature · 1 line
2.20.1
PATCH v6 1/8 hugetlb: code clean for hugetlb_hstate_alloc_pages
The readability of hugetlb_hstate_alloc_pages is poor. By cleaning the code, its readability can be improved, facilitating future modifications.
This patch extracts two functions to reduce the complexity of hugetlb_hstate_alloc_pages and has no functional changes.
- hugetlb_hstate_alloc_pages_node_specific() to handle iterates through each online node and performs allocation if necessary.
- hugetlb_hstate_alloc_pages_report() report error during allocation. And the value of h->max_huge_pages is updated accordingly.
--- mm/hugetlb.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-)
mm/hugetlb.c +29 -17
@@ -3482,6 +3482,33 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid) h->max_huge_pages_node[nid] = i; } +static bool __init hugetlb_hstate_alloc_pages_specific_nodes (struct hstate *h) +{ +int i; +bool node_specific_alloc = false; + +for_each_online_node (i) { +if (h->max_huge_pages_node[i] >0 ) { +hugetlb_hstate_alloc_pages_onenode (h, i); + node_specific_alloc = true; + } + } + +return node_specific_alloc; +} + +static void __init hugetlb_hstate_alloc_pages_errcheck (unsigned long allocated,struct hstate *h) +{ +if (allocated < h->max_huge_pages) { +char buf[32 ]; + +string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); +pr_warn ("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n" , + h->max_huge_pages, buf, allocated); + h->max_huge_pages = allocated; + } +} +/* * NOTE: this routine is called in different contexts for gigantic and * non-gigantic pages.
@@ -3499,7 +3526,6 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)struct folio *folio;LIST_HEAD (folio_list);nodemask_t *node_alloc_noretry; -bool node_specific_alloc = false;/* skip gigantic hugepages allocation if hugetlb_cma enabled */ if (hstate_is_gigantic (h) && hugetlb_cma_size) {
@@ -3508,14 +3534,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) }/* do node specific alloc */ -for_each_online_node (i) { -if (h->max_huge_pages_node[i] >0 ) { -hugetlb_hstate_alloc_pages_onenode (h, i); - node_specific_alloc = true; - } - } - -if (node_specific_alloc) +if (hugetlb_hstate_alloc_pages_specific_nodes (h))return ;/* below will do all node balanced alloc */
@@ -3558,14 +3577,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)/* list will be empty if hstate_is_gigantic */ prep_and_add_allocated_folios (h, &folio_list); -if (i < h->max_huge_pages) { -char buf[32 ]; - -string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); -pr_warn ("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n" , - h->max_huge_pages, buf, i); - h->max_huge_pages = i; - } +hugetlb_hstate_alloc_pages_errcheck (i, h);kfree (node_alloc_noretry); }
Signature · 1 line
2.20.1
PATCH v6 2/8 hugetlb: split hugetlb_hstate_alloc_pages
In reply to Gang Li1G and 2M huge pages have different allocation and initialization logic, which leads to subtle differences in parallelization. Therefore, it is appropriate to split hugetlb_hstate_alloc_pages into gigantic and non-gigantic.
This patch has no functional changes.
--- mm/hugetlb.c | 87 ++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 44 deletions(-)
mm/hugetlb.c +43 -44
@@ -3509,6 +3509,43 @@ static void __init hugetlb_hstate_alloc_pages_errcheck(unsigned long allocated, } } +static unsigned long __init hugetlb_gigantic_pages_alloc_boot (struct hstate *h) +{ +unsigned long i; + +for (i =0 ; i < h->max_huge_pages; ++i) { +if (!alloc_bootmem_huge_page (h,NUMA_NO_NODE )) +break ; +cond_resched (); + } + +return i; +} + +static unsigned long __init hugetlb_pages_alloc_boot (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); + +return i; +} +/* * NOTE: this routine is called in different contexts for gigantic and * non-gigantic pages.
@@ -3522,10 +3559,7 @@ static void __init hugetlb_hstate_alloc_pages_errcheck(unsigned long allocated,*/ static void __init hugetlb_hstate_alloc_pages (struct hstate *h) { -unsigned long i; -struct folio *folio; -LIST_HEAD (folio_list); -nodemask_t *node_alloc_noretry; +unsigned long allocated;/* skip gigantic hugepages allocation if hugetlb_cma enabled */ if (hstate_is_gigantic (h) && hugetlb_cma_size) {
@@ -3538,47 +3572,12 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)return ;/* below will do all node balanced alloc */ -if (!hstate_is_gigantic (h)) { -/* -* Bit mask controlling how hard we retry per-node allocations. -* Ignore errors as lower level routines can deal with -* node_alloc_noretry == NULL. If this kmalloc fails at boot -* time, we are likely in bigger trouble. -*/ - node_alloc_noretry =kmalloc (sizeof (*node_alloc_noretry), -GFP_KERNEL ); - }else { -/* allocations done at boot time */ - node_alloc_noretry =NULL ; - } - -/* bit mask controlling how hard we retry per-node allocations */ -if (node_alloc_noretry) -nodes_clear (*node_alloc_noretry); - -for (i =0 ; i < h->max_huge_pages; ++i) { -if (hstate_is_gigantic (h)) { -/* -* gigantic pages not added to list as they are not -* added to pools now. -*/ -if (!alloc_bootmem_huge_page (h,NUMA_NO_NODE )) -break ; - }else { - folio =alloc_pool_huge_folio (h, &node_states[N_MEMORY ], - node_alloc_noretry); -if (!folio) -break ; -list_add (&folio->lru, &folio_list); - } -cond_resched (); - } - -/* list will be empty if hstate_is_gigantic */ -prep_and_add_allocated_folios (h, &folio_list); +if (hstate_is_gigantic (h)) + allocated =hugetlb_gigantic_pages_alloc_boot (h); +else + allocated =hugetlb_pages_alloc_boot (h); -hugetlb_hstate_alloc_pages_errcheck (i, h); -kfree (node_alloc_noretry); +hugetlb_hstate_alloc_pages_errcheck (allocated, h); }static void __init hugetlb_init_hstates (void )
Signature · 1 line
2.20.1
PATCH v6 3/8 hugetlb: pass *next_nid_to_alloc directly to for_each_node_mask_to_alloc
In reply to Gang LiWith parallelization of hugetlb allocation across different threads, each thread works on a differnet node to allocate pages from, instead of all allocating from a common node h->next_nid_to_alloc. To address this, it's necessary to assign a separate next_nid_to_alloc for each thread.
Consequently, the hstate_next_node_to_alloc and for_each_node_mask_to_alloc have been modified to directly accept a *next_nid_to_alloc parameter, ensuring thread-specific allocation and avoiding concurrent access issues.
--- mm/hugetlb.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
mm/hugetlb.c +12 -10
@@ -1464,15 +1464,15 @@ static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)* next node from which to allocate, handling wrap at end of node * mask. */ -static int hstate_next_node_to_alloc (struct hstate *h, +static int hstate_next_node_to_alloc (int *next_node,nodemask_t *nodes_allowed) {int nid;VM_BUG_ON (!nodes_allowed); - nid =get_valid_node_allowed (h->next_nid_to_alloc, nodes_allowed); - h->next_nid_to_alloc =next_node_allowed (nid, nodes_allowed); + nid =get_valid_node_allowed (*next_node, nodes_allowed); + *next_node =next_node_allowed (nid, nodes_allowed);return nid; }
@@ -1495,10 +1495,10 @@ static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)return nid; } -#define for_each_node_mask_to_alloc (hs, nr_nodes, node, mask) \ +#define for_each_node_mask_to_alloc (next_node, nr_nodes, node, mask) \ for (nr_nodes = nodes_weight(*mask); \ nr_nodes > 0 && \ - ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \ + ((node = hstate_next_node_to_alloc(next_node, mask)) || 1); \ nr_nodes--)#define for_each_node_mask_to_free (hs, nr_nodes, node, mask) \
@@ -2350,12 +2350,13 @@ static void prep_and_add_allocated_folios(struct hstate *h,*/ static struct folio *alloc_pool_huge_folio (struct hstate *h,nodemask_t *nodes_allowed, -nodemask_t *node_alloc_noretry) +nodemask_t *node_alloc_noretry, +int *next_node) {gfp_t gfp_mask =htlb_alloc_mask (h) | __GFP_THISNODE;int nr_nodes, node; -for_each_node_mask_to_alloc (h, nr_nodes, node, nodes_allowed) { +for_each_node_mask_to_alloc (next_node, nr_nodes, node, nodes_allowed) {struct folio *folio; folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, node,
@@ -3310,7 +3311,7 @@ int __alloc_bootmem_huge_page(struct hstate *h, int nid) goto found; }/* allocate from next node when distributing huge pages */ -for_each_node_mask_to_alloc (h, nr_nodes, node, &node_states[N_MEMORY ]) { +for_each_node_mask_to_alloc (&h->next_nid_to_alloc, nr_nodes, node, &node_states[N_MEMORY ]) { m =memblock_alloc_try_nid_raw (huge_page_size (h),huge_page_size (h),0 ,MEMBLOCK_ALLOC_ACCESSIBLE , node);
@@ -3679,7 +3680,7 @@ static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,VM_BUG_ON (delta !=-1 && delta !=1 );if (delta <0 ) { -for_each_node_mask_to_alloc (h, nr_nodes, node, nodes_allowed) { +for_each_node_mask_to_alloc (&h->next_nid_to_alloc, nr_nodes, node, nodes_allowed) {if (h->surplus_huge_pages_node[node]) goto found; }
@@ -3794,7 +3795,8 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,cond_resched (); folio =alloc_pool_huge_folio (h, nodes_allowed, - node_alloc_noretry); + node_alloc_noretry, + &h->next_nid_to_alloc);if (!folio) {prep_and_add_allocated_folios (h, &page_list);spin_lock_irq (&hugetlb_lock);
Signature · 1 line
2.20.1
PATCH v6 4/8 padata: dispatch works on different nodes
In reply to Gang LiWhen a group of tasks that access different nodes are scheduled on the same node, they may encounter bandwidth bottlenecks and access latency.
Thus, numa_aware flag is introduced here, allowing tasks to be distributed across different nodes to fully utilize the advantage of multi-node systems.
--- include/linux/padata.h | 2 ++ kernel/padata.c | 14 ++++++++++++-- mm/mm_init.c | 1 + 3 files changed, 15 insertions(+), 2 deletions(-)
include/linux/padata.h +2 -0
@@ -137,6 +137,7 @@ struct padata_shell {* appropriate for one worker thread to do at once. * @max_threads: Max threads to use for the job, actual number may be less * depending on task size and minimum chunk size. +* @numa_aware: Distribute jobs to different nodes with CPU in a round robin fashion. */ struct padata_mt_job {void (*thread_fn)(unsigned long start,unsigned long end,void *arg);
@@ -146,6 +147,7 @@ struct padata_mt_job {unsigned long align;unsigned long min_chunk;int max_threads; +bool numa_aware; };/**
kernel/padata.c +12 -2
@@ -485,7 +485,8 @@ void __init padata_do_multithreaded(struct padata_mt_job *job)struct padata_work my_work, *pw;struct padata_mt_job_state ps;LIST_HEAD (works); -int nworks; +int nworks, nid; +static atomic_t last_used_nid__initdata ;if (job->size ==0 )return ;
@@ -517,7 +518,16 @@ void __init padata_do_multithreaded(struct padata_mt_job *job) ps.chunk_size =roundup (ps.chunk_size, job->align);list_for_each_entry (pw, &works, pw_list) -queue_work (system_unbound_wq, &pw->pw_work); +if (job->numa_aware) { +int old_node =atomic_read (&last_used_nid); + +do { + nid =next_node_in (old_node, node_states[N_CPU ]); + }while (!atomic_try_cmpxchg (&last_used_nid, &old_node, nid)); +queue_work_node (nid, system_unbound_wq, &pw->pw_work); + }else { +queue_work (system_unbound_wq, &pw->pw_work); + }/* Use the current thread, which saves starting a workqueue worker. */ padata_work_init (&my_work, padata_mt_helper, &ps,PADATA_WORK_ONSTACK );
mm/mm_init.c +1 -0
@@ -2231,6 +2231,7 @@ static int __init deferred_init_memmap(void *data) .align = PAGES_PER_SECTION, .min_chunk = PAGES_PER_SECTION, .max_threads = max_threads, + .numa_aware = false, };padata_do_multithreaded (&job);
Signature · 1 line
2.20.1
Daniel Jordan
maintainerRe: [PATCH v6 4/8] padata: dispatch works on different nodes
Hi,
Quoted 22 lines · Gang Li
When a group of tasks that access different nodes are scheduled on the same node, they may encounter bandwidth bottlenecks and access latency.
Thus,
numa_awareflag is introduced here, allowing tasks to be distributed across different nodes to fully utilize the advantage of multi-node systems.Signed-off-by Gang Li <[email protected]>Tested-by David Rientjes <[email protected]>Reviewed-by Muchun Song <[email protected]>Reviewed-by Tim Chen <[email protected]>--- include/linux/padata.h | 2 ++ kernel/padata.c | 14 ++++++++++++-- mm/mm_init.c | 1 + 3 files changed, 15 insertions(+), 2 deletions(-)include/linux/padata.h +0
-0
include/linux/padata.h struct padata_shell { · line 140
Quoted 4 lines
include/linux/padata.h +1
-0@@ -137,6 +137,7 @@ struct padata_shell {* appropriate for one worker thread to do at once. * @max_threads: Max threads to use for the job, actual number may be less * depending on task size and minimum chunk size. +* @numa_aware: Distribute jobs to different nodes with CPU in a round robin fashion.
numa_interleave seems more descriptive.
Quoted 16 lines
include/linux/padata.h +1
-0*/ struct padata_mt_job {void (*thread_fn)(unsigned long start,unsigned long end,void *arg);@@ -146,6 +147,7 @@ struct padata_mt_job {unsigned long align;unsigned long min_chunk;int max_threads; +bool numa_aware; };/** kernel/padata.c +0
-0
kernel/padata.c void __init padata_do_multithreaded(struct padata_mt_job *job) · line 489
Quoted 6 lines
kernel/padata.c +2
-1@@ -485,7 +485,8 @@ void __init padata_do_multithreaded(struct padata_mt_job *job)struct padata_work my_work, *pw;struct padata_mt_job_state ps;LIST_HEAD (works); -int nworks; +int nworks, nid; +static atomic_t last_used_nid__initdata ;
nit, move last_used_nid up so it's below load_balance_factor to keep that nice tree shape
Quoted 4 lines
kernel/padata.c +0
-0if (job->size ==0 )return ;
kernel/padata.c void __init padata_do_multithreaded(struct padata_mt_job *job) · line 526
Quoted 10 lines
kernel/padata.c +6
-1@@ -517,7 +518,16 @@ void __init padata_do_multithreaded(struct padata_mt_job *job) ps.chunk_size =roundup (ps.chunk_size, job->align);list_for_each_entry (pw, &works, pw_list) -queue_work (system_unbound_wq, &pw->pw_work); +if (job->numa_aware) { +int old_node =atomic_read (&last_used_nid); + +do { + nid =next_node_in (old_node, node_states[N_CPU ]); + }while (!atomic_try_cmpxchg (&last_used_nid, &old_node, nid));
There aren't concurrent NUMA-aware _do_multithreaded calls now, so an atomic per work seems like an unnecessary expense for guarding against possible uneven thread distribution in the future. Non-atomic access instead?
Quoted 22 lines
kernel/padata.c +4
-0+queue_work_node (nid, system_unbound_wq, &pw->pw_work); + }else { +queue_work (system_unbound_wq, &pw->pw_work); + }/* Use the current thread, which saves starting a workqueue worker. */ padata_work_init (&my_work, padata_mt_helper, &ps,PADATA_WORK_ONSTACK );mm/mm_init.c +1
-0@@ -2231,6 +2231,7 @@ static int __init deferred_init_memmap(void *data) .align = PAGES_PER_SECTION, .min_chunk = PAGES_PER_SECTION, .max_threads = max_threads, + .numa_aware = false, };padata_do_multithreaded (&job);Signature · 1 line
2.20.1
Re: [PATCH v6 4/8] padata: dispatch works on different nodes
Quoted 64 lines · Daniel Jordan
Hi,
Quoted 22 lines · Gang Li
When a group of tasks that access different nodes are scheduled on the same node, they may encounter bandwidth bottlenecks and access latency.
Thus,
numa_awareflag is introduced here, allowing tasks to be distributed across different nodes to fully utilize the advantage of multi-node systems.Signed-off-by Gang Li <[email protected]>Tested-by David Rientjes <[email protected]>Reviewed-by Muchun Song <[email protected]>Reviewed-by Tim Chen <[email protected]>--- include/linux/padata.h | 2 ++ kernel/padata.c | 14 ++++++++++++-- mm/mm_init.c | 1 + 3 files changed, 15 insertions(+), 2 deletions(-)include/linux/padata.h +0
-0include/linux/padata.h struct padata_shell { · line 140
Quoted 4 lines
include/linux/padata.h +1
-0@@ -137,6 +137,7 @@ struct padata_shell {* appropriate for one worker thread to do at once. * @max_threads: Max threads to use for the job, actual number may be less * depending on task size and minimum chunk size. +* @numa_aware: Distribute jobs to different nodes with CPU in a round robin fashion.
numa_interleaveseems more descriptive.Quoted 16 lines
include/linux/padata.h +1
-0*/ struct padata_mt_job {void (*thread_fn)(unsigned long start,unsigned long end,void *arg);@@ -146,6 +147,7 @@ struct padata_mt_job {unsigned long align;unsigned long min_chunk;int max_threads; +bool numa_aware; };/** kernel/padata.c +0
-0kernel/padata.c void __init padata_do_multithreaded(struct padata_mt_job *job) · line 489
Quoted 6 lines
kernel/padata.c +2
-1@@ -485,7 +485,8 @@ void __init padata_do_multithreaded(struct padata_mt_job *job)struct padata_work my_work, *pw;struct padata_mt_job_state ps;LIST_HEAD (works); -int nworks; +int nworks, nid; +static atomic_t last_used_nid__initdata ;nit, move
last_used_nidup so it's belowload_balance_factorto keep that nice tree shapeQuoted 6 lines
kernel/padata.c +0
-0if (job->size ==0 )return ;@@ -517,7 +518,16 @@ void __init padata_do_multithreaded(struct padata_mt_job *job) ps.chunk_size =roundup (ps.chunk_size, job->align);
Quoted 11 lines
kernel/padata.c void __init padata_do_multithreaded(struct padata_mt_job *job) · line 526
Quoted 8 lines
kernel/padata.c +6
-1list_for_each_entry (pw, &works, pw_list) -queue_work (system_unbound_wq, &pw->pw_work); +if (job->numa_aware) { +int old_node =atomic_read (&last_used_nid); + +do { + nid =next_node_in (old_node, node_states[N_CPU ]); + }while (!atomic_try_cmpxchg (&last_used_nid, &old_node, nid));There aren't concurrent NUMA-aware
_do_multithreadedcalls now, so an atomic per work seems like an unnecessary expense for guarding against
Hi Daniel,
Yes, this is not necessary. But I think this operation is infrequent, so the burden shouldn't be too great?
Quoted 26 lines
possible uneven thread distribution in the future. Non-atomic access instead?
Quoted 22 lines
kernel/padata.c +4
-0+queue_work_node (nid, system_unbound_wq, &pw->pw_work); + }else { +queue_work (system_unbound_wq, &pw->pw_work); + }/* Use the current thread, which saves starting a workqueue worker. */ padata_work_init (&my_work, padata_mt_helper, &ps,PADATA_WORK_ONSTACK );mm/mm_init.c +1
-0@@ -2231,6 +2231,7 @@ static int __init deferred_init_memmap(void *data) .align = PAGES_PER_SECTION, .min_chunk = PAGES_PER_SECTION, .max_threads = max_threads, + .numa_aware = false, };padata_do_multithreaded (&job);Signature · 1 line
2.20.1
Re: [PATCH v6 4/8] padata: dispatch works on different nodes
Hello Gang,
Quoted 16 lines · Gang Li
Quoted 14 lines · Daniel Jordan
kernel/padata.c void __init padata_do_multithreaded(struct padata_mt_job *job) · line 525
Quoted 10 lines · Gang Li
kernel/padata.c +6
-1@@ -517,7 +518,16 @@ void __init padata_do_multithreaded(struct padata_mt_job *job) ps.chunk_size =roundup (ps.chunk_size, job->align);list_for_each_entry (pw, &works, pw_list) -queue_work (system_unbound_wq, &pw->pw_work); +if (job->numa_aware) { +int old_node =atomic_read (&last_used_nid); + +do { + nid =next_node_in (old_node, node_states[N_CPU ]); + }while (!atomic_try_cmpxchg (&last_used_nid, &old_node, nid));There aren't concurrent NUMA-aware
_do_multithreadedcalls now, so an atomic per work seems like an unnecessary expense for guarding against
Quoted 4 lines
Hi Daniel,
Yes, this is not necessary. But I think this operation is infrequent, so the burden shouldn't be too great?
I can only guess, but I bet you're right. It's also that people might wonder what the atomic guards against, so non-atomic would make the code a bit easier to understand. Either way, looks fine.
PATCH v6 5/8 padata: downgrade padata_do_multithreaded to serial execution for non-SMP
In reply to Gang Lihugetlb parallelization depends on PADATA, and PADATA depends on SMP.
PADATA consists of two distinct functionality: One part is padata_do_multithreaded which disregards order and simply divides tasks into several groups for parallel execution. Hugetlb init parallelization depends on padata_do_multithreaded.
The other part is composed of a set of APIs that, while handling data in an out-of-order parallel manner, can eventually return the data with ordered sequence. Currently Only crypto/pcrypt.c use them.
All users of PADATA of non-SMP case currently only use padata_do_multithreaded. It is easy to implement a serial one in include/linux/padata.h. And it is not necessary to implement another functionality unless the only user of crypto/pcrypt.c does not depend on SMP in the future.
--- include/linux/padata.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/padata.h +8 -4
@@ -180,10 +180,6 @@ struct padata_instance {#ifdef CONFIG_PADATA extern void __init padata_init (void ); -#else -static inline void __init padata_init (void ) {} -#endif -extern struct padata_instance *padata_alloc (const char *name);extern void padata_free (struct padata_instance *pinst);extern struct padata_shell *padata_alloc_shell (struct padata_instance *pinst);
@@ -194,4 +190,12 @@ extern void padata_do_serial(struct padata_priv *padata);extern void __init padata_do_multithreaded (struct padata_mt_job *job);extern int padata_set_cpumask (struct padata_instance *pinst,int cpumask_type,cpumask_var_t cpumask); +#else +static inline void __init padata_init (void ) {} +static inline void __init padata_do_multithreaded (struct padata_mt_job *job) +{ + job->thread_fn (job->start, job->start + job->size, job->fn_arg); +} +#endif +#endif
Signature · 1 line
2.20.1
Re: [PATCH v6 5/8] padata: downgrade padata_do_multithreaded to serial execution for non-SMP
Quoted 39 lines · Gang Li
hugetlb parallelization depends on PADATA, and PADATA depends on SMP.
PADATA consists of two distinct functionality: One part is
padata_do_multithreadedwhich disregards order and simply divides tasks into several groups for parallel execution. Hugetlb init parallelization depends onpadata_do_multithreaded.The other part is composed of a set of APIs that, while handling data in an out-of-order parallel manner, can eventually return the data with ordered sequence. Currently Only
crypto/pcrypt.cuse them.All users of PADATA of non-SMP case currently only use
padata_do_multithreaded. It is easy to implement a serial one ininclude/linux/padata.h. And it is not necessary to implement another functionality unless the only user ofcrypto/pcrypt.cdoes not depend on SMP in the future.Signed-off-by Gang Li <[email protected]>Tested-by Paul E. McKenney <[email protected]>--- include/linux/padata.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)include/linux/padata.h +0
-4@@ -180,10 +180,6 @@ struct padata_instance {#ifdef CONFIG_PADATA extern void __init padata_init (void ); -#else -static inline void __init padata_init (void ) {} -#endif -extern struct padata_instance *padata_alloc (const char *name);extern void padata_free (struct padata_instance *pinst);extern struct padata_shell *padata_alloc_shell (struct padata_instance *pinst);
include/linux/padata.h extern void padata_do_serial(struct padata_priv *padata); · line 196
Quoted 7 lines
include/linux/padata.h +4
-0@@ -194,4 +190,12 @@ extern void padata_do_serial(struct padata_priv *padata);extern void __init padata_do_multithreaded (struct padata_mt_job *job);extern int padata_set_cpumask (struct padata_instance *pinst,int cpumask_type,cpumask_var_t cpumask); +#else +static inline void __init padata_init (void ) {} +static inline void __init padata_do_multithreaded (struct padata_mt_job *job) +{
An early return here for zero-sized jobs is consistent with the CONFIG_PADATA version and avoids hugetlb_pages_alloc_boot taking a lock and flushing the tlb when there's no work to do.
With that,
Quoted 8 lines
include/linux/padata.h +4
-0+ job->thread_fn (job->start, job->start + job->size, job->fn_arg); +} +#endif +#endif Signature · 1 line
2.20.1
Re: [PATCH v6 5/8] padata: downgrade padata_do_multithreaded to serial execution for non-SMP
Quoted 43 lines · Daniel Jordan
Quoted 42 lines · Gang Li
hugetlb parallelization depends on PADATA, and PADATA depends on SMP.
PADATA consists of two distinct functionality: One part is
padata_do_multithreadedwhich disregards order and simply divides tasks into several groups for parallel execution. Hugetlb init parallelization depends onpadata_do_multithreaded.The other part is composed of a set of APIs that, while handling data in an out-of-order parallel manner, can eventually return the data with ordered sequence. Currently Only
crypto/pcrypt.cuse them.All users of PADATA of non-SMP case currently only use
padata_do_multithreaded. It is easy to implement a serial one ininclude/linux/padata.h. And it is not necessary to implement another functionality unless the only user ofcrypto/pcrypt.cdoes not depend on SMP in the future.Signed-off-by Gang Li <[email protected]>Tested-by Paul E. McKenney <[email protected]>--- include/linux/padata.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)include/linux/padata.h +0
-4@@ -180,10 +180,6 @@ struct padata_instance {#ifdef CONFIG_PADATA extern void __init padata_init (void ); -#else -static inline void __init padata_init (void ) {} -#endif -extern struct padata_instance *padata_alloc (const char *name);extern void padata_free (struct padata_instance *pinst);extern struct padata_shell *padata_alloc_shell (struct padata_instance *pinst);@@ -194,4 +190,12 @@ extern void padata_do_serial(struct padata_priv *padata);extern void __init padata_do_multithreaded (struct padata_mt_job *job);extern int padata_set_cpumask (struct padata_instance *pinst,int cpumask_type,cpumask_var_t cpumask);
Quoted 8 lines
include/linux/padata.h extern void padata_do_serial(struct padata_priv *padata); · line 196
Quoted 4 lines
include/linux/padata.h +4
-0+#else +static inline void __init padata_init (void ) {} +static inline void __init padata_do_multithreaded (struct padata_mt_job *job) +{An early return here for zero-sized jobs is consistent with the
CONFIG_PADATAversion and avoidshugetlb_pages_alloc_boottaking a lock and flushing the tlb when there's no work to do.
That's reasonable, thanks!
Since it's single-threaded, the lock isn't contested, but tlb does need to be treated with caution.
Quoted 5 lines
With that,
Acked-by Daniel Jordan <[email protected]>
And thanks again.
Quoted 8 lines
Quoted 8 lines
include/linux/padata.h +4
-0+ job->thread_fn (job->start, job->start + job->size, job->fn_arg); +} +#endif +#endif Signature · 1 line
2.20.1
PATCH v6 6/8 hugetlb: have CONFIG_HUGETLBFS select CONFIG_PADATA
In reply to Gang LiAllow hugetlb use padata_do_multithreaded for parallel initialization. Select CONFIG_PADATA in this case.
--- fs/Kconfig | 1 + 1 file changed, 1 insertion(+)
fs/Kconfig +1 -0
@@ -262,6 +262,7 @@ menuconfig HUGETLBFS depends on X86 || SPARC64 || ARCH_SUPPORTS_HUGETLBFS || BROKEN depends on (SYSFS || SYSCTL) select MEMFD_CREATE + select PADATA if SMP help hugetlbfs is a filesystem backing for HugeTLB pages, based on ramfs. For architectures that support it, say Y here and read
Signature · 1 line
2.20.1
Re: [PATCH v6 6/8] hugetlb: have CONFIG_HUGETLBFS select CONFIG_PADATA
Quoted 7 lines · Gang Li
Allow hugetlb use
padata_do_multithreadedfor parallel initialization. SelectCONFIG_PADATAin this case.Signed-off-by Gang Li <[email protected]>Tested-by David Rientjes <[email protected]>Reviewed-by Muchun Song <[email protected]>Tested-by Paul E. McKenney <[email protected]>
Quoted 19 lines
--- fs/Kconfig | 1 + 1 file changed, 1 insertion(+)fs/Kconfig +1
-0@@ -262,6 +262,7 @@ menuconfig HUGETLBFS depends on X86 || SPARC64 || ARCH_SUPPORTS_HUGETLBFS || BROKEN depends on (SYSFS || SYSCTL) select MEMFD_CREATE + select PADATA if SMP help hugetlbfs is a filesystem backing for HugeTLB pages, based on ramfs. For architectures that support it, say Y here and readSignature · 1 line
2.20.1
PATCH v6 7/8 hugetlb: parallelize 2M hugetlb allocation and initialization
In reply to Gang LiBy 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 case no patch(ms) patched(ms) saved
------------------- -------------- ------------- --------
256c2T(4 node) 2M 3336 1051 68.52% 128c1T(2 node) 2M 1943 716 63.15%
--- mm/hugetlb.c | 73 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 17 deletions(-)
mm/hugetlb.c +56 -17
@@ -35,6 +35,7 @@#include <linux/delayacct.h> #include <linux/memory.h> #include <linux/mm_inline.h> +#include <linux/padata.h> #include <asm/page.h> #include <asm/pgalloc.h>
@@ -3510,6 +3511,30 @@ static void __init hugetlb_hstate_alloc_pages_errcheck(unsigned long allocated, } } +static void __init hugetlb_pages_alloc_boot_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; +LIST_HEAD (folio_list); +int next_node = first_online_node; + +/* 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_node); +if (!folio) +break ; + +list_move (&folio->lru, &folio_list); +cond_resched (); + } + +prep_and_add_allocated_folios (h, &folio_list); +} +static unsigned long __init hugetlb_gigantic_pages_alloc_boot (struct hstate *h) {unsigned long i;
@@ -3525,26 +3550,40 @@ static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h)static unsigned long __init hugetlb_pages_alloc_boot (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); +struct padata_mt_job job = { + .fn_arg = h, + .align =1 , + .numa_aware = true + }; -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 (); - } + job.thread_fn = hugetlb_pages_alloc_boot_node; + job.start =0 ; + job.size = h->max_huge_pages; -prep_and_add_allocated_folios (h, &folio_list); +/* +* job.max_threads is twice the num_node_state(N_MEMORY), +* +* Tests below indicate that a multiplier of 2 significantly improves +* performance, and although larger values also provide improvements, +* the gains are marginal. +* +* Therefore, choosing 2 as the multiplier strikes a good balance between +* enhancing parallel processing capabilities and maintaining efficient +* resource management. +* +* +------------+-------+-------+-------+-------+-------+ +* | multiplier | 1 | 2 | 3 | 4 | 5 | +* +------------+-------+-------+-------+-------+-------+ +* | 256G 2node | 358ms | 215ms | 157ms | 134ms | 126ms | +* | 2T 4node | 979ms | 679ms | 543ms | 489ms | 481ms | +* | 50G 2node | 71ms | 44ms | 37ms | 30ms | 31ms | +* +------------+-------+-------+-------+-------+-------+ +*/ + job.max_threads =num_node_state (N_MEMORY ) *2 ; + job.min_chunk = h->max_huge_pages /num_node_state (N_MEMORY ) /2 ; +padata_do_multithreaded (&job); -return i; +return h->nr_huge_pages; }/*
Signature · 1 line
2.20.1
Re: [PATCH v6 7/8] hugetlb: parallelize 2M hugetlb allocation and initialization
Hi,
Quoted 9 lines · Gang Li
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 case no patch(ms) patched(ms) saved
------------------- -------------- ------------- --------256c2T(4 node) 2M 3336 1051 68.52% 128c1T(2 node) 2M 1943 716 63.15%
Great improvement, and glad to see the multithreading is useful here.
Quoted 39 lines
static unsigned long
__inithugetlb_pages_alloc_boot(struct hstate *h){mm/hugetlb.c +19
-16-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); +struct padata_mt_job job = { + .fn_arg = h, + .align =1 , + .numa_aware = true + }; -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 (); - } + job.thread_fn = hugetlb_pages_alloc_boot_node; + job.start =0 ; + job.size = h->max_huge_pages; -prep_and_add_allocated_folios (h, &folio_list); +/* +* job.max_threads is twice the num_node_state(N_MEMORY), +* +* Tests below indicate that a multiplier of 2 significantly improves +* performance, and although larger values also provide improvements, +* the gains are marginal. +* +* Therefore, choosing 2 as the multiplier strikes a good balance between +* enhancing parallel processing capabilities and maintaining efficient +* resource management. +*
mm/hugetlb.c static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h) · line 3583
Quoted 10 lines
mm/hugetlb.c +10
-0+* +------------+-------+-------+-------+-------+-------+ +* | multiplier | 1 | 2 | 3 | 4 | 5 | +* +------------+-------+-------+-------+-------+-------+ +* | 256G 2node | 358ms | 215ms | 157ms | 134ms | 126ms | +* | 2T 4node | 979ms | 679ms | 543ms | 489ms | 481ms | +* | 50G 2node | 71ms | 44ms | 37ms | 30ms | 31ms | +* +------------+-------+-------+-------+-------+-------+ +*/ + job.max_threads =num_node_state (N_MEMORY ) *2 ; + job.min_chunk = h->max_huge_pages /num_node_state (N_MEMORY ) /2 ;
For a single huge page, we get min_chunk of 0. padata doesn't explicitly handle that, but 'align' happens to save us from div by 0 later on. It's an odd case, something to fix if there were another version.
Not sure what efficient resource management means here. Avoiding lock contention? The system is waiting on this initialization to start pid
1. On big systems, most CPUs will be idle, so why not use available resources to optimize it more? max_threads could scale with CPU count rather than a magic multiplier.
With that said, the major gain is already there, so either way,
PATCH v6 8/8 hugetlb: parallelize 1G hugetlb initialization
In reply to Gang LiOptimizing 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 case 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%--- arch/powerpc/mm/hugetlbpage.c | 2 +- include/linux/hugetlb.h | 2 +- mm/hugetlb.c | 51 +++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 10 deletions(-)
arch/powerpc/mm/hugetlbpage.c +1 -1
@@ -226,7 +226,7 @@ static int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)return 0 ; m =phys_to_virt (gpage_freearray[--nr_gpages]); gpage_freearray[nr_gpages] =0 ; -list_add (&m->list, &huge_boot_pages); +list_add (&m->list, &huge_boot_pages[0 ]); m->hstate = hstate;return 1 ; }
include/linux/hugetlb.h +1 -1
@@ -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 */
mm/hugetlb.c +43 -8
113 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v6 8/8] hugetlb: parallelize 1G hugetlb initialization
Quoted 8 lines · Gang Li
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.
Quoted 6 lines
Here are some test results:
test case 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%
Another great improvement.
Quoted 14 lines
mm/hugetlb.c +14
-0+static void __init gather_bootmem_prealloc_parallel (unsigned long start, +unsigned long end,void *arg) +{ +int nid; + +for (nid = start; nid < end; nid++) +gather_bootmem_prealloc_node (nid); +} + +static void __init gather_bootmem_prealloc (void ) +{ +struct padata_mt_job job = { + .thread_fn = gather_bootmem_prealloc_parallel, + .fn_arg =NULL ,
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 3479
Quoted 10 lines
mm/hugetlb.c +10
-0+ .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); +}
Looks fine from the padata side.
Re: [PATCH v6 8/8] hugetlb: parallelize 1G hugetlb initialization
Thanks for your review :)
Quoted 46 lines · Daniel Jordan
Quoted 8 lines · Gang Li
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.Quoted 6 lines
Here are some test results:
test case 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%Another great improvement.
Quoted 14 lines
mm/hugetlb.c +14
-0+static void __init gather_bootmem_prealloc_parallel (unsigned long start, +unsigned long end,void *arg) +{ +int nid; + +for (nid = start; nid < end; nid++) +gather_bootmem_prealloc_node (nid); +} + +static void __init gather_bootmem_prealloc (void ) +{ +struct padata_mt_job job = { + .thread_fn = gather_bootmem_prealloc_parallel, + .fn_arg =NULL ,mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 3479
Quoted 10 lines
mm/hugetlb.c +10
-0+ .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); +}Looks fine from the padata side.