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. 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 rather than returning a value that would make rds_trans_get_preferred() fall back to another transport. rds_ib_exit() flushes rds_wq after unregistering the transport, 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 | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/net/rds/ib.c b/net/rds/ib.c index 4ea9838d0..624374ecf 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() runs it while rdma_cm's handler_mutex is held + * (the passive connection path) or while rds_trans_get_preferred() holds + * rds_trans_sem, and rdma_destroy_id() takes handler_mutex again. Queue + * the destruction on rds_wq, which runs without either lock. + */ +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; } @@ -542,6 +584,12 @@ void rds_ib_exit(void) rds_ib_sysctl_exit(); rds_ib_recv_exit(); rds_trans_unregister(&rds_ib_transport); + + /* + * 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(); }