fls: change parameter to unsigned int

When testing in userspace, UBSAN pointed out that shifting into the sign
bit is undefined behaviour.  It doesn't really make sense to ask for the
highest set bit of a negative value, so just turn the argument type into
an unsigned int.

Some architectures (eg ppc) already had it declared as an unsigned int,
so I don't expect too many problems.

Link: http://lkml.kernel.org/r/20181105221117.31828-1-willy@infradead.org
Signed-off-by: Matthew Wilcox <willy@infradead.org>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: <linux-arch@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Matthew Wilcox 2019-01-03 15:26:41 -08:00 committed by Linus Torvalds
parent e6310f0fb5
commit 3fc2579e6f
16 changed files with 19 additions and 20 deletions

View File

@ -391,9 +391,9 @@ static inline unsigned long __fls(unsigned long x)
return fls64(x) - 1; return fls64(x) - 1;
} }
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
return fls64((unsigned int) x); return fls64(x);
} }
/* /*

View File

@ -278,7 +278,7 @@ static inline __attribute__ ((const)) int clz(unsigned int x)
return res; return res;
} }
static inline int constant_fls(int x) static inline int constant_fls(unsigned int x)
{ {
int r = 32; int r = 32;
@ -312,7 +312,7 @@ static inline int constant_fls(int x)
* @result: [1-32] * @result: [1-32]
* fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
*/ */
static inline __attribute__ ((const)) int fls(unsigned long x) static inline __attribute__ ((const)) int fls(unsigned int x)
{ {
if (__builtin_constant_p(x)) if (__builtin_constant_p(x))
return constant_fls(x); return constant_fls(x);

View File

@ -54,7 +54,7 @@ static inline unsigned long __ffs(unsigned long x)
* This is defined the same way as ffs. * This is defined the same way as ffs.
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
if (!x) if (!x)
return 0; return 0;

View File

@ -40,7 +40,7 @@ static __always_inline unsigned long __ffs(unsigned long x)
/* /*
* asm-generic/bitops/fls.h * asm-generic/bitops/fls.h
*/ */
static __always_inline int fls(int x) static __always_inline int fls(unsigned int x)
{ {
asm volatile( asm volatile(
"ff1 %0\n" "ff1 %0\n"

View File

@ -211,7 +211,7 @@ static inline long ffz(int x)
* This is defined the same way as ffs. * This is defined the same way as ffs.
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
int r; int r;

View File

@ -388,8 +388,7 @@ ia64_fls (unsigned long x)
* Find the last (most significant) bit set. Returns 0 for x==0 and * Find the last (most significant) bit set. Returns 0 for x==0 and
* bits are numbered from 1..32 (e.g., fls(9) == 4). * bits are numbered from 1..32 (e.g., fls(9) == 4).
*/ */
static inline int static inline int fls(unsigned int t)
fls (int t)
{ {
unsigned long x = t & 0xffffffffu; unsigned long x = t & 0xffffffffu;

View File

@ -502,7 +502,7 @@ static inline unsigned long __ffs(unsigned long x)
/* /*
* fls: find last bit set. * fls: find last bit set.
*/ */
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
int cnt; int cnt;

View File

@ -555,7 +555,7 @@ static inline unsigned long __ffs(unsigned long word)
* This is defined the same way as ffs. * This is defined the same way as ffs.
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
int r; int r;

View File

@ -15,7 +15,7 @@
#ifdef CONFIG_OPENRISC_HAVE_INST_FL1 #ifdef CONFIG_OPENRISC_HAVE_INST_FL1
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
int ret; int ret;

View File

@ -188,7 +188,7 @@ static __inline__ int ffs(int x)
* fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static __inline__ int fls(int x) static __inline__ int fls(unsigned int x)
{ {
int ret; int ret;
if (!x) if (!x)

View File

@ -397,9 +397,9 @@ static inline int fls64(unsigned long word)
* This is defined the same way as ffs. * This is defined the same way as ffs.
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static inline int fls(int word) static inline int fls(unsigned int word)
{ {
return fls64((unsigned int)word); return fls64(word);
} }
#else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */ #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */

View File

@ -22,7 +22,7 @@
* the cntlz instruction for much better code efficiency. * the cntlz instruction for much better code efficiency.
*/ */
static inline int fls(int x) static inline int fls(unsigned int x)
{ {
int ret; int ret;

View File

@ -448,7 +448,7 @@ static __always_inline int ffs(int x)
* set bit if value is nonzero. The last (most significant) bit is * set bit if value is nonzero. The last (most significant) bit is
* at position 32. * at position 32.
*/ */
static __always_inline int fls(int x) static __always_inline int fls(unsigned int x)
{ {
int r; int r;

View File

@ -9,7 +9,7 @@
* This is defined the same way as ffs. * This is defined the same way as ffs.
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static __always_inline int fls(int x) static __always_inline int fls(unsigned int x)
{ {
return x ? sizeof(x) * 8 - __builtin_clz(x) : 0; return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
} }

View File

@ -10,7 +10,7 @@
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static __always_inline int fls(int x) static __always_inline int fls(unsigned int x)
{ {
int r = 32; int r = 32;

View File

@ -10,7 +10,7 @@
* Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
*/ */
static __always_inline int fls(int x) static __always_inline int fls(unsigned int x)
{ {
int r = 32; int r = 32;