mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lai Jiangshan <jiangshanlai@gmail.com>
To: Schspa Shi <schspa@gmail.com>, tj@kernel.org
Cc: linux-kernel@vger.kernel.org, zhaohui.shi@horizon.ai,
	Peter Zijlstra <peterz@infradead.org>
Subject: Re: [PATCH] workqueue: Use active mask for new worker when pool is DISASSOCIATED
Date: Wed, 13 Jul 2022 17:52:58 +0800	[thread overview]
Message-ID: <0320c5f9-cbda-1652-1f97-24d1a22fb298@gmail.com> (raw)
In-Reply-To: <20220707090501.55483-1-schspa@gmail.com>



CC Peter.
Peter has changed the CPU binding code in workqueue.c.

I'm not understanding the problem enough, if kthread_bind_mask() is buggy
in workqueue.c, it would be buggy in other places too.


On 2022/7/7 17:05, Schspa Shi wrote:

>   
> -	if (worker->rescue_wq)
> -		set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
> +	if (worker->rescue_wq) {
> +		if (pool->flags & POOL_DISASSOCIATED)
> +			set_cpus_allowed_ptr(worker->task, cpu_active_mask);
> +		else
> +			set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
> +	}
>   

For unbound pools (which also has POOL_DISASSOCIATED), pool->attrs->cpumask
should be used if pool->attrs->cpumask has active cpu.


> +
> +	mutex_lock(&wq_pool_attach_mutex);
> +	if ((pool->flags & POOL_DISASSOCIATED)) {
> +		/* We can't call get_online_cpus, there will be deadlock
> +		 * cpu_active_mask will no change, because we have
> +		 * wq_pool_attach_mutex hold.
> +		 **/
> +		kthread_bind_mask(worker->task, cpu_active_mask);
> +	} else {
> +		kthread_bind_mask(worker->task, pool->attrs->cpumask);
> +	}
> +	mutex_unlock(&wq_pool_attach_mutex);


For unbound pools, pool->attrs->cpumask should be used if pool->attrs->cpumask
has active cpu.

wq_pool_attach_mutex is held here and in worker_attach_to_pool() which smells bad.



The change is complex.  And if kthread_bind_mask() can't work as expected here,
the change I prefer would be:

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 4056f2a3f9d5..1ad8aef5fe98 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -1862,6 +1862,12 @@ static void worker_attach_to_pool(struct worker *worker,
  {
  	mutex_lock(&wq_pool_attach_mutex);

+	/*
+	 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
+	 * online CPUs.  It'll be re-applied when any of the CPUs come up.
+	 */
+	set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
+
  	/*
  	 * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains
  	 * stable across this function.  See the comments above the flag
@@ -1872,9 +1877,6 @@ static void worker_attach_to_pool(struct worker *worker,
  	else
  		kthread_set_per_cpu(worker->task, pool->cpu);

-	if (worker->rescue_wq)
-		set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
-
  	list_add_tail(&worker->node, &pool->workers);
  	worker->pool = pool;

@@ -1952,7 +1954,7 @@ 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);
+	worker->flags |= PF_NO_SETAFFINITY;

  	/* successful, attach the worker to the pool */
  	worker_attach_to_pool(worker, pool);
@@ -4270,7 +4272,7 @@ static int init_rescuer(struct workqueue_struct *wq)
  	}

  	wq->rescuer = rescuer;
-	kthread_bind_mask(rescuer->task, cpu_possible_mask);
+	rescuer->flags |= PF_NO_SETAFFINITY;
  	wake_up_process(rescuer->task);

  	return 0;


It is untested.  It effectively reverts the commit 640f17c82460e
("workqueue: Restrict affinity change to rescuer").
It avoids using kthread_bind_mask().



  parent reply	other threads:[~2022-07-13  9:53 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-07  9:05 Schspa Shi
2022-07-13  2:52 ` [workqueue] 1a0a67f5ef: phoronix-test-suite.fio.SequentialRead.IO_uring.Yes.No.1MB.DefaultTestDirectory.mb_s -17.7% regression kernel test robot
2022-07-13  6:09   ` Schspa Shi
2022-07-13  9:52 ` Lai Jiangshan [this message]
2022-07-13 11:22   ` [PATCH] workqueue: Use active mask for new worker when pool is DISASSOCIATED Schspa Shi
2022-07-14 14:39   ` Peter Zijlstra
2022-07-14 15:17     ` Schspa Shi
2022-07-30  3:49     ` Schspa Shi
2022-08-01  3:56       ` Lai Jiangshan
2022-08-01  4:42         ` Schspa Shi
2022-08-01  8:48           ` Lai Jiangshan
2022-08-01  9:32             ` Schspa Shi
2022-08-07 13:52             ` Schspa Shi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0320c5f9-cbda-1652-1f97-24d1a22fb298@gmail.com \
    --to=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=schspa@gmail.com \
    --cc=tj@kernel.org \
    --cc=zhaohui.shi@horizon.ai \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®