]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
lib/div64.c: off by one in shift
authorStanislaw Gruszka <sgruszka@redhat.com>
Fri, 8 Mar 2019 00:28:18 +0000 (16:28 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 20 Apr 2019 07:15:07 +0000 (09:15 +0200)
[ Upstream commit cdc94a37493135e355dfc0b0e086d84e3eadb50d ]

fls counts bits starting from 1 to 32 (returns 0 for zero argument).  If
we add 1 we shift right one bit more and loose precision from divisor,
what cause function incorect results with some numbers.

Corrected code was tested in user-space, see bugzilla:
   https://bugzilla.kernel.org/show_bug.cgi?id=202391

Link: http://lkml.kernel.org/r/1548686944-11891-1-git-send-email-sgruszka@redhat.com
Fixes: 658716d19f8f ("div64_u64(): improve precision on 32bit platforms")
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Reported-by: Siarhei Volkau <lis8215@gmail.com>
Tested-by: Siarhei Volkau <lis8215@gmail.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
lib/div64.c

index 58e2a404097e9f5a47269138dcb843d1a39084a8..a2688b882461fd2c24228d9edd1b3e938852bb85 100644 (file)
@@ -103,7 +103,7 @@ u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
                quot = div_u64_rem(dividend, divisor, &rem32);
                *remainder = rem32;
        } else {
-               int n = 1 + fls(high);
+               int n = fls(high);
                quot = div_u64(dividend >> n, divisor >> n);
 
                if (quot != 0)
@@ -141,7 +141,7 @@ u64 div64_u64(u64 dividend, u64 divisor)
        if (high == 0) {
                quot = div_u64(dividend, divisor);
        } else {
-               int n = 1 + fls(high);
+               int n = fls(high);
                quot = div_u64(dividend >> n, divisor >> n);
 
                if (quot != 0)