Re: [PATCH RFC 12/15] arm_mpam: Fix get_cpumask_from_cache() to clear mask on error

From: Andre Przywara

Date: Wed Sep 02 2026 - 14:31:28 EST


Hi,

On 8/11/26 15:30, Yin Li wrote:
If mpam_get_cpumask_from_cache_id() fails, the affinity mask may have
been partially filled before the error occurred. The caller has no way
to distinguish a partial mask from a valid one, so it may misuse the
incomplete result.

But it returns an error, in which case any caller should ignore what's in the affinity pointer?
I am not sure that clearing the mask in necessarily the right solution.
Either we leave it untouched (which might be tricky), or we check that the callers restore or discard the affinity mask when the function returns an error.

Did this trigger any real problem, or was it some bored^Woverzealous AI review tool pointing that out?

Cheers,
Andre

Clear the affinity mask before returning the error so the caller always
receives either a fully valid mask or an empty one.

Co-developed-by: Huang Yiwei <huang.yiwei@xxxxxxxxxxxxxxxx>
Signed-off-by: Huang Yiwei <huang.yiwei@xxxxxxxxxxxxxxxx>
Signed-off-by: Yin Li <yin.li@xxxxxxxxxxxxxxxx>
---
drivers/resctrl/mpam_devices.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/resctrl/mpam_devices.c b/drivers/resctrl/mpam_devices.c
index eac3bc695fd1..d8856864e89a 100644
--- a/drivers/resctrl/mpam_devices.c
+++ b/drivers/resctrl/mpam_devices.c
@@ -243,7 +243,12 @@ static int get_cpumask_from_cache(struct device_node *cache,
return -ENOENT;
}
- return mpam_get_cpumask_from_cache_id(cache_id, cache_level, affinity);
+ err = mpam_get_cpumask_from_cache_id(cache_id, cache_level, affinity);
+ if (err)
+ /* Don't leave a partially-filled mask for the caller to misuse */
+ cpumask_clear(affinity);
+
+ return err;
}
static int mpam_dt_count_msc(void)