public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Kohli, Gaurav" <gkohli@codeaurora.org>
To: tglx@linutronix.de, mpe@ellerman.id.au, dzickus@redhat.com,
	peterz@infradead.org, mingo@kernel.org, bigeasy@linutronix.de
Cc: linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	Neeraj Upadhyay <neeraju@codeaurora.org>
Subject: Re: [PATCH] kthread/smpboot: Serialize kthread parking against wakeup
Date: Tue, 24 Apr 2018 20:12:49 +0530	[thread overview]
Message-ID: <3e823654-9701-9b01-43fc-1a29feb7a298@codeaurora.org> (raw)
In-Reply-To: <1524562105-31026-1-git-send-email-gkohli@codeaurora.org>

Hi ,

We can also fix below race by smpboot code as well:

@@ -109,7 +109,6 @@ static int smpboot_thread_fn(void *data)

         struct smp_hotplug_thread *ht = td->ht;

         while (1) {

-               set_current_state(TASK_INTERRUPTIBLE);

                 preempt_disable();

                 if (kthread_should_stop()) {

                         __set_current_state(TASK_RUNNING);

@@ -157,6 +156,7 @@ static int smpboot_thread_fn(void *data)

                 if (!ht->thread_should_run(td->cpu)) {

                         preempt_enable_no_resched();

+                       set_current_state(TASK_INTERRUPTIBLE);

                         schedule();

                 } else {

                         __set_current_state(TASK_RUNNING);

Please suggest if this approach is better.

Regards

Gaurav

On 4/24/2018 2:58 PM, Gaurav Kohli wrote:

> The control cpu thread which initiates hotplug calls kthread_park()
> for hotplug thread and sets KTHREAD_SHOULD_PARK. After this control
> thread wakes up the hotplug thread. There is a chance that wakeup
> code sees the hotplug thread (running on AP core) in INTERRUPTIBLE
> state, but sets its state to RUNNING after hotplug thread has entered
> kthread_parkme() and changed its state to TASK_PARKED. This can result
> in panic later on in kthread_unpark(), as it sees KTHREAD_IS_PARKED
> flag set but fails to rebind the kthread, due to it being not in
> TASK_PARKED state. Fix this, by serializing wakeup state change,
> against state change before parking the kthread.
>
> Below is the possible race:
>
> Control thread				      Hotplug Thread
>
> kthread_park()
> set KTHREAD_SHOULD_PARK
> 					      smpboot_thread_fn
> 					      set_current_state(TASK_INTERRUPTIBLE);
> 					      kthread_parkme
>
> wake_up_process()
>
> raw_spin_lock_irqsave(&p->pi_lock, flags);
> if (!(p->state & state)) -> this will fail
>              goto out;
>
> 					      __kthread_parkme
> 					       __set_current_state(TASK_PARKED);
>
> if (p->on_rq && ttwu_remote(p, wake_flags))
>      ttwu_remote()
>          p->state = TASK_RUNNING;
> 						schedule();
>
> So to avoid this race, take pi_lock to serial state changes.
>
> Suggested-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
> Co-developed-by: Neeraj Upadhyay <neeraju@codeaurora.org>
> Signed-off-by: Neeraj Upadhyay <neeraju@codeaurora.org>
> Signed-off-by: Gaurav Kohli <gkohli@codeaurora.org>
>
> diff --git a/kernel/smpboot.c b/kernel/smpboot.c
> index 1650578..514b232 100644
> --- a/kernel/smpboot.c
> +++ b/kernel/smpboot.c
> @@ -121,7 +121,9 @@ static int smpboot_thread_fn(void *data)
>   		}
>   
>   		if (kthread_should_park()) {
> +			raw_spin_lock(&current->pi_lock);
>   			__set_current_state(TASK_RUNNING);
> +			raw_spin_unlock(&current->pi_lock);
>   			preempt_enable();
>   			if (ht->park && td->status == HP_THREAD_ACTIVE) {
>   				BUG_ON(td->cpu != smp_processor_id());
>
-- 
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.

  reply	other threads:[~2018-04-24 14:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-24  9:28 [PATCH] kthread/smpboot: Serialize kthread parking against wakeup Gaurav Kohli
2018-04-24 14:42 ` Kohli, Gaurav [this message]
2018-04-24 18:05   ` Peter Zijlstra
2018-04-24 18:26 ` Peter Zijlstra
2018-04-24 18:46   ` Kohli, Gaurav

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=3e823654-9701-9b01-43fc-1a29feb7a298@codeaurora.org \
    --to=gkohli@codeaurora.org \
    --cc=bigeasy@linutronix.de \
    --cc=dzickus@redhat.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mpe@ellerman.id.au \
    --cc=neeraju@codeaurora.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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