2003-09-29 13:25:25

by Artur Klauser

[permalink] [raw]
Subject: div64.h:do_div() bug

I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
conversion of timestamps in smbfs (and probably ntfs as well) from NT to
Unix format. I'll post a patch that fixes the bug, but I think it is also
present in other architectures - at least SPARC, SH, and CRIS look
suspicious.

If people with access to these architectures could run the following small
test and let me know the outcome, I can fix it there too - thanks.

//-----------------------------------------------------------------------------
#define __KERNEL__
#include <asm/types.h> // get kernel definition of u64, u32
#undef __KERNEL__
#include <asm/div64.h> // get definition of do_div()
#include <stdio.h>

main () {
union {
u64 n64;
u32 n32[2];
} in, out;

in.n32[0] = 1;
in.n32[1] = 1;
out = in;

do_div(out.n64, 1);

if (in.n64 != out.n64) {
printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
exit(1);
} else {
printf("Congratulations: asm/div64.h:do_div() handles 64-bit dividends\n");
}
return 0;
}
//-----------------------------------------------------------------------------


2003-09-29 14:18:31

by Matti Aarnio

[permalink] [raw]
Subject: Re: div64.h:do_div() bug

On Mon, Sep 29, 2003 at 03:25:19PM +0200, Artur Klauser wrote:
> I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
> conversion of timestamps in smbfs (and probably ntfs as well) from NT to
> Unix format. I'll post a patch that fixes the bug, but I think it is also
> present in other architectures - at least SPARC, SH, and CRIS look
> suspicious.
>
> If people with access to these architectures could run the following small
> test and let me know the outcome, I can fix it there too - thanks.

Call it "lack/lazyness of implementation"

Long ago it was used only in printk() debug printouts.
Now it is used all over the place. At least its users are aware of
it being slow, and not just using GCC's magic bultin codes.


> //-----------------------------------------------------------------------------
> #define __KERNEL__
> #include <asm/types.h> // get kernel definition of u64, u32
> #undef __KERNEL__
> #include <asm/div64.h> // get definition of do_div()
> #include <stdio.h>
>
> main () {
> union {
> u64 n64;
> u32 n32[2];
> } in, out;
>
> in.n32[0] = 1;
> in.n32[1] = 1;
> out = in;
>
> do_div(out.n64, 1);
>
> if (in.n64 != out.n64) {
> printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
> exit(1);
> } else {
> printf("Congratulations: asm/div64.h:do_div() handles 64-bit dividends\n");
> }
> return 0;
> }

2003-09-30 09:52:31

by Rogier Wolff

[permalink] [raw]
Subject: Re: div64.h:do_div() bug

On Mon, Sep 29, 2003 at 03:25:19PM +0200, Artur Klauser wrote:
> I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
> conversion of timestamps in smbfs (and probably ntfs as well) from NT to

Nope.

> if (in.n64 != out.n64) {
> printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");

do_div should be/is documented as not doing 64 bit dividents. It does
64/32 -> 32 divides, IIRC...

Roger.

(This EMail has a Unique reply address, so that I can count the number
of times that SWEN bites... :-)

--
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
**** "Linux is like a wigwam - no windows, no gates, apache inside!" ****

2003-09-30 10:14:46

by Matti Aarnio

[permalink] [raw]
Subject: Re: div64.h:do_div() bug

On Tue, Sep 30, 2003 at 11:52:29AM +0200, Rogier Wolff wrote:
> On Mon, Sep 29, 2003 at 03:25:19PM +0200, Artur Klauser wrote:
> > I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
> > conversion of timestamps in smbfs (and probably ntfs as well) from NT to
>
> Nope.

Nope yourself.

> > if (in.n64 != out.n64) {
> > printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
>
> do_div should be/is documented as not doing 64 bit dividents. It does
> 64/32 -> 32 divides, IIRC...

64/32 -> 64,32

The REMAINDER is 32 bit value.

> Roger.

/Matti Aarnio

2003-09-30 11:28:35

by Matti Aarnio

[permalink] [raw]
Subject: Re: div64.h:do_div() bug

On Tue, Sep 30, 2003 at 01:14:38PM +0300, Matti Aarnio wrote:
> On Tue, Sep 30, 2003 at 11:52:29AM +0200, Rogier Wolff wrote:
> > On Mon, Sep 29, 2003 at 03:25:19PM +0200, Artur Klauser wrote:
> > > I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
> > > conversion of timestamps in smbfs (and probably ntfs as well) from NT to
> >
> > Nope.
>
> Nope yourself.
>
> > > if (in.n64 != out.n64) {
> > > printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
> >
> > do_div should be/is documented as not doing 64 bit dividents. It does
> > 64/32 -> 32 divides, IIRC...
>
> 64/32 -> 64,32
>
> The REMAINDER is 32 bit value.

Non-native english speaker makes the mistake.. MODULUS is 32 bits as is
DIVISOR, REMAINDER is 64 bit, as is DIVIDEND.

That is:
DIVIDEND / DIVISOR -> REMAINDER , MODULUS

and
REMAINDER * DIVISOR + MODULUS = DIVIDEND

> > Roger.

/Matti Aarnio

2003-09-30 12:42:02

by Artur Klauser

[permalink] [raw]
Subject: Re: div64.h:do_div() bug

On Tue, 30 Sep 2003, Andreas Schwab wrote:
> Matti Aarnio <[email protected]> writes:
> > On Tue, Sep 30, 2003 at 01:14:38PM +0300, Matti Aarnio wrote:
> >> On Tue, Sep 30, 2003 at 11:52:29AM +0200, Rogier Wolff wrote:
> >> > On Mon, Sep 29, 2003 at 03:25:19PM +0200, Artur Klauser wrote:
> >> > > I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
> >> > > conversion of timestamps in smbfs (and probably ntfs as well) from NT to
> >> >
> >> > Nope.
> >>
> >> Nope yourself.
> >>
> >> > > if (in.n64 != out.n64) {
> >> > > printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
> >> >
> >> > do_div should be/is documented as not doing 64 bit dividents. It does
> >> > 64/32 -> 32 divides, IIRC...
> >>
> >> 64/32 -> 64,32
> >>
> >> The REMAINDER is 32 bit value.
> >
> > Non-native english speaker makes the mistake.. MODULUS is 32 bits as is
> > DIVISOR, REMAINDER is 64 bit, as is DIVIDEND.
> >
> > That is:
> > DIVIDEND / DIVISOR -> REMAINDER , MODULUS
> ^^^^^^^^^^^^^^^^^^^
> ???
> QUOTIENT, REMAINDER

Actually, I have to agree with Andreas here - and with Matti's 1st naming
attempt :)

But whatever the naming is, the operation AFAICT should be, and is
implemented that way in other asm-<arch>/div64.h:do_div() macros:

do_div(u64, u32)
u64 / u32 -> u64 (modifies 1st argument)
u64 % u32 -> u32 (return value)

-r2r-

2003-09-30 12:33:04

by Andreas Schwab

[permalink] [raw]
Subject: Re: div64.h:do_div() bug

Matti Aarnio <[email protected]> writes:

> On Tue, Sep 30, 2003 at 01:14:38PM +0300, Matti Aarnio wrote:
>> On Tue, Sep 30, 2003 at 11:52:29AM +0200, Rogier Wolff wrote:
>> > On Mon, Sep 29, 2003 at 03:25:19PM +0200, Artur Klauser wrote:
>> > > I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
>> > > conversion of timestamps in smbfs (and probably ntfs as well) from NT to
>> >
>> > Nope.
>>
>> Nope yourself.
>>
>> > > if (in.n64 != out.n64) {
>> > > printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
>> >
>> > do_div should be/is documented as not doing 64 bit dividents. It does
>> > 64/32 -> 32 divides, IIRC...
>>
>> 64/32 -> 64,32
>>
>> The REMAINDER is 32 bit value.
>
> Non-native english speaker makes the mistake.. MODULUS is 32 bits as is
> DIVISOR, REMAINDER is 64 bit, as is DIVIDEND.
>
> That is:
> DIVIDEND / DIVISOR -> REMAINDER , MODULUS
^^^^^^^^^^^^^^^^^^^
???
QUOTIENT, REMAINDER

Andreas.

--
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 N?rnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."

2003-09-30 13:02:08

by Artur Klauser

[permalink] [raw]
Subject: Re: div64.h:do_div() bug - SPARC, SH anbody?

Got confirmation that ARM and CRIS experience the problem, pending info
from folks with access to SPARC or SH architectures... anybody?

-r2r-

On Mon, 29 Sep 2003, Artur Klauser wrote:
> I've found that a bug in asm-arm/div64.h:do_div() is preventing correct
> conversion of timestamps in smbfs (and probably ntfs as well) from NT to
> Unix format. I'll post a patch that fixes the bug, but I think it is also
> present in other architectures - at least SPARC, SH, and CRIS look
> suspicious.
>
> If people with access to these architectures could run the following small
> test and let me know the outcome, I can fix it there too - thanks.
>
> //-----------------------------------------------------------------------------
> #define __KERNEL__
> #include <asm/types.h> // get kernel definition of u64, u32
> #undef __KERNEL__
> #include <asm/div64.h> // get definition of do_div()
> #include <stdio.h>
>
> main () {
> union {
> u64 n64;
> u32 n32[2];
> } in, out;
>
> in.n32[0] = 1;
> in.n32[1] = 1;
> out = in;
>
> do_div(out.n64, 1);
>
> if (in.n64 != out.n64) {
> printf("FAILURE: asm/div64.h:do_div() is broken for 64-bit dividends\n");
> exit(1);
> } else {
> printf("Congratulations: asm/div64.h:do_div() handles 64-bit dividends\n");
> }
> return 0;
> }
> //-----------------------------------------------------------------------------