Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / md / dm-crypt.c
index 5503e43..de62888 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2003 Jana Saout <jana@saout.de>
  * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
- * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2006-2015 Red Hat, Inc. All rights reserved.
  * Copyright (C) 2013 Milan Broz <gmazyland@gmail.com>
  *
  * This file is released under the GPL.
@@ -891,6 +891,11 @@ static void crypt_alloc_req(struct crypt_config *cc,
                ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
 
        ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
+
+       /*
+        * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
+        * requests if driver request queue is full.
+        */
        ablkcipher_request_set_callback(ctx->req,
            CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
            kcryptd_async_done, dmreq_of_req(cc, ctx->req));
@@ -924,24 +929,32 @@ static int crypt_convert(struct crypt_config *cc,
                r = crypt_convert_block(cc, ctx, ctx->req);
 
                switch (r) {
-               /* async */
+               /*
+                * The request was queued by a crypto driver
+                * but the driver request queue is full, let's wait.
+                */
                case -EBUSY:
                        wait_for_completion(&ctx->restart);
                        reinit_completion(&ctx->restart);
-                       /* fall through*/
+                       /* fall through */
+               /*
+                * The request is queued and processed asynchronously,
+                * completion function kcryptd_async_done() will be called.
+                */
                case -EINPROGRESS:
                        ctx->req = NULL;
                        ctx->cc_sector++;
                        continue;
-
-               /* sync */
+               /*
+                * The request was already processed (synchronously).
+                */
                case 0:
                        atomic_dec(&ctx->cc_pending);
                        ctx->cc_sector++;
                        cond_resched();
                        continue;
 
-               /* error */
+               /* There was an error while processing the request. */
                default:
                        atomic_dec(&ctx->cc_pending);
                        return r;
@@ -955,7 +968,8 @@ static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
 
 /*
  * Generate a new unfragmented bio with the given size
- * This should never violate the device limitations
+ * This should never violate the device limitations (but only because
+ * max_segment_size is being constrained to PAGE_SIZE).
  *
  * This function may be called concurrently. If we allocate from the mempool
  * concurrently, there is a possibility of deadlock. For example, if we have
@@ -980,7 +994,7 @@ static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
        struct bio_vec *bvec;
 
 retry:
-       if (unlikely(gfp_mask & __GFP_WAIT))
+       if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
                mutex_lock(&cc->bio_alloc_lock);
 
        clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
@@ -996,7 +1010,7 @@ retry:
                if (!page) {
                        crypt_free_buffer_pages(cc, clone);
                        bio_put(clone);
-                       gfp_mask |= __GFP_WAIT;
+                       gfp_mask |= __GFP_DIRECT_RECLAIM;
                        goto retry;
                }
 
@@ -1013,7 +1027,7 @@ retry:
        }
 
 return_clone:
-       if (unlikely(gfp_mask & __GFP_WAIT))
+       if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
                mutex_unlock(&cc->bio_alloc_lock);
 
        return clone;
@@ -1063,7 +1077,8 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
        if (io->ctx.req)
                crypt_free_req(cc, io->ctx.req, base_bio);
 
-       bio_endio(base_bio, error);
+       base_bio->bi_error = error;
+       bio_endio(base_bio);
 }
 
 /*
@@ -1083,14 +1098,12 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
  * The work is done per CPU global for all dm-crypt instances.
  * They should not depend on each other and do not block.
  */
-static void crypt_endio(struct bio *clone, int error)
+static void crypt_endio(struct bio *clone)
 {
        struct dm_crypt_io *io = clone->bi_private;
        struct crypt_config *cc = io->cc;
        unsigned rw = bio_data_dir(clone);
-
-       if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
-               error = -EIO;
+       int error;
 
        /*
         * free the processed pages
@@ -1098,6 +1111,7 @@ static void crypt_endio(struct bio *clone, int error)
        if (rw == WRITE)
                crypt_free_buffer_pages(cc, clone);
 
+       error = clone->bi_error;
        bio_put(clone);
 
        if (rw == READ && !error) {
@@ -1189,7 +1203,7 @@ continue_locked:
                if (!RB_EMPTY_ROOT(&cc->write_tree))
                        goto pop_from_list;
 
-               __set_current_state(TASK_INTERRUPTIBLE);
+               set_current_state(TASK_INTERRUPTIBLE);
                __add_wait_queue(&cc->write_thread_wait, &wait);
 
                spin_unlock_irq(&cc->write_thread_wait.lock);
@@ -1346,6 +1360,11 @@ static void kcryptd_async_done(struct crypto_async_request *async_req,
        struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
        struct crypt_config *cc = io->cc;
 
+       /*
+        * A request from crypto driver backlog is going to be processed now,
+        * finish the completion and continue in crypt_convert().
+        * (Callback will be called for the second time for this request.)
+        */
        if (error == -EINPROGRESS) {
                complete(&ctx->restart);
                return;
@@ -1481,12 +1500,15 @@ static int crypt_set_key(struct crypt_config *cc, char *key)
        if (!cc->key_size && strcmp(key, "-"))
                goto out;
 
+       /* clear the flag since following operations may invalidate previously valid key */
+       clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
+
        if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
                goto out;
 
-       set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
-
        r = crypt_setkey_allcpus(cc);
+       if (!r)
+               set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
 
 out:
        /* Hex key string not needed after here, so wipe it. */
@@ -1525,10 +1547,8 @@ static void crypt_dtr(struct dm_target *ti)
        if (cc->bs)
                bioset_free(cc->bs);
 
-       if (cc->page_pool)
-               mempool_destroy(cc->page_pool);
-       if (cc->req_pool)
-               mempool_destroy(cc->req_pool);
+       mempool_destroy(cc->page_pool);
+       mempool_destroy(cc->req_pool);
 
        if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
                cc->iv_gen_ops->dtr(cc);
@@ -1793,11 +1813,13 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
        }
        cc->iv_offset = tmpll;
 
-       if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
+       ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
+       if (ret) {
                ti->error = "Device lookup failed";
                goto bad;
        }
 
+       ret = -EINVAL;
        if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
                ti->error = "Invalid device sector";
                goto bad;
@@ -1897,6 +1919,13 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
                return DM_MAPIO_REMAPPED;
        }
 
+       /*
+        * Check if bio is too large, split as needed.
+        */
+       if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
+           bio_data_dir(bio) == WRITE)
+               dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
+
        io = dm_per_bio_data(bio, cc->per_bio_data_size);
        crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
        io->ctx.req = (struct ablkcipher_request *)(io + 1);
@@ -2017,21 +2046,6 @@ error:
        return -EINVAL;
 }
 
-static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
-                      struct bio_vec *biovec, int max_size)
-{
-       struct crypt_config *cc = ti->private;
-       struct request_queue *q = bdev_get_queue(cc->dev->bdev);
-
-       if (!q->merge_bvec_fn)
-               return max_size;
-
-       bvm->bi_bdev = cc->dev->bdev;
-       bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
-
-       return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
-}
-
 static int crypt_iterate_devices(struct dm_target *ti,
                                 iterate_devices_callout_fn fn, void *data)
 {
@@ -2040,9 +2054,20 @@ static int crypt_iterate_devices(struct dm_target *ti,
        return fn(ti, cc->dev, cc->start, ti->len, data);
 }
 
+static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
+{
+       /*
+        * Unfortunate constraint that is required to avoid the potential
+        * for exceeding underlying device's max_segments limits -- due to
+        * crypt_alloc_buffer() possibly allocating pages for the encryption
+        * bio that are not as physically contiguous as the original bio.
+        */
+       limits->max_segment_size = PAGE_SIZE;
+}
+
 static struct target_type crypt_target = {
        .name   = "crypt",
-       .version = {1, 14, 0},
+       .version = {1, 14, 1},
        .module = THIS_MODULE,
        .ctr    = crypt_ctr,
        .dtr    = crypt_dtr,
@@ -2052,8 +2077,8 @@ static struct target_type crypt_target = {
        .preresume = crypt_preresume,
        .resume = crypt_resume,
        .message = crypt_message,
-       .merge  = crypt_merge,
        .iterate_devices = crypt_iterate_devices,
+       .io_hints = crypt_io_hints,
 };
 
 static int __init dm_crypt_init(void)