diff mbox series

[v0] NFC: reorganize the functions in nci_request

Message ID 20211115142938.8109-1-linma@zju.edu.cn (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v0] NFC: reorganize the functions in nci_request | expand

Checks

Context Check Description
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1 this patch: 1
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 19 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Lin Ma Nov. 15, 2021, 2:29 p.m. UTC
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>
---
 net/nfc/nci/core.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Comments

Denis Kirjanov Nov. 15, 2021, 2:32 p.m. UTC | #1
11/15/21 5:29 PM, 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().
Please add Fixes tag
> 
> Signed-off-by: Lin Ma <linma@zju.edu.cn>
> ---
>   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..2ab2d6a1a143 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;
>
diff mbox series

Patch

diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 6fd873aa86be..2ab2d6a1a143 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;