Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yanze Zhang" <zhang.yanze@zte.com.cn>
Cc: linux-rdma@vger.kernel.org
Subject: Re: [PATCH rdma v10 5/7] RDMA/zrdma: Implement device and port query operations
Date: Thu, 03 Dec 2026 13:32:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-247861@kernel.org> (raw)
In-Reply-To: <20261203124928.48716-6-zhang.yanze@zte.com.cn>

Thank you for your contribution! Sashiko AI review found 10 potential
issues to consider:
- [High] RDMA/zrdma: zxdh_get_drvinfo() lacks netdev ref and state
  validation
- [Medium] RDMA/zrdma: per-query global rtnl_lock() contention in
  zxdh_get_drvinfo()
- [Medium] RDMA/zrdma: wrong device caps and resource limits in
  zxdh_query_device()
- [Medium] RDMA/zrdma: zxdh_roce_port_immutable() advertises unsupported
  RoCEv1
- [Medium] RDMA/zrdma: zxdh_query_port() ignores ib_get_eth_speed()
  errors
- [Medium] RDMA/zrdma: zxdh_get_drvinfo() bypasses ethtool_ops locking
  and PM contract
- [Medium] RDMA/zrdma: raw zdev->netdev access without
  ib_device_get_netdev()
- [Low] RDMA/zrdma: zxdh_get_drvinfo() falls back to bond master for
  fw_ver
- [Low] RDMA/zrdma: zxdh_get_dev_fw_str() returns N/A instead of empty
  string
- [Low] RDMA/zrdma: lockless plain read of netdev->mtu in
  zxdh_query_port()
--

commit 0e0d51e910e9f9f58edf3a99788eb26f71ff9a42
Author: Yanze Zhang <zhang.yanze@zte.com.cn>

RDMA/zrdma: Implement device and port query operations

This commit implements the query_device, query_port, query_pkey,
get_link_layer, get_port_immutable, and get_dev_fw_str callbacks for
zrdma, along with RoCE device initialization and ethtool firmware version
extraction.

> diff --git a/drivers/infiniband/hw/zrdma/zrdma_verbs.c b/drivers/infiniband/hw/zrdma/zrdma_verbs.c
> index 84ee7be418139..b5322a8e129bb 100644
> --- a/drivers/infiniband/hw/zrdma/zrdma_verbs.c
> +++ b/drivers/infiniband/hw/zrdma/zrdma_verbs.c
> @@ -7,11 +7,305 @@

[ ... ]

> +static bool zxdh_get_drvinfo(struct zxdh_device *zdev,
> +			     struct ethtool_drvinfo *info)
> +{
> +	struct net_device *slave = NULL, *dev;
> +	struct list_head *iter;
> +	bool ret = true;
> +
> +	memset(info, 0, sizeof(*info));
> +
> +	if (!zdev->netdev)
> +		return false;

[Severity: High]
Can dereferencing zdev->netdev directly without taking a reference or
checking device state cause a use-after-free or invoke get_drvinfo() on an
unregistered or detached netdev?

During ib_unregister_device(), __ib_unregister_device() calls free_netdevs()
before device_del() removes the fw_ver sysfs attribute.  Since fw_ver_show()
does not hold an ib_device reference, a concurrent read can run after the
sole netdev reference from ib_device_set_netdev() has been dropped:

CPU0                              CPU1
__ib_unregister_device()
  free_netdevs(ib_dev)
    netdev_put(netdev)
    ...
    free_netdev(netdev)
                                  fw_ver_show()
                                    ib_get_device_fw_str()
                                      zxdh_get_drvinfo()
                                        zdev->netdev  <-- use-after-free
  device_del(&ib_dev->dev)

Even while a reference exists, zxdh_get_drvinfo() never checks
READ_ONCE(dev->reg_state) == NETREG_REGISTERED under rtnl_lock(), so it can
call get_drvinfo() on an unregistering netdev after ndo_uninit() has torn
down driver state.  It also omits netif_device_present(dev), allowing calls
on a detached netdev or bond slave during reset or hot-unplug.

Pinning the netdev with ib_device_get_netdev(), checking reg_state and
netif_device_present() under rtnl_lock(), and releasing it with dev_put()
before rtnl_unlock() (as ib_get_eth_speed() does) avoids these hazards.

> +
> +	rtnl_lock();

[Severity: Medium]
Does taking rtnl_lock() unconditionally in zxdh_get_drvinfo() introduce
unnecessary global lock contention on every query?

zxdh_get_drvinfo() runs on every ibv_query_device_ex() call, every sysfs
fw_ver read, and once per device during RDMA_NLDEV_CMD_GET netlink dumps.
Each call acquires global rtnl_lock() and runs netdev_run_todo() on unlock,
and ib_enum_all_devs() performs O(N) such cycles while holding
devices_rwsem, which can stall RDMA device registration and unregistration.

Caching the firmware version at initialization and refreshing it only on
reset or bond changes would avoid per-query global RTNL serialization.

> +
> +	/* Only the master of a bond has the slaves in its lower device
> +	 * list. A slave carries the bonding flag as well, but it has no
> +	 * lower devices of its own.
> +	 */
> +	if (netif_is_bond_master(zdev->netdev))
> +		netdev_for_each_lower_dev(zdev->netdev, slave, iter)
> +			break;
> +
> +	dev = slave ? slave : zdev->netdev;

[Severity: Low]
What happens here when zdev->netdev is a bond master with no lower slave
devices attached?

slave remains NULL and dev falls back to the bond master itself.
bond_ethtool_get_drvinfo() then writes BOND_ABI_VERSION ("2") into
info->fw_version, which extract_version() preserves verbatim and
zxdh_get_dev_fw_str() reports as the RDMA firmware version instead of
treating it as unavailable.

> +	if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo)
> +		dev->ethtool_ops->get_drvinfo(dev, info);

[Severity: Medium]
Is it safe to call dev->ethtool_ops->get_drvinfo() directly under only
rtnl_lock() without the locking, runtime PM, and begin/complete guards that
ethtool core enforces?

Because zxdh_get_drvinfo() traverses bond lower devices, dev can be an
arbitrary Ethernet driver enslaved to the bond.  For runtime-PM drivers like
igc, calling get_drvinfo() without pm_runtime_get_sync(dev->dev.parent) and
netif_device_present(dev) can issue live PCI MMIO against a suspended
device.

For ops-locked drivers like bnxt, ethtool_ops run under netdev_lock_ops(dev)
rather than rtnl_lock(), so invoking get_drvinfo() without netdev_lock_ops()
races with netdev_lock-only paths such as bnxt_fw_reset_task().  It also
skips dev->ethtool_ops->begin() and complete().

[ ... ]

> +	/* Only the capabilities that the driver implements are reported.
> +	 * The memory management extensions need the advise_mr operation,
> +	 * the notification of the completion queue needs req_notify_cq and
> +	 * the RNR NAK generation needs the queue pair data path.
> +	 */
> +	props->device_cap_flags =
> +		IB_DEVICE_MEM_WINDOW | IB_DEVICE_SYS_IMAGE_GUID;
> +	props->vendor_id = pcidev->vendor;
> +	props->vendor_part_id = pcidev->device;
> +	props->hw_ver = pcidev->revision;
> +	props->page_size_cap = SZ_4K | SZ_2M | SZ_1G;
> +	props->max_mr_size = MAX_MR_SIZE;
> +	props->max_qp = MAX_QP;
> +	props->max_qp_wr = MAX_QP_WR;
> +	props->max_cq = MAX_CQ;
> +	props->max_cqe = MAX_CQE;
> +	props->max_mr = MAX_MR;
> +	props->max_mw = MAX_MW;
> +	props->max_pd = MAX_PD;
> +	props->max_send_sge = MAX_SEND_SGE;
> +	props->max_recv_sge = MAX_RECV_SGE;
> +	props->max_sge_rd = MAX_SGE_RD;
> +	props->max_qp_rd_atom = MAX_QP_RD_ATOM;
> +	props->max_res_rd_atom = MAX_RES_RD_ATOM;
> +	props->max_qp_init_rd_atom = MAX_QP_INIT_RD_ATOM;
> +	props->max_srq = MAX_SRQ;
> +	props->max_srq_wr = MAX_SRQ_WR;
> +	props->max_srq_sge = MAX_SRQ_SGE;
> +	props->local_ca_ack_delay = LOCAL_CA_ACK_DELAY;
> +	props->hca_core_clock = HCA_CORE_CLOCK;
> +	props->max_wq_type_rq = MAX_WQ_TYPE_RQ;
> +	if (rdma_protocol_roce(ibdev, rdma_start_port(ibdev))) {
> +		props->max_pkeys = ZXDH_PKEY_TBL_SZ;
> +		props->max_ah = MAX_AH;
> +		props->max_mcast_grp = 0;
> +		props->max_mcast_qp_attach = 0;
> +		props->max_total_mcast_qp_attach = 0;
> +	}
> +	props->max_fast_reg_page_list_len = MAX_FAST_REG_PAGE_LIST_LEN;
> +	props->cq_caps.max_cq_moderation_count = MAX_CQ_MODERATION_COUNT;
> +	props->cq_caps.max_cq_moderation_period = MAX_CQ_MODERATION_PERIOD;
> +	props->timestamp_mask = TIMESTAMP_MASK;

[Severity: Medium]
Should zxdh_query_device() avoid advertising unimplemented capabilities and
align its reported resource limits with the actual hardware pools?

Despite the comment above device_cap_flags, this sets IB_DEVICE_MEM_WINDOW
and max_mw even though zxdh_dev_ops implements neither alloc_mw nor
dealloc_mw.  It also reports non-zero cq_caps moderation limits without
modify_cq, non-zero timestamp_mask without timestamp support, and non-zero
max_fast_reg_page_list_len without IB_DEVICE_MEM_MGT_EXTENSIONS (and the
comment attributes IB_DEVICE_MEM_MGT_EXTENSIONS to advise_mr rather than
FRWR alloc_mr / map_mr_sg).

The hardcoded MAX_* macros also diverge from the pools initialized in
zxdh_initialize_hw_rsrc(): max_srq reports MAX_SRQ (0x200) while rf->max_srq
is ZXDH_MAX_SRQ (0x2000), max_ah reports MAX_AH (0x10000) even though entry
0 is reserved in rf->allocated_ahs leaving 0xffff allocatable AHs, and
max_cq reports MAX_CQ (0x3fff) while rf->max_cq is ZXDH_MAX_CQ (0x4000)
with entry 0 not reserved.

[ ... ]

> +static int zxdh_query_port(struct ib_device *ibdev, u32 port,
> +			   struct ib_port_attr *props)
> +{
> +	struct zxdh_device *zdev = to_zdev(ibdev);
> +	struct net_device *netdev = zdev->netdev;

[Severity: Medium]
Can accessing zdev->netdev directly in zxdh_query_port() race with
ib_unregister_device()?

In __ib_unregister_device(), free_netdevs() drops the ib_device_set_netdev()
reference before ib_free_port_attrs() removes the port sysfs attributes.  If
zdev->netdev is a bond master being deleted while a port sysfs read enters
zxdh_query_port(), it can dereference a freed net_device; zrdma also
registers no NETDEV_UNREGISTER notifier.  Pinning the netdev with
ib_device_get_netdev(ibdev, port) and calling dev_put() on exit avoids the
raw pointer lifetime hazard.

> +
> +	props->max_mtu = IB_MTU_4096;
> +	props->active_mtu = ib_mtu_int_to_enum(netdev->mtu - ZXDH_MTU_HEADER_RSV);

[Severity: Low]
Should netdev->mtu be read with READ_ONCE() here?

zxdh_query_port() runs without rtnl_lock(), while __netif_set_mtu() updates
dev->mtu with WRITE_ONCE(), so a plain load creates a KCSAN data race.

[ ... ]

> +	/* The link speed is read from the netdev on every query, so a
> +	 * link change is never reported with a stale value.
> +	 */
> +	if (ib_get_eth_speed(ibdev, port, &props->active_speed,
> +			     &props->active_width))
> +		pr_warn_ratelimited("zrdma: failed to get the port speed\n");

[Severity: Medium]
Should zxdh_query_port() propagate the error from ib_get_eth_speed() instead
of returning 0?

When ib_get_eth_speed() fails with -ENODEV (for example during
ib_unregister_device() after free_netdevs() or while the netdev is
unregistering), props->active_speed and props->active_width remain 0.
Because 0 is not a valid speed or width enum value, sysfs rate_show() turns
the error into -EINVAL via ib_width_enum_to_int(0), and uverbs QUERY_PORT
succeeds while returning invalid zero values to userspace.  Since
ib_get_eth_speed() already defaults unknown ethtool link speeds to
SPEED_1000 and returns 0, a non-zero return indicates an unavailable netdev.

[ ... ]

> +static int zxdh_roce_port_immutable(struct ib_device *ibdev, u32 port_num,
> +				    struct ib_port_immutable *immutable)
> +{

[ ... ]

> +	immutable->core_cap_flags = RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP |
> +				    RDMA_CORE_CAP_PROT_ROCE;

[Severity: Medium]
Does setting RDMA_CORE_CAP_PROT_ROCE advertise unsupported RoCEv1 on this
device?

enum zxdh_rdma_protocol defines only iWARP and RoCEv2, and
RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP alone already satisfies
rdma_protocol_roce().  Adding RDMA_CORE_CAP_PROT_ROCE enables
rdma_protocol_roce_eth_encap(), causing the RDMA core to reserve an extra
default GID slot and populate unsupported RoCEv1 GIDs for every address,
halving effective GID table capacity and exposing them to userspace.

[ ... ]

> +static void zxdh_get_dev_fw_str(struct ib_device *dev, char *str)
> +{
> +	struct zxdh_device *zdev = to_zdev(dev);
> +	struct ethtool_drvinfo info;
> +	char extracted_version[32];
> +
> +	strscpy(str, "N/A", IB_FW_VERSION_NAME_MAX);

[Severity: Low]
Should zxdh_get_dev_fw_str() leave str empty instead of writing "N/A" when
the firmware version is unavailable?

ib_get_device_fw_str() and fill_dev_info() in drivers/infiniband/core/nldev.c
use strlen(fw) == 0 to detect a missing firmware version and omit
RDMA_NLDEV_ATTR_FW_VERSION.  Returning "N/A" causes RDMA netlink to emit
that placeholder as a firmware version string.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203124928.48716-1-zhang.yanze@zte.com.cn?part=5

  reply	other threads:[~2026-12-03 13:32 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 12:49 [PATCH rdma v10 0/7] Add ZTE DingHai Ethernet Protocol Driver for RDMA Yanze Zhang
2026-12-03 12:49 ` [PATCH rdma v10 1/7] RDMA/zrdma: Add build infrastructure and module skeleton Yanze Zhang
2026-12-03 13:19   ` sashiko-bot
2026-12-03 12:49 ` [PATCH rdma v10 2/7] RDMA/zrdma: Add core data structures and ABI definitions Yanze Zhang
2026-12-03 13:21   ` sashiko-bot
2026-12-03 12:49 ` [PATCH rdma v10 3/7] RDMA/zrdma: Add internal headers and hardware register definitions Yanze Zhang
2026-12-03 13:17   ` sashiko-bot
2026-12-03 12:49 ` [PATCH rdma v10 4/7] RDMA/zrdma: Add hardware resource initialization and verbs skeleton Yanze Zhang
2026-12-03 13:20   ` sashiko-bot
2026-12-03 12:49 ` [PATCH rdma v10 5/7] RDMA/zrdma: Implement device and port query operations Yanze Zhang
2026-12-03 13:32   ` sashiko-bot [this message]
2026-12-03 12:49 ` [PATCH rdma v10 6/7] RDMA/zrdma: Add device probe and remove logic Yanze Zhang
2026-12-03 13:28   ` sashiko-bot
2026-12-03 12:49 ` [PATCH rdma v10 7/7] RDMA/zrdma: Implement context, PD, and mmap operations Yanze Zhang
2026-12-03 13:42   ` 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-247861@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zhang.yanze@zte.com.cn \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox