__ib_alloc_cq() sets comp_handler before it initializes the poll context the handler schedules, and for the workqueue contexts it arms the CQ before setting comp_wq. The CQ is reachable from the completion interrupt once ops.create_cq() returns, so a completion event raised in that window would schedule an uninitialized irq_poll or work, or queue work on a NULL workqueue. A conforming device raises no event for a new, empty CQ, but nothing in the core relies on that. Initialize the poll context first, then set comp_handler, and arm the CQ only after that. Fixes: 14d3a3b2498e ("IB: add a proper completion queue abstraction") Signed-off-by: Yishai Hadas --- drivers/infiniband/core/cq.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/infiniband/core/cq.c b/drivers/infiniband/core/cq.c index 12304c9a9403..42e4d272f5ad 100644 --- a/drivers/infiniband/core/cq.c +++ b/drivers/infiniband/core/cq.c @@ -217,6 +217,7 @@ struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, int nr_cqe, .cqe = nr_cqe, .comp_vector = comp_vector, }; + ib_comp_handler comp_handler; struct ib_cq *cq; int ret = -ENOMEM; @@ -248,19 +249,16 @@ struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, int nr_cqe, switch (cq->poll_ctx) { case IB_POLL_DIRECT: - cq->comp_handler = ib_cq_completion_direct; + comp_handler = ib_cq_completion_direct; break; case IB_POLL_SOFTIRQ: - cq->comp_handler = ib_cq_completion_softirq; - + comp_handler = ib_cq_completion_softirq; irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler); - ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); break; case IB_POLL_WORKQUEUE: case IB_POLL_UNBOUND_WORKQUEUE: - cq->comp_handler = ib_cq_completion_workqueue; + comp_handler = ib_cq_completion_workqueue; INIT_WORK(&cq->work, ib_cq_poll_work); - ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); cq->comp_wq = (cq->poll_ctx == IB_POLL_WORKQUEUE) ? ib_comp_wq : ib_comp_unbound_wq; break; @@ -269,6 +267,15 @@ struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, int nr_cqe, goto out_destroy_cq; } + /* + * The CQ is reachable from the completion interrupt once created. Set + * the handler only after the poll context it schedules is set up, and + * arm the CQ only after that. + */ + cq->comp_handler = comp_handler; + if (cq->poll_ctx != IB_POLL_DIRECT) + ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); + rdma_restrack_add(&cq->res); trace_cq_alloc(cq, nr_cqe, comp_vector, poll_ctx); return cq; -- 2.18.1