mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	"Li,Rongqing" <lirongqing@baidu.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	linux-kernel@vger.kernel.org, x86@kernel.org
Subject: Re: [PATCH] x86/math64: handle #DE in mul_u64_u64_div_u64()
Date: Tue, 22 Jul 2025 13:09:47 +0100	[thread overview]
Message-ID: <20250722130947.0c97c96a@pumpkin> (raw)
In-Reply-To: <20250722105034.GA2845@redhat.com>

On Tue, 22 Jul 2025 12:50:35 +0200
Oleg Nesterov <oleg@redhat.com> wrote:

> On 07/21, David Laight wrote:
> >
> > On Mon, 21 Jul 2025 15:04:22 +0200
> > Oleg Nesterov <oleg@redhat.com> wrote:
> >  
> > > Change mul_u64_u64_div_u64() to return ULONG_MAX if the result doesn't
> > > fit u64, this matches the generic implementation in lib/math/div64.c.  
> >
> > Not quite, the generic version is likely to trap on divide by zero.  
> 
> I meant that the generic implementation returns -1ul too if the result
> doesn't fit into u64.
> 
> > I think it would be better to always trap (eg BUG_ON(!div)).  
> 
> Well, I don't like adding a BUG_ON(), but OK.
> 
> > The trouble there is that (an ignored) ~(u64)0 is likely to cause another
> > arithmetic overflow with even more consequences.
> >
> > So I'm not at all sure what it should look like or whether 0 is a better
> > error return (esp for div == 0).  
> 
> I'm not sure either but x86/generic versions should be consistent. Let's
> discuss this and possibly change both implementations later?

My thought as well.
Getting both to agree is a start.

My latest thought is to add another parameter for the return value
when the result overflows or is infinity/NaN.
So the calling code can get 0, 1, ~0 (or any other 'safe' value) returned.
A special 'magic' value could be used to mean BUG().

> 
> > >  static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
> > >  {
> > > +	int ok = 0;
> > >  	u64 q;
> > >
> > > -	asm ("mulq %2; divq %3" : "=a" (q)
> > > -				: "a" (a), "rm" (mul), "rm" (div)
> > > -				: "rdx");
> > > +	asm ("mulq %3; 1: divq %4; movl $1,%1; 2:\n"  
> >
> > The "movl $1,%1" is a 5 byte instruction.
> > Better to use either 'incl' or get the constraints right for 'movb'  
> 
> Agreed, thanks,
> 
> > > +	if (ok)
> > > +		return q;
> > > +	WARN_ON_ONCE(!div);  
> >
> > I think you need to WARN for overflow as well as divide by zero.  
> 
> The generic implementation doesn't WARN... OK, I won't argue.

I've a set of patches I need to do a new version of.
I'll add a WARN_ON_ONCE() to the generic version.
I'll also put a copy of this patch in my set so that the later patches
will apply after this is applied without too much hastle.

> How about
> 
> 	static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
> 	{
> 		char ok = 0;
> 		u64 q;
> 
> 		asm ("mulq %3; 1: divq %4; movb $1,%1; 2:\n"
> 			_ASM_EXTABLE(1b, 2b)
> 			: "=a" (q), "+r" (ok)

That needs to be "+q" (ok)

> 			: "a" (a), "rm" (mul), "rm" (div)
> 			: "rdx");
> 
> 		if (ok)
> 			return q;
> 		BUG_ON(!div);
> 		WARN_ON_ONCE(1);

I know there are are a lot of WARN_ON_ONCE(1) out there,
but maybe WARN_ON_ONCE("muldiv overflow") would be better?
(The linker will merge the strings).

	David

> 		return ~(u64)0;
> 	}
> 
> ?
> 
> Oleg.
> 


  reply	other threads:[~2025-07-22 12:09 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-21 13:04 Oleg Nesterov
2025-07-21 18:20 ` David Laight
2025-07-22 10:50   ` Oleg Nesterov
2025-07-22 12:09     ` David Laight [this message]
2025-07-22 13:21       ` Oleg Nesterov
2025-07-22 22:03         ` David Laight
2025-07-23  9:38           ` Oleg Nesterov
2025-07-23 21:48             ` David Laight
2025-07-24  8:11               ` Oleg Nesterov
2025-07-24  8:25                 ` Oleg Nesterov
2025-07-24 11:14                   ` Oleg Nesterov
2025-07-25  1:00                     ` H. Peter Anvin
2025-07-25 10:12                       ` Oleg Nesterov
2025-07-25 21:46                         ` David Laight
2025-07-26  9:55                           ` Oleg Nesterov
2025-07-24 12:00                   ` David Laight
2025-07-24 13:58                     ` Oleg Nesterov
2025-07-22 16:53       ` H. Peter Anvin
2025-07-22 21:53         ` David Laight
2025-07-22 16:50     ` H. Peter Anvin
2025-07-22 17:58       ` Oleg Nesterov
2025-07-22 18:12         ` H. Peter Anvin
2025-07-22 18:38           ` Oleg Nesterov
2025-07-22 19:26             ` H. Peter Anvin
2025-07-22 21:56             ` David Laight
2025-07-27 12:34 ` [PATCH v2] " Oleg Nesterov
2025-07-28 18:53   ` David Laight
2025-07-30  2:30   ` [????] " 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=20250722130947.0c97c96a@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lirongqing@baidu.com \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=x86@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®