xref: /linux/block/bio-integrity-auto.c (revision 0fc8f6200d2313278fbf4539bbab74677c685531)
1e5167911SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2e5167911SChristoph Hellwig /*
3e5167911SChristoph Hellwig  * Copyright (C) 2007, 2008, 2009 Oracle Corporation
4e5167911SChristoph Hellwig  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
5e5167911SChristoph Hellwig  *
6e5167911SChristoph Hellwig  * Automatically generate and verify integrity data on PI capable devices if the
7e5167911SChristoph Hellwig  * bio submitter didn't provide PI itself.  This ensures that kernel verifies
8e5167911SChristoph Hellwig  * data integrity even if the file system (or other user of the block device) is
9e5167911SChristoph Hellwig  * not aware of PI.
10e5167911SChristoph Hellwig  */
11e5167911SChristoph Hellwig #include <linux/blk-integrity.h>
128098514bSKeith Busch #include <linux/t10-pi.h>
13e5167911SChristoph Hellwig #include <linux/workqueue.h>
14e5167911SChristoph Hellwig #include "blk.h"
15e5167911SChristoph Hellwig 
16105ca2a2SChristoph Hellwig struct bio_integrity_data {
17105ca2a2SChristoph Hellwig 	struct bio			*bio;
18105ca2a2SChristoph Hellwig 	struct bvec_iter		saved_bio_iter;
19105ca2a2SChristoph Hellwig 	struct work_struct		work;
20105ca2a2SChristoph Hellwig 	struct bio_integrity_payload	bip;
21105ca2a2SChristoph Hellwig 	struct bio_vec			bvec;
22105ca2a2SChristoph Hellwig };
23105ca2a2SChristoph Hellwig 
24105ca2a2SChristoph Hellwig static struct kmem_cache *bid_slab;
25105ca2a2SChristoph Hellwig static mempool_t bid_pool;
26e5167911SChristoph Hellwig static struct workqueue_struct *kintegrityd_wq;
27e5167911SChristoph Hellwig 
bio_integrity_finish(struct bio_integrity_data * bid)28105ca2a2SChristoph Hellwig static void bio_integrity_finish(struct bio_integrity_data *bid)
29105ca2a2SChristoph Hellwig {
30105ca2a2SChristoph Hellwig 	bid->bio->bi_integrity = NULL;
31105ca2a2SChristoph Hellwig 	bid->bio->bi_opf &= ~REQ_INTEGRITY;
32ec7f31b2SChristoph Hellwig 	bio_integrity_free_buf(&bid->bip);
33105ca2a2SChristoph Hellwig 	mempool_free(bid, &bid_pool);
34105ca2a2SChristoph Hellwig }
35105ca2a2SChristoph Hellwig 
bio_integrity_verify_fn(struct work_struct * work)36e5167911SChristoph Hellwig static void bio_integrity_verify_fn(struct work_struct *work)
37e5167911SChristoph Hellwig {
38105ca2a2SChristoph Hellwig 	struct bio_integrity_data *bid =
39105ca2a2SChristoph Hellwig 		container_of(work, struct bio_integrity_data, work);
40105ca2a2SChristoph Hellwig 	struct bio *bio = bid->bio;
41e5167911SChristoph Hellwig 
423f006268SChristoph Hellwig 	bio->bi_status = bio_integrity_verify(bio, &bid->saved_bio_iter);
43105ca2a2SChristoph Hellwig 	bio_integrity_finish(bid);
44e5167911SChristoph Hellwig 	bio_endio(bio);
45e5167911SChristoph Hellwig }
46e5167911SChristoph Hellwig 
478098514bSKeith Busch #define BIP_CHECK_FLAGS (BIP_CHECK_GUARD | BIP_CHECK_REFTAG | BIP_CHECK_APPTAG)
bip_should_check(struct bio_integrity_payload * bip)488098514bSKeith Busch static bool bip_should_check(struct bio_integrity_payload *bip)
498098514bSKeith Busch {
508098514bSKeith Busch 	return bip->bip_flags & BIP_CHECK_FLAGS;
518098514bSKeith Busch }
528098514bSKeith Busch 
53e5167911SChristoph Hellwig /**
54e5167911SChristoph Hellwig  * __bio_integrity_endio - Integrity I/O completion function
55e5167911SChristoph Hellwig  * @bio:	Protected bio
56e5167911SChristoph Hellwig  *
57e5167911SChristoph Hellwig  * Normally I/O completion is done in interrupt context.  However, verifying I/O
58e5167911SChristoph Hellwig  * integrity is a time-consuming task which must be run in process context.
59e5167911SChristoph Hellwig  *
60e5167911SChristoph Hellwig  * This function postpones completion accordingly.
61e5167911SChristoph Hellwig  */
__bio_integrity_endio(struct bio * bio)62e5167911SChristoph Hellwig bool __bio_integrity_endio(struct bio *bio)
63e5167911SChristoph Hellwig {
64e5167911SChristoph Hellwig 	struct bio_integrity_payload *bip = bio_integrity(bio);
65105ca2a2SChristoph Hellwig 	struct bio_integrity_data *bid =
66105ca2a2SChristoph Hellwig 		container_of(bip, struct bio_integrity_data, bip);
67e5167911SChristoph Hellwig 
688098514bSKeith Busch 	if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
698098514bSKeith Busch 	    bip_should_check(bip)) {
70105ca2a2SChristoph Hellwig 		INIT_WORK(&bid->work, bio_integrity_verify_fn);
71105ca2a2SChristoph Hellwig 		queue_work(kintegrityd_wq, &bid->work);
72e5167911SChristoph Hellwig 		return false;
73e5167911SChristoph Hellwig 	}
74e5167911SChristoph Hellwig 
75105ca2a2SChristoph Hellwig 	bio_integrity_finish(bid);
76e5167911SChristoph Hellwig 	return true;
77e5167911SChristoph Hellwig }
78e5167911SChristoph Hellwig 
79e5167911SChristoph Hellwig /**
80e5167911SChristoph Hellwig  * bio_integrity_prep - Prepare bio for integrity I/O
81e5167911SChristoph Hellwig  * @bio:	bio to prepare
827ea25eaaSChristoph Hellwig  * @action:	preparation action needed (BI_ACT_*)
83e5167911SChristoph Hellwig  *
847ea25eaaSChristoph Hellwig  * Allocate the integrity payload.  For writes, generate the integrity metadata
857ea25eaaSChristoph Hellwig  * and for reads, setup the completion handler to verify the metadata.
867ea25eaaSChristoph Hellwig  *
877ea25eaaSChristoph Hellwig  * This is used for bios that do not have user integrity payloads attached.
88e5167911SChristoph Hellwig  */
bio_integrity_prep(struct bio * bio,unsigned int action)897ea25eaaSChristoph Hellwig void bio_integrity_prep(struct bio *bio, unsigned int action)
90e5167911SChristoph Hellwig {
91105ca2a2SChristoph Hellwig 	struct bio_integrity_data *bid;
92105ca2a2SChristoph Hellwig 
93105ca2a2SChristoph Hellwig 	bid = mempool_alloc(&bid_pool, GFP_NOIO);
94105ca2a2SChristoph Hellwig 	bio_integrity_init(bio, &bid->bip, &bid->bvec, 1);
95105ca2a2SChristoph Hellwig 	bid->bio = bio;
96105ca2a2SChristoph Hellwig 	bid->bip.bip_flags |= BIP_BLOCK_INTEGRITY;
977ea25eaaSChristoph Hellwig 	bio_integrity_alloc_buf(bio, action & BI_ACT_ZERO);
98a9366556SChristoph Hellwig 	if (action & BI_ACT_CHECK)
99a9366556SChristoph Hellwig 		bio_integrity_setup_default(bio);
100e5167911SChristoph Hellwig 
101e5167911SChristoph Hellwig 	/* Auto-generate integrity metadata if this is a write */
1028098514bSKeith Busch 	if (bio_data_dir(bio) == WRITE && bip_should_check(&bid->bip))
1033f006268SChristoph Hellwig 		bio_integrity_generate(bio);
104e5167911SChristoph Hellwig 	else
105105ca2a2SChristoph Hellwig 		bid->saved_bio_iter = bio->bi_iter;
106e5167911SChristoph Hellwig }
107e5167911SChristoph Hellwig EXPORT_SYMBOL(bio_integrity_prep);
108e5167911SChristoph Hellwig 
blk_flush_integrity(void)109e5167911SChristoph Hellwig void blk_flush_integrity(void)
110e5167911SChristoph Hellwig {
111e5167911SChristoph Hellwig 	flush_workqueue(kintegrityd_wq);
112e5167911SChristoph Hellwig }
113e5167911SChristoph Hellwig 
blk_integrity_auto_init(void)114e5167911SChristoph Hellwig static int __init blk_integrity_auto_init(void)
115e5167911SChristoph Hellwig {
116105ca2a2SChristoph Hellwig 	bid_slab = kmem_cache_create("bio_integrity_data",
117105ca2a2SChristoph Hellwig 			sizeof(struct bio_integrity_data), 0,
118105ca2a2SChristoph Hellwig 			SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
119105ca2a2SChristoph Hellwig 
120105ca2a2SChristoph Hellwig 	if (mempool_init_slab_pool(&bid_pool, BIO_POOL_SIZE, bid_slab))
121105ca2a2SChristoph Hellwig 		panic("bio: can't create integrity pool\n");
122105ca2a2SChristoph Hellwig 
123e5167911SChristoph Hellwig 	/*
124e5167911SChristoph Hellwig 	 * kintegrityd won't block much but may burn a lot of CPU cycles.
125e5167911SChristoph Hellwig 	 * Make it highpri CPU intensive wq with max concurrency of 1.
126e5167911SChristoph Hellwig 	 */
127e5167911SChristoph Hellwig 	kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
128*98236343SMarco Crivellari 					 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_PERCPU, 1);
129e5167911SChristoph Hellwig 	if (!kintegrityd_wq)
130e5167911SChristoph Hellwig 		panic("Failed to create kintegrityd\n");
131e5167911SChristoph Hellwig 	return 0;
132e5167911SChristoph Hellwig }
133e5167911SChristoph Hellwig subsys_initcall(blk_integrity_auto_init);
134