linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] NFC: reorganize the functions in nci_request
@ 2021-11-15 14:56 Lin Ma
  2021-11-15 14:57 ` CHANGELOG: " Lin Ma
  2021-11-18  4:30 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 4+ messages in thread
From: Lin Ma @ 2021-11-15 14:56 UTC (permalink / raw)
  To: netdev
  Cc: krzysztof.kozlowski, davem, kuba, jirislaby, gregkh,
	linux-kernel, Lin Ma

There is a possible data race as shown below:

thread-A in nci_request()       | thread-B in nci_close_device()
                                | mutex_lock(&ndev->req_lock);
test_bit(NCI_UP, &ndev->flags); |
...                             | test_and_clear_bit(NCI_UP, &ndev->flags)
mutex_lock(&ndev->req_lock);    |
                                |

This race will allow __nci_request() to be awaked while the device is
getting removed.

Similar to commit e2cb6b891ad2 ("bluetooth: eliminate the potential race
condition when removing the HCI controller"). this patch alters the
function sequence in nci_request() to prevent the data races between the
nci_close_device().

Signed-off-by: Lin Ma <linma@zju.edu.cn>
Fixes: 6a2968aaf50c ("NFC: basic NCI protocol implementation")
---
 net/nfc/nci/core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 6fd873aa86be..1dd0269c1a72 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -144,12 +144,15 @@ inline int nci_request(struct nci_dev *ndev,
 {
 	int rc;
 
-	if (!test_bit(NCI_UP, &ndev->flags))
-		return -ENETDOWN;
-
 	/* Serialize all requests */
 	mutex_lock(&ndev->req_lock);
-	rc = __nci_request(ndev, req, opt, timeout);
+	/* check the state after obtaing the lock against any races
+	 * from nci_close_device when the device gets removed.
+	 */
+	if (test_bit(NCI_UP, &ndev->flags))
+		rc = __nci_request(ndev, req, opt, timeout);
+	else
+		rc = -ENETDOWN;
 	mutex_unlock(&ndev->req_lock);
 
 	return rc;
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* CHANGELOG: [PATCH v1] NFC: reorganize the functions in nci_request
  2021-11-15 14:56 [PATCH v1] NFC: reorganize the functions in nci_request Lin Ma
@ 2021-11-15 14:57 ` Lin Ma
  2021-11-17  9:32   ` Krzysztof Kozlowski
  2021-11-18  4:30 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 4+ messages in thread
From: Lin Ma @ 2021-11-15 14:57 UTC (permalink / raw)
  To: netdev; +Cc: krzysztof.kozlowski, davem, kuba, jirislaby, gregkh, linux-kernel

Hi there,

Compared to the version-0 patch, this patch add fixes tag in the patch file

+ Fixes: 6a2968aaf50c ("NFC: basic NCI protocol implementation")


Best regards
Lin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: CHANGELOG: [PATCH v1] NFC: reorganize the functions in nci_request
  2021-11-15 14:57 ` CHANGELOG: " Lin Ma
@ 2021-11-17  9:32   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2021-11-17  9:32 UTC (permalink / raw)
  To: Lin Ma, netdev; +Cc: davem, kuba, jirislaby, gregkh, linux-kernel

On 15/11/2021 15:57, Lin Ma wrote:
> Hi there,
> 
> Compared to the version-0 patch, this patch add fixes tag in the patch file
> 
> + Fixes: 6a2968aaf50c ("NFC: basic NCI protocol implementation")

Changelog goes either as cover letter (git help format-patch) or after
'---' separator to individual patch.

Best regards,
Krzysztof

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] NFC: reorganize the functions in nci_request
  2021-11-15 14:56 [PATCH v1] NFC: reorganize the functions in nci_request Lin Ma
  2021-11-15 14:57 ` CHANGELOG: " Lin Ma
@ 2021-11-18  4:30 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-11-18  4:30 UTC (permalink / raw)
  To: Lin Ma
  Cc: netdev, krzysztof.kozlowski, davem, kuba, jirislaby, gregkh,
	linux-kernel

Hello:

This patch was applied to netdev/net.git (master)
by Jakub Kicinski <kuba@kernel.org>:

On Mon, 15 Nov 2021 22:56:00 +0800 you wrote:
> There is a possible data race as shown below:
> 
> thread-A in nci_request()       | thread-B in nci_close_device()
>                                 | mutex_lock(&ndev->req_lock);
> test_bit(NCI_UP, &ndev->flags); |
> ...                             | test_and_clear_bit(NCI_UP, &ndev->flags)
> mutex_lock(&ndev->req_lock);    |
>                                 |
> 
> [...]

Here is the summary with links:
  - [v1] NFC: reorganize the functions in nci_request
    https://git.kernel.org/netdev/net/c/86cdf8e38792

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-11-18  4:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-15 14:56 [PATCH v1] NFC: reorganize the functions in nci_request Lin Ma
2021-11-15 14:57 ` CHANGELOG: " Lin Ma
2021-11-17  9:32   ` Krzysztof Kozlowski
2021-11-18  4:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).