Lines Matching +full:wait +full:- +full:pin

1 // SPDX-License-Identifier: GPL-2.0
10 * btree pointers - they must match for the pointer to be considered valid.
19 * When we invalidate a bucket, we have to write its new gen to disk and wait
20 * for that write to complete before we use it - otherwise after a crash we
29 * free_inc isn't the only freelist - if it was, we'd often to sleep while
30 * priorities and gens were being written before we could allocate. c->free is a
37 * have nothing pointing into them - these we can reuse without waiting for
40 * them (because they were overwritten). That's the unused list - buckets on the
43 * It's also important to ensure that gens don't wrap around - with respect to
45 * difficult to do in practice, but we explicitly guard against it anyways - if
60 * invalidated, and then invalidate them and stick them on the free_inc list -
78 uint8_t ret = ++b->gen; in bch_inc_gen()
80 ca->set->need_gc = max(ca->set->need_gc, bucket_gc_gen(b)); in bch_inc_gen()
81 WARN_ON_ONCE(ca->set->need_gc > BUCKET_GC_GEN_MAX); in bch_inc_gen()
90 unsigned long next = c->nbuckets * c->cache->sb.bucket_size / 1024; in bch_rescale_priorities()
93 atomic_sub(sectors, &c->rescale); in bch_rescale_priorities()
96 r = atomic_read(&c->rescale); in bch_rescale_priorities()
100 } while (atomic_cmpxchg(&c->rescale, r, r + next) != r); in bch_rescale_priorities()
102 mutex_lock(&c->bucket_lock); in bch_rescale_priorities()
104 c->min_prio = USHRT_MAX; in bch_rescale_priorities()
106 ca = c->cache; in bch_rescale_priorities()
108 if (b->prio && in bch_rescale_priorities()
109 b->prio != BTREE_PRIO && in bch_rescale_priorities()
110 !atomic_read(&b->pin)) { in bch_rescale_priorities()
111 b->prio--; in bch_rescale_priorities()
112 c->min_prio = min(c->min_prio, b->prio); in bch_rescale_priorities()
115 mutex_unlock(&c->bucket_lock); in bch_rescale_priorities()
132 BUG_ON(!ca->set->gc_mark_valid); in bch_can_invalidate_bucket()
136 !atomic_read(&b->pin) && in bch_can_invalidate_bucket()
142 lockdep_assert_held(&ca->set->bucket_lock); in __bch_invalidate_one_bucket()
146 trace_bcache_invalidate(ca, b - ca->buckets); in __bch_invalidate_one_bucket()
149 b->prio = INITIAL_PRIO; in __bch_invalidate_one_bucket()
150 atomic_inc(&b->pin); in __bch_invalidate_one_bucket()
157 fifo_push(&ca->free_inc, b - ca->buckets); in bch_invalidate_one_bucket()
171 unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
173 (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b); \
184 ca->heap.used = 0; in invalidate_buckets_lru()
190 if (!heap_full(&ca->heap)) in invalidate_buckets_lru()
191 heap_add(&ca->heap, b, bucket_max_cmp); in invalidate_buckets_lru()
192 else if (bucket_max_cmp(b, heap_peek(&ca->heap))) { in invalidate_buckets_lru()
193 ca->heap.data[0] = b; in invalidate_buckets_lru()
194 heap_sift(&ca->heap, 0, bucket_max_cmp); in invalidate_buckets_lru()
198 for (i = ca->heap.used / 2 - 1; i >= 0; --i) in invalidate_buckets_lru()
199 heap_sift(&ca->heap, i, bucket_min_cmp); in invalidate_buckets_lru()
201 while (!fifo_full(&ca->free_inc)) { in invalidate_buckets_lru()
202 if (!heap_pop(&ca->heap, b, bucket_min_cmp)) { in invalidate_buckets_lru()
207 ca->invalidate_needs_gc = 1; in invalidate_buckets_lru()
208 wake_up_gc(ca->set); in invalidate_buckets_lru()
221 while (!fifo_full(&ca->free_inc)) { in invalidate_buckets_fifo()
222 if (ca->fifo_last_bucket < ca->sb.first_bucket || in invalidate_buckets_fifo()
223 ca->fifo_last_bucket >= ca->sb.nbuckets) in invalidate_buckets_fifo()
224 ca->fifo_last_bucket = ca->sb.first_bucket; in invalidate_buckets_fifo()
226 b = ca->buckets + ca->fifo_last_bucket++; in invalidate_buckets_fifo()
231 if (++checked >= ca->sb.nbuckets) { in invalidate_buckets_fifo()
232 ca->invalidate_needs_gc = 1; in invalidate_buckets_fifo()
233 wake_up_gc(ca->set); in invalidate_buckets_fifo()
244 while (!fifo_full(&ca->free_inc)) { in invalidate_buckets_random()
249 n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket); in invalidate_buckets_random()
250 n += ca->sb.first_bucket; in invalidate_buckets_random()
252 b = ca->buckets + n; in invalidate_buckets_random()
257 if (++checked >= ca->sb.nbuckets / 2) { in invalidate_buckets_random()
258 ca->invalidate_needs_gc = 1; in invalidate_buckets_random()
259 wake_up_gc(ca->set); in invalidate_buckets_random()
267 BUG_ON(ca->invalidate_needs_gc); in invalidate_buckets()
269 switch (CACHE_REPLACEMENT(&ca->sb)) { in invalidate_buckets()
289 mutex_unlock(&(ca)->set->bucket_lock); \
291 test_bit(CACHE_SET_IO_DISABLE, &ca->set->flags)) { \
297 mutex_lock(&(ca)->set->bucket_lock); \
307 if (fifo_push(&ca->free[RESERVE_PRIO], bucket)) in bch_allocator_push()
311 if (fifo_push(&ca->free[i], bucket)) in bch_allocator_push()
321 mutex_lock(&ca->set->bucket_lock); in bch_allocator_thread()
332 if (!fifo_pop(&ca->free_inc, bucket)) in bch_allocator_thread()
335 if (ca->discard) { in bch_allocator_thread()
336 mutex_unlock(&ca->set->bucket_lock); in bch_allocator_thread()
337 blkdev_issue_discard(ca->bdev, in bch_allocator_thread()
338 bucket_to_sector(ca->set, bucket), in bch_allocator_thread()
339 ca->sb.bucket_size, GFP_KERNEL); in bch_allocator_thread()
340 mutex_lock(&ca->set->bucket_lock); in bch_allocator_thread()
344 wake_up(&ca->set->btree_cache_wait); in bch_allocator_thread()
345 wake_up(&ca->set->bucket_wait); in bch_allocator_thread()
355 allocator_wait(ca, ca->set->gc_mark_valid && in bch_allocator_thread()
356 !ca->invalidate_needs_gc); in bch_allocator_thread()
363 allocator_wait(ca, !atomic_read(&ca->set->prio_blocked)); in bch_allocator_thread()
364 if (CACHE_SYNC(&ca->sb)) { in bch_allocator_thread()
367 * node locked ever blocked - having the btree node in bch_allocator_thread()
376 if (!fifo_full(&ca->free_inc)) in bch_allocator_thread()
380 ca->invalidate_needs_gc = 1; in bch_allocator_thread()
381 wake_up_gc(ca->set); in bch_allocator_thread()
392 long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait) in bch_bucket_alloc() argument
400 if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &ca->set->flags))) in bch_bucket_alloc()
401 return -1; in bch_bucket_alloc()
404 if (fifo_pop(&ca->free[RESERVE_NONE], r) || in bch_bucket_alloc()
405 fifo_pop(&ca->free[reserve], r)) in bch_bucket_alloc()
408 if (!wait) { in bch_bucket_alloc()
410 return -1; in bch_bucket_alloc()
414 prepare_to_wait(&ca->set->bucket_wait, &w, in bch_bucket_alloc()
417 mutex_unlock(&ca->set->bucket_lock); in bch_bucket_alloc()
419 mutex_lock(&ca->set->bucket_lock); in bch_bucket_alloc()
420 } while (!fifo_pop(&ca->free[RESERVE_NONE], r) && in bch_bucket_alloc()
421 !fifo_pop(&ca->free[reserve], r)); in bch_bucket_alloc()
423 finish_wait(&ca->set->bucket_wait, &w); in bch_bucket_alloc()
425 if (ca->alloc_thread) in bch_bucket_alloc()
426 wake_up_process(ca->alloc_thread); in bch_bucket_alloc()
430 if (expensive_debug_checks(ca->set)) { in bch_bucket_alloc()
436 BUG_ON(ca->prio_buckets[iter] == (uint64_t) r); in bch_bucket_alloc()
439 fifo_for_each(i, &ca->free[j], iter) in bch_bucket_alloc()
441 fifo_for_each(i, &ca->free_inc, iter) in bch_bucket_alloc()
445 b = ca->buckets + r; in bch_bucket_alloc()
447 BUG_ON(atomic_read(&b->pin) != 1); in bch_bucket_alloc()
449 SET_GC_SECTORS_USED(b, ca->sb.bucket_size); in bch_bucket_alloc()
454 b->prio = BTREE_PRIO; in bch_bucket_alloc()
458 b->prio = INITIAL_PRIO; in bch_bucket_alloc()
461 if (ca->set->avail_nbuckets > 0) { in bch_bucket_alloc()
462 ca->set->avail_nbuckets--; in bch_bucket_alloc()
463 bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); in bch_bucket_alloc()
474 if (ca->set->avail_nbuckets < ca->set->nbuckets) { in __bch_bucket_free()
475 ca->set->avail_nbuckets++; in __bch_bucket_free()
476 bch_update_bucket_in_use(ca->set, &ca->set->gc_stats); in __bch_bucket_free()
485 __bch_bucket_free(c->cache, PTR_BUCKET(c, k, i)); in bch_bucket_free()
489 struct bkey *k, bool wait) in __bch_bucket_alloc_set() argument
495 if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags))) in __bch_bucket_alloc_set()
496 return -1; in __bch_bucket_alloc_set()
498 lockdep_assert_held(&c->bucket_lock); in __bch_bucket_alloc_set()
502 ca = c->cache; in __bch_bucket_alloc_set()
503 b = bch_bucket_alloc(ca, reserve, wait); in __bch_bucket_alloc_set()
504 if (b == -1) in __bch_bucket_alloc_set()
507 k->ptr[0] = MAKE_PTR(ca->buckets[b].gen, in __bch_bucket_alloc_set()
509 ca->sb.nr_this_dev); in __bch_bucket_alloc_set()
517 return -1; in __bch_bucket_alloc_set()
521 struct bkey *k, bool wait) in bch_bucket_alloc_set() argument
525 mutex_lock(&c->bucket_lock); in bch_bucket_alloc_set()
526 ret = __bch_bucket_alloc_set(c, reserve, k, wait); in bch_bucket_alloc_set()
527 mutex_unlock(&c->bucket_lock); in bch_bucket_alloc_set()
572 list_for_each_entry_reverse(ret, &c->data_buckets, list) in pick_data_bucket()
573 if (UUID_FLASH_ONLY(&c->uuids[KEY_INODE(&ret->key)]) != in pick_data_bucket()
574 UUID_FLASH_ONLY(&c->uuids[KEY_INODE(search)])) in pick_data_bucket()
576 else if (!bkey_cmp(&ret->key, search)) in pick_data_bucket()
578 else if (ret->last_write_point == write_point) in pick_data_bucket()
581 ret = ret_task ?: list_first_entry(&c->data_buckets, in pick_data_bucket()
584 if (!ret->sectors_free && KEY_PTRS(alloc)) { in pick_data_bucket()
585 ret->sectors_free = c->cache->sb.bucket_size; in pick_data_bucket()
586 bkey_copy(&ret->key, alloc); in pick_data_bucket()
590 if (!ret->sectors_free) in pick_data_bucket()
604 * If s->writeback is true, will not fail.
611 bool wait) in bch_alloc_sectors() argument
625 spin_lock(&c->data_bucket_lock); in bch_alloc_sectors()
632 spin_unlock(&c->data_bucket_lock); in bch_alloc_sectors()
634 if (bch_bucket_alloc_set(c, watermark, &alloc.key, wait)) in bch_alloc_sectors()
637 spin_lock(&c->data_bucket_lock); in bch_alloc_sectors()
648 for (i = 0; i < KEY_PTRS(&b->key); i++) in bch_alloc_sectors()
649 EBUG_ON(ptr_stale(c, &b->key, i)); in bch_alloc_sectors()
653 for (i = 0; i < KEY_PTRS(&b->key); i++) in bch_alloc_sectors()
654 k->ptr[i] = b->key.ptr[i]; in bch_alloc_sectors()
656 sectors = min(sectors, b->sectors_free); in bch_alloc_sectors()
660 SET_KEY_PTRS(k, KEY_PTRS(&b->key)); in bch_alloc_sectors()
666 list_move_tail(&b->list, &c->data_buckets); in bch_alloc_sectors()
667 bkey_copy_key(&b->key, k); in bch_alloc_sectors()
668 b->last_write_point = write_point; in bch_alloc_sectors()
670 b->sectors_free -= sectors; in bch_alloc_sectors()
672 for (i = 0; i < KEY_PTRS(&b->key); i++) { in bch_alloc_sectors()
673 SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors); in bch_alloc_sectors()
676 &c->cache->sectors_written); in bch_alloc_sectors()
679 if (b->sectors_free < c->cache->sb.block_size) in bch_alloc_sectors()
680 b->sectors_free = 0; in bch_alloc_sectors()
687 if (b->sectors_free) in bch_alloc_sectors()
688 for (i = 0; i < KEY_PTRS(&b->key); i++) in bch_alloc_sectors()
689 atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin); in bch_alloc_sectors()
691 spin_unlock(&c->data_bucket_lock); in bch_alloc_sectors()
701 while (!list_empty(&c->data_buckets)) { in bch_open_buckets_free()
702 b = list_first_entry(&c->data_buckets, in bch_open_buckets_free()
704 list_del(&b->list); in bch_open_buckets_free()
713 spin_lock_init(&c->data_bucket_lock); in bch_open_buckets_alloc()
719 return -ENOMEM; in bch_open_buckets_alloc()
721 list_add(&b->list, &c->data_buckets); in bch_open_buckets_alloc()
734 ca->alloc_thread = k; in bch_cache_allocator_start()