From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F342A3C8C77 for ; Mon, 20 Jul 2026 22:09:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784585373; cv=none; b=Z2tR7BYDKa0AadxJ1GsOb/d6lwEWvGWLra36OF1s0GPyWNwxq4p41ik50i2LqUrIGRETbikx1JfLX1RxxoN0ZXj2cb2fKuKiiCHLxp30H4S95bFHDsrIq6UvaY0TOdvV2EkXO6iFINzqjPg/7rYtjqgpoVnYAm4FIlRfcgcCxoY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784585373; c=relaxed/simple; bh=j2uFqRJTtq0oHCqZwh7ihvOPg4hr9XZlqCnI1xq0VF4=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=ewWcgbFlPNkMkjWG96lyL0FXL3+euMRuUpQ4PsxUzz/FpW2MjwCAlZBm5VgttttVPiO2TGLCk78BmEBkSxFzJYasbtifABJoKhlBX8vaprrNOIOacDu85DgMaeOFfOvUto1F5VgKITGi4Esy0CcnyhexnAKZYXU07jMHmiwLG7Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Az0aIugX; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Az0aIugX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1784585372; x=1816121372; h=message-id:subject:from:to:cc:date:in-reply-to: references:content-transfer-encoding:mime-version; bh=j2uFqRJTtq0oHCqZwh7ihvOPg4hr9XZlqCnI1xq0VF4=; b=Az0aIugXEANqMDYvHZvUXa7QpG+LNDltMAmYFy5MELbYetLJa8gqEOJi GH831NrI2TzpO92d4QbzdK3MvmyE26s4/Ap2caPY2VQLHHpQ5f+Jbjmde +uMDfFWvDZI3HenTql8jiSi/5sWB6M7HLTejrBxy7RVusnJF7PCMo+VcD sMrNYPUt5U3OwOh6I8XyMOio2hiAtbEAFpQWgl8qYJYwfkORUkqM5fDFe 0yLKwiwzXQZWLSZcB996rqLjhhBv4q5EaPIZXtO/I20375NPn9ybmb1jG 5O7xnS/gTnRkJxvj2hD0CdL5tQdtUprplpmBN1AQRUrkoF+WjoRmUNYAJ g==; X-CSE-ConnectionGUID: 1Z9TPb1gQfmxB1zNcQg8hg== X-CSE-MsgGUID: XLJIaKhXQhC+e1ANp+g0Cw== X-IronPort-AV: E=McAfee;i="6800,10657,11852"; a="96688672" X-IronPort-AV: E=Sophos;i="6.25,175,1779174000"; d="scan'208";a="96688672" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2026 15:09:31 -0700 X-CSE-ConnectionGUID: 9kzxOpUWQKSw0eebsiryFA== X-CSE-MsgGUID: 64ivub+uRUynEi0EJeXdpw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,175,1779174000"; d="scan'208";a="255797682" Received: from unknown (HELO [10.241.243.185]) ([10.241.243.185]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2026 15:09:31 -0700 Message-ID: Subject: Re: [PATCH v7 linux 1/2] sched/cache: Reduce the overhead of task_cache_work by only scan the visisted cpus From: Tim Chen To: Luo Gengkun , 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 Date: Mon, 20 Jul 2026 15:09:30 -0700 In-Reply-To: <20260720122214.3977092-2-luogengkun2@huawei.com> References: <20260720122214.3977092-1-luogengkun2@huawei.com> <20260720122214.3977092-2-luogengkun2@huawei.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.1 (3.58.1-1.fc43) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Mon, 2026-07-20 at 12:22 +0000, Luo Gengkun wrote: > The overhead of task_cache_work() is high, especially in multi-NUMA syste= ms. > Currently, task_cache_work() tries to find the pref_llc by scanning all C= PUs > in the system. However, most of these scans are meaningless, such as thos= e > for CPUs that have never been visited or were accessed a long time ago. >=20 > 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. >=20 > Signed-off-by: Luo Gengkun > --- > include/linux/mm_types.h | 6 +++++ > include/linux/sched.h | 2 ++ > kernel/sched/fair.c | 48 +++++++++++++++++++++++++++++++--------- > 3 files changed, 46 insertions(+), 10 deletions(-) >=20 > 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; > =20 > + 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_struc= t *mm) > { > free_percpu(mm->sc_stat.pcpu_sched); > mm->sc_stat.pcpu_sched =3D NULL; > + free_cpumask_var(mm->sc_stat.visited_cpus); > } > #else /* !CONFIG_SCHED_CACHE */ > =20 > 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; > }; > =20 > 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; > =20 > #else > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index d78467ec6ee1..c56b2df66de8 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 =3D 0; > /* a slightly stale cpu epoch is acceptible */ > pcpu_sched->epoch =3D rq->cpu_epoch; > + pcpu_sched->epoch_last_visit =3D rq->cpu_epoch; > epoch =3D rq->cpu_epoch; > } > =20 > @@ -1635,13 +1636,23 @@ static inline void __update_mm_sched(struct rq *r= q, > } > } > =20 > -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 =3D > + per_cpu_ptr(mm->sc_stat.pcpu_sched, cpu); > + struct rq *rq =3D cpu_rq(cpu); > + > guard(raw_spinlock_irqsave)(&rq->cpu_epoch_lock); > =20 > __update_mm_sched(rq, pcpu_sched); > =20 > + /* 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=3D0.5) and as such will sum to twic= e > * the accumulation period, this means the multiplcation here should > @@ -1711,6 +1722,9 @@ void account_mm_sched(struct rq *rq, struct task_st= ruct *p, s64 delta_exec) > pcpu_sched->runtime +=3D delta_exec; > rq->cpu_runtime +=3D delta_exec; > epoch =3D rq->cpu_epoch; > + pcpu_sched->epoch_last_visit =3D epoch; We need to make sure that the epoch_last_visit update is seen by the cpu running task_cache_work(), before it attempts to do the epoch comparison and clear the cpu, and causing inconsistency in the visited_cpus. For example CPU A (e.g. doing LLC/affinity selection, reading remote pcpu_sched) C= PU B (=3D `cpu`, running account_mm_sched() locally) -------------------------------------------------------------------- -= ---------------------------------------------------- read pcpu_sched->epoch_last_visit (stale, old value) -> looks like it timed out = pcpu_sched->epoch_last_visit =3D epoch (fresh visit!) = cpumask_set_cpu(cpu, visited_cpus) (correctly marks it visited) cpumask_clear_cpu(cpu, visited_cpus) <-- wipes out the fresh set! Perhaps we will need a memory barrier and double check for expiration befor= e we clear the visited mask. if (rq->cpu_epoch - READ_ONCE(pcpu_sched->epoch_last_visit > llc_epoch_aff= inity_timeout && cpumask_test_cpu(cpu, mm-sc_stat.visited_cpus)) { /* * account_mm_sched() may have raced in right here and refreshed * epoch_last_visit + set the bit again after we read it as stale. * Re-check timeout after applying memory barrier. */ smp_mb(); if ((rq->cpu_epoch - READ_ONCE(pcpu_sched->epoch_last_visit)) > llc_epoch_= affinity_timeout)) cpumask_clear_cpu(cpu, mm->sc_stat.visited_cpus);=09 } This fix is ugly and there's probably a better way. > + if (!cpumask_test_cpu(cpu_of(rq), mm->sc_stat.visited_cpus)) > + cpumask_set_cpu(cpu_of(rq), mm->sc_stat.visited_cpus); > } > =20 > /* > @@ -1867,6 +1881,18 @@ static void task_cache_work(struct callback_head *= work) > guard(rcu)(); > =20 > get_scan_cpumasks(cpus, p); Now that we know exactly which cpus to scan from visited_cpus, we can get rid of get_scan_cpumasks(), which attempts to limit the scope of cpu scan. Thanks. Tim > + /* > + * 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, cpus, mm->sc_stat.visited_cpus); > =20 > for_each_cpu(cpu, cpus) { > /* XXX sched_cluster_active */ > @@ -1877,19 +1903,21 @@ static void task_cache_work(struct callback_head = *work) > if (!sd) > continue; > =20 > - for_each_cpu(i, sched_domain_span(sd)) { > - occ =3D 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 =3D rcu_dereference_all(cpu_rq(i)->curr); > + if (cur && !(cur->flags & (PF_EXITING | PF_KTHREAD)) && > + cur->mm =3D=3D mm) > + nr_running++; > + > + occ =3D fraction_mm_sched(i, mm); > + if (occ =3D=3D 0) > + continue; > + > a_occ +=3D occ; > if (occ > m_occ) { > m_occ =3D occ; > m_cpu =3D i; > } > - > - cur =3D rcu_dereference_all(cpu_rq(i)->curr); > - if (cur && !(cur->flags & (PF_EXITING | PF_KTHREAD)) && > - cur->mm =3D=3D mm) > - nr_running++; > } > =20 > /*