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