bio-integrity-auto.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007, 2008, 2009 Oracle Corporation
  4. * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  5. *
  6. * Automatically generate and verify integrity data on PI capable devices if the
  7. * bio submitter didn't provide PI itself. This ensures that kernel verifies
  8. * data integrity even if the file system (or other user of the block device) is
  9. * not aware of PI.
  10. */
  11. #include <linux/blk-integrity.h>
  12. #include <linux/t10-pi.h>
  13. #include <linux/workqueue.h>
  14. #include "blk.h"
  15. struct bio_integrity_data {
  16. struct bio *bio;
  17. struct bvec_iter saved_bio_iter;
  18. struct work_struct work;
  19. struct bio_integrity_payload bip;
  20. struct bio_vec bvec;
  21. };
  22. static struct kmem_cache *bid_slab;
  23. static mempool_t bid_pool;
  24. static struct workqueue_struct *kintegrityd_wq;
  25. static void bio_integrity_finish(struct bio_integrity_data *bid)
  26. {
  27. bid->bio->bi_integrity = NULL;
  28. bid->bio->bi_opf &= ~REQ_INTEGRITY;
  29. bio_integrity_free_buf(&bid->bip);
  30. mempool_free(bid, &bid_pool);
  31. }
  32. static void bio_integrity_verify_fn(struct work_struct *work)
  33. {
  34. struct bio_integrity_data *bid =
  35. container_of(work, struct bio_integrity_data, work);
  36. struct bio *bio = bid->bio;
  37. blk_integrity_verify_iter(bio, &bid->saved_bio_iter);
  38. bio_integrity_finish(bid);
  39. bio_endio(bio);
  40. }
  41. #define BIP_CHECK_FLAGS (BIP_CHECK_GUARD | BIP_CHECK_REFTAG | BIP_CHECK_APPTAG)
  42. static bool bip_should_check(struct bio_integrity_payload *bip)
  43. {
  44. return bip->bip_flags & BIP_CHECK_FLAGS;
  45. }
  46. static bool bi_offload_capable(struct blk_integrity *bi)
  47. {
  48. return bi->metadata_size == bi->pi_tuple_size;
  49. }
  50. /**
  51. * __bio_integrity_endio - Integrity I/O completion function
  52. * @bio: Protected bio
  53. *
  54. * Normally I/O completion is done in interrupt context. However, verifying I/O
  55. * integrity is a time-consuming task which must be run in process context.
  56. *
  57. * This function postpones completion accordingly.
  58. */
  59. bool __bio_integrity_endio(struct bio *bio)
  60. {
  61. struct bio_integrity_payload *bip = bio_integrity(bio);
  62. struct bio_integrity_data *bid =
  63. container_of(bip, struct bio_integrity_data, bip);
  64. if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
  65. bip_should_check(bip)) {
  66. INIT_WORK(&bid->work, bio_integrity_verify_fn);
  67. queue_work(kintegrityd_wq, &bid->work);
  68. return false;
  69. }
  70. bio_integrity_finish(bid);
  71. return true;
  72. }
  73. /**
  74. * bio_integrity_prep - Prepare bio for integrity I/O
  75. * @bio: bio to prepare
  76. *
  77. * Checks if the bio already has an integrity payload attached. If it does, the
  78. * payload has been generated by another kernel subsystem, and we just pass it
  79. * through.
  80. * Otherwise allocates integrity payload and for writes the integrity metadata
  81. * will be generated. For reads, the completion handler will verify the
  82. * metadata.
  83. */
  84. bool bio_integrity_prep(struct bio *bio)
  85. {
  86. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  87. struct bio_integrity_data *bid;
  88. bool set_flags = true;
  89. gfp_t gfp = GFP_NOIO;
  90. if (!bi)
  91. return true;
  92. if (!bio_sectors(bio))
  93. return true;
  94. /* Already protected? */
  95. if (bio_integrity(bio))
  96. return true;
  97. switch (bio_op(bio)) {
  98. case REQ_OP_READ:
  99. if (bi->flags & BLK_INTEGRITY_NOVERIFY) {
  100. if (bi_offload_capable(bi))
  101. return true;
  102. set_flags = false;
  103. }
  104. break;
  105. case REQ_OP_WRITE:
  106. /*
  107. * Zero the memory allocated to not leak uninitialized kernel
  108. * memory to disk for non-integrity metadata where nothing else
  109. * initializes the memory.
  110. */
  111. if (bi->flags & BLK_INTEGRITY_NOGENERATE) {
  112. if (bi_offload_capable(bi))
  113. return true;
  114. set_flags = false;
  115. gfp |= __GFP_ZERO;
  116. } else if (bi->metadata_size > bi->pi_tuple_size)
  117. gfp |= __GFP_ZERO;
  118. break;
  119. default:
  120. return true;
  121. }
  122. if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
  123. return true;
  124. bid = mempool_alloc(&bid_pool, GFP_NOIO);
  125. bio_integrity_init(bio, &bid->bip, &bid->bvec, 1);
  126. bid->bio = bio;
  127. bid->bip.bip_flags |= BIP_BLOCK_INTEGRITY;
  128. bio_integrity_alloc_buf(bio, gfp & __GFP_ZERO);
  129. bip_set_seed(&bid->bip, bio->bi_iter.bi_sector);
  130. if (set_flags) {
  131. if (bi->csum_type == BLK_INTEGRITY_CSUM_IP)
  132. bid->bip.bip_flags |= BIP_IP_CHECKSUM;
  133. if (bi->csum_type)
  134. bid->bip.bip_flags |= BIP_CHECK_GUARD;
  135. if (bi->flags & BLK_INTEGRITY_REF_TAG)
  136. bid->bip.bip_flags |= BIP_CHECK_REFTAG;
  137. }
  138. /* Auto-generate integrity metadata if this is a write */
  139. if (bio_data_dir(bio) == WRITE && bip_should_check(&bid->bip))
  140. blk_integrity_generate(bio);
  141. else
  142. bid->saved_bio_iter = bio->bi_iter;
  143. return true;
  144. }
  145. EXPORT_SYMBOL(bio_integrity_prep);
  146. void blk_flush_integrity(void)
  147. {
  148. flush_workqueue(kintegrityd_wq);
  149. }
  150. static int __init blk_integrity_auto_init(void)
  151. {
  152. bid_slab = kmem_cache_create("bio_integrity_data",
  153. sizeof(struct bio_integrity_data), 0,
  154. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  155. if (mempool_init_slab_pool(&bid_pool, BIO_POOL_SIZE, bid_slab))
  156. panic("bio: can't create integrity pool\n");
  157. /*
  158. * kintegrityd won't block much but may burn a lot of CPU cycles.
  159. * Make it highpri CPU intensive wq with max concurrency of 1.
  160. */
  161. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  162. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  163. if (!kintegrityd_wq)
  164. panic("Failed to create kintegrityd\n");
  165. return 0;
  166. }
  167. subsys_initcall(blk_integrity_auto_init);