1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 286db1e29SJens Axboe /* 386db1e29SJens Axboe * Functions related to mapping data to requests 486db1e29SJens Axboe */ 586db1e29SJens Axboe #include <linux/kernel.h> 668db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 786db1e29SJens Axboe #include <linux/module.h> 886db1e29SJens Axboe #include <linux/bio.h> 986db1e29SJens Axboe #include <linux/blkdev.h> 1026e49cfcSKent Overstreet #include <linux/uio.h> 1186db1e29SJens Axboe 1286db1e29SJens Axboe #include "blk.h" 1386db1e29SJens Axboe 14*130879f1SChristoph Hellwig struct bio_map_data { 15*130879f1SChristoph Hellwig int is_our_pages; 16*130879f1SChristoph Hellwig struct iov_iter iter; 17*130879f1SChristoph Hellwig struct iovec iov[]; 18*130879f1SChristoph Hellwig }; 19*130879f1SChristoph Hellwig 20*130879f1SChristoph Hellwig static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data, 21*130879f1SChristoph Hellwig gfp_t gfp_mask) 22*130879f1SChristoph Hellwig { 23*130879f1SChristoph Hellwig struct bio_map_data *bmd; 24*130879f1SChristoph Hellwig 25*130879f1SChristoph Hellwig if (data->nr_segs > UIO_MAXIOV) 26*130879f1SChristoph Hellwig return NULL; 27*130879f1SChristoph Hellwig 28*130879f1SChristoph Hellwig bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask); 29*130879f1SChristoph Hellwig if (!bmd) 30*130879f1SChristoph Hellwig return NULL; 31*130879f1SChristoph Hellwig memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); 32*130879f1SChristoph Hellwig bmd->iter = *data; 33*130879f1SChristoph Hellwig bmd->iter.iov = bmd->iov; 34*130879f1SChristoph Hellwig return bmd; 35*130879f1SChristoph Hellwig } 36*130879f1SChristoph Hellwig 37*130879f1SChristoph Hellwig /** 38*130879f1SChristoph Hellwig * bio_copy_from_iter - copy all pages from iov_iter to bio 39*130879f1SChristoph Hellwig * @bio: The &struct bio which describes the I/O as destination 40*130879f1SChristoph Hellwig * @iter: iov_iter as source 41*130879f1SChristoph Hellwig * 42*130879f1SChristoph Hellwig * Copy all pages from iov_iter to bio. 43*130879f1SChristoph Hellwig * Returns 0 on success, or error on failure. 44*130879f1SChristoph Hellwig */ 45*130879f1SChristoph Hellwig static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter) 46*130879f1SChristoph Hellwig { 47*130879f1SChristoph Hellwig struct bio_vec *bvec; 48*130879f1SChristoph Hellwig struct bvec_iter_all iter_all; 49*130879f1SChristoph Hellwig 50*130879f1SChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 51*130879f1SChristoph Hellwig ssize_t ret; 52*130879f1SChristoph Hellwig 53*130879f1SChristoph Hellwig ret = copy_page_from_iter(bvec->bv_page, 54*130879f1SChristoph Hellwig bvec->bv_offset, 55*130879f1SChristoph Hellwig bvec->bv_len, 56*130879f1SChristoph Hellwig iter); 57*130879f1SChristoph Hellwig 58*130879f1SChristoph Hellwig if (!iov_iter_count(iter)) 59*130879f1SChristoph Hellwig break; 60*130879f1SChristoph Hellwig 61*130879f1SChristoph Hellwig if (ret < bvec->bv_len) 62*130879f1SChristoph Hellwig return -EFAULT; 63*130879f1SChristoph Hellwig } 64*130879f1SChristoph Hellwig 65*130879f1SChristoph Hellwig return 0; 66*130879f1SChristoph Hellwig } 67*130879f1SChristoph Hellwig 68*130879f1SChristoph Hellwig /** 69*130879f1SChristoph Hellwig * bio_copy_to_iter - copy all pages from bio to iov_iter 70*130879f1SChristoph Hellwig * @bio: The &struct bio which describes the I/O as source 71*130879f1SChristoph Hellwig * @iter: iov_iter as destination 72*130879f1SChristoph Hellwig * 73*130879f1SChristoph Hellwig * Copy all pages from bio to iov_iter. 74*130879f1SChristoph Hellwig * Returns 0 on success, or error on failure. 75*130879f1SChristoph Hellwig */ 76*130879f1SChristoph Hellwig static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter) 77*130879f1SChristoph Hellwig { 78*130879f1SChristoph Hellwig struct bio_vec *bvec; 79*130879f1SChristoph Hellwig struct bvec_iter_all iter_all; 80*130879f1SChristoph Hellwig 81*130879f1SChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 82*130879f1SChristoph Hellwig ssize_t ret; 83*130879f1SChristoph Hellwig 84*130879f1SChristoph Hellwig ret = copy_page_to_iter(bvec->bv_page, 85*130879f1SChristoph Hellwig bvec->bv_offset, 86*130879f1SChristoph Hellwig bvec->bv_len, 87*130879f1SChristoph Hellwig &iter); 88*130879f1SChristoph Hellwig 89*130879f1SChristoph Hellwig if (!iov_iter_count(&iter)) 90*130879f1SChristoph Hellwig break; 91*130879f1SChristoph Hellwig 92*130879f1SChristoph Hellwig if (ret < bvec->bv_len) 93*130879f1SChristoph Hellwig return -EFAULT; 94*130879f1SChristoph Hellwig } 95*130879f1SChristoph Hellwig 96*130879f1SChristoph Hellwig return 0; 97*130879f1SChristoph Hellwig } 98*130879f1SChristoph Hellwig 99*130879f1SChristoph Hellwig /** 100*130879f1SChristoph Hellwig * bio_uncopy_user - finish previously mapped bio 101*130879f1SChristoph Hellwig * @bio: bio being terminated 102*130879f1SChristoph Hellwig * 103*130879f1SChristoph Hellwig * Free pages allocated from bio_copy_user_iov() and write back data 104*130879f1SChristoph Hellwig * to user space in case of a read. 105*130879f1SChristoph Hellwig */ 106*130879f1SChristoph Hellwig static int bio_uncopy_user(struct bio *bio) 107*130879f1SChristoph Hellwig { 108*130879f1SChristoph Hellwig struct bio_map_data *bmd = bio->bi_private; 109*130879f1SChristoph Hellwig int ret = 0; 110*130879f1SChristoph Hellwig 111*130879f1SChristoph Hellwig if (!bio_flagged(bio, BIO_NULL_MAPPED)) { 112*130879f1SChristoph Hellwig /* 113*130879f1SChristoph Hellwig * if we're in a workqueue, the request is orphaned, so 114*130879f1SChristoph Hellwig * don't copy into a random user address space, just free 115*130879f1SChristoph Hellwig * and return -EINTR so user space doesn't expect any data. 116*130879f1SChristoph Hellwig */ 117*130879f1SChristoph Hellwig if (!current->mm) 118*130879f1SChristoph Hellwig ret = -EINTR; 119*130879f1SChristoph Hellwig else if (bio_data_dir(bio) == READ) 120*130879f1SChristoph Hellwig ret = bio_copy_to_iter(bio, bmd->iter); 121*130879f1SChristoph Hellwig if (bmd->is_our_pages) 122*130879f1SChristoph Hellwig bio_free_pages(bio); 123*130879f1SChristoph Hellwig } 124*130879f1SChristoph Hellwig kfree(bmd); 125*130879f1SChristoph Hellwig bio_put(bio); 126*130879f1SChristoph Hellwig return ret; 127*130879f1SChristoph Hellwig } 128*130879f1SChristoph Hellwig 129*130879f1SChristoph Hellwig /** 130*130879f1SChristoph Hellwig * bio_copy_user_iov - copy user data to bio 131*130879f1SChristoph Hellwig * @q: destination block queue 132*130879f1SChristoph Hellwig * @map_data: pointer to the rq_map_data holding pages (if necessary) 133*130879f1SChristoph Hellwig * @iter: iovec iterator 134*130879f1SChristoph Hellwig * @gfp_mask: memory allocation flags 135*130879f1SChristoph Hellwig * 136*130879f1SChristoph Hellwig * Prepares and returns a bio for indirect user io, bouncing data 137*130879f1SChristoph Hellwig * to/from kernel pages as necessary. Must be paired with 138*130879f1SChristoph Hellwig * call bio_uncopy_user() on io completion. 139*130879f1SChristoph Hellwig */ 140*130879f1SChristoph Hellwig static struct bio *bio_copy_user_iov(struct request_queue *q, 141*130879f1SChristoph Hellwig struct rq_map_data *map_data, struct iov_iter *iter, 142*130879f1SChristoph Hellwig gfp_t gfp_mask) 143*130879f1SChristoph Hellwig { 144*130879f1SChristoph Hellwig struct bio_map_data *bmd; 145*130879f1SChristoph Hellwig struct page *page; 146*130879f1SChristoph Hellwig struct bio *bio; 147*130879f1SChristoph Hellwig int i = 0, ret; 148*130879f1SChristoph Hellwig int nr_pages; 149*130879f1SChristoph Hellwig unsigned int len = iter->count; 150*130879f1SChristoph Hellwig unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0; 151*130879f1SChristoph Hellwig 152*130879f1SChristoph Hellwig bmd = bio_alloc_map_data(iter, gfp_mask); 153*130879f1SChristoph Hellwig if (!bmd) 154*130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 155*130879f1SChristoph Hellwig 156*130879f1SChristoph Hellwig /* 157*130879f1SChristoph Hellwig * We need to do a deep copy of the iov_iter including the iovecs. 158*130879f1SChristoph Hellwig * The caller provided iov might point to an on-stack or otherwise 159*130879f1SChristoph Hellwig * shortlived one. 160*130879f1SChristoph Hellwig */ 161*130879f1SChristoph Hellwig bmd->is_our_pages = map_data ? 0 : 1; 162*130879f1SChristoph Hellwig 163*130879f1SChristoph Hellwig nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE); 164*130879f1SChristoph Hellwig if (nr_pages > BIO_MAX_PAGES) 165*130879f1SChristoph Hellwig nr_pages = BIO_MAX_PAGES; 166*130879f1SChristoph Hellwig 167*130879f1SChristoph Hellwig ret = -ENOMEM; 168*130879f1SChristoph Hellwig bio = bio_kmalloc(gfp_mask, nr_pages); 169*130879f1SChristoph Hellwig if (!bio) 170*130879f1SChristoph Hellwig goto out_bmd; 171*130879f1SChristoph Hellwig 172*130879f1SChristoph Hellwig ret = 0; 173*130879f1SChristoph Hellwig 174*130879f1SChristoph Hellwig if (map_data) { 175*130879f1SChristoph Hellwig nr_pages = 1 << map_data->page_order; 176*130879f1SChristoph Hellwig i = map_data->offset / PAGE_SIZE; 177*130879f1SChristoph Hellwig } 178*130879f1SChristoph Hellwig while (len) { 179*130879f1SChristoph Hellwig unsigned int bytes = PAGE_SIZE; 180*130879f1SChristoph Hellwig 181*130879f1SChristoph Hellwig bytes -= offset; 182*130879f1SChristoph Hellwig 183*130879f1SChristoph Hellwig if (bytes > len) 184*130879f1SChristoph Hellwig bytes = len; 185*130879f1SChristoph Hellwig 186*130879f1SChristoph Hellwig if (map_data) { 187*130879f1SChristoph Hellwig if (i == map_data->nr_entries * nr_pages) { 188*130879f1SChristoph Hellwig ret = -ENOMEM; 189*130879f1SChristoph Hellwig break; 190*130879f1SChristoph Hellwig } 191*130879f1SChristoph Hellwig 192*130879f1SChristoph Hellwig page = map_data->pages[i / nr_pages]; 193*130879f1SChristoph Hellwig page += (i % nr_pages); 194*130879f1SChristoph Hellwig 195*130879f1SChristoph Hellwig i++; 196*130879f1SChristoph Hellwig } else { 197*130879f1SChristoph Hellwig page = alloc_page(q->bounce_gfp | gfp_mask); 198*130879f1SChristoph Hellwig if (!page) { 199*130879f1SChristoph Hellwig ret = -ENOMEM; 200*130879f1SChristoph Hellwig break; 201*130879f1SChristoph Hellwig } 202*130879f1SChristoph Hellwig } 203*130879f1SChristoph Hellwig 204*130879f1SChristoph Hellwig if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) { 205*130879f1SChristoph Hellwig if (!map_data) 206*130879f1SChristoph Hellwig __free_page(page); 207*130879f1SChristoph Hellwig break; 208*130879f1SChristoph Hellwig } 209*130879f1SChristoph Hellwig 210*130879f1SChristoph Hellwig len -= bytes; 211*130879f1SChristoph Hellwig offset = 0; 212*130879f1SChristoph Hellwig } 213*130879f1SChristoph Hellwig 214*130879f1SChristoph Hellwig if (ret) 215*130879f1SChristoph Hellwig goto cleanup; 216*130879f1SChristoph Hellwig 217*130879f1SChristoph Hellwig if (map_data) 218*130879f1SChristoph Hellwig map_data->offset += bio->bi_iter.bi_size; 219*130879f1SChristoph Hellwig 220*130879f1SChristoph Hellwig /* 221*130879f1SChristoph Hellwig * success 222*130879f1SChristoph Hellwig */ 223*130879f1SChristoph Hellwig if ((iov_iter_rw(iter) == WRITE && 224*130879f1SChristoph Hellwig (!map_data || !map_data->null_mapped)) || 225*130879f1SChristoph Hellwig (map_data && map_data->from_user)) { 226*130879f1SChristoph Hellwig ret = bio_copy_from_iter(bio, iter); 227*130879f1SChristoph Hellwig if (ret) 228*130879f1SChristoph Hellwig goto cleanup; 229*130879f1SChristoph Hellwig } else { 230*130879f1SChristoph Hellwig if (bmd->is_our_pages) 231*130879f1SChristoph Hellwig zero_fill_bio(bio); 232*130879f1SChristoph Hellwig iov_iter_advance(iter, bio->bi_iter.bi_size); 233*130879f1SChristoph Hellwig } 234*130879f1SChristoph Hellwig 235*130879f1SChristoph Hellwig bio->bi_private = bmd; 236*130879f1SChristoph Hellwig if (map_data && map_data->null_mapped) 237*130879f1SChristoph Hellwig bio_set_flag(bio, BIO_NULL_MAPPED); 238*130879f1SChristoph Hellwig return bio; 239*130879f1SChristoph Hellwig cleanup: 240*130879f1SChristoph Hellwig if (!map_data) 241*130879f1SChristoph Hellwig bio_free_pages(bio); 242*130879f1SChristoph Hellwig bio_put(bio); 243*130879f1SChristoph Hellwig out_bmd: 244*130879f1SChristoph Hellwig kfree(bmd); 245*130879f1SChristoph Hellwig return ERR_PTR(ret); 246*130879f1SChristoph Hellwig } 247*130879f1SChristoph Hellwig 248*130879f1SChristoph Hellwig /** 249*130879f1SChristoph Hellwig * bio_map_user_iov - map user iovec into bio 250*130879f1SChristoph Hellwig * @q: the struct request_queue for the bio 251*130879f1SChristoph Hellwig * @iter: iovec iterator 252*130879f1SChristoph Hellwig * @gfp_mask: memory allocation flags 253*130879f1SChristoph Hellwig * 254*130879f1SChristoph Hellwig * Map the user space address into a bio suitable for io to a block 255*130879f1SChristoph Hellwig * device. Returns an error pointer in case of error. 256*130879f1SChristoph Hellwig */ 257*130879f1SChristoph Hellwig static struct bio *bio_map_user_iov(struct request_queue *q, 258*130879f1SChristoph Hellwig struct iov_iter *iter, gfp_t gfp_mask) 259*130879f1SChristoph Hellwig { 260*130879f1SChristoph Hellwig int j; 261*130879f1SChristoph Hellwig struct bio *bio; 262*130879f1SChristoph Hellwig int ret; 263*130879f1SChristoph Hellwig 264*130879f1SChristoph Hellwig if (!iov_iter_count(iter)) 265*130879f1SChristoph Hellwig return ERR_PTR(-EINVAL); 266*130879f1SChristoph Hellwig 267*130879f1SChristoph Hellwig bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES)); 268*130879f1SChristoph Hellwig if (!bio) 269*130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 270*130879f1SChristoph Hellwig 271*130879f1SChristoph Hellwig while (iov_iter_count(iter)) { 272*130879f1SChristoph Hellwig struct page **pages; 273*130879f1SChristoph Hellwig ssize_t bytes; 274*130879f1SChristoph Hellwig size_t offs, added = 0; 275*130879f1SChristoph Hellwig int npages; 276*130879f1SChristoph Hellwig 277*130879f1SChristoph Hellwig bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs); 278*130879f1SChristoph Hellwig if (unlikely(bytes <= 0)) { 279*130879f1SChristoph Hellwig ret = bytes ? bytes : -EFAULT; 280*130879f1SChristoph Hellwig goto out_unmap; 281*130879f1SChristoph Hellwig } 282*130879f1SChristoph Hellwig 283*130879f1SChristoph Hellwig npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE); 284*130879f1SChristoph Hellwig 285*130879f1SChristoph Hellwig if (unlikely(offs & queue_dma_alignment(q))) { 286*130879f1SChristoph Hellwig ret = -EINVAL; 287*130879f1SChristoph Hellwig j = 0; 288*130879f1SChristoph Hellwig } else { 289*130879f1SChristoph Hellwig for (j = 0; j < npages; j++) { 290*130879f1SChristoph Hellwig struct page *page = pages[j]; 291*130879f1SChristoph Hellwig unsigned int n = PAGE_SIZE - offs; 292*130879f1SChristoph Hellwig bool same_page = false; 293*130879f1SChristoph Hellwig 294*130879f1SChristoph Hellwig if (n > bytes) 295*130879f1SChristoph Hellwig n = bytes; 296*130879f1SChristoph Hellwig 297*130879f1SChristoph Hellwig if (!__bio_add_pc_page(q, bio, page, n, offs, 298*130879f1SChristoph Hellwig &same_page)) { 299*130879f1SChristoph Hellwig if (same_page) 300*130879f1SChristoph Hellwig put_page(page); 301*130879f1SChristoph Hellwig break; 302*130879f1SChristoph Hellwig } 303*130879f1SChristoph Hellwig 304*130879f1SChristoph Hellwig added += n; 305*130879f1SChristoph Hellwig bytes -= n; 306*130879f1SChristoph Hellwig offs = 0; 307*130879f1SChristoph Hellwig } 308*130879f1SChristoph Hellwig iov_iter_advance(iter, added); 309*130879f1SChristoph Hellwig } 310*130879f1SChristoph Hellwig /* 311*130879f1SChristoph Hellwig * release the pages we didn't map into the bio, if any 312*130879f1SChristoph Hellwig */ 313*130879f1SChristoph Hellwig while (j < npages) 314*130879f1SChristoph Hellwig put_page(pages[j++]); 315*130879f1SChristoph Hellwig kvfree(pages); 316*130879f1SChristoph Hellwig /* couldn't stuff something into bio? */ 317*130879f1SChristoph Hellwig if (bytes) 318*130879f1SChristoph Hellwig break; 319*130879f1SChristoph Hellwig } 320*130879f1SChristoph Hellwig 321*130879f1SChristoph Hellwig bio_set_flag(bio, BIO_USER_MAPPED); 322*130879f1SChristoph Hellwig 323*130879f1SChristoph Hellwig /* 324*130879f1SChristoph Hellwig * subtle -- if bio_map_user_iov() ended up bouncing a bio, 325*130879f1SChristoph Hellwig * it would normally disappear when its bi_end_io is run. 326*130879f1SChristoph Hellwig * however, we need it for the unmap, so grab an extra 327*130879f1SChristoph Hellwig * reference to it 328*130879f1SChristoph Hellwig */ 329*130879f1SChristoph Hellwig bio_get(bio); 330*130879f1SChristoph Hellwig return bio; 331*130879f1SChristoph Hellwig 332*130879f1SChristoph Hellwig out_unmap: 333*130879f1SChristoph Hellwig bio_release_pages(bio, false); 334*130879f1SChristoph Hellwig bio_put(bio); 335*130879f1SChristoph Hellwig return ERR_PTR(ret); 336*130879f1SChristoph Hellwig } 337*130879f1SChristoph Hellwig 338*130879f1SChristoph Hellwig /** 339*130879f1SChristoph Hellwig * bio_unmap_user - unmap a bio 340*130879f1SChristoph Hellwig * @bio: the bio being unmapped 341*130879f1SChristoph Hellwig * 342*130879f1SChristoph Hellwig * Unmap a bio previously mapped by bio_map_user_iov(). Must be called from 343*130879f1SChristoph Hellwig * process context. 344*130879f1SChristoph Hellwig * 345*130879f1SChristoph Hellwig * bio_unmap_user() may sleep. 346*130879f1SChristoph Hellwig */ 347*130879f1SChristoph Hellwig static void bio_unmap_user(struct bio *bio) 348*130879f1SChristoph Hellwig { 349*130879f1SChristoph Hellwig bio_release_pages(bio, bio_data_dir(bio) == READ); 350*130879f1SChristoph Hellwig bio_put(bio); 351*130879f1SChristoph Hellwig bio_put(bio); 352*130879f1SChristoph Hellwig } 353*130879f1SChristoph Hellwig 354*130879f1SChristoph Hellwig static void bio_invalidate_vmalloc_pages(struct bio *bio) 355*130879f1SChristoph Hellwig { 356*130879f1SChristoph Hellwig #ifdef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE 357*130879f1SChristoph Hellwig if (bio->bi_private && !op_is_write(bio_op(bio))) { 358*130879f1SChristoph Hellwig unsigned long i, len = 0; 359*130879f1SChristoph Hellwig 360*130879f1SChristoph Hellwig for (i = 0; i < bio->bi_vcnt; i++) 361*130879f1SChristoph Hellwig len += bio->bi_io_vec[i].bv_len; 362*130879f1SChristoph Hellwig invalidate_kernel_vmap_range(bio->bi_private, len); 363*130879f1SChristoph Hellwig } 364*130879f1SChristoph Hellwig #endif 365*130879f1SChristoph Hellwig } 366*130879f1SChristoph Hellwig 367*130879f1SChristoph Hellwig static void bio_map_kern_endio(struct bio *bio) 368*130879f1SChristoph Hellwig { 369*130879f1SChristoph Hellwig bio_invalidate_vmalloc_pages(bio); 370*130879f1SChristoph Hellwig bio_put(bio); 371*130879f1SChristoph Hellwig } 372*130879f1SChristoph Hellwig 373*130879f1SChristoph Hellwig /** 374*130879f1SChristoph Hellwig * bio_map_kern - map kernel address into bio 375*130879f1SChristoph Hellwig * @q: the struct request_queue for the bio 376*130879f1SChristoph Hellwig * @data: pointer to buffer to map 377*130879f1SChristoph Hellwig * @len: length in bytes 378*130879f1SChristoph Hellwig * @gfp_mask: allocation flags for bio allocation 379*130879f1SChristoph Hellwig * 380*130879f1SChristoph Hellwig * Map the kernel address into a bio suitable for io to a block 381*130879f1SChristoph Hellwig * device. Returns an error pointer in case of error. 382*130879f1SChristoph Hellwig */ 383*130879f1SChristoph Hellwig static struct bio *bio_map_kern(struct request_queue *q, void *data, 384*130879f1SChristoph Hellwig unsigned int len, gfp_t gfp_mask) 385*130879f1SChristoph Hellwig { 386*130879f1SChristoph Hellwig unsigned long kaddr = (unsigned long)data; 387*130879f1SChristoph Hellwig unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 388*130879f1SChristoph Hellwig unsigned long start = kaddr >> PAGE_SHIFT; 389*130879f1SChristoph Hellwig const int nr_pages = end - start; 390*130879f1SChristoph Hellwig bool is_vmalloc = is_vmalloc_addr(data); 391*130879f1SChristoph Hellwig struct page *page; 392*130879f1SChristoph Hellwig int offset, i; 393*130879f1SChristoph Hellwig struct bio *bio; 394*130879f1SChristoph Hellwig 395*130879f1SChristoph Hellwig bio = bio_kmalloc(gfp_mask, nr_pages); 396*130879f1SChristoph Hellwig if (!bio) 397*130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 398*130879f1SChristoph Hellwig 399*130879f1SChristoph Hellwig if (is_vmalloc) { 400*130879f1SChristoph Hellwig flush_kernel_vmap_range(data, len); 401*130879f1SChristoph Hellwig bio->bi_private = data; 402*130879f1SChristoph Hellwig } 403*130879f1SChristoph Hellwig 404*130879f1SChristoph Hellwig offset = offset_in_page(kaddr); 405*130879f1SChristoph Hellwig for (i = 0; i < nr_pages; i++) { 406*130879f1SChristoph Hellwig unsigned int bytes = PAGE_SIZE - offset; 407*130879f1SChristoph Hellwig 408*130879f1SChristoph Hellwig if (len <= 0) 409*130879f1SChristoph Hellwig break; 410*130879f1SChristoph Hellwig 411*130879f1SChristoph Hellwig if (bytes > len) 412*130879f1SChristoph Hellwig bytes = len; 413*130879f1SChristoph Hellwig 414*130879f1SChristoph Hellwig if (!is_vmalloc) 415*130879f1SChristoph Hellwig page = virt_to_page(data); 416*130879f1SChristoph Hellwig else 417*130879f1SChristoph Hellwig page = vmalloc_to_page(data); 418*130879f1SChristoph Hellwig if (bio_add_pc_page(q, bio, page, bytes, 419*130879f1SChristoph Hellwig offset) < bytes) { 420*130879f1SChristoph Hellwig /* we don't support partial mappings */ 421*130879f1SChristoph Hellwig bio_put(bio); 422*130879f1SChristoph Hellwig return ERR_PTR(-EINVAL); 423*130879f1SChristoph Hellwig } 424*130879f1SChristoph Hellwig 425*130879f1SChristoph Hellwig data += bytes; 426*130879f1SChristoph Hellwig len -= bytes; 427*130879f1SChristoph Hellwig offset = 0; 428*130879f1SChristoph Hellwig } 429*130879f1SChristoph Hellwig 430*130879f1SChristoph Hellwig bio->bi_end_io = bio_map_kern_endio; 431*130879f1SChristoph Hellwig return bio; 432*130879f1SChristoph Hellwig } 433*130879f1SChristoph Hellwig 434*130879f1SChristoph Hellwig static void bio_copy_kern_endio(struct bio *bio) 435*130879f1SChristoph Hellwig { 436*130879f1SChristoph Hellwig bio_free_pages(bio); 437*130879f1SChristoph Hellwig bio_put(bio); 438*130879f1SChristoph Hellwig } 439*130879f1SChristoph Hellwig 440*130879f1SChristoph Hellwig static void bio_copy_kern_endio_read(struct bio *bio) 441*130879f1SChristoph Hellwig { 442*130879f1SChristoph Hellwig char *p = bio->bi_private; 443*130879f1SChristoph Hellwig struct bio_vec *bvec; 444*130879f1SChristoph Hellwig struct bvec_iter_all iter_all; 445*130879f1SChristoph Hellwig 446*130879f1SChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 447*130879f1SChristoph Hellwig memcpy(p, page_address(bvec->bv_page), bvec->bv_len); 448*130879f1SChristoph Hellwig p += bvec->bv_len; 449*130879f1SChristoph Hellwig } 450*130879f1SChristoph Hellwig 451*130879f1SChristoph Hellwig bio_copy_kern_endio(bio); 452*130879f1SChristoph Hellwig } 453*130879f1SChristoph Hellwig 454*130879f1SChristoph Hellwig /** 455*130879f1SChristoph Hellwig * bio_copy_kern - copy kernel address into bio 456*130879f1SChristoph Hellwig * @q: the struct request_queue for the bio 457*130879f1SChristoph Hellwig * @data: pointer to buffer to copy 458*130879f1SChristoph Hellwig * @len: length in bytes 459*130879f1SChristoph Hellwig * @gfp_mask: allocation flags for bio and page allocation 460*130879f1SChristoph Hellwig * @reading: data direction is READ 461*130879f1SChristoph Hellwig * 462*130879f1SChristoph Hellwig * copy the kernel address into a bio suitable for io to a block 463*130879f1SChristoph Hellwig * device. Returns an error pointer in case of error. 464*130879f1SChristoph Hellwig */ 465*130879f1SChristoph Hellwig static struct bio *bio_copy_kern(struct request_queue *q, void *data, 466*130879f1SChristoph Hellwig unsigned int len, gfp_t gfp_mask, int reading) 467*130879f1SChristoph Hellwig { 468*130879f1SChristoph Hellwig unsigned long kaddr = (unsigned long)data; 469*130879f1SChristoph Hellwig unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 470*130879f1SChristoph Hellwig unsigned long start = kaddr >> PAGE_SHIFT; 471*130879f1SChristoph Hellwig struct bio *bio; 472*130879f1SChristoph Hellwig void *p = data; 473*130879f1SChristoph Hellwig int nr_pages = 0; 474*130879f1SChristoph Hellwig 475*130879f1SChristoph Hellwig /* 476*130879f1SChristoph Hellwig * Overflow, abort 477*130879f1SChristoph Hellwig */ 478*130879f1SChristoph Hellwig if (end < start) 479*130879f1SChristoph Hellwig return ERR_PTR(-EINVAL); 480*130879f1SChristoph Hellwig 481*130879f1SChristoph Hellwig nr_pages = end - start; 482*130879f1SChristoph Hellwig bio = bio_kmalloc(gfp_mask, nr_pages); 483*130879f1SChristoph Hellwig if (!bio) 484*130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 485*130879f1SChristoph Hellwig 486*130879f1SChristoph Hellwig while (len) { 487*130879f1SChristoph Hellwig struct page *page; 488*130879f1SChristoph Hellwig unsigned int bytes = PAGE_SIZE; 489*130879f1SChristoph Hellwig 490*130879f1SChristoph Hellwig if (bytes > len) 491*130879f1SChristoph Hellwig bytes = len; 492*130879f1SChristoph Hellwig 493*130879f1SChristoph Hellwig page = alloc_page(q->bounce_gfp | gfp_mask); 494*130879f1SChristoph Hellwig if (!page) 495*130879f1SChristoph Hellwig goto cleanup; 496*130879f1SChristoph Hellwig 497*130879f1SChristoph Hellwig if (!reading) 498*130879f1SChristoph Hellwig memcpy(page_address(page), p, bytes); 499*130879f1SChristoph Hellwig 500*130879f1SChristoph Hellwig if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) 501*130879f1SChristoph Hellwig break; 502*130879f1SChristoph Hellwig 503*130879f1SChristoph Hellwig len -= bytes; 504*130879f1SChristoph Hellwig p += bytes; 505*130879f1SChristoph Hellwig } 506*130879f1SChristoph Hellwig 507*130879f1SChristoph Hellwig if (reading) { 508*130879f1SChristoph Hellwig bio->bi_end_io = bio_copy_kern_endio_read; 509*130879f1SChristoph Hellwig bio->bi_private = data; 510*130879f1SChristoph Hellwig } else { 511*130879f1SChristoph Hellwig bio->bi_end_io = bio_copy_kern_endio; 512*130879f1SChristoph Hellwig } 513*130879f1SChristoph Hellwig 514*130879f1SChristoph Hellwig return bio; 515*130879f1SChristoph Hellwig 516*130879f1SChristoph Hellwig cleanup: 517*130879f1SChristoph Hellwig bio_free_pages(bio); 518*130879f1SChristoph Hellwig bio_put(bio); 519*130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 520*130879f1SChristoph Hellwig } 521*130879f1SChristoph Hellwig 52298d61d5bSChristoph Hellwig /* 5230abc2a10SJens Axboe * Append a bio to a passthrough request. Only works if the bio can be merged 5240abc2a10SJens Axboe * into the request based on the driver constraints. 52598d61d5bSChristoph Hellwig */ 5260abc2a10SJens Axboe int blk_rq_append_bio(struct request *rq, struct bio **bio) 52786db1e29SJens Axboe { 5280abc2a10SJens Axboe struct bio *orig_bio = *bio; 52914ccb66bSChristoph Hellwig struct bvec_iter iter; 53014ccb66bSChristoph Hellwig struct bio_vec bv; 53114ccb66bSChristoph Hellwig unsigned int nr_segs = 0; 5320abc2a10SJens Axboe 5330abc2a10SJens Axboe blk_queue_bounce(rq->q, bio); 534caa4b024SChristoph Hellwig 53514ccb66bSChristoph Hellwig bio_for_each_bvec(bv, *bio, iter) 53614ccb66bSChristoph Hellwig nr_segs++; 53714ccb66bSChristoph Hellwig 53898d61d5bSChristoph Hellwig if (!rq->bio) { 53914ccb66bSChristoph Hellwig blk_rq_bio_prep(rq, *bio, nr_segs); 54098d61d5bSChristoph Hellwig } else { 54114ccb66bSChristoph Hellwig if (!ll_back_merge_fn(rq, *bio, nr_segs)) { 5420abc2a10SJens Axboe if (orig_bio != *bio) { 5430abc2a10SJens Axboe bio_put(*bio); 5440abc2a10SJens Axboe *bio = orig_bio; 5450abc2a10SJens Axboe } 54686db1e29SJens Axboe return -EINVAL; 5470abc2a10SJens Axboe } 54898d61d5bSChristoph Hellwig 5490abc2a10SJens Axboe rq->biotail->bi_next = *bio; 5500abc2a10SJens Axboe rq->biotail = *bio; 5510abc2a10SJens Axboe rq->__data_len += (*bio)->bi_iter.bi_size; 55286db1e29SJens Axboe } 55398d61d5bSChristoph Hellwig 55486db1e29SJens Axboe return 0; 55586db1e29SJens Axboe } 55698d61d5bSChristoph Hellwig EXPORT_SYMBOL(blk_rq_append_bio); 55786db1e29SJens Axboe 55886db1e29SJens Axboe static int __blk_rq_unmap_user(struct bio *bio) 55986db1e29SJens Axboe { 56086db1e29SJens Axboe int ret = 0; 56186db1e29SJens Axboe 56286db1e29SJens Axboe if (bio) { 56386db1e29SJens Axboe if (bio_flagged(bio, BIO_USER_MAPPED)) 56486db1e29SJens Axboe bio_unmap_user(bio); 56586db1e29SJens Axboe else 56686db1e29SJens Axboe ret = bio_uncopy_user(bio); 56786db1e29SJens Axboe } 56886db1e29SJens Axboe 56986db1e29SJens Axboe return ret; 57086db1e29SJens Axboe } 57186db1e29SJens Axboe 5724d6af73dSChristoph Hellwig static int __blk_rq_map_user_iov(struct request *rq, 5734d6af73dSChristoph Hellwig struct rq_map_data *map_data, struct iov_iter *iter, 5744d6af73dSChristoph Hellwig gfp_t gfp_mask, bool copy) 5754d6af73dSChristoph Hellwig { 5764d6af73dSChristoph Hellwig struct request_queue *q = rq->q; 5774d6af73dSChristoph Hellwig struct bio *bio, *orig_bio; 5784d6af73dSChristoph Hellwig int ret; 5794d6af73dSChristoph Hellwig 5804d6af73dSChristoph Hellwig if (copy) 5814d6af73dSChristoph Hellwig bio = bio_copy_user_iov(q, map_data, iter, gfp_mask); 5824d6af73dSChristoph Hellwig else 5834d6af73dSChristoph Hellwig bio = bio_map_user_iov(q, iter, gfp_mask); 5844d6af73dSChristoph Hellwig 5854d6af73dSChristoph Hellwig if (IS_ERR(bio)) 5864d6af73dSChristoph Hellwig return PTR_ERR(bio); 5874d6af73dSChristoph Hellwig 588aebf526bSChristoph Hellwig bio->bi_opf &= ~REQ_OP_MASK; 589aebf526bSChristoph Hellwig bio->bi_opf |= req_op(rq); 590aebf526bSChristoph Hellwig 5914d6af73dSChristoph Hellwig orig_bio = bio; 5924d6af73dSChristoph Hellwig 5934d6af73dSChristoph Hellwig /* 5944d6af73dSChristoph Hellwig * We link the bounce buffer in and could have to traverse it 5954d6af73dSChristoph Hellwig * later so we have to get a ref to prevent it from being freed 5964d6af73dSChristoph Hellwig */ 5970abc2a10SJens Axboe ret = blk_rq_append_bio(rq, &bio); 5984d6af73dSChristoph Hellwig if (ret) { 5994d6af73dSChristoph Hellwig __blk_rq_unmap_user(orig_bio); 6004d6af73dSChristoph Hellwig return ret; 6014d6af73dSChristoph Hellwig } 6020abc2a10SJens Axboe bio_get(bio); 6034d6af73dSChristoph Hellwig 6044d6af73dSChristoph Hellwig return 0; 6054d6af73dSChristoph Hellwig } 6064d6af73dSChristoph Hellwig 60786db1e29SJens Axboe /** 608aebf526bSChristoph Hellwig * blk_rq_map_user_iov - map user data to a request, for passthrough requests 60986db1e29SJens Axboe * @q: request queue where request should be inserted 61086db1e29SJens Axboe * @rq: request to map data to 611152e283fSFUJITA Tomonori * @map_data: pointer to the rq_map_data holding pages (if necessary) 61226e49cfcSKent Overstreet * @iter: iovec iterator 613a3bce90eSFUJITA Tomonori * @gfp_mask: memory allocation flags 61486db1e29SJens Axboe * 61586db1e29SJens Axboe * Description: 616710027a4SRandy Dunlap * Data will be mapped directly for zero copy I/O, if possible. Otherwise 61786db1e29SJens Axboe * a kernel bounce buffer is used. 61886db1e29SJens Axboe * 619710027a4SRandy Dunlap * A matching blk_rq_unmap_user() must be issued at the end of I/O, while 62086db1e29SJens Axboe * still in process context. 62186db1e29SJens Axboe * 62286db1e29SJens Axboe * Note: The mapped bio may need to be bounced through blk_queue_bounce() 62386db1e29SJens Axboe * before being submitted to the device, as pages mapped may be out of 62486db1e29SJens Axboe * reach. It's the callers responsibility to make sure this happens. The 62586db1e29SJens Axboe * original bio must be passed back in to blk_rq_unmap_user() for proper 62686db1e29SJens Axboe * unmapping. 62786db1e29SJens Axboe */ 62886db1e29SJens Axboe int blk_rq_map_user_iov(struct request_queue *q, struct request *rq, 62926e49cfcSKent Overstreet struct rq_map_data *map_data, 63026e49cfcSKent Overstreet const struct iov_iter *iter, gfp_t gfp_mask) 63186db1e29SJens Axboe { 632357f435dSAl Viro bool copy = false; 633357f435dSAl Viro unsigned long align = q->dma_pad_mask | queue_dma_alignment(q); 6344d6af73dSChristoph Hellwig struct bio *bio = NULL; 6354d6af73dSChristoph Hellwig struct iov_iter i; 63669e0927bSDouglas Gilbert int ret = -EINVAL; 63786db1e29SJens Axboe 638a0ac402cSLinus Torvalds if (!iter_is_iovec(iter)) 639a0ac402cSLinus Torvalds goto fail; 640a0ac402cSLinus Torvalds 641357f435dSAl Viro if (map_data) 6424d6af73dSChristoph Hellwig copy = true; 643357f435dSAl Viro else if (iov_iter_alignment(iter) & align) 644357f435dSAl Viro copy = true; 645357f435dSAl Viro else if (queue_virt_boundary(q)) 646357f435dSAl Viro copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter); 647afdc1a78SFUJITA Tomonori 6484d6af73dSChristoph Hellwig i = *iter; 6494d6af73dSChristoph Hellwig do { 6504d6af73dSChristoph Hellwig ret =__blk_rq_map_user_iov(rq, map_data, &i, gfp_mask, copy); 6514d6af73dSChristoph Hellwig if (ret) 6524d6af73dSChristoph Hellwig goto unmap_rq; 6534d6af73dSChristoph Hellwig if (!bio) 6544d6af73dSChristoph Hellwig bio = rq->bio; 6554d6af73dSChristoph Hellwig } while (iov_iter_count(&i)); 65686db1e29SJens Axboe 657f18573abSFUJITA Tomonori if (!bio_flagged(bio, BIO_USER_MAPPED)) 658e8064021SChristoph Hellwig rq->rq_flags |= RQF_COPY_USER; 65986db1e29SJens Axboe return 0; 6604d6af73dSChristoph Hellwig 6614d6af73dSChristoph Hellwig unmap_rq: 6623b7995a9SYang Yingliang blk_rq_unmap_user(bio); 663a0ac402cSLinus Torvalds fail: 6644d6af73dSChristoph Hellwig rq->bio = NULL; 66569e0927bSDouglas Gilbert return ret; 66686db1e29SJens Axboe } 667152e283fSFUJITA Tomonori EXPORT_SYMBOL(blk_rq_map_user_iov); 66886db1e29SJens Axboe 669ddad8dd0SChristoph Hellwig int blk_rq_map_user(struct request_queue *q, struct request *rq, 670ddad8dd0SChristoph Hellwig struct rq_map_data *map_data, void __user *ubuf, 671ddad8dd0SChristoph Hellwig unsigned long len, gfp_t gfp_mask) 672ddad8dd0SChristoph Hellwig { 67326e49cfcSKent Overstreet struct iovec iov; 67426e49cfcSKent Overstreet struct iov_iter i; 6758f7e885aSAl Viro int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i); 676ddad8dd0SChristoph Hellwig 6778f7e885aSAl Viro if (unlikely(ret < 0)) 6788f7e885aSAl Viro return ret; 679ddad8dd0SChristoph Hellwig 68026e49cfcSKent Overstreet return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask); 681ddad8dd0SChristoph Hellwig } 682ddad8dd0SChristoph Hellwig EXPORT_SYMBOL(blk_rq_map_user); 683ddad8dd0SChristoph Hellwig 68486db1e29SJens Axboe /** 68586db1e29SJens Axboe * blk_rq_unmap_user - unmap a request with user data 68686db1e29SJens Axboe * @bio: start of bio list 68786db1e29SJens Axboe * 68886db1e29SJens Axboe * Description: 68986db1e29SJens Axboe * Unmap a rq previously mapped by blk_rq_map_user(). The caller must 69086db1e29SJens Axboe * supply the original rq->bio from the blk_rq_map_user() return, since 691710027a4SRandy Dunlap * the I/O completion may have changed rq->bio. 69286db1e29SJens Axboe */ 69386db1e29SJens Axboe int blk_rq_unmap_user(struct bio *bio) 69486db1e29SJens Axboe { 69586db1e29SJens Axboe struct bio *mapped_bio; 69686db1e29SJens Axboe int ret = 0, ret2; 69786db1e29SJens Axboe 69886db1e29SJens Axboe while (bio) { 69986db1e29SJens Axboe mapped_bio = bio; 70086db1e29SJens Axboe if (unlikely(bio_flagged(bio, BIO_BOUNCED))) 70186db1e29SJens Axboe mapped_bio = bio->bi_private; 70286db1e29SJens Axboe 70386db1e29SJens Axboe ret2 = __blk_rq_unmap_user(mapped_bio); 70486db1e29SJens Axboe if (ret2 && !ret) 70586db1e29SJens Axboe ret = ret2; 70686db1e29SJens Axboe 70786db1e29SJens Axboe mapped_bio = bio; 70886db1e29SJens Axboe bio = bio->bi_next; 70986db1e29SJens Axboe bio_put(mapped_bio); 71086db1e29SJens Axboe } 71186db1e29SJens Axboe 71286db1e29SJens Axboe return ret; 71386db1e29SJens Axboe } 71486db1e29SJens Axboe EXPORT_SYMBOL(blk_rq_unmap_user); 71586db1e29SJens Axboe 71686db1e29SJens Axboe /** 717aebf526bSChristoph Hellwig * blk_rq_map_kern - map kernel data to a request, for passthrough requests 71886db1e29SJens Axboe * @q: request queue where request should be inserted 71986db1e29SJens Axboe * @rq: request to fill 72086db1e29SJens Axboe * @kbuf: the kernel buffer 72186db1e29SJens Axboe * @len: length of user data 72286db1e29SJens Axboe * @gfp_mask: memory allocation flags 72368154e90SFUJITA Tomonori * 72468154e90SFUJITA Tomonori * Description: 72568154e90SFUJITA Tomonori * Data will be mapped directly if possible. Otherwise a bounce 726e227867fSMasanari Iida * buffer is used. Can be called multiple times to append multiple 7273a5a3927SJames Bottomley * buffers. 72886db1e29SJens Axboe */ 72986db1e29SJens Axboe int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf, 73086db1e29SJens Axboe unsigned int len, gfp_t gfp_mask) 73186db1e29SJens Axboe { 73268154e90SFUJITA Tomonori int reading = rq_data_dir(rq) == READ; 73314417799SNamhyung Kim unsigned long addr = (unsigned long) kbuf; 73468154e90SFUJITA Tomonori int do_copy = 0; 7350abc2a10SJens Axboe struct bio *bio, *orig_bio; 7363a5a3927SJames Bottomley int ret; 73786db1e29SJens Axboe 738ae03bf63SMartin K. Petersen if (len > (queue_max_hw_sectors(q) << 9)) 73986db1e29SJens Axboe return -EINVAL; 74086db1e29SJens Axboe if (!len || !kbuf) 74186db1e29SJens Axboe return -EINVAL; 74286db1e29SJens Axboe 74314417799SNamhyung Kim do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf); 74468154e90SFUJITA Tomonori if (do_copy) 74568154e90SFUJITA Tomonori bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading); 74668154e90SFUJITA Tomonori else 74786db1e29SJens Axboe bio = bio_map_kern(q, kbuf, len, gfp_mask); 74868154e90SFUJITA Tomonori 74986db1e29SJens Axboe if (IS_ERR(bio)) 75086db1e29SJens Axboe return PTR_ERR(bio); 75186db1e29SJens Axboe 752aebf526bSChristoph Hellwig bio->bi_opf &= ~REQ_OP_MASK; 753aebf526bSChristoph Hellwig bio->bi_opf |= req_op(rq); 75486db1e29SJens Axboe 75568154e90SFUJITA Tomonori if (do_copy) 756e8064021SChristoph Hellwig rq->rq_flags |= RQF_COPY_USER; 75768154e90SFUJITA Tomonori 7580abc2a10SJens Axboe orig_bio = bio; 7590abc2a10SJens Axboe ret = blk_rq_append_bio(rq, &bio); 7603a5a3927SJames Bottomley if (unlikely(ret)) { 7613a5a3927SJames Bottomley /* request is too big */ 7620abc2a10SJens Axboe bio_put(orig_bio); 7633a5a3927SJames Bottomley return ret; 7643a5a3927SJames Bottomley } 7653a5a3927SJames Bottomley 76686db1e29SJens Axboe return 0; 76786db1e29SJens Axboe } 76886db1e29SJens Axboe EXPORT_SYMBOL(blk_rq_map_kern); 769