mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Vincent Guittot <vincent.guittot@linaro.org>
Cc: "Li,Rongqing" <lirongqing@baidu.com>,
	"mingo@redhat.com" <mingo@redhat.com>,
	"peterz@infradead.org" <peterz@infradead.org>,
	"juri.lelli@redhat.com" <juri.lelli@redhat.com>,
	"dietmar.eggemann@arm.com" <dietmar.eggemann@arm.com>,
	"rostedt@goodmis.org" <rostedt@goodmis.org>,
	"bsegall@google.com" <bsegall@google.com>,
	"mgorman@suse.de" <mgorman@suse.de>,
	"vschneid@redhat.com" <vschneid@redhat.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] sched/cputime: Fix a mostly theoretical divide by zero
Date: Mon, 16 Jun 2025 15:48:01 +0100	[thread overview]
Message-ID: <20250616154801.0dc94c1f@pumpkin> (raw)
In-Reply-To: <CAKfTPtBKm7WgwcUHcMXnH3PL3KFSniy02mb6Ag4eQ1fA3e+H3g@mail.gmail.com>

On Mon, 16 Jun 2025 14:51:56 +0200
Vincent Guittot <vincent.guittot@linaro.org> wrote:

> On Mon, 16 Jun 2025 at 13:46, Li,Rongqing <lirongqing@baidu.com> wrote:
> >  
> > >
> > > Sum of utime and stime can overflow to 0, when a process with many threads
> > > run over total 2^64 ns  
> 
> Theoretical is the right word; If all 2^32 possible threads belong to
> the process, we can get an overflow to 0 after ~4sec run time of each
> thread. But then how long will it take to have those 2^32 threads run
> 4sec on a system ...
> 
> It would be good to get number to show how realistic or not it could
> be to reach this value

I did wonder when re-writing mul_u64_u64_div_u64() how common this path
is and whether both stime and utime could be zero.

The current mul_u64_u64_div_u64() is particularly horrid on 32bit.
(Note that it no longer generates an approximate result.)
On 32bit x86 the worst case (lots of 1 bits in the result) is ~900 clocks,
my new version takes ~130 for pretty much all (large) values.

That is in userspace with cmov, without cmov it will be worse.
I also think the kernel has one less register to play with - %epb.

Other architectures are likely to be worse, sh[rl]d makes double
length shifts less painful - especially when combined with cmov.

See: https://www.spinics.net/lists/kernel/msg5723178.html

	David


> 
> > >
> > > Signed-off-by: Li RongQing <lirongqing@baidu.com>  
> >
> >
> > Ping
> >
> > Thanks
> >
> > -Li
> >  
> > > ---
> > >  kernel/sched/cputime.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index
> > > 6dab4854..c35fc4c 100644
> > > --- a/kernel/sched/cputime.c
> > > +++ b/kernel/sched/cputime.c
> > > @@ -579,7 +579,8 @@ void cputime_adjust(struct task_cputime *curr, struct
> > > prev_cputime *prev,
> > >               goto update;
> > >       }
> > >
> > > -     stime = mul_u64_u64_div_u64(stime, rtime, stime + utime);
> > > +     if (likely(stime + utime))
> > > +             stime = mul_u64_u64_div_u64(stime, rtime, stime + utime);
> > >       /*
> > >        * Because mul_u64_u64_div_u64() can approximate on some
> > >        * achitectures; enforce the constraint that: a*b/(b+c) <= a.
> > > --
> > > 2.9.4  
> >  
> 


  reply	other threads:[~2025-06-16 14:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-04 11:04 lirongqing
2025-06-16 11:46 ` 答复: " Li,Rongqing
2025-06-16 12:51   ` Vincent Guittot
2025-06-16 14:48     ` David Laight [this message]
2025-06-17  6:36     ` 答复: [外部邮件] " Li,Rongqing

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=20250616154801.0dc94c1f@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lirongqing@baidu.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 \
    /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®