Merge tag 'for-5.20/block-2022-07-29' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / block / bounce.c
index 8f7b6fe..c8f487a 100644 (file)
@@ -205,19 +205,26 @@ void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
        int rw = bio_data_dir(*bio_orig);
        struct bio_vec *to, from;
        struct bvec_iter iter;
-       unsigned i = 0;
+       unsigned i = 0, bytes = 0;
        bool bounce = false;
-       int sectors = 0;
+       int sectors;
 
        bio_for_each_segment(from, *bio_orig, iter) {
                if (i++ < BIO_MAX_VECS)
-                       sectors += from.bv_len >> 9;
+                       bytes += from.bv_len;
                if (PageHighMem(from.bv_page))
                        bounce = true;
        }
        if (!bounce)
                return;
 
+       /*
+        * Individual bvecs might not be logical block aligned. Round down
+        * the split size so that each bio is properly block size aligned,
+        * even if we do not use the full hardware limits.
+        */
+       sectors = ALIGN_DOWN(bytes, queue_logical_block_size(q)) >>
+                       SECTOR_SHIFT;
        if (sectors < bio_sectors(*bio_orig)) {
                bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split);
                bio_chain(bio, *bio_orig);