mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Lin Ma" <linma@zju.edu.cn>
To: "Greg KH" <gregkh@linuxfoundation.org>
Cc: "Jakub Kicinski" <kuba@kernel.org>,
	"Duoming Zhou" <duoming@zju.edu.cn>,
	krzysztof.kozlowski@linaro.org, pabeni@redhat.com,
	linux-kernel@vger.kernel.org, davem@davemloft.net,
	alexander.deucher@amd.com, akpm@linux-foundation.org,
	broonie@kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH net v4] nfc: ... device_is_registered() is data race-able
Date: Thu, 28 Apr 2022 16:49:18 +0800 (GMT+08:00)	[thread overview]
Message-ID: <15d09db2.2f76.1806f5c4187.Coremail.linma@zju.edu.cn> (raw)
In-Reply-To: <YmpNZOaJ1+vWdccK@kroah.com>

Hello Greg,

> 
> It shouldn't be, if you are using it properly :)
> 
> [...]
> 
> Yes, you should almost never use that call.  Seems the nfc subsystem is
> the most common user of it for some reason :(

Cool, and I believe that the current nfc core code does not use it properly. :(

> 
> What state are you trying to track here exactly?
> 

Forget about the firmware downloading race that raised by Duoming in this channel,
all the netlink handler code in net/nfc/core.c depends on the device_is_registered
macro.

My idea is to introduce a patch like below:

 include/net/nfc/nfc.h |  1 +
 net/nfc/core.c        | 26 ++++++++++++++------------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
index 5dee575fbe86..d84e53802b06 100644
--- a/include/net/nfc/nfc.h
+++ b/include/net/nfc/nfc.h
@@ -168,6 +168,7 @@ struct nfc_dev {
 	int targets_generation;
 	struct device dev;
 	bool dev_up;
+	bool dev_register;
 	bool fw_download_in_progress;
 	u8 rf_mode;
 	bool polling;
diff --git a/net/nfc/core.c b/net/nfc/core.c
index dc7a2404efdf..208e6bb0804e 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -38,7 +38,7 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (!dev->dev_register) {
 		rc = -ENODEV;
 		goto error;
 	}
@@ -94,7 +94,7 @@ int nfc_dev_up(struct nfc_dev *dev)
 
 	device_lock(&dev->dev);
 
-	if (!device_is_registered(&dev->dev)) {
+	if (!dev->dev_register) {
 		rc = -ENODEV;
 		goto error;
 	}

[...]

@@ -1134,6 +1134,7 @@ int nfc_register_device(struct nfc_dev *dev)
 			dev->rfkill = NULL;
 		}
 	}
+	dev->dev_register = true;
 	device_unlock(&dev->dev);
 
 	rc = nfc_genl_device_added(dev);
@@ -1162,6 +1163,7 @@ void nfc_unregister_device(struct nfc_dev *dev)
 			 "was removed\n", dev_name(&dev->dev));
 
 	device_lock(&dev->dev);
+	dev->dev_register = false;
 	if (dev->rfkill) {
 		rfkill_unregister(dev->rfkill);
 		rfkill_destroy(dev->rfkill);
-- 
2.35.1

The added dev_register variable can function like the original device_is_registered and does not race-able
because of the protection of device_lock.

I think after such a patch is adopted, the reorder version of patch from Duoming 
-> https://lists.openwall.net/netdev/2022/04/25/10
can be used to fix the firmware downloading bug.

Do you agree on this or should we use another macro that is suitable than device_is_registered?

> thanks,
> 
> greg k-h

Thanks
Lin

  reply	other threads:[~2022-04-28  8:50 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27  1:14 [PATCH net v4] nfc: nfcmrvl: main: reorder destructive operations in nfcmrvl_nci_unregister_dev to avoid bugs Duoming Zhou
2022-04-28  0:45 ` Jakub Kicinski
2022-04-28  4:03   ` duoming
2022-04-28  7:15   ` [PATCH net v4] nfc: ... device_is_registered() is data race-able Lin Ma
2022-04-28  7:38     ` Greg KH
2022-04-28  7:55       ` Lin Ma
2022-04-28  8:16         ` Greg KH
2022-04-28  8:49           ` Lin Ma [this message]
2022-04-28  9:20             ` Greg KH
2022-04-28 13:06               ` Jakub Kicinski
2022-04-28 13:22                 ` Lin Ma
2022-04-28 13:37                   ` Greg KH
2022-04-28 13:53                     ` Lin Ma
2022-04-28 14:12                       ` Greg KH
2022-04-29  3:17                         ` Lin Ma

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15d09db2.2f76.1806f5c4187.Coremail.linma@zju.edu.cn \
    --to=linma@zju.edu.cn \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.deucher@amd.com \
    --cc=broonie@kernel.org \
    --cc=davem@davemloft.net \
    --cc=duoming@zju.edu.cn \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzysztof.kozlowski@linaro.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®