linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mihai Carabas <mihai.carabas@oracle.com>
To: linux-kernel@vger.kernel.org
Cc: Peng Hao <peng.hao2@zte.com.cn>,
	Mihai Carabas <mihai.carabas@oracle.com>
Subject: [PATCH 3/4] misc/pvpanic: Avoid initializing multiple pvpanic devices
Date: Thu, 29 Oct 2020 13:43:06 +0200	[thread overview]
Message-ID: <1603971787-16784-4-git-send-email-mihai.carabas@oracle.com> (raw)
In-Reply-To: <1603971787-16784-1-git-send-email-mihai.carabas@oracle.com>

From: Peng Hao <peng.hao2@zte.com.cn>

Avoid initializing multiple pvpanic devices when configure multiple
pvpanic device driver type. Make sure that only one pvpanic device
is working.

Signed-off-by: Peng Hao <peng.hao2@zte.com.cn>
Signed-off-by: Mihai Carabas <mihai.carabas@oracle.com>
---
 drivers/misc/pvpanic/pvpanic.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/pvpanic/pvpanic.c b/drivers/misc/pvpanic/pvpanic.c
index 4c831f5..8c7b534 100644
--- a/drivers/misc/pvpanic/pvpanic.c
+++ b/drivers/misc/pvpanic/pvpanic.c
@@ -17,10 +17,13 @@
 #include <uapi/misc/pvpanic.h>
 
 static struct {
+	struct mutex lock;
 	struct platform_device *pdev;
 	void __iomem *base;
 	bool is_ioport;
-} pvpanic_data;
+} pvpanic_data = {
+	.lock = __MUTEX_INITIALIZER(pvpanic_data.lock),
+};
 
 MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
 MODULE_DESCRIPTION("pvpanic device driver");
@@ -56,9 +59,17 @@ int pvpanic_add_device(struct device *dev, struct resource *res)
 	struct platform_device *pdev;
 	int ret;
 
+	mutex_lock(&pvpanic_data.lock);
+	if (pvpanic_data.pdev) {
+		mutex_unlock(&pvpanic_data.lock);
+		return -EEXIST;
+	}
+
 	pdev = platform_device_alloc("pvpanic", -1);
-	if (!pdev)
+	if (!pdev) {
+		mutex_unlock(&pvpanic_data.lock);
 		return -ENOMEM;
+	}
 
 	pdev->dev.parent = dev;
 
@@ -70,9 +81,11 @@ int pvpanic_add_device(struct device *dev, struct resource *res)
 	if (ret)
 		goto err;
 	pvpanic_data.pdev = pdev;
+	mutex_unlock(&pvpanic_data.lock);
 
 	return 0;
 err:
+	mutex_unlock(&pvpanic_data.lock);
 	platform_device_put(pdev);
 	return ret;
 }
@@ -80,8 +93,10 @@ int pvpanic_add_device(struct device *dev, struct resource *res)
 
 void pvpanic_remove_device(void)
 {
+	mutex_lock(&pvpanic_data.lock);
 	platform_device_unregister(pvpanic_data.pdev);
 	pvpanic_data.pdev = NULL;
+	mutex_unlock(&pvpanic_data.lock);
 }
 EXPORT_SYMBOL_GPL(pvpanic_remove_device);
 
-- 
1.8.3.1


  parent reply	other threads:[~2020-10-29 12:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-29 11:43 [PATCH] add support for pci in the pvpanic driver Mihai Carabas
2020-10-29 11:43 ` [PATCH 1/4] misc/pvpanic : preparing for pvpanic driver framework Mihai Carabas
2020-10-29 17:02   ` Andy Shevchenko
2020-10-29 11:43 ` [PATCH 2/4] misc/pvpanic: Add " Mihai Carabas
2020-10-29 13:48   ` kernel test robot
2020-10-29 14:48   ` kernel test robot
2020-10-29 11:43 ` Mihai Carabas [this message]
2020-10-29 11:43 ` [PATCH 4/4] misc/pvpanic: add new pvpanic pci driver Mihai Carabas
2020-11-23 13:18 ` Re [PATCH] add support for pci in the pvpanic driver Mihai Carabas

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=1603971787-16784-4-git-send-email-mihai.carabas@oracle.com \
    --to=mihai.carabas@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peng.hao2@zte.com.cn \
    /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;
as well as URLs for NNTP newsgroup(s).