1 // SPDX-License-Identifier: GPL-2.0
3 * Moving/copying garbage collector
5 * Copyright 2012 Google, Inc.
9 #include "alloc_foreground.h"
10 #include "btree_iter.h"
11 #include "btree_update.h"
14 #include "disk_groups.h"
17 #include "eytzinger.h"
24 #include <trace/events/bcachefs.h>
25 #include <linux/freezer.h>
26 #include <linux/kthread.h>
27 #include <linux/math64.h>
28 #include <linux/sched/task.h>
29 #include <linux/sort.h>
30 #include <linux/wait.h>
33 * We can't use the entire copygc reserve in one iteration of copygc: we may
34 * need the buckets we're freeing up to go back into the copygc reserve to make
35 * forward progress, but if the copygc reserve is full they'll be available for
36 * any allocation - and it's possible that in a given iteration, we free up most
37 * of the buckets we're going to free before we allocate most of the buckets
38 * we're going to allocate.
40 * If we only use half of the reserve per iteration, then in steady state we'll
41 * always have room in the reserve for the buckets we're going to need in the
44 #define COPYGC_BUCKETS_PER_ITER(ca) \
45 ((ca)->free[RESERVE_MOVINGGC].size / 2)
47 static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
49 const struct copygc_heap_entry *l = _l;
50 const struct copygc_heap_entry *r = _r;
52 return cmp_int(l->dev, r->dev) ?:
53 cmp_int(l->offset, r->offset);
56 static enum data_cmd copygc_pred(struct bch_fs *c, void *arg,
58 struct bch_io_opts *io_opts,
59 struct data_opts *data_opts)
61 copygc_heap *h = &c->copygc_heap;
62 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
63 const union bch_extent_entry *entry;
64 struct extent_ptr_decoded p = { 0 };
66 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
67 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
68 struct copygc_heap_entry search = {
70 .offset = p.ptr.offset,
73 ssize_t i = eytzinger0_find_le(h->data, h->used,
75 bucket_offset_cmp, &search);
77 /* eytzinger search verify code: */
80 for (k = 0; k < h->used; k++)
81 if (h->data[k].offset <= ptr->offset &&
82 (j < 0 || h->data[k].offset > h->data[j].offset))
88 p.ptr.offset < h->data[i].offset + ca->mi.bucket_size &&
89 p.ptr.gen == h->data[i].gen) {
90 data_opts->target = io_opts->background_target;
91 data_opts->nr_replicas = 1;
92 data_opts->btree_insert_flags = BTREE_INSERT_USE_RESERVE;
93 data_opts->rewrite_dev = p.ptr.dev;
96 data_opts->nr_replicas += p.ec.redundancy;
105 static bool have_copygc_reserve(struct bch_dev *ca)
109 spin_lock(&ca->fs->freelist_lock);
110 ret = fifo_full(&ca->free[RESERVE_MOVINGGC]) ||
111 ca->allocator_state != ALLOCATOR_RUNNING;
112 spin_unlock(&ca->fs->freelist_lock);
117 static inline int fragmentation_cmp(copygc_heap *heap,
118 struct copygc_heap_entry l,
119 struct copygc_heap_entry r)
121 return cmp_int(l.fragmentation, r.fragmentation);
124 static int bch2_copygc(struct bch_fs *c)
126 copygc_heap *h = &c->copygc_heap;
127 struct copygc_heap_entry e, *i;
128 struct bucket_array *buckets;
129 struct bch_move_stats move_stats;
130 u64 sectors_to_move = 0, sectors_not_moved = 0;
131 u64 sectors_reserved = 0;
132 u64 buckets_to_move, buckets_not_moved = 0;
135 size_t b, heap_size = 0;
138 memset(&move_stats, 0, sizeof(move_stats));
140 * Find buckets with lowest sector counts, skipping completely
141 * empty buckets, by building a maxheap sorted by sector count,
142 * and repeatedly replacing the maximum element until all
143 * buckets have been visited.
147 for_each_rw_member(ca, c, dev_idx)
148 heap_size += ca->mi.nbuckets >> 7;
150 if (h->size < heap_size) {
151 free_heap(&c->copygc_heap);
152 if (!init_heap(&c->copygc_heap, heap_size, GFP_KERNEL)) {
153 bch_err(c, "error allocating copygc heap");
158 for_each_rw_member(ca, c, dev_idx) {
159 closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
161 spin_lock(&ca->fs->freelist_lock);
162 sectors_reserved += fifo_used(&ca->free[RESERVE_MOVINGGC]) * ca->mi.bucket_size;
163 spin_unlock(&ca->fs->freelist_lock);
165 down_read(&ca->bucket_lock);
166 buckets = bucket_array(ca);
168 for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
169 struct bucket *g = buckets->b + b;
170 struct bucket_mark m = READ_ONCE(g->mark);
171 struct copygc_heap_entry e;
173 if (m.owned_by_allocator ||
174 m.data_type != BCH_DATA_user ||
175 !bucket_sectors_used(m) ||
176 bucket_sectors_used(m) >= ca->mi.bucket_size)
179 WARN_ON(m.stripe && !g->stripe_redundancy);
181 e = (struct copygc_heap_entry) {
184 .replicas = 1 + g->stripe_redundancy,
185 .fragmentation = bucket_sectors_used(m) * (1U << 15)
186 / ca->mi.bucket_size,
187 .sectors = bucket_sectors_used(m),
188 .offset = bucket_to_sector(ca, b),
190 heap_add_or_replace(h, e, -fragmentation_cmp, NULL);
192 up_read(&ca->bucket_lock);
195 if (!sectors_reserved) {
196 bch2_fs_fatal_error(c, "stuck, ran out of copygc reserve!");
201 * Our btree node allocations also come out of RESERVE_MOVINGGC:
203 sectors_to_move = (sectors_to_move * 3) / 4;
205 for (i = h->data; i < h->data + h->used; i++)
206 sectors_to_move += i->sectors * i->replicas;
208 while (sectors_to_move > sectors_reserved) {
209 BUG_ON(!heap_pop(h, e, -fragmentation_cmp, NULL));
210 sectors_to_move -= e.sectors * e.replicas;
213 buckets_to_move = h->used;
215 if (!buckets_to_move)
218 eytzinger0_sort(h->data, h->used,
220 bucket_offset_cmp, NULL);
222 ret = bch2_move_data(c, &c->copygc_pd.rate,
223 writepoint_ptr(&c->copygc_write_point),
228 for_each_rw_member(ca, c, dev_idx) {
229 down_read(&ca->bucket_lock);
230 buckets = bucket_array(ca);
231 for (i = h->data; i < h->data + h->used; i++) {
232 struct bucket_mark m;
235 if (i->dev != dev_idx)
238 b = sector_to_bucket(ca, i->offset);
239 m = READ_ONCE(buckets->b[b].mark);
241 if (i->gen == m.gen &&
242 bucket_sectors_used(m)) {
243 sectors_not_moved += bucket_sectors_used(m);
247 up_read(&ca->bucket_lock);
250 if (sectors_not_moved && !ret)
251 bch_warn_ratelimited(c,
252 "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved (move stats: moved %llu sectors, raced %llu keys, %llu sectors)",
253 sectors_not_moved, sectors_to_move,
254 buckets_not_moved, buckets_to_move,
255 atomic64_read(&move_stats.sectors_moved),
256 atomic64_read(&move_stats.keys_raced),
257 atomic64_read(&move_stats.sectors_raced));
260 atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
261 buckets_to_move, buckets_not_moved);
266 * Copygc runs when the amount of fragmented data is above some arbitrary
269 * The threshold at the limit - when the device is full - is the amount of space
270 * we reserved in bch2_recalc_capacity; we can't have more than that amount of
271 * disk space stranded due to fragmentation and store everything we have
274 * But we don't want to be running copygc unnecessarily when the device still
275 * has plenty of free space - rather, we want copygc to smoothly run every so
276 * often and continually reduce the amount of fragmented space as the device
277 * fills up. So, we increase the threshold by half the current free space.
279 unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
283 u64 fragmented_allowed = c->copygc_threshold;
286 for_each_rw_member(ca, c, dev_idx) {
287 struct bch_dev_usage usage = bch2_dev_usage_read(ca);
289 fragmented_allowed += ((__dev_buckets_available(ca, usage) *
290 ca->mi.bucket_size) >> 1);
291 fragmented += usage.d[BCH_DATA_user].fragmented;
294 return max_t(s64, 0, fragmented_allowed - fragmented);
297 static int bch2_copygc_thread(void *arg)
299 struct bch_fs *c = arg;
300 struct io_clock *clock = &c->io_clock[WRITE];
305 while (!kthread_should_stop()) {
306 if (kthread_wait_freezable(c->copy_gc_enabled))
309 last = atomic64_read(&clock->now);
310 wait = bch2_copygc_wait_amount(c);
312 if (wait > clock->max_slop) {
313 bch2_kthread_io_clock_wait(clock, last + wait,
314 MAX_SCHEDULE_TIMEOUT);
325 void bch2_copygc_stop(struct bch_fs *c)
327 c->copygc_pd.rate.rate = UINT_MAX;
328 bch2_ratelimit_reset(&c->copygc_pd.rate);
330 if (c->copygc_thread) {
331 kthread_stop(c->copygc_thread);
332 put_task_struct(c->copygc_thread);
334 c->copygc_thread = NULL;
337 int bch2_copygc_start(struct bch_fs *c)
339 struct task_struct *t;
341 if (c->copygc_thread)
344 if (c->opts.nochanges)
347 if (bch2_fs_init_fault("copygc_start"))
350 t = kthread_create(bch2_copygc_thread, c, "bch-copygc/%s", c->name);
352 bch_err(c, "error creating copygc thread: %li", PTR_ERR(t));
358 c->copygc_thread = t;
359 wake_up_process(c->copygc_thread);
364 void bch2_fs_copygc_init(struct bch_fs *c)
366 bch2_pd_controller_init(&c->copygc_pd);
367 c->copygc_pd.d_term = 0;