[PATCH v2 23/45] arm_mpam: resctrl: Add rmid index helpers

Ben Horgan posted 45 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v2 23/45] arm_mpam: resctrl: Add rmid index helpers
Posted by Ben Horgan 1 month, 3 weeks ago
From: James Morse <james.morse@arm.com>

Because MPAM's pmg aren't identical to RDT's rmid, resctrl handles some
data structures by index. This allows x86 to map indexes to RMID, and MPAM
to map them to partid-and-pmg.

Add the helpers to do this.

Signed-off-by: James Morse <james.morse@arm.com>
Signed-off-by: Ben Horgan <ben.horgan@arm.com>
---
Changes since rfc:
Use ~0U instead of ~0 in lhs of left shift
---
 drivers/resctrl/mpam_resctrl.c | 28 ++++++++++++++++++++++++++++
 include/linux/arm_mpam.h       |  3 +++
 2 files changed, 31 insertions(+)

diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
index 4275b1a85887..bdbc5504964b 100644
--- a/drivers/resctrl/mpam_resctrl.c
+++ b/drivers/resctrl/mpam_resctrl.c
@@ -120,6 +120,34 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
 	return mpam_partid_max + 1;
 }
 
+u32 resctrl_arch_system_num_rmid_idx(void)
+{
+	u8 closid_shift = fls(mpam_pmg_max);
+	u32 num_partid = resctrl_arch_get_num_closid(NULL);
+
+	return num_partid << closid_shift;
+}
+
+u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid)
+{
+	u8 closid_shift = fls(mpam_pmg_max);
+
+	WARN_ON_ONCE(closid_shift > 8);
+
+	return (closid << closid_shift) | rmid;
+}
+
+void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid)
+{
+	u8 closid_shift = fls(mpam_pmg_max);
+	u32 pmg_mask = ~(~0U << closid_shift);
+
+	WARN_ON_ONCE(closid_shift > 8);
+
+	*closid = idx >> closid_shift;
+	*rmid = idx & pmg_mask;
+}
+
 void resctrl_arch_sched_in(struct task_struct *tsk)
 {
 	lockdep_assert_preemption_disabled();
diff --git a/include/linux/arm_mpam.h b/include/linux/arm_mpam.h
index ba0312b55d9f..385554ceb452 100644
--- a/include/linux/arm_mpam.h
+++ b/include/linux/arm_mpam.h
@@ -59,6 +59,9 @@ void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid, u32 rmid);
 void resctrl_arch_sched_in(struct task_struct *tsk);
 bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid);
 bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 closid, u32 rmid);
+u32 resctrl_arch_rmid_idx_encode(u32 closid, u32 rmid);
+void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid);
+u32 resctrl_arch_system_num_rmid_idx(void);
 
 /**
  * mpam_register_requestor() - Register a requestor with the MPAM driver
-- 
2.43.0
Re: [PATCH v2 23/45] arm_mpam: resctrl: Add rmid index helpers
Posted by Jonathan Cameron 1 month ago
On Fri, 19 Dec 2025 18:11:25 +0000
Ben Horgan <ben.horgan@arm.com> wrote:

> From: James Morse <james.morse@arm.com>
> 
> Because MPAM's pmg aren't identical to RDT's rmid, resctrl handles some
> data structures by index. This allows x86 to map indexes to RMID, and MPAM
> to map them to partid-and-pmg.
> 
> Add the helpers to do this.
> 
> Signed-off-by: James Morse <james.morse@arm.com>
> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
one comment inline.
I messed around with GENMASK + field_prep()/field_get() - new
versions of these with no need for runtime constant masks, but
it ended up as not that much more readable than what you have here.

Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
> ---
> Changes since rfc:
> Use ~0U instead of ~0 in lhs of left shift
> ---
>  drivers/resctrl/mpam_resctrl.c | 28 ++++++++++++++++++++++++++++
>  include/linux/arm_mpam.h       |  3 +++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
> index 4275b1a85887..bdbc5504964b 100644
> --- a/drivers/resctrl/mpam_resctrl.c
> +++ b/drivers/resctrl/mpam_resctrl.c
> @@ -120,6 +120,34 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
>  	return mpam_partid_max + 1;
>  }
>  
> +u32 resctrl_arch_system_num_rmid_idx(void)
> +{
> +	u8 closid_shift = fls(mpam_pmg_max);
> +	u32 num_partid = resctrl_arch_get_num_closid(NULL);
> +
> +	return num_partid << closid_shift;

Given I think you restrict mpam_pmg_max to be power of 2 elsewhere,
doesn't this end up the same as something like
	return mpam_pmg_max * resctrl_arch_get_num_closid(NULL);
Maybe its worth keeping it in the form you have here as
it sort of provides documentation for how you pack those IDs

> +}
Re: [PATCH v2 23/45] arm_mpam: resctrl: Add rmid index helpers
Posted by Ben Horgan 1 month ago
Hi Jonathan,

On 1/6/26 11:21, Jonathan Cameron wrote:
> On Fri, 19 Dec 2025 18:11:25 +0000
> Ben Horgan <ben.horgan@arm.com> wrote:
> 
>> From: James Morse <james.morse@arm.com>
>>
>> Because MPAM's pmg aren't identical to RDT's rmid, resctrl handles some
>> data structures by index. This allows x86 to map indexes to RMID, and MPAM
>> to map them to partid-and-pmg.
>>
>> Add the helpers to do this.
>>
>> Signed-off-by: James Morse <james.morse@arm.com>
>> Signed-off-by: Ben Horgan <ben.horgan@arm.com>
> one comment inline.
> I messed around with GENMASK + field_prep()/field_get() - new
> versions of these with no need for runtime constant masks, but
> it ended up as not that much more readable than what you have here.
> 
> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
>> ---
>> Changes since rfc:
>> Use ~0U instead of ~0 in lhs of left shift
>> ---
>>  drivers/resctrl/mpam_resctrl.c | 28 ++++++++++++++++++++++++++++
>>  include/linux/arm_mpam.h       |  3 +++
>>  2 files changed, 31 insertions(+)
>>
>> diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
>> index 4275b1a85887..bdbc5504964b 100644
>> --- a/drivers/resctrl/mpam_resctrl.c
>> +++ b/drivers/resctrl/mpam_resctrl.c
>> @@ -120,6 +120,34 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
>>  	return mpam_partid_max + 1;
>>  }
>>  
>> +u32 resctrl_arch_system_num_rmid_idx(void)
>> +{
>> +	u8 closid_shift = fls(mpam_pmg_max);
>> +	u32 num_partid = resctrl_arch_get_num_closid(NULL);
>> +
>> +	return num_partid << closid_shift;
> 
> Given I think you restrict mpam_pmg_max to be power of 2 elsewhere,
> doesn't this end up the same as something like
> 	return mpam_pmg_max * resctrl_arch_get_num_closid(NULL);
> Maybe its worth keeping it in the form you have here as
> it sort of provides documentation for how you pack those IDs

We only warn if (mpam_pmg_max + 1) is a power of 2 and so I'll keep this
as it is, although yours is equivalent in the expected case.

> 
>> +}
> 

Thanks,

Ben
Re: [PATCH v2 23/45] arm_mpam: resctrl: Add rmid index helpers
Posted by Jonathan Cameron 1 month ago
On Tue, 6 Jan 2026 11:33:44 +0000
Ben Horgan <ben.horgan@arm.com> wrote:

> Hi Jonathan,
> 
> On 1/6/26 11:21, Jonathan Cameron wrote:
> > On Fri, 19 Dec 2025 18:11:25 +0000
> > Ben Horgan <ben.horgan@arm.com> wrote:
> >   
> >> From: James Morse <james.morse@arm.com>
> >>
> >> Because MPAM's pmg aren't identical to RDT's rmid, resctrl handles some
> >> data structures by index. This allows x86 to map indexes to RMID, and MPAM
> >> to map them to partid-and-pmg.
> >>
> >> Add the helpers to do this.
> >>
> >> Signed-off-by: James Morse <james.morse@arm.com>
> >> Signed-off-by: Ben Horgan <ben.horgan@arm.com>  
> > one comment inline.
> > I messed around with GENMASK + field_prep()/field_get() - new
> > versions of these with no need for runtime constant masks, but
> > it ended up as not that much more readable than what you have here.
> > 
> > Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>  
> >> ---
> >> Changes since rfc:
> >> Use ~0U instead of ~0 in lhs of left shift
> >> ---
> >>  drivers/resctrl/mpam_resctrl.c | 28 ++++++++++++++++++++++++++++
> >>  include/linux/arm_mpam.h       |  3 +++
> >>  2 files changed, 31 insertions(+)
> >>
> >> diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
> >> index 4275b1a85887..bdbc5504964b 100644
> >> --- a/drivers/resctrl/mpam_resctrl.c
> >> +++ b/drivers/resctrl/mpam_resctrl.c
> >> @@ -120,6 +120,34 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
> >>  	return mpam_partid_max + 1;
> >>  }
> >>  
> >> +u32 resctrl_arch_system_num_rmid_idx(void)
> >> +{
> >> +	u8 closid_shift = fls(mpam_pmg_max);
> >> +	u32 num_partid = resctrl_arch_get_num_closid(NULL);
> >> +
> >> +	return num_partid << closid_shift;  
> > 
> > Given I think you restrict mpam_pmg_max to be power of 2 elsewhere,
> > doesn't this end up the same as something like
> > 	return mpam_pmg_max * resctrl_arch_get_num_closid(NULL);
> > Maybe its worth keeping it in the form you have here as
> > it sort of provides documentation for how you pack those IDs  
> 
> We only warn if (mpam_pmg_max + 1) is a power of 2 and so I'll keep this
> as it is, although yours is equivalent in the expected case.

Do the resulting 'holes' in the values that are valid here cause
trouble it isn't power of 2? 

> 
> >   
> >> +}  
> >   
> 
> Thanks,
> 
> Ben
> 
>
Re: [PATCH v2 23/45] arm_mpam: resctrl: Add rmid index helpers
Posted by Ben Horgan 1 month ago
Hi Jonathan,

On 1/6/26 14:04, Jonathan Cameron wrote:
> On Tue, 6 Jan 2026 11:33:44 +0000
> Ben Horgan <ben.horgan@arm.com> wrote:
> 
>> Hi Jonathan,
>>
>> On 1/6/26 11:21, Jonathan Cameron wrote:
>>> On Fri, 19 Dec 2025 18:11:25 +0000
>>> Ben Horgan <ben.horgan@arm.com> wrote:
>>>   
>>>> From: James Morse <james.morse@arm.com>
>>>>
>>>> Because MPAM's pmg aren't identical to RDT's rmid, resctrl handles some
>>>> data structures by index. This allows x86 to map indexes to RMID, and MPAM
>>>> to map them to partid-and-pmg.
>>>>
>>>> Add the helpers to do this.
>>>>
>>>> Signed-off-by: James Morse <james.morse@arm.com>
>>>> Signed-off-by: Ben Horgan <ben.horgan@arm.com>  
>>> one comment inline.
>>> I messed around with GENMASK + field_prep()/field_get() - new
>>> versions of these with no need for runtime constant masks, but
>>> it ended up as not that much more readable than what you have here.
>>>
>>> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>  
>>>> ---
>>>> Changes since rfc:
>>>> Use ~0U instead of ~0 in lhs of left shift
>>>> ---
>>>>  drivers/resctrl/mpam_resctrl.c | 28 ++++++++++++++++++++++++++++
>>>>  include/linux/arm_mpam.h       |  3 +++
>>>>  2 files changed, 31 insertions(+)
>>>>
>>>> diff --git a/drivers/resctrl/mpam_resctrl.c b/drivers/resctrl/mpam_resctrl.c
>>>> index 4275b1a85887..bdbc5504964b 100644
>>>> --- a/drivers/resctrl/mpam_resctrl.c
>>>> +++ b/drivers/resctrl/mpam_resctrl.c
>>>> @@ -120,6 +120,34 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *ignored)
>>>>  	return mpam_partid_max + 1;
>>>>  }
>>>>  
>>>> +u32 resctrl_arch_system_num_rmid_idx(void)
>>>> +{
>>>> +	u8 closid_shift = fls(mpam_pmg_max);
>>>> +	u32 num_partid = resctrl_arch_get_num_closid(NULL);
>>>> +
>>>> +	return num_partid << closid_shift;  
>>>
>>> Given I think you restrict mpam_pmg_max to be power of 2 elsewhere,
>>> doesn't this end up the same as something like
>>> 	return mpam_pmg_max * resctrl_arch_get_num_closid(NULL);
>>> Maybe its worth keeping it in the form you have here as
>>> it sort of provides documentation for how you pack those IDs  
>>
>> We only warn if (mpam_pmg_max + 1) is a power of 2 and so I'll keep this
>> as it is, although yours is equivalent in the expected case.
> 
> Do the resulting 'holes' in the values that are valid here cause
> trouble it isn't power of 2? 

Theoretically they are a problem, and if resctrl allocates them then we
would get invalid pmg values which can cause msc error interrupts.
However, I don't know of any platforms where pmg_max is more than 1.

Anyhow, I'll look into using multiplication in these rmid arch helpers
rather than just shifting to avoid leaving these holes.

> 
>>
>>>   
>>>> +}  
>>>   
>>
>> Thanks,
>>
>> Ben
>>
>>
> 

-- 
Thanks,

Ben