Skip to content

Commit 0a63b66

Browse files
author
Kent Overstreet
committed
bcache: Rework btree cache reserve handling
This changes the bucket allocation reserves to use _real_ reserves - separate freelists - instead of watermarks, which if nothing else makes the current code saner to reason about and is going to be important in the future when we add support for multiple btrees. It also adds btree_check_reserve(), which checks (and locks) the reserves for both bucket allocation and memory allocation for btree nodes; the old code just kinda sorta assumed that since (e.g. for btree node splits) it had the root locked and that meant no other threads could try to make use of the same reserve; this technically should have been ok for memory allocation (we should always have a reserve for memory allocation (the btree node cache is used as a reserve and we preallocate it)), but multiple btrees will mean that locking the root won't be sufficient anymore, and for the bucket allocation reserve it was technically possible for the old code to deadlock. Signed-off-by: Kent Overstreet <kmo@daterainc.com>
1 parent 56b3077 commit 0a63b66

6 files changed

Lines changed: 145 additions & 139 deletions

File tree

‎drivers/md/bcache/alloc.c‎

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ static int bch_allocator_thread(void *arg)
375375
}
376376

377377
allocator_wait(ca, bch_allocator_push(ca, bucket));
378+
wake_up(&ca->set->btree_cache_wait);
378379
wake_up(&ca->set->bucket_wait);
379380
}
380381

@@ -717,25 +718,3 @@ int bch_cache_allocator_start(struct cache *ca)
717718
ca->alloc_thread = k;
718719
return 0;
719720
}
720-
721-
int bch_cache_allocator_init(struct cache *ca)
722-
{
723-
/*
724-
* Reserve:
725-
* Prio/gen writes first
726-
* Then 8 for btree allocations
727-
* Then half for the moving garbage collector
728-
*/
729-
#if 0
730-
ca->watermark[WATERMARK_PRIO] = 0;
731-
732-
ca->watermark[WATERMARK_METADATA] = prio_buckets(ca);
733-
734-
ca->watermark[WATERMARK_MOVINGGC] = 8 +
735-
ca->watermark[WATERMARK_METADATA];
736-
737-
ca->watermark[WATERMARK_NONE] = ca->free.size / 2 +
738-
ca->watermark[WATERMARK_MOVINGGC];
739-
#endif
740-
return 0;
741-
}

‎drivers/md/bcache/bcache.h‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,16 @@ struct cache_set {
562562
struct list_head btree_cache_freed;
563563

564564
/* Number of elements in btree_cache + btree_cache_freeable lists */
565-
unsigned bucket_cache_used;
565+
unsigned btree_cache_used;
566566

567567
/*
568568
* If we need to allocate memory for a new btree node and that
569569
* allocation fails, we can cannibalize another node in the btree cache
570-
* to satisfy the allocation. However, only one thread can be doing this
571-
* at a time, for obvious reasons - try_harder and try_wait are
572-
* basically a lock for this that we can wait on asynchronously. The
573-
* btree_root() macro releases the lock when it returns.
570+
* to satisfy the allocation - lock to guarantee only one thread does
571+
* this at a time:
574572
*/
575-
struct task_struct *try_harder;
576-
wait_queue_head_t try_wait;
577-
uint64_t try_harder_start;
573+
wait_queue_head_t btree_cache_wait;
574+
struct task_struct *btree_cache_alloc_lock;
578575

579576
/*
580577
* When we free a btree node, we increment the gen of the bucket the
@@ -669,7 +666,6 @@ struct cache_set {
669666
struct time_stats btree_gc_time;
670667
struct time_stats btree_split_time;
671668
struct time_stats btree_read_time;
672-
struct time_stats try_harder_time;
673669

674670
atomic_long_t cache_read_races;
675671
atomic_long_t writeback_keys_done;
@@ -956,7 +952,6 @@ int bch_open_buckets_alloc(struct cache_set *);
956952
void bch_open_buckets_free(struct cache_set *);
957953

958954
int bch_cache_allocator_start(struct cache *ca);
959-
int bch_cache_allocator_init(struct cache *ca);
960955

961956
void bch_debug_exit(void);
962957
int bch_debug_init(struct kobject *);

0 commit comments

Comments
 (0)