1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2003 Sistina Software 4 * Copyright (C) 2006 Red Hat GmbH 5 * 6 * This file is released under the GPL. 7 */ 8 9 #include "dm-core.h" 10 11 #include <linux/device-mapper.h> 12 13 #include <linux/bio.h> 14 #include <linux/completion.h> 15 #include <linux/mempool.h> 16 #include <linux/module.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 #include <linux/dm-io.h> 20 21 #define DM_MSG_PREFIX "io" 22 23 #define DM_IO_MAX_REGIONS BITS_PER_LONG 24 25 struct dm_io_client { 26 mempool_t pool; 27 struct bio_set bios; 28 }; 29 30 /* 31 * Aligning 'struct io' reduces the number of bits required to store 32 * its address. Refer to store_io_and_region_in_bio() below. 33 */ 34 struct io { 35 unsigned long error_bits; 36 unsigned long unsup_bits; 37 atomic_t count; 38 struct dm_io_client *client; 39 io_notify_fn callback; 40 void *context; 41 void *vma_invalidate_address; 42 unsigned long vma_invalidate_size; 43 } __aligned(DM_IO_MAX_REGIONS); 44 45 static struct kmem_cache *_dm_io_cache; 46 47 /* 48 * Create a client with mempool and bioset. 49 */ 50 struct dm_io_client *dm_io_client_create(void) 51 { 52 struct dm_io_client *client; 53 unsigned int min_ios = dm_get_reserved_bio_based_ios(); 54 int ret; 55 56 client = kzalloc_obj(*client); 57 if (!client) 58 return ERR_PTR(-ENOMEM); 59 60 ret = mempool_init_slab_pool(&client->pool, min_ios, _dm_io_cache); 61 if (ret) 62 goto bad; 63 64 ret = bioset_init(&client->bios, min_ios, 0, BIOSET_NEED_BVECS); 65 if (ret) 66 goto bad; 67 68 return client; 69 70 bad: 71 mempool_exit(&client->pool); 72 kfree(client); 73 return ERR_PTR(ret); 74 } 75 EXPORT_SYMBOL(dm_io_client_create); 76 77 void dm_io_client_destroy(struct dm_io_client *client) 78 { 79 mempool_exit(&client->pool); 80 bioset_exit(&client->bios); 81 kfree(client); 82 } 83 EXPORT_SYMBOL(dm_io_client_destroy); 84 85 /* 86 *------------------------------------------------------------------- 87 * We need to keep track of which region a bio is doing io for. 88 * To avoid a memory allocation to store just 5 or 6 bits, we 89 * ensure the 'struct io' pointer is aligned so enough low bits are 90 * always zero and then combine it with the region number directly in 91 * bi_private. 92 *------------------------------------------------------------------- 93 */ 94 static void store_io_and_region_in_bio(struct bio *bio, struct io *io, 95 unsigned int region) 96 { 97 if (unlikely(!IS_ALIGNED((unsigned long)io, DM_IO_MAX_REGIONS))) { 98 DMCRIT("Unaligned struct io pointer %p", io); 99 BUG(); 100 } 101 102 bio->bi_private = (void *)((unsigned long)io | region); 103 } 104 105 static void retrieve_io_and_region_from_bio(struct bio *bio, struct io **io, 106 unsigned int *region) 107 { 108 unsigned long val = (unsigned long)bio->bi_private; 109 110 *io = (void *)(val & -(unsigned long)DM_IO_MAX_REGIONS); 111 *region = val & (DM_IO_MAX_REGIONS - 1); 112 } 113 114 /* 115 *-------------------------------------------------------------- 116 * We need an io object to keep track of the number of bios that 117 * have been dispatched for a particular io. 118 *-------------------------------------------------------------- 119 */ 120 static void complete_io(struct io *io) 121 { 122 unsigned long error_bits = io->error_bits; 123 unsigned long unsup_bits = io->unsup_bits; 124 io_notify_fn fn = io->callback; 125 void *context = io->context; 126 127 if (io->vma_invalidate_size) 128 invalidate_kernel_vmap_range(io->vma_invalidate_address, 129 io->vma_invalidate_size); 130 131 mempool_free(io, &io->client->pool); 132 fn(error_bits, unsup_bits, context); 133 } 134 135 static void dec_count(struct io *io, unsigned int region, blk_status_t error) 136 { 137 if (unlikely(error)) { 138 if (error == BLK_STS_NOTSUPP || error == BLK_STS_INVAL) 139 set_bit(region, &io->unsup_bits); 140 else 141 set_bit(region, &io->error_bits); 142 } 143 144 if (atomic_dec_and_test(&io->count)) 145 complete_io(io); 146 } 147 148 static void endio(struct bio *bio) 149 { 150 struct io *io; 151 unsigned int region; 152 blk_status_t error; 153 154 if (bio->bi_status && bio_data_dir(bio) == READ) 155 zero_fill_bio(bio); 156 157 /* 158 * The bio destructor in bio_put() may use the io object. 159 */ 160 retrieve_io_and_region_from_bio(bio, &io, ®ion); 161 162 error = bio->bi_status; 163 bio_put(bio); 164 165 dec_count(io, region, error); 166 } 167 168 /* 169 *-------------------------------------------------------------- 170 * These little objects provide an abstraction for getting a new 171 * destination page for io. 172 *-------------------------------------------------------------- 173 */ 174 struct dpages { 175 void (*get_page)(struct dpages *dp, 176 struct page **p, unsigned long *len, unsigned int *offset); 177 void (*next_page)(struct dpages *dp); 178 179 unsigned int context_u; 180 void *context_ptr; 181 182 struct bio *orig_bio; 183 184 void *vma_invalidate_address; 185 unsigned long vma_invalidate_size; 186 }; 187 188 /* 189 * Functions for getting the pages from a list. 190 */ 191 static void list_get_page(struct dpages *dp, 192 struct page **p, unsigned long *len, unsigned int *offset) 193 { 194 unsigned int o = dp->context_u; 195 struct page_list *pl = dp->context_ptr; 196 197 *p = pl->page; 198 *len = PAGE_SIZE - o; 199 *offset = o; 200 } 201 202 static void list_next_page(struct dpages *dp) 203 { 204 struct page_list *pl = dp->context_ptr; 205 206 dp->context_ptr = pl->next; 207 dp->context_u = 0; 208 } 209 210 static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned int offset) 211 { 212 dp->get_page = list_get_page; 213 dp->next_page = list_next_page; 214 dp->context_u = offset; 215 dp->context_ptr = pl; 216 } 217 218 /* 219 * Functions for getting the pages from a VMA. 220 */ 221 static void vm_get_page(struct dpages *dp, 222 struct page **p, unsigned long *len, unsigned int *offset) 223 { 224 *p = vmalloc_to_page(dp->context_ptr); 225 *offset = dp->context_u; 226 *len = PAGE_SIZE - dp->context_u; 227 } 228 229 static void vm_next_page(struct dpages *dp) 230 { 231 dp->context_ptr += PAGE_SIZE - dp->context_u; 232 dp->context_u = 0; 233 } 234 235 static void vm_dp_init(struct dpages *dp, void *data) 236 { 237 dp->get_page = vm_get_page; 238 dp->next_page = vm_next_page; 239 dp->context_u = offset_in_page(data); 240 dp->context_ptr = data; 241 } 242 243 /* 244 * Functions for getting the pages from kernel memory. 245 */ 246 static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len, 247 unsigned int *offset) 248 { 249 *p = virt_to_page(dp->context_ptr); 250 *offset = dp->context_u; 251 *len = PAGE_SIZE - dp->context_u; 252 } 253 254 static void km_next_page(struct dpages *dp) 255 { 256 dp->context_ptr += PAGE_SIZE - dp->context_u; 257 dp->context_u = 0; 258 } 259 260 static void km_dp_init(struct dpages *dp, void *data) 261 { 262 dp->get_page = km_get_page; 263 dp->next_page = km_next_page; 264 dp->context_u = offset_in_page(data); 265 dp->context_ptr = data; 266 } 267 268 /* 269 *--------------------------------------------------------------- 270 * IO routines that accept a list of pages. 271 *--------------------------------------------------------------- 272 */ 273 static void do_region(const blk_opf_t opf, unsigned int region, 274 struct dm_io_region *where, struct dpages *dp, 275 struct io *io, unsigned short ioprio) 276 { 277 struct bio *bio; 278 struct page *page; 279 unsigned long len; 280 unsigned int offset; 281 unsigned int num_bvecs; 282 sector_t remaining = where->count; 283 struct request_queue *q = bdev_get_queue(where->bdev); 284 sector_t num_sectors; 285 unsigned int special_cmd_max_sectors; 286 const enum req_op op = opf & REQ_OP_MASK; 287 288 /* 289 * Reject unsupported discard and write same requests. 290 */ 291 if (op == REQ_OP_DISCARD) 292 special_cmd_max_sectors = bdev_max_discard_sectors(where->bdev); 293 else if (op == REQ_OP_WRITE_ZEROES) 294 special_cmd_max_sectors = q->limits.max_write_zeroes_sectors; 295 if ((op == REQ_OP_DISCARD || op == REQ_OP_WRITE_ZEROES) && 296 special_cmd_max_sectors == 0) { 297 atomic_inc(&io->count); 298 dec_count(io, region, BLK_STS_NOTSUPP); 299 return; 300 } 301 302 if (dp->orig_bio) { 303 bio = bio_alloc_clone(where->bdev, dp->orig_bio, GFP_NOIO, 304 &io->client->bios); 305 bio->bi_iter.bi_sector = where->sector; 306 bio->bi_iter.bi_size = where->count << SECTOR_SHIFT; 307 bio->bi_opf = opf; 308 bio->bi_end_io = endio; 309 bio->bi_ioprio = ioprio; 310 store_io_and_region_in_bio(bio, io, region); 311 312 atomic_inc(&io->count); 313 submit_bio(bio); 314 return; 315 } 316 317 /* 318 * where->count may be zero if op holds a flush and we need to 319 * send a zero-sized flush. 320 */ 321 do { 322 /* 323 * Allocate a suitably sized-bio. 324 */ 325 switch (op) { 326 case REQ_OP_DISCARD: 327 case REQ_OP_WRITE_ZEROES: 328 num_bvecs = 0; 329 break; 330 default: 331 num_bvecs = bio_max_segs(dm_sector_div_up(remaining, 332 (PAGE_SIZE >> SECTOR_SHIFT)) + 1); 333 } 334 335 bio = bio_alloc_bioset(where->bdev, num_bvecs, opf, GFP_NOIO, 336 &io->client->bios); 337 bio->bi_iter.bi_sector = where->sector + (where->count - remaining); 338 bio->bi_end_io = endio; 339 bio->bi_ioprio = ioprio; 340 store_io_and_region_in_bio(bio, io, region); 341 342 if (op == REQ_OP_DISCARD || op == REQ_OP_WRITE_ZEROES) { 343 num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining); 344 bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT; 345 remaining -= num_sectors; 346 } else { 347 while (remaining) { 348 /* 349 * Try and add as many pages as possible. 350 */ 351 dp->get_page(dp, &page, &len, &offset); 352 len = min(len, to_bytes(remaining)); 353 if (!bio_add_page(bio, page, len, offset)) 354 break; 355 356 offset = 0; 357 remaining -= to_sector(len); 358 dp->next_page(dp); 359 } 360 } 361 362 atomic_inc(&io->count); 363 submit_bio(bio); 364 WARN_ON_ONCE(opf & REQ_ATOMIC && remaining); 365 } while (remaining); 366 } 367 368 static void dispatch_io(blk_opf_t opf, unsigned int num_regions, 369 struct dm_io_region *where, struct dpages *dp, 370 struct io *io, unsigned short ioprio) 371 { 372 int i; 373 struct dpages old_pages = *dp; 374 375 BUG_ON(num_regions > DM_IO_MAX_REGIONS); 376 377 /* 378 * For multiple regions we need to be careful to rewind 379 * the dp object for each call to do_region. 380 */ 381 for (i = 0; i < num_regions; i++) { 382 *dp = old_pages; 383 if (where[i].count || (opf & REQ_PREFLUSH)) 384 do_region(opf, i, where + i, dp, io, ioprio); 385 } 386 387 /* 388 * Drop the extra reference that we were holding to avoid 389 * the io being completed too early. 390 */ 391 dec_count(io, 0, 0); 392 } 393 394 static void async_io(struct dm_io_client *client, unsigned int num_regions, 395 struct dm_io_region *where, blk_opf_t opf, 396 struct dpages *dp, io_notify_fn fn, void *context, 397 unsigned short ioprio) 398 { 399 struct io *io; 400 401 io = mempool_alloc(&client->pool, GFP_NOIO); 402 io->error_bits = 0; 403 io->unsup_bits = 0; 404 atomic_set(&io->count, 1); /* see dispatch_io() */ 405 io->client = client; 406 io->callback = fn; 407 io->context = context; 408 409 io->vma_invalidate_address = dp->vma_invalidate_address; 410 io->vma_invalidate_size = dp->vma_invalidate_size; 411 412 dispatch_io(opf, num_regions, where, dp, io, ioprio); 413 } 414 415 struct sync_io { 416 unsigned long error_bits; 417 unsigned long unsup_bits; 418 struct completion wait; 419 }; 420 421 static void sync_io_complete(unsigned long error, unsigned long unsup, void *context) 422 { 423 struct sync_io *sio = context; 424 425 sio->error_bits = error; 426 sio->unsup_bits = unsup; 427 complete(&sio->wait); 428 } 429 430 static int sync_io(struct dm_io_client *client, unsigned int num_regions, 431 struct dm_io_region *where, blk_opf_t opf, struct dpages *dp, 432 unsigned long *error_bits, unsigned long *unsup_bits, 433 unsigned short ioprio) 434 { 435 struct sync_io sio; 436 437 init_completion(&sio.wait); 438 439 async_io(client, num_regions, where, opf | REQ_SYNC, dp, 440 sync_io_complete, &sio, ioprio); 441 442 wait_for_completion_io(&sio.wait); 443 444 if (error_bits) 445 *error_bits = sio.error_bits; 446 if (unsup_bits) 447 *unsup_bits = sio.unsup_bits; 448 449 return sio.error_bits ? -EIO : sio.unsup_bits ? -EOPNOTSUPP : 0; 450 } 451 452 static int dp_init(struct dm_io_request *io_req, struct dpages *dp, 453 unsigned long size) 454 { 455 /* Set up dpages based on memory type */ 456 457 dp->vma_invalidate_address = NULL; 458 dp->vma_invalidate_size = 0; 459 dp->orig_bio = NULL; 460 461 switch (io_req->mem.type) { 462 case DM_IO_PAGE_LIST: 463 list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset); 464 break; 465 466 case DM_IO_BIO: 467 /* 468 * The destination bios clone this bio's biovec directly, so 469 * there are no per-page accessors to set up here. 470 */ 471 dp->orig_bio = io_req->mem.ptr.bio; 472 break; 473 474 case DM_IO_VMA: 475 flush_kernel_vmap_range(io_req->mem.ptr.vma, size); 476 if ((io_req->bi_opf & REQ_OP_MASK) == REQ_OP_READ) { 477 dp->vma_invalidate_address = io_req->mem.ptr.vma; 478 dp->vma_invalidate_size = size; 479 } 480 vm_dp_init(dp, io_req->mem.ptr.vma); 481 break; 482 483 case DM_IO_KMEM: 484 km_dp_init(dp, io_req->mem.ptr.addr); 485 break; 486 487 default: 488 return -EINVAL; 489 } 490 491 return 0; 492 } 493 494 int dm_io(struct dm_io_request *io_req, unsigned int num_regions, 495 struct dm_io_region *where, unsigned long *sync_error_bits, 496 unsigned long *sync_unsup_bits, unsigned short ioprio) 497 { 498 int r; 499 struct dpages dp; 500 501 if (num_regions > 1 && !op_is_write(io_req->bi_opf)) { 502 WARN_ON(1); 503 return -EIO; 504 } 505 506 r = dp_init(io_req, &dp, (unsigned long)where->count << SECTOR_SHIFT); 507 if (r) 508 return r; 509 510 if (!io_req->notify.fn) 511 return sync_io(io_req->client, num_regions, where, 512 io_req->bi_opf, &dp, sync_error_bits, 513 sync_unsup_bits, ioprio); 514 515 async_io(io_req->client, num_regions, where, io_req->bi_opf, &dp, 516 io_req->notify.fn, io_req->notify.context, ioprio); 517 return 0; 518 } 519 EXPORT_SYMBOL(dm_io); 520 521 int __init dm_io_init(void) 522 { 523 _dm_io_cache = KMEM_CACHE(io, 0); 524 if (!_dm_io_cache) 525 return -ENOMEM; 526 527 return 0; 528 } 529 530 void dm_io_exit(void) 531 { 532 kmem_cache_destroy(_dm_io_cache); 533 _dm_io_cache = NULL; 534 } 535