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 16 struct bio_integrity_data { 17 struct bio *bio; 18 struct bvec_iter saved_bio_iter; 19 struct work_struct work; 20 struct bio_integrity_payload bip; 21 struct bio_vec bvec; 22 }; 23 24 static struct kmem_cache *bid_slab; 25 static mempool_t bid_pool; 26 static struct workqueue_struct *kintegrityd_wq; 27 28 static void bio_integrity_finish(struct bio_integrity_data *bid) 29 { 30 bid->bio->bi_integrity = NULL; 31 bid->bio->bi_opf &= ~REQ_INTEGRITY; 32 bio_integrity_free_buf(&bid->bip); 33 mempool_free(bid, &bid_pool); 34 } 35 36 static void bio_integrity_verify_fn(struct work_struct *work) 37 { 38 struct bio_integrity_data *bid = 39 container_of(work, struct bio_integrity_data, work); 40 struct bio *bio = bid->bio; 41 42 bio->bi_status = bio_integrity_verify(bio, &bid->saved_bio_iter); 43 bio_integrity_finish(bid); 44 bio_endio(bio); 45 } 46 47 #define BIP_CHECK_FLAGS (BIP_CHECK_GUARD | BIP_CHECK_REFTAG | BIP_CHECK_APPTAG) 48 static bool bip_should_check(struct bio_integrity_payload *bip) 49 { 50 return bip->bip_flags & BIP_CHECK_FLAGS; 51 } 52 53 /** 54 * __bio_integrity_endio - Integrity I/O completion function 55 * @bio: Protected bio 56 * 57 * Normally I/O completion is done in interrupt context. However, verifying I/O 58 * integrity is a time-consuming task which must be run in process context. 59 * 60 * This function postpones completion accordingly. 61 */ 62 bool __bio_integrity_endio(struct bio *bio) 63 { 64 struct bio_integrity_payload *bip = bio_integrity(bio); 65 struct bio_integrity_data *bid = 66 container_of(bip, struct bio_integrity_data, bip); 67 68 if (bio_op(bio) == REQ_OP_READ && !bio->bi_status && 69 bip_should_check(bip)) { 70 INIT_WORK(&bid->work, bio_integrity_verify_fn); 71 queue_work(kintegrityd_wq, &bid->work); 72 return false; 73 } 74 75 bio_integrity_finish(bid); 76 return true; 77 } 78 79 /** 80 * bio_integrity_prep - Prepare bio for integrity I/O 81 * @bio: bio to prepare 82 * @action: preparation action needed (BI_ACT_*) 83 * 84 * Allocate the integrity payload. For writes, generate the integrity metadata 85 * and for reads, setup the completion handler to verify the metadata. 86 * 87 * This is used for bios that do not have user integrity payloads attached. 88 */ 89 void bio_integrity_prep(struct bio *bio, unsigned int action) 90 { 91 struct bio_integrity_data *bid; 92 93 bid = mempool_alloc(&bid_pool, GFP_NOIO); 94 bio_integrity_init(bio, &bid->bip, &bid->bvec, 1); 95 bid->bio = bio; 96 bid->bip.bip_flags |= BIP_BLOCK_INTEGRITY; 97 bio_integrity_alloc_buf(bio, action & BI_ACT_ZERO); 98 if (action & BI_ACT_CHECK) 99 bio_integrity_setup_default(bio); 100 101 /* Auto-generate integrity metadata if this is a write */ 102 if (bio_data_dir(bio) == WRITE && bip_should_check(&bid->bip)) 103 bio_integrity_generate(bio); 104 else 105 bid->saved_bio_iter = bio->bi_iter; 106 } 107 EXPORT_SYMBOL(bio_integrity_prep); 108 109 void blk_flush_integrity(void) 110 { 111 flush_workqueue(kintegrityd_wq); 112 } 113 114 static int __init blk_integrity_auto_init(void) 115 { 116 bid_slab = kmem_cache_create("bio_integrity_data", 117 sizeof(struct bio_integrity_data), 0, 118 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 119 120 if (mempool_init_slab_pool(&bid_pool, BIO_POOL_SIZE, bid_slab)) 121 panic("bio: can't create integrity pool\n"); 122 123 /* 124 * kintegrityd won't block much but may burn a lot of CPU cycles. 125 * Make it highpri CPU intensive wq with max concurrency of 1. 126 */ 127 kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM | 128 WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1); 129 if (!kintegrityd_wq) 130 panic("Failed to create kintegrityd\n"); 131 return 0; 132 } 133 subsys_initcall(blk_integrity_auto_init); 134