From: Andrew Lunn <andrew@lunn.ch>
To: 何敏红 <heminhong@kylinos.cn>
Cc: courmisch <courmisch@gmail.com>, davem <davem@davemloft.net>,
edumazet <edumazet@google.com>, kuba <kuba@kernel.org>,
pabeni <pabeni@redhat.com>, horms <horms@kernel.org>,
"remi.denis-courmont" <remi.denis-courmont@nokia.com>,
netdev <netdev@vger.kernel.org>,
linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: 回复: Re: [PATCH net v2] phonet: check register_netdevice_notifier() error in phonet_device_init()
Date: Fri, 17 Jul 2026 15:56:58 +0200 [thread overview]
Message-ID: <080f4047-2fb7-47ca-a4b7-9cd0683756d0@lunn.ch> (raw)
In-Reply-To: <3v5ygapsdjgl-3v61zwni6m6e@nsmail8.2--kylin--1>
On Fri, Jul 17, 2026 at 02:55:54PM +0800, 何敏红 wrote:
>
>
> Hi Andrew,
>
> Thanks for the review.
>
> > Think about what happens when phonet_netlink_register() fails.
>
> I checked that path. By the time phonet_netlink_register() runs,
> pernet, the proc entry and the netdevice notifier have all been
> registered successfully. On failure, rtnl_register_many() already
> unwinds any partially registered handlers, and phonet_device_exit()
> then tears down the notifier/pernet/proc that were set up. So this is
> not the same issue as calling unregister_netdevice_notifier() for a
> notifier that never got registered.
int __init phonet_device_init(void)
{
int err = register_pernet_subsys(&phonet_net_ops);
if (err)
return err;
proc_create_net("pnresource", 0, init_net.proc_net, &pn_res_seq_ops,
sizeof(struct seq_net_private));
register_netdevice_notifier(&phonet_device_notifier);
err = phonet_netlink_register();
if (err)
phonet_device_exit();
If we get here, phonet_netlink_register() failed.
What exactly does phonet_netlink_register() do:
int __init phonet_netlink_register(void)
{
return rtnl_register_many(phonet_rtnl_msg_handlers);
}
And what does phonet_device_exit() do?
void phonet_device_exit(void)
{
rtnl_unregister_all(PF_PHONET);
unregister_netdevice_notifier(&phonet_device_notifier);
unregister_pernet_subsys(&phonet_net_ops);
remove_proc_entry("pnresource", init_net.proc_net);
}
So it tries to unregister something which was not registered. That is
generally a bad idea.
The general pattern in the Linux kernel is that on error, you
carefully unwind everything which succeeded so far. Often you do that
at the end, with a series of goto statements and labels.
Calling the "mirror" function on error does not work, since that
function assumes everything went correctly in its peer.
Andrew
parent reply other threads:[~2026-07-17 13:57 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <3v5ygapsdjgl-3v61zwni6m6e@nsmail8.2--kylin--1>]
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=080f4047-2fb7-47ca-a4b7-9cd0683756d0@lunn.ch \
--to=andrew@lunn.ch \
--cc=courmisch@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=heminhong@kylinos.cn \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=remi.denis-courmont@nokia.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