Skip to content

Commit

Permalink
cxl/pmem: Fix reference counting for delayed work
Browse files Browse the repository at this point in the history
There is a potential race between queue_work() returning and the
queued-work running that could result in put_device() running before
get_device(). Introduce the cxl_nvdimm_bridge_state_work() helper that
takes the reference unconditionally, but drops it if no new work was
queued, to keep the references balanced.

Fixes: 8fdcb17 ("cxl/pmem: Add initial infrastructure for pmem support")
Cc: <[email protected]>
Reviewed-by: Jonathan Cameron <[email protected]>
Reviewed-by: Ben Widawsky <[email protected]>
Link: https://lore.kernel.org/r/163553734757.2509761.3305231863616785470.stgit@dwillia2-desk3.amr.corp.intel.com
Signed-off-by: Dan Williams <[email protected]>
  • Loading branch information
djbw committed Nov 15, 2021
1 parent fa55b7d commit 08b9e0a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions drivers/cxl/pmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,24 @@ static void cxl_nvb_update_state(struct work_struct *work)
put_device(&cxl_nvb->dev);
}

static void cxl_nvdimm_bridge_state_work(struct cxl_nvdimm_bridge *cxl_nvb)
{
/*
* Take a reference that the workqueue will drop if new work
* gets queued.
*/
get_device(&cxl_nvb->dev);
if (!queue_work(cxl_pmem_wq, &cxl_nvb->state_work))
put_device(&cxl_nvb->dev);
}

static void cxl_nvdimm_bridge_remove(struct device *dev)
{
struct cxl_nvdimm_bridge *cxl_nvb = to_cxl_nvdimm_bridge(dev);

if (cxl_nvb->state == CXL_NVB_ONLINE)
cxl_nvb->state = CXL_NVB_OFFLINE;
if (queue_work(cxl_pmem_wq, &cxl_nvb->state_work))
get_device(&cxl_nvb->dev);
cxl_nvdimm_bridge_state_work(cxl_nvb);
}

static int cxl_nvdimm_bridge_probe(struct device *dev)
Expand All @@ -294,8 +304,7 @@ static int cxl_nvdimm_bridge_probe(struct device *dev)
}

cxl_nvb->state = CXL_NVB_ONLINE;
if (queue_work(cxl_pmem_wq, &cxl_nvb->state_work))
get_device(&cxl_nvb->dev);
cxl_nvdimm_bridge_state_work(cxl_nvb);

return 0;
}
Expand Down

0 comments on commit 08b9e0a

Please sign in to comment.