mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Su Weifeng <suweifeng1@huawei.com>
To: <gregkh@linuxfoundation.org>, <mst@redhat.com>
Cc: <linux-kernel@vger.kernel.org>, <shikemeng@huawei.com>,
	<liuzhiqiang26@huawei.com>, <linfeilong@huawei.com>,
	<zhanghongtao22@huawei.com>, Weifeng Su <suweifeng1@huawei.com>
Subject: [PATCH v2] uio:uio_pci_generic:Don't clear master bit when the process does not exit
Date: Tue, 21 Feb 2023 01:10:44 +0800	[thread overview]
Message-ID: <20230220171045.689736-1-suweifeng1@huawei.com> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="y", Size: 2577 bytes --]

From: Weifeng Su <suweifeng1@huawei.com>

The /dev/uioX device has concurrent operations in a few scenarios.

For example, when a process using the device exits abnormally,
the management program starts the same process to operate the device.
When the process exits and closes the /dev/uioX device,
the master bit of the device is cleared. In this case, if the
new process is issuing commands, I/Os are suspended and cannot be
automatically recovered.

Therefore, reference counting is added to clear the master bit
only when the last process exits.

Signed-off-by: Weifeng Su <suweifeng1@huawei.com>
---
The difference between the first patch and the first patch is that 
the reference counting operation is performed using the atomic semantics, 
just like other drivers under UIO:
cdfa835c6e5e87d145f("uio_hv_generic: defer opening vmbus until first use").

 drivers/uio/uio_pci_generic.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/uio/uio_pci_generic.c b/drivers/uio/uio_pci_generic.c
index e03f9b532..df436e3d9 100644
--- a/drivers/uio/uio_pci_generic.c
+++ b/drivers/uio/uio_pci_generic.c
@@ -31,6 +31,7 @@
 struct uio_pci_generic_dev {
 	struct uio_info info;
 	struct pci_dev *pdev;
+	atomic_t	refcnt;
 };
 
 static inline struct uio_pci_generic_dev *
@@ -39,10 +40,19 @@ to_uio_pci_generic_dev(struct uio_info *info)
 	return container_of(info, struct uio_pci_generic_dev, info);
 }
 
+static int open(struct uio_info *info, struct inode *inode)
+{
+	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
+
+	atomic_inc(&gdev->refcnt);
+	return 0;
+}
+
 static int release(struct uio_info *info, struct inode *inode)
 {
 	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
 
+
 	/*
 	 * This driver is insecure when used with devices doing DMA, but some
 	 * people (mis)use it with such devices.
@@ -51,7 +61,8 @@ static int release(struct uio_info *info, struct inode *inode)
 	 * Note that there's a non-zero chance doing this will wedge the device
 	 * at least until reset.
 	 */
-	pci_clear_master(gdev->pdev);
+	if (atomic_dec_and_test(&gdev->refcnt))
+		pci_clear_master(gdev->pdev);
 	return 0;
 }
 
@@ -93,7 +104,9 @@ static int probe(struct pci_dev *pdev,
 	gdev->info.name = "uio_pci_generic";
 	gdev->info.version = DRIVER_VERSION;
 	gdev->info.release = release;
+	gdev->info.open = open;
 	gdev->pdev = pdev;
+	atomic_set(&gdev->refcnt, 0);
 	if (pdev->irq && (pdev->irq != IRQ_NOTCONNECTED)) {
 		gdev->info.irq = pdev->irq;
 		gdev->info.irq_flags = IRQF_SHARED;
-- 
2.33.0


             reply	other threads:[~2023-02-20 16:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-20 17:10 Su Weifeng [this message]
2023-02-20 18:11 ` Greg KH
2023-02-21 12:48   ` Weifeng Su
2023-02-23 15:09     ` Weifeng Su

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=20230220171045.689736-1-suweifeng1@huawei.com \
    --to=suweifeng1@huawei.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linfeilong@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuzhiqiang26@huawei.com \
    --cc=mst@redhat.com \
    --cc=shikemeng@huawei.com \
    --cc=zhanghongtao22@huawei.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®