Re: [PATCH RFC] arm_mpam: Save and restore the MSC state across system sleep
From: Jinjie Ruan
Date: Tue Nov 24 2026 - 20:59:20 EST
在 2026/11/25 1:13, Ben Horgan 写道:
> Hi Jinjie,
Hi Ben,
>
> Thanks for the patch and looking into this.
>
> On 24/11/2026 08:27, Jinjie Ruan wrote:
>> The MPAM driver reprograms an MSC's configuration when the first CPU
>> that can access it comes online, and resets it, saving the bandwidth
>> monitor state, when the last one goes offline.
>>
>> The CPU that suspends the system stays online, so the MSCs it can
>> access, such as the L3 and memory side MSCs, are never seen going
>> offline by these cpuhp callbacks. An MSC can still lose its registers
>> while the system sleeps, for example when its power domain is not
>> retained across suspend-to-RAM. resctrl then reports the cached
>> configuration while the hardware is back at its defaults, and the
>> bandwidth monitor counters restart from zero, losing the traffic
>> counted before the sleep.
>>
>> Save the state from the driver's system sleep callback, which runs
>> while the MSC is still accessible, and reprogram it from the matching
>> resume callback. The cpuhp callbacks then leave the MSC alone: a second
>> save when its last CPU goes offline would find the monitors already
>> disabled by the first save, and lose the monitor configuration from
>> the saved state.
>>
>> Cc: James Morse <james.morse@xxxxxxx>
>> Cc: Ben Horgan <ben.horgan@xxxxxxx>
>> Cc: Reinette Chatre <reinette.chatre@xxxxxxxxx>
>> Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
>> ---
>> drivers/resctrl/mpam_devices.c | 101 ++++++++++++++++++++++++--------
>> drivers/resctrl/mpam_internal.h | 1 +
>> 2 files changed, 78 insertions(+), 24 deletions(-)
>>
>> diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c
>> index c067b6d57d71..49ace93280a7 100644
>> --- a/drivers/resctrl/mpam_devices.c
>> +++ b/drivers/resctrl/mpam_devices.c
>> @@ -23,6 +23,7 @@
>> #include <linux/mailbox_client.h>
>> #include <linux/mutex.h>
>> #include <linux/platform_device.h>
>> +#include <linux/pm.h>
>> #include <linux/printk.h>
>> #include <linux/property.h>
>> #include <linux/srcu.h>
>> @@ -2223,6 +2224,37 @@ static void _enable_percpu_irq(void *_irq)
>> enable_percpu_irq(*irq, IRQ_TYPE_NONE);
>> }
>>
>> +static void mpam_msc_power_on(struct mpam_msc *msc)
>> +{
>> + mutex_lock(&msc->error_irq_lock);
>> + if (msc->error_irq_hw_enabled)
>> + mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
>> + mutex_unlock(&msc->error_irq_lock);
>> +
>> + mpam_reprogram_msc(msc);
>> +}
>> +
>> +static void mpam_msc_power_off(struct mpam_msc *msc)
>> +{
>> + struct mpam_msc_ris *ris;
>> +
>> + mutex_lock(&msc->cfg_lock);
>> + list_for_each_entry_srcu(ris, &msc->ris, msc_list,
>> + srcu_read_lock_held(&mpam_srcu)) {
>> + mpam_touch_msc(msc, &mpam_reset_ris, ris);
>> +
>> + /*
>> + * The reset state for non-zero partid may be
>> + * lost while the CPUs are offline.
>> + */
>> + ris->in_reset_state = false;
>> +
>> + if (mpam_is_enabled())
>> + mpam_touch_msc(msc, &mpam_save_mbwu_state, ris);
>> + }
>> + mutex_unlock(&msc->cfg_lock);
>> +}
>> +
>> static int mpam_cpu_online(unsigned int cpu)
>> {
>> struct mpam_msc *msc;
>> @@ -2236,14 +2268,8 @@ static int mpam_cpu_online(unsigned int cpu)
>> if (msc->reenable_error_ppi)
>> _enable_percpu_irq(&msc->reenable_error_ppi);
>>
>> - if (atomic_fetch_inc(&msc->online_refs) == 0) {
>> - mutex_lock(&msc->error_irq_lock);
>> - if (msc->error_irq_hw_enabled)
>> - mpam_touch_msc(msc, mpam_enable_msc_ecr, msc);
>> - mutex_unlock(&msc->error_irq_lock);
>> -
>> - mpam_reprogram_msc(msc);
>> - }
>> + if (atomic_fetch_inc(&msc->online_refs) == 0 && !msc->pm_suspended)
>> + mpam_msc_power_on(msc);
>> }
>>
>> if (mpam_resctrl_enabled)
>> @@ -2304,30 +2330,56 @@ static int mpam_cpu_offline(unsigned int cpu)
>> if (msc->reenable_error_ppi)
>> disable_percpu_irq(msc->reenable_error_ppi);
>>
>> - if (atomic_dec_and_test(&msc->online_refs)) {
>> - struct mpam_msc_ris *ris;
>> + if (atomic_dec_and_test(&msc->online_refs) && !msc->pm_suspended)
>> + mpam_msc_power_off(msc);
>> + }
>>
>> - mutex_lock(&msc->cfg_lock);
>> - list_for_each_entry_srcu(ris, &msc->ris, msc_list,
>> - srcu_read_lock_held(&mpam_srcu)) {
>> - mpam_touch_msc(msc, &mpam_reset_ris, ris);
>> + return 0;
>> +}
>>
>> - /*
>> - * The reset state for non-zero partid may be
>> - * lost while the CPUs are offline.
>> - */
>> - ris->in_reset_state = false;
>> +static int mpam_msc_suspend(struct device *dev)
>> +{
>> + struct mpam_msc *msc = dev_get_drvdata(dev);
>>
>> - if (mpam_is_enabled())
>> - mpam_touch_msc(msc, &mpam_save_mbwu_state, ris);
>> - }
>> - mutex_unlock(&msc->cfg_lock);
>> - }
>> + if (!msc)
>> + return 0;
>> +
>> + guard(mutex)(&mpam_cpuhp_state_lock);
>> + if (!mpam_is_enabled())
>> + return 0;
>> +
>> + guard(cpus_read_lock)();
>> + guard(srcu)(&mpam_srcu);
>> + if (atomic_read(&msc->online_refs)) {
>> + mpam_msc_power_off(msc);
>> + msc->pm_suspended = true;
>> }
>>
>> return 0;
>> }
>>
>> +static int mpam_msc_resume(struct device *dev)
>> +{
>> + struct mpam_msc *msc = dev_get_drvdata(dev);
>> +
>> + if (!msc)
>> + return 0;
>> +
>> + guard(mutex)(&mpam_cpuhp_state_lock);
>> + if (!mpam_is_enabled() || !msc->pm_suspended)
>> + return 0;
>> +
>> + guard(cpus_read_lock)();
>> + guard(srcu)(&mpam_srcu);
>> + msc->pm_suspended = false;
>> + if (atomic_read(&msc->online_refs))
>> + mpam_msc_power_on(msc);
>> +
>> + return 0;
>> +}
>> +
>> +static DEFINE_SIMPLE_DEV_PM_OPS(mpam_msc_pm_ops, mpam_msc_suspend, mpam_msc_resume);
>
> If I understand correctly this covers both system system and hibernate but the commit message just
> mentions system suspend. Is that the case?
Yes, this patch cover both suspend and hibernate, this should be updated
in the commit message.
Additionally, it seems we can first add a patch to extract the
mpam_msc_power_on/off() helpers? This would make things look clearer.
Best regards,
Jinjie
>
> Thanks,
>
> Ben
>
>> +
>> static void mpam_register_cpuhp_callbacks(int (*online)(unsigned int online),
>> int (*offline)(unsigned int offline),
>> char *name)
>> @@ -2578,6 +2630,7 @@ static struct platform_driver mpam_msc_driver = {
>> .driver = {
>> .name = "mpam_msc",
>> .suppress_bind_attrs = true,
>> + .pm = pm_sleep_ptr(&mpam_msc_pm_ops),
>> },
>> .probe = mpam_msc_drv_probe,
>> .remove = mpam_msc_drv_remove,
>> diff --git a/drivers/resctrl/mpam_internal.h b/drivers/resctrl/mpam_internal.h
>> index 1f86cc158100..2e6da63c61c6 100644
>> --- a/drivers/resctrl/mpam_internal.h
>> +++ b/drivers/resctrl/mpam_internal.h
>> @@ -86,6 +86,7 @@ struct mpam_msc {
>> struct mpam_msc * __percpu *error_dev_id;
>>
>> atomic_t online_refs;
>> + bool pm_suspended;
>>
>> /*
>> * probe_lock is only taken during discovery. After discovery these
>