linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Linus Torvalds' <torvalds@linux-foundation.org>
Cc: Jules Bashizi Irenge <jbi.octave@gmail.com>,
	"linux-perf-users@vger.kernel.org"
	<linux-perf-users@vger.kernel.org>
Subject: RE: [PATCH] perf/x86/amd/power: Use div64_u64 onstead of do_div()
Date: Mon, 6 May 2024 17:43:25 +0000	[thread overview]
Message-ID: <bd71228e767a4d589afe8fbda957b74f@AcuMS.aculab.com> (raw)
In-Reply-To: <CAHk-=wh8=yVY5vJC0EgM8=-4FOFxA+0w-OoLsZDypjpjsB5www@mail.gmail.com>

From: Linus Torvalds
> Sent: 06 May 2024 17:46
> 
> On Mon, 6 May 2024 at 09:40, David Laight <David.Laight@aculab.com> wrote:
> > >
> > > We don 't do stupid things in the kernel. We basically never need the
> > > 64-by-64 divide.
> >
> > There are 381 references to div64_u64() some of them are just plain stupid.
> > Constant divisors of 100 and 1000 and div64_u64(x_ns * 100, y_us * 1000).
> 
> Sadly, some peopel will get the compile error for "you can't do that"
> because they did a regular "a = b / c" and gcc created a mess, and
> then they mindlessly just replace it with div64_u64".

A bit like the mindless min_t() - not hard to find:
	len = min_t(unsigned int, len, sizeof (int));
	if (len < 0)
		return -ERANGE;

> > But someone wrote a script that complains that is a full 64bit divide
> > on 64bit systems - so suggests div64_u64().
> 
> Yeah, that's well-intentioned, but it screws up.

The suggestion should be to double check the domain of the divisor,
and then change the type to u32.

> The reason is that "do_div()" _always_ does a 64-by-32 divide, and
> then if you have a divisor that is "long" (which is indeed 32 bit on
> 32-bit architectures), now on 64-bit architectures it truncates what
> can be a 64-bit value.
> 
> So we probably _should_ have made "do_div()" be a "64-by-long" divide.

Or got through the kernel and change lots of the 'long' to 'u32' :-)
I'm sure a lot are there because people were scared int might be 16bit.
Perhaps M$ got it right by leaving 'long' as 32bit :-)
But really what was the last 16bit Unix?
I never used ICL's 286 unix port - that might have been 16bit.
All the 386 ones were 32bit, as was the VAX (never used one).
So we're looking at 1970's systems like the AT&T 3B2 and DEC pdp-11.
Certainly far before you'd even though of Linux.

> On actual 64-bit architectures, doing a 64-bti divide is typically
> fine (I can imagine some broken cases, but not enough to care).

Well Intel x86 always take twice as long for the full 128 by 64 divide
than the 64 by 32 one (amd is the same speed if the values fit).
So selecting between the two divides may make sense.
OTOH you probably don't want to speculatively execute the wrong divide! 

> The whole problem is because of 32-bit architectures.

Indeed.
Both x86 and m68k have a 64 by 32 divide. I'm not whether any other do.
Nios-II wont (it may not even have divide), probably all MIPS derivatives
are the same (including RISC-V).
The suggested divide function in my sparc-32 book is several pages long.
(Quite what does the to I-cache is any-bodies guess.)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

      parent reply	other threads:[~2024-05-06 17:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-28 16:40 [PATCH] perf/x86/amd/power: Use div64_u64 onstead of do_div() Jules Irenge
2024-05-02 16:25 ` David Laight
2024-05-02 16:34   ` Jules Bashizi Irenge
2024-05-02 22:18     ` David Laight
2024-05-02 22:59       ` Linus Torvalds
2024-05-04 22:53         ` David Laight
2024-05-05 19:38           ` Linus Torvalds
2024-05-06 16:39             ` David Laight
2024-05-06 16:45               ` Linus Torvalds
2024-05-06 17:26                 ` Linus Torvalds
2024-05-06 20:25                   ` David Laight
2024-05-06 17:43                 ` David Laight [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=bd71228e767a4d589afe8fbda957b74f@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=jbi.octave@gmail.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=torvalds@linux-foundation.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;
as well as URLs for NNTP newsgroup(s).