bcache: writeback: properly order backing device IO
authorMichael Lyle <mlyle@lyle.org>
Mon, 8 Jan 2018 20:21:23 +0000 (12:21 -0800)
committerJens Axboe <axboe@kernel.dk>
Mon, 8 Jan 2018 20:29:00 +0000 (13:29 -0700)
Writeback keys are presently iterated and dispatched for writeback in
order of the logical block address on the backing device.  Multiple may
be, in parallel, read from the cache device and then written back
(especially when there are contiguous I/O).

However-- there was no guarantee with the existing code that the writes
would be issued in LBA order, as the reads from the cache device are
often re-ordered.  In turn, when writing back quickly, the backing disk
often has to seek backwards-- this slows writeback and increases
utilization.

This patch introduces an ordering mechanism that guarantees that the
original order of issue is maintained for the write portion of the I/O.
Performance for writeback is significantly improved when there are
multiple contiguous keys or high writeback rates.

Signed-off-by: Michael Lyle <mlyle@lyle.org>
Reviewed-by: Tang Junhui <tang.junhui@zte.com.cn>
Tested-by: Tang Junhui <tang.junhui@zte.com.cn>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/md/bcache/bcache.h
drivers/md/bcache/writeback.c

index 1784e50eb857769d6fbcc7df2bc234b5ce824206..3be0fcc19b1f81564e4d282723b6abd60e043307 100644 (file)
@@ -330,6 +330,14 @@ struct cached_dev {
 
        struct keybuf           writeback_keys;
 
+       /*
+        * Order the write-half of writeback operations strongly in dispatch
+        * order.  (Maintain LBA order; don't allow reads completing out of
+        * order to re-order the writes...)
+        */
+       struct closure_waitlist writeback_ordering_wait;
+       atomic_t                writeback_sequence_next;
+
        /* For tracking sequential IO */
 #define RECENT_IO_BITS 7
 #define RECENT_IO      (1 << RECENT_IO_BITS)
index 479095987f22af13593b4dd9eb9a339bbff122f1..6e1d2fde43dfa2746a3300d717f38ca8fcc28e43 100644 (file)
@@ -116,6 +116,7 @@ static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors)
 struct dirty_io {
        struct closure          cl;
        struct cached_dev       *dc;
+       uint16_t                sequence;
        struct bio              bio;
 };
 
@@ -194,6 +195,27 @@ static void write_dirty(struct closure *cl)
 {
        struct dirty_io *io = container_of(cl, struct dirty_io, cl);
        struct keybuf_key *w = io->bio.bi_private;
+       struct cached_dev *dc = io->dc;
+
+       uint16_t next_sequence;
+
+       if (atomic_read(&dc->writeback_sequence_next) != io->sequence) {
+               /* Not our turn to write; wait for a write to complete */
+               closure_wait(&dc->writeback_ordering_wait, cl);
+
+               if (atomic_read(&dc->writeback_sequence_next) == io->sequence) {
+                       /*
+                        * Edge case-- it happened in indeterminate order
+                        * relative to when we were added to wait list..
+                        */
+                       closure_wake_up(&dc->writeback_ordering_wait);
+               }
+
+               continue_at(cl, write_dirty, io->dc->writeback_write_wq);
+               return;
+       }
+
+       next_sequence = io->sequence + 1;
 
        /*
         * IO errors are signalled using the dirty bit on the key.
@@ -211,6 +233,9 @@ static void write_dirty(struct closure *cl)
                closure_bio_submit(&io->bio, cl);
        }
 
+       atomic_set(&dc->writeback_sequence_next, next_sequence);
+       closure_wake_up(&dc->writeback_ordering_wait);
+
        continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq);
 }
 
@@ -242,7 +267,10 @@ static void read_dirty(struct cached_dev *dc)
        int nk, i;
        struct dirty_io *io;
        struct closure cl;
+       uint16_t sequence = 0;
 
+       BUG_ON(!llist_empty(&dc->writeback_ordering_wait.list));
+       atomic_set(&dc->writeback_sequence_next, sequence);
        closure_init_stack(&cl);
 
        /*
@@ -303,6 +331,7 @@ static void read_dirty(struct cached_dev *dc)
 
                        w->private      = io;
                        io->dc          = dc;
+                       io->sequence    = sequence++;
 
                        dirty_init(w);
                        bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);