Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Matthew Wilcox and committed by
Linus Torvalds
3fc2579e e6310f0f

+19 -20
+2 -2
arch/alpha/include/asm/bitops.h
··· 391 391 return fls64(x) - 1; 392 392 } 393 393 394 - static inline int fls(int x) 394 + static inline int fls(unsigned int x) 395 395 { 396 - return fls64((unsigned int) x); 396 + return fls64(x); 397 397 } 398 398 399 399 /*
+2 -2
arch/arc/include/asm/bitops.h
··· 278 278 return res; 279 279 } 280 280 281 - static inline int constant_fls(int x) 281 + static inline int constant_fls(unsigned int x) 282 282 { 283 283 int r = 32; 284 284 ··· 312 312 * @result: [1-32] 313 313 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0 314 314 */ 315 - static inline __attribute__ ((const)) int fls(unsigned long x) 315 + static inline __attribute__ ((const)) int fls(unsigned int x) 316 316 { 317 317 if (__builtin_constant_p(x)) 318 318 return constant_fls(x);
+1 -1
arch/c6x/include/asm/bitops.h
··· 54 54 * This is defined the same way as ffs. 55 55 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 56 56 */ 57 - static inline int fls(int x) 57 + static inline int fls(unsigned int x) 58 58 { 59 59 if (!x) 60 60 return 0;
+1 -1
arch/csky/include/asm/bitops.h
··· 40 40 /* 41 41 * asm-generic/bitops/fls.h 42 42 */ 43 - static __always_inline int fls(int x) 43 + static __always_inline int fls(unsigned int x) 44 44 { 45 45 asm volatile( 46 46 "ff1 %0\n"
+1 -1
arch/hexagon/include/asm/bitops.h
··· 211 211 * This is defined the same way as ffs. 212 212 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 213 213 */ 214 - static inline int fls(int x) 214 + static inline int fls(unsigned int x) 215 215 { 216 216 int r; 217 217
+1 -2
arch/ia64/include/asm/bitops.h
··· 388 388 * Find the last (most significant) bit set. Returns 0 for x==0 and 389 389 * bits are numbered from 1..32 (e.g., fls(9) == 4). 390 390 */ 391 - static inline int 392 - fls (int t) 391 + static inline int fls(unsigned int t) 393 392 { 394 393 unsigned long x = t & 0xffffffffu; 395 394
+1 -1
arch/m68k/include/asm/bitops.h
··· 502 502 /* 503 503 * fls: find last bit set. 504 504 */ 505 - static inline int fls(int x) 505 + static inline int fls(unsigned int x) 506 506 { 507 507 int cnt; 508 508
+1 -1
arch/mips/include/asm/bitops.h
··· 555 555 * This is defined the same way as ffs. 556 556 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 557 557 */ 558 - static inline int fls(int x) 558 + static inline int fls(unsigned int x) 559 559 { 560 560 int r; 561 561
+1 -1
arch/openrisc/include/asm/bitops/fls.h
··· 15 15 16 16 #ifdef CONFIG_OPENRISC_HAVE_INST_FL1 17 17 18 - static inline int fls(int x) 18 + static inline int fls(unsigned int x) 19 19 { 20 20 int ret; 21 21
+1 -1
arch/parisc/include/asm/bitops.h
··· 188 188 * fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 189 189 */ 190 190 191 - static __inline__ int fls(int x) 191 + static __inline__ int fls(unsigned int x) 192 192 { 193 193 int ret; 194 194 if (!x)
+2 -2
arch/s390/include/asm/bitops.h
··· 397 397 * This is defined the same way as ffs. 398 398 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 399 399 */ 400 - static inline int fls(int word) 400 + static inline int fls(unsigned int word) 401 401 { 402 - return fls64((unsigned int)word); 402 + return fls64(word); 403 403 } 404 404 405 405 #else /* CONFIG_HAVE_MARCH_Z9_109_FEATURES */
+1 -1
arch/unicore32/include/asm/bitops.h
··· 22 22 * the cntlz instruction for much better code efficiency. 23 23 */ 24 24 25 - static inline int fls(int x) 25 + static inline int fls(unsigned int x) 26 26 { 27 27 int ret; 28 28
+1 -1
arch/x86/include/asm/bitops.h
··· 448 448 * set bit if value is nonzero. The last (most significant) bit is 449 449 * at position 32. 450 450 */ 451 - static __always_inline int fls(int x) 451 + static __always_inline int fls(unsigned int x) 452 452 { 453 453 int r; 454 454
+1 -1
include/asm-generic/bitops/builtin-fls.h
··· 9 9 * This is defined the same way as ffs. 10 10 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 11 11 */ 12 - static __always_inline int fls(int x) 12 + static __always_inline int fls(unsigned int x) 13 13 { 14 14 return x ? sizeof(x) * 8 - __builtin_clz(x) : 0; 15 15 }
+1 -1
include/asm-generic/bitops/fls.h
··· 10 10 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 11 11 */ 12 12 13 - static __always_inline int fls(int x) 13 + static __always_inline int fls(unsigned int x) 14 14 { 15 15 int r = 32; 16 16
+1 -1
tools/include/asm-generic/bitops/fls.h
··· 10 10 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 11 11 */ 12 12 13 - static __always_inline int fls(int x) 13 + static __always_inline int fls(unsigned int x) 14 14 { 15 15 int r = 32; 16 16