All of lore.kernel.org
 help / color / mirror / Atom feed
From: Coly Li <colyli@suse.de>
To: axboe@kernel.dk
Cc: linux-bcache@vger.kernel.org, linux-block@vger.kernel.org,
	Coly Li <colyli@suse.de>
Subject: [PATCH 01/10] bcache: fix fifo index swapping condition in journal_pin_cmp()
Date: Wed, 13 Nov 2019 13:33:37 +0800	[thread overview]
Message-ID: <20191113053346.63536-2-colyli@suse.de> (raw)
In-Reply-To: <20191113053346.63536-1-colyli@suse.de>

Fifo structure journal.pin is implemented by a cycle buffer, if the back
index reaches highest location of the cycle buffer, it will be swapped
to 0. Once the swapping happens, it means a smaller fifo index might be
associated to a newer journal entry. So the btree node with oldest
journal entry won't be selected in bch_btree_leaf_dirty() to reference
the dirty B+tree leaf node. This problem may cause bcache journal won't
protect unflushed oldest B+tree dirty leaf node in power failure, and
this B+tree leaf node is possible to beinconsistent after reboot from
power failure.

This patch fixes the fifo index comparing logic in journal_pin_cmp(),
to avoid potential corrupted B+tree leaf node when the back index of
journal pin is swapped.

Signed-off-by: Coly Li <colyli@suse.de>
---
 drivers/md/bcache/btree.c   | 26 ++++++++++++++++++++++++++
 drivers/md/bcache/journal.h |  4 ----
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index ba434d9ac720..00523cd1db80 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -528,6 +528,32 @@ static void btree_node_write_work(struct work_struct *w)
 	mutex_unlock(&b->write_lock);
 }
 
+/* return true if journal pin 'l' is newer than 'r' */
+static bool journal_pin_cmp(struct cache_set *c,
+			    atomic_t *l,
+			    atomic_t *r)
+{
+	int l_idx, r_idx, f_idx, b_idx;
+	bool ret = false;
+
+	l_idx = fifo_idx(&(c)->journal.pin, (l));
+	r_idx = fifo_idx(&(c)->journal.pin, (r));
+	f_idx = (c)->journal.pin.front;
+	b_idx = (c)->journal.pin.back;
+
+	if (l_idx > r_idx)
+		ret = true;
+	/* in case fifo back pointer is swapped */
+	if (b_idx < f_idx) {
+		if (l_idx <= b_idx && r_idx >= f_idx)
+			ret = true;
+		else if (l_idx >= f_idx && r_idx <= b_idx)
+			ret = false;
+	}
+
+	return ret;
+}
+
 static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref)
 {
 	struct bset *i = btree_bset_last(b);
diff --git a/drivers/md/bcache/journal.h b/drivers/md/bcache/journal.h
index f2ea34d5f431..06b3eaab7d16 100644
--- a/drivers/md/bcache/journal.h
+++ b/drivers/md/bcache/journal.h
@@ -157,10 +157,6 @@ struct journal_device {
 };
 
 #define BTREE_FLUSH_NR	8
-
-#define journal_pin_cmp(c, l, r)				\
-	(fifo_idx(&(c)->journal.pin, (l)) > fifo_idx(&(c)->journal.pin, (r)))
-
 #define JOURNAL_PIN	20000
 
 #define journal_full(j)						\
-- 
2.16.4


  reply	other threads:[~2019-11-13  5:34 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-13  5:33 [PATCH 00/10] bcache patches for Linux v5.5 Coly Li
2019-11-13  5:33 ` Coly Li [this message]
2019-11-17  3:27   ` [PATCH 01/10] bcache: fix fifo index swapping condition in journal_pin_cmp() Eric Wheeler
2019-11-13  5:33 ` [PATCH 02/10] bcache: fix a lost wake-up problem caused by mca_cannibalize_lock Coly Li
2019-11-13  5:33 ` [PATCH 03/10] bcache: fix static checker warning in bcache_device_free() Coly Li
2019-11-13  5:33 ` [PATCH 04/10] bcache: add more accurate error messages in read_super() Coly Li
2019-11-13  5:33 ` [PATCH 05/10] bcache: deleted code comments for dead code in bch_data_insert_keys() Coly Li
2019-11-13  5:33 ` [PATCH 06/10] bcache: add code comment bch_keylist_pop() and bch_keylist_pop_front() Coly Li
2019-11-13  5:33 ` [PATCH 07/10] bcache: fix deadlock in bcache_allocator Coly Li
2019-11-13  5:33 ` [PATCH 08/10] bcache: add code comments in bch_btree_leaf_dirty() Coly Li
2019-11-13  5:33 ` [PATCH 09/10] bcache: add idle_max_writeback_rate sysfs interface Coly Li
2019-11-13  5:33 ` [PATCH 10/10] bcache: at least try to shrink 1 node in bch_mca_scan() Coly Li
2019-11-13  7:19 ` [PATCH 00/10] bcache patches for Linux v5.5 Christoph Hellwig
2019-11-13  8:04   ` Coly Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191113053346.63536-2-colyli@suse.de \
    --to=colyli@suse.de \
    --cc=axboe@kernel.dk \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.