mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tim Chen <tim.c.chen@linux.intel.com>
To: Luo Gengkun <luogengkun2@huawei.com>,
	peterz@infradead.org,  mingo@redhat.com, juri.lelli@redhat.com,
	vincent.guittot@linaro.org,  yu.c.chen@intel.com
Cc: dietmar.eggemann@arm.com, rostedt@goodmis.org,
	bsegall@google.com,  mgorman@suse.de, vschneid@redhat.com,
	kprateek.nayak@amd.com,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v8 1/2] sched/cache: Reduce the overhead of task_cache_work by only scan the visisted cpus
Date: Tue, 28 Jul 2026 13:16:16 -0700	[thread overview]
Message-ID: <ffa0f0497dafe66cdc1b3b26ac5832cfa3eebcd8.camel@linux.intel.com> (raw)
In-Reply-To: <20260723040429.630176-2-luogengkun2@huawei.com>

On Thu, 2026-07-23 at 04:04 +0000, Luo Gengkun wrote:
> The overhead of task_cache_work() is high, especially in multi-NUMA systems.
> Currently, task_cache_work() tries to find the pref_llc by scanning all CPUs
> in the system. However, most of these scans are meaningless, such as those
> for CPUs that have never been visited or were accessed a long time ago.
> 
> To address this problem, introduce visited_cpus to track the visited CPUs
> and evict them once they have not been accessed for a duration exceeding
> llc_epoch_affinity_timeout.
> 
> Now that we know exactly which CPUs to scan from visited_cpus, we can remove
> get_scan_cpumasks().

Thanks.

The code looks good to me.

Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>

> 
> Signed-off-by: Luo Gengkun <luogengkun2@huawei.com>
> ---
>  include/linux/mm_types.h |  6 +++
>  include/linux/sched.h    |  2 +
>  kernel/sched/fair.c      | 94 ++++++++++++++++------------------------
>  3 files changed, 46 insertions(+), 56 deletions(-)
> 
> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index b18c2b2e7d2c..35559079e4d4 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -1620,6 +1620,11 @@ static inline int mm_alloc_sched_noprof(struct mm_struct *mm)
>  	if (!pcpu_sched)
>  		return -ENOMEM;
>  
> +	if (!zalloc_cpumask_var(&mm->sc_stat.visited_cpus, GFP_KERNEL)) {
> +		free_percpu(pcpu_sched);
> +		return -ENOMEM;
> +	}
> +
>  	mm_init_sched(mm, pcpu_sched);
>  	return 0;
>  }
> @@ -1630,6 +1635,7 @@ static inline void mm_destroy_sched(struct mm_struct *mm)
>  {
>  	free_percpu(mm->sc_stat.pcpu_sched);
>  	mm->sc_stat.pcpu_sched = NULL;
> +	free_cpumask_var(mm->sc_stat.visited_cpus);
>  }
>  #else /* !CONFIG_SCHED_CACHE */
>  
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 373bcc0598d1..b461a71a65da 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -2388,6 +2388,7 @@ static __always_inline int task_mm_cid(struct task_struct *t)
>  struct sched_cache_time {
>  	u64 runtime;
>  	unsigned long epoch;
> +	unsigned long epoch_last_visit;
>  };
>  
>  struct sched_cache_stat {
> @@ -2398,6 +2399,7 @@ struct sched_cache_stat {
>  	unsigned long next_scan;
>  	unsigned long footprint;
>  	int cpu;
> +	cpumask_var_t visited_cpus;
>  } ____cacheline_aligned_in_smp;
>  
>  #else
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index d78467ec6ee1..10d442074f21 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -1585,6 +1585,7 @@ void mm_init_sched(struct mm_struct *mm,
>  		pcpu_sched->runtime = 0;
>  		/* a slightly stale cpu epoch is acceptible */
>  		pcpu_sched->epoch = rq->cpu_epoch;
> +		pcpu_sched->epoch_last_visit = rq->cpu_epoch;
>  		epoch = rq->cpu_epoch;
>  	}
>  
> @@ -1635,13 +1636,23 @@ static inline void __update_mm_sched(struct rq *rq,
>  	}
>  }
>  
> -static unsigned long fraction_mm_sched(struct rq *rq,
> -				       struct sched_cache_time *pcpu_sched)
> +static unsigned long fraction_mm_sched(int cpu,
> +				       struct mm_struct *mm)
>  {
> +	struct sched_cache_time *pcpu_sched =
> +		per_cpu_ptr(mm->sc_stat.pcpu_sched, cpu);
> +	struct rq *rq = cpu_rq(cpu);
> +
>  	guard(raw_spinlock_irqsave)(&rq->cpu_epoch_lock);
>  
>  	__update_mm_sched(rq, pcpu_sched);
>  
> +	/* Skip the rq that has not been hit for a long time */
> +	if ((rq->cpu_epoch - pcpu_sched->epoch_last_visit) > llc_epoch_affinity_timeout) {
> +		cpumask_clear_cpu(cpu, mm->sc_stat.visited_cpus);
> +		return 0;
> +	}
> +
>  	/*
>  	 * Runtime is a geometric series (r=0.5) and as such will sum to twice
>  	 * the accumulation period, this means the multiplcation here should
> @@ -1711,6 +1722,9 @@ void account_mm_sched(struct rq *rq, struct task_struct *p, s64 delta_exec)
>  		pcpu_sched->runtime += delta_exec;
>  		rq->cpu_runtime += delta_exec;
>  		epoch = rq->cpu_epoch;
> +		pcpu_sched->epoch_last_visit = epoch;
> +		if (!cpumask_test_cpu(cpu_of(rq), mm->sc_stat.visited_cpus))
> +			cpumask_set_cpu(cpu_of(rq), mm->sc_stat.visited_cpus);
>  	}
>  
>  	/*
> @@ -1761,51 +1775,6 @@ static void task_tick_cache(struct rq *rq, struct task_struct *p)
>  	}
>  }
>  
> -static void get_scan_cpumasks(cpumask_var_t cpus, struct task_struct *p)
> -{
> -#ifdef CONFIG_NUMA_BALANCING
> -	int cpu, curr_cpu, nid, pref_nid;
> -
> -	if (!static_branch_likely(&sched_numa_balancing))
> -		goto out;
> -
> -	cpu = READ_ONCE(p->mm->sc_stat.cpu);
> -	if (cpu != -1)
> -		nid = cpu_to_node(cpu);
> -	curr_cpu = task_cpu(p);
> -
> -	/*
> -	 * Scanning in the preferred NUMA node is ideal. However, the NUMA
> -	 * preferred node is per-task rather than per-process. It is possible
> -	 * for different threads of the process to have distinct preferred
> -	 * nodes; consequently, the process-wide preferred LLC may bounce
> -	 * between different nodes. As a workaround, maintain the scan
> -	 * CPU mask to also cover the process's current preferred LLC and the
> -	 * current running node to mitigate the bouncing risk.
> -	 * TBD: numa_group should be considered during task aggregation.
> -	 */
> -	pref_nid = p->numa_preferred_nid;
> -	/* honor the task's preferred node */
> -	if (pref_nid == NUMA_NO_NODE)
> -		goto out;
> -
> -	cpumask_or(cpus, cpus, cpumask_of_node(pref_nid));
> -
> -	/* honor the task's preferred LLC CPU */
> -	if (cpu != -1 && !cpumask_test_cpu(cpu, cpus) && nid != NUMA_NO_NODE)
> -		cpumask_or(cpus, cpus, cpumask_of_node(nid));
> -
> -	/* make sure the task's current running node is included */
> -	if (!cpumask_test_cpu(curr_cpu, cpus))
> -		cpumask_or(cpus, cpus, cpumask_of_node(cpu_to_node(curr_cpu)));
> -
> -	return;
> -
> -out:
> -#endif
> -	cpumask_copy(cpus, cpu_online_mask);
> -}
> -
>  static inline void update_avg_scale(u64 *avg, u64 sample)
>  {
>  	int factor = per_cpu(sd_llc_size, raw_smp_processor_id());
> @@ -1866,7 +1835,18 @@ static void task_cache_work(struct callback_head *work)
>  	scoped_guard (cpus_read_lock) {
>  		guard(rcu)();
>  
> -		get_scan_cpumasks(cpus, p);
> +		/*
> +		 * Data race: While evaluating the visited_cpus without
> +		 * a lock, a CPU could be concurrently set by
> +		 * account_mm_sched(), meaning the scan might skip the newly
> +		 * visited CPU if the bit changes during the scan. This is
> +		 * a deliberate trade-off between accuracy and efficiency:
> +		 * locking would prevent this race but incur extra overhead.
> +		 * The missed runtime contribution is negligible because it
> +		 * implies this process hasn't run on that CPU for a long
> +		 * time, and will be captured in the next cycle.
> +		 */
> +		cpumask_and(cpus, cpu_online_mask, mm->sc_stat.visited_cpus);
>  
>  		for_each_cpu(cpu, cpus) {
>  			/* XXX sched_cluster_active */
> @@ -1877,19 +1857,21 @@ static void task_cache_work(struct callback_head *work)
>  			if (!sd)
>  				continue;
>  
> -			for_each_cpu(i, sched_domain_span(sd)) {
> -				occ = fraction_mm_sched(cpu_rq(i),
> -							per_cpu_ptr(mm->sc_stat.pcpu_sched, i));
> +			for_each_cpu_and(i, sched_domain_span(sd), mm->sc_stat.visited_cpus) {
> +				cur = rcu_dereference_all(cpu_rq(i)->curr);
> +				if (cur && !(cur->flags & (PF_EXITING | PF_KTHREAD)) &&
> +				    cur->mm == mm)
> +					nr_running++;
> +
> +				occ = fraction_mm_sched(i, mm);
> +				if (occ == 0)
> +					continue;
> +
>  				a_occ += occ;
>  				if (occ > m_occ) {
>  					m_occ = occ;
>  					m_cpu = i;
>  				}
> -
> -				cur = rcu_dereference_all(cpu_rq(i)->curr);
> -				if (cur && !(cur->flags & (PF_EXITING | PF_KTHREAD)) &&
> -				    cur->mm == mm)
> -					nr_running++;
>  			}
>  
>  			/*

  parent reply	other threads:[~2026-07-28 20:16 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23  4:04 [PATCH v8 0/2] Cache aware scheduling: Reduce the overhead of task_cache_work Luo Gengkun
2026-07-23  4:04 ` [PATCH v8 1/2] sched/cache: Reduce the overhead of task_cache_work by only scan the visisted cpus Luo Gengkun
2026-07-27  1:09   ` Chen, Yu C
2026-07-28  8:53     ` Luo Gengkun
2026-07-28 12:08       ` : " Chen Yu
2026-07-29  9:19         ` Luo Gengkun
2026-07-29 13:48           ` Chen, Yu C
2026-07-29 18:29           ` Tim Chen
2026-07-30  2:22             ` Tim Chen
2026-07-30 13:40               ` Luo Gengkun
2026-07-30 16:57                 ` Tim Chen
2026-07-31  6:58                   ` Chen, Yu C
2026-07-31  8:31                     ` Luo Gengkun
2026-07-28 20:16   ` Tim Chen [this message]
2026-07-23  4:04 ` [PATCH v8 2/2] -- DO NOT APPLY!!! -- sched/cache/debug: Add trace event and sched feature to track scan cost Luo Gengkun

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=ffa0f0497dafe66cdc1b3b26ac5832cfa3eebcd8.camel@linux.intel.com \
    --to=tim.c.chen@linux.intel.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=luogengkun2@huawei.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --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®