The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v1 0/1] firmware: arm_scmi: Lock powercap state updates
@ 2026-12-04 11:14 Philip Radford
  2026-12-04 11:14 ` [PATCH v1 1/1] " Philip Radford
  2026-12-04 11:39 ` [PATCH v1 0/1] " Cristian Marussi
  0 siblings, 2 replies; 4+ messages in thread
From: Philip Radford @ 2026-12-04 11:14 UTC (permalink / raw)
  To: linux-kernel, linux-arm-kernel, arm-scmi
  Cc: sudeep.holla, cristian.marussi, Philip Radford

Hi,

This patch addresses an SCMI powercap bug reported by Sashiko.

Concurrent sysfs writes could lead to conflicting cached values in a
powercap domain, specifically the enabled and last_pcap values. This patch
adds a mutex lock to avoid this from happening.

Based on v7.4-rc5 and tested on an emulated setup.

Regards,
Phil

Philip Radford (1):
  firmware: arm_scmi: Lock powercap state updates

 drivers/firmware/arm_scmi/powercap.c | 37 +++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 deletions(-)

--
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v1 1/1] firmware: arm_scmi: Lock powercap state updates
  2026-12-04 11:14 [PATCH v1 0/1] firmware: arm_scmi: Lock powercap state updates Philip Radford
@ 2026-12-04 11:14 ` Philip Radford
  2026-12-04 13:33   ` Cristian Marussi
  2026-12-04 11:39 ` [PATCH v1 0/1] " Cristian Marussi
  1 sibling, 1 reply; 4+ messages in thread
From: Philip Radford @ 2026-12-04 11:14 UTC (permalink / raw)
  To: linux-kernel, linux-arm-kernel, arm-scmi
  Cc: sudeep.holla, cristian.marussi, Philip Radford, Sashiko

Add a per-domain mutex to prevent concurrent limit and enable writes
leaving the cached state out of sync during CAP_SET operations.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-1d52cced-b877-4a25-a7eb-fa3de15745f9
Fixes: 758cd5fc13b2 ("firmware: arm_scmi: Add Powercap protocol enable support")
Signed-off-by: Philip Radford <philip.radford@arm.com>
---
 drivers/firmware/arm_scmi/powercap.c | 37 +++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/arm_scmi/powercap.c b/drivers/firmware/arm_scmi/powercap.c
index e6dd18c6a154..144e6435869a 100644
--- a/drivers/firmware/arm_scmi/powercap.c
+++ b/drivers/firmware/arm_scmi/powercap.c
@@ -10,6 +10,7 @@
 #include <linux/bitfield.h>
 #include <linux/io.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/scmi_protocol.h>
 
 #include <trace/events/scmi.h>
@@ -111,6 +112,8 @@ struct scmi_powercap_meas_changed_notify_payld {
 };
 
 struct scmi_powercap_state {
+	/* Lock protects enabled and last_pcap while changing a domain power cap */
+	struct mutex lock;
 	bool enabled;
 	u32 last_pcap;
 	bool meas_notif_enabled;
@@ -444,6 +447,7 @@ static int scmi_powercap_cap_set(const struct scmi_protocol_handle *ph,
 				 bool ignore_dresp)
 {
 	struct powercap_info *pi = ph->get_priv(ph);
+	int ret;
 
 	/*
 	 * Disallow zero as a possible explicitly requested powercap:
@@ -457,10 +461,18 @@ static int scmi_powercap_cap_set(const struct scmi_protocol_handle *ph,
 		if (!scmi_powercap_dom_info_get(ph, domain_id))
 			return -EINVAL;
 
+		mutex_lock(&pi->states[domain_id].lock);
+
 		if (!pi->states[domain_id].enabled) {
 			pi->states[domain_id].last_pcap = power_cap;
-			return 0;
+			ret = 0;
+		} else {
+			ret = __scmi_powercap_cap_set(ph, pi, domain_id, power_cap,
+						      ignore_dresp);
 		}
+
+		mutex_unlock(&pi->states[domain_id].lock);
+		return ret;
 	}
 
 	return __scmi_powercap_cap_set(ph, pi, domain_id,
@@ -644,13 +656,20 @@ static int scmi_powercap_cap_enable_set(const struct scmi_protocol_handle *ph,
 	if (!scmi_powercap_dom_info_get(ph, domain_id))
 		return -EINVAL;
 
-	if (enable == pi->states[domain_id].enabled)
+	mutex_lock(&pi->states[domain_id].lock);
+
+	if (enable == pi->states[domain_id].enabled) {
+		mutex_unlock(&pi->states[domain_id].lock);
 		return 0;
+	}
 
 	if (enable) {
 		/* Cannot enable with a zero powercap. */
-		if (!pi->states[domain_id].last_pcap)
+		if (!pi->states[domain_id].last_pcap) {
+			mutex_unlock(&pi->states[domain_id].lock);
 			return -EINVAL;
+		}
+
 
 		ret = __scmi_powercap_cap_set(ph, pi, domain_id,
 					      pi->states[domain_id].last_pcap,
@@ -659,8 +678,10 @@ static int scmi_powercap_cap_enable_set(const struct scmi_protocol_handle *ph,
 		ret = __scmi_powercap_cap_set(ph, pi, domain_id, 0, true);
 	}
 
-	if (ret)
+	if (ret) {
+		mutex_unlock(&pi->states[domain_id].lock);
 		return ret;
+	}
 
 	/*
 	 * Update our internal state to reflect final platform state: the SCMI
@@ -671,6 +692,8 @@ static int scmi_powercap_cap_enable_set(const struct scmi_protocol_handle *ph,
 	if (!ret)
 		pi->states[domain_id].enabled = !!power_cap;
 
+	mutex_unlock(&pi->states[domain_id].lock);
+
 	return ret;
 }
 
@@ -688,6 +711,8 @@ static int scmi_powercap_cap_enable_get(const struct scmi_protocol_handle *ph,
 	if (!scmi_powercap_dom_info_get(ph, domain_id))
 		return -EINVAL;
 
+	mutex_lock(&pi->states[domain_id].lock);
+
 	/*
 	 * Report always real platform state; platform could have ignored
 	 * a previous disable request. Default true on any error.
@@ -699,6 +724,8 @@ static int scmi_powercap_cap_enable_get(const struct scmi_protocol_handle *ph,
 	/* Update internal state with current real platform state */
 	pi->states[domain_id].enabled = *enable;
 
+	mutex_unlock(&pi->states[domain_id].lock);
+
 	return 0;
 }
 
@@ -1000,6 +1027,8 @@ scmi_powercap_protocol_init(const struct scmi_protocol_handle *ph)
 	 * formed and correlated by sane parent-child relationship (if any).
 	 */
 	for (domain = 0; domain < pinfo->num_domains; domain++) {
+		mutex_init(&pinfo->states[domain].lock);
+
 		ret = scmi_powercap_domain_attributes_get(ph, pinfo, domain);
 		if (ret)
 			return ret;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v1 0/1] firmware: arm_scmi: Lock powercap state updates
  2026-12-04 11:14 [PATCH v1 0/1] firmware: arm_scmi: Lock powercap state updates Philip Radford
  2026-12-04 11:14 ` [PATCH v1 1/1] " Philip Radford
@ 2026-12-04 11:39 ` Cristian Marussi
  1 sibling, 0 replies; 4+ messages in thread
From: Cristian Marussi @ 2026-12-04 11:39 UTC (permalink / raw)
  To: Philip Radford
  Cc: linux-kernel, linux-arm-kernel, arm-scmi, sudeep.holla,
	cristian.marussi

On Fri, Dec 04, 2026 at 11:14:27AM +0000, Philip Radford wrote:
> Hi,
> 

Hi,

a quick one Philip, no need usually to add a cover-letter for a single
series patch, unless you want to communicate something really verbose
or, say, test results...if it can fit within the patch commit message
and eventually the offline comments under the --- area a cover-letter
can be avoided.

Thanks,
Cristian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v1 1/1] firmware: arm_scmi: Lock powercap state updates
  2026-12-04 11:14 ` [PATCH v1 1/1] " Philip Radford
@ 2026-12-04 13:33   ` Cristian Marussi
  0 siblings, 0 replies; 4+ messages in thread
From: Cristian Marussi @ 2026-12-04 13:33 UTC (permalink / raw)
  To: Philip Radford
  Cc: linux-kernel, linux-arm-kernel, arm-scmi, sudeep.holla,
	cristian.marussi, Sashiko

On Fri, Dec 04, 2026 at 11:14:28AM +0000, Philip Radford wrote:
> Add a per-domain mutex to prevent concurrent limit and enable writes
> leaving the cached state out of sync during CAP_SET operations.
> 

Hi Philip,

> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/bug/linux-1d52cced-b877-4a25-a7eb-fa3de15745f9
> Fixes: 758cd5fc13b2 ("firmware: arm_scmi: Add Powercap protocol enable support")
> Signed-off-by: Philip Radford <philip.radford@arm.com>
> ---
>  drivers/firmware/arm_scmi/powercap.c | 37 +++++++++++++++++++++++++---
>  1 file changed, 33 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/firmware/arm_scmi/powercap.c b/drivers/firmware/arm_scmi/powercap.c
> index e6dd18c6a154..144e6435869a 100644
> --- a/drivers/firmware/arm_scmi/powercap.c
> +++ b/drivers/firmware/arm_scmi/powercap.c
> @@ -10,6 +10,7 @@
>  #include <linux/bitfield.h>
>  #include <linux/io.h>
>  #include <linux/module.h>
> +#include <linux/mutex.h>
>  #include <linux/scmi_protocol.h>
>  
>  #include <trace/events/scmi.h>
> @@ -111,6 +112,8 @@ struct scmi_powercap_meas_changed_notify_payld {
>  };
>  
>  struct scmi_powercap_state {
> +	/* Lock protects enabled and last_pcap while changing a domain power cap */
> +	struct mutex lock;
>  	bool enabled;
>  	u32 last_pcap;
>  	bool meas_notif_enabled;
> @@ -444,6 +447,7 @@ static int scmi_powercap_cap_set(const struct scmi_protocol_handle *ph,
>  				 bool ignore_dresp)
>  {
>  	struct powercap_info *pi = ph->get_priv(ph);
> +	int ret;
>  
>  	/*
>  	 * Disallow zero as a possible explicitly requested powercap:
> @@ -457,10 +461,18 @@ static int scmi_powercap_cap_set(const struct scmi_protocol_handle *ph,
>  		if (!scmi_powercap_dom_info_get(ph, domain_id))
>  			return -EINVAL;
>  
> +		mutex_lock(&pi->states[domain_id].lock);
> +
>  		if (!pi->states[domain_id].enabled) {
>  			pi->states[domain_id].last_pcap = power_cap;
> -			return 0;
> +			ret = 0;

Use cleanup handlers all over this series and it should be much more clean.

Just this.

Thanks,
Cristian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04 13:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 11:14 [PATCH v1 0/1] firmware: arm_scmi: Lock powercap state updates Philip Radford
2026-12-04 11:14 ` [PATCH v1 1/1] " Philip Radford
2026-12-04 13:33   ` Cristian Marussi
2026-12-04 11:39 ` [PATCH v1 0/1] " Cristian Marussi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox