public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-kernel@vger.kernel.org, Nicholas Piggin <npiggin@gmail.com>,
	Mike Christie <michael.christie@oracle.com>,
	Zqiang <qiang1.zhang@intel.com>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH] kthread: kthread_should_stop() checks if we're a kthread
Date: Tue, 28 Nov 2023 10:30:49 +0100	[thread overview]
Message-ID: <ZWWzSZeyRuPDwBHZ@alley> (raw)
In-Reply-To: <20231120221503.3378095-1-kent.overstreet@linux.dev>

Adding Andrew into Cc. He usually takes changes in kernel/kthread.c.

On Mon 2023-11-20 17:15:03, Kent Overstreet wrote:
> bcachefs has a fair amount of code that may or may not be running from a
> kthread (it may instead be called by a userspace ioctl); having
> kthread_should_stop() check if we're a kthread enables a fair bit of
> cleanup and makes it safer to use.
> 
> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
> ---
>  kernel/kthread.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/kthread.c b/kernel/kthread.c
> index 1eea53050bab..fe6090ddf414 100644
> --- a/kernel/kthread.c
> +++ b/kernel/kthread.c
> @@ -155,7 +155,8 @@ void free_kthread_struct(struct task_struct *k)
>   */
>  bool kthread_should_stop(void)
>  {
> -	return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
> +	return (current->flags & PF_KTHREAD) &&
> +		test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
>  }
>  EXPORT_SYMBOL(kthread_should_stop);

I agree that it makes the API more safe because &to_kthread(current)
is NULL when the process is not a kthread.

Well, I do not like the idea of quietly ignoring a misuse of
the kthread_*() API. I would personally prefer to do:

// define this in include/linux/kthread.h
static inline bool in_kthread(void)
{
	return current->flags & PF_KTHREAD
}

// add WARN() into kthread_should_stop()
bool kthread_should_stop(void)
{
	if (WARN_ON_ONCE(!in_kthread))
		return false;

	return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
}
EXPORT_SYMBOL(kthread_should_stop);


And use the following in bcachefs() code:

	if (in_kthread() && kthread_should_stop())
		goto exit;


Is see several advantages:

    + It will warn when the API is misused.

    + It will be more clear that the bcachefs code might be
      used in both kthread and userspace code.

    + in_kthread() might be used around other code which is
      needed only when the process is a kthread.

    + Similar check and WARN() might be used also in the other
      kthread() API.


Best Regards,
Petr

  reply	other threads:[~2023-11-28  9:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 22:15 [PATCH] kthread: kthread_should_stop() checks if we're a kthread Kent Overstreet
2023-11-28  9:30 ` Petr Mladek [this message]
2023-11-28 17:40   ` Kent Overstreet
2023-11-30 11:24     ` Petr Mladek

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=ZWWzSZeyRuPDwBHZ@alley \
    --to=pmladek@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.christie@oracle.com \
    --cc=npiggin@gmail.com \
    --cc=qiang1.zhang@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox