]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_key_cache.c
Update bcachefs sources to fcf8a0889c bcachefs: bch2_alloc_write() should be writing...
[bcachefs-tools-debian] / libbcachefs / btree_key_cache.c
1
2 #include "bcachefs.h"
3 #include "btree_cache.h"
4 #include "btree_iter.h"
5 #include "btree_key_cache.h"
6 #include "btree_locking.h"
7 #include "btree_update.h"
8 #include "error.h"
9 #include "journal.h"
10 #include "journal_reclaim.h"
11
12 #include <linux/sched/mm.h>
13 #include <trace/events/bcachefs.h>
14
15 static struct kmem_cache *bch2_key_cache;
16
17 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
18                                        const void *obj)
19 {
20         const struct bkey_cached *ck = obj;
21         const struct bkey_cached_key *key = arg->key;
22
23         return cmp_int(ck->key.btree_id, key->btree_id) ?:
24                 bkey_cmp(ck->key.pos, key->pos);
25 }
26
27 static const struct rhashtable_params bch2_btree_key_cache_params = {
28         .head_offset    = offsetof(struct bkey_cached, hash),
29         .key_offset     = offsetof(struct bkey_cached, key),
30         .key_len        = sizeof(struct bkey_cached_key),
31         .obj_cmpfn      = bch2_btree_key_cache_cmp_fn,
32 };
33
34 __flatten
35 inline struct bkey_cached *
36 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
37 {
38         struct bkey_cached_key key = {
39                 .btree_id       = btree_id,
40                 .pos            = pos,
41         };
42
43         return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
44                                       bch2_btree_key_cache_params);
45 }
46
47 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
48 {
49         if (!six_trylock_intent(&ck->c.lock))
50                 return false;
51
52         if (!six_trylock_write(&ck->c.lock)) {
53                 six_unlock_intent(&ck->c.lock);
54                 return false;
55         }
56
57         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
58                 six_unlock_write(&ck->c.lock);
59                 six_unlock_intent(&ck->c.lock);
60                 return false;
61         }
62
63         return true;
64 }
65
66 static void bkey_cached_evict(struct btree_key_cache *c,
67                               struct bkey_cached *ck)
68 {
69         BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
70                                       bch2_btree_key_cache_params));
71         memset(&ck->key, ~0, sizeof(ck->key));
72
73         c->nr_keys--;
74 }
75
76 static void bkey_cached_free(struct btree_key_cache *bc,
77                              struct bkey_cached *ck)
78 {
79         struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
80
81         BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
82
83         ck->btree_trans_barrier_seq =
84                 start_poll_synchronize_srcu(&c->btree_trans_barrier);
85
86         list_move_tail(&ck->list, &bc->freed);
87         bc->nr_freed++;
88
89         kfree(ck->k);
90         ck->k           = NULL;
91         ck->u64s        = 0;
92
93         six_unlock_write(&ck->c.lock);
94         six_unlock_intent(&ck->c.lock);
95 }
96
97 static struct bkey_cached *
98 bkey_cached_alloc(struct btree_key_cache *c)
99 {
100         struct bkey_cached *ck;
101
102         list_for_each_entry_reverse(ck, &c->freed, list)
103                 if (bkey_cached_lock_for_evict(ck)) {
104                         c->nr_freed--;
105                         return ck;
106                 }
107
108         ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
109         if (likely(ck)) {
110                 INIT_LIST_HEAD(&ck->list);
111                 six_lock_init(&ck->c.lock);
112                 BUG_ON(!six_trylock_intent(&ck->c.lock));
113                 BUG_ON(!six_trylock_write(&ck->c.lock));
114                 return ck;
115         }
116
117         list_for_each_entry(ck, &c->clean, list)
118                 if (bkey_cached_lock_for_evict(ck)) {
119                         bkey_cached_evict(c, ck);
120                         return ck;
121                 }
122
123         return NULL;
124 }
125
126 static struct bkey_cached *
127 btree_key_cache_create(struct btree_key_cache *c,
128                        enum btree_id btree_id,
129                        struct bpos pos)
130 {
131         struct bkey_cached *ck;
132
133         ck = bkey_cached_alloc(c);
134         if (!ck)
135                 return ERR_PTR(-ENOMEM);
136
137         ck->c.level             = 0;
138         ck->c.btree_id          = btree_id;
139         ck->key.btree_id        = btree_id;
140         ck->key.pos             = pos;
141         ck->valid               = false;
142         ck->flags               = 1U << BKEY_CACHED_ACCESSED;
143
144         if (rhashtable_lookup_insert_fast(&c->table,
145                                           &ck->hash,
146                                           bch2_btree_key_cache_params)) {
147                 /* We raced with another fill: */
148                 bkey_cached_free(c, ck);
149                 return NULL;
150         }
151
152         c->nr_keys++;
153
154         list_move(&ck->list, &c->clean);
155         six_unlock_write(&ck->c.lock);
156
157         return ck;
158 }
159
160 static int btree_key_cache_fill(struct btree_trans *trans,
161                                 struct btree_iter *ck_iter,
162                                 struct bkey_cached *ck)
163 {
164         struct btree_iter *iter;
165         struct bkey_s_c k;
166         unsigned new_u64s = 0;
167         struct bkey_i *new_k = NULL;
168         int ret;
169
170         iter = bch2_trans_get_iter(trans, ck->key.btree_id,
171                                    ck->key.pos, BTREE_ITER_SLOTS);
172         k = bch2_btree_iter_peek_slot(iter);
173         ret = bkey_err(k);
174         if (ret) {
175                 bch2_trans_iter_put(trans, iter);
176                 return ret;
177         }
178
179         if (!bch2_btree_node_relock(ck_iter, 0)) {
180                 bch2_trans_iter_put(trans, iter);
181                 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
182                 return -EINTR;
183         }
184
185         if (k.k->u64s > ck->u64s) {
186                 new_u64s = roundup_pow_of_two(k.k->u64s);
187                 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
188                 if (!new_k) {
189                         bch2_trans_iter_put(trans, iter);
190                         return -ENOMEM;
191                 }
192         }
193
194         bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
195         if (new_k) {
196                 kfree(ck->k);
197                 ck->u64s = new_u64s;
198                 ck->k = new_k;
199         }
200
201         bkey_reassemble(ck->k, k);
202         ck->valid = true;
203         bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
204
205         /* We're not likely to need this iterator again: */
206         bch2_trans_iter_free(trans, iter);
207
208         return 0;
209 }
210
211 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
212 {
213         struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
214         const struct btree_iter *iter = p;
215
216         return ck->key.btree_id == iter->btree_id &&
217                 !bkey_cmp(ck->key.pos, iter->pos) ? 0 : -1;
218 }
219
220 __flatten
221 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
222 {
223         struct btree_trans *trans = iter->trans;
224         struct bch_fs *c = trans->c;
225         struct bkey_cached *ck;
226         int ret = 0;
227
228         BUG_ON(iter->level);
229
230         if (btree_node_locked(iter, 0)) {
231                 ck = (void *) iter->l[0].b;
232                 goto fill;
233         }
234 retry:
235         ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
236         if (!ck) {
237                 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
238                         iter->l[0].b = NULL;
239                         return 0;
240                 }
241
242                 mutex_lock(&c->btree_key_cache.lock);
243                 ck = btree_key_cache_create(&c->btree_key_cache,
244                                             iter->btree_id, iter->pos);
245                 mutex_unlock(&c->btree_key_cache.lock);
246
247                 ret = PTR_ERR_OR_ZERO(ck);
248                 if (ret)
249                         goto err;
250                 if (!ck)
251                         goto retry;
252
253                 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
254                 iter->locks_want = 1;
255         } else {
256                 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
257
258                 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
259                                      bkey_cached_check_fn, iter, _THIS_IP_)) {
260                         if (ck->key.btree_id != iter->btree_id ||
261                             bkey_cmp(ck->key.pos, iter->pos)) {
262                                 goto retry;
263                         }
264
265                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
266                         ret = -EINTR;
267                         goto err;
268                 }
269
270                 if (ck->key.btree_id != iter->btree_id ||
271                     bkey_cmp(ck->key.pos, iter->pos)) {
272                         six_unlock_type(&ck->c.lock, lock_want);
273                         goto retry;
274                 }
275
276                 mark_btree_node_locked(iter, 0, lock_want);
277         }
278
279         iter->l[0].lock_seq     = ck->c.lock.state.seq;
280         iter->l[0].b            = (void *) ck;
281 fill:
282         if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
283                 if (!btree_node_intent_locked(iter, 0))
284                         bch2_btree_iter_upgrade(iter, 1);
285                 if (!btree_node_intent_locked(iter, 0)) {
286                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
287                         ret = -EINTR;
288                         goto err;
289                 }
290
291                 ret = btree_key_cache_fill(trans, iter, ck);
292                 if (ret)
293                         goto err;
294         }
295
296         if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
297                 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
298
299         iter->uptodate = BTREE_ITER_NEED_PEEK;
300         bch2_btree_iter_downgrade(iter);
301         return ret;
302 err:
303         if (ret != -EINTR) {
304                 btree_node_unlock(iter, 0);
305                 iter->flags |= BTREE_ITER_ERROR;
306                 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
307         }
308         return ret;
309 }
310
311 static int btree_key_cache_flush_pos(struct btree_trans *trans,
312                                      struct bkey_cached_key key,
313                                      u64 journal_seq,
314                                      bool evict)
315 {
316         struct bch_fs *c = trans->c;
317         struct journal *j = &c->journal;
318         struct btree_iter *c_iter = NULL, *b_iter = NULL;
319         struct bkey_cached *ck = NULL;
320         int ret;
321
322         b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
323                                      BTREE_ITER_SLOTS|
324                                      BTREE_ITER_INTENT);
325         c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
326                                      BTREE_ITER_CACHED|
327                                      BTREE_ITER_CACHED_NOFILL|
328                                      BTREE_ITER_CACHED_NOCREATE|
329                                      BTREE_ITER_INTENT);
330 retry:
331         ret = bch2_btree_iter_traverse(c_iter);
332         if (ret)
333                 goto err;
334
335         ck = (void *) c_iter->l[0].b;
336         if (!ck ||
337             (journal_seq && ck->journal.seq != journal_seq))
338                 goto out;
339
340         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
341                 if (!evict)
342                         goto out;
343                 goto evict;
344         }
345
346         ret   = bch2_btree_iter_traverse(b_iter) ?:
347                 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
348                 bch2_trans_commit(trans, NULL, NULL,
349                                   BTREE_INSERT_NOUNLOCK|
350                                   BTREE_INSERT_NOCHECK_RW|
351                                   BTREE_INSERT_NOFAIL|
352                                   BTREE_INSERT_JOURNAL_RESERVED|
353                                   BTREE_INSERT_JOURNAL_RECLAIM);
354 err:
355         if (ret == -EINTR)
356                 goto retry;
357
358         if (ret) {
359                 bch2_fs_fatal_err_on(!bch2_journal_error(j), c,
360                         "error flushing key cache: %i", ret);
361                 goto out;
362         }
363
364         bch2_journal_pin_drop(j, &ck->journal);
365         bch2_journal_preres_put(j, &ck->res);
366
367         if (!evict) {
368                 mutex_lock(&c->btree_key_cache.lock);
369                 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
370                         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
371                         c->btree_key_cache.nr_dirty--;
372                 }
373
374                 list_move_tail(&ck->list, &c->btree_key_cache.clean);
375                 mutex_unlock(&c->btree_key_cache.lock);
376         } else {
377 evict:
378                 BUG_ON(!btree_node_intent_locked(c_iter, 0));
379
380                 mark_btree_node_unlocked(c_iter, 0);
381                 c_iter->l[0].b = NULL;
382
383                 six_lock_write(&ck->c.lock, NULL, NULL);
384
385                 mutex_lock(&c->btree_key_cache.lock);
386                 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
387                         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
388                         c->btree_key_cache.nr_dirty--;
389                 }
390
391                 bkey_cached_evict(&c->btree_key_cache, ck);
392                 bkey_cached_free(&c->btree_key_cache, ck);
393                 mutex_unlock(&c->btree_key_cache.lock);
394         }
395 out:
396         bch2_trans_iter_put(trans, b_iter);
397         bch2_trans_iter_put(trans, c_iter);
398         return ret;
399 }
400
401 static void btree_key_cache_journal_flush(struct journal *j,
402                                           struct journal_entry_pin *pin,
403                                           u64 seq)
404 {
405         struct bch_fs *c = container_of(j, struct bch_fs, journal);
406         struct bkey_cached *ck =
407                 container_of(pin, struct bkey_cached, journal);
408         struct bkey_cached_key key;
409         struct btree_trans trans;
410
411         int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
412
413         six_lock_read(&ck->c.lock, NULL, NULL);
414         key = ck->key;
415
416         if (ck->journal.seq != seq ||
417             !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
418                 six_unlock_read(&ck->c.lock);
419                 goto unlock;
420         }
421         six_unlock_read(&ck->c.lock);
422
423         bch2_trans_init(&trans, c, 0, 0);
424         btree_key_cache_flush_pos(&trans, key, seq, false);
425         bch2_trans_exit(&trans);
426 unlock:
427         srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
428 }
429
430 /*
431  * Flush and evict a key from the key cache:
432  */
433 int bch2_btree_key_cache_flush(struct btree_trans *trans,
434                                enum btree_id id, struct bpos pos)
435 {
436         struct bch_fs *c = trans->c;
437         struct bkey_cached_key key = { id, pos };
438
439         /* Fastpath - assume it won't be found: */
440         if (!bch2_btree_key_cache_find(c, id, pos))
441                 return 0;
442
443         return btree_key_cache_flush_pos(trans, key, 0, true);
444 }
445
446 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
447                                   struct btree_iter *iter,
448                                   struct bkey_i *insert)
449 {
450         struct bch_fs *c = trans->c;
451         struct bkey_cached *ck = (void *) iter->l[0].b;
452         bool kick_reclaim = false;
453
454         BUG_ON(insert->u64s > ck->u64s);
455
456         if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
457                 int difference;
458
459                 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
460
461                 difference = jset_u64s(insert->u64s) - ck->res.u64s;
462                 if (difference > 0) {
463                         trans->journal_preres.u64s      -= difference;
464                         ck->res.u64s                    += difference;
465                 }
466         }
467
468         bkey_copy(ck->k, insert);
469         ck->valid = true;
470
471         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
472                 mutex_lock(&c->btree_key_cache.lock);
473                 list_move(&ck->list, &c->btree_key_cache.dirty);
474
475                 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
476                 c->btree_key_cache.nr_dirty++;
477
478                 if (bch2_nr_btree_keys_need_flush(c))
479                         kick_reclaim = true;
480
481                 mutex_unlock(&c->btree_key_cache.lock);
482         }
483
484         bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
485                                 &ck->journal, btree_key_cache_journal_flush);
486
487         if (kick_reclaim)
488                 journal_reclaim_kick(&c->journal);
489         return true;
490 }
491
492 #ifdef CONFIG_BCACHEFS_DEBUG
493 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
494                                enum btree_id id, struct bpos pos)
495 {
496         BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
497 }
498 #endif
499
500 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
501                                            struct shrink_control *sc)
502 {
503         struct bch_fs *c = container_of(shrink, struct bch_fs,
504                                         btree_key_cache.shrink);
505         struct btree_key_cache *bc = &c->btree_key_cache;
506         struct bkey_cached *ck, *t;
507         size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
508         unsigned flags;
509
510         /* Return -1 if we can't do anything right now */
511         if (sc->gfp_mask & __GFP_FS)
512                 mutex_lock(&bc->lock);
513         else if (!mutex_trylock(&bc->lock))
514                 return -1;
515
516         flags = memalloc_nofs_save();
517
518         /*
519          * Newest freed entries are at the end of the list - once we hit one
520          * that's too new to be freed, we can bail out:
521          */
522         list_for_each_entry_safe(ck, t, &bc->freed, list) {
523                 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
524                                                  ck->btree_trans_barrier_seq))
525                         break;
526
527                 list_del(&ck->list);
528                 kmem_cache_free(bch2_key_cache, ck);
529                 bc->nr_freed--;
530                 scanned++;
531                 freed++;
532         }
533
534         if (scanned >= nr)
535                 goto out;
536
537         list_for_each_entry_safe(ck, t, &bc->clean, list) {
538                 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
539                         clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
540                 else if (bkey_cached_lock_for_evict(ck)) {
541                         bkey_cached_evict(bc, ck);
542                         bkey_cached_free(bc, ck);
543                 }
544
545                 scanned++;
546                 if (scanned >= nr) {
547                         if (&t->list != &bc->clean)
548                                 list_move_tail(&bc->clean, &t->list);
549                         goto out;
550                 }
551         }
552 out:
553         memalloc_nofs_restore(flags);
554         mutex_unlock(&bc->lock);
555
556         return freed;
557 }
558
559 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
560                                             struct shrink_control *sc)
561 {
562         struct bch_fs *c = container_of(shrink, struct bch_fs,
563                                         btree_key_cache.shrink);
564         struct btree_key_cache *bc = &c->btree_key_cache;
565
566         return bc->nr_keys;
567 }
568
569 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
570 {
571         struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
572         struct bkey_cached *ck, *n;
573
574         if (bc->shrink.list.next)
575                 unregister_shrinker(&bc->shrink);
576
577         mutex_lock(&bc->lock);
578         list_splice(&bc->dirty, &bc->clean);
579
580         list_for_each_entry_safe(ck, n, &bc->clean, list) {
581                 cond_resched();
582
583                 bch2_journal_pin_drop(&c->journal, &ck->journal);
584                 bch2_journal_preres_put(&c->journal, &ck->res);
585
586                 kfree(ck->k);
587                 list_del(&ck->list);
588                 kmem_cache_free(bch2_key_cache, ck);
589                 bc->nr_keys--;
590         }
591
592         BUG_ON(bc->nr_dirty && !bch2_journal_error(&c->journal));
593         BUG_ON(bc->nr_keys);
594
595         list_for_each_entry_safe(ck, n, &bc->freed, list) {
596                 cond_resched();
597
598                 list_del(&ck->list);
599                 kmem_cache_free(bch2_key_cache, ck);
600         }
601         mutex_unlock(&bc->lock);
602
603         if (bc->table_init_done)
604                 rhashtable_destroy(&bc->table);
605 }
606
607 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
608 {
609         mutex_init(&c->lock);
610         INIT_LIST_HEAD(&c->freed);
611         INIT_LIST_HEAD(&c->clean);
612         INIT_LIST_HEAD(&c->dirty);
613 }
614
615 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
616 {
617         int ret;
618
619         c->shrink.seeks                 = 1;
620         c->shrink.count_objects         = bch2_btree_key_cache_count;
621         c->shrink.scan_objects          = bch2_btree_key_cache_scan;
622
623         ret = register_shrinker(&c->shrink);
624         if (ret)
625                 return ret;
626
627         ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
628         if (ret)
629                 return ret;
630
631         c->table_init_done = true;
632         return 0;
633 }
634
635 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
636 {
637         pr_buf(out, "nr_freed:\t%zu\n", c->nr_freed);
638         pr_buf(out, "nr_keys:\t%zu\n",  c->nr_keys);
639         pr_buf(out, "nr_dirty:\t%zu\n", c->nr_dirty);
640 }
641
642 void bch2_btree_key_cache_exit(void)
643 {
644         if (bch2_key_cache)
645                 kmem_cache_destroy(bch2_key_cache);
646 }
647
648 int __init bch2_btree_key_cache_init(void)
649 {
650         bch2_key_cache = KMEM_CACHE(bkey_cached, 0);
651         if (!bch2_key_cache)
652                 return -ENOMEM;
653
654         return 0;
655 }