All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gustav Wiklander <gustav.wiklander@axis.com>
To: <linux-pm@vger.kernel.org>
Cc: <rjw@rjwysocki.net>, <khilman@kernel.org>,
	<ulf.hansson@linaro.org>, <len.brown@intel.com>, <pavel@ucw.cz>,
	<gregkh@linuxfoundation.org>, <kernel@axis.com>,
	Gustav Wiklander <gustavwi@axis.com>
Subject: [PATCHv2] PM / Domains: Add module ref count for each consumer
Date: Wed, 10 Jun 2020 21:31:56 +0200	[thread overview]
Message-ID: <20200610193156.20363-1-gustav.wiklander@axis.com> (raw)

From: Gustav Wiklander <gustavwi@axis.com>

Currently a pm_domain can be unloaded without regard for consumers.
This patch adds a module dependecy for every registered consumer.
Now a power domain driver can only be unloaded if no consumers are
registered.

Signed-off-by: Gustav Wiklander <gustavwi@axis.com>
---
Automated setting genpd->owner when calling pm_genpd_init.
Similar to how usb_register_driver does it.

 drivers/base/power/domain.c | 22 +++++++++++++++++-----
 include/linux/pm_domain.h   | 10 ++++++++--
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 0a01df608849..70c8b59bfed9 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -1499,11 +1499,18 @@ static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
 	if (IS_ERR(gpd_data))
 		return PTR_ERR(gpd_data);
 
+	if (!try_module_get(genpd->owner)) {
+		ret = -ENODEV;
+		goto out;
+	}
+
 	gpd_data->cpu = genpd_get_cpu(genpd, base_dev);
 
 	ret = genpd->attach_dev ? genpd->attach_dev(genpd, dev) : 0;
-	if (ret)
+	if (ret) {
+		module_put(genpd->owner);
 		goto out;
+	}
 
 	genpd_lock(genpd);
 
@@ -1579,6 +1586,8 @@ static int genpd_remove_device(struct generic_pm_domain *genpd,
 
 	genpd_free_dev_data(dev, gpd_data);
 
+	module_put(genpd->owner);
+
 	return 0;
 
  out:
@@ -1755,15 +1764,17 @@ static void genpd_lock_init(struct generic_pm_domain *genpd)
 }
 
 /**
- * pm_genpd_init - Initialize a generic I/O PM domain object.
+ * __pm_genpd_init - Initialize a generic I/O PM domain object.
  * @genpd: PM domain object to initialize.
  * @gov: PM domain governor to associate with the domain (may be NULL).
  * @is_off: Initial value of the domain's power_is_off field.
+ * @owner: module owner of this power domain object.
  *
  * Returns 0 on successful initialization, else a negative error code.
  */
-int pm_genpd_init(struct generic_pm_domain *genpd,
-		  struct dev_power_governor *gov, bool is_off)
+int __pm_genpd_init(struct generic_pm_domain *genpd,
+		  struct dev_power_governor *gov, bool is_off,
+		  struct module *owner)
 {
 	int ret;
 
@@ -1777,6 +1788,7 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
 	genpd->gov = gov;
 	INIT_WORK(&genpd->power_off_work, genpd_power_off_work_fn);
 	atomic_set(&genpd->sd_count, 0);
+	genpd->owner = owner;
 	genpd->status = is_off ? GPD_STATE_POWER_OFF : GPD_STATE_ACTIVE;
 	genpd->device_count = 0;
 	genpd->max_off_time_ns = -1;
@@ -1831,7 +1843,7 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(pm_genpd_init);
+EXPORT_SYMBOL_GPL(__pm_genpd_init);
 
 static int genpd_remove(struct generic_pm_domain *genpd)
 {
diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h
index 9ec78ee53652..21a3183e00bc 100644
--- a/include/linux/pm_domain.h
+++ b/include/linux/pm_domain.h
@@ -9,6 +9,7 @@
 #define _LINUX_PM_DOMAIN_H
 
 #include <linux/device.h>
+#include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
 #include <linux/err.h>
@@ -93,6 +94,7 @@ struct opp_table;
 
 struct generic_pm_domain {
 	struct device dev;
+	struct module *owner;           /* Module owner of the PM domain */
 	struct dev_pm_domain domain;	/* PM domain operations */
 	struct list_head gpd_list_node;	/* Node in the global PM domains list */
 	struct list_head master_links;	/* Links with PM domain as a master */
@@ -200,8 +202,12 @@ int pm_genpd_add_subdomain(struct generic_pm_domain *genpd,
 			   struct generic_pm_domain *subdomain);
 int pm_genpd_remove_subdomain(struct generic_pm_domain *genpd,
 			      struct generic_pm_domain *subdomain);
-int pm_genpd_init(struct generic_pm_domain *genpd,
-		  struct dev_power_governor *gov, bool is_off);
+int __pm_genpd_init(struct generic_pm_domain *genpd,
+		    struct dev_power_governor *gov, bool is_off,
+		    struct module *module);
+#define pm_genpd_init(genpd, gov, is_off) \
+	__pm_genpd_init(genpd, gov, is_off, THIS_MODULE)
+
 int pm_genpd_remove(struct generic_pm_domain *genpd);
 int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state);
 
-- 
2.11.0


             reply	other threads:[~2020-06-10 19:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-10 19:31 Gustav Wiklander [this message]
2020-07-09 12:53 ` [PATCHv2] PM / Domains: Add module ref count for each consumer Rafael J. Wysocki
2020-07-10 10:18 ` Ulf Hansson
2020-07-10 10:37   ` Greg Kroah-Hartman
2020-07-10 12:01     ` Ulf Hansson
2020-07-10 12:37       ` Greg Kroah-Hartman
2020-07-10 13:16         ` Ulf Hansson

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=20200610193156.20363-1-gustav.wiklander@axis.com \
    --to=gustav.wiklander@axis.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=gustavwi@axis.com \
    --cc=kernel@axis.com \
    --cc=khilman@kernel.org \
    --cc=len.brown@intel.com \
    --cc=linux-pm@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=rjw@rjwysocki.net \
    --cc=ulf.hansson@linaro.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.