Skip to content

Commit c335b49

Browse files
vaibhav92gregkh
authored andcommitted
cxl: Wrap iterations over afu slices inside 'afu_list_lock'
commit edeb304 upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5467498 commit c335b49

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
@@ -1807,7 +1807,7 @@ static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
18071807
/* There should only be one entry, but go through the list
18081808
* anyway
18091809
*/
1810-
if (afu->phb == NULL)
1810+
if (afu == NULL || afu->phb == NULL)
18111811
return result;
18121812

18131813
list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
@@ -1834,7 +1834,8 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18341834
{
18351835
struct cxl *adapter = pci_get_drvdata(pdev);
18361836
struct cxl_afu *afu;
1837-
pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET, afu_result;
1837+
pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1838+
pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
18381839
int i;
18391840

18401841
/* At this point, we could still have an interrupt pending.
@@ -1845,6 +1846,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18451846

18461847
/* If we're permanently dead, give up. */
18471848
if (state == pci_channel_io_perm_failure) {
1849+
spin_lock(&adapter->afu_list_lock);
18481850
for (i = 0; i < adapter->slices; i++) {
18491851
afu = adapter->afu[i];
18501852
/*
@@ -1853,6 +1855,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
18531855
*/
18541856
cxl_vphb_error_detected(afu, state);
18551857
}
1858+
spin_unlock(&adapter->afu_list_lock);
18561859
return PCI_ERS_RESULT_DISCONNECT;
18571860
}
18581861

@@ -1934,11 +1937,17 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
19341937
* * In slot_reset, free the old resources and allocate new ones.
19351938
* * In resume, clear the flag to allow things to start.
19361939
*/
1940+
1941+
/* Make sure no one else changes the afu list */
1942+
spin_lock(&adapter->afu_list_lock);
1943+
19371944
for (i = 0; i < adapter->slices; i++) {
19381945
afu = adapter->afu[i];
19391946

1940-
afu_result = cxl_vphb_error_detected(afu, state);
1947+
if (afu == NULL)
1948+
continue;
19411949

1950+
afu_result = cxl_vphb_error_detected(afu, state);
19421951
cxl_context_detach_all(afu);
19431952
cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
19441953
pci_deconfigure_afu(afu);
@@ -1950,6 +1959,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
19501959
(result == PCI_ERS_RESULT_NEED_RESET))
19511960
result = PCI_ERS_RESULT_NONE;
19521961
}
1962+
spin_unlock(&adapter->afu_list_lock);
19531963

19541964
/* should take the context lock here */
19551965
if (cxl_adapter_context_lock(adapter) != 0)
@@ -1982,14 +1992,18 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
19821992
*/
19831993
cxl_adapter_context_unlock(adapter);
19841994

1995+
spin_lock(&adapter->afu_list_lock);
19851996
for (i = 0; i < adapter->slices; i++) {
19861997
afu = adapter->afu[i];
19871998

1999+
if (afu == NULL)
2000+
continue;
2001+
19882002
if (pci_configure_afu(afu, adapter, pdev))
1989-
goto err;
2003+
goto err_unlock;
19902004

19912005
if (cxl_afu_select_best_mode(afu))
1992-
goto err;
2006+
goto err_unlock;
19932007

19942008
if (afu->phb == NULL)
19952009
continue;
@@ -2001,16 +2015,16 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
20012015
ctx = cxl_get_context(afu_dev);
20022016

20032017
if (ctx && cxl_release_context(ctx))
2004-
goto err;
2018+
goto err_unlock;
20052019

20062020
ctx = cxl_dev_context_init(afu_dev);
20072021
if (IS_ERR(ctx))
2008-
goto err;
2022+
goto err_unlock;
20092023

20102024
afu_dev->dev.archdata.cxl_ctx = ctx;
20112025

20122026
if (cxl_ops->afu_check_and_enable(afu))
2013-
goto err;
2027+
goto err_unlock;
20142028

20152029
afu_dev->error_state = pci_channel_io_normal;
20162030

@@ -2031,8 +2045,13 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
20312045
result = PCI_ERS_RESULT_DISCONNECT;
20322046
}
20332047
}
2048+
2049+
spin_unlock(&adapter->afu_list_lock);
20342050
return result;
20352051

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

2059-
if (afu->phb == NULL)
2079+
if (afu == NULL || afu->phb == NULL)
20602080
continue;
20612081

20622082
list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
@@ -2065,6 +2085,7 @@ static void cxl_pci_resume(struct pci_dev *pdev)
20652085
afu_dev->driver->err_handler->resume(afu_dev);
20662086
}
20672087
}
2088+
spin_unlock(&adapter->afu_list_lock);
20682089
}
20692090

20702091
static const struct pci_error_handlers cxl_err_handler = {

0 commit comments

Comments
 (0)