From: Peter Zijlstra <peterz@infradead.org>
To: Wanpeng Li <wanpeng.li@hotmail.com>
Cc: Ingo Molnar <mingo@kernel.org>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sched: fix tsk->pi_lock isn't held when do_set_cpus_allowed()
Date: Fri, 28 Aug 2015 00:18:28 +0200 [thread overview]
Message-ID: <20150827221828.GZ16853@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <BLU437-SMTP7261DF7A82716F49242C7B80610@phx.gbl>
On Tue, Aug 25, 2015 at 07:47:44PM +0800, Wanpeng Li wrote:
> On 8/25/15 6:32 PM, Peter Zijlstra wrote:
> >So Possibly, Maybe (I'm still to wrecked to say for sure), something
> >like this would work:
> >
> > WARN_ON(debug_locks && (lockdep_is_held(&p->pi_lock) ||
> > (p->on_rq && lockdep_is_held(&rq->lock))));
> >
> >Instead of those two separate lockdep asserts.
> >
> >Please consider carefully.
So the normal rules for changing task_struct::cpus_allowed are holding
both pi_lock and rq->lock, such that holding either stabilizes the mask.
This is so that wakeup can happen without rq->lock and load-balance
without pi_lock.
>From this we already get the relaxation that we can omit acquiring
rq->lock if the task is not on the rq, because in that case
load-balancing will not apply to it.
** these are the rules currently tested in do_set_cpus_allowed() **
Now, since __set_cpus_allowed_ptr() uses task_rq_lock() which
unconditionally acquires both locks, we could get away with holding just
rq->lock when on_rq for modification because that'd still exclude
__set_cpus_allowed_ptr(), it would also work against
__kthread_bind_mask() because that assumes !on_rq.
That said, this is all somewhat fragile.
> Commit (5e16bbc2f: sched: Streamline the task migration locking a little)
> won't hold the pi_lock in migrate_tasks() path any more, actually pi_lock
> was still not held when call select_fallback_rq() and it was held in
> __migrate_task() before the commit. Then commit (25834c73f93: sched: Fix a
> race between __kthread_bind() and sched_setaffinity()) add a
> lockdep_assert_held() in do_set_cpus_allowed(), the bug is triggered. How
> about something like below:
>
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -5186,6 +5186,15 @@ static void migrate_tasks(struct rq *dead_rq)
> BUG_ON(!next);
> next->sched_class->put_prev_task(rq, next);
>
> + raw_spin_unlock(&rq->lock);
> + raw_spin_lock(&next->pi_lock);
> + raw_spin_lock(&rq->lock);
> + if (!(task_rq(next) == rq && task_on_rq_queued(next))) {
> + raw_spin_unlock(&rq->lock);
> + raw_spin_unlock(&next->pi_lock);
> + continue;
> + }
Yeah, that's quite disgusting.. also you'll trip over the lockdep_pin if
you were to actually run this.
Now, I don't think dropping rq->lock is quite as disastrous as it
usually is because !cpu_active at this point, which means load-balance
will not interfere, but that too is somewhat fragile.
So we end up with a choice of two fragile.. let me ponder that a wee
bit more.
next prev parent reply other threads:[~2015-08-27 22:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-25 7:59 [PATCH] sched: fix tsk->pi_lock isn't held when do_set_cpus_allowed() Wanpeng Li
2015-08-25 8:13 ` Leo Yan
2015-08-25 8:24 ` Wanpeng Li
2015-08-25 8:30 ` Ingo Molnar
2015-08-25 8:38 ` Wanpeng Li
2015-08-25 10:05 ` Peter Zijlstra
2015-08-25 10:10 ` Peter Zijlstra
2015-08-25 10:32 ` Peter Zijlstra
[not found] ` <BLU437-SMTP7261DF7A82716F49242C7B80610@phx.gbl>
2015-08-27 22:18 ` Peter Zijlstra [this message]
2015-08-28 1:49 ` Wanpeng Li
2015-08-25 10:18 ` Wanpeng Li
2015-08-27 13:47 ` T. Zhou
-- strict thread matches above, loose matches on Subject: below --
2015-08-25 7:56 Wanpeng Li
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=20150827221828.GZ16853@twins.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=wanpeng.li@hotmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.