Re: 回复: Re: [PATCH net v2] phonet: checkregister_netdevice_notifier() error in phonet_device_init()
From: Andrew Lunn <andrew@lunn.ch>
Date: 2026-07-17 13:57:16
Also in:
lkml
On Fri, Jul 17, 2026 at 02:55:54PM +0800, 何敏红 wrote:
Hi Andrew, Thanks for the review.quoted
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