Merge branch 'nvme-5.8' of git://git.infradead.org/nvme into block-5.8
[linux-block.git] / block / bio-integrity.c
CommitLineData
8c16567d 1// SPDX-License-Identifier: GPL-2.0
7ba1ba12
MP
2/*
3 * bio-integrity.c - bio data integrity extensions
4 *
7878cba9 5 * Copyright (C) 2007, 2008, 2009 Oracle Corporation
7ba1ba12 6 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7ba1ba12
MP
7 */
8
9#include <linux/blkdev.h>
10#include <linux/mempool.h>
afeacc8c 11#include <linux/export.h>
7ba1ba12
MP
12#include <linux/bio.h>
13#include <linux/workqueue.h>
5a0e3ad6 14#include <linux/slab.h>
1179a5a0 15#include "blk.h"
7ba1ba12 16
9f060e22 17#define BIP_INLINE_VECS 4
7878cba9 18
9f060e22 19static struct kmem_cache *bip_slab;
7ba1ba12
MP
20static struct workqueue_struct *kintegrityd_wq;
21
5a48fc14
DW
22void blk_flush_integrity(void)
23{
24 flush_workqueue(kintegrityd_wq);
25}
26
0b8eb629
CX
27void __bio_integrity_free(struct bio_set *bs, struct bio_integrity_payload *bip)
28{
29 if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
30 if (bip->bip_vec)
31 bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
32 bip->bip_slab);
33 mempool_free(bip, &bs->bio_integrity_pool);
34 } else {
35 kfree(bip);
36 }
37}
38
7ba1ba12 39/**
1e2a410f 40 * bio_integrity_alloc - Allocate integrity payload and attach it to bio
7ba1ba12
MP
41 * @bio: bio to attach integrity metadata to
42 * @gfp_mask: Memory allocation mask
43 * @nr_vecs: Number of integrity metadata scatter-gather elements
7ba1ba12
MP
44 *
45 * Description: This function prepares a bio for attaching integrity
46 * metadata. nr_vecs specifies the maximum number of pages containing
47 * integrity metadata that can be attached.
48 */
1e2a410f
KO
49struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
50 gfp_t gfp_mask,
51 unsigned int nr_vecs)
7ba1ba12
MP
52{
53 struct bio_integrity_payload *bip;
1e2a410f 54 struct bio_set *bs = bio->bi_pool;
9f060e22
KO
55 unsigned inline_vecs;
56
d145dc23
ST
57 if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
58 return ERR_PTR(-EOPNOTSUPP);
59
8aa6ba2f 60 if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
7a102d90 61 bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
9f060e22
KO
62 inline_vecs = nr_vecs;
63 } else {
8aa6ba2f 64 bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
9f060e22 65 inline_vecs = BIP_INLINE_VECS;
7ba1ba12
MP
66 }
67
9f060e22 68 if (unlikely(!bip))
06c1e390 69 return ERR_PTR(-ENOMEM);
9f060e22 70
7878cba9
MP
71 memset(bip, 0, sizeof(*bip));
72
9f060e22 73 if (nr_vecs > inline_vecs) {
ed996a52
CH
74 unsigned long idx = 0;
75
9f060e22 76 bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
8aa6ba2f 77 &bs->bvec_integrity_pool);
9f060e22
KO
78 if (!bip->bip_vec)
79 goto err;
cbcd1054 80 bip->bip_max_vcnt = bvec_nr_vecs(idx);
ed996a52 81 bip->bip_slab = idx;
9f060e22
KO
82 } else {
83 bip->bip_vec = bip->bip_inline_vecs;
cbcd1054 84 bip->bip_max_vcnt = inline_vecs;
9f060e22
KO
85 }
86
7ba1ba12
MP
87 bip->bip_bio = bio;
88 bio->bi_integrity = bip;
1eff9d32 89 bio->bi_opf |= REQ_INTEGRITY;
7ba1ba12
MP
90
91 return bip;
9f060e22 92err:
0b8eb629 93 __bio_integrity_free(bs, bip);
06c1e390 94 return ERR_PTR(-ENOMEM);
7ba1ba12 95}
7ba1ba12
MP
96EXPORT_SYMBOL(bio_integrity_alloc);
97
98/**
99 * bio_integrity_free - Free bio integrity payload
100 * @bio: bio containing bip to be freed
7ba1ba12
MP
101 *
102 * Description: Used to free the integrity portion of a bio. Usually
103 * called from bio_free().
104 */
ece841ab 105void bio_integrity_free(struct bio *bio)
7ba1ba12 106{
180b2f95 107 struct bio_integrity_payload *bip = bio_integrity(bio);
1e2a410f
KO
108 struct bio_set *bs = bio->bi_pool;
109
b1f01388 110 if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
5f9378fa
MP
111 kfree(page_address(bip->bip_vec->bv_page) +
112 bip->bip_vec->bv_offset);
7ba1ba12 113
0b8eb629 114 __bio_integrity_free(bs, bip);
7ba1ba12 115 bio->bi_integrity = NULL;
7c20f116 116 bio->bi_opf &= ~REQ_INTEGRITY;
7ba1ba12 117}
7ba1ba12
MP
118
119/**
120 * bio_integrity_add_page - Attach integrity metadata
121 * @bio: bio to update
122 * @page: page containing integrity metadata
123 * @len: number of bytes of integrity metadata in page
124 * @offset: start offset within page
125 *
126 * Description: Attach a page containing integrity metadata to bio.
127 */
128int bio_integrity_add_page(struct bio *bio, struct page *page,
129 unsigned int len, unsigned int offset)
130{
180b2f95 131 struct bio_integrity_payload *bip = bio_integrity(bio);
7ba1ba12
MP
132 struct bio_vec *iv;
133
cbcd1054 134 if (bip->bip_vcnt >= bip->bip_max_vcnt) {
7ba1ba12
MP
135 printk(KERN_ERR "%s: bip_vec full\n", __func__);
136 return 0;
137 }
138
d57a5f7c 139 iv = bip->bip_vec + bip->bip_vcnt;
7ba1ba12 140
87a816df 141 if (bip->bip_vcnt &&
74d46992 142 bvec_gap_to_prev(bio->bi_disk->queue,
87a816df
SG
143 &bip->bip_vec[bip->bip_vcnt - 1], offset))
144 return 0;
145
7ba1ba12
MP
146 iv->bv_page = page;
147 iv->bv_len = len;
148 iv->bv_offset = offset;
149 bip->bip_vcnt++;
150
151 return len;
152}
153EXPORT_SYMBOL(bio_integrity_add_page);
154
7ba1ba12 155/**
18593088 156 * bio_integrity_process - Process integrity metadata for a bio
bf36f9cf 157 * @bio: bio to generate/verify integrity metadata for
63573e35 158 * @proc_iter: iterator to process
18593088 159 * @proc_fn: Pointer to the relevant processing function
7ba1ba12 160 */
4e4cbee9 161static blk_status_t bio_integrity_process(struct bio *bio,
63573e35 162 struct bvec_iter *proc_iter, integrity_processing_fn *proc_fn)
7ba1ba12 163{
74d46992 164 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
18593088 165 struct blk_integrity_iter iter;
594416a7
DW
166 struct bvec_iter bviter;
167 struct bio_vec bv;
5f9378fa 168 struct bio_integrity_payload *bip = bio_integrity(bio);
4e4cbee9 169 blk_status_t ret = BLK_STS_OK;
5f9378fa
MP
170 void *prot_buf = page_address(bip->bip_vec->bv_page) +
171 bip->bip_vec->bv_offset;
7ba1ba12 172
74d46992 173 iter.disk_name = bio->bi_disk->disk_name;
a48f041d 174 iter.interval = 1 << bi->interval_exp;
63573e35 175 iter.seed = proc_iter->bi_sector;
18593088 176 iter.prot_buf = prot_buf;
7ba1ba12 177
63573e35 178 __bio_for_each_segment(bv, bio, bviter, *proc_iter) {
594416a7 179 void *kaddr = kmap_atomic(bv.bv_page);
7ba1ba12 180
594416a7
DW
181 iter.data_buf = kaddr + bv.bv_offset;
182 iter.data_size = bv.bv_len;
18593088
MP
183
184 ret = proc_fn(&iter);
185 if (ret) {
186 kunmap_atomic(kaddr);
187 return ret;
188 }
7ba1ba12 189
e8e3c3d6 190 kunmap_atomic(kaddr);
7ba1ba12 191 }
bf36f9cf
GZ
192 return ret;
193}
194
7ba1ba12
MP
195/**
196 * bio_integrity_prep - Prepare bio for integrity I/O
197 * @bio: bio to prepare
198 *
e23947bd
DM
199 * Description: Checks if the bio already has an integrity payload attached.
200 * If it does, the payload has been generated by another kernel subsystem,
201 * and we just pass it through. Otherwise allocates integrity payload.
202 * The bio must have data direction, target device and start sector set priot
203 * to calling. In the WRITE case, integrity metadata will be generated using
204 * the block device's integrity function. In the READ case, the buffer
7ba1ba12
MP
205 * will be prepared for DMA and a suitable end_io handler set up.
206 */
e23947bd 207bool bio_integrity_prep(struct bio *bio)
7ba1ba12
MP
208{
209 struct bio_integrity_payload *bip;
74d46992
CH
210 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
211 struct request_queue *q = bio->bi_disk->queue;
7ba1ba12
MP
212 void *buf;
213 unsigned long start, end;
214 unsigned int len, nr_pages;
215 unsigned int bytes, offset, i;
3be91c4a 216 unsigned int intervals;
e23947bd 217 blk_status_t status;
7ba1ba12 218
9346beb9
CH
219 if (!bi)
220 return true;
221
e23947bd
DM
222 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
223 return true;
224
225 if (!bio_sectors(bio))
226 return true;
7ba1ba12 227
e23947bd
DM
228 /* Already protected? */
229 if (bio_integrity(bio))
230 return true;
231
e23947bd
DM
232 if (bio_data_dir(bio) == READ) {
233 if (!bi->profile->verify_fn ||
234 !(bi->flags & BLK_INTEGRITY_VERIFY))
235 return true;
236 } else {
237 if (!bi->profile->generate_fn ||
238 !(bi->flags & BLK_INTEGRITY_GENERATE))
239 return true;
240 }
3be91c4a 241 intervals = bio_integrity_intervals(bi, bio_sectors(bio));
7ba1ba12
MP
242
243 /* Allocate kernel buffer for protection data */
3be91c4a 244 len = intervals * bi->tuple_size;
72f46503 245 buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
e23947bd 246 status = BLK_STS_RESOURCE;
7ba1ba12
MP
247 if (unlikely(buf == NULL)) {
248 printk(KERN_ERR "could not allocate integrity buffer\n");
e23947bd 249 goto err_end_io;
7ba1ba12
MP
250 }
251
252 end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
253 start = ((unsigned long) buf) >> PAGE_SHIFT;
254 nr_pages = end - start;
255
256 /* Allocate bio integrity payload and integrity vectors */
257 bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
7b6c0f80 258 if (IS_ERR(bip)) {
7ba1ba12
MP
259 printk(KERN_ERR "could not allocate data integrity bioset\n");
260 kfree(buf);
e23947bd
DM
261 status = BLK_STS_RESOURCE;
262 goto err_end_io;
7ba1ba12
MP
263 }
264
b1f01388 265 bip->bip_flags |= BIP_BLOCK_INTEGRITY;
d57a5f7c 266 bip->bip_iter.bi_size = len;
18593088 267 bip_set_seed(bip, bio->bi_iter.bi_sector);
7ba1ba12 268
aae7df50
MP
269 if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
270 bip->bip_flags |= BIP_IP_CHECKSUM;
271
7ba1ba12
MP
272 /* Map it */
273 offset = offset_in_page(buf);
274 for (i = 0 ; i < nr_pages ; i++) {
275 int ret;
276 bytes = PAGE_SIZE - offset;
277
278 if (len <= 0)
279 break;
280
281 if (bytes > len)
282 bytes = len;
283
284 ret = bio_integrity_add_page(bio, virt_to_page(buf),
285 bytes, offset);
286
e7bf90e5
WW
287 if (ret == 0) {
288 printk(KERN_ERR "could not attach integrity payload\n");
e7bf90e5
WW
289 status = BLK_STS_RESOURCE;
290 goto err_end_io;
291 }
7ba1ba12
MP
292
293 if (ret < bytes)
294 break;
295
296 buf += bytes;
297 len -= bytes;
298 offset = 0;
299 }
300
7ba1ba12 301 /* Auto-generate integrity metadata if this is a write */
63573e35
DM
302 if (bio_data_dir(bio) == WRITE) {
303 bio_integrity_process(bio, &bio->bi_iter,
304 bi->profile->generate_fn);
7759eb23
ML
305 } else {
306 bip->bio_iter = bio->bi_iter;
63573e35 307 }
e23947bd
DM
308 return true;
309
310err_end_io:
311 bio->bi_status = status;
312 bio_endio(bio);
313 return false;
7ba1ba12 314
7ba1ba12
MP
315}
316EXPORT_SYMBOL(bio_integrity_prep);
317
7ba1ba12
MP
318/**
319 * bio_integrity_verify_fn - Integrity I/O completion worker
320 * @work: Work struct stored in bio to be verified
321 *
322 * Description: This workqueue function is called to complete a READ
323 * request. The function verifies the transferred integrity metadata
324 * and then calls the original bio end_io function.
325 */
326static void bio_integrity_verify_fn(struct work_struct *work)
327{
b984679e 328 struct bio_integrity_payload *bip =
7ba1ba12
MP
329 container_of(work, struct bio_integrity_payload, bip_work);
330 struct bio *bio = bip->bip_bio;
74d46992 331 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
63573e35
DM
332
333 /*
334 * At the moment verify is called bio's iterator was advanced
335 * during split and completion, we need to rewind iterator to
336 * it's original position.
337 */
7759eb23
ML
338 bio->bi_status = bio_integrity_process(bio, &bip->bio_iter,
339 bi->profile->verify_fn);
7c20f116 340 bio_integrity_free(bio);
4246a0b6 341 bio_endio(bio);
7ba1ba12
MP
342}
343
344/**
7c20f116 345 * __bio_integrity_endio - Integrity I/O completion function
7ba1ba12 346 * @bio: Protected bio
7ba1ba12
MP
347 *
348 * Description: Completion for integrity I/O
349 *
350 * Normally I/O completion is done in interrupt context. However,
351 * verifying I/O integrity is a time-consuming task which must be run
352 * in process context. This function postpones completion
353 * accordingly.
354 */
7c20f116 355bool __bio_integrity_endio(struct bio *bio)
7ba1ba12 356{
97e05463 357 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
f86e28c4 358 struct bio_integrity_payload *bip = bio_integrity(bio);
c775d209
MB
359
360 if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
f86e28c4 361 (bip->bip_flags & BIP_BLOCK_INTEGRITY) && bi->profile->verify_fn) {
7c20f116
CH
362 INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
363 queue_work(kintegrityd_wq, &bip->bip_work);
364 return false;
7b24fc4d
MP
365 }
366
7c20f116
CH
367 bio_integrity_free(bio);
368 return true;
7ba1ba12 369}
7ba1ba12 370
7ba1ba12
MP
371/**
372 * bio_integrity_advance - Advance integrity vector
373 * @bio: bio whose integrity vector to update
374 * @bytes_done: number of data bytes that have been completed
375 *
376 * Description: This function calculates how many integrity bytes the
377 * number of completed data bytes correspond to and advances the
378 * integrity vector accordingly.
379 */
380void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
381{
180b2f95 382 struct bio_integrity_payload *bip = bio_integrity(bio);
74d46992 383 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
d57a5f7c 384 unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
7ba1ba12 385
309a62fa 386 bip->bip_iter.bi_sector += bytes_done >> 9;
d57a5f7c 387 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
7ba1ba12 388}
7ba1ba12
MP
389
390/**
391 * bio_integrity_trim - Trim integrity vector
392 * @bio: bio whose integrity vector to update
7ba1ba12
MP
393 *
394 * Description: Used to trim the integrity vector in a cloned bio.
7ba1ba12 395 */
fbd08e76 396void bio_integrity_trim(struct bio *bio)
7ba1ba12 397{
180b2f95 398 struct bio_integrity_payload *bip = bio_integrity(bio);
74d46992 399 struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
7ba1ba12 400
fbd08e76 401 bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
7ba1ba12
MP
402}
403EXPORT_SYMBOL(bio_integrity_trim);
404
7ba1ba12
MP
405/**
406 * bio_integrity_clone - Callback for cloning bios with integrity metadata
407 * @bio: New bio
408 * @bio_src: Original bio
87092698 409 * @gfp_mask: Memory allocation mask
7ba1ba12
MP
410 *
411 * Description: Called to allocate a bip when cloning a bio
412 */
7878cba9 413int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
1e2a410f 414 gfp_t gfp_mask)
7ba1ba12 415{
180b2f95 416 struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
7ba1ba12
MP
417 struct bio_integrity_payload *bip;
418
419 BUG_ON(bip_src == NULL);
420
1e2a410f 421 bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
7b6c0f80
DC
422 if (IS_ERR(bip))
423 return PTR_ERR(bip);
7ba1ba12
MP
424
425 memcpy(bip->bip_vec, bip_src->bip_vec,
426 bip_src->bip_vcnt * sizeof(struct bio_vec));
427
7ba1ba12 428 bip->bip_vcnt = bip_src->bip_vcnt;
d57a5f7c 429 bip->bip_iter = bip_src->bip_iter;
7ba1ba12
MP
430
431 return 0;
432}
433EXPORT_SYMBOL(bio_integrity_clone);
434
7878cba9 435int bioset_integrity_create(struct bio_set *bs, int pool_size)
7ba1ba12 436{
8aa6ba2f 437 if (mempool_initialized(&bs->bio_integrity_pool))
a91a2785
MP
438 return 0;
439
8aa6ba2f
KO
440 if (mempool_init_slab_pool(&bs->bio_integrity_pool,
441 pool_size, bip_slab))
9f060e22 442 return -1;
7ba1ba12 443
8aa6ba2f
KO
444 if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
445 mempool_exit(&bs->bio_integrity_pool);
7878cba9 446 return -1;
bc5c8f07 447 }
7878cba9
MP
448
449 return 0;
450}
451EXPORT_SYMBOL(bioset_integrity_create);
452
453void bioset_integrity_free(struct bio_set *bs)
454{
8aa6ba2f
KO
455 mempool_exit(&bs->bio_integrity_pool);
456 mempool_exit(&bs->bvec_integrity_pool);
7878cba9 457}
7878cba9
MP
458
459void __init bio_integrity_init(void)
460{
a6e8dc46
TH
461 /*
462 * kintegrityd won't block much but may burn a lot of CPU cycles.
463 * Make it highpri CPU intensive wq with max concurrency of 1.
464 */
465 kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
466 WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
6d2a78e7
MP
467 if (!kintegrityd_wq)
468 panic("Failed to create kintegrityd\n");
7ba1ba12 469
9f060e22
KO
470 bip_slab = kmem_cache_create("bio_integrity_payload",
471 sizeof(struct bio_integrity_payload) +
472 sizeof(struct bio_vec) * BIP_INLINE_VECS,
473 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
7ba1ba12 474}