From: Jemmy Wong <jemmywong512@gmail.com>
To: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Jemmy Wong <jemmywong512@gmail.com>,
Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Chen Yu <yu.c.chen@intel.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sched/cache: Remove the old cache group footprint on exec
Date: Sat, 10 Oct 2026 11:13:31 +0800 [thread overview]
Message-ID: <650309CC-380D-4EC6-88F7-45A2CEA0F33E@gmail.com> (raw)
In-Reply-To: <b73d47b04b62d2637920f59134b632d607a71aee.camel@linux.intel.com>
Hi Tim,
Thanks for the review.
This was found by code inspection, not a workload reproducer.
I'll mention vfork() and clarify that the issue mainly concerns
longer-lived CLONE_VM tasks that accumulate NUMA faults before exec.
I'll also add the Fixes tag and document that footprint subtraction
must precede task_numa_free() resetting p->total_numa_faults.
I'll send v2 with these changes and your Reviewed-by tag.
Thanks,
Jemmy
> On Oct 10, 2026, at 5:01 AM, Tim Chen <tim.c.chen@linux.intel.com> wrote:
>
> On Fri, 2026-10-09 at 23:12 +0800, Jemmy Wong wrote:
>> Exec replaces the task's cache group before resetting its NUMA fault
>> statistics, but only the exit path subtracts the task's contribution
>> from the old group's footprint.
>>
>> Although de_thread() removes other members of the executing task's
>> thread group, tasks created with CLONE_VM without CLONE_THREAD can
>> retain the old mm and cache group. The executing task's contribution
>> then remains in that group without further updates or decay, potentially
>> suppressing cache-aware aggregation through the LLC capacity check.
>
> Thanks for catching this.
>
> It might help to mention vfork() explicitly, where the parent keeps the old mm
> alive while the child execs. In practice a vfork child rarely builds
> up NUMA faults before exec, since NUMA scanning starts late, so the
> leak mostly matters for longer-lived CLONE_VM tasks that later exec. If
> you have a workload where you saw this, please mention it. Otherwise,
> saying it was found by code inspection is fine.
>
> Please also add:
>
> Fixes: b636fef85bda ("sched/cache: Introduce task_struct->sched_cache_grp to fix UAF")
>
>
>>
>> Factor the existing footprint subtraction into a helper and use it when
>> leaving a cache group on both exec and exit. Subtract before dropping the
>> old group reference, preserving the existing underflow protection.
>>
>> Signed-off-by: Jemmy Wong <jemmywong512@gmail.com>
>> ---
>> kernel/sched/fair.c | 51 ++++++++++++++++++++++++++-------------------
>> 1 file changed, 29 insertions(+), 22 deletions(-)
>>
>> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
>> index 57360f5cdde4..af182c9fcde7 100644
>> --- a/kernel/sched/fair.c
>> +++ b/kernel/sched/fair.c
>> @@ -1771,6 +1771,25 @@ void sched_cache_fork_cleanup(struct task_struct *p)
>> RCU_INIT_POINTER(p->sched_cache_grp, NULL);
>> }
>>
>> +static void sched_cache_footprint_sub(struct sched_cache_group *grp,
>> + struct task_struct *p)
>> +{
>> +#ifdef CONFIG_NUMA_BALANCING
>> + /*
>> + * Remove this task's contribution when it leaves the group, either
>> + * through exit or exec. Other thread groups sharing the old mm can
>> + * keep the group alive after exec resets this task's NUMA statistics.
>> + * Unlocked for performance; clamp to avoid underflow.
>> + */
>> + if (grp && p->total_numa_faults) {
>> + unsigned long fp = READ_ONCE(grp->footprint);
>> + unsigned long sub = min(fp, p->total_numa_faults);
>> +
>> + WRITE_ONCE(grp->footprint, fp - sub);
>> + }
>> +#endif
>> +}
>> +
>> void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
>> {
>> struct sched_cache_group *old;
>> @@ -1780,6 +1799,7 @@ void sched_cache_exec_mmap(struct task_struct *p, struct mm_struct *mm)
>> * the old one. @p is current and the only writer of its own pointer.
>> */
>> old = sched_cache_replace_grp(p, sched_cache_group_get(mm->sched_cache_grp));
>> + sched_cache_footprint_sub(old, p);
>> sched_cache_group_put(old);
>> }
>
> This relies on running before task_numa_free() in bprm_execve()
> clears p->total_numa_faults, and nothing enforces that ordering. A
> short comment here noting the dependency would keep a future exec
> rework from quietly bringing the leak back.
>
> With the changelog and comment tweaks above:
>
> Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>
>
>
>
>>
>> @@ -1787,19 +1807,7 @@ void sched_cache_exit_mm(struct task_struct *p)
>> {
>> struct sched_cache_group *grp = sched_cache_replace_grp(p, NULL);
>>
>> -#ifdef CONFIG_NUMA_BALANCING
>> - /*
>> - * Subtract this task's footprint from the group before dropping the
>> - * reference, so the group footprint converges as its threads exit.
>> - * Unlocked for performance; clamp to avoid underflow.
>> - */
>> - if (grp && p->total_numa_faults) {
>> - unsigned long fp = READ_ONCE(grp->footprint);
>> - unsigned long sub = min(fp, p->total_numa_faults);
>> -
>> - WRITE_ONCE(grp->footprint, fp - sub);
>> - }
>> -#endif
>> + sched_cache_footprint_sub(grp, p);
>> sched_cache_group_put(grp);
>> }
>>
>> @@ -3975,16 +3983,15 @@ static void task_numa_placement(struct task_struct *p)
>> * sharing this mm. Acceptable since footprint is a
>> * heuristic and occasional lost updates are tolerable.
>> *
>> - * If a task exits, its corresponding footprint must
>> - * be subtracted from p->sched_cache_grp->footprint,
>> - * otherwise the footprint will not converge: the
>> - * exiting thread's footprint remains unchanged/undecayed.
>> - * See exit_mm().
>> + * If a task leaves its cache group through exit or exec,
>> + * its contribution must be subtracted from the old group's
>> + * footprint. Otherwise, that contribution remains
>> + * unchanged/undecayed while other tasks keep the group alive.
>> + * See sched_cache_footprint_sub().
>> *
>> - * Lost updates and unsynchronized subtraction
>> - * in exit_mm() can cause footprint + diff to
>> - * go negative. Clamp to zero to prevent the
>> - * unsigned footprint from wrapping.
>> + * Lost updates and unsynchronized subtraction on exit or
>> + * exec can cause footprint + diff to go negative. Clamp
>> + * to zero to prevent the unsigned footprint from wrapping.
>> */
>> scoped_guard(rcu) {
>> grp = rcu_dereference(p->sched_cache_grp);
prev parent reply other threads:[~2026-10-10 3:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-09 15:12 Jemmy Wong
2026-10-09 21:01 ` Tim Chen
2026-10-10 3:13 ` Jemmy Wong [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=650309CC-380D-4EC6-88F7-45A2CEA0F33E@gmail.com \
--to=jemmywong512@gmail.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tim.c.chen@linux.intel.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yu.c.chen@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®