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 14:07:57 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef __LINUX_BITMAP_H
|
|
|
|
#define __LINUX_BITMAP_H
|
|
|
|
|
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
2021-05-07 01:02:30 +00:00
|
|
|
#include <linux/align.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/bitops.h>
|
2024-01-22 12:42:40 +00:00
|
|
|
#include <linux/cleanup.h>
|
2023-09-25 02:38:17 +00:00
|
|
|
#include <linux/errno.h>
|
2021-08-14 21:16:59 +00:00
|
|
|
#include <linux/find.h>
|
2021-05-07 01:02:30 +00:00
|
|
|
#include <linux/limits.h>
|
2021-03-15 09:13:55 +00:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/types.h>
|
2023-10-07 23:35:10 +00:00
|
|
|
#include <linux/bitmap-str.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2021-03-15 09:13:56 +00:00
|
|
|
struct device;
|
|
|
|
|
2005-04-16 22:20:36 +00: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
|
2024-12-05 13:20:44 +00:00
|
|
|
* specific are in various arch/<arch>/include/asm/bitops.h headers
|
2005-04-16 22:20:36 +00:00
|
|
|
* and other arch/<arch> specific files.
|
|
|
|
*
|
|
|
|
* See lib/bitmap.c for more details.
|
|
|
|
*/
|
|
|
|
|
2017-10-16 23:32:51 +00:00
|
|
|
/**
|
|
|
|
* DOC: bitmap overview
|
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
* The available bitmap operations and their rough meaning in the
|
|
|
|
* case that the bitmap is a single unsigned long are thus:
|
|
|
|
*
|
2018-10-30 22:05:10 +00:00
|
|
|
* The generated code is more efficient when nbits is known at
|
|
|
|
* compile-time and at most BITS_PER_LONG.
|
2006-06-26 11:57:10 +00:00
|
|
|
*
|
2017-10-16 23:32:51 +00: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
|
2022-09-18 03:07:12 +00:00
|
|
|
* bitmap_weight_and(src1, src2, nbits) Hamming Weight of and'ed bitmap
|
2024-01-29 06:21:04 +00:00
|
|
|
* bitmap_weight_andnot(src1, src2, nbits) Hamming Weight of andnot'ed bitmap
|
2017-10-16 23:32:51 +00:00
|
|
|
* 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 14:09:19 +00:00
|
|
|
* bitmap_find_next_zero_area_off(buf, len, pos, n, mask, mask_off) as above
|
2017-10-16 23:32:51 +00:00
|
|
|
* bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
|
|
|
|
* bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
|
2020-01-21 23:17:54 +00:00
|
|
|
* bitmap_cut(dst, src, first, n, nbits) Cut n bits from first, copy rest
|
2019-12-05 00:53:26 +00:00
|
|
|
* bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask)
|
2024-03-07 11:39:06 +00:00
|
|
|
* bitmap_scatter(dst, src, mask, nbits) *dst = map(dense, sparse)(src)
|
|
|
|
* bitmap_gather(dst, src, mask, nbits) *dst = map(sparse, dense)(src)
|
2017-10-16 23:32:51 +00: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-06 23:38:02 +00:00
|
|
|
* bitmap_from_arr32(dst, buf, nbits) Copy nbits from u32[] buf to dst
|
2022-06-24 09:31:47 +00:00
|
|
|
* bitmap_from_arr64(dst, buf, nbits) Copy nbits from u64[] buf to dst
|
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-06 23:38:02 +00:00
|
|
|
* bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
|
2022-04-28 20:51:13 +00:00
|
|
|
* bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] 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 00:50:57 +00: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
|
2024-03-27 15:23:38 +00:00
|
|
|
* bitmap_read(map, start, nbits) Read an nbits-sized value from
|
|
|
|
* map at start
|
|
|
|
* bitmap_write(map, value, start, nbits) Write an nbits-sized value to
|
|
|
|
* map at start
|
2017-10-16 23:32:51 +00:00
|
|
|
*
|
2018-02-06 23:38:20 +00: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-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-16 23:32:51 +00: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-06 23:38:34 +00:00
|
|
|
* find_next_zero_bit(addr, nbits, bit)
|
|
|
|
* Position next zero bit in *addr >= bit
|
2017-10-16 23:32:51 +00: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-06 23:38:34 +00:00
|
|
|
* find_next_and_bit(addr1, addr2, nbits, bit)
|
|
|
|
* Same as find_next_bit, but in
|
|
|
|
* (*addr1 & *addr2)
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-10-16 23:32:51 +00:00
|
|
|
/**
|
|
|
|
* DOC: declare bitmap
|
2005-04-16 22:20:36 +00: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-01 22:42:56 +00:00
|
|
|
/*
|
|
|
|
* Allocation and deallocation of bitmap.
|
|
|
|
* Provided in lib/bitmap.c to avoid circular dependency.
|
|
|
|
*/
|
2021-03-15 09:13:54 +00:00
|
|
|
unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
|
|
|
|
unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
|
2020-12-30 09:41:52 +00: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 09:13:54 +00:00
|
|
|
void bitmap_free(const unsigned long *bitmap);
|
2018-08-01 22:42:56 +00:00
|
|
|
|
2024-01-22 12:42:40 +00:00
|
|
|
DEFINE_FREE(bitmap, unsigned long *, if (_T) bitmap_free(_T))
|
|
|
|
|
2021-03-15 09:13:56 +00: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-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* lib/bitmap.c provides these functions:
|
|
|
|
*/
|
|
|
|
|
2022-05-18 20:52:22 +00:00
|
|
|
bool __bitmap_equal(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2021-03-15 09:13:54 +00:00
|
|
|
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);
|
2022-07-01 12:54:24 +00:00
|
|
|
bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
2021-03-15 09:13:54 +00:00
|
|
|
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);
|
2022-07-01 12:54:24 +00:00
|
|
|
bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
2021-03-15 09:13:54 +00:00
|
|
|
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);
|
2022-05-18 20:52:22 +00:00
|
|
|
bool __bitmap_intersects(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
|
|
|
bool __bitmap_subset(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2022-08-08 00:52:35 +00:00
|
|
|
unsigned int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
|
2022-09-18 03:07:12 +00:00
|
|
|
unsigned int __bitmap_weight_and(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2024-01-29 06:21:04 +00:00
|
|
|
unsigned int __bitmap_weight_andnot(const unsigned long *bitmap1,
|
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2021-03-15 09:13:54 +00:00
|
|
|
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 00:54:45 +00:00
|
|
|
|
2021-03-15 09:13:54 +00: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 00:54:45 +00: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.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
unsigned long bitmap_find_next_zero_area(unsigned long *map,
|
|
|
|
unsigned long size,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned int nr,
|
|
|
|
unsigned long align_mask)
|
2014-12-13 00:54:45 +00:00
|
|
|
{
|
|
|
|
return bitmap_find_next_zero_area_off(map, size, start, nr,
|
|
|
|
align_mask, 0);
|
|
|
|
}
|
2009-12-16 00:48:25 +00:00
|
|
|
|
2021-03-15 09:13:54 +00:00
|
|
|
void bitmap_remap(unsigned long *dst, const unsigned long *src,
|
2015-02-12 23:02:13 +00:00
|
|
|
const unsigned long *old, const unsigned long *new, unsigned int nbits);
|
2021-03-15 09:13:54 +00:00
|
|
|
int bitmap_bitremap(int oldbit,
|
2005-10-30 23:02:33 +00:00
|
|
|
const unsigned long *old, const unsigned long *new, int bits);
|
2021-03-15 09:13:54 +00:00
|
|
|
void bitmap_onto(unsigned long *dst, const unsigned long *orig,
|
2015-02-12 23:02:01 +00:00
|
|
|
const unsigned long *relmap, unsigned int bits);
|
2021-03-15 09:13:54 +00:00
|
|
|
void bitmap_fold(unsigned long *dst, const unsigned long *orig,
|
2015-02-12 23:02:04 +00:00
|
|
|
unsigned int sz, unsigned int nbits);
|
2018-02-06 23:38:06 +00: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-15 23:17:42 +00: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-16 22:20:36 +00:00
|
|
|
|
bitmap: introduce generic optimized bitmap_size()
The number of times yet another open coded
`BITS_TO_LONGS(nbits) * sizeof(long)` can be spotted is huge.
Some generic helper is long overdue.
Add one, bitmap_size(), but with one detail.
BITS_TO_LONGS() uses DIV_ROUND_UP(). The latter works well when both
divident and divisor are compile-time constants or when the divisor
is not a pow-of-2. When it is however, the compilers sometimes tend
to generate suboptimal code (GCC 13):
48 83 c0 3f add $0x3f,%rax
48 c1 e8 06 shr $0x6,%rax
48 8d 14 c5 00 00 00 00 lea 0x0(,%rax,8),%rdx
%BITS_PER_LONG is always a pow-2 (either 32 or 64), but GCC still does
full division of `nbits + 63` by it and then multiplication by 8.
Instead of BITS_TO_LONGS(), use ALIGN() and then divide by 8. GCC:
8d 50 3f lea 0x3f(%rax),%edx
c1 ea 03 shr $0x3,%edx
81 e2 f8 ff ff 1f and $0x1ffffff8,%edx
Now it shifts `nbits + 63` by 3 positions (IOW performs fast division
by 8) and then masks bits[2:0]. bloat-o-meter:
add/remove: 0/0 grow/shrink: 20/133 up/down: 156/-773 (-617)
Clang does it better and generates the same code before/after starting
from -O1, except that with the ALIGN() approach it uses %edx and thus
still saves some bytes:
add/remove: 0/0 grow/shrink: 9/133 up/down: 18/-538 (-520)
Note that we can't expand DIV_ROUND_UP() by adding a check and using
this approach there, as it's used in array declarations where
expressions are not allowed.
Add this helper to tools/ as well.
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Acked-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2024-03-27 15:23:49 +00:00
|
|
|
#define bitmap_size(nbits) (ALIGN(nbits, BITS_PER_LONG) / BITS_PER_BYTE)
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
bitmap: introduce generic optimized bitmap_size()
The number of times yet another open coded
`BITS_TO_LONGS(nbits) * sizeof(long)` can be spotted is huge.
Some generic helper is long overdue.
Add one, bitmap_size(), but with one detail.
BITS_TO_LONGS() uses DIV_ROUND_UP(). The latter works well when both
divident and divisor are compile-time constants or when the divisor
is not a pow-of-2. When it is however, the compilers sometimes tend
to generate suboptimal code (GCC 13):
48 83 c0 3f add $0x3f,%rax
48 c1 e8 06 shr $0x6,%rax
48 8d 14 c5 00 00 00 00 lea 0x0(,%rax,8),%rdx
%BITS_PER_LONG is always a pow-2 (either 32 or 64), but GCC still does
full division of `nbits + 63` by it and then multiplication by 8.
Instead of BITS_TO_LONGS(), use ALIGN() and then divide by 8. GCC:
8d 50 3f lea 0x3f(%rax),%edx
c1 ea 03 shr $0x3,%edx
81 e2 f8 ff ff 1f and $0x1ffffff8,%edx
Now it shifts `nbits + 63` by 3 positions (IOW performs fast division
by 8) and then masks bits[2:0]. bloat-o-meter:
add/remove: 0/0 grow/shrink: 20/133 up/down: 156/-773 (-617)
Clang does it better and generates the same code before/after starting
from -O1, except that with the ALIGN() approach it uses %edx and thus
still saves some bytes:
add/remove: 0/0 grow/shrink: 9/133 up/down: 18/-538 (-520)
Note that we can't expand DIV_ROUND_UP() by adding a check and using
this approach there, as it's used in array declarations where
expressions are not allowed.
Add this helper to tools/ as well.
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Acked-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2024-03-27 15:23:49 +00:00
|
|
|
unsigned int len = bitmap_size(nbits);
|
bitmap: don't assume compiler evaluates small mem*() builtins calls
Intel kernel bot triggered the build bug on ARC architecture that
in fact is as follows:
DECLARE_BITMAP(bitmap, BITS_PER_LONG);
bitmap_clear(bitmap, 0, BITS_PER_LONG);
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
which can be expanded to:
unsigned long bitmap[1];
memset(bitmap, 0, sizeof(*bitmap));
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
In most cases, a compiler is able to expand small/simple mem*()
calls to simple assignments or bitops, in this case that would mean:
unsigned long bitmap[1] = { 0 };
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
and on most architectures this works, but not on ARC, despite having
-O3 for every build.
So, to make this work, in case when the last bit to modify is still
within the first long (small_const_nbits()), just use plain
assignments for the rest of bitmap_*() functions which still use
mem*(), but didn't receive such compile-time optimizations yet.
This doesn't have the same coverage as compilers provide, but at
least something to start:
text: add/remove: 3/7 grow/shrink: 43/78 up/down: 1848/-3370 (-1546)
data: add/remove: 1/11 grow/shrink: 0/8 up/down: 4/-356 (-352)
notably cpumask_*() family when NR_CPUS <= BITS_PER_LONG:
netif_get_num_default_rss_queues 38 4 -34
cpumask_copy 90 - -90
cpumask_clear 146 - -146
and the abovementioned assertion started passing.
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 12:13:12 +00:00
|
|
|
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
*dst = 0;
|
|
|
|
else
|
|
|
|
memset(dst, 0, len);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
bitmap: introduce generic optimized bitmap_size()
The number of times yet another open coded
`BITS_TO_LONGS(nbits) * sizeof(long)` can be spotted is huge.
Some generic helper is long overdue.
Add one, bitmap_size(), but with one detail.
BITS_TO_LONGS() uses DIV_ROUND_UP(). The latter works well when both
divident and divisor are compile-time constants or when the divisor
is not a pow-of-2. When it is however, the compilers sometimes tend
to generate suboptimal code (GCC 13):
48 83 c0 3f add $0x3f,%rax
48 c1 e8 06 shr $0x6,%rax
48 8d 14 c5 00 00 00 00 lea 0x0(,%rax,8),%rdx
%BITS_PER_LONG is always a pow-2 (either 32 or 64), but GCC still does
full division of `nbits + 63` by it and then multiplication by 8.
Instead of BITS_TO_LONGS(), use ALIGN() and then divide by 8. GCC:
8d 50 3f lea 0x3f(%rax),%edx
c1 ea 03 shr $0x3,%edx
81 e2 f8 ff ff 1f and $0x1ffffff8,%edx
Now it shifts `nbits + 63` by 3 positions (IOW performs fast division
by 8) and then masks bits[2:0]. bloat-o-meter:
add/remove: 0/0 grow/shrink: 20/133 up/down: 156/-773 (-617)
Clang does it better and generates the same code before/after starting
from -O1, except that with the ALIGN() approach it uses %edx and thus
still saves some bytes:
add/remove: 0/0 grow/shrink: 9/133 up/down: 18/-538 (-520)
Note that we can't expand DIV_ROUND_UP() by adding a check and using
this approach there, as it's used in array declarations where
expressions are not allowed.
Add this helper to tools/ as well.
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Acked-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2024-03-27 15:23:49 +00:00
|
|
|
unsigned int len = bitmap_size(nbits);
|
bitmap: don't assume compiler evaluates small mem*() builtins calls
Intel kernel bot triggered the build bug on ARC architecture that
in fact is as follows:
DECLARE_BITMAP(bitmap, BITS_PER_LONG);
bitmap_clear(bitmap, 0, BITS_PER_LONG);
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
which can be expanded to:
unsigned long bitmap[1];
memset(bitmap, 0, sizeof(*bitmap));
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
In most cases, a compiler is able to expand small/simple mem*()
calls to simple assignments or bitops, in this case that would mean:
unsigned long bitmap[1] = { 0 };
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
and on most architectures this works, but not on ARC, despite having
-O3 for every build.
So, to make this work, in case when the last bit to modify is still
within the first long (small_const_nbits()), just use plain
assignments for the rest of bitmap_*() functions which still use
mem*(), but didn't receive such compile-time optimizations yet.
This doesn't have the same coverage as compilers provide, but at
least something to start:
text: add/remove: 3/7 grow/shrink: 43/78 up/down: 1848/-3370 (-1546)
data: add/remove: 1/11 grow/shrink: 0/8 up/down: 4/-356 (-352)
notably cpumask_*() family when NR_CPUS <= BITS_PER_LONG:
netif_get_num_default_rss_queues 38 4 -34
cpumask_copy 90 - -90
cpumask_clear 146 - -146
and the abovementioned assertion started passing.
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 12:13:12 +00:00
|
|
|
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
*dst = ~0UL;
|
|
|
|
else
|
|
|
|
memset(dst, 0xff, len);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_copy(unsigned long *dst, const unsigned long *src, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
bitmap: introduce generic optimized bitmap_size()
The number of times yet another open coded
`BITS_TO_LONGS(nbits) * sizeof(long)` can be spotted is huge.
Some generic helper is long overdue.
Add one, bitmap_size(), but with one detail.
BITS_TO_LONGS() uses DIV_ROUND_UP(). The latter works well when both
divident and divisor are compile-time constants or when the divisor
is not a pow-of-2. When it is however, the compilers sometimes tend
to generate suboptimal code (GCC 13):
48 83 c0 3f add $0x3f,%rax
48 c1 e8 06 shr $0x6,%rax
48 8d 14 c5 00 00 00 00 lea 0x0(,%rax,8),%rdx
%BITS_PER_LONG is always a pow-2 (either 32 or 64), but GCC still does
full division of `nbits + 63` by it and then multiplication by 8.
Instead of BITS_TO_LONGS(), use ALIGN() and then divide by 8. GCC:
8d 50 3f lea 0x3f(%rax),%edx
c1 ea 03 shr $0x3,%edx
81 e2 f8 ff ff 1f and $0x1ffffff8,%edx
Now it shifts `nbits + 63` by 3 positions (IOW performs fast division
by 8) and then masks bits[2:0]. bloat-o-meter:
add/remove: 0/0 grow/shrink: 20/133 up/down: 156/-773 (-617)
Clang does it better and generates the same code before/after starting
from -O1, except that with the ALIGN() approach it uses %edx and thus
still saves some bytes:
add/remove: 0/0 grow/shrink: 9/133 up/down: 18/-538 (-520)
Note that we can't expand DIV_ROUND_UP() by adding a check and using
this approach there, as it's used in array declarations where
expressions are not allowed.
Add this helper to tools/ as well.
Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Acked-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2024-03-27 15:23:49 +00:00
|
|
|
unsigned int len = bitmap_size(nbits);
|
bitmap: don't assume compiler evaluates small mem*() builtins calls
Intel kernel bot triggered the build bug on ARC architecture that
in fact is as follows:
DECLARE_BITMAP(bitmap, BITS_PER_LONG);
bitmap_clear(bitmap, 0, BITS_PER_LONG);
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
which can be expanded to:
unsigned long bitmap[1];
memset(bitmap, 0, sizeof(*bitmap));
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
In most cases, a compiler is able to expand small/simple mem*()
calls to simple assignments or bitops, in this case that would mean:
unsigned long bitmap[1] = { 0 };
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
and on most architectures this works, but not on ARC, despite having
-O3 for every build.
So, to make this work, in case when the last bit to modify is still
within the first long (small_const_nbits()), just use plain
assignments for the rest of bitmap_*() functions which still use
mem*(), but didn't receive such compile-time optimizations yet.
This doesn't have the same coverage as compilers provide, but at
least something to start:
text: add/remove: 3/7 grow/shrink: 43/78 up/down: 1848/-3370 (-1546)
data: add/remove: 1/11 grow/shrink: 0/8 up/down: 4/-356 (-352)
notably cpumask_*() family when NR_CPUS <= BITS_PER_LONG:
netif_get_num_default_rss_queues 38 4 -34
cpumask_copy 90 - -90
cpumask_clear 146 - -146
and the abovementioned assertion started passing.
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 12:13:12 +00:00
|
|
|
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
*dst = *src;
|
|
|
|
else
|
|
|
|
memcpy(dst, src, len);
|
2005-04-16 22:20:36 +00: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-06 23:38:02 +00:00
|
|
|
/*
|
|
|
|
* Copy bitmap and clear tail bits in last word.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_copy_clear_tail(unsigned long *dst, const unsigned long *src, unsigned int nbits)
|
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-06 23:38:02 +00:00
|
|
|
{
|
|
|
|
bitmap_copy(dst, src, nbits);
|
|
|
|
if (nbits % BITS_PER_LONG)
|
|
|
|
dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
}
|
|
|
|
|
fix bitmap corruption on close_range() with CLOSE_RANGE_UNSHARE
copy_fd_bitmaps(new, old, count) is expected to copy the first
count/BITS_PER_LONG bits from old->full_fds_bits[] and fill
the rest with zeroes. What it does is copying enough words
(BITS_TO_LONGS(count/BITS_PER_LONG)), then memsets the rest.
That works fine, *if* all bits past the cutoff point are
clear. Otherwise we are risking garbage from the last word
we'd copied.
For most of the callers that is true - expand_fdtable() has
count equal to old->max_fds, so there's no open descriptors
past count, let alone fully occupied words in ->open_fds[],
which is what bits in ->full_fds_bits[] correspond to.
The other caller (dup_fd()) passes sane_fdtable_size(old_fdt, max_fds),
which is the smallest multiple of BITS_PER_LONG that covers all
opened descriptors below max_fds. In the common case (copying on
fork()) max_fds is ~0U, so all opened descriptors will be below
it and we are fine, by the same reasons why the call in expand_fdtable()
is safe.
Unfortunately, there is a case where max_fds is less than that
and where we might, indeed, end up with junk in ->full_fds_bits[] -
close_range(from, to, CLOSE_RANGE_UNSHARE) with
* descriptor table being currently shared
* 'to' being above the current capacity of descriptor table
* 'from' being just under some chunk of opened descriptors.
In that case we end up with observably wrong behaviour - e.g. spawn
a child with CLONE_FILES, get all descriptors in range 0..127 open,
then close_range(64, ~0U, CLOSE_RANGE_UNSHARE) and watch dup(0) ending
up with descriptor #128, despite #64 being observably not open.
The minimally invasive fix would be to deal with that in dup_fd().
If this proves to add measurable overhead, we can go that way, but
let's try to fix copy_fd_bitmaps() first.
* new helper: bitmap_copy_and_expand(to, from, bits_to_copy, size).
* make copy_fd_bitmaps() take the bitmap size in words, rather than
bits; it's 'count' argument is always a multiple of BITS_PER_LONG,
so we are not losing any information, and that way we can use the
same helper for all three bitmaps - compiler will see that count
is a multiple of BITS_PER_LONG for the large ones, so it'll generate
plain memcpy()+memset().
Reproducer added to tools/testing/selftests/core/close_range_test.c
Cc: stable@vger.kernel.org
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2024-08-03 22:02:00 +00:00
|
|
|
static inline void bitmap_copy_and_extend(unsigned long *to,
|
|
|
|
const unsigned long *from,
|
|
|
|
unsigned int count, unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int copy = BITS_TO_LONGS(count);
|
|
|
|
|
|
|
|
memcpy(to, from, copy * sizeof(long));
|
|
|
|
if (count % BITS_PER_LONG)
|
|
|
|
to[copy - 1] &= BITMAP_LAST_WORD_MASK(count);
|
|
|
|
memset(to + copy, 0, bitmap_size(size) - copy * sizeof(long));
|
|
|
|
}
|
|
|
|
|
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-06 23:38:02 +00:00
|
|
|
/*
|
2022-04-28 20:51:12 +00:00
|
|
|
* On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64
|
|
|
|
* machines the order of hi and lo parts of numbers match the bitmap structure.
|
|
|
|
* In both cases conversion is not needed when copying data from/to arrays of
|
|
|
|
* u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead
|
|
|
|
* to out-of-bound access. To avoid that, both LE and BE variants of 64-bit
|
|
|
|
* architectures are not using bitmap_copy_clear_tail().
|
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-06 23:38:02 +00:00
|
|
|
*/
|
|
|
|
#if BITS_PER_LONG == 64
|
2021-03-15 09:13:54 +00: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-06 23:38:02 +00:00
|
|
|
unsigned int nbits);
|
2021-03-15 09:13:54 +00: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-06 23:38:02 +00: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
|
|
|
|
|
2022-04-28 20:51:13 +00:00
|
|
|
/*
|
2023-02-27 19:24:36 +00:00
|
|
|
* On 64-bit systems bitmaps are represented as u64 arrays internally. So,
|
|
|
|
* the conversion is not needed when copying data from/to arrays of u64.
|
2022-04-28 20:51:13 +00:00
|
|
|
*/
|
2023-02-27 19:24:36 +00:00
|
|
|
#if BITS_PER_LONG == 32
|
2022-04-28 20:51:13 +00:00
|
|
|
void bitmap_from_arr64(unsigned long *bitmap, const u64 *buf, unsigned int nbits);
|
|
|
|
void bitmap_to_arr64(u64 *buf, const unsigned long *bitmap, unsigned int nbits);
|
|
|
|
#else
|
|
|
|
#define bitmap_from_arr64(bitmap, buf, nbits) \
|
|
|
|
bitmap_copy_clear_tail((unsigned long *)(bitmap), (const unsigned long *)(buf), (nbits))
|
|
|
|
#define bitmap_to_arr64(buf, bitmap, nbits) \
|
|
|
|
bitmap_copy_clear_tail((unsigned long *)(buf), (const unsigned long *)(bitmap), (nbits))
|
|
|
|
#endif
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_and(unsigned long *dst, const unsigned long *src1,
|
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-06 23:10:22 +00:00
|
|
|
return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
2009-08-21 16:26:15 +00:00
|
|
|
return __bitmap_and(dst, src1, src2, nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-16 22:20:36 +00:00
|
|
|
*dst = *src1 | *src2;
|
|
|
|
else
|
|
|
|
__bitmap_or(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_xor(unsigned long *dst, const unsigned long *src1,
|
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-16 22:20:36 +00:00
|
|
|
*dst = *src1 ^ *src2;
|
|
|
|
else
|
|
|
|
__bitmap_xor(dst, src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
|
|
|
const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-06 23:10:24 +00:00
|
|
|
return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
2009-08-21 16:26:15 +00:00
|
|
|
return __bitmap_andnot(dst, src1, src2, nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2014-08-06 23:09:57 +00:00
|
|
|
*dst = ~(*src);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
|
|
|
__bitmap_complement(dst, src, nbits);
|
|
|
|
}
|
|
|
|
|
2018-04-02 22:58:31 +00: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)
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_equal(const unsigned long *src1, const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2016-08-03 20:45:54 +00:00
|
|
|
return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
|
2018-04-02 22:58:31 +00:00
|
|
|
if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
|
|
|
|
IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
|
2016-05-25 07:32:20 +00:00
|
|
|
return !memcmp(src1, src2, nbits / 8);
|
2016-08-03 20:45:54 +00:00
|
|
|
return __bitmap_equal(src1, src2, nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:47:24 +00:00
|
|
|
/**
|
2019-10-14 21:12:20 +00:00
|
|
|
* bitmap_or_equal - Check whether the or of two bitmaps is equal to a third
|
2019-07-22 18:47:24 +00: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-14 21:12:20 +00:00
|
|
|
* @nbits: number of bits in each of these bitmaps
|
2019-07-22 18:47:24 +00:00
|
|
|
*
|
|
|
|
* Returns: True if (*@src1 | *@src2) == *@src3, false otherwise
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_or_equal(const unsigned long *src1, const unsigned long *src2,
|
|
|
|
const unsigned long *src3, unsigned int nbits)
|
2019-07-22 18:47:24 +00:00
|
|
|
{
|
|
|
|
if (!small_const_nbits(nbits))
|
|
|
|
return __bitmap_or_equal(src1, src2, src3, nbits);
|
|
|
|
|
|
|
|
return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_intersects(const unsigned long *src1, const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-16 22:20:36 +00:00
|
|
|
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
|
|
|
else
|
|
|
|
return __bitmap_intersects(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_subset(const unsigned long *src1, const unsigned long *src2, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-16 22:20:36 +00:00
|
|
|
return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
else
|
|
|
|
return __bitmap_subset(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_empty(const unsigned long *src, unsigned nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-16 22:20:36 +00:00
|
|
|
return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
|
2015-04-16 19:44:00 +00:00
|
|
|
|
|
|
|
return find_first_bit(src, nbits) == nbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
bool bitmap_full(const unsigned long *src, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2005-04-16 22:20:36 +00:00
|
|
|
return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
|
2015-04-16 19:44:00 +00:00
|
|
|
|
|
|
|
return find_first_zero_bit(src, nbits) == nbits;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 12:54:25 +00:00
|
|
|
static __always_inline
|
2022-08-08 00:52:35 +00:00
|
|
|
unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2006-06-26 11:57:10 +00:00
|
|
|
return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
|
2005-04-16 22:20:36 +00:00
|
|
|
return __bitmap_weight(src, nbits);
|
|
|
|
}
|
|
|
|
|
2022-09-18 03:07:12 +00:00
|
|
|
static __always_inline
|
|
|
|
unsigned long bitmap_weight_and(const unsigned long *src1,
|
|
|
|
const unsigned long *src2, unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
return hweight_long(*src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
return __bitmap_weight_and(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2024-01-29 06:21:04 +00:00
|
|
|
static __always_inline
|
|
|
|
unsigned long bitmap_weight_andnot(const unsigned long *src1,
|
|
|
|
const unsigned long *src2, unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
return hweight_long(*src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits));
|
|
|
|
return __bitmap_weight_andnot(src1, src2, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_set(unsigned long *map, unsigned int start, unsigned int nbits)
|
2017-07-10 22:51:29 +00:00
|
|
|
{
|
|
|
|
if (__builtin_constant_p(nbits) && nbits == 1)
|
|
|
|
__set_bit(start, map);
|
bitmap: don't assume compiler evaluates small mem*() builtins calls
Intel kernel bot triggered the build bug on ARC architecture that
in fact is as follows:
DECLARE_BITMAP(bitmap, BITS_PER_LONG);
bitmap_clear(bitmap, 0, BITS_PER_LONG);
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
which can be expanded to:
unsigned long bitmap[1];
memset(bitmap, 0, sizeof(*bitmap));
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
In most cases, a compiler is able to expand small/simple mem*()
calls to simple assignments or bitops, in this case that would mean:
unsigned long bitmap[1] = { 0 };
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
and on most architectures this works, but not on ARC, despite having
-O3 for every build.
So, to make this work, in case when the last bit to modify is still
within the first long (small_const_nbits()), just use plain
assignments for the rest of bitmap_*() functions which still use
mem*(), but didn't receive such compile-time optimizations yet.
This doesn't have the same coverage as compilers provide, but at
least something to start:
text: add/remove: 3/7 grow/shrink: 43/78 up/down: 1848/-3370 (-1546)
data: add/remove: 1/11 grow/shrink: 0/8 up/down: 4/-356 (-352)
notably cpumask_*() family when NR_CPUS <= BITS_PER_LONG:
netif_get_num_default_rss_queues 38 4 -34
cpumask_copy 90 - -90
cpumask_clear 146 - -146
and the abovementioned assertion started passing.
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 12:13:12 +00:00
|
|
|
else if (small_const_nbits(start + nbits))
|
|
|
|
*map |= GENMASK(start + nbits - 1, start);
|
2018-04-02 22:58:31 +00: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-10 22:51:32 +00:00
|
|
|
memset((char *)map + start / 8, 0xff, nbits / 8);
|
2017-07-10 22:51:29 +00:00
|
|
|
else
|
|
|
|
__bitmap_set(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_clear(unsigned long *map, unsigned int start, unsigned int nbits)
|
2017-07-10 22:51:29 +00:00
|
|
|
{
|
|
|
|
if (__builtin_constant_p(nbits) && nbits == 1)
|
|
|
|
__clear_bit(start, map);
|
bitmap: don't assume compiler evaluates small mem*() builtins calls
Intel kernel bot triggered the build bug on ARC architecture that
in fact is as follows:
DECLARE_BITMAP(bitmap, BITS_PER_LONG);
bitmap_clear(bitmap, 0, BITS_PER_LONG);
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
which can be expanded to:
unsigned long bitmap[1];
memset(bitmap, 0, sizeof(*bitmap));
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
In most cases, a compiler is able to expand small/simple mem*()
calls to simple assignments or bitops, in this case that would mean:
unsigned long bitmap[1] = { 0 };
BUILD_BUG_ON(!__builtin_constant_p(*bitmap));
and on most architectures this works, but not on ARC, despite having
-O3 for every build.
So, to make this work, in case when the last bit to modify is still
within the first long (small_const_nbits()), just use plain
assignments for the rest of bitmap_*() functions which still use
mem*(), but didn't receive such compile-time optimizations yet.
This doesn't have the same coverage as compilers provide, but at
least something to start:
text: add/remove: 3/7 grow/shrink: 43/78 up/down: 1848/-3370 (-1546)
data: add/remove: 1/11 grow/shrink: 0/8 up/down: 4/-356 (-352)
notably cpumask_*() family when NR_CPUS <= BITS_PER_LONG:
netif_get_num_default_rss_queues 38 4 -34
cpumask_copy 90 - -90
cpumask_clear 146 - -146
and the abovementioned assertion started passing.
Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
2022-06-24 12:13:12 +00:00
|
|
|
else if (small_const_nbits(start + nbits))
|
|
|
|
*map &= ~GENMASK(start + nbits - 1, start);
|
2018-04-02 22:58:31 +00: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-10 22:51:32 +00:00
|
|
|
memset((char *)map + start / 8, 0, nbits / 8);
|
2017-07-10 22:51:29 +00:00
|
|
|
else
|
|
|
|
__bitmap_clear(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2015-02-13 22:36:02 +00:00
|
|
|
*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2015-02-13 22:36:02 +00:00
|
|
|
__bitmap_shift_right(dst, src, shift, nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-12-29 22:35:13 +00:00
|
|
|
if (small_const_nbits(nbits))
|
2015-02-13 22:36:13 +00:00
|
|
|
*dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2015-02-13 22:36:13 +00:00
|
|
|
__bitmap_shift_left(dst, src, shift, nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_replace(unsigned long *dst,
|
|
|
|
const unsigned long *old,
|
|
|
|
const unsigned long *new,
|
|
|
|
const unsigned long *mask,
|
|
|
|
unsigned int nbits)
|
2019-12-05 00:53:26 +00:00
|
|
|
{
|
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
*dst = (*old & ~(*mask)) | (*new & *mask);
|
|
|
|
else
|
|
|
|
__bitmap_replace(dst, old, new, mask, nbits);
|
|
|
|
}
|
|
|
|
|
2024-03-07 11:39:06 +00:00
|
|
|
/**
|
|
|
|
* bitmap_scatter - Scatter a bitmap according to the given mask
|
|
|
|
* @dst: scattered bitmap
|
|
|
|
* @src: gathered bitmap
|
|
|
|
* @mask: mask representing bits to assign to in the scattered bitmap
|
|
|
|
* @nbits: number of bits in each of these bitmaps
|
|
|
|
*
|
|
|
|
* Scatters bitmap with sequential bits according to the given @mask.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* If @src bitmap = 0x005a, with @mask = 0x1313, @dst will be 0x0302.
|
|
|
|
*
|
|
|
|
* Or in binary form
|
|
|
|
* @src @mask @dst
|
|
|
|
* 0000000001011010 0001001100010011 0000001100000010
|
|
|
|
*
|
|
|
|
* (Bits 0, 1, 2, 3, 4, 5 are copied to the bits 0, 1, 4, 8, 9, 12)
|
|
|
|
*
|
2024-03-14 12:00:06 +00:00
|
|
|
* A more 'visual' description of the operation::
|
|
|
|
*
|
|
|
|
* src: 0000000001011010
|
|
|
|
* ||||||
|
|
|
|
* +------+|||||
|
|
|
|
* | +----+||||
|
|
|
|
* | |+----+|||
|
|
|
|
* | || +-+||
|
|
|
|
* | || | ||
|
|
|
|
* mask: ...v..vv...v..vv
|
|
|
|
* ...0..11...0..10
|
|
|
|
* dst: 0000001100000010
|
2024-03-07 11:39:06 +00:00
|
|
|
*
|
2025-02-03 12:19:19 +00:00
|
|
|
* A relationship exists between bitmap_scatter() and bitmap_gather(). See
|
|
|
|
* bitmap_gather() for the bitmap gather detailed operations. TL;DR:
|
2024-03-07 11:39:06 +00:00
|
|
|
* bitmap_gather() can be seen as the 'reverse' bitmap_scatter() operation.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_scatter(unsigned long *dst, const unsigned long *src,
|
|
|
|
const unsigned long *mask, unsigned int nbits)
|
2024-03-07 11:39:06 +00:00
|
|
|
{
|
|
|
|
unsigned int n = 0;
|
|
|
|
unsigned int bit;
|
|
|
|
|
|
|
|
bitmap_zero(dst, nbits);
|
|
|
|
|
|
|
|
for_each_set_bit(bit, mask, nbits)
|
|
|
|
__assign_bit(bit, dst, test_bit(n++, src));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_gather - Gather a bitmap according to given mask
|
|
|
|
* @dst: gathered bitmap
|
|
|
|
* @src: scattered bitmap
|
|
|
|
* @mask: mask representing bits to extract from in the scattered bitmap
|
|
|
|
* @nbits: number of bits in each of these bitmaps
|
|
|
|
*
|
|
|
|
* Gathers bitmap with sparse bits according to the given @mask.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* If @src bitmap = 0x0302, with @mask = 0x1313, @dst will be 0x001a.
|
|
|
|
*
|
|
|
|
* Or in binary form
|
|
|
|
* @src @mask @dst
|
|
|
|
* 0000001100000010 0001001100010011 0000000000011010
|
|
|
|
*
|
|
|
|
* (Bits 0, 1, 4, 8, 9, 12 are copied to the bits 0, 1, 2, 3, 4, 5)
|
|
|
|
*
|
2024-03-14 12:00:06 +00:00
|
|
|
* A more 'visual' description of the operation::
|
|
|
|
*
|
|
|
|
* mask: ...v..vv...v..vv
|
|
|
|
* src: 0000001100000010
|
|
|
|
* ^ ^^ ^ 0
|
|
|
|
* | || | 10
|
|
|
|
* | || > 010
|
|
|
|
* | |+--> 1010
|
|
|
|
* | +--> 11010
|
|
|
|
* +----> 011010
|
|
|
|
* dst: 0000000000011010
|
2024-03-07 11:39:06 +00:00
|
|
|
*
|
|
|
|
* A relationship exists between bitmap_gather() and bitmap_scatter(). See
|
2025-02-03 12:19:19 +00:00
|
|
|
* bitmap_scatter() for the bitmap scatter detailed operations. TL;DR:
|
|
|
|
* bitmap_scatter() can be seen as the 'reverse' bitmap_gather() operation.
|
|
|
|
*
|
2024-03-07 11:39:06 +00:00
|
|
|
* Suppose scattered computed using bitmap_scatter(scattered, src, mask, n).
|
|
|
|
* The operation bitmap_gather(result, scattered, mask, n) leads to a result
|
|
|
|
* equal or equivalent to src.
|
|
|
|
*
|
|
|
|
* The result can be 'equivalent' because bitmap_scatter() and bitmap_gather()
|
|
|
|
* are not bijective.
|
|
|
|
* The result and src values are equivalent in that sense that a call to
|
|
|
|
* bitmap_scatter(res, src, mask, n) and a call to
|
|
|
|
* bitmap_scatter(res, result, mask, n) will lead to the same res value.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_gather(unsigned long *dst, const unsigned long *src,
|
|
|
|
const unsigned long *mask, unsigned int nbits)
|
2024-03-07 11:39:06 +00:00
|
|
|
{
|
|
|
|
unsigned int n = 0;
|
|
|
|
unsigned int bit;
|
|
|
|
|
|
|
|
bitmap_zero(dst, nbits);
|
|
|
|
|
|
|
|
for_each_set_bit(bit, mask, nbits)
|
|
|
|
__assign_bit(n++, dst, test_bit(bit, src));
|
|
|
|
}
|
|
|
|
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_next_set_region(unsigned long *bitmap, unsigned int *rs,
|
|
|
|
unsigned int *re, unsigned int end)
|
2019-12-14 00:22:10 +00:00
|
|
|
{
|
|
|
|
*rs = find_next_bit(bitmap, end, *rs);
|
|
|
|
*re = find_next_zero_bit(bitmap, end, *rs + 1);
|
|
|
|
}
|
|
|
|
|
2023-09-25 02:38:17 +00:00
|
|
|
/**
|
|
|
|
* bitmap_release_region - release allocated bitmap region
|
|
|
|
* @bitmap: array of unsigned longs corresponding to the bitmap
|
|
|
|
* @pos: beginning of bit region to release
|
|
|
|
* @order: region size (log base 2 of number of bits) to release
|
|
|
|
*
|
|
|
|
* This is the complement to __bitmap_find_free_region() and releases
|
|
|
|
* the found region (by clearing it in the bitmap).
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order)
|
2023-09-25 02:38:17 +00:00
|
|
|
{
|
|
|
|
bitmap_clear(bitmap, pos, BIT(order));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_allocate_region - allocate bitmap region
|
|
|
|
* @bitmap: array of unsigned longs corresponding to the bitmap
|
|
|
|
* @pos: beginning of bit region to allocate
|
|
|
|
* @order: region size (log base 2 of number of bits) to allocate
|
|
|
|
*
|
|
|
|
* Allocate (set bits in) a specified region of a bitmap.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, or %-EBUSY if specified region wasn't
|
|
|
|
* free (not all bits were zero).
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order)
|
2023-09-25 02:38:17 +00:00
|
|
|
{
|
|
|
|
unsigned int len = BIT(order);
|
|
|
|
|
|
|
|
if (find_next_bit(bitmap, pos + len, pos) < pos + len)
|
|
|
|
return -EBUSY;
|
|
|
|
bitmap_set(bitmap, pos, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_find_free_region - find a contiguous aligned mem region
|
|
|
|
* @bitmap: array of unsigned longs corresponding to the bitmap
|
|
|
|
* @bits: number of bits in the bitmap
|
|
|
|
* @order: region size (log base 2 of number of bits) to find
|
|
|
|
*
|
|
|
|
* Find a region of free (zero) bits in a @bitmap of @bits bits and
|
|
|
|
* allocate them (set them to one). Only consider regions of length
|
|
|
|
* a power (@order) of two, aligned to that power of two, which
|
|
|
|
* makes the search algorithm much faster.
|
|
|
|
*
|
|
|
|
* Returns: the bit offset in bitmap of the allocated region,
|
|
|
|
* or -errno on failure.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order)
|
2023-09-25 02:38:17 +00:00
|
|
|
{
|
|
|
|
unsigned int pos, end; /* scans bitmap by regions of size order */
|
|
|
|
|
|
|
|
for (pos = 0; (end = pos + BIT(order)) <= bits; pos = end) {
|
|
|
|
if (!bitmap_allocate_region(bitmap, pos, order))
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-09-18 02:07:10 +00:00
|
|
|
/**
|
2017-09-08 23:15:41 +00:00
|
|
|
* BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
|
2017-09-18 02:07:10 +00:00
|
|
|
* @n: u64 value
|
2017-09-08 23:15:41 +00: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 02:07:10 +00:00
|
|
|
/**
|
2016-08-17 09:36:08 +00:00
|
|
|
* bitmap_from_u64 - Check and swap words within u64.
|
|
|
|
* @mask: source bitmap
|
|
|
|
* @dst: destination bitmap
|
|
|
|
*
|
2017-09-18 02:07:10 +00:00
|
|
|
* In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
|
2016-08-17 09:36:08 +00:00
|
|
|
* to read u64 mask, we will get the wrong word.
|
2017-09-18 02:07:10 +00:00
|
|
|
* That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
|
2016-08-17 09:36:08 +00:00
|
|
|
* but we expect the lower 32-bits of u64.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline void bitmap_from_u64(unsigned long *dst, u64 mask)
|
2016-08-17 09:36:08 +00:00
|
|
|
{
|
2022-04-28 20:51:13 +00:00
|
|
|
bitmap_from_arr64(dst, &mask, 64);
|
2016-08-17 09:36:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 15:23:38 +00:00
|
|
|
/**
|
|
|
|
* bitmap_read - read a value of n-bits from the memory region
|
|
|
|
* @map: address to the bitmap memory region
|
|
|
|
* @start: bit offset of the n-bit value
|
|
|
|
* @nbits: size of value in bits, nonzero, up to BITS_PER_LONG
|
|
|
|
*
|
|
|
|
* Returns: value of @nbits bits located at the @start bit offset within the
|
|
|
|
* @map memory region. For @nbits = 0 and @nbits > BITS_PER_LONG the return
|
|
|
|
* value is undefined.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
unsigned long bitmap_read(const unsigned long *map, unsigned long start, unsigned long nbits)
|
2024-03-27 15:23:38 +00:00
|
|
|
{
|
|
|
|
size_t index = BIT_WORD(start);
|
|
|
|
unsigned long offset = start % BITS_PER_LONG;
|
|
|
|
unsigned long space = BITS_PER_LONG - offset;
|
|
|
|
unsigned long value_low, value_high;
|
|
|
|
|
|
|
|
if (unlikely(!nbits || nbits > BITS_PER_LONG))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (space >= nbits)
|
|
|
|
return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
|
|
|
|
value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
|
|
|
|
value_high = map[index + 1] & BITMAP_LAST_WORD_MASK(start + nbits);
|
|
|
|
return (value_low >> offset) | (value_high << space);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_write - write n-bit value within a memory region
|
|
|
|
* @map: address to the bitmap memory region
|
|
|
|
* @value: value to write, clamped to nbits
|
|
|
|
* @start: bit offset of the n-bit value
|
|
|
|
* @nbits: size of value in bits, nonzero, up to BITS_PER_LONG.
|
|
|
|
*
|
|
|
|
* bitmap_write() behaves as-if implemented as @nbits calls of __assign_bit(),
|
|
|
|
* i.e. bits beyond @nbits are ignored:
|
|
|
|
*
|
|
|
|
* for (bit = 0; bit < nbits; bit++)
|
|
|
|
* __assign_bit(start + bit, bitmap, val & BIT(bit));
|
|
|
|
*
|
|
|
|
* For @nbits == 0 and @nbits > BITS_PER_LONG no writes are performed.
|
|
|
|
*/
|
2024-07-19 00:50:38 +00:00
|
|
|
static __always_inline
|
|
|
|
void bitmap_write(unsigned long *map, unsigned long value,
|
|
|
|
unsigned long start, unsigned long nbits)
|
2024-03-27 15:23:38 +00:00
|
|
|
{
|
|
|
|
size_t index;
|
|
|
|
unsigned long offset;
|
|
|
|
unsigned long space;
|
|
|
|
unsigned long mask;
|
|
|
|
bool fit;
|
|
|
|
|
|
|
|
if (unlikely(!nbits || nbits > BITS_PER_LONG))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mask = BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
value &= mask;
|
|
|
|
offset = start % BITS_PER_LONG;
|
|
|
|
space = BITS_PER_LONG - offset;
|
|
|
|
fit = space >= nbits;
|
|
|
|
index = BIT_WORD(start);
|
|
|
|
|
|
|
|
map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
|
|
|
|
map[index] |= value << offset;
|
|
|
|
if (fit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
|
|
|
|
map[index + 1] |= (value >> space);
|
|
|
|
}
|
|
|
|
|
2024-03-27 15:23:50 +00:00
|
|
|
#define bitmap_get_value8(map, start) \
|
|
|
|
bitmap_read(map, start, BITS_PER_BYTE)
|
|
|
|
#define bitmap_set_value8(map, value, start) \
|
|
|
|
bitmap_write(map, value, start, BITS_PER_BYTE)
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
|
|
|
#endif /* __LINUX_BITMAP_H */
|