dm-crypt: eboiv/elephant sleeps in softirq with no_read_workqueue
Hello,
I am reporting scheduling-while-atomic in dm-crypt IV generation. With no_read_workqueue, a read is decrypted in the bio completion softirq, and crypt_iv_eboiv_gen() allocates with GFP_NOIO.
Summary
=======
kcryptd_queue_crypt() calls kcryptd_crypt() inline when no_read_workqueue is set and the caller is not in hardirq and irqs are enabled. Softirq completion matches that test.
crypt_iv_eboiv_gen() does:
req = kmalloc(reqsize + cc->iv_size, GFP_NOIO); err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
GFP_NOIO can sleep in direct reclaim. crypt_iv_elephant_gen() calls crypt_iv_eboiv_gen() on every read.
On 6.18.65 the hardirq fallback queues that work to system_bh_wq, which also cannot sleep. A checkout at v7.1-rc5-6-ge8c2f9fdadee still has the GFP_NOIO allocation and the softirq inline.
Affected
========
Campaign hit on Linux 6.6.144 (KASAN), crash 70886c5a803fe30fe412ffc7b50c4ffa50b9a801, preempt_count 0x00000101:
BUG: scheduling while atomic: kworker/1:1H/69/0x00000101 Workqueue: kblockd blk_mq_run_work_fn schedule_timeout should_reclaim_retry kmalloc crypt_iv_eboiv_gen+0x1f1/0x780 drivers/md/dm-crypt.c:762 crypt_iv_elephant_gen drivers/md/dm-crypt.c:1038 kcryptd_queue_crypt crypt_endio
The same kmalloc(GFP_NOIO) is at drivers/md/dm-crypt.c:762 on 6.6.163 and at line 786 on 6.18.65.
Reproducer
==========
On Linux 6.6.163 #1 (KASAN, 2048 MB, 2 CPUs) the same warning printed from ksoftirqd while it was running BLOCK_SOFTIRQ:
BUG: scheduling while atomic: ksoftirqd/0/15/0x00000101 schedule_timeout __alloc_pages_slowpath+0x13c0 kmalloc crypt_iv_eboiv_gen+0x1ea/0x740 crypt_convert kcryptd_crypt_read_convert blk_complete_reqs run_ksoftirqd
That offset is schedule_timeout_uninterruptible() in __alloc_pages_may_oom() after mutex_trylock(&oom_lock) fails. The program loads aes-cbc-eboiv with no_read_workqueue on a loop device, mlocks 1100 MiB, and sets min_free_kbytes above MemFree so the eboiv slab allocation enters the slow path. The OOM killer ran in the same window and held oom_lock. The campaign hit is the other sleep in that slow path, should_reclaim_retry(), on kblockd. Both sleep from crypt_iv_eboiv_gen()'s GFP_NOIO allocation in softirq.
Yu Junzhe / FuzzAnything <[email protected]>