2 * Moving/copying garbage collector
4 * Copyright 2012 Google, Inc.
8 #include "btree_iter.h"
9 #include "btree_update.h"
13 #include "eytzinger.h"
20 #include <trace/events/bcachefs.h>
21 #include <linux/freezer.h>
22 #include <linux/kthread.h>
23 #include <linux/math64.h>
24 #include <linux/sched/task.h>
25 #include <linux/sort.h>
26 #include <linux/wait.h>
29 * We can't use the entire copygc reserve in one iteration of copygc: we may
30 * need the buckets we're freeing up to go back into the copygc reserve to make
31 * forward progress, but if the copygc reserve is full they'll be available for
32 * any allocation - and it's possible that in a given iteration, we free up most
33 * of the buckets we're going to free before we allocate most of the buckets
34 * we're going to allocate.
36 * If we only use half of the reserve per iteration, then in steady state we'll
37 * always have room in the reserve for the buckets we're going to need in the
40 #define COPYGC_BUCKETS_PER_ITER(ca) \
41 ((ca)->free[RESERVE_MOVINGGC].size / 2)
44 * Max sectors to move per iteration: Have to take into account internal
45 * fragmentation from the multiple write points for each generation:
47 #define COPYGC_SECTORS_PER_ITER(ca) \
48 ((ca)->mi.bucket_size * COPYGC_BUCKETS_PER_ITER(ca))
50 static inline int sectors_used_cmp(copygc_heap *heap,
51 struct copygc_heap_entry l,
52 struct copygc_heap_entry r)
54 return bucket_sectors_used(l.mark) - bucket_sectors_used(r.mark);
57 static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
59 const struct copygc_heap_entry *l = _l;
60 const struct copygc_heap_entry *r = _r;
62 return (l->offset > r->offset) - (l->offset < r->offset);
65 static bool __copygc_pred(struct bch_dev *ca,
66 struct bkey_s_c_extent e)
68 copygc_heap *h = &ca->copygc_heap;
69 const struct bch_extent_ptr *ptr =
70 bch2_extent_has_device(e, ca->dev_idx);
73 struct copygc_heap_entry search = { .offset = ptr->offset };
75 ssize_t i = eytzinger0_find_le(h->data, h->used,
77 bucket_offset_cmp, &search);
80 ptr->offset < h->data[i].offset + ca->mi.bucket_size &&
81 ptr->gen == h->data[i].mark.gen);
87 static enum data_cmd copygc_pred(struct bch_fs *c, void *arg,
89 struct bkey_s_c_extent e,
90 struct bch_io_opts *io_opts,
91 struct data_opts *data_opts)
93 struct bch_dev *ca = arg;
95 if (!__copygc_pred(ca, e))
98 data_opts->target = dev_to_target(ca->dev_idx);
99 data_opts->btree_insert_flags = BTREE_INSERT_USE_RESERVE;
100 data_opts->rewrite_dev = ca->dev_idx;
104 static bool have_copygc_reserve(struct bch_dev *ca)
108 spin_lock(&ca->freelist_lock);
109 ret = fifo_full(&ca->free[RESERVE_MOVINGGC]) ||
110 ca->allocator_blocked;
111 spin_unlock(&ca->freelist_lock);
116 static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
118 copygc_heap *h = &ca->copygc_heap;
119 struct copygc_heap_entry e, *i;
120 struct bucket_array *buckets;
121 struct bch_move_stats move_stats;
122 u64 sectors_to_move = 0, sectors_not_moved = 0;
123 u64 buckets_to_move, buckets_not_moved = 0;
127 memset(&move_stats, 0, sizeof(move_stats));
128 closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
131 * Find buckets with lowest sector counts, skipping completely
132 * empty buckets, by building a maxheap sorted by sector count,
133 * and repeatedly replacing the maximum element until all
134 * buckets have been visited.
139 * We need bucket marks to be up to date - gc can't be recalculating
142 down_read(&c->gc_lock);
143 down_read(&ca->bucket_lock);
144 buckets = bucket_array(ca);
146 for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
147 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
148 struct copygc_heap_entry e;
150 if (m.owned_by_allocator ||
151 m.data_type != BCH_DATA_USER ||
152 !bucket_sectors_used(m) ||
153 bucket_sectors_used(m) >= ca->mi.bucket_size)
156 e = (struct copygc_heap_entry) {
157 .offset = bucket_to_sector(ca, b),
160 heap_add_or_replace(h, e, -sectors_used_cmp);
162 up_read(&ca->bucket_lock);
163 up_read(&c->gc_lock);
165 for (i = h->data; i < h->data + h->used; i++)
166 sectors_to_move += bucket_sectors_used(i->mark);
168 while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
169 BUG_ON(!heap_pop(h, e, -sectors_used_cmp));
170 sectors_to_move -= bucket_sectors_used(e.mark);
173 buckets_to_move = h->used;
175 if (!buckets_to_move)
178 eytzinger0_sort(h->data, h->used,
180 bucket_offset_cmp, NULL);
182 ret = bch2_move_data(c, &ca->copygc_pd.rate,
183 writepoint_ptr(&ca->copygc_write_point),
188 down_read(&ca->bucket_lock);
189 buckets = bucket_array(ca);
190 for (i = h->data; i < h->data + h->used; i++) {
191 size_t b = sector_to_bucket(ca, i->offset);
192 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
194 if (i->mark.gen == m.gen && bucket_sectors_used(m)) {
195 sectors_not_moved += bucket_sectors_used(m);
199 up_read(&ca->bucket_lock);
201 if (sectors_not_moved && !ret)
202 bch_warn(c, "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved",
203 sectors_not_moved, sectors_to_move,
204 buckets_not_moved, buckets_to_move);
207 atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
208 buckets_to_move, buckets_not_moved);
211 static int bch2_copygc_thread(void *arg)
213 struct bch_dev *ca = arg;
214 struct bch_fs *c = ca->fs;
215 struct io_clock *clock = &c->io_clock[WRITE];
216 struct bch_dev_usage usage;
218 u64 available, fragmented, reserve, next;
222 while (!kthread_should_stop()) {
223 if (kthread_wait_freezable(c->copy_gc_enabled))
226 last = atomic_long_read(&clock->now);
228 reserve = div64_u64((ca->mi.nbuckets - ca->mi.first_bucket) *
230 c->opts.gc_reserve_percent, 200);
232 usage = bch2_dev_usage_read(c, ca);
235 * don't start copygc until less than half the gc reserve is
238 available = __dev_buckets_available(ca, usage) *
240 if (available > reserve) {
241 next = last + available - reserve;
242 bch2_kthread_io_clock_wait(clock, next);
247 * don't start copygc until there's more than half the copygc
248 * reserve of fragmented space:
250 fragmented = usage.sectors_fragmented;
251 if (fragmented < reserve) {
252 next = last + reserve - fragmented;
253 bch2_kthread_io_clock_wait(clock, next);
263 void bch2_copygc_stop(struct bch_dev *ca)
265 ca->copygc_pd.rate.rate = UINT_MAX;
266 bch2_ratelimit_reset(&ca->copygc_pd.rate);
268 if (ca->copygc_thread) {
269 kthread_stop(ca->copygc_thread);
270 put_task_struct(ca->copygc_thread);
272 ca->copygc_thread = NULL;
275 int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
277 struct task_struct *t;
279 BUG_ON(ca->copygc_thread);
281 if (c->opts.nochanges)
284 if (bch2_fs_init_fault("copygc_start"))
287 t = kthread_create(bch2_copygc_thread, ca, "bch_copygc");
293 ca->copygc_thread = t;
294 wake_up_process(ca->copygc_thread);
299 void bch2_dev_copygc_init(struct bch_dev *ca)
301 bch2_pd_controller_init(&ca->copygc_pd);
302 ca->copygc_pd.d_term = 0;