Re: [RFC PATCH v4 02/13] ktask: multithread CPU-intensive kernel work

2018-11-05 Thread Daniel Jordan
On Mon, Nov 05, 2018 at 12:51:33PM -0800, Randy Dunlap wrote:
> On 11/5/18 8:55 AM, Daniel Jordan wrote:
> > diff --git a/init/Kconfig b/init/Kconfig
> > index 41583f468cb4..ed82f76ed0b7 100644
> > --- a/init/Kconfig
> > +++ b/init/Kconfig
> > @@ -346,6 +346,17 @@ config AUDIT_TREE
> > depends on AUDITSYSCALL
> > select FSNOTIFY
> >  
> > +config KTASK
> > +   bool "Multithread CPU-intensive kernel work"
> > +   depends on SMP
> > +   default y
> > +   help
> > + Parallelize CPU-intensive kernel work.  This feature is designed for
> > +  big machines that can take advantage of their extra CPUs to 
> > speed up
> > + large kernel tasks.  When enabled, kworker threads may occupy more
> > +  CPU time during these kernel tasks, but these threads are 
> > throttled
> > +  when other tasks on the system need CPU time.
> 
> Use tab + 2 spaces consistently for help text indentation, please.

Ok, will do.  Thanks for pointing it out.


Re: [RFC PATCH v4 02/13] ktask: multithread CPU-intensive kernel work

2018-11-05 Thread Randy Dunlap
On 11/5/18 8:55 AM, Daniel Jordan wrote:
> diff --git a/init/Kconfig b/init/Kconfig
> index 41583f468cb4..ed82f76ed0b7 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -346,6 +346,17 @@ config AUDIT_TREE
>   depends on AUDITSYSCALL
>   select FSNOTIFY
>  
> +config KTASK
> + bool "Multithread CPU-intensive kernel work"
> + depends on SMP
> + default y
> + help
> +   Parallelize CPU-intensive kernel work.  This feature is designed for
> +  big machines that can take advantage of their extra CPUs to speed 
> up
> +   large kernel tasks.  When enabled, kworker threads may occupy more
> +  CPU time during these kernel tasks, but these threads are throttled
> +  when other tasks on the system need CPU time.

Use tab + 2 spaces consistently for help text indentation, please.

> +
>  source "kernel/irq/Kconfig"
>  source "kernel/time/Kconfig"
>  source "kernel/Kconfig.preempt"


-- 
~Randy


[RFC PATCH v4 02/13] ktask: multithread CPU-intensive kernel work

2018-11-05 Thread Daniel Jordan
A single CPU can spend an excessive amount of time in the kernel operating on
large amounts of data.  Often these situations arise during initialization- and
destruction-related tasks, where the data involved scales with system size.
These long-running jobs can slow startup and shutdown of applications and the
system itself while extra CPUs sit idle.

To ensure that applications and the kernel continue to perform well as
core counts and memory sizes increase, harness these idle CPUs to
complete such jobs more quickly.

ktask is a generic framework for parallelizing CPU-intensive work in the
kernel.  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, maintaining an efficient
concurrency level, starting these threads, and load balancing the work
between them.

The Documentation patch earlier in this series, from which the above was
swiped, has more background.

Inspired by work from Pavel Tatashin, Steve Sistare, and Jonathan Adams.

Signed-off-by: Daniel Jordan 
Suggested-by: Pavel Tatashin 
Suggested-by: Steve Sistare 
Suggested-by: Jonathan Adams 
---
 include/linux/ktask.h | 237 +++
 init/Kconfig  |  11 +
 init/main.c   |   2 +
 kernel/Makefile   |   2 +-
 kernel/ktask.c| 526 ++
 5 files changed, 777 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/ktask.h
 create mode 100644 kernel/ktask.c

diff --git a/include/linux/ktask.h b/include/linux/ktask.h
new file mode 100644
index ..9c75a93b51b9
--- /dev/null
+++ b/include/linux/ktask.h
@@ -0,0 +1,237 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * ktask.h - framework to parallelize CPU-intensive kernel work
+ *
+ * For more information, see Documentation/core-api/ktask.rst.
+ *
+ * Copyright (c) 2018 Oracle Corporation
+ * Author: Daniel Jordan 
+ */
+#ifndef _LINUX_KTASK_H
+#define _LINUX_KTASK_H
+
+#include 
+#include 
+
+#defineKTASK_RETURN_SUCCESS0
+
+/**
+ * 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: size of this node's work (units are task-specific)
+ * @kn_nid: NUMA node id to run threads on
+ */
+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.  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 a client-specific nonzero error code.
+ */
+typedef int (*ktask_thread_func)(void *start, void *end, void *arg);
+
+/**
+ * typedef ktask_iter_func
+ *
+ * An iterator function that advances the position by size units.
+ *
+ * @position: An object that describes the current position in the task.
+ * @size: The amount 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 size);
+
+/**
+ * ktask_iter_range
+ *
+ * An iterator function for a contiguous range such as an array or address
+ * range.  This is the default iterator; clients may override with
+ * ktask_ctl_set_iter_func.
+ *
+ * @position: An object that describes the current position in the task.
+ *Interpreted as an unsigned long.
+ * @size: The amount to advance in the task (in task-specific units).
+ *
+ * RETURNS:
+ * (position + size)
+ */
+void *ktask_iter_range(void *position, size_t size);
+
+/**
+ * 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_func_arg: An argument to be passed to the thread and undo functions.
+ * @kc_iter_func: 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: max threads to use for the task, actual number may be less
+ *  depending on CPU count, task size, and minimum chunk size.
+ */
+struct ktask_ctl {
+   /* Required arguments set with DEFINE_KTASK_CTL. */
+   ktask_thread_func   kc_thread_func;
+   void*kc_func_arg;
+   size_t  kc_mi