dm verity: optimize verity_verify_io if FEC not configured
[linux-2.6-microblaze.git] / drivers / md / dm-verity-target.c
index d6dbd47..5a4ee32 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/reboot.h>
 #include <linux/scatterlist.h>
+#include <linux/jump_label.h>
 
 #define DM_MSG_PREFIX                  "verity"
 
@@ -34,6 +35,7 @@
 #define DM_VERITY_OPT_PANIC            "panic_on_corruption"
 #define DM_VERITY_OPT_IGN_ZEROES       "ignore_zero_blocks"
 #define DM_VERITY_OPT_AT_MOST_ONCE     "check_at_most_once"
+#define DM_VERITY_OPT_TASKLET_VERIFY   "try_verify_in_tasklet"
 
 #define DM_VERITY_OPTS_MAX             (3 + DM_VERITY_OPTS_FEC + \
                                         DM_VERITY_ROOT_HASH_VERIFICATION_OPTS)
@@ -42,6 +44,8 @@ static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
 
 module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
 
+static DEFINE_STATIC_KEY_FALSE(use_tasklet_enabled);
+
 struct dm_verity_prefetch_work {
        struct work_struct work;
        struct dm_verity *v;
@@ -220,7 +224,7 @@ static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
        struct mapped_device *md = dm_table_get_md(v->ti->table);
 
        /* Corruption should be visible in device status in all modes */
-       v->hash_failed = 1;
+       v->hash_failed = true;
 
        if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
                goto out;
@@ -286,7 +290,19 @@ static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
 
        verity_hash_at_level(v, block, level, &hash_block, &offset);
 
-       data = dm_bufio_read(v->bufio, hash_block, &buf);
+       if (static_branch_unlikely(&use_tasklet_enabled) && io->in_tasklet) {
+               data = dm_bufio_get(v->bufio, hash_block, &buf);
+               if (data == NULL) {
+                       /*
+                        * In tasklet and the hash was not in the bufio cache.
+                        * Return early and resume execution from a work-queue
+                        * to read the hash from disk.
+                        */
+                       return -EAGAIN;
+               }
+       } else
+               data = dm_bufio_read(v->bufio, hash_block, &buf);
+
        if (IS_ERR(data))
                return PTR_ERR(data);
 
@@ -307,6 +323,15 @@ static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
                if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
                                  v->digest_size) == 0))
                        aux->hash_verified = 1;
+               else if (static_branch_unlikely(&use_tasklet_enabled) &&
+                        io->in_tasklet) {
+                       /*
+                        * Error handling code (FEC included) cannot be run in a
+                        * tasklet since it may sleep, so fallback to work-queue.
+                        */
+                       r = -EAGAIN;
+                       goto release_ret_r;
+               }
                else if (verity_fec_decode(v, io,
                                           DM_VERITY_BLOCK_TYPE_METADATA,
                                           hash_block, data, NULL) == 0)
@@ -473,10 +498,17 @@ static int verity_verify_io(struct dm_verity_io *io)
 {
        bool is_zero;
        struct dm_verity *v = io->v;
+#if defined(CONFIG_DM_VERITY_FEC)
        struct bvec_iter start;
-       unsigned b;
+#endif
+       /*
+        * Copy the iterator in case we need to restart verification in a
+        * work-queue.
+        */
+       struct bvec_iter iter_copy = io->iter;
        struct crypto_wait wait;
        struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
+       unsigned int b;
 
        for (b = 0; b < io->n_blocks; b++) {
                int r;
@@ -485,7 +517,7 @@ static int verity_verify_io(struct dm_verity_io *io)
 
                if (v->validated_blocks &&
                    likely(test_bit(cur_block, v->validated_blocks))) {
-                       verity_bv_skip_block(v, io, &io->iter);
+                       verity_bv_skip_block(v, io, &iter_copy);
                        continue;
                }
 
@@ -500,7 +532,7 @@ static int verity_verify_io(struct dm_verity_io *io)
                         * If we expect a zero block, don't validate, just
                         * return zeros.
                         */
-                       r = verity_for_bv_block(v, io, &io->iter,
+                       r = verity_for_bv_block(v, io, &iter_copy,
                                                verity_bv_zero);
                        if (unlikely(r < 0))
                                return r;
@@ -512,8 +544,11 @@ static int verity_verify_io(struct dm_verity_io *io)
                if (unlikely(r < 0))
                        return r;
 
-               start = io->iter;
-               r = verity_for_io_block(v, io, &io->iter, &wait);
+#if defined(CONFIG_DM_VERITY_FEC)
+               if (verity_fec_is_enabled(v))
+                       start = iter_copy;
+#endif
+               r = verity_for_io_block(v, io, &iter_copy, &wait);
                if (unlikely(r < 0))
                        return r;
 
@@ -527,11 +562,19 @@ static int verity_verify_io(struct dm_verity_io *io)
                        if (v->validated_blocks)
                                set_bit(cur_block, v->validated_blocks);
                        continue;
-               }
-               else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
-                                          cur_block, NULL, &start) == 0)
+               } else if (static_branch_unlikely(&use_tasklet_enabled) &&
+                          io->in_tasklet) {
+                       /*
+                        * Error handling code (FEC included) cannot be run in a
+                        * tasklet since it may sleep, so fallback to work-queue.
+                        */
+                       return -EAGAIN;
+#if defined(CONFIG_DM_VERITY_FEC)
+               } else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
+                                            cur_block, NULL, &start) == 0) {
                        continue;
-               else {
+#endif
+               } else {
                        if (bio->bi_status) {
                                /*
                                 * Error correction failed; Just return error
@@ -567,7 +610,8 @@ static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
        bio->bi_end_io = io->orig_bi_end_io;
        bio->bi_status = status;
 
-       verity_fec_finish_io(io);
+       if (!static_branch_unlikely(&use_tasklet_enabled) || !io->in_tasklet)
+               verity_fec_finish_io(io);
 
        bio_endio(bio);
 }
@@ -576,9 +620,29 @@ static void verity_work(struct work_struct *w)
 {
        struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
 
+       io->in_tasklet = false;
+
+       verity_fec_init_io(io);
        verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
 }
 
+static void verity_tasklet(unsigned long data)
+{
+       struct dm_verity_io *io = (struct dm_verity_io *)data;
+       int err;
+
+       io->in_tasklet = true;
+       err = verity_verify_io(io);
+       if (err == -EAGAIN) {
+               /* fallback to retrying with work-queue */
+               INIT_WORK(&io->work, verity_work);
+               queue_work(io->v->verify_wq, &io->work);
+               return;
+       }
+
+       verity_finish_io(io, errno_to_blk_status(err));
+}
+
 static void verity_end_io(struct bio *bio)
 {
        struct dm_verity_io *io = bio->bi_private;
@@ -589,8 +653,13 @@ static void verity_end_io(struct bio *bio)
                return;
        }
 
-       INIT_WORK(&io->work, verity_work);
-       queue_work(io->v->verify_wq, &io->work);
+       if (static_branch_unlikely(&use_tasklet_enabled) && io->v->use_tasklet) {
+               tasklet_init(&io->tasklet, verity_tasklet, (unsigned long)io);
+               tasklet_schedule(&io->tasklet);
+       } else {
+               INIT_WORK(&io->work, verity_work);
+               queue_work(io->v->verify_wq, &io->work);
+       }
 }
 
 /*
@@ -701,8 +770,6 @@ static int verity_map(struct dm_target *ti, struct bio *bio)
        bio->bi_private = io;
        io->iter = bio->bi_iter;
 
-       verity_fec_init_io(io);
-
        verity_submit_prefetch(v, io);
 
        submit_bio_noacct(bio);
@@ -752,6 +819,8 @@ static void verity_status(struct dm_target *ti, status_type_t type,
                        args++;
                if (v->validated_blocks)
                        args++;
+               if (v->use_tasklet)
+                       args++;
                if (v->signature_key_desc)
                        args += DM_VERITY_ROOT_HASH_VERIFICATION_OPTS;
                if (!args)
@@ -777,6 +846,8 @@ static void verity_status(struct dm_target *ti, status_type_t type,
                        DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
                if (v->validated_blocks)
                        DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
+               if (v->use_tasklet)
+                       DMEMIT(" " DM_VERITY_OPT_TASKLET_VERIFY);
                sz = verity_fec_status_table(v, sz, result, maxlen);
                if (v->signature_key_desc)
                        DMEMIT(" " DM_VERITY_ROOT_HASH_VERIFICATION_OPT_SIG_KEY
@@ -890,6 +961,9 @@ static void verity_dtr(struct dm_target *ti)
 
        kfree(v->signature_key_desc);
 
+       if (v->use_tasklet)
+               static_branch_dec(&use_tasklet_enabled);
+
        kfree(v);
 }
 
@@ -968,7 +1042,8 @@ static int verity_parse_verity_mode(struct dm_verity *v, const char *arg_name)
 }
 
 static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
-                                struct dm_verity_sig_opts *verify_args)
+                                struct dm_verity_sig_opts *verify_args,
+                                bool only_modifier_opts)
 {
        int r;
        unsigned argc;
@@ -991,6 +1066,8 @@ static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
                argc--;
 
                if (verity_is_verity_mode(arg_name)) {
+                       if (only_modifier_opts)
+                               continue;
                        r = verity_parse_verity_mode(v, arg_name);
                        if (r) {
                                ti->error = "Conflicting error handling parameters";
@@ -999,6 +1076,8 @@ static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
                        continue;
 
                } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
+                       if (only_modifier_opts)
+                               continue;
                        r = verity_alloc_zero_digest(v);
                        if (r) {
                                ti->error = "Cannot allocate zero digest";
@@ -1007,24 +1086,35 @@ static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
                        continue;
 
                } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
+                       if (only_modifier_opts)
+                               continue;
                        r = verity_alloc_most_once(v);
                        if (r)
                                return r;
                        continue;
 
+               } else if (!strcasecmp(arg_name, DM_VERITY_OPT_TASKLET_VERIFY)) {
+                       v->use_tasklet = true;
+                       static_branch_inc(&use_tasklet_enabled);
+                       continue;
+
                } else if (verity_is_fec_opt_arg(arg_name)) {
+                       if (only_modifier_opts)
+                               continue;
                        r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
                        if (r)
                                return r;
                        continue;
+
                } else if (verity_verify_is_sig_opt_arg(arg_name)) {
+                       if (only_modifier_opts)
+                               continue;
                        r = verity_verify_sig_parse_opt_args(as, v,
                                                             verify_args,
                                                             &argc, arg_name);
                        if (r)
                                return r;
                        continue;
-
                }
 
                ti->error = "Unrecognized verity feature request";
@@ -1085,6 +1175,15 @@ static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
                goto bad;
        }
 
+       /* Parse optional parameters that modify primary args */
+       if (argc > 10) {
+               as.argc = argc - 10;
+               as.argv = argv + 10;
+               r = verity_parse_opt_args(&as, v, &verify_args, true);
+               if (r < 0)
+                       goto bad;
+       }
+
        if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
            num > 1) {
                ti->error = "Invalid version";
@@ -1156,7 +1255,8 @@ static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
                goto bad;
        }
 
-       v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
+       v->tfm = crypto_alloc_ahash(v->alg_name, 0,
+                                   v->use_tasklet ? CRYPTO_ALG_ASYNC : 0);
        if (IS_ERR(v->tfm)) {
                ti->error = "Cannot initialize hash function";
                r = PTR_ERR(v->tfm);
@@ -1218,8 +1318,7 @@ static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
        if (argc) {
                as.argc = argc;
                as.argv = argv;
-
-               r = verity_parse_opt_args(&as, v, &verify_args);
+               r = verity_parse_opt_args(&as, v, &verify_args, false);
                if (r < 0)
                        goto bad;
        }
@@ -1266,7 +1365,8 @@ static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
 
        v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
                1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
-               dm_bufio_alloc_callback, NULL);
+               dm_bufio_alloc_callback, NULL,
+               v->use_tasklet ? DM_BUFIO_CLIENT_NO_SLEEP : 0);
        if (IS_ERR(v->bufio)) {
                ti->error = "Cannot initialize dm-bufio";
                r = PTR_ERR(v->bufio);
@@ -1313,7 +1413,7 @@ bad:
 static struct target_type verity_target = {
        .name           = "verity",
        .features       = DM_TARGET_IMMUTABLE,
-       .version        = {1, 8, 0},
+       .version        = {1, 9, 0},
        .module         = THIS_MODULE,
        .ctr            = verity_ctr,
        .dtr            = verity_dtr,