Skip to contentSkip to conversation

bcache: dont reset bio opf in bch_data_insert_start

PATCH Superseded Dongsheng Yang 3 participants 8 messages Jan 25, 2021 – Jan 28, 2021

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcache: dont reset bio opf in bch_data_insert_start

commit ad0d9e76(bcache: use bio op accessors) makes the bi_opf modified by bio_set_op_attrs(). But there is a logical problem in this commit:

                trace_bcache_cache_insert(k);
                bch_keylist_push(&op->insert_keys);

- n->bi_rw |= REQ_WRITE;

+               bio_set_op_attrs(n, REQ_OP_WRITE, 0);
                bch_submit_bbio(n, op->c, k, 0);
        } while (n != bio);

The old code add REQ_WRITE into bio n and keep other flags; the new code set REQ_OP_WRITE to bi_opf, but reset all other flags.

This problem is discoverd in our performance testing:
(1) start a fio with 1M x 128depth for read in /dev/nvme0n1p1
(2) start a fio with 1M x 128depth for write in /dev/escache0 (cache device is /dev/nvme0n1p2)

We found the BW of reading is 2000+M/s, but the BW of writing is 0-100M/s. After some debugging, we found the problem is io submit in writting is very slow.

bch_data_insert_start() insert a bio to /dev/nvme0n1p1, but as cached_dev submit stack bio will be added into current->bio_list, and return.Then __submit_bio_noacct() will submit the new bio in bio_list into /dev/nvme0n1p1. This operation would be slow in blk_mq_submit_bio() -> rq_qos_throttle(q, bio);

The rq_qos_throttle() will call wbt_should_throttle(),
static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)

{
        switch (bio_op(bio)) {
        case REQ_OP_WRITE:
                /*
                 * Don't throttle WRITE_ODIRECT
                 */
                if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
                    (REQ_SYNC | REQ_IDLE))
                        return false;

... ...
}

As the bio_set_op_attrs() reset the (REQ_SYNC | REQ_IDLE), so this write bio will be considered as non-direct write.

After this fix, bio to nvme will flaged as (REQ_SYNC | REQ_IDLE), then fio for writing will get about 1000M/s bandwidth.

Fixes ad0d9e76a4124708dddd00c04fc4b56fc86c02d6
---
 drivers/md/bcache/request.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/request.c +1 -1
index c7cadaafa947..eb734f7ddaac 100644
@@ -244,7 +244,7 @@ static void bch_data_insert_start(struct closure *cl)
 		trace_bcache_cache_insert(k);
 		bch_keylist_push(&op->insert_keys);
 
-		bio_set_op_attrs(n, REQ_OP_WRITE, 0);
+		n->bi_opf |= REQ_OP_WRITE;
 		bch_submit_bbio(n, op->c, k, 0);
 	} while (n != bio);
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 44 lines · Dongsheng Yang

commit ad0d9e76(bcache: use bio op accessors) makes the bi_opf modified by bio_set_op_attrs(). But there is a logical problem in this commit:

                trace_bcache_cache_insert(k);
                bch_keylist_push(&op->insert_keys);
drivers/md/bcache/request.c +1 -1
-               n->bi_rw |= REQ_WRITE;
+               bio_set_op_attrs(n, REQ_OP_WRITE, 0);
                bch_submit_bbio(n, op->c, k, 0);
        } while (n != bio);

The old code add REQ_WRITE into bio n and keep other flags; the new code set REQ_OP_WRITE to bi_opf, but reset all other flags.

This problem is discoverd in our performance testing:
(1) start a fio with 1M x 128depth for read in /dev/nvme0n1p1
(2) start a fio with 1M x 128depth for write in /dev/escache0 (cache device is /dev/nvme0n1p2)

We found the BW of reading is 2000+M/s, but the BW of writing is 0-100M/s. After some debugging, we found the problem is io submit in writting is very slow.

bch_data_insert_start() insert a bio to /dev/nvme0n1p1, but as cached_dev submit stack bio will be added into current->bio_list, and return.Then __submit_bio_noacct() will submit the new bio in bio_list into /dev/nvme0n1p1. This operation would be slow in blk_mq_submit_bio() -> rq_qos_throttle(q, bio);

The rq_qos_throttle() will call wbt_should_throttle(),
static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)

{
        switch (bio_op(bio)) {
        case REQ_OP_WRITE:
                /*
                 * Don't throttle WRITE_ODIRECT
                 */
                if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
                    (REQ_SYNC | REQ_IDLE))
                        return false;

... ...
}

Quoted 7 lines

As the bio_set_op_attrs() reset the (REQ_SYNC | REQ_IDLE), so this write bio will be considered as non-direct write.

After this fix, bio to nvme will flaged as (REQ_SYNC | REQ_IDLE), then fio for writing will get about 1000M/s bandwidth.

Fixes ad0d9e76a4124708dddd00c04fc4b56fc86c02d6

It should be,

Fixes ad0d9e76a412 ("bcache: use bio op accessors")
Quoted 1 line

Please CC the fixed patch author Mike Christie <[email protected]>.

Quoted 9 lines
---
 drivers/md/bcache/request.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/request.c +0 -0
index c7cadaafa947..eb734f7ddaac 100644

drivers/md/bcache/request.c static void bch_data_insert_start(struct closure *cl) · line 249

Quoted 7 lines
drivers/md/bcache/request.c +1 -1
@@ -244,7 +244,7 @@ static void bch_data_insert_start(struct closure *cl)
 		trace_bcache_cache_insert(k);
 		bch_keylist_push(&op->insert_keys);
 
-		bio_set_op_attrs(n, REQ_OP_WRITE, 0);
+		n->bi_opf |= REQ_OP_WRITE;
 		bch_submit_bbio(n, op->c, k, 0);
 	} while (n != bio);

The fix is OK to me, I'd like to see opinion from Mike Christie too.

Thanks for the catch.

Coly Li

#2 lore raw
Quoted 51 lines · Coly Li
Quoted 50 lines · Dongsheng Yang

commit ad0d9e76(bcache: use bio op accessors) makes the bi_opf modified by bio_set_op_attrs(). But there is a logical problem in this commit:

                 trace_bcache_cache_insert(k);
                 bch_keylist_push(&op->insert_keys);
drivers/md/bcache/request.c +1 -1
-               n->bi_rw |= REQ_WRITE;
+               bio_set_op_attrs(n, REQ_OP_WRITE, 0);
                 bch_submit_bbio(n, op->c, k, 0);
         } while (n != bio);

The old code add REQ_WRITE into bio n and keep other flags; the new code set REQ_OP_WRITE to bi_opf, but reset all other flags.

This problem is discoverd in our performance testing:
(1) start a fio with 1M x 128depth for read in /dev/nvme0n1p1
(2) start a fio with 1M x 128depth for write in /dev/escache0 (cache device is /dev/nvme0n1p2)

We found the BW of reading is 2000+M/s, but the BW of writing is 0-100M/s. After some debugging, we found the problem is io submit in writting is very slow.

bch_data_insert_start() insert a bio to /dev/nvme0n1p1, but as cached_dev submit stack bio will be added into current->bio_list, and return.Then __submit_bio_noacct() will submit the new bio in bio_list into /dev/nvme0n1p1. This operation would be slow in blk_mq_submit_bio() -> rq_qos_throttle(q, bio);

The rq_qos_throttle() will call wbt_should_throttle(),
static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)

{
         switch (bio_op(bio)) {
         case REQ_OP_WRITE:
                 /*
                  * Don't throttle WRITE_ODIRECT
                  */
                 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
                     (REQ_SYNC | REQ_IDLE))
                         return false;

... ...
}

As the bio_set_op_attrs() reset the (REQ_SYNC | REQ_IDLE), so this write bio will be considered as non-direct write.

After this fix, bio to nvme will flaged as (REQ_SYNC | REQ_IDLE), then fio for writing will get about 1000M/s bandwidth.

Quoted 6 lines
Quoted 1 line
Fixes ad0d9e76a4124708dddd00c04fc4b56fc86c02d6

It should be,

Fixes ad0d9e76a412 ("bcache: use bio op accessors")
Quoted 1 line

Please CC the fixed patch author Mike Christie<[email protected]>.

Hi Coly,

    Should I send a V2 for commit message update?

Or you can help to fix it when you take it from maillist?

Thanx

Yang

Quoted 21 lines
Quoted 9 lines
---
  drivers/md/bcache/request.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/request.c +0 -0
index c7cadaafa947..eb734f7ddaac 100644

drivers/md/bcache/request.c static void bch_data_insert_start(struct closure *cl) · line 249

Quoted 7 lines
drivers/md/bcache/request.c +1 -1
@@ -244,7 +244,7 @@ static void bch_data_insert_start(struct closure *cl)
  		trace_bcache_cache_insert(k);
  		bch_keylist_push(&op->insert_keys);
 
-		bio_set_op_attrs(n, REQ_OP_WRITE, 0);
+		n->bi_opf |= REQ_OP_WRITE;
  		bch_submit_bbio(n, op->c, k, 0);
  	} while (n != bio);

The fix is OK to me, I'd like to see opinion from Mike Christie too.

Thanks for the catch.

Coly Li

#3 lore raw
Quoted 59 lines · Dongsheng Yang
Quoted 57 lines · Coly Li
Quoted 45 lines · Dongsheng Yang

commit ad0d9e76(bcache: use bio op accessors) makes the bi_opf modified by bio_set_op_attrs(). But there is a logical problem in this commit:

                 trace_bcache_cache_insert(k); bch_keylist_push(&op->insert_keys);

-               n->bi_rw |= REQ_WRITE;
+               bio_set_op_attrs(n, REQ_OP_WRITE, 0); bch_submit_bbio(n, op->c, k, 0); } while (n != bio);

The old code add REQ_WRITE into bio n and keep other flags; the new code set REQ_OP_WRITE to bi_opf, but reset all other flags.

This problem is discoverd in our performance testing:
(1) start a fio with 1M x 128depth for read in /dev/nvme0n1p1
(2) start a fio with 1M x 128depth for write in /dev/escache0 (cache device is /dev/nvme0n1p2)

We found the BW of reading is 2000+M/s, but the BW of writing is 0-100M/s. After some debugging, we found the problem is io submit in writting is very slow.

bch_data_insert_start() insert a bio to /dev/nvme0n1p1, but as cached_dev submit stack bio will be added into current->bio_list, and return.Then __submit_bio_noacct() will submit the new bio in bio_list into /dev/nvme0n1p1. This operation would be slow in blk_mq_submit_bio() -> rq_qos_throttle(q, bio);

The rq_qos_throttle() will call wbt_should_throttle(), static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
{
         switch (bio_op(bio)) {
         case REQ_OP_WRITE:
                 /*
                  * Don't throttle WRITE_ODIRECT
                  */
                 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == (REQ_SYNC | REQ_IDLE))
                         return false;
... ...
}

Quoted 7 lines

As the bio_set_op_attrs() reset the (REQ_SYNC | REQ_IDLE), so this write bio will be considered as non-direct write.

After this fix, bio to nvme will flaged as (REQ_SYNC | REQ_IDLE), then fio for writing will get about 1000M/s bandwidth.

Fixes ad0d9e76a4124708dddd00c04fc4b56fc86c02d6

It should be,

Fixes ad0d9e76a412 ("bcache: use bio op accessors")
Quoted 1 line
Quoted 9 lines
Quoted 1 line

Please CC the fixed patch author  Mike Christie<[email protected]>.

Hi Coly,

    Should I send a V2 for commit message update?

Or you can help to fix it when you take it from maillist?

Yes, please fix it in v2 version. And let's wait for response from Mike, maybe he has better suggestion to fix.

Thanks.

Coly Li

#4 lore raw
Quoted 61 lines · Coly Li
Quoted 60 lines · Dongsheng Yang
Quoted 58 lines · Coly Li
Quoted 45 lines · Dongsheng Yang

commit ad0d9e76(bcache: use bio op accessors) makes the bi_opf modified by bio_set_op_attrs(). But there is a logical problem in this commit:

                 trace_bcache_cache_insert(k); bch_keylist_push(&op->insert_keys);

-               n->bi_rw |= REQ_WRITE;
+               bio_set_op_attrs(n, REQ_OP_WRITE, 0);
                 bch_submit_bbio(n, op->c, k, 0); } while (n != bio);

The old code add REQ_WRITE into bio n and keep other flags; the new code set REQ_OP_WRITE to bi_opf, but reset all other flags.

This problem is discoverd in our performance testing:
(1) start a fio with 1M x 128depth for read in /dev/nvme0n1p1
(2) start a fio with 1M x 128depth for write in /dev/escache0 (cache device is /dev/nvme0n1p2)

We found the BW of reading is 2000+M/s, but the BW of writing is 0-100M/s. After some debugging, we found the problem is io submit in writting is very slow.

bch_data_insert_start() insert a bio to /dev/nvme0n1p1, but as cached_dev submit stack bio will be added into current->bio_list, and return.Then __submit_bio_noacct() will submit the new bio in bio_list into /dev/nvme0n1p1. This operation would be slow in blk_mq_submit_bio() -> rq_qos_throttle(q, bio);

The rq_qos_throttle() will call wbt_should_throttle(), static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio)
{
         switch (bio_op(bio)) {
         case REQ_OP_WRITE:
                 /*
                  * Don't throttle WRITE_ODIRECT
                  */
                 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == (REQ_SYNC | REQ_IDLE))
                         return false;
... ...
}

Quoted 7 lines

As the bio_set_op_attrs() reset the (REQ_SYNC | REQ_IDLE), so this write bio will be considered as non-direct write.

After this fix, bio to nvme will flaged as (REQ_SYNC | REQ_IDLE), then fio for writing will get about 1000M/s bandwidth.

Fixes ad0d9e76a4124708dddd00c04fc4b56fc86c02d6

It should be,

Fixes ad0d9e76a412 ("bcache: use bio op accessors")
Quoted 1 line

Please CC the fixed patch author  Mike Christie<[email protected]>.

Quoted 8 lines
Quoted 6 lines

Hi Coly,

    Should I send a V2 for commit message update?

Or you can help to fix it when you take it from maillist?

Yes, please fix it in v2 version. And let's wait for response from Mike, maybe he has better suggestion to fix.

okey,actually, Mike is in my cc list of first mail (but not note in commit message), so he can receive my patch.

But anyway, I will send a v2

Quoted 5 lines

Thanks.

Coly Li

#5 lore raw
In reply to Dongsheng Yang

But the old code is also completely broken. We can't just OR in the op, as that implicitly assumes the old op was 0 (REQ_OP_READ).

Please fix this to explicitly set the exact op and flags that you want instead of this fragile magic.

#6 lore raw

Hi Christop:

Quoted 2 lines · Christoph Hellwig

But the old code is also completely broken. We can't just OR in the op, as that implicitly assumes the old op was 0 (REQ_OP_READ).

Yes, indeed, there is an assume that the op is just possible to be 0 (REQ_OP_READ) or 1 (REQ_OP_WRITE).

REQ_OP_WRITE is from cached_dev_submit_bio() which would be submitted by upper user.

REQ_OP_READ is from bcache itself, such as cached_dev_read_done() (when we found cache miss, we will read

data from backing and then we want to insert it into cache device. then there is a read bio with data reach here, we

need to set the bio_op to REQ_OP_WRITE, and send this bio to cache device).

Quoted 2 lines

Please fix this to explicitly set the exact op and flags that you want instead of this fragile magic.blk_rq_map_kern

This commit only want to fix the logic bug introduced in ad0d9e76a412 ("bcache: use bio op accessors"),

that's more likely a partial revert.

I agree that we can make it more clearly and explicitly.

But I found there is no accessor to set op only, besides, the bio_set_op_attrs() was marked as obsolete.

There are some others doing similar things as below:

blk_rq_map_kern():

bio->bi_opf &= ~REQ_OP_MASK;

bio->bi_opf |= req_op(rq);

So what about below:

drivers/md/bcache/request.c +0 -0
index c7cadaafa947..bacc7366002f 100644
@@ -244,7 +244,14 @@ static void bch_data_insert_start(struct closure *cl)

                trace_bcache_cache_insert(k); bch_keylist_push(&op->insert_keys);

-               bio_set_op_attrs(n, REQ_OP_WRITE, 0); +               /*
+                * n here would be REQ_OP_READ, if +                * we are inserting data read from +                * backing device in cache miss or +                * inserting data in movinggc. +                */
+               n->bi_opf &= ~REQ_OP_MASK;
+               n->bi_opf |= REQ_OP_WRITE; bch_submit_bbio(n, op->c, k, 0); } while (n != bio);

Thanx

Yang

#7 lore raw

Hi Christoph,

Quoted 49 lines · Dongsheng Yang

Hi Christop:

Quoted 2 lines · Christoph Hellwig

But the old code is also completely broken.  We can't just OR in the op, as that implicitly assumes the old op was 0 (REQ_OP_READ).

Yes, indeed, there is an assume that the op is just possible to be 0 (REQ_OP_READ) or 1 (REQ_OP_WRITE).

REQ_OP_WRITE is from cached_dev_submit_bio() which would be submitted by upper user.

REQ_OP_READ is from bcache itself, such as cached_dev_read_done() (when we found cache miss, we will read

data from backing and then we want to insert it into cache device. then there is a read bio with data reach here, we

need to set the bio_op to REQ_OP_WRITE, and send this bio to cache device).

Quoted 2 lines

Please fix this to explicitly set the exact op and flags that you want instead of this fragile magic.blk_rq_map_kern

This commit only want to fix the logic bug introduced in ad0d9e76a412 ("bcache: use bio op accessors"),

that's more likely a partial revert.

I agree that we can make it more clearly and explicitly.

But I found there is no accessor to set op only, besides, the bio_set_op_attrs() was marked as obsolete.

There are some others doing similar things as below:

blk_rq_map_kern():

bio->bi_opf &= ~REQ_OP_MASK;

bio->bi_opf |= req_op(rq);

So what about below:

drivers/md/bcache/request.c +0 -1
index c7cadaafa947..bacc7366002f 100644
@@ -244,7 +244,14 @@ static void bch_data_insert_start(struct closure *cl)
                 trace_bcache_cache_insert(k);
                 bch_keylist_push(&op->insert_keys);
 
-               bio_set_op_attrs(n, REQ_OP_WRITE, 0);

drivers/md/bcache/request.c static void bch_data_insert_start(struct closure *cl) · line 256

Quoted 10 lines
drivers/md/bcache/request.c +8 -0
+               /*
+                * n here would be REQ_OP_READ, if
+                * we are inserting data read from
+                * backing device in cache miss or
+                * inserting data in movinggc.
+                */
+               n->bi_opf &= ~REQ_OP_MASK;
+               n->bi_opf |= REQ_OP_WRITE;
                 bch_submit_bbio(n, op->c, k, 0);
         } while (n != bio);

Another solution is introducing an accessor to set op only, something like bio_set_op(). Then we should keep the bcache patch as what it was to fix the bug.

And send another patch to introduce bio_set_op():

block/blk-map.c +1 -3
index 6e804892d5ec..83bc33a59fa5 100644
@@ -587,9 +587,7 @@ static int __blk_rq_map_user_iov(struct request *rq,
        if (IS_ERR(bio))
                return PTR_ERR(bio);
 
-       bio->bi_opf &= ~REQ_OP_MASK;
-       bio->bi_opf |= req_op(rq);
-
+       bio_set_op(bio, req_op(rq));
        orig_bio = bio;
 
        /*
drivers/md/bcache/request.c +7 -1
index eb734f7ddaac..d8839300805e 100644
@@ -244,7 +244,13 @@ static void bch_data_insert_start(struct closure *cl)
                trace_bcache_cache_insert(k);
                bch_keylist_push(&op->insert_keys);
 
-               n->bi_opf |= REQ_OP_WRITE;
+               /*
+                * n here would be REQ_OP_READ, if
+                * we are inserting data read from
+                * backing device in cache miss or
+                * inserting data in movinggc.
+                */
+               bio_set_op(n, REQ_OP_WRITE);
                bch_submit_bbio(n, op->c, k, 0);
        } while (n != bio);
 
include/linux/blk_types.h +6 -0
index b3fc5d3dd8ea..2affd3269bdc 100644
@@ -439,6 +439,12 @@ static inline void bio_set_op_attrs(struct bio *bio, unsigned op,
        bio->bi_opf = op | op_flags;
 }
 
+static inline void bio_set_op(struct bio *bio, unsigned op)
+{
+       bio->bi_opf &= ~REQ_OP_MASK;
+       bio->bi_opf |= op;
+}
+
 static inline bool op_is_write(unsigned int op)
 {
        return (op & 1);
Quoted 6 lines

Thanx

Yang