ktask: multithread cpu-intensive kernel work
Subsystems (6)
v2 cover ktask: multithread cpu-intensive kernel work
ktask is a generic framework for parallelizing cpu-intensive work in the kernel. The intended use is for big machines that can use their cpu power to speed up large tasks that can't otherwise be multithreaded in userland. The API is generic enough to add concurrency to many different kinds of tasks--for example, zeroing a range of pages or evicting a list of inodes--and aims to save its clients the trouble of splitting up the work, choosing the number of threads to use, starting these threads, and load balancing the work between them.
Why do we need ktask when the kernel has other APIs for managing concurrency? After all, kthread_workers and workqueues already provide ways to start threads, and the kernel can handle large tasks with a single thread by periodically yielding the cpu with cond_resched or doing the work in fixed size batches.
Of the existing concurrency facilities, kthread_worker isn't suited for providing parallelism because each comes with only a single thread. Workqueues are a better fit for this, and in fact ktask is built on an unbound workqueue, but workqueues aren't designed for splitting up a large task. ktask instead uses unbound workqueue threads to run "chunks" of a task.
More background is available in the documentation commit (first commit of the series).
This patchset is based on 4.13-rc6 and contains three ktask users so far, with more to come:
- clearing gigantic pages
- fallocate for HugeTLB pages
- deferred struct page initialization at boot time
The core ktask code is based on work by Pavel Tatashin, Steve Sistare, and Jonathan Adams.
v1 -> v2:
- Added deferred struct page initialization use case.
- Explained the source of the performance improvement from parallelizing clear_gigantic_page (comment from Dave Hansen).
- Fixed Documentation and build warnings from CONFIG_KTASK=n kernels.
link to v1: https://lkml.org/lkml/2017/7/14/666
Daniel Jordan (7):
ktask: add documentation
ktask: multithread cpu-intensive kernel work
ktask: add /proc/sys/debug/ktask_max_threads
mm: enlarge type of offset argument in mem_map_offset and mem_map_next
mm: parallelize clear_gigantic_page
hugetlbfs: parallelize hugetlbfs_fallocate with ktask
mm: parallelize deferred struct page initialization within each node
Documentation/core-api/index.rst | 1 + Documentation/core-api/ktask.rst | 104 ++++++++++ fs/hugetlbfs/inode.c | 117 +++++++++--- include/linux/ktask.h | 235 +++++++++++++++++++++++ include/linux/ktask_internal.h | 19 ++ include/linux/mm.h | 6 + init/Kconfig | 7 + init/main.c | 2 + kernel/Makefile | 2 +- kernel/ktask.c | 396 +++++++++++++++++++++++++++++++++++++++ kernel/sysctl.c | 10 + mm/internal.h | 7 +- mm/memory.c | 35 +++- mm/page_alloc.c | 174 ++++++++++------- 14 files changed, 1014 insertions(+), 101 deletions(-) create mode 100644 Documentation/core-api/ktask.rst create mode 100644 include/linux/ktask.h create mode 100644 include/linux/ktask_internal.h create mode 100644 kernel/ktask.c
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 1/7 ktask: add documentation
Motivates and explains the ktask API for kernel clients.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/ktask.rst | 104 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Documentation/core-api/ktask.rst
Documentation/core-api/index.rst +1 -0
@@ -15,6 +15,7 @@ Core utilities assoc_array atomic_ops cpu_hotplug + ktask local_ops workqueue genericirq
Documentation/core-api/ktask.rst new file +104 -0
104 lines not on this page · raw message
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Randy Dunlap
reviewerRe: [RFC PATCH v2 1/7] ktask: add documentation
Quoted 24 lines · Daniel Jordan
Motivates and explains the ktask API for kernel clients.
Signed-off-by Daniel Jordan <[email protected]>Reviewed-by Steve Sistare <[email protected]>Cc Aaron Lu <[email protected]>Cc Andrew Morton <[email protected]>Cc Dave Hansen <[email protected]>Cc Mel Gorman <[email protected]>Cc Michal Hocko <[email protected]>Cc Mike Kravetz <[email protected]>Cc Pavel Tatashin <[email protected]>Cc Tim Chen <[email protected]>--- Documentation/core-api/index.rst | 1 + Documentation/core-api/ktask.rst | 104 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Documentation/core-api/ktask.rstDocumentation/core-api/ktask.rst new file +0
-0
Documentation/core-api/ktask.rst line 3
Quoted 3 lines
Documentation/core-api/ktask.rst new file +3
-0@@ -0,0 +1,104 @@ +============================================ +ktask: parallelize cpu-intensive kernel work +============================================
Hi,
I would prefer to use CPU instead of cpu. Otherwise, Reviewed-by: Randy Dunlap <[email protected]>
Signature · 1 line
~Randy
Re: [RFC PATCH v2 1/7] ktask: add documentation
Quoted 12 lines · Randy Dunlap
Documentation/core-api/ktask.rst line 3
Quoted 9 lines · Daniel Jordan
Documentation/core-api/ktask.rst new file +3
-0@@ -0,0 +1,104 @@ +============================================ +ktask: parallelize cpu-intensive kernel work +============================================Hi,
Quoted 1 line
I would prefer to use CPU instead of cpu.
Ok, a quick grep through Documentation shows that CPU is used more often than cpu, so for consistency I'll change it.
Quoted 1 line
Otherwise, Reviewed-by: Randy Dunlap <[email protected]>
Thanks for the review, Randy.
Daniel
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 2/7 ktask: multithread cpu-intensive kernel work
In reply to Daniel Jordanktask is a generic framework for parallelizing cpu-intensive work in the kernel. The intended use is for big machines that can use their cpu power to speed up large tasks that can't otherwise be multithreaded in userland. The API is generic enough to add concurrency to many different kinds of tasks--for example, zeroing a range of pages or evicting a list of inodes--and aims to save its clients the trouble of splitting up the work, choosing the number of threads to use, starting these threads, and load balancing the work between them.
The Documentation patch earlier in this series has more background.
Introduces the ktask API; consumers appear in subsequent patches.
Based on work by Pavel Tatashin, Steve Sistare, and Jonathan Adams.
--- include/linux/ktask.h | 235 ++++++++++++++++++++++++ include/linux/ktask_internal.h | 19 ++ include/linux/mm.h | 6 + init/Kconfig | 7 + init/main.c | 2 + kernel/Makefile | 2 +- kernel/ktask.c | 396 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 666 insertions(+), 1 deletion(-) create mode 100644 include/linux/ktask.h create mode 100644 include/linux/ktask_internal.h create mode 100644 kernel/ktask.c
include/linux/ktask.h new file +235 -0
@@ -0,0 +1,235 @@ +/* +* ktask.h +* +* Framework to parallelize cpu-intensive kernel work such as zeroing +* huge pages or freeing many pages at once. For more information, see +* Documentation/core-api/ktask.rst. +* +* This is the interface to ktask; everything in this file is +* accessible to ktask clients. +* +* If CONFIG_KTASK=n, calls to the ktask API are simply #define'd to run the +* thread function that the client provides so that the task is completed +* without concurrency in the current thread. +*/ + +#ifndef _LINUX_KTASK_H +#define _LINUX_KTASK_H + +#include <linux/types.h> + +struct ktask_ctl ; +struct ktask_node ; + +#define KTASK_RETURN_SUCCESS 0 +#define KTASK_RETURN_ERROR (-1) + +#ifdef CONFIG_KTASK + +/** +* ktask_run - Runs one task. It doesn't account for NUMA locality. +* +* @start: An object that describes the start of the task. The client thread +* function interprets the object however it sees fit (e.g. an array +* index, a simple pointer, or a pointer to a more complicated +* representation of job position. +* @task_size: The size of the task (units are task-specific). +* @ctl: A control structure containing information about the task, including +* the client thread function (see the definition of struct ktask_ctl). +* +* RETURNS: +* KTASK_RETURN_SUCCESS or KTASK_RETURN_ERROR. +*/ +int ktask_run (void *start,size_t task_size,struct ktask_ctl *ctl); + +/** +* ktask_run_numa - Runs one task while accounting for NUMA locality. +* +* The ktask framework ensures worker threads are scheduled on a CPU local to +* each chunk of a task. The client is responsible for organizing the work +* along NUMA boundaries in the 'nodes' array. +* +* @nodes: An array of struct ktask_node's, each of which describes the task on +* a NUMA node (see struct ktask_node). +* @nr_nodes: The length of the 'nodes' array. +* @ctl: A control structure containing information about the task (see +* the definition of struct ktask_ctl). +* +* RETURNS: +* KTASK_RETURN_SUCCESS or KTASK_RETURN_ERROR. +*/ +int ktask_run_numa (struct ktask_node *nodes,size_t nr_nodes, +struct ktask_ctl *ctl); + +/* +* Two possible future enhancements related to error handling, should the +* need arise, are: +* +* - Add client specific error reporting. It's possible for tasks to fail for +* different reasons, so let the client pass a pointer for its own error +* information. +* +* - Allow clients to pass an "undo" callback to ktask that is responsible for +* undoing those parts of the task that fail if an error occurs. +*/ + +void ktask_init (void ); + +#else /* CONFIG_KTASK */ + +#define ktask_run (start, task_size, ctl) \ + ((ctl)->kc_thread_func((start), \ + (ctl)->kc_iter_advance((start), (task_size)), \ + (ctl)->kc_thread_func_arg)) + +#define ktask_run_numa (nodes, nr_nodes, ctl) \ +({ \ + size_t __i; \ + int __ret = KTASK_RETURN_SUCCESS; \ + \ + for (__i = 0; __i < (nr_nodes); ++__i) { \ + __ret = (ctl)->kc_thread_func( \ + (nodes)->kn_start, \ + (ctl)->kc_iter_advance((nodes)->kn_start, \ + (nodes)->kn_task_size), \ + (ctl)->kc_thread_func_arg); \ + \ + if (__ret == KTASK_RETURN_ERROR) \ + break; \ + } \ + \ + __ret; \ +}) + +static inline void ktask_init (void ) { } + +#endif /* CONFIG_KTASK */ + +/** +* struct ktask_node - Holds per-NUMA-node information about a task. +* +* @kn_start: An object that describes the start of the task on this NUMA node. +* @kn_task_size: The size of the task on this NUMA node (units are +* task-specific). +* @kn_nid: The NUMA node id (or NUMA_NO_NODE, in which case the work is done on +* the current node). +*/ +struct ktask_node { +void *kn_start; +size_t kn_task_size; +int kn_nid; +}; + +/** +* typedef ktask_thread_func +* +* Called on each chunk of work that a ktask thread does, where the chunk is +* delimited by [start, end). A thread may call this multiple times during one +* task. +* +* @start: An object that describes the start of the chunk. +* @end: An object that describes the end of the chunk. +* @arg: The thread function argument (provided with struct ktask_ctl). +* +* RETURNS: +* KTASK_RETURN_SUCCESS or KTASK_RETURN_ERROR. +*/ +typedef int (*ktask_thread_func )(void *start,void *end,void *arg); + +/** +* typedef ktask_iter_func +* +* An iterator function that advances the position by a given number of steps. +* +* @position: An object that describes the current position in the task. +* @nsteps: The number of steps to advance in the task (in task-specific +* units). +* +* RETURNS: +* An object representing the new position. +*/ +typedef void *(*ktask_iter_func )(void *position,size_t nsteps); + +/** +* ktask_iter_range +* +* An iterator function for a contiguous range such as an array or address +* range. Clients should use this to avoid reinventing the wheel for this +* common case. +* +* This is an instance of 'ktask_iter_func' where 'position' is interpreted as +* an unsigned long. +* +* @position: An object that describes the current position in the task. +* @nsteps: The number of steps to advance in the task (in task-specific +* units). +* +* RETURNS: +* (position + nsteps) +*/ +void *ktask_iter_range (void *position,size_t nsteps); + +/** +* struct ktask_ctl - Client-provided per-task control information. +* +* @kc_thread_func: A thread function that completes one chunk of the task per +* call. +* @kc_thread_func_arg: An argument to be passed to the thread function. +* @kc_iter_advance: An iterator function to advance the iterator by some number +* of task-specific units. +* @kc_min_chunk_size: The minimum chunk size in task-specific units. This +* allows the client to communicate the minimum amount of +* work that's appropriate for one worker thread to do at +* once. +* @kc_max_threads: The maximum number of threads to use for the task. +* The actual number used may be less than this if the +* framework determines that fewer threads would be better, +* taking into account such things as total cpu count and +* task size. Pass 0 to use ktask's default maximum. +* @kc_gfp_flags: gfp flags for allocating ktask metadata during the task. +*/ +struct ktask_ctl { +ktask_thread_func kc_thread_func; +void *kc_thread_func_arg; +ktask_iter_func kc_iter_advance; +size_t kc_min_chunk_size; +size_t kc_max_threads; +gfp_t kc_gfp_flags; +}; + +#define KTASK_CTL_INITIALIZER (thread_func, thread_func_arg, iter_advance, \ + min_chunk_size, max_threads, gfp_flags) \ + { \ + .kc_thread_func = (ktask_thread_func)(thread_func), \ + .kc_thread_func_arg = (thread_func_arg), \ + .kc_iter_advance = (iter_advance), \ + .kc_min_chunk_size = (min_chunk_size), \ + .kc_max_threads = (max_threads), \ + .kc_gfp_flags = (gfp_flags), \ + } + +/* +* Note that KTASK_CTL_INITIALIZER casts 'thread_func' to be of type +* ktask_thread_func. This is to help clients write cleaner thread functions +* by relieving them of the need to cast the three void * arguments. Clients +* can just use the actual argument types instead. +*/ +#define DEFINE_KTASK_CTL (ctl_name, thread_func, thread_func_arg, \ + iter_advance, min_chunk_size, max_threads, \ + gfp_flags) \ + struct ktask_ctl ctl_name = \ + KTASK_CTL_INITIALIZER(thread_func, thread_func_arg, \ + iter_advance, min_chunk_size, \ + kc_max_threads, gfp_flags) +/* +* Similar to DEFINE_KTASK_CTL, but omits the iterator argument in favor of +* using ktask_iter_range. +*/ +#define DEFINE_KTASK_CTL_RANGE (ctl_name, thread_func, thread_func_arg, \ + min_chunk_size, kc_max_threads, gfp_flags) \ + struct ktask_ctl ctl_name = \ + KTASK_CTL_INITIALIZER(thread_func, thread_func_arg, \ + ktask_iter_range, min_chunk_size, \ + kc_max_threads, gfp_flags) + +#endif /* _LINUX_KTASK_H */
include/linux/ktask_internal.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* ktask_internal.h +* +* Framework to parallelize cpu-intensive kernel work such as zeroing +* huge pages or freeing many pages at once. For more information, see +* Documentation/core-api/ktask.rst. +* +* This file contains implementation details of ktask for core kernel code that +* needs to be aware of them. ktask clients should not include this file. +*/ +#ifndef _LINUX_KTASK_INTERNAL_H +#define _LINUX_KTASK_INTERNAL_H + +#ifdef CONFIG_KTASK +/* Caps the number of threads that are allowed to be used in one task. */ +extern int ktask_max_threads; +#endif + +#endif /* _LINUX_KTASK_INTERNAL_H */
include/linux/mm.h +6 -0
@@ -2557,5 +2557,11 @@ void __init setup_nr_node_ids(void);static inline void setup_nr_node_ids (void ) {}#endif +/* +* The minimum chunk size for a task that uses base page units. For now, say +* 1G's worth of pages. +*/ +#define KTASK_BPGS_MINCHUNK ((1ul << 30) / PAGE_SIZE) +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
init/Kconfig +7 -0
@@ -332,6 +332,13 @@ config AUDIT_TREE depends on AUDITSYSCALL select FSNOTIFY +config KTASK + bool "Multithread cpu-intensive kernel tasks" + depends on SMP + default n + help + Parallelize expensive kernel tasks such as zeroing huge pages. + source "kernel/irq/Kconfig" source "kernel/time/Kconfig"
init/main.c +2 -0
@@ -88,6 +88,7 @@#include <linux/io.h> #include <linux/cache.h> #include <linux/rodata_test.h> +#include <linux/ktask.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -1051,6 +1052,7 @@ static noinline void __init kernel_init_freeable(void)smp_init ();sched_init_smp (); +ktask_init ();page_alloc_init_late ();
kernel/Makefile +1 -1
@@ -9,7 +9,7 @@ obj-y = fork.o exec_domain.o panic.o \ extable.o params.o \ kthread.o sys_ni.o nsproxy.o \ notifier.o ksysfs.o cred.o reboot.o \ - async.o range.o smpboot.o ucount.o + async.o range.o smpboot.o ucount.o ktask.o obj-$(CONFIG_MULTIUSER) += groups.o
kernel/ktask.c new file +396 -0
396 lines not on this page · raw message
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 3/7 ktask: add /proc/sys/debug/ktask_max_threads
In reply to Daniel JordanAdds a proc file to control the maximum number of ktask threads in use for any one job. Its primary use is to aid in debugging.
--- kernel/sysctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
kernel/sysctl.c +10 -0
@@ -67,6 +67,7 @@#include <linux/kexec.h> #include <linux/bpf.h> #include <linux/mount.h> +#include <linux/ktask_internal.h> #include <linux/uaccess.h> #include <asm/processor.h>
@@ -1876,6 +1877,15 @@ static struct ctl_table debug_table[] = { .extra2 = &one, },#endif +#if defined(CONFIG_KTASK ) + { + .procname = "ktask_max_threads", + .data = &ktask_max_threads, + .maxlen = sizeof(ktask_max_threads), + .mode = 0644, + .proc_handler = proc_dointvec, + }, +#endif { } };
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 4/7 mm: enlarge type of offset argument in mem_map_offset and mem_map_next
In reply to Daniel JordanChanges the type of 'offset' from int to unsigned long in both mem_map_offset and mem_map_next.
This facilitates ktask's use of mem_map_next with its unsigned long types to avoid silent truncation when these unsigned longs are passed as ints.
It also fixes the preexisting truncation of 'offset' from unsigned long to int by the sole caller of mem_map_offset, follow_hugetlb_page.
--- mm/internal.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/internal.h +4 -3
@@ -365,7 +365,8 @@ static inline void mlock_migrate_page(struct page *new, struct page *old) { }* the maximally aligned gigantic page 'base'. Handle any discontiguity * in the mem_map at MAX_ORDER_NR_PAGES boundaries. */ -static inline struct page *mem_map_offset (struct page *base,int offset) +static inline struct page *mem_map_offset (struct page *base, +unsigned long offset) {if (unlikely (offset >=MAX_ORDER_NR_PAGES ))return nth_page (base, offset);
@@ -376,8 +377,8 @@ static inline struct page *mem_map_offset(struct page *base, int offset)* Iterator over all subpages within the maximally aligned gigantic * page 'base'. Handle any discontiguity in the mem_map. */ -static inline struct page *mem_map_next (struct page *iter, -struct page *base,int offset) +static inline struct page *mem_map_next (struct page *iter,struct page *base, +unsigned long offset) {if (unlikely ((offset & (MAX_ORDER_NR_PAGES -1 )) ==0 )) {unsigned long pfn =page_to_pfn (base) + offset;
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 5/7 mm: parallelize clear_gigantic_page
In reply to Daniel JordanParallelize clear_gigantic_page, which zeroes any page size larger than 8M (e.g. 1G on x86 or 2G on SPARC).
Performance results (the default number of threads is 4; higher thread counts shown for context only):
Machine: SPARC T7-4, 1024 cpus, 504G memory Test: Clear a range of gigantic pages
nthread speedup size (GiB) min time (s) stdev
1 50 7.77 0.02
2 1.97x 50 3.95 0.04
4 3.85x 50 2.02 0.05
8 6.27x 50 1.24 0.10
16 9.84x 50 0.79 0.06 1 100 15.50 0.07
2 1.91x 100 8.10 0.05
4 3.48x 100 4.45 0.07
8 5.18x 100 2.99 0.05
16 7.79x 100 1.99 0.12 1 200 31.03 0.15
2 1.88x 200 16.47 0.02
4 3.37x 200 9.20 0.14
8 5.16x 200 6.01 0.19
16 7.04x 200 4.41 0.06Machine: Intel(R) Xeon(R) CPU E7-8895 v3 @ 2.60GHz, 288 cpus, 1T memory Test: Clear a range of gigantic pages
nthread speedup size (GiB) min time (s) stdev
1 100 41.13 0.03
2 2.03x 100 20.26 0.14
4 4.28x 100 9.62 0.09
8 8.39x 100 4.90 0.05
16 10.44x 100 3.94 0.03 1 200 89.68 0.35
2 2.21x 200 40.64 0.18
4 4.64x 200 19.33 0.32
8 8.99x 200 9.98 0.04
16 11.27x 200 7.96 0.04 1 400 188.20 1.57
2 2.30x 400 81.84 0.09
4 4.63x 400 40.62 0.26
8 8.92x 400 21.09 0.50
16 11.78x 400 15.97 0.25 1 800 434.91 1.81
2 2.54x 800 170.97 1.46
4 4.98x 800 87.38 1.91
8 10.15x 800 42.86 2.59
16 12.99x 800 33.48 0.83The speedups are mostly due to the fact that more threads can use more memory bandwidth. The loop we're stressing on the x86 chip in this test is clear_page_erms, which tops out at a bandwidth of 2550 MiB/s with one thread. We get the same bandwidth per thread for 2, 4, or 8 threads, but at 16 threads the per-thread bandwidth drops to 1420 MiB/s.
However, the performance also improves over a single thread because of the ktask threads' NUMA awareness (ktask migrates worker threads to the node local to the work being done). This becomes a bigger factor as the amount of pages to zero grows to include memory from multiple nodes, so that speedups increase as the size increases.
--- mm/memory.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-)
mm/memory.c +27 -8
@@ -69,6 +69,7 @@#include <linux/userfaultfd_k.h> #include <linux/dax.h> #include <linux/oom.h> +#include <linux/ktask.h> #include <asm/io.h> #include <asm/mmu_context.h>
@@ -4325,27 +4326,45 @@ EXPORT_SYMBOL(__might_fault);#endif #if defined(CONFIG_TRANSPARENT_HUGEPAGE ) || defined(CONFIG_HUGETLBFS ) -static void clear_gigantic_page (struct page *page, -unsigned long addr, -unsigned int pages_per_huge_page) + +struct cgp_args { +struct page *base_page; +unsigned long addr; +}; + +static int clear_gigantic_page_chunk (unsigned long start,unsigned long end, +struct cgp_args *args) { -int i; -struct page *p = page; +struct page *base_page = args->base_page; +struct page *p = base_page; +unsigned long addr = args->addr; +unsigned long i;might_sleep (); -for (i =0 ; i < pages_per_huge_page; - i++, p =mem_map_next (p, page, i)) { +for (i = start; i < end; ++i) {cond_resched ();clear_user_highpage (p, addr + i *PAGE_SIZE ); + + p =mem_map_next (p, base_page, i); } + +return KTASK_RETURN_SUCCESS ; } +void clear_huge_page (struct page *page,unsigned long addr,unsigned int pages_per_huge_page) {int i;if (unlikely (pages_per_huge_page >MAX_ORDER_NR_PAGES )) { -clear_gigantic_page (page, addr, pages_per_huge_page); +struct cgp_args args = {page, addr}; +struct ktask_node node = {0 , pages_per_huge_page, +page_to_nid (page)}; +DEFINE_KTASK_CTL_RANGE (ctl, clear_gigantic_page_chunk, &args, +KTASK_BPGS_MINCHUNK ,0 ,GFP_KERNEL ); + +ktask_run_numa (&node,1 , &ctl); +return ; }
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 6/7 hugetlbfs: parallelize hugetlbfs_fallocate with ktask
In reply to Daniel Jordanhugetlbfs_fallocate preallocates huge pages to back a file in a hugetlbfs filesystem. The time to call this function grows linearly with size.
ktask performs well with its default thread count of 4; higher thread counts are given for context only.
Machine: Intel(R) Xeon(R) CPU E7-8895 v3 @ 2.60GHz, 288 cpus, 1T memory Test: fallocate(1) a file on a hugetlbfs filesystem
nthread speedup size (GiB) min time (s) stdev
1 200 127.53 2.19
2 3.09x 200 41.30 2.11
4 5.72x 200 22.29 0.51
8 9.45x 200 13.50 2.58
16 9.74x 200 13.09 1.64 1 400 193.09 2.47
2 2.14x 400 90.31 3.39
4 3.84x 400 50.32 0.44
8 5.11x 400 37.75 1.23
16 6.12x 400 31.54 3.13Machine: SPARC T7-4, 1024 cpus, 504G memory Test: fallocate(1) a file on a hugetlbfs filesystem
nthread speedup size (GiB) min time (s) stdev
1 100 15.55 0.05
2 1.92x 100 8.08 0.01
4 3.55x 100 4.38 0.02
8 5.87x 100 2.65 0.06
16 6.45x 100 2.41 0.09 1 200 31.26 0.02
2 1.92x 200 16.26 0.02
4 3.58x 200 8.73 0.04
8 5.54x 200 5.64 0.16
16 6.96x 200 4.49 0.35 1 400 62.18 0.09
2 1.98x 400 31.36 0.04
4 3.55x 400 17.52 0.03
8 5.53x 400 11.25 0.04
16 6.61x 400 9.40 0.17The primary bottleneck for better scaling at higher thread counts is hugetlb_fault_mutex_table[hash]. perf showed L1-dcache-loads increase with 8 threads and again sharply with 16 threads, and a cpu counter profile showed that 31% of the L1d misses were on hugetlb_fault_mutex_table[hash] in the 16-thread case.
--- fs/hugetlbfs/inode.c | 117 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 95 insertions(+), 22 deletions(-)
fs/hugetlbfs/inode.c +95 -22
@@ -36,6 +36,7 @@#include <linux/magic.h> #include <linux/migrate.h> #include <linux/uio.h> +#include <linux/ktask.h> #include <linux/uaccess.h>
@@ -86,11 +87,16 @@ static const match_table_t tokens = { };#ifdef CONFIG_NUMA +static inline struct shared_policy *hugetlb_get_shared_policy ( +struct inode *inode) +{ +return &HUGETLBFS_I (inode)->policy; +} +static inline void hugetlb_set_vma_policy (struct vm_area_struct *vma, -struct inode *inode,pgoff_t index) +struct shared_policy *policy,pgoff_t index) { - vma->vm_policy =mpol_shared_policy_lookup (&HUGETLBFS_I (inode)->policy, - index); + vma->vm_policy =mpol_shared_policy_lookup (policy, index); }static inline void hugetlb_drop_vma_policy (struct vm_area_struct *vma)
@@ -98,8 +104,14 @@ static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma) mpol_cond_put(vma->vm_policy); }#else +static inline struct shared_policy *hugetlb_get_shared_policy ( +struct inode *inode) +{ +return NULL ; +} +static inline void hugetlb_set_vma_policy (struct vm_area_struct *vma, -struct inode *inode,pgoff_t index) +struct shared_policy *policy,pgoff_t index) { }
@@ -551,19 +563,29 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)return 0 ; } +struct hf_args { +struct file *file; +struct task_struct *parent_task; +struct mm_struct *mm; +struct shared_policy *shared_policy; +struct hstate *hstate; +struct address_space *mapping; +int error; +}; + +static int hugetlbfs_fallocate_chunk (pgoff_t start,pgoff_t end, +struct hf_args *args); +static long hugetlbfs_fallocate (struct file *file,int mode,loff_t offset,loff_t len) {struct inode *inode =file_inode (file); -struct address_space *mapping = inode->i_mapping;struct hstate *h =hstate_inode (inode); -struct vm_area_struct pseudo_vma; -struct mm_struct *mm = current->mm;loff_t hpage_size =huge_page_size (h);unsigned long hpage_shift =huge_page_shift (h); -pgoff_t start, index, end; +pgoff_t start, end; +struct hf_args hf_args;int error; -u32 hash;if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE ))return -EOPNOTSUPP ;
@@ -586,16 +608,67 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,if (error) goto out; + hf_args.file = file; + hf_args.parent_task = current; + hf_args.mm = current->mm; + hf_args.shared_policy =hugetlb_get_shared_policy (inode); + hf_args.hstate = h; + hf_args.mapping = inode->i_mapping; + hf_args.error =0 ; + +if (unlikely (hstate_is_gigantic (h))) { +/* +* Use multiple threads in clear_gigantic_page instead of here, +* so just do a 1-threaded hugetlbfs_fallocate_chunk. +*/ + error =hugetlbfs_fallocate_chunk (start, end, &hf_args); + }else { +DEFINE_KTASK_CTL_RANGE (ctl, hugetlbfs_fallocate_chunk, + &hf_args,KTASK_BPGS_MINCHUNK , +0 ,GFP_KERNEL ); + + error =ktask_run ((void *)start, end - start, &ctl); + } + +if (error ==KTASK_RETURN_ERROR && hf_args.error != -EINTR ) + goto out; + +if (!(mode &FALLOC_FL_KEEP_SIZE ) && offset + len > inode->i_size) +i_size_write (inode, offset + len); + inode->i_ctime =current_time (inode); +out: +inode_unlock (inode); +return error; +} + +static int hugetlbfs_fallocate_chunk (pgoff_t start,pgoff_t end, +struct hf_args *args) +{ +struct file *file = args->file; +struct task_struct *parent_task = args->parent_task; +struct mm_struct *mm = args->mm; +struct shared_policy *shared_policy = args->shared_policy; +struct hstate *h = args->hstate; +struct address_space *mapping = args->mapping; +int error =0 ; +pgoff_t index; +struct vm_area_struct pseudo_vma; +loff_t hpage_size; +u32 hash; + + hpage_size =huge_page_size (h); +/* * Initialize a pseudo vma as this is required by the huge page * allocation routines. If NUMA is configured, use page index -* as input to create an allocation policy. +* as input to create an allocation policy. Each thread gets its +* own pseudo vma because mempolicies can differ by page. */ memset (&pseudo_vma,0 ,sizeof (struct vm_area_struct )); pseudo_vma.vm_flags = (VM_HUGETLB |VM_MAYSHARE |VM_SHARED ); pseudo_vma.vm_file = file; -for (index = start; index < end; index++) { +for (index = start; index < end; ++index) {/* * This is supposed to be the vaddr where the page is being * faulted in, but we have no vaddr here.
@@ -610,13 +683,13 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,* fallocate(2) manpage permits EINTR; we may have been * interrupted because we are using up too much memory. */ -if (signal_pending (current)) { +if (signal_pending (parent_task) ||signal_pending (current)) { error = -EINTR ; -break ; + goto err; }/* Set numa allocation policy based on index */ -hugetlb_set_vma_policy (&pseudo_vma, inode, index); +hugetlb_set_vma_policy (&pseudo_vma, shared_policy, index);/* addr is the offset within the file (zero based) */ addr = index * hpage_size;
@@ -641,7 +714,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,if (IS_ERR (page)) {mutex_unlock (&hugetlb_fault_mutex_table[hash]); error =PTR_ERR (page); - goto out; + goto err; }clear_huge_page (page, addr,pages_per_huge_page (h));__SetPageUptodate (page);
@@ -649,7 +722,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,if (unlikely (error)) {put_page (page);mutex_unlock (&hugetlb_fault_mutex_table[hash]); - goto out; + goto err; }mutex_unlock (&hugetlb_fault_mutex_table[hash]);
@@ -662,12 +735,12 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,unlock_page (page); } -if (!(mode &FALLOC_FL_KEEP_SIZE ) && offset + len > inode->i_size) -i_size_write (inode, offset + len); - inode->i_ctime =current_time (inode); -out: -inode_unlock (inode); -return error; +return KTASK_RETURN_SUCCESS ; + +err: + args->error = error; + +return KTASK_RETURN_ERROR ; }static int hugetlbfs_setattr (struct dentry *dentry,struct iattr *attr)
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v2 7/7 mm: parallelize deferred struct page initialization within each node
In reply to Daniel JordanDeferred struct page initialization currently uses one thread per node (pgdatinit threads), but this can still be a bottleneck during boot on big machines. To reduce boot time, use ktask within each pgdatinit thread to parallelize the struct page initialization on each node, allowing the system to use more memory bandwidth.
The number of cpus used depends on a few factors, including the size of the memory on that node (see the Documentation commit earlier in the series for more information), but in this special case, since cpus are not being used for much else at this phase of boot, we raise ktask's cap on the maximum number of cpus to the number of cpus on the node. Up to this many cpus participate in initializing struct pages per node.
Machine: Intel(R) Xeon(R) CPU E7-8895 v3 @ 2.60GHz, 288 cpus, 1T memory Test: Boot the machine with deferred struct page initialization
kernel speedup min time per stdev
node (ms)
baseline (4.13-rc5) 483 0.5 ktask (4.13-rc5 based) 3.66x 132 1.5
Machine: SPARC M6 30-node LDom, 256 cpus, 30T memory Test: Boot the machine with deferred struct page initialization
kernel speedup min time per stdev
node (ms)
baseline (4.13-rc5) 9566 1.4
ktask (4.13-rc5 based) 1.55x 6172 19.5
[There is a patch series under review upstream to defer the zeroing of struct pages to pgdatinit threads:
complete deferred page initialization
http://www.spinics.net/lists/linux-mm/msg132805.html
We get bigger speedups and save more boot time when incorporating this pending series because there is more work to parallelize.]
--- mm/page_alloc.c | 174 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 107 insertions(+), 67 deletions(-)
mm/page_alloc.c +107 -67
@@ -66,6 +66,7 @@#include <linux/kthread.h> #include <linux/memcontrol.h> #include <linux/ftrace.h> +#include <linux/ktask.h> #include <asm/sections.h> #include <asm/tlbflush.h>
@@ -1268,8 +1269,6 @@ static void __init __free_pages_boot_core(struct page *page, unsigned int order) }__ClearPageReserved (p);set_page_count (p,0 ); - -page_zone (page)->managed_pages += nr_pages;set_page_refcounted (page);__free_pages (page, order); }
@@ -1333,7 +1332,8 @@ void __init __free_pages_bootmem(struct page *page, unsigned long pfn, {if (early_page_uninitialised (pfn))return ; -return __free_pages_boot_core (page, order); +__free_pages_boot_core (page, order); +page_zone (page)->managed_pages += (1ul << order); }/*
@@ -1441,12 +1441,99 @@ static inline void __init pgdat_init_report_one_done(void)complete (&pgdat_init_all_done_comp); } +struct deferred_init_args { +int nid; +int zid; +struct zone *zone; +atomic64_t nr_pages; +}; + +int __init deferred_init_memmap_chunk (unsigned long start_pfn, +unsigned long end_pfn, +struct deferred_init_args *args) +{ +unsigned long pfn; +int nid = args->nid; +int zid = args->zid; +struct zone *zone = args->zone; +struct page *page =NULL ; +struct page *free_base_page =NULL ; +unsigned long free_base_pfn =0 ; +unsigned long nr_pages =0 ; +int nr_to_free =0 ; +struct mminit_pfnnid_cache nid_init_state = { }; + +for (pfn = start_pfn; pfn < end_pfn; pfn++) { +if (!pfn_valid_within (pfn)) + goto free_range; + +/* +* Ensure pfn_valid is checked every +* pageblock_nr_pages for memory holes +*/ +if ((pfn & (pageblock_nr_pages -1 )) ==0 ) { +if (!pfn_valid (pfn)) { + page =NULL ; + goto free_range; + } + } + +if (!meminit_pfn_in_nid (pfn, nid, &nid_init_state)) { + page =NULL ; + goto free_range; + } + +/* Minimise pfn page lookups and scheduler checks */ +if (page && (pfn & (pageblock_nr_pages -1 )) !=0 ) { + page++; + }else { + nr_pages += nr_to_free; +deferred_free_range (free_base_page, + free_base_pfn, nr_to_free); + free_base_page =NULL ; + free_base_pfn = nr_to_free =0 ; + + page =pfn_to_page (pfn); +cond_resched (); + } + +if (page->flags) { +VM_BUG_ON (page_zone (page) != zone); + goto free_range; + } + +__init_single_page (page, pfn, zid, nid); +if (!free_base_page) { + free_base_page = page; + free_base_pfn = pfn; + nr_to_free =0 ; + } + nr_to_free++; + +/* Where possible, batch up pages for a single free */ +continue ; +free_range: +/* Free the current block of pages to allocator */ + nr_pages += nr_to_free; +deferred_free_range (free_base_page, free_base_pfn, + nr_to_free); + free_base_page =NULL ; + free_base_pfn = nr_to_free =0 ; + } +/* Free the last block of pages to allocator */ + nr_pages += nr_to_free; +deferred_free_range (free_base_page, free_base_pfn, nr_to_free); + +atomic64_add (nr_pages, &args->nr_pages); + +return KTASK_RETURN_SUCCESS ; +} +/* Initialise remaining memory on a node */ static int __init deferred_init_memmap (void *data) {pg_data_t *pgdat = data;int nid = pgdat->node_id; -struct mminit_pfnnid_cache nid_init_state = { };unsigned long start = jiffies;unsigned long nr_pages =0 ;unsigned long walk_start, walk_end;
@@ -1454,6 +1541,7 @@ static int __init deferred_init_memmap(void *data)struct zone *zone;unsigned long first_init_pfn = pgdat->first_deferred_pfn;const struct cpumask *cpumask =cpumask_of_node (pgdat->node_id); +unsigned long nr_node_cpus =cpumask_weight (cpumask);if (first_init_pfn ==ULONG_MAX ) {pgdat_init_report_one_done ();
@@ -1478,10 +1566,12 @@ static int __init deferred_init_memmap(void *data)for_each_mem_pfn_range (i, nid, &walk_start, &walk_end,NULL ) {unsigned long pfn, end_pfn; -struct page *page =NULL ; -struct page *free_base_page =NULL ; -unsigned long free_base_pfn =0 ; -int nr_to_free =0 ; +struct ktask_node kn; +struct deferred_init_args args = { nid, zid, zone, +ATOMIC64_INIT (0 ) }; +DEFINE_KTASK_CTL_RANGE (ctl, deferred_init_memmap_chunk, &args, +KTASK_BPGS_MINCHUNK , nr_node_cpus, +GFP_KERNEL ); end_pfn =min (walk_end,zone_end_pfn (zone)); pfn = first_init_pfn;
@@ -1490,73 +1580,23 @@ static int __init deferred_init_memmap(void *data)if (pfn < zone->zone_start_pfn) pfn = zone->zone_start_pfn; -for (; pfn < end_pfn; pfn++) { -if (!pfn_valid_within (pfn)) - goto free_range; - -/* -* Ensure pfn_valid is checked every -* pageblock_nr_pages for memory holes -*/ -if ((pfn & (pageblock_nr_pages -1 )) ==0 ) { -if (!pfn_valid (pfn)) { - page =NULL ; - goto free_range; - } - } - -if (!meminit_pfn_in_nid (pfn, nid, &nid_init_state)) { - page =NULL ; - goto free_range; - } - -/* Minimise pfn page lookups and scheduler checks */ -if (page && (pfn & (pageblock_nr_pages -1 )) !=0 ) { - page++; - }else { - nr_pages += nr_to_free; -deferred_free_range (free_base_page, - free_base_pfn, nr_to_free); - free_base_page =NULL ; - free_base_pfn = nr_to_free =0 ; - - page =pfn_to_page (pfn); -cond_resched (); - } - -if (page->flags) { -VM_BUG_ON (page_zone (page) != zone); - goto free_range; - } - -__init_single_page (page, pfn, zid, nid); -if (!free_base_page) { - free_base_page = page; - free_base_pfn = pfn; - nr_to_free =0 ; - } - nr_to_free++; - -/* Where possible, batch up pages for a single free */ +if (pfn >= end_pfn)continue ; -free_range: -/* Free the current block of pages to allocator */ - nr_pages += nr_to_free; -deferred_free_range (free_base_page, free_base_pfn, - nr_to_free); - free_base_page =NULL ; - free_base_pfn = nr_to_free =0 ; - } -/* Free the last block of pages to allocator */ - nr_pages += nr_to_free; -deferred_free_range (free_base_page, free_base_pfn, nr_to_free); + + kn.kn_start = (void *)pfn; + kn.kn_task_size = end_pfn - pfn; + kn.kn_nid = nid; + (void )ktask_run_numa (&kn,1 , &ctl); first_init_pfn =max (end_pfn, first_init_pfn); + nr_pages +=atomic64_read (&args.nr_pages); }/* Sanity check that the next zone really is unpopulated */ WARN_ON (++zid <MAX_NR_ZONES &&populated_zone (++zone)); + zone->managed_pages += nr_pages; +pr_info ("node %d initialised, %lu pages in %ums\n" , nid, nr_pages,jiffies_to_msecs (jiffies - start));
Signature · 7 lines
2.12.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>