All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cristian Marussi <cristian.marussi@arm.com>
To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org
Cc: sudeep.holla@arm.com, lukasz.luba@arm.com,
	james.quinlan@broadcom.com, Jonathan.Cameron@Huawei.com,
	f.fainelli@gmail.com, etienne.carriere@linaro.org,
	thara.gopinath@linaro.org, vincent.guittot@linaro.org,
	souvik.chakravarty@arm.com, cristian.marussi@arm.com
Subject: [PATCH v4 05/37] firmware: arm_scmi: make notifications aware of protocols users
Date: Wed,  6 Jan 2021 20:15:38 +0000	[thread overview]
Message-ID: <20210106201610.26538-6-cristian.marussi@arm.com> (raw)
In-Reply-To: <20210106201610.26538-1-cristian.marussi@arm.com>

Account for any active registered notifier against the proper related
protocol; do not consider pending event handlers, only active handlers
will concur to protocol usage accounting.

Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
 drivers/firmware/arm_scmi/notify.c | 51 ++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 7 deletions(-)

diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index 66196b293b6c..cdaf0f962d84 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -91,6 +91,7 @@
 #include <linux/types.h>
 #include <linux/workqueue.h>
 
+#include "common.h"
 #include "notify.h"
 
 #define SCMI_MAX_PROTO		256
@@ -368,7 +369,7 @@ static struct scmi_event_handler *
 scmi_get_active_handler(struct scmi_notify_instance *ni, u32 evt_key);
 static void scmi_put_active_handler(struct scmi_notify_instance *ni,
 				    struct scmi_event_handler *hndl);
-static void scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
+static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
 				      struct scmi_event_handler *hndl);
 
 /**
@@ -900,9 +901,21 @@ static inline int scmi_bind_event_handler(struct scmi_notify_instance *ni,
 	if (!r_evt)
 		return -EINVAL;
 
-	/* Remove from pending and insert into registered */
+	/*
+	 * Remove from pending and insert into registered while getting hold
+	 * of protocol instance.
+	 */
 	hash_del(&hndl->hash);
+	/*
+	 * Acquire protocols only for NON pending handlers, so as NOT to trigger
+	 * protocol initialization when a notifier is registered against a still
+	 * not registered protocol, since it would make little sense to force init
+	 * protocols for which still no SCMI driver user exists: they wouldn't
+	 * emit any event anyway till some SCMI driver starts using it.
+	 */
+	scmi_acquire_protocol(ni->handle, KEY_XTRACT_PROTO_ID(hndl->key));
 	hndl->r_evt = r_evt;
+
 	mutex_lock(&r_evt->proto->registered_mtx);
 	hash_add(r_evt->proto->registered_events_handlers,
 		 &hndl->hash, hndl->key);
@@ -1193,41 +1206,65 @@ static int scmi_disable_events(struct scmi_event_handler *hndl)
  * * unregister and free the handler itself
  *
  * Context: Assumes all the proper locking has been managed by the caller.
+ *
+ * Return: True if handler was freed (users dropped to zero)
  */
-static void scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
+static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
 				      struct scmi_event_handler *hndl)
 {
+	bool freed = false;
+
 	if (refcount_dec_and_test(&hndl->users)) {
 		if (!IS_HNDL_PENDING(hndl))
 			scmi_disable_events(hndl);
 		scmi_free_event_handler(hndl);
+		freed = true;
 	}
+
+	return freed;
 }
 
 static void scmi_put_handler(struct scmi_notify_instance *ni,
 			     struct scmi_event_handler *hndl)
 {
+	bool freed;
+	u8 protocol_id;
 	struct scmi_registered_event *r_evt = hndl->r_evt;
 
 	mutex_lock(&ni->pending_mtx);
-	if (r_evt)
+	if (r_evt) {
+		protocol_id = r_evt->proto->id;
 		mutex_lock(&r_evt->proto->registered_mtx);
+	}
 
-	scmi_put_handler_unlocked(ni, hndl);
+	freed = scmi_put_handler_unlocked(ni, hndl);
 
-	if (r_evt)
+	if (r_evt) {
 		mutex_unlock(&r_evt->proto->registered_mtx);
+		/*
+		 * Only registered handler acquired protocol; must be here
+		 * released only AFTER unlocking registered_mtx, since
+		 * releasing a protocol can trigger its de-initialization
+		 * (ie. including r_evt and registered_mtx)
+		 */
+		if (freed)
+			scmi_release_protocol(ni->handle, protocol_id);
+	}
 	mutex_unlock(&ni->pending_mtx);
 }
 
 static void scmi_put_active_handler(struct scmi_notify_instance *ni,
 				    struct scmi_event_handler *hndl)
 {
+	bool freed;
 	struct scmi_registered_event *r_evt = hndl->r_evt;
+	u8 protocol_id = r_evt->proto->id;
 
 	mutex_lock(&r_evt->proto->registered_mtx);
-	scmi_put_handler_unlocked(ni, hndl);
+	freed = scmi_put_handler_unlocked(ni, hndl);
 	mutex_unlock(&r_evt->proto->registered_mtx);
+	if (freed)
+		scmi_release_protocol(ni->handle, protocol_id);
 }
 
 /**
-- 
2.17.1


WARNING: multiple messages have this Message-ID (diff)
From: Cristian Marussi <cristian.marussi@arm.com>
To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org
Cc: f.fainelli@gmail.com, vincent.guittot@linaro.org,
	sudeep.holla@arm.com, thara.gopinath@linaro.org,
	cristian.marussi@arm.com, james.quinlan@broadcom.com,
	Jonathan.Cameron@Huawei.com, souvik.chakravarty@arm.com,
	etienne.carriere@linaro.org, lukasz.luba@arm.com
Subject: [PATCH v4 05/37] firmware: arm_scmi: make notifications aware of protocols users
Date: Wed,  6 Jan 2021 20:15:38 +0000	[thread overview]
Message-ID: <20210106201610.26538-6-cristian.marussi@arm.com> (raw)
In-Reply-To: <20210106201610.26538-1-cristian.marussi@arm.com>

Account for any active registered notifier against the proper related
protocol; do not consider pending event handlers, only active handlers
will concur to protocol usage accounting.

Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
 drivers/firmware/arm_scmi/notify.c | 51 ++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 7 deletions(-)

diff --git a/drivers/firmware/arm_scmi/notify.c b/drivers/firmware/arm_scmi/notify.c
index 66196b293b6c..cdaf0f962d84 100644
--- a/drivers/firmware/arm_scmi/notify.c
+++ b/drivers/firmware/arm_scmi/notify.c
@@ -91,6 +91,7 @@
 #include <linux/types.h>
 #include <linux/workqueue.h>
 
+#include "common.h"
 #include "notify.h"
 
 #define SCMI_MAX_PROTO		256
@@ -368,7 +369,7 @@ static struct scmi_event_handler *
 scmi_get_active_handler(struct scmi_notify_instance *ni, u32 evt_key);
 static void scmi_put_active_handler(struct scmi_notify_instance *ni,
 				    struct scmi_event_handler *hndl);
-static void scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
+static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
 				      struct scmi_event_handler *hndl);
 
 /**
@@ -900,9 +901,21 @@ static inline int scmi_bind_event_handler(struct scmi_notify_instance *ni,
 	if (!r_evt)
 		return -EINVAL;
 
-	/* Remove from pending and insert into registered */
+	/*
+	 * Remove from pending and insert into registered while getting hold
+	 * of protocol instance.
+	 */
 	hash_del(&hndl->hash);
+	/*
+	 * Acquire protocols only for NON pending handlers, so as NOT to trigger
+	 * protocol initialization when a notifier is registered against a still
+	 * not registered protocol, since it would make little sense to force init
+	 * protocols for which still no SCMI driver user exists: they wouldn't
+	 * emit any event anyway till some SCMI driver starts using it.
+	 */
+	scmi_acquire_protocol(ni->handle, KEY_XTRACT_PROTO_ID(hndl->key));
 	hndl->r_evt = r_evt;
+
 	mutex_lock(&r_evt->proto->registered_mtx);
 	hash_add(r_evt->proto->registered_events_handlers,
 		 &hndl->hash, hndl->key);
@@ -1193,41 +1206,65 @@ static int scmi_disable_events(struct scmi_event_handler *hndl)
  * * unregister and free the handler itself
  *
  * Context: Assumes all the proper locking has been managed by the caller.
+ *
+ * Return: True if handler was freed (users dropped to zero)
  */
-static void scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
+static bool scmi_put_handler_unlocked(struct scmi_notify_instance *ni,
 				      struct scmi_event_handler *hndl)
 {
+	bool freed = false;
+
 	if (refcount_dec_and_test(&hndl->users)) {
 		if (!IS_HNDL_PENDING(hndl))
 			scmi_disable_events(hndl);
 		scmi_free_event_handler(hndl);
+		freed = true;
 	}
+
+	return freed;
 }
 
 static void scmi_put_handler(struct scmi_notify_instance *ni,
 			     struct scmi_event_handler *hndl)
 {
+	bool freed;
+	u8 protocol_id;
 	struct scmi_registered_event *r_evt = hndl->r_evt;
 
 	mutex_lock(&ni->pending_mtx);
-	if (r_evt)
+	if (r_evt) {
+		protocol_id = r_evt->proto->id;
 		mutex_lock(&r_evt->proto->registered_mtx);
+	}
 
-	scmi_put_handler_unlocked(ni, hndl);
+	freed = scmi_put_handler_unlocked(ni, hndl);
 
-	if (r_evt)
+	if (r_evt) {
 		mutex_unlock(&r_evt->proto->registered_mtx);
+		/*
+		 * Only registered handler acquired protocol; must be here
+		 * released only AFTER unlocking registered_mtx, since
+		 * releasing a protocol can trigger its de-initialization
+		 * (ie. including r_evt and registered_mtx)
+		 */
+		if (freed)
+			scmi_release_protocol(ni->handle, protocol_id);
+	}
 	mutex_unlock(&ni->pending_mtx);
 }
 
 static void scmi_put_active_handler(struct scmi_notify_instance *ni,
 				    struct scmi_event_handler *hndl)
 {
+	bool freed;
 	struct scmi_registered_event *r_evt = hndl->r_evt;
+	u8 protocol_id = r_evt->proto->id;
 
 	mutex_lock(&r_evt->proto->registered_mtx);
-	scmi_put_handler_unlocked(ni, hndl);
+	freed = scmi_put_handler_unlocked(ni, hndl);
 	mutex_unlock(&r_evt->proto->registered_mtx);
+	if (freed)
+		scmi_release_protocol(ni->handle, protocol_id);
 }
 
 /**
-- 
2.17.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2021-01-06 20:21 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-06 20:15 [PATCH v4 0/37] SCMI vendor protocols and modularization Cristian Marussi
2021-01-06 20:15 ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 01/37] firmware: arm_scmi: review protocol registration interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 02/37] firmware: arm_scmi: introduce protocol handle definitions Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-07 14:29   ` Thara Gopinath
2021-01-07 14:29     ` Thara Gopinath
2021-01-08 12:04     ` Cristian Marussi
2021-01-08 12:04       ` Cristian Marussi
2021-01-08 15:50       ` Thara Gopinath
2021-01-08 15:50         ` Thara Gopinath
2021-01-06 20:15 ` [PATCH v4 03/37] firmware: arm_scmi: introduce devres get/put protocols operations Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-07 14:28   ` Thara Gopinath
2021-01-07 14:28     ` Thara Gopinath
2021-01-08 12:24     ` Cristian Marussi
2021-01-08 12:24       ` Cristian Marussi
2021-01-08 13:42       ` Etienne Carriere
2021-01-08 13:42         ` Etienne Carriere
2021-01-08 16:42         ` Cristian Marussi
2021-01-08 16:42           ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 04/37] [RFC] firmware: arm_scmi: introduce bare get/put protocols ops Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-07 14:30   ` Thara Gopinath
2021-01-07 14:30     ` Thara Gopinath
2021-01-06 20:15 ` Cristian Marussi [this message]
2021-01-06 20:15   ` [PATCH v4 05/37] firmware: arm_scmi: make notifications aware of protocols users Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 06/37] firmware: arm_scmi: introduce new devres notification ops Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 07/37] firmware: arm_scmi: refactor events registration Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 08/37] firmware: arm_scmi: convert events registration to protocol handles Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 09/37] firmware: arm_scmi: add new protocol handle core xfer ops Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 10/37] firmware: arm_scmi: add helper to access revision area memory Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 11/37] firmware: arm_scmi: port Base protocol to new interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 12/37] firmware: arm_scmi: port Perf protocol to new protocols interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 13/37] cpufreq: scmi: port driver to the new scmi_perf_proto_ops interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 14/37] firmware: arm_scmi: remove legacy scmi_perf_ops protocol interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 15/37] firmware: arm_scmi: port Power protocol to new protocols interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 16/37] firmware: arm_scmi: port GenPD driver to the new scmi_power_proto_ops interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 17/37] firmware: arm_scmi: remove legacy scmi_power_ops protocol interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 18/37] firmware: arm_scmi: port Clock protocol to new protocols interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 19/37] clk: scmi: port driver to the new scmi_clk_proto_ops interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 20/37] firmware: arm_scmi: remove legacy scmi_clk_ops protocol interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 21/37] firmware: arm_scmi: port Reset protocol to new protocols interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 22/37] reset: reset-scmi: port driver to the new scmi_reset_proto_ops interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 23/37] firmware: arm_scmi: remove legacy scmi_reset_ops protocol interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 24/37] firmware: arm_scmi: port Sensor protocol to new protocols interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 25/37] hwmon: (scmi) port driver to the new scmi_sensor_proto_ops interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:15 ` [PATCH v4 26/37] firmware: arm_scmi: remove legacy scmi_sensor_ops protocol interface Cristian Marussi
2021-01-06 20:15   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 27/37] firmware: arm_scmi: port SystemPower protocol to new protocols interface Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 28/37] firmware: arm_scmi: port Voltage " Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 29/37] regulator: scmi: port driver to the new scmi_voltage_proto_ops interface Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 30/37] firmware: arm_scmi: remove legacy scmi_voltage_ops protocol interface Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 31/37] firmware: arm_scmi: make references to handle const Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 32/37] firmware: arm_scmi: cleanup legacy protocol init code Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 33/37] firmware: arm_scmi: cleanup unused core xfer wrappers Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 34/37] firmware: arm_scmi: cleanup events registration transient code Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 35/37] firmware: arm_scmi: make notify_priv really private Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 36/37] firmware: arm_scmi: add protocol modularization support Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-06 20:16 ` [PATCH v4 37/37] firmware: arm_scmi: add dynamic scmi devices creation Cristian Marussi
2021-01-06 20:16   ` Cristian Marussi
2021-01-07 14:28   ` Thara Gopinath
2021-01-07 14:28     ` Thara Gopinath
2021-01-08 14:42     ` Cristian Marussi
2021-01-08 14:42       ` Cristian Marussi
2021-01-08 16:32       ` Thara Gopinath
2021-01-08 16:32         ` Thara Gopinath
2021-01-08 16:52         ` Cristian Marussi
2021-01-08 16:52           ` Cristian Marussi

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=20210106201610.26538-6-cristian.marussi@arm.com \
    --to=cristian.marussi@arm.com \
    --cc=Jonathan.Cameron@Huawei.com \
    --cc=etienne.carriere@linaro.org \
    --cc=f.fainelli@gmail.com \
    --cc=james.quinlan@broadcom.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=souvik.chakravarty@arm.com \
    --cc=sudeep.holla@arm.com \
    --cc=thara.gopinath@linaro.org \
    --cc=vincent.guittot@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.