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 21:07:57 +07:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-17 05:20:36 +07:00
|
|
|
#ifndef __LINUX_BITMAP_H
|
|
|
|
#define __LINUX_BITMAP_H
|
|
|
|
|
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/string.h>
|
2007-10-19 13:40:37 +07:00
|
|
|
#include <linux/kernel.h>
|
2005-04-17 05:20:36 +07: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 06:32:51 +07:00
|
|
|
/**
|
|
|
|
* DOC: bitmap overview
|
|
|
|
*
|
2005-04-17 05:20:36 +07: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 05:05:10 +07:00
|
|
|
* The generated code is more efficient when nbits is known at
|
|
|
|
* compile-time and at most BITS_PER_LONG.
|
2006-06-26 18:57:10 +07:00
|
|
|
*
|
2017-10-17 06:32:51 +07: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
|
|
|
|
* bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above
|
|
|
|
* bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
|
|
|
|
* bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
|
2020-01-22 06:17:54 +07:00
|
|
|
* bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
|
2019-12-05 07:53:26 +07:00
|
|
|
* bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
|
2017-10-17 06:32:51 +07: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 06:38:02 +07: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 07:50:57 +07: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 06:32:51 +07:00
|
|
|
*
|
2018-02-07 06:38:20 +07: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 05:20:36 +07:00
|
|
|
*/
|
|
|
|
|
2017-10-17 06:32:51 +07: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 06:38:34 +07:00
|
|
|
* find_next_zero_bit(addr, nbits, bit)
|
|
|
|
* Position next zero bit in *addr >= bit
|
2017-10-17 06:32:51 +07: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 06:38:34 +07:00
|
|
|
* find_next_and_bit(addr1, addr2, nbits, bit)
|
|
|
|
* Same as find_next_bit, but in
|
|
|
|
* (*addr1 & *addr2)
|
2005-04-17 05:20:36 +07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-10-17 06:32:51 +07:00
|
|
|
/**
|
|
|
|
* DOC: declare bitmap
|
2005-04-17 05:20:36 +07: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 05:42:56 +07:00
|
|
|
/*
|
|
|
|
* Allocation and deallocation of bitmap.
|
|
|
|
* Provided in lib/bitmap.c to avoid circular dependency.
|
|
|
|
*/
|
|
|
|
extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
|
|
|
|
extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
|
|
|
|
extern void bitmap_free(const unsigned long *bitmap);
|
|
|
|
|
2005-04-17 05:20:36 +07:00
|
|
|
/*
|
|
|
|
* lib/bitmap.c provides these functions:
|
|
|
|
*/
|
|
|
|
|
lib: bitmap: make nbits parameter of bitmap_empty unsigned
Many functions in lib/bitmap.c start with an expression such as lim =
bits/BITS_PER_LONG. Since bits has type (signed) int, and since gcc
cannot know that it is in fact non-negative, it generates worse code
than it could. These patches, mostly consisting of changing various
parameters to unsigned, gives a slight overall code reduction:
add/remove: 1/1 grow/shrink: 8/16 up/down: 251/-414 (-163)
function old new delta
tick_device_uses_broadcast 335 425 +90
__irq_alloc_descs 498 554 +56
__bitmap_andnot 73 115 +42
__bitmap_and 70 101 +31
bitmap_weight - 11 +11
copy_hugetlb_page_range 752 762 +10
follow_hugetlb_page 846 854 +8
hugetlb_init 1415 1417 +2
hugetlb_nrpages_setup 130 131 +1
hugetlb_add_hstate 377 376 -1
bitmap_allocate_region 82 80 -2
select_task_rq_fair 2202 2191 -11
hweight_long 66 55 -11
__reg_op 230 219 -11
dm_stats_message 2849 2833 -16
bitmap_parselist 92 74 -18
__bitmap_weight 115 97 -18
__bitmap_subset 153 129 -24
__bitmap_full 128 104 -24
__bitmap_empty 120 96 -24
bitmap_set 179 149 -30
bitmap_clear 185 155 -30
__bitmap_equal 136 105 -31
__bitmap_intersects 148 108 -40
__bitmap_complement 109 67 -42
tick_device_setup_broadcast_func.isra 81 - -81
[The increases in __bitmap_and{,not} are due to bug fixes 17/18,18/18.
No idea why bitmap_weight suddenly appears.] While 163 bytes treewide is
insignificant, I believe the bitmap functions are often called with
locks held, so saving even a few cycles might be worth it.
While making these changes, I found a few other things that might be
worth including. 16,17,18 are actual bug fixes. The rest shouldn't
change the behaviour of any of the functions, provided no-one passed
negative nbits values. If something should come up, it should be fairly
bisectable.
A few issues I thought about, but didn't know what to do with:
* Many of the functions misbehave if nbits is compile-time 0; the
out-of-line functions generally handle 0 correctly. bitmap_fill() is
particularly bad, whether the 0 is known at compile time or not. It
would probably be nice to add detection of at least compile-time 0 and
handle that appropriately.
* I didn't change __bitmap_shift_{left,right} to use unsigned because I
want to fully understand why the algorithm works before making that
change. However, AFAICT, they behave correctly for all (positive) shift
amounts. This is not the case for the small_const_nbits versions. If
for example nbits = n = BITS_PER_LONG, the shift operators turn into
no-ops (at least on x86), so one get *dst = *src, whereas one would
expect to get *dst=0. That difference in behaviour is somewhat
annoying.
This patch (of 18):
The compiler can generate slightly smaller and simpler code when it
knows that "nbits" is non-negative. Since no-one passes a negative
bit-count, this shouldn't affect the semantics.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-07 06:09:49 +07:00
|
|
|
extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
|
2014-08-07 06:09:51 +07:00
|
|
|
extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
extern int __bitmap_equal(const unsigned long *bitmap1,
|
2014-08-07 06:09:53 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2019-07-23 01:47:24 +07:00
|
|
|
extern bool __pure __bitmap_or_equal(const unsigned long *src1,
|
|
|
|
const unsigned long *src2,
|
|
|
|
const unsigned long *src3,
|
|
|
|
unsigned int nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
|
2014-08-07 06:09:55 +07:00
|
|
|
unsigned int nbits);
|
2015-02-14 05:36:02 +07:00
|
|
|
extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits);
|
2015-02-14 05:36:13 +07:00
|
|
|
extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits);
|
2020-01-22 06:17:54 +07:00
|
|
|
extern void bitmap_cut(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int first, unsigned int cut,
|
|
|
|
unsigned int nbits);
|
2009-08-21 23:26:15 +07:00
|
|
|
extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2009-08-21 23:26:15 +07:00
|
|
|
extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2019-12-05 07:53:26 +07:00
|
|
|
extern void __bitmap_replace(unsigned long *dst,
|
|
|
|
const unsigned long *old, const unsigned long *new,
|
|
|
|
const unsigned long *mask, unsigned int nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
extern int __bitmap_intersects(const unsigned long *bitmap1,
|
2014-08-07 06:10:01 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
extern int __bitmap_subset(const unsigned long *bitmap1,
|
2014-08-07 06:10:03 +07:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2014-08-07 06:10:05 +07:00
|
|
|
extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
|
2017-07-11 05:51:29 +07:00
|
|
|
extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
|
|
|
|
extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
|
2014-12-13 07:54:45 +07:00
|
|
|
|
|
|
|
extern 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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 07:48:25 +07:00
|
|
|
|
2020-02-04 08:37:34 +07:00
|
|
|
extern int bitmap_parse(const char *buf, unsigned int buflen,
|
2006-10-11 15:21:55 +07:00
|
|
|
unsigned long *dst, int nbits);
|
|
|
|
extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
|
2005-04-17 05:20:36 +07:00
|
|
|
unsigned long *dst, int nbits);
|
|
|
|
extern int bitmap_parselist(const char *buf, unsigned long *maskp,
|
|
|
|
int nmaskbits);
|
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 07:13:12 +07:00
|
|
|
extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
|
|
|
|
unsigned long *dst, int nbits);
|
2005-10-31 06:02:33 +07:00
|
|
|
extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
|
2015-02-13 06:02:13 +07:00
|
|
|
const unsigned long *old, const unsigned long *new, unsigned int nbits);
|
2005-10-31 06:02:33 +07:00
|
|
|
extern int bitmap_bitremap(int oldbit,
|
|
|
|
const unsigned long *old, const unsigned long *new, int bits);
|
mempolicy: add bitmap_onto() and bitmap_fold() operations
The following adds two more bitmap operators, bitmap_onto() and bitmap_fold(),
with the usual cpumask and nodemask wrappers.
The bitmap_onto() operator computes one bitmap relative to another. If the
n-th bit in the origin mask is set, then the m-th bit of the destination mask
will be set, where m is the position of the n-th set bit in the relative mask.
The bitmap_fold() operator folds a bitmap into a second that has bit m set iff
the input bitmap has some bit n set, where m == n mod sz, for the specified sz
value.
There are two substantive changes between this patch and its
predecessor bitmap_relative:
1) Renamed bitmap_relative() to be bitmap_onto().
2) Added bitmap_fold().
The essential motivation for bitmap_onto() is to provide a mechanism for
converting a cpuset-relative CPU or Node mask to an absolute mask. Cpuset
relative masks are written as if the current task were in a cpuset whose CPUs
or Nodes were just the consecutive ones numbered 0..N-1, for some N. The
bitmap_onto() operator is provided in anticipation of adding support for the
first such cpuset relative mask, by the mbind() and set_mempolicy() system
calls, using a planned flag of MPOL_F_RELATIVE_NODES. These bitmap operators
(and their nodemask wrappers, in particular) will be used in code that
converts the user specified cpuset relative memory policy to a specific system
node numbered policy, given the current mems_allowed of the tasks cpuset.
Such cpuset relative mempolicies will address two deficiencies
of the existing interface between cpusets and mempolicies:
1) A task cannot at present reliably establish a cpuset
relative mempolicy because there is an essential race
condition, in that the tasks cpuset may be changed in
between the time the task can query its cpuset placement,
and the time the task can issue the applicable mbind or
set_memplicy system call.
2) A task cannot at present establish what cpuset relative
mempolicy it would like to have, if it is in a smaller
cpuset than it might have mempolicy preferences for,
because the existing interface only allows specifying
mempolicies for nodes currently allowed by the cpuset.
Cpuset relative mempolicies are useful for tasks that don't distinguish
particularly between one CPU or Node and another, but only between how many of
each are allowed, and the proper placement of threads and memory pages on the
various CPUs and Nodes available.
The motivation for the added bitmap_fold() can be seen in the following
example.
Let's say an application has specified some mempolicies that presume 16 memory
nodes, including say a mempolicy that specified MPOL_F_RELATIVE_NODES (cpuset
relative) nodes 12-15. Then lets say that application is crammed into a
cpuset that only has 8 memory nodes, 0-7. If one just uses bitmap_onto(),
this mempolicy, mapped to that cpuset, would ignore the requested relative
nodes above 7, leaving it empty of nodes. That's not good; better to fold the
higher nodes down, so that some nodes are included in the resulting mapped
mempolicy. In this case, the mempolicy nodes 12-15 are taken modulo 8 (the
weight of the mems_allowed of the confining cpuset), resulting in a mempolicy
specifying nodes 4-7.
Signed-off-by: Paul Jackson <pj@sgi.com>
Signed-off-by: David Rientjes <rientjes@google.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: <kosaki.motohiro@jp.fujitsu.com>
Cc: <ray-lk@madrabbit.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 16:12:29 +07:00
|
|
|
extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
|
2015-02-13 06:02:01 +07:00
|
|
|
const unsigned long *relmap, unsigned int bits);
|
mempolicy: add bitmap_onto() and bitmap_fold() operations
The following adds two more bitmap operators, bitmap_onto() and bitmap_fold(),
with the usual cpumask and nodemask wrappers.
The bitmap_onto() operator computes one bitmap relative to another. If the
n-th bit in the origin mask is set, then the m-th bit of the destination mask
will be set, where m is the position of the n-th set bit in the relative mask.
The bitmap_fold() operator folds a bitmap into a second that has bit m set iff
the input bitmap has some bit n set, where m == n mod sz, for the specified sz
value.
There are two substantive changes between this patch and its
predecessor bitmap_relative:
1) Renamed bitmap_relative() to be bitmap_onto().
2) Added bitmap_fold().
The essential motivation for bitmap_onto() is to provide a mechanism for
converting a cpuset-relative CPU or Node mask to an absolute mask. Cpuset
relative masks are written as if the current task were in a cpuset whose CPUs
or Nodes were just the consecutive ones numbered 0..N-1, for some N. The
bitmap_onto() operator is provided in anticipation of adding support for the
first such cpuset relative mask, by the mbind() and set_mempolicy() system
calls, using a planned flag of MPOL_F_RELATIVE_NODES. These bitmap operators
(and their nodemask wrappers, in particular) will be used in code that
converts the user specified cpuset relative memory policy to a specific system
node numbered policy, given the current mems_allowed of the tasks cpuset.
Such cpuset relative mempolicies will address two deficiencies
of the existing interface between cpusets and mempolicies:
1) A task cannot at present reliably establish a cpuset
relative mempolicy because there is an essential race
condition, in that the tasks cpuset may be changed in
between the time the task can query its cpuset placement,
and the time the task can issue the applicable mbind or
set_memplicy system call.
2) A task cannot at present establish what cpuset relative
mempolicy it would like to have, if it is in a smaller
cpuset than it might have mempolicy preferences for,
because the existing interface only allows specifying
mempolicies for nodes currently allowed by the cpuset.
Cpuset relative mempolicies are useful for tasks that don't distinguish
particularly between one CPU or Node and another, but only between how many of
each are allowed, and the proper placement of threads and memory pages on the
various CPUs and Nodes available.
The motivation for the added bitmap_fold() can be seen in the following
example.
Let's say an application has specified some mempolicies that presume 16 memory
nodes, including say a mempolicy that specified MPOL_F_RELATIVE_NODES (cpuset
relative) nodes 12-15. Then lets say that application is crammed into a
cpuset that only has 8 memory nodes, 0-7. If one just uses bitmap_onto(),
this mempolicy, mapped to that cpuset, would ignore the requested relative
nodes above 7, leaving it empty of nodes. That's not good; better to fold the
higher nodes down, so that some nodes are included in the resulting mapped
mempolicy. In this case, the mempolicy nodes 12-15 are taken modulo 8 (the
weight of the mems_allowed of the confining cpuset), resulting in a mempolicy
specifying nodes 4-7.
Signed-off-by: Paul Jackson <pj@sgi.com>
Signed-off-by: David Rientjes <rientjes@google.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: <kosaki.motohiro@jp.fujitsu.com>
Cc: <ray-lk@madrabbit.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 16:12:29 +07:00
|
|
|
extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
|
2015-02-13 06:02:04 +07:00
|
|
|
unsigned int sz, unsigned int nbits);
|
2014-08-07 06:10:16 +07:00
|
|
|
extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
|
|
|
|
extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
|
|
|
|
extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
|
2018-02-07 06:38:06 +07:00
|
|
|
|
2015-02-14 05:36:00 +07:00
|
|
|
#ifdef __BIG_ENDIAN
|
2015-02-14 05:35:57 +07:00
|
|
|
extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
|
2015-02-14 05:36:00 +07:00
|
|
|
#else
|
|
|
|
#define bitmap_copy_le bitmap_copy
|
|
|
|
#endif
|
2015-02-13 06:02:10 +07:00
|
|
|
extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
|
2014-09-30 20:48:22 +07:00
|
|
|
extern int bitmap_print_to_pagebuf(bool list, char *buf,
|
|
|
|
const unsigned long *maskp, int nmaskbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
|
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 06:17:42 +07: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 05:20:36 +07:00
|
|
|
|
2018-10-31 05:04:59 +07:00
|
|
|
/*
|
|
|
|
* The static inlines below do not handle constant nbits==0 correctly,
|
|
|
|
* so make such users (should any ever turn up) call the out-of-line
|
|
|
|
* versions.
|
|
|
|
*/
|
2008-12-30 05:35:13 +07:00
|
|
|
#define small_const_nbits(nbits) \
|
2018-10-31 05:04:59 +07:00
|
|
|
(__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG && (nbits) > 0)
|
2008-12-30 05:35:13 +07:00
|
|
|
|
2015-02-13 06:01:53 +07:00
|
|
|
static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
|
2005-04-17 05:20:36 +07: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 05:05:02 +07:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memset(dst, 0, len);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2015-02-13 06:01:53 +07:00
|
|
|
static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
|
2005-04-17 05:20:36 +07: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 05:05:02 +07:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memset(dst, 0xff, len);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
|
2015-02-13 06:01:53 +07:00
|
|
|
unsigned int nbits)
|
2005-04-17 05:20:36 +07: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 05:05:02 +07:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memcpy(dst, src, len);
|
2005-04-17 05:20:36 +07: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 06:38:02 +07: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
|
|
|
|
extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
|
|
|
|
unsigned int nbits);
|
|
|
|
extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
|
|
|
|
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-21 23:26:15 +07:00
|
|
|
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-07 06:10:22 +07:00
|
|
|
return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
2009-08-21 23:26:15 +07:00
|
|
|
return __bitmap_and(dst, src1, src2, nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 05:20:36 +07: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 06:09:59 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 05:20:36 +07:00
|
|
|
*dst = *src1 ^ *src2;
|
|
|
|
else
|
|
|
|
__bitmap_xor(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2009-08-21 23:26:15 +07:00
|
|
|
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
2014-08-07 06:09:59 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-07 06:10:24 +07:00
|
|
|
return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
2009-08-21 23:26:15 +07:00
|
|
|
return __bitmap_andnot(dst, src1, src2, nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
|
2014-08-07 06:09:55 +07:00
|
|
|
unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-07 06:09:57 +07:00
|
|
|
*dst = ~(*src);
|
2005-04-17 05:20:36 +07:00
|
|
|
else
|
|
|
|
__bitmap_complement(dst, src, nbits);
|
|
|
|
}
|
|
|
|
|
2018-04-03 05:58:31 +07: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 05:20:36 +07:00
|
|
|
static inline int bitmap_equal(const unsigned long *src1,
|
2014-08-07 06:09:55 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2016-08-04 03:45:54 +07:00
|
|
|
return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
|
2018-04-03 05:58:31 +07:00
|
|
|
if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
|
2016-05-25 14:32:20 +07:00
|
|
|
return !memcmp(src1, src2, nbits / 8);
|
2016-08-04 03:45:54 +07:00
|
|
|
return __bitmap_equal(src1, src2, nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2019-07-23 01:47:24 +07:00
|
|
|
/**
|
2019-10-15 04:12:20 +07:00
|
|
|
* bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
|
2019-07-23 01:47:24 +07: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 04:12:20 +07:00
|
|
|
* @nbits: number of bits in each of these bitmaps
|
2019-07-23 01:47:24 +07: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 05:20:36 +07:00
|
|
|
static inline int bitmap_intersects(const unsigned long *src1,
|
2014-08-07 06:10:01 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 05:20:36 +07: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 06:10:03 +07:00
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 05:20:36 +07:00
|
|
|
return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
else
|
|
|
|
return __bitmap_subset(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
lib: bitmap: make nbits parameter of bitmap_empty unsigned
Many functions in lib/bitmap.c start with an expression such as lim =
bits/BITS_PER_LONG. Since bits has type (signed) int, and since gcc
cannot know that it is in fact non-negative, it generates worse code
than it could. These patches, mostly consisting of changing various
parameters to unsigned, gives a slight overall code reduction:
add/remove: 1/1 grow/shrink: 8/16 up/down: 251/-414 (-163)
function old new delta
tick_device_uses_broadcast 335 425 +90
__irq_alloc_descs 498 554 +56
__bitmap_andnot 73 115 +42
__bitmap_and 70 101 +31
bitmap_weight - 11 +11
copy_hugetlb_page_range 752 762 +10
follow_hugetlb_page 846 854 +8
hugetlb_init 1415 1417 +2
hugetlb_nrpages_setup 130 131 +1
hugetlb_add_hstate 377 376 -1
bitmap_allocate_region 82 80 -2
select_task_rq_fair 2202 2191 -11
hweight_long 66 55 -11
__reg_op 230 219 -11
dm_stats_message 2849 2833 -16
bitmap_parselist 92 74 -18
__bitmap_weight 115 97 -18
__bitmap_subset 153 129 -24
__bitmap_full 128 104 -24
__bitmap_empty 120 96 -24
bitmap_set 179 149 -30
bitmap_clear 185 155 -30
__bitmap_equal 136 105 -31
__bitmap_intersects 148 108 -40
__bitmap_complement 109 67 -42
tick_device_setup_broadcast_func.isra 81 - -81
[The increases in __bitmap_and{,not} are due to bug fixes 17/18,18/18.
No idea why bitmap_weight suddenly appears.] While 163 bytes treewide is
insignificant, I believe the bitmap functions are often called with
locks held, so saving even a few cycles might be worth it.
While making these changes, I found a few other things that might be
worth including. 16,17,18 are actual bug fixes. The rest shouldn't
change the behaviour of any of the functions, provided no-one passed
negative nbits values. If something should come up, it should be fairly
bisectable.
A few issues I thought about, but didn't know what to do with:
* Many of the functions misbehave if nbits is compile-time 0; the
out-of-line functions generally handle 0 correctly. bitmap_fill() is
particularly bad, whether the 0 is known at compile time or not. It
would probably be nice to add detection of at least compile-time 0 and
handle that appropriately.
* I didn't change __bitmap_shift_{left,right} to use unsigned because I
want to fully understand why the algorithm works before making that
change. However, AFAICT, they behave correctly for all (positive) shift
amounts. This is not the case for the small_const_nbits versions. If
for example nbits = n = BITS_PER_LONG, the shift operators turn into
no-ops (at least on x86), so one get *dst = *src, whereas one would
expect to get *dst=0. That difference in behaviour is somewhat
annoying.
This patch (of 18):
The compiler can generate slightly smaller and simpler code when it
knows that "nbits" is non-negative. Since no-one passes a negative
bit-count, this shouldn't affect the semantics.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-07 06:09:49 +07:00
|
|
|
static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 05:20:36 +07:00
|
|
|
return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
|
2015-04-17 02:44:00 +07:00
|
|
|
|
|
|
|
return find_first_bit(src, nbits) == nbits;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2014-08-07 06:09:51 +07:00
|
|
|
static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-17 05:20:36 +07:00
|
|
|
return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
|
2015-04-17 02:44:00 +07:00
|
|
|
|
|
|
|
return find_first_zero_bit(src, nbits) == nbits;
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2015-08-04 21:15:14 +07:00
|
|
|
static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2006-06-26 18:57:10 +07:00
|
|
|
return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
|
2005-04-17 05:20:36 +07:00
|
|
|
return __bitmap_weight(src, nbits);
|
|
|
|
}
|
|
|
|
|
2017-07-11 05:51:29 +07: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 05:58:31 +07: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 05:51:32 +07:00
|
|
|
memset((char *)map + start / 8, 0xff, nbits / 8);
|
2017-07-11 05:51:29 +07: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 05:58:31 +07: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 05:51:32 +07:00
|
|
|
memset((char *)map + start / 8, 0, nbits / 8);
|
2017-07-11 05:51:29 +07:00
|
|
|
else
|
|
|
|
__bitmap_clear(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
2015-02-14 05:36:02 +07:00
|
|
|
static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
2018-10-31 05:05:07 +07:00
|
|
|
unsigned int shift, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2015-02-14 05:36:02 +07:00
|
|
|
*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
|
2005-04-17 05:20:36 +07:00
|
|
|
else
|
2015-02-14 05:36:02 +07:00
|
|
|
__bitmap_shift_right(dst, src, shift, nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2015-02-14 05:36:13 +07:00
|
|
|
static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits)
|
2005-04-17 05:20:36 +07:00
|
|
|
{
|
2008-12-30 05:35:13 +07:00
|
|
|
if (small_const_nbits(nbits))
|
2015-02-14 05:36:13 +07:00
|
|
|
*dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
else
|
2015-02-14 05:36:13 +07:00
|
|
|
__bitmap_shift_left(dst, src, shift, nbits);
|
2005-04-17 05:20:36 +07:00
|
|
|
}
|
|
|
|
|
2019-12-05 07:53:26 +07: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 07:22:10 +07:00
|
|
|
static inline void bitmap_next_clear_region(unsigned long *bitmap,
|
|
|
|
unsigned int *rs, unsigned int *re,
|
|
|
|
unsigned int end)
|
|
|
|
{
|
|
|
|
*rs = find_next_zero_bit(bitmap, end, *rs);
|
|
|
|
*re = find_next_bit(bitmap, end, *rs + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bitmap region iterators. Iterates over the bitmap between [@start, @end).
|
|
|
|
* @rs and @re should be integer variables and will be set to start and end
|
|
|
|
* index of the current clear or set region.
|
|
|
|
*/
|
|
|
|
#define bitmap_for_each_clear_region(bitmap, rs, re, start, end) \
|
|
|
|
for ((rs) = (start), \
|
|
|
|
bitmap_next_clear_region((bitmap), &(rs), &(re), (end)); \
|
|
|
|
(rs) < (re); \
|
|
|
|
(rs) = (re) + 1, \
|
|
|
|
bitmap_next_clear_region((bitmap), &(rs), &(re), (end)))
|
|
|
|
|
|
|
|
#define bitmap_for_each_set_region(bitmap, rs, re, start, end) \
|
|
|
|
for ((rs) = (start), \
|
|
|
|
bitmap_next_set_region((bitmap), &(rs), &(re), (end)); \
|
|
|
|
(rs) < (re); \
|
|
|
|
(rs) = (re) + 1, \
|
|
|
|
bitmap_next_set_region((bitmap), &(rs), &(re), (end)))
|
|
|
|
|
2017-09-18 09:07:10 +07:00
|
|
|
/**
|
2017-09-09 06:15:41 +07:00
|
|
|
* BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
|
2017-09-18 09:07:10 +07:00
|
|
|
* @n: u64 value
|
2017-09-09 06:15:41 +07: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 09:07:10 +07:00
|
|
|
/**
|
2016-08-17 16:36:08 +07:00
|
|
|
* bitmap_from_u64 - Check and swap words within u64.
|
|
|
|
* @mask: source bitmap
|
|
|
|
* @dst: destination bitmap
|
|
|
|
*
|
2017-09-18 09:07:10 +07:00
|
|
|
* In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
|
2016-08-17 16:36:08 +07:00
|
|
|
* to read u64 mask, we will get the wrong word.
|
2017-09-18 09:07:10 +07:00
|
|
|
* That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
|
2016-08-17 16:36:08 +07: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 07:50:57 +07: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 05:20:36 +07:00
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
|
|
|
#endif /* __LINUX_BITMAP_H */
|