rds_rdma_init() calls rds_ib_init(), which registers rds_ib_transport, and only then creates the RDMA CM listener. If the listener setup fails, rds_ib_exit() unregisters the transport again. Between the two calls the transport is visible to rds_trans_get_preferred() while the module is still being initialized, and try_module_get() succeeds for a module in MODULE_STATE_COMING. A bind() that races with a failed initialization can then run ->laddr_check() after do_init_module() freed the module. Register the transport only once the listener is up, so that it is never reachable from a module that can still fail to initialize, and unregister it in rds_rdma_exit() before the transport is torn down. Signed-off-by: Aibing Chen --- net/rds/ib.c | 3 --- net/rds/rdma_transport.c | 7 ++++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/net/rds/ib.c b/net/rds/ib.c index 4ea9838d0..784d358fe 100644 --- a/net/rds/ib.c +++ b/net/rds/ib.c @@ -541,7 +541,6 @@ void rds_ib_exit(void) rds_ib_destroy_nodev_conns(); rds_ib_sysctl_exit(); rds_ib_recv_exit(); - rds_trans_unregister(&rds_ib_transport); rds_ib_mr_exit(); } @@ -605,8 +604,6 @@ int rds_ib_init(void) if (ret) goto out_sysctl; - rds_trans_register(&rds_ib_transport); - rds_info_register_func(RDS_INFO_IB_CONNECTIONS, rds_ib_ic_info); #if IS_ENABLED(CONFIG_IPV6) rds_info_register_func(RDS6_INFO_IB_CONNECTIONS, rds6_ib_ic_info); diff --git a/net/rds/rdma_transport.c b/net/rds/rdma_transport.c index 75532734c..690b8a211 100644 --- a/net/rds/rdma_transport.c +++ b/net/rds/rdma_transport.c @@ -344,8 +344,12 @@ static int __init rds_rdma_init(void) goto out; ret = rds_rdma_listen_init(); - if (ret) + if (ret) { rds_ib_exit(); + goto out; + } + + rds_trans_register(&rds_ib_transport); out: return ret; } @@ -355,6 +359,7 @@ static void __exit rds_rdma_exit(void) { /* stop listening first to ensure no new connections are attempted */ rds_rdma_listen_stop(); + rds_trans_unregister(&rds_ib_transport); rds_ib_exit(); } module_exit(rds_rdma_exit); rds_trans_get_preferred() calls ->laddr_check() while holding rds_trans_sem for reading. The IB transport's laddr_check() creates a temporary rdma_cm id and takes rdma_cm's handler_mutex, while the CM callback path takes handler_mutex before entering RDS through rds_conn_create() -> rds_trans_get_preferred() -> down_read(rds_trans_sem), so lockdep reports a circular locking dependency between the semaphore and the handler mutex class. All handler_mutex instances share one lock class and the probe id's mutex is private to it, so this is a lock class conflict rather than a single deadlock, but rds_trans_sem should not be held across a call that reaches into another subsystem's locking. Take the module reference for the candidate transport under rds_trans_sem, drop the semaphore, and only then call ->laddr_check(). The module reference keeps the transport and its callback alive, and rds_trans_sem is no longer held when handler_mutex is taken. A transport that fails its check is put and the scan continues. The transport is only registered once rds_rdma_init() can no longer fail, so the reference cannot be taken on a module that is about to be freed. Reported-by: syzbot+ccdb5058c3df2d94811c@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=ccdb5058c3df2d94811c Suggested-by: Allison Henderson Link: https://lore.kernel.org/all/d83709308a038467b3a4849d814978b84328d2bc.camel@kernel.org/ Fixes: ec16227e1414 ("RDS/IB: Infiniband transport") Cc: stable@vger.kernel.org Signed-off-by: Aibing Chen --- net/rds/transport.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/net/rds/transport.c b/net/rds/transport.c index f8001ec80..4e667580f 100644 --- a/net/rds/transport.c +++ b/net/rds/transport.c @@ -86,7 +86,6 @@ struct rds_transport *rds_trans_get_preferred(struct net *net, const struct in6_addr *addr, __u32 scope_id) { - struct rds_transport *ret = NULL; struct rds_transport *trans; unsigned int i; @@ -101,15 +100,26 @@ struct rds_transport *rds_trans_get_preferred(struct net *net, for (i = 0; i < RDS_TRANS_COUNT; i++) { trans = transports[i]; - if (trans && (trans->laddr_check(net, addr, scope_id) == 0) && - (!trans->t_owner || try_module_get(trans->t_owner))) { - ret = trans; - break; - } + if (!trans || + (trans->t_owner && !try_module_get(trans->t_owner))) + continue; + + /* + * ->laddr_check() may create an rdma_cm id and take + * rdma_cm's handler_mutex. Don't hold rds_trans_sem + * across it: the module reference keeps trans alive. + */ + up_read(&rds_trans_sem); + + if (trans->laddr_check(net, addr, scope_id) == 0) + return trans; + + rds_trans_put(trans); + down_read(&rds_trans_sem); } up_read(&rds_trans_sem); - return ret; + return NULL; } struct rds_transport *rds_trans_get(int t_type) rds_ib_laddr_check() is called from rds_trans_get_preferred(), which runs both on the bind path and from the passive connection path, where rdma_cm's handler_mutex is already held: cma_ib_req_handler() rds_ib_cm_handle_connect() rds_conn_create() rds_trans_get_preferred() rds_ib_laddr_check() rdma_destroy_id() mutex_lock(&id_priv->handler_mutex) rdma_destroy_id() takes the handler mutex of the probe id, which is a different instance of the same lock class, so lockdep reports a recursive locking dependency as soon as a connection is established. Destroy the probe id from rds_wq instead, so that the destroy runs without either rds_trans_sem or handler_mutex held. The work item is allocated before rdma_create_id(), so an allocation failure fails the address check instead of falling back to the synchronous destroy, and the number of pending destroys is bounded by RDS_IB_MAX_PROBE_PENDING (64). Above the bound the check fails with -ENOMEM; callers treat that like any other unsuccessful laddr_check(). rds_ib_exit() flushes rds_wq before the transport is torn down, so a queued destroy cannot run after the module text is gone. Fixes: ec16227e1414 ("RDS/IB: Infiniband transport") Cc: stable@vger.kernel.org Signed-off-by: Aibing Chen --- net/rds/ib.c | 53 +++++++++++++++++++++++++++++++++++++++++++-- net/rds/transport.c | 7 +++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/net/rds/ib.c b/net/rds/ib.c index 784d358fe..a3ee52fa9 100644 --- a/net/rds/ib.c +++ b/net/rds/ib.c @@ -391,6 +391,31 @@ static void rds6_ib_ic_info(struct socket *sock, unsigned int len, } #endif +/* + * The probe cm_id must not be destroyed from the caller's context: + * rds_ib_laddr_check() may run while rdma_cm's handler_mutex is held (the + * passive connection path), and rdma_destroy_id() takes the handler_mutex + * of the probe id, which is a different instance of the same lock class. + * Queue the destruction on rds_wq, which runs without either that mutex + * or rds_trans_sem. + */ +struct rds_ib_probe_work { + struct work_struct work; + struct rdma_cm_id *cm_id; +}; + +static atomic_t rds_ib_probe_pending = ATOMIC_INIT(0); +#define RDS_IB_MAX_PROBE_PENDING 64 + +static void rds_ib_probe_work_fn(struct work_struct *work) +{ + struct rds_ib_probe_work *pw = + container_of(work, struct rds_ib_probe_work, work); + + rdma_destroy_id(pw->cm_id); + kfree(pw); + atomic_dec(&rds_ib_probe_pending); +} /* * Early RDS/IB was built to only bind to an address if there is an IPoIB * device with that address set. @@ -406,6 +431,7 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr, { int ret; struct rdma_cm_id *cm_id; + struct rds_ib_probe_work *pw; #if IS_ENABLED(CONFIG_IPV6) struct sockaddr_in6 sin6; #endif @@ -414,11 +440,25 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr, bool isv4; isv4 = ipv6_addr_v4mapped(addr); + + pw = kzalloc_obj(*pw); + if (!pw) + return -ENOMEM; + + if (atomic_inc_return(&rds_ib_probe_pending) > RDS_IB_MAX_PROBE_PENDING) { + atomic_dec(&rds_ib_probe_pending); + kfree(pw); + return -ENOMEM; + } + /* Create a CMA ID restricted to IB devices and try to bind it. */ cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler, NULL, RDMA_PS_TCP, IB_QPT_RC); - if (IS_ERR(cm_id)) + if (IS_ERR(cm_id)) { + kfree(pw); + atomic_dec(&rds_ib_probe_pending); return PTR_ERR(cm_id); + } ret = rdma_restrict_node_type(cm_id, RDMA_NODE_IB_CA); if (ret) goto out; @@ -484,7 +524,9 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr, cm_id->device ? cm_id->device->node_type : -1); out: - rdma_destroy_id(cm_id); + INIT_WORK(&pw->work, rds_ib_probe_work_fn); + pw->cm_id = cm_id; + queue_work(rds_wq, &pw->work); return ret; } @@ -541,6 +583,13 @@ void rds_ib_exit(void) rds_ib_destroy_nodev_conns(); rds_ib_sysctl_exit(); rds_ib_recv_exit(); + + /* + * rds_ib_laddr_check_cm() may have queued a probe destroy on rds_wq; + * wait for it before the module text goes away. + */ + flush_workqueue(rds_wq); + rds_ib_mr_exit(); } diff --git a/net/rds/transport.c b/net/rds/transport.c index 4e667580f..cb37d9b08 100644 --- a/net/rds/transport.c +++ b/net/rds/transport.c @@ -105,9 +105,10 @@ struct rds_transport *rds_trans_get_preferred(struct net *net, continue; /* - * ->laddr_check() may create an rdma_cm id and take - * rdma_cm's handler_mutex. Don't hold rds_trans_sem - * across it: the module reference keeps trans alive. + * ->laddr_check() creates an rdma_cm id and may reach into + * rdma_cm's locking. Its destruction is deferred to rds_wq, + * but don't hold rds_trans_sem across the check itself: the + * module reference keeps trans alive. */ up_read(&rds_trans_sem);