Skip to content

Commit d9a02e0

Browse files
author
Mike Snitzer
committed
dm crypt: avoid accessing uninitialized tasklet
When neither "no_read_workqueue" nor "no_write_workqueue" are enabled, tasklet_trylock() in crypt_dec_pending() may still return false due to an uninitialized state, and dm-crypt will unnecessarily do io completion in io_queue workqueue instead of current context. Fix this by adding an 'in_tasklet' flag to dm_crypt_io struct and initialize it to false in crypt_io_init(). Set this flag to true in kcryptd_queue_crypt() before calling tasklet_schedule(). If set crypt_dec_pending() will punt io completion to a workqueue. This also nicely avoids the tasklet_trylock/unlock hack when tasklets aren't in use. Fixes: 8e14f61 ("dm crypt: do not call bio_endio() from the dm-crypt tasklet") Cc: [email protected] Reported-by: Hou Tao <[email protected]> Suggested-by: Ignat Korchagin <[email protected]> Reviewed-by: Ignat Korchagin <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent fb294b1 commit d9a02e0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

drivers/md/dm-crypt.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ struct dm_crypt_io {
7272
struct crypt_config *cc;
7373
struct bio *base_bio;
7474
u8 *integrity_metadata;
75-
bool integrity_metadata_from_pool;
75+
bool integrity_metadata_from_pool:1;
76+
bool in_tasklet:1;
77+
7678
struct work_struct work;
7779
struct tasklet_struct tasklet;
7880

@@ -1731,6 +1733,7 @@ static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
17311733
io->ctx.r.req = NULL;
17321734
io->integrity_metadata = NULL;
17331735
io->integrity_metadata_from_pool = false;
1736+
io->in_tasklet = false;
17341737
atomic_set(&io->io_pending, 0);
17351738
}
17361739

@@ -1777,14 +1780,13 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
17771780
* our tasklet. In this case we need to delay bio_endio()
17781781
* execution to after the tasklet is done and dequeued.
17791782
*/
1780-
if (tasklet_trylock(&io->tasklet)) {
1781-
tasklet_unlock(&io->tasklet);
1782-
bio_endio(base_bio);
1783+
if (io->in_tasklet) {
1784+
INIT_WORK(&io->work, kcryptd_io_bio_endio);
1785+
queue_work(cc->io_queue, &io->work);
17831786
return;
17841787
}
17851788

1786-
INIT_WORK(&io->work, kcryptd_io_bio_endio);
1787-
queue_work(cc->io_queue, &io->work);
1789+
bio_endio(base_bio);
17881790
}
17891791

17901792
/*
@@ -2233,6 +2235,7 @@ static void kcryptd_queue_crypt(struct dm_crypt_io *io)
22332235
* it is being executed with irqs disabled.
22342236
*/
22352237
if (in_hardirq() || irqs_disabled()) {
2238+
io->in_tasklet = true;
22362239
tasklet_init(&io->tasklet, kcryptd_crypt_tasklet, (unsigned long)&io->work);
22372240
tasklet_schedule(&io->tasklet);
22382241
return;

0 commit comments

Comments
 (0)