Skip to contentSkip to conversation

mwifiex: reset card->adapter during device unregister

PATCH Superseded Amitkumar Karwar 4 participants 16 messages Oct 4, 2016 – Oct 10, 2016 Marvell mwifiex wireless driver

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 mwifiex: reset card->adapter during device unregister

From: Xinming Hu <[email protected]>

card->adapter gets initialized during device registration. As it's not cleared, we may end up accessing invalid memory in some corner cases. This patch fixes the problem.

---
v2: Same as v1

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 1 +
 drivers/net/wireless/marvell/mwifiex/sdio.c | 1 +
 2 files changed, 2 insertions(+)
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
index f1eeb73..ba9e068 100644
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
 				pci_disable_msi(pdev);
 	       }
 	}
+	card->adapter = NULL;
 }
 
 /* This function initializes the PCI-E host memory space, WCB rings, etc.
drivers/net/wireless/marvell/mwifiex/sdio.c +1 -0
index 8718950..4cad1c2 100644
@@ -2066,6 +2066,7 @@ mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
 	struct sdio_mmc_card *card = adapter->card;
 
 	if (adapter->card) {
+		card->adapter = NULL;
 		sdio_claim_host(card->func);
 		sdio_disable_func(card->func);
 		sdio_release_host(card->func);
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 2/2 mwifiex: check hw_status in suspend and resume handlers

From: Xinming Hu <[email protected]>

We have observed a kernel crash when system immediately suspends after booting. There is a race between suspend and driver initialization paths.
This patch adds hw_status checks to fix the problem

---
v2: Return failure in suspend/resume handler in this scenario.

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +6 -4
index ba9e068..fa6bf85 100644
@@ -122,9 +122,10 @@ static int mwifiex_pcie_suspend(struct device *dev)
 
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;
 		}
 	} else {
 		pr_err("PCIE device is not specified\n");
@@ -166,9 +167,10 @@ static int mwifiex_pcie_resume(struct device *dev)
 
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;
 		}
 	} else {
 		pr_err("PCIE device is not specified\n");
Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Hi,

Quoted 20 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

We have observed a kernel crash when system immediately suspends after booting. There is a race between suspend and driver initialization paths.
This patch adds hw_status checks to fix the problem

---
v2: Return failure in suspend/resume handler in this scenario.

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index ba9e068..fa6bf85 100644

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 126

Quoted 6 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
@@ -122,9 +122,10 @@ static int mwifiex_pcie_suspend(struct device *dev)
 
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {

Wait, is there no locking on the 'hw_status' field? That is inherently an unsafe race all on its own; you're not guaranteed that this will be read/written atomically. And you also aren't guaranteed that writes to this happen in the order they appear in the code -- in other words, reading this flag doesn't necessarily guarantee that initialization is actually complete (even if that's very likely to be true, given that it's probably just a single-instruction word-access, and any prior HW polling or interrupts likely have done some synchronization and can't be reordered).

This is probably better than nothing, but it's not very good.

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 128

Quoted 3 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -1
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;

So the above cases all mean that the driver hasn't finished loading, right?

!card => can't happen (PCIe probe() would have failed mwifiex_add_card()), but fine to check

!card->adapter => only happens after patch 1; i.e., when tearing down the device and detaching it from the driver

card->adapter->hw_status != MWIFIEX_HW_STATUS_READY => FW is not loaded (i.e., in the process of starting or stopping FW?)

I guess all of those cases make sense to be -EBUSY.

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 131

Quoted 3 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
 		}
 	} else {
 		pr_err("PCIE device is not specified\n");

I was going to complain about this branch (!pdev) returning 0, since that looked asymmetric. But this case will never happen, actually, since to_pci_dev() is just doing struct offset arithmetic on struct device, and struct device is never going to be NULL. It probably makes more sense to just remove this branch entirely, but that's for another patch.

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_resume(struct device *dev) · line 171

Quoted 7 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
@@ -166,9 +167,10 @@ static int mwifiex_pcie_resume(struct device *dev)
 
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {

Same complaint, except if you've screwed up here, you probably already screwed up in suspend().

Brian

Quoted 9 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -1
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;
 		}
 	} else {
 		pr_err("PCIE device is not specified\n");
Signature · 1 line
1.9.1
maintainer
#7 lore raw
In reply to Amitkumar Karwar

Hi,

Quoted 20 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

card->adapter gets initialized during device registration. As it's not cleared, we may end up accessing invalid memory in some corner cases. This patch fixes the problem.

---
v2: Same as v1

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 1 +
 drivers/net/wireless/marvell/mwifiex/sdio.c | 1 +
 2 files changed, 2 insertions(+)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index f1eeb73..ba9e068 100644

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
 				pci_disable_msi(pdev);
 	       }
 	}
+	card->adapter = NULL;

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Brian

Quoted 18 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
 }
 
 /* This function initializes the PCI-E host memory space, WCB rings, etc.
drivers/net/wireless/marvell/mwifiex/sdio.c +1 -0
index 8718950..4cad1c2 100644
@@ -2066,6 +2066,7 @@ mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
 	struct sdio_mmc_card *card = adapter->card;
 
 	if (adapter->card) {
+		card->adapter = NULL;
 		sdio_claim_host(card->func);
 		sdio_disable_func(card->func);
 		sdio_release_host(card->func);
Signature · 1 line
1.9.1
#3 lore raw

RE: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

In reply to Brian Norris

Hi Brian,

Thanks for your thorough review.

Quoted 34 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 2:35 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Hi,

Quoted 23 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

We have observed a kernel crash when system immediately suspends after booting. There is a race between suspend and driver initialization paths.
This patch adds hw_status checks to fix the problem

---
v2: Return failure in suspend/resume handler in this scenario.

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index ba9e068..fa6bf85 100644
@@ -122,9 +122,10 @@ static int mwifiex_pcie_suspend(struct device
 *dev)
 
Quoted 16 lines

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 126

Quoted 5 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {

Wait, is there no locking on the 'hw_status' field? That is inherently an unsafe race all on its own; you're not guaranteed that this will be read/written atomically. And you also aren't guaranteed that writes to this happen in the order they appear in the code -- in other words, reading this flag doesn't necessarily guarantee that initialization is actually complete (even if that's very likely to be true, given that it's probably just a single-instruction word-access, and any prior HW polling or interrupts likely have done some synchronization and can't be reordered).
actually complete

Here is the brief info on how "hw_status" flag is updated.
1) It gets changed incrementally during initialization.

    MWIFIEX_HW_STATUS_INITIALIZING -> MWIFIEX_HW_STATUS_INIT_DONE -> MWIFIEX_HW_STATUS_READY

2) Status will remain READY once driver+firmware is up and running.

3) Below is status during teardown
MWIFIEX_HW_STATUS_READY -> MWIFIEX_HW_STATUS_RESET -> MWIFIEX_HW_STATUS_CLOSING -> MWIFIEX_HW_STATUS_NOT_READY

As the events occur one after another, we don't expect a race and don't need locking here for the flag. Flag status MWIFIEX_HW_STATUS_READY guarantees that initialization is completed.

In worst case scenario, only first system suspend attempt issued immediately after system boot will be aborted with BUSY error. I think, that should be fine.

Let me know if you have any concerns.

Quoted 10 lines

This is probably better than nothing, but it's not very good.

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 128

Quoted 3 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -1
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;

So the above cases all mean that the driver hasn't finished loading, right?

Quoted 2 lines

!card => can't happen (PCIe probe() would have failed mwifiex_add_card()), but fine to check

NULL "card" or "card->adapter" pointers are expected in below cases
1) Driver initialization failed after downloading the firmware. Driver is performing cleanup in init failure path. Now system suspends.
2) Race of teardown + suspend

Quoted 8 lines

!card->adapter => only happens after patch 1; i.e., when tearing down the device and detaching it from the driver

card->adapter->hw_status != MWIFIEX_HW_STATUS_READY => FW is not loaded (i.e., in the process of starting or stopping FW?)

I guess all of those cases make sense to be -EBUSY.

Yes. we can keep -EBUSY for all of the cases.

Quoted 10 lines
Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
 		}
 	} else {

pr_err("PCIE device is not specified\n");

I was going to complain about this branch (!pdev) returning 0, since that looked asymmetric. But this case will never happen, actually, since to_pci_dev() is just doing struct offset arithmetic on struct device, and struct device is never going to be NULL. It probably makes more sense to just remove this branch entirely, but that's for another patch.

Thanks for pointing this out. I will create separate patch for this.

Quoted 15 lines

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_resume(struct device · line 172

Quoted 9 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
@@ -166,9 +167,10 @@ static int mwifiex_pcie_resume(struct device
 *dev)
 
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {

Same complaint, except if you've screwed up here, you probably already screwed up in suspend().

Brian

Quoted 9 lines
Quoted 9 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -1
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;
 		}
 	} else {

pr_err("PCIE device is not specified\n");
--
1.9.1

Regards,
Amitkumar

#8 lore raw
In reply to Brian Norris

Hi Brian,

Quoted 45 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Hi,

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 21 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

card->adapter gets initialized during device registration. As it's not cleared, we may end up accessing invalid memory in some corner cases. This patch fixes the problem.

---
v2: Same as v1

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 1 +
drivers/net/wireless/marvell/mwifiex/sdio.c | 1 +
 2 files changed, 2 insertions(+)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index f1eeb73..ba9e068 100644
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
 				pci_disable_msi(pdev);
 	       }
 	}
+	card->adapter = NULL;

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Quoted 2 lines

Brian

We have a global "add_remove_card_sem" semaphore in our code for synchronizing initialization and teardown threads. Ideally "init + teardown/reboot" should not have a race issue with this logic

Later there was a loophole introduced in this after using async firmware download API. During initialization, firmware downloads asynchronously in a separate thread where might have released the semaphore. I am working on a patch to fix this.

So "card->adapter" doesn't need to have locking. Even if we have two write operations, those two threads can't run simultaneously due to above mentioned logic.

Regards,
Amitkumar

maintainer
#9 lore raw

Hi,

Quoted 35 lines · Amitkumar Karwar
Quoted 28 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Hi,

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 3 lines · Amitkumar Karwar
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
 				pci_disable_msi(pdev);
 	       }
 	}
+	card->adapter = NULL;

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Quoted 2 lines

Brian

We have a global "add_remove_card_sem" semaphore in our code for synchronizing initialization and teardown threads. Ideally "init + teardown/reboot" should not have a race issue with this logic

Quoted 8 lines

Later there was a loophole introduced in this after using async firmware download API. During initialization, firmware downloads asynchronously in a separate thread where might have released the semaphore. I am working on a patch to fix this.

So "card->adapter" doesn't need to have locking. Even if we have two write operations, those two threads can't run simultaneously due to above mentioned logic.

What about writes racing with reads? You have lots of unsynchronized cases that read this, although most of them should be halted by now (e.g., cmd processing). I was looking at suspend() in particular, which I thought you were looking at in this patch series.

Brian

maintainer
#4 lore raw

Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

In reply to Amitkumar Karwar

Hi Amit,

Quoted 53 lines · Amitkumar Karwar
Quoted 32 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 2:35 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Quoted 23 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

We have observed a kernel crash when system immediately suspends after booting. There is a race between suspend and driver initialization paths.
This patch adds hw_status checks to fix the problem

---
v2: Return failure in suspend/resume handler in this scenario.

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index ba9e068..fa6bf85 100644
@@ -122,9 +122,10 @@ static int mwifiex_pcie_suspend(struct device
 *dev)
 
Quoted 16 lines

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 126

Quoted 5 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {

Wait, is there no locking on the 'hw_status' field? That is inherently an unsafe race all on its own; you're not guaranteed that this will be read/written atomically. And you also aren't guaranteed that writes to this happen in the order they appear in the code -- in other words, reading this flag doesn't necessarily guarantee that initialization is actually complete (even if that's very likely to be true, given that it's probably just a single-instruction word-access, and any prior HW polling or interrupts likely have done some synchronization and can't be reordered).
actually complete

Here is the brief info on how "hw_status" flag is updated.
1) It gets changed incrementally during initialization.

    MWIFIEX_HW_STATUS_INITIALIZING -> MWIFIEX_HW_STATUS_INIT_DONE -> MWIFIEX_HW_STATUS_READY
Quoted 8 lines

2) Status will remain READY once driver+firmware is up and running.

3) Below is status during teardown
MWIFIEX_HW_STATUS_READY -> MWIFIEX_HW_STATUS_RESET -> MWIFIEX_HW_STATUS_CLOSING -> MWIFIEX_HW_STATUS_NOT_READY

As the events occur one after another, we don't expect a race and don't need locking here for the flag. Flag status MWIFIEX_HW_STATUS_READY guarantees that initialization is completed.

It seems like, as with patch 1, you're mostly arguing about the writes to this variable. But writes race with reads as well; how do you guarantee that you're not seeing incorrect values of 'hw_status' here in suspend() -- e.g., either old or new values of it, or even partially-written values, if for some reason the compiler decides it can't read/write this all in one go?

Quoted 3 lines

In worst case scenario, only first system suspend attempt issued immediately after system boot will be aborted with BUSY error. I think, that should be fine.

(For the record, my concern about -EBUSY is separate from my concern about the potential race condition.)

Quoted 1 line

Let me know if you have any concerns.

Sorry, I probably didn't completely flesh out my thought here.

I think I was concerned about a failed initialization causing the system to never enter suspend again. So specifically: what happens if (e.g.) the firmware fails to load? AFAICT, the device doesn't actually unbind itself from the driver, so instead, you have a device in limbo that will always return -EBUSY in suspend(), and your system can never again enter suspend. Am I correct? If so, that doesn't sound great.

Quoted 9 lines
Quoted 9 lines

This is probably better than nothing, but it's not very good.

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 128

Quoted 3 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -1
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;

So the above cases all mean that the driver hasn't finished loading, right?

Quoted 15 lines
Quoted 2 lines

!card => can't happen (PCIe probe() would have failed mwifiex_add_card()), but fine to check

NULL "card" or "card->adapter" pointers are expected in below cases
1) Driver initialization failed after downloading the firmware. Driver is performing cleanup in init failure path. Now system suspends.
2) Race of teardown + suspend

Quoted 8 lines

!card->adapter => only happens after patch 1; i.e., when tearing down the device and detaching it from the driver

card->adapter->hw_status != MWIFIEX_HW_STATUS_READY => FW is not loaded (i.e., in the process of starting or stopping FW?)

I guess all of those cases make sense to be -EBUSY.

^^ I'm second-guessing my claim here then.

Quoted 1 line

Yes. we can keep -EBUSY for all of the cases.

Brian

#5 lore raw

RE: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Hi Brian

I think your concern is right, we will update patch to handle the case you mentioned below.

If our firmware is dead or we init failure, we should allow the system to suspend.

We have following hardware status supported in driver. We should return -EBUSY only when hardware status is MWIFIEX_HW_STATUS_INITIALIZING/MWIFIEX_HW_STATUS_INIT_DONE/ MWIFIEX_HW_STATUS_CLOSING.

enum MWIFIEX_HARDWARE_STATUS {
MWIFIEX_HW_STATUS_READY,
MWIFIEX_HW_STATUS_INITIALIZING,
MWIFIEX_HW_STATUS_INIT_DONE,
MWIFIEX_HW_STATUS_RESET,
MWIFIEX_HW_STATUS_CLOSING,
MWIFIEX_HW_STATUS_NOT_READY
};

Amit will help prepare the new patch to handle this.

Regards

Cathy

-----Original Message-----
From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 9:42 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Hi Amit,

Quoted 56 lines · Amitkumar Karwar
Quoted 32 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 2:35 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Quoted 23 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

We have observed a kernel crash when system immediately suspends after booting. There is a race between suspend and driver initialization paths.
This patch adds hw_status checks to fix the problem

---
v2: Return failure in suspend/resume handler in this scenario.

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index ba9e068..fa6bf85 100644
@@ -122,9 +122,10 @@ static int mwifiex_pcie_suspend(struct device
 *dev)
 
Quoted 16 lines

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 126

Quoted 5 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status != MWIFIEX_HW_STATUS_READY) {

Wait, is there no locking on the 'hw_status' field? That is inherently an unsafe race all on its own; you're not guaranteed that this will be read/written atomically. And you also aren't guaranteed that writes to this happen in the order they appear in the code -- in other words, reading this flag doesn't necessarily guarantee that initialization is actually complete (even if that's very likely to be true, given that it's probably just a single-instruction word-access, and any prior HW polling or interrupts likely have done some synchronization and can't be reordered).
actually complete

Here is the brief info on how "hw_status" flag is updated.
1) It gets changed incrementally during initialization.

    MWIFIEX_HW_STATUS_INITIALIZING -> MWIFIEX_HW_STATUS_INIT_DONE -> 

MWIFIEX_HW_STATUS_READY

2) Status will remain READY once driver+firmware is up and running.

Quoted 7 lines

3) Below is status during teardown MWIFIEX_HW_STATUS_READY -> MWIFIEX_HW_STATUS_RESET -> MWIFIEX_HW_STATUS_CLOSING -> MWIFIEX_HW_STATUS_NOT_READY

As the events occur one after another, we don't expect a race and don't need locking here for the flag. Flag status MWIFIEX_HW_STATUS_READY guarantees that initialization is completed.

It seems like, as with patch 1, you're mostly arguing about the writes to this variable. But writes race with reads as well; how do you guarantee that you're not seeing incorrect values of 'hw_status' here in suspend() -- e.g., either old or new values of it, or even partially-written values, if for some reason the compiler decides it can't read/write this all in one go?

Quoted 3 lines

In worst case scenario, only first system suspend attempt issued immediately after system boot will be aborted with BUSY error. I think, that should be fine.

(For the record, my concern about -EBUSY is separate from my concern about the potential race condition.)

Quoted 1 line

Let me know if you have any concerns.

Sorry, I probably didn't completely flesh out my thought here.

I think I was concerned about a failed initialization causing the system to never enter suspend again. So specifically: what happens if (e.g.) the firmware fails to load? AFAICT, the device doesn't actually unbind itself from the driver, so instead, you have a device in limbo that will always return -EBUSY in suspend(), and your system can never again enter suspend. Am I correct? If so, that doesn't sound great.

Quoted 9 lines
Quoted 9 lines

This is probably better than nothing, but it's not very good.

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev) · line 128

Quoted 3 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -1
 			pr_err("Card or adapter structure is not valid\n");
-			return 0;
+			return -EBUSY;

So the above cases all mean that the driver hasn't finished loading, right?

Quoted 16 lines
Quoted 2 lines

!card => can't happen (PCIe probe() would have failed mwifiex_add_card()), but fine to check

NULL "card" or "card->adapter" pointers are expected in below cases
1) Driver initialization failed after downloading the firmware. Driver is performing cleanup in init failure path. Now system suspends.
2) Race of teardown + suspend

Quoted 9 lines

!card->adapter => only happens after patch 1; i.e., when tearing down the device and detaching it from the driver

card->adapter->hw_status != MWIFIEX_HW_STATUS_READY => FW is not card->adapter->loaded
(i.e., in the process of starting or stopping FW?)

I guess all of those cases make sense to be -EBUSY.

^^ I'm second-guessing my claim here then.

Quoted 1 line

Yes. we can keep -EBUSY for all of the cases.

Brian

#10 lore raw
In reply to Brian Norris

Hi Brian,

Quoted 52 lines

From: [email protected] [mailto:linux-wireless- [email protected]] On Behalf Of Brian Norris
Sent: Wednesday, October 05, 2016 10:00 PM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Hi,

Quoted 40 lines · Amitkumar Karwar
Quoted 28 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Hi,

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 3 lines · Amitkumar Karwar
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
 				pci_disable_msi(pdev);
 	       }
 	}
+	card->adapter = NULL;

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Quoted 2 lines

Brian

We have a global "add_remove_card_sem" semaphore in our code for synchronizing initialization and teardown threads. Ideally "init + teardown/reboot" should not have a race issue with this logic

Later there was a loophole introduced in this after using async firmware download API. During initialization, firmware downloads asynchronously in a separate thread where might have released the semaphore. I am working on a patch to fix this.

Quoted 8 lines
Quoted 3 lines

So "card->adapter" doesn't need to have locking. Even if we have two write operations, those two threads can't run simultaneously due to above mentioned logic.

What about writes racing with reads? You have lots of unsynchronized cases that read this, although most of them should be halted by now (e.g., cmd processing). I was looking at suspend() in particular, which I thought you were looking at in this patch series.

Please note that "card->adapter" is used only in pcie.c/sdio.c/usb.c files

Writes won't have race with reads.

1) write 1 --- "card->adapter = adapter;" in mwifiex_register_dev()

	This place is at the beginning of initialization. 
	mwifiex_pcie_probe() -> mwifiex_add_card() -> adapter->if_ops.register_dev()
	There is no chance that "card->adapter" is read anywhere at this point. FW is not yet downloaded

2) write 2 ---- "card->adapter = NULL;" in mwifiex_unregister_dev()

	This place the end of teardown phase.
	Interrupts are disabled and all cleanup is done. We have "card->adapter" NULL checks at entry point of suspend/remove/resume, if they get called after this.

Regards,
Amitkumar

#6 lore raw

RE: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

In reply to Brian Norris

Hi Brain,

Quoted 76 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 10:12 PM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Hi Amit,

Quoted 32 lines · Amitkumar Karwar
Quoted 32 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 2:35 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 2/2] mwifiex: check hw_status in suspend and resume handlers

Quoted 23 lines · Amitkumar Karwar

From: Xinming Hu <[email protected]>

We have observed a kernel crash when system immediately suspends after booting. There is a race between suspend and driver initialization paths.
This patch adds hw_status checks to fix the problem

---
v2: Return failure in suspend/resume handler in this scenario.

---
 drivers/net/wireless/marvell/mwifiex/pcie.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
index ba9e068..fa6bf85 100644
@@ -122,9 +122,10 @@ static int mwifiex_pcie_suspend(struct device
 *dev)
 
Quoted 5 lines
Quoted 5 lines

drivers/net/wireless/marvell/mwifiex/pcie.c static int mwifiex_pcie_suspend(struct device *dev)

Quoted 5 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +2 -1
 	if (pdev) {
 		card = pci_get_drvdata(pdev);
-		if (!card || !card->adapter) {
+		if (!card || !card->adapter ||
+		    card->adapter->hw_status !=

MWIFIEX_HW_STATUS_READY) {

Quoted 19 lines
Quoted 11 lines

Wait, is there no locking on the 'hw_status' field? That is inherently an unsafe race all on its own; you're not guaranteed that this will be read/written atomically. And you also aren't guaranteed that writes to this happen in the order they appear in the code -- in other words, reading this flag doesn't necessarily guarantee that initialization is actually complete (even if that's very likely to be true, given that it's probably just a single-instruction word-access, and any prior HW polling or interrupts likely have done some synchronization and can't be reordered).
actually complete

Here is the brief info on how "hw_status" flag is updated.
1) It gets changed incrementally during initialization.

    MWIFIEX_HW_STATUS_INITIALIZING -> MWIFIEX_HW_STATUS_INIT_DONE ->

MWIFIEX_HW_STATUS_READY

2) Status will remain READY once driver+firmware is up and running.

Quoted 7 lines

3) Below is status during teardown MWIFIEX_HW_STATUS_READY -> MWIFIEX_HW_STATUS_RESET -> MWIFIEX_HW_STATUS_CLOSING -> MWIFIEX_HW_STATUS_NOT_READY

As the events occur one after another, we don't expect a race and don't need locking here for the flag. Flag status MWIFIEX_HW_STATUS_READY guarantees that initialization is completed.

Quoted 6 lines

It seems like, as with patch 1, you're mostly arguing about the writes to this variable. But writes race with reads as well; how do you guarantee that you're not seeing incorrect values of 'hw_status' here in suspend() -- e.g., either old or new values of it, or even partially- written values, if for some reason the compiler decides it can't read/write this all in one go?

Got it. I think, we need to define "hw_status" as atomic variable to resolve the issue. I will create separate patch for this.

Quoted 10 lines
Quoted 4 lines

In worst case scenario, only first system suspend attempt issued immediately after system boot will be aborted with BUSY error. I think, that should be fine.

(For the record, my concern about -EBUSY is separate from my concern about the potential race condition.)

Quoted 1 line

Let me know if you have any concerns.

Quoted 8 lines

Sorry, I probably didn't completely flesh out my thought here.

I think I was concerned about a failed initialization causing the system to never enter suspend again. So specifically: what happens if (e.g.) the firmware fails to load? AFAICT, the device doesn't actually unbind itself from the driver, so instead, you have a device in limbo that will always return -EBUSY in suspend(), and your system can never again enter suspend. Am I correct? If so, that doesn't sound great.

You are right. I will add an extra check for this so that both the cases would be handled
Case 1:
Firmware has gone bad or has failed to initialize. We will return zero here, so that it won't block subsequent suspend attempts.
Case 2:
Init or teardown is in process. We will return -EBUSY here. Next suspend attempt would be successful.

I will submit v3 with these changes shortly.

Regards,
Amitkumar

maintainer Reviewed-by
#11 lore raw
In reply to Amitkumar Karwar

Hi Amit,

Quoted 28 lines · Amitkumar Karwar
Quoted 28 lines

From: [email protected] [mailto:linux-wireless- [email protected]] On Behalf Of Brian Norris
Sent: Wednesday, October 05, 2016 10:00 PM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Quoted 18 lines · Amitkumar Karwar
Quoted 18 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 3 lines · Amitkumar Karwar
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
 				pci_disable_msi(pdev);
 	       }
 	}
+	card->adapter = NULL;

...

Quoted 5 lines
Quoted 4 lines

What about writes racing with reads? You have lots of unsynchronized cases that read this, although most of them should be halted by now (e.g., cmd processing). I was looking at suspend() in particular, which I thought you were looking at in this patch series.

Quoted 8 lines

Please note that "card->adapter" is used only in pcie.c/sdio.c/usb.c files

Writes won't have race with reads.

1) write 1 --- "card->adapter = adapter;" in mwifiex_register_dev()

	This place is at the beginning of initialization. 
	mwifiex_pcie_probe() -> mwifiex_add_card() -> adapter->if_ops.register_dev()
	There is no chance that "card->adapter" is read anywhere at this point. FW is not yet downloaded

Sure.

Quoted 5 lines

2) write 2 ---- "card->adapter = NULL;" in mwifiex_unregister_dev() This place the end of teardown phase.
Interrupts are disabled and all cleanup is done. We have "card->adapter" NULL checks at entry point of suspend/remove/resume, if they get called after this.

I guess the question boils down to: can driver suspend() race with mwifiex_unregister_dev() here, then?

And I guess the answer is "no", because unregistration only happens via

PCIe driver remove() -> mwifiex_remove_card() -> adapter->if_ops.unregister_dev()

and I think the device driver core guarantees that suspend() and remove() won't race.

In that case:

I'll reply to the latest revision too, since it's identical.

Thanks for the explanations.

Regards,
Brian

#12 lore raw
In reply to Amitkumar Karwar
Quoted 67 lines · Amitkumar Karwar

Hi Brian,

Quoted 52 lines

From: [email protected] [mailto:linux-wireless- [email protected]] On Behalf Of Brian Norris
Sent: Wednesday, October 05, 2016 10:00 PM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Hi,

Quoted 40 lines · Amitkumar Karwar
Quoted 21 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Hi,

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 3 lines · Amitkumar Karwar
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
                                pci_disable_msi(pdev);
               }
        }
+       card->adapter = NULL;
Quoted 9 lines

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Brian

We have a global "add_remove_card_sem" semaphore in our code for synchronizing initialization and teardown threads. Ideally "init + teardown/reboot" should not have a race issue with this logic

Later there was a loophole introduced in this after using async firmware download API. During initialization, firmware downloads asynchronously in a separate thread where might have released the semaphore. I am working on a patch to fix this.

Quoted 8 lines
Quoted 3 lines

So "card->adapter" doesn't need to have locking. Even if we have two write operations, those two threads can't run simultaneously due to above mentioned logic.

What about writes racing with reads? You have lots of unsynchronized cases that read this, although most of them should be halted by now (e.g., cmd processing). I was looking at suspend() in particular, which I thought you were looking at in this patch series.

Please note that "card->adapter" is used only in pcie.c/sdio.c/usb.c files

Writes won't have race with reads.

Quoted 8 lines

1) write 1 --- "card->adapter = adapter;" in mwifiex_register_dev()

        This place is at the beginning of initialization.
        mwifiex_pcie_probe() -> mwifiex_add_card() -> adapter->if_ops.register_dev()
        There is no chance that "card->adapter" is read anywhere at this point. FW is not yet downloaded

2) write 2 ---- "card->adapter = NULL;" in mwifiex_unregister_dev()
This place the end of teardown phase.
Interrupts are disabled and all cleanup is done. We have "card->adapter" NULL checks at entry point of suspend/remove/resume, if they get called after this.

How exactly are you preventing ->suspend() from being called *while* you are running mwifiex_unregister_dev()? I.e. user slamming the lid of a laptop and throwing it in their backpack?

Thanks.

Signature · 1 line
Dmitry
maintainer
#13 lore raw

Hi Dmitry,

Quoted 67 lines · Dmitry Torokhov
Quoted 66 lines · Amitkumar Karwar
Quoted 48 lines

From: [email protected] [mailto:linux-wireless- [email protected]] On Behalf Of Brian Norris
Sent: Wednesday, October 05, 2016 10:00 PM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Quoted 38 lines · Amitkumar Karwar
Quoted 19 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 3 lines · Amitkumar Karwar
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter) · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
                                pci_disable_msi(pdev);
               }
        }
+       card->adapter = NULL;
Quoted 9 lines

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Brian

We have a global "add_remove_card_sem" semaphore in our code for synchronizing initialization and teardown threads. Ideally "init + teardown/reboot" should not have a race issue with this logic

Later there was a loophole introduced in this after using async firmware download API. During initialization, firmware downloads asynchronously in a separate thread where might have released the semaphore. I am working on a patch to fix this.

Quoted 8 lines
Quoted 3 lines

So "card->adapter" doesn't need to have locking. Even if we have two write operations, those two threads can't run simultaneously due to above mentioned logic.

What about writes racing with reads? You have lots of unsynchronized cases that read this, although most of them should be halted by now (e.g., cmd processing). I was looking at suspend() in particular, which I thought you were looking at in this patch series.

Please note that "card->adapter" is used only in pcie.c/sdio.c/usb.c files

Writes won't have race with reads.

1) write 1 --- "card->adapter = adapter;" in mwifiex_register_dev()

        This place is at the beginning of initialization.
        mwifiex_pcie_probe() -> mwifiex_add_card() -> adapter->if_ops.register_dev()
        There is no chance that "card->adapter" is read anywhere at this point. FW is not yet downloaded
Quoted 7 lines
Quoted 3 lines

2) write 2 ---- "card->adapter = NULL;" in mwifiex_unregister_dev()
This place the end of teardown phase.
Interrupts are disabled and all cleanup is done. We have "card->adapter" NULL checks at entry point of suspend/remove/resume, if they get called after this.

How exactly are you preventing ->suspend() from being called *while* you are running mwifiex_unregister_dev()? I.e. user slamming the lid of a laptop and throwing it in their backpack?

As I already commented, isn't this primarily [*] called from the driver remove() callback? remove() doesn't race with suspend(), does it?

[*] The other cases are in error handling cases. I guess I should make sure those didn't race too...

Brian

#14 lore raw

Hi Brian,

Quoted 74 lines · Brian Norris

Hi Dmitry,

Quoted 71 lines · Dmitry Torokhov
Quoted 69 lines · Amitkumar Karwar
Quoted 48 lines

From: [email protected] [mailto:linux-wireless- [email protected]] On Behalf Of Brian Norris
Sent: Wednesday, October 05, 2016 10:00 PM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

Quoted 38 lines · Amitkumar Karwar
Quoted 19 lines

From: Brian Norris [mailto:[email protected]]
Sent: Wednesday, October 05, 2016 3:28 AM
To: Amitkumar Karwar
Cc: [email protected]; Cathy Luo; Nishant Sarmukadam; [email protected]; [email protected]; Xinming Hu
Subject: Re: [PATCH v2 1/2] mwifiex: reset card->adapter during device unregister

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct

Quoted 3 lines · Amitkumar Karwar
drivers/net/wireless/marvell/mwifiex/pcie.c +0 -0
@@ -3042,6 +3042,7 @@ static void mwifiex_unregister_dev(struct

mwifiex_adapter *adapter)

drivers/net/wireless/marvell/mwifiex/pcie.c static void mwifiex_unregister_dev(struct · line 3045

Quoted 4 lines
drivers/net/wireless/marvell/mwifiex/pcie.c +1 -0
                                pci_disable_msi(pdev);
               }
        }
+       card->adapter = NULL;
Quoted 9 lines

I think you have a similar problem here as in patch 2; there is no locking to protect fields in struct pcie_service_card or struct sdio_mmc_card below. That problem kind of already exists, except that you only write the value of card->adapter once at registration time, so it's not actually unsafe. But now that you're introducing a second write, you have a problem.

Brian

We have a global "add_remove_card_sem" semaphore in our code for synchronizing initialization and teardown threads. Ideally "init + teardown/reboot" should not have a race issue with this logic

Later there was a loophole introduced in this after using async firmware download API. During initialization, firmware downloads asynchronously in a separate thread where might have released the semaphore. I am working on a patch to fix this.

Quoted 8 lines
Quoted 3 lines

So "card->adapter" doesn't need to have locking. Even if we have two write operations, those two threads can't run simultaneously due to above mentioned logic.

What about writes racing with reads? You have lots of unsynchronized cases that read this, although most of them should be halted by now (e.g., cmd processing). I was looking at suspend() in particular, which I thought you were looking at in this patch series.

Please note that "card->adapter" is used only in pcie.c/sdio.c/usb.c files

Writes won't have race with reads.

1) write 1 --- "card->adapter = adapter;" in mwifiex_register_dev()

        This place is at the beginning of initialization.
        mwifiex_pcie_probe() -> mwifiex_add_card() -> adapter->if_ops.register_dev()
        There is no chance that "card->adapter" is read anywhere at this point. FW is not yet downloaded

2) write 2 ---- "card->adapter = NULL;" in mwifiex_unregister_dev()
This place the end of teardown phase.
Interrupts are disabled and all cleanup is done. We have "card->adapter" NULL checks at entry point of suspend/remove/resume, if they get called after this.

Quoted 6 lines
Quoted 3 lines

How exactly are you preventing ->suspend() from being called *while* you are running mwifiex_unregister_dev()? I.e. user slamming the lid of a laptop and throwing it in their backpack?

As I already commented, isn't this primarily [*] called from the driver remove() callback? remove() doesn't race with suspend(), does it?

Nope, there is request_firmware_nowait() path that is asynchronous with device registration/unregistration and power management. Maybe the right way is to move the driver to async probing and switch that request_firmware_nowait() into plain request_firmware(). I haven't looked that closely.

The wording "we may end up accessing invalid memory in some corner cases" which is a synonym for "the code is racy" caught my eye, thus the response on the patch.

Thanks.

Signature · 1 line
Dmitry
maintainer
#15 lore raw
In reply to Brian Norris

(I think Dmitry noticed the same while I wrote this.)

Quoted 2 lines · Brian Norris

[*] The other cases are in error handling cases. I guess I should make sure those didn't race too...

Ah, well I think I missed one case:

For the async FW request code path, the callback mwifiex_fw_dpc() can fail to load FW, and so it unwinds with:

        if (adapter->if_ops.unregister_dev)
                adapter->if_ops.unregister_dev(adapter);

This all happens after probe() is finished, so we can have a race on card->adapter being read in suspend() and written in the $subject patch.

Can I revoke my Reviewed-by? (Or make it Reviewed-and-found-wanting-by?)

Brian