Thanks for the review. Most of this is addressed in v7, where this patch
is 1/2 (patch 1 of v6 has been applied upstream); one item is not taken
and explained below.
> [Severity: Medium]
> This asks for exactly 78 MSI-X vectors (min_vecs == max_vecs). It doesn't
> check the function's MSI-X table size with pci_msix_vec_count(), and there
> is no smaller fallback layout. If the call fails, zxdh_pf_probe() aborts.
>
> The same probe path is also bound to the VF device ID:
>
> { PCI_DEVICE(ZXDH_PF_VENDOR_ID, ZXDH_VF_DEVICE_ID) },
>
> Will a VF, or any function with fewer than 78 MSI-X table entries, now fail
> to probe? This commit only uses the 8 async vectors, but it requires all 78.
>
> The commit message calls this "the fixed MSI-X vector layout of the
> device". Could it also say whether VFs expose the same table size, and that
> probe now fails without the full set?
A VF will not fail: the layout is 78 vectors for a PF and 20 for a VF, and
the driver now selects it from the device ID. The VF device ID is matched
explicitly because pci_dev::is_virtfn is only set when the PF created the
VFs in the same kernel, so it is not set in a guest that received the VF
through passthrough.
There is no smaller fallback layout. The vector layout is part of the device
interface: a function that cannot provide the vectors the layout requires is
not one this driver supports. The driver now reads the table size with
pci_msix_vec_count() before requesting the vectors, and fails probe with
"device provides N MSI-X vectors, M required" instead of the generic
allocation error.
The whole layout is requested at probe because it is partitioned into pools;
only the async pool is populated in this series, the vq pool comes with the
netdev series.
The commit message now states the layout for both function types.
> [Severity: Low]
> Probe now requires MSI-X. But DINGHAI_PF in
> drivers/net/ethernet/zte/dinghai/Kconfig only gets depends on PCI from its
> parent DINGHAI:
>
> config DINGHAI_PF
> tristate "ZTE DingHai PF (Physical Function) driver"
>
> With CONFIG_PCI=y and CONFIG_PCI_MSI=n, the pci_alloc_irq_vectors_affinity()
> stub returns -ENOSPC unless PCI_IRQ_INTX is set and min_vecs is 1. Neither
> holds here, so the driver builds but can never bind.
>
> Should DINGHAI_PF depend on PCI_MSI, as other MSI-X only ethernet drivers
> do (amazon, google, huawei/hinic3, intel/idpf and others)?
Yes. The driver is MSI-X only, and DINGHAI_PF now depends on PCI_MSI.
> [Severity: Medium]
> The pool code in this file looks closely derived from mlx5's
> drivers/net/ethernet/mellanox/mlx5/core/irq_affinity.c. That file carries:
>
> // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
> /* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
>
> The functions match up as follows:
>
> zxdh_cpu_get()/zxdh_cpu_put() -> cpu_get()/cpu_put()
> zxdh_cpu_get_least_loaded() -> cpu_get_least_loaded()
> zxdh_irq_pool_request_irq() -> irq_pool_request_irq()
> zxdh_irq_pool_find_least_loaded() -> irq_pool_find_least_loaded()
> zxdh_irq_affinity_request() -> mlx5_irq_affinity_request()
>
> The control flow is the same, apart from the renames:
>
> - the early break on a CPU with no IRQs
> - the cpumask_first(cpu_online_mask) fallback
> - the cpumask_weight() > 1 split
> - the goto out/unlock structure and the "sharing" fallback
> - the overload message that divides by the refs per IRQ constant
>
> The comment in zxdh_irq_release() about clearing the affinity hint before
> free_irq() is a rewording of the one in irq_release() in mlx5's pci_irq.c..
>
> Should this file keep the NVIDIA copyright notice, and should the commit
> message mention where the code comes from? Picking the GPL-2.0 side of the
> dual license is fine, but the original copyright line is dropped here.
Both are addressed: the NVIDIA copyright notice is kept, and the file and
the commit message state that the code is derived from mlx5's
irq_affinity.c. The license stays GPL-2.0-only; only the notice and the
attribution were missing.
> [Severity: Medium]
> Can a firmware event get lost here? request_irq() unmasks the MSI-X entry
> while irq->nh is still empty, and the only consumer is attached later.
>
> At the end of the series (after "dinghai: add async event queue for
> firmware notifications"), the order is:
>
> zxdh_pf_probe()
> zxdh_pf_wait_riscv_ready() /* firmware already up */
> zxdh_pf_irq_table_init() /* MSI-X enabled, entries masked */
> zxdh_pf_eq_table_init()
> zxdh_pf_async_irq_request()
> zxdh_get_irq_of_pool()
> zxdh_irq_affinity_request()
> zxdh_irq_pool_request_irq()
> zxdh_irq_alloc()
> request_irq() /* unmasked, chain empty */
> zxdh_irq_attach_nb() /* notifier registered here */
>
> A message raised while the entry was masked is latched in the PBA and sent
> when the entry is unmasked. If that happens before zxdh_irq_attach_nb(),
> zxdh_irq_int_handler() walks an empty chain and returns IRQ_HANDLED.
> Nothing re-reads the BAR message channel after the notifier is attached.
>
> Would using IRQF_NO_AUTOEN and calling enable_irq() after the notifier is
> attached avoid this? Re-checking the event source after registration would
> also work.
>
> Whether this happens depends on firmware signalling async vector 0 before
> the driver's vector handshake, which a later series is said to add. No
> subscriber registers on eq_table.nh within this series yet.
This is the one item not taken in this version, and the reasoning at the end
of your comment is also where we ended up: the firmware is not told which
vector to signal until the BAR message channel handshake arrives in a later
series, so no notification is expected on this vector before then, and
enabling the vector late would not change any behavior today.
When that series adds the first consumer we will request the vector with
IRQF_NO_AUTOEN and enable it after the notifier is attached, which closes
the window you describe.
> [Severity: Low]
> This isn't a bug, but can the affinity == NULL branch ever run? The only
> caller, zxdh_irq_pool_request_irq(), always passes req_mask or auto_mask.
> Before that, it already dereferences req_mask in cpumask_weight() and
> cpumask_first(). zxdh_get_irq_of_pool() always passes a copy of
> cpu_online_mask.
>
> The comment suggests NULL is a valid input, but a NULL mask would crash
> before reaching this branch. Could the branch be removed?
Removed, together with the comment about the no-preference case. The only
caller always passes a mask.
> [Severity: Low]
> This isn't a bug, but the comment says this only finds the least loaded
> CPU. It also increments irqs_per_cpu[best_cpu].
>
> zxdh_irq_pool_request_irq() depends on that increment. It calls
> zxdh_cpu_get() only in the single CPU branch, and undoes the count with
> zxdh_cpu_put() on error. If someone followed the comment and added a
> zxdh_cpu_get() to the multi CPU branch, the CPU would be counted twice and
> the WARN_ON() in zxdh_irq_pool_free() would fire.
>
> The lock in struct zxdh_irq_pool in zxdh_irq.h has a similar problem. It is
> documented as:
>
> struct mutex lock; /* serializes IRQ creation */
>
> The code relies on it for more than creation:
>
> - zxdh_irq_release() and zxdh_irq_pool_find_least_loaded() both assert it
> - the "cannot fail" zxdh_irq_get() in zxdh_irq_affinity_request() is only
> safe because release is serialized by it
> - it covers all irqs_per_cpu updates
>
> Could both comments describe what the code relies on?
Both comments now describe what the code does. zxdh_cpu_get_least_loaded()
is documented as picking the least loaded online CPU and accounting one more
IRQ on it, and the pool lock is documented as protecting every field of the
pool, which is what the asserts and the "cannot fail" comment rely on.
> [Severity: Medium]
> Does the header comment describe the opposite of what the code does? It
> says an existing IRQ is shared "once it passes the pool minimum threshold".
> The commit message says the same:
>
> The pool hands out the least loaded matching IRQ once it passes
> the pool minimum threshold
>
> The code shares an IRQ only while its refcount is below min_threshold. At
> or above the threshold, it creates a new IRQ with
> zxdh_irq_pool_request_irq(). It shares only if that fails because the pool
> is exhausted.
>
> The early return in zxdh_irq_pool_find_least_loaded() and the inner
> comments here match the code, not the header comment or the commit message.
>
> For the async pool, ZXDH_PF_ASYNC_IRQ_MIN_COMP is 0:
>
> #define ZXDH_PF_ASYNC_IRQ_MIN_COMP 0
>
> So pool->min_threshold is 0, and this unsigned comparison can never be true.
> Every async request creates a new vector until all 8 are used. Only then
> does it share, with the "exhausted" warning.
>
> Is that the intended policy? If so, could the header comment and the commit
> message be updated to match?
It is the intended policy: the async pool has a minimum threshold of zero,
so every event queue gets its own vector until the pool is exhausted, and
only then do the queues share. The header comment and the commit message now
say so.
> [Severity: Low]
> This isn't a bug, but the units look mixed up here. Each event queue takes
> two references: one from zxdh_get_irq_of_pool() and one from
> zxdh_irq_attach_nb(). So refcount / ZXDH_EQ_REFS_PER_IRQ is the number of
> queues, not "refs on this irq". The mlx5 version prints "%u EQs on this
> irq".
>
> The comment in zxdh_irq_pool_alloc() has the same mix-up:
>
> /* Thresholds are expressed in queue references, two per IRQ. */
>
> The factor is two per queue, not per IRQ, and the ZXDH_EQ_REFS_PER_IRQ
> name has the same problem.
>
> This warning also prints the int returned by pci_irq_vector() with %u.
> Could it use least_loaded_irq->irqn instead?
I went with the mlx5 wording: the message reports event queues, the constant
is now named ZXDH_REFS_PER_EQ, and the comment says the thresholds are
expressed two references per queue. The warning prints irqn.