Skip to contentSkip to conversation

md/raid1: serialize overlap io for writemostly disk

PATCH Applied Xiao Ni 2 participants 2 messages Mar 24 – Apr 7 MD RAID

lore
Share & tools
lore

Xiao Ni

author
#0 lore raw

PATCH v3 1/1 md/raid1: serialize overlap io for writemostly disk

Previously, using wait_event() would wake up all waiters simultaneously, and they would compete for the tree lock. The bio which gets the lock first will be handled, so the write sequence cannot be guaranteed.

For example:
bio1(100,200)
bio2(150,200)
bio3(150,300)

The write sequence of fast device is bio1,bio2,bio3. But the write sequence of slow device could be bio1,bio3,bio2 due to lock competition. This causes data corruption.

Replace waitqueue with a fifo list to guarantee the write sequence. And it also needs to iterate the list when removing one entry. If not, it may miss the opportunity to wake up the waiting io.

For example:
bio1(1,3), bio2(2,4)
bio3(5,7), bio4(6,8)
These four bios are in the same bucket. bio1 and bio3 are inserted into the rbtree. bio2 and bio4 are added to the waiting list and bio2 is the first one. bio3 returns from slow disk and tries to wake up the waiting bios. bio2 is removed from the list and will be handled. But bio1 hasn't finished. So bio2 will be added into waiting list again. Then bio1 returns from slow disk and wakes up waiting bios. bio4 is removed from the list and will be handled. Now bio1, bio3 and bio4 all finish and bio2 is left on the waiting list. So it needs to iterate the waiting list to wake up the right bio.

---
v2: use prepare_to_wait_exclusive
v3: return back to self managed fifo list to find the right waiting node

 drivers/md/md.c    |  1 -
 drivers/md/md.h    |  5 ++++-
 drivers/md/raid1.c | 45 ++++++++++++++++++++++++++++++++++-----------
 3 files changed, 38 insertions(+), 13 deletions(-)
drivers/md/md.c +0 -1
index 521d9b34cd9e..3348224e36f8 100644
@@ -188,7 +188,6 @@ static int rdev_init_serial(struct md_rdev *rdev)
 
 		spin_lock_init(&serial_tmp->serial_lock);
 		serial_tmp->serial_rb = RB_ROOT_CACHED;
-		init_waitqueue_head(&serial_tmp->serial_io_wait);
 	}
 
 	rdev->serial = serial;
drivers/md/md.h +4 -1
index ac84289664cd..2208da7915e6 100644
@@ -126,7 +126,6 @@ enum sync_action {
 struct serial_in_rdev {
 	struct rb_root_cached serial_rb;
 	spinlock_t serial_lock;
-	wait_queue_head_t serial_io_wait;
 };
 
 /*
@@ -381,7 +380,11 @@ struct serial_info {
 	struct rb_node node;
 	sector_t start;		/* start sector of rb node */
 	sector_t last;		/* end sector of rb node */
+	sector_t wnode_start; /* address of waiting nodes on the same list */
 	sector_t _subtree_last; /* highest sector in subtree of rb node */
+	struct list_head	list_node;
+	struct list_head	waiters;
+	struct completion	ready;
 };
 
 /*
drivers/md/raid1.c +34 -11

90 lines not on this page · raw message

Signature · 1 line
2.50.1 (Apple Git-155)
Applied
#1 lore raw
Quoted 30 lines · Xiao Ni

Previously, using wait_event() would wake up all waiters simultaneously, and they would compete for the tree lock. The bio which gets the lock first will be handled, so the write sequence cannot be guaranteed.

For example:
bio1(100,200)
bio2(150,200)
bio3(150,300)

The write sequence of fast device is bio1,bio2,bio3. But the write sequence of slow device could be bio1,bio3,bio2 due to lock competition. This causes data corruption.

Replace waitqueue with a fifo list to guarantee the write sequence. And it also needs to iterate the list when removing one entry. If not, it may miss the opportunity to wake up the waiting io.

For example:
bio1(1,3), bio2(2,4)
bio3(5,7), bio4(6,8)
These four bios are in the same bucket. bio1 and bio3 are inserted into the rbtree. bio2 and bio4 are added to the waiting list and bio2 is the first one. bio3 returns from slow disk and tries to wake up the waiting bios. bio2 is removed from the list and will be handled. But bio1 hasn't finished. So bio2 will be added into waiting list again. Then bio1 returns from slow disk and wakes up waiting bios. bio4 is removed from the list and will be handled. Now bio1, bio3 and bio4 all finish and bio2 is left on the waiting list. So it needs to iterate the waiting list to wake up the right bio.

Quoted 8 lines

---
v2: use prepare_to_wait_exclusive
v3: return back to self managed fifo list to find the right waiting node

  drivers/md/md.c    |  1 -
  drivers/md/md.h    |  5 ++++-
  drivers/md/raid1.c | 45 ++++++++++++++++++++++++++++++++++-----------
  3 files changed, 38 insertions(+), 13 deletions(-)

Applied to md-7.1

Signature · 2 lines
Thansk,
Kuai