Skip to content

Commit 5467498

Browse files
mjruhlgregkh
authored andcommitted
IB/hfi1: Close race condition on user context disable and close
commit bc5add0 upstream. When disabling and removing a receive context, it is possible for an asynchronous event (i.e IRQ) to occur. Because of this, there is a race between cleaning up the context, and the context being used by the asynchronous event. cpu 0 (context cleanup) rc->ref_count-- (ref_count == 0) hfi1_rcd_free() cpu 1 (IRQ (with rcd index)) rcd_get_by_index() lock ref_count+++ <-- reference count race (WARNING) return rcd unlock cpu 0 hfi1_free_ctxtdata() <-- incorrect free location lock remove rcd from array unlock free rcd This race will cause the following WARNING trace: WARNING: CPU: 0 PID: 175027 at include/linux/kref.h:52 hfi1_rcd_get_by_index+0x84/0xa0 [hfi1] CPU: 0 PID: 175027 Comm: IMB-MPI1 Kdump: loaded Tainted: G OE ------------ 3.10.0-957.el7.x86_64 raspberrypi#1 Hardware name: Intel Corporation S2600KP/S2600KP, BIOS SE5C610.86B.11.01.0076.C4.111920150602 11/19/2015 Call Trace: dump_stack+0x19/0x1b __warn+0xd8/0x100 warn_slowpath_null+0x1d/0x20 hfi1_rcd_get_by_index+0x84/0xa0 [hfi1] is_rcv_urgent_int+0x24/0x90 [hfi1] general_interrupt+0x1b6/0x210 [hfi1] __handle_irq_event_percpu+0x44/0x1c0 handle_irq_event_percpu+0x32/0x80 handle_irq_event+0x3c/0x60 handle_edge_irq+0x7f/0x150 handle_irq+0xe4/0x1a0 do_IRQ+0x4d/0xf0 common_interrupt+0x162/0x162 The race can also lead to a use after free which could be similar to: general protection fault: 0000 1 SMP CPU: 71 PID: 177147 Comm: IMB-MPI1 Kdump: loaded Tainted: G W OE ------------ 3.10.0-957.el7.x86_64 raspberrypi#1 Hardware name: Intel Corporation S2600KP/S2600KP, BIOS SE5C610.86B.11.01.0076.C4.111920150602 11/19/2015 task: ffff9962a8098000 ti: ffff99717a508000 task.ti: ffff99717a508000 __kmalloc+0x94/0x230 Call Trace: ? hfi1_user_sdma_process_request+0x9c8/0x1250 [hfi1] hfi1_user_sdma_process_request+0x9c8/0x1250 [hfi1] hfi1_aio_write+0xba/0x110 [hfi1] do_sync_readv_writev+0x7b/0xd0 do_readv_writev+0xce/0x260 ? handle_mm_fault+0x39d/0x9b0 ? pick_next_task_fair+0x5f/0x1b0 ? sched_clock_cpu+0x85/0xc0 ? __schedule+0x13a/0x890 vfs_writev+0x35/0x60 SyS_writev+0x7f/0x110 system_call_fastpath+0x22/0x27 Use the appropriate kref API to verify access. Reorder context cleanup to ensure context removal before cleanup occurs correctly. Cc: stable@vger.kernel.org # v4.14.0+ Fixes: f683c80 ("IB/hfi1: Resolve kernel panics by reference counting receive contexts") Reviewed-by: Mike Marciniszyn <mike.marciniszyn@intel.com> Signed-off-by: Michael J. Ruhl <michael.j.ruhl@intel.com> Signed-off-by: Dennis Dalessandro <dennis.dalessandro@intel.com> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 09bc2f5 commit 5467498

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎drivers/infiniband/hw/hfi1/hfi.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
14251425
struct hfi1_devdata *dd, u8 hw_pidx, u8 port);
14261426
void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd);
14271427
int hfi1_rcd_put(struct hfi1_ctxtdata *rcd);
1428-
void hfi1_rcd_get(struct hfi1_ctxtdata *rcd);
1428+
int hfi1_rcd_get(struct hfi1_ctxtdata *rcd);
14291429
struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd,
14301430
u16 ctxt);
14311431
struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt);

‎drivers/infiniband/hw/hfi1/init.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ static void hfi1_rcd_free(struct kref *kref)
213213
struct hfi1_ctxtdata *rcd =
214214
container_of(kref, struct hfi1_ctxtdata, kref);
215215

216-
hfi1_free_ctxtdata(rcd->dd, rcd);
217-
218216
spin_lock_irqsave(&rcd->dd->uctxt_lock, flags);
219217
rcd->dd->rcd[rcd->ctxt] = NULL;
220218
spin_unlock_irqrestore(&rcd->dd->uctxt_lock, flags);
221219

220+
hfi1_free_ctxtdata(rcd->dd, rcd);
221+
222222
kfree(rcd);
223223
}
224224

@@ -241,10 +241,13 @@ int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
241241
* @rcd: pointer to an initialized rcd data structure
242242
*
243243
* Use this to get a reference after the init.
244+
*
245+
* Return : reflect kref_get_unless_zero(), which returns non-zero on
246+
* increment, otherwise 0.
244247
*/
245-
void hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
248+
int hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
246249
{
247-
kref_get(&rcd->kref);
250+
return kref_get_unless_zero(&rcd->kref);
248251
}
249252

250253
/**
@@ -324,7 +327,8 @@ struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt)
324327
spin_lock_irqsave(&dd->uctxt_lock, flags);
325328
if (dd->rcd[ctxt]) {
326329
rcd = dd->rcd[ctxt];
327-
hfi1_rcd_get(rcd);
330+
if (!hfi1_rcd_get(rcd))
331+
rcd = NULL;
328332
}
329333
spin_unlock_irqrestore(&dd->uctxt_lock, flags);
330334

0 commit comments

Comments
 (0)