GNU Linux-libre 5.4.241-gnu1
[releases.git] / block / bio-integrity.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bio-integrity.c - bio data integrity extensions
4  *
5  * Copyright (C) 2007, 2008, 2009 Oracle Corporation
6  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7  */
8
9 #include <linux/blkdev.h>
10 #include <linux/mempool.h>
11 #include <linux/export.h>
12 #include <linux/bio.h>
13 #include <linux/workqueue.h>
14 #include <linux/slab.h>
15 #include "blk.h"
16
17 #define BIP_INLINE_VECS 4
18
19 static struct kmem_cache *bip_slab;
20 static struct workqueue_struct *kintegrityd_wq;
21
22 void blk_flush_integrity(void)
23 {
24         flush_workqueue(kintegrityd_wq);
25 }
26
27 void __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
39 /**
40  * bio_integrity_alloc - Allocate integrity payload and attach it to bio
41  * @bio:        bio to attach integrity metadata to
42  * @gfp_mask:   Memory allocation mask
43  * @nr_vecs:    Number of integrity metadata scatter-gather elements
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  */
49 struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
50                                                   gfp_t gfp_mask,
51                                                   unsigned int nr_vecs)
52 {
53         struct bio_integrity_payload *bip;
54         struct bio_set *bs = bio->bi_pool;
55         unsigned inline_vecs;
56
57         if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
58                 bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
59                 inline_vecs = nr_vecs;
60         } else {
61                 bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
62                 inline_vecs = BIP_INLINE_VECS;
63         }
64
65         if (unlikely(!bip))
66                 return ERR_PTR(-ENOMEM);
67
68         memset(bip, 0, sizeof(*bip));
69
70         if (nr_vecs > inline_vecs) {
71                 unsigned long idx = 0;
72
73                 bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
74                                           &bs->bvec_integrity_pool);
75                 if (!bip->bip_vec)
76                         goto err;
77                 bip->bip_max_vcnt = bvec_nr_vecs(idx);
78                 bip->bip_slab = idx;
79         } else {
80                 bip->bip_vec = bip->bip_inline_vecs;
81                 bip->bip_max_vcnt = inline_vecs;
82         }
83
84         bip->bip_bio = bio;
85         bio->bi_integrity = bip;
86         bio->bi_opf |= REQ_INTEGRITY;
87
88         return bip;
89 err:
90         __bio_integrity_free(bs, bip);
91         return ERR_PTR(-ENOMEM);
92 }
93 EXPORT_SYMBOL(bio_integrity_alloc);
94
95 /**
96  * bio_integrity_free - Free bio integrity payload
97  * @bio:        bio containing bip to be freed
98  *
99  * Description: Used to free the integrity portion of a bio. Usually
100  * called from bio_free().
101  */
102 void bio_integrity_free(struct bio *bio)
103 {
104         struct bio_integrity_payload *bip = bio_integrity(bio);
105         struct bio_set *bs = bio->bi_pool;
106
107         if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
108                 kfree(page_address(bip->bip_vec->bv_page) +
109                       bip->bip_vec->bv_offset);
110
111         __bio_integrity_free(bs, bip);
112         bio->bi_integrity = NULL;
113         bio->bi_opf &= ~REQ_INTEGRITY;
114 }
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  */
125 int bio_integrity_add_page(struct bio *bio, struct page *page,
126                            unsigned int len, unsigned int offset)
127 {
128         struct bio_integrity_payload *bip = bio_integrity(bio);
129         struct bio_vec *iv;
130
131         if (bip->bip_vcnt >= bip->bip_max_vcnt) {
132                 printk(KERN_ERR "%s: bip_vec full\n", __func__);
133                 return 0;
134         }
135
136         iv = bip->bip_vec + bip->bip_vcnt;
137
138         if (bip->bip_vcnt &&
139             bvec_gap_to_prev(bio->bi_disk->queue,
140                              &bip->bip_vec[bip->bip_vcnt - 1], offset))
141                 return 0;
142
143         iv->bv_page = page;
144         iv->bv_len = len;
145         iv->bv_offset = offset;
146         bip->bip_vcnt++;
147
148         return len;
149 }
150 EXPORT_SYMBOL(bio_integrity_add_page);
151
152 /**
153  * bio_integrity_process - Process integrity metadata for a bio
154  * @bio:        bio to generate/verify integrity metadata for
155  * @proc_iter:  iterator to process
156  * @proc_fn:    Pointer to the relevant processing function
157  */
158 static blk_status_t bio_integrity_process(struct bio *bio,
159                 struct bvec_iter *proc_iter, integrity_processing_fn *proc_fn)
160 {
161         struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
162         struct blk_integrity_iter iter;
163         struct bvec_iter bviter;
164         struct bio_vec bv;
165         struct bio_integrity_payload *bip = bio_integrity(bio);
166         blk_status_t ret = BLK_STS_OK;
167         void *prot_buf = page_address(bip->bip_vec->bv_page) +
168                 bip->bip_vec->bv_offset;
169
170         iter.disk_name = bio->bi_disk->disk_name;
171         iter.interval = 1 << bi->interval_exp;
172         iter.seed = proc_iter->bi_sector;
173         iter.prot_buf = prot_buf;
174
175         __bio_for_each_segment(bv, bio, bviter, *proc_iter) {
176                 void *kaddr = kmap_atomic(bv.bv_page);
177
178                 iter.data_buf = kaddr + bv.bv_offset;
179                 iter.data_size = bv.bv_len;
180
181                 ret = proc_fn(&iter);
182                 if (ret) {
183                         kunmap_atomic(kaddr);
184                         return ret;
185                 }
186
187                 kunmap_atomic(kaddr);
188         }
189         return ret;
190 }
191
192 /**
193  * bio_integrity_prep - Prepare bio for integrity I/O
194  * @bio:        bio to prepare
195  *
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
202  * will be prepared for DMA and a suitable end_io handler set up.
203  */
204 bool bio_integrity_prep(struct bio *bio)
205 {
206         struct bio_integrity_payload *bip;
207         struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
208         struct request_queue *q = bio->bi_disk->queue;
209         void *buf;
210         unsigned long start, end;
211         unsigned int len, nr_pages;
212         unsigned int bytes, offset, i;
213         unsigned int intervals;
214         blk_status_t status;
215
216         if (!bi)
217                 return true;
218
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;
224
225         /* Already protected? */
226         if (bio_integrity(bio))
227                 return true;
228
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         }
238         intervals = bio_integrity_intervals(bi, bio_sectors(bio));
239
240         /* Allocate kernel buffer for protection data */
241         len = intervals * bi->tuple_size;
242         buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
243         status = BLK_STS_RESOURCE;
244         if (unlikely(buf == NULL)) {
245                 printk(KERN_ERR "could not allocate integrity buffer\n");
246                 goto err_end_io;
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);
255         if (IS_ERR(bip)) {
256                 printk(KERN_ERR "could not allocate data integrity bioset\n");
257                 kfree(buf);
258                 status = BLK_STS_RESOURCE;
259                 goto err_end_io;
260         }
261
262         bip->bip_flags |= BIP_BLOCK_INTEGRITY;
263         bip->bip_iter.bi_size = len;
264         bip_set_seed(bip, bio->bi_iter.bi_sector);
265
266         if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
267                 bip->bip_flags |= BIP_IP_CHECKSUM;
268
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
284                 if (ret == 0) {
285                         printk(KERN_ERR "could not attach integrity payload\n");
286                         status = BLK_STS_RESOURCE;
287                         goto err_end_io;
288                 }
289
290                 if (ret < bytes)
291                         break;
292
293                 buf += bytes;
294                 len -= bytes;
295                 offset = 0;
296         }
297
298         /* Auto-generate integrity metadata if this is a write */
299         if (bio_data_dir(bio) == WRITE) {
300                 bio_integrity_process(bio, &bio->bi_iter,
301                                       bi->profile->generate_fn);
302         } else {
303                 bip->bio_iter = bio->bi_iter;
304         }
305         return true;
306
307 err_end_io:
308         bio->bi_status = status;
309         bio_endio(bio);
310         return false;
311
312 }
313 EXPORT_SYMBOL(bio_integrity_prep);
314
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  */
323 static void bio_integrity_verify_fn(struct work_struct *work)
324 {
325         struct bio_integrity_payload *bip =
326                 container_of(work, struct bio_integrity_payload, bip_work);
327         struct bio *bio = bip->bip_bio;
328         struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
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          */
335         bio->bi_status = bio_integrity_process(bio, &bip->bio_iter,
336                                                 bi->profile->verify_fn);
337         bio_integrity_free(bio);
338         bio_endio(bio);
339 }
340
341 /**
342  * __bio_integrity_endio - Integrity I/O completion function
343  * @bio:        Protected bio
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  */
352 bool __bio_integrity_endio(struct bio *bio)
353 {
354         struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
355         struct bio_integrity_payload *bip = bio_integrity(bio);
356
357         if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
358             (bip->bip_flags & BIP_BLOCK_INTEGRITY) && bi->profile->verify_fn) {
359                 INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
360                 queue_work(kintegrityd_wq, &bip->bip_work);
361                 return false;
362         }
363
364         bio_integrity_free(bio);
365         return true;
366 }
367
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  */
377 void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
378 {
379         struct bio_integrity_payload *bip = bio_integrity(bio);
380         struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
381         unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
382
383         bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
384         bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
385 }
386
387 /**
388  * bio_integrity_trim - Trim integrity vector
389  * @bio:        bio whose integrity vector to update
390  *
391  * Description: Used to trim the integrity vector in a cloned bio.
392  */
393 void bio_integrity_trim(struct bio *bio)
394 {
395         struct bio_integrity_payload *bip = bio_integrity(bio);
396         struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
397
398         bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
399 }
400 EXPORT_SYMBOL(bio_integrity_trim);
401
402 /**
403  * bio_integrity_clone - Callback for cloning bios with integrity metadata
404  * @bio:        New bio
405  * @bio_src:    Original bio
406  * @gfp_mask:   Memory allocation mask
407  *
408  * Description: Called to allocate a bip when cloning a bio
409  */
410 int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
411                         gfp_t gfp_mask)
412 {
413         struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
414         struct bio_integrity_payload *bip;
415
416         BUG_ON(bip_src == NULL);
417
418         bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
419         if (IS_ERR(bip))
420                 return PTR_ERR(bip);
421
422         memcpy(bip->bip_vec, bip_src->bip_vec,
423                bip_src->bip_vcnt * sizeof(struct bio_vec));
424
425         bip->bip_vcnt = bip_src->bip_vcnt;
426         bip->bip_iter = bip_src->bip_iter;
427         bip->bip_flags = bip_src->bip_flags & ~BIP_BLOCK_INTEGRITY;
428
429         return 0;
430 }
431 EXPORT_SYMBOL(bio_integrity_clone);
432
433 int bioset_integrity_create(struct bio_set *bs, int pool_size)
434 {
435         if (mempool_initialized(&bs->bio_integrity_pool))
436                 return 0;
437
438         if (mempool_init_slab_pool(&bs->bio_integrity_pool,
439                                    pool_size, bip_slab))
440                 return -1;
441
442         if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
443                 mempool_exit(&bs->bio_integrity_pool);
444                 return -1;
445         }
446
447         return 0;
448 }
449 EXPORT_SYMBOL(bioset_integrity_create);
450
451 void bioset_integrity_free(struct bio_set *bs)
452 {
453         mempool_exit(&bs->bio_integrity_pool);
454         mempool_exit(&bs->bvec_integrity_pool);
455 }
456
457 void __init bio_integrity_init(void)
458 {
459         /*
460          * kintegrityd won't block much but may burn a lot of CPU cycles.
461          * Make it highpri CPU intensive wq with max concurrency of 1.
462          */
463         kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
464                                          WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
465         if (!kintegrityd_wq)
466                 panic("Failed to create kintegrityd\n");
467
468         bip_slab = kmem_cache_create("bio_integrity_payload",
469                                      sizeof(struct bio_integrity_payload) +
470                                      sizeof(struct bio_vec) * BIP_INLINE_VECS,
471                                      0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
472 }