Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * bio-integrity.c - bio data integrity extensions
0004  *
0005  * Copyright (C) 2007, 2008, 2009 Oracle Corporation
0006  * Written by: Martin K. Petersen <martin.petersen@oracle.com>
0007  */
0008 
0009 #include <linux/blk-integrity.h>
0010 #include <linux/mempool.h>
0011 #include <linux/export.h>
0012 #include <linux/bio.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/slab.h>
0015 #include "blk.h"
0016 
0017 static struct kmem_cache *bip_slab;
0018 static struct workqueue_struct *kintegrityd_wq;
0019 
0020 void blk_flush_integrity(void)
0021 {
0022     flush_workqueue(kintegrityd_wq);
0023 }
0024 
0025 static void __bio_integrity_free(struct bio_set *bs,
0026                  struct bio_integrity_payload *bip)
0027 {
0028     if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
0029         if (bip->bip_vec)
0030             bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
0031                   bip->bip_max_vcnt);
0032         mempool_free(bip, &bs->bio_integrity_pool);
0033     } else {
0034         kfree(bip);
0035     }
0036 }
0037 
0038 /**
0039  * bio_integrity_alloc - Allocate integrity payload and attach it to bio
0040  * @bio:    bio to attach integrity metadata to
0041  * @gfp_mask:   Memory allocation mask
0042  * @nr_vecs:    Number of integrity metadata scatter-gather elements
0043  *
0044  * Description: This function prepares a bio for attaching integrity
0045  * metadata.  nr_vecs specifies the maximum number of pages containing
0046  * integrity metadata that can be attached.
0047  */
0048 struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
0049                           gfp_t gfp_mask,
0050                           unsigned int nr_vecs)
0051 {
0052     struct bio_integrity_payload *bip;
0053     struct bio_set *bs = bio->bi_pool;
0054     unsigned inline_vecs;
0055 
0056     if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
0057         return ERR_PTR(-EOPNOTSUPP);
0058 
0059     if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
0060         bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
0061         inline_vecs = nr_vecs;
0062     } else {
0063         bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
0064         inline_vecs = BIO_INLINE_VECS;
0065     }
0066 
0067     if (unlikely(!bip))
0068         return ERR_PTR(-ENOMEM);
0069 
0070     memset(bip, 0, sizeof(*bip));
0071 
0072     if (nr_vecs > inline_vecs) {
0073         bip->bip_max_vcnt = nr_vecs;
0074         bip->bip_vec = bvec_alloc(&bs->bvec_integrity_pool,
0075                       &bip->bip_max_vcnt, gfp_mask);
0076         if (!bip->bip_vec)
0077             goto err;
0078     } else {
0079         bip->bip_vec = bip->bip_inline_vecs;
0080         bip->bip_max_vcnt = inline_vecs;
0081     }
0082 
0083     bip->bip_bio = bio;
0084     bio->bi_integrity = bip;
0085     bio->bi_opf |= REQ_INTEGRITY;
0086 
0087     return bip;
0088 err:
0089     __bio_integrity_free(bs, bip);
0090     return ERR_PTR(-ENOMEM);
0091 }
0092 EXPORT_SYMBOL(bio_integrity_alloc);
0093 
0094 /**
0095  * bio_integrity_free - Free bio integrity payload
0096  * @bio:    bio containing bip to be freed
0097  *
0098  * Description: Used to free the integrity portion of a bio. Usually
0099  * called from bio_free().
0100  */
0101 void bio_integrity_free(struct bio *bio)
0102 {
0103     struct bio_integrity_payload *bip = bio_integrity(bio);
0104     struct bio_set *bs = bio->bi_pool;
0105 
0106     if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
0107         kfree(bvec_virt(bip->bip_vec));
0108 
0109     __bio_integrity_free(bs, bip);
0110     bio->bi_integrity = NULL;
0111     bio->bi_opf &= ~REQ_INTEGRITY;
0112 }
0113 
0114 /**
0115  * bio_integrity_add_page - Attach integrity metadata
0116  * @bio:    bio to update
0117  * @page:   page containing integrity metadata
0118  * @len:    number of bytes of integrity metadata in page
0119  * @offset: start offset within page
0120  *
0121  * Description: Attach a page containing integrity metadata to bio.
0122  */
0123 int bio_integrity_add_page(struct bio *bio, struct page *page,
0124                unsigned int len, unsigned int offset)
0125 {
0126     struct bio_integrity_payload *bip = bio_integrity(bio);
0127     struct bio_vec *iv;
0128 
0129     if (bip->bip_vcnt >= bip->bip_max_vcnt) {
0130         printk(KERN_ERR "%s: bip_vec full\n", __func__);
0131         return 0;
0132     }
0133 
0134     iv = bip->bip_vec + bip->bip_vcnt;
0135 
0136     if (bip->bip_vcnt &&
0137         bvec_gap_to_prev(&bdev_get_queue(bio->bi_bdev)->limits,
0138                  &bip->bip_vec[bip->bip_vcnt - 1], offset))
0139         return 0;
0140 
0141     iv->bv_page = page;
0142     iv->bv_len = len;
0143     iv->bv_offset = offset;
0144     bip->bip_vcnt++;
0145 
0146     return len;
0147 }
0148 EXPORT_SYMBOL(bio_integrity_add_page);
0149 
0150 /**
0151  * bio_integrity_process - Process integrity metadata for a bio
0152  * @bio:    bio to generate/verify integrity metadata for
0153  * @proc_iter:  iterator to process
0154  * @proc_fn:    Pointer to the relevant processing function
0155  */
0156 static blk_status_t bio_integrity_process(struct bio *bio,
0157         struct bvec_iter *proc_iter, integrity_processing_fn *proc_fn)
0158 {
0159     struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0160     struct blk_integrity_iter iter;
0161     struct bvec_iter bviter;
0162     struct bio_vec bv;
0163     struct bio_integrity_payload *bip = bio_integrity(bio);
0164     blk_status_t ret = BLK_STS_OK;
0165 
0166     iter.disk_name = bio->bi_bdev->bd_disk->disk_name;
0167     iter.interval = 1 << bi->interval_exp;
0168     iter.tuple_size = bi->tuple_size;
0169     iter.seed = proc_iter->bi_sector;
0170     iter.prot_buf = bvec_virt(bip->bip_vec);
0171 
0172     __bio_for_each_segment(bv, bio, bviter, *proc_iter) {
0173         void *kaddr = bvec_kmap_local(&bv);
0174 
0175         iter.data_buf = kaddr;
0176         iter.data_size = bv.bv_len;
0177         ret = proc_fn(&iter);
0178         kunmap_local(kaddr);
0179 
0180         if (ret)
0181             break;
0182 
0183     }
0184     return ret;
0185 }
0186 
0187 /**
0188  * bio_integrity_prep - Prepare bio for integrity I/O
0189  * @bio:    bio to prepare
0190  *
0191  * Description:  Checks if the bio already has an integrity payload attached.
0192  * If it does, the payload has been generated by another kernel subsystem,
0193  * and we just pass it through. Otherwise allocates integrity payload.
0194  * The bio must have data direction, target device and start sector set priot
0195  * to calling.  In the WRITE case, integrity metadata will be generated using
0196  * the block device's integrity function.  In the READ case, the buffer
0197  * will be prepared for DMA and a suitable end_io handler set up.
0198  */
0199 bool bio_integrity_prep(struct bio *bio)
0200 {
0201     struct bio_integrity_payload *bip;
0202     struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0203     void *buf;
0204     unsigned long start, end;
0205     unsigned int len, nr_pages;
0206     unsigned int bytes, offset, i;
0207     unsigned int intervals;
0208     blk_status_t status;
0209 
0210     if (!bi)
0211         return true;
0212 
0213     if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
0214         return true;
0215 
0216     if (!bio_sectors(bio))
0217         return true;
0218 
0219     /* Already protected? */
0220     if (bio_integrity(bio))
0221         return true;
0222 
0223     if (bio_data_dir(bio) == READ) {
0224         if (!bi->profile->verify_fn ||
0225             !(bi->flags & BLK_INTEGRITY_VERIFY))
0226             return true;
0227     } else {
0228         if (!bi->profile->generate_fn ||
0229             !(bi->flags & BLK_INTEGRITY_GENERATE))
0230             return true;
0231     }
0232     intervals = bio_integrity_intervals(bi, bio_sectors(bio));
0233 
0234     /* Allocate kernel buffer for protection data */
0235     len = intervals * bi->tuple_size;
0236     buf = kmalloc(len, GFP_NOIO);
0237     status = BLK_STS_RESOURCE;
0238     if (unlikely(buf == NULL)) {
0239         printk(KERN_ERR "could not allocate integrity buffer\n");
0240         goto err_end_io;
0241     }
0242 
0243     end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
0244     start = ((unsigned long) buf) >> PAGE_SHIFT;
0245     nr_pages = end - start;
0246 
0247     /* Allocate bio integrity payload and integrity vectors */
0248     bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
0249     if (IS_ERR(bip)) {
0250         printk(KERN_ERR "could not allocate data integrity bioset\n");
0251         kfree(buf);
0252         status = BLK_STS_RESOURCE;
0253         goto err_end_io;
0254     }
0255 
0256     bip->bip_flags |= BIP_BLOCK_INTEGRITY;
0257     bip->bip_iter.bi_size = len;
0258     bip_set_seed(bip, bio->bi_iter.bi_sector);
0259 
0260     if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
0261         bip->bip_flags |= BIP_IP_CHECKSUM;
0262 
0263     /* Map it */
0264     offset = offset_in_page(buf);
0265     for (i = 0 ; i < nr_pages ; i++) {
0266         int ret;
0267         bytes = PAGE_SIZE - offset;
0268 
0269         if (len <= 0)
0270             break;
0271 
0272         if (bytes > len)
0273             bytes = len;
0274 
0275         ret = bio_integrity_add_page(bio, virt_to_page(buf),
0276                          bytes, offset);
0277 
0278         if (ret == 0) {
0279             printk(KERN_ERR "could not attach integrity payload\n");
0280             status = BLK_STS_RESOURCE;
0281             goto err_end_io;
0282         }
0283 
0284         if (ret < bytes)
0285             break;
0286 
0287         buf += bytes;
0288         len -= bytes;
0289         offset = 0;
0290     }
0291 
0292     /* Auto-generate integrity metadata if this is a write */
0293     if (bio_data_dir(bio) == WRITE) {
0294         bio_integrity_process(bio, &bio->bi_iter,
0295                       bi->profile->generate_fn);
0296     } else {
0297         bip->bio_iter = bio->bi_iter;
0298     }
0299     return true;
0300 
0301 err_end_io:
0302     bio->bi_status = status;
0303     bio_endio(bio);
0304     return false;
0305 
0306 }
0307 EXPORT_SYMBOL(bio_integrity_prep);
0308 
0309 /**
0310  * bio_integrity_verify_fn - Integrity I/O completion worker
0311  * @work:   Work struct stored in bio to be verified
0312  *
0313  * Description: This workqueue function is called to complete a READ
0314  * request.  The function verifies the transferred integrity metadata
0315  * and then calls the original bio end_io function.
0316  */
0317 static void bio_integrity_verify_fn(struct work_struct *work)
0318 {
0319     struct bio_integrity_payload *bip =
0320         container_of(work, struct bio_integrity_payload, bip_work);
0321     struct bio *bio = bip->bip_bio;
0322     struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0323 
0324     /*
0325      * At the moment verify is called bio's iterator was advanced
0326      * during split and completion, we need to rewind iterator to
0327      * it's original position.
0328      */
0329     bio->bi_status = bio_integrity_process(bio, &bip->bio_iter,
0330                         bi->profile->verify_fn);
0331     bio_integrity_free(bio);
0332     bio_endio(bio);
0333 }
0334 
0335 /**
0336  * __bio_integrity_endio - Integrity I/O completion function
0337  * @bio:    Protected bio
0338  *
0339  * Description: Completion for integrity I/O
0340  *
0341  * Normally I/O completion is done in interrupt context.  However,
0342  * verifying I/O integrity is a time-consuming task which must be run
0343  * in process context.  This function postpones completion
0344  * accordingly.
0345  */
0346 bool __bio_integrity_endio(struct bio *bio)
0347 {
0348     struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0349     struct bio_integrity_payload *bip = bio_integrity(bio);
0350 
0351     if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
0352         (bip->bip_flags & BIP_BLOCK_INTEGRITY) && bi->profile->verify_fn) {
0353         INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
0354         queue_work(kintegrityd_wq, &bip->bip_work);
0355         return false;
0356     }
0357 
0358     bio_integrity_free(bio);
0359     return true;
0360 }
0361 
0362 /**
0363  * bio_integrity_advance - Advance integrity vector
0364  * @bio:    bio whose integrity vector to update
0365  * @bytes_done: number of data bytes that have been completed
0366  *
0367  * Description: This function calculates how many integrity bytes the
0368  * number of completed data bytes correspond to and advances the
0369  * integrity vector accordingly.
0370  */
0371 void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
0372 {
0373     struct bio_integrity_payload *bip = bio_integrity(bio);
0374     struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0375     unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
0376 
0377     bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
0378     bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
0379 }
0380 
0381 /**
0382  * bio_integrity_trim - Trim integrity vector
0383  * @bio:    bio whose integrity vector to update
0384  *
0385  * Description: Used to trim the integrity vector in a cloned bio.
0386  */
0387 void bio_integrity_trim(struct bio *bio)
0388 {
0389     struct bio_integrity_payload *bip = bio_integrity(bio);
0390     struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0391 
0392     bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
0393 }
0394 EXPORT_SYMBOL(bio_integrity_trim);
0395 
0396 /**
0397  * bio_integrity_clone - Callback for cloning bios with integrity metadata
0398  * @bio:    New bio
0399  * @bio_src:    Original bio
0400  * @gfp_mask:   Memory allocation mask
0401  *
0402  * Description: Called to allocate a bip when cloning a bio
0403  */
0404 int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
0405             gfp_t gfp_mask)
0406 {
0407     struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
0408     struct bio_integrity_payload *bip;
0409 
0410     BUG_ON(bip_src == NULL);
0411 
0412     bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
0413     if (IS_ERR(bip))
0414         return PTR_ERR(bip);
0415 
0416     memcpy(bip->bip_vec, bip_src->bip_vec,
0417            bip_src->bip_vcnt * sizeof(struct bio_vec));
0418 
0419     bip->bip_vcnt = bip_src->bip_vcnt;
0420     bip->bip_iter = bip_src->bip_iter;
0421 
0422     return 0;
0423 }
0424 
0425 int bioset_integrity_create(struct bio_set *bs, int pool_size)
0426 {
0427     if (mempool_initialized(&bs->bio_integrity_pool))
0428         return 0;
0429 
0430     if (mempool_init_slab_pool(&bs->bio_integrity_pool,
0431                    pool_size, bip_slab))
0432         return -1;
0433 
0434     if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
0435         mempool_exit(&bs->bio_integrity_pool);
0436         return -1;
0437     }
0438 
0439     return 0;
0440 }
0441 EXPORT_SYMBOL(bioset_integrity_create);
0442 
0443 void bioset_integrity_free(struct bio_set *bs)
0444 {
0445     mempool_exit(&bs->bio_integrity_pool);
0446     mempool_exit(&bs->bvec_integrity_pool);
0447 }
0448 
0449 void __init bio_integrity_init(void)
0450 {
0451     /*
0452      * kintegrityd won't block much but may burn a lot of CPU cycles.
0453      * Make it highpri CPU intensive wq with max concurrency of 1.
0454      */
0455     kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
0456                      WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
0457     if (!kintegrityd_wq)
0458         panic("Failed to create kintegrityd\n");
0459 
0460     bip_slab = kmem_cache_create("bio_integrity_payload",
0461                      sizeof(struct bio_integrity_payload) +
0462                      sizeof(struct bio_vec) * BIO_INLINE_VECS,
0463                      0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
0464 }