bio-integrity.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <[email protected]>
  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. static void __bio_integrity_free(struct bio_set *bs,
  22. struct bio_integrity_payload *bip)
  23. {
  24. if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
  25. if (bip->bip_vec)
  26. bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
  27. bip->bip_max_vcnt);
  28. mempool_free(bip, &bs->bio_integrity_pool);
  29. } else {
  30. kfree(bip);
  31. }
  32. }
  33. /**
  34. * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  35. * @bio: bio to attach integrity metadata to
  36. * @gfp_mask: Memory allocation mask
  37. * @nr_vecs: Number of integrity metadata scatter-gather elements
  38. *
  39. * Description: This function prepares a bio for attaching integrity
  40. * metadata. nr_vecs specifies the maximum number of pages containing
  41. * integrity metadata that can be attached.
  42. */
  43. struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
  44. gfp_t gfp_mask,
  45. unsigned int nr_vecs)
  46. {
  47. struct bio_integrity_payload *bip;
  48. struct bio_set *bs = bio->bi_pool;
  49. unsigned inline_vecs;
  50. if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
  51. return ERR_PTR(-EOPNOTSUPP);
  52. if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
  53. bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
  54. inline_vecs = nr_vecs;
  55. } else {
  56. bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
  57. inline_vecs = BIO_INLINE_VECS;
  58. }
  59. if (unlikely(!bip))
  60. return ERR_PTR(-ENOMEM);
  61. memset(bip, 0, sizeof(*bip));
  62. if (nr_vecs > inline_vecs) {
  63. bip->bip_max_vcnt = nr_vecs;
  64. bip->bip_vec = bvec_alloc(&bs->bvec_integrity_pool,
  65. &bip->bip_max_vcnt, gfp_mask);
  66. if (!bip->bip_vec)
  67. goto err;
  68. } else {
  69. bip->bip_vec = bip->bip_inline_vecs;
  70. bip->bip_max_vcnt = inline_vecs;
  71. }
  72. bip->bip_bio = bio;
  73. bio->bi_integrity = bip;
  74. bio->bi_opf |= REQ_INTEGRITY;
  75. return bip;
  76. err:
  77. __bio_integrity_free(bs, bip);
  78. return ERR_PTR(-ENOMEM);
  79. }
  80. EXPORT_SYMBOL(bio_integrity_alloc);
  81. /**
  82. * bio_integrity_free - Free bio integrity payload
  83. * @bio: bio containing bip to be freed
  84. *
  85. * Description: Used to free the integrity portion of a bio. Usually
  86. * called from bio_free().
  87. */
  88. void bio_integrity_free(struct bio *bio)
  89. {
  90. struct bio_integrity_payload *bip = bio_integrity(bio);
  91. struct bio_set *bs = bio->bi_pool;
  92. if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
  93. kfree(bvec_virt(bip->bip_vec));
  94. __bio_integrity_free(bs, bip);
  95. bio->bi_integrity = NULL;
  96. bio->bi_opf &= ~REQ_INTEGRITY;
  97. }
  98. /**
  99. * bio_integrity_add_page - Attach integrity metadata
  100. * @bio: bio to update
  101. * @page: page containing integrity metadata
  102. * @len: number of bytes of integrity metadata in page
  103. * @offset: start offset within page
  104. *
  105. * Description: Attach a page containing integrity metadata to bio.
  106. */
  107. int bio_integrity_add_page(struct bio *bio, struct page *page,
  108. unsigned int len, unsigned int offset)
  109. {
  110. struct bio_integrity_payload *bip = bio_integrity(bio);
  111. if (bip->bip_vcnt >= bip->bip_max_vcnt) {
  112. printk(KERN_ERR "%s: bip_vec full\n", __func__);
  113. return 0;
  114. }
  115. if (bip->bip_vcnt &&
  116. bvec_gap_to_prev(&bdev_get_queue(bio->bi_bdev)->limits,
  117. &bip->bip_vec[bip->bip_vcnt - 1], offset))
  118. return 0;
  119. bvec_set_page(&bip->bip_vec[bip->bip_vcnt], page, len, offset);
  120. bip->bip_vcnt++;
  121. return len;
  122. }
  123. EXPORT_SYMBOL(bio_integrity_add_page);
  124. /**
  125. * bio_integrity_process - Process integrity metadata for a bio
  126. * @bio: bio to generate/verify integrity metadata for
  127. * @proc_iter: iterator to process
  128. * @proc_fn: Pointer to the relevant processing function
  129. */
  130. static blk_status_t bio_integrity_process(struct bio *bio,
  131. struct bvec_iter *proc_iter, integrity_processing_fn *proc_fn)
  132. {
  133. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  134. struct blk_integrity_iter iter;
  135. struct bvec_iter bviter;
  136. struct bio_vec bv;
  137. struct bio_integrity_payload *bip = bio_integrity(bio);
  138. blk_status_t ret = BLK_STS_OK;
  139. iter.disk_name = bio->bi_bdev->bd_disk->disk_name;
  140. iter.interval = 1 << bi->interval_exp;
  141. iter.tuple_size = bi->tuple_size;
  142. iter.seed = proc_iter->bi_sector;
  143. iter.prot_buf = bvec_virt(bip->bip_vec);
  144. __bio_for_each_segment(bv, bio, bviter, *proc_iter) {
  145. void *kaddr = bvec_kmap_local(&bv);
  146. iter.data_buf = kaddr;
  147. iter.data_size = bv.bv_len;
  148. ret = proc_fn(&iter);
  149. kunmap_local(kaddr);
  150. if (ret)
  151. break;
  152. }
  153. return ret;
  154. }
  155. /**
  156. * bio_integrity_prep - Prepare bio for integrity I/O
  157. * @bio: bio to prepare
  158. *
  159. * Description: Checks if the bio already has an integrity payload attached.
  160. * If it does, the payload has been generated by another kernel subsystem,
  161. * and we just pass it through. Otherwise allocates integrity payload.
  162. * The bio must have data direction, target device and start sector set priot
  163. * to calling. In the WRITE case, integrity metadata will be generated using
  164. * the block device's integrity function. In the READ case, the buffer
  165. * will be prepared for DMA and a suitable end_io handler set up.
  166. */
  167. bool bio_integrity_prep(struct bio *bio)
  168. {
  169. struct bio_integrity_payload *bip;
  170. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  171. void *buf;
  172. unsigned long start, end;
  173. unsigned int len, nr_pages;
  174. unsigned int bytes, offset, i;
  175. unsigned int intervals;
  176. blk_status_t status;
  177. if (!bi)
  178. return true;
  179. if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
  180. return true;
  181. if (!bio_sectors(bio))
  182. return true;
  183. /* Already protected? */
  184. if (bio_integrity(bio))
  185. return true;
  186. if (bio_data_dir(bio) == READ) {
  187. if (!bi->profile->verify_fn ||
  188. !(bi->flags & BLK_INTEGRITY_VERIFY))
  189. return true;
  190. } else {
  191. if (!bi->profile->generate_fn ||
  192. !(bi->flags & BLK_INTEGRITY_GENERATE))
  193. return true;
  194. }
  195. intervals = bio_integrity_intervals(bi, bio_sectors(bio));
  196. /* Allocate kernel buffer for protection data */
  197. len = intervals * bi->tuple_size;
  198. buf = kmalloc(len, GFP_NOIO);
  199. status = BLK_STS_RESOURCE;
  200. if (unlikely(buf == NULL)) {
  201. printk(KERN_ERR "could not allocate integrity buffer\n");
  202. goto err_end_io;
  203. }
  204. end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  205. start = ((unsigned long) buf) >> PAGE_SHIFT;
  206. nr_pages = end - start;
  207. /* Allocate bio integrity payload and integrity vectors */
  208. bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
  209. if (IS_ERR(bip)) {
  210. printk(KERN_ERR "could not allocate data integrity bioset\n");
  211. kfree(buf);
  212. status = BLK_STS_RESOURCE;
  213. goto err_end_io;
  214. }
  215. bip->bip_flags |= BIP_BLOCK_INTEGRITY;
  216. bip->bip_iter.bi_size = len;
  217. bip_set_seed(bip, bio->bi_iter.bi_sector);
  218. if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
  219. bip->bip_flags |= BIP_IP_CHECKSUM;
  220. /* Map it */
  221. offset = offset_in_page(buf);
  222. for (i = 0 ; i < nr_pages ; i++) {
  223. int ret;
  224. bytes = PAGE_SIZE - offset;
  225. if (len <= 0)
  226. break;
  227. if (bytes > len)
  228. bytes = len;
  229. ret = bio_integrity_add_page(bio, virt_to_page(buf),
  230. bytes, offset);
  231. if (ret == 0) {
  232. printk(KERN_ERR "could not attach integrity payload\n");
  233. status = BLK_STS_RESOURCE;
  234. goto err_end_io;
  235. }
  236. if (ret < bytes)
  237. break;
  238. buf += bytes;
  239. len -= bytes;
  240. offset = 0;
  241. }
  242. /* Auto-generate integrity metadata if this is a write */
  243. if (bio_data_dir(bio) == WRITE) {
  244. bio_integrity_process(bio, &bio->bi_iter,
  245. bi->profile->generate_fn);
  246. } else {
  247. bip->bio_iter = bio->bi_iter;
  248. }
  249. return true;
  250. err_end_io:
  251. bio->bi_status = status;
  252. bio_endio(bio);
  253. return false;
  254. }
  255. EXPORT_SYMBOL(bio_integrity_prep);
  256. /**
  257. * bio_integrity_verify_fn - Integrity I/O completion worker
  258. * @work: Work struct stored in bio to be verified
  259. *
  260. * Description: This workqueue function is called to complete a READ
  261. * request. The function verifies the transferred integrity metadata
  262. * and then calls the original bio end_io function.
  263. */
  264. static void bio_integrity_verify_fn(struct work_struct *work)
  265. {
  266. struct bio_integrity_payload *bip =
  267. container_of(work, struct bio_integrity_payload, bip_work);
  268. struct bio *bio = bip->bip_bio;
  269. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  270. /*
  271. * At the moment verify is called bio's iterator was advanced
  272. * during split and completion, we need to rewind iterator to
  273. * it's original position.
  274. */
  275. bio->bi_status = bio_integrity_process(bio, &bip->bio_iter,
  276. bi->profile->verify_fn);
  277. bio_integrity_free(bio);
  278. bio_endio(bio);
  279. }
  280. /**
  281. * __bio_integrity_endio - Integrity I/O completion function
  282. * @bio: Protected bio
  283. *
  284. * Description: Completion for integrity I/O
  285. *
  286. * Normally I/O completion is done in interrupt context. However,
  287. * verifying I/O integrity is a time-consuming task which must be run
  288. * in process context. This function postpones completion
  289. * accordingly.
  290. */
  291. bool __bio_integrity_endio(struct bio *bio)
  292. {
  293. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  294. struct bio_integrity_payload *bip = bio_integrity(bio);
  295. if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
  296. (bip->bip_flags & BIP_BLOCK_INTEGRITY) && bi->profile->verify_fn) {
  297. INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
  298. queue_work(kintegrityd_wq, &bip->bip_work);
  299. return false;
  300. }
  301. bio_integrity_free(bio);
  302. return true;
  303. }
  304. /**
  305. * bio_integrity_advance - Advance integrity vector
  306. * @bio: bio whose integrity vector to update
  307. * @bytes_done: number of data bytes that have been completed
  308. *
  309. * Description: This function calculates how many integrity bytes the
  310. * number of completed data bytes correspond to and advances the
  311. * integrity vector accordingly.
  312. */
  313. void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
  314. {
  315. struct bio_integrity_payload *bip = bio_integrity(bio);
  316. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  317. unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
  318. bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
  319. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
  320. }
  321. /**
  322. * bio_integrity_trim - Trim integrity vector
  323. * @bio: bio whose integrity vector to update
  324. *
  325. * Description: Used to trim the integrity vector in a cloned bio.
  326. */
  327. void bio_integrity_trim(struct bio *bio)
  328. {
  329. struct bio_integrity_payload *bip = bio_integrity(bio);
  330. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  331. bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
  332. }
  333. EXPORT_SYMBOL(bio_integrity_trim);
  334. /**
  335. * bio_integrity_clone - Callback for cloning bios with integrity metadata
  336. * @bio: New bio
  337. * @bio_src: Original bio
  338. * @gfp_mask: Memory allocation mask
  339. *
  340. * Description: Called to allocate a bip when cloning a bio
  341. */
  342. int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  343. gfp_t gfp_mask)
  344. {
  345. struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
  346. struct bio_integrity_payload *bip;
  347. BUG_ON(bip_src == NULL);
  348. bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
  349. if (IS_ERR(bip))
  350. return PTR_ERR(bip);
  351. memcpy(bip->bip_vec, bip_src->bip_vec,
  352. bip_src->bip_vcnt * sizeof(struct bio_vec));
  353. bip->bip_vcnt = bip_src->bip_vcnt;
  354. bip->bip_iter = bip_src->bip_iter;
  355. bip->bip_flags = bip_src->bip_flags & ~BIP_BLOCK_INTEGRITY;
  356. return 0;
  357. }
  358. int bioset_integrity_create(struct bio_set *bs, int pool_size)
  359. {
  360. if (mempool_initialized(&bs->bio_integrity_pool))
  361. return 0;
  362. if (mempool_init_slab_pool(&bs->bio_integrity_pool,
  363. pool_size, bip_slab))
  364. return -1;
  365. if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
  366. mempool_exit(&bs->bio_integrity_pool);
  367. return -1;
  368. }
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(bioset_integrity_create);
  372. void bioset_integrity_free(struct bio_set *bs)
  373. {
  374. mempool_exit(&bs->bio_integrity_pool);
  375. mempool_exit(&bs->bvec_integrity_pool);
  376. }
  377. void __init bio_integrity_init(void)
  378. {
  379. /*
  380. * kintegrityd won't block much but may burn a lot of CPU cycles.
  381. * Make it highpri CPU intensive wq with max concurrency of 1.
  382. */
  383. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  384. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  385. if (!kintegrityd_wq)
  386. panic("Failed to create kintegrityd\n");
  387. bip_slab = kmem_cache_create("bio_integrity_payload",
  388. sizeof(struct bio_integrity_payload) +
  389. sizeof(struct bio_vec) * BIO_INLINE_VECS,
  390. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  391. }