mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jemmy Wong <jemmywong512@gmail.com>
To: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Jemmy Wong <jemmywong512@gmail.com>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sched: Add _TIF_NEED_RESCHED_LAZY to __resched_curr check
Date: Tue, 30 Sep 2025 01:40:44 +0800	[thread overview]
Message-ID: <173ABBF7-4EF9-410A-B36F-C15C0D01CED8@gmail.com> (raw)
In-Reply-To: <c8864652-d763-48a3-9292-24c29c25838d@amd.com>

Hi Prateek,

Thank you for the very detailed example, which helped me clearly understand the 
difference between TIF_NEED_RESCHED and TIF_NEED_RESCHED_LAZY.

The source code logic seems correct and effectively prevents repeated flag settings:
> if (cti->flags & ((1 << tif) | _TIF_NEED_RESCHED))
>     return;

The inclusion of _TIF_NEED_RESCHED in the check indicates:
1. TIF_NEED_RESCHED has higher priority/urgent than TIF_NEED_RESCHED_LAZY.
2. TIF_NEED_RESCHED_LAZY is not needed if TIF_NEED_RESCHED is set.

> On Sep 29, 2025, at 12:37 PM, K Prateek Nayak <kprateek.nayak@amd.com> wrote:
> __resched_curr() is used to set both TIF_NEED_RESCHED_LAZY and
> TIF_NEED_RESCHED.
> 
> By putting this check here, any effort to set NEED_RESCHED and force an
> early preemption will bail out if NEED_RESCHED_LAZY is already set which
> will delay the preemption.
> 
> An example:
> 
>    /* New fair task wakes up. */
>    check_preempt_wakeup_fair()
>        resched_curr_lazy()
>            __resched_curr(TIF_NEED_RESCHED_LAZY)
> 
>    /* New RT task wakes up. */
>    wakeup_preempt()
>        resched_curr()
>            __resched_curr(TIF_NEED_RESCHED)
>                /* Sees NEED_RESCHED_LAZY is already set. */
>                /* Does not do a set_preempt_need_resched() */
> 
>    ... /* Added latency */
>    sched_tick()
>        if (tif_test_bit(TIF_NEED_RESCHED_LAZY))
>            resched_curr()
>                __resched_curr(TIF_NEED_RESCHED)
>                    /* Again bails out early! */
> 
>    ... /* More latency! */
> 
> 
> So, the tick doesn't even upgrade the LAZY flag to a full NEED_RESCHED
> and the only time you actually schedule is either at exit to user mode
> or if a kthread decides to yield.
> 
> Going back to your commit message, something like:
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 7f1e5cb94c53..3275abce9ca2 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -1164,6 +1164,9 @@ static __always_inline int get_lazy_tif_bit(void)
> 
> void resched_curr_lazy(struct rq *rq)
> {
> + if (task_thread_info(rq->curr)->flags & TIF_NEED_RESCHED_MASK)
> + return;
> +
> __resched_curr(rq, get_lazy_tif_bit());
> }
> 
> probably fits the bill better.
> 
>> 
>> cpu = cpu_of(rq);
>> --
>> 2.50.1 (Apple Git-155)
>> 
> 
> -- 
> Thanks and Regards,
> Prateek
> 

Best Regards,
Jemmy


      reply	other threads:[~2025-09-29 17:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-28 15:14 Jemmy Wong
2025-09-29  4:37 ` K Prateek Nayak
2025-09-29 17:40   ` 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=173ABBF7-4EF9-410A-B36F-C15C0D01CED8@gmail.com \
    --to=jemmywong512@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    /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®