Skip to content

Commit edeb304

Browse files
vaibhav92mpe
authored andcommitted
cxl: Wrap iterations over afu slices inside 'afu_list_lock'
Within cxl module, iteration over array 'adapter->afu' may be racy at few points as it might be simultaneously read during an EEH and its contents being set to NULL while driver is being unloaded or unbound from the adapter. This might result in a NULL pointer to 'struct afu' being de-referenced during an EEH thereby causing a kernel oops. This patch fixes this by making sure that all access to the array 'adapter->afu' is wrapped within the context of spin-lock 'adapter->afu_list_lock'. Fixes: 9e8df8a ("cxl: EEH support") Cc: stable@vger.kernel.org # v4.3+ Acked-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com> Acked-by: Frederic Barrat <fbarrat@linux.ibm.com> Acked-by: Christophe Lombard <clombard@linux.vnet.ibm.com> Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 9bf3d3c commit edeb304

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎drivers/misc/cxl/guest.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ static int guest_reset(struct cxl *adapter)
267267
int i, rc;
268268

269269
pr_devel("Adapter reset request\n");
270+
spin_lock(&adapter->afu_list_lock);
270271
for (i = 0; i < adapter->slices; i++) {
271272
if ((afu = adapter->afu[i])) {
272273
pci_error_handlers(afu, CXL_ERROR_DETECTED_EVENT,
@@ -283,6 +284,7 @@ static int guest_reset(struct cxl *adapter)
283284
pci_error_handlers(afu, CXL_RESUME_EVENT, 0);
284285
}
285286
}
287+
spin_unlock(&adapter->afu_list_lock);
286288
return rc;
287289
}
288290

‎drivers/misc/cxl/pci.c‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
18051805
/* There should only be one entry, but go through the list
18061806
* anyway
18071807
*/
1808-
if (afu->phb == NULL)
1808+
if (afu == NULL || afu->phb == NULL)
18091809
return result;
18101810

18111811
list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
@@ -1832,7 +1832,8 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18321832
{
18331833
struct cxl *adapter = pci_get_drvdata(pdev);
18341834
struct cxl_afu *afu;
1835-
pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET, afu_result;
1835+
pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1836+
pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
18361837
int i;
18371838

18381839
/* At this point, we could still have an interrupt pending.
@@ -1843,6 +1844,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18431844

18441845
/* If we're permanently dead, give up. */
18451846
if (state == pci_channel_io_perm_failure) {
1847+
spin_lock(&adapter->afu_list_lock);
18461848
for (i = 0; i < adapter->slices; i++) {
18471849
afu = adapter->afu[i];
18481850
/*
@@ -1851,6 +1853,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18511853
*/
18521854
cxl_vphb_error_detected(afu, state);
18531855
}
1856+
spin_unlock(&adapter->afu_list_lock);
18541857
return PCI_ERS_RESULT_DISCONNECT;
18551858
}
18561859

@@ -1932,11 +1935,17 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
19321935
* * In slot_reset, free the old resources and allocate new ones.
19331936
* * In resume, clear the flag to allow things to start.
19341937
*/
1938+
1939+
/* Make sure no one else changes the afu list */
1940+
spin_lock(&adapter->afu_list_lock);
1941+
19351942
for (i = 0; i < adapter->slices; i++) {
19361943
afu = adapter->afu[i];
19371944

1938-
afu_result = cxl_vphb_error_detected(afu, state);
1945+
if (afu == NULL)
1946+
continue;
19391947

1948+
afu_result = cxl_vphb_error_detected(afu, state);
19401949
cxl_context_detach_all(afu);
19411950
cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
19421951
pci_deconfigure_afu(afu);
@@ -1948,6 +1957,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
19481957
(result == PCI_ERS_RESULT_NEED_RESET))
19491958
result = PCI_ERS_RESULT_NONE;
19501959
}
1960+
spin_unlock(&adapter->afu_list_lock);
19511961

19521962
/* should take the context lock here */
19531963
if (cxl_adapter_context_lock(adapter) != 0)
@@ -1980,14 +1990,18 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
19801990
*/
19811991
cxl_adapter_context_unlock(adapter);
19821992

1993+
spin_lock(&adapter->afu_list_lock);
19831994
for (i = 0; i < adapter->slices; i++) {
19841995
afu = adapter->afu[i];
19851996

1997+
if (afu == NULL)
1998+
continue;
1999+
19862000
if (pci_configure_afu(afu, adapter, pdev))
1987-
goto err;
2001+
goto err_unlock;
19882002

19892003
if (cxl_afu_select_best_mode(afu))
1990-
goto err;
2004+
goto err_unlock;
19912005

19922006
if (afu->phb == NULL)
19932007
continue;
@@ -1999,16 +2013,16 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
19992013
ctx = cxl_get_context(afu_dev);
20002014

20012015
if (ctx && cxl_release_context(ctx))
2002-
goto err;
2016+
goto err_unlock;
20032017

20042018
ctx = cxl_dev_context_init(afu_dev);
20052019
if (IS_ERR(ctx))
2006-
goto err;
2020+
goto err_unlock;
20072021

20082022
afu_dev->dev.archdata.cxl_ctx = ctx;
20092023

20102024
if (cxl_ops->afu_check_and_enable(afu))
2011-
goto err;
2025+
goto err_unlock;
20122026

20132027
afu_dev->error_state = pci_channel_io_normal;
20142028

@@ -2029,8 +2043,13 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
20292043
result = PCI_ERS_RESULT_DISCONNECT;
20302044
}
20312045
}
2046+
2047+
spin_unlock(&adapter->afu_list_lock);
20322048
return result;
20332049

2050+
err_unlock:
2051+
spin_unlock(&adapter->afu_list_lock);
2052+
20342053
err:
20352054
/* All the bits that happen in both error_detected and cxl_remove
20362055
* should be idempotent, so we don't need to worry about leaving a mix
@@ -2051,10 +2070,11 @@ static void cxl_pci_resume(struct pci_dev *pdev)
20512070
* This is not the place to be checking if everything came back up
20522071
* properly, because there's no return value: do that in slot_reset.
20532072
*/
2073+
spin_lock(&adapter->afu_list_lock);
20542074
for (i = 0; i < adapter->slices; i++) {
20552075
afu = adapter->afu[i];
20562076

2057-
if (afu->phb == NULL)
2077+
if (afu == NULL || afu->phb == NULL)
20582078
continue;
20592079

20602080
list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
@@ -2063,6 +2083,7 @@ static void cxl_pci_resume(struct pci_dev *pdev)
20632083
afu_dev->driver->err_handler->resume(afu_dev);
20642084
}
20652085
}
2086+
spin_unlock(&adapter->afu_list_lock);
20662087
}
20672088

20682089
static const struct pci_error_handlers cxl_err_handler = {

0 commit comments

Comments
 (0)