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