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 22:07:57 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
#ifndef _BCACHE_UTIL_H
|
|
|
|
#define _BCACHE_UTIL_H
|
|
|
|
|
2013-12-21 09:24:46 +08:00
|
|
|
#include <linux/blkdev.h>
|
2013-03-24 07:11:31 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
2017-02-01 23:36:40 +08:00
|
|
|
#include <linux/sched/clock.h>
|
2013-03-24 07:11:31 +08:00
|
|
|
#include <linux/llist.h>
|
|
|
|
#include <linux/ratelimit.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/workqueue.h>
|
2018-08-22 12:57:15 +08:00
|
|
|
#include <linux/crc64.h>
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
#include "closure.h"
|
|
|
|
|
|
|
|
struct closure;
|
|
|
|
|
2013-10-25 07:36:03 +08:00
|
|
|
#ifdef CONFIG_BCACHE_DEBUG
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-12-18 15:47:33 +08:00
|
|
|
#define EBUG_ON(cond) BUG_ON(cond)
|
2013-03-24 07:11:31 +08:00
|
|
|
#define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0)
|
|
|
|
#define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i)
|
|
|
|
|
2013-10-25 07:36:03 +08:00
|
|
|
#else /* DEBUG */
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2021-04-11 21:43:13 +08:00
|
|
|
#define EBUG_ON(cond) do { if (cond) do {} while (0); } while (0)
|
2013-03-24 07:11:31 +08:00
|
|
|
#define atomic_dec_bug(v) atomic_dec(v)
|
|
|
|
#define atomic_inc_bug(v, i) atomic_inc(v)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DECLARE_HEAP(type, name) \
|
|
|
|
struct { \
|
|
|
|
size_t size, used; \
|
|
|
|
type *data; \
|
|
|
|
} name
|
|
|
|
|
|
|
|
#define init_heap(heap, _size, gfp) \
|
|
|
|
({ \
|
|
|
|
size_t _bytes; \
|
|
|
|
(heap)->used = 0; \
|
|
|
|
(heap)->size = (_size); \
|
|
|
|
_bytes = (heap)->size * sizeof(*(heap)->data); \
|
2017-05-09 06:57:27 +08:00
|
|
|
(heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
|
2013-03-24 07:11:31 +08:00
|
|
|
(heap)->data; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define free_heap(heap) \
|
|
|
|
do { \
|
2015-07-01 05:59:30 +08:00
|
|
|
kvfree((heap)->data); \
|
2013-03-24 07:11:31 +08:00
|
|
|
(heap)->data = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
|
|
|
|
|
|
|
|
#define heap_sift(h, i, cmp) \
|
|
|
|
do { \
|
|
|
|
size_t _r, _j = i; \
|
|
|
|
\
|
|
|
|
for (; _j * 2 + 1 < (h)->used; _j = _r) { \
|
|
|
|
_r = _j * 2 + 1; \
|
|
|
|
if (_r + 1 < (h)->used && \
|
|
|
|
cmp((h)->data[_r], (h)->data[_r + 1])) \
|
|
|
|
_r++; \
|
|
|
|
\
|
|
|
|
if (cmp((h)->data[_r], (h)->data[_j])) \
|
|
|
|
break; \
|
|
|
|
heap_swap(h, _r, _j); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define heap_sift_down(h, i, cmp) \
|
|
|
|
do { \
|
|
|
|
while (i) { \
|
|
|
|
size_t p = (i - 1) / 2; \
|
|
|
|
if (cmp((h)->data[i], (h)->data[p])) \
|
|
|
|
break; \
|
|
|
|
heap_swap(h, i, p); \
|
|
|
|
i = p; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define heap_add(h, d, cmp) \
|
|
|
|
({ \
|
|
|
|
bool _r = !heap_full(h); \
|
|
|
|
if (_r) { \
|
|
|
|
size_t _i = (h)->used++; \
|
|
|
|
(h)->data[_i] = d; \
|
|
|
|
\
|
|
|
|
heap_sift_down(h, _i, cmp); \
|
|
|
|
heap_sift(h, _i, cmp); \
|
|
|
|
} \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define heap_pop(h, d, cmp) \
|
|
|
|
({ \
|
|
|
|
bool _r = (h)->used; \
|
|
|
|
if (_r) { \
|
|
|
|
(d) = (h)->data[0]; \
|
|
|
|
(h)->used--; \
|
|
|
|
heap_swap(h, 0, (h)->used); \
|
|
|
|
heap_sift(h, 0, cmp); \
|
|
|
|
} \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
2013-10-24 08:35:26 +08:00
|
|
|
#define heap_peek(h) ((h)->used ? (h)->data[0] : NULL)
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
#define heap_full(h) ((h)->used == (h)->size)
|
|
|
|
|
|
|
|
#define DECLARE_FIFO(type, name) \
|
|
|
|
struct { \
|
|
|
|
size_t front, back, size, mask; \
|
|
|
|
type *data; \
|
|
|
|
} name
|
|
|
|
|
|
|
|
#define fifo_for_each(c, fifo, iter) \
|
|
|
|
for (iter = (fifo)->front; \
|
|
|
|
c = (fifo)->data[iter], iter != (fifo)->back; \
|
|
|
|
iter = (iter + 1) & (fifo)->mask)
|
|
|
|
|
|
|
|
#define __init_fifo(fifo, gfp) \
|
|
|
|
({ \
|
|
|
|
size_t _allocated_size, _bytes; \
|
|
|
|
BUG_ON(!(fifo)->size); \
|
|
|
|
\
|
|
|
|
_allocated_size = roundup_pow_of_two((fifo)->size + 1); \
|
|
|
|
_bytes = _allocated_size * sizeof(*(fifo)->data); \
|
|
|
|
\
|
|
|
|
(fifo)->mask = _allocated_size - 1; \
|
|
|
|
(fifo)->front = (fifo)->back = 0; \
|
|
|
|
\
|
2017-05-09 06:57:27 +08:00
|
|
|
(fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
|
2013-03-24 07:11:31 +08:00
|
|
|
(fifo)->data; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define init_fifo_exact(fifo, _size, gfp) \
|
|
|
|
({ \
|
|
|
|
(fifo)->size = (_size); \
|
|
|
|
__init_fifo(fifo, gfp); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define init_fifo(fifo, _size, gfp) \
|
|
|
|
({ \
|
|
|
|
(fifo)->size = (_size); \
|
|
|
|
if ((fifo)->size > 4) \
|
|
|
|
(fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
|
|
|
|
__init_fifo(fifo, gfp); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define free_fifo(fifo) \
|
|
|
|
do { \
|
2015-07-01 05:59:30 +08:00
|
|
|
kvfree((fifo)->data); \
|
2013-03-24 07:11:31 +08:00
|
|
|
(fifo)->data = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
|
|
|
|
#define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
|
|
|
|
|
|
|
|
#define fifo_empty(fifo) (!fifo_used(fifo))
|
|
|
|
#define fifo_full(fifo) (!fifo_free(fifo))
|
|
|
|
|
|
|
|
#define fifo_front(fifo) ((fifo)->data[(fifo)->front])
|
|
|
|
#define fifo_back(fifo) \
|
|
|
|
((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
|
|
|
|
|
|
|
|
#define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
|
|
|
|
|
|
|
|
#define fifo_push_back(fifo, i) \
|
|
|
|
({ \
|
|
|
|
bool _r = !fifo_full((fifo)); \
|
|
|
|
if (_r) { \
|
|
|
|
(fifo)->data[(fifo)->back++] = (i); \
|
|
|
|
(fifo)->back &= (fifo)->mask; \
|
|
|
|
} \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define fifo_pop_front(fifo, i) \
|
|
|
|
({ \
|
|
|
|
bool _r = !fifo_empty((fifo)); \
|
|
|
|
if (_r) { \
|
|
|
|
(i) = (fifo)->data[(fifo)->front++]; \
|
|
|
|
(fifo)->front &= (fifo)->mask; \
|
|
|
|
} \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define fifo_push_front(fifo, i) \
|
|
|
|
({ \
|
|
|
|
bool _r = !fifo_full((fifo)); \
|
|
|
|
if (_r) { \
|
|
|
|
--(fifo)->front; \
|
|
|
|
(fifo)->front &= (fifo)->mask; \
|
|
|
|
(fifo)->data[(fifo)->front] = (i); \
|
|
|
|
} \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define fifo_pop_back(fifo, i) \
|
|
|
|
({ \
|
|
|
|
bool _r = !fifo_empty((fifo)); \
|
|
|
|
if (_r) { \
|
|
|
|
--(fifo)->back; \
|
|
|
|
(fifo)->back &= (fifo)->mask; \
|
|
|
|
(i) = (fifo)->data[(fifo)->back] \
|
|
|
|
} \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define fifo_push(fifo, i) fifo_push_back(fifo, (i))
|
|
|
|
#define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
|
|
|
|
|
|
|
|
#define fifo_swap(l, r) \
|
|
|
|
do { \
|
|
|
|
swap((l)->front, (r)->front); \
|
|
|
|
swap((l)->back, (r)->back); \
|
|
|
|
swap((l)->size, (r)->size); \
|
|
|
|
swap((l)->mask, (r)->mask); \
|
|
|
|
swap((l)->data, (r)->data); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define fifo_move(dest, src) \
|
|
|
|
do { \
|
|
|
|
typeof(*((dest)->data)) _t; \
|
|
|
|
while (!fifo_full(dest) && \
|
|
|
|
fifo_pop(src, _t)) \
|
|
|
|
fifo_push(dest, _t); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple array based allocator - preallocates a number of elements and you can
|
|
|
|
* never allocate more than that, also has no locking.
|
|
|
|
*
|
|
|
|
* Handy because if you know you only need a fixed number of elements you don't
|
|
|
|
* have to worry about memory allocation failure, and sometimes a mempool isn't
|
|
|
|
* what you want.
|
|
|
|
*
|
|
|
|
* We treat the free elements as entries in a singly linked list, and the
|
|
|
|
* freelist as a stack - allocating and freeing push and pop off the freelist.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
|
|
|
|
struct { \
|
|
|
|
type *freelist; \
|
|
|
|
type data[size]; \
|
|
|
|
} name
|
|
|
|
|
|
|
|
#define array_alloc(array) \
|
|
|
|
({ \
|
|
|
|
typeof((array)->freelist) _ret = (array)->freelist; \
|
|
|
|
\
|
|
|
|
if (_ret) \
|
|
|
|
(array)->freelist = *((typeof((array)->freelist) *) _ret);\
|
|
|
|
\
|
|
|
|
_ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define array_free(array, ptr) \
|
|
|
|
do { \
|
|
|
|
typeof((array)->freelist) _ptr = ptr; \
|
|
|
|
\
|
|
|
|
*((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
|
|
|
|
(array)->freelist = _ptr; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define array_allocator_init(array) \
|
|
|
|
do { \
|
|
|
|
typeof((array)->freelist) _i; \
|
|
|
|
\
|
|
|
|
BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
|
|
|
|
(array)->freelist = NULL; \
|
|
|
|
\
|
|
|
|
for (_i = (array)->data; \
|
|
|
|
_i < (array)->data + ARRAY_SIZE((array)->data); \
|
|
|
|
_i++) \
|
|
|
|
array_free(array, _i); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define array_freelist_empty(array) ((array)->freelist == NULL)
|
|
|
|
|
|
|
|
#define ANYSINT_MAX(t) \
|
|
|
|
((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
|
|
|
|
|
2018-08-11 13:19:46 +08:00
|
|
|
int bch_strtoint_h(const char *cp, int *res);
|
|
|
|
int bch_strtouint_h(const char *cp, unsigned int *res);
|
|
|
|
int bch_strtoll_h(const char *cp, long long *res);
|
|
|
|
int bch_strtoull_h(const char *cp, unsigned long long *res);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-03-29 02:50:55 +08:00
|
|
|
static inline int bch_strtol_h(const char *cp, long *res)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
|
|
|
#if BITS_PER_LONG == 32
|
2013-03-29 02:50:55 +08:00
|
|
|
return bch_strtoint_h(cp, (int *) res);
|
2013-03-24 07:11:31 +08:00
|
|
|
#else
|
2013-03-29 02:50:55 +08:00
|
|
|
return bch_strtoll_h(cp, (long long *) res);
|
2013-03-24 07:11:31 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-29 02:50:55 +08:00
|
|
|
static inline int bch_strtoul_h(const char *cp, long *res)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
|
|
|
#if BITS_PER_LONG == 32
|
2013-03-29 02:50:55 +08:00
|
|
|
return bch_strtouint_h(cp, (unsigned int *) res);
|
2013-03-24 07:11:31 +08:00
|
|
|
#else
|
2013-03-29 02:50:55 +08:00
|
|
|
return bch_strtoull_h(cp, (unsigned long long *) res);
|
2013-03-24 07:11:31 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define strtoi_h(cp, res) \
|
|
|
|
(__builtin_types_compatible_p(typeof(*res), int) \
|
2013-03-29 02:50:55 +08:00
|
|
|
? bch_strtoint_h(cp, (void *) res) \
|
2013-03-24 07:11:31 +08:00
|
|
|
: __builtin_types_compatible_p(typeof(*res), long) \
|
2013-03-29 02:50:55 +08:00
|
|
|
? bch_strtol_h(cp, (void *) res) \
|
2013-03-24 07:11:31 +08:00
|
|
|
: __builtin_types_compatible_p(typeof(*res), long long) \
|
2013-03-29 02:50:55 +08:00
|
|
|
? bch_strtoll_h(cp, (void *) res) \
|
2013-03-24 07:11:31 +08:00
|
|
|
: __builtin_types_compatible_p(typeof(*res), unsigned int) \
|
2013-03-29 02:50:55 +08:00
|
|
|
? bch_strtouint_h(cp, (void *) res) \
|
2013-03-24 07:11:31 +08:00
|
|
|
: __builtin_types_compatible_p(typeof(*res), unsigned long) \
|
2013-03-29 02:50:55 +08:00
|
|
|
? bch_strtoul_h(cp, (void *) res) \
|
2013-03-24 07:11:31 +08:00
|
|
|
: __builtin_types_compatible_p(typeof(*res), unsigned long long)\
|
2013-03-29 02:50:55 +08:00
|
|
|
? bch_strtoull_h(cp, (void *) res) : -EINVAL)
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
#define strtoul_safe(cp, var) \
|
|
|
|
({ \
|
|
|
|
unsigned long _v; \
|
|
|
|
int _r = kstrtoul(cp, 10, &_v); \
|
|
|
|
if (!_r) \
|
|
|
|
var = _v; \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define strtoul_safe_clamp(cp, var, min, max) \
|
|
|
|
({ \
|
|
|
|
unsigned long _v; \
|
|
|
|
int _r = kstrtoul(cp, 10, &_v); \
|
|
|
|
if (!_r) \
|
|
|
|
var = clamp_t(typeof(var), _v, min, max); \
|
|
|
|
_r; \
|
|
|
|
})
|
|
|
|
|
2013-03-29 02:50:55 +08:00
|
|
|
ssize_t bch_hprint(char *buf, int64_t v);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-03-29 02:50:55 +08:00
|
|
|
bool bch_is_zero(const char *p, size_t n);
|
|
|
|
int bch_parse_uuid(const char *s, char *uuid);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
struct time_stats {
|
2013-07-31 15:03:54 +08:00
|
|
|
spinlock_t lock;
|
2013-03-24 07:11:31 +08:00
|
|
|
/*
|
|
|
|
* all fields are in nanoseconds, averages are ewmas stored left shifted
|
|
|
|
* by 8
|
|
|
|
*/
|
|
|
|
uint64_t max_duration;
|
|
|
|
uint64_t average_duration;
|
|
|
|
uint64_t average_frequency;
|
|
|
|
uint64_t last;
|
|
|
|
};
|
|
|
|
|
2013-03-29 02:50:55 +08:00
|
|
|
void bch_time_stats_update(struct time_stats *stats, uint64_t time);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2018-08-11 13:19:44 +08:00
|
|
|
static inline unsigned int local_clock_us(void)
|
2013-12-21 09:24:46 +08:00
|
|
|
{
|
|
|
|
return local_clock() >> 10;
|
|
|
|
}
|
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
#define NSEC_PER_ns 1L
|
|
|
|
#define NSEC_PER_us NSEC_PER_USEC
|
|
|
|
#define NSEC_PER_ms NSEC_PER_MSEC
|
|
|
|
#define NSEC_PER_sec NSEC_PER_SEC
|
|
|
|
|
|
|
|
#define __print_time_stat(stats, name, stat, units) \
|
|
|
|
sysfs_print(name ## _ ## stat ## _ ## units, \
|
|
|
|
div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
|
|
|
|
|
|
|
|
#define sysfs_print_time_stats(stats, name, \
|
|
|
|
frequency_units, \
|
|
|
|
duration_units) \
|
|
|
|
do { \
|
|
|
|
__print_time_stat(stats, name, \
|
|
|
|
average_frequency, frequency_units); \
|
|
|
|
__print_time_stat(stats, name, \
|
|
|
|
average_duration, duration_units); \
|
2014-04-18 03:07:04 +08:00
|
|
|
sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
|
2018-08-11 13:19:47 +08:00
|
|
|
div_u64((stats)->max_duration, \
|
|
|
|
NSEC_PER_ ## duration_units)); \
|
2013-03-24 07:11:31 +08:00
|
|
|
\
|
|
|
|
sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
|
|
|
|
? div_s64(local_clock() - (stats)->last, \
|
|
|
|
NSEC_PER_ ## frequency_units) \
|
|
|
|
: -1LL); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define sysfs_time_stats_attribute(name, \
|
|
|
|
frequency_units, \
|
|
|
|
duration_units) \
|
|
|
|
read_attribute(name ## _average_frequency_ ## frequency_units); \
|
|
|
|
read_attribute(name ## _average_duration_ ## duration_units); \
|
|
|
|
read_attribute(name ## _max_duration_ ## duration_units); \
|
|
|
|
read_attribute(name ## _last_ ## frequency_units)
|
|
|
|
|
|
|
|
#define sysfs_time_stats_attribute_list(name, \
|
|
|
|
frequency_units, \
|
|
|
|
duration_units) \
|
|
|
|
&sysfs_ ## name ## _average_frequency_ ## frequency_units, \
|
|
|
|
&sysfs_ ## name ## _average_duration_ ## duration_units, \
|
|
|
|
&sysfs_ ## name ## _max_duration_ ## duration_units, \
|
|
|
|
&sysfs_ ## name ## _last_ ## frequency_units,
|
|
|
|
|
|
|
|
#define ewma_add(ewma, val, weight, factor) \
|
|
|
|
({ \
|
|
|
|
(ewma) *= (weight) - 1; \
|
|
|
|
(ewma) += (val) << factor; \
|
|
|
|
(ewma) /= (weight); \
|
|
|
|
(ewma) >> factor; \
|
|
|
|
})
|
|
|
|
|
2013-09-24 14:17:31 +08:00
|
|
|
struct bch_ratelimit {
|
|
|
|
/* Next time we want to do some work, in nanoseconds */
|
2013-03-24 07:11:31 +08:00
|
|
|
uint64_t next;
|
2013-09-24 14:17:31 +08:00
|
|
|
|
|
|
|
/*
|
2017-10-14 07:35:38 +08:00
|
|
|
* Rate at which we want to do work, in units per second
|
2013-09-24 14:17:31 +08:00
|
|
|
* The units here correspond to the units passed to bch_next_delay()
|
|
|
|
*/
|
2018-08-09 15:48:49 +08:00
|
|
|
atomic_long_t rate;
|
2013-03-24 07:11:31 +08:00
|
|
|
};
|
|
|
|
|
2013-09-24 14:17:31 +08:00
|
|
|
static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
|
|
|
d->next = local_clock();
|
|
|
|
}
|
|
|
|
|
2013-09-24 14:17:31 +08:00
|
|
|
uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
#define __DIV_SAFE(n, d, zero) \
|
|
|
|
({ \
|
|
|
|
typeof(n) _n = (n); \
|
|
|
|
typeof(d) _d = (d); \
|
|
|
|
_d ? _n / _d : zero; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
|
|
|
|
|
|
|
|
#define container_of_or_null(ptr, type, member) \
|
|
|
|
({ \
|
|
|
|
typeof(ptr) _ptr = ptr; \
|
|
|
|
_ptr ? container_of(_ptr, type, member) : NULL; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define RB_INSERT(root, new, member, cmp) \
|
|
|
|
({ \
|
|
|
|
__label__ dup; \
|
|
|
|
struct rb_node **n = &(root)->rb_node, *parent = NULL; \
|
|
|
|
typeof(new) this; \
|
|
|
|
int res, ret = -1; \
|
|
|
|
\
|
|
|
|
while (*n) { \
|
|
|
|
parent = *n; \
|
|
|
|
this = container_of(*n, typeof(*(new)), member); \
|
|
|
|
res = cmp(new, this); \
|
|
|
|
if (!res) \
|
|
|
|
goto dup; \
|
|
|
|
n = res < 0 \
|
|
|
|
? &(*n)->rb_left \
|
|
|
|
: &(*n)->rb_right; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
rb_link_node(&(new)->member, parent, n); \
|
|
|
|
rb_insert_color(&(new)->member, root); \
|
|
|
|
ret = 0; \
|
|
|
|
dup: \
|
|
|
|
ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define RB_SEARCH(root, search, member, cmp) \
|
|
|
|
({ \
|
|
|
|
struct rb_node *n = (root)->rb_node; \
|
|
|
|
typeof(&(search)) this, ret = NULL; \
|
|
|
|
int res; \
|
|
|
|
\
|
|
|
|
while (n) { \
|
|
|
|
this = container_of(n, typeof(search), member); \
|
|
|
|
res = cmp(&(search), this); \
|
|
|
|
if (!res) { \
|
|
|
|
ret = this; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
n = res < 0 \
|
|
|
|
? n->rb_left \
|
|
|
|
: n->rb_right; \
|
|
|
|
} \
|
|
|
|
ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define RB_GREATER(root, search, member, cmp) \
|
|
|
|
({ \
|
|
|
|
struct rb_node *n = (root)->rb_node; \
|
|
|
|
typeof(&(search)) this, ret = NULL; \
|
|
|
|
int res; \
|
|
|
|
\
|
|
|
|
while (n) { \
|
|
|
|
this = container_of(n, typeof(search), member); \
|
|
|
|
res = cmp(&(search), this); \
|
|
|
|
if (res < 0) { \
|
|
|
|
ret = this; \
|
|
|
|
n = n->rb_left; \
|
|
|
|
} else \
|
|
|
|
n = n->rb_right; \
|
|
|
|
} \
|
|
|
|
ret; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define RB_FIRST(root, type, member) \
|
|
|
|
container_of_or_null(rb_first(root), type, member)
|
|
|
|
|
|
|
|
#define RB_LAST(root, type, member) \
|
|
|
|
container_of_or_null(rb_last(root), type, member)
|
|
|
|
|
|
|
|
#define RB_NEXT(ptr, member) \
|
|
|
|
container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
|
|
|
|
|
|
|
|
#define RB_PREV(ptr, member) \
|
|
|
|
container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
|
|
|
|
|
2018-08-22 12:57:15 +08:00
|
|
|
static inline uint64_t bch_crc64(const void *p, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t crc = 0xffffffffffffffffULL;
|
|
|
|
|
|
|
|
crc = crc64_be(crc, p, len);
|
|
|
|
return crc ^ 0xffffffffffffffffULL;
|
|
|
|
}
|
|
|
|
|
bcache: Clean up bch_get_congested()
There are a few nits in this function. They could in theory all
be separate patches, but that's probably taking small commits
too far.
1) I added a brief comment saying what it does.
2) I like to declare pointer parameters "const" where possible
for documentation reasons.
3) It uses bitmap_weight(&rand, BITS_PER_LONG) to compute the Hamming
weight of a 32-bit random number (giving a random integer with
mean 16 and variance 8). Passing by reference in a 64-bit variable
is silly; just use hweight32().
4) Its helper function fract_exp_two is unnecessarily tangled.
Gcc can optimize the multiply by (1 << x) to a shift, but it can
be written in a much more straightforward way at the cost of one
more bit of internal precision. Some analysis reveals that this
bit is always available.
This shrinks the object code for fract_exp_two(x, 6) from 23 bytes:
0000000000000000 <foo1>:
0: 89 f9 mov %edi,%ecx
2: c1 e9 06 shr $0x6,%ecx
5: b8 01 00 00 00 mov $0x1,%eax
a: d3 e0 shl %cl,%eax
c: 83 e7 3f and $0x3f,%edi
f: d3 e7 shl %cl,%edi
11: c1 ef 06 shr $0x6,%edi
14: 01 f8 add %edi,%eax
16: c3 retq
To 19:
0000000000000017 <foo2>:
17: 89 f8 mov %edi,%eax
19: 83 e0 3f and $0x3f,%eax
1c: 83 c0 40 add $0x40,%eax
1f: 89 f9 mov %edi,%ecx
21: c1 e9 06 shr $0x6,%ecx
24: d3 e0 shl %cl,%eax
26: c1 e8 06 shr $0x6,%eax
29: c3 retq
(Verified with 0 <= frac_bits <= 8, 0 <= x < 16<<frac_bits;
both versions produce the same output.)
5) And finally, the call to bch_get_congested() in check_should_bypass()
is separated from the use of the value by multiple tests which
could moot the need to compute it. Move the computation down to
where it's needed. This also saves a local register to hold the
computed value.
Signed-off-by: George Spelvin <lkml@sdf.org>
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-04-25 00:48:30 +08:00
|
|
|
/*
|
|
|
|
* A stepwise-linear pseudo-exponential. This returns 1 << (x >>
|
|
|
|
* frac_bits), with the less-significant bits filled in by linear
|
|
|
|
* interpolation.
|
|
|
|
*
|
|
|
|
* This can also be interpreted as a floating-point number format,
|
|
|
|
* where the low frac_bits are the mantissa (with implicit leading
|
|
|
|
* 1 bit), and the more significant bits are the exponent.
|
|
|
|
* The return value is 1.mantissa * 2^exponent.
|
|
|
|
*
|
|
|
|
* The way this is used, fract_bits is 6 and the largest possible
|
|
|
|
* input is CONGESTED_MAX-1 = 1023 (exponent 16, mantissa 0x1.fc),
|
|
|
|
* so the maximum output is 0x1fc00.
|
|
|
|
*/
|
2018-08-11 13:19:44 +08:00
|
|
|
static inline unsigned int fract_exp_two(unsigned int x,
|
|
|
|
unsigned int fract_bits)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
bcache: Clean up bch_get_congested()
There are a few nits in this function. They could in theory all
be separate patches, but that's probably taking small commits
too far.
1) I added a brief comment saying what it does.
2) I like to declare pointer parameters "const" where possible
for documentation reasons.
3) It uses bitmap_weight(&rand, BITS_PER_LONG) to compute the Hamming
weight of a 32-bit random number (giving a random integer with
mean 16 and variance 8). Passing by reference in a 64-bit variable
is silly; just use hweight32().
4) Its helper function fract_exp_two is unnecessarily tangled.
Gcc can optimize the multiply by (1 << x) to a shift, but it can
be written in a much more straightforward way at the cost of one
more bit of internal precision. Some analysis reveals that this
bit is always available.
This shrinks the object code for fract_exp_two(x, 6) from 23 bytes:
0000000000000000 <foo1>:
0: 89 f9 mov %edi,%ecx
2: c1 e9 06 shr $0x6,%ecx
5: b8 01 00 00 00 mov $0x1,%eax
a: d3 e0 shl %cl,%eax
c: 83 e7 3f and $0x3f,%edi
f: d3 e7 shl %cl,%edi
11: c1 ef 06 shr $0x6,%edi
14: 01 f8 add %edi,%eax
16: c3 retq
To 19:
0000000000000017 <foo2>:
17: 89 f8 mov %edi,%eax
19: 83 e0 3f and $0x3f,%eax
1c: 83 c0 40 add $0x40,%eax
1f: 89 f9 mov %edi,%ecx
21: c1 e9 06 shr $0x6,%ecx
24: d3 e0 shl %cl,%eax
26: c1 e8 06 shr $0x6,%eax
29: c3 retq
(Verified with 0 <= frac_bits <= 8, 0 <= x < 16<<frac_bits;
both versions produce the same output.)
5) And finally, the call to bch_get_congested() in check_should_bypass()
is separated from the use of the value by multiple tests which
could moot the need to compute it. Move the computation down to
where it's needed. This also saves a local register to hold the
computed value.
Signed-off-by: George Spelvin <lkml@sdf.org>
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-04-25 00:48:30 +08:00
|
|
|
unsigned int mantissa = 1 << fract_bits; /* Implicit bit */
|
2013-03-24 07:11:31 +08:00
|
|
|
|
bcache: Clean up bch_get_congested()
There are a few nits in this function. They could in theory all
be separate patches, but that's probably taking small commits
too far.
1) I added a brief comment saying what it does.
2) I like to declare pointer parameters "const" where possible
for documentation reasons.
3) It uses bitmap_weight(&rand, BITS_PER_LONG) to compute the Hamming
weight of a 32-bit random number (giving a random integer with
mean 16 and variance 8). Passing by reference in a 64-bit variable
is silly; just use hweight32().
4) Its helper function fract_exp_two is unnecessarily tangled.
Gcc can optimize the multiply by (1 << x) to a shift, but it can
be written in a much more straightforward way at the cost of one
more bit of internal precision. Some analysis reveals that this
bit is always available.
This shrinks the object code for fract_exp_two(x, 6) from 23 bytes:
0000000000000000 <foo1>:
0: 89 f9 mov %edi,%ecx
2: c1 e9 06 shr $0x6,%ecx
5: b8 01 00 00 00 mov $0x1,%eax
a: d3 e0 shl %cl,%eax
c: 83 e7 3f and $0x3f,%edi
f: d3 e7 shl %cl,%edi
11: c1 ef 06 shr $0x6,%edi
14: 01 f8 add %edi,%eax
16: c3 retq
To 19:
0000000000000017 <foo2>:
17: 89 f8 mov %edi,%eax
19: 83 e0 3f and $0x3f,%eax
1c: 83 c0 40 add $0x40,%eax
1f: 89 f9 mov %edi,%ecx
21: c1 e9 06 shr $0x6,%ecx
24: d3 e0 shl %cl,%eax
26: c1 e8 06 shr $0x6,%eax
29: c3 retq
(Verified with 0 <= frac_bits <= 8, 0 <= x < 16<<frac_bits;
both versions produce the same output.)
5) And finally, the call to bch_get_congested() in check_should_bypass()
is separated from the use of the value by multiple tests which
could moot the need to compute it. Move the computation down to
where it's needed. This also saves a local register to hold the
computed value.
Signed-off-by: George Spelvin <lkml@sdf.org>
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-04-25 00:48:30 +08:00
|
|
|
mantissa += x & (mantissa - 1);
|
|
|
|
x >>= fract_bits; /* The exponent */
|
|
|
|
/* Largest intermediate value 0x7f0000 */
|
|
|
|
return mantissa << x >> fract_bits;
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2013-03-29 02:50:55 +08:00
|
|
|
void bch_bio_map(struct bio *bio, void *base);
|
2017-12-18 20:22:10 +08:00
|
|
|
int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
#endif /* _BCACHE_UTIL_H */
|