mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: duoming@zju.edu.cn
To: "Jakub Kicinski" <kuba@kernel.org>
Cc: krzysztof.kozlowski@linaro.org, pabeni@redhat.com,
	linux-kernel@vger.kernel.org, davem@davemloft.net,
	gregkh@linuxfoundation.org, alexander.deucher@amd.com,
	akpm@linux-foundation.org, broonie@kernel.org,
	netdev@vger.kernel.org, linma@zju.edu.cn
Subject: Re: Re: [PATCH net v4] nfc: nfcmrvl: main: reorder destructive operations in nfcmrvl_nci_unregister_dev to avoid bugs
Date: Thu, 28 Apr 2022 12:03:45 +0800 (GMT+08:00)	[thread overview]
Message-ID: <12cdcfe5.1d62.1806e56d31f.Coremail.duoming@zju.edu.cn> (raw)
In-Reply-To: <20220427174548.2ae53b84@kernel.org>

Hello,

On Wed, 27 Apr 2022 17:45:48 -0700 Jakub Kicinski wrote:

> > diff --git a/net/nfc/core.c b/net/nfc/core.c
> > index dc7a2404efd..1d91334ee86 100644
> > --- a/net/nfc/core.c
> > +++ b/net/nfc/core.c
> > @@ -25,6 +25,8 @@
> >  #define NFC_CHECK_PRES_FREQ_MS	2000
> >  
> >  int nfc_devlist_generation;
> > +/* nfc_download: used to judge whether nfc firmware download could start */
> > +static bool nfc_download;
> >  DEFINE_MUTEX(nfc_devlist_mutex);
> >  
> >  /* NFC device ID bitmap */
> > @@ -38,7 +40,7 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
> >  
> >  	device_lock(&dev->dev);
> >  
> > -	if (!device_is_registered(&dev->dev)) {
> > +	if (!device_is_registered(&dev->dev) || !nfc_download) {
> >  		rc = -ENODEV;
> >  		goto error;
> >  	}
> > @@ -1134,6 +1136,7 @@ int nfc_register_device(struct nfc_dev *dev)
> >  			dev->rfkill = NULL;
> >  		}
> >  	}
> > +	nfc_download = true;
> >  	device_unlock(&dev->dev);
> >  
> >  	rc = nfc_genl_device_added(dev);
> > @@ -1166,6 +1169,7 @@ void nfc_unregister_device(struct nfc_dev *dev)
> >  		rfkill_unregister(dev->rfkill);
> >  		rfkill_destroy(dev->rfkill);
> >  	}
> > +	nfc_download = false;
> >  	device_unlock(&dev->dev);
> >  
> >  	if (dev->ops->check_presence) {
> 
> You can't use a single global variable, there can be many devices 
> each with their own lock.
> 
> Paolo suggested adding a lock, if spin lock doesn't fit the bill
> why not add a mutex?

We could not use mutex either, because the release_firmware() is also called by fw_dnld_timeout()
which is a timer handler. If we use mutex lock in a timer handler, it will cause sleep in atomic bug.
The process is shown below:

nfcmrvl_fw_dnld_start
 ...              
 mod_timer 
 (wait a time)  
 fw_dnld_timeout
   fw_dnld_over 
    release_firmware       

I will change the single global variable to dev->dev_up flag, which is shown below:

diff --git a/drivers/nfc/nfcmrvl/main.c b/drivers/nfc/nfcmrvl/main.c
index 2fcf545012b..1a5284de434 100644
--- a/drivers/nfc/nfcmrvl/main.c
+++ b/drivers/nfc/nfcmrvl/main.c
@@ -183,6 +183,7 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
 {
        struct nci_dev *ndev = priv->ndev;

+       nci_unregister_device(ndev);
        if (priv->ndev->nfc_dev->fw_download_in_progress)
                nfcmrvl_fw_dnld_abort(priv);

@@ -191,7 +192,6 @@ void nfcmrvl_nci_unregister_dev(struct nfcmrvl_private *priv)
        if (gpio_is_valid(priv->config.reset_n_io))
                gpio_free(priv->config.reset_n_io);

-       nci_unregister_device(ndev);
        nci_free_device(ndev);
        kfree(priv);
 }
diff --git a/net/nfc/core.c b/net/nfc/core.c
index dc7a2404efd..09f54c599fe 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -1166,6 +1166,7 @@ void nfc_unregister_device(struct nfc_dev *dev)
                rfkill_unregister(dev->rfkill);
                rfkill_destroy(dev->rfkill);
        }
+       dev->dev_up = false;
        device_unlock(&dev->dev);

        if (dev->ops->check_presence) {

The above solution has been tested, it is well synchronized.

Best regards,
Duoming Zhou

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

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27  1:14 Duoming Zhou
2022-04-28  0:45 ` Jakub Kicinski
2022-04-28  4:03   ` duoming [this message]
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
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=12cdcfe5.1d62.1806e56d31f.Coremail.duoming@zju.edu.cn \
    --to=duoming@zju.edu.cn \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.deucher@amd.com \
    --cc=broonie@kernel.org \
    --cc=davem@davemloft.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzysztof.kozlowski@linaro.org \
    --cc=kuba@kernel.org \
    --cc=linma@zju.edu.cn \
    --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®