Commit d9a02e01 authored by Mike Snitzer's avatar Mike Snitzer

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: 8e14f610 ("dm crypt: do not call bio_endio() from the dm-crypt tasklet")
Cc: stable@vger.kernel.org
Reported-by: default avatarHou Tao <houtao1@huawei.com>
Suggested-by: default avatarIgnat Korchagin <ignat@cloudflare.com>
Reviewed-by: default avatarIgnat Korchagin <ignat@cloudflare.com>
Signed-off-by: default avatarMike Snitzer <snitzer@kernel.org>
parent fb294b1c
...@@ -72,7 +72,9 @@ struct dm_crypt_io { ...@@ -72,7 +72,9 @@ struct dm_crypt_io {
struct crypt_config *cc; struct crypt_config *cc;
struct bio *base_bio; struct bio *base_bio;
u8 *integrity_metadata; u8 *integrity_metadata;
bool integrity_metadata_from_pool; bool integrity_metadata_from_pool:1;
bool in_tasklet:1;
struct work_struct work; struct work_struct work;
struct tasklet_struct tasklet; struct tasklet_struct tasklet;
...@@ -1731,6 +1733,7 @@ static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, ...@@ -1731,6 +1733,7 @@ static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
io->ctx.r.req = NULL; io->ctx.r.req = NULL;
io->integrity_metadata = NULL; io->integrity_metadata = NULL;
io->integrity_metadata_from_pool = false; io->integrity_metadata_from_pool = false;
io->in_tasklet = false;
atomic_set(&io->io_pending, 0); atomic_set(&io->io_pending, 0);
} }
...@@ -1777,14 +1780,13 @@ static void crypt_dec_pending(struct dm_crypt_io *io) ...@@ -1777,14 +1780,13 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
* our tasklet. In this case we need to delay bio_endio() * our tasklet. In this case we need to delay bio_endio()
* execution to after the tasklet is done and dequeued. * execution to after the tasklet is done and dequeued.
*/ */
if (tasklet_trylock(&io->tasklet)) { if (io->in_tasklet) {
tasklet_unlock(&io->tasklet); INIT_WORK(&io->work, kcryptd_io_bio_endio);
bio_endio(base_bio); queue_work(cc->io_queue, &io->work);
return; return;
} }
INIT_WORK(&io->work, kcryptd_io_bio_endio); bio_endio(base_bio);
queue_work(cc->io_queue, &io->work);
} }
/* /*
...@@ -2233,6 +2235,7 @@ static void kcryptd_queue_crypt(struct dm_crypt_io *io) ...@@ -2233,6 +2235,7 @@ static void kcryptd_queue_crypt(struct dm_crypt_io *io)
* it is being executed with irqs disabled. * it is being executed with irqs disabled.
*/ */
if (in_hardirq() || irqs_disabled()) { if (in_hardirq() || irqs_disabled()) {
io->in_tasklet = true;
tasklet_init(&io->tasklet, kcryptd_crypt_tasklet, (unsigned long)&io->work); tasklet_init(&io->tasklet, kcryptd_crypt_tasklet, (unsigned long)&io->work);
tasklet_schedule(&io->tasklet); tasklet_schedule(&io->tasklet);
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment