Re: [PATCH v2 0/4] fork,arm,x86,fs/resctrl: Fix stale resource group assignment on fork

From: Luck, Tony

Date: Fri Dec 04 2026 - 16:08:14 EST


On Fri, Dec 04, 2026 at 11:15:12AM -0800, Reinette Chatre wrote:
> What remains?
> =============
> On x86 the CLOSID and RMID are separate values in struct task_struct and
> __resctrl_sched_in() reads them without a lock, so a task can still be
> labelled with a mismatched pair for the duration of a move. Unlike the
> fork case, which is permanent and invisible to every later walk of the
> task list, that window is transient and the pair is corrected at the next
> context switch. MPAM stores the two as a single value.

I think the patch below covers this (only atomic on 64-bit CPUs).

Builds and passes tools/testing/selftests/resctrl/resctrl_tests.

-Tony

---
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d7855..c79ac345136d 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1364,8 +1364,13 @@ struct task_struct {
#endif /* CONFIG_PREEMPT_RT */
#endif /* CONFIG_CGROUPS */
#ifdef CONFIG_X86_CPU_RESCTRL
- u32 closid;
- u32 rmid;
+ union x86_pqosid {
+ u64 pqosid;
+ struct {
+ u32 closid;
+ u32 rmid;
+ };
+ };
#endif

struct futex_sched_data futex;
diff --git a/arch/x86/include/asm/resctrl.h b/arch/x86/include/asm/resctrl.h
index 48bf22fb6b47..1b842c55d92f 100644
--- a/arch/x86/include/asm/resctrl.h
+++ b/arch/x86/include/asm/resctrl.h
@@ -104,23 +104,23 @@ static inline void __resctrl_sched_in(struct task_struct *tsk)
struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
u32 closid = READ_ONCE(state->default_closid);
u32 rmid = READ_ONCE(state->default_rmid);
+ union x86_pqosid tmp;
struct msr val;
- u32 tmp;
+
+ tmp.pqosid = READ_ONCE(tsk->pqosid);

/*
* If this task has a closid/rmid assigned, use it.
* Else use the closid/rmid assigned to this cpu.
*/
if (static_branch_likely(&rdt_alloc_enable_key)) {
- tmp = READ_ONCE(tsk->closid);
- if (tmp)
- closid = tmp;
+ if (tmp.closid)
+ closid = tmp.closid;
}

if (static_branch_likely(&rdt_mon_enable_key)) {
- tmp = READ_ONCE(tsk->rmid);
- if (tmp)
- rmid = tmp;
+ if (tmp.rmid)
+ rmid = tmp.rmid;
}

if (closid != state->cur_closid || rmid != state->cur_rmid) {
@@ -160,8 +160,7 @@ static inline void resctrl_arch_set_closid_rmid(struct task_struct *tsk,
static inline void resctrl_arch_copy_closid_rmid(struct task_struct *dst,
struct task_struct *src)
{
- WRITE_ONCE(dst->closid, READ_ONCE(src->closid));
- WRITE_ONCE(dst->rmid, READ_ONCE(src->rmid));
+ WRITE_ONCE(dst->pqosid, READ_ONCE(src->pqosid));
}

static inline bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid)