Register a netlink notifier to detect when the NBD management process closes its netlink socket (on exit or crash). Without this, in-flight I/O waits for socket timeout while udevd holds disk->open_mutex for partition scanning, blocking poweroff's sync_bdevs() and causing a hung task during shutdown. Fix this by tracking the management process's netlink portid and, on NETLINK_URELEASE, calling sock_shutdown() on all devices it owns. This fails in-flight I/O immediately so shutdown can proceed. Signed-off-by: Yun Zhou --- drivers/block/nbd.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 43aa4121f0c5..3bf88f7b3535 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -45,6 +45,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -131,6 +132,7 @@ struct nbd_device { unsigned long flags; pid_t pid; /* pid of nbd-client, if attached */ + u32 owner_portid; /* netlink portid of management process */ char *backend; }; @@ -2331,12 +2333,19 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) } set_bit(NBD_RT_HAS_BACKEND_FILE, &config->runtime_flags); + /* + * Set owner before starting device so that the netlink notifier + * can clean up if the management process exits during setup. + */ + nbd->owner_portid = info->snd_portid; ret = nbd_start_device(nbd); out: if (!ret) { set_bit(NBD_RT_HAS_CONFIG_REF, &config->runtime_flags); refcount_inc(&nbd->config_refs); nbd_connect_reply(info, nbd->index); + } else { + nbd->owner_portid = 0; } mutex_unlock(&nbd->config_lock); @@ -2541,6 +2550,12 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) } } out: + /* + * Update owner to the reconfiguring process so the notifier + * tracks the correct management process. + */ + if (!ret) + nbd->owner_portid = info->snd_portid; mutex_unlock(&nbd->config_lock); nbd_config_put(nbd); nbd_put(nbd); @@ -2743,6 +2758,48 @@ static void nbd_dead_link_work(struct work_struct *work) kfree(args); } +/* + * Detect when the management process (the one that issued NBD_CMD_CONNECT) + * closes its netlink socket (e.g., exit or crash). When this happens, + * no one can reconnect, so mark all its devices as disconnected to stop + * waiting for reconnect and fail pending I/O promptly. + */ +static int nbd_netlink_event(struct notifier_block *this, + unsigned long event, void *ptr) +{ + struct netlink_notify *n = ptr; + struct nbd_device *nbd; + int id; + + if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) + return NOTIFY_DONE; + + mutex_lock(&nbd_index_mutex); + idr_for_each_entry(&nbd_index_idr, nbd, id) { + if (nbd->owner_portid != n->portid) + continue; + + nbd->owner_portid = 0; + if (refcount_inc_not_zero(&nbd->config_refs)) { + /* + * Shut down all sockets. sock_shutdown() holds + * tx_lock for each socket, preventing races with + * reconnect, and sets NBD_RT_DISCONNECTED so that + * nbd_reconnect_possible() fails immediately. + */ + sock_shutdown(nbd); + nbd_config_put(nbd); + } + } + mutex_unlock(&nbd_index_mutex); + + return NOTIFY_DONE; +} + +static struct notifier_block nbd_netlink_notifier = { + .notifier_call = nbd_netlink_event, +}; + static int __init nbd_init(void) { int i; @@ -2789,6 +2846,7 @@ static int __init nbd_init(void) unregister_blkdev(NBD_MAJOR, "nbd"); return -EINVAL; } + netlink_register_notifier(&nbd_netlink_notifier); nbd_dbg_init(); for (i = 0; i < nbds_max; i++) @@ -2818,6 +2876,7 @@ static void __exit nbd_cleanup(void) * for the completion of netlink commands. */ genl_unregister_family(&nbd_genl_family); + netlink_unregister_notifier(&nbd_netlink_notifier); nbd_dbg_close(); -- 2.43.0