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; 79*02e70742SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 80*02e70742SMing Lei bool do_split = true; 81*02e70742SMing Lei struct bio *new = NULL; 8254efd50bSKent Overstreet 8354efd50bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 8452cc6eeaSMing Lei if (sectors + (bv.bv_len >> 9) > queue_max_sectors(q)) 8554efd50bSKent Overstreet goto split; 8654efd50bSKent Overstreet 8754efd50bSKent Overstreet /* 8854efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 8954efd50bSKent Overstreet * offset would create a gap, disallow it. 9054efd50bSKent Overstreet */ 915014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 9254efd50bSKent Overstreet goto split; 9354efd50bSKent Overstreet 945014c311SJens Axboe if (bvprvp && blk_queue_cluster(q)) { 9554efd50bSKent Overstreet if (seg_size + bv.bv_len > queue_max_segment_size(q)) 9654efd50bSKent Overstreet goto new_segment; 975014c311SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv)) 9854efd50bSKent Overstreet goto new_segment; 995014c311SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv)) 10054efd50bSKent Overstreet goto new_segment; 10154efd50bSKent Overstreet 10254efd50bSKent Overstreet seg_size += bv.bv_len; 10354efd50bSKent Overstreet bvprv = bv; 104578270bfSMing Lei bvprvp = &bvprv; 10552cc6eeaSMing Lei sectors += bv.bv_len >> 9; 10654efd50bSKent Overstreet continue; 10754efd50bSKent Overstreet } 10854efd50bSKent Overstreet new_segment: 10954efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 11054efd50bSKent Overstreet goto split; 11154efd50bSKent Overstreet 11254efd50bSKent Overstreet nsegs++; 11354efd50bSKent Overstreet bvprv = bv; 114578270bfSMing Lei bvprvp = &bvprv; 11554efd50bSKent Overstreet seg_size = bv.bv_len; 11652cc6eeaSMing Lei sectors += bv.bv_len >> 9; 117*02e70742SMing Lei 118*02e70742SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 119*02e70742SMing Lei front_seg_size = seg_size; 12054efd50bSKent Overstreet } 12154efd50bSKent Overstreet 122*02e70742SMing Lei do_split = false; 12354efd50bSKent Overstreet split: 124bdced438SMing Lei *segs = nsegs; 125*02e70742SMing Lei 126*02e70742SMing Lei if (do_split) { 127*02e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 128*02e70742SMing Lei if (new) 129*02e70742SMing Lei bio = new; 130*02e70742SMing Lei } 131*02e70742SMing Lei 132*02e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 133*02e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 134*02e70742SMing Lei bio->bi_seg_back_size = seg_size; 135*02e70742SMing Lei 136*02e70742SMing Lei return do_split ? new : NULL; 13754efd50bSKent Overstreet } 13854efd50bSKent Overstreet 13954efd50bSKent Overstreet void blk_queue_split(struct request_queue *q, struct bio **bio, 14054efd50bSKent Overstreet struct bio_set *bs) 14154efd50bSKent Overstreet { 142bdced438SMing Lei struct bio *split, *res; 143bdced438SMing Lei unsigned nsegs; 14454efd50bSKent Overstreet 14554efd50bSKent Overstreet if ((*bio)->bi_rw & REQ_DISCARD) 146bdced438SMing Lei split = blk_bio_discard_split(q, *bio, bs, &nsegs); 14754efd50bSKent Overstreet else if ((*bio)->bi_rw & REQ_WRITE_SAME) 148bdced438SMing Lei split = blk_bio_write_same_split(q, *bio, bs, &nsegs); 14954efd50bSKent Overstreet else 150bdced438SMing Lei split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs); 151bdced438SMing Lei 152bdced438SMing Lei /* physical segments can be figured out during splitting */ 153bdced438SMing Lei res = split ? split : *bio; 154bdced438SMing Lei res->bi_phys_segments = nsegs; 155bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 15654efd50bSKent Overstreet 15754efd50bSKent Overstreet if (split) { 1586ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 1596ac45aebSMing Lei split->bi_rw |= REQ_NOMERGE; 1606ac45aebSMing Lei 16154efd50bSKent Overstreet bio_chain(split, *bio); 16254efd50bSKent Overstreet generic_make_request(*bio); 16354efd50bSKent Overstreet *bio = split; 16454efd50bSKent Overstreet } 16554efd50bSKent Overstreet } 16654efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 16754efd50bSKent Overstreet 1681e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 16907388549SMing Lei struct bio *bio, 17007388549SMing Lei bool no_sg_merge) 171d6d48196SJens Axboe { 1727988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 17354efd50bSKent Overstreet int cluster, prev = 0; 1741e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 17559247eaeSJens Axboe struct bio *fbio, *bbio; 1767988613bSKent Overstreet struct bvec_iter iter; 177d6d48196SJens Axboe 1781e428079SJens Axboe if (!bio) 1791e428079SJens Axboe return 0; 180d6d48196SJens Axboe 1815cb8850cSKent Overstreet /* 1825cb8850cSKent Overstreet * This should probably be returning 0, but blk_add_request_payload() 1835cb8850cSKent Overstreet * (Christoph!!!!) 1845cb8850cSKent Overstreet */ 1855cb8850cSKent Overstreet if (bio->bi_rw & REQ_DISCARD) 1865cb8850cSKent Overstreet return 1; 1875cb8850cSKent Overstreet 1885cb8850cSKent Overstreet if (bio->bi_rw & REQ_WRITE_SAME) 1895cb8850cSKent Overstreet return 1; 1905cb8850cSKent Overstreet 1911e428079SJens Axboe fbio = bio; 192e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 1935df97b91SMikulas Patocka seg_size = 0; 1942c8919deSAndi Kleen nr_phys_segs = 0; 1951e428079SJens Axboe for_each_bio(bio) { 1967988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 197d6d48196SJens Axboe /* 19805f1dd53SJens Axboe * If SG merging is disabled, each bio vector is 19905f1dd53SJens Axboe * a segment 20005f1dd53SJens Axboe */ 20105f1dd53SJens Axboe if (no_sg_merge) 20205f1dd53SJens Axboe goto new_segment; 20305f1dd53SJens Axboe 20454efd50bSKent Overstreet if (prev && cluster) { 2057988613bSKent Overstreet if (seg_size + bv.bv_len 206ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 207d6d48196SJens Axboe goto new_segment; 2087988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) 209d6d48196SJens Axboe goto new_segment; 2107988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) 211d6d48196SJens Axboe goto new_segment; 212d6d48196SJens Axboe 2137988613bSKent Overstreet seg_size += bv.bv_len; 214d6d48196SJens Axboe bvprv = bv; 215d6d48196SJens Axboe continue; 216d6d48196SJens Axboe } 217d6d48196SJens Axboe new_segment: 2181e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 2191e428079SJens Axboe fbio->bi_seg_front_size) 2201e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 22186771427SFUJITA Tomonori 222d6d48196SJens Axboe nr_phys_segs++; 223d6d48196SJens Axboe bvprv = bv; 22454efd50bSKent Overstreet prev = 1; 2257988613bSKent Overstreet seg_size = bv.bv_len; 226d6d48196SJens Axboe } 22759247eaeSJens Axboe bbio = bio; 2281e428079SJens Axboe } 229d6d48196SJens Axboe 23059247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 23159247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 23259247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 23359247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 2341e428079SJens Axboe 2351e428079SJens Axboe return nr_phys_segs; 2361e428079SJens Axboe } 2371e428079SJens Axboe 2381e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 2391e428079SJens Axboe { 24007388549SMing Lei bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, 24107388549SMing Lei &rq->q->queue_flags); 24207388549SMing Lei 24307388549SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, 24407388549SMing Lei no_sg_merge); 245d6d48196SJens Axboe } 246d6d48196SJens Axboe 247d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 248d6d48196SJens Axboe { 2497f60dcaaSMing Lei unsigned short seg_cnt; 250764f612cSMing Lei 2517f60dcaaSMing Lei /* estimate segment number by bi_vcnt for non-cloned bio */ 2527f60dcaaSMing Lei if (bio_flagged(bio, BIO_CLONED)) 2537f60dcaaSMing Lei seg_cnt = bio_segments(bio); 2547f60dcaaSMing Lei else 2557f60dcaaSMing Lei seg_cnt = bio->bi_vcnt; 2567f60dcaaSMing Lei 2577f60dcaaSMing Lei if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && 2587f60dcaaSMing Lei (seg_cnt < queue_max_segments(q))) 2597f60dcaaSMing Lei bio->bi_phys_segments = seg_cnt; 26005f1dd53SJens Axboe else { 261d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 2621e428079SJens Axboe 263d6d48196SJens Axboe bio->bi_next = NULL; 2647f60dcaaSMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); 265d6d48196SJens Axboe bio->bi_next = nxt; 26605f1dd53SJens Axboe } 26705f1dd53SJens Axboe 268b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 269d6d48196SJens Axboe } 270d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 271d6d48196SJens Axboe 272d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 273d6d48196SJens Axboe struct bio *nxt) 274d6d48196SJens Axboe { 2752b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 276f619d254SKent Overstreet struct bvec_iter iter; 277f619d254SKent Overstreet 278e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 279d6d48196SJens Axboe return 0; 280d6d48196SJens Axboe 28186771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 282ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 283d6d48196SJens Axboe return 0; 284d6d48196SJens Axboe 285e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 286e17fc0a1SDavid Woodhouse return 1; 287e17fc0a1SDavid Woodhouse 288f619d254SKent Overstreet bio_for_each_segment(end_bv, bio, iter) 289f619d254SKent Overstreet if (end_bv.bv_len == iter.bi_size) 290f619d254SKent Overstreet break; 291f619d254SKent Overstreet 292f619d254SKent Overstreet nxt_bv = bio_iovec(nxt); 293f619d254SKent Overstreet 294f619d254SKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) 295e17fc0a1SDavid Woodhouse return 0; 296e17fc0a1SDavid Woodhouse 297d6d48196SJens Axboe /* 298e17fc0a1SDavid Woodhouse * bio and nxt are contiguous in memory; check if the queue allows 299d6d48196SJens Axboe * these two to be merged into one 300d6d48196SJens Axboe */ 301f619d254SKent Overstreet if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) 302d6d48196SJens Axboe return 1; 303d6d48196SJens Axboe 304d6d48196SJens Axboe return 0; 305d6d48196SJens Axboe } 306d6d48196SJens Axboe 3077988613bSKent Overstreet static inline void 308963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 3097988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 310963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 311963ab9e5SAsias He { 312963ab9e5SAsias He 313963ab9e5SAsias He int nbytes = bvec->bv_len; 314963ab9e5SAsias He 3157988613bSKent Overstreet if (*sg && *cluster) { 316963ab9e5SAsias He if ((*sg)->length + nbytes > queue_max_segment_size(q)) 317963ab9e5SAsias He goto new_segment; 318963ab9e5SAsias He 3197988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 320963ab9e5SAsias He goto new_segment; 3217988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 322963ab9e5SAsias He goto new_segment; 323963ab9e5SAsias He 324963ab9e5SAsias He (*sg)->length += nbytes; 325963ab9e5SAsias He } else { 326963ab9e5SAsias He new_segment: 327963ab9e5SAsias He if (!*sg) 328963ab9e5SAsias He *sg = sglist; 329963ab9e5SAsias He else { 330963ab9e5SAsias He /* 331963ab9e5SAsias He * If the driver previously mapped a shorter 332963ab9e5SAsias He * list, we could see a termination bit 333963ab9e5SAsias He * prematurely unless it fully inits the sg 334963ab9e5SAsias He * table on each mapping. We KNOW that there 335963ab9e5SAsias He * must be more entries here or the driver 336963ab9e5SAsias He * would be buggy, so force clear the 337963ab9e5SAsias He * termination bit to avoid doing a full 338963ab9e5SAsias He * sg_init_table() in drivers for each command. 339963ab9e5SAsias He */ 340c8164d89SPaolo Bonzini sg_unmark_end(*sg); 341963ab9e5SAsias He *sg = sg_next(*sg); 342963ab9e5SAsias He } 343963ab9e5SAsias He 344963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 345963ab9e5SAsias He (*nsegs)++; 346963ab9e5SAsias He } 3477988613bSKent Overstreet *bvprv = *bvec; 348963ab9e5SAsias He } 349963ab9e5SAsias He 3505cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 3515cb8850cSKent Overstreet struct scatterlist *sglist, 3525cb8850cSKent Overstreet struct scatterlist **sg) 3535cb8850cSKent Overstreet { 3545cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 3555cb8850cSKent Overstreet struct bvec_iter iter; 3565cb8850cSKent Overstreet int nsegs, cluster; 3575cb8850cSKent Overstreet 3585cb8850cSKent Overstreet nsegs = 0; 3595cb8850cSKent Overstreet cluster = blk_queue_cluster(q); 3605cb8850cSKent Overstreet 3615cb8850cSKent Overstreet if (bio->bi_rw & REQ_DISCARD) { 3625cb8850cSKent Overstreet /* 3635cb8850cSKent Overstreet * This is a hack - drivers should be neither modifying the 3645cb8850cSKent Overstreet * biovec, nor relying on bi_vcnt - but because of 3655cb8850cSKent Overstreet * blk_add_request_payload(), a discard bio may or may not have 3665cb8850cSKent Overstreet * a payload we need to set up here (thank you Christoph) and 3675cb8850cSKent Overstreet * bi_vcnt is really the only way of telling if we need to. 3685cb8850cSKent Overstreet */ 3695cb8850cSKent Overstreet 3705cb8850cSKent Overstreet if (bio->bi_vcnt) 3715cb8850cSKent Overstreet goto single_segment; 3725cb8850cSKent Overstreet 3735cb8850cSKent Overstreet return 0; 3745cb8850cSKent Overstreet } 3755cb8850cSKent Overstreet 3765cb8850cSKent Overstreet if (bio->bi_rw & REQ_WRITE_SAME) { 3775cb8850cSKent Overstreet single_segment: 3785cb8850cSKent Overstreet *sg = sglist; 3795cb8850cSKent Overstreet bvec = bio_iovec(bio); 3805cb8850cSKent Overstreet sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset); 3815cb8850cSKent Overstreet return 1; 3825cb8850cSKent Overstreet } 3835cb8850cSKent Overstreet 3845cb8850cSKent Overstreet for_each_bio(bio) 3855cb8850cSKent Overstreet bio_for_each_segment(bvec, bio, iter) 3865cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 3875cb8850cSKent Overstreet &nsegs, &cluster); 3885cb8850cSKent Overstreet 3895cb8850cSKent Overstreet return nsegs; 3905cb8850cSKent Overstreet } 3915cb8850cSKent Overstreet 392d6d48196SJens Axboe /* 393d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 394d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 395d6d48196SJens Axboe */ 396d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 397d6d48196SJens Axboe struct scatterlist *sglist) 398d6d48196SJens Axboe { 3995cb8850cSKent Overstreet struct scatterlist *sg = NULL; 4005cb8850cSKent Overstreet int nsegs = 0; 401d6d48196SJens Axboe 4025cb8850cSKent Overstreet if (rq->bio) 4035cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 404f18573abSFUJITA Tomonori 405f18573abSFUJITA Tomonori if (unlikely(rq->cmd_flags & REQ_COPY_USER) && 4062e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 4072e46e8b2STejun Heo unsigned int pad_len = 4082e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 409f18573abSFUJITA Tomonori 410f18573abSFUJITA Tomonori sg->length += pad_len; 411f18573abSFUJITA Tomonori rq->extra_len += pad_len; 412f18573abSFUJITA Tomonori } 413f18573abSFUJITA Tomonori 4142fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 4157b6d91daSChristoph Hellwig if (rq->cmd_flags & REQ_WRITE) 416db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 417db0a2e00STejun Heo 418da81ed16SDan Williams sg_unmark_end(sg); 419d6d48196SJens Axboe sg = sg_next(sg); 420d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 421d6d48196SJens Axboe q->dma_drain_size, 422d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 423d6d48196SJens Axboe (PAGE_SIZE - 1)); 424d6d48196SJens Axboe nsegs++; 4257a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 426d6d48196SJens Axboe } 427d6d48196SJens Axboe 428d6d48196SJens Axboe if (sg) 429d6d48196SJens Axboe sg_mark_end(sg); 430d6d48196SJens Axboe 431d6d48196SJens Axboe return nsegs; 432d6d48196SJens Axboe } 433d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 434d6d48196SJens Axboe 435d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 436d6d48196SJens Axboe struct request *req, 437d6d48196SJens Axboe struct bio *bio) 438d6d48196SJens Axboe { 439d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 440d6d48196SJens Axboe 44113f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 44213f05c8dSMartin K. Petersen goto no_merge; 44313f05c8dSMartin K. Petersen 4444eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 44513f05c8dSMartin K. Petersen goto no_merge; 446d6d48196SJens Axboe 447d6d48196SJens Axboe /* 448d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 449d6d48196SJens Axboe * counters. 450d6d48196SJens Axboe */ 451d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 452d6d48196SJens Axboe return 1; 45313f05c8dSMartin K. Petersen 45413f05c8dSMartin K. Petersen no_merge: 45513f05c8dSMartin K. Petersen req->cmd_flags |= REQ_NOMERGE; 45613f05c8dSMartin K. Petersen if (req == q->last_merge) 45713f05c8dSMartin K. Petersen q->last_merge = NULL; 45813f05c8dSMartin K. Petersen return 0; 459d6d48196SJens Axboe } 460d6d48196SJens Axboe 461d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 462d6d48196SJens Axboe struct bio *bio) 463d6d48196SJens Axboe { 4645e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 4655e7c4274SJens Axboe return 0; 4667f39add3SSagi Grimberg if (blk_integrity_rq(req) && 4677f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 4687f39add3SSagi Grimberg return 0; 469f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 470f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) { 471d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 472d6d48196SJens Axboe if (req == q->last_merge) 473d6d48196SJens Axboe q->last_merge = NULL; 474d6d48196SJens Axboe return 0; 475d6d48196SJens Axboe } 4762cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 477d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 4782cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 479d6d48196SJens Axboe blk_recount_segments(q, bio); 480d6d48196SJens Axboe 481d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 482d6d48196SJens Axboe } 483d6d48196SJens Axboe 484d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 485d6d48196SJens Axboe struct bio *bio) 486d6d48196SJens Axboe { 4875e7c4274SJens Axboe 4885e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 4895e7c4274SJens Axboe return 0; 4907f39add3SSagi Grimberg if (blk_integrity_rq(req) && 4917f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 4927f39add3SSagi Grimberg return 0; 493f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 494f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) { 495d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 496d6d48196SJens Axboe if (req == q->last_merge) 497d6d48196SJens Axboe q->last_merge = NULL; 498d6d48196SJens Axboe return 0; 499d6d48196SJens Axboe } 5002cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 501d6d48196SJens Axboe blk_recount_segments(q, bio); 5022cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 503d6d48196SJens Axboe blk_recount_segments(q, req->bio); 504d6d48196SJens Axboe 505d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 506d6d48196SJens Axboe } 507d6d48196SJens Axboe 508e7e24500SJens Axboe /* 509e7e24500SJens Axboe * blk-mq uses req->special to carry normal driver per-request payload, it 510e7e24500SJens Axboe * does not indicate a prepared command that we cannot merge with. 511e7e24500SJens Axboe */ 512e7e24500SJens Axboe static bool req_no_special_merge(struct request *req) 513e7e24500SJens Axboe { 514e7e24500SJens Axboe struct request_queue *q = req->q; 515e7e24500SJens Axboe 516e7e24500SJens Axboe return !q->mq_ops && req->special; 517e7e24500SJens Axboe } 518e7e24500SJens Axboe 519d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 520d6d48196SJens Axboe struct request *next) 521d6d48196SJens Axboe { 522d6d48196SJens Axboe int total_phys_segments; 52386771427SFUJITA Tomonori unsigned int seg_size = 52486771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 525d6d48196SJens Axboe 526d6d48196SJens Axboe /* 527d6d48196SJens Axboe * First check if the either of the requests are re-queued 528d6d48196SJens Axboe * requests. Can't merge them if they are. 529d6d48196SJens Axboe */ 530e7e24500SJens Axboe if (req_no_special_merge(req) || req_no_special_merge(next)) 531d6d48196SJens Axboe return 0; 532d6d48196SJens Axboe 5335e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 534854fbb9cSKeith Busch return 0; 535854fbb9cSKeith Busch 536d6d48196SJens Axboe /* 537d6d48196SJens Axboe * Will it become too large? 538d6d48196SJens Axboe */ 539f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 540f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) 541d6d48196SJens Axboe return 0; 542d6d48196SJens Axboe 543d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 54486771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 54586771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 54686771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 54786771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 54886771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 549d6d48196SJens Axboe total_phys_segments--; 55086771427SFUJITA Tomonori } 551d6d48196SJens Axboe 5528a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 553d6d48196SJens Axboe return 0; 554d6d48196SJens Axboe 5554eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 55613f05c8dSMartin K. Petersen return 0; 55713f05c8dSMartin K. Petersen 558d6d48196SJens Axboe /* Merge is OK... */ 559d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 560d6d48196SJens Axboe return 1; 561d6d48196SJens Axboe } 562d6d48196SJens Axboe 56380a761fdSTejun Heo /** 56480a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 56580a761fdSTejun Heo * @rq: request to mark as mixed merge 56680a761fdSTejun Heo * 56780a761fdSTejun Heo * Description: 56880a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 56980a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 57080a761fdSTejun Heo * merged. 57180a761fdSTejun Heo */ 57280a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 57380a761fdSTejun Heo { 57480a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 57580a761fdSTejun Heo struct bio *bio; 57680a761fdSTejun Heo 57780a761fdSTejun Heo if (rq->cmd_flags & REQ_MIXED_MERGE) 57880a761fdSTejun Heo return; 57980a761fdSTejun Heo 58080a761fdSTejun Heo /* 58180a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 58280a761fdSTejun Heo * contained bios. It will just track those of the first one. 58380a761fdSTejun Heo * Distributes the attributs to each bio. 58480a761fdSTejun Heo */ 58580a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 58680a761fdSTejun Heo WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && 58780a761fdSTejun Heo (bio->bi_rw & REQ_FAILFAST_MASK) != ff); 58880a761fdSTejun Heo bio->bi_rw |= ff; 58980a761fdSTejun Heo } 59080a761fdSTejun Heo rq->cmd_flags |= REQ_MIXED_MERGE; 59180a761fdSTejun Heo } 59280a761fdSTejun Heo 59326308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 59426308eabSJerome Marchand { 59526308eabSJerome Marchand if (blk_do_io_stat(req)) { 59626308eabSJerome Marchand struct hd_struct *part; 59726308eabSJerome Marchand int cpu; 59826308eabSJerome Marchand 59926308eabSJerome Marchand cpu = part_stat_lock(); 60009e099d4SJerome Marchand part = req->part; 60126308eabSJerome Marchand 60226308eabSJerome Marchand part_round_stats(cpu, part); 603316d315bSNikanth Karthikesan part_dec_in_flight(part, rq_data_dir(req)); 60426308eabSJerome Marchand 6056c23a968SJens Axboe hd_struct_put(part); 60626308eabSJerome Marchand part_stat_unlock(); 60726308eabSJerome Marchand } 60826308eabSJerome Marchand } 60926308eabSJerome Marchand 610d6d48196SJens Axboe /* 611d6d48196SJens Axboe * Has to be called with the request spinlock acquired 612d6d48196SJens Axboe */ 613d6d48196SJens Axboe static int attempt_merge(struct request_queue *q, struct request *req, 614d6d48196SJens Axboe struct request *next) 615d6d48196SJens Axboe { 616d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 617d6d48196SJens Axboe return 0; 618d6d48196SJens Axboe 619f31dc1cdSMartin K. Petersen if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags)) 620f31dc1cdSMartin K. Petersen return 0; 621f31dc1cdSMartin K. Petersen 622d6d48196SJens Axboe /* 623d6d48196SJens Axboe * not contiguous 624d6d48196SJens Axboe */ 62583096ebfSTejun Heo if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) 626d6d48196SJens Axboe return 0; 627d6d48196SJens Axboe 628d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 629d6d48196SJens Axboe || req->rq_disk != next->rq_disk 630e7e24500SJens Axboe || req_no_special_merge(next)) 631d6d48196SJens Axboe return 0; 632d6d48196SJens Axboe 6334363ac7cSMartin K. Petersen if (req->cmd_flags & REQ_WRITE_SAME && 6344363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 6354363ac7cSMartin K. Petersen return 0; 6364363ac7cSMartin K. Petersen 637d6d48196SJens Axboe /* 638d6d48196SJens Axboe * If we are allowed to merge, then append bio list 639d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 640d6d48196SJens Axboe * will have updated segment counts, update sector 641d6d48196SJens Axboe * counts here. 642d6d48196SJens Axboe */ 643d6d48196SJens Axboe if (!ll_merge_requests_fn(q, req, next)) 644d6d48196SJens Axboe return 0; 645d6d48196SJens Axboe 646d6d48196SJens Axboe /* 64780a761fdSTejun Heo * If failfast settings disagree or any of the two is already 64880a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 64980a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 65080a761fdSTejun Heo * set properly. 65180a761fdSTejun Heo */ 65280a761fdSTejun Heo if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || 65380a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 65480a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 65580a761fdSTejun Heo blk_rq_set_mixed_merge(req); 65680a761fdSTejun Heo blk_rq_set_mixed_merge(next); 65780a761fdSTejun Heo } 65880a761fdSTejun Heo 65980a761fdSTejun Heo /* 660d6d48196SJens Axboe * At this point we have either done a back merge 661d6d48196SJens Axboe * or front merge. We need the smaller start_time of 662d6d48196SJens Axboe * the merged requests to be the current request 663d6d48196SJens Axboe * for accounting purposes. 664d6d48196SJens Axboe */ 665d6d48196SJens Axboe if (time_after(req->start_time, next->start_time)) 666d6d48196SJens Axboe req->start_time = next->start_time; 667d6d48196SJens Axboe 668d6d48196SJens Axboe req->biotail->bi_next = next->bio; 669d6d48196SJens Axboe req->biotail = next->biotail; 670d6d48196SJens Axboe 671a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 672d6d48196SJens Axboe 673d6d48196SJens Axboe elv_merge_requests(q, req, next); 674d6d48196SJens Axboe 67542dad764SJerome Marchand /* 67642dad764SJerome Marchand * 'next' is going away, so update stats accordingly 67742dad764SJerome Marchand */ 67842dad764SJerome Marchand blk_account_io_merge(next); 679d6d48196SJens Axboe 680d6d48196SJens Axboe req->ioprio = ioprio_best(req->ioprio, next->ioprio); 681ab780f1eSJens Axboe if (blk_rq_cpu_valid(next)) 682ab780f1eSJens Axboe req->cpu = next->cpu; 683d6d48196SJens Axboe 6841cd96c24SBoaz Harrosh /* owner-ship of bio passed from next to req */ 6851cd96c24SBoaz Harrosh next->bio = NULL; 686d6d48196SJens Axboe __blk_put_request(q, next); 687d6d48196SJens Axboe return 1; 688d6d48196SJens Axboe } 689d6d48196SJens Axboe 690d6d48196SJens Axboe int attempt_back_merge(struct request_queue *q, struct request *rq) 691d6d48196SJens Axboe { 692d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 693d6d48196SJens Axboe 694d6d48196SJens Axboe if (next) 695d6d48196SJens Axboe return attempt_merge(q, rq, next); 696d6d48196SJens Axboe 697d6d48196SJens Axboe return 0; 698d6d48196SJens Axboe } 699d6d48196SJens Axboe 700d6d48196SJens Axboe int attempt_front_merge(struct request_queue *q, struct request *rq) 701d6d48196SJens Axboe { 702d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 703d6d48196SJens Axboe 704d6d48196SJens Axboe if (prev) 705d6d48196SJens Axboe return attempt_merge(q, prev, rq); 706d6d48196SJens Axboe 707d6d48196SJens Axboe return 0; 708d6d48196SJens Axboe } 7095e84ea3aSJens Axboe 7105e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 7115e84ea3aSJens Axboe struct request *next) 7125e84ea3aSJens Axboe { 7135e84ea3aSJens Axboe return attempt_merge(q, rq, next); 7145e84ea3aSJens Axboe } 715050c8ea8STejun Heo 716050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 717050c8ea8STejun Heo { 718e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 719050c8ea8STejun Heo return false; 720050c8ea8STejun Heo 721f31dc1cdSMartin K. Petersen if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw)) 722f31dc1cdSMartin K. Petersen return false; 723f31dc1cdSMartin K. Petersen 724050c8ea8STejun Heo /* different data direction or already started, don't merge */ 725050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 726050c8ea8STejun Heo return false; 727050c8ea8STejun Heo 728050c8ea8STejun Heo /* must be same device and not a special request */ 729e7e24500SJens Axboe if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq)) 730050c8ea8STejun Heo return false; 731050c8ea8STejun Heo 732050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 7334eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 734050c8ea8STejun Heo return false; 735050c8ea8STejun Heo 7364363ac7cSMartin K. Petersen /* must be using the same buffer */ 7374363ac7cSMartin K. Petersen if (rq->cmd_flags & REQ_WRITE_SAME && 7384363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 7394363ac7cSMartin K. Petersen return false; 7404363ac7cSMartin K. Petersen 741050c8ea8STejun Heo return true; 742050c8ea8STejun Heo } 743050c8ea8STejun Heo 744050c8ea8STejun Heo int blk_try_merge(struct request *rq, struct bio *bio) 745050c8ea8STejun Heo { 7464f024f37SKent Overstreet if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 747050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 7484f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 749050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 750050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 751050c8ea8STejun Heo } 752