Jonathan G. Underwood 5b6dfa
From 9cc6a94790eecfc808335b759355a4005d66f6e3 Mon Sep 17 00:00:00 2001
Jonathan G. Underwood 5b6dfa
From: "Jonathan G. Underwood" <jonathan.underwood@gmail.com>
Jonathan G. Underwood 5b6dfa
Date: Tue, 22 Dec 2020 20:04:52 +0000
Jonathan G. Underwood 5b6dfa
Subject: [PATCH] cryptsetup: add support for workqueue options
Jonathan G. Underwood 5b6dfa
Jonathan G. Underwood 5b6dfa
This commit adds support for disabling the read and write
Jonathan G. Underwood 5b6dfa
workqueues with the new crypttab options no-read-workqueue
Jonathan G. Underwood 5b6dfa
and no-write-workqueue. These correspond to the cryptsetup
Jonathan G. Underwood 5b6dfa
options --perf-no_read_workqueue and --perf-no_write_workqueue
Jonathan G. Underwood 5b6dfa
respectively.
Jonathan G. Underwood 5b6dfa
---
Jonathan G. Underwood 5b6dfa
 man/crypttab.xml             | 19 +++++++++++++++++++
Jonathan G. Underwood 5b6dfa
 src/cryptsetup/cryptsetup.c  | 12 ++++++++++++
Jonathan G. Underwood 5b6dfa
 src/shared/cryptsetup-util.h |  8 ++++++++
Jonathan G. Underwood 5b6dfa
 3 files changed, 39 insertions(+)
Jonathan G. Underwood 5b6dfa
Jonathan G. Underwood 5b6dfa
diff --git a/man/crypttab.xml b/man/crypttab.xml
Jonathan G. Underwood 5b6dfa
index 2062a5b8e70..72fe2e692da 100644
Jonathan G. Underwood 5b6dfa
--- a/man/crypttab.xml
Jonathan G. Underwood 5b6dfa
+++ b/man/crypttab.xml
Jonathan G. Underwood 5b6dfa
@@ -342,6 +342,25 @@
Jonathan G. Underwood 5b6dfa
         </listitem>
Jonathan G. Underwood 5b6dfa
       </varlistentry>
Jonathan G. Underwood 5b6dfa
 
Jonathan G. Underwood 5b6dfa
+      <varlistentry>
Jonathan G. Underwood 5b6dfa
+        <term><option>no-read-workqueue</option></term>
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
+        <listitem><para>Bypass dm-crypt internal workqueue and process read requests synchronously. The
Jonathan G. Underwood 5b6dfa
+        default is to queue these requests and process them asynchronously.</para>
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
+        <para>This requires kernel 5.9 or newer.</para>
Jonathan G. Underwood 5b6dfa
+        </listitem>
Jonathan G. Underwood 5b6dfa
+      </varlistentry>
Jonathan G. Underwood 5b6dfa
+      <varlistentry>
Jonathan G. Underwood 5b6dfa
+        <term><option>no-write-workqueue</option></term>
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
+        <listitem><para>Bypass dm-crypt internal workqueue and process write requests synchronously. The
Jonathan G. Underwood 5b6dfa
+        default is to queue these requests and process them asynchronously.</para>
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
+        <para>This requires kernel 5.9 or newer.</para>
Jonathan G. Underwood 5b6dfa
+        </listitem>
Jonathan G. Underwood 5b6dfa
+      </varlistentry>
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
       <varlistentry>
Jonathan G. Underwood 5b6dfa
         <term><option>skip=</option></term>
Jonathan G. Underwood 5b6dfa
 
Jonathan G. Underwood 5b6dfa
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
Jonathan G. Underwood 5b6dfa
index 7b21a7457a1..65cbd1aec83 100644
Jonathan G. Underwood 5b6dfa
--- a/src/cryptsetup/cryptsetup.c
Jonathan G. Underwood 5b6dfa
+++ b/src/cryptsetup/cryptsetup.c
Jonathan G. Underwood 5b6dfa
@@ -60,6 +60,8 @@ static bool arg_verify = false;
Jonathan G. Underwood 5b6dfa
 static bool arg_discards = false;
Jonathan G. Underwood 5b6dfa
 static bool arg_same_cpu_crypt = false;
Jonathan G. Underwood 5b6dfa
 static bool arg_submit_from_crypt_cpus = false;
Jonathan G. Underwood 5b6dfa
+static bool arg_no_read_workqueue = false;
Jonathan G. Underwood 5b6dfa
+static bool arg_no_write_workqueue = false;
Jonathan G. Underwood 5b6dfa
 static bool arg_tcrypt_hidden = false;
Jonathan G. Underwood 5b6dfa
 static bool arg_tcrypt_system = false;
Jonathan G. Underwood 5b6dfa
 static bool arg_tcrypt_veracrypt = false;
Jonathan G. Underwood 5b6dfa
@@ -236,6 +238,10 @@ static int parse_one_option(const char *option) {
Jonathan G. Underwood 5b6dfa
                 arg_same_cpu_crypt = true;
Jonathan G. Underwood 5b6dfa
         else if (streq(option, "submit-from-crypt-cpus"))
Jonathan G. Underwood 5b6dfa
                 arg_submit_from_crypt_cpus = true;
Jonathan G. Underwood 5b6dfa
+        else if (streq(option, "no-read-workqueue"))
Jonathan G. Underwood 5b6dfa
+                arg_no_read_workqueue = true;
Jonathan G. Underwood 5b6dfa
+        else if (streq(option, "no-write-workqueue"))
Jonathan G. Underwood 5b6dfa
+                arg_no_write_workqueue = true;
Jonathan G. Underwood 5b6dfa
         else if (streq(option, "luks"))
Jonathan G. Underwood 5b6dfa
                 arg_type = ANY_LUKS;
Jonathan G. Underwood 5b6dfa
 /* since cryptsetup 2.3.0 (Feb 2020) */
Jonathan G. Underwood 5b6dfa
@@ -1352,6 +1358,12 @@ static uint32_t determine_flags(void) {
Jonathan G. Underwood 5b6dfa
         if (arg_submit_from_crypt_cpus)
Jonathan G. Underwood 5b6dfa
                 flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
Jonathan G. Underwood 5b6dfa
 
Jonathan G. Underwood 5b6dfa
+        if (arg_no_read_workqueue)
Jonathan G. Underwood 5b6dfa
+                flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
+        if (arg_no_write_workqueue)
Jonathan G. Underwood 5b6dfa
+                flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
 #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF
Jonathan G. Underwood 5b6dfa
         /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
Jonathan G. Underwood 5b6dfa
         /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
Jonathan G. Underwood 5b6dfa
diff --git a/src/shared/cryptsetup-util.h b/src/shared/cryptsetup-util.h
Jonathan G. Underwood 5b6dfa
index fa2d2f65f3c..afac5cd46bd 100644
Jonathan G. Underwood 5b6dfa
--- a/src/shared/cryptsetup-util.h
Jonathan G. Underwood 5b6dfa
+++ b/src/shared/cryptsetup-util.h
Jonathan G. Underwood 5b6dfa
@@ -7,6 +7,14 @@
Jonathan G. Underwood 5b6dfa
 #if HAVE_LIBCRYPTSETUP
Jonathan G. Underwood 5b6dfa
 #include <libcryptsetup.h>
Jonathan G. Underwood 5b6dfa
 
Jonathan G. Underwood 5b6dfa
+/* These next two are defined in libcryptsetup.h from cryptsetup version 2.3.4 forwards. */
Jonathan G. Underwood 5b6dfa
+#ifndef CRYPT_ACTIVATE_NO_READ_WORKQUEUE
Jonathan G. Underwood 5b6dfa
+#define CRYPT_ACTIVATE_NO_READ_WORKQUEUE (1 << 24)
Jonathan G. Underwood 5b6dfa
+#endif
Jonathan G. Underwood 5b6dfa
+#ifndef CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE
Jonathan G. Underwood 5b6dfa
+#define CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE (1 << 25)
Jonathan G. Underwood 5b6dfa
+#endif
Jonathan G. Underwood 5b6dfa
+
Jonathan G. Underwood 5b6dfa
 extern int (*sym_crypt_activate_by_passphrase)(struct crypt_device *cd, const char *name, int keyslot, const char *passphrase, size_t passphrase_size, uint32_t flags);
Jonathan G. Underwood 5b6dfa
 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
Jonathan G. Underwood 5b6dfa
 extern int (*sym_crypt_activate_by_signed_key)(struct crypt_device *cd, const char *name, const char *volume_key, size_t volume_key_size, const char *signature, size_t signature_size, uint32_t flags);