License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifndef _LINUX_BITOPS_H
|
|
|
|
#define _LINUX_BITOPS_H
|
2020-12-16 12:42:48 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/types.h>
|
2018-06-19 20:53:08 +08:00
|
|
|
#include <linux/bits.h>
|
2021-07-02 19:18:22 +08:00
|
|
|
#include <linux/typecheck.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-12-16 12:42:48 +08:00
|
|
|
#include <uapi/linux/kernel.h>
|
|
|
|
|
2019-10-01 09:10:52 +08:00
|
|
|
/* Set bits in the first 'n' bytes when loaded from memory */
|
|
|
|
#ifdef __LITTLE_ENDIAN
|
|
|
|
# define aligned_byte_mask(n) ((1UL << 8*(n))-1)
|
|
|
|
#else
|
|
|
|
# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n)))
|
|
|
|
#endif
|
|
|
|
|
2020-02-04 09:37:24 +08:00
|
|
|
#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
|
2020-12-16 12:42:48 +08:00
|
|
|
#define BITS_TO_LONGS(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
|
|
|
|
#define BITS_TO_U64(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
|
|
|
|
#define BITS_TO_U32(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
|
|
|
|
#define BITS_TO_BYTES(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
|
2013-10-19 05:29:07 +08:00
|
|
|
|
2010-05-03 20:57:11 +08:00
|
|
|
extern unsigned int __sw_hweight8(unsigned int w);
|
|
|
|
extern unsigned int __sw_hweight16(unsigned int w);
|
|
|
|
extern unsigned int __sw_hweight32(unsigned int w);
|
|
|
|
extern unsigned long __sw_hweight64(__u64 w);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Include this here because some architectures need generic_ffs/fls in
|
|
|
|
* scope
|
|
|
|
*/
|
|
|
|
#include <asm/bitops.h>
|
|
|
|
|
2010-03-06 05:41:37 +08:00
|
|
|
#define for_each_set_bit(bit, addr, size) \
|
2011-11-18 19:35:21 +08:00
|
|
|
for ((bit) = find_first_bit((addr), (size)); \
|
|
|
|
(bit) < (size); \
|
|
|
|
(bit) = find_next_bit((addr), (size), (bit) + 1))
|
|
|
|
|
|
|
|
/* same as for_each_set_bit() but use bit as value to start with */
|
2012-03-24 06:02:03 +08:00
|
|
|
#define for_each_set_bit_from(bit, addr, size) \
|
2011-11-18 19:35:21 +08:00
|
|
|
for ((bit) = find_next_bit((addr), (size), (bit)); \
|
|
|
|
(bit) < (size); \
|
2007-10-16 16:27:40 +08:00
|
|
|
(bit) = find_next_bit((addr), (size), (bit) + 1))
|
|
|
|
|
2012-03-24 06:02:04 +08:00
|
|
|
#define for_each_clear_bit(bit, addr, size) \
|
|
|
|
for ((bit) = find_first_zero_bit((addr), (size)); \
|
|
|
|
(bit) < (size); \
|
|
|
|
(bit) = find_next_zero_bit((addr), (size), (bit) + 1))
|
|
|
|
|
|
|
|
/* same as for_each_clear_bit() but use bit as value to start with */
|
|
|
|
#define for_each_clear_bit_from(bit, addr, size) \
|
|
|
|
for ((bit) = find_next_zero_bit((addr), (size), (bit)); \
|
|
|
|
(bit) < (size); \
|
|
|
|
(bit) = find_next_zero_bit((addr), (size), (bit) + 1))
|
|
|
|
|
bitops: introduce the for_each_set_clump8 macro
Pach series "Introduce the for_each_set_clump8 macro", v18.
While adding GPIO get_multiple/set_multiple callback support for various
drivers, I noticed a pattern of looping manifesting that would be useful
standardized as a macro.
This patchset introduces the for_each_set_clump8 macro and utilizes it
in several GPIO drivers. The for_each_set_clump macro8 facilitates a
for-loop syntax that iterates over a memory region entire groups of set
bits at a time.
For example, suppose you would like to iterate over a 32-bit integer 8
bits at a time, skipping over 8-bit groups with no set bit, where
XXXXXXXX represents the current 8-bit group:
Example: 10111110 00000000 11111111 00110011
First loop: 10111110 00000000 11111111 XXXXXXXX
Second loop: 10111110 00000000 XXXXXXXX 00110011
Third loop: XXXXXXXX 00000000 11111111 00110011
Each iteration of the loop returns the next 8-bit group that has at
least one set bit.
The for_each_set_clump8 macro has four parameters:
* start: set to the bit offset of the current clump
* clump: set to the current clump value
* bits: bitmap to search within
* size: bitmap size in number of bits
In this version of the patchset, the for_each_set_clump macro has been
reimplemented and simplified based on the suggestions provided by Rasmus
Villemoes and Andy Shevchenko in the version 4 submission.
In particular, the function of the for_each_set_clump macro has been
restricted to handle only 8-bit clumps; the drivers that use the
for_each_set_clump macro only handle 8-bit ports so a generic
for_each_set_clump implementation is not necessary. Thus, a solution
for large clumps (i.e. those larger than the width of a bitmap word)
can be postponed until a driver appears that actually requires such a
generic for_each_set_clump implementation.
For what it's worth, a semi-generic for_each_set_clump (i.e. for clumps
smaller than the width of a bitmap word) can be implemented by simply
replacing the hardcoded '8' and '0xFF' instances with respective
variables. I have not yet had a need for such an implementation, and
since it falls short of a true generic for_each_set_clump function, I
have decided to forgo such an implementation for now.
In addition, the bitmap_get_value8 and bitmap_set_value8 functions are
introduced to get and set 8-bit values respectively. Their use is based
on the behavior suggested in the patchset version 4 review.
This patch (of 14):
This macro iterates for each 8-bit group of bits (clump) with set bits,
within a bitmap memory region. For each iteration, "start" is set to
the bit offset of the found clump, while the respective clump value is
stored to the location pointed by "clump". Additionally, the
bitmap_get_value8 and bitmap_set_value8 functions are introduced to
respectively get and set an 8-bit value in a bitmap memory region.
[gustavo@embeddedor.com: fix potential sign-extension overflow]
Link: http://lkml.kernel.org/r/20191015184657.GA26541@embeddedor
[akpm@linux-foundation.org: s/ULL/UL/, per Joe]
[vilhelm.gray@gmail.com: add for_each_set_clump8 documentation]
Link: http://lkml.kernel.org/r/20191016161825.301082-1-vilhelm.gray@gmail.com
Link: http://lkml.kernel.org/r/893c3b4f03266c9496137cc98ac2b1bd27f92c73.1570641097.git.vilhelm.gray@gmail.com
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Suggested-by: Lukas Wunner <lukas@wunner.de>
Tested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Phil Reid <preid@electromag.com.au>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Mathias Duckeck <m.duckeck@kunbus.de>
Cc: Morten Hein Tiljeset <morten.tiljeset@prevas.dk>
Cc: Sean Nyekjaer <sean.nyekjaer@prevas.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-12-05 08:50:57 +08:00
|
|
|
/**
|
|
|
|
* for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
|
|
|
|
* @start: bit offset to start search and to store the current iteration offset
|
|
|
|
* @clump: location to store copy of current 8-bit clump
|
|
|
|
* @bits: bitmap address to base the search on
|
|
|
|
* @size: bitmap size in number of bits
|
|
|
|
*/
|
|
|
|
#define for_each_set_clump8(start, clump, bits, size) \
|
|
|
|
for ((start) = find_first_clump8(&(clump), (bits), (size)); \
|
|
|
|
(start) < (size); \
|
|
|
|
(start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
|
|
|
|
|
2015-08-04 22:15:14 +08:00
|
|
|
static inline int get_bitmask_order(unsigned int count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int order;
|
2010-01-22 22:59:29 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
order = fls(count);
|
|
|
|
return order; /* We could be slightly more clever with -1 here... */
|
|
|
|
}
|
|
|
|
|
2015-08-04 22:15:14 +08:00
|
|
|
static __always_inline unsigned long hweight_long(unsigned long w)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-06-05 07:50:30 +08:00
|
|
|
return sizeof(w) == 4 ? hweight32(w) : hweight64((__u64)w);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2012-01-15 02:44:49 +08:00
|
|
|
/**
|
|
|
|
* rol64 - rotate a 64-bit value left
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u64 rol64(__u64 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word << (shift & 63)) | (word >> ((-shift) & 63));
|
2012-01-15 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ror64 - rotate a 64-bit value right
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u64 ror64(__u64 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word >> (shift & 63)) | (word << ((-shift) & 63));
|
2012-01-15 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
2007-01-26 16:57:09 +08:00
|
|
|
/**
|
2005-04-17 06:20:36 +08:00
|
|
|
* rol32 - rotate a 32-bit value left
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u32 rol32(__u32 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word << (shift & 31)) | (word >> ((-shift) & 31));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-01-26 16:57:09 +08:00
|
|
|
/**
|
2005-04-17 06:20:36 +08:00
|
|
|
* ror32 - rotate a 32-bit value right
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u32 ror32(__u32 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word >> (shift & 31)) | (word << ((-shift) & 31));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-03-29 05:16:01 +08:00
|
|
|
/**
|
|
|
|
* rol16 - rotate a 16-bit value left
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u16 rol16(__u16 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word << (shift & 15)) | (word >> ((-shift) & 15));
|
2008-03-29 05:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ror16 - rotate a 16-bit value right
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u16 ror16(__u16 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word >> (shift & 15)) | (word << ((-shift) & 15));
|
2008-03-29 05:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rol8 - rotate an 8-bit value left
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u8 rol8(__u8 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word << (shift & 7)) | (word >> ((-shift) & 7));
|
2008-03-29 05:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ror8 - rotate an 8-bit value right
|
|
|
|
* @word: value to rotate
|
|
|
|
* @shift: bits to roll
|
|
|
|
*/
|
|
|
|
static inline __u8 ror8(__u8 word, unsigned int shift)
|
|
|
|
{
|
include/linux/bitops.h: sanitize rotate primitives
The ror32 implementation (word >> shift) | (word << (32 - shift) has
undefined behaviour if shift is outside the [1, 31] range. Similarly
for the 64 bit variants. Most callers pass a compile-time constant
(naturally in that range), but there's an UBSAN report that these may
actually be called with a shift count of 0.
Instead of special-casing that, we can make them DTRT for all values of
shift while also avoiding UB. For some reason, this was already partly
done for rol32 (which was well-defined for [0, 31]). gcc 8 recognizes
these patterns as rotates, so for example
__u32 rol32(__u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
compiles to
0000000000000020 <rol32>:
20: 89 f8 mov %edi,%eax
22: 89 f1 mov %esi,%ecx
24: d3 c0 rol %cl,%eax
26: c3 retq
Older compilers unfortunately do not do as well, but this only affects
the small minority of users that don't pass constants.
Due to integer promotions, ro[lr]8 were already well-defined for shifts
in [0, 8], and ro[lr]16 were mostly well-defined for shifts in [0, 16]
(only mostly - u16 gets promoted to _signed_ int, so if bit 15 is set,
word << 16 is undefined). For consistency, update those as well.
Link: http://lkml.kernel.org/r/20190410211906.2190-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reported-by: Ido Schimmel <idosch@mellanox.com>
Tested-by: Ido Schimmel <idosch@mellanox.com>
Reviewed-by: Will Deacon <will.deacon@arm.com>
Cc: Vadim Pasternak <vadimp@mellanox.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-15 06:43:27 +08:00
|
|
|
return (word >> (shift & 7)) | (word << ((-shift) & 7));
|
2008-03-29 05:16:01 +08:00
|
|
|
}
|
2010-08-31 03:04:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* sign_extend32 - sign extend a 32-bit value using specified bit as sign-bit
|
|
|
|
* @value: value to sign extend
|
|
|
|
* @index: 0 based bit index (0<=index<32) to sign bit
|
2015-11-07 08:30:58 +08:00
|
|
|
*
|
|
|
|
* This is safe to use for 16- and 8-bit types as well.
|
2010-08-31 03:04:01 +08:00
|
|
|
*/
|
2020-04-07 11:09:43 +08:00
|
|
|
static __always_inline __s32 sign_extend32(__u32 value, int index)
|
2010-08-31 03:04:01 +08:00
|
|
|
{
|
|
|
|
__u8 shift = 31 - index;
|
|
|
|
return (__s32)(value << shift) >> shift;
|
|
|
|
}
|
2008-03-29 05:16:01 +08:00
|
|
|
|
2015-11-07 08:31:02 +08:00
|
|
|
/**
|
|
|
|
* sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit
|
|
|
|
* @value: value to sign extend
|
|
|
|
* @index: 0 based bit index (0<=index<64) to sign bit
|
|
|
|
*/
|
2020-04-07 11:09:43 +08:00
|
|
|
static __always_inline __s64 sign_extend64(__u64 value, int index)
|
2015-11-07 08:31:02 +08:00
|
|
|
{
|
|
|
|
__u8 shift = 63 - index;
|
|
|
|
return (__s64)(value << shift) >> shift;
|
|
|
|
}
|
|
|
|
|
2006-03-25 19:08:01 +08:00
|
|
|
static inline unsigned fls_long(unsigned long l)
|
|
|
|
{
|
|
|
|
if (sizeof(l) == 4)
|
|
|
|
return fls(l);
|
|
|
|
return fls64(l);
|
|
|
|
}
|
|
|
|
|
2016-10-08 07:57:26 +08:00
|
|
|
static inline int get_count_order(unsigned int count)
|
|
|
|
{
|
2020-10-16 11:11:46 +08:00
|
|
|
if (count == 0)
|
|
|
|
return -1;
|
2016-10-08 07:57:26 +08:00
|
|
|
|
2020-10-16 11:11:46 +08:00
|
|
|
return fls(--count);
|
2016-10-08 07:57:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_count_order_long - get order after rounding @l up to power of 2
|
|
|
|
* @l: parameter
|
|
|
|
*
|
|
|
|
* it is same as get_count_order() but with long type parameter
|
|
|
|
*/
|
|
|
|
static inline int get_count_order_long(unsigned long l)
|
|
|
|
{
|
|
|
|
if (l == 0UL)
|
|
|
|
return -1;
|
2020-10-16 11:11:41 +08:00
|
|
|
return (int)fls_long(--l);
|
2016-10-08 07:57:26 +08:00
|
|
|
}
|
|
|
|
|
2009-04-23 15:48:15 +08:00
|
|
|
/**
|
|
|
|
* __ffs64 - find first set bit in a 64 bit word
|
|
|
|
* @word: The 64 bit word
|
|
|
|
*
|
2021-02-26 09:21:37 +08:00
|
|
|
* On 64 bit arches this is a synonym for __ffs
|
2009-04-23 15:48:15 +08:00
|
|
|
* The result is not defined if no bits are set, so check that @word
|
|
|
|
* is non-zero before calling this.
|
|
|
|
*/
|
|
|
|
static inline unsigned long __ffs64(u64 word)
|
|
|
|
{
|
|
|
|
#if BITS_PER_LONG == 32
|
|
|
|
if (((u32)word) == 0UL)
|
|
|
|
return __ffs((u32)(word >> 32)) + 32;
|
|
|
|
#elif BITS_PER_LONG != 64
|
|
|
|
#error BITS_PER_LONG not 32 or 64
|
|
|
|
#endif
|
|
|
|
return __ffs((unsigned long)word);
|
|
|
|
}
|
|
|
|
|
2017-10-12 18:40:10 +08:00
|
|
|
/**
|
|
|
|
* assign_bit - Assign value to a bit in memory
|
|
|
|
* @nr: the bit to set
|
|
|
|
* @addr: the address to start counting from
|
|
|
|
* @value: the value to assign
|
|
|
|
*/
|
|
|
|
static __always_inline void assign_bit(long nr, volatile unsigned long *addr,
|
|
|
|
bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
set_bit(nr, addr);
|
|
|
|
else
|
|
|
|
clear_bit(nr, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline void __assign_bit(long nr, volatile unsigned long *addr,
|
|
|
|
bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
__set_bit(nr, addr);
|
|
|
|
else
|
|
|
|
__clear_bit(nr, addr);
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:18:22 +08:00
|
|
|
/**
|
|
|
|
* __ptr_set_bit - Set bit in a pointer's value
|
|
|
|
* @nr: the bit to set
|
|
|
|
* @addr: the address of the pointer variable
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* void *p = foo();
|
|
|
|
* __ptr_set_bit(bit, &p);
|
|
|
|
*/
|
|
|
|
#define __ptr_set_bit(nr, addr) \
|
|
|
|
({ \
|
|
|
|
typecheck_pointer(*(addr)); \
|
|
|
|
__set_bit(nr, (unsigned long *)(addr)); \
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __ptr_clear_bit - Clear bit in a pointer's value
|
|
|
|
* @nr: the bit to clear
|
|
|
|
* @addr: the address of the pointer variable
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* void *p = foo();
|
|
|
|
* __ptr_clear_bit(bit, &p);
|
|
|
|
*/
|
|
|
|
#define __ptr_clear_bit(nr, addr) \
|
|
|
|
({ \
|
|
|
|
typecheck_pointer(*(addr)); \
|
|
|
|
__clear_bit(nr, (unsigned long *)(addr)); \
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __ptr_test_bit - Test bit in a pointer's value
|
|
|
|
* @nr: the bit to test
|
|
|
|
* @addr: the address of the pointer variable
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* void *p = foo();
|
|
|
|
* if (__ptr_test_bit(bit, &p)) {
|
|
|
|
* ...
|
|
|
|
* } else {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
#define __ptr_test_bit(nr, addr) \
|
|
|
|
({ \
|
|
|
|
typecheck_pointer(*(addr)); \
|
|
|
|
test_bit(nr, (unsigned long *)(addr)); \
|
|
|
|
})
|
|
|
|
|
2008-03-11 23:17:19 +08:00
|
|
|
#ifdef __KERNEL__
|
2008-04-01 17:46:19 +08:00
|
|
|
|
2014-03-30 22:20:01 +08:00
|
|
|
#ifndef set_mask_bits
|
2018-10-15 21:43:06 +08:00
|
|
|
#define set_mask_bits(ptr, mask, bits) \
|
2014-03-30 22:20:01 +08:00
|
|
|
({ \
|
2018-10-15 21:43:06 +08:00
|
|
|
const typeof(*(ptr)) mask__ = (mask), bits__ = (bits); \
|
|
|
|
typeof(*(ptr)) old__, new__; \
|
2014-03-30 22:20:01 +08:00
|
|
|
\
|
|
|
|
do { \
|
2018-10-15 21:43:06 +08:00
|
|
|
old__ = READ_ONCE(*(ptr)); \
|
|
|
|
new__ = (old__ & ~mask__) | bits__; \
|
|
|
|
} while (cmpxchg(ptr, old__, new__) != old__); \
|
2014-03-30 22:20:01 +08:00
|
|
|
\
|
2019-03-08 08:28:14 +08:00
|
|
|
old__; \
|
2014-03-30 22:20:01 +08:00
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 10:22:13 +08:00
|
|
|
#ifndef bit_clear_unless
|
2018-10-15 21:43:06 +08:00
|
|
|
#define bit_clear_unless(ptr, clear, test) \
|
2016-05-04 10:22:13 +08:00
|
|
|
({ \
|
2018-10-15 21:43:06 +08:00
|
|
|
const typeof(*(ptr)) clear__ = (clear), test__ = (test);\
|
|
|
|
typeof(*(ptr)) old__, new__; \
|
2016-05-04 10:22:13 +08:00
|
|
|
\
|
|
|
|
do { \
|
2018-10-15 21:43:06 +08:00
|
|
|
old__ = READ_ONCE(*(ptr)); \
|
|
|
|
new__ = old__ & ~clear__; \
|
|
|
|
} while (!(old__ & test__) && \
|
|
|
|
cmpxchg(ptr, old__, new__) != old__); \
|
2016-05-04 10:22:13 +08:00
|
|
|
\
|
2018-10-15 21:43:06 +08:00
|
|
|
!(old__ & test__); \
|
2016-05-04 10:22:13 +08:00
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2008-03-11 23:17:19 +08:00
|
|
|
#endif /* __KERNEL__ */
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|