RE: [RFC PATCH v2 12/23] sched/cache: Introduce rq affinity gain calculation

From: Jianyong Wu

Date: Tue Sep 01 2026 - 08:45:01 EST




> -----Original Message-----
> From: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Sent: Tuesday, September 1, 2026 6:16 PM
> To: Jianyong Wu <wujianyong@xxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>; Juri Lelli <juri.lelli@xxxxxxxxxx>;
> Vincent Guittot <vincent.guittot@xxxxxxxxxx>; Chen Yu
> <yu.c.chen@xxxxxxxxx>; Tim Chen <tim.c.chen@xxxxxxxxxxxxxxx>; Dietmar
> Eggemann <dietmar.eggemann@xxxxxxx>; Steven Rostedt
> <rostedt@xxxxxxxxxxx>; Ben Segall <bsegall@xxxxxxxxxx>; Mel Gorman
> <mgorman@xxxxxxx>; Valentin Schneider <vschneid@xxxxxxxxxx>; K
> Prateek Nayak <kprateek.nayak@xxxxxxx>; Shrikanth Hegde
> <sshegde@xxxxxxxxxxxxx>; Phil Auld <pauld@xxxxxxxxxx>; Andrew
> Morton <akpm@xxxxxxxxxxxxxxxxxxxx>; David Hildenbrand
> <david@xxxxxxxxxx>; linux-kernel@xxxxxxxxxxxxxxx; linux-mm@xxxxxxxxx;
> jianyong.wu@xxxxxxxxxxx; Yuan Zhong <zhongyuan@xxxxxxxx>; Huangsj
> <huangsj@xxxxxxxx>; Fengyu Wang <wangfengyu@xxxxxxxx>; Zhiwei Ying
> <yingzhiwei@xxxxxxxx>; justin.he@xxxxxxx
> Subject: Re: [RFC PATCH v2 12/23] sched/cache: Introduce rq affinity gain
> calculation
>
> On Thu, Aug 27, 2026 at 08:28:05PM +0800, Jianyong Wu wrote:
>
> > +static int get_affi_llcs(struct sched_domain *sd, int src_llc, int dst_llc,
> > + int *affi_llcs, int *affi)
> > +{
>
> > + if (src_llc > dst_llc) {
> > + affi[j] = clamp(src_llc - dst_llc, 1, 1024);
> > + affi_llcs[j++] = i;
> > + }
>
> > + return j;
> > +}
> > +
> > +static int get_affi_numas(int src_node, int dst_node, int *affi_nodes, int
> *affi)
> > +{
>
> > + if (src_dist > dst_dist) {
> > + affi[j] = clamp(src_dist - dst_dist, 4, 1024);
> > + affi_nodes[j++] = node;
> > + }
>
> > + return j;
> > +}
> > +
> > +static int calc_affinity_numa_score(struct sched_domain *sd, int src_cpu,
> int dst_cpu,
> > + int *affi_node, int *affi, int *last_node, int *num)
> > +{
> > + int src_node, dst_node, score = 0;
> > +
> > + src_node = cpu_to_node(src_cpu);
> > + dst_node = cpu_to_node(dst_cpu);
> > + if (src_node != *last_node) {
> > + *last_node = src_node;
> > + memset(affi_node, 0, (max_lid + 1) * sizeof(int));
> > + memset(affi, 0, (max_lid + 1) * sizeof(int));
>
> This and..
>
> > + *num = get_affi_numas(src_node, dst_node, affi_node, affi);
> > + }
> > +
> > + for (int i = 0; i < *num; i++) {
> > + if ((unsigned int)affi_node[i] < nr_node_ids)
> > + score += sd->numa_counts[affi_node[i]] * affi[i];
> > + }
> > +
> > + return score;
> > +}
> > +
> > +static int calc_affinity_llc_score(struct sched_domain *sd_cur, struct
> sched_domain *sd,
> > + int src_cpu, int dst_cpu, int *affi_llc,
> > + int *affi, int *last_llc, int *num)
> > +{
> > + int src_llc, dst_llc, score = 0;
> > +
> > + src_llc = llc_id(src_cpu);
> > + dst_llc = llc_id(dst_cpu);
> > +
> > + if (src_llc != *last_llc) {
> > + *last_llc = src_llc;
> > + memset(affi_llc, 0, (max_lid + 1) * sizeof(int));
> > + memset(affi, 0, (max_lid + 1) * sizeof(int));
>
> ... this. Why do we need the memset()? AFAICT the get_affi_*() functions
> use direct assignment and the sum is limited to the number returned.
>

Yes, the memset is unnecessary. get_affi_*() assigns directly from
index 0 and the sum loop only reads the *num entries it returned, so the
rest of the array is never touched. I'll drop it.

Thanks
Jianyong
> > + *num = get_affi_llcs(sd_cur, src_llc, dst_llc, affi_llc, affi);
> > + }
> > +
> > + for (int i = 0; i < *num; i++) {
> > + if ((unsigned int)affi_llc[i] < sd->llc_max)
> > + score += sd->llc_counts[affi_llc[i]] * affi[i];
> > + }
> > +
> > + return score;
> > +}