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);
|
|
|
|
|
2022-06-24 20:13:09 +08:00
|
|
|
/*
|
|
|
|
* Defined here because those may be needed by architecture-specific static
|
|
|
|
* inlines.
|
|
|
|
*/
|
|
|
|
|
bitops: unify non-atomic bitops prototypes across architectures
Currently, there is a mess with the prototypes of the non-atomic
bitops across the different architectures:
ret bool, int, unsigned long
nr int, long, unsigned int, unsigned long
addr volatile unsigned long *, volatile void *
Thankfully, it doesn't provoke any bugs, but can sometimes make
the compiler angry when it's not handy at all.
Adjust all the prototypes to the following standard:
ret bool retval can be only 0 or 1
nr unsigned long native; signed makes no sense
addr volatile unsigned long * bitmaps are arrays of ulongs
Next, some architectures don't define 'arch_' versions as they don't
support instrumentation, others do. To make sure there is always the
same set of callables present and to ease any potential future
changes, make them all follow the rule:
* architecture-specific files define only 'arch_' versions;
* non-prefixed versions can be defined only in asm-generic files;
and place the non-prefixed definitions into a new file in
asm-generic to be included by non-instrumented architectures.
Finally, add some static assertions in order to prevent people from
making a mess in this room again.
I also used the %__always_inline attribute consistently, so that
they always get resolved to the actual operations.
Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 20:13:07 +08:00
|
|
|
#include <asm-generic/bitops/generic-non-atomic.h>
|
|
|
|
|
bitops: let optimize out non-atomic bitops on compile-time constants
Currently, many architecture-specific non-atomic bitop
implementations use inline asm or other hacks which are faster or
more robust when working with "real" variables (i.e. fields from
the structures etc.), but the compilers have no clue how to optimize
them out when called on compile-time constants. That said, the
following code:
DECLARE_BITMAP(foo, BITS_PER_LONG) = { }; // -> unsigned long foo[1];
unsigned long bar = BIT(BAR_BIT);
unsigned long baz = 0;
__set_bit(FOO_BIT, foo);
baz |= BIT(BAZ_BIT);
BUILD_BUG_ON(!__builtin_constant_p(test_bit(FOO_BIT, foo));
BUILD_BUG_ON(!__builtin_constant_p(bar & BAR_BIT));
BUILD_BUG_ON(!__builtin_constant_p(baz & BAZ_BIT));
triggers the first assertion on x86_64, which means that the
compiler is unable to evaluate it to a compile-time initializer
when the architecture-specific bitop is used even if it's obvious.
In order to let the compiler optimize out such cases, expand the
bitop() macro to use the "constant" C non-atomic bitop
implementations when all of the arguments passed are compile-time
constants, which means that the result will be a compile-time
constant as well, so that it produces more efficient and simple
code in 100% cases, comparing to the architecture-specific
counterparts.
The savings are architecture, compiler and compiler flags dependent,
for example, on x86_64 -O2:
GCC 12: add/remove: 78/29 grow/shrink: 332/525 up/down: 31325/-61560 (-30235)
LLVM 13: add/remove: 79/76 grow/shrink: 184/537 up/down: 55076/-141892 (-86816)
LLVM 14: add/remove: 10/3 grow/shrink: 93/138 up/down: 3705/-6992 (-3287)
and ARM64 (courtesy of Mark):
GCC 11: add/remove: 92/29 grow/shrink: 933/2766 up/down: 39340/-82580 (-43240)
LLVM 14: add/remove: 21/11 grow/shrink: 620/651 up/down: 12060/-15824 (-3764)
Cc: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Reviewed-by: Marco Elver <elver@google.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 20:13:10 +08:00
|
|
|
/*
|
|
|
|
* Many architecture-specific non-atomic bitops contain inline asm code and due
|
|
|
|
* to that the compiler can't optimize them to compile-time expressions or
|
|
|
|
* constants. In contrary, generic_*() helpers are defined in pure C and
|
|
|
|
* compilers optimize them just well.
|
|
|
|
* Therefore, to make `unsigned long foo = 0; __set_bit(BAR, &foo)` effectively
|
|
|
|
* equal to `unsigned long foo = BIT(BAR)`, pick the generic C alternative when
|
|
|
|
* the arguments can be resolved at compile time. That expression itself is a
|
|
|
|
* constant and doesn't bring any functional changes to the rest of cases.
|
|
|
|
* The casts to `uintptr_t` are needed to mitigate `-Waddress` warnings when
|
|
|
|
* passing a bitmap from .bss or .data (-> `!!addr` is always true).
|
|
|
|
*/
|
2022-06-24 20:13:09 +08:00
|
|
|
#define bitop(op, nr, addr) \
|
bitops: let optimize out non-atomic bitops on compile-time constants
Currently, many architecture-specific non-atomic bitop
implementations use inline asm or other hacks which are faster or
more robust when working with "real" variables (i.e. fields from
the structures etc.), but the compilers have no clue how to optimize
them out when called on compile-time constants. That said, the
following code:
DECLARE_BITMAP(foo, BITS_PER_LONG) = { }; // -> unsigned long foo[1];
unsigned long bar = BIT(BAR_BIT);
unsigned long baz = 0;
__set_bit(FOO_BIT, foo);
baz |= BIT(BAZ_BIT);
BUILD_BUG_ON(!__builtin_constant_p(test_bit(FOO_BIT, foo));
BUILD_BUG_ON(!__builtin_constant_p(bar & BAR_BIT));
BUILD_BUG_ON(!__builtin_constant_p(baz & BAZ_BIT));
triggers the first assertion on x86_64, which means that the
compiler is unable to evaluate it to a compile-time initializer
when the architecture-specific bitop is used even if it's obvious.
In order to let the compiler optimize out such cases, expand the
bitop() macro to use the "constant" C non-atomic bitop
implementations when all of the arguments passed are compile-time
constants, which means that the result will be a compile-time
constant as well, so that it produces more efficient and simple
code in 100% cases, comparing to the architecture-specific
counterparts.
The savings are architecture, compiler and compiler flags dependent,
for example, on x86_64 -O2:
GCC 12: add/remove: 78/29 grow/shrink: 332/525 up/down: 31325/-61560 (-30235)
LLVM 13: add/remove: 79/76 grow/shrink: 184/537 up/down: 55076/-141892 (-86816)
LLVM 14: add/remove: 10/3 grow/shrink: 93/138 up/down: 3705/-6992 (-3287)
and ARM64 (courtesy of Mark):
GCC 11: add/remove: 92/29 grow/shrink: 933/2766 up/down: 39340/-82580 (-43240)
LLVM 14: add/remove: 21/11 grow/shrink: 620/651 up/down: 12060/-15824 (-3764)
Cc: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Reviewed-by: Marco Elver <elver@google.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 20:13:10 +08:00
|
|
|
((__builtin_constant_p(nr) && \
|
|
|
|
__builtin_constant_p((uintptr_t)(addr) != (uintptr_t)NULL) && \
|
|
|
|
(uintptr_t)(addr) != (uintptr_t)NULL && \
|
|
|
|
__builtin_constant_p(*(const unsigned long *)(addr))) ? \
|
|
|
|
const##op(nr, addr) : op(nr, addr))
|
2022-06-24 20:13:09 +08:00
|
|
|
|
|
|
|
#define __set_bit(nr, addr) bitop(___set_bit, nr, addr)
|
|
|
|
#define __clear_bit(nr, addr) bitop(___clear_bit, nr, addr)
|
|
|
|
#define __change_bit(nr, addr) bitop(___change_bit, nr, addr)
|
|
|
|
#define __test_and_set_bit(nr, addr) bitop(___test_and_set_bit, nr, addr)
|
|
|
|
#define __test_and_clear_bit(nr, addr) bitop(___test_and_clear_bit, nr, addr)
|
|
|
|
#define __test_and_change_bit(nr, addr) bitop(___test_and_change_bit, nr, addr)
|
|
|
|
#define test_bit(nr, addr) bitop(_test_bit, nr, addr)
|
2022-08-26 21:17:08 +08:00
|
|
|
#define test_bit_acquire(nr, addr) bitop(_test_bit_acquire, nr, addr)
|
2022-06-24 20:13:09 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Include this here because some architectures need generic_ffs/fls in
|
|
|
|
* scope
|
|
|
|
*/
|
|
|
|
#include <asm/bitops.h>
|
|
|
|
|
bitops: unify non-atomic bitops prototypes across architectures
Currently, there is a mess with the prototypes of the non-atomic
bitops across the different architectures:
ret bool, int, unsigned long
nr int, long, unsigned int, unsigned long
addr volatile unsigned long *, volatile void *
Thankfully, it doesn't provoke any bugs, but can sometimes make
the compiler angry when it's not handy at all.
Adjust all the prototypes to the following standard:
ret bool retval can be only 0 or 1
nr unsigned long native; signed makes no sense
addr volatile unsigned long * bitmaps are arrays of ulongs
Next, some architectures don't define 'arch_' versions as they don't
support instrumentation, others do. To make sure there is always the
same set of callables present and to ease any potential future
changes, make them all follow the rule:
* architecture-specific files define only 'arch_' versions;
* non-prefixed versions can be defined only in asm-generic files;
and place the non-prefixed definitions into a new file in
asm-generic to be included by non-instrumented architectures.
Finally, add some static assertions in order to prevent people from
making a mess in this room again.
I also used the %__always_inline attribute consistently, so that
they always get resolved to the actual operations.
Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 20:13:07 +08:00
|
|
|
/* Check that the bitops prototypes are sane */
|
|
|
|
#define __check_bitop_pr(name) \
|
|
|
|
static_assert(__same_type(arch_##name, generic_##name) && \
|
2022-06-24 20:13:08 +08:00
|
|
|
__same_type(const_##name, generic_##name) && \
|
2022-06-24 20:13:09 +08:00
|
|
|
__same_type(_##name, generic_##name))
|
bitops: unify non-atomic bitops prototypes across architectures
Currently, there is a mess with the prototypes of the non-atomic
bitops across the different architectures:
ret bool, int, unsigned long
nr int, long, unsigned int, unsigned long
addr volatile unsigned long *, volatile void *
Thankfully, it doesn't provoke any bugs, but can sometimes make
the compiler angry when it's not handy at all.
Adjust all the prototypes to the following standard:
ret bool retval can be only 0 or 1
nr unsigned long native; signed makes no sense
addr volatile unsigned long * bitmaps are arrays of ulongs
Next, some architectures don't define 'arch_' versions as they don't
support instrumentation, others do. To make sure there is always the
same set of callables present and to ease any potential future
changes, make them all follow the rule:
* architecture-specific files define only 'arch_' versions;
* non-prefixed versions can be defined only in asm-generic files;
and place the non-prefixed definitions into a new file in
asm-generic to be included by non-instrumented architectures.
Finally, add some static assertions in order to prevent people from
making a mess in this room again.
I also used the %__always_inline attribute consistently, so that
they always get resolved to the actual operations.
Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 20:13:07 +08:00
|
|
|
|
|
|
|
__check_bitop_pr(__set_bit);
|
|
|
|
__check_bitop_pr(__clear_bit);
|
|
|
|
__check_bitop_pr(__change_bit);
|
|
|
|
__check_bitop_pr(__test_and_set_bit);
|
|
|
|
__check_bitop_pr(__test_and_clear_bit);
|
|
|
|
__check_bitop_pr(__test_and_change_bit);
|
|
|
|
__check_bitop_pr(test_bit);
|
|
|
|
|
|
|
|
#undef __check_bitop_pr
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
lib: add find_nth{,_and,_andnot}_bit()
Kernel lacks for a function that searches for Nth bit in a bitmap.
Usually people do it like this:
for_each_set_bit(bit, mask, size)
if (n-- == 0)
return bit;
We can do it more efficiently, if we:
1. find a word containing Nth bit, using hweight(); and
2. find the bit, using a helper fns(), that works similarly to
__ffs() and ffz().
fns() is implemented as a simple loop. For x86_64, there's PDEP instruction
to do that: ret = clz(pdep(1 << idx, num)). However, for large bitmaps the
most of improvement comes from using hweight(), so I kept fns() simple.
New find_nth_bit() is ~70 times faster on x86_64/kvm in find_bit benchmark:
find_nth_bit: 7154190 ns, 16411 iterations
for_each_bit: 505493126 ns, 16315 iterations
With all that, a family of 3 new functions is added, and used where
appropriate in the following patches.
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-09-18 11:07:13 +08:00
|
|
|
/**
|
|
|
|
* fns - find N'th set bit in a word
|
|
|
|
* @word: The word to search
|
|
|
|
* @n: Bit to find
|
|
|
|
*/
|
|
|
|
static inline unsigned long fns(unsigned long word, unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned int bit;
|
|
|
|
|
|
|
|
while (word) {
|
|
|
|
bit = __ffs(word);
|
|
|
|
if (n-- == 0)
|
|
|
|
return bit;
|
|
|
|
__clear_bit(bit, &word);
|
|
|
|
}
|
|
|
|
|
|
|
|
return BITS_PER_LONG;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
\
|
2022-08-22 22:38:51 +08:00
|
|
|
old__ = READ_ONCE(*(ptr)); \
|
2014-03-30 22:20:01 +08:00
|
|
|
do { \
|
2018-10-15 21:43:06 +08:00
|
|
|
new__ = (old__ & ~mask__) | bits__; \
|
2022-08-22 22:38:51 +08:00
|
|
|
} while (!try_cmpxchg(ptr, &old__, new__)); \
|
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
|
|
|
\
|
2022-08-22 22:38:51 +08:00
|
|
|
old__ = READ_ONCE(*(ptr)); \
|
2016-05-04 10:22:13 +08:00
|
|
|
do { \
|
2022-08-22 22:38:51 +08:00
|
|
|
if (old__ & test__) \
|
|
|
|
break; \
|
2018-10-15 21:43:06 +08:00
|
|
|
new__ = old__ & ~clear__; \
|
2022-08-22 22:38:51 +08:00
|
|
|
} while (!try_cmpxchg(ptr, &old__, new__)); \
|
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
|