Skip to contentSkip to conversation

arm_mpam: Only schedule mpam_enable work after first successful MSC probe

PATCH Superseded ping.li 3 participants 13 messages Aug 18 – Oct 13 1 message not in the archive MPAM driver

lore
Share & tools
lore

ping.li

author
#0 lore raw

PATCH 1/1 arm_mpam: Only schedule mpam_enable work after first successful MSC probe

mpam_discovery_cpu_online() sets new_device_probed unconditionally after processing each reachable MSC. Once an MSC has already been probed (msc->probed is true), later CPUs sharing it skip mpam_msc_hw_probe() but still leave err at its default value of 0. As a result, new_device_probed is still set to true, causing mpam_enable_work to be scheduled again even though no new hardware was probed.

Set new_device_probed only when mpam_msc_hw_probe() is called and succeeds.

Fixes 8f8d0ac1da78 ("arm_mpam: Add cpuhp callbacks to probe MSC hardware")
---
 drivers/resctrl/mpam_devices.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/resctrl/mpam_devices.c +4 -2
index 2f09f4b78bd3..fefdcf588932 100644
@@ -1866,13 +1866,15 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 			continue;
 
 		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
+		if (!msc->probed) {
 			err = mpam_msc_hw_probe(msc);
+			if (!err)
+				new_device_probed = true;
+		}
 		mutex_unlock(&msc->probe_lock);
 
 		if (err)
 			break;
-		new_device_probed = true;
 	}
 
 	if (new_device_probed && !err)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/1 arm_mpam: Only schedule mpam_enable work after first successful MSC probe

From: Ping Li <[email protected]>

mpam_discovery_cpu_online() sets new_device_probed unconditionally after processing each reachable MSC. Once an MSC has already been probed (msc->probed is true), later CPUs sharing it skip mpam_msc_hw_probe() but still leave err at its default value of 0. As a result, new_device_probed is still set to true, causing mpam_enable_work to be scheduled again even though no new hardware was probed.

Set new_device_probed only when mpam_msc_hw_probe() is called and succeeds.

---
Changes in v2:
- Drop the Fixes: tag, as the extra mpam_enable() calls cause no real
harm: schedule_work() merges the duplicate work, and mpam_enable() is a no-op until all MSCs have been probed. This is a cleanup, not a bug fix.

 drivers/resctrl/mpam_devices.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/resctrl/mpam_devices.c +4 -2
index 2f09f4b78bd3..fefdcf588932 100644
@@ -1866,13 +1866,15 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 			continue;
 
 		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
+		if (!msc->probed) {
 			err = mpam_msc_hw_probe(msc);
+			if (!err)
+				new_device_probed = true;
+		}
 		mutex_unlock(&msc->probe_lock);
 
 		if (err)
 			break;
-		new_device_probed = true;
 	}
 
 	if (new_device_probed && !err)
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Hi Ping,

Quoted 9 lines · ping.li

From: Ping Li <[email protected]>

mpam_discovery_cpu_online() sets new_device_probed unconditionally after processing each reachable MSC. Once an MSC has already been probed (msc->probed is true), later CPUs sharing it skip mpam_msc_hw_probe() but still leave err at its default value of 0. As a result, new_device_probed is still set to true, causing mpam_enable_work to be scheduled again even though no new hardware was probed.

This patch is an improvement but, thinking again, it looks there is scope getting rid of mpam_enable() altogether. Rather than walking the list after each hw probe we could increment an atomic variable, similar to what is done in mpam_msc_drv_probe(), and then just schedule mpam_enable_once(). What do you think?

Thanks,

Ben

Quoted 37 lines

Set new_device_probed only when mpam_msc_hw_probe() is called and succeeds.

---
Changes in v2:
- Drop the Fixes: tag, as the extra mpam_enable() calls cause no real
harm: schedule_work() merges the duplicate work, and mpam_enable() is a no-op until all MSCs have been probed. This is a cleanup, not a bug fix.

 drivers/resctrl/mpam_devices.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/resctrl/mpam_devices.c +4 -2
index 2f09f4b78bd3..fefdcf588932 100644
@@ -1866,13 +1866,15 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 			continue;
 
 		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
+		if (!msc->probed) {
 			err = mpam_msc_hw_probe(msc);
+			if (!err)
+				new_device_probed = true;
+		}
 		mutex_unlock(&msc->probe_lock);
 
 		if (err)
 			break;
-		new_device_probed = true;
 	}
 
 	if (new_device_probed && !err)
#3 lore raw
Quoted 13 lines · Ben Horgan

Hi Ping,

Quoted 9 lines · ping.li

From: Ping Li <[email protected]>

mpam_discovery_cpu_online() sets new_device_probed unconditionally after processing each reachable MSC. Once an MSC has already been probed (msc->probed is true), later CPUs sharing it skip mpam_msc_hw_probe() but still leave err at its default value of 0. As a result, new_device_probed is still set to true, causing mpam_enable_work to be scheduled again even though no new hardware was probed.

Quoted 5 lines

This patch is an improvement but, thinking again, it looks there is scope getting rid of mpam_enable() altogether. Rather than walking the list after each hw probe we could increment an atomic variable, similar to what is done in mpam_msc_drv_probe(), and then just schedule mpam_enable_once(). What do you think?

Hi Ben,

Seeing another atomic-counter based sequencing mechanism in this patch reminded me of a similar issue I explored while working on MPAM DT support.

At the time, I experimented with removing the fw_num_msc pre-counting logic and moving the discovery callback registration to a late_initcall() stage. The motivation was to avoid separate DT/ACPI counting paths and allow discovery to proceed based on successfully probed MSCs.

However, I eventually dropped that approach because it relied on synchronous probing and would not behave correctly in deferred-probe or future asynchronous-probe scenarios.

That made me curious about the motivation behind this change:
- Is MPAM intentionally designed around the assumption that all
firmware-described MSCs must probe successfully before discovery can proceed?
- Is the count-and-compare model primarily retained to guarantee
correct ordering under deferred/asynchronous probing?

I'm not suggesting changing the implementation, just interested in understanding the design rationale.

Thanks,
Yin

Quoted 42 lines

Thanks,

Ben

Quoted 37 lines

Set new_device_probed only when mpam_msc_hw_probe() is called and succeeds.

---
Changes in v2:
- Drop the Fixes: tag, as the extra mpam_enable() calls cause no real
harm: schedule_work() merges the duplicate work, and mpam_enable() is a no-op until all MSCs have been probed. This is a cleanup, not a bug fix.

  drivers/resctrl/mpam_devices.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/resctrl/mpam_devices.c +4 -2
index 2f09f4b78bd3..fefdcf588932 100644
@@ -1866,13 +1866,15 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
  			continue;
 
  		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
+		if (!msc->probed) {
  			err = mpam_msc_hw_probe(msc);
+			if (!err)
+				new_device_probed = true;
+		}
  		mutex_unlock(&msc->probe_lock);
 
  		if (err)
  			break;
-		new_device_probed = true;
  	}
 
  	if (new_device_probed && !err)
Signature · 2 lines
Thx and BRs,
Yin
maintainer
#4 lore raw

Hi Yin,

Quoted 31 lines · Yin Li
Quoted 13 lines · Ben Horgan

Hi Ping,

Quoted 9 lines · ping.li

From: Ping Li <[email protected]>

mpam_discovery_cpu_online() sets new_device_probed unconditionally after processing each reachable MSC. Once an MSC has already been probed (msc->probed is true), later CPUs sharing it skip mpam_msc_hw_probe() but still leave err at its default value of 0. As a result, new_device_probed is still set to true, causing mpam_enable_work to be scheduled again even though no new hardware was probed.

Quoted 5 lines

This patch is an improvement but, thinking again, it looks there is scope getting rid of mpam_enable() altogether. Rather than walking the list after each hw probe we could increment an atomic variable, similar to what is done in mpam_msc_drv_probe(), and then just schedule mpam_enable_once(). What do you think?

Hi Ben,

Seeing another atomic-counter based sequencing mechanism in this patch reminded me of a similar issue I explored while working on MPAM DT support.

At the time, I experimented with removing the fw_num_msc pre-counting logic and moving the discovery callback registration to a late_initcall() stage. The motivation was to avoid separate DT/ACPI counting paths and allow discovery to proceed based on successfully probed MSCs.

Quoted 6 lines

However, I eventually dropped that approach because it relied on synchronous probing and would not behave correctly in deferred-probe or future asynchronous-probe scenarios.

That made me curious about the motivation behind this change:
  - Is MPAM intentionally designed around the assumption that all firmware-described MSCs must probe successfully before discovery can proceed?

James wrote the code but here's how I see it. The important point of synchronization is for MPAM enabling after the h/w probe rather than before discovery. This allows the number of usable PARTID and PMG to be calculated and allows the ris/comp/class lists to be considered read only after this point (except if MPAM is being disabled). For discovery I expect the MSC still be considered independently. However, the synchronization is convenient at discovery as it allows for cpu hotplug callbacks to do the initialisation, first for all MSC that have online affine CPUs and then as those CPUs come online.

Quoted 2 lines

  - Is the count-and-compare model primarily retained to guarantee correct ordering under deferred/ asynchronous probing?

Deferred probing for the discovery will possibly be required for enabling interrupts with GICv5.

Thanks,

Ben

Quoted 47 lines

I'm not suggesting changing the implementation, just interested in understanding the design rationale.

Thanks,
Yin

Quoted 39 lines

Thanks,

Ben

Quoted 34 lines

Set new_device_probed only when mpam_msc_hw_probe() is called and succeeds.

---
Changes in v2:
- Drop the Fixes: tag, as the extra mpam_enable() calls cause no real
   harm: schedule_work() merges the duplicate work, and mpam_enable() is a no-op until all MSCs have been probed. This is a cleanup, not a bug fix.

  drivers/resctrl/mpam_devices.c | 6 ++++--

  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/resctrl/mpam_devices.c +4 -2
index 2f09f4b78bd3..fefdcf588932 100644
@@ -1866,13 +1866,15 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
               continue;
             mutex_lock(&msc->probe_lock);
-        if (!msc->probed)
+        if (!msc->probed) {
               err = mpam_msc_hw_probe(msc);
+            if (!err)
+                new_device_probed = true;
+        }
           mutex_unlock(&msc->probe_lock);
             if (err)
               break;
-        new_device_probed = true;
       }
         if (new_device_probed && !err)
#5 lore raw
Quoted 41 lines · Ben Horgan

Hi Yin,

Quoted 31 lines · Yin Li
Quoted 13 lines · Ben Horgan

Hi Ping,

Quoted 9 lines · ping.li

From: Ping Li <[email protected]>

mpam_discovery_cpu_online() sets new_device_probed unconditionally after processing each reachable MSC. Once an MSC has already been probed (msc->probed is true), later CPUs sharing it skip mpam_msc_hw_probe() but still leave err at its default value of 0. As a result, new_device_probed is still set to true, causing mpam_enable_work to be scheduled again even though no new hardware was probed.

Quoted 5 lines

This patch is an improvement but, thinking again, it looks there is scope getting rid of mpam_enable() altogether. Rather than walking the list after each hw probe we could increment an atomic variable, similar to what is done in mpam_msc_drv_probe(), and then just schedule mpam_enable_once(). What do you think?

Hi Ben,

Seeing another atomic-counter based sequencing mechanism in this patch reminded me of a similar issue I explored while working on MPAM DT support.

At the time, I experimented with removing the fw_num_msc pre-counting logic and moving the discovery callback registration to a late_initcall() stage. The motivation was to avoid separate DT/ACPI counting paths and allow discovery to proceed based on successfully probed MSCs.

Quoted 6 lines

However, I eventually dropped that approach because it relied on synchronous probing and would not behave correctly in deferred-probe or future asynchronous-probe scenarios.

That made me curious about the motivation behind this change:
  - Is MPAM intentionally designed around the assumption that all firmware-described MSCs must probe
successfully before discovery can proceed?

Quoted 8 lines

James wrote the code but here's how I see it. The important point of synchronization is for MPAM enabling after the h/w probe rather than before discovery. This allows the number of usable PARTID and PMG to be calculated and allows the ris/comp/class lists to be considered read only after this point (except if MPAM is being disabled). For discovery I expect the MSC still be considered independently. However, the synchronization is convenient at discovery as it allows for cpu hotplug callbacks to do the initialisation, first for all MSC that have online affine CPUs and then as those CPUs come online.

Hi Ben,

Thanks for the explanation.

Agreed that MSC enabling is handled independently.I understand that the "(atomic_add_return(1, &mpam_num_msc) == fw_num_msc)" condition as an ordering guarantee: it ensures mpam_all_msc is complete before the cpuhp callback is registered, so no MSC is missed when the callback
walks the MSC list.

CPU onlining and MSC probing are two independent sequences. The cpuhp mechanism handles the case where a CPU comes online after an MSC is probed. But it cannot handle the reverse: an MSC that completes probing after cpuhp has already executed will be missed.

Quoted 5 lines
Quoted 2 lines

  - Is the count-and-compare model primarily retained to guarantee correct ordering under deferred/
asynchronous probing?

Deferred probing for the discovery will possibly be required for enabling interrupts with GICv5.

Deferred probing for GICv5 would indeed require the count-and-compare model to ensure all MSCs have probed before the cpuhp callback is registered. Thanks for the clarification.

Thanks,
Yin

Quoted 52 lines

Thanks,

Ben

Quoted 47 lines

I'm not suggesting changing the implementation, just interested in understanding the design rationale.

Thanks,
Yin

Quoted 39 lines

Thanks,

Ben

Quoted 34 lines

Set new_device_probed only when mpam_msc_hw_probe() is called and succeeds.

---
Changes in v2:
- Drop the Fixes: tag, as the extra mpam_enable() calls cause no real
   harm: schedule_work() merges the duplicate work, and mpam_enable() is a no-op until all MSCs have been probed. This is a cleanup, not a bug fix.

   drivers/resctrl/mpam_devices.c | 6 ++++--
   1 file changed, 4 insertions(+), 2 deletions(-)
drivers/resctrl/mpam_devices.c +4 -2
index 2f09f4b78bd3..fefdcf588932 100644
@@ -1866,13 +1866,15 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
               continue;
             mutex_lock(&msc->probe_lock);
-        if (!msc->probed)
+        if (!msc->probed) {
               err = mpam_msc_hw_probe(msc);
+            if (!err)
+                new_device_probed = true;
+        }
           mutex_unlock(&msc->probe_lock);
             if (err)
               break;
-        new_device_probed = true;
       }
         if (new_device_probed && !err)
Signature · 2 lines
Thx and BRs,
Yin
author
#6 lore raw

PATCH v3 1/1 arm_mpam: Use an atomic counter to schedule mpam_enable_once()

In reply to ping.li

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches mpam_num_msc, the number of MSCs the driver discovered. MSCs that were already probed no longer cause work to be scheduled.

---
Changes in v3:
- Rework per review feedback: instead of scheduling mpam_enable() after each probe and walking the MSC list to check whether all MSCs have been probed, count the probed MSCs and schedule mpam_enable_once() directly when the last one is probed.

 drivers/resctrl/mpam_devices.c  | 47 +++++++++------------------------
 drivers/resctrl/mpam_internal.h |  1 -
 2 files changed, 12 insertions(+), 36 deletions(-)
drivers/resctrl/mpam_devices.c +12 -35
index 2f09f4b78bd3..c6000bc2157a 100644
@@ -55,6 +55,11 @@ struct srcu_struct mpam_srcu;
  */
 static atomic_t mpam_num_msc;
 
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
@@ -72,7 +77,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
  * was not brought online at boot, this can happen surprisingly late.
  */
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
 
 /*
  * All mpam error interrupts indicate a software bug. On receipt, disable the
@@ -1854,7 +1859,6 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 {
 	int err = 0;
 	struct mpam_msc *msc;
-	bool new_device_probed = false;
 
 	if (mpam_is_enabled())
 		return 0;
@@ -1866,17 +1870,18 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 			continue;
 
 		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
+		if (!msc->probed) {
 			err = mpam_msc_hw_probe(msc);
+			if (!err && atomic_add_return(1, &mpam_num_msc_probed) ==
+			    atomic_read(&mpam_num_msc))
+				schedule_work(&mpam_enable_work);
+		}
 		mutex_unlock(&msc->probe_lock);
 
 		if (err)
 			break;
-		new_device_probed = true;
 	}
 
-	if (new_device_probed && !err)
-		schedule_work(&mpam_enable_work);
 	if (err) {
 		mpam_disable_reason = "error during probing";
 		schedule_work(&mpam_broken_work);
@@ -2713,7 +2718,7 @@ static int mpam_allocate_config(void)
 	return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
 	int err;
 
@@ -2867,34 +2872,6 @@ void mpam_disable(struct work_struct *ignored)
 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */
-void mpam_enable(struct work_struct *work)
-{
-	static atomic_t once;
-	struct mpam_msc *msc;
-	bool all_devices_probed = true;
-
-	/* Have we probed all the hw devices? */
-	guard(srcu)(&mpam_srcu);
-	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-				 srcu_read_lock_held(&mpam_srcu)) {
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			all_devices_probed = false;
-		mutex_unlock(&msc->probe_lock);
-
-		if (!all_devices_probed)
-			break;
-	}
-
-	if (all_devices_probed && !atomic_fetch_inc(&once))
-		mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
 	if (mpam_has_feature(feature, newcfg) &&			\
 	    (newcfg)->member != (cfg)->member) {			\
drivers/resctrl/mpam_internal.h +0 -1
index 04d1a59f02af..72bf296c846e 100644
@@ -453,7 +453,6 @@ extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
 /* Scheduled work callback to enable mpam once all MSC have been probed */
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH v3] arm_mpam: Use an atomic counter to schedule mpam_enable_once()

Hi Ping,

Quoted 10 lines · Ping Li

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches mpam_num_msc, the number of MSCs the driver discovered. MSCs that were already probed no longer cause work to be scheduled.

Thanks for implementing the suggestion :)

Quoted 46 lines

---
Changes in v3:
- Rework per review feedback: instead of scheduling mpam_enable() after each probe and walking the MSC list to check whether all MSCs have been probed, count the probed MSCs and schedule mpam_enable_once() directly when the last one is probed.

 drivers/resctrl/mpam_devices.c  | 47 +++++++++------------------------
 drivers/resctrl/mpam_internal.h |  1 -
 2 files changed, 12 insertions(+), 36 deletions(-)
drivers/resctrl/mpam_devices.c +6 -2
index 2f09f4b78bd3..c6000bc2157a 100644
@@ -55,6 +55,11 @@ struct srcu_struct mpam_srcu;
  */
 static atomic_t mpam_num_msc;
 
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
@@ -72,7 +77,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
  * was not brought online at boot, this can happen surprisingly late.
  */
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
 
 /*
  * All mpam error interrupts indicate a software bug. On receipt, disable the
@@ -1854,7 +1859,6 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 {
 	int err = 0;
 	struct mpam_msc *msc;
-	bool new_device_probed = false;
 
 	if (mpam_is_enabled())
 		return 0;

drivers/resctrl/mpam_devices.c static int mpam_discovery_cpu_online(unsigned int cpu) · line 1872

Quoted 3 lines
drivers/resctrl/mpam_devices.c +0 -0
@@ -1866,17 +1870,18 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 			continue;
 
 		mutex_lock(&msc->probe_lock);

If we switch to use guard(mutex)(&msc->probe_lock) then we can get rid of the nested ifs. This allows us to...

drivers/resctrl/mpam_devices.c static int mpam_discovery_cpu_online(unsigned int cpu) · line 1869

Quoted 1 line
drivers/resctrl/mpam_devices.c +0 -1
-		if (!msc->probed)

... 'continue' if already probed

drivers/resctrl/mpam_devices.c static int mpam_discovery_cpu_online(unsigned int cpu) · line 1874

Quoted 2 lines
drivers/resctrl/mpam_devices.c +1 -0
+		if (!msc->probed) {
 			err = mpam_msc_hw_probe(msc);

... return early on error

drivers/resctrl/mpam_devices.c static int mpam_discovery_cpu_online(unsigned int cpu) · line 1876

Quoted 2 lines
drivers/resctrl/mpam_devices.c +2 -0
+			if (!err && atomic_add_return(1, &mpam_num_msc_probed) ==
+			    atomic_read(&mpam_num_msc))

... and remove the !err from this check.

Instead of mpam_num_msc check against fw_num_msc so that we don't need to take into account extra calls to mpam_msc_drv_probe() due to unbind/bind potentially incrementing mpam_num_msc past fw_num_msc. As we disable bind/unbind by setting 'suppress_bind_attrs' this can't actually occur but I think using fw_num_msc is more robust.

Quoted 63 lines
drivers/resctrl/mpam_devices.c +3 -32
+				schedule_work(&mpam_enable_work);
+		}
 		mutex_unlock(&msc->probe_lock);
 
 		if (err)
 			break;
-		new_device_probed = true;
 	}
 
-	if (new_device_probed && !err)
-		schedule_work(&mpam_enable_work);
 	if (err) {
 		mpam_disable_reason = "error during probing";
 		schedule_work(&mpam_broken_work);
@@ -2713,7 +2718,7 @@ static int mpam_allocate_config(void)
 	return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
 	int err;
 
@@ -2867,34 +2872,6 @@ void mpam_disable(struct work_struct *ignored)
 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */
-void mpam_enable(struct work_struct *work)
-{
-	static atomic_t once;
-	struct mpam_msc *msc;
-	bool all_devices_probed = true;
-
-	/* Have we probed all the hw devices? */
-	guard(srcu)(&mpam_srcu);
-	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-				 srcu_read_lock_held(&mpam_srcu)) {
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			all_devices_probed = false;
-		mutex_unlock(&msc->probe_lock);
-
-		if (!all_devices_probed)
-			break;
-	}
-
-	if (all_devices_probed && !atomic_fetch_inc(&once))
-		mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
 	if (mpam_has_feature(feature, newcfg) &&			\
 	    (newcfg)->member != (cfg)->member) {			\
drivers/resctrl/mpam_internal.h +0 -0
index 04d1a59f02af..72bf296c846e 100644

drivers/resctrl/mpam_internal.h extern u16 mpam_partid_max; · line 455

Quoted 3 lines
drivers/resctrl/mpam_internal.h +0 -0
@@ -453,7 +453,6 @@ extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
 /* Scheduled work callback to enable mpam once all MSC have been probed */

This comment should be removed along with the declaration.

Thanks,

Ben

Quoted 4 lines
drivers/resctrl/mpam_internal.h +0 -1
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
author
#8 lore raw

PATCH v4 1/1 arm_mpam: Use an atomic counter to schedule mpam_enable_once()

In reply to Ping Li

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches fw_num_msc, the number of MSCs reported by firmware. MSCs that were already probed no longer cause work to be scheduled.

---
Changes in v4:
- Use guard(mutex) for the probe lock and continue/break to flatten the nesting, per review feedback.
- Compare the probed count against fw_num_msc instead of mpam_num_msc, which is more robust against unbind/bind incrementing mpam_num_msc past fw_num_msc.
- Remove the stale comment for the removed mpam_enable() declaration.

 drivers/resctrl/mpam_devices.c  | 57 ++++++++++-----------------------
 drivers/resctrl/mpam_internal.h |  2 --
 2 files changed, 17 insertions(+), 42 deletions(-)
drivers/resctrl/mpam_devices.c +17 -40
index 2f09f4b78bd3..adfb734755e6 100644
@@ -49,12 +49,20 @@ static LIST_HEAD(mpam_all_msc);
 
 struct srcu_struct mpam_srcu;
 
+/* Number of MSCs reported by firmware */
+static int fw_num_msc;
+
 /*
  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
  * can be enabled.
  */
 static atomic_t mpam_num_msc;
 
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
@@ -72,7 +80,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
  * was not brought online at boot, this can happen surprisingly late.
  */
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
 
 /*
  * All mpam error interrupts indicate a software bug. On receipt, disable the
@@ -1854,7 +1862,6 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 {
 	int err = 0;
 	struct mpam_msc *msc;
-	bool new_device_probed = false;
 
 	if (mpam_is_enabled())
 		return 0;
@@ -1865,18 +1872,18 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
 			continue;
 
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			err = mpam_msc_hw_probe(msc);
-		mutex_unlock(&msc->probe_lock);
+		guard(mutex)(&msc->probe_lock);
+		if (msc->probed)
+			continue;
 
+		err = mpam_msc_hw_probe(msc);
 		if (err)
 			break;
-		new_device_probed = true;
+
+		if (atomic_add_return(1, &mpam_num_msc_probed) == fw_num_msc)
+			schedule_work(&mpam_enable_work);
 	}
 
-	if (new_device_probed && !err)
-		schedule_work(&mpam_enable_work);
 	if (err) {
 		mpam_disable_reason = "error during probing";
 		schedule_work(&mpam_broken_work);
@@ -2106,8 +2113,6 @@ static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
 	return msc;
 }
 
-static int fw_num_msc;
-
 static int mpam_msc_drv_probe(struct platform_device *pdev)
 {
 	int err;
@@ -2713,7 +2718,7 @@ static int mpam_allocate_config(void)
 	return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
 	int err;
 
@@ -2867,34 +2872,6 @@ void mpam_disable(struct work_struct *ignored)
 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */
-void mpam_enable(struct work_struct *work)
-{
-	static atomic_t once;
-	struct mpam_msc *msc;
-	bool all_devices_probed = true;
-
-	/* Have we probed all the hw devices? */
-	guard(srcu)(&mpam_srcu);
-	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-				 srcu_read_lock_held(&mpam_srcu)) {
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			all_devices_probed = false;
-		mutex_unlock(&msc->probe_lock);
-
-		if (!all_devices_probed)
-			break;
-	}
-
-	if (all_devices_probed && !atomic_fetch_inc(&once))
-		mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
 	if (mpam_has_feature(feature, newcfg) &&			\
 	    (newcfg)->member != (cfg)->member) {			\
drivers/resctrl/mpam_internal.h +0 -2
index 04d1a59f02af..085f086e2b2d 100644
@@ -452,8 +452,6 @@ extern struct list_head mpam_classes;
 extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
-/* Scheduled work callback to enable mpam once all MSC have been probed */
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
Signature · 1 line
2.34.1
maintainer
#9 lore raw

Re: [PATCH v4] arm_mpam: Use an atomic counter to schedule mpam_enable_once()

Hi Ping,

Quoted 71 lines · Ping Li

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches fw_num_msc, the number of MSCs reported by firmware. MSCs that were already probed no longer cause work to be scheduled.

---
Changes in v4:
- Use guard(mutex) for the probe lock and continue/break to flatten the nesting, per review feedback.
- Compare the probed count against fw_num_msc instead of mpam_num_msc, which is more robust against unbind/bind incrementing mpam_num_msc past fw_num_msc.
- Remove the stale comment for the removed mpam_enable() declaration.

 drivers/resctrl/mpam_devices.c  | 57 ++++++++++-----------------------
 drivers/resctrl/mpam_internal.h |  2 --
 2 files changed, 17 insertions(+), 42 deletions(-)
drivers/resctrl/mpam_devices.c +9 -3
index 2f09f4b78bd3..adfb734755e6 100644
@@ -49,12 +49,20 @@ static LIST_HEAD(mpam_all_msc);
 
 struct srcu_struct mpam_srcu;
 
+/* Number of MSCs reported by firmware */
+static int fw_num_msc;
+
 /*
  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
  * can be enabled.
  */
 static atomic_t mpam_num_msc;
 
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
@@ -72,7 +80,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
  * was not brought online at boot, this can happen surprisingly late.
  */
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
 
 /*
  * All mpam error interrupts indicate a software bug. On receipt, disable the
@@ -1854,7 +1862,6 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 {
 	int err = 0;
 	struct mpam_msc *msc;
-	bool new_device_probed = false;
 
 	if (mpam_is_enabled())
 		return 0;
@@ -1865,18 +1872,18 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
 			continue;
 
-		mutex_lock(&msc->probe_lock);

drivers/resctrl/mpam_devices.c static int mpam_discovery_cpu_online(unsigned int cpu) · line 1881

Quoted 10 lines
drivers/resctrl/mpam_devices.c +4 -3
-		if (!msc->probed)
-			err = mpam_msc_hw_probe(msc);
-		mutex_unlock(&msc->probe_lock);
+		guard(mutex)(&msc->probe_lock);
+		if (msc->probed)
+			continue;
 
+		err = mpam_msc_hw_probe(msc);
 		if (err)
 			break;

Can we schedule the mpam_broken_work here to avoid having the same if condition twice?

Thanks,

Ben

Quoted 77 lines
drivers/resctrl/mpam_devices.c +4 -34
-		new_device_probed = true;
+
+		if (atomic_add_return(1, &mpam_num_msc_probed) == fw_num_msc)
+			schedule_work(&mpam_enable_work);
 	}
 
-	if (new_device_probed && !err)
-		schedule_work(&mpam_enable_work);
 	if (err) {
 		mpam_disable_reason = "error during probing";
 		schedule_work(&mpam_broken_work);
@@ -2106,8 +2113,6 @@ static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
 	return msc;
 }
 
-static int fw_num_msc;
-
 static int mpam_msc_drv_probe(struct platform_device *pdev)
 {
 	int err;
@@ -2713,7 +2718,7 @@ static int mpam_allocate_config(void)
 	return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
 	int err;
 
@@ -2867,34 +2872,6 @@ void mpam_disable(struct work_struct *ignored)
 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */
-void mpam_enable(struct work_struct *work)
-{
-	static atomic_t once;
-	struct mpam_msc *msc;
-	bool all_devices_probed = true;
-
-	/* Have we probed all the hw devices? */
-	guard(srcu)(&mpam_srcu);
-	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-				 srcu_read_lock_held(&mpam_srcu)) {
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			all_devices_probed = false;
-		mutex_unlock(&msc->probe_lock);
-
-		if (!all_devices_probed)
-			break;
-	}
-
-	if (all_devices_probed && !atomic_fetch_inc(&once))
-		mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
 	if (mpam_has_feature(feature, newcfg) &&			\
 	    (newcfg)->member != (cfg)->member) {			\
drivers/resctrl/mpam_internal.h +0 -2
index 04d1a59f02af..085f086e2b2d 100644
@@ -452,8 +452,6 @@ extern struct list_head mpam_classes;
 extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
-/* Scheduled work callback to enable mpam once all MSC have been probed */
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
author
#10 lore raw

PATCH v5 1/1 arm_mpam: Use an atomic counter to schedule mpam_enable_once()

In reply to Ping Li

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches fw_num_msc, the number of MSCs reported by firmware. MSCs that were already probed no longer cause work to be scheduled.

---
Changes in v5:
- Schedule mpam_broken_work at the error site and return early, removing the duplicate err check at the end of the function.

 drivers/resctrl/mpam_devices.c  | 69 +++++++++++----------------------
 drivers/resctrl/mpam_internal.h |  2 -
 2 files changed, 22 insertions(+), 49 deletions(-)
drivers/resctrl/mpam_devices.c +22 -47
index 2f09f4b78bd3..4fc82acd854e 100644
@@ -49,12 +49,20 @@ static LIST_HEAD(mpam_all_msc);
 
 struct srcu_struct mpam_srcu;
 
+/* Number of MSCs reported by firmware */
+static int fw_num_msc;
+
 /*
  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
  * can be enabled.
  */
 static atomic_t mpam_num_msc;
 
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
@@ -72,7 +80,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
  * was not brought online at boot, this can happen surprisingly late.
  */
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
 
 /*
  * All mpam error interrupts indicate a software bug. On receipt, disable the
@@ -1854,7 +1862,6 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 {
 	int err = 0;
 	struct mpam_msc *msc;
-	bool new_device_probed = false;
 
 	if (mpam_is_enabled())
 		return 0;
@@ -1865,24 +1872,22 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
 			continue;
 
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			err = mpam_msc_hw_probe(msc);
-		mutex_unlock(&msc->probe_lock);
+		guard(mutex)(&msc->probe_lock);
+		if (msc->probed)
+			continue;
 
-		if (err)
-			break;
-		new_device_probed = true;
-	}
+		err = mpam_msc_hw_probe(msc);
+		if (err) {
+			mpam_disable_reason = "error during probing";
+			schedule_work(&mpam_broken_work);
+			return err;
+		}
 
-	if (new_device_probed && !err)
-		schedule_work(&mpam_enable_work);
-	if (err) {
-		mpam_disable_reason = "error during probing";
-		schedule_work(&mpam_broken_work);
+		if (atomic_add_return(1, &mpam_num_msc_probed) == fw_num_msc)
+			schedule_work(&mpam_enable_work);
 	}
 
-	return err;
+	return 0;
 }
 
 static int mpam_cpu_offline(unsigned int cpu)
@@ -2106,8 +2111,6 @@ static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
 	return msc;
 }
 
-static int fw_num_msc;
-
 static int mpam_msc_drv_probe(struct platform_device *pdev)
 {
 	int err;
@@ -2713,7 +2716,7 @@ static int mpam_allocate_config(void)
 	return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
 	int err;
 
@@ -2867,34 +2870,6 @@ void mpam_disable(struct work_struct *ignored)
 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */
-void mpam_enable(struct work_struct *work)
-{
-	static atomic_t once;
-	struct mpam_msc *msc;
-	bool all_devices_probed = true;
-
-	/* Have we probed all the hw devices? */
-	guard(srcu)(&mpam_srcu);
-	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-				 srcu_read_lock_held(&mpam_srcu)) {
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			all_devices_probed = false;
-		mutex_unlock(&msc->probe_lock);
-
-		if (!all_devices_probed)
-			break;
-	}
-
-	if (all_devices_probed && !atomic_fetch_inc(&once))
-		mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
 	if (mpam_has_feature(feature, newcfg) &&			\
 	    (newcfg)->member != (cfg)->member) {			\
drivers/resctrl/mpam_internal.h +0 -2
index 04d1a59f02af..085f086e2b2d 100644
@@ -452,8 +452,6 @@ extern struct list_head mpam_classes;
 extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
-/* Scheduled work callback to enable mpam once all MSC have been probed */
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): CAKpSQZD2hEuAF9CDD7o9F6v6j-s3sDEwVPtEk9_QLBFjk=nqvA@mail.gmail.com

maintainer Reviewed-by
#12 lore raw

Re: [PATCH v5] arm_mpam: Use an atomic counter to schedule mpam_enable_once()

Hi Ping,

Quoted 6 lines · Ping Li

Hi Ben,
Gentle ping on this patch. It would be good to know if there is anything else you would like changed.
Thanks,
Ping

Quoted 13 lines
Quoted 11 lines · Ping Li <[email protected]> 于2026年9月21日周一 19:50写道:

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches fw_num_msc, the number of MSCs reported by firmware. MSCs that were already probed no longer cause work to be scheduled.

It's a bit late for 7.4 now but I should be able to take this next cycle.

Thanks,

Ben

Quoted 44 lines
Quoted 44 lines

---
Changes in v5:
- Schedule mpam_broken_work at the error site and return early, removing the duplicate err check at the end of the function.

 drivers/resctrl/mpam_devices.c  | 69 +++++++++++----------------------
 drivers/resctrl/mpam_internal.h |  2 -
 2 files changed, 22 insertions(+), 49 deletions(-)
drivers/resctrl/mpam_devices.c +3 -0
index 2f09f4b78bd3..4fc82acd854e 100644
@@ -49,12 +49,20 @@ static LIST_HEAD(mpam_all_msc);
 
 struct srcu_struct mpam_srcu;
 
+/* Number of MSCs reported by firmware */
+static int fw_num_msc;
+
 /*
  * Number of MSCs that have been probed. Once all MSCs have been probed

MPAM
* can be enabled.
*/
static atomic_t mpam_num_msc;

drivers/resctrl/mpam_devices.c +5 -0
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
@@ -72,7 +80,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU

that
* was not brought online at boot, this can happen surprisingly late. */

drivers/resctrl/mpam_devices.c +1 -1
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
Quoted 7 lines

drivers/resctrl/mpam_devices.c static int mpam_discovery_cpu_online(unsigned int · line 1864

Quoted 7 lines

/*
* All mpam error interrupts indicate a software bug. On receipt, disable
the

drivers/resctrl/mpam_devices.c +0 -0
@@ -1854,7 +1862,6 @@ static int mpam_discovery_cpu_online(unsigned int
 cpu)
 {
        int err = 0;

nit: don't need the ' = 0' anymore.

Quoted 118 lines
Quoted 117 lines
drivers/resctrl/mpam_devices.c +0 -1
        struct mpam_msc *msc;
-       bool new_device_probed = false;
 
        if (mpam_is_enabled())
                return 0;
drivers/resctrl/mpam_devices.c +12 -14
@@ -1865,24 +1872,22 @@ static int mpam_discovery_cpu_online(unsigned int
 cpu)
                if (!cpumask_test_cpu(cpu, &msc->accessibility))
                        continue;
 
-               mutex_lock(&msc->probe_lock);
-               if (!msc->probed)
-                       err = mpam_msc_hw_probe(msc);
-               mutex_unlock(&msc->probe_lock);
+               guard(mutex)(&msc->probe_lock);
+               if (msc->probed)
+                       continue;
 
-               if (err)
-                       break;
-               new_device_probed = true;
-       }
+               err = mpam_msc_hw_probe(msc);
+               if (err) {
+                       mpam_disable_reason = "error during probing";
+                       schedule_work(&mpam_broken_work);
+                       return err;
+               }
 
-       if (new_device_probed && !err)
-               schedule_work(&mpam_enable_work);
-       if (err) {
-               mpam_disable_reason = "error during probing";
-               schedule_work(&mpam_broken_work);
+               if (atomic_add_return(1, &mpam_num_msc_probed) ==
 fw_num_msc)
+                       schedule_work(&mpam_enable_work);
        }
 
-       return err;
+       return 0;
 }

static int mpam_cpu_offline(unsigned int cpu)

drivers/resctrl/mpam_devices.c +0 -2
@@ -2106,8 +2111,6 @@ static struct mpam_msc *do_mpam_msc_drv_probe(struct
 platform_device *pdev)
        return msc;
 }
 
-static int fw_num_msc;
-
 static int mpam_msc_drv_probe(struct platform_device *pdev)
 {
        int err;
drivers/resctrl/mpam_devices.c +1 -4
@@ -2713,7 +2716,7 @@ static int mpam_allocate_config(void)
        return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
        int err;
 
@@ -2867,34 +2870,6 @@ void mpam_disable(struct work_struct *ignored)
        pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been

created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */

drivers/resctrl/mpam_devices.c +0 -23
-void mpam_enable(struct work_struct *work)
-{
-       static atomic_t once;
-       struct mpam_msc *msc;
-       bool all_devices_probed = true;
-
-       /* Have we probed all the hw devices? */
-       guard(srcu)(&mpam_srcu);
-       list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-                                srcu_read_lock_held(&mpam_srcu)) {
-               mutex_lock(&msc->probe_lock);
-               if (!msc->probed)
-                       all_devices_probed = false;
-               mutex_unlock(&msc->probe_lock);
-
-               if (!all_devices_probed)
-                       break;
-       }
-
-       if (all_devices_probed && !atomic_fetch_inc(&once))
-               mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
        if (mpam_has_feature(feature, newcfg) &&                        \
            (newcfg)->member != (cfg)->member) {                        \
drivers/resctrl/mpam_internal.h +0 -3
index 04d1a59f02af..085f086e2b2d 100644
@@ -452,8 +452,6 @@ extern struct list_head mpam_classes;
 extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
-/* Scheduled work callback to enable mpam once all MSC have been probed */
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
--

2.34.1

author
#13 lore raw

PATCH v6 1/1 arm_mpam: Use an atomic counter to schedule mpam_enable_once()

mpam_discovery_cpu_online() scheduled mpam_enable() after each MSC probe, and mpam_enable() walked the whole MSC list taking each probe lock in turn to check if all the MSCs had been probed.

Count the MSCs that have had their hardware probed with mpam_num_msc_probed, and schedule mpam_enable_once() directly when the count reaches fw_num_msc, the number of MSCs reported by firmware. MSCs that were already probed no longer cause work to be scheduled.

---
Changes in v6:
- Drop the redundant initialisation of err, per review feedback.

 drivers/resctrl/mpam_devices.c  | 71 +++++++++++----------------------
 drivers/resctrl/mpam_internal.h |  2 -
 2 files changed, 23 insertions(+), 50 deletions(-)
drivers/resctrl/mpam_devices.c +23 -48
index dd422c56fbb1..0d0803146f24 100644
@@ -49,12 +49,20 @@ static LIST_HEAD(mpam_all_msc);
 
 struct srcu_struct mpam_srcu;
 
+/* Number of MSCs reported by firmware */
+static int fw_num_msc;
+
 /*
  * Number of MSCs that have been probed. Once all MSCs have been probed MPAM
  * can be enabled.
  */
 static atomic_t mpam_num_msc;
 
+/* Number of MSCs that have had their hardware probed */
+static atomic_t mpam_num_msc_probed;
+
+static void mpam_enable_once(struct work_struct *work);
+
 static int mpam_cpuhp_state;
 static DEFINE_MUTEX(mpam_cpuhp_state_lock);
 
@@ -72,7 +80,7 @@ static DEFINE_SPINLOCK(partid_max_lock);
  * scheduled via this work_struct. If access to an MSC depends on a CPU that
  * was not brought online at boot, this can happen surprisingly late.
  */
-static DECLARE_WORK(mpam_enable_work, &mpam_enable);
+static DECLARE_WORK(mpam_enable_work, &mpam_enable_once);
 
 /*
  * All mpam error interrupts indicate a software bug. On receipt, disable the
@@ -1850,9 +1858,8 @@ static int mpam_cpu_online(unsigned int cpu)
 /* Before mpam is enabled, try to probe new MSC */
 static int mpam_discovery_cpu_online(unsigned int cpu)
 {
-	int err = 0;
+	int err;
 	struct mpam_msc *msc;
-	bool new_device_probed = false;
 
 	if (mpam_is_enabled())
 		return 0;
@@ -1863,24 +1870,22 @@ static int mpam_discovery_cpu_online(unsigned int cpu)
 		if (!cpumask_test_cpu(cpu, &msc->accessibility))
 			continue;
 
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			err = mpam_msc_hw_probe(msc);
-		mutex_unlock(&msc->probe_lock);
+		guard(mutex)(&msc->probe_lock);
+		if (msc->probed)
+			continue;
 
-		if (err)
-			break;
-		new_device_probed = true;
-	}
+		err = mpam_msc_hw_probe(msc);
+		if (err) {
+			mpam_disable_reason = "error during probing";
+			schedule_work(&mpam_broken_work);
+			return err;
+		}
 
-	if (new_device_probed && !err)
-		schedule_work(&mpam_enable_work);
-	if (err) {
-		mpam_disable_reason = "error during probing";
-		schedule_work(&mpam_broken_work);
+		if (atomic_add_return(1, &mpam_num_msc_probed) == fw_num_msc)
+			schedule_work(&mpam_enable_work);
 	}
 
-	return err;
+	return 0;
 }
 
 static int mpam_cpu_offline(unsigned int cpu)
@@ -2107,8 +2112,6 @@ static struct mpam_msc *do_mpam_msc_drv_probe(struct platform_device *pdev)
 	return msc;
 }
 
-static int fw_num_msc;
-
 static int mpam_msc_drv_probe(struct platform_device *pdev)
 {
 	int err;
@@ -2715,7 +2718,7 @@ static int mpam_allocate_config(void)
 	return 0;
 }
 
-static void mpam_enable_once(void)
+static void mpam_enable_once(struct work_struct *work)
 {
 	int err;
 
@@ -2869,34 +2872,6 @@ void mpam_disable(struct work_struct *ignored)
 	pr_err_once("MPAM disabled due to %s\n", mpam_disable_reason);
 }
 
-/*
- * Enable mpam once all devices have been probed.
- * Scheduled by mpam_discovery_cpu_online() once all devices have been created.
- * Also scheduled when new devices are probed when new CPUs come online.
- */
-void mpam_enable(struct work_struct *work)
-{
-	static atomic_t once;
-	struct mpam_msc *msc;
-	bool all_devices_probed = true;
-
-	/* Have we probed all the hw devices? */
-	guard(srcu)(&mpam_srcu);
-	list_for_each_entry_srcu(msc, &mpam_all_msc, all_msc_list,
-				 srcu_read_lock_held(&mpam_srcu)) {
-		mutex_lock(&msc->probe_lock);
-		if (!msc->probed)
-			all_devices_probed = false;
-		mutex_unlock(&msc->probe_lock);
-
-		if (!all_devices_probed)
-			break;
-	}
-
-	if (all_devices_probed && !atomic_fetch_inc(&once))
-		mpam_enable_once();
-}
-
 #define maybe_update_config(cfg, feature, newcfg, member, changes) do { \
 	if (mpam_has_feature(feature, newcfg) &&			\
 	    (newcfg)->member != (cfg)->member) {			\
drivers/resctrl/mpam_internal.h +0 -2
index def0e3a65c23..021dedb9eab7 100644
@@ -456,8 +456,6 @@ extern struct list_head mpam_classes;
 extern u16 mpam_partid_max;
 extern u8 mpam_pmg_max;
 
-/* Scheduled work callback to enable mpam once all MSC have been probed */
-void mpam_enable(struct work_struct *work);
 void mpam_disable(struct work_struct *work);
 
 /* Reset all the RIS in a class under cpus_read_lock() */
Signature · 1 line
2.34.1