Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1653687 > unrolled thread
| Started by | Junaid Shahid <junaids@google.com> |
|---|---|
| First post | 2017-05-31 00:40 +0200 |
| Last post | 2017-05-31 00:50 +0200 |
| Articles | 2 — 2 participants |
Back to article view | Back to linux.kernel
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: [PATCH] kthread: Fix race condition between kthread_parkme() and kthread_unpark() Junaid Shahid <junaids@google.com> - 2017-05-31 00:40 +0200
Re: [PATCH] kthread: Fix race condition between kthread_parkme() and kthread_unpark() Andrew Morton <akpm@linux-foundation.org> - 2017-05-31 00:50 +0200
| From | Junaid Shahid <junaids@google.com> |
|---|---|
| Date | 2017-05-31 00:40 +0200 |
| Subject | Re: [PATCH] kthread: Fix race condition between kthread_parkme() and kthread_unpark() |
| Message-ID | <tMXXX-4Lf-5@gated-at.bofh.it> |
(Resending)
On Friday, April 28, 2017 07:32:36 PM Junaid Shahid wrote:
> In general, if kthread_unpark() and kthread_parkme() execute together,
> the kthread is supposed to be in an unparked state. This is because
> kthread_unpark() either wakes up the thread if it already got parked,
> or it cancels a prior kthread_park() call and hence renders the
> kthread_parkme() moot.
>
> However, if kthread_unpark() happens to execute between the time that
> kthread_parkme() checks the KTHREAD_SHOULD_STOP flag and sets the
> KTHREAD_IS_PARKED flag, then kthread_unpark() will not wake up the
> thread and it will remain in a parked state.
>
> This is fixed by making the checking of KTHREAD_SHOULD_STOP and the
> setting of KTHREAD_IS_PARKED atomic via a cmpxchg inside kthread_parkme.
>
> Signed-off-by: Junaid Shahid <junaids@google.com>
> ---
> kernel/kthread.c | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/kthread.c b/kernel/kthread.c
> index 26db528c1d88..651f03baf62f 100644
> --- a/kernel/kthread.c
> +++ b/kernel/kthread.c
> @@ -169,12 +169,20 @@ void *kthread_probe_data(struct task_struct *task)
>
> static void __kthread_parkme(struct kthread *self)
> {
> + ulong flags;
> +
> __set_current_state(TASK_PARKED);
> - while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
> - if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
> - complete(&self->parked);
> - schedule();
> + flags = self->flags;
> +
> + while (test_bit(KTHREAD_SHOULD_PARK, &flags)) {
> + if (cmpxchg(&self->flags, flags,
> + flags | (1 << KTHREAD_IS_PARKED)) == flags) {
> + if (!test_bit(KTHREAD_IS_PARKED, &flags))
> + complete(&self->parked);
> + schedule();
> + }
> __set_current_state(TASK_PARKED);
> + flags = self->flags;
> }
> clear_bit(KTHREAD_IS_PARKED, &self->flags);
> __set_current_state(TASK_RUNNING);
>
[toc] | [next] | [standalone]
| From | Andrew Morton <akpm@linux-foundation.org> |
|---|---|
| Date | 2017-05-31 00:50 +0200 |
| Subject | Re: [PATCH] kthread: Fix race condition between kthread_parkme() and kthread_unpark() |
| Message-ID | <tMY7D-4OE-13@gated-at.bofh.it> |
| In reply to | #1653687 |
(cc tglx & tj)
On Tue, 30 May 2017 15:37:23 -0700 Junaid Shahid <junaids@google.com> wrote:
> (Resending)
>
> On Friday, April 28, 2017 07:32:36 PM Junaid Shahid wrote:
> > In general, if kthread_unpark() and kthread_parkme() execute together,
> > the kthread is supposed to be in an unparked state. This is because
> > kthread_unpark() either wakes up the thread if it already got parked,
> > or it cancels a prior kthread_park() call and hence renders the
> > kthread_parkme() moot.
> >
> > However, if kthread_unpark() happens to execute between the time that
> > kthread_parkme() checks the KTHREAD_SHOULD_STOP flag and sets the
> > KTHREAD_IS_PARKED flag, then kthread_unpark() will not wake up the
> > thread and it will remain in a parked state.
> >
> > This is fixed by making the checking of KTHREAD_SHOULD_STOP and the
> > setting of KTHREAD_IS_PARKED atomic via a cmpxchg inside kthread_parkme.
> >
> > Signed-off-by: Junaid Shahid <junaids@google.com>
> > ---
> > kernel/kthread.c | 16 ++++++++++++----
> > 1 file changed, 12 insertions(+), 4 deletions(-)
> >
> > diff --git a/kernel/kthread.c b/kernel/kthread.c
> > index 26db528c1d88..651f03baf62f 100644
> > --- a/kernel/kthread.c
> > +++ b/kernel/kthread.c
> > @@ -169,12 +169,20 @@ void *kthread_probe_data(struct task_struct *task)
> >
> > static void __kthread_parkme(struct kthread *self)
> > {
> > + ulong flags;
> > +
> > __set_current_state(TASK_PARKED);
> > - while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
> > - if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
> > - complete(&self->parked);
> > - schedule();
> > + flags = self->flags;
> > +
> > + while (test_bit(KTHREAD_SHOULD_PARK, &flags)) {
> > + if (cmpxchg(&self->flags, flags,
> > + flags | (1 << KTHREAD_IS_PARKED)) == flags) {
> > + if (!test_bit(KTHREAD_IS_PARKED, &flags))
> > + complete(&self->parked);
> > + schedule();
> > + }
> > __set_current_state(TASK_PARKED);
> > + flags = self->flags;
> > }
> > clear_bit(KTHREAD_IS_PARKED, &self->flags);
> > __set_current_state(TASK_RUNNING);
> >
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web