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. Reviewed-by: Ido Schimmel Signed-off-by: Petr Machata --- drivers/net/ethernet/mellanox/mlxsw/pci.c | 31 ++++++++--------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlxsw/pci.c b/drivers/net/ethernet/mellanox/mlxsw/pci.c index dceb69945d26..b2a7f568c74a 100644 --- a/drivers/net/ethernet/mellanox/mlxsw/pci.c +++ b/drivers/net/ethernet/mellanox/mlxsw/pci.c @@ -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) { -- 2.56.1 In his report (see the link below), Myeonghun Pak 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. Cc: Myeonghun Pak Cc: Ijae Kim Cc: Jiale Yao Reported-by: Myeonghun Pak Link: https://lore.kernel.org/netdev/20260721125753.35944-1-mhun512@gmail.com/ Reported-by: Jiale Yao Link: https://lore.kernel.org/netdev/20261018040653.534066-1-yaojiale02@163.com/ Reviewed-by: Ido Schimmel Signed-off-by: Petr Machata --- drivers/net/ethernet/mellanox/mlxsw/pci.c | 46 ++++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlxsw/pci.c b/drivers/net/ethernet/mellanox/mlxsw/pci.c index b2a7f568c74a..dc6cad5637d6 100644 --- a/drivers/net/ethernet/mellanox/mlxsw/pci.c +++ b/drivers/net/ethernet/mellanox/mlxsw/pci.c @@ -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); -- 2.56.1