Skip to contentSkip to conversation

btrfs: simplify async csum synchronization

PATCH Superseded Daniel Vacek 2 participants 3 messages Nov 13, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: simplify async csum synchronization

We don't need the completion csum_done which marks the csum work has been executed. We can simply flush_work() instead.

This way we can slim down the btrfs_bio structure by 32 bytes matching it's size to what it used to be before introducing the async csums. Hence not making any change with respect to the structure size.
---
This is a simple fixup for "btrfs: introduce btrfs_bio::async_csum" in for-next and can be squashed into it.

---
 fs/btrfs/bio.c       | 2 +-
 fs/btrfs/bio.h       | 1 -
 fs/btrfs/file-item.c | 6 +++---
 3 files changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/bio.c +1 -1
index a73652b8724a..fd6e4278a62f 100644
@@ -106,7 +106,7 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
 	ASSERT(in_task());
 
 	if (bbio->async_csum)
-		wait_for_completion(&bbio->csum_done);
+		flush_work(&bbio->csum_work);
 
 	bbio->bio.bi_status = status;
 	if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
fs/btrfs/bio.h +0 -1
index deaeea3becf4..0b09d9122fa2 100644
@@ -57,7 +57,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;
 		};
fs/btrfs/file-item.c +3 -3
index 72be3ede0edf..3e9241f360c8 100644
@@ -792,7 +792,6 @@ static void csum_one_bio_work(struct work_struct *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);
 }
 
 /*
@@ -805,6 +804,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 	struct bio *bio = &bbio->bio;
 	struct btrfs_ordered_sum *sums;
+	struct workqueue_struct *wq;
 	unsigned nofs_flag;
 
 	nofs_flag = memalloc_nofs_save();
@@ -825,11 +825,11 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
 		csum_one_bio(bbio, &bbio->bio.bi_iter);
 		return 0;
 	}
-	init_completion(&bbio->csum_done);
 	bbio->async_csum = true;
 	bbio->csum_saved_iter = bbio->bio.bi_iter;
 	INIT_WORK(&bbio->csum_work, csum_one_bio_work);
-	schedule_work(&bbio->csum_work);
+	wq = bio->bi_opf & REQ_META? fs_info->endio_meta_workers: fs_info->endio_workers;
+	queue_work(wq, &bbio->csum_work);
 	return 0;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 61 lines · Daniel Vacek

We don't need the completion csum_done which marks the csum work has been executed. We can simply flush_work() instead.

This way we can slim down the btrfs_bio structure by 32 bytes matching it's size to what it used to be before introducing the async csums. Hence not making any change with respect to the structure size.
---
This is a simple fixup for "btrfs: introduce btrfs_bio::async_csum" in for-next and can be squashed into it.

---
  fs/btrfs/bio.c       | 2 +-
  fs/btrfs/bio.h       | 1 -
  fs/btrfs/file-item.c | 6 +++---
  3 files changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/bio.c +1 -1
index a73652b8724a..fd6e4278a62f 100644
@@ -106,7 +106,7 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
  	ASSERT(in_task());
 
  	if (bbio->async_csum)
-		wait_for_completion(&bbio->csum_done);
+		flush_work(&bbio->csum_work);
 
  	bbio->bio.bi_status = status;
  	if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
fs/btrfs/bio.h +0 -1
index deaeea3becf4..0b09d9122fa2 100644
@@ -57,7 +57,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;
  		};
fs/btrfs/file-item.c +1 -1
index 72be3ede0edf..3e9241f360c8 100644
@@ -792,7 +792,6 @@ static void csum_one_bio_work(struct work_struct *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);
  }
 
  /*
@@ -805,6 +804,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
  	struct btrfs_fs_info *fs_info = inode->root->fs_info;
  	struct bio *bio = &bbio->bio;
  	struct btrfs_ordered_sum *sums;
+	struct workqueue_struct *wq;
  	unsigned nofs_flag;
 
  	nofs_flag = memalloc_nofs_save();

fs/btrfs/file-item.c int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async) · line 831

Quoted 9 lines
fs/btrfs/file-item.c +1 -2
@@ -825,11 +825,11 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
  		csum_one_bio(bbio, &bbio->bio.bi_iter);
  		return 0;
  	}
-	init_completion(&bbio->csum_done);
  	bbio->async_csum = true;
  	bbio->csum_saved_iter = bbio->bio.bi_iter;
  	INIT_WORK(&bbio->csum_work, csum_one_bio_work);
-	schedule_work(&bbio->csum_work);
+	wq = bio->bi_opf & REQ_META? fs_info->endio_meta_workers: fs_info->endio_workers;

Metadata will not go into btrfs_csum_one_bio(), thus it's fixed to endio_workers().

Quoted 4 lines
fs/btrfs/file-item.c +1 -0
+	queue_work(wq, &bbio->csum_work);
  	return 0;
  }
 
#2 lore raw
Quoted 60 lines · Qu Wenruo
Quoted 59 lines · Daniel Vacek

We don't need the completion csum_done which marks the csum work has been executed. We can simply flush_work() instead.

This way we can slim down the btrfs_bio structure by 32 bytes matching it's size to what it used to be before introducing the async csums. Hence not making any change with respect to the structure size.
---
This is a simple fixup for "btrfs: introduce btrfs_bio::async_csum" in for-next and can be squashed into it.

---
  fs/btrfs/bio.c       | 2 +-
  fs/btrfs/bio.h       | 1 -
  fs/btrfs/file-item.c | 6 +++---
  3 files changed, 4 insertions(+), 5 deletions(-)
fs/btrfs/bio.c +1 -1
index a73652b8724a..fd6e4278a62f 100644
@@ -106,7 +106,7 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
      ASSERT(in_task());
 
      if (bbio->async_csum)
-             wait_for_completion(&bbio->csum_done);
+             flush_work(&bbio->csum_work);
 
      bbio->bio.bi_status = status;
      if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
fs/btrfs/bio.h +0 -1
index deaeea3becf4..0b09d9122fa2 100644
@@ -57,7 +57,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;
              };
fs/btrfs/file-item.c +1 -1
index 72be3ede0edf..3e9241f360c8 100644
@@ -792,7 +792,6 @@ static void csum_one_bio_work(struct work_struct *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);
  }
 
  /*
@@ -805,6 +804,7 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
      struct btrfs_fs_info *fs_info = inode->root->fs_info;
      struct bio *bio = &bbio->bio;
      struct btrfs_ordered_sum *sums;
+     struct workqueue_struct *wq;
      unsigned nofs_flag;
 
Quoted 14 lines

fs/btrfs/file-item.c int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async) · line 831

Quoted 11 lines
fs/btrfs/file-item.c +1 -2
      nofs_flag = memalloc_nofs_save();
@@ -825,11 +825,11 @@ int btrfs_csum_one_bio(struct btrfs_bio *bbio, bool async)
              csum_one_bio(bbio, &bbio->bio.bi_iter);
              return 0;
      }
-     init_completion(&bbio->csum_done);
      bbio->async_csum = true;
      bbio->csum_saved_iter = bbio->bio.bi_iter;
      INIT_WORK(&bbio->csum_work, csum_one_bio_work);
-     schedule_work(&bbio->csum_work);
+     wq = bio->bi_opf & REQ_META? fs_info->endio_meta_workers: fs_info->endio_workers;

Metadata will not go into btrfs_csum_one_bio(), thus it's fixed to endio_workers().

I suspected, but I was not sure so rather be safe than sorry. That simplifies it quite nicely. Thanks.

Quoted 5 lines
Quoted 4 lines
fs/btrfs/file-item.c +1 -0
+     queue_work(wq, &bbio->csum_work);
      return 0;
  }