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__
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/string.h>
|
2007-10-19 06:40:37 +00:00
|
|
|
#include <linux/kernel.h>
|
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
|
|
|
|
* specific are in various include/asm-<arch>/bitops.h headers
|
|
|
|
* and other arch/<arch> specific files.
|
|
|
|
*
|
|
|
|
* See lib/bitmap.c for more details.
|
|
|
|
*/
|
|
|
|
|
2017-10-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:
|
|
|
|
*
|
2006-06-26 11:57:10 +00:00
|
|
|
* Note that nbits should be always a compile time evaluable constant.
|
|
|
|
* Otherwise many inlines will generate horrible code.
|
|
|
|
*
|
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
|
|
|
|
* bitmap_set(dst, pos, nbits) Set specified bit area
|
|
|
|
* bitmap_clear(dst, pos, nbits) Clear specified bit area
|
|
|
|
* bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
|
|
|
|
* bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above
|
|
|
|
* bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
|
|
|
|
* bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
|
|
|
|
* 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
|
|
|
|
* bitmap_to_arr32(buf, src, nbits) Copy nbits from buf to u32[] dst
|
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
|
|
|
|
* find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
|
|
|
|
* find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lib/bitmap.c provides these functions:
|
|
|
|
*/
|
|
|
|
|
lib: bitmap: make nbits parameter of bitmap_empty unsigned
Many functions in lib/bitmap.c start with an expression such as lim =
bits/BITS_PER_LONG. Since bits has type (signed) int, and since gcc
cannot know that it is in fact non-negative, it generates worse code
than it could. These patches, mostly consisting of changing various
parameters to unsigned, gives a slight overall code reduction:
add/remove: 1/1 grow/shrink: 8/16 up/down: 251/-414 (-163)
function old new delta
tick_device_uses_broadcast 335 425 +90
__irq_alloc_descs 498 554 +56
__bitmap_andnot 73 115 +42
__bitmap_and 70 101 +31
bitmap_weight - 11 +11
copy_hugetlb_page_range 752 762 +10
follow_hugetlb_page 846 854 +8
hugetlb_init 1415 1417 +2
hugetlb_nrpages_setup 130 131 +1
hugetlb_add_hstate 377 376 -1
bitmap_allocate_region 82 80 -2
select_task_rq_fair 2202 2191 -11
hweight_long 66 55 -11
__reg_op 230 219 -11
dm_stats_message 2849 2833 -16
bitmap_parselist 92 74 -18
__bitmap_weight 115 97 -18
__bitmap_subset 153 129 -24
__bitmap_full 128 104 -24
__bitmap_empty 120 96 -24
bitmap_set 179 149 -30
bitmap_clear 185 155 -30
__bitmap_equal 136 105 -31
__bitmap_intersects 148 108 -40
__bitmap_complement 109 67 -42
tick_device_setup_broadcast_func.isra 81 - -81
[The increases in __bitmap_and{,not} are due to bug fixes 17/18,18/18.
No idea why bitmap_weight suddenly appears.] While 163 bytes treewide is
insignificant, I believe the bitmap functions are often called with
locks held, so saving even a few cycles might be worth it.
While making these changes, I found a few other things that might be
worth including. 16,17,18 are actual bug fixes. The rest shouldn't
change the behaviour of any of the functions, provided no-one passed
negative nbits values. If something should come up, it should be fairly
bisectable.
A few issues I thought about, but didn't know what to do with:
* Many of the functions misbehave if nbits is compile-time 0; the
out-of-line functions generally handle 0 correctly. bitmap_fill() is
particularly bad, whether the 0 is known at compile time or not. It
would probably be nice to add detection of at least compile-time 0 and
handle that appropriately.
* I didn't change __bitmap_shift_{left,right} to use unsigned because I
want to fully understand why the algorithm works before making that
change. However, AFAICT, they behave correctly for all (positive) shift
amounts. This is not the case for the small_const_nbits versions. If
for example nbits = n = BITS_PER_LONG, the shift operators turn into
no-ops (at least on x86), so one get *dst = *src, whereas one would
expect to get *dst=0. That difference in behaviour is somewhat
annoying.
This patch (of 18):
The compiler can generate slightly smaller and simpler code when it
knows that "nbits" is non-negative. Since no-one passes a negative
bit-count, this shouldn't affect the semantics.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-06 23:09:49 +00:00
|
|
|
extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
|
2014-08-06 23:09:51 +00:00
|
|
|
extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern int __bitmap_equal(const unsigned long *bitmap1,
|
2014-08-06 23:09:53 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
|
2014-08-06 23:09:55 +00:00
|
|
|
unsigned int nbits);
|
2015-02-13 22:36:02 +00:00
|
|
|
extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits);
|
2015-02-13 22:36:13 +00:00
|
|
|
extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, unsigned int nbits);
|
2009-08-21 16:26:15 +00:00
|
|
|
extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-06 23:09:59 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-06 23:09:59 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-06 23:09:59 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2009-08-21 16:26:15 +00:00
|
|
|
extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
2014-08-06 23:09:59 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern int __bitmap_intersects(const unsigned long *bitmap1,
|
2014-08-06 23:10:01 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2005-04-16 22:20:36 +00:00
|
|
|
extern int __bitmap_subset(const unsigned long *bitmap1,
|
2014-08-06 23:10:03 +00:00
|
|
|
const unsigned long *bitmap2, unsigned int nbits);
|
2014-08-06 23:10:05 +00:00
|
|
|
extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
|
2017-07-10 22:51:29 +00:00
|
|
|
extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
|
|
|
|
extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
|
2014-12-13 00:54:45 +00:00
|
|
|
|
|
|
|
extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
|
|
|
|
unsigned long size,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned int nr,
|
|
|
|
unsigned long align_mask,
|
|
|
|
unsigned long align_offset);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_find_next_zero_area - find a contiguous aligned zero area
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @align_mask: Alignment mask for zero area
|
|
|
|
*
|
|
|
|
* The @align_mask should be one less than a power of 2; the effect is that
|
|
|
|
* the bit offset of all zero areas this function finds is multiples of that
|
|
|
|
* power of 2. A @align_mask of 0 means no alignment is required.
|
|
|
|
*/
|
|
|
|
static inline unsigned long
|
|
|
|
bitmap_find_next_zero_area(unsigned long *map,
|
|
|
|
unsigned long size,
|
|
|
|
unsigned long start,
|
|
|
|
unsigned int nr,
|
|
|
|
unsigned long align_mask)
|
|
|
|
{
|
|
|
|
return bitmap_find_next_zero_area_off(map, size, start, nr,
|
|
|
|
align_mask, 0);
|
|
|
|
}
|
2009-12-16 00:48:25 +00:00
|
|
|
|
2006-10-11 08:21:55 +00:00
|
|
|
extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
|
|
|
|
unsigned long *dst, int nbits);
|
|
|
|
extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long *dst, int nbits);
|
|
|
|
extern int bitmap_parselist(const char *buf, unsigned long *maskp,
|
|
|
|
int nmaskbits);
|
bitmap, irq: add smp_affinity_list interface to /proc/irq
Manually adjusting the smp_affinity for IRQ's becomes unwieldy when the
cpu count is large.
Setting smp affinity to cpus 256 to 263 would be:
echo 000000ff,00000000,00000000,00000000,00000000,00000000,00000000,00000000 > smp_affinity
instead of:
echo 256-263 > smp_affinity_list
Think about what it looks like for cpus around say, 4088 to 4095.
We already have many alternate "list" interfaces:
/sys/devices/system/cpu/cpuX/indexY/shared_cpu_list
/sys/devices/system/cpu/cpuX/topology/thread_siblings_list
/sys/devices/system/cpu/cpuX/topology/core_siblings_list
/sys/devices/system/node/nodeX/cpulist
/sys/devices/pci***/***/local_cpulist
Add a companion interface, smp_affinity_list to use cpu lists instead of
cpu maps. This conforms to other companion interfaces where both a map
and a list interface exists.
This required adding a bitmap_parselist_user() function in a manner
similar to the bitmap_parse_user() function.
[akpm@linux-foundation.org: make __bitmap_parselist() static]
Signed-off-by: Mike Travis <travis@sgi.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jack Steiner <steiner@sgi.com>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-05-25 00:13:12 +00:00
|
|
|
extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
|
|
|
|
unsigned long *dst, int nbits);
|
2005-10-30 23:02:33 +00:00
|
|
|
extern 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);
|
2005-10-30 23:02:33 +00:00
|
|
|
extern int bitmap_bitremap(int oldbit,
|
|
|
|
const unsigned long *old, const unsigned long *new, int bits);
|
mempolicy: add bitmap_onto() and bitmap_fold() operations
The following adds two more bitmap operators, bitmap_onto() and bitmap_fold(),
with the usual cpumask and nodemask wrappers.
The bitmap_onto() operator computes one bitmap relative to another. If the
n-th bit in the origin mask is set, then the m-th bit of the destination mask
will be set, where m is the position of the n-th set bit in the relative mask.
The bitmap_fold() operator folds a bitmap into a second that has bit m set iff
the input bitmap has some bit n set, where m == n mod sz, for the specified sz
value.
There are two substantive changes between this patch and its
predecessor bitmap_relative:
1) Renamed bitmap_relative() to be bitmap_onto().
2) Added bitmap_fold().
The essential motivation for bitmap_onto() is to provide a mechanism for
converting a cpuset-relative CPU or Node mask to an absolute mask. Cpuset
relative masks are written as if the current task were in a cpuset whose CPUs
or Nodes were just the consecutive ones numbered 0..N-1, for some N. The
bitmap_onto() operator is provided in anticipation of adding support for the
first such cpuset relative mask, by the mbind() and set_mempolicy() system
calls, using a planned flag of MPOL_F_RELATIVE_NODES. These bitmap operators
(and their nodemask wrappers, in particular) will be used in code that
converts the user specified cpuset relative memory policy to a specific system
node numbered policy, given the current mems_allowed of the tasks cpuset.
Such cpuset relative mempolicies will address two deficiencies
of the existing interface between cpusets and mempolicies:
1) A task cannot at present reliably establish a cpuset
relative mempolicy because there is an essential race
condition, in that the tasks cpuset may be changed in
between the time the task can query its cpuset placement,
and the time the task can issue the applicable mbind or
set_memplicy system call.
2) A task cannot at present establish what cpuset relative
mempolicy it would like to have, if it is in a smaller
cpuset than it might have mempolicy preferences for,
because the existing interface only allows specifying
mempolicies for nodes currently allowed by the cpuset.
Cpuset relative mempolicies are useful for tasks that don't distinguish
particularly between one CPU or Node and another, but only between how many of
each are allowed, and the proper placement of threads and memory pages on the
various CPUs and Nodes available.
The motivation for the added bitmap_fold() can be seen in the following
example.
Let's say an application has specified some mempolicies that presume 16 memory
nodes, including say a mempolicy that specified MPOL_F_RELATIVE_NODES (cpuset
relative) nodes 12-15. Then lets say that application is crammed into a
cpuset that only has 8 memory nodes, 0-7. If one just uses bitmap_onto(),
this mempolicy, mapped to that cpuset, would ignore the requested relative
nodes above 7, leaving it empty of nodes. That's not good; better to fold the
higher nodes down, so that some nodes are included in the resulting mapped
mempolicy. In this case, the mempolicy nodes 12-15 are taken modulo 8 (the
weight of the mems_allowed of the confining cpuset), resulting in a mempolicy
specifying nodes 4-7.
Signed-off-by: Paul Jackson <pj@sgi.com>
Signed-off-by: David Rientjes <rientjes@google.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: <kosaki.motohiro@jp.fujitsu.com>
Cc: <ray-lk@madrabbit.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 09:12:29 +00:00
|
|
|
extern 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);
|
mempolicy: add bitmap_onto() and bitmap_fold() operations
The following adds two more bitmap operators, bitmap_onto() and bitmap_fold(),
with the usual cpumask and nodemask wrappers.
The bitmap_onto() operator computes one bitmap relative to another. If the
n-th bit in the origin mask is set, then the m-th bit of the destination mask
will be set, where m is the position of the n-th set bit in the relative mask.
The bitmap_fold() operator folds a bitmap into a second that has bit m set iff
the input bitmap has some bit n set, where m == n mod sz, for the specified sz
value.
There are two substantive changes between this patch and its
predecessor bitmap_relative:
1) Renamed bitmap_relative() to be bitmap_onto().
2) Added bitmap_fold().
The essential motivation for bitmap_onto() is to provide a mechanism for
converting a cpuset-relative CPU or Node mask to an absolute mask. Cpuset
relative masks are written as if the current task were in a cpuset whose CPUs
or Nodes were just the consecutive ones numbered 0..N-1, for some N. The
bitmap_onto() operator is provided in anticipation of adding support for the
first such cpuset relative mask, by the mbind() and set_mempolicy() system
calls, using a planned flag of MPOL_F_RELATIVE_NODES. These bitmap operators
(and their nodemask wrappers, in particular) will be used in code that
converts the user specified cpuset relative memory policy to a specific system
node numbered policy, given the current mems_allowed of the tasks cpuset.
Such cpuset relative mempolicies will address two deficiencies
of the existing interface between cpusets and mempolicies:
1) A task cannot at present reliably establish a cpuset
relative mempolicy because there is an essential race
condition, in that the tasks cpuset may be changed in
between the time the task can query its cpuset placement,
and the time the task can issue the applicable mbind or
set_memplicy system call.
2) A task cannot at present establish what cpuset relative
mempolicy it would like to have, if it is in a smaller
cpuset than it might have mempolicy preferences for,
because the existing interface only allows specifying
mempolicies for nodes currently allowed by the cpuset.
Cpuset relative mempolicies are useful for tasks that don't distinguish
particularly between one CPU or Node and another, but only between how many of
each are allowed, and the proper placement of threads and memory pages on the
various CPUs and Nodes available.
The motivation for the added bitmap_fold() can be seen in the following
example.
Let's say an application has specified some mempolicies that presume 16 memory
nodes, including say a mempolicy that specified MPOL_F_RELATIVE_NODES (cpuset
relative) nodes 12-15. Then lets say that application is crammed into a
cpuset that only has 8 memory nodes, 0-7. If one just uses bitmap_onto(),
this mempolicy, mapped to that cpuset, would ignore the requested relative
nodes above 7, leaving it empty of nodes. That's not good; better to fold the
higher nodes down, so that some nodes are included in the resulting mapped
mempolicy. In this case, the mempolicy nodes 12-15 are taken modulo 8 (the
weight of the mems_allowed of the confining cpuset), resulting in a mempolicy
specifying nodes 4-7.
Signed-off-by: Paul Jackson <pj@sgi.com>
Signed-off-by: David Rientjes <rientjes@google.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Andi Kleen <ak@suse.de>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: <kosaki.motohiro@jp.fujitsu.com>
Cc: <ray-lk@madrabbit.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-28 09:12:29 +00:00
|
|
|
extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
|
2015-02-12 23:02:04 +00:00
|
|
|
unsigned int sz, unsigned int nbits);
|
2014-08-06 23:10:16 +00:00
|
|
|
extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
|
|
|
|
extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
|
|
|
|
extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
|
2018-02-06 23:38:06 +00:00
|
|
|
|
2015-02-13 22:36:00 +00:00
|
|
|
#ifdef __BIG_ENDIAN
|
2015-02-13 22:35:57 +00:00
|
|
|
extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
|
2015-02-13 22:36:00 +00:00
|
|
|
#else
|
|
|
|
#define bitmap_copy_le bitmap_copy
|
|
|
|
#endif
|
2015-02-12 23:02:10 +00:00
|
|
|
extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
|
2014-09-30 13:48:22 +00:00
|
|
|
extern int bitmap_print_to_pagebuf(bool list, char *buf,
|
|
|
|
const unsigned long *maskp, int nmaskbits);
|
2005-04-16 22:20:36 +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
|
|
|
|
2008-12-29 22:35:13 +00:00
|
|
|
#define small_const_nbits(nbits) \
|
|
|
|
(__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
|
|
|
|
|
2015-02-12 23:01:53 +00:00
|
|
|
static inline void bitmap_zero(unsigned long *dst, 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 = 0UL;
|
|
|
|
else {
|
2015-02-12 23:01:53 +00:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
2005-04-16 22:20:36 +00:00
|
|
|
memset(dst, 0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 23:01:53 +00:00
|
|
|
static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2018-02-06 23:38:20 +00:00
|
|
|
if (small_const_nbits(nbits))
|
|
|
|
*dst = ~0UL;
|
|
|
|
else {
|
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
|
|
|
memset(dst, 0xff, len);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
|
2015-02-12 23:01:53 +00:00
|
|
|
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 = *src;
|
|
|
|
else {
|
2015-02-12 23:01:53 +00:00
|
|
|
unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
2005-04-16 22:20:36 +00:00
|
|
|
memcpy(dst, src, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static inline void bitmap_copy_clear_tail(unsigned long *dst,
|
|
|
|
const unsigned long *src, unsigned int nbits)
|
|
|
|
{
|
|
|
|
bitmap_copy(dst, src, nbits);
|
|
|
|
if (nbits % BITS_PER_LONG)
|
|
|
|
dst[nbits / BITS_PER_LONG] &= BITMAP_LAST_WORD_MASK(nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On 32-bit systems bitmaps are represented as u32 arrays internally, and
|
|
|
|
* therefore conversion is not needed when copying data from/to arrays of u32.
|
|
|
|
*/
|
|
|
|
#if BITS_PER_LONG == 64
|
|
|
|
extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
|
|
|
|
unsigned int nbits);
|
|
|
|
extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
|
|
|
|
unsigned int nbits);
|
|
|
|
#else
|
|
|
|
#define bitmap_from_arr32(bitmap, buf, nbits) \
|
|
|
|
bitmap_copy_clear_tail((unsigned long *) (bitmap), \
|
|
|
|
(const unsigned long *) (buf), (nbits))
|
|
|
|
#define bitmap_to_arr32(buf, bitmap, nbits) \
|
|
|
|
bitmap_copy_clear_tail((unsigned long *) (buf), \
|
|
|
|
(const unsigned long *) (bitmap), (nbits))
|
|
|
|
#endif
|
|
|
|
|
2009-08-21 16:26:15 +00:00
|
|
|
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
|
2014-08-06 23:09:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
2014-08-06 23:09:59 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
|
2014-08-06 23:09:59 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-08-21 16:26:15 +00:00
|
|
|
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
2014-08-06 23:09:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
|
2014-08-06 23:09:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_equal(const unsigned long *src1,
|
2014-08-06 23:09:55 +00:00
|
|
|
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));
|
2017-07-10 22:51:35 +00:00
|
|
|
if (__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_intersects(const unsigned long *src1,
|
2014-08-06 23:10:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bitmap_subset(const unsigned long *src1,
|
2014-08-06 23:10:03 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
lib: bitmap: make nbits parameter of bitmap_empty unsigned
Many functions in lib/bitmap.c start with an expression such as lim =
bits/BITS_PER_LONG. Since bits has type (signed) int, and since gcc
cannot know that it is in fact non-negative, it generates worse code
than it could. These patches, mostly consisting of changing various
parameters to unsigned, gives a slight overall code reduction:
add/remove: 1/1 grow/shrink: 8/16 up/down: 251/-414 (-163)
function old new delta
tick_device_uses_broadcast 335 425 +90
__irq_alloc_descs 498 554 +56
__bitmap_andnot 73 115 +42
__bitmap_and 70 101 +31
bitmap_weight - 11 +11
copy_hugetlb_page_range 752 762 +10
follow_hugetlb_page 846 854 +8
hugetlb_init 1415 1417 +2
hugetlb_nrpages_setup 130 131 +1
hugetlb_add_hstate 377 376 -1
bitmap_allocate_region 82 80 -2
select_task_rq_fair 2202 2191 -11
hweight_long 66 55 -11
__reg_op 230 219 -11
dm_stats_message 2849 2833 -16
bitmap_parselist 92 74 -18
__bitmap_weight 115 97 -18
__bitmap_subset 153 129 -24
__bitmap_full 128 104 -24
__bitmap_empty 120 96 -24
bitmap_set 179 149 -30
bitmap_clear 185 155 -30
__bitmap_equal 136 105 -31
__bitmap_intersects 148 108 -40
__bitmap_complement 109 67 -42
tick_device_setup_broadcast_func.isra 81 - -81
[The increases in __bitmap_and{,not} are due to bug fixes 17/18,18/18.
No idea why bitmap_weight suddenly appears.] While 163 bytes treewide is
insignificant, I believe the bitmap functions are often called with
locks held, so saving even a few cycles might be worth it.
While making these changes, I found a few other things that might be
worth including. 16,17,18 are actual bug fixes. The rest shouldn't
change the behaviour of any of the functions, provided no-one passed
negative nbits values. If something should come up, it should be fairly
bisectable.
A few issues I thought about, but didn't know what to do with:
* Many of the functions misbehave if nbits is compile-time 0; the
out-of-line functions generally handle 0 correctly. bitmap_fill() is
particularly bad, whether the 0 is known at compile time or not. It
would probably be nice to add detection of at least compile-time 0 and
handle that appropriately.
* I didn't change __bitmap_shift_{left,right} to use unsigned because I
want to fully understand why the algorithm works before making that
change. However, AFAICT, they behave correctly for all (positive) shift
amounts. This is not the case for the small_const_nbits versions. If
for example nbits = n = BITS_PER_LONG, the shift operators turn into
no-ops (at least on x86), so one get *dst = *src, whereas one would
expect to get *dst=0. That difference in behaviour is somewhat
annoying.
This patch (of 18):
The compiler can generate slightly smaller and simpler code when it
knows that "nbits" is non-negative. Since no-one passes a negative
bit-count, this shouldn't affect the semantics.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-08-06 23:09:49 +00:00
|
|
|
static inline int 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
|
|
|
}
|
|
|
|
|
2014-08-06 23:09:51 +00:00
|
|
|
static inline int 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
|
|
|
}
|
|
|
|
|
2015-08-04 14:15:14 +00:00
|
|
|
static __always_inline 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);
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:51:29 +00:00
|
|
|
static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
|
|
|
|
unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (__builtin_constant_p(nbits) && nbits == 1)
|
|
|
|
__set_bit(start, map);
|
2017-07-10 22:51:32 +00:00
|
|
|
else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
|
|
|
|
__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
|
|
|
|
memset((char *)map + start / 8, 0xff, nbits / 8);
|
2017-07-10 22:51:29 +00:00
|
|
|
else
|
|
|
|
__bitmap_set(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
|
|
|
|
unsigned int nbits)
|
|
|
|
{
|
|
|
|
if (__builtin_constant_p(nbits) && nbits == 1)
|
|
|
|
__clear_bit(start, map);
|
2017-07-10 22:51:32 +00:00
|
|
|
else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
|
|
|
|
__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
|
|
|
|
memset((char *)map + start / 8, 0, nbits / 8);
|
2017-07-10 22:51:29 +00:00
|
|
|
else
|
|
|
|
__bitmap_clear(map, start, nbits);
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:02 +00:00
|
|
|
static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
|
|
|
|
unsigned int shift, 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
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:13 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2006-10-11 08:21:55 +00:00
|
|
|
static inline int bitmap_parse(const char *buf, unsigned int buflen,
|
|
|
|
unsigned long *maskp, int nmaskbits)
|
|
|
|
{
|
|
|
|
return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
|
|
|
|
{
|
|
|
|
dst[0] = mask & ULONG_MAX;
|
|
|
|
|
|
|
|
if (sizeof(mask) > sizeof(unsigned long))
|
|
|
|
dst[1] = mask >> 32;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
|
|
|
#endif /* __LINUX_BITMAP_H */
|