@@ -606,15 +606,15 @@ struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask)
606
606
}
607
607
EXPORT_SYMBOL (bio_kmalloc );
608
608
609
- void zero_fill_bio (struct bio * bio )
609
+ void zero_fill_bio_iter (struct bio * bio , struct bvec_iter start )
610
610
{
611
611
struct bio_vec bv ;
612
612
struct bvec_iter iter ;
613
613
614
- bio_for_each_segment (bv , bio , iter )
614
+ __bio_for_each_segment (bv , bio , iter , start )
615
615
memzero_bvec (& bv );
616
616
}
617
- EXPORT_SYMBOL (zero_fill_bio );
617
+ EXPORT_SYMBOL (zero_fill_bio_iter );
618
618
619
619
/**
620
620
* bio_truncate - truncate the bio to small size of @new_size
@@ -903,9 +903,8 @@ static inline bool bio_full(struct bio *bio, unsigned len)
903
903
return false;
904
904
}
905
905
906
- static inline bool page_is_mergeable (const struct bio_vec * bv ,
907
- struct page * page , unsigned int len , unsigned int off ,
908
- bool * same_page )
906
+ static bool bvec_try_merge_page (struct bio_vec * bv , struct page * page ,
907
+ unsigned int len , unsigned int off , bool * same_page )
909
908
{
910
909
size_t bv_end = bv -> bv_offset + bv -> bv_len ;
911
910
phys_addr_t vec_end_addr = page_to_phys (bv -> bv_page ) + bv_end - 1 ;
@@ -919,61 +918,26 @@ static inline bool page_is_mergeable(const struct bio_vec *bv,
919
918
return false;
920
919
921
920
* same_page = ((vec_end_addr & PAGE_MASK ) == page_addr );
922
- if (* same_page )
923
- return true;
924
- else if (IS_ENABLED (CONFIG_KMSAN ))
925
- return false;
926
- return (bv -> bv_page + bv_end / PAGE_SIZE ) == (page + off / PAGE_SIZE );
927
- }
928
-
929
- /**
930
- * __bio_try_merge_page - try appending data to an existing bvec.
931
- * @bio: destination bio
932
- * @page: start page to add
933
- * @len: length of the data to add
934
- * @off: offset of the data relative to @page
935
- * @same_page: return if the segment has been merged inside the same page
936
- *
937
- * Try to add the data at @page + @off to the last bvec of @bio. This is a
938
- * useful optimisation for file systems with a block size smaller than the
939
- * page size.
940
- *
941
- * Warn if (@len, @off) crosses pages in case that @same_page is true.
942
- *
943
- * Return %true on success or %false on failure.
944
- */
945
- static bool __bio_try_merge_page (struct bio * bio , struct page * page ,
946
- unsigned int len , unsigned int off , bool * same_page )
947
- {
948
- if (WARN_ON_ONCE (bio_flagged (bio , BIO_CLONED )))
949
- return false;
950
-
951
- if (bio -> bi_vcnt > 0 ) {
952
- struct bio_vec * bv = & bio -> bi_io_vec [bio -> bi_vcnt - 1 ];
953
-
954
- if (page_is_mergeable (bv , page , len , off , same_page )) {
955
- if (bio -> bi_iter .bi_size > UINT_MAX - len ) {
956
- * same_page = false;
957
- return false;
958
- }
959
- bv -> bv_len += len ;
960
- bio -> bi_iter .bi_size += len ;
961
- return true;
962
- }
921
+ if (!* same_page ) {
922
+ if (IS_ENABLED (CONFIG_KMSAN ))
923
+ return false;
924
+ if (bv -> bv_page + bv_end / PAGE_SIZE != page + off / PAGE_SIZE )
925
+ return false;
963
926
}
964
- return false;
927
+
928
+ bv -> bv_len += len ;
929
+ return true;
965
930
}
966
931
967
932
/*
968
933
* Try to merge a page into a segment, while obeying the hardware segment
969
934
* size limit. This is not for normal read/write bios, but for passthrough
970
935
* or Zone Append operations that we can't split.
971
936
*/
972
- static bool bio_try_merge_hw_seg (struct request_queue * q , struct bio * bio ,
973
- struct page * page , unsigned len ,
974
- unsigned offset , bool * same_page )
937
+ bool bvec_try_merge_hw_page (struct request_queue * q , struct bio_vec * bv ,
938
+ struct page * page , unsigned len , unsigned offset ,
939
+ bool * same_page )
975
940
{
976
- struct bio_vec * bv = & bio -> bi_io_vec [bio -> bi_vcnt - 1 ];
977
941
unsigned long mask = queue_segment_boundary (q );
978
942
phys_addr_t addr1 = page_to_phys (bv -> bv_page ) + bv -> bv_offset ;
979
943
phys_addr_t addr2 = page_to_phys (page ) + offset + len - 1 ;
@@ -982,7 +946,7 @@ static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio,
982
946
return false;
983
947
if (bv -> bv_len + len > queue_max_segment_size (q ))
984
948
return false;
985
- return __bio_try_merge_page ( bio , page , len , offset , same_page );
949
+ return bvec_try_merge_page ( bv , page , len , offset , same_page );
986
950
}
987
951
988
952
/**
@@ -1002,33 +966,33 @@ int bio_add_hw_page(struct request_queue *q, struct bio *bio,
1002
966
struct page * page , unsigned int len , unsigned int offset ,
1003
967
unsigned int max_sectors , bool * same_page )
1004
968
{
1005
- struct bio_vec * bvec ;
1006
-
1007
969
if (WARN_ON_ONCE (bio_flagged (bio , BIO_CLONED )))
1008
970
return 0 ;
1009
971
1010
- if (((bio -> bi_iter .bi_size + len ) >> 9 ) > max_sectors )
972
+ if (((bio -> bi_iter .bi_size + len ) >> SECTOR_SHIFT ) > max_sectors )
1011
973
return 0 ;
1012
974
1013
975
if (bio -> bi_vcnt > 0 ) {
1014
- if (bio_try_merge_hw_seg (q , bio , page , len , offset , same_page ))
976
+ struct bio_vec * bv = & bio -> bi_io_vec [bio -> bi_vcnt - 1 ];
977
+
978
+ if (bvec_try_merge_hw_page (q , bv , page , len , offset ,
979
+ same_page )) {
980
+ bio -> bi_iter .bi_size += len ;
1015
981
return len ;
982
+ }
983
+
984
+ if (bio -> bi_vcnt >=
985
+ min (bio -> bi_max_vecs , queue_max_segments (q )))
986
+ return 0 ;
1016
987
1017
988
/*
1018
989
* If the queue doesn't support SG gaps and adding this segment
1019
990
* would create a gap, disallow it.
1020
991
*/
1021
- bvec = & bio -> bi_io_vec [bio -> bi_vcnt - 1 ];
1022
- if (bvec_gap_to_prev (& q -> limits , bvec , offset ))
992
+ if (bvec_gap_to_prev (& q -> limits , bv , offset ))
1023
993
return 0 ;
1024
994
}
1025
995
1026
- if (bio_full (bio , len ))
1027
- return 0 ;
1028
-
1029
- if (bio -> bi_vcnt >= queue_max_segments (q ))
1030
- return 0 ;
1031
-
1032
996
bvec_set_page (& bio -> bi_io_vec [bio -> bi_vcnt ], page , len , offset );
1033
997
bio -> bi_vcnt ++ ;
1034
998
bio -> bi_iter .bi_size += len ;
@@ -1129,11 +1093,21 @@ int bio_add_page(struct bio *bio, struct page *page,
1129
1093
{
1130
1094
bool same_page = false;
1131
1095
1132
- if (!__bio_try_merge_page (bio , page , len , offset , & same_page )) {
1133
- if (bio_full (bio , len ))
1134
- return 0 ;
1135
- __bio_add_page (bio , page , len , offset );
1096
+ if (WARN_ON_ONCE (bio_flagged (bio , BIO_CLONED )))
1097
+ return 0 ;
1098
+ if (bio -> bi_iter .bi_size > UINT_MAX - len )
1099
+ return 0 ;
1100
+
1101
+ if (bio -> bi_vcnt > 0 &&
1102
+ bvec_try_merge_page (& bio -> bi_io_vec [bio -> bi_vcnt - 1 ],
1103
+ page , len , offset , & same_page )) {
1104
+ bio -> bi_iter .bi_size += len ;
1105
+ return len ;
1136
1106
}
1107
+
1108
+ if (bio -> bi_vcnt >= bio -> bi_max_vecs )
1109
+ return 0 ;
1110
+ __bio_add_page (bio , page , len , offset );
1137
1111
return len ;
1138
1112
}
1139
1113
EXPORT_SYMBOL (bio_add_page );
@@ -1207,13 +1181,18 @@ static int bio_iov_add_page(struct bio *bio, struct page *page,
1207
1181
{
1208
1182
bool same_page = false;
1209
1183
1210
- if (!__bio_try_merge_page (bio , page , len , offset , & same_page )) {
1211
- __bio_add_page (bio , page , len , offset );
1184
+ if (WARN_ON_ONCE (bio -> bi_iter .bi_size > UINT_MAX - len ))
1185
+ return - EIO ;
1186
+
1187
+ if (bio -> bi_vcnt > 0 &&
1188
+ bvec_try_merge_page (& bio -> bi_io_vec [bio -> bi_vcnt - 1 ],
1189
+ page , len , offset , & same_page )) {
1190
+ bio -> bi_iter .bi_size += len ;
1191
+ if (same_page )
1192
+ bio_release_page (bio , page );
1212
1193
return 0 ;
1213
1194
}
1214
-
1215
- if (same_page )
1216
- bio_release_page (bio , page );
1195
+ __bio_add_page (bio , page , len , offset );
1217
1196
return 0 ;
1218
1197
}
1219
1198
@@ -1252,7 +1231,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
1252
1231
struct page * * pages = (struct page * * )bv ;
1253
1232
ssize_t size , left ;
1254
1233
unsigned len , i = 0 ;
1255
- size_t offset , trim ;
1234
+ size_t offset ;
1256
1235
int ret = 0 ;
1257
1236
1258
1237
/*
@@ -1281,10 +1260,12 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
1281
1260
1282
1261
nr_pages = DIV_ROUND_UP (offset + size , PAGE_SIZE );
1283
1262
1284
- trim = size & (bdev_logical_block_size (bio -> bi_bdev ) - 1 );
1285
- iov_iter_revert (iter , trim );
1263
+ if (bio -> bi_bdev ) {
1264
+ size_t trim = size & (bdev_logical_block_size (bio -> bi_bdev ) - 1 );
1265
+ iov_iter_revert (iter , trim );
1266
+ size -= trim ;
1267
+ }
1286
1268
1287
- size -= trim ;
1288
1269
if (unlikely (!size )) {
1289
1270
ret = - EFAULT ;
1290
1271
goto out ;
@@ -1337,6 +1318,9 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
1337
1318
{
1338
1319
int ret = 0 ;
1339
1320
1321
+ if (WARN_ON_ONCE (bio_flagged (bio , BIO_CLONED )))
1322
+ return - EIO ;
1323
+
1340
1324
if (iov_iter_is_bvec (iter )) {
1341
1325
bio_iov_bvec_set (bio , iter );
1342
1326
iov_iter_advance (iter , bio -> bi_iter .bi_size );
@@ -1490,6 +1474,7 @@ void bio_set_pages_dirty(struct bio *bio)
1490
1474
set_page_dirty_lock (bvec -> bv_page );
1491
1475
}
1492
1476
}
1477
+ EXPORT_SYMBOL_GPL (bio_set_pages_dirty );
1493
1478
1494
1479
/*
1495
1480
* bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
@@ -1549,6 +1534,7 @@ void bio_check_pages_dirty(struct bio *bio)
1549
1534
spin_unlock_irqrestore (& bio_dirty_lock , flags );
1550
1535
schedule_work (& bio_dirty_work );
1551
1536
}
1537
+ EXPORT_SYMBOL_GPL (bio_check_pages_dirty );
1552
1538
1553
1539
static inline bool bio_remaining_done (struct bio * bio )
1554
1540
{
0 commit comments