1
linux/tools/perf/util/comm.c

234 lines
5.0 KiB
C
Raw Permalink 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 07:07:57 -07:00
// SPDX-License-Identifier: GPL-2.0
#include "comm.h"
#include <errno.h>
#include <string.h>
#include <internal/rc_check.h>
#include <linux/refcount.h>
#include <linux/zalloc.h>
#include "rwsem.h"
DECLARE_RC_STRUCT(comm_str) {
refcount_t refcnt;
char str[];
};
static struct comm_strs {
struct rw_semaphore lock;
struct comm_str **strs;
int num_strs;
int capacity;
} _comm_strs;
static void comm_strs__remove_if_last(struct comm_str *cs);
static void comm_strs__init(void)
{
init_rwsem(&_comm_strs.lock);
_comm_strs.capacity = 16;
_comm_strs.num_strs = 0;
_comm_strs.strs = calloc(16, sizeof(*_comm_strs.strs));
}
static struct comm_strs *comm_strs__get(void)
{
static pthread_once_t comm_strs_type_once = PTHREAD_ONCE_INIT;
pthread_once(&comm_strs_type_once, comm_strs__init);
return &_comm_strs;
}
static refcount_t *comm_str__refcnt(struct comm_str *cs)
{
return &RC_CHK_ACCESS(cs)->refcnt;
}
static const char *comm_str__str(const struct comm_str *cs)
{
return &RC_CHK_ACCESS(cs)->str[0];
}
static struct comm_str *comm_str__get(struct comm_str *cs)
{
struct comm_str *result;
if (RC_CHK_GET(result, cs))
refcount_inc_not_zero(comm_str__refcnt(cs));
perf tools: Fix struct comm_str removal crash We occasionaly hit following assert failure in 'perf top', when processing the /proc info in multiple threads. perf: ...include/linux/refcount.h:109: refcount_inc: Assertion `!(!refcount_inc_not_zero(r))' failed. The gdb backtrace looks like this: [Switching to Thread 0x7ffff11ba700 (LWP 13749)] 0x00007ffff50839fb in raise () from /lib64/libc.so.6 (gdb) #0 0x00007ffff50839fb in raise () from /lib64/libc.so.6 #1 0x00007ffff5085800 in abort () from /lib64/libc.so.6 #2 0x00007ffff507c0da in __assert_fail_base () from /lib64/libc.so.6 #3 0x00007ffff507c152 in __assert_fail () from /lib64/libc.so.6 #4 0x0000000000535373 in refcount_inc (r=0x7fffdc009be0) at ...include/linux/refcount.h:109 #5 0x00000000005354f1 in comm_str__get (cs=0x7fffdc009bc0) at util/comm.c:24 #6 0x00000000005356bd in __comm_str__findnew (str=0x7fffd000b260 ":2", root=0xbed5c0 <comm_str_root>) at util/comm.c:72 #7 0x000000000053579e in comm_str__findnew (str=0x7fffd000b260 ":2", root=0xbed5c0 <comm_str_root>) at util/comm.c:95 #8 0x000000000053582e in comm__new (str=0x7fffd000b260 ":2", timestamp=0, exec=false) at util/comm.c:111 #9 0x00000000005363bc in thread__new (pid=2, tid=2) at util/thread.c:57 #10 0x0000000000523da0 in ____machine__findnew_thread (machine=0xbfde38, threads=0xbfdf28, pid=2, tid=2, create=true) at util/machine.c:457 #11 0x0000000000523eb4 in __machine__findnew_thread (machine=0xbfde38, ... The failing assertion is this one: REFCOUNT_WARN(!refcount_inc_not_zero(r), ... The problem is that we keep global comm_str_root list, which is accessed by multiple threads during the 'perf top' startup and following 2 paths can race: thread 1: ... thread__new comm__new comm_str__findnew down_write(&comm_str_lock); __comm_str__findnew comm_str__get thread 2: ... comm__override or comm__free comm_str__put refcount_dec_and_test down_write(&comm_str_lock); rb_erase(&cs->rb_node, &comm_str_root); Because thread 2 first decrements the refcnt and only after then it removes the struct comm_str from the list, the thread 1 can find this object on the list with refcnt equls to 0 and hit the assert. This patch fixes the thread 1 __comm_str__findnew path, by ignoring objects that already dropped the refcnt to 0. For the rest of the objects we take the refcnt before comparing its name and release it afterwards with comm_str__put, which can also release the object completely. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Lukasz Odzioba <lukasz.odzioba@intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Wang Nan <wangnan0@huawei.com> Cc: kernel-team@lge.com Link: http://lkml.kernel.org/r/20180720101740.GA27176@krava Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2018-07-20 03:17:40 -07:00
return result;
}
static void comm_str__put(struct comm_str *cs)
{
if (!cs)
return;
if (refcount_dec_and_test(comm_str__refcnt(cs))) {
RC_CHK_FREE(cs);
} else {
if (refcount_read(comm_str__refcnt(cs)) == 1)
comm_strs__remove_if_last(cs);
RC_CHK_PUT(cs);
}
}
static struct comm_str *comm_str__new(const char *str)
{
struct comm_str *result = NULL;
RC_STRUCT(comm_str) *cs;
cs = malloc(sizeof(*cs) + strlen(str) + 1);
if (ADD_RC_CHK(result, cs)) {
refcount_set(comm_str__refcnt(result), 1);
strcpy(&cs->str[0], str);
}
return result;
}
static int comm_str__search(const void *_key, const void *_member)
{
const char *key = _key;
const struct comm_str *member = *(const struct comm_str * const *)_member;
return strcmp(key, comm_str__str(member));
}
static void comm_strs__remove_if_last(struct comm_str *cs)
{
struct comm_strs *comm_strs = comm_strs__get();
down_write(&comm_strs->lock);
/*
* Are there only references from the array, if so remove the array
* reference under the write lock so that we don't race with findnew.
*/
if (refcount_read(comm_str__refcnt(cs)) == 1) {
struct comm_str **entry;
entry = bsearch(comm_str__str(cs), comm_strs->strs, comm_strs->num_strs,
sizeof(struct comm_str *), comm_str__search);
comm_str__put(*entry);
for (int i = entry - comm_strs->strs; i < comm_strs->num_strs - 1; i++)
comm_strs->strs[i] = comm_strs->strs[i + 1];
comm_strs->num_strs--;
}
up_write(&comm_strs->lock);
}
static struct comm_str *__comm_strs__find(struct comm_strs *comm_strs, const char *str)
{
struct comm_str **result;
result = bsearch(str, comm_strs->strs, comm_strs->num_strs, sizeof(struct comm_str *),
comm_str__search);
if (!result)
return NULL;
return comm_str__get(*result);
}
static struct comm_str *comm_strs__findnew(const char *str)
{
struct comm_strs *comm_strs = comm_strs__get();
struct comm_str *result;
if (!comm_strs)
return NULL;
down_read(&comm_strs->lock);
result = __comm_strs__find(comm_strs, str);
up_read(&comm_strs->lock);
if (result)
return result;
down_write(&comm_strs->lock);
result = __comm_strs__find(comm_strs, str);
if (!result) {
if (comm_strs->num_strs == comm_strs->capacity) {
struct comm_str **tmp;
tmp = reallocarray(comm_strs->strs,
comm_strs->capacity + 16,
sizeof(*comm_strs->strs));
if (!tmp) {
up_write(&comm_strs->lock);
return NULL;
}
comm_strs->strs = tmp;
comm_strs->capacity += 16;
}
result = comm_str__new(str);
if (result) {
int low = 0, high = comm_strs->num_strs - 1;
int insert = comm_strs->num_strs; /* Default to inserting at the end. */
while (low <= high) {
int mid = low + (high - low) / 2;
int cmp = strcmp(comm_str__str(comm_strs->strs[mid]), str);
if (cmp < 0) {
low = mid + 1;
} else {
high = mid - 1;
insert = mid;
}
}
memmove(&comm_strs->strs[insert + 1], &comm_strs->strs[insert],
(comm_strs->num_strs - insert) * sizeof(struct comm_str *));
comm_strs->num_strs++;
comm_strs->strs[insert] = result;
}
}
up_write(&comm_strs->lock);
return comm_str__get(result);
}
struct comm *comm__new(const char *str, u64 timestamp, bool exec)
{
struct comm *comm = zalloc(sizeof(*comm));
if (!comm)
return NULL;
comm->start = timestamp;
comm->exec = exec;
comm->comm_str = comm_strs__findnew(str);
if (!comm->comm_str) {
free(comm);
return NULL;
}
return comm;
}
int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
{
struct comm_str *new, *old = comm->comm_str;
new = comm_strs__findnew(str);
if (!new)
return -ENOMEM;
comm_str__put(old);
comm->comm_str = new;
comm->start = timestamp;
if (exec)
comm->exec = true;
return 0;
}
void comm__free(struct comm *comm)
{
comm_str__put(comm->comm_str);
free(comm);
}
const char *comm__str(const struct comm *comm)
{
return comm_str__str(comm->comm_str);
}