workqueue: make workers threads stick to HK_TYPE_KTHREAD cpumask

Message ID 20221103030933.840989-1-l3b2w1@gmail.com
State New
Headers
Series workqueue: make workers threads stick to HK_TYPE_KTHREAD cpumask |

Commit Message

Binglei Wang Nov. 3, 2022, 3:09 a.m. UTC
  From: Binglei Wang <l3b2w1@gmail.com>

When new worker thread created, set its affinity to HK_TYPE_KTHREAD
cpumask.
When hotplug cpu online, rebind workers's affinity to HK_TYPE_KTHREAD
cpumask.
Make workers threads stick to HK_TYPE_KTHREAD cpumask all the time.

Signed-off-by: Binglei Wang <l3b2w1@gmail.com>
---
 kernel/workqueue.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
  

Comments

kernel test robot Nov. 4, 2022, 3:18 a.m. UTC | #1
Hi Binglei,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tj-wq/for-next]
[also build test ERROR on linus/master v6.1-rc3 next-20221103]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Binglei-Wang/workqueue-make-workers-threads-stick-to-HK_TYPE_KTHREAD-cpumask/20221103-111140
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git for-next
patch link:    https://lore.kernel.org/r/20221103030933.840989-1-l3b2w1%40gmail.com
patch subject: [PATCH] workqueue: make workers threads stick to HK_TYPE_KTHREAD cpumask
config: nios2-randconfig-r014-20221103
compiler: nios2-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/96d045fb333d1098f273ad89d6a7215d8ea02ac4
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Binglei-Wang/workqueue-make-workers-threads-stick-to-HK_TYPE_KTHREAD-cpumask/20221103-111140
        git checkout 96d045fb333d1098f273ad89d6a7215d8ea02ac4
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=nios2 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   kernel/workqueue.c: In function 'create_worker':
>> kernel/workqueue.c:1958:25: error: assignment to 'const struct cupmask *' from incompatible pointer type 'const struct cpumask *' [-Werror=incompatible-pointer-types]
    1958 |                 cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
         |                         ^
   kernel/workqueue.c:1959:59: warning: pointer type mismatch in conditional expression
    1959 |         kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
         |                                                           ^
   cc1: some warnings being treated as errors


vim +1958 kernel/workqueue.c

  1913	
  1914	/**
  1915	 * create_worker - create a new workqueue worker
  1916	 * @pool: pool the new worker will belong to
  1917	 *
  1918	 * Create and start a new worker which is attached to @pool.
  1919	 *
  1920	 * CONTEXT:
  1921	 * Might sleep.  Does GFP_KERNEL allocations.
  1922	 *
  1923	 * Return:
  1924	 * Pointer to the newly created worker.
  1925	 */
  1926	static struct worker *create_worker(struct worker_pool *pool)
  1927	{
  1928		struct worker *worker;
  1929		int id;
  1930		char id_buf[16];
  1931		const struct cupmask *cpumask = NULL;
  1932	
  1933		/* ID is needed to determine kthread name */
  1934		id = ida_alloc(&pool->worker_ida, GFP_KERNEL);
  1935		if (id < 0)
  1936			return NULL;
  1937	
  1938		worker = alloc_worker(pool->node);
  1939		if (!worker)
  1940			goto fail;
  1941	
  1942		worker->id = id;
  1943	
  1944		if (pool->cpu >= 0)
  1945			snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
  1946				 pool->attrs->nice < 0  ? "H" : "");
  1947		else
  1948			snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
  1949	
  1950		worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
  1951						      "kworker/%s", id_buf);
  1952		if (IS_ERR(worker->task))
  1953			goto fail;
  1954	
  1955		set_user_nice(worker->task, pool->attrs->nice);
  1956	
  1957		if (housekeeping_enabled(HK_TYPE_KTHREAD))
> 1958			cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
  1959		kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
  1960	
  1961		/* successful, attach the worker to the pool */
  1962		worker_attach_to_pool(worker, pool);
  1963	
  1964		/* start the newly created worker */
  1965		raw_spin_lock_irq(&pool->lock);
  1966		worker->pool->nr_workers++;
  1967		worker_enter_idle(worker);
  1968		wake_up_process(worker->task);
  1969		raw_spin_unlock_irq(&pool->lock);
  1970	
  1971		return worker;
  1972	
  1973	fail:
  1974		ida_free(&pool->worker_ida, id);
  1975		kfree(worker);
  1976		return NULL;
  1977	}
  1978
  
kernel test robot Nov. 4, 2022, 6:30 a.m. UTC | #2
Hi Binglei,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tj-wq/for-next]
[also build test ERROR on linus/master v6.1-rc3 next-20221103]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Binglei-Wang/workqueue-make-workers-threads-stick-to-HK_TYPE_KTHREAD-cpumask/20221103-111140
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git for-next
patch link:    https://lore.kernel.org/r/20221103030933.840989-1-l3b2w1%40gmail.com
patch subject: [PATCH] workqueue: make workers threads stick to HK_TYPE_KTHREAD cpumask
config: hexagon-randconfig-r014-20221102
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 791a7ae1ba3efd6bca96338e10ffde557ba83920)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/96d045fb333d1098f273ad89d6a7215d8ea02ac4
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Binglei-Wang/workqueue-make-workers-threads-stick-to-HK_TYPE_KTHREAD-cpumask/20221103-111140
        git checkout 96d045fb333d1098f273ad89d6a7215d8ea02ac4
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All error/warnings (new ones prefixed by >>):

>> kernel/workqueue.c:1958:11: error: incompatible pointer types assigning to 'const struct cupmask *' from 'const struct cpumask *' [-Werror,-Wincompatible-pointer-types]
                   cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
                           ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/workqueue.c:1959:42: warning: pointer type mismatch ('const struct cupmask *' and 'struct cpumask *') [-Wpointer-type-mismatch]
           kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
                                                   ^ ~~~~~~~   ~~~~~~~~~~~~~~~~~~~~
   1 warning and 1 error generated.


vim +1958 kernel/workqueue.c

  1913	
  1914	/**
  1915	 * create_worker - create a new workqueue worker
  1916	 * @pool: pool the new worker will belong to
  1917	 *
  1918	 * Create and start a new worker which is attached to @pool.
  1919	 *
  1920	 * CONTEXT:
  1921	 * Might sleep.  Does GFP_KERNEL allocations.
  1922	 *
  1923	 * Return:
  1924	 * Pointer to the newly created worker.
  1925	 */
  1926	static struct worker *create_worker(struct worker_pool *pool)
  1927	{
  1928		struct worker *worker;
  1929		int id;
  1930		char id_buf[16];
  1931		const struct cupmask *cpumask = NULL;
  1932	
  1933		/* ID is needed to determine kthread name */
  1934		id = ida_alloc(&pool->worker_ida, GFP_KERNEL);
  1935		if (id < 0)
  1936			return NULL;
  1937	
  1938		worker = alloc_worker(pool->node);
  1939		if (!worker)
  1940			goto fail;
  1941	
  1942		worker->id = id;
  1943	
  1944		if (pool->cpu >= 0)
  1945			snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
  1946				 pool->attrs->nice < 0  ? "H" : "");
  1947		else
  1948			snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
  1949	
  1950		worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
  1951						      "kworker/%s", id_buf);
  1952		if (IS_ERR(worker->task))
  1953			goto fail;
  1954	
  1955		set_user_nice(worker->task, pool->attrs->nice);
  1956	
  1957		if (housekeeping_enabled(HK_TYPE_KTHREAD))
> 1958			cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
> 1959		kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
  1960	
  1961		/* successful, attach the worker to the pool */
  1962		worker_attach_to_pool(worker, pool);
  1963	
  1964		/* start the newly created worker */
  1965		raw_spin_lock_irq(&pool->lock);
  1966		worker->pool->nr_workers++;
  1967		worker_enter_idle(worker);
  1968		wake_up_process(worker->task);
  1969		raw_spin_unlock_irq(&pool->lock);
  1970	
  1971		return worker;
  1972	
  1973	fail:
  1974		ida_free(&pool->worker_ida, id);
  1975		kfree(worker);
  1976		return NULL;
  1977	}
  1978
  
kernel test robot Nov. 4, 2022, 6:18 p.m. UTC | #3
Hi Binglei,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tj-wq/for-next]
[also build test WARNING on linus/master v6.1-rc3 next-20221104]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Binglei-Wang/workqueue-make-workers-threads-stick-to-HK_TYPE_KTHREAD-cpumask/20221103-111140
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git for-next
patch link:    https://lore.kernel.org/r/20221103030933.840989-1-l3b2w1%40gmail.com
patch subject: [PATCH] workqueue: make workers threads stick to HK_TYPE_KTHREAD cpumask
config: arm64-randconfig-r032-20221102
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 791a7ae1ba3efd6bca96338e10ffde557ba83920)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm64 cross compiling tool for clang build
        # apt-get install binutils-aarch64-linux-gnu
        # https://github.com/intel-lab-lkp/linux/commit/96d045fb333d1098f273ad89d6a7215d8ea02ac4
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Binglei-Wang/workqueue-make-workers-threads-stick-to-HK_TYPE_KTHREAD-cpumask/20221103-111140
        git checkout 96d045fb333d1098f273ad89d6a7215d8ea02ac4
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   kernel/workqueue.c:1958:11: error: incompatible pointer types assigning to 'const struct cupmask *' from 'const struct cpumask *' [-Werror,-Wincompatible-pointer-types]
                   cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
                           ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/workqueue.c:1959:42: warning: pointer type mismatch ('const struct cupmask *' and 'cpumask_var_t' (aka 'struct cpumask *')) [-Wpointer-type-mismatch]
           kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
                                                   ^ ~~~~~~~   ~~~~~~~~~~~~~~~~~~~~
   1 warning and 1 error generated.


vim +1959 kernel/workqueue.c

  1913	
  1914	/**
  1915	 * create_worker - create a new workqueue worker
  1916	 * @pool: pool the new worker will belong to
  1917	 *
  1918	 * Create and start a new worker which is attached to @pool.
  1919	 *
  1920	 * CONTEXT:
  1921	 * Might sleep.  Does GFP_KERNEL allocations.
  1922	 *
  1923	 * Return:
  1924	 * Pointer to the newly created worker.
  1925	 */
  1926	static struct worker *create_worker(struct worker_pool *pool)
  1927	{
  1928		struct worker *worker;
  1929		int id;
  1930		char id_buf[16];
  1931		const struct cupmask *cpumask = NULL;
  1932	
  1933		/* ID is needed to determine kthread name */
  1934		id = ida_alloc(&pool->worker_ida, GFP_KERNEL);
  1935		if (id < 0)
  1936			return NULL;
  1937	
  1938		worker = alloc_worker(pool->node);
  1939		if (!worker)
  1940			goto fail;
  1941	
  1942		worker->id = id;
  1943	
  1944		if (pool->cpu >= 0)
  1945			snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
  1946				 pool->attrs->nice < 0  ? "H" : "");
  1947		else
  1948			snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
  1949	
  1950		worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
  1951						      "kworker/%s", id_buf);
  1952		if (IS_ERR(worker->task))
  1953			goto fail;
  1954	
  1955		set_user_nice(worker->task, pool->attrs->nice);
  1956	
  1957		if (housekeeping_enabled(HK_TYPE_KTHREAD))
  1958			cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
> 1959		kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
  1960	
  1961		/* successful, attach the worker to the pool */
  1962		worker_attach_to_pool(worker, pool);
  1963	
  1964		/* start the newly created worker */
  1965		raw_spin_lock_irq(&pool->lock);
  1966		worker->pool->nr_workers++;
  1967		worker_enter_idle(worker);
  1968		wake_up_process(worker->task);
  1969		raw_spin_unlock_irq(&pool->lock);
  1970	
  1971		return worker;
  1972	
  1973	fail:
  1974		ida_free(&pool->worker_ida, id);
  1975		kfree(worker);
  1976		return NULL;
  1977	}
  1978
  

Patch

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 7cd5f5e7e..77b303f5e 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -1928,6 +1928,7 @@  static struct worker *create_worker(struct worker_pool *pool)
 	struct worker *worker;
 	int id;
 	char id_buf[16];
+	const struct cupmask *cpumask = NULL;
 
 	/* ID is needed to determine kthread name */
 	id = ida_alloc(&pool->worker_ida, GFP_KERNEL);
@@ -1952,7 +1953,10 @@  static struct worker *create_worker(struct worker_pool *pool)
 		goto fail;
 
 	set_user_nice(worker->task, pool->attrs->nice);
-	kthread_bind_mask(worker->task, pool->attrs->cpumask);
+
+	if (housekeeping_enabled(HK_TYPE_KTHREAD))
+		cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
+	kthread_bind_mask(worker->task, cpumask ? cpumask : pool->attrs->cpumask);
 
 	/* successful, attach the worker to the pool */
 	worker_attach_to_pool(worker, pool);
@@ -5027,20 +5031,26 @@  static void unbind_workers(int cpu)
 static void rebind_workers(struct worker_pool *pool)
 {
 	struct worker *worker;
+	const struct cpumask *cpumask = NULL;
 
 	lockdep_assert_held(&wq_pool_attach_mutex);
 
+	if (housekeeping_enabled(HK_TYPE_KTHREAD))
+		cpumask = housekeeping_cpumask(HK_TYPE_KTHREAD);
+
 	/*
 	 * Restore CPU affinity of all workers.  As all idle workers should
 	 * be on the run-queue of the associated CPU before any local
 	 * wake-ups for concurrency management happen, restore CPU affinity
 	 * of all workers first and then clear UNBOUND.  As we're called
 	 * from CPU_ONLINE, the following shouldn't fail.
+	 *
+	 * Also consider the housekeeping HK_TYPE_KTHREAD cpumask.
 	 */
 	for_each_pool_worker(worker, pool) {
 		kthread_set_per_cpu(worker->task, pool->cpu);
 		WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
-						  pool->attrs->cpumask) < 0);
+						  cpumask ? cpumask : pool->attrs->cpumask) < 0);
 	}
 
 	raw_spin_lock_irq(&pool->lock);