Under heavy concurrent read bursts, dm-verity workers blocking on
dm-bufio reads or locks can cause workqueue concurrency management to
spawn hundreds of per-CPU kverityd threads across the system.

Export verify_wq to sysfs via WQ_SYSFS so userspace can inspect and
configure workqueue attributes (such as max_active) per dm-verity
device under /sys/bus/workqueue/devices/kverityd-<dev>-<id>/.

Co-developed-by: Brian Geffon <[email protected]>
Signed-off-by: Brian Geffon <[email protected]>
Co-developed-by: David Stevens <[email protected]>
Signed-off-by: David Stevens <[email protected]>
Signed-off-by: Sarthak Kukreti <[email protected]>
---
 drivers/md/dm-verity-target.c | 22 ++++++++++++++++++----
 drivers/md/dm-verity.h        |  1 +
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index 306ed1f6c118..730adf931494 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -18,6 +18,7 @@
 #include "dm-verity-verify-sig.h"
 #include "dm-audit.h"
 #include <linux/hex.h>
+#include <linux/idr.h>
 #include <linux/module.h>
 #include <linux/reboot.h>
 #include <linux/string.h>
@@ -26,6 +27,8 @@
 
 #define DM_MSG_PREFIX                  "verity"
 
+static DEFINE_IDA(workqueue_ida);
+
 #define DM_VERITY_ENV_LENGTH           46
 #define DM_VERITY_ENV_VAR_NAME         "DM_VERITY_ERR_BLOCK_NR"
 
@@ -1065,6 +1068,8 @@ static void verity_dtr(struct dm_target *ti)
 
        if (v->verify_wq)
                destroy_workqueue(v->verify_wq);
+       if (v->workqueue_id)
+               ida_free(&workqueue_ida, v->workqueue_id);
 
        mempool_exit(&v->recheck_pool);
        if (v->io)
@@ -1421,7 +1426,7 @@ static int verity_ctr(struct dm_target *ti, unsigned int 
argc, char **argv)
        struct dm_arg_set as;
        unsigned int num;
        unsigned long long num_ll;
-       int r;
+       int r, wq_id;
        int i;
        sector_t hash_position;
        char dummy;
@@ -1637,6 +1642,14 @@ static int verity_ctr(struct dm_target *ti, unsigned int 
argc, char **argv)
                goto bad;
        }
 
+       wq_id = ida_alloc_min(&workqueue_ida, 1, GFP_KERNEL);
+       if (wq_id < 0) {
+               ti->error = "Cannot allocate workqueue id";
+               r = wq_id;
+               goto bad;
+       }
+       v->workqueue_id = wq_id;
+
        /*
         * Using WQ_HIGHPRI improves throughput and completion latency by
         * reducing wait times when reading from a dm-verity device.
@@ -1646,9 +1659,10 @@ static int verity_ctr(struct dm_target *ti, unsigned int 
argc, char **argv)
         * will fall-back to using it for error handling (or if the bufio cache
         * doesn't have required hashes).
         */
-       v->verify_wq = alloc_workqueue("kverityd",
-                                      WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_PERCPU,
-                                      0);
+       v->verify_wq = alloc_workqueue("kverityd-%s-%d",
+                                      WQ_MEM_RECLAIM | WQ_HIGHPRI |
+                                      WQ_PERCPU | WQ_SYSFS, 0,
+                                      dm_table_device_name(ti->table), wq_id);
        if (!v->verify_wq) {
                ti->error = "Cannot allocate workqueue";
                r = -ENOMEM;
diff --git a/drivers/md/dm-verity.h b/drivers/md/dm-verity.h
index e104a651c657..53f894f259d3 100644
--- a/drivers/md/dm-verity.h
+++ b/drivers/md/dm-verity.h
@@ -71,6 +71,7 @@ struct dm_verity {
        atomic_t corrupted_errs;/* Number of errors for corrupted blocks */
 
        struct workqueue_struct *verify_wq;
+       int workqueue_id;
 
        /* starting blocks for each tree level. 0 is the lowest level. */
        sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
-- 
2.56.0.679.gc527c65306-goog


Reply via email to