1d6d48196SJens Axboe /* 2d6d48196SJens Axboe * Functions related to segment and merge handling 3d6d48196SJens Axboe */ 4d6d48196SJens Axboe #include <linux/kernel.h> 5d6d48196SJens Axboe #include <linux/module.h> 6d6d48196SJens Axboe #include <linux/bio.h> 7d6d48196SJens Axboe #include <linux/blkdev.h> 8d6d48196SJens Axboe #include <linux/scatterlist.h> 9d6d48196SJens Axboe 10d6d48196SJens Axboe #include "blk.h" 11d6d48196SJens Axboe 1254efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 1354efd50bSKent Overstreet struct bio *bio, 14bdced438SMing Lei struct bio_set *bs, 15bdced438SMing Lei unsigned *nsegs) 1654efd50bSKent Overstreet { 1754efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 1854efd50bSKent Overstreet int alignment; 1954efd50bSKent Overstreet sector_t tmp; 2054efd50bSKent Overstreet unsigned split_sectors; 2154efd50bSKent Overstreet 22bdced438SMing Lei *nsegs = 1; 23bdced438SMing Lei 2454efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 2554efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 2654efd50bSKent Overstreet 2754efd50bSKent Overstreet max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); 2854efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 2954efd50bSKent Overstreet 3054efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 3154efd50bSKent Overstreet /* XXX: warn */ 3254efd50bSKent Overstreet return NULL; 3354efd50bSKent Overstreet } 3454efd50bSKent Overstreet 3554efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 3654efd50bSKent Overstreet return NULL; 3754efd50bSKent Overstreet 3854efd50bSKent Overstreet split_sectors = max_discard_sectors; 3954efd50bSKent Overstreet 4054efd50bSKent Overstreet /* 4154efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 4254efd50bSKent Overstreet * the previous aligned sector. 4354efd50bSKent Overstreet */ 4454efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 4554efd50bSKent Overstreet 4654efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 4754efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 4854efd50bSKent Overstreet 4954efd50bSKent Overstreet if (split_sectors > tmp) 5054efd50bSKent Overstreet split_sectors -= tmp; 5154efd50bSKent Overstreet 5254efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 5354efd50bSKent Overstreet } 5454efd50bSKent Overstreet 5554efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 5654efd50bSKent Overstreet struct bio *bio, 57bdced438SMing Lei struct bio_set *bs, 58bdced438SMing Lei unsigned *nsegs) 5954efd50bSKent Overstreet { 60bdced438SMing Lei *nsegs = 1; 61bdced438SMing Lei 6254efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 6354efd50bSKent Overstreet return NULL; 6454efd50bSKent Overstreet 6554efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 6654efd50bSKent Overstreet return NULL; 6754efd50bSKent Overstreet 6854efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 6954efd50bSKent Overstreet } 7054efd50bSKent Overstreet 7154efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 7254efd50bSKent Overstreet struct bio *bio, 73bdced438SMing Lei struct bio_set *bs, 74bdced438SMing Lei unsigned *segs) 7554efd50bSKent Overstreet { 765014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 7754efd50bSKent Overstreet struct bvec_iter iter; 788ae12666SKent Overstreet unsigned seg_size = 0, nsegs = 0, sectors = 0; 7954efd50bSKent Overstreet 8054efd50bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 8152cc6eeaSMing Lei if (sectors + (bv.bv_len >> 9) > queue_max_sectors(q)) 8254efd50bSKent Overstreet goto split; 8354efd50bSKent Overstreet 8454efd50bSKent Overstreet /* 8554efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 8654efd50bSKent Overstreet * offset would create a gap, disallow it. 8754efd50bSKent Overstreet */ 885014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 8954efd50bSKent Overstreet goto split; 9054efd50bSKent Overstreet 915014c311SJens Axboe if (bvprvp && blk_queue_cluster(q)) { 9254efd50bSKent Overstreet if (seg_size + bv.bv_len > queue_max_segment_size(q)) 9354efd50bSKent Overstreet goto new_segment; 945014c311SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv)) 9554efd50bSKent Overstreet goto new_segment; 965014c311SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv)) 9754efd50bSKent Overstreet goto new_segment; 9854efd50bSKent Overstreet 9954efd50bSKent Overstreet seg_size += bv.bv_len; 10054efd50bSKent Overstreet bvprv = bv; 1015014c311SJens Axboe bvprvp = &bv; 10252cc6eeaSMing Lei sectors += bv.bv_len >> 9; 10354efd50bSKent Overstreet continue; 10454efd50bSKent Overstreet } 10554efd50bSKent Overstreet new_segment: 10654efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 10754efd50bSKent Overstreet goto split; 10854efd50bSKent Overstreet 10954efd50bSKent Overstreet nsegs++; 11054efd50bSKent Overstreet bvprv = bv; 1115014c311SJens Axboe bvprvp = &bv; 11254efd50bSKent Overstreet seg_size = bv.bv_len; 11352cc6eeaSMing Lei sectors += bv.bv_len >> 9; 11454efd50bSKent Overstreet } 11554efd50bSKent Overstreet 116bdced438SMing Lei *segs = nsegs; 11754efd50bSKent Overstreet return NULL; 11854efd50bSKent Overstreet split: 119bdced438SMing Lei *segs = nsegs; 12052cc6eeaSMing Lei return bio_split(bio, sectors, GFP_NOIO, bs); 12154efd50bSKent Overstreet } 12254efd50bSKent Overstreet 12354efd50bSKent Overstreet void blk_queue_split(struct request_queue *q, struct bio **bio, 12454efd50bSKent Overstreet struct bio_set *bs) 12554efd50bSKent Overstreet { 126bdced438SMing Lei struct bio *split, *res; 127bdced438SMing Lei unsigned nsegs; 12854efd50bSKent Overstreet 12954efd50bSKent Overstreet if ((*bio)->bi_rw & REQ_DISCARD) 130bdced438SMing Lei split = blk_bio_discard_split(q, *bio, bs, &nsegs); 13154efd50bSKent Overstreet else if ((*bio)->bi_rw & REQ_WRITE_SAME) 132bdced438SMing Lei split = blk_bio_write_same_split(q, *bio, bs, &nsegs); 13354efd50bSKent Overstreet else 134bdced438SMing Lei split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs); 135bdced438SMing Lei 136bdced438SMing Lei /* physical segments can be figured out during splitting */ 137bdced438SMing Lei res = split ? split : *bio; 138bdced438SMing Lei res->bi_phys_segments = nsegs; 139bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 14054efd50bSKent Overstreet 14154efd50bSKent Overstreet if (split) { 142*6ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 143*6ac45aebSMing Lei split->bi_rw |= REQ_NOMERGE; 144*6ac45aebSMing Lei 14554efd50bSKent Overstreet bio_chain(split, *bio); 14654efd50bSKent Overstreet generic_make_request(*bio); 14754efd50bSKent Overstreet *bio = split; 14854efd50bSKent Overstreet } 14954efd50bSKent Overstreet } 15054efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 15154efd50bSKent Overstreet 1521e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 15307388549SMing Lei struct bio *bio, 15407388549SMing Lei bool no_sg_merge) 155d6d48196SJens Axboe { 1567988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 15754efd50bSKent Overstreet int cluster, prev = 0; 1581e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 15959247eaeSJens Axboe struct bio *fbio, *bbio; 1607988613bSKent Overstreet struct bvec_iter iter; 161d6d48196SJens Axboe 1621e428079SJens Axboe if (!bio) 1631e428079SJens Axboe return 0; 164d6d48196SJens Axboe 1655cb8850cSKent Overstreet /* 1665cb8850cSKent Overstreet * This should probably be returning 0, but blk_add_request_payload() 1675cb8850cSKent Overstreet * (Christoph!!!!) 1685cb8850cSKent Overstreet */ 1695cb8850cSKent Overstreet if (bio->bi_rw & REQ_DISCARD) 1705cb8850cSKent Overstreet return 1; 1715cb8850cSKent Overstreet 1725cb8850cSKent Overstreet if (bio->bi_rw & REQ_WRITE_SAME) 1735cb8850cSKent Overstreet return 1; 1745cb8850cSKent Overstreet 1751e428079SJens Axboe fbio = bio; 176e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 1775df97b91SMikulas Patocka seg_size = 0; 1782c8919deSAndi Kleen nr_phys_segs = 0; 1791e428079SJens Axboe for_each_bio(bio) { 1807988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 181d6d48196SJens Axboe /* 18205f1dd53SJens Axboe * If SG merging is disabled, each bio vector is 18305f1dd53SJens Axboe * a segment 18405f1dd53SJens Axboe */ 18505f1dd53SJens Axboe if (no_sg_merge) 18605f1dd53SJens Axboe goto new_segment; 18705f1dd53SJens Axboe 18854efd50bSKent Overstreet if (prev && cluster) { 1897988613bSKent Overstreet if (seg_size + bv.bv_len 190ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 191d6d48196SJens Axboe goto new_segment; 1927988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) 193d6d48196SJens Axboe goto new_segment; 1947988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) 195d6d48196SJens Axboe goto new_segment; 196d6d48196SJens Axboe 1977988613bSKent Overstreet seg_size += bv.bv_len; 198d6d48196SJens Axboe bvprv = bv; 199d6d48196SJens Axboe continue; 200d6d48196SJens Axboe } 201d6d48196SJens Axboe new_segment: 2021e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 2031e428079SJens Axboe fbio->bi_seg_front_size) 2041e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 20586771427SFUJITA Tomonori 206d6d48196SJens Axboe nr_phys_segs++; 207d6d48196SJens Axboe bvprv = bv; 20854efd50bSKent Overstreet prev = 1; 2097988613bSKent Overstreet seg_size = bv.bv_len; 210d6d48196SJens Axboe } 21159247eaeSJens Axboe bbio = bio; 2121e428079SJens Axboe } 213d6d48196SJens Axboe 21459247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 21559247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 21659247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 21759247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 2181e428079SJens Axboe 2191e428079SJens Axboe return nr_phys_segs; 2201e428079SJens Axboe } 2211e428079SJens Axboe 2221e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 2231e428079SJens Axboe { 22407388549SMing Lei bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, 22507388549SMing Lei &rq->q->queue_flags); 22607388549SMing Lei 22707388549SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, 22807388549SMing Lei no_sg_merge); 229d6d48196SJens Axboe } 230d6d48196SJens Axboe 231d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 232d6d48196SJens Axboe { 2337f60dcaaSMing Lei unsigned short seg_cnt; 234764f612cSMing Lei 2357f60dcaaSMing Lei /* estimate segment number by bi_vcnt for non-cloned bio */ 2367f60dcaaSMing Lei if (bio_flagged(bio, BIO_CLONED)) 2377f60dcaaSMing Lei seg_cnt = bio_segments(bio); 2387f60dcaaSMing Lei else 2397f60dcaaSMing Lei seg_cnt = bio->bi_vcnt; 2407f60dcaaSMing Lei 2417f60dcaaSMing Lei if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && 2427f60dcaaSMing Lei (seg_cnt < queue_max_segments(q))) 2437f60dcaaSMing Lei bio->bi_phys_segments = seg_cnt; 24405f1dd53SJens Axboe else { 245d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 2461e428079SJens Axboe 247d6d48196SJens Axboe bio->bi_next = NULL; 2487f60dcaaSMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); 249d6d48196SJens Axboe bio->bi_next = nxt; 25005f1dd53SJens Axboe } 25105f1dd53SJens Axboe 252b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 253d6d48196SJens Axboe } 254d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 255d6d48196SJens Axboe 256d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 257d6d48196SJens Axboe struct bio *nxt) 258d6d48196SJens Axboe { 2592b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 260f619d254SKent Overstreet struct bvec_iter iter; 261f619d254SKent Overstreet 262e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 263d6d48196SJens Axboe return 0; 264d6d48196SJens Axboe 26586771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 266ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 267d6d48196SJens Axboe return 0; 268d6d48196SJens Axboe 269e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 270e17fc0a1SDavid Woodhouse return 1; 271e17fc0a1SDavid Woodhouse 272f619d254SKent Overstreet bio_for_each_segment(end_bv, bio, iter) 273f619d254SKent Overstreet if (end_bv.bv_len == iter.bi_size) 274f619d254SKent Overstreet break; 275f619d254SKent Overstreet 276f619d254SKent Overstreet nxt_bv = bio_iovec(nxt); 277f619d254SKent Overstreet 278f619d254SKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) 279e17fc0a1SDavid Woodhouse return 0; 280e17fc0a1SDavid Woodhouse 281d6d48196SJens Axboe /* 282e17fc0a1SDavid Woodhouse * bio and nxt are contiguous in memory; check if the queue allows 283d6d48196SJens Axboe * these two to be merged into one 284d6d48196SJens Axboe */ 285f619d254SKent Overstreet if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) 286d6d48196SJens Axboe return 1; 287d6d48196SJens Axboe 288d6d48196SJens Axboe return 0; 289d6d48196SJens Axboe } 290d6d48196SJens Axboe 2917988613bSKent Overstreet static inline void 292963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 2937988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 294963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 295963ab9e5SAsias He { 296963ab9e5SAsias He 297963ab9e5SAsias He int nbytes = bvec->bv_len; 298963ab9e5SAsias He 2997988613bSKent Overstreet if (*sg && *cluster) { 300963ab9e5SAsias He if ((*sg)->length + nbytes > queue_max_segment_size(q)) 301963ab9e5SAsias He goto new_segment; 302963ab9e5SAsias He 3037988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 304963ab9e5SAsias He goto new_segment; 3057988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 306963ab9e5SAsias He goto new_segment; 307963ab9e5SAsias He 308963ab9e5SAsias He (*sg)->length += nbytes; 309963ab9e5SAsias He } else { 310963ab9e5SAsias He new_segment: 311963ab9e5SAsias He if (!*sg) 312963ab9e5SAsias He *sg = sglist; 313963ab9e5SAsias He else { 314963ab9e5SAsias He /* 315963ab9e5SAsias He * If the driver previously mapped a shorter 316963ab9e5SAsias He * list, we could see a termination bit 317963ab9e5SAsias He * prematurely unless it fully inits the sg 318963ab9e5SAsias He * table on each mapping. We KNOW that there 319963ab9e5SAsias He * must be more entries here or the driver 320963ab9e5SAsias He * would be buggy, so force clear the 321963ab9e5SAsias He * termination bit to avoid doing a full 322963ab9e5SAsias He * sg_init_table() in drivers for each command. 323963ab9e5SAsias He */ 324c8164d89SPaolo Bonzini sg_unmark_end(*sg); 325963ab9e5SAsias He *sg = sg_next(*sg); 326963ab9e5SAsias He } 327963ab9e5SAsias He 328963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 329963ab9e5SAsias He (*nsegs)++; 330963ab9e5SAsias He } 3317988613bSKent Overstreet *bvprv = *bvec; 332963ab9e5SAsias He } 333963ab9e5SAsias He 3345cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 3355cb8850cSKent Overstreet struct scatterlist *sglist, 3365cb8850cSKent Overstreet struct scatterlist **sg) 3375cb8850cSKent Overstreet { 3385cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 3395cb8850cSKent Overstreet struct bvec_iter iter; 3405cb8850cSKent Overstreet int nsegs, cluster; 3415cb8850cSKent Overstreet 3425cb8850cSKent Overstreet nsegs = 0; 3435cb8850cSKent Overstreet cluster = blk_queue_cluster(q); 3445cb8850cSKent Overstreet 3455cb8850cSKent Overstreet if (bio->bi_rw & REQ_DISCARD) { 3465cb8850cSKent Overstreet /* 3475cb8850cSKent Overstreet * This is a hack - drivers should be neither modifying the 3485cb8850cSKent Overstreet * biovec, nor relying on bi_vcnt - but because of 3495cb8850cSKent Overstreet * blk_add_request_payload(), a discard bio may or may not have 3505cb8850cSKent Overstreet * a payload we need to set up here (thank you Christoph) and 3515cb8850cSKent Overstreet * bi_vcnt is really the only way of telling if we need to. 3525cb8850cSKent Overstreet */ 3535cb8850cSKent Overstreet 3545cb8850cSKent Overstreet if (bio->bi_vcnt) 3555cb8850cSKent Overstreet goto single_segment; 3565cb8850cSKent Overstreet 3575cb8850cSKent Overstreet return 0; 3585cb8850cSKent Overstreet } 3595cb8850cSKent Overstreet 3605cb8850cSKent Overstreet if (bio->bi_rw & REQ_WRITE_SAME) { 3615cb8850cSKent Overstreet single_segment: 3625cb8850cSKent Overstreet *sg = sglist; 3635cb8850cSKent Overstreet bvec = bio_iovec(bio); 3645cb8850cSKent Overstreet sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset); 3655cb8850cSKent Overstreet return 1; 3665cb8850cSKent Overstreet } 3675cb8850cSKent Overstreet 3685cb8850cSKent Overstreet for_each_bio(bio) 3695cb8850cSKent Overstreet bio_for_each_segment(bvec, bio, iter) 3705cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 3715cb8850cSKent Overstreet &nsegs, &cluster); 3725cb8850cSKent Overstreet 3735cb8850cSKent Overstreet return nsegs; 3745cb8850cSKent Overstreet } 3755cb8850cSKent Overstreet 376d6d48196SJens Axboe /* 377d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 378d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 379d6d48196SJens Axboe */ 380d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 381d6d48196SJens Axboe struct scatterlist *sglist) 382d6d48196SJens Axboe { 3835cb8850cSKent Overstreet struct scatterlist *sg = NULL; 3845cb8850cSKent Overstreet int nsegs = 0; 385d6d48196SJens Axboe 3865cb8850cSKent Overstreet if (rq->bio) 3875cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 388f18573abSFUJITA Tomonori 389f18573abSFUJITA Tomonori if (unlikely(rq->cmd_flags & REQ_COPY_USER) && 3902e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 3912e46e8b2STejun Heo unsigned int pad_len = 3922e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 393f18573abSFUJITA Tomonori 394f18573abSFUJITA Tomonori sg->length += pad_len; 395f18573abSFUJITA Tomonori rq->extra_len += pad_len; 396f18573abSFUJITA Tomonori } 397f18573abSFUJITA Tomonori 3982fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 3997b6d91daSChristoph Hellwig if (rq->cmd_flags & REQ_WRITE) 400db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 401db0a2e00STejun Heo 402da81ed16SDan Williams sg_unmark_end(sg); 403d6d48196SJens Axboe sg = sg_next(sg); 404d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 405d6d48196SJens Axboe q->dma_drain_size, 406d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 407d6d48196SJens Axboe (PAGE_SIZE - 1)); 408d6d48196SJens Axboe nsegs++; 4097a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 410d6d48196SJens Axboe } 411d6d48196SJens Axboe 412d6d48196SJens Axboe if (sg) 413d6d48196SJens Axboe sg_mark_end(sg); 414d6d48196SJens Axboe 415d6d48196SJens Axboe return nsegs; 416d6d48196SJens Axboe } 417d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 418d6d48196SJens Axboe 419d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 420d6d48196SJens Axboe struct request *req, 421d6d48196SJens Axboe struct bio *bio) 422d6d48196SJens Axboe { 423d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 424d6d48196SJens Axboe 42513f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 42613f05c8dSMartin K. Petersen goto no_merge; 42713f05c8dSMartin K. Petersen 4284eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 42913f05c8dSMartin K. Petersen goto no_merge; 430d6d48196SJens Axboe 431d6d48196SJens Axboe /* 432d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 433d6d48196SJens Axboe * counters. 434d6d48196SJens Axboe */ 435d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 436d6d48196SJens Axboe return 1; 43713f05c8dSMartin K. Petersen 43813f05c8dSMartin K. Petersen no_merge: 43913f05c8dSMartin K. Petersen req->cmd_flags |= REQ_NOMERGE; 44013f05c8dSMartin K. Petersen if (req == q->last_merge) 44113f05c8dSMartin K. Petersen q->last_merge = NULL; 44213f05c8dSMartin K. Petersen return 0; 443d6d48196SJens Axboe } 444d6d48196SJens Axboe 445d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 446d6d48196SJens Axboe struct bio *bio) 447d6d48196SJens Axboe { 4485e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 4495e7c4274SJens Axboe return 0; 4507f39add3SSagi Grimberg if (blk_integrity_rq(req) && 4517f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 4527f39add3SSagi Grimberg return 0; 453f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 454f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) { 455d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 456d6d48196SJens Axboe if (req == q->last_merge) 457d6d48196SJens Axboe q->last_merge = NULL; 458d6d48196SJens Axboe return 0; 459d6d48196SJens Axboe } 4602cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 461d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 4622cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 463d6d48196SJens Axboe blk_recount_segments(q, bio); 464d6d48196SJens Axboe 465d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 466d6d48196SJens Axboe } 467d6d48196SJens Axboe 468d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 469d6d48196SJens Axboe struct bio *bio) 470d6d48196SJens Axboe { 4715e7c4274SJens Axboe 4725e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 4735e7c4274SJens Axboe return 0; 4747f39add3SSagi Grimberg if (blk_integrity_rq(req) && 4757f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 4767f39add3SSagi Grimberg return 0; 477f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 478f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) { 479d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 480d6d48196SJens Axboe if (req == q->last_merge) 481d6d48196SJens Axboe q->last_merge = NULL; 482d6d48196SJens Axboe return 0; 483d6d48196SJens Axboe } 4842cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 485d6d48196SJens Axboe blk_recount_segments(q, bio); 4862cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 487d6d48196SJens Axboe blk_recount_segments(q, req->bio); 488d6d48196SJens Axboe 489d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 490d6d48196SJens Axboe } 491d6d48196SJens Axboe 492e7e24500SJens Axboe /* 493e7e24500SJens Axboe * blk-mq uses req->special to carry normal driver per-request payload, it 494e7e24500SJens Axboe * does not indicate a prepared command that we cannot merge with. 495e7e24500SJens Axboe */ 496e7e24500SJens Axboe static bool req_no_special_merge(struct request *req) 497e7e24500SJens Axboe { 498e7e24500SJens Axboe struct request_queue *q = req->q; 499e7e24500SJens Axboe 500e7e24500SJens Axboe return !q->mq_ops && req->special; 501e7e24500SJens Axboe } 502e7e24500SJens Axboe 503d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 504d6d48196SJens Axboe struct request *next) 505d6d48196SJens Axboe { 506d6d48196SJens Axboe int total_phys_segments; 50786771427SFUJITA Tomonori unsigned int seg_size = 50886771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 509d6d48196SJens Axboe 510d6d48196SJens Axboe /* 511d6d48196SJens Axboe * First check if the either of the requests are re-queued 512d6d48196SJens Axboe * requests. Can't merge them if they are. 513d6d48196SJens Axboe */ 514e7e24500SJens Axboe if (req_no_special_merge(req) || req_no_special_merge(next)) 515d6d48196SJens Axboe return 0; 516d6d48196SJens Axboe 5175e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 518854fbb9cSKeith Busch return 0; 519854fbb9cSKeith Busch 520d6d48196SJens Axboe /* 521d6d48196SJens Axboe * Will it become too large? 522d6d48196SJens Axboe */ 523f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 524f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) 525d6d48196SJens Axboe return 0; 526d6d48196SJens Axboe 527d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 52886771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 52986771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 53086771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 53186771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 53286771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 533d6d48196SJens Axboe total_phys_segments--; 53486771427SFUJITA Tomonori } 535d6d48196SJens Axboe 5368a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 537d6d48196SJens Axboe return 0; 538d6d48196SJens Axboe 5394eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 54013f05c8dSMartin K. Petersen return 0; 54113f05c8dSMartin K. Petersen 542d6d48196SJens Axboe /* Merge is OK... */ 543d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 544d6d48196SJens Axboe return 1; 545d6d48196SJens Axboe } 546d6d48196SJens Axboe 54780a761fdSTejun Heo /** 54880a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 54980a761fdSTejun Heo * @rq: request to mark as mixed merge 55080a761fdSTejun Heo * 55180a761fdSTejun Heo * Description: 55280a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 55380a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 55480a761fdSTejun Heo * merged. 55580a761fdSTejun Heo */ 55680a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 55780a761fdSTejun Heo { 55880a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 55980a761fdSTejun Heo struct bio *bio; 56080a761fdSTejun Heo 56180a761fdSTejun Heo if (rq->cmd_flags & REQ_MIXED_MERGE) 56280a761fdSTejun Heo return; 56380a761fdSTejun Heo 56480a761fdSTejun Heo /* 56580a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 56680a761fdSTejun Heo * contained bios. It will just track those of the first one. 56780a761fdSTejun Heo * Distributes the attributs to each bio. 56880a761fdSTejun Heo */ 56980a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 57080a761fdSTejun Heo WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && 57180a761fdSTejun Heo (bio->bi_rw & REQ_FAILFAST_MASK) != ff); 57280a761fdSTejun Heo bio->bi_rw |= ff; 57380a761fdSTejun Heo } 57480a761fdSTejun Heo rq->cmd_flags |= REQ_MIXED_MERGE; 57580a761fdSTejun Heo } 57680a761fdSTejun Heo 57726308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 57826308eabSJerome Marchand { 57926308eabSJerome Marchand if (blk_do_io_stat(req)) { 58026308eabSJerome Marchand struct hd_struct *part; 58126308eabSJerome Marchand int cpu; 58226308eabSJerome Marchand 58326308eabSJerome Marchand cpu = part_stat_lock(); 58409e099d4SJerome Marchand part = req->part; 58526308eabSJerome Marchand 58626308eabSJerome Marchand part_round_stats(cpu, part); 587316d315bSNikanth Karthikesan part_dec_in_flight(part, rq_data_dir(req)); 58826308eabSJerome Marchand 5896c23a968SJens Axboe hd_struct_put(part); 59026308eabSJerome Marchand part_stat_unlock(); 59126308eabSJerome Marchand } 59226308eabSJerome Marchand } 59326308eabSJerome Marchand 594d6d48196SJens Axboe /* 595d6d48196SJens Axboe * Has to be called with the request spinlock acquired 596d6d48196SJens Axboe */ 597d6d48196SJens Axboe static int attempt_merge(struct request_queue *q, struct request *req, 598d6d48196SJens Axboe struct request *next) 599d6d48196SJens Axboe { 600d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 601d6d48196SJens Axboe return 0; 602d6d48196SJens Axboe 603f31dc1cdSMartin K. Petersen if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags)) 604f31dc1cdSMartin K. Petersen return 0; 605f31dc1cdSMartin K. Petersen 606d6d48196SJens Axboe /* 607d6d48196SJens Axboe * not contiguous 608d6d48196SJens Axboe */ 60983096ebfSTejun Heo if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) 610d6d48196SJens Axboe return 0; 611d6d48196SJens Axboe 612d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 613d6d48196SJens Axboe || req->rq_disk != next->rq_disk 614e7e24500SJens Axboe || req_no_special_merge(next)) 615d6d48196SJens Axboe return 0; 616d6d48196SJens Axboe 6174363ac7cSMartin K. Petersen if (req->cmd_flags & REQ_WRITE_SAME && 6184363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 6194363ac7cSMartin K. Petersen return 0; 6204363ac7cSMartin K. Petersen 621d6d48196SJens Axboe /* 622d6d48196SJens Axboe * If we are allowed to merge, then append bio list 623d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 624d6d48196SJens Axboe * will have updated segment counts, update sector 625d6d48196SJens Axboe * counts here. 626d6d48196SJens Axboe */ 627d6d48196SJens Axboe if (!ll_merge_requests_fn(q, req, next)) 628d6d48196SJens Axboe return 0; 629d6d48196SJens Axboe 630d6d48196SJens Axboe /* 63180a761fdSTejun Heo * If failfast settings disagree or any of the two is already 63280a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 63380a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 63480a761fdSTejun Heo * set properly. 63580a761fdSTejun Heo */ 63680a761fdSTejun Heo if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || 63780a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 63880a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 63980a761fdSTejun Heo blk_rq_set_mixed_merge(req); 64080a761fdSTejun Heo blk_rq_set_mixed_merge(next); 64180a761fdSTejun Heo } 64280a761fdSTejun Heo 64380a761fdSTejun Heo /* 644d6d48196SJens Axboe * At this point we have either done a back merge 645d6d48196SJens Axboe * or front merge. We need the smaller start_time of 646d6d48196SJens Axboe * the merged requests to be the current request 647d6d48196SJens Axboe * for accounting purposes. 648d6d48196SJens Axboe */ 649d6d48196SJens Axboe if (time_after(req->start_time, next->start_time)) 650d6d48196SJens Axboe req->start_time = next->start_time; 651d6d48196SJens Axboe 652d6d48196SJens Axboe req->biotail->bi_next = next->bio; 653d6d48196SJens Axboe req->biotail = next->biotail; 654d6d48196SJens Axboe 655a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 656d6d48196SJens Axboe 657d6d48196SJens Axboe elv_merge_requests(q, req, next); 658d6d48196SJens Axboe 65942dad764SJerome Marchand /* 66042dad764SJerome Marchand * 'next' is going away, so update stats accordingly 66142dad764SJerome Marchand */ 66242dad764SJerome Marchand blk_account_io_merge(next); 663d6d48196SJens Axboe 664d6d48196SJens Axboe req->ioprio = ioprio_best(req->ioprio, next->ioprio); 665ab780f1eSJens Axboe if (blk_rq_cpu_valid(next)) 666ab780f1eSJens Axboe req->cpu = next->cpu; 667d6d48196SJens Axboe 6681cd96c24SBoaz Harrosh /* owner-ship of bio passed from next to req */ 6691cd96c24SBoaz Harrosh next->bio = NULL; 670d6d48196SJens Axboe __blk_put_request(q, next); 671d6d48196SJens Axboe return 1; 672d6d48196SJens Axboe } 673d6d48196SJens Axboe 674d6d48196SJens Axboe int attempt_back_merge(struct request_queue *q, struct request *rq) 675d6d48196SJens Axboe { 676d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 677d6d48196SJens Axboe 678d6d48196SJens Axboe if (next) 679d6d48196SJens Axboe return attempt_merge(q, rq, next); 680d6d48196SJens Axboe 681d6d48196SJens Axboe return 0; 682d6d48196SJens Axboe } 683d6d48196SJens Axboe 684d6d48196SJens Axboe int attempt_front_merge(struct request_queue *q, struct request *rq) 685d6d48196SJens Axboe { 686d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 687d6d48196SJens Axboe 688d6d48196SJens Axboe if (prev) 689d6d48196SJens Axboe return attempt_merge(q, prev, rq); 690d6d48196SJens Axboe 691d6d48196SJens Axboe return 0; 692d6d48196SJens Axboe } 6935e84ea3aSJens Axboe 6945e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 6955e84ea3aSJens Axboe struct request *next) 6965e84ea3aSJens Axboe { 6975e84ea3aSJens Axboe return attempt_merge(q, rq, next); 6985e84ea3aSJens Axboe } 699050c8ea8STejun Heo 700050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 701050c8ea8STejun Heo { 702e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 703050c8ea8STejun Heo return false; 704050c8ea8STejun Heo 705f31dc1cdSMartin K. Petersen if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw)) 706f31dc1cdSMartin K. Petersen return false; 707f31dc1cdSMartin K. Petersen 708050c8ea8STejun Heo /* different data direction or already started, don't merge */ 709050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 710050c8ea8STejun Heo return false; 711050c8ea8STejun Heo 712050c8ea8STejun Heo /* must be same device and not a special request */ 713e7e24500SJens Axboe if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq)) 714050c8ea8STejun Heo return false; 715050c8ea8STejun Heo 716050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 7174eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 718050c8ea8STejun Heo return false; 719050c8ea8STejun Heo 7204363ac7cSMartin K. Petersen /* must be using the same buffer */ 7214363ac7cSMartin K. Petersen if (rq->cmd_flags & REQ_WRITE_SAME && 7224363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 7234363ac7cSMartin K. Petersen return false; 7244363ac7cSMartin K. Petersen 725050c8ea8STejun Heo return true; 726050c8ea8STejun Heo } 727050c8ea8STejun Heo 728050c8ea8STejun Heo int blk_try_merge(struct request *rq, struct bio *bio) 729050c8ea8STejun Heo { 7304f024f37SKent Overstreet if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 731050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 7324f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 733050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 734050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 735050c8ea8STejun Heo } 736