bio-integrity.c 16 KB

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