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 121e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 1359247eaeSJens Axboe struct bio *bio) 14d6d48196SJens Axboe { 157988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 167988613bSKent Overstreet int cluster, high, highprv = 1; 171e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 1859247eaeSJens Axboe struct bio *fbio, *bbio; 197988613bSKent Overstreet struct bvec_iter iter; 20d6d48196SJens Axboe 211e428079SJens Axboe if (!bio) 221e428079SJens Axboe return 0; 23d6d48196SJens Axboe 241e428079SJens Axboe fbio = bio; 25e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 265df97b91SMikulas Patocka seg_size = 0; 272c8919deSAndi Kleen nr_phys_segs = 0; 281e428079SJens Axboe for_each_bio(bio) { 297988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 30d6d48196SJens Axboe /* 311e428079SJens Axboe * the trick here is making sure that a high page is 321e428079SJens Axboe * never considered part of another segment, since that 331e428079SJens Axboe * might change with the bounce page. 34d6d48196SJens Axboe */ 357988613bSKent Overstreet high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q); 367988613bSKent Overstreet if (!high && !highprv && cluster) { 377988613bSKent Overstreet if (seg_size + bv.bv_len 38ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 39d6d48196SJens Axboe goto new_segment; 407988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) 41d6d48196SJens Axboe goto new_segment; 427988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) 43d6d48196SJens Axboe goto new_segment; 44d6d48196SJens Axboe 457988613bSKent Overstreet seg_size += bv.bv_len; 46d6d48196SJens Axboe bvprv = bv; 47d6d48196SJens Axboe continue; 48d6d48196SJens Axboe } 49d6d48196SJens Axboe new_segment: 501e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 511e428079SJens Axboe fbio->bi_seg_front_size) 521e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 5386771427SFUJITA Tomonori 54d6d48196SJens Axboe nr_phys_segs++; 55d6d48196SJens Axboe bvprv = bv; 567988613bSKent Overstreet seg_size = bv.bv_len; 57d6d48196SJens Axboe highprv = high; 58d6d48196SJens Axboe } 5959247eaeSJens Axboe bbio = bio; 601e428079SJens Axboe } 61d6d48196SJens Axboe 6259247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 6359247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 6459247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 6559247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 661e428079SJens Axboe 671e428079SJens Axboe return nr_phys_segs; 681e428079SJens Axboe } 691e428079SJens Axboe 701e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 711e428079SJens Axboe { 7259247eaeSJens Axboe rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio); 73d6d48196SJens Axboe } 74d6d48196SJens Axboe 75d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 76d6d48196SJens Axboe { 77d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 781e428079SJens Axboe 79d6d48196SJens Axboe bio->bi_next = NULL; 8059247eaeSJens Axboe bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio); 81d6d48196SJens Axboe bio->bi_next = nxt; 82d6d48196SJens Axboe bio->bi_flags |= (1 << BIO_SEG_VALID); 83d6d48196SJens Axboe } 84d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 85d6d48196SJens Axboe 86d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 87d6d48196SJens Axboe struct bio *nxt) 88d6d48196SJens Axboe { 89*2b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 90f619d254SKent Overstreet struct bvec_iter iter; 91f619d254SKent Overstreet 92e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 93d6d48196SJens Axboe return 0; 94d6d48196SJens Axboe 9586771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 96ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 97d6d48196SJens Axboe return 0; 98d6d48196SJens Axboe 99e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 100e17fc0a1SDavid Woodhouse return 1; 101e17fc0a1SDavid Woodhouse 102f619d254SKent Overstreet bio_for_each_segment(end_bv, bio, iter) 103f619d254SKent Overstreet if (end_bv.bv_len == iter.bi_size) 104f619d254SKent Overstreet break; 105f619d254SKent Overstreet 106f619d254SKent Overstreet nxt_bv = bio_iovec(nxt); 107f619d254SKent Overstreet 108f619d254SKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) 109e17fc0a1SDavid Woodhouse return 0; 110e17fc0a1SDavid Woodhouse 111d6d48196SJens Axboe /* 112e17fc0a1SDavid Woodhouse * bio and nxt are contiguous in memory; check if the queue allows 113d6d48196SJens Axboe * these two to be merged into one 114d6d48196SJens Axboe */ 115f619d254SKent Overstreet if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) 116d6d48196SJens Axboe return 1; 117d6d48196SJens Axboe 118d6d48196SJens Axboe return 0; 119d6d48196SJens Axboe } 120d6d48196SJens Axboe 1217988613bSKent Overstreet static inline void 122963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 1237988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 124963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 125963ab9e5SAsias He { 126963ab9e5SAsias He 127963ab9e5SAsias He int nbytes = bvec->bv_len; 128963ab9e5SAsias He 1297988613bSKent Overstreet if (*sg && *cluster) { 130963ab9e5SAsias He if ((*sg)->length + nbytes > queue_max_segment_size(q)) 131963ab9e5SAsias He goto new_segment; 132963ab9e5SAsias He 1337988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 134963ab9e5SAsias He goto new_segment; 1357988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 136963ab9e5SAsias He goto new_segment; 137963ab9e5SAsias He 138963ab9e5SAsias He (*sg)->length += nbytes; 139963ab9e5SAsias He } else { 140963ab9e5SAsias He new_segment: 141963ab9e5SAsias He if (!*sg) 142963ab9e5SAsias He *sg = sglist; 143963ab9e5SAsias He else { 144963ab9e5SAsias He /* 145963ab9e5SAsias He * If the driver previously mapped a shorter 146963ab9e5SAsias He * list, we could see a termination bit 147963ab9e5SAsias He * prematurely unless it fully inits the sg 148963ab9e5SAsias He * table on each mapping. We KNOW that there 149963ab9e5SAsias He * must be more entries here or the driver 150963ab9e5SAsias He * would be buggy, so force clear the 151963ab9e5SAsias He * termination bit to avoid doing a full 152963ab9e5SAsias He * sg_init_table() in drivers for each command. 153963ab9e5SAsias He */ 154c8164d89SPaolo Bonzini sg_unmark_end(*sg); 155963ab9e5SAsias He *sg = sg_next(*sg); 156963ab9e5SAsias He } 157963ab9e5SAsias He 158963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 159963ab9e5SAsias He (*nsegs)++; 160963ab9e5SAsias He } 1617988613bSKent Overstreet *bvprv = *bvec; 162963ab9e5SAsias He } 163963ab9e5SAsias He 164d6d48196SJens Axboe /* 165d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 166d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 167d6d48196SJens Axboe */ 168d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 169d6d48196SJens Axboe struct scatterlist *sglist) 170d6d48196SJens Axboe { 171*2b8221e1SKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 172d6d48196SJens Axboe struct req_iterator iter; 173d6d48196SJens Axboe struct scatterlist *sg; 174d6d48196SJens Axboe int nsegs, cluster; 175d6d48196SJens Axboe 176d6d48196SJens Axboe nsegs = 0; 177e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 178d6d48196SJens Axboe 179d6d48196SJens Axboe /* 180d6d48196SJens Axboe * for each bio in rq 181d6d48196SJens Axboe */ 182d6d48196SJens Axboe sg = NULL; 183d6d48196SJens Axboe rq_for_each_segment(bvec, rq, iter) { 1847988613bSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg, 185963ab9e5SAsias He &nsegs, &cluster); 186d6d48196SJens Axboe } /* segments in rq */ 187d6d48196SJens Axboe 188f18573abSFUJITA Tomonori 189f18573abSFUJITA Tomonori if (unlikely(rq->cmd_flags & REQ_COPY_USER) && 1902e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 1912e46e8b2STejun Heo unsigned int pad_len = 1922e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 193f18573abSFUJITA Tomonori 194f18573abSFUJITA Tomonori sg->length += pad_len; 195f18573abSFUJITA Tomonori rq->extra_len += pad_len; 196f18573abSFUJITA Tomonori } 197f18573abSFUJITA Tomonori 1982fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 1997b6d91daSChristoph Hellwig if (rq->cmd_flags & REQ_WRITE) 200db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 201db0a2e00STejun Heo 202d6d48196SJens Axboe sg->page_link &= ~0x02; 203d6d48196SJens Axboe sg = sg_next(sg); 204d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 205d6d48196SJens Axboe q->dma_drain_size, 206d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 207d6d48196SJens Axboe (PAGE_SIZE - 1)); 208d6d48196SJens Axboe nsegs++; 2097a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 210d6d48196SJens Axboe } 211d6d48196SJens Axboe 212d6d48196SJens Axboe if (sg) 213d6d48196SJens Axboe sg_mark_end(sg); 214d6d48196SJens Axboe 215d6d48196SJens Axboe return nsegs; 216d6d48196SJens Axboe } 217d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 218d6d48196SJens Axboe 21985b9f66aSAsias He /** 22085b9f66aSAsias He * blk_bio_map_sg - map a bio to a scatterlist 22185b9f66aSAsias He * @q: request_queue in question 22285b9f66aSAsias He * @bio: bio being mapped 22385b9f66aSAsias He * @sglist: scatterlist being mapped 22485b9f66aSAsias He * 22585b9f66aSAsias He * Note: 22685b9f66aSAsias He * Caller must make sure sg can hold bio->bi_phys_segments entries 22785b9f66aSAsias He * 22885b9f66aSAsias He * Will return the number of sg entries setup 22985b9f66aSAsias He */ 23085b9f66aSAsias He int blk_bio_map_sg(struct request_queue *q, struct bio *bio, 23185b9f66aSAsias He struct scatterlist *sglist) 23285b9f66aSAsias He { 233*2b8221e1SKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 23485b9f66aSAsias He struct scatterlist *sg; 23585b9f66aSAsias He int nsegs, cluster; 2367988613bSKent Overstreet struct bvec_iter iter; 23785b9f66aSAsias He 23885b9f66aSAsias He nsegs = 0; 23985b9f66aSAsias He cluster = blk_queue_cluster(q); 24085b9f66aSAsias He 24185b9f66aSAsias He sg = NULL; 2427988613bSKent Overstreet bio_for_each_segment(bvec, bio, iter) { 2437988613bSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, &sg, 24485b9f66aSAsias He &nsegs, &cluster); 24585b9f66aSAsias He } /* segments in bio */ 24685b9f66aSAsias He 24785b9f66aSAsias He if (sg) 24885b9f66aSAsias He sg_mark_end(sg); 24985b9f66aSAsias He 25085b9f66aSAsias He BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments); 25185b9f66aSAsias He return nsegs; 25285b9f66aSAsias He } 25385b9f66aSAsias He EXPORT_SYMBOL(blk_bio_map_sg); 25485b9f66aSAsias He 255d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 256d6d48196SJens Axboe struct request *req, 257d6d48196SJens Axboe struct bio *bio) 258d6d48196SJens Axboe { 259d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 260d6d48196SJens Axboe 26113f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 26213f05c8dSMartin K. Petersen goto no_merge; 26313f05c8dSMartin K. Petersen 26413f05c8dSMartin K. Petersen if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio)) 26513f05c8dSMartin K. Petersen goto no_merge; 266d6d48196SJens Axboe 267d6d48196SJens Axboe /* 268d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 269d6d48196SJens Axboe * counters. 270d6d48196SJens Axboe */ 271d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 272d6d48196SJens Axboe return 1; 27313f05c8dSMartin K. Petersen 27413f05c8dSMartin K. Petersen no_merge: 27513f05c8dSMartin K. Petersen req->cmd_flags |= REQ_NOMERGE; 27613f05c8dSMartin K. Petersen if (req == q->last_merge) 27713f05c8dSMartin K. Petersen q->last_merge = NULL; 27813f05c8dSMartin K. Petersen return 0; 279d6d48196SJens Axboe } 280d6d48196SJens Axboe 281d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 282d6d48196SJens Axboe struct bio *bio) 283d6d48196SJens Axboe { 284f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 285f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) { 286d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 287d6d48196SJens Axboe if (req == q->last_merge) 288d6d48196SJens Axboe q->last_merge = NULL; 289d6d48196SJens Axboe return 0; 290d6d48196SJens Axboe } 2912cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 292d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 2932cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 294d6d48196SJens Axboe blk_recount_segments(q, bio); 295d6d48196SJens Axboe 296d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 297d6d48196SJens Axboe } 298d6d48196SJens Axboe 299d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 300d6d48196SJens Axboe struct bio *bio) 301d6d48196SJens Axboe { 302f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 303f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) { 304d6d48196SJens Axboe req->cmd_flags |= REQ_NOMERGE; 305d6d48196SJens Axboe if (req == q->last_merge) 306d6d48196SJens Axboe q->last_merge = NULL; 307d6d48196SJens Axboe return 0; 308d6d48196SJens Axboe } 3092cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 310d6d48196SJens Axboe blk_recount_segments(q, bio); 3112cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 312d6d48196SJens Axboe blk_recount_segments(q, req->bio); 313d6d48196SJens Axboe 314d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 315d6d48196SJens Axboe } 316d6d48196SJens Axboe 317e7e24500SJens Axboe /* 318e7e24500SJens Axboe * blk-mq uses req->special to carry normal driver per-request payload, it 319e7e24500SJens Axboe * does not indicate a prepared command that we cannot merge with. 320e7e24500SJens Axboe */ 321e7e24500SJens Axboe static bool req_no_special_merge(struct request *req) 322e7e24500SJens Axboe { 323e7e24500SJens Axboe struct request_queue *q = req->q; 324e7e24500SJens Axboe 325e7e24500SJens Axboe return !q->mq_ops && req->special; 326e7e24500SJens Axboe } 327e7e24500SJens Axboe 328d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 329d6d48196SJens Axboe struct request *next) 330d6d48196SJens Axboe { 331d6d48196SJens Axboe int total_phys_segments; 33286771427SFUJITA Tomonori unsigned int seg_size = 33386771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 334d6d48196SJens Axboe 335d6d48196SJens Axboe /* 336d6d48196SJens Axboe * First check if the either of the requests are re-queued 337d6d48196SJens Axboe * requests. Can't merge them if they are. 338d6d48196SJens Axboe */ 339e7e24500SJens Axboe if (req_no_special_merge(req) || req_no_special_merge(next)) 340d6d48196SJens Axboe return 0; 341d6d48196SJens Axboe 342d6d48196SJens Axboe /* 343d6d48196SJens Axboe * Will it become too large? 344d6d48196SJens Axboe */ 345f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 346f31dc1cdSMartin K. Petersen blk_rq_get_max_sectors(req)) 347d6d48196SJens Axboe return 0; 348d6d48196SJens Axboe 349d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 35086771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 35186771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 35286771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 35386771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 35486771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 355d6d48196SJens Axboe total_phys_segments--; 35686771427SFUJITA Tomonori } 357d6d48196SJens Axboe 3588a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 359d6d48196SJens Axboe return 0; 360d6d48196SJens Axboe 36113f05c8dSMartin K. Petersen if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next)) 36213f05c8dSMartin K. Petersen return 0; 36313f05c8dSMartin K. Petersen 364d6d48196SJens Axboe /* Merge is OK... */ 365d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 366d6d48196SJens Axboe return 1; 367d6d48196SJens Axboe } 368d6d48196SJens Axboe 36980a761fdSTejun Heo /** 37080a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 37180a761fdSTejun Heo * @rq: request to mark as mixed merge 37280a761fdSTejun Heo * 37380a761fdSTejun Heo * Description: 37480a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 37580a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 37680a761fdSTejun Heo * merged. 37780a761fdSTejun Heo */ 37880a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 37980a761fdSTejun Heo { 38080a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 38180a761fdSTejun Heo struct bio *bio; 38280a761fdSTejun Heo 38380a761fdSTejun Heo if (rq->cmd_flags & REQ_MIXED_MERGE) 38480a761fdSTejun Heo return; 38580a761fdSTejun Heo 38680a761fdSTejun Heo /* 38780a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 38880a761fdSTejun Heo * contained bios. It will just track those of the first one. 38980a761fdSTejun Heo * Distributes the attributs to each bio. 39080a761fdSTejun Heo */ 39180a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 39280a761fdSTejun Heo WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && 39380a761fdSTejun Heo (bio->bi_rw & REQ_FAILFAST_MASK) != ff); 39480a761fdSTejun Heo bio->bi_rw |= ff; 39580a761fdSTejun Heo } 39680a761fdSTejun Heo rq->cmd_flags |= REQ_MIXED_MERGE; 39780a761fdSTejun Heo } 39880a761fdSTejun Heo 39926308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 40026308eabSJerome Marchand { 40126308eabSJerome Marchand if (blk_do_io_stat(req)) { 40226308eabSJerome Marchand struct hd_struct *part; 40326308eabSJerome Marchand int cpu; 40426308eabSJerome Marchand 40526308eabSJerome Marchand cpu = part_stat_lock(); 40609e099d4SJerome Marchand part = req->part; 40726308eabSJerome Marchand 40826308eabSJerome Marchand part_round_stats(cpu, part); 409316d315bSNikanth Karthikesan part_dec_in_flight(part, rq_data_dir(req)); 41026308eabSJerome Marchand 4116c23a968SJens Axboe hd_struct_put(part); 41226308eabSJerome Marchand part_stat_unlock(); 41326308eabSJerome Marchand } 41426308eabSJerome Marchand } 41526308eabSJerome Marchand 416d6d48196SJens Axboe /* 417d6d48196SJens Axboe * Has to be called with the request spinlock acquired 418d6d48196SJens Axboe */ 419d6d48196SJens Axboe static int attempt_merge(struct request_queue *q, struct request *req, 420d6d48196SJens Axboe struct request *next) 421d6d48196SJens Axboe { 422d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 423d6d48196SJens Axboe return 0; 424d6d48196SJens Axboe 425f31dc1cdSMartin K. Petersen if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags)) 426f31dc1cdSMartin K. Petersen return 0; 427f31dc1cdSMartin K. Petersen 428d6d48196SJens Axboe /* 429d6d48196SJens Axboe * not contiguous 430d6d48196SJens Axboe */ 43183096ebfSTejun Heo if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) 432d6d48196SJens Axboe return 0; 433d6d48196SJens Axboe 434d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 435d6d48196SJens Axboe || req->rq_disk != next->rq_disk 436e7e24500SJens Axboe || req_no_special_merge(next)) 437d6d48196SJens Axboe return 0; 438d6d48196SJens Axboe 4394363ac7cSMartin K. Petersen if (req->cmd_flags & REQ_WRITE_SAME && 4404363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 4414363ac7cSMartin K. Petersen return 0; 4424363ac7cSMartin K. Petersen 443d6d48196SJens Axboe /* 444d6d48196SJens Axboe * If we are allowed to merge, then append bio list 445d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 446d6d48196SJens Axboe * will have updated segment counts, update sector 447d6d48196SJens Axboe * counts here. 448d6d48196SJens Axboe */ 449d6d48196SJens Axboe if (!ll_merge_requests_fn(q, req, next)) 450d6d48196SJens Axboe return 0; 451d6d48196SJens Axboe 452d6d48196SJens Axboe /* 45380a761fdSTejun Heo * If failfast settings disagree or any of the two is already 45480a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 45580a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 45680a761fdSTejun Heo * set properly. 45780a761fdSTejun Heo */ 45880a761fdSTejun Heo if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || 45980a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 46080a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 46180a761fdSTejun Heo blk_rq_set_mixed_merge(req); 46280a761fdSTejun Heo blk_rq_set_mixed_merge(next); 46380a761fdSTejun Heo } 46480a761fdSTejun Heo 46580a761fdSTejun Heo /* 466d6d48196SJens Axboe * At this point we have either done a back merge 467d6d48196SJens Axboe * or front merge. We need the smaller start_time of 468d6d48196SJens Axboe * the merged requests to be the current request 469d6d48196SJens Axboe * for accounting purposes. 470d6d48196SJens Axboe */ 471d6d48196SJens Axboe if (time_after(req->start_time, next->start_time)) 472d6d48196SJens Axboe req->start_time = next->start_time; 473d6d48196SJens Axboe 474d6d48196SJens Axboe req->biotail->bi_next = next->bio; 475d6d48196SJens Axboe req->biotail = next->biotail; 476d6d48196SJens Axboe 477a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 478d6d48196SJens Axboe 479d6d48196SJens Axboe elv_merge_requests(q, req, next); 480d6d48196SJens Axboe 48142dad764SJerome Marchand /* 48242dad764SJerome Marchand * 'next' is going away, so update stats accordingly 48342dad764SJerome Marchand */ 48442dad764SJerome Marchand blk_account_io_merge(next); 485d6d48196SJens Axboe 486d6d48196SJens Axboe req->ioprio = ioprio_best(req->ioprio, next->ioprio); 487ab780f1eSJens Axboe if (blk_rq_cpu_valid(next)) 488ab780f1eSJens Axboe req->cpu = next->cpu; 489d6d48196SJens Axboe 4901cd96c24SBoaz Harrosh /* owner-ship of bio passed from next to req */ 4911cd96c24SBoaz Harrosh next->bio = NULL; 492d6d48196SJens Axboe __blk_put_request(q, next); 493d6d48196SJens Axboe return 1; 494d6d48196SJens Axboe } 495d6d48196SJens Axboe 496d6d48196SJens Axboe int attempt_back_merge(struct request_queue *q, struct request *rq) 497d6d48196SJens Axboe { 498d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 499d6d48196SJens Axboe 500d6d48196SJens Axboe if (next) 501d6d48196SJens Axboe return attempt_merge(q, rq, next); 502d6d48196SJens Axboe 503d6d48196SJens Axboe return 0; 504d6d48196SJens Axboe } 505d6d48196SJens Axboe 506d6d48196SJens Axboe int attempt_front_merge(struct request_queue *q, struct request *rq) 507d6d48196SJens Axboe { 508d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 509d6d48196SJens Axboe 510d6d48196SJens Axboe if (prev) 511d6d48196SJens Axboe return attempt_merge(q, prev, rq); 512d6d48196SJens Axboe 513d6d48196SJens Axboe return 0; 514d6d48196SJens Axboe } 5155e84ea3aSJens Axboe 5165e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 5175e84ea3aSJens Axboe struct request *next) 5185e84ea3aSJens Axboe { 5195e84ea3aSJens Axboe return attempt_merge(q, rq, next); 5205e84ea3aSJens Axboe } 521050c8ea8STejun Heo 522050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 523050c8ea8STejun Heo { 524e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 525050c8ea8STejun Heo return false; 526050c8ea8STejun Heo 527f31dc1cdSMartin K. Petersen if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw)) 528f31dc1cdSMartin K. Petersen return false; 529f31dc1cdSMartin K. Petersen 530050c8ea8STejun Heo /* different data direction or already started, don't merge */ 531050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 532050c8ea8STejun Heo return false; 533050c8ea8STejun Heo 534050c8ea8STejun Heo /* must be same device and not a special request */ 535e7e24500SJens Axboe if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq)) 536050c8ea8STejun Heo return false; 537050c8ea8STejun Heo 538050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 539050c8ea8STejun Heo if (bio_integrity(bio) != blk_integrity_rq(rq)) 540050c8ea8STejun Heo return false; 541050c8ea8STejun Heo 5424363ac7cSMartin K. Petersen /* must be using the same buffer */ 5434363ac7cSMartin K. Petersen if (rq->cmd_flags & REQ_WRITE_SAME && 5444363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 5454363ac7cSMartin K. Petersen return false; 5464363ac7cSMartin K. Petersen 547050c8ea8STejun Heo return true; 548050c8ea8STejun Heo } 549050c8ea8STejun Heo 550050c8ea8STejun Heo int blk_try_merge(struct request *rq, struct bio *bio) 551050c8ea8STejun Heo { 5524f024f37SKent Overstreet if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 553050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 5544f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 555050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 556050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 557050c8ea8STejun Heo } 558