From 3187aa8d57025f60f1b8f9e14b6fc33f5e2d2960 Mon Sep 17 00:00:00 2001
From: Kent Overstreet <kent.overstreet@gmail.com>
Date: Mon, 21 Dec 2020 17:17:18 -0500
Subject: [PATCH] bcachefs: Don't use BTREE_INSERT_USE_RESERVE so much

Previously, we were using BTREE_INSERT_RESERVE in a lot of places where
it no longer makes sense.

 - we now have more open_buckets than we used to, and the reserves work
   better, so we shouldn't need to use BTREE_INSERT_RESERVE just because
   we're holding open_buckets pinned anymore.

 - We have the btree key cache for updates to the alloc btree, meaning
   we no longer need the btree reserve to ensure the allocator can make
   forward progress.

This means that we should only need a reserve for btree updates to
ensure that copygc can make forward progress.

Since it's now just for copygc, we can also fold RESERVE_BTREE into
RESERVE_MOVINGGC (the allocator's freelist reserve).

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/alloc_background.c      | 10 +++-------
 fs/bcachefs/alloc_foreground.c      | 14 +-------------
 fs/bcachefs/alloc_types.h           |  8 +++-----
 fs/bcachefs/btree_gc.c              |  1 -
 fs/bcachefs/btree_key_cache.c       |  2 --
 fs/bcachefs/btree_update.h          |  2 --
 fs/bcachefs/btree_update_interior.c | 23 +++--------------------
 fs/bcachefs/btree_update_leaf.c     |  3 +--
 fs/bcachefs/buckets.c               |  3 +--
 fs/bcachefs/ec.c                    |  3 +--
 fs/bcachefs/io.c                    |  3 +--
 fs/bcachefs/journal.c               |  2 +-
 fs/bcachefs/move.c                  |  1 -
 fs/bcachefs/movinggc.c              |  5 +++++
 fs/bcachefs/sysfs.c                 |  2 --
 15 files changed, 20 insertions(+), 62 deletions(-)

diff --git a/fs/bcachefs/alloc_background.c b/fs/bcachefs/alloc_background.c
index 1ef695acc7d6..9920e902d383 100644
--- a/fs/bcachefs/alloc_background.c
+++ b/fs/bcachefs/alloc_background.c
@@ -319,9 +319,7 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
 	bch2_trans_update(trans, iter, &a->k_i,
 			  BTREE_TRIGGER_NORUN);
 	ret = bch2_trans_commit(trans, NULL, NULL,
-				BTREE_INSERT_NOFAIL|
-				BTREE_INSERT_USE_RESERVE|
-				flags);
+				BTREE_INSERT_NOFAIL|flags);
 err:
 	if (ret == -EINTR)
 		goto retry;
@@ -575,8 +573,7 @@ static int wait_buckets_available(struct bch_fs *c, struct bch_dev *ca)
 
 		if (available > fifo_free(&ca->free_inc) ||
 		    (available &&
-		     (!fifo_full(&ca->free[RESERVE_BTREE]) ||
-		      !fifo_full(&ca->free[RESERVE_MOVINGGC]))))
+		     !fifo_full(&ca->free[RESERVE_MOVINGGC])))
 			break;
 
 		up_read(&c->gc_lock);
@@ -977,8 +974,7 @@ static int bch2_invalidate_one_bucket2(struct btree_trans *trans,
 				BTREE_INSERT_NOUNLOCK|
 				BTREE_INSERT_NOCHECK_RW|
 				BTREE_INSERT_NOFAIL|
-				BTREE_INSERT_USE_RESERVE|
-				BTREE_INSERT_USE_ALLOC_RESERVE|
+				BTREE_INSERT_JOURNAL_RESERVED|
 				flags);
 	if (ret == -EINTR)
 		goto retry;
diff --git a/fs/bcachefs/alloc_foreground.c b/fs/bcachefs/alloc_foreground.c
index 82a49831afb7..1ea8ee99956b 100644
--- a/fs/bcachefs/alloc_foreground.c
+++ b/fs/bcachefs/alloc_foreground.c
@@ -204,10 +204,8 @@ long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
 {
 	switch (reserve) {
-	case RESERVE_ALLOC:
+	case RESERVE_MOVINGGC:
 		return 0;
-	case RESERVE_BTREE:
-		return OPEN_BUCKETS_COUNT / 4;
 	default:
 		return OPEN_BUCKETS_COUNT / 2;
 	}
@@ -263,16 +261,6 @@ struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
 		goto out;
 
 	switch (reserve) {
-	case RESERVE_ALLOC:
-		if (fifo_pop(&ca->free[RESERVE_BTREE], bucket))
-			goto out;
-		break;
-	case RESERVE_BTREE:
-		if (fifo_used(&ca->free[RESERVE_BTREE]) * 2 >=
-		    ca->free[RESERVE_BTREE].size &&
-		    fifo_pop(&ca->free[RESERVE_BTREE], bucket))
-			goto out;
-		break;
 	case RESERVE_MOVINGGC:
 		if (fifo_pop(&ca->free[RESERVE_MOVINGGC], bucket))
 			goto out;
diff --git a/fs/bcachefs/alloc_types.h b/fs/bcachefs/alloc_types.h
index 20705460bb0a..a510ca9a295b 100644
--- a/fs/bcachefs/alloc_types.h
+++ b/fs/bcachefs/alloc_types.h
@@ -37,11 +37,9 @@ struct bucket_clock {
 /* There is one reserve for each type of btree, one for prios and gens
  * and one for moving GC */
 enum alloc_reserve {
-	RESERVE_ALLOC		= -1,
-	RESERVE_BTREE		= 0,
-	RESERVE_MOVINGGC	= 1,
-	RESERVE_NONE		= 2,
-	RESERVE_NR		= 3,
+	RESERVE_MOVINGGC	= 0,
+	RESERVE_NONE		= 1,
+	RESERVE_NR		= 2,
 };
 
 typedef FIFO(long)	alloc_fifo;
diff --git a/fs/bcachefs/btree_gc.c b/fs/bcachefs/btree_gc.c
index 5f5686466d7d..8ab4c0df0d83 100644
--- a/fs/bcachefs/btree_gc.c
+++ b/fs/bcachefs/btree_gc.c
@@ -233,7 +233,6 @@ static int bch2_gc_btree(struct bch_fs *c, enum btree_id btree_id,
 			if (max_stale > 64)
 				bch2_btree_node_rewrite(c, iter,
 						b->data->keys.seq,
-						BTREE_INSERT_USE_RESERVE|
 						BTREE_INSERT_NOWAIT|
 						BTREE_INSERT_GC_LOCK_HELD);
 			else if (!bch2_btree_gc_rewrite_disabled &&
diff --git a/fs/bcachefs/btree_key_cache.c b/fs/bcachefs/btree_key_cache.c
index e6808d7139c6..6dc13fa3d1f4 100644
--- a/fs/bcachefs/btree_key_cache.c
+++ b/fs/bcachefs/btree_key_cache.c
@@ -350,8 +350,6 @@ static int btree_key_cache_flush_pos(struct btree_trans *trans,
 				  BTREE_INSERT_NOUNLOCK|
 				  BTREE_INSERT_NOCHECK_RW|
 				  BTREE_INSERT_NOFAIL|
-				  BTREE_INSERT_USE_RESERVE|
-				  BTREE_INSERT_USE_ALLOC_RESERVE|
 				  BTREE_INSERT_JOURNAL_RESERVED|
 				  BTREE_INSERT_JOURNAL_RECLAIM);
 err:
diff --git a/fs/bcachefs/btree_update.h b/fs/bcachefs/btree_update.h
index adb07043cbb3..a25138080169 100644
--- a/fs/bcachefs/btree_update.h
+++ b/fs/bcachefs/btree_update.h
@@ -20,7 +20,6 @@ enum btree_insert_flags {
 	__BTREE_INSERT_NOCHECK_RW,
 	__BTREE_INSERT_LAZY_RW,
 	__BTREE_INSERT_USE_RESERVE,
-	__BTREE_INSERT_USE_ALLOC_RESERVE,
 	__BTREE_INSERT_JOURNAL_REPLAY,
 	__BTREE_INSERT_JOURNAL_RESERVED,
 	__BTREE_INSERT_JOURNAL_RECLAIM,
@@ -43,7 +42,6 @@ enum btree_insert_flags {
 
 /* for copygc, or when merging btree nodes */
 #define BTREE_INSERT_USE_RESERVE	(1 << __BTREE_INSERT_USE_RESERVE)
-#define BTREE_INSERT_USE_ALLOC_RESERVE	(1 << __BTREE_INSERT_USE_ALLOC_RESERVE)
 
 /* Insert is for journal replay - don't get journal reservations: */
 #define BTREE_INSERT_JOURNAL_REPLAY	(1 << __BTREE_INSERT_JOURNAL_REPLAY)
diff --git a/fs/bcachefs/btree_update_interior.c b/fs/bcachefs/btree_update_interior.c
index 3ae920a223f9..6d69c7cb3665 100644
--- a/fs/bcachefs/btree_update_interior.c
+++ b/fs/bcachefs/btree_update_interior.c
@@ -201,12 +201,9 @@ static struct btree *__bch2_btree_node_alloc(struct bch_fs *c,
 	unsigned nr_reserve;
 	enum alloc_reserve alloc_reserve;
 
-	if (flags & BTREE_INSERT_USE_ALLOC_RESERVE) {
+	if (flags & BTREE_INSERT_USE_RESERVE) {
 		nr_reserve	= 0;
-		alloc_reserve	= RESERVE_ALLOC;
-	} else if (flags & BTREE_INSERT_USE_RESERVE) {
-		nr_reserve	= BTREE_NODE_RESERVE / 2;
-		alloc_reserve	= RESERVE_BTREE;
+		alloc_reserve	= RESERVE_MOVINGGC;
 	} else {
 		nr_reserve	= BTREE_NODE_RESERVE;
 		alloc_reserve	= RESERVE_NONE;
@@ -577,8 +574,6 @@ static void btree_update_nodes_written(struct btree_update *as)
 	bch2_trans_init(&trans, c, 0, 512);
 	ret = __bch2_trans_do(&trans, &as->disk_res, &journal_seq,
 			      BTREE_INSERT_NOFAIL|
-			      BTREE_INSERT_USE_RESERVE|
-			      BTREE_INSERT_USE_ALLOC_RESERVE|
 			      BTREE_INSERT_NOCHECK_RW|
 			      BTREE_INSERT_JOURNAL_RECLAIM|
 			      BTREE_INSERT_JOURNAL_RESERVED,
@@ -1457,15 +1452,6 @@ int bch2_btree_split_leaf(struct bch_fs *c, struct btree_iter *iter,
 	struct btree_update *as;
 	struct closure cl;
 	int ret = 0;
-	struct btree_insert_entry *i;
-
-	/*
-	 * We already have a disk reservation and open buckets pinned; this
-	 * allocation must not block:
-	 */
-	trans_for_each_update(trans, i)
-		if (btree_node_type_needs_gc(i->iter->btree_id))
-			flags |= BTREE_INSERT_USE_RESERVE;
 
 	closure_init_stack(&cl);
 
@@ -1926,10 +1912,7 @@ int bch2_btree_node_update_key(struct bch_fs *c, struct btree_iter *iter,
 retry:
 	as = bch2_btree_update_start(iter->trans, iter->btree_id,
 		parent ? btree_update_reserve_required(c, parent) : 0,
-		BTREE_INSERT_NOFAIL|
-		BTREE_INSERT_USE_RESERVE|
-		BTREE_INSERT_USE_ALLOC_RESERVE,
-		&cl);
+		BTREE_INSERT_NOFAIL, &cl);
 
 	if (IS_ERR(as)) {
 		ret = PTR_ERR(as);
diff --git a/fs/bcachefs/btree_update_leaf.c b/fs/bcachefs/btree_update_leaf.c
index a2ec2e58f9e4..a25cc3b7db39 100644
--- a/fs/bcachefs/btree_update_leaf.c
+++ b/fs/bcachefs/btree_update_leaf.c
@@ -1084,8 +1084,7 @@ int bch2_btree_delete_at(struct btree_trans *trans,
 
 	bch2_trans_update(trans, iter, &k, 0);
 	return bch2_trans_commit(trans, NULL, NULL,
-				 BTREE_INSERT_NOFAIL|
-				 BTREE_INSERT_USE_RESERVE|flags);
+				 BTREE_INSERT_NOFAIL|flags);
 }
 
 int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
diff --git a/fs/bcachefs/buckets.c b/fs/bcachefs/buckets.c
index 44d08434855d..31a2d3dbfe8f 100644
--- a/fs/bcachefs/buckets.c
+++ b/fs/bcachefs/buckets.c
@@ -2186,7 +2186,7 @@ int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
 			     ca->mi.bucket_size / c->opts.btree_node_size);
 	/* XXX: these should be tunable */
 	size_t reserve_none	= max_t(size_t, 1, nbuckets >> 9);
-	size_t copygc_reserve	= max_t(size_t, 2, nbuckets >> 7);
+	size_t copygc_reserve	= max_t(size_t, 2, nbuckets >> 6);
 	size_t free_inc_nr	= max(max_t(size_t, 1, nbuckets >> 12),
 				      btree_reserve * 2);
 	bool resize = ca->buckets[0] != NULL;
@@ -2203,7 +2203,6 @@ int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
 	    !(buckets_nouse	= kvpmalloc(BITS_TO_LONGS(nbuckets) *
 					    sizeof(unsigned long),
 					    GFP_KERNEL|__GFP_ZERO)) ||
-	    !init_fifo(&free[RESERVE_BTREE], btree_reserve, GFP_KERNEL) ||
 	    !init_fifo(&free[RESERVE_MOVINGGC],
 		       copygc_reserve, GFP_KERNEL) ||
 	    !init_fifo(&free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
index 95abc00bd0e0..76509c5970d2 100644
--- a/fs/bcachefs/ec.c
+++ b/fs/bcachefs/ec.c
@@ -800,8 +800,7 @@ static int ec_stripe_update_ptrs(struct bch_fs *c,
 		bch2_trans_update(&trans, iter, sk.k, 0);
 
 		ret = bch2_trans_commit(&trans, NULL, NULL,
-					BTREE_INSERT_NOFAIL|
-					BTREE_INSERT_USE_RESERVE);
+					BTREE_INSERT_NOFAIL);
 		if (ret == -EINTR)
 			ret = 0;
 		if (ret)
diff --git a/fs/bcachefs/io.c b/fs/bcachefs/io.c
index ee2ba1b8aff9..20c31176b131 100644
--- a/fs/bcachefs/io.c
+++ b/fs/bcachefs/io.c
@@ -330,8 +330,7 @@ int bch2_extent_update(struct btree_trans *trans,
 
 	ret = bch2_trans_commit(trans, disk_res, journal_seq,
 				BTREE_INSERT_NOCHECK_RW|
-				BTREE_INSERT_NOFAIL|
-				BTREE_INSERT_USE_RESERVE);
+				BTREE_INSERT_NOFAIL);
 	if (ret)
 		return ret;
 
diff --git a/fs/bcachefs/journal.c b/fs/bcachefs/journal.c
index 9c0de18930ac..be2c2d92384e 100644
--- a/fs/bcachefs/journal.c
+++ b/fs/bcachefs/journal.c
@@ -776,7 +776,7 @@ static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
 			}
 		} else {
 			rcu_read_lock();
-			ob = bch2_bucket_alloc(c, ca, RESERVE_ALLOC,
+			ob = bch2_bucket_alloc(c, ca, RESERVE_NONE,
 					       false, cl);
 			rcu_read_unlock();
 			if (IS_ERR(ob)) {
diff --git a/fs/bcachefs/move.c b/fs/bcachefs/move.c
index 1b1a14d2fa23..50b7363fe84b 100644
--- a/fs/bcachefs/move.c
+++ b/fs/bcachefs/move.c
@@ -167,7 +167,6 @@ static int bch2_migrate_index_update(struct bch_write_op *op)
 		ret = bch2_trans_commit(&trans, &op->res,
 				op_journal_seq(op),
 				BTREE_INSERT_NOFAIL|
-				BTREE_INSERT_USE_RESERVE|
 				m->data_opts.btree_insert_flags);
 		if (!ret)
 			atomic_long_inc(&c->extent_migrate_done);
diff --git a/fs/bcachefs/movinggc.c b/fs/bcachefs/movinggc.c
index f0cfd109a022..659dcfb2cca1 100644
--- a/fs/bcachefs/movinggc.c
+++ b/fs/bcachefs/movinggc.c
@@ -200,6 +200,11 @@ static int bch2_copygc(struct bch_fs *c)
 		return -1;
 	}
 
+	/*
+	 * Our btree node allocations also come out of RESERVE_MOVINGGC:
+	 */
+	sectors_to_move = (sectors_to_move * 3) / 4;
+
 	for (i = h->data; i < h->data + h->used; i++)
 		sectors_to_move += i->sectors * i->replicas;
 
diff --git a/fs/bcachefs/sysfs.c b/fs/bcachefs/sysfs.c
index afe0238d0cc0..aa58c595c5cb 100644
--- a/fs/bcachefs/sysfs.c
+++ b/fs/bcachefs/sysfs.c
@@ -798,7 +798,6 @@ static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
 
 	pr_buf(out,
 		"free_inc:               %zu/%zu\n"
-		"free[RESERVE_BTREE]:    %zu/%zu\n"
 		"free[RESERVE_MOVINGGC]: %zu/%zu\n"
 		"free[RESERVE_NONE]:     %zu/%zu\n"
 		"buckets:\n"
@@ -826,7 +825,6 @@ static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
 		"open_buckets_user:      %u\n"
 		"btree reserve cache:    %u\n",
 		fifo_used(&ca->free_inc),		ca->free_inc.size,
-		fifo_used(&ca->free[RESERVE_BTREE]),	ca->free[RESERVE_BTREE].size,
 		fifo_used(&ca->free[RESERVE_MOVINGGC]),	ca->free[RESERVE_MOVINGGC].size,
 		fifo_used(&ca->free[RESERVE_NONE]),	ca->free[RESERVE_NONE].size,
 		ca->mi.nbuckets - ca->mi.first_bucket,
-- 
2.30.9