bio-integrity.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * bio-integrity.c - bio data integrity extensions
  4. *
  5. * Copyright (C) 2007, 2008, 2009 Oracle Corporation
  6. * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  7. */
  8. #include <linux/blk-integrity.h>
  9. #include <linux/mempool.h>
  10. #include <linux/export.h>
  11. #include <linux/bio.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/slab.h>
  14. #include "blk.h"
  15. static struct kmem_cache *bip_slab;
  16. static struct workqueue_struct *kintegrityd_wq;
  17. void blk_flush_integrity(void)
  18. {
  19. flush_workqueue(kintegrityd_wq);
  20. }
  21. /**
  22. * bio_integrity_free - Free bio integrity payload
  23. * @bio: bio containing bip to be freed
  24. *
  25. * Description: Free the integrity portion of a bio.
  26. */
  27. void bio_integrity_free(struct bio *bio)
  28. {
  29. struct bio_integrity_payload *bip = bio_integrity(bio);
  30. struct bio_set *bs = bio->bi_pool;
  31. if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
  32. if (bip->bip_vec)
  33. bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
  34. bip->bip_max_vcnt);
  35. mempool_free(bip, &bs->bio_integrity_pool);
  36. } else {
  37. kfree(bip);
  38. }
  39. bio->bi_integrity = NULL;
  40. bio->bi_opf &= ~REQ_INTEGRITY;
  41. }
  42. /**
  43. * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  44. * @bio: bio to attach integrity metadata to
  45. * @gfp_mask: Memory allocation mask
  46. * @nr_vecs: Number of integrity metadata scatter-gather elements
  47. *
  48. * Description: This function prepares a bio for attaching integrity
  49. * metadata. nr_vecs specifies the maximum number of pages containing
  50. * integrity metadata that can be attached.
  51. */
  52. struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
  53. gfp_t gfp_mask,
  54. unsigned int nr_vecs)
  55. {
  56. struct bio_integrity_payload *bip;
  57. struct bio_set *bs = bio->bi_pool;
  58. unsigned inline_vecs;
  59. if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
  60. return ERR_PTR(-EOPNOTSUPP);
  61. if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
  62. bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
  63. inline_vecs = nr_vecs;
  64. } else {
  65. bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
  66. inline_vecs = BIO_INLINE_VECS;
  67. }
  68. if (unlikely(!bip))
  69. return ERR_PTR(-ENOMEM);
  70. memset(bip, 0, sizeof(*bip));
  71. /* always report as many vecs as asked explicitly, not inline vecs */
  72. bip->bip_max_vcnt = nr_vecs;
  73. if (nr_vecs > inline_vecs) {
  74. bip->bip_vec = bvec_alloc(&bs->bvec_integrity_pool,
  75. &bip->bip_max_vcnt, gfp_mask);
  76. if (!bip->bip_vec)
  77. goto err;
  78. } else if (nr_vecs) {
  79. bip->bip_vec = bip->bip_inline_vecs;
  80. }
  81. bip->bip_bio = bio;
  82. bio->bi_integrity = bip;
  83. bio->bi_opf |= REQ_INTEGRITY;
  84. return bip;
  85. err:
  86. if (bs && mempool_initialized(&bs->bio_integrity_pool))
  87. mempool_free(bip, &bs->bio_integrity_pool);
  88. else
  89. kfree(bip);
  90. return ERR_PTR(-ENOMEM);
  91. }
  92. EXPORT_SYMBOL(bio_integrity_alloc);
  93. static void bio_integrity_unpin_bvec(struct bio_vec *bv, int nr_vecs,
  94. bool dirty)
  95. {
  96. int i;
  97. for (i = 0; i < nr_vecs; i++) {
  98. if (dirty && !PageCompound(bv[i].bv_page))
  99. set_page_dirty_lock(bv[i].bv_page);
  100. unpin_user_page(bv[i].bv_page);
  101. }
  102. }
  103. static void bio_integrity_uncopy_user(struct bio_integrity_payload *bip)
  104. {
  105. unsigned short orig_nr_vecs = bip->bip_max_vcnt - 1;
  106. struct bio_vec *orig_bvecs = &bip->bip_vec[1];
  107. struct bio_vec *bounce_bvec = &bip->bip_vec[0];
  108. size_t bytes = bounce_bvec->bv_len;
  109. struct iov_iter orig_iter;
  110. int ret;
  111. iov_iter_bvec(&orig_iter, ITER_DEST, orig_bvecs, orig_nr_vecs, bytes);
  112. ret = copy_to_iter(bvec_virt(bounce_bvec), bytes, &orig_iter);
  113. WARN_ON_ONCE(ret != bytes);
  114. bio_integrity_unpin_bvec(orig_bvecs, orig_nr_vecs, true);
  115. }
  116. /**
  117. * bio_integrity_unmap_user - Unmap user integrity payload
  118. * @bio: bio containing bip to be unmapped
  119. *
  120. * Unmap the user mapped integrity portion of a bio.
  121. */
  122. void bio_integrity_unmap_user(struct bio *bio)
  123. {
  124. struct bio_integrity_payload *bip = bio_integrity(bio);
  125. if (bip->bip_flags & BIP_COPY_USER) {
  126. if (bio_data_dir(bio) == READ)
  127. bio_integrity_uncopy_user(bip);
  128. kfree(bvec_virt(bip->bip_vec));
  129. return;
  130. }
  131. bio_integrity_unpin_bvec(bip->bip_vec, bip->bip_max_vcnt,
  132. bio_data_dir(bio) == READ);
  133. }
  134. /**
  135. * bio_integrity_add_page - Attach integrity metadata
  136. * @bio: bio to update
  137. * @page: page containing integrity metadata
  138. * @len: number of bytes of integrity metadata in page
  139. * @offset: start offset within page
  140. *
  141. * Description: Attach a page containing integrity metadata to bio.
  142. */
  143. int bio_integrity_add_page(struct bio *bio, struct page *page,
  144. unsigned int len, unsigned int offset)
  145. {
  146. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  147. struct bio_integrity_payload *bip = bio_integrity(bio);
  148. if (bip->bip_vcnt > 0) {
  149. struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt - 1];
  150. bool same_page = false;
  151. if (bvec_try_merge_hw_page(q, bv, page, len, offset,
  152. &same_page)) {
  153. bip->bip_iter.bi_size += len;
  154. return len;
  155. }
  156. if (bip->bip_vcnt >=
  157. min(bip->bip_max_vcnt, queue_max_integrity_segments(q)))
  158. return 0;
  159. /*
  160. * If the queue doesn't support SG gaps and adding this segment
  161. * would create a gap, disallow it.
  162. */
  163. if (bvec_gap_to_prev(&q->limits, bv, offset))
  164. return 0;
  165. }
  166. bvec_set_page(&bip->bip_vec[bip->bip_vcnt], page, len, offset);
  167. bip->bip_vcnt++;
  168. bip->bip_iter.bi_size += len;
  169. return len;
  170. }
  171. EXPORT_SYMBOL(bio_integrity_add_page);
  172. static int bio_integrity_copy_user(struct bio *bio, struct bio_vec *bvec,
  173. int nr_vecs, unsigned int len,
  174. unsigned int direction, u32 seed)
  175. {
  176. bool write = direction == ITER_SOURCE;
  177. struct bio_integrity_payload *bip;
  178. struct iov_iter iter;
  179. void *buf;
  180. int ret;
  181. buf = kmalloc(len, GFP_KERNEL);
  182. if (!buf)
  183. return -ENOMEM;
  184. if (write) {
  185. iov_iter_bvec(&iter, direction, bvec, nr_vecs, len);
  186. if (!copy_from_iter_full(buf, len, &iter)) {
  187. ret = -EFAULT;
  188. goto free_buf;
  189. }
  190. bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
  191. } else {
  192. memset(buf, 0, len);
  193. /*
  194. * We need to preserve the original bvec and the number of vecs
  195. * in it for completion handling
  196. */
  197. bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs + 1);
  198. }
  199. if (IS_ERR(bip)) {
  200. ret = PTR_ERR(bip);
  201. goto free_buf;
  202. }
  203. if (write)
  204. bio_integrity_unpin_bvec(bvec, nr_vecs, false);
  205. else
  206. memcpy(&bip->bip_vec[1], bvec, nr_vecs * sizeof(*bvec));
  207. ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
  208. offset_in_page(buf));
  209. if (ret != len) {
  210. ret = -ENOMEM;
  211. goto free_bip;
  212. }
  213. bip->bip_flags |= BIP_COPY_USER;
  214. bip->bip_iter.bi_sector = seed;
  215. bip->bip_vcnt = nr_vecs;
  216. return 0;
  217. free_bip:
  218. bio_integrity_free(bio);
  219. free_buf:
  220. kfree(buf);
  221. return ret;
  222. }
  223. static int bio_integrity_init_user(struct bio *bio, struct bio_vec *bvec,
  224. int nr_vecs, unsigned int len, u32 seed)
  225. {
  226. struct bio_integrity_payload *bip;
  227. bip = bio_integrity_alloc(bio, GFP_KERNEL, nr_vecs);
  228. if (IS_ERR(bip))
  229. return PTR_ERR(bip);
  230. memcpy(bip->bip_vec, bvec, nr_vecs * sizeof(*bvec));
  231. bip->bip_iter.bi_sector = seed;
  232. bip->bip_iter.bi_size = len;
  233. bip->bip_vcnt = nr_vecs;
  234. return 0;
  235. }
  236. static unsigned int bvec_from_pages(struct bio_vec *bvec, struct page **pages,
  237. int nr_vecs, ssize_t bytes, ssize_t offset)
  238. {
  239. unsigned int nr_bvecs = 0;
  240. int i, j;
  241. for (i = 0; i < nr_vecs; i = j) {
  242. size_t size = min_t(size_t, bytes, PAGE_SIZE - offset);
  243. struct folio *folio = page_folio(pages[i]);
  244. bytes -= size;
  245. for (j = i + 1; j < nr_vecs; j++) {
  246. size_t next = min_t(size_t, PAGE_SIZE, bytes);
  247. if (page_folio(pages[j]) != folio ||
  248. pages[j] != pages[j - 1] + 1)
  249. break;
  250. unpin_user_page(pages[j]);
  251. size += next;
  252. bytes -= next;
  253. }
  254. bvec_set_page(&bvec[nr_bvecs], pages[i], size, offset);
  255. offset = 0;
  256. nr_bvecs++;
  257. }
  258. return nr_bvecs;
  259. }
  260. int bio_integrity_map_user(struct bio *bio, void __user *ubuf, ssize_t bytes,
  261. u32 seed)
  262. {
  263. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  264. unsigned int align = blk_lim_dma_alignment_and_pad(&q->limits);
  265. struct page *stack_pages[UIO_FASTIOV], **pages = stack_pages;
  266. struct bio_vec stack_vec[UIO_FASTIOV], *bvec = stack_vec;
  267. unsigned int direction, nr_bvecs;
  268. struct iov_iter iter;
  269. int ret, nr_vecs;
  270. size_t offset;
  271. bool copy;
  272. if (bio_integrity(bio))
  273. return -EINVAL;
  274. if (bytes >> SECTOR_SHIFT > queue_max_hw_sectors(q))
  275. return -E2BIG;
  276. if (bio_data_dir(bio) == READ)
  277. direction = ITER_DEST;
  278. else
  279. direction = ITER_SOURCE;
  280. iov_iter_ubuf(&iter, direction, ubuf, bytes);
  281. nr_vecs = iov_iter_npages(&iter, BIO_MAX_VECS + 1);
  282. if (nr_vecs > BIO_MAX_VECS)
  283. return -E2BIG;
  284. if (nr_vecs > UIO_FASTIOV) {
  285. bvec = kcalloc(nr_vecs, sizeof(*bvec), GFP_KERNEL);
  286. if (!bvec)
  287. return -ENOMEM;
  288. pages = NULL;
  289. }
  290. copy = !iov_iter_is_aligned(&iter, align, align);
  291. ret = iov_iter_extract_pages(&iter, &pages, bytes, nr_vecs, 0, &offset);
  292. if (unlikely(ret < 0))
  293. goto free_bvec;
  294. nr_bvecs = bvec_from_pages(bvec, pages, nr_vecs, bytes, offset);
  295. if (pages != stack_pages)
  296. kvfree(pages);
  297. if (nr_bvecs > queue_max_integrity_segments(q))
  298. copy = true;
  299. if (copy)
  300. ret = bio_integrity_copy_user(bio, bvec, nr_bvecs, bytes,
  301. direction, seed);
  302. else
  303. ret = bio_integrity_init_user(bio, bvec, nr_bvecs, bytes, seed);
  304. if (ret)
  305. goto release_pages;
  306. if (bvec != stack_vec)
  307. kfree(bvec);
  308. return 0;
  309. release_pages:
  310. bio_integrity_unpin_bvec(bvec, nr_bvecs, false);
  311. free_bvec:
  312. if (bvec != stack_vec)
  313. kfree(bvec);
  314. return ret;
  315. }
  316. /**
  317. * bio_integrity_prep - Prepare bio for integrity I/O
  318. * @bio: bio to prepare
  319. *
  320. * Description: Checks if the bio already has an integrity payload attached.
  321. * If it does, the payload has been generated by another kernel subsystem,
  322. * and we just pass it through. Otherwise allocates integrity payload.
  323. * The bio must have data direction, target device and start sector set priot
  324. * to calling. In the WRITE case, integrity metadata will be generated using
  325. * the block device's integrity function. In the READ case, the buffer
  326. * will be prepared for DMA and a suitable end_io handler set up.
  327. */
  328. bool bio_integrity_prep(struct bio *bio)
  329. {
  330. struct bio_integrity_payload *bip;
  331. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  332. unsigned int len;
  333. void *buf;
  334. gfp_t gfp = GFP_NOIO;
  335. if (!bi)
  336. return true;
  337. if (!bio_sectors(bio))
  338. return true;
  339. /* Already protected? */
  340. if (bio_integrity(bio))
  341. return true;
  342. switch (bio_op(bio)) {
  343. case REQ_OP_READ:
  344. if (bi->flags & BLK_INTEGRITY_NOVERIFY)
  345. return true;
  346. break;
  347. case REQ_OP_WRITE:
  348. if (bi->flags & BLK_INTEGRITY_NOGENERATE)
  349. return true;
  350. /*
  351. * Zero the memory allocated to not leak uninitialized kernel
  352. * memory to disk for non-integrity metadata where nothing else
  353. * initializes the memory.
  354. */
  355. if (bi->csum_type == BLK_INTEGRITY_CSUM_NONE)
  356. gfp |= __GFP_ZERO;
  357. break;
  358. default:
  359. return true;
  360. }
  361. /* Allocate kernel buffer for protection data */
  362. len = bio_integrity_bytes(bi, bio_sectors(bio));
  363. buf = kmalloc(len, gfp);
  364. if (unlikely(buf == NULL)) {
  365. goto err_end_io;
  366. }
  367. bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
  368. if (IS_ERR(bip)) {
  369. kfree(buf);
  370. goto err_end_io;
  371. }
  372. bip->bip_flags |= BIP_BLOCK_INTEGRITY;
  373. bip_set_seed(bip, bio->bi_iter.bi_sector);
  374. if (bi->csum_type == BLK_INTEGRITY_CSUM_IP)
  375. bip->bip_flags |= BIP_IP_CHECKSUM;
  376. if (bio_integrity_add_page(bio, virt_to_page(buf), len,
  377. offset_in_page(buf)) < len) {
  378. printk(KERN_ERR "could not attach integrity payload\n");
  379. goto err_end_io;
  380. }
  381. /* Auto-generate integrity metadata if this is a write */
  382. if (bio_data_dir(bio) == WRITE)
  383. blk_integrity_generate(bio);
  384. else
  385. bip->bio_iter = bio->bi_iter;
  386. return true;
  387. err_end_io:
  388. bio->bi_status = BLK_STS_RESOURCE;
  389. bio_endio(bio);
  390. return false;
  391. }
  392. EXPORT_SYMBOL(bio_integrity_prep);
  393. /**
  394. * bio_integrity_verify_fn - Integrity I/O completion worker
  395. * @work: Work struct stored in bio to be verified
  396. *
  397. * Description: This workqueue function is called to complete a READ
  398. * request. The function verifies the transferred integrity metadata
  399. * and then calls the original bio end_io function.
  400. */
  401. static void bio_integrity_verify_fn(struct work_struct *work)
  402. {
  403. struct bio_integrity_payload *bip =
  404. container_of(work, struct bio_integrity_payload, bip_work);
  405. struct bio *bio = bip->bip_bio;
  406. blk_integrity_verify(bio);
  407. kfree(bvec_virt(bip->bip_vec));
  408. bio_integrity_free(bio);
  409. bio_endio(bio);
  410. }
  411. /**
  412. * __bio_integrity_endio - Integrity I/O completion function
  413. * @bio: Protected bio
  414. *
  415. * Description: Completion for integrity I/O
  416. *
  417. * Normally I/O completion is done in interrupt context. However,
  418. * verifying I/O integrity is a time-consuming task which must be run
  419. * in process context. This function postpones completion
  420. * accordingly.
  421. */
  422. bool __bio_integrity_endio(struct bio *bio)
  423. {
  424. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  425. struct bio_integrity_payload *bip = bio_integrity(bio);
  426. if (bio_op(bio) == REQ_OP_READ && !bio->bi_status && bi->csum_type) {
  427. INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
  428. queue_work(kintegrityd_wq, &bip->bip_work);
  429. return false;
  430. }
  431. kfree(bvec_virt(bip->bip_vec));
  432. bio_integrity_free(bio);
  433. return true;
  434. }
  435. /**
  436. * bio_integrity_advance - Advance integrity vector
  437. * @bio: bio whose integrity vector to update
  438. * @bytes_done: number of data bytes that have been completed
  439. *
  440. * Description: This function calculates how many integrity bytes the
  441. * number of completed data bytes correspond to and advances the
  442. * integrity vector accordingly.
  443. */
  444. void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
  445. {
  446. struct bio_integrity_payload *bip = bio_integrity(bio);
  447. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  448. unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
  449. bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
  450. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
  451. }
  452. /**
  453. * bio_integrity_trim - Trim integrity vector
  454. * @bio: bio whose integrity vector to update
  455. *
  456. * Description: Used to trim the integrity vector in a cloned bio.
  457. */
  458. void bio_integrity_trim(struct bio *bio)
  459. {
  460. struct bio_integrity_payload *bip = bio_integrity(bio);
  461. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  462. bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
  463. }
  464. EXPORT_SYMBOL(bio_integrity_trim);
  465. /**
  466. * bio_integrity_clone - Callback for cloning bios with integrity metadata
  467. * @bio: New bio
  468. * @bio_src: Original bio
  469. * @gfp_mask: Memory allocation mask
  470. *
  471. * Description: Called to allocate a bip when cloning a bio
  472. */
  473. int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  474. gfp_t gfp_mask)
  475. {
  476. struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
  477. struct bio_integrity_payload *bip;
  478. BUG_ON(bip_src == NULL);
  479. bip = bio_integrity_alloc(bio, gfp_mask, 0);
  480. if (IS_ERR(bip))
  481. return PTR_ERR(bip);
  482. bip->bip_vec = bip_src->bip_vec;
  483. bip->bip_iter = bip_src->bip_iter;
  484. bip->bip_flags = bip_src->bip_flags & ~BIP_BLOCK_INTEGRITY;
  485. return 0;
  486. }
  487. int bioset_integrity_create(struct bio_set *bs, int pool_size)
  488. {
  489. if (mempool_initialized(&bs->bio_integrity_pool))
  490. return 0;
  491. if (mempool_init_slab_pool(&bs->bio_integrity_pool,
  492. pool_size, bip_slab))
  493. return -1;
  494. if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
  495. mempool_exit(&bs->bio_integrity_pool);
  496. return -1;
  497. }
  498. return 0;
  499. }
  500. EXPORT_SYMBOL(bioset_integrity_create);
  501. void bioset_integrity_free(struct bio_set *bs)
  502. {
  503. mempool_exit(&bs->bio_integrity_pool);
  504. mempool_exit(&bs->bvec_integrity_pool);
  505. }
  506. void __init bio_integrity_init(void)
  507. {
  508. /*
  509. * kintegrityd won't block much but may burn a lot of CPU cycles.
  510. * Make it highpri CPU intensive wq with max concurrency of 1.
  511. */
  512. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  513. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  514. if (!kintegrityd_wq)
  515. panic("Failed to create kintegrityd\n");
  516. bip_slab = kmem_cache_create("bio_integrity_payload",
  517. sizeof(struct bio_integrity_payload) +
  518. sizeof(struct bio_vec) * BIO_INLINE_VECS,
  519. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  520. }