linux/tools/power/cpupower/utils/helpers/bitmask.c

294 lines
7.3 KiB
C
Raw Normal View History

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <helpers/bitmask.h>
/* How many bits in an unsigned long */
#define bitsperlong (8 * sizeof(unsigned long))
/* howmany(a,b) : how many elements of size b needed to hold all of a */
#define howmany(x, y) (((x)+((y)-1))/(y))
/* How many longs in mask of n bits */
#define longsperbits(n) howmany(n, bitsperlong)
#define max(a, b) ((a) > (b) ? (a) : (b))
/*
* Allocate and free `struct bitmask *`
*/
/* Allocate a new `struct bitmask` with a size of n bits */
struct bitmask *bitmask_alloc(unsigned int n)
{
struct bitmask *bmp;
bmp = malloc(sizeof(*bmp));
if (!bmp)
return 0;
bmp->size = n;
bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));
if (!bmp->maskp) {
free(bmp);
return 0;
}
return bmp;
}
/* Free `struct bitmask` */
void bitmask_free(struct bitmask *bmp)
{
if (!bmp)
return;
free(bmp->maskp);
bmp->maskp = (unsigned long *)0xdeadcdef; /* double free tripwire */
free(bmp);
}
/*
* The routines _getbit() and _setbit() are the only
* routines that actually understand the layout of bmp->maskp[].
*
* On little endian architectures, this could simply be an array of
* bytes. But the kernel layout of bitmasks _is_ visible to userspace
* via the sched_(set/get)affinity calls in Linux 2.6, and on big
* endian architectures, it is painfully obvious that this is an
* array of unsigned longs.
*/
/* Return the value (0 or 1) of bit n in bitmask bmp */
static unsigned int _getbit(const struct bitmask *bmp, unsigned int n)
{
if (n < bmp->size)
return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;
else
return 0;
}
/* Set bit n in bitmask bmp to value v (0 or 1) */
static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v)
{
if (n < bmp->size) {
if (v)
bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);
else
bmp->maskp[n/bitsperlong] &=
~(1UL << (n % bitsperlong));
}
}
/*
* When parsing bitmask lists, only allow numbers, separated by one
* of the allowed next characters.
*
* The parameter 'sret' is the return from a sscanf "%u%c". It is
* -1 if the sscanf input string was empty. It is 0 if the first
* character in the sscanf input string was not a decimal number.
* It is 1 if the unsigned number matching the "%u" was the end of the
* input string. It is 2 if one or more additional characters followed
* the matched unsigned number. If it is 2, then 'nextc' is the first
* character following the number. The parameter 'ok_next_chars'
* is the nul-terminated list of allowed next characters.
*
* The mask term just scanned was ok if and only if either the numbers
* matching the %u were all of the input or if the next character in
* the input past the numbers was one of the allowed next characters.
*/
static int scan_was_ok(int sret, char nextc, const char *ok_next_chars)
{
return sret == 1 ||
(sret == 2 && strchr(ok_next_chars, nextc) != NULL);
}
static const char *nexttoken(const char *q, int sep)
{
if (q)
q = strchr(q, sep);
if (q)
q++;
return q;
}
/* Set a single bit i in bitmask */
struct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i)
{
_setbit(bmp, i, 1);
return bmp;
}
/* Set all bits in bitmask: bmp = ~0 */
struct bitmask *bitmask_setall(struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 1);
return bmp;
}
/* Clear all bits in bitmask: bmp = 0 */
struct bitmask *bitmask_clearall(struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 0);
return bmp;
}
/* True if all bits are clear */
int bitmask_isallclear(const struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
if (_getbit(bmp, i))
return 0;
return 1;
}
/* True if specified bit i is set */
int bitmask_isbitset(const struct bitmask *bmp, unsigned int i)
{
return _getbit(bmp, i);
}
/* Number of lowest set bit (min) */
unsigned int bitmask_first(const struct bitmask *bmp)
{
return bitmask_next(bmp, 0);
}
/* Number of highest set bit (max) */
unsigned int bitmask_last(const struct bitmask *bmp)
{
unsigned int i;
unsigned int m = bmp->size;
for (i = 0; i < bmp->size; i++)
if (_getbit(bmp, i))
m = i;
return m;
}
/* Number of next set bit at or above given bit i */
unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i)
{
unsigned int n;
for (n = i; n < bmp->size; n++)
if (_getbit(bmp, n))
break;
return n;
}
/*
* Parses a comma-separated list of numbers and ranges of numbers,
* with optional ':%u' strides modifying ranges, into provided bitmask.
* Some examples of input lists and their equivalent simple list:
* Input Equivalent to
* 0-3 0,1,2,3
* 0-7:2 0,2,4,6
* 1,3,5-7 1,3,5,6,7
* 0-3:2,8-15:4 0,2,8,12
*/
int bitmask_parselist(const char *buf, struct bitmask *bmp)
{
const char *p, *q;
bitmask_clearall(bmp);
q = buf;
while (p = q, q = nexttoken(q, ','), p) {
unsigned int a; /* begin of range */
unsigned int b; /* end of range */
unsigned int s; /* stride */
const char *c1, *c2; /* next tokens after '-' or ',' */
char nextc; /* char after sscanf %u match */
int sret; /* sscanf return (number of matches) */
sret = sscanf(p, "%u%c", &a, &nextc);
if (!scan_was_ok(sret, nextc, ",-"))
goto err;
b = a;
s = 1;
c1 = nexttoken(p, '-');
c2 = nexttoken(p, ',');
if (c1 != NULL && (c2 == NULL || c1 < c2)) {
sret = sscanf(c1, "%u%c", &b, &nextc);
if (!scan_was_ok(sret, nextc, ",:"))
goto err;
c1 = nexttoken(c1, ':');
if (c1 != NULL && (c2 == NULL || c1 < c2)) {
sret = sscanf(c1, "%u%c", &s, &nextc);
if (!scan_was_ok(sret, nextc, ","))
goto err;
}
}
if (!(a <= b))
goto err;
if (b >= bmp->size)
goto err;
while (a <= b) {
_setbit(bmp, a, 1);
a += s;
}
}
return 0;
err:
bitmask_clearall(bmp);
return -1;
}
/*
* emit(buf, buflen, rbot, rtop, len)
*
* Helper routine for bitmask_displaylist(). Write decimal number
* or range to buf+len, suppressing output past buf+buflen, with optional
* comma-prefix. Return len of what would be written to buf, if it
* all fit.
*/
static inline int emit(char *buf, int buflen, int rbot, int rtop, int len)
{
if (len > 0)
len += snprintf(buf + len, max(buflen - len, 0), ",");
if (rbot == rtop)
len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot);
else
len += snprintf(buf + len, max(buflen - len, 0), "%d-%d",
rbot, rtop);
return len;
}
/*
* Write decimal list representation of bmp to buf.
*
* Output format is a comma-separated list of decimal numbers and
* ranges. Consecutively set bits are shown as two hyphen-separated
* decimal numbers, the smallest and largest bit numbers set in
* the range. Output format is compatible with the format
* accepted as input by bitmap_parselist().
*
* The return value is the number of characters which would be
* generated for the given input, excluding the trailing '\0', as
* per ISO C99.
*/
int bitmask_displaylist(char *buf, int buflen, const struct bitmask *bmp)
{
int len = 0;
/* current bit is 'cur', most recently seen range is [rbot, rtop] */
unsigned int cur, rbot, rtop;
if (buflen > 0)
*buf = 0;
rbot = cur = bitmask_first(bmp);
while (cur < bmp->size) {
rtop = cur;
cur = bitmask_next(bmp, cur+1);
if (cur >= bmp->size || cur > rtop + 1) {
len = emit(buf, buflen, rbot, rtop, len);
rbot = cur;
}
}
return len;
}