Re: [PATCH v2 23/29] arm_mpam: Add mpam_msmon_read() to read monitor value

James Morse
2025-10-09T17:49:00Z

Hi Jonathan,

On 12/09/2025 14:21, Jonathan Cameron wrote:
> On Wed, 10 Sep 2025 20:43:03 +0000
> James Morse <james.morse@arm.com> wrote:
>
>> Reading a monitor involves configuring what you want to monitor, and
>> reading the value. Components made up of multiple MSC may need values
>> from each MSC. MSCs may take time to configure, returning 'not ready'.
>> The maximum 'not ready' time should have been provided by firmware.
>>
>> Add mpam_msmon_read() to hide all this. If (one of) the MSC returns
>> not ready, then wait the full timeout value before trying again.
>>
>> CC: Shanker Donthineni <sdonthineni@nvidia.com>
>> Signed-off-by: James Morse <james.morse@arm.com>

>> +static void write_msmon_ctl_flt_vals(struct mon_read *m, u32 ctl_val,
>> + u32 flt_val)
>> +{
>> + struct mpam_msc *msc = m->ris->vmsc->msc;
>> +
>> + /*
>> + * Write the ctl_val with the enable bit cleared, reset the counter,
>> + * then enable counter.
>> + */
>> + switch (m->type) {
>> + case mpam_feat_msmon_csu:
>> + mpam_write_monsel_reg(msc, CFG_CSU_FLT, flt_val);
>> + mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val);
>> + mpam_write_monsel_reg(msc, CSU, 0);
>> + mpam_write_monsel_reg(msc, CFG_CSU_CTL, ctl_val | MSMON_CFG_x_CTL_EN);
>> + break;
>> + case mpam_feat_msmon_mbwu:
>> + mpam_write_monsel_reg(msc, CFG_MBWU_FLT, flt_val);
>> + mpam_write_monsel_reg(msc, CFG_MBWU_CTL, ctl_val);
>> + mpam_write_monsel_reg(msc, MBWU, 0);
>> + mpam_write_monsel_reg(msc, CFG_MBWU_CTL, ctl_val | MSMON_CFG_x_CTL_EN);
>> + break;
>
> Given nothing to do later, I'd just return at end of each case.
> Entirely up to you though as this is just a personal style preference.

Out of habit I try to avoid functions returning from different places, as it makes it
harder to add locking later. Maybe the fancy cleanup c++ thing changes that.

>> + default:
>> + return;

But I'm clearly inconsistent!
I've changes this as you suggest.

>> + }
>> +}
>
>> +
>> +static int _msmon_read(struct mpam_component *comp, struct mon_read *arg)
>> +{
>> + int err, idx;
>> + struct mpam_msc *msc;
>> + struct mpam_vmsc *vmsc;
>> + struct mpam_msc_ris *ris;
>> +
>> + idx = srcu_read_lock(&mpam_srcu);
>
> guard(srcu)(&mpam_srcu);
>
> Then you can do direct returns on errors which looks like it will simplify
> things somewhat by letting you just return on err.
>
>
>> + list_for_each_entry_rcu(vmsc, &comp->vmsc, comp_list) {
>> + msc = vmsc->msc;
> I'd bring the declaration down here as well.
> struct mpam_msc *msc = vmsc->msc;
> Could bring ris down here as well.
>
>> +
>> + list_for_each_entry_rcu(ris, &vmsc->ris, vmsc_list) {
>> + arg->ris = ris;
>> +
>> + err = smp_call_function_any(&msc->accessibility,
>> + __ris_msmon_read, arg,
>> + true);
>> + if (!err && arg->err)
>> + err = arg->err;
>> + if (err)
>> + break;
>> + }
>> + if (err)
>> + break;
>
> This won't be needed if you returned on error above.
>
>> + }
>> + srcu_read_unlock(&mpam_srcu, idx);
>> +
>> + return err;

> And you only reach here with above changes if err == 0 so return 0; appropriate.

I've done all of the above.
(I'd even already worked it out from your earlier feedback)

>> +}
>> +
>> +int mpam_msmon_read(struct mpam_component *comp, struct mon_cfg *ctx,
>> + enum mpam_device_features type, u64 *val)
>> +{
>> + int err;
>> + struct mon_read arg;
>> + u64 wait_jiffies = 0;
>> + struct mpam_props *cprops = &comp->class->props;
>> +
>> + might_sleep();
>> +
>> + if (!mpam_is_enabled())
>> + return -EIO;
>> +
>> + if (!mpam_has_feature(type, cprops))
>> + return -EOPNOTSUPP;
>> +
>> + memset(&arg, 0, sizeof(arg));

> Either use = { }; at declaration or maybe
> arg = (struct mon_read) {
> .ctx = ctx,
> .type = type,
> .val = val,
> };
>
> rather than bothering with separate memset.

The memset is because arg gets re-used, but there are fields not touched here that get
modified, like 'err'. But this struct assignment works too...

>> + arg.ctx = ctx;
>> + arg.type = type;
>> + arg.val = val;
>> + *val = 0;
>> +
>> + err = _msmon_read(comp, &arg);
>> + if (err == -EBUSY && comp->class->nrdy_usec)
>> + wait_jiffies = usecs_to_jiffies(comp->class->nrdy_usec);
>> +
>> + while (wait_jiffies)
>> + wait_jiffies = schedule_timeout_uninterruptible(wait_jiffies);
>> +
>> + if (err == -EBUSY) {
>> + memset(&arg, 0, sizeof(arg));

> Same as above.

Yup - its like this so that they look the same.

>> + arg.ctx = ctx;
>> + arg.type = type;
>> + arg.val = val;
>> + *val = 0;
>> +
>> + err = _msmon_read(comp, &arg);
>> + }
>> +
>> + return err;
>> +}
>

Thanks,

James