mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luo Gengkun <luogengkun2@huawei.com>
To: "Chen, Yu C" <yu.c.chen@intel.com>,
	Tim Chen <tim.c.chen@linux.intel.com>
Cc: <dietmar.eggemann@arm.com>, <rostedt@goodmis.org>,
	<peterz@infradead.org>, <bsegall@google.com>, <mgorman@suse.de>,
	<vschneid@redhat.com>, <kprateek.nayak@amd.com>,
	<linux-kernel@vger.kernel.org>, <mingo@redhat.com>,
	<juri.lelli@redhat.com>, <vincent.guittot@linaro.org>,
	Chen Yu <chen.yu@linux.dev>
Subject: Re: : [PATCH v8 1/2] sched/cache: Reduce the overhead of task_cache_work by only scan the visisted cpus
Date: Fri, 31 Jul 2026 16:31:31 +0800	[thread overview]
Message-ID: <60a02a7a-22ba-47d3-8bc6-e3eba6f7b4c7@huawei.com> (raw)
In-Reply-To: <84f81335-796b-4eef-9d01-510af9d9cf14@intel.com>



On 2026/7/31 14:58, Chen, Yu C wrote:
> On 7/31/2026 12:57 AM, Tim Chen wrote:
> 
> [ ... ]
> 
>>>
>>> When talking about mm->sc_stat.lock, I found an issue that may be
>>> worth paying attention. Below is the relevant code snippet:
>>>
>>> task_tick_cache()
>>> {
>>>     ...
>>>           /* avoid moving backwards */
>>>           if (time_after_eq(mm->sc_stat.epoch, epoch))
>>>                   return;
>>>
>>>           guard(raw_spinlock)(&mm->sc_stat.lock);
>>>
>>>           if (work->next == work) {
>>>                   task_work_add(p, work, TWA_RESUME);
>>>                   WRITE_ONCE(mm->sc_stat.epoch, epoch);
>>>           }
>>>     ..
>>> }
>>> Actually, I don't think time_after_eq() can effectively avoid moving
>>> backwards because this check is performed entirely outside the
>>> protection of the spinlock. The following sequence diagram describes
>>> this race condition in detail:
>>>
>>>        Thread A (CPU 0)                             Thread B (CPU 1)
>>>        ============================                 ============================
>>>        [ Initial State: mm->sc_stat.epoch = 90 ]
>>>        task_tick_cache()                            task_tick_cache()
>>>          |                                            |
>>>          +-> Read rq->cpu_epoch = 100                 +-> Read rq->cpu_epoch = 101
>>>          |                                            |
>>>          +-> Lockless Check (90, 100) -> PASS         +-> Lockless Check (90, 101) -> PASS
>>>          |                                            |
>>>          |                                            +-> Acquires lock first
>>>          |                                            +-> Writes mm->sc_stat.epoch = 101
>>>          |                                            +-> Releases lock
>>>          |                                                [ mm->sc_stat.epoch is now 101 ]
>>>          |
>>>          +-> Acquires lock
>>>          |
>>>          +-> work->next == work (STILL TRUE!
>>>          |   Because cache_work is per-thread, Thread B's
>>>          |   submission cannot clear Thread A's local state)
>>>          |
>>>          +-> WRITE_ONCE(mm->sc_stat.epoch, 100) !!! <-- BUG: Epoch moves backwards!
>>
>>
>> That would not happen because in __update_mm_sched(), Thread A will be
>> reading the updated rq->cpu_epoch and again check
>> whether time is moving backwards and will skip the update if
>> that's the case.
>>
>>
> 
> Maybe the backward comment in the code was a little confusing.
> The "avoid moving backwards" logic was introduced to prevent a negative
> timeout value in commit df0d98475954:
> 
> if (epoch - READ_ONCE(mm->sc_stat.epoch) > EPOCH_LLC_AFFINITY_TIMEOUT)
> 
> That is to say, by design, we want mm->sc_stat.epoch to chase after the
> CPU's epoch and never jump ahead of any CPU's epoch. Otherwise, the subtraction
> above could result in a huge value.
> 
> Later, in commit c1e7fe5e75ed, that negative delta was avoided by:
> 
> if ((long)(epoch - READ_ONCE(mm->sc_stat.epoch))
> So now, mm->sc_stat.epoch is only best-effort to not go backward. If it
> actually does go backward, in my opinion it’s not a big deal.
> 
> thanks,
> Chenyu
> 
> 
Thank you for your clarifying explanation :)

Gengkun
> 

  reply	other threads:[~2026-07-31  8:31 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 [this message]
2026-07-28 20:16   ` Tim Chen
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=60a02a7a-22ba-47d3-8bc6-e3eba6f7b4c7@huawei.com \
    --to=luogengkun2@huawei.com \
    --cc=bsegall@google.com \
    --cc=chen.yu@linux.dev \
    --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®