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_BITMAP_H
|
|
|
|
#define __LINUX_BITMAP_H
|
|
|
|
|
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
2021-05-07 09:02:30 +08:00
|
|
|
#include <linux/align.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/bitops.h>
|
2021-08-15 05:16:59 +08:00
|
|
|
#include <linux/find.h>
|
2021-05-07 09:02:30 +08:00
|
|
|
#include <linux/limits.h>
|
2021-03-15 17:13:55 +08:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/types.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2021-03-15 17:13:56 +08:00
|
|
|
struct device;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* bitmaps provide bit arrays that consume one or more unsigned
|
|
|
|
* longs. The bitmap interface and available operations are listed
|
|
|
|
* here, in bitmap.h
|
|
|
|
*
|
|
|
|
* Function implementations generic to all architectures are in
|
|
|
|
* lib/bitmap.c. Functions implementations that are architecture
|
|
|
|
* specific are in various include/asm-<arch>/bitops.h headers
|
|
|
|
* and other arch/<arch> specific files.
|
|
|
|
*
|
|
|
|
* See lib/bitmap.c for more details.
|
|
|
|
*/
|
|
|
|
|
2017-10-17 07:32:51 +08:00
|
|
|
/**
|
|
|
|
* DOC: bitmap overview
|
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* The available bitmap operations and their rough meaning in the
|
|
|
|
* case that the bitmap is a single unsigned long are thus:
|
|
|
|
*
|
2018-10-31 06:05:10 +08:00
|
|
|
* The generated code is more efficient when nbits is known at
|
|
|
|
* compile-time and at most BITS_PER_LONG.
|
2006-06-26 19:57:10 +08:00
|
|
|
*
|
2017-10-17 07:32:51 +08:00
|
|
|
* ::
|
|
|
|
*
|
|
|
|
* bitmap_zero(dst, nbits) *dst = 0UL
|
|
|
|
* bitmap_fill(dst, nbits) *dst = ~0UL
|
|
|
|
* bitmap_copy(dst, src, nbits) *dst = *src
|
|
|
|
* bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
|
|
|
|
* bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
|
|
|
|
* bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
|
|
|
|
* bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
|
|
|
|
* bitmap_complement(dst, src, nbits) *dst = ~(*src)
|
|
|
|
* bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
|
|
|
|
* bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
|
|
|
|
* bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
|
|
|
|
* bitmap_empty(src, nbits) Are all bits zero in *src?
|
|
|
|
* bitmap_full(src, nbits) Are all bits set in *src?
|
|
|
|
* bitmap_weight(src, nbits) Hamming Weight: number set bits
|
|
|
|
* bitmap_set(dst, pos, nbits) Set specified bit area
|
|
|
|
* bitmap_clear(dst, pos, nbits) Clear specified bit area
|
|
|
|
* bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
|
2020-03-04 22:09:19 +08:00
|
|
|
* bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
|
2017-10-17 07:32:51 +08:00
|
|
|
* bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
|
|
|
|
* bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
|
2020-01-22 07:17:54 +08:00
|
|
|
* bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
|
2019-12-05 08:53:26 +08:00
|
|
|
* bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
|
2017-10-17 07:32:51 +08:00
|
|
|
* bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
|
|
|
|
* bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
|
|
|
|
* bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
|
|
|
|
* bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
|
|
|
|
* bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
|
|
|
|
* bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
|
|
|
|
* bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
|
|
|
|
* bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
|
|
|
|
* bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
|
|
|
|
* bitmap_release_region(bitmap, pos, order) Free specified bit region
|
|
|
|
* bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
|
bitmap: new bitmap_copy_safe and bitmap_{from,to}_arr32
This patchset replaces bitmap_{to,from}_u32array with more simple and
standard looking copy-like functions.
bitmap_from_u32array() takes 4 arguments (bitmap_to_u32array is similar):
- unsigned long *bitmap, which is destination;
- unsigned int nbits, the length of destination bitmap, in bits;
- const u32 *buf, the source; and
- unsigned int nwords, the length of source buffer in ints.
In description to the function it is detailed like:
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
* bits between nword and nbits in @bitmap (if any) are cleared.
Having two size arguments looks unneeded and potentially dangerous.
It is unneeded because normally user of copy-like function should take
care of the size of destination and make it big enough to fit source
data.
And it is dangerous because function may hide possible error if user
doesn't provide big enough bitmap, and data becomes silently dropped.
That's why all copy-like functions have 1 argument for size of copying
data, and I don't see any reason to make bitmap_from_u32array()
different.
One exception that comes in mind is strncpy() which also provides size
of destination in arguments, but it's strongly argued by the possibility
of taking broken strings in source. This is not the case of
bitmap_{from,to}_u32array().
There is no many real users of bitmap_{from,to}_u32array(), and they all
very clearly provide size of destination matched with the size of
source, so additional functionality is not used in fact. Like this:
bitmap_from_u32array(to->link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NBITS,
link_usettings.link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NU32);
Where:
#define __ETHTOOL_LINK_MODE_MASK_NU32 \
DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
In this patch, bitmap_copy_safe and bitmap_{from,to}_arr32 are introduced.
'Safe' in bitmap_copy_safe() stands for clearing unused bits in bitmap
beyond last bit till the end of last word. It is useful for hardening
API when bitmap is assumed to be exposed to userspace.
bitmap_{from,to}_arr32 functions are replacements for
bitmap_{from,to}_u32array. They don't take unneeded nwords argument, and
so simpler in implementation and understanding.
This patch suggests optimization for 32-bit systems - aliasing
bitmap_{from,to}_arr32 to bitmap_copy_safe.
Other possible optimization is aliasing 64-bit LE bitmap_{from,to}_arr32 to
more generic function(s). But I didn't end up with the function that would
be helpful by itself, and can be used to alias 64-bit LE
bitmap_{from,to}_arr32, like bitmap_copy_safe() does. So I preferred to
leave things as is.
The following patch switches kernel to new API and introduces test for it.
Discussion is here: https://lkml.org/lkml/2017/11/15/592
[ynorov@caviumnetworks.com: rename bitmap_copy_safe to bitmap_copy_clear_tail]
Link: http://lkml.kernel.org/r/20180201172508.5739-3-ynorov@caviumnetworks.com
Link: http://lkml.kernel.org/r/20171228150019.27953-1-ynorov@caviumnetworks.com
Signed-off-by: Yury Norov <ynorov@caviumnetworks.com>
Cc: Ben Hutchings <ben@decadent.org.uk>
Cc: David Decotigny <decot@googlers.com>,
Cc: David S. Miller <davem@davemloft.net>,
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-07 07:38:02 +08:00
|
|
|
* bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
|
|
|
|
* bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
|
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
|
|
|
* bitmap_get_value8(map, start) Get 8bit value from map at start
|
|
|
|
* bitmap_set_value8(map, value, start) Set 8bit value to map at start
|
2017-10-17 07:32:51 +08:00
|
|
|
*
|
2018-02-07 07:38:20 +08:00
|
|
|
* Note, bitmap_zero() and bitmap_fill() operate over the region of
|
|
|
|
* unsigned longs, that is, bits behind bitmap till the unsigned long
|
|
|
|
* boundary will be zeroed or filled as well. Consider to use
|
|
|
|
* bitmap_clear() or bitmap_set() to make explicit zeroing or filling
|
|
|
|
* respectively.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2017-10-17 07:32:51 +08:00
|
|
|
/**
|
|
|
|
* DOC: bitmap bitops
|
|
|
|
*
|
|
|
|
* Also the following operations in asm/bitops.h apply to bitmaps.::
|
|
|
|
*
|
|
|
|
* set_bit(bit, addr) *addr |= bit
|
|
|
|
* clear_bit(bit, addr) *addr &= ~bit
|
|
|
|
* change_bit(bit, addr) *addr ^= bit
|
|
|
|
* test_bit(bit, addr) Is bit set in *addr?
|
|
|
|
* test_and_set_bit(bit, addr) Set bit and return old value
|
|
|
|
* test_and_clear_bit(bit, addr) Clear bit and return old value
|
|
|
|
* test_and_change_bit(bit, addr) Change bit and return old value
|
|
|
|
* find_first_zero_bit(addr, nbits) Position first zero bit in *addr
|
|
|
|
* find_first_bit(addr, nbits) Position first set bit in *addr
|
lib: optimize cpumask_next_and()
We've measured that we spend ~0.6% of sys cpu time in cpumask_next_and().
It's essentially a joined iteration in search for a non-zero bit, which is
currently implemented as a lookup join (find a nonzero bit on the lhs,
lookup the rhs to see if it's set there).
Implement a direct join (find a nonzero bit on the incrementally built
join). Also add generic bitmap benchmarks in the new `test_find_bit`
module for new function (see `find_next_and_bit` in [2] and [3] below).
For cpumask_next_and, direct benchmarking shows that it's 1.17x to 14x
faster with a geometric mean of 2.1 on 32 CPUs [1]. No impact on memory
usage. Note that on Arm, the new pure-C implementation still outperforms
the old one that uses a mix of C and asm (`find_next_bit`) [3].
[1] Approximate benchmark code:
```
unsigned long src1p[nr_cpumask_longs] = {pattern1};
unsigned long src2p[nr_cpumask_longs] = {pattern2};
for (/*a bunch of repetitions*/) {
for (int n = -1; n <= nr_cpu_ids; ++n) {
asm volatile("" : "+rm"(src1p)); // prevent any optimization
asm volatile("" : "+rm"(src2p));
unsigned long result = cpumask_next_and(n, src1p, src2p);
asm volatile("" : "+rm"(result));
}
}
```
Results:
pattern1 pattern2 time_before/time_after
0x0000ffff 0x0000ffff 1.65
0x0000ffff 0x00005555 2.24
0x0000ffff 0x00001111 2.94
0x0000ffff 0x00000000 14.0
0x00005555 0x0000ffff 1.67
0x00005555 0x00005555 1.71
0x00005555 0x00001111 1.90
0x00005555 0x00000000 6.58
0x00001111 0x0000ffff 1.46
0x00001111 0x00005555 1.49
0x00001111 0x00001111 1.45
0x00001111 0x00000000 3.10
0x00000000 0x0000ffff 1.18
0x00000000 0x00005555 1.18
0x00000000 0x00001111 1.17
0x00000000 0x00000000 1.25
-----------------------------
geo.mean 2.06
[2] test_find_next_bit, X86 (skylake)
[ 3913.477422] Start testing find_bit() with random-filled bitmap
[ 3913.477847] find_next_bit: 160868 cycles, 16484 iterations
[ 3913.477933] find_next_zero_bit: 169542 cycles, 16285 iterations
[ 3913.478036] find_last_bit: 201638 cycles, 16483 iterations
[ 3913.480214] find_first_bit: 4353244 cycles, 16484 iterations
[ 3913.480216] Start testing find_next_and_bit() with random-filled
bitmap
[ 3913.481074] find_next_and_bit: 89604 cycles, 8216 iterations
[ 3913.481075] Start testing find_bit() with sparse bitmap
[ 3913.481078] find_next_bit: 2536 cycles, 66 iterations
[ 3913.481252] find_next_zero_bit: 344404 cycles, 32703 iterations
[ 3913.481255] find_last_bit: 2006 cycles, 66 iterations
[ 3913.481265] find_first_bit: 17488 cycles, 66 iterations
[ 3913.481266] Start testing find_next_and_bit() with sparse bitmap
[ 3913.481272] find_next_and_bit: 764 cycles, 1 iterations
[3] test_find_next_bit, arm (v7 odroid XU3).
[ 267.206928] Start testing find_bit() with random-filled bitmap
[ 267.214752] find_next_bit: 4474 cycles, 16419 iterations
[ 267.221850] find_next_zero_bit: 5976 cycles, 16350 iterations
[ 267.229294] find_last_bit: 4209 cycles, 16419 iterations
[ 267.279131] find_first_bit: 1032991 cycles, 16420 iterations
[ 267.286265] Start testing find_next_and_bit() with random-filled
bitmap
[ 267.302386] find_next_and_bit: 2290 cycles, 8140 iterations
[ 267.309422] Start testing find_bit() with sparse bitmap
[ 267.316054] find_next_bit: 191 cycles, 66 iterations
[ 267.322726] find_next_zero_bit: 8758 cycles, 32703 iterations
[ 267.329803] find_last_bit: 84 cycles, 66 iterations
[ 267.336169] find_first_bit: 4118 cycles, 66 iterations
[ 267.342627] Start testing find_next_and_bit() with sparse bitmap
[ 267.356919] find_next_and_bit: 91 cycles, 1 iterations
[courbet@google.com: v6]
Link: http://lkml.kernel.org/r/20171129095715.23430-1-courbet@google.com
[geert@linux-m68k.org: m68k/bitops: always include <asm-generic/bitops/find.h>]
Link: http://lkml.kernel.org/r/1512556816-28627-1-git-send-email-geert@linux-m68k.org
Link: http://lkml.kernel.org/r/20171128131334.23491-1-courbet@google.com
Signed-off-by: Clement Courbet <courbet@google.com>
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Yury Norov <ynorov@caviumnetworks.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-07 07:38:34 +08:00
|
|
|
* find_next_zero_bit(addr, nbits, bit)
|
|
|
|
* Position next zero bit in *addr >= bit
|
2017-10-17 07:32:51 +08:00
|
|
|
* find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
|
lib: optimize cpumask_next_and()
We've measured that we spend ~0.6% of sys cpu time in cpumask_next_and().
It's essentially a joined iteration in search for a non-zero bit, which is
currently implemented as a lookup join (find a nonzero bit on the lhs,
lookup the rhs to see if it's set there).
Implement a direct join (find a nonzero bit on the incrementally built
join). Also add generic bitmap benchmarks in the new `test_find_bit`
module for new function (see `find_next_and_bit` in [2] and [3] below).
For cpumask_next_and, direct benchmarking shows that it's 1.17x to 14x
faster with a geometric mean of 2.1 on 32 CPUs [1]. No impact on memory
usage. Note that on Arm, the new pure-C implementation still outperforms
the old one that uses a mix of C and asm (`find_next_bit`) [3].
[1] Approximate benchmark code:
```
unsigned long src1p[nr_cpumask_longs] = {pattern1};
unsigned long src2p[nr_cpumask_longs] = {pattern2};
for (/*a bunch of repetitions*/) {
for (int n = -1; n <= nr_cpu_ids; ++n) {
asm volatile("" : "+rm"(src1p)); // prevent any optimization
asm volatile("" : "+rm"(src2p));
unsigned long result = cpumask_next_and(n, src1p, src2p);
asm volatile("" : "+rm"(result));
}
}
```
Results:
pattern1 pattern2 time_before/time_after
0x0000ffff 0x0000ffff 1.65
0x0000ffff 0x00005555 2.24
0x0000ffff 0x00001111 2.94
0x0000ffff 0x00000000 14.0
0x00005555 0x0000ffff 1.67
0x00005555 0x00005555 1.71
0x00005555 0x00001111 1.90
0x00005555 0x00000000 6.58
0x00001111 0x0000ffff 1.46
0x00001111 0x00005555 1.49
0x00001111 0x00001111 1.45
0x00001111 0x00000000 3.10
0x00000000 0x0000ffff 1.18
0x00000000 0x00005555 1.18
0x00000000 0x00001111 1.17
0x00000000 0x00000000 1.25
-----------------------------
geo.mean 2.06
[2] test_find_next_bit, X86 (skylake)
[ 3913.477422] Start testing find_bit() with random-filled bitmap
[ 3913.477847] find_next_bit: 160868 cycles, 16484 iterations
[ 3913.477933] find_next_zero_bit: 169542 cycles, 16285 iterations
[ 3913.478036] find_last_bit: 201638 cycles, 16483 iterations
[ 3913.480214] find_first_bit: 4353244 cycles, 16484 iterations
[ 3913.480216] Start testing find_next_and_bit() with random-filled
bitmap
[ 3913.481074] find_next_and_bit: 89604 cycles, 8216 iterations
[ 3913.481075] Start testing find_bit() with sparse bitmap
[ 3913.481078] find_next_bit: 2536 cycles, 66 iterations
[ 3913.481252] find_next_zero_bit: 344404 cycles, 32703 iterations
[ 3913.481255] find_last_bit: 2006 cycles, 66 iterations
[ 3913.481265] find_first_bit: 17488 cycles, 66 iterations
[ 3913.481266] Start testing find_next_and_bit() with sparse bitmap
[ 3913.481272] find_next_and_bit: 764 cycles, 1 iterations
[3] test_find_next_bit, arm (v7 odroid XU3).
[ 267.206928] Start testing find_bit() with random-filled bitmap
[ 267.214752] find_next_bit: 4474 cycles, 16419 iterations
[ 267.221850] find_next_zero_bit: 5976 cycles, 16350 iterations
[ 267.229294] find_last_bit: 4209 cycles, 16419 iterations
[ 267.279131] find_first_bit: 1032991 cycles, 16420 iterations
[ 267.286265] Start testing find_next_and_bit() with random-filled
bitmap
[ 267.302386] find_next_and_bit: 2290 cycles, 8140 iterations
[ 267.309422] Start testing find_bit() with sparse bitmap
[ 267.316054] find_next_bit: 191 cycles, 66 iterations
[ 267.322726] find_next_zero_bit: 8758 cycles, 32703 iterations
[ 267.329803] find_last_bit: 84 cycles, 66 iterations
[ 267.336169] find_first_bit: 4118 cycles, 66 iterations
[ 267.342627] Start testing find_next_and_bit() with sparse bitmap
[ 267.356919] find_next_and_bit: 91 cycles, 1 iterations
[courbet@google.com: v6]
Link: http://lkml.kernel.org/r/20171129095715.23430-1-courbet@google.com
[geert@linux-m68k.org: m68k/bitops: always include <asm-generic/bitops/find.h>]
Link: http://lkml.kernel.org/r/1512556816-28627-1-git-send-email-geert@linux-m68k.org
Link: http://lkml.kernel.org/r/20171128131334.23491-1-courbet@google.com
Signed-off-by: Clement Courbet <courbet@google.com>
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Yury Norov <ynorov@caviumnetworks.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-07 07:38:34 +08:00
|
|
|
* find_next_and_bit(addr1, addr2, nbits, bit)
|
|
|
|
* Same as find_next_bit, but in
|
|
|
|
* (*addr1 & *addr2)
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-10-17 07:32:51 +08:00
|
|
|
/**
|
|
|
|
* DOC: declare bitmap
|
2005-04-17 06:20:36 +08:00
|
|
|
* The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
|
|
|
|
* to declare an array named 'name' of just enough unsigned longs to
|
|
|
|
* contain all bit positions from 0 to 'bits' - 1.
|
|
|
|
*/
|
|
|
|
|
2018-08-02 06:42:56 +08:00
|
|
|
/*
|
|
|
|
* Allocation and deallocation of bitmap.
|
|
|
|
* Provided in lib/bitmap.c to avoid circular dependency.
|
|
|
|
*/
|
2021-03-15 17:13:54 +08:00
|
|
|
unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
|
|
|
|
unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
|
2020-12-30 17:41:52 +08:00
|
|
|
unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
|
|
|
|
unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_free(const unsigned long *bitmap);
|
2018-08-02 06:42:56 +08:00
|
|
|
|
2021-03-15 17:13:56 +08:00
|
|
|
/* Managed variants of the above. */
|
|
|
|
unsigned long *devm_bitmap_alloc(struct device *dev,
|
|
|
|
unsigned int nbits, gfp_t flags);
|
|
|
|
unsigned long *devm_bitmap_zalloc(struct device *dev,
|
|
|
|
unsigned int nbits, gfp_t flags);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* lib/bitmap.c provides these functions:
|
|
|
|
*/
|
|
|
|
|
2021-03-15 17:13:54 +08:00
|
|
|
int __bitmap_equal(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
bool __pure __bitmap_or_equal(const unsigned long *src1,
|
|
|
|
const unsigned long *src2,
|
|
|
|
const unsigned long *src3,
|
|
|
|
unsigned int nbits);
|
|
|
|
void __bitmap_complement(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int nbits);
|
|
|
|
void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits);
|
|
|
|
void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits);
|
|
|
|
void bitmap_cut(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int first, unsigned int cut, unsigned int nbits);
|
|
|
|
int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
void __bitmap_replace(unsigned long *dst,
|
|
|
|
const unsigned long *old, const unsigned long *new,
|
|
|
|
const unsigned long *mask, unsigned int nbits);
|
|
|
|
int __bitmap_intersects(const unsigned long *bitmap1,
|
2014-08-07 07:09:59 +08:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
int __bitmap_subset(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
|
|
|
|
void __bitmap_set(unsigned long *map, unsigned int start, int len);
|
|
|
|
void __bitmap_clear(unsigned long *map, unsigned int start, int len);
|
2014-12-13 08:54:45 +08:00
|
|
|
|
2021-03-15 17:13:54 +08:00
|
|
|
unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
|
|
|
|
unsigned long size,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned int nr,
|
|
|
|
unsigned long align_mask,
|
|
|
|
unsigned long align_offset);
|
2014-12-13 08:54:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_find_next_zero_area - find a contiguous aligned zero area
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @align_mask: Alignment mask for zero area
|
|
|
|
*
|
|
|
|
* The @align_mask should be one less than a power of 2; the effect is that
|
|
|
|
* the bit offset of all zero areas this function finds is multiples of that
|
|
|
|
* power of 2. A @align_mask of 0 means no alignment is required.
|
|
|
|
*/
|
|
|
|
static inline unsigned long
|
|
|
|
bitmap_find_next_zero_area(unsigned long *map,
|
|
|
|
unsigned long size,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned int nr,
|
|
|
|
unsigned long align_mask)
|
|
|
|
{
|
|
|
|
return bitmap_find_next_zero_area_off(map, size, start, nr,
|
|
|
|
align_mask, 0);
|
|
|
|
}
|
2009-12-16 08:48:25 +08:00
|
|
|
|
2021-03-15 17:13:54 +08:00
|
|
|
int bitmap_parse(const char *buf, unsigned int buflen,
|
2006-10-11 16:21:55 +08:00
|
|
|
unsigned long *dst, int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long *dst, int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
int bitmap_parselist(const char *buf, unsigned long *maskp,
|
2005-04-17 06:20:36 +08:00
|
|
|
int nmaskbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
|
bitmap, irq: add smp_affinity_list interface to /proc/irq
Manually adjusting the smp_affinity for IRQ's becomes unwieldy when the
cpu count is large.
Setting smp affinity to cpus 256 to 263 would be:
echo 000000ff,00000000,00000000,00000000,00000000,00000000,00000000,00000000 > smp_affinity
instead of:
echo 256-263 > smp_affinity_list
Think about what it looks like for cpus around say, 4088 to 4095.
We already have many alternate "list" interfaces:
/sys/devices/system/cpu/cpuX/indexY/shared_cpu_list
/sys/devices/system/cpu/cpuX/topology/thread_siblings_list
/sys/devices/system/cpu/cpuX/topology/core_siblings_list
/sys/devices/system/node/nodeX/cpulist
/sys/devices/pci***/***/local_cpulist
Add a companion interface, smp_affinity_list to use cpu lists instead of
cpu maps. This conforms to other companion interfaces where both a map
and a list interface exists.
This required adding a bitmap_parselist_user() function in a manner
similar to the bitmap_parse_user() function.
[akpm@linux-foundation.org: make __bitmap_parselist() static]
Signed-off-by: Mike Travis <travis@sgi.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jack Steiner <steiner@sgi.com>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-05-25 08:13:12 +08:00
|
|
|
unsigned long *dst, int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_remap(unsigned long *dst, const unsigned long *src,
|
2015-02-13 07:02:13 +08:00
|
|
|
const unsigned long *old, const unsigned long *new, unsigned int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
int bitmap_bitremap(int oldbit,
|
2005-10-31 07:02:33 +08:00
|
|
|
const unsigned long *old, const unsigned long *new, int bits);
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_onto(unsigned long *dst, const unsigned long *orig,
|
2015-02-13 07:02:01 +08:00
|
|
|
const unsigned long *relmap, unsigned int bits);
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_fold(unsigned long *dst, const unsigned long *orig,
|
2015-02-13 07:02:04 +08:00
|
|
|
unsigned int sz, unsigned int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
|
|
|
|
void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
|
|
|
|
int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
|
2018-02-07 07:38:06 +08:00
|
|
|
|
2015-02-14 06:36:00 +08:00
|
|
|
#ifdef __BIG_ENDIAN
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
|
2015-02-14 06:36:00 +08:00
|
|
|
#else
|
|
|
|
#define bitmap_copy_le bitmap_copy
|
|
|
|
#endif
|
2021-03-15 17:13:54 +08:00
|
|
|
unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
|
|
|
|
int bitmap_print_to_pagebuf(bool list, char *buf,
|
2014-09-30 21:48:22 +08:00
|
|
|
const unsigned long *maskp, int nmaskbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
cpumask: introduce cpumap_print_list/bitmask_to_buf to support large bitmask and list
The existing cpumap_print_to_pagebuf() is used by cpu topology and other
drivers to export hexadecimal bitmask and decimal list to userspace by
sysfs ABI.
Right now, those drivers are using a normal attribute for this kind of
ABIs. A normal attribute typically has show entry as below:
static ssize_t example_dev_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
...
return cpumap_print_to_pagebuf(true, buf, &pmu_mmdc->cpu);
}
show entry of attribute has no offset and count parameters and this
means the file is limited to one page only.
cpumap_print_to_pagebuf() API works terribly well for this kind of
normal attribute with buf parameter and without offset, count:
static inline ssize_t
cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
{
return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
nr_cpu_ids);
}
The problem is once we have many cpus, we have a chance to make bitmask
or list more than one page. Especially for list, it could be as complex
as 0,3,5,7,9,...... We have no simple way to know it exact size.
It turns out bin_attribute is a way to break this limit. bin_attribute
has show entry as below:
static ssize_t
example_bin_attribute_show(struct file *filp, struct kobject *kobj,
struct bin_attribute *attr, char *buf,
loff_t offset, size_t count)
{
...
}
With the new offset and count parameters, this makes sysfs ABI be able
to support file size more than one page. For example, offset could be
>= 4096.
This patch introduces cpumap_print_bitmask/list_to_buf() and their bitmap
infrastructure bitmap_print_bitmask/list_to_buf() so that those drivers
can move to bin_attribute to support large bitmask and list. At the same
time, we have to pass those corresponding parameters such as offset, count
from bin_attribute to this new API.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Stefano Brivio <sbrivio@redhat.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: "Ma, Jianpeng" <jianpeng.ma@intel.com>
Cc: Yury Norov <yury.norov@gmail.com>
Cc: Valentin Schneider <valentin.schneider@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Daniel Bristot de Oliveira <bristot@redhat.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Tian Tao <tiantao6@hisilicon.com>
Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
Link: https://lore.kernel.org/r/20210806110251.560-2-song.bao.hua@hisilicon.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-08-06 19:02:47 +08:00
|
|
|
extern int bitmap_print_bitmask_to_buf(char *buf, const unsigned long *maskp,
|
|
|
|
int nmaskbits, loff_t off, size_t count);
|
|
|
|
|
|
|
|
extern int bitmap_print_list_to_buf(char *buf, const unsigned long *maskp,
|
|
|
|
int nmaskbits, loff_t off, size_t count);
|
|
|
|
|
linux/bitmap.h: improve BITMAP_{LAST,FIRST}_WORD_MASK
The macro BITMAP_LAST_WORD_MASK can be implemented without a conditional,
which will generally lead to slightly better generated code (221 bytes
saved for allmodconfig-GCOV_KERNEL, ~2k with GCOV_KERNEL). As a small
bonus, this also ensures that the nbits parameter is expanded exactly
once.
In BITMAP_FIRST_WORD_MASK, if start is signed gcc is technically allowed
to assume it is positive (or divisible by BITS_PER_LONG), and hence just
do the simple mask. It doesn't seem to use this, and even on an
architecture like x86 where the shift only depends on the lower 5 or 6
bits, and these bits are not affected by the signedness of the expression,
gcc still generates code to compute the C99 mandated value of start %
BITS_PER_LONG. So just use a mask explicitly, also for consistency with
BITMAP_LAST_WORD_MASK.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Tejun Heo <tj@kernel.org>
Reviewed-by: George Spelvin <linux@horizon.com>
Cc: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-04-16 07:17:42 +08:00
|
|
|
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
|
|
|
|
#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-02-13 07:01:53 +08:00
|
|
|
static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
linux/bitmap.h: remove redundant uses of small_const_nbits()
In the _zero, _fill and _copy functions, the small_const_nbits branch is
redundant. If nbits is small and const, gcc knows full well that
BITS_TO_LONGS(nbits) is 1, so len is also a compile-time constant
(sizeof(long)), and calling memset or memcpy with a length argument of
sizeof(long) makes gcc generate the expected code anyway:
#include <string.h>
void a(unsigned long *x) { memset(x, 0, 8); }
void b(unsigned long *x) { memset(x, 0xff, 8); }
void c(unsigned long *x, const unsigned long *y) { memcpy(x, y, 8); }
turns into
0000000000000000 <a>:
0: 48 c7 07 00 00 00 00 movq $0x0,(%rdi)
7: c3 retq
8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
f: 00
0000000000000010 <b>:
10: 48 c7 07 ff ff ff ff movq $0xffffffffffffffff,(%rdi)
17: c3 retq
18: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
1f: 00
0000000000000020 <c>:
20: 48 8b 06 mov (%rsi),%rax
23: 48 89 07 mov %rax,(%rdi)
26: c3 retq
Link: http://lkml.kernel.org/r/20180818131623.8755-4-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Yury Norov <ynorov@caviumnetworks.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-31 06:05:02 +08:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memset(dst, 0, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 07:01:53 +08:00
|
|
|
static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
linux/bitmap.h: remove redundant uses of small_const_nbits()
In the _zero, _fill and _copy functions, the small_const_nbits branch is
redundant. If nbits is small and const, gcc knows full well that
BITS_TO_LONGS(nbits) is 1, so len is also a compile-time constant
(sizeof(long)), and calling memset or memcpy with a length argument of
sizeof(long) makes gcc generate the expected code anyway:
#include <string.h>
void a(unsigned long *x) { memset(x, 0, 8); }
void b(unsigned long *x) { memset(x, 0xff, 8); }
void c(unsigned long *x, const unsigned long *y) { memcpy(x, y, 8); }
turns into
0000000000000000 <a>:
0: 48 c7 07 00 00 00 00 movq $0x0,(%rdi)
7: c3 retq
8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
f: 00
0000000000000010 <b>:
10: 48 c7 07 ff ff ff ff movq $0xffffffffffffffff,(%rdi)
17: c3 retq
18: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
1f: 00
0000000000000020 <c>:
20: 48 8b 06 mov (%rsi),%rax
23: 48 89 07 mov %rax,(%rdi)
26: c3 retq
Link: http://lkml.kernel.org/r/20180818131623.8755-4-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Yury Norov <ynorov@caviumnetworks.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-31 06:05:02 +08:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memset(dst, 0xff, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
|
2015-02-13 07:01:53 +08:00
|
|
|
unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
linux/bitmap.h: remove redundant uses of small_const_nbits()
In the _zero, _fill and _copy functions, the small_const_nbits branch is
redundant. If nbits is small and const, gcc knows full well that
BITS_TO_LONGS(nbits) is 1, so len is also a compile-time constant
(sizeof(long)), and calling memset or memcpy with a length argument of
sizeof(long) makes gcc generate the expected code anyway:
#include <string.h>
void a(unsigned long *x) { memset(x, 0, 8); }
void b(unsigned long *x) { memset(x, 0xff, 8); }
void c(unsigned long *x, const unsigned long *y) { memcpy(x, y, 8); }
turns into
0000000000000000 <a>:
0: 48 c7 07 00 00 00 00 movq $0x0,(%rdi)
7: c3 retq
8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
f: 00
0000000000000010 <b>:
10: 48 c7 07 ff ff ff ff movq $0xffffffffffffffff,(%rdi)
17: c3 retq
18: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
1f: 00
0000000000000020 <c>:
20: 48 8b 06 mov (%rsi),%rax
23: 48 89 07 mov %rax,(%rdi)
26: c3 retq
Link: http://lkml.kernel.org/r/20180818131623.8755-4-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Yury Norov <ynorov@caviumnetworks.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-10-31 06:05:02 +08:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memcpy(dst, src, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
bitmap: new bitmap_copy_safe and bitmap_{from,to}_arr32
This patchset replaces bitmap_{to,from}_u32array with more simple and
standard looking copy-like functions.
bitmap_from_u32array() takes 4 arguments (bitmap_to_u32array is similar):
- unsigned long *bitmap, which is destination;
- unsigned int nbits, the length of destination bitmap, in bits;
- const u32 *buf, the source; and
- unsigned int nwords, the length of source buffer in ints.
In description to the function it is detailed like:
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
* bits between nword and nbits in @bitmap (if any) are cleared.
Having two size arguments looks unneeded and potentially dangerous.
It is unneeded because normally user of copy-like function should take
care of the size of destination and make it big enough to fit source
data.
And it is dangerous because function may hide possible error if user
doesn't provide big enough bitmap, and data becomes silently dropped.
That's why all copy-like functions have 1 argument for size of copying
data, and I don't see any reason to make bitmap_from_u32array()
different.
One exception that comes in mind is strncpy() which also provides size
of destination in arguments, but it's strongly argued by the possibility
of taking broken strings in source. This is not the case of
bitmap_{from,to}_u32array().
There is no many real users of bitmap_{from,to}_u32array(), and they all
very clearly provide size of destination matched with the size of
source, so additional functionality is not used in fact. Like this:
bitmap_from_u32array(to->link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NBITS,
link_usettings.link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NU32);
Where:
#define __ETHTOOL_LINK_MODE_MASK_NU32 \
DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
In this patch, bitmap_copy_safe and bitmap_{from,to}_arr32 are introduced.
'Safe' in bitmap_copy_safe() stands for clearing unused bits in bitmap
beyond last bit till the end of last word. It is useful for hardening
API when bitmap is assumed to be exposed to userspace.
bitmap_{from,to}_arr32 functions are replacements for
bitmap_{from,to}_u32array. They don't take unneeded nwords argument, and
so simpler in implementation and understanding.
This patch suggests optimization for 32-bit systems - aliasing
bitmap_{from,to}_arr32 to bitmap_copy_safe.
Other possible optimization is aliasing 64-bit LE bitmap_{from,to}_arr32 to
more generic function(s). But I didn't end up with the function that would
be helpful by itself, and can be used to alias 64-bit LE
bitmap_{from,to}_arr32, like bitmap_copy_safe() does. So I preferred to
leave things as is.
The following patch switches kernel to new API and introduces test for it.
Discussion is here: https://lkml.org/lkml/2017/11/15/592
[ynorov@caviumnetworks.com: rename bitmap_copy_safe to bitmap_copy_clear_tail]
Link: http://lkml.kernel.org/r/20180201172508.5739-3-ynorov@caviumnetworks.com
Link: http://lkml.kernel.org/r/20171228150019.27953-1-ynorov@caviumnetworks.com
Signed-off-by: Yury Norov <ynorov@caviumnetworks.com>
Cc: Ben Hutchings <ben@decadent.org.uk>
Cc: David Decotigny <decot@googlers.com>,
Cc: David S. Miller <davem@davemloft.net>,
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-07 07:38:02 +08:00
|
|
|
/*
|
|
|
|
* Copy bitmap and clear tail bits in last word.
|
|
|
|
*/
|
|
|
|
static inline void bitmap_copy_clear_tail(unsigned long *dst,
|
|
|
|
const unsigned long *src, unsigned int nbits)
|
|
|
|
{
|
|
|
|
bitmap_copy(dst, src, nbits);
|
|
|
|
if (nbits % BITS_PER_LONG)
|
|
|
|
dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On 32-bit systems bitmaps are represented as u32 arrays internally, and
|
|
|
|
* therefore conversion is not needed when copying data from/to arrays of u32.
|
|
|
|
*/
|
|
|
|
#if BITS_PER_LONG == 64
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
|
bitmap: new bitmap_copy_safe and bitmap_{from,to}_arr32
This patchset replaces bitmap_{to,from}_u32array with more simple and
standard looking copy-like functions.
bitmap_from_u32array() takes 4 arguments (bitmap_to_u32array is similar):
- unsigned long *bitmap, which is destination;
- unsigned int nbits, the length of destination bitmap, in bits;
- const u32 *buf, the source; and
- unsigned int nwords, the length of source buffer in ints.
In description to the function it is detailed like:
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
* bits between nword and nbits in @bitmap (if any) are cleared.
Having two size arguments looks unneeded and potentially dangerous.
It is unneeded because normally user of copy-like function should take
care of the size of destination and make it big enough to fit source
data.
And it is dangerous because function may hide possible error if user
doesn't provide big enough bitmap, and data becomes silently dropped.
That's why all copy-like functions have 1 argument for size of copying
data, and I don't see any reason to make bitmap_from_u32array()
different.
One exception that comes in mind is strncpy() which also provides size
of destination in arguments, but it's strongly argued by the possibility
of taking broken strings in source. This is not the case of
bitmap_{from,to}_u32array().
There is no many real users of bitmap_{from,to}_u32array(), and they all
very clearly provide size of destination matched with the size of
source, so additional functionality is not used in fact. Like this:
bitmap_from_u32array(to->link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NBITS,
link_usettings.link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NU32);
Where:
#define __ETHTOOL_LINK_MODE_MASK_NU32 \
DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
In this patch, bitmap_copy_safe and bitmap_{from,to}_arr32 are introduced.
'Safe' in bitmap_copy_safe() stands for clearing unused bits in bitmap
beyond last bit till the end of last word. It is useful for hardening
API when bitmap is assumed to be exposed to userspace.
bitmap_{from,to}_arr32 functions are replacements for
bitmap_{from,to}_u32array. They don't take unneeded nwords argument, and
so simpler in implementation and understanding.
This patch suggests optimization for 32-bit systems - aliasing
bitmap_{from,to}_arr32 to bitmap_copy_safe.
Other possible optimization is aliasing 64-bit LE bitmap_{from,to}_arr32 to
more generic function(s). But I didn't end up with the function that would
be helpful by itself, and can be used to alias 64-bit LE
bitmap_{from,to}_arr32, like bitmap_copy_safe() does. So I preferred to
leave things as is.
The following patch switches kernel to new API and introduces test for it.
Discussion is here: https://lkml.org/lkml/2017/11/15/592
[ynorov@caviumnetworks.com: rename bitmap_copy_safe to bitmap_copy_clear_tail]
Link: http://lkml.kernel.org/r/20180201172508.5739-3-ynorov@caviumnetworks.com
Link: http://lkml.kernel.org/r/20171228150019.27953-1-ynorov@caviumnetworks.com
Signed-off-by: Yury Norov <ynorov@caviumnetworks.com>
Cc: Ben Hutchings <ben@decadent.org.uk>
Cc: David Decotigny <decot@googlers.com>,
Cc: David S. Miller <davem@davemloft.net>,
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-07 07:38:02 +08:00
|
|
|
unsigned int nbits);
|
2021-03-15 17:13:54 +08:00
|
|
|
void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
|
bitmap: new bitmap_copy_safe and bitmap_{from,to}_arr32
This patchset replaces bitmap_{to,from}_u32array with more simple and
standard looking copy-like functions.
bitmap_from_u32array() takes 4 arguments (bitmap_to_u32array is similar):
- unsigned long *bitmap, which is destination;
- unsigned int nbits, the length of destination bitmap, in bits;
- const u32 *buf, the source; and
- unsigned int nwords, the length of source buffer in ints.
In description to the function it is detailed like:
* copy min(nbits, 32*nwords) bits from @buf to @bitmap, remaining
* bits between nword and nbits in @bitmap (if any) are cleared.
Having two size arguments looks unneeded and potentially dangerous.
It is unneeded because normally user of copy-like function should take
care of the size of destination and make it big enough to fit source
data.
And it is dangerous because function may hide possible error if user
doesn't provide big enough bitmap, and data becomes silently dropped.
That's why all copy-like functions have 1 argument for size of copying
data, and I don't see any reason to make bitmap_from_u32array()
different.
One exception that comes in mind is strncpy() which also provides size
of destination in arguments, but it's strongly argued by the possibility
of taking broken strings in source. This is not the case of
bitmap_{from,to}_u32array().
There is no many real users of bitmap_{from,to}_u32array(), and they all
very clearly provide size of destination matched with the size of
source, so additional functionality is not used in fact. Like this:
bitmap_from_u32array(to->link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NBITS,
link_usettings.link_modes.supported,
__ETHTOOL_LINK_MODE_MASK_NU32);
Where:
#define __ETHTOOL_LINK_MODE_MASK_NU32 \
DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
In this patch, bitmap_copy_safe and bitmap_{from,to}_arr32 are introduced.
'Safe' in bitmap_copy_safe() stands for clearing unused bits in bitmap
beyond last bit till the end of last word. It is useful for hardening
API when bitmap is assumed to be exposed to userspace.
bitmap_{from,to}_arr32 functions are replacements for
bitmap_{from,to}_u32array. They don't take unneeded nwords argument, and
so simpler in implementation and understanding.
This patch suggests optimization for 32-bit systems - aliasing
bitmap_{from,to}_arr32 to bitmap_copy_safe.
Other possible optimization is aliasing 64-bit LE bitmap_{from,to}_arr32 to
more generic function(s). But I didn't end up with the function that would
be helpful by itself, and can be used to alias 64-bit LE
bitmap_{from,to}_arr32, like bitmap_copy_safe() does. So I preferred to
leave things as is.
The following patch switches kernel to new API and introduces test for it.
Discussion is here: https://lkml.org/lkml/2017/11/15/592
[ynorov@caviumnetworks.com: rename bitmap_copy_safe to bitmap_copy_clear_tail]
Link: http://lkml.kernel.org/r/20180201172508.5739-3-ynorov@caviumnetworks.com
Link: http://lkml.kernel.org/r/20171228150019.27953-1-ynorov@caviumnetworks.com
Signed-off-by: Yury Norov <ynorov@caviumnetworks.com>
Cc: Ben Hutchings <ben@decadent.org.uk>
Cc: David Decotigny <decot@googlers.com>,
Cc: David S. Miller <davem@davemloft.net>,
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-02-07 07:38:02 +08:00
|
|
|
unsigned int nbits);
|
|
|
|
#else
|
|
|
|
#define bitmap_from_arr32(bitmap, buf, nbits) \
|
|
|
|
bitmap_copy_clear_tail((unsigned long *) (bitmap), \
|
|
|
|
(const unsigned long *) (buf), (nbits))
|
|
|
|
#define bitmap_to_arr32(buf, bitmap, nbits) \
|
|
|
|
bitmap_copy_clear_tail((unsigned long *) (buf), \
|
|
|
|
(const unsigned long *) (bitmap), (nbits))
|
|
|
|
#endif
|
|
|
|
|
2009-08-22 00:26:15 +08:00
|
|
|
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 07:09:59 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-07 07:10:22 +08:00
|
|
|
return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
2009-08-22 00:26:15 +08:00
|
|
|
return __bitmap_and(dst, src1, src2, nbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 07:09:59 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 06:20:36 +08:00
|
|
|
*dst = *src1 | *src2;
|
|
|
|
else
|
|
|
|
__bitmap_or(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 07:09:59 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 06:20:36 +08:00
|
|
|
*dst = *src1 ^ *src2;
|
|
|
|
else
|
|
|
|
__bitmap_xor(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2009-08-22 00:26:15 +08:00
|
|
|
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 07:09:59 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-07 07:10:24 +08:00
|
|
|
return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
2009-08-22 00:26:15 +08:00
|
|
|
return __bitmap_andnot(dst, src1, src2, nbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
|
2014-08-07 07:09:55 +08:00
|
|
|
unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-07 07:09:57 +08:00
|
|
|
*dst = ~(*src);
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
|
|
|
__bitmap_complement(dst, src, nbits);
|
|
|
|
}
|
|
|
|
|
2018-04-03 06:58:31 +08:00
|
|
|
#ifdef __LITTLE_ENDIAN
|
|
|
|
#define BITMAP_MEM_ALIGNMENT 8
|
|
|
|
#else
|
|
|
|
#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
|
|
|
|
#endif
|
|
|
|
#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static inline int bitmap_equal(const unsigned long *src1,
|
2014-08-07 07:09:55 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2016-08-04 04:45:54 +08:00
|
|
|
return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
|
2018-04-03 06:58:31 +08:00
|
|
|
if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
|
2016-05-25 15:32:20 +08:00
|
|
|
return !memcmp(src1, src2, nbits / 8);
|
2016-08-04 04:45:54 +08:00
|
|
|
return __bitmap_equal(src1, src2, nbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-07-23 02:47:24 +08:00
|
|
|
/**
|
2019-10-15 05:12:20 +08:00
|
|
|
* bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
|
2019-07-23 02:47:24 +08:00
|
|
|
* @src1: Pointer to bitmap 1
|
|
|
|
* @src2: Pointer to bitmap 2 will be or'ed with bitmap 1
|
|
|
|
* @src3: Pointer to bitmap 3. Compare to the result of *@src1 | *@src2
|
2019-10-15 05:12:20 +08:00
|
|
|
* @nbits: number of bits in each of these bitmaps
|
2019-07-23 02:47:24 +08:00
|
|
|
*
|
|
|
|
* Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
|
|
|
|
*/
|
|
|
|
static inline bool bitmap_or_equal(const unsigned long *src1,
|
|
|
|
const unsigned long *src2,
|
|
|
|
const unsigned long *src3,
|
|
|
|
unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (!small_const_nbits(nbits))
|
|
|
|
return __bitmap_or_equal(src1, src2, src3, nbits);
|
|
|
|
|
|
|
|
return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static inline int bitmap_intersects(const unsigned long *src1,
|
2014-08-07 07:10:01 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 06:20:36 +08:00
|
|
|
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
|
|
|
else
|
|
|
|
return __bitmap_intersects(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_subset(const unsigned long *src1,
|
2014-08-07 07:10:03 +08:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 06:20:36 +08:00
|
|
|
return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
else
|
|
|
|
return __bitmap_subset(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:42:55 +08:00
|
|
|
static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 06:20:36 +08:00
|
|
|
return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
|
2015-04-17 03:44:00 +08:00
|
|
|
|
|
|
|
return find_first_bit(src, nbits) == nbits;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2020-12-16 12:42:55 +08:00
|
|
|
static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 06:20:36 +08:00
|
|
|
return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
|
2015-04-17 03:44:00 +08:00
|
|
|
|
|
|
|
return find_first_zero_bit(src, nbits) == nbits;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 22:15:14 +08:00
|
|
|
static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2006-06-26 19:57:10 +08:00
|
|
|
return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
|
2005-04-17 06:20:36 +08:00
|
|
|
return __bitmap_weight(src, nbits);
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:51:29 +08:00
|
|
|
static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
|
|
|
|
unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (__builtin_constant_p(nbits) && nbits == 1)
|
|
|
|
__set_bit(start, map);
|
2018-04-03 06:58:31 +08:00
|
|
|
else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
|
|
|
|
__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
|
2017-07-11 06:51:32 +08:00
|
|
|
memset((char *)map + start / 8, 0xff, nbits / 8);
|
2017-07-11 06:51:29 +08:00
|
|
|
else
|
|
|
|
__bitmap_set(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
|
|
|
|
unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (__builtin_constant_p(nbits) && nbits == 1)
|
|
|
|
__clear_bit(start, map);
|
2018-04-03 06:58:31 +08:00
|
|
|
else if (__builtin_constant_p(start & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) &&
|
|
|
|
__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
|
2017-07-11 06:51:32 +08:00
|
|
|
memset((char *)map + start / 8, 0, nbits / 8);
|
2017-07-11 06:51:29 +08:00
|
|
|
else
|
|
|
|
__bitmap_clear(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
2015-02-14 06:36:02 +08:00
|
|
|
static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
2018-10-31 06:05:07 +08:00
|
|
|
unsigned int shift, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2015-02-14 06:36:02 +08:00
|
|
|
*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
2015-02-14 06:36:02 +08:00
|
|
|
__bitmap_shift_right(dst, src, shift, nbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-02-14 06:36:13 +08:00
|
|
|
static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-12-30 06:35:13 +08:00
|
|
|
if (small_const_nbits(nbits))
|
2015-02-14 06:36:13 +08:00
|
|
|
*dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
2015-02-14 06:36:13 +08:00
|
|
|
__bitmap_shift_left(dst, src, shift, nbits);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:53:26 +08:00
|
|
|
static inline void bitmap_replace(unsigned long *dst,
|
|
|
|
const unsigned long *old,
|
|
|
|
const unsigned long *new,
|
|
|
|
const unsigned long *mask,
|
|
|
|
unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
*dst = (*old & ~(*mask)) | (*new & *mask);
|
|
|
|
else
|
|
|
|
__bitmap_replace(dst, old, new, mask, nbits);
|
|
|
|
}
|
|
|
|
|
2019-12-14 08:22:10 +08:00
|
|
|
static inline void bitmap_next_set_region(unsigned long *bitmap,
|
|
|
|
unsigned int *rs, unsigned int *re,
|
|
|
|
unsigned int end)
|
|
|
|
{
|
|
|
|
*rs = find_next_bit(bitmap, end, *rs);
|
|
|
|
*re = find_next_zero_bit(bitmap, end, *rs + 1);
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:07:10 +08:00
|
|
|
/**
|
2017-09-09 07:15:41 +08:00
|
|
|
* BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
|
2017-09-18 10:07:10 +08:00
|
|
|
* @n: u64 value
|
2017-09-09 07:15:41 +08:00
|
|
|
*
|
|
|
|
* Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
|
|
|
|
* integers in 32-bit environment, and 64-bit integers in 64-bit one.
|
|
|
|
*
|
|
|
|
* There are four combinations of endianness and length of the word in linux
|
|
|
|
* ABIs: LE64, BE64, LE32 and BE32.
|
|
|
|
*
|
|
|
|
* On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
|
|
|
|
* bitmaps and therefore don't require any special handling.
|
|
|
|
*
|
|
|
|
* On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
|
|
|
|
* prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
|
|
|
|
* other hand is represented as an array of 32-bit words and the position of
|
|
|
|
* bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
|
|
|
|
* word. For example, bit #42 is located at 10th position of 2nd word.
|
|
|
|
* It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
|
|
|
|
* values in memory as it usually does. But for BE we need to swap hi and lo
|
|
|
|
* words manually.
|
|
|
|
*
|
|
|
|
* With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
|
|
|
|
* lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
|
|
|
|
* hi and lo words, as is expected by bitmap.
|
|
|
|
*/
|
|
|
|
#if __BITS_PER_LONG == 64
|
|
|
|
#define BITMAP_FROM_U64(n) (n)
|
|
|
|
#else
|
|
|
|
#define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
|
|
|
|
((unsigned long) ((u64)(n) >> 32))
|
|
|
|
#endif
|
|
|
|
|
2017-09-18 10:07:10 +08:00
|
|
|
/**
|
2016-08-17 17:36:08 +08:00
|
|
|
* bitmap_from_u64 - Check and swap words within u64.
|
|
|
|
* @mask: source bitmap
|
|
|
|
* @dst: destination bitmap
|
|
|
|
*
|
2017-09-18 10:07:10 +08:00
|
|
|
* In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
|
2016-08-17 17:36:08 +08:00
|
|
|
* to read u64 mask, we will get the wrong word.
|
2017-09-18 10:07:10 +08:00
|
|
|
* That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
|
2016-08-17 17:36:08 +08:00
|
|
|
* but we expect the lower 32-bits of u64.
|
|
|
|
*/
|
|
|
|
static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
|
|
|
|
{
|
|
|
|
dst[0] = mask & ULONG_MAX;
|
|
|
|
|
|
|
|
if (sizeof(mask) > sizeof(unsigned long))
|
|
|
|
dst[1] = mask >> 32;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* bitmap_get_value8 - get an 8-bit value within a memory region
|
|
|
|
* @map: address to the bitmap memory region
|
|
|
|
* @start: bit offset of the 8-bit value; must be a multiple of 8
|
|
|
|
*
|
|
|
|
* Returns the 8-bit value located at the @start bit offset within the @src
|
|
|
|
* memory region.
|
|
|
|
*/
|
|
|
|
static inline unsigned long bitmap_get_value8(const unsigned long *map,
|
|
|
|
unsigned long start)
|
|
|
|
{
|
|
|
|
const size_t index = BIT_WORD(start);
|
|
|
|
const unsigned long offset = start % BITS_PER_LONG;
|
|
|
|
|
|
|
|
return (map[index] >> offset) & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_set_value8 - set an 8-bit value within a memory region
|
|
|
|
* @map: address to the bitmap memory region
|
|
|
|
* @value: the 8-bit value; values wider than 8 bits may clobber bitmap
|
|
|
|
* @start: bit offset of the 8-bit value; must be a multiple of 8
|
|
|
|
*/
|
|
|
|
static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
|
|
|
|
unsigned long start)
|
|
|
|
{
|
|
|
|
const size_t index = BIT_WORD(start);
|
|
|
|
const unsigned long offset = start % BITS_PER_LONG;
|
|
|
|
|
|
|
|
map[index] &= ~(0xFFUL << offset);
|
|
|
|
map[index] |= value << offset;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
|
|
|
#endif /* __LINUX_BITMAP_H */
|