All of lore.kernel.org
 help / color / mirror / Atom feed
From: Valentin Schneider <vschneid@redhat.com>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Herbert Xu <herbert@gondor.apana.org.au>
Cc: linux-wireless@vger.kernel.org, kvalo@kernel.org,
	stable@vger.kernel.org, "Gregory Erwin" <gregerwin256@gmail.com>,
	"Toke Høiland-Jørgensen" <toke@toke.dk>,
	"Eric W . Biederman" <ebiederm@xmission.com>
Subject: Re: [PATCH RESEND v11] hwrng: core - let sleep be interrupted when unregistering hwrng
Date: Tue, 26 Jul 2022 12:32:50 +0100	[thread overview]
Message-ID: <xhsmhpmhsds7x.mognet@vschneid.remote.csb> (raw)
In-Reply-To: <Yt+/HvfC+OYRVrr+@zx2c4.com>

On 26/07/22 12:17, Jason A. Donenfeld wrote:
>>                 if (rc <= 0) {
>> -                       pr_warn("hwrng: no data available\n");
>> -                       msleep_interruptible(10000);
>> +                       set_current_state(TASK_INTERRUPTIBLE);
>> +                       if (kthread_should_stop())
>> +                               __set_current_state(TASK_RUNNING);
>> +                       schedule_timeout(10 * HZ);
>>                         continue;
>>                 }
>
> Here you made a change whose utility I don't understand. My original
> hunk was:
>
> +                       if (kthread_should_stop())
> +                               break;
> +                       schedule_timeout_interruptible(HZ * 10);
>
> Which I think is a bit cleaner, as schedule_timeout_interruptible sets
> the state to INTERRUPTIBLE and such.
>

For any sort of wait loop, you need the state write to happen before the
loop-break condition is checked.

Consider:

  READ kthread_should_stop() == false
                                                  kthread_stop()
                                                    set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
                                                    wake_up_process(k); // Reads TASK_RUNNING,
  schedule_timeout_interruptible();                                     // does nothing
  // We're now blocked, having missed a wakeup

That's why the canonical wait loop pattern is:

   for (;;) {
      set_current_state(TASK_UNINTERRUPTIBLE);

      if (CONDITION)
         break;

      schedule();
   }
   __set_current_state(TASK_RUNNING);

(grep "wait loop" in kernel/sched/core.c)

> Regards,
> Jason


  reply	other threads:[~2022-07-26 11:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-25 21:55 [PATCH RESEND v10] ath9k: let sleep be interrupted when unregistering hwrng Jason A. Donenfeld
2022-07-26  9:44 ` [PATCH RESEND v11] hwrng: core - " Herbert Xu
2022-07-26 10:17   ` Jason A. Donenfeld
2022-07-26 11:32     ` Valentin Schneider [this message]
2022-07-27  6:32     ` Kalle Valo
2022-07-27  8:34       ` Herbert Xu
2022-07-27  8:44         ` Kalle Valo
2022-07-27  8:50     ` Herbert Xu
2022-07-27  9:01     ` Herbert Xu
2022-07-28 10:22   ` [v12 PATCH] " Herbert Xu
2022-07-28 12:39     ` Toke Høiland-Jørgensen
2022-07-28 13:01       ` Kalle Valo

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=xhsmhpmhsds7x.mognet@vschneid.remote.csb \
    --to=vschneid@redhat.com \
    --cc=Jason@zx2c4.com \
    --cc=ebiederm@xmission.com \
    --cc=gregerwin256@gmail.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=kvalo@kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=toke@toke.dk \
    /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.