2 * Moving/copying garbage collector
4 * Copyright 2012 Google, Inc.
8 #include "alloc_foreground.h"
9 #include "btree_iter.h"
10 #include "btree_update.h"
13 #include "disk_groups.h"
15 #include "eytzinger.h"
22 #include <trace/events/bcachefs.h>
23 #include <linux/freezer.h>
24 #include <linux/kthread.h>
25 #include <linux/math64.h>
26 #include <linux/sched/task.h>
27 #include <linux/sort.h>
28 #include <linux/wait.h>
31 * We can't use the entire copygc reserve in one iteration of copygc: we may
32 * need the buckets we're freeing up to go back into the copygc reserve to make
33 * forward progress, but if the copygc reserve is full they'll be available for
34 * any allocation - and it's possible that in a given iteration, we free up most
35 * of the buckets we're going to free before we allocate most of the buckets
36 * we're going to allocate.
38 * If we only use half of the reserve per iteration, then in steady state we'll
39 * always have room in the reserve for the buckets we're going to need in the
42 #define COPYGC_BUCKETS_PER_ITER(ca) \
43 ((ca)->free[RESERVE_MOVINGGC].size / 2)
46 * Max sectors to move per iteration: Have to take into account internal
47 * fragmentation from the multiple write points for each generation:
49 #define COPYGC_SECTORS_PER_ITER(ca) \
50 ((ca)->mi.bucket_size * COPYGC_BUCKETS_PER_ITER(ca))
52 static inline int sectors_used_cmp(copygc_heap *heap,
53 struct copygc_heap_entry l,
54 struct copygc_heap_entry r)
56 return (l.sectors > r.sectors) - (l.sectors < r.sectors);
59 static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
61 const struct copygc_heap_entry *l = _l;
62 const struct copygc_heap_entry *r = _r;
64 return (l->offset > r->offset) - (l->offset < r->offset);
67 static bool __copygc_pred(struct bch_dev *ca,
68 struct bkey_s_c_extent e)
70 copygc_heap *h = &ca->copygc_heap;
71 const struct bch_extent_ptr *ptr =
72 bch2_extent_has_device(e, ca->dev_idx);
75 struct copygc_heap_entry search = { .offset = ptr->offset };
77 ssize_t i = eytzinger0_find_le(h->data, h->used,
79 bucket_offset_cmp, &search);
82 ptr->offset < h->data[i].offset + ca->mi.bucket_size &&
83 ptr->gen == h->data[i].gen);
89 static enum data_cmd copygc_pred(struct bch_fs *c, void *arg,
91 struct bkey_s_c_extent e,
92 struct bch_io_opts *io_opts,
93 struct data_opts *data_opts)
95 struct bch_dev *ca = arg;
97 if (!__copygc_pred(ca, e))
100 data_opts->target = dev_to_target(ca->dev_idx);
101 data_opts->btree_insert_flags = BTREE_INSERT_USE_RESERVE;
102 data_opts->rewrite_dev = ca->dev_idx;
106 static bool have_copygc_reserve(struct bch_dev *ca)
110 spin_lock(&ca->freelist_lock);
111 ret = fifo_full(&ca->free[RESERVE_MOVINGGC]) ||
112 ca->allocator_blocked;
113 spin_unlock(&ca->freelist_lock);
118 static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
120 copygc_heap *h = &ca->copygc_heap;
121 struct copygc_heap_entry e, *i;
122 struct bucket_array *buckets;
123 struct bch_move_stats move_stats;
124 u64 sectors_to_move = 0, sectors_not_moved = 0;
125 u64 buckets_to_move, buckets_not_moved = 0;
129 memset(&move_stats, 0, sizeof(move_stats));
130 closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
133 * Find buckets with lowest sector counts, skipping completely
134 * empty buckets, by building a maxheap sorted by sector count,
135 * and repeatedly replacing the maximum element until all
136 * buckets have been visited.
141 * We need bucket marks to be up to date - gc can't be recalculating
144 down_read(&c->gc_lock);
145 down_read(&ca->bucket_lock);
146 buckets = bucket_array(ca);
148 for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
149 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
150 struct copygc_heap_entry e;
152 if (m.owned_by_allocator ||
153 m.data_type != BCH_DATA_USER ||
154 !bucket_sectors_used(m) ||
155 bucket_sectors_used(m) >= ca->mi.bucket_size)
158 e = (struct copygc_heap_entry) {
160 .sectors = bucket_sectors_used(m),
161 .offset = bucket_to_sector(ca, b),
163 heap_add_or_replace(h, e, -sectors_used_cmp, NULL);
165 up_read(&ca->bucket_lock);
166 up_read(&c->gc_lock);
168 for (i = h->data; i < h->data + h->used; i++)
169 sectors_to_move += i->sectors;
171 while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
172 BUG_ON(!heap_pop(h, e, -sectors_used_cmp, NULL));
173 sectors_to_move -= e.sectors;
176 buckets_to_move = h->used;
178 if (!buckets_to_move)
181 eytzinger0_sort(h->data, h->used,
183 bucket_offset_cmp, NULL);
185 ret = bch2_move_data(c, &ca->copygc_pd.rate,
186 writepoint_ptr(&ca->copygc_write_point),
191 down_read(&ca->bucket_lock);
192 buckets = bucket_array(ca);
193 for (i = h->data; i < h->data + h->used; i++) {
194 size_t b = sector_to_bucket(ca, i->offset);
195 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
197 if (i->gen == m.gen && bucket_sectors_used(m)) {
198 sectors_not_moved += bucket_sectors_used(m);
202 up_read(&ca->bucket_lock);
204 if (sectors_not_moved && !ret)
205 bch_warn(c, "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved",
206 sectors_not_moved, sectors_to_move,
207 buckets_not_moved, buckets_to_move);
210 atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
211 buckets_to_move, buckets_not_moved);
214 static int bch2_copygc_thread(void *arg)
216 struct bch_dev *ca = arg;
217 struct bch_fs *c = ca->fs;
218 struct io_clock *clock = &c->io_clock[WRITE];
219 struct bch_dev_usage usage;
221 u64 available, fragmented, reserve, next;
225 while (!kthread_should_stop()) {
226 if (kthread_wait_freezable(c->copy_gc_enabled))
229 last = atomic_long_read(&clock->now);
231 reserve = ca->copygc_threshold;
233 usage = bch2_dev_usage_read(c, ca);
235 available = __dev_buckets_available(ca, usage) *
237 if (available > reserve) {
238 next = last + available - reserve;
239 bch2_kthread_io_clock_wait(clock, next,
240 MAX_SCHEDULE_TIMEOUT);
245 * don't start copygc until there's more than half the copygc
246 * reserve of fragmented space:
248 fragmented = usage.sectors_fragmented;
249 if (fragmented < reserve) {
250 next = last + reserve - fragmented;
251 bch2_kthread_io_clock_wait(clock, next,
252 MAX_SCHEDULE_TIMEOUT);
262 void bch2_copygc_stop(struct bch_dev *ca)
264 ca->copygc_pd.rate.rate = UINT_MAX;
265 bch2_ratelimit_reset(&ca->copygc_pd.rate);
267 if (ca->copygc_thread) {
268 kthread_stop(ca->copygc_thread);
269 put_task_struct(ca->copygc_thread);
271 ca->copygc_thread = NULL;
274 int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
276 struct task_struct *t;
278 BUG_ON(ca->copygc_thread);
280 if (c->opts.nochanges)
283 if (bch2_fs_init_fault("copygc_start"))
286 t = kthread_create(bch2_copygc_thread, ca,
287 "bch_copygc[%s]", ca->name);
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;