Skip to contentSkip to conversation

bcache-remove-heap-related-macros-and-switch-to-generic-min_heap.patch removed from -mm tree

merged mm-nonmm-stable Andrew Morton 1 participant 1 message Jun 25, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: bcache: remove heap-related macros and switch to generic min_heap
has been removed from the -mm tree. Its filename was
bcache-remove-heap-related-macros-and-switch-to-generic-min_heap.patch

This patch was dropped because it was merged into the mm-nonmm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Kuan-Wei Chiu <[email protected]>
Subject: bcache: remove heap-related macros and switch to generic min_heap
Date: Fri, 24 May 2024 23:29:57 +0800

Drop the heap-related macros from bcache and replacing them with the generic min_heap implementation from include/linux. By doing so, code readability is improved by using functions instead of macros. Moreover, the min_heap implementation in include/linux adopts a bottom-up variation compared to the textbook version currently used in bcache. This bottom-up variation allows for approximately 50% reduction in the number of comparison operations during heap siftdown, without changing the number of swaps, thus making it more efficient.

---

 drivers/md/bcache/alloc.c     |   64 ++++++++++++----
 drivers/md/bcache/bcache.h    |    2 
 drivers/md/bcache/bset.c      |  124 ++++++++++++++++++++------------
 drivers/md/bcache/bset.h      |   42 ++++------
 drivers/md/bcache/btree.c     |   69 ++++++++++-------
 drivers/md/bcache/extents.c   |   51 ++++++++-----
 drivers/md/bcache/movinggc.c  |   41 ++++++++--
 drivers/md/bcache/super.c     |    3 
 drivers/md/bcache/sysfs.c     |    4 -
 drivers/md/bcache/util.h      |   67 -----------------
 drivers/md/bcache/writeback.c |   13 ++-
 11 files changed, 263 insertions(+), 217 deletions(-)
drivers/md/bcache/alloc.c renamed from drivers/md/bcache/alloc.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +47 -17
@@ -164,40 +164,68 @@ static void bch_invalidate_one_bucket(st
  * prio is worth 1/8th of what INITIAL_PRIO is worth.
  */
 
-#define bucket_prio(b)							\
-({									\
-	unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8;	\
-									\
-	(b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b);	\
-})
+static inline unsigned int new_bucket_prio(struct cache *ca, struct bucket *b)
+{
+	unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8;
+
+	return (b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b);
+}
+
+static inline bool new_bucket_max_cmp(const void *l, const void *r, void *args)
+{
+	struct bucket **lhs = (struct bucket **)l;
+	struct bucket **rhs = (struct bucket **)r;
+	struct cache *ca = args;
+
+	return new_bucket_prio(ca, *lhs) > new_bucket_prio(ca, *rhs);
+}
+
+static inline bool new_bucket_min_cmp(const void *l, const void *r, void *args)
+{
+	struct bucket **lhs = (struct bucket **)l;
+	struct bucket **rhs = (struct bucket **)r;
+	struct cache *ca = args;
+
+	return new_bucket_prio(ca, *lhs) < new_bucket_prio(ca, *rhs);
+}
+
+static inline void new_bucket_swap(void *l, void *r, void __always_unused *args)
+{
+	struct bucket **lhs = l, **rhs = r;
 
-#define bucket_max_cmp(l, r)	(bucket_prio(l) < bucket_prio(r))
-#define bucket_min_cmp(l, r)	(bucket_prio(l) > bucket_prio(r))
+	swap(*lhs, *rhs);
+}
 
 static void invalidate_buckets_lru(struct cache *ca)
 {
 	struct bucket *b;
-	ssize_t i;
+	const struct min_heap_callbacks bucket_max_cmp_callback = {
+		.less = new_bucket_max_cmp,
+		.swp = new_bucket_swap,
+	};
+	const struct min_heap_callbacks bucket_min_cmp_callback = {
+		.less = new_bucket_min_cmp,
+		.swp = new_bucket_swap,
+	};
 
-	ca->heap.used = 0;
+	ca->heap.nr = 0;
 
 	for_each_bucket(b, ca) {
 		if (!bch_can_invalidate_bucket(ca, b))
 			continue;
 
-		if (!heap_full(&ca->heap))
-			heap_add(&ca->heap, b, bucket_max_cmp);
-		else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
+		if (!min_heap_full(&ca->heap))
+			min_heap_push(&ca->heap, &b, &bucket_max_cmp_callback, ca);
+		else if (!new_bucket_max_cmp(&b, min_heap_peek(&ca->heap), ca)) {
 			ca->heap.data[0] = b;
-			heap_sift(&ca->heap, 0, bucket_max_cmp);
+			min_heap_sift_down(&ca->heap, 0, &bucket_max_cmp_callback, ca);
 		}
 	}
 
-	for (i = ca->heap.used / 2 - 1; i >= 0; --i)
-		heap_sift(&ca->heap, i, bucket_min_cmp);
+	min_heapify_all(&ca->heap, &bucket_min_cmp_callback, ca);
 
 	while (!fifo_full(&ca->free_inc)) {
-		if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
+		if (!ca->heap.nr) {
 			/*
 			 * We don't want to be calling invalidate_buckets()
 			 * multiple times when it can't do anything
@@ -206,6 +234,8 @@ static void invalidate_buckets_lru(struc
 			wake_up_gc(ca->set);
 			return;
 		}
+		b = min_heap_peek(&ca->heap)[0];
+		min_heap_pop(&ca->heap, &bucket_min_cmp_callback, ca);
 
 		bch_invalidate_one_bucket(ca, b);
 	}
drivers/md/bcache/bcache.h renamed from drivers/md/bcache/bcache.h~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +1 -1
@@ -458,7 +458,7 @@ struct cache {
 	/* Allocation stuff: */
 	struct bucket		*buckets;
 
-	DECLARE_HEAP(struct bucket *, heap);
+	DEFINE_MIN_HEAP(struct bucket *, cache_heap) heap;
 
 	/*
 	 * If nonzero, we know we aren't going to find any buckets to invalidate
drivers/md/bcache/bset.c renamed from drivers/md/bcache/bset.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +79 -45

257 lines not on this page · raw message

drivers/md/bcache/bset.h renamed from drivers/md/bcache/bset.h~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +18 -24

72 lines not on this page · raw message

drivers/md/bcache/btree.c renamed from drivers/md/bcache/btree.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +40 -29

188 lines not on this page · raw message

drivers/md/bcache/extents.c renamed from drivers/md/bcache/extents.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +33 -18

105 lines not on this page · raw message

drivers/md/bcache/movinggc.c renamed from drivers/md/bcache/movinggc.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +31 -10

85 lines not on this page · raw message

drivers/md/bcache/super.c renamed from drivers/md/bcache/super.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +1 -2
@@ -1914,8 +1914,7 @@ struct cache_set *bch_cache_set_alloc(st
 	INIT_LIST_HEAD(&c->btree_cache_freed);
 	INIT_LIST_HEAD(&c->data_buckets);
 
-	iter_size = sizeof(struct btree_iter) +
-		    ((meta_bucket_pages(sb) * PAGE_SECTORS) / sb->block_size) *
+	iter_size = ((meta_bucket_pages(sb) * PAGE_SECTORS) / sb->block_size) *
 			    sizeof(struct btree_iter_set);
 
 	c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL);
drivers/md/bcache/sysfs.c renamed from drivers/md/bcache/sysfs.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +3 -1
@@ -660,7 +660,9 @@ static unsigned int bch_root_usage(struc
 	unsigned int bytes = 0;
 	struct bkey *k;
 	struct btree *b;
-	struct btree_iter_stack iter;
+	struct btree_iter iter;
+
+	min_heap_init(&iter.heap, NULL, MAX_BSETS);
 
 	goto lock_root;
 
drivers/md/bcache/util.h renamed from drivers/md/bcache/util.h~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +2 -65

88 lines not on this page · raw message

drivers/md/bcache/writeback.c renamed from drivers/md/bcache/writeback.c~bcache-remove-heap-related-macros-and-switch-to-generic-min_heap +8 -5
@@ -908,15 +908,16 @@ static int bch_dirty_init_thread(void *a
 	struct dirty_init_thrd_info *info = arg;
 	struct bch_dirty_init_state *state = info->state;
 	struct cache_set *c = state->c;
-	struct btree_iter_stack iter;
+	struct btree_iter iter;
 	struct bkey *k, *p;
 	int cur_idx, prev_idx, skip_nr;
 
 	k = p = NULL;
 	prev_idx = 0;
 
-	bch_btree_iter_stack_init(&c->root->keys, &iter, NULL);
-	k = bch_btree_iter_next_filter(&iter.iter, &c->root->keys, bch_ptr_bad);
+	min_heap_init(&iter.heap, NULL, MAX_BSETS);
+	bch_btree_iter_init(&c->root->keys, &iter, NULL);
+	k = bch_btree_iter_next_filter(&iter, &c->root->keys, bch_ptr_bad);
 	BUG_ON(!k);
 
 	p = k;
@@ -930,7 +931,7 @@ static int bch_dirty_init_thread(void *a
 		skip_nr = cur_idx - prev_idx;
 
 		while (skip_nr) {
-			k = bch_btree_iter_next_filter(&iter.iter,
+			k = bch_btree_iter_next_filter(&iter,
 						       &c->root->keys,
 						       bch_ptr_bad);
 			if (k)
@@ -979,11 +980,13 @@ void bch_sectors_dirty_init(struct bcach
 	int i;
 	struct btree *b = NULL;
 	struct bkey *k = NULL;
-	struct btree_iter_stack iter;
+	struct btree_iter iter;
 	struct sectors_dirty_init op;
 	struct cache_set *c = d->c;
 	struct bch_dirty_init_state state;
 
+	min_heap_init(&iter.heap, NULL, MAX_BSETS);
+
 retry_lock:
 	b = c->root;
 	rw_lock(0, b, b->level);

_

Patches currently in -mm which might be from [email protected] are