btrfs: async checksumming cleanups
cover btrfs: async checksumming cleanups
While trying to deal with encryption bounce bios checksumming, a solution came up which covers all writes (also the regular !encrypted ones). As a result the code cleans up quite nicely. That's the second patch in this series. The first one is just a nit. Details in the patches themselves.
Daniel Vacek (2):
btrfs: consume the given iter directly instead of copying in
csum_one_bio()
btrfs: use bio::remaining for async checksumming synchronization
fs/btrfs/bio.c | 4 ---- fs/btrfs/bio.h | 4 ---- fs/btrfs/file-item.c | 20 +++++++++----------- 3 files changed, 9 insertions(+), 19 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/2 btrfs: consume the given iter directly instead of copying in csum_one_bio()
This is just a small cleanup to avoid copying the iter twice in async case. We already have a copy csum_one_bio() can consume directly. No need to copy again the second time.
In the sync case we can copy in the caller (as we did before dd57c78aec39 ("btrfs: introduce btrfs_bio::async_csum")).
--- fs/btrfs/file-item.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/file-item.c +7 -7
@@ -797,18 +797,16 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,return ret; } -static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *src) +static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *iter) {struct btrfs_inode *inode = bbio->inode;struct btrfs_fs_info *fs_info = inode->root->fs_info;struct btrfs_ordered_sum *sums = bbio->sums; -struct bvec_iter iter;const u32 blocksize = fs_info->sectorsize;int index =0 ; -for (iter = *src; iter.bi_size;bio_advance_iter (&bbio->bio, &iter, blocksize)) { -btrfs_csum_one_bio_block (fs_info, &bbio->bio, &iter, - sums->sums + index); +for (; iter->bi_size;bio_advance_iter (&bbio->bio, iter, blocksize)) { +btrfs_csum_one_bio_block (fs_info, &bbio->bio, iter, sums->sums + index); index += fs_info->csum_size; }
@@ -851,12 +849,14 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)btrfs_add_ordered_sum (ordered, sums);if (!async) { -csum_one_bio (bbio, &bbio->bio.bi_iter); +struct bvec_iter iter = bio->bi_iter; + +csum_one_bio (bbio, &iter);return 0 ; }init_completion (&bbio->csum_done); bbio->async_csum = true; - bbio->csum_saved_iter = bbio->bio.bi_iter; + bbio->csum_saved_iter = bio->bi_iter;INIT_WORK (&bbio->csum_work, csum_one_bio_work);schedule_work (&bbio->csum_work);return 0 ;
Signature · 1 line
2.53.0
Re: [PATCH 1/2] btrfs: consume the given iter directly instead of copying in csum_one_bio()
Quoted 16 lines · Daniel Vacek
This is just a small cleanup to avoid copying the iter twice in async case. We already have a copy
csum_one_bio()can consume directly. No need to copy again the second time.
In the sync case we can copy in the caller (as we did beforedd57c78aec39("btrfs: introducebtrfs_bio::async_csum")).Signed-off-by Daniel Vacek <[email protected]>--- fs/btrfs/file-item.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)fs/btrfs/file-item.c +0
-0
fs/btrfs/file-item.c int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path, · line 800
Quoted 5 lines
fs/btrfs/file-item.c +1
-1@@ -797,18 +797,16 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,return ret; } -static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *src) +static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *iter)
Since we're here, what about adding const prefix to iter?
fs/btrfs/file-item.c int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path, · line 809
Quoted 9 lines
fs/btrfs/file-item.c +0
-2{struct btrfs_inode *inode = bbio->inode;struct btrfs_fs_info *fs_info = inode->root->fs_info;struct btrfs_ordered_sum *sums = bbio->sums; -struct bvec_iter iter;const u32 blocksize = fs_info->sectorsize;int index =0 ; -for (iter = *src; iter.bi_size;bio_advance_iter (&bbio->bio, &iter, blocksize)) {
I'm thinking the opposite way.
What about still keeping a local bvec_iter copy, but remove csum_saved_iter completely?
Since at bio calculation time, the bio is not yet submitted, so the csum_saved_iter always match bio.bi_iter.
I think removing csum_saved_iter would also reduce the size of btrfs_bio.
Thanks,
Qu
Quoted 24 lines
fs/btrfs/file-item.c +6
-4-btrfs_csum_one_bio_block (fs_info, &bbio->bio, &iter, - sums->sums + index); +for (; iter->bi_size;bio_advance_iter (&bbio->bio, iter, blocksize)) { +btrfs_csum_one_bio_block (fs_info, &bbio->bio, iter, sums->sums + index); index += fs_info->csum_size; }@@ -851,12 +849,14 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)btrfs_add_ordered_sum (ordered, sums);if (!async) { -csum_one_bio (bbio, &bbio->bio.bi_iter); +struct bvec_iter iter = bio->bi_iter; + +csum_one_bio (bbio, &iter);return 0 ; }init_completion (&bbio->csum_done); bbio->async_csum = true; - bbio->csum_saved_iter = bbio->bio.bi_iter; + bbio->csum_saved_iter = bio->bi_iter;INIT_WORK (&bbio->csum_work, csum_one_bio_work);schedule_work (&bbio->csum_work);return 0 ;
Qu Wenruo
Reviewed-byRe: [PATCH 1/2] btrfs: consume the given iter directly instead of copying in csum_one_bio()
Quoted 14 lines · Qu Wenruo
Quoted 11 lines · Daniel Vacek
This is just a small cleanup to avoid copying the iter twice in async case. We already have a copy
csum_one_bio()can consume directly. No need to copy again the second time.
In the sync case we can copy in the caller (as we did beforedd57c78aec39("btrfs: introducebtrfs_bio::async_csum")).Signed-off-by Daniel Vacek <[email protected]>---
fs/btrfs/file-item.c| 14 +++++++-------1 file changed, 7 insertions(+), 7 deletions(-)
Quoted 12 lines
fs/btrfs/file-item.c int btrfs_lookup_csums_bitmap(struct btrfs_root · line 800
Quoted 10 lines
fs/btrfs/file-item.c +1
-1@@ -797,18 +797,16 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path, return ret; } -static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *src) +static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *iter)Since we're here, what about adding const prefix to iter?
My bad, we're advancing the iter, so it can not be const.
Quoted 11 lines
fs/btrfs/file-item.c int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,
Quoted 10 lines
fs/btrfs/file-item.c +0
-2{ struct btrfs_inode *inode = bbio->inode; struct btrfs_fs_info *fs_info = inode->root->fs_info; struct btrfs_ordered_sum *sums = bbio->sums; - struct bvec_iter iter; const u32 blocksize = fs_info->sectorsize; int index = 0; - for (iter = *src; iter.bi_size; bio_advance_iter(&bbio->bio, &iter, blocksize)) {
Quoted 7 lines
I'm thinking the opposite way.
What about still keeping a local
bvec_itercopy, but removecsum_saved_itercompletely?Since at bio calculation time, the bio is not yet submitted, so the
csum_saved_iteralways match bio.bi_iter.I think removing
csum_saved_iterwould also reduce the size ofbtrfs_bio.
My bad, this won't work.
During csum calculation the bio can be submitted already, thus its bio::bi_iter is no longer reliable.
And since it's a data write, we do not have bbio::saved_iter to utilize either.
Now it looks good to me.
Thanks,
Qu
Quoted 30 lines
Thanks,
QuQuoted 25 lines
fs/btrfs/file-item.c +2
-2- btrfs_csum_one_bio_block(fs_info, &bbio->bio, &iter, - sums->sums + index); + for (; iter->bi_size; bio_advance_iter(&bbio->bio, iter, blocksize)) { + btrfs_csum_one_bio_block(fs_info, &bbio->bio, iter, sums- >sums + index); index += fs_info->csum_size;}
fs/btrfs/file-item.c +4
-2@@ -851,12 +849,14 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async) btrfs_add_ordered_sum(ordered, sums); if (!async) { - csum_one_bio(bbio, &bbio->bio.bi_iter); + struct bvec_iter iter = bio->bi_iter; + + csum_one_bio(bbio, &iter); return 0; } init_completion(&bbio->csum_done); bbio->async_csum = true; - bbio->csum_saved_iter = bbio->bio.bi_iter; + bbio->csum_saved_iter = bio->bi_iter; INIT_WORK(&bbio->csum_work, csum_one_bio_work); schedule_work(&bbio->csum_work); return 0;
Re: [PATCH 1/2] btrfs: consume the given iter directly instead of copying in csum_one_bio()
In reply to Daniel VacekQuoted 41 lines · Daniel Vacek
This is just a small cleanup to avoid copying the iter twice in async case. We already have a copy
csum_one_bio()can consume directly. No need to copy again the second time.
In the sync case we can copy in the caller (as we did beforedd57c78aec39("btrfs: introducebtrfs_bio::async_csum")).Signed-off-by Daniel Vacek <[email protected]>--- fs/btrfs/file-item.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)fs/btrfs/file-item.c +3
-5@@ -797,18 +797,16 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,return ret; } -static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *src) +static void csum_one_bio (struct btrfs_bio *bbio,struct bvec_iter *iter) {struct btrfs_inode *inode = bbio->inode;struct btrfs_fs_info *fs_info = inode->root->fs_info;struct btrfs_ordered_sum *sums = bbio->sums; -struct bvec_iter iter;const u32 blocksize = fs_info->sectorsize;int index =0 ; -for (iter = *src; iter.bi_size;bio_advance_iter (&bbio->bio, &iter, blocksize)) { -btrfs_csum_one_bio_block (fs_info, &bbio->bio, &iter, - sums->sums + index); +for (; iter->bi_size;bio_advance_iter (&bbio->bio, iter, blocksize)) { +btrfs_csum_one_bio_block (fs_info, &bbio->bio, iter, sums->sums + index); index += fs_info->csum_size; }@@ -851,12 +849,14 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)btrfs_add_ordered_sum (ordered, sums);if (!async) {
fs/btrfs/file-item.c int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async) · line 859
Quoted 10 lines
fs/btrfs/file-item.c +4
-2-csum_one_bio (bbio, &bbio->bio.bi_iter); +struct bvec_iter iter = bio->bi_iter; + +csum_one_bio (bbio, &iter);return 0 ; }init_completion (&bbio->csum_done); bbio->async_csum = true; - bbio->csum_saved_iter = bbio->bio.bi_iter; + bbio->csum_saved_iter = bio->bi_iter;
Actually we can use this one unconditionally in both cases and save the stack space.
And unifying both cases will allow us getting rid of the paramert.
I'll send a v2 shortly.
--nX
Quoted 6 lines
fs/btrfs/file-item.c +0
-1INIT_WORK (&bbio->csum_work, csum_one_bio_work);schedule_work (&bbio->csum_work);return 0 ; --2.53.0
PATCH 2/2 btrfs: use bio::remaining for async checksumming synchronization
In reply to Daniel VacekWe can use bio::remaining counter to sync the offloaded checksuming. As a result we can slim down the btrfs_bio structure by 24 bytes and simplify the code a bit.
$ pahole | diff - /* size: 328, cachelines: 6, members: 15 */ + /* size: 304, cachelines: 5, members: 14 */
Moreover this will allow us enabling async checksumming with encryption where we need to checksum the bounce bio instead of our regular one embedded in btrfs_bio. And so we need to extend it's lifetime. This is the preffered way to do so.
--- fs/btrfs/bio.c | 4 ---- fs/btrfs/bio.h | 4 ---- fs/btrfs/file-item.c | 6 ++---- 3 files changed, 2 insertions(+), 12 deletions(-)
fs/btrfs/bio.c +0 -4
@@ -103,7 +103,6 @@ static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info, bbio->can_use_append = orig_bbio->can_use_append; bbio->is_scrub = orig_bbio->is_scrub; bbio->is_remap = orig_bbio->is_remap; - bbio->async_csum = orig_bbio->async_csum;atomic_inc (&orig_bbio->pending_ios);return bbio;
@@ -114,9 +113,6 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)/* Make sure we're already in task context. */ ASSERT (in_task ()); -if (bbio->async_csum) -wait_for_completion (&bbio->csum_done); - bbio->bio.bi_status = status;if (bbio->bio.bi_pool == &btrfs_clone_bioset) {struct btrfs_bio *orig_bbio = bbio->private;
fs/btrfs/bio.h +0 -4
@@ -58,7 +58,6 @@ struct btrfs_bio {struct btrfs_ordered_extent *ordered;struct btrfs_ordered_sum *sums;struct work_struct csum_work; -struct completion csum_done;struct bvec_iter csum_saved_iter;u64 orig_physical;u64 orig_logical;
@@ -93,9 +92,6 @@ struct btrfs_bio {/* Whether the bio is coming from copy_remapped_data_io(). */ bool is_remap:1; -/* Whether the csum generation for data write is async. */ - bool async_csum:1; -/* Whether the bio is written using zone append. */ bool can_use_append:1;
fs/btrfs/file-item.c +2 -4
@@ -817,9 +817,8 @@ static void csum_one_bio_work(struct work_struct *work) struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, csum_work);ASSERT (btrfs_op (&bbio->bio) ==BTRFS_MAP_WRITE ); -ASSERT (bbio->async_csum == true);csum_one_bio (bbio, &bbio->csum_saved_iter); -complete (&bbio->csum_done); +bio_endio (&bbio->bio); }/*
@@ -854,8 +853,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)csum_one_bio (bbio, &iter);return 0 ; } -init_completion (&bbio->csum_done); - bbio->async_csum = true; +bio_inc_remaining (bio); bbio->csum_saved_iter = bio->bi_iter;INIT_WORK (&bbio->csum_work, csum_one_bio_work);schedule_work (&bbio->csum_work);
Signature · 1 line
2.53.0
Re: [PATCH 2/2] btrfs: use bio::remaining for async checksumming synchronization
Quoted 33 lines · Daniel Vacek
We can use
bio::remainingcounter to sync the offloaded checksuming. As a result we can slim down thebtrfs_biostructure by 24 bytes and simplify the code a bit.$ pahole | diff(quoted diff) +1
-1- /* size: 328, cachelines: 6, members: 15 */ + /* size: 304, cachelines: 5, members: 14 */Moreover this will allow us enabling async checksumming with encryption where we need to checksum the bounce bio instead of our regular one embedded in
btrfs_bio. And so we need to extend it's lifetime. This is the preffered way to do so.Signed-off-by Daniel Vacek <[email protected]>--- fs/btrfs/bio.c | 4 ---- fs/btrfs/bio.h | 4 ---- fs/btrfs/file-item.c | 6 ++---- 3 files changed, 2 insertions(+), 12 deletions(-)fs/btrfs/bio.c +0
-1@@ -103,7 +103,6 @@ static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info, bbio->can_use_append = orig_bbio->can_use_append; bbio->is_scrub = orig_bbio->is_scrub; bbio->is_remap = orig_bbio->is_remap; - bbio->async_csum = orig_bbio->async_csum;atomic_inc (&orig_bbio->pending_ios);return bbio;
fs/btrfs/bio.c void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status) · line 119
Quoted 6 lines
fs/btrfs/bio.c +0
-3@@ -114,9 +113,6 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)/* Make sure we're already in task context. */ ASSERT (in_task ()); -if (bbio->async_csum) -wait_for_completion (&bbio->csum_done); -
But what if there is still a running csum calculation at this stage?
Without waiting for the completion, we are going to get incorrect csums.
Quoted 50 lines
fs/btrfs/bio.c +0
-0bbio->bio.bi_status = status;if (bbio->bio.bi_pool == &btrfs_clone_bioset) {struct btrfs_bio *orig_bbio = bbio->private;fs/btrfs/bio.h +0
-4@@ -58,7 +58,6 @@ struct btrfs_bio {struct btrfs_ordered_extent *ordered;struct btrfs_ordered_sum *sums;struct work_struct csum_work; -struct completion csum_done;struct bvec_iter csum_saved_iter;u64 orig_physical;u64 orig_logical;@@ -93,9 +92,6 @@ struct btrfs_bio {/* Whether the bio is coming from copy_remapped_data_io(). */ bool is_remap:1; -/* Whether the csum generation for data write is async. */ - bool async_csum:1; -/* Whether the bio is written using zone append. */ bool can_use_append:1;fs/btrfs/file-item.c +2
-4@@ -817,9 +817,8 @@ static void csum_one_bio_work(struct work_struct *work) struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, csum_work);ASSERT (btrfs_op (&bbio->bio) ==BTRFS_MAP_WRITE ); -ASSERT (bbio->async_csum == true);csum_one_bio (bbio, &bbio->csum_saved_iter); -complete (&bbio->csum_done); +bio_endio (&bbio->bio); }/* @@ -854,8 +853,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)csum_one_bio (bbio, &iter);return 0 ; } -init_completion (&bbio->csum_done); - bbio->async_csum = true; +bio_inc_remaining (bio); bbio->csum_saved_iter = bio->bi_iter;INIT_WORK (&bbio->csum_work, csum_one_bio_work);schedule_work (&bbio->csum_work);
Qu Wenruo
Reviewed-byRe: [PATCH 2/2] btrfs: use bio::remaining for async checksumming synchronization
Quoted 45 lines · Qu Wenruo
Quoted 33 lines · Daniel Vacek
We can use
bio::remainingcounter to sync the offloaded checksuming. As a result we can slim down thebtrfs_biostructure by 24 bytes and simplify the code a bit.$ pahole | diff - /* size: 328, cachelines: 6, members: 15 */ + /* size: 304, cachelines: 5, members: 14 */Moreover this will allow us enabling async checksumming with encryption where we need to checksum the bounce bio instead of our regular one embedded in
btrfs_bio. And so we need to extend it's lifetime. This is the preffered way to do so.Signed-off-by Daniel Vacek <[email protected]>---
fs/btrfs/bio.c | 4 ----
fs/btrfs/bio.h | 4 ----
fs/btrfs/file-item.c| 6 ++----3 files changed, 2 insertions(+), 12 deletions(-)fs/btrfs/bio.c +0
-1@@ -103,7 +103,6 @@ static struct btrfs_bio *btrfs_split_bio(structbtrfs_fs_info *fs_info, bbio->can_use_append = orig_bbio->can_use_append; bbio->is_scrub = orig_bbio->is_scrub; bbio->is_remap = orig_bbio->is_remap; - bbio->async_csum = orig_bbio->async_csum; atomic_inc(&orig_bbio->pending_ios); return bbio;fs/btrfs/bio.c void btrfs_bio_end_io(struct btrfs_bio *bbio, · line 119
Quoted 6 lines
fs/btrfs/bio.c +0
-3@@ -114,9 +113,6 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status) /* Make sure we're already in task context. */ ASSERT(in_task()); - if (bbio->async_csum) - wait_for_completion(&bbio->csum_done); -But what if there is still a running csum calculation at this stage?
Quoted 1 line
Without waiting for the completion, we are going to get incorrect csums.
Forget this. We no longer need to wait because we won't even reach the bio->bi_end_io() until all remaining works (including the csum calculation and mirrored writes) finished.
Thanks,
Qu
Quoted 50 lines
Quoted 49 lines
fs/btrfs/bio.c +0
-0bbio->bio.bi_status = status; if (bbio->bio.bi_pool == &btrfs_clone_bioset) { struct btrfs_bio *orig_bbio = bbio->private;fs/btrfs/bio.h +0
-4@@ -58,7 +58,6 @@ struct btrfs_bio { struct btrfs_ordered_extent *ordered; struct btrfs_ordered_sum *sums; struct work_struct csum_work; - struct completion csum_done; struct bvec_iter csum_saved_iter; u64 orig_physical; u64 orig_logical;@@ -93,9 +92,6 @@ struct btrfs_bio { /* Whether the bio is coming from copy_remapped_data_io(). */ bool is_remap:1; - /* Whether the csum generation for data write is async. */ - bool async_csum:1; - /* Whether the bio is written using zone append. */ bool can_use_append:1;fs/btrfs/file-item.c +2
-4@@ -817,9 +817,8 @@ static void csum_one_bio_work(struct work_struct *work) struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, csum_work); ASSERT(btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE); - ASSERT(bbio->async_csum == true); csum_one_bio(bbio, &bbio->csum_saved_iter); - complete(&bbio->csum_done); + bio_endio(&bbio->bio); } /*@@ -854,8 +853,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async) csum_one_bio(bbio, &iter); return 0; } - init_completion(&bbio->csum_done); - bbio->async_csum = true; + bio_inc_remaining(bio); bbio->csum_saved_iter = bio->bi_iter; INIT_WORK(&bbio->csum_work, csum_one_bio_work);
schedule_work(&bbio->csum_work);