mlxsw: Fix teardown order of CQ NAPI instances
Subsystems (3)
Petr Machata
maintainercover mlxsw: Fix teardown order of CQ NAPI instances
There are the following principal objects for processing of hardware
events: event queue (EQ), completion queues (CQ), and receive and send queues (RDQ, SDQ). RDQ and SDQ are then configured with a CQ to which they report completion, and CQ is configured with an EQ, to which it reports completion events. The order of configuration is therefore EQ, then CQs, then RDQs and SDQs.
Further, there are NAPI contexts for processing CQ events and working with either RDQ, or SDQ, a tasklet for processing EQ and scheduling per-CQ NAPI contexts, and an IRQ handler that schedules the tasklet. The order of configuration thus must be, first the NAPI contexts (associated with CQs), then the tasklet (associated with the EQ), then the IRQ handler.
The code however initializes the queue together with the scheduled entity that processes that queue, in order of the queue configuration. Thus is the tasklet initialized prior to NAPI contexts, even though it refers to these NAPIs. This is actually a problem during destruction, when the tasklet is destroyed after the queues that it refers to, and SDQ and RDQ are destroyed prior to the respective NAPI context that they are processed by.
IRQ handler is then set up as first, and torn down as last, which is correct.
In this patchset address the ordering issue as follows.
- Patch #1 folds the current tasklet processing directly into the IRQ handler. This is possible, because the amount of work is relatively small and strictly bounded. The heavy lifting is done in NAPI queues scheduled from this code, and the indirection through tasklet does not bring much. The reason to actually do it is because it simplifies the whole situation, and because the tasklet interface is deprecated (see include/linux/interrupt.h).
- In patch #2, per-queue start and stop callbacks are introduced. These are invoked after the queues themselves are initialized (and, respectively, before they are deinitialized), and are used to set up the NAPI context after all relevant resources are ready, and to tear them down first before tearing down the resources they consume.
Petr Machata (2):
mlxsw: pci: Process EQ in the IRQ handler instead of a tasklet
mlxsw: pci: Fix teardown order of CQ NAPI instances
drivers/net/ethernet/mellanox/mlxsw/pci.c | 77 ++++++++++++++++------- 1 file changed, 55 insertions(+), 22 deletions(-)
Signature · 1 line
2.56.1
Petr Machata
maintainerPATCH 1/2 mlxsw: pci: Process EQ in the IRQ handler instead of a tasklet
mlxsw_pci_eq_irq_handler() schedules a tasklet, which then reads the EQ and schedules NAPI instances for the CQs that have pending events. The actual completion processing itself is done by NAPI.
The EQ processing is bounded by half of the EQ size (see credits in mlxsw_pci_eq_tasklet()), and all it does is to ring two doorbells and call napi_schedule(), both of which are safe to do from a hard IRQ context. The EQ is only re-armed at the end, so the handler cannot be re-entered.
The tasklet therefore does not really serve a useful purpose: neither structurally, nor with regards to permissible runtime operations, nor with regards to amount of time spent in IRQ. It is just an extra deferral layer.
Drop the tasklet and do the work directly in the IRQ handler. Change napi_schedule() to napi_schedule_irqoff() to leverage the fact that interrupts are already masked.
--- drivers/net/ethernet/mellanox/mlxsw/pci.c | 31 ++++++++--------------- 1 file changed, 11 insertions(+), 20 deletions(-)
drivers/net/ethernet/mellanox/mlxsw/pci.c +11 -20
@@ -88,9 +88,6 @@ struct mlxsw_pci_queue {struct napi_struct napi;struct page_pool *page_pool; } cq; -struct { -struct tasklet_struct tasklet; - } eq;struct {struct mlxsw_pci_queue *cq; } rdq;
@@ -1111,16 +1108,19 @@ static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)return elem; } -static void mlxsw_pci_eq_tasklet (struct tasklet_struct *t) +static irqreturn_t mlxsw_pci_eq_irq_handler (int irq,void *dev_id) {unsigned long active_cqns[BITS_TO_LONGS (MLXSW_PCI_CQS_MAX )]; -struct mlxsw_pci_queue *q =from_tasklet (q, t, u.eq.tasklet); -struct mlxsw_pci *mlxsw_pci = q->pci; -int credits = q->count >>1 ; +struct mlxsw_pci *mlxsw_pci = dev_id; +struct mlxsw_pci_queue *q;u8 cqn, cq_count;int items =0 ; +int credits;char *eqe; + q =mlxsw_pci_eq_get (mlxsw_pci); + credits = q->count >>1 ; +memset (&active_cqns,0 ,sizeof (active_cqns));while ((eqe =mlxsw_pci_eq_sw_eqe_get (q))) {
@@ -1132,7 +1132,7 @@ static void mlxsw_pci_eq_tasklet(struct tasklet_struct *t) }if (!items) -return ; +return IRQ_HANDLED ;mlxsw_pci_queue_doorbell_consumer_ring (mlxsw_pci, q);mlxsw_pci_queue_doorbell_arm_consumer_ring (mlxsw_pci, q);
@@ -1140,8 +1140,10 @@ static void mlxsw_pci_eq_tasklet(struct tasklet_struct *t) cq_count = mlxsw_pci->num_cqs;for_each_set_bit (cqn, active_cqns, cq_count) { q =mlxsw_pci_cq_get (mlxsw_pci, cqn); -napi_schedule (&q->u.cq.napi); +napi_schedule_irqoff (&q->u.cq.napi); } + +return IRQ_HANDLED ; } static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
@@ -1176,7 +1178,6 @@ static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox, err =mlxsw_cmd_sw2hw_eq (mlxsw_pci->core, mbox, q->num);if (err)return err; -tasklet_setup (&q->u.eq.tasklet, mlxsw_pci_eq_tasklet);mlxsw_pci_queue_doorbell_consumer_ring (mlxsw_pci, q);mlxsw_pci_queue_doorbell_arm_consumer_ring (mlxsw_pci, q);return 0 ;
@@ -1730,16 +1731,6 @@ static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)kfree (mlxsw_pci->fw_area.items); } -static irqreturn_t mlxsw_pci_eq_irq_handler (int irq,void *dev_id) -{ -struct mlxsw_pci *mlxsw_pci = dev_id; -struct mlxsw_pci_queue *q; - - q =mlxsw_pci_eq_get (mlxsw_pci); -tasklet_schedule (&q->u.eq.tasklet); -return IRQ_HANDLED ; -} -static int mlxsw_pci_mbox_alloc (struct mlxsw_pci *mlxsw_pci,struct mlxsw_pci_mem_item *mbox) {
Signature · 1 line
2.56.1
Petr Machata
maintainerPATCH 2/2 mlxsw: pci: Fix teardown order of CQ NAPI instances
In reply to Petr MachataIn his report (see the link below), Myeonghun Pak <[email protected]>
Quoted 10 lines · writes:
mlxsw_pci_eq_irq_handler()schedules the EQ tasklet. The tasklet reads the EQ ring and schedules CQ NAPI instances. The CQ poll callbacks, in turn, dereference the RDQ or SDQ associated with the CQ.
mlxsw_pci_fini()unregisters the IRQ and immediately tears down the asynchronous queues in RDQ, SDQ, CQ, EQ order.free_irq()waits for IRQ handlers, but not for a tasklet already scheduled by one. In addition,mlxsw_pci_cq_fini()disables each CQ NAPI only after all RDQs and SDQs have been freed. A pending tasklet or NAPI poll can therefore access freed queue storage.
The previous patch folded the tasklet into the IRQ handler, which takes care of the first half of the problem: free_irq() in mlxsw_pci_fini() now guarantees that nothing schedules CQ NAPI instances anymore. The second half remains. A NAPI poll that was scheduled right before free_irq(), or is currently running, can still dereference the RDQ or SDQ of its CQ, which mlxsw_pci_aqs_fini() frees before the CQ, and where napi_disable() is currently called.
In this patch, introduce a process-related duals to init / fini per-queue
callbacks: start and stop. Only CQ needs them for now, to manage the NAPI instances. Invoke them at the end of mlxsw_pci_aqs_init() and at the beginning of mlxsw_pci_aqs_fini(), respectively, so that NAPI is enabled only once all queues are set up, and disabled before any of them is destroyed. This also keeps napi_enable() and napi_disable() paired on error paths of mlxsw_pci_aqs_init().
In his report, Myeonghun Pak notes that the issue was originally discovered by static analysis.
--- drivers/net/ethernet/mellanox/mlxsw/pci.c | 46 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlxsw/pci.c +44 -2
@@ -1059,7 +1059,6 @@ static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,if (err) goto err_page_pool_init; -napi_enable (&q->u.cq.napi);mlxsw_pci_queue_doorbell_consumer_ring (mlxsw_pci, q);mlxsw_pci_queue_doorbell_arm_consumer_ring (mlxsw_pci, q);return 0 ;
@@ -1074,12 +1073,23 @@ static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci, {enum mlxsw_pci_cq_type cq_type =mlxsw_pci_cq_type (mlxsw_pci, q); -napi_disable (&q->u.cq.napi);mlxsw_pci_cq_page_pool_fini (q, cq_type);mlxsw_pci_cq_napi_teardown (q);mlxsw_cmd_hw2sw_cq (mlxsw_pci->core, q->num); } +static void mlxsw_pci_cq_start (struct mlxsw_pci *mlxsw_pci, +struct mlxsw_pci_queue *q) +{ +napi_enable (&q->u.cq.napi); +} + +static void mlxsw_pci_cq_stop (struct mlxsw_pci *mlxsw_pci, +struct mlxsw_pci_queue *q) +{ +napi_disable (&q->u.cq.napi); +} +static u16 mlxsw_pci_cq_elem_count (const struct mlxsw_pci_queue *q) { return q->u.cq.v == MLXSW_PCI_CQE_V2 ? MLXSW_PCI_CQE2_COUNT :
@@ -1198,6 +1208,10 @@ struct mlxsw_pci_queue_ops { struct mlxsw_pci_queue *q);void (*fini)(struct mlxsw_pci *mlxsw_pci,struct mlxsw_pci_queue *q); +void (*start)(struct mlxsw_pci *mlxsw_pci, +struct mlxsw_pci_queue *q); +void (*stop)(struct mlxsw_pci *mlxsw_pci, +struct mlxsw_pci_queue *q);u16 (*elem_count_f)(const struct mlxsw_pci_queue *q);u8 (*elem_size_f)(const struct mlxsw_pci_queue *q);u16 elem_count;
@@ -1225,6 +1239,8 @@ static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = { .pre_init = mlxsw_pci_cq_pre_init, .init = mlxsw_pci_cq_init, .fini = mlxsw_pci_cq_fini, + .start = mlxsw_pci_cq_start, + .stop = mlxsw_pci_cq_stop, .elem_count_f = mlxsw_pci_cq_elem_count, .elem_size_f = mlxsw_pci_cq_elem_size };
@@ -1349,6 +1365,28 @@ static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,kfree (queue_group->q); } +static void mlxsw_pci_queue_group_start (struct mlxsw_pci *mlxsw_pci, +const struct mlxsw_pci_queue_ops *q_ops) +{ +struct mlxsw_pci_queue_type_group *queue_group; +int i; + + queue_group =mlxsw_pci_queue_type_group_get (mlxsw_pci, q_ops->type); +for (i =0 ; i < queue_group->count; i++) + q_ops->start (mlxsw_pci, &queue_group->q[i]); +} + +static void mlxsw_pci_queue_group_stop (struct mlxsw_pci *mlxsw_pci, +const struct mlxsw_pci_queue_ops *q_ops) +{ +struct mlxsw_pci_queue_type_group *queue_group; +int i; + + queue_group =mlxsw_pci_queue_type_group_get (mlxsw_pci, q_ops->type); +for (i =0 ; i < queue_group->count; i++) + q_ops->stop (mlxsw_pci, &queue_group->q[i]); +} +static int mlxsw_pci_aqs_init (struct mlxsw_pci *mlxsw_pci,char *mbox) {struct pci_dev *pdev = mlxsw_pci->pdev;
@@ -1426,6 +1464,8 @@ static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox) goto err_rdqs_init; } +mlxsw_pci_queue_group_start (mlxsw_pci, &mlxsw_pci_cq_ops); +return 0 ; err_rdqs_init:
@@ -1439,6 +1479,8 @@ static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)static void mlxsw_pci_aqs_fini (struct mlxsw_pci *mlxsw_pci) { +mlxsw_pci_queue_group_stop (mlxsw_pci, &mlxsw_pci_cq_ops); +mlxsw_pci_queue_group_fini (mlxsw_pci, &mlxsw_pci_rdq_ops);mlxsw_pci_queue_group_fini (mlxsw_pci, &mlxsw_pci_sdq_ops);mlxsw_pci_queue_group_fini (mlxsw_pci, &mlxsw_pci_cq_ops);
Signature · 1 line
2.56.1
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Petr Machata
maintainerQuoted 6 lines · [email protected] writes:
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
In Myeonghun Pak's original report, linked from patch #2 and available online, the issue is claimed to have been discovered by static analysis. A second report that came later does not declare the discovery method, but given the circumstances, it is almost certainly not organic either.
We have not attempted to trigger the issue. When pointed out as such, the code is obviously wrong.
Quoted 3 lines
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
The patches were tested across supported Spectrum machines as part of our internal regression for about a week.
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Mon, 16 Nov 2026 11:16:45 +0100 you wrote:
There are the following principal objects for processing of hardware
events: event queue (EQ), completion queues (CQ), and receive and send queues (RDQ, SDQ). RDQ and SDQ are then configured with a CQ to which they report completion, and CQ is configured with an EQ, to which it reports completion events. The order of configuration is therefore EQ, then CQs, then RDQs and SDQs.[...]
Here is the summary with links:
- [net-next,1/2] mlxsw: pci: Process EQ in the IRQ handler instead of a tasklet
https://git.kernel.org/netdev/net-next/c/a467c628a7cb - [net-next,2/2] mlxsw: pci: Fix teardown order of CQ NAPI instances https://git.kernel.org/netdev/net-next/c/b494a6d7da32
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html