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
|
|
|
/*
|
|
|
|
* Primary bucket allocation code
|
|
|
|
*
|
|
|
|
* Copyright 2012 Google, Inc.
|
|
|
|
*
|
|
|
|
* Allocation in bcache is done in terms of buckets:
|
|
|
|
*
|
|
|
|
* Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
|
|
|
|
* btree pointers - they must match for the pointer to be considered valid.
|
|
|
|
*
|
|
|
|
* Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
|
|
|
|
* bucket simply by incrementing its gen.
|
|
|
|
*
|
|
|
|
* The gens (along with the priorities; it's really the gens are important but
|
|
|
|
* the code is named as if it's the priorities) are written in an arbitrary list
|
|
|
|
* of buckets on disk, with a pointer to them in the journal header.
|
|
|
|
*
|
|
|
|
* When we invalidate a bucket, we have to write its new gen to disk and wait
|
|
|
|
* for that write to complete before we use it - otherwise after a crash we
|
|
|
|
* could have pointers that appeared to be good but pointed to data that had
|
|
|
|
* been overwritten.
|
|
|
|
*
|
|
|
|
* Since the gens and priorities are all stored contiguously on disk, we can
|
|
|
|
* batch this up: We fill up the free_inc list with freshly invalidated buckets,
|
|
|
|
* call prio_write(), and when prio_write() finishes we pull buckets off the
|
|
|
|
* free_inc list and optionally discard them.
|
|
|
|
*
|
|
|
|
* free_inc isn't the only freelist - if it was, we'd often to sleep while
|
|
|
|
* priorities and gens were being written before we could allocate. c->free is a
|
|
|
|
* smaller freelist, and buckets on that list are always ready to be used.
|
|
|
|
*
|
|
|
|
* If we've got discards enabled, that happens when a bucket moves from the
|
|
|
|
* free_inc list to the free list.
|
|
|
|
*
|
|
|
|
* There is another freelist, because sometimes we have buckets that we know
|
|
|
|
* have nothing pointing into them - these we can reuse without waiting for
|
|
|
|
* priorities to be rewritten. These come from freed btree nodes and buckets
|
|
|
|
* that garbage collection discovered no longer had valid keys pointing into
|
|
|
|
* them (because they were overwritten). That's the unused list - buckets on the
|
|
|
|
* unused list move to the free list, optionally being discarded in the process.
|
|
|
|
*
|
|
|
|
* It's also important to ensure that gens don't wrap around - with respect to
|
|
|
|
* either the oldest gen in the btree or the gen on disk. This is quite
|
|
|
|
* difficult to do in practice, but we explicitly guard against it anyways - if
|
|
|
|
* a bucket is in danger of wrapping around we simply skip invalidating it that
|
|
|
|
* time around, and we garbage collect or rewrite the priorities sooner than we
|
|
|
|
* would have otherwise.
|
|
|
|
*
|
|
|
|
* bch_bucket_alloc() allocates a single bucket from a specific cache.
|
|
|
|
*
|
|
|
|
* bch_bucket_alloc_set() allocates one or more buckets from different caches
|
|
|
|
* out of a cache set.
|
|
|
|
*
|
|
|
|
* free_some_buckets() drives all the processes described above. It's called
|
|
|
|
* from bch_bucket_alloc() and a few other places that need to make sure free
|
|
|
|
* buckets are ready.
|
|
|
|
*
|
|
|
|
* invalidate_buckets_(lru|fifo)() find buckets that are available to be
|
|
|
|
* invalidated, and then invalidate them and stick them on the free_inc list -
|
|
|
|
* in either lru or fifo order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bcache.h"
|
|
|
|
#include "btree.h"
|
|
|
|
|
2013-07-25 08:16:09 +08:00
|
|
|
#include <linux/blkdev.h>
|
2013-04-25 10:01:12 +08:00
|
|
|
#include <linux/kthread.h>
|
2013-03-24 07:11:31 +08:00
|
|
|
#include <linux/random.h>
|
2013-04-27 06:39:55 +08:00
|
|
|
#include <trace/events/bcache.h>
|
2013-03-24 07:11:31 +08:00
|
|
|
|
bcache: increase the number of open buckets
In currently, we only alloc 6 open buckets for each cache set,
but in usually, we always attach about 10 or so backend devices for
each cache set, and the each bcache device are always accessed by
about 10 or so threads in top application layer. So 6 open buckets
are too few, It has led to that each of the same thread write data
to different buckets, which would cause low efficiency write-back,
and also cause buckets inefficient, and would be Very easy to run
out of.
I add debug message in bch_open_buckets_alloc() to print alloc bucket
info, and test with ten bcache devices with a cache set, and each
bcache device is accessed by ten threads.
From the debug message, we can see that, after the modification, One
bucket is more likely to assign to the same thread, and the data from
the same thread are more likely to write the same bucket. Usually the
same thread always write/read the same backend device, so it is good
for write-back and also promote the usage efficiency of buckets.
Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
Reviewed-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2017-09-06 14:25:58 +08:00
|
|
|
#define MAX_OPEN_BUCKETS 128
|
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
/* Bucket heap / gen */
|
|
|
|
|
|
|
|
uint8_t bch_inc_gen(struct cache *ca, struct bucket *b)
|
|
|
|
{
|
|
|
|
uint8_t ret = ++b->gen;
|
|
|
|
|
|
|
|
ca->set->need_gc = max(ca->set->need_gc, bucket_gc_gen(b));
|
|
|
|
WARN_ON_ONCE(ca->set->need_gc > BUCKET_GC_GEN_MAX);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch_rescale_priorities(struct cache_set *c, int sectors)
|
|
|
|
{
|
|
|
|
struct cache *ca;
|
|
|
|
struct bucket *b;
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int next = c->nbuckets * c->sb.bucket_size / 1024;
|
|
|
|
unsigned int i;
|
2013-03-24 07:11:31 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
atomic_sub(sectors, &c->rescale);
|
|
|
|
|
|
|
|
do {
|
|
|
|
r = atomic_read(&c->rescale);
|
|
|
|
|
|
|
|
if (r >= 0)
|
|
|
|
return;
|
|
|
|
} while (atomic_cmpxchg(&c->rescale, r, r + next) != r);
|
|
|
|
|
|
|
|
mutex_lock(&c->bucket_lock);
|
|
|
|
|
|
|
|
c->min_prio = USHRT_MAX;
|
|
|
|
|
|
|
|
for_each_cache(ca, c, i)
|
|
|
|
for_each_bucket(b, ca)
|
|
|
|
if (b->prio &&
|
|
|
|
b->prio != BTREE_PRIO &&
|
|
|
|
!atomic_read(&b->pin)) {
|
|
|
|
b->prio--;
|
|
|
|
c->min_prio = min(c->min_prio, b->prio);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&c->bucket_lock);
|
|
|
|
}
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
/*
|
|
|
|
* Background allocation thread: scans for buckets to be invalidated,
|
|
|
|
* invalidates them, rewrites prios/gens (marking them as invalidated on disk),
|
|
|
|
* then optionally issues discard commands to the newly free buckets, then puts
|
|
|
|
* them on the various freelists.
|
|
|
|
*/
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
static inline bool can_inc_bucket_gen(struct bucket *b)
|
|
|
|
{
|
2014-03-18 07:55:55 +08:00
|
|
|
return bucket_gc_gen(b) < BUCKET_GC_GEN_MAX;
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
2014-03-18 07:55:55 +08:00
|
|
|
BUG_ON(!ca->set->gc_mark_valid);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2014-03-14 04:46:29 +08:00
|
|
|
return (!GC_MARK(b) ||
|
|
|
|
GC_MARK(b) == GC_MARK_RECLAIMABLE) &&
|
2013-03-24 07:11:31 +08:00
|
|
|
!atomic_read(&b->pin) &&
|
|
|
|
can_inc_bucket_gen(b);
|
|
|
|
}
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
2014-03-18 07:55:55 +08:00
|
|
|
lockdep_assert_held(&ca->set->bucket_lock);
|
|
|
|
BUG_ON(GC_MARK(b) && GC_MARK(b) != GC_MARK_RECLAIMABLE);
|
2014-02-13 10:43:32 +08:00
|
|
|
|
|
|
|
if (GC_SECTORS_USED(b))
|
2014-03-18 07:55:55 +08:00
|
|
|
trace_bcache_invalidate(ca, b - ca->buckets);
|
2014-02-13 10:43:32 +08:00
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
bch_inc_gen(ca, b);
|
|
|
|
b->prio = INITIAL_PRIO;
|
|
|
|
atomic_inc(&b->pin);
|
2014-03-18 07:55:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bch_invalidate_one_bucket(struct cache *ca, struct bucket *b)
|
|
|
|
{
|
|
|
|
__bch_invalidate_one_bucket(ca, b);
|
|
|
|
|
|
|
|
fifo_push(&ca->free_inc, b - ca->buckets);
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2013-11-13 05:49:10 +08:00
|
|
|
/*
|
|
|
|
* Determines what order we're going to reuse buckets, smallest bucket_prio()
|
|
|
|
* first: we also take into account the number of sectors of live data in that
|
|
|
|
* bucket, and in order for that multiply to make sense we have to scale bucket
|
|
|
|
*
|
|
|
|
* Thus, we scale the bucket priorities so that the bucket with the smallest
|
|
|
|
* prio is worth 1/8th of what INITIAL_PRIO is worth.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define bucket_prio(b) \
|
|
|
|
({ \
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
|
2013-11-13 05:49:10 +08:00
|
|
|
\
|
|
|
|
(b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \
|
|
|
|
})
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-03-26 02:46:44 +08:00
|
|
|
#define bucket_max_cmp(l, r) (bucket_prio(l) < bucket_prio(r))
|
|
|
|
#define bucket_min_cmp(l, r) (bucket_prio(l) > bucket_prio(r))
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-03-26 02:46:44 +08:00
|
|
|
static void invalidate_buckets_lru(struct cache *ca)
|
|
|
|
{
|
2013-03-24 07:11:31 +08:00
|
|
|
struct bucket *b;
|
|
|
|
ssize_t i;
|
|
|
|
|
|
|
|
ca->heap.used = 0;
|
|
|
|
|
|
|
|
for_each_bucket(b, ca) {
|
2014-03-18 07:55:55 +08:00
|
|
|
if (!bch_can_invalidate_bucket(ca, b))
|
2013-05-01 10:14:40 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!heap_full(&ca->heap))
|
|
|
|
heap_add(&ca->heap, b, bucket_max_cmp);
|
|
|
|
else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
|
|
|
|
ca->heap.data[0] = b;
|
|
|
|
heap_sift(&ca->heap, 0, bucket_max_cmp);
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = ca->heap.used / 2 - 1; i >= 0; --i)
|
|
|
|
heap_sift(&ca->heap, i, bucket_min_cmp);
|
|
|
|
|
|
|
|
while (!fifo_full(&ca->free_inc)) {
|
|
|
|
if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
|
2013-05-01 10:14:40 +08:00
|
|
|
/*
|
|
|
|
* We don't want to be calling invalidate_buckets()
|
2013-03-24 07:11:31 +08:00
|
|
|
* multiple times when it can't do anything
|
|
|
|
*/
|
|
|
|
ca->invalidate_needs_gc = 1;
|
2013-10-25 08:19:26 +08:00
|
|
|
wake_up_gc(ca->set);
|
2013-03-24 07:11:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
bch_invalidate_one_bucket(ca, b);
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void invalidate_buckets_fifo(struct cache *ca)
|
|
|
|
{
|
|
|
|
struct bucket *b;
|
|
|
|
size_t checked = 0;
|
|
|
|
|
|
|
|
while (!fifo_full(&ca->free_inc)) {
|
|
|
|
if (ca->fifo_last_bucket < ca->sb.first_bucket ||
|
|
|
|
ca->fifo_last_bucket >= ca->sb.nbuckets)
|
|
|
|
ca->fifo_last_bucket = ca->sb.first_bucket;
|
|
|
|
|
|
|
|
b = ca->buckets + ca->fifo_last_bucket++;
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
if (bch_can_invalidate_bucket(ca, b))
|
|
|
|
bch_invalidate_one_bucket(ca, b);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
if (++checked >= ca->sb.nbuckets) {
|
|
|
|
ca->invalidate_needs_gc = 1;
|
2013-10-25 08:19:26 +08:00
|
|
|
wake_up_gc(ca->set);
|
2013-03-24 07:11:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void invalidate_buckets_random(struct cache *ca)
|
|
|
|
{
|
|
|
|
struct bucket *b;
|
|
|
|
size_t checked = 0;
|
|
|
|
|
|
|
|
while (!fifo_full(&ca->free_inc)) {
|
|
|
|
size_t n;
|
2018-08-11 13:19:45 +08:00
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
get_random_bytes(&n, sizeof(n));
|
|
|
|
|
|
|
|
n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
|
|
|
|
n += ca->sb.first_bucket;
|
|
|
|
|
|
|
|
b = ca->buckets + n;
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
if (bch_can_invalidate_bucket(ca, b))
|
|
|
|
bch_invalidate_one_bucket(ca, b);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
if (++checked >= ca->sb.nbuckets / 2) {
|
|
|
|
ca->invalidate_needs_gc = 1;
|
2013-10-25 08:19:26 +08:00
|
|
|
wake_up_gc(ca->set);
|
2013-03-24 07:11:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void invalidate_buckets(struct cache *ca)
|
|
|
|
{
|
2014-03-18 07:55:55 +08:00
|
|
|
BUG_ON(ca->invalidate_needs_gc);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
switch (CACHE_REPLACEMENT(&ca->sb)) {
|
|
|
|
case CACHE_REPLACEMENT_LRU:
|
|
|
|
invalidate_buckets_lru(ca);
|
|
|
|
break;
|
|
|
|
case CACHE_REPLACEMENT_FIFO:
|
|
|
|
invalidate_buckets_fifo(ca);
|
|
|
|
break;
|
|
|
|
case CACHE_REPLACEMENT_RANDOM:
|
|
|
|
invalidate_buckets_random(ca);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define allocator_wait(ca, cond) \
|
|
|
|
do { \
|
2013-05-01 10:14:40 +08:00
|
|
|
while (1) { \
|
2013-04-25 10:01:12 +08:00
|
|
|
set_current_state(TASK_INTERRUPTIBLE); \
|
2013-05-01 10:14:40 +08:00
|
|
|
if (cond) \
|
|
|
|
break; \
|
2013-03-24 07:11:31 +08:00
|
|
|
\
|
|
|
|
mutex_unlock(&(ca)->set->bucket_lock); \
|
bcache: add CACHE_SET_IO_DISABLE to struct cache_set flags
When too many I/Os failed on cache device, bch_cache_set_error() is called
in the error handling code path to retire whole problematic cache set. If
new I/O requests continue to come and take refcount dc->count, the cache
set won't be retired immediately, this is a problem.
Further more, there are several kernel thread and self-armed kernel work
may still running after bch_cache_set_error() is called. It needs to wait
quite a while for them to stop, or they won't stop at all. They also
prevent the cache set from being retired.
The solution in this patch is, to add per cache set flag to disable I/O
request on this cache and all attached backing devices. Then new coming I/O
requests can be rejected in *_make_request() before taking refcount, kernel
threads and self-armed kernel worker can stop very fast when flags bit
CACHE_SET_IO_DISABLE is set.
Because bcache also do internal I/Os for writeback, garbage collection,
bucket allocation, journaling, this kind of I/O should be disabled after
bch_cache_set_error() is called. So closure_bio_submit() is modified to
check whether CACHE_SET_IO_DISABLE is set on cache_set->flags. If set,
closure_bio_submit() will set bio->bi_status to BLK_STS_IOERR and
return, generic_make_request() won't be called.
A sysfs interface is also added to set or clear CACHE_SET_IO_DISABLE bit
from cache_set->flags, to disable or enable cache set I/O for debugging. It
is helpful to trigger more corner case issues for failed cache device.
Changelog
v4, add wait_for_kthread_stop(), and call it before exits writeback and gc
kernel threads.
v3, change CACHE_SET_IO_DISABLE from 4 to 3, since it is bit index.
remove "bcache: " prefix when printing out kernel message.
v2, more changes by previous review,
- Use CACHE_SET_IO_DISABLE of cache_set->flags, suggested by Junhui.
- Check CACHE_SET_IO_DISABLE in bch_btree_gc() to stop a while-loop, this
is reported and inspired from origal patch of Pavel Vazharov.
v1, initial version.
Signed-off-by: Coly Li <colyli@suse.de>
Reviewed-by: Hannes Reinecke <hare@suse.com>
Reviewed-by: Michael Lyle <mlyle@lyle.org>
Cc: Junhui Tang <tang.junhui@zte.com.cn>
Cc: Michael Lyle <mlyle@lyle.org>
Cc: Pavel Vazharov <freakpv@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-03-19 08:36:17 +08:00
|
|
|
if (kthread_should_stop() || \
|
|
|
|
test_bit(CACHE_SET_IO_DISABLE, &ca->set->flags)) { \
|
bcache: properly set task state in bch_writeback_thread()
Kernel thread routine bch_writeback_thread() has the following code block,
447 down_write(&dc->writeback_lock);
448~450 if (check conditions) {
451 up_write(&dc->writeback_lock);
452 set_current_state(TASK_INTERRUPTIBLE);
453
454 if (kthread_should_stop())
455 return 0;
456
457 schedule();
458 continue;
459 }
If condition check is true, its task state is set to TASK_INTERRUPTIBLE
and call schedule() to wait for others to wake up it.
There are 2 issues in current code,
1, Task state is set to TASK_INTERRUPTIBLE after the condition checks, if
another process changes the condition and call wake_up_process(dc->
writeback_thread), then at line 452 task state is set back to
TASK_INTERRUPTIBLE, the writeback kernel thread will lose a chance to be
waken up.
2, At line 454 if kthread_should_stop() is true, writeback kernel thread
will return to kernel/kthread.c:kthread() with TASK_INTERRUPTIBLE and
call do_exit(). It is not good to enter do_exit() with task state
TASK_INTERRUPTIBLE, in following code path might_sleep() is called and a
warning message is reported by __might_sleep(): "WARNING: do not call
blocking ops when !TASK_RUNNING; state=1 set at [xxxx]".
For the first issue, task state should be set before condition checks.
Ineed because dc->writeback_lock is required when modifying all the
conditions, calling set_current_state() inside code block where dc->
writeback_lock is hold is safe. But this is quite implicit, so I still move
set_current_state() before all the condition checks.
For the second issue, frankley speaking it does not hurt when kernel thread
exits with TASK_INTERRUPTIBLE state, but this warning message scares users,
makes them feel there might be something risky with bcache and hurt their
data. Setting task state to TASK_RUNNING before returning fixes this
problem.
In alloc.c:allocator_wait(), there is also a similar issue, and is also
fixed in this patch.
Changelog:
v3: merge two similar fixes into one patch
v2: fix the race issue in v1 patch.
v1: initial buggy fix.
Signed-off-by: Coly Li <colyli@suse.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Michael Lyle <mlyle@lyle.org>
Cc: Michael Lyle <mlyle@lyle.org>
Cc: Junhui Tang <tang.junhui@zte.com.cn>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-02-08 03:41:41 +08:00
|
|
|
set_current_state(TASK_RUNNING); \
|
2018-05-03 18:51:35 +08:00
|
|
|
goto out; \
|
bcache: properly set task state in bch_writeback_thread()
Kernel thread routine bch_writeback_thread() has the following code block,
447 down_write(&dc->writeback_lock);
448~450 if (check conditions) {
451 up_write(&dc->writeback_lock);
452 set_current_state(TASK_INTERRUPTIBLE);
453
454 if (kthread_should_stop())
455 return 0;
456
457 schedule();
458 continue;
459 }
If condition check is true, its task state is set to TASK_INTERRUPTIBLE
and call schedule() to wait for others to wake up it.
There are 2 issues in current code,
1, Task state is set to TASK_INTERRUPTIBLE after the condition checks, if
another process changes the condition and call wake_up_process(dc->
writeback_thread), then at line 452 task state is set back to
TASK_INTERRUPTIBLE, the writeback kernel thread will lose a chance to be
waken up.
2, At line 454 if kthread_should_stop() is true, writeback kernel thread
will return to kernel/kthread.c:kthread() with TASK_INTERRUPTIBLE and
call do_exit(). It is not good to enter do_exit() with task state
TASK_INTERRUPTIBLE, in following code path might_sleep() is called and a
warning message is reported by __might_sleep(): "WARNING: do not call
blocking ops when !TASK_RUNNING; state=1 set at [xxxx]".
For the first issue, task state should be set before condition checks.
Ineed because dc->writeback_lock is required when modifying all the
conditions, calling set_current_state() inside code block where dc->
writeback_lock is hold is safe. But this is quite implicit, so I still move
set_current_state() before all the condition checks.
For the second issue, frankley speaking it does not hurt when kernel thread
exits with TASK_INTERRUPTIBLE state, but this warning message scares users,
makes them feel there might be something risky with bcache and hurt their
data. Setting task state to TASK_RUNNING before returning fixes this
problem.
In alloc.c:allocator_wait(), there is also a similar issue, and is also
fixed in this patch.
Changelog:
v3: merge two similar fixes into one patch
v2: fix the race issue in v1 patch.
v1: initial buggy fix.
Signed-off-by: Coly Li <colyli@suse.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Reviewed-by: Michael Lyle <mlyle@lyle.org>
Cc: Michael Lyle <mlyle@lyle.org>
Cc: Junhui Tang <tang.junhui@zte.com.cn>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-02-08 03:41:41 +08:00
|
|
|
} \
|
2013-03-24 07:11:31 +08:00
|
|
|
\
|
|
|
|
schedule(); \
|
|
|
|
mutex_lock(&(ca)->set->bucket_lock); \
|
|
|
|
} \
|
2013-04-25 10:01:12 +08:00
|
|
|
__set_current_state(TASK_RUNNING); \
|
2013-03-24 07:11:31 +08:00
|
|
|
} while (0)
|
|
|
|
|
2013-12-17 17:29:34 +08:00
|
|
|
static int bch_allocator_push(struct cache *ca, long bucket)
|
|
|
|
{
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int i;
|
2013-12-17 17:29:34 +08:00
|
|
|
|
|
|
|
/* Prios/gens are actually the most important reserve */
|
|
|
|
if (fifo_push(&ca->free[RESERVE_PRIO], bucket))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (i = 0; i < RESERVE_NR; i++)
|
|
|
|
if (fifo_push(&ca->free[i], bucket))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-25 10:01:12 +08:00
|
|
|
static int bch_allocator_thread(void *arg)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
2013-04-25 10:01:12 +08:00
|
|
|
struct cache *ca = arg;
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
mutex_lock(&ca->set->bucket_lock);
|
|
|
|
|
|
|
|
while (1) {
|
2013-05-01 10:14:40 +08:00
|
|
|
/*
|
|
|
|
* First, we pull buckets off of the unused and free_inc lists,
|
|
|
|
* possibly issue discards to them, then we add the bucket to
|
|
|
|
* the free list:
|
|
|
|
*/
|
bcache: avoid clang -Wunintialized warning
clang has identified a code path in which it thinks a
variable may be unused:
drivers/md/bcache/alloc.c:333:4: error: variable 'bucket' is used uninitialized whenever 'if' condition is false
[-Werror,-Wsometimes-uninitialized]
fifo_pop(&ca->free_inc, bucket);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/md/bcache/util.h:219:27: note: expanded from macro 'fifo_pop'
#define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/md/bcache/util.h:189:6: note: expanded from macro 'fifo_pop_front'
if (_r) { \
^~
drivers/md/bcache/alloc.c:343:46: note: uninitialized use occurs here
allocator_wait(ca, bch_allocator_push(ca, bucket));
^~~~~~
drivers/md/bcache/alloc.c:287:7: note: expanded from macro 'allocator_wait'
if (cond) \
^~~~
drivers/md/bcache/alloc.c:333:4: note: remove the 'if' if its condition is always true
fifo_pop(&ca->free_inc, bucket);
^
drivers/md/bcache/util.h:219:27: note: expanded from macro 'fifo_pop'
#define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
^
drivers/md/bcache/util.h:189:2: note: expanded from macro 'fifo_pop_front'
if (_r) { \
^
drivers/md/bcache/alloc.c:331:15: note: initialize the variable 'bucket' to silence this warning
long bucket;
^
This cannot happen in practice because we only enter the loop
if there is at least one element in the list.
Slightly rearranging the code makes this clearer to both the
reader and the compiler, which avoids the warning.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-04-25 00:48:28 +08:00
|
|
|
while (1) {
|
2013-03-24 07:11:31 +08:00
|
|
|
long bucket;
|
|
|
|
|
bcache: avoid clang -Wunintialized warning
clang has identified a code path in which it thinks a
variable may be unused:
drivers/md/bcache/alloc.c:333:4: error: variable 'bucket' is used uninitialized whenever 'if' condition is false
[-Werror,-Wsometimes-uninitialized]
fifo_pop(&ca->free_inc, bucket);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/md/bcache/util.h:219:27: note: expanded from macro 'fifo_pop'
#define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/md/bcache/util.h:189:6: note: expanded from macro 'fifo_pop_front'
if (_r) { \
^~
drivers/md/bcache/alloc.c:343:46: note: uninitialized use occurs here
allocator_wait(ca, bch_allocator_push(ca, bucket));
^~~~~~
drivers/md/bcache/alloc.c:287:7: note: expanded from macro 'allocator_wait'
if (cond) \
^~~~
drivers/md/bcache/alloc.c:333:4: note: remove the 'if' if its condition is always true
fifo_pop(&ca->free_inc, bucket);
^
drivers/md/bcache/util.h:219:27: note: expanded from macro 'fifo_pop'
#define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
^
drivers/md/bcache/util.h:189:2: note: expanded from macro 'fifo_pop_front'
if (_r) { \
^
drivers/md/bcache/alloc.c:331:15: note: initialize the variable 'bucket' to silence this warning
long bucket;
^
This cannot happen in practice because we only enter the loop
if there is at least one element in the list.
Slightly rearranging the code makes this clearer to both the
reader and the compiler, which avoids the warning.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Nathan Chancellor <natechancellor@gmail.com>
Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-04-25 00:48:28 +08:00
|
|
|
if (!fifo_pop(&ca->free_inc, bucket))
|
|
|
|
break;
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
if (ca->discard) {
|
2013-07-25 08:16:09 +08:00
|
|
|
mutex_unlock(&ca->set->bucket_lock);
|
|
|
|
blkdev_issue_discard(ca->bdev,
|
|
|
|
bucket_to_sector(ca->set, bucket),
|
2014-04-22 09:22:35 +08:00
|
|
|
ca->sb.bucket_size, GFP_KERNEL, 0);
|
2013-07-25 08:16:09 +08:00
|
|
|
mutex_lock(&ca->set->bucket_lock);
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
2013-07-25 08:16:09 +08:00
|
|
|
|
2013-12-17 17:29:34 +08:00
|
|
|
allocator_wait(ca, bch_allocator_push(ca, bucket));
|
2014-03-18 08:15:53 +08:00
|
|
|
wake_up(&ca->set->btree_cache_wait);
|
2013-07-25 08:29:09 +08:00
|
|
|
wake_up(&ca->set->bucket_wait);
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2013-05-01 10:14:40 +08:00
|
|
|
/*
|
|
|
|
* We've run out of free buckets, we need to find some buckets
|
|
|
|
* we can invalidate. First, invalidate them in memory and add
|
|
|
|
* them to the free_inc list:
|
|
|
|
*/
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
retry_invalidate:
|
2013-05-01 10:14:40 +08:00
|
|
|
allocator_wait(ca, ca->set->gc_mark_valid &&
|
2014-03-18 07:55:55 +08:00
|
|
|
!ca->invalidate_needs_gc);
|
2013-05-01 10:14:40 +08:00
|
|
|
invalidate_buckets(ca);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-05-01 10:14:40 +08:00
|
|
|
/*
|
|
|
|
* Now, we write their new gens to disk so we can start writing
|
|
|
|
* new stuff to them:
|
|
|
|
*/
|
|
|
|
allocator_wait(ca, !atomic_read(&ca->set->prio_blocked));
|
2014-03-18 07:55:55 +08:00
|
|
|
if (CACHE_SYNC(&ca->set->sb)) {
|
|
|
|
/*
|
|
|
|
* This could deadlock if an allocation with a btree
|
|
|
|
* node locked ever blocked - having the btree node
|
|
|
|
* locked would block garbage collection, but here we're
|
|
|
|
* waiting on garbage collection before we invalidate
|
|
|
|
* and free anything.
|
|
|
|
*
|
|
|
|
* But this should be safe since the btree code always
|
|
|
|
* uses btree_check_reserve() before allocating now, and
|
|
|
|
* if it fails it blocks without btree nodes locked.
|
|
|
|
*/
|
|
|
|
if (!fifo_full(&ca->free_inc))
|
|
|
|
goto retry_invalidate;
|
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
bch_prio_write(ca);
|
2014-03-18 07:55:55 +08:00
|
|
|
}
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
2018-05-03 18:51:35 +08:00
|
|
|
out:
|
|
|
|
wait_for_kthread_stop();
|
|
|
|
return 0;
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
/* Allocation */
|
|
|
|
|
2018-08-11 13:19:44 +08:00
|
|
|
long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
2013-07-25 08:29:09 +08:00
|
|
|
DEFINE_WAIT(w);
|
|
|
|
struct bucket *b;
|
|
|
|
long r;
|
|
|
|
|
2019-06-28 19:59:35 +08:00
|
|
|
|
|
|
|
/* No allocation if CACHE_SET_IO_DISABLE bit is set */
|
|
|
|
if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &ca->set->flags)))
|
|
|
|
return -1;
|
|
|
|
|
2013-07-25 08:29:09 +08:00
|
|
|
/* fastpath */
|
2013-12-17 17:29:34 +08:00
|
|
|
if (fifo_pop(&ca->free[RESERVE_NONE], r) ||
|
|
|
|
fifo_pop(&ca->free[reserve], r))
|
2013-07-25 08:29:09 +08:00
|
|
|
goto out;
|
|
|
|
|
2014-02-13 10:43:32 +08:00
|
|
|
if (!wait) {
|
|
|
|
trace_bcache_alloc_fail(ca, reserve);
|
2013-07-25 08:29:09 +08:00
|
|
|
return -1;
|
2014-02-13 10:43:32 +08:00
|
|
|
}
|
2013-07-25 08:29:09 +08:00
|
|
|
|
2013-12-17 17:29:34 +08:00
|
|
|
do {
|
2013-07-25 08:29:09 +08:00
|
|
|
prepare_to_wait(&ca->set->bucket_wait, &w,
|
|
|
|
TASK_UNINTERRUPTIBLE);
|
|
|
|
|
|
|
|
mutex_unlock(&ca->set->bucket_lock);
|
|
|
|
schedule();
|
|
|
|
mutex_lock(&ca->set->bucket_lock);
|
2013-12-17 17:29:34 +08:00
|
|
|
} while (!fifo_pop(&ca->free[RESERVE_NONE], r) &&
|
|
|
|
!fifo_pop(&ca->free[reserve], r));
|
2013-07-25 08:29:09 +08:00
|
|
|
|
|
|
|
finish_wait(&ca->set->bucket_wait, &w);
|
|
|
|
out:
|
2017-10-14 07:35:29 +08:00
|
|
|
if (ca->alloc_thread)
|
|
|
|
wake_up_process(ca->alloc_thread);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2014-02-13 10:43:32 +08:00
|
|
|
trace_bcache_alloc(ca, reserve);
|
|
|
|
|
2013-10-25 07:36:03 +08:00
|
|
|
if (expensive_debug_checks(ca->set)) {
|
2013-03-24 07:11:31 +08:00
|
|
|
size_t iter;
|
|
|
|
long i;
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int j;
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
|
|
|
|
BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
|
|
|
|
|
2013-12-17 17:29:34 +08:00
|
|
|
for (j = 0; j < RESERVE_NR; j++)
|
|
|
|
fifo_for_each(i, &ca->free[j], iter)
|
|
|
|
BUG_ON(i == r);
|
2013-03-24 07:11:31 +08:00
|
|
|
fifo_for_each(i, &ca->free_inc, iter)
|
|
|
|
BUG_ON(i == r);
|
|
|
|
}
|
2013-10-25 07:36:03 +08:00
|
|
|
|
2013-07-25 08:29:09 +08:00
|
|
|
b = ca->buckets + r;
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-07-25 08:29:09 +08:00
|
|
|
BUG_ON(atomic_read(&b->pin) != 1);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-07-25 08:29:09 +08:00
|
|
|
SET_GC_SECTORS_USED(b, ca->sb.bucket_size);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2013-12-17 17:29:34 +08:00
|
|
|
if (reserve <= RESERVE_PRIO) {
|
2013-07-25 08:29:09 +08:00
|
|
|
SET_GC_MARK(b, GC_MARK_METADATA);
|
2013-11-08 09:53:19 +08:00
|
|
|
SET_GC_MOVE(b, 0);
|
2013-07-25 08:29:09 +08:00
|
|
|
b->prio = BTREE_PRIO;
|
|
|
|
} else {
|
|
|
|
SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
|
2013-11-08 09:53:19 +08:00
|
|
|
SET_GC_MOVE(b, 0);
|
2013-07-25 08:29:09 +08:00
|
|
|
b->prio = INITIAL_PRIO;
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2017-10-31 05:46:33 +08:00
|
|
|
if (ca->set->avail_nbuckets > 0) {
|
|
|
|
ca->set->avail_nbuckets--;
|
|
|
|
bch_update_bucket_in_use(ca->set, &ca->set->gc_stats);
|
|
|
|
}
|
|
|
|
|
2013-07-25 08:29:09 +08:00
|
|
|
return r;
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
void __bch_bucket_free(struct cache *ca, struct bucket *b)
|
|
|
|
{
|
|
|
|
SET_GC_MARK(b, 0);
|
|
|
|
SET_GC_SECTORS_USED(b, 0);
|
2017-10-31 05:46:33 +08:00
|
|
|
|
|
|
|
if (ca->set->avail_nbuckets < ca->set->nbuckets) {
|
|
|
|
ca->set->avail_nbuckets++;
|
|
|
|
bch_update_bucket_in_use(ca->set, &ca->set->gc_stats);
|
|
|
|
}
|
2014-03-18 07:55:55 +08:00
|
|
|
}
|
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
void bch_bucket_free(struct cache_set *c, struct bkey *k)
|
|
|
|
{
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int i;
|
2013-03-24 07:11:31 +08:00
|
|
|
|
2014-03-18 07:55:55 +08:00
|
|
|
for (i = 0; i < KEY_PTRS(k); i++)
|
|
|
|
__bch_bucket_free(PTR_CACHE(c, k, i),
|
|
|
|
PTR_BUCKET(c, k, i));
|
2013-03-24 07:11:31 +08:00
|
|
|
}
|
|
|
|
|
2018-08-11 13:19:44 +08:00
|
|
|
int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
|
2013-07-25 08:29:09 +08:00
|
|
|
struct bkey *k, int n, bool wait)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-06-28 19:59:35 +08:00
|
|
|
/* No allocation if CACHE_SET_IO_DISABLE bit is set */
|
|
|
|
if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags)))
|
|
|
|
return -1;
|
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
lockdep_assert_held(&c->bucket_lock);
|
2018-10-08 20:41:19 +08:00
|
|
|
BUG_ON(!n || n > c->caches_loaded || n > MAX_CACHES_PER_SET);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
bkey_init(k);
|
|
|
|
|
|
|
|
/* sort by free space/prio of oldest data in caches */
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct cache *ca = c->cache_by_alloc[i];
|
2013-12-17 17:29:34 +08:00
|
|
|
long b = bch_bucket_alloc(ca, reserve, wait);
|
2013-03-24 07:11:31 +08:00
|
|
|
|
|
|
|
if (b == -1)
|
|
|
|
goto err;
|
|
|
|
|
2017-11-25 07:14:25 +08:00
|
|
|
k->ptr[i] = MAKE_PTR(ca->buckets[b].gen,
|
2013-03-24 07:11:31 +08:00
|
|
|
bucket_to_sector(c, b),
|
|
|
|
ca->sb.nr_this_dev);
|
|
|
|
|
|
|
|
SET_KEY_PTRS(k, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
bch_bucket_free(c, k);
|
2013-07-25 07:46:42 +08:00
|
|
|
bkey_put(c, k);
|
2013-03-24 07:11:31 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-08-11 13:19:44 +08:00
|
|
|
int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
|
2013-07-25 08:29:09 +08:00
|
|
|
struct bkey *k, int n, bool wait)
|
2013-03-24 07:11:31 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2018-08-11 13:19:45 +08:00
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
mutex_lock(&c->bucket_lock);
|
2013-12-17 17:29:34 +08:00
|
|
|
ret = __bch_bucket_alloc_set(c, reserve, k, n, wait);
|
2013-03-24 07:11:31 +08:00
|
|
|
mutex_unlock(&c->bucket_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-25 09:11:11 +08:00
|
|
|
/* Sector allocator */
|
|
|
|
|
|
|
|
struct open_bucket {
|
|
|
|
struct list_head list;
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int last_write_point;
|
|
|
|
unsigned int sectors_free;
|
2013-07-25 09:11:11 +08:00
|
|
|
BKEY_PADDED(key);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We keep multiple buckets open for writes, and try to segregate different
|
bcache: segregate flash only volume write streams
In such scenario that there are some flash only volumes
, and some cached devices, when many tasks request these devices in
writeback mode, the write IOs may fall to the same bucket as bellow:
| cached data | flash data | cached data | cached data| flash data|
then after writeback of these cached devices, the bucket would
be like bellow bucket:
| free | flash data | free | free | flash data |
So, there are many free space in this bucket, but since data of flash
only volumes still exists, so this bucket cannot be reclaimable,
which would cause waste of bucket space.
In this patch, we segregate flash only volume write streams from
cached devices, so data from flash only volumes and cached devices
can store in different buckets.
Compare to v1 patch, this patch do not add a additionally open bucket
list, and it is try best to segregate flash only volume write streams
from cached devices, sectors of flash only volumes may still be mixed
with dirty sectors of cached device, but the number is very small.
[mlyle: fixed commit log formatting, permissions, line endings]
Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
Reviewed-by: Michael Lyle <mlyle@lyle.org>
Signed-off-by: Michael Lyle <mlyle@lyle.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-01-09 04:21:21 +08:00
|
|
|
* write streams for better cache utilization: first we try to segregate flash
|
|
|
|
* only volume write streams from cached devices, secondly we look for a bucket
|
|
|
|
* where the last write to it was sequential with the current write, and
|
|
|
|
* failing that we look for a bucket that was last used by the same task.
|
2013-07-25 09:11:11 +08:00
|
|
|
*
|
|
|
|
* The ideas is if you've got multiple tasks pulling data into the cache at the
|
|
|
|
* same time, you'll get better cache utilization if you try to segregate their
|
|
|
|
* data and preserve locality.
|
|
|
|
*
|
bcache: segregate flash only volume write streams
In such scenario that there are some flash only volumes
, and some cached devices, when many tasks request these devices in
writeback mode, the write IOs may fall to the same bucket as bellow:
| cached data | flash data | cached data | cached data| flash data|
then after writeback of these cached devices, the bucket would
be like bellow bucket:
| free | flash data | free | free | flash data |
So, there are many free space in this bucket, but since data of flash
only volumes still exists, so this bucket cannot be reclaimable,
which would cause waste of bucket space.
In this patch, we segregate flash only volume write streams from
cached devices, so data from flash only volumes and cached devices
can store in different buckets.
Compare to v1 patch, this patch do not add a additionally open bucket
list, and it is try best to segregate flash only volume write streams
from cached devices, sectors of flash only volumes may still be mixed
with dirty sectors of cached device, but the number is very small.
[mlyle: fixed commit log formatting, permissions, line endings]
Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
Reviewed-by: Michael Lyle <mlyle@lyle.org>
Signed-off-by: Michael Lyle <mlyle@lyle.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-01-09 04:21:21 +08:00
|
|
|
* For example, dirty sectors of flash only volume is not reclaimable, if their
|
|
|
|
* dirty sectors mixed with dirty sectors of cached device, such buckets will
|
|
|
|
* be marked as dirty and won't be reclaimed, though the dirty data of cached
|
|
|
|
* device have been written back to backend device.
|
|
|
|
*
|
|
|
|
* And say you've starting Firefox at the same time you're copying a
|
2013-07-25 09:11:11 +08:00
|
|
|
* bunch of files. Firefox will likely end up being fairly hot and stay in the
|
|
|
|
* cache awhile, but the data you copied might not be; if you wrote all that
|
|
|
|
* data to the same buckets it'd get invalidated at the same time.
|
|
|
|
*
|
|
|
|
* Both of those tasks will be doing fairly random IO so we can't rely on
|
|
|
|
* detecting sequential IO to segregate their data, but going off of the task
|
|
|
|
* should be a sane heuristic.
|
|
|
|
*/
|
|
|
|
static struct open_bucket *pick_data_bucket(struct cache_set *c,
|
|
|
|
const struct bkey *search,
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int write_point,
|
2013-07-25 09:11:11 +08:00
|
|
|
struct bkey *alloc)
|
|
|
|
{
|
|
|
|
struct open_bucket *ret, *ret_task = NULL;
|
|
|
|
|
|
|
|
list_for_each_entry_reverse(ret, &c->data_buckets, list)
|
bcache: segregate flash only volume write streams
In such scenario that there are some flash only volumes
, and some cached devices, when many tasks request these devices in
writeback mode, the write IOs may fall to the same bucket as bellow:
| cached data | flash data | cached data | cached data| flash data|
then after writeback of these cached devices, the bucket would
be like bellow bucket:
| free | flash data | free | free | flash data |
So, there are many free space in this bucket, but since data of flash
only volumes still exists, so this bucket cannot be reclaimable,
which would cause waste of bucket space.
In this patch, we segregate flash only volume write streams from
cached devices, so data from flash only volumes and cached devices
can store in different buckets.
Compare to v1 patch, this patch do not add a additionally open bucket
list, and it is try best to segregate flash only volume write streams
from cached devices, sectors of flash only volumes may still be mixed
with dirty sectors of cached device, but the number is very small.
[mlyle: fixed commit log formatting, permissions, line endings]
Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
Reviewed-by: Michael Lyle <mlyle@lyle.org>
Signed-off-by: Michael Lyle <mlyle@lyle.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2018-01-09 04:21:21 +08:00
|
|
|
if (UUID_FLASH_ONLY(&c->uuids[KEY_INODE(&ret->key)]) !=
|
|
|
|
UUID_FLASH_ONLY(&c->uuids[KEY_INODE(search)]))
|
|
|
|
continue;
|
|
|
|
else if (!bkey_cmp(&ret->key, search))
|
2013-07-25 09:11:11 +08:00
|
|
|
goto found;
|
|
|
|
else if (ret->last_write_point == write_point)
|
|
|
|
ret_task = ret;
|
|
|
|
|
|
|
|
ret = ret_task ?: list_first_entry(&c->data_buckets,
|
|
|
|
struct open_bucket, list);
|
|
|
|
found:
|
|
|
|
if (!ret->sectors_free && KEY_PTRS(alloc)) {
|
|
|
|
ret->sectors_free = c->sb.bucket_size;
|
|
|
|
bkey_copy(&ret->key, alloc);
|
|
|
|
bkey_init(alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret->sectors_free)
|
|
|
|
ret = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocates some space in the cache to write to, and k to point to the newly
|
|
|
|
* allocated space, and updates KEY_SIZE(k) and KEY_OFFSET(k) (to point to the
|
|
|
|
* end of the newly allocated space).
|
|
|
|
*
|
|
|
|
* May allocate fewer sectors than @sectors, KEY_SIZE(k) indicates how many
|
|
|
|
* sectors were actually allocated.
|
|
|
|
*
|
|
|
|
* If s->writeback is true, will not fail.
|
|
|
|
*/
|
2018-08-11 13:19:44 +08:00
|
|
|
bool bch_alloc_sectors(struct cache_set *c,
|
|
|
|
struct bkey *k,
|
|
|
|
unsigned int sectors,
|
|
|
|
unsigned int write_point,
|
|
|
|
unsigned int write_prio,
|
|
|
|
bool wait)
|
2013-07-25 09:11:11 +08:00
|
|
|
{
|
|
|
|
struct open_bucket *b;
|
|
|
|
BKEY_PADDED(key) alloc;
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int i;
|
2013-07-25 09:11:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We might have to allocate a new bucket, which we can't do with a
|
|
|
|
* spinlock held. So if we have to allocate, we drop the lock, allocate
|
|
|
|
* and then retry. KEY_PTRS() indicates whether alloc points to
|
|
|
|
* allocated bucket(s).
|
|
|
|
*/
|
|
|
|
|
|
|
|
bkey_init(&alloc.key);
|
|
|
|
spin_lock(&c->data_bucket_lock);
|
|
|
|
|
|
|
|
while (!(b = pick_data_bucket(c, k, write_point, &alloc.key))) {
|
2018-08-11 13:19:44 +08:00
|
|
|
unsigned int watermark = write_prio
|
2013-12-17 17:29:34 +08:00
|
|
|
? RESERVE_MOVINGGC
|
|
|
|
: RESERVE_NONE;
|
2013-07-25 09:11:11 +08:00
|
|
|
|
|
|
|
spin_unlock(&c->data_bucket_lock);
|
|
|
|
|
|
|
|
if (bch_bucket_alloc_set(c, watermark, &alloc.key, 1, wait))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
spin_lock(&c->data_bucket_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we had to allocate, we might race and not need to allocate the
|
2017-10-14 07:35:30 +08:00
|
|
|
* second time we call pick_data_bucket(). If we allocated a bucket but
|
2013-07-25 09:11:11 +08:00
|
|
|
* didn't use it, drop the refcount bch_bucket_alloc_set() took:
|
|
|
|
*/
|
|
|
|
if (KEY_PTRS(&alloc.key))
|
2013-07-25 07:46:42 +08:00
|
|
|
bkey_put(c, &alloc.key);
|
2013-07-25 09:11:11 +08:00
|
|
|
|
|
|
|
for (i = 0; i < KEY_PTRS(&b->key); i++)
|
|
|
|
EBUG_ON(ptr_stale(c, &b->key, i));
|
|
|
|
|
|
|
|
/* Set up the pointer to the space we're allocating: */
|
|
|
|
|
|
|
|
for (i = 0; i < KEY_PTRS(&b->key); i++)
|
|
|
|
k->ptr[i] = b->key.ptr[i];
|
|
|
|
|
|
|
|
sectors = min(sectors, b->sectors_free);
|
|
|
|
|
|
|
|
SET_KEY_OFFSET(k, KEY_OFFSET(k) + sectors);
|
|
|
|
SET_KEY_SIZE(k, sectors);
|
|
|
|
SET_KEY_PTRS(k, KEY_PTRS(&b->key));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move b to the end of the lru, and keep track of what this bucket was
|
|
|
|
* last used for:
|
|
|
|
*/
|
|
|
|
list_move_tail(&b->list, &c->data_buckets);
|
|
|
|
bkey_copy_key(&b->key, k);
|
|
|
|
b->last_write_point = write_point;
|
|
|
|
|
|
|
|
b->sectors_free -= sectors;
|
|
|
|
|
|
|
|
for (i = 0; i < KEY_PTRS(&b->key); i++) {
|
|
|
|
SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors);
|
|
|
|
|
|
|
|
atomic_long_add(sectors,
|
|
|
|
&PTR_CACHE(c, &b->key, i)->sectors_written);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b->sectors_free < c->sb.block_size)
|
|
|
|
b->sectors_free = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* k takes refcounts on the buckets it points to until it's inserted
|
|
|
|
* into the btree, but if we're done with this bucket we just transfer
|
|
|
|
* get_data_bucket()'s refcount.
|
|
|
|
*/
|
|
|
|
if (b->sectors_free)
|
|
|
|
for (i = 0; i < KEY_PTRS(&b->key); i++)
|
|
|
|
atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin);
|
|
|
|
|
|
|
|
spin_unlock(&c->data_bucket_lock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-24 07:11:31 +08:00
|
|
|
/* Init */
|
|
|
|
|
2013-07-25 09:11:11 +08:00
|
|
|
void bch_open_buckets_free(struct cache_set *c)
|
|
|
|
{
|
|
|
|
struct open_bucket *b;
|
|
|
|
|
|
|
|
while (!list_empty(&c->data_buckets)) {
|
|
|
|
b = list_first_entry(&c->data_buckets,
|
|
|
|
struct open_bucket, list);
|
|
|
|
list_del(&b->list);
|
|
|
|
kfree(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch_open_buckets_alloc(struct cache_set *c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
spin_lock_init(&c->data_bucket_lock);
|
|
|
|
|
bcache: increase the number of open buckets
In currently, we only alloc 6 open buckets for each cache set,
but in usually, we always attach about 10 or so backend devices for
each cache set, and the each bcache device are always accessed by
about 10 or so threads in top application layer. So 6 open buckets
are too few, It has led to that each of the same thread write data
to different buckets, which would cause low efficiency write-back,
and also cause buckets inefficient, and would be Very easy to run
out of.
I add debug message in bch_open_buckets_alloc() to print alloc bucket
info, and test with ten bcache devices with a cache set, and each
bcache device is accessed by ten threads.
From the debug message, we can see that, after the modification, One
bucket is more likely to assign to the same thread, and the data from
the same thread are more likely to write the same bucket. Usually the
same thread always write/read the same backend device, so it is good
for write-back and also promote the usage efficiency of buckets.
Signed-off-by: Tang Junhui <tang.junhui@zte.com.cn>
Reviewed-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2017-09-06 14:25:58 +08:00
|
|
|
for (i = 0; i < MAX_OPEN_BUCKETS; i++) {
|
2013-07-25 09:11:11 +08:00
|
|
|
struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
|
2018-08-11 13:19:45 +08:00
|
|
|
|
2013-07-25 09:11:11 +08:00
|
|
|
if (!b)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
list_add(&b->list, &c->data_buckets);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-25 10:01:12 +08:00
|
|
|
int bch_cache_allocator_start(struct cache *ca)
|
|
|
|
{
|
2013-07-11 09:31:58 +08:00
|
|
|
struct task_struct *k = kthread_run(bch_allocator_thread,
|
|
|
|
ca, "bcache_allocator");
|
|
|
|
if (IS_ERR(k))
|
|
|
|
return PTR_ERR(k);
|
2013-04-25 10:01:12 +08:00
|
|
|
|
2013-07-11 09:31:58 +08:00
|
|
|
ca->alloc_thread = k;
|
2013-04-25 10:01:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|