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 14130879f1SChristoph Hellwig struct bio_map_data { 15f3256075SChristoph Hellwig bool is_our_pages : 1; 16f3256075SChristoph Hellwig bool is_null_mapped : 1; 17130879f1SChristoph Hellwig struct iov_iter iter; 18130879f1SChristoph Hellwig struct iovec iov[]; 19130879f1SChristoph Hellwig }; 20130879f1SChristoph Hellwig 21130879f1SChristoph Hellwig static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data, 22130879f1SChristoph Hellwig gfp_t gfp_mask) 23130879f1SChristoph Hellwig { 24130879f1SChristoph Hellwig struct bio_map_data *bmd; 25130879f1SChristoph Hellwig 26130879f1SChristoph Hellwig if (data->nr_segs > UIO_MAXIOV) 27130879f1SChristoph Hellwig return NULL; 28130879f1SChristoph Hellwig 29130879f1SChristoph Hellwig bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask); 30130879f1SChristoph Hellwig if (!bmd) 31130879f1SChristoph Hellwig return NULL; 32130879f1SChristoph Hellwig memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); 33130879f1SChristoph Hellwig bmd->iter = *data; 34130879f1SChristoph Hellwig bmd->iter.iov = bmd->iov; 35130879f1SChristoph Hellwig return bmd; 36130879f1SChristoph Hellwig } 37130879f1SChristoph Hellwig 38130879f1SChristoph Hellwig /** 39130879f1SChristoph Hellwig * bio_copy_from_iter - copy all pages from iov_iter to bio 40130879f1SChristoph Hellwig * @bio: The &struct bio which describes the I/O as destination 41130879f1SChristoph Hellwig * @iter: iov_iter as source 42130879f1SChristoph Hellwig * 43130879f1SChristoph Hellwig * Copy all pages from iov_iter to bio. 44130879f1SChristoph Hellwig * Returns 0 on success, or error on failure. 45130879f1SChristoph Hellwig */ 46130879f1SChristoph Hellwig static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter) 47130879f1SChristoph Hellwig { 48130879f1SChristoph Hellwig struct bio_vec *bvec; 49130879f1SChristoph Hellwig struct bvec_iter_all iter_all; 50130879f1SChristoph Hellwig 51130879f1SChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 52130879f1SChristoph Hellwig ssize_t ret; 53130879f1SChristoph Hellwig 54130879f1SChristoph Hellwig ret = copy_page_from_iter(bvec->bv_page, 55130879f1SChristoph Hellwig bvec->bv_offset, 56130879f1SChristoph Hellwig bvec->bv_len, 57130879f1SChristoph Hellwig iter); 58130879f1SChristoph Hellwig 59130879f1SChristoph Hellwig if (!iov_iter_count(iter)) 60130879f1SChristoph Hellwig break; 61130879f1SChristoph Hellwig 62130879f1SChristoph Hellwig if (ret < bvec->bv_len) 63130879f1SChristoph Hellwig return -EFAULT; 64130879f1SChristoph Hellwig } 65130879f1SChristoph Hellwig 66130879f1SChristoph Hellwig return 0; 67130879f1SChristoph Hellwig } 68130879f1SChristoph Hellwig 69130879f1SChristoph Hellwig /** 70130879f1SChristoph Hellwig * bio_copy_to_iter - copy all pages from bio to iov_iter 71130879f1SChristoph Hellwig * @bio: The &struct bio which describes the I/O as source 72130879f1SChristoph Hellwig * @iter: iov_iter as destination 73130879f1SChristoph Hellwig * 74130879f1SChristoph Hellwig * Copy all pages from bio to iov_iter. 75130879f1SChristoph Hellwig * Returns 0 on success, or error on failure. 76130879f1SChristoph Hellwig */ 77130879f1SChristoph Hellwig static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter) 78130879f1SChristoph Hellwig { 79130879f1SChristoph Hellwig struct bio_vec *bvec; 80130879f1SChristoph Hellwig struct bvec_iter_all iter_all; 81130879f1SChristoph Hellwig 82130879f1SChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 83130879f1SChristoph Hellwig ssize_t ret; 84130879f1SChristoph Hellwig 85130879f1SChristoph Hellwig ret = copy_page_to_iter(bvec->bv_page, 86130879f1SChristoph Hellwig bvec->bv_offset, 87130879f1SChristoph Hellwig bvec->bv_len, 88130879f1SChristoph Hellwig &iter); 89130879f1SChristoph Hellwig 90130879f1SChristoph Hellwig if (!iov_iter_count(&iter)) 91130879f1SChristoph Hellwig break; 92130879f1SChristoph Hellwig 93130879f1SChristoph Hellwig if (ret < bvec->bv_len) 94130879f1SChristoph Hellwig return -EFAULT; 95130879f1SChristoph Hellwig } 96130879f1SChristoph Hellwig 97130879f1SChristoph Hellwig return 0; 98130879f1SChristoph Hellwig } 99130879f1SChristoph Hellwig 100130879f1SChristoph Hellwig /** 101130879f1SChristoph Hellwig * bio_uncopy_user - finish previously mapped bio 102130879f1SChristoph Hellwig * @bio: bio being terminated 103130879f1SChristoph Hellwig * 104130879f1SChristoph Hellwig * Free pages allocated from bio_copy_user_iov() and write back data 105130879f1SChristoph Hellwig * to user space in case of a read. 106130879f1SChristoph Hellwig */ 107130879f1SChristoph Hellwig static int bio_uncopy_user(struct bio *bio) 108130879f1SChristoph Hellwig { 109130879f1SChristoph Hellwig struct bio_map_data *bmd = bio->bi_private; 110130879f1SChristoph Hellwig int ret = 0; 111130879f1SChristoph Hellwig 1123310eebaSChristoph Hellwig if (!bmd->is_null_mapped) { 113130879f1SChristoph Hellwig /* 114130879f1SChristoph Hellwig * if we're in a workqueue, the request is orphaned, so 115130879f1SChristoph Hellwig * don't copy into a random user address space, just free 116130879f1SChristoph Hellwig * and return -EINTR so user space doesn't expect any data. 117130879f1SChristoph Hellwig */ 118130879f1SChristoph Hellwig if (!current->mm) 119130879f1SChristoph Hellwig ret = -EINTR; 120130879f1SChristoph Hellwig else if (bio_data_dir(bio) == READ) 121130879f1SChristoph Hellwig ret = bio_copy_to_iter(bio, bmd->iter); 122130879f1SChristoph Hellwig if (bmd->is_our_pages) 123130879f1SChristoph Hellwig bio_free_pages(bio); 124130879f1SChristoph Hellwig } 125130879f1SChristoph Hellwig kfree(bmd); 126130879f1SChristoph Hellwig return ret; 127130879f1SChristoph Hellwig } 128130879f1SChristoph Hellwig 1297589ad67SChristoph Hellwig static int bio_copy_user_iov(struct request *rq, struct rq_map_data *map_data, 1307589ad67SChristoph Hellwig struct iov_iter *iter, gfp_t gfp_mask) 131130879f1SChristoph Hellwig { 132130879f1SChristoph Hellwig struct bio_map_data *bmd; 133130879f1SChristoph Hellwig struct page *page; 134393bb12eSChristoph Hellwig struct bio *bio; 135130879f1SChristoph Hellwig int i = 0, ret; 136130879f1SChristoph Hellwig int nr_pages; 137130879f1SChristoph Hellwig unsigned int len = iter->count; 138130879f1SChristoph Hellwig unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0; 139130879f1SChristoph Hellwig 140130879f1SChristoph Hellwig bmd = bio_alloc_map_data(iter, gfp_mask); 141130879f1SChristoph Hellwig if (!bmd) 1427589ad67SChristoph Hellwig return -ENOMEM; 143130879f1SChristoph Hellwig 144130879f1SChristoph Hellwig /* 145130879f1SChristoph Hellwig * We need to do a deep copy of the iov_iter including the iovecs. 146130879f1SChristoph Hellwig * The caller provided iov might point to an on-stack or otherwise 147130879f1SChristoph Hellwig * shortlived one. 148130879f1SChristoph Hellwig */ 149f3256075SChristoph Hellwig bmd->is_our_pages = !map_data; 15003859717SChristoph Hellwig bmd->is_null_mapped = (map_data && map_data->null_mapped); 151130879f1SChristoph Hellwig 1525f7136dbSMatthew Wilcox (Oracle) nr_pages = bio_max_segs(DIV_ROUND_UP(offset + len, PAGE_SIZE)); 153130879f1SChristoph Hellwig 154130879f1SChristoph Hellwig ret = -ENOMEM; 155*066ff571SChristoph Hellwig bio = bio_kmalloc(nr_pages, gfp_mask); 156130879f1SChristoph Hellwig if (!bio) 157130879f1SChristoph Hellwig goto out_bmd; 158*066ff571SChristoph Hellwig bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, req_op(rq)); 159130879f1SChristoph Hellwig 160130879f1SChristoph Hellwig if (map_data) { 161130879f1SChristoph Hellwig nr_pages = 1 << map_data->page_order; 162130879f1SChristoph Hellwig i = map_data->offset / PAGE_SIZE; 163130879f1SChristoph Hellwig } 164130879f1SChristoph Hellwig while (len) { 165130879f1SChristoph Hellwig unsigned int bytes = PAGE_SIZE; 166130879f1SChristoph Hellwig 167130879f1SChristoph Hellwig bytes -= offset; 168130879f1SChristoph Hellwig 169130879f1SChristoph Hellwig if (bytes > len) 170130879f1SChristoph Hellwig bytes = len; 171130879f1SChristoph Hellwig 172130879f1SChristoph Hellwig if (map_data) { 173130879f1SChristoph Hellwig if (i == map_data->nr_entries * nr_pages) { 174130879f1SChristoph Hellwig ret = -ENOMEM; 1757589ad67SChristoph Hellwig goto cleanup; 176130879f1SChristoph Hellwig } 177130879f1SChristoph Hellwig 178130879f1SChristoph Hellwig page = map_data->pages[i / nr_pages]; 179130879f1SChristoph Hellwig page += (i % nr_pages); 180130879f1SChristoph Hellwig 181130879f1SChristoph Hellwig i++; 182130879f1SChristoph Hellwig } else { 183ce288e05SChristoph Hellwig page = alloc_page(GFP_NOIO | gfp_mask); 184130879f1SChristoph Hellwig if (!page) { 185130879f1SChristoph Hellwig ret = -ENOMEM; 1867589ad67SChristoph Hellwig goto cleanup; 187130879f1SChristoph Hellwig } 188130879f1SChristoph Hellwig } 189130879f1SChristoph Hellwig 1907589ad67SChristoph Hellwig if (bio_add_pc_page(rq->q, bio, page, bytes, offset) < bytes) { 191130879f1SChristoph Hellwig if (!map_data) 192130879f1SChristoph Hellwig __free_page(page); 193130879f1SChristoph Hellwig break; 194130879f1SChristoph Hellwig } 195130879f1SChristoph Hellwig 196130879f1SChristoph Hellwig len -= bytes; 197130879f1SChristoph Hellwig offset = 0; 198130879f1SChristoph Hellwig } 199130879f1SChristoph Hellwig 200130879f1SChristoph Hellwig if (map_data) 201130879f1SChristoph Hellwig map_data->offset += bio->bi_iter.bi_size; 202130879f1SChristoph Hellwig 203130879f1SChristoph Hellwig /* 204130879f1SChristoph Hellwig * success 205130879f1SChristoph Hellwig */ 206130879f1SChristoph Hellwig if ((iov_iter_rw(iter) == WRITE && 207130879f1SChristoph Hellwig (!map_data || !map_data->null_mapped)) || 208130879f1SChristoph Hellwig (map_data && map_data->from_user)) { 209130879f1SChristoph Hellwig ret = bio_copy_from_iter(bio, iter); 210130879f1SChristoph Hellwig if (ret) 211130879f1SChristoph Hellwig goto cleanup; 212130879f1SChristoph Hellwig } else { 213130879f1SChristoph Hellwig if (bmd->is_our_pages) 214130879f1SChristoph Hellwig zero_fill_bio(bio); 215130879f1SChristoph Hellwig iov_iter_advance(iter, bio->bi_iter.bi_size); 216130879f1SChristoph Hellwig } 217130879f1SChristoph Hellwig 218130879f1SChristoph Hellwig bio->bi_private = bmd; 2197589ad67SChristoph Hellwig 220393bb12eSChristoph Hellwig ret = blk_rq_append_bio(rq, bio); 2217589ad67SChristoph Hellwig if (ret) 2227589ad67SChristoph Hellwig goto cleanup; 2237589ad67SChristoph Hellwig return 0; 224130879f1SChristoph Hellwig cleanup: 225130879f1SChristoph Hellwig if (!map_data) 226130879f1SChristoph Hellwig bio_free_pages(bio); 227*066ff571SChristoph Hellwig bio_uninit(bio); 228*066ff571SChristoph Hellwig kfree(bio); 229130879f1SChristoph Hellwig out_bmd: 230130879f1SChristoph Hellwig kfree(bmd); 2317589ad67SChristoph Hellwig return ret; 232130879f1SChristoph Hellwig } 233130879f1SChristoph Hellwig 2347589ad67SChristoph Hellwig static int bio_map_user_iov(struct request *rq, struct iov_iter *iter, 2357589ad67SChristoph Hellwig gfp_t gfp_mask) 236130879f1SChristoph Hellwig { 2377589ad67SChristoph Hellwig unsigned int max_sectors = queue_max_hw_sectors(rq->q); 238*066ff571SChristoph Hellwig unsigned int nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS); 239393bb12eSChristoph Hellwig struct bio *bio; 240130879f1SChristoph Hellwig int ret; 2417589ad67SChristoph Hellwig int j; 242130879f1SChristoph Hellwig 243130879f1SChristoph Hellwig if (!iov_iter_count(iter)) 2447589ad67SChristoph Hellwig return -EINVAL; 245130879f1SChristoph Hellwig 246*066ff571SChristoph Hellwig bio = bio_kmalloc(nr_vecs, gfp_mask); 247130879f1SChristoph Hellwig if (!bio) 2487589ad67SChristoph Hellwig return -ENOMEM; 249*066ff571SChristoph Hellwig bio_init(bio, NULL, bio->bi_inline_vecs, nr_vecs, req_op(rq)); 250130879f1SChristoph Hellwig 251130879f1SChristoph Hellwig while (iov_iter_count(iter)) { 252130879f1SChristoph Hellwig struct page **pages; 253130879f1SChristoph Hellwig ssize_t bytes; 254130879f1SChristoph Hellwig size_t offs, added = 0; 255130879f1SChristoph Hellwig int npages; 256130879f1SChristoph Hellwig 257130879f1SChristoph Hellwig bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs); 258130879f1SChristoph Hellwig if (unlikely(bytes <= 0)) { 259130879f1SChristoph Hellwig ret = bytes ? bytes : -EFAULT; 260130879f1SChristoph Hellwig goto out_unmap; 261130879f1SChristoph Hellwig } 262130879f1SChristoph Hellwig 263130879f1SChristoph Hellwig npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE); 264130879f1SChristoph Hellwig 2657589ad67SChristoph Hellwig if (unlikely(offs & queue_dma_alignment(rq->q))) { 266130879f1SChristoph Hellwig ret = -EINVAL; 267130879f1SChristoph Hellwig j = 0; 268130879f1SChristoph Hellwig } else { 269130879f1SChristoph Hellwig for (j = 0; j < npages; j++) { 270130879f1SChristoph Hellwig struct page *page = pages[j]; 271130879f1SChristoph Hellwig unsigned int n = PAGE_SIZE - offs; 272130879f1SChristoph Hellwig bool same_page = false; 273130879f1SChristoph Hellwig 274130879f1SChristoph Hellwig if (n > bytes) 275130879f1SChristoph Hellwig n = bytes; 276130879f1SChristoph Hellwig 2777589ad67SChristoph Hellwig if (!bio_add_hw_page(rq->q, bio, page, n, offs, 278e4581105SChristoph Hellwig max_sectors, &same_page)) { 279130879f1SChristoph Hellwig if (same_page) 280130879f1SChristoph Hellwig put_page(page); 281130879f1SChristoph Hellwig break; 282130879f1SChristoph Hellwig } 283130879f1SChristoph Hellwig 284130879f1SChristoph Hellwig added += n; 285130879f1SChristoph Hellwig bytes -= n; 286130879f1SChristoph Hellwig offs = 0; 287130879f1SChristoph Hellwig } 288130879f1SChristoph Hellwig iov_iter_advance(iter, added); 289130879f1SChristoph Hellwig } 290130879f1SChristoph Hellwig /* 291130879f1SChristoph Hellwig * release the pages we didn't map into the bio, if any 292130879f1SChristoph Hellwig */ 293130879f1SChristoph Hellwig while (j < npages) 294130879f1SChristoph Hellwig put_page(pages[j++]); 295130879f1SChristoph Hellwig kvfree(pages); 296130879f1SChristoph Hellwig /* couldn't stuff something into bio? */ 297130879f1SChristoph Hellwig if (bytes) 298130879f1SChristoph Hellwig break; 299130879f1SChristoph Hellwig } 300130879f1SChristoph Hellwig 301393bb12eSChristoph Hellwig ret = blk_rq_append_bio(rq, bio); 3027589ad67SChristoph Hellwig if (ret) 303393bb12eSChristoph Hellwig goto out_unmap; 3047589ad67SChristoph Hellwig return 0; 3057589ad67SChristoph Hellwig 306130879f1SChristoph Hellwig out_unmap: 307130879f1SChristoph Hellwig bio_release_pages(bio, false); 308*066ff571SChristoph Hellwig bio_uninit(bio); 309*066ff571SChristoph Hellwig kfree(bio); 3107589ad67SChristoph Hellwig return ret; 311130879f1SChristoph Hellwig } 312130879f1SChristoph Hellwig 313130879f1SChristoph Hellwig static void bio_invalidate_vmalloc_pages(struct bio *bio) 314130879f1SChristoph Hellwig { 315f358afc5SChristoph Hellwig #ifdef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE 316130879f1SChristoph Hellwig if (bio->bi_private && !op_is_write(bio_op(bio))) { 317130879f1SChristoph Hellwig unsigned long i, len = 0; 318130879f1SChristoph Hellwig 319130879f1SChristoph Hellwig for (i = 0; i < bio->bi_vcnt; i++) 320130879f1SChristoph Hellwig len += bio->bi_io_vec[i].bv_len; 321130879f1SChristoph Hellwig invalidate_kernel_vmap_range(bio->bi_private, len); 322130879f1SChristoph Hellwig } 323130879f1SChristoph Hellwig #endif 324130879f1SChristoph Hellwig } 325130879f1SChristoph Hellwig 326130879f1SChristoph Hellwig static void bio_map_kern_endio(struct bio *bio) 327130879f1SChristoph Hellwig { 328130879f1SChristoph Hellwig bio_invalidate_vmalloc_pages(bio); 329*066ff571SChristoph Hellwig bio_uninit(bio); 330*066ff571SChristoph Hellwig kfree(bio); 331130879f1SChristoph Hellwig } 332130879f1SChristoph Hellwig 333130879f1SChristoph Hellwig /** 334130879f1SChristoph Hellwig * bio_map_kern - map kernel address into bio 335130879f1SChristoph Hellwig * @q: the struct request_queue for the bio 336130879f1SChristoph Hellwig * @data: pointer to buffer to map 337130879f1SChristoph Hellwig * @len: length in bytes 338130879f1SChristoph Hellwig * @gfp_mask: allocation flags for bio allocation 339130879f1SChristoph Hellwig * 340130879f1SChristoph Hellwig * Map the kernel address into a bio suitable for io to a block 341130879f1SChristoph Hellwig * device. Returns an error pointer in case of error. 342130879f1SChristoph Hellwig */ 343130879f1SChristoph Hellwig static struct bio *bio_map_kern(struct request_queue *q, void *data, 344130879f1SChristoph Hellwig unsigned int len, gfp_t gfp_mask) 345130879f1SChristoph Hellwig { 346130879f1SChristoph Hellwig unsigned long kaddr = (unsigned long)data; 347130879f1SChristoph Hellwig unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 348130879f1SChristoph Hellwig unsigned long start = kaddr >> PAGE_SHIFT; 349130879f1SChristoph Hellwig const int nr_pages = end - start; 350130879f1SChristoph Hellwig bool is_vmalloc = is_vmalloc_addr(data); 351130879f1SChristoph Hellwig struct page *page; 352130879f1SChristoph Hellwig int offset, i; 353130879f1SChristoph Hellwig struct bio *bio; 354130879f1SChristoph Hellwig 355*066ff571SChristoph Hellwig bio = bio_kmalloc(nr_pages, gfp_mask); 356130879f1SChristoph Hellwig if (!bio) 357130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 358*066ff571SChristoph Hellwig bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, 0); 359130879f1SChristoph Hellwig 360130879f1SChristoph Hellwig if (is_vmalloc) { 361130879f1SChristoph Hellwig flush_kernel_vmap_range(data, len); 362130879f1SChristoph Hellwig bio->bi_private = data; 363130879f1SChristoph Hellwig } 364130879f1SChristoph Hellwig 365130879f1SChristoph Hellwig offset = offset_in_page(kaddr); 366130879f1SChristoph Hellwig for (i = 0; i < nr_pages; i++) { 367130879f1SChristoph Hellwig unsigned int bytes = PAGE_SIZE - offset; 368130879f1SChristoph Hellwig 369130879f1SChristoph Hellwig if (len <= 0) 370130879f1SChristoph Hellwig break; 371130879f1SChristoph Hellwig 372130879f1SChristoph Hellwig if (bytes > len) 373130879f1SChristoph Hellwig bytes = len; 374130879f1SChristoph Hellwig 375130879f1SChristoph Hellwig if (!is_vmalloc) 376130879f1SChristoph Hellwig page = virt_to_page(data); 377130879f1SChristoph Hellwig else 378130879f1SChristoph Hellwig page = vmalloc_to_page(data); 379130879f1SChristoph Hellwig if (bio_add_pc_page(q, bio, page, bytes, 380130879f1SChristoph Hellwig offset) < bytes) { 381130879f1SChristoph Hellwig /* we don't support partial mappings */ 382*066ff571SChristoph Hellwig bio_uninit(bio); 383*066ff571SChristoph Hellwig kfree(bio); 384130879f1SChristoph Hellwig return ERR_PTR(-EINVAL); 385130879f1SChristoph Hellwig } 386130879f1SChristoph Hellwig 387130879f1SChristoph Hellwig data += bytes; 388130879f1SChristoph Hellwig len -= bytes; 389130879f1SChristoph Hellwig offset = 0; 390130879f1SChristoph Hellwig } 391130879f1SChristoph Hellwig 392130879f1SChristoph Hellwig bio->bi_end_io = bio_map_kern_endio; 393130879f1SChristoph Hellwig return bio; 394130879f1SChristoph Hellwig } 395130879f1SChristoph Hellwig 396130879f1SChristoph Hellwig static void bio_copy_kern_endio(struct bio *bio) 397130879f1SChristoph Hellwig { 398130879f1SChristoph Hellwig bio_free_pages(bio); 399*066ff571SChristoph Hellwig bio_uninit(bio); 400*066ff571SChristoph Hellwig kfree(bio); 401130879f1SChristoph Hellwig } 402130879f1SChristoph Hellwig 403130879f1SChristoph Hellwig static void bio_copy_kern_endio_read(struct bio *bio) 404130879f1SChristoph Hellwig { 405130879f1SChristoph Hellwig char *p = bio->bi_private; 406130879f1SChristoph Hellwig struct bio_vec *bvec; 407130879f1SChristoph Hellwig struct bvec_iter_all iter_all; 408130879f1SChristoph Hellwig 409130879f1SChristoph Hellwig bio_for_each_segment_all(bvec, bio, iter_all) { 410d24920e2SChristoph Hellwig memcpy_from_bvec(p, bvec); 411130879f1SChristoph Hellwig p += bvec->bv_len; 412130879f1SChristoph Hellwig } 413130879f1SChristoph Hellwig 414130879f1SChristoph Hellwig bio_copy_kern_endio(bio); 415130879f1SChristoph Hellwig } 416130879f1SChristoph Hellwig 417130879f1SChristoph Hellwig /** 418130879f1SChristoph Hellwig * bio_copy_kern - copy kernel address into bio 419130879f1SChristoph Hellwig * @q: the struct request_queue for the bio 420130879f1SChristoph Hellwig * @data: pointer to buffer to copy 421130879f1SChristoph Hellwig * @len: length in bytes 422130879f1SChristoph Hellwig * @gfp_mask: allocation flags for bio and page allocation 423130879f1SChristoph Hellwig * @reading: data direction is READ 424130879f1SChristoph Hellwig * 425130879f1SChristoph Hellwig * copy the kernel address into a bio suitable for io to a block 426130879f1SChristoph Hellwig * device. Returns an error pointer in case of error. 427130879f1SChristoph Hellwig */ 428130879f1SChristoph Hellwig static struct bio *bio_copy_kern(struct request_queue *q, void *data, 429130879f1SChristoph Hellwig unsigned int len, gfp_t gfp_mask, int reading) 430130879f1SChristoph Hellwig { 431130879f1SChristoph Hellwig unsigned long kaddr = (unsigned long)data; 432130879f1SChristoph Hellwig unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 433130879f1SChristoph Hellwig unsigned long start = kaddr >> PAGE_SHIFT; 434130879f1SChristoph Hellwig struct bio *bio; 435130879f1SChristoph Hellwig void *p = data; 436130879f1SChristoph Hellwig int nr_pages = 0; 437130879f1SChristoph Hellwig 438130879f1SChristoph Hellwig /* 439130879f1SChristoph Hellwig * Overflow, abort 440130879f1SChristoph Hellwig */ 441130879f1SChristoph Hellwig if (end < start) 442130879f1SChristoph Hellwig return ERR_PTR(-EINVAL); 443130879f1SChristoph Hellwig 444130879f1SChristoph Hellwig nr_pages = end - start; 445*066ff571SChristoph Hellwig bio = bio_kmalloc(nr_pages, gfp_mask); 446130879f1SChristoph Hellwig if (!bio) 447130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 448*066ff571SChristoph Hellwig bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, 0); 449130879f1SChristoph Hellwig 450130879f1SChristoph Hellwig while (len) { 451130879f1SChristoph Hellwig struct page *page; 452130879f1SChristoph Hellwig unsigned int bytes = PAGE_SIZE; 453130879f1SChristoph Hellwig 454130879f1SChristoph Hellwig if (bytes > len) 455130879f1SChristoph Hellwig bytes = len; 456130879f1SChristoph Hellwig 457cc8f7fe1SHaimin Zhang page = alloc_page(GFP_NOIO | __GFP_ZERO | gfp_mask); 458130879f1SChristoph Hellwig if (!page) 459130879f1SChristoph Hellwig goto cleanup; 460130879f1SChristoph Hellwig 461130879f1SChristoph Hellwig if (!reading) 462130879f1SChristoph Hellwig memcpy(page_address(page), p, bytes); 463130879f1SChristoph Hellwig 464130879f1SChristoph Hellwig if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) 465130879f1SChristoph Hellwig break; 466130879f1SChristoph Hellwig 467130879f1SChristoph Hellwig len -= bytes; 468130879f1SChristoph Hellwig p += bytes; 469130879f1SChristoph Hellwig } 470130879f1SChristoph Hellwig 471130879f1SChristoph Hellwig if (reading) { 472130879f1SChristoph Hellwig bio->bi_end_io = bio_copy_kern_endio_read; 473130879f1SChristoph Hellwig bio->bi_private = data; 474130879f1SChristoph Hellwig } else { 475130879f1SChristoph Hellwig bio->bi_end_io = bio_copy_kern_endio; 476130879f1SChristoph Hellwig } 477130879f1SChristoph Hellwig 478130879f1SChristoph Hellwig return bio; 479130879f1SChristoph Hellwig 480130879f1SChristoph Hellwig cleanup: 481130879f1SChristoph Hellwig bio_free_pages(bio); 482*066ff571SChristoph Hellwig bio_uninit(bio); 483*066ff571SChristoph Hellwig kfree(bio); 484130879f1SChristoph Hellwig return ERR_PTR(-ENOMEM); 485130879f1SChristoph Hellwig } 486130879f1SChristoph Hellwig 48798d61d5bSChristoph Hellwig /* 4880abc2a10SJens Axboe * Append a bio to a passthrough request. Only works if the bio can be merged 4890abc2a10SJens Axboe * into the request based on the driver constraints. 49098d61d5bSChristoph Hellwig */ 491393bb12eSChristoph Hellwig int blk_rq_append_bio(struct request *rq, struct bio *bio) 49286db1e29SJens Axboe { 49314ccb66bSChristoph Hellwig struct bvec_iter iter; 49414ccb66bSChristoph Hellwig struct bio_vec bv; 49514ccb66bSChristoph Hellwig unsigned int nr_segs = 0; 4960abc2a10SJens Axboe 497393bb12eSChristoph Hellwig bio_for_each_bvec(bv, bio, iter) 49814ccb66bSChristoph Hellwig nr_segs++; 49914ccb66bSChristoph Hellwig 50098d61d5bSChristoph Hellwig if (!rq->bio) { 501393bb12eSChristoph Hellwig blk_rq_bio_prep(rq, bio, nr_segs); 50298d61d5bSChristoph Hellwig } else { 503393bb12eSChristoph Hellwig if (!ll_back_merge_fn(rq, bio, nr_segs)) 50486db1e29SJens Axboe return -EINVAL; 505393bb12eSChristoph Hellwig rq->biotail->bi_next = bio; 506393bb12eSChristoph Hellwig rq->biotail = bio; 507393bb12eSChristoph Hellwig rq->__data_len += (bio)->bi_iter.bi_size; 508393bb12eSChristoph Hellwig bio_crypt_free_ctx(bio); 50986db1e29SJens Axboe } 51098d61d5bSChristoph Hellwig 51186db1e29SJens Axboe return 0; 51286db1e29SJens Axboe } 51398d61d5bSChristoph Hellwig EXPORT_SYMBOL(blk_rq_append_bio); 51486db1e29SJens Axboe 51586db1e29SJens Axboe /** 516aebf526bSChristoph Hellwig * blk_rq_map_user_iov - map user data to a request, for passthrough requests 51786db1e29SJens Axboe * @q: request queue where request should be inserted 51886db1e29SJens Axboe * @rq: request to map data to 519152e283fSFUJITA Tomonori * @map_data: pointer to the rq_map_data holding pages (if necessary) 52026e49cfcSKent Overstreet * @iter: iovec iterator 521a3bce90eSFUJITA Tomonori * @gfp_mask: memory allocation flags 52286db1e29SJens Axboe * 52386db1e29SJens Axboe * Description: 524710027a4SRandy Dunlap * Data will be mapped directly for zero copy I/O, if possible. Otherwise 52586db1e29SJens Axboe * a kernel bounce buffer is used. 52686db1e29SJens Axboe * 527710027a4SRandy Dunlap * A matching blk_rq_unmap_user() must be issued at the end of I/O, while 52886db1e29SJens Axboe * still in process context. 52986db1e29SJens Axboe */ 53086db1e29SJens Axboe int blk_rq_map_user_iov(struct request_queue *q, struct request *rq, 53126e49cfcSKent Overstreet struct rq_map_data *map_data, 53226e49cfcSKent Overstreet const struct iov_iter *iter, gfp_t gfp_mask) 53386db1e29SJens Axboe { 534357f435dSAl Viro bool copy = false; 535357f435dSAl Viro unsigned long align = q->dma_pad_mask | queue_dma_alignment(q); 5364d6af73dSChristoph Hellwig struct bio *bio = NULL; 5374d6af73dSChristoph Hellwig struct iov_iter i; 53869e0927bSDouglas Gilbert int ret = -EINVAL; 53986db1e29SJens Axboe 540a0ac402cSLinus Torvalds if (!iter_is_iovec(iter)) 541a0ac402cSLinus Torvalds goto fail; 542a0ac402cSLinus Torvalds 543357f435dSAl Viro if (map_data) 5444d6af73dSChristoph Hellwig copy = true; 545393bb12eSChristoph Hellwig else if (blk_queue_may_bounce(q)) 546393bb12eSChristoph Hellwig copy = true; 547357f435dSAl Viro else if (iov_iter_alignment(iter) & align) 548357f435dSAl Viro copy = true; 549357f435dSAl Viro else if (queue_virt_boundary(q)) 550357f435dSAl Viro copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter); 551afdc1a78SFUJITA Tomonori 5524d6af73dSChristoph Hellwig i = *iter; 5534d6af73dSChristoph Hellwig do { 5547589ad67SChristoph Hellwig if (copy) 5557589ad67SChristoph Hellwig ret = bio_copy_user_iov(rq, map_data, &i, gfp_mask); 5567589ad67SChristoph Hellwig else 5577589ad67SChristoph Hellwig ret = bio_map_user_iov(rq, &i, gfp_mask); 5584d6af73dSChristoph Hellwig if (ret) 5594d6af73dSChristoph Hellwig goto unmap_rq; 5604d6af73dSChristoph Hellwig if (!bio) 5614d6af73dSChristoph Hellwig bio = rq->bio; 5624d6af73dSChristoph Hellwig } while (iov_iter_count(&i)); 56386db1e29SJens Axboe 56486db1e29SJens Axboe return 0; 5654d6af73dSChristoph Hellwig 5664d6af73dSChristoph Hellwig unmap_rq: 5673b7995a9SYang Yingliang blk_rq_unmap_user(bio); 568a0ac402cSLinus Torvalds fail: 5694d6af73dSChristoph Hellwig rq->bio = NULL; 57069e0927bSDouglas Gilbert return ret; 57186db1e29SJens Axboe } 572152e283fSFUJITA Tomonori EXPORT_SYMBOL(blk_rq_map_user_iov); 57386db1e29SJens Axboe 574ddad8dd0SChristoph Hellwig int blk_rq_map_user(struct request_queue *q, struct request *rq, 575ddad8dd0SChristoph Hellwig struct rq_map_data *map_data, void __user *ubuf, 576ddad8dd0SChristoph Hellwig unsigned long len, gfp_t gfp_mask) 577ddad8dd0SChristoph Hellwig { 57826e49cfcSKent Overstreet struct iovec iov; 57926e49cfcSKent Overstreet struct iov_iter i; 5808f7e885aSAl Viro int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i); 581ddad8dd0SChristoph Hellwig 5828f7e885aSAl Viro if (unlikely(ret < 0)) 5838f7e885aSAl Viro return ret; 584ddad8dd0SChristoph Hellwig 58526e49cfcSKent Overstreet return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask); 586ddad8dd0SChristoph Hellwig } 587ddad8dd0SChristoph Hellwig EXPORT_SYMBOL(blk_rq_map_user); 588ddad8dd0SChristoph Hellwig 58986db1e29SJens Axboe /** 59086db1e29SJens Axboe * blk_rq_unmap_user - unmap a request with user data 59186db1e29SJens Axboe * @bio: start of bio list 59286db1e29SJens Axboe * 59386db1e29SJens Axboe * Description: 59486db1e29SJens Axboe * Unmap a rq previously mapped by blk_rq_map_user(). The caller must 59586db1e29SJens Axboe * supply the original rq->bio from the blk_rq_map_user() return, since 596710027a4SRandy Dunlap * the I/O completion may have changed rq->bio. 59786db1e29SJens Axboe */ 59886db1e29SJens Axboe int blk_rq_unmap_user(struct bio *bio) 59986db1e29SJens Axboe { 600393bb12eSChristoph Hellwig struct bio *next_bio; 60186db1e29SJens Axboe int ret = 0, ret2; 60286db1e29SJens Axboe 60386db1e29SJens Axboe while (bio) { 6043310eebaSChristoph Hellwig if (bio->bi_private) { 605393bb12eSChristoph Hellwig ret2 = bio_uncopy_user(bio); 60686db1e29SJens Axboe if (ret2 && !ret) 60786db1e29SJens Axboe ret = ret2; 6083310eebaSChristoph Hellwig } else { 609393bb12eSChristoph Hellwig bio_release_pages(bio, bio_data_dir(bio) == READ); 6107b63c052SChristoph Hellwig } 61186db1e29SJens Axboe 612393bb12eSChristoph Hellwig next_bio = bio; 61386db1e29SJens Axboe bio = bio->bi_next; 614*066ff571SChristoph Hellwig bio_uninit(next_bio); 615*066ff571SChristoph Hellwig kfree(next_bio); 61686db1e29SJens Axboe } 61786db1e29SJens Axboe 61886db1e29SJens Axboe return ret; 61986db1e29SJens Axboe } 62086db1e29SJens Axboe EXPORT_SYMBOL(blk_rq_unmap_user); 62186db1e29SJens Axboe 62286db1e29SJens Axboe /** 623aebf526bSChristoph Hellwig * blk_rq_map_kern - map kernel data to a request, for passthrough requests 62486db1e29SJens Axboe * @q: request queue where request should be inserted 62586db1e29SJens Axboe * @rq: request to fill 62686db1e29SJens Axboe * @kbuf: the kernel buffer 62786db1e29SJens Axboe * @len: length of user data 62886db1e29SJens Axboe * @gfp_mask: memory allocation flags 62968154e90SFUJITA Tomonori * 63068154e90SFUJITA Tomonori * Description: 63168154e90SFUJITA Tomonori * Data will be mapped directly if possible. Otherwise a bounce 632e227867fSMasanari Iida * buffer is used. Can be called multiple times to append multiple 6333a5a3927SJames Bottomley * buffers. 63486db1e29SJens Axboe */ 63586db1e29SJens Axboe int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf, 63686db1e29SJens Axboe unsigned int len, gfp_t gfp_mask) 63786db1e29SJens Axboe { 63868154e90SFUJITA Tomonori int reading = rq_data_dir(rq) == READ; 63914417799SNamhyung Kim unsigned long addr = (unsigned long) kbuf; 640393bb12eSChristoph Hellwig struct bio *bio; 6413a5a3927SJames Bottomley int ret; 64286db1e29SJens Axboe 643ae03bf63SMartin K. Petersen if (len > (queue_max_hw_sectors(q) << 9)) 64486db1e29SJens Axboe return -EINVAL; 64586db1e29SJens Axboe if (!len || !kbuf) 64686db1e29SJens Axboe return -EINVAL; 64786db1e29SJens Axboe 648393bb12eSChristoph Hellwig if (!blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf) || 649393bb12eSChristoph Hellwig blk_queue_may_bounce(q)) 65068154e90SFUJITA Tomonori bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading); 65168154e90SFUJITA Tomonori else 65286db1e29SJens Axboe bio = bio_map_kern(q, kbuf, len, gfp_mask); 65368154e90SFUJITA Tomonori 65486db1e29SJens Axboe if (IS_ERR(bio)) 65586db1e29SJens Axboe return PTR_ERR(bio); 65686db1e29SJens Axboe 657aebf526bSChristoph Hellwig bio->bi_opf &= ~REQ_OP_MASK; 658aebf526bSChristoph Hellwig bio->bi_opf |= req_op(rq); 65986db1e29SJens Axboe 660393bb12eSChristoph Hellwig ret = blk_rq_append_bio(rq, bio); 661*066ff571SChristoph Hellwig if (unlikely(ret)) { 662*066ff571SChristoph Hellwig bio_uninit(bio); 663*066ff571SChristoph Hellwig kfree(bio); 664*066ff571SChristoph Hellwig } 6653a5a3927SJames Bottomley return ret; 6663a5a3927SJames Bottomley } 66786db1e29SJens Axboe EXPORT_SYMBOL(blk_rq_map_kern); 668