All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Stefan Metzmacher" <metze@samba.org>
Cc: linux-rdma@vger.kernel.org
Subject: Re: [PATCH net-next 1/3] cxgb4: support the enable_rdma devlink param in runtime cmode
Date: Tue, 01 Dec 2026 22:10:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-244320@kernel.org> (raw)
In-Reply-To: <2532e7d175c698b77b7a733641608eed83d07f90.1796076486.git.metze@samba.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] cxgb4: implicit declaration of module_refcount() when
  !CONFIG_MODULE_UNLOAD
- [Medium] cxgb4: race with c4iw_exit_module() in cxgb4_uld_set_rdma()
  without module pin
- [Medium] cxgb4: NULL uld_info.owner when built-in bypasses
  cxgb4_uld_set_rdma() check
- [Medium] cxgb4: struct uld_ctx memory leak across cxgb4_uld_set_rdma()
  toggle cycles
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] cxgb4: resource leak in setup_sge_queues_uld() [1]
--

commit 5d2fd6080d0c69d62f374ea901aaa5147340d6f5
Author: Stefan Metzmacher <metze@samba.org>

cxgb4: support the enable_rdma devlink param in runtime cmode

This commit registers a devlink instance for offload-capable cxgb4 adapters
and implements the generic enable_rdma parameter in runtime mode to attach or
detach the iw_cxgb4 RDMA upper layer driver per adapter.

> diff --git a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_uld.c b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_uld.c
> index 23244cdfc1bc6..f66899b4f9322 100644
> --- a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_uld.c
> +++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_uld.c
> @@ -732,6 +732,8 @@ static void cxgb4_uld_alloc_resources(struct adapter *adap,
>  		return;
>  	if (type == CXGB4_ULD_ISCSIT && is_t4(adap->params.chip))
>  		return;
> +	if (type == CXGB4_ULD_RDMA && adap->rdma_disabled)
> +		return;
>  	ret = cfg_queues_uld(adap, type, p);
>  	if (ret)
>  		goto out;

[Severity: Critical]
This problem wasn't introduced by this patch, but in
cxgb4_uld_alloc_resources() [1], when setup_sge_queues_uld() fails
it jumps to free_queues instead of free_rxq:

drivers/net/ethernet/chelsio/cxgb4/cxgb4_uld.c:setup_sge_queues_uld() {
	...
	ret = alloc_uld_rxqs(adap, rxq_info, lro);
	if (ret)
		return ret;

	/* Tell uP to route control queue completions to rdma rspq */
	if (adap->flags & CXGB4_FULL_INIT_DONE && uld_type == CXGB4_ULD_RDMA) {
		...
		for_each_port(adap, i) {
			...
			ret = t4_set_params(adap, adap->mbox, adap->pf,
					    0, 1, &param, &cmplqid);
		}
	}
	return ret;
}

If t4_set_params() fails after alloc_uld_rxqs() has allocated the hardware
RX queues, jumping to free_queues skips free_sge_queues_uld() and frees
only the software structures via free_queues_uld(), leaking the ULD RX
queues and leaving active hardware DMA rings pointing to freed memory.

> @@ -771,6 +773,78 @@ static void cxgb4_uld_alloc_resources(struct adapter *adap,
>  		 "ULD registration failed for uld type %d\n", type);
>  }
>  
> +/**
> + * cxgb4_uld_set_rdma - detach or attach the RDMA ULD of an adapter
> + * @adap: the adapter
> + * @enable: attach (true) or detach (false) the RDMA ULD
> + *
> + * Detaching has the same effect as unloading iw_cxgb4, but only for
> + * this adapter, the netdevs stay. While detached, loading iw_cxgb4
> + * doesn't attach it to this adapter. If iw_cxgb4 isn't loaded,
> + * attaching only allows it to attach once it's loaded. Used for the
> + * enable_rdma devlink param.
> + *
> + * iw_cxgb4 doesn't support disassociating user contexts, so detaching
> + * waits in ib_unregister_device() until all user space applications
> + * closed the RDMA device, while holding uld_mutex. So detaching is
> + * refused with -EBUSY while iw_cxgb4 is in use, like unloading it. The
> + * module refcount covers the RDMA devices of all adapters, so an
> + * application using the RDMA device of another adapter also makes
> + * detaching fail. An application opening the device right after that
> + * check still makes detaching wait until it's closed.
> + */
> +int cxgb4_uld_set_rdma(struct adapter *adap, bool enable)
> +{
> +	struct cxgb4_uld_list *uld_entry;
> +	int ret = 0;
> +
> +	if (!is_offload(adap))
> +		return -EOPNOTSUPP;
> +
> +	mutex_lock(&uld_mutex);
> +	if (adap->flags & CXGB4_SHUTTING_DOWN) {
> +		ret = -ESHUTDOWN;
> +		goto out;
> +	}
> +	if (adap->rdma_disabled == !enable)
> +		goto out;
> +
> +	if (!enable && adap->uld[CXGB4_ULD_RDMA].handle) {
> +		list_for_each_entry(uld_entry, &uld_list, list_node) {
> +			struct module *owner = uld_entry->uld_info.owner;
> +
> +			if (uld_entry->uld_type == CXGB4_ULD_RDMA &&
> +			    owner && module_refcount(owner) > 0) {
> +				ret = -EBUSY;
> +				goto out;
> +			}
> +		}
> +	}

[Severity: High]
Does calling module_refcount() here break the build when
CONFIG_MODULE_UNLOAD is disabled?

In include/linux/module.h, module_refcount() is only declared under
CONFIG_MODULE_UNLOAD, with no stub for !CONFIG_MODULE_UNLOAD or
!CONFIG_MODULES, and CONFIG_CHELSIO_T4 does not depend on MODULE_UNLOAD.

[Severity: Medium]
What happens when CONFIG_INFINIBAND_CXGB4=y is built into the kernel?

When iw_cxgb4 is built-in, THIS_MODULE is NULL, so owner is NULL and this
check is skipped.  Disabling enable_rdma while user-space RDMA contexts
are open then proceeds into state_change(CXGB4_STATE_DETACH) ->
c4iw_remove() -> ib_unregister_device() and blocks until all applications
close the device while holding uld_mutex and the devlink instance lock.

[Severity: Medium]
Can cxgb4_uld_set_rdma() race with c4iw_exit_module() during iw_cxgb4
module unload because owner is never pinned with try_module_get()?

drivers/infiniband/hw/cxgb4/device.c:c4iw_exit_module() {
	mutex_lock(&dev_mutex);
	list_for_each_entry_safe(ctx, tmp, &uld_ctx_list, entry) {
		if (ctx->dev)
			c4iw_remove(ctx);
		kfree(ctx);
	}
	mutex_unlock(&dev_mutex);
	destroy_workqueue(reg_workq);
	cxgb4_unregister_uld(CXGB4_ULD_RDMA);
}

During delete_module(), try_stop_module() decrements the module refcount
so module_refcount(owner) becomes -1 before c4iw_exit_module() runs, and
c4iw_exit_module() tears down all uld_ctx entries and reg_workq before
cxgb4_unregister_uld() acquires uld_mutex:

CPU0                                CPU1
delete_module()
  try_stop_module()
    /* module_refcount() is now -1 */
  c4iw_exit_module()
    mutex_lock(&dev_mutex)
                                    cxgb4_uld_set_rdma(adap, false)
                                      mutex_lock(&uld_mutex)
                                      module_refcount(owner) > 0 /* false */
                                      state_change(ctx, CXGB4_STATE_DETACH)
                                        c4iw_remove(ctx)
    c4iw_remove(ctx)
    kfree(ctx)  <-- concurrent c4iw_remove() and use-after-free

Similarly, on the enable path, if cxgb4_uld_set_rdma(adap, true) runs
after c4iw_exit_module() has destroyed reg_workq but before
cxgb4_unregister_uld() acquires uld_mutex, uld_attach() allocates a new
ctx and queues reg_work onto the destroyed workqueue.  Taking a module
reference with try_module_get(owner) under uld_mutex (and releasing it
with module_put() after detaching or attaching) would prevent
c4iw_exit_module() from running concurrently.

> +	adap->rdma_disabled = !enable;
> +
> +	if (!enable) {
> +		if (adap->uld[CXGB4_ULD_RDMA].handle)
> +			adap->uld[CXGB4_ULD_RDMA].state_change(adap->uld[CXGB4_ULD_RDMA].handle,
> +							       CXGB4_STATE_DETACH);
> +		cxgb4_shutdown_uld_adapter(adap, CXGB4_ULD_RDMA);
> +		goto out;
> +	}
> +
> +	list_for_each_entry(uld_entry, &uld_list, list_node) {
> +		if (uld_entry->uld_type != CXGB4_ULD_RDMA)
> +			continue;
> +		cxgb4_uld_alloc_resources(adap, CXGB4_ULD_RDMA,
> +					  &uld_entry->uld_info);
> +		if (!adap->uld[CXGB4_ULD_RDMA].handle) {
> +			adap->rdma_disabled = true;
> +			ret = -EIO;
> +		}
> +	}

[Severity: Medium]
Does toggling enable_rdma off and back on leak a struct uld_ctx on every
cycle?

On disable, c4iw_uld_state_change(CXGB4_STATE_DETACH) destroys ctx->dev
via c4iw_remove(ctx) but leaves ctx allocated on uld_ctx_list, while
cxgb4_shutdown_uld_adapter() frees rxq_info->rspq_id (leaving
ctx->lldi.rxq_ids and ciq_ids dangling) and clears
adap->uld[CXGB4_ULD_RDMA].handle.  On re-enable, cxgb4_uld_alloc_resources()
-> uld_attach() -> c4iw_uld_add() allocates and links a new struct uld_ctx
onto uld_ctx_list without freeing or reusing the detached one.  As noted
in the commit message, avoiding this leak requires the companion iw_cxgb4
change; since this commit already touches drivers/infiniband/hw/cxgb4/device.c,
should that fix be included before or together with this change so bisection
and trees without the separate patch do not leak memory on each toggle?

[1] https://sashiko.dev/#/bug/linux-000bf912-26b7-4dd2-95b7-a98a4da8e793

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796076486.git.metze@samba.org?part=1

  reply	other threads:[~2026-12-01 22:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:09 [PATCH net-next 0/3] devlink: support the enable_rdma param in runtime cmode for cxgb4, mlx5 and ice Stefan Metzmacher
2026-11-30 22:09 ` [PATCH net-next 1/3] cxgb4: support the enable_rdma devlink param in runtime cmode Stefan Metzmacher
2026-12-01 22:10   ` sashiko-bot [this message]
2026-11-30 22:09 ` [PATCH net-next 2/3] net/mlx5: " Stefan Metzmacher
2026-12-01 22:10   ` sashiko-bot
2026-11-30 22:10 ` [PATCH net-next 3/3] ice: " Stefan Metzmacher
2026-12-01 13:36   ` Przemek Kitszel
2026-12-01 22:10   ` sashiko-bot

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=sashiko-outbox-244320@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=metze@samba.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.