Skip to content

Commit dc26762

Browse files
Mikulas Patockasnitm
authored andcommitted
dm crypt: offload writes to thread
Submitting write bios directly in the encryption thread caused serious performance degradation. On a multiprocessor machine, encryption requests finish in a different order than they were submitted. Consequently, write requests would be submitted in a different order and it could cause severe performance degradation. Move the submission of write requests to a separate thread so that the requests can be sorted before submitting. But this commit improves dm-crypt performance even without having dm-crypt perform request sorting (in particular it enables IO schedulers like CFQ to sort more effectively). Note: it is required that a previous commit ("dm crypt: don't allocate pages for a partial request") be applied before applying this patch. Otherwise, this commit could introduce a crash. Signed-off-by: Mikulas Patocka <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent 94f5e02 commit dc26762

File tree

1 file changed

+94
-20
lines changed

1 file changed

+94
-20
lines changed

drivers/md/dm-crypt.c

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/slab.h>
1919
#include <linux/crypto.h>
2020
#include <linux/workqueue.h>
21+
#include <linux/kthread.h>
2122
#include <linux/backing-dev.h>
2223
#include <linux/atomic.h>
2324
#include <linux/scatterlist.h>
@@ -58,6 +59,8 @@ struct dm_crypt_io {
5859
atomic_t io_pending;
5960
int error;
6061
sector_t sector;
62+
63+
struct list_head list;
6164
} CRYPTO_MINALIGN_ATTR;
6265

6366
struct dm_crypt_request {
@@ -128,6 +131,10 @@ struct crypt_config {
128131
struct workqueue_struct *io_queue;
129132
struct workqueue_struct *crypt_queue;
130133

134+
struct task_struct *write_thread;
135+
wait_queue_head_t write_thread_wait;
136+
struct list_head write_thread_list;
137+
131138
char *cipher;
132139
char *cipher_string;
133140

@@ -1136,37 +1143,89 @@ static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
11361143
return 0;
11371144
}
11381145

1146+
static void kcryptd_io_read_work(struct work_struct *work)
1147+
{
1148+
struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1149+
1150+
crypt_inc_pending(io);
1151+
if (kcryptd_io_read(io, GFP_NOIO))
1152+
io->error = -ENOMEM;
1153+
crypt_dec_pending(io);
1154+
}
1155+
1156+
static void kcryptd_queue_read(struct dm_crypt_io *io)
1157+
{
1158+
struct crypt_config *cc = io->cc;
1159+
1160+
INIT_WORK(&io->work, kcryptd_io_read_work);
1161+
queue_work(cc->io_queue, &io->work);
1162+
}
1163+
11391164
static void kcryptd_io_write(struct dm_crypt_io *io)
11401165
{
11411166
struct bio *clone = io->ctx.bio_out;
1167+
11421168
generic_make_request(clone);
11431169
}
11441170

1145-
static void kcryptd_io(struct work_struct *work)
1171+
static int dmcrypt_write(void *data)
11461172
{
1147-
struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1173+
struct crypt_config *cc = data;
1174+
while (1) {
1175+
struct list_head local_list;
1176+
struct blk_plug plug;
11481177

1149-
if (bio_data_dir(io->base_bio) == READ) {
1150-
crypt_inc_pending(io);
1151-
if (kcryptd_io_read(io, GFP_NOIO))
1152-
io->error = -ENOMEM;
1153-
crypt_dec_pending(io);
1154-
} else
1155-
kcryptd_io_write(io);
1156-
}
1178+
DECLARE_WAITQUEUE(wait, current);
11571179

1158-
static void kcryptd_queue_io(struct dm_crypt_io *io)
1159-
{
1160-
struct crypt_config *cc = io->cc;
1180+
spin_lock_irq(&cc->write_thread_wait.lock);
1181+
continue_locked:
11611182

1162-
INIT_WORK(&io->work, kcryptd_io);
1163-
queue_work(cc->io_queue, &io->work);
1183+
if (!list_empty(&cc->write_thread_list))
1184+
goto pop_from_list;
1185+
1186+
__set_current_state(TASK_INTERRUPTIBLE);
1187+
__add_wait_queue(&cc->write_thread_wait, &wait);
1188+
1189+
spin_unlock_irq(&cc->write_thread_wait.lock);
1190+
1191+
if (unlikely(kthread_should_stop())) {
1192+
set_task_state(current, TASK_RUNNING);
1193+
remove_wait_queue(&cc->write_thread_wait, &wait);
1194+
break;
1195+
}
1196+
1197+
schedule();
1198+
1199+
set_task_state(current, TASK_RUNNING);
1200+
spin_lock_irq(&cc->write_thread_wait.lock);
1201+
__remove_wait_queue(&cc->write_thread_wait, &wait);
1202+
goto continue_locked;
1203+
1204+
pop_from_list:
1205+
local_list = cc->write_thread_list;
1206+
local_list.next->prev = &local_list;
1207+
local_list.prev->next = &local_list;
1208+
INIT_LIST_HEAD(&cc->write_thread_list);
1209+
1210+
spin_unlock_irq(&cc->write_thread_wait.lock);
1211+
1212+
blk_start_plug(&plug);
1213+
do {
1214+
struct dm_crypt_io *io = container_of(local_list.next,
1215+
struct dm_crypt_io, list);
1216+
list_del(&io->list);
1217+
kcryptd_io_write(io);
1218+
} while (!list_empty(&local_list));
1219+
blk_finish_plug(&plug);
1220+
}
1221+
return 0;
11641222
}
11651223

11661224
static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
11671225
{
11681226
struct bio *clone = io->ctx.bio_out;
11691227
struct crypt_config *cc = io->cc;
1228+
unsigned long flags;
11701229

11711230
if (unlikely(io->error < 0)) {
11721231
crypt_free_buffer_pages(cc, clone);
@@ -1180,10 +1239,10 @@ static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
11801239

11811240
clone->bi_iter.bi_sector = cc->start + io->sector;
11821241

1183-
if (async)
1184-
kcryptd_queue_io(io);
1185-
else
1186-
generic_make_request(clone);
1242+
spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
1243+
list_add_tail(&io->list, &cc->write_thread_list);
1244+
wake_up_locked(&cc->write_thread_wait);
1245+
spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
11871246
}
11881247

11891248
static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
@@ -1426,6 +1485,9 @@ static void crypt_dtr(struct dm_target *ti)
14261485
if (!cc)
14271486
return;
14281487

1488+
if (cc->write_thread)
1489+
kthread_stop(cc->write_thread);
1490+
14291491
if (cc->io_queue)
14301492
destroy_workqueue(cc->io_queue);
14311493
if (cc->crypt_queue)
@@ -1764,6 +1826,18 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
17641826
goto bad;
17651827
}
17661828

1829+
init_waitqueue_head(&cc->write_thread_wait);
1830+
INIT_LIST_HEAD(&cc->write_thread_list);
1831+
1832+
cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
1833+
if (IS_ERR(cc->write_thread)) {
1834+
ret = PTR_ERR(cc->write_thread);
1835+
cc->write_thread = NULL;
1836+
ti->error = "Couldn't spawn write thread";
1837+
goto bad;
1838+
}
1839+
wake_up_process(cc->write_thread);
1840+
17671841
ti->num_flush_bios = 1;
17681842
ti->discard_zeroes_data_unsupported = true;
17691843

@@ -1798,7 +1872,7 @@ static int crypt_map(struct dm_target *ti, struct bio *bio)
17981872

17991873
if (bio_data_dir(io->base_bio) == READ) {
18001874
if (kcryptd_io_read(io, GFP_NOWAIT))
1801-
kcryptd_queue_io(io);
1875+
kcryptd_queue_read(io);
18021876
} else
18031877
kcryptd_queue_crypt(io);
18041878

0 commit comments

Comments
 (0)