Skip to contentSkip to conversation

bcachefs: Simplify bch2_bio_map()

PATCH In review Youling Tang 2 participants 4 messages Jun 5, 2025 – Jun 27, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bcachefs: Simplify bch2_bio_map()

From: Youling Tang <[email protected]>

For the part of directly mapping the kernel virtual address, there is no need to increase to bio page-by-page. It can be directly replaced by bio_add_virt_nofail().

For the address part of the vmalloc region, its physical address is discontinuous and needs to be increased page-by-page to bio. The helper function bio_add_vmalloc() can be used to simplify the implementation of bch2_bio_map().

---
NOTE:
The following patch needs to be applied (because the bcachefs.git repository has not been synchronized to the latest):
commit 850e210d5ad2 ("block: add a bio_add_virt_nofail helper") commit 8dd16f5e3469 ("block: add a bio_add_vmalloc helpers")

 fs/bcachefs/util.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
fs/bcachefs/util.c +4 -11
index dc3817f545fa..5e5075630bc6 100644
@@ -623,17 +623,10 @@ void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_contro
 
 void bch2_bio_map(struct bio *bio, void *base, size_t size)
 {
-	while (size) {
-		struct page *page = is_vmalloc_addr(base)
-				? vmalloc_to_page(base)
-				: virt_to_page(base);
-		unsigned offset = offset_in_page(base);
-		unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
-
-		BUG_ON(!bio_add_page(bio, page, len, offset));
-		size -= len;
-		base += len;
-	}
+	if (is_vmalloc_addr(base))
+		bio_add_vmalloc(bio, base, size);
+	else
+		bio_add_virt_nofail(bio, base, size);
 }
 
 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 bcachefs: Use bio_add_folio_nofail() for unfailable operations

From: Youling Tang <[email protected]>

Use bio_add_folio_nofail() to replace the unfailable bio_add_folio() operation.

---
 fs/bcachefs/fs-io-buffered.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/bcachefs/fs-io-buffered.c +5 -5
index 66bacdd49f78..dad48d44f47b 100644
@@ -145,7 +145,7 @@ static int readpage_bio_extend(struct btree_trans *trans,
 
 		BUG_ON(folio_sector(folio) != bio_end_sector(bio));
 
-		BUG_ON(!bio_add_folio(bio, folio, folio_size(folio), 0));
+		bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
 	}
 
 	return bch2_trans_relock(trans);
@@ -311,7 +311,7 @@ void bch2_readahead(struct readahead_control *ractl)
 		readpage_iter_advance(&readpages_iter);
 
 		rbio->bio.bi_iter.bi_sector = folio_sector(folio);
-		BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
+		bio_add_folio_nofail(&rbio->bio, folio, folio_size(folio), 0);
 
 		bchfs_read(trans, rbio, inode_inum(inode),
 			   &readpages_iter);
@@ -354,7 +354,7 @@ int bch2_read_single_folio(struct folio *folio, struct address_space *mapping)
 	rbio->bio.bi_private = &done;
 	rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
 	rbio->bio.bi_iter.bi_sector = folio_sector(folio);
-	BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
+	bio_add_folio_nofail(&rbio->bio, folio, folio_size(folio), 0);
 
 	blk_start_plug(&plug);
 	bch2_trans_run(c, (bchfs_read(trans, rbio, inode_inum(inode), NULL), 0));
@@ -639,8 +639,8 @@ static int __bch2_writepage(struct folio *folio,
 		atomic_inc(&s->write_count);
 
 		BUG_ON(inode != w->io->inode);
-		BUG_ON(!bio_add_folio(&w->io->op.wbio.bio, folio,
-				     sectors << 9, offset << 9));
+		bio_add_folio_nofail(&w->io->op.wbio.bio, folio,
+				     sectors << 9, offset << 9);
 
 		w->io->op.res.sectors += reserved_sectors;
 		w->io->op.i_sectors_delta -= dirty_sectors;
Signature · 1 line
2.34.1
#2 lore raw
In reply to Youling Tang
Quoted 6 lines · Youling Tang

From: Youling Tang <[email protected]>

For the part of directly mapping the kernel virtual address, there is no need to increase to bio page-by-page. It can be directly replaced by bio_add_virt_nofail().

Quoted 6 lines

For the address part of the vmalloc region, its physical address is discontinuous and needs to be increased page-by-page to bio. The helper function bio_add_vmalloc() can be used to simplify the implementation of bch2_bio_map().

Thanks, I'll apply these after I rebase on 6.16-rc1.

Quoted 39 lines

---
NOTE:
The following patch needs to be applied (because the bcachefs.git repository has not been synchronized to the latest):
commit 850e210d5ad2 ("block: add a bio_add_virt_nofail helper") commit 8dd16f5e3469 ("block: add a bio_add_vmalloc helpers")

 fs/bcachefs/util.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
fs/bcachefs/util.c +4 -11
index dc3817f545fa..5e5075630bc6 100644
@@ -623,17 +623,10 @@ void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_contro
 
 void bch2_bio_map(struct bio *bio, void *base, size_t size)
 {
-	while (size) {
-		struct page *page = is_vmalloc_addr(base)
-				? vmalloc_to_page(base)
-				: virt_to_page(base);
-		unsigned offset = offset_in_page(base);
-		unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
-
-		BUG_ON(!bio_add_page(bio, page, len, offset));
-		size -= len;
-		base += len;
-	}
+	if (is_vmalloc_addr(base))
+		bio_add_vmalloc(bio, base, size);
+	else
+		bio_add_virt_nofail(bio, base, size);
 }
 
 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
Signature · 1 line
2.34.1
#3 lore raw
In reply to Youling Tang
Quoted 6 lines · Youling Tang

From: Youling Tang <[email protected]>

For the part of directly mapping the kernel virtual address, there is no need to increase to bio page-by-page. It can be directly replaced by bio_add_virt_nofail().

Quoted 6 lines

For the address part of the vmalloc region, its physical address is discontinuous and needs to be increased page-by-page to bio. The helper function bio_add_vmalloc() can be used to simplify the implementation of bch2_bio_map().

Thanks, I'll apply this

Quoted 39 lines

---
NOTE:
The following patch needs to be applied (because the bcachefs.git repository has not been synchronized to the latest):
commit 850e210d5ad2 ("block: add a bio_add_virt_nofail helper") commit 8dd16f5e3469 ("block: add a bio_add_vmalloc helpers")

 fs/bcachefs/util.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
fs/bcachefs/util.c +4 -11
index dc3817f545fa..5e5075630bc6 100644
@@ -623,17 +623,10 @@ void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_contro
 
 void bch2_bio_map(struct bio *bio, void *base, size_t size)
 {
-	while (size) {
-		struct page *page = is_vmalloc_addr(base)
-				? vmalloc_to_page(base)
-				: virt_to_page(base);
-		unsigned offset = offset_in_page(base);
-		unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
-
-		BUG_ON(!bio_add_page(bio, page, len, offset));
-		size -= len;
-		base += len;
-	}
+	if (is_vmalloc_addr(base))
+		bio_add_vmalloc(bio, base, size);
+	else
+		bio_add_virt_nofail(bio, base, size);
 }
 
 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
Signature · 1 line
2.34.1