dm-io.c 13 KB

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