From: Al Viro <viro@zeniv.linux.org.uk>
To: zhangjiao2 <zhangjiao2@cmss.chinamobile.com>
Cc: axboe@kernel.dk, justin@coraid.com, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] aoe: Use IS_ERR_OR_NULL() to clean code
Date: Fri, 23 Aug 2024 08:30:55 +0100 [thread overview]
Message-ID: <20240823073055.GD1049718@ZenIV> (raw)
In-Reply-To: <20240823052640.3668-1-zhangjiao2@cmss.chinamobile.com>
On Fri, Aug 23, 2024 at 01:26:40PM +0800, zhangjiao2 wrote:
> From: Zhang Jiao <zhangjiao2@cmss.chinamobile.com>
>
> Use IS_ERR_OR_NULL() to make the code cleaner.
ITYM "obfuscate the bogus code".
Take a look at kthread_run() definition:
#define kthread_run(threadfn, data, namefmt, ...) \
({ \
struct task_struct *__k \
= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
if (!IS_ERR(__k)) \
wake_up_process(__k); \
__k; \
})
OK, what would need to happen for that to return NULL? kthread_create()
returning NULL *AND* wake_up_process(NULL) surviving, right?
int wake_up_process(struct task_struct *p)
{
return try_to_wake_up(p, TASK_NORMAL, 0);
}
OK, so we'd need try_to_wake_up(NULL, ...) to survive execution:
int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
{
guard(preempt)();
int cpu, success = 0;
if (p == current) {
whatever, current is never NULL or a lot of places would be
utterly screwed
}
/* some comment */
scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
... and that would start with trying to grab &NULL->pi_lock, which is not
going to survive.
> task = kthread_run(kthread, k, "%s", k->name);
> - if (task == NULL || IS_ERR(task))
> + if (IS_ERR_OR_NULL(task))
> return -ENOMEM;
In other words, task == NULL had been pointless all along. Your change only
makes it harder to spot.
IS_ERR_OR_NULL is almost never the right thing to do; there are cases where
a function may legitimately return a pointer to object, NULL *or* ERR_PTR(something),
but most of the time it's either impossible (and the caller couldn't have been
arsed to check what the calling conventions are) or a sign of a function in
bad need of saner calling conventions.
In this case it's the former - kthread_run() never returns a NULL and
actually if you look into kthread_create() you'll see that it returns
a pointer to new task_struct instance on success and ERR_PTR(-E...) on
failure. NULL is never returned by that thing.
This kind of "defensive" programming only confuses the readers; please,
don't paper over that garbage.
prev parent reply other threads:[~2024-08-23 7:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-23 5:26 [PATCH] aoe: Use IS_ERR_OR_NULL() to clean code zhangjiao2
2024-08-23 7:30 ` Al Viro [this message]
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=20240823073055.GD1049718@ZenIV \
--to=viro@zeniv.linux.org.uk \
--cc=axboe@kernel.dk \
--cc=justin@coraid.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=zhangjiao2@cmss.chinamobile.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;
as well as URLs for NNTP newsgroup(s).