From b1310cb291935c788f34ed6eb1e7e9b42cfac045 Mon Sep 17 00:00:00 2001 From: guolingxing Date: Wed, 14 Oct 2026 03:11:50 +0000 Subject: [PATCH 01/34] dm-pcache: do not advance dirty_tail after backing flush failure After all writeback bios for a kset complete, writeback_ctx_end() flushes the backing device and then advances the persistent dirty_tail. However, backing_dev_flush() discards the error returned by blkdev_issue_flush(), so dirty_tail is advanced even when the written data is not known to be durable. dirty_tail points to the first kset that still requires writeback. Advancing it causes recovery to treat the preceding kset as clean and allows its cache copy to eventually be reclaimed by garbage collection. If the backing device flush failed, a subsequent power failure may therefore result in data loss. Make backing_dev_flush() return the flush status and handle a flush failure like a writeback I/O failure. Leave dirty_tail unchanged so that the same kset will be retried. Fixes: 1d57628ff95b ("dm-pcache: add persistent cache target in device-mapper") Cc: stable@vger.kernel.org Signed-off-by: guolingxing Signed-off-by: Mikulas Patocka Reviewed-by: Dongsheng Yang --- drivers/md/dm-pcache/backing_dev.c | 4 ++-- drivers/md/dm-pcache/backing_dev.h | 2 +- drivers/md/dm-pcache/cache_writeback.c | 28 +++++++++++++++++++------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/drivers/md/dm-pcache/backing_dev.c b/drivers/md/dm-pcache/backing_dev.c index 23002beb192a..84b4e76367fa 100644 --- a/drivers/md/dm-pcache/backing_dev.c +++ b/drivers/md/dm-pcache/backing_dev.c @@ -336,9 +336,9 @@ struct pcache_backing_dev_req *backing_dev_req_create(struct pcache_backing_dev return backing_req; } -void backing_dev_flush(struct pcache_backing_dev *backing_dev) +int backing_dev_flush(struct pcache_backing_dev *backing_dev) { - blkdev_issue_flush(backing_dev->dm_dev->bdev); + return blkdev_issue_flush(backing_dev->dm_dev->bdev); } int pcache_backing_init(void) diff --git a/drivers/md/dm-pcache/backing_dev.h b/drivers/md/dm-pcache/backing_dev.h index b371cba483b9..8422e5e76e29 100644 --- a/drivers/md/dm-pcache/backing_dev.h +++ b/drivers/md/dm-pcache/backing_dev.h @@ -120,7 +120,7 @@ struct pcache_backing_dev_req *backing_dev_req_alloc(struct pcache_backing_dev * struct pcache_backing_dev_req_opts *opts); void backing_dev_req_init(struct pcache_backing_dev_req *backing_req, struct pcache_backing_dev_req_opts *opts); -void backing_dev_flush(struct pcache_backing_dev *backing_dev); +int backing_dev_flush(struct pcache_backing_dev *backing_dev); int pcache_backing_init(void); void pcache_backing_exit(void); diff --git a/drivers/md/dm-pcache/cache_writeback.c b/drivers/md/dm-pcache/cache_writeback.c index c8a4c8110a58..e09560f172dd 100644 --- a/drivers/md/dm-pcache/cache_writeback.c +++ b/drivers/md/dm-pcache/cache_writeback.c @@ -18,12 +18,18 @@ static void writeback_ctx_end(struct pcache_cache *cache, int ret) return; if (!cache->writeback_ctx.ret) { - backing_dev_flush(cache->backing_dev); - - mutex_lock(&cache->dirty_tail_lock); - cache_pos_advance(&cache->dirty_tail, cache->writeback_ctx.advance); - cache_encode_dirty_tail(cache); - mutex_unlock(&cache->dirty_tail_lock); + ret = backing_dev_flush(cache->backing_dev); + if (ret) { + pcache_dev_err(CACHE_TO_PCACHE(cache), + "failed to flush backing device: %d", ret); + cache->writeback_ctx.ret = ret; + } else { + mutex_lock(&cache->dirty_tail_lock); + cache_pos_advance(&cache->dirty_tail, + cache->writeback_ctx.advance); + cache_encode_dirty_tail(cache); + mutex_unlock(&cache->dirty_tail_lock); + } } queue_delayed_work(cache_get_wq(cache), &cache->writeback_work, 0); } @@ -76,8 +82,16 @@ static inline bool is_cache_clean(struct pcache_cache *cache, struct pcache_cach void cache_writeback_exit(struct pcache_cache *cache) { + int ret; + cancel_delayed_work_sync(&cache->writeback_work); - backing_dev_flush(cache->backing_dev); + + ret = backing_dev_flush(cache->backing_dev); + if (ret) + pcache_dev_err(CACHE_TO_PCACHE(cache), + "failed to flush backing device during exit: %d", + ret); + cache_tree_exit(&cache->writeback_key_tree); } From 648320de920b30ae1730bbf0e8b86c0df0fb490a Mon Sep 17 00:00:00 2001 From: Mark Bundschuh Date: Mon, 19 Oct 2026 19:28:19 +0000 Subject: [PATCH 02/34] dm: fix infinite loop in dm_blk_do_report_zones() If the device underlying a zoned target is removed, its capacity is set to 0 and blkdev_report_zones() returns 0 without reporting any zones. The mapped device keeps its old capacity, so args->zone_idx and args->next_sector never advance and dm_blk_do_report_zones() loops forever. On non-preemptible kernels this causes soft lockups and RCU stalls. The following sequence of commands triggers the hang: modprobe scsi_debug zbc=managed dev_size_mb=1024 zone_size_mb=64 udevadm settle echo '0 2097152 linear /dev/sda 0' | dmsetup create lin echo 1 > /sys/block/sda/device/delete blkzone report -o 0 -c 1 /dev/mapper/lin Fix this by returning -EIO when a target reports no zones. The target always maps args->next_sector, so a zero return means the underlying device no longer covers that sector. Fixes: d41003513e61 ("block: rework zone reporting") Cc: stable@vger.kernel.org Signed-off-by: Mark Bundschuh Signed-off-by: Mikulas Patocka Reviewed-by: Damien Le Moal --- drivers/md/dm-zone.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/md/dm-zone.c b/drivers/md/dm-zone.c index 35916c6e490e..9ddbb67e97e5 100644 --- a/drivers/md/dm-zone.c +++ b/drivers/md/dm-zone.c @@ -33,6 +33,8 @@ static int dm_blk_do_report_zones(struct mapped_device *md, struct dm_table *t, nr_zones - args->zone_idx); if (ret < 0) return ret; + if (!ret) + return -EIO; } while (args->zone_idx < nr_zones && args->next_sector < get_capacity(md->disk)); From 11b5cd14c0eecae458cecbe53d21a3dbc10190f5 Mon Sep 17 00:00:00 2001 From: Jay Vadayath Date: Thu, 22 Oct 2026 11:19:34 -0700 Subject: [PATCH 03/34] dm-raid: bound array_position before indexing failed_devices super_init_validation() reads role = le32_to_cpu(sb2->array_position) out of an on-disk dm_raid_superblock and then uses it as a bit index into the failed_devices bitmap via test_bit(role, (void *)failed_devices). failed_devices is a uint64_t[DISKS_ARRAY_ELEMS] (DISKS_ARRAY_ELEMS == 4, i.e. 256 bits) living on the stack. The only guard is role < 0, so any on-disk array_position >= 256 makes test_bit() read past the end of the stack array (array_position == 256 reads failed_devices[4], 8 bytes past the object). For a raid1 set the role != raid_disk reorder branch falls through to the same test_bit(). A crafted dm-raid superblock therefore causes a stack out-of-bounds read during table load. Reachable by anyone able to issue a dmsetup create with attacker-controlled metadata (CAP_SYS_ADMIN in the target namespace); the crash fires in the constructor / analyse_superblocks path, before any md_run or array sizing. BUG: KASAN: stack-out-of-bounds in super_validate+0x1cd0/0x3390 Read of size 8 at addr ffff888007b17828 by task poc/152 Call Trace: dump_stack_lvl+0x64/0x80 print_report+0xd0/0x630 kasan_report+0xe5/0x120 kasan_check_range+0x105/0x1b0 super_validate+0x1cd0/0x3390 (super_init_validation, inlined) raid_ctr+0x2c4b/0xa0f0 dm_table_add_target+0x66f/0xf80 table_load+0x2ee/0x780 ctl_ioctl+0x362/0x9f0 dm_ctl_ioctl+0x9/0x10 __x64_sys_ioctl+0x134/0x1c0 do_syscall_64+0xe2/0x4b0 entry_SYSCALL_64_after_hwframe+0x77/0x7f role is a device position that must refer to an actual member of the array, so reject anything outside [0, rs->raid_disks) instead of only the negative case. rs->raid_disks is always <= the 256-bit capacity of failed_devices, so this both closes the out-of-bounds access and matches the intended semantics. This bug was discovered by Artiphishell's vTriage pipeline, which generated a crafted raid1 superblock (magic "DmRd", compat_features=1, num_devices=2, array_position=256, level=1) and a dmsetup-create reproducer that reliably triggers the KASAN report on an unpatched kernel (the precursor DMINFO "raid device #256 now at position #0" is visible in the serial log just before the splat). The fix below was drafted with the Claude coding assistant; a userspace reproducer is available on request. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Jay Vadayath Signed-off-by: Mikulas Patocka Cc: stable@vger.kernel.org --- drivers/md/dm-raid.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-raid.c b/drivers/md/dm-raid.c index ac019a0f5a92..b973181bdce2 100644 --- a/drivers/md/dm-raid.c +++ b/drivers/md/dm-raid.c @@ -2420,7 +2420,8 @@ static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev) */ if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) { role = le32_to_cpu(sb2->array_position); - if (role < 0) + /* Bound @role before it indexes the failed_devices stack bitmap. */ + if (role < 0 || role >= rs->raid_disks) continue; if (role != r->raid_disk) { From 8b97b168eb4d3affcf23d2980a0c3f35ed9262e2 Mon Sep 17 00:00:00 2001 From: Edward Adam Davis Date: Mon, 26 Oct 2026 12:21:37 +0800 Subject: [PATCH 04/34] dm cache: reject kcopyd client destory if it setup failed The value of cache->copier is -EINTR when failed to create a rescuer kthread for wq kcopyd, in cache_create(), the code jumps directly to __destroy(cache), it merely performs a simple check to see whether copier is NULL, potentially hitting: BUG: KASAN: null-ptr-deref in dm_kcopyd_client_destroy+0xae/0x480 drivers/md/dm-kcopyd.c:980 Read of size 4 at addr 0000000000000174 by task syz.2.19/6560 Call Trace: dm_kcopyd_client_destroy+0xae/0x480 drivers/md/dm-kcopyd.c:980 __destroy+0x172/0x2f0 drivers/md/dm-cache-target.c:1973 cache_create drivers/md/dm-cache-target.c:2604 [inline] cache_ctr+0x1674/0x2bf0 drivers/md/dm-cache-target.c:2649 dm_table_add_target+0x58d/0xc70 drivers/md/dm-table.c:749 populate_table drivers/md/dm-ioctl.c:1602 [inline] table_load+0x325/0x8a0 drivers/md/dm-ioctl.c:1645 ctl_ioctl drivers/md/dm-ioctl.c:2221 [inline] Set copier to NULL to avoid destroying a invalid kcopy client. Cc: stable@vger.kernel.org Fixes: c6b4fcbad044 ("dm: add cache target") Reported-by: syzbot+a2626a25eb2595e3ec3c@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=a2626a25eb2595e3ec3c Signed-off-by: Edward Adam Davis Signed-off-by: Mikulas Patocka --- drivers/md/dm-cache-target.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c index 1a5072425c4a..b562bf59124c 100644 --- a/drivers/md/dm-cache-target.c +++ b/drivers/md/dm-cache-target.c @@ -2544,6 +2544,7 @@ static int cache_create(struct cache_args *ca, struct cache **result) if (IS_ERR(cache->copier)) { *error = "could not create kcopyd client"; r = PTR_ERR(cache->copier); + cache->copier = NULL; goto bad; } From d7216d7be2bbfdf3312695b6e41a4fc7c5e22902 Mon Sep 17 00:00:00 2001 From: wuyuehong Date: Mon, 26 Oct 2026 17:35:00 +0800 Subject: [PATCH 05/34] dm snapshot: fix discard_granularity unit in snapshot_io_hints snapshot_io_hints() copies the snapshot store's chunk_size, which is in 512-byte sectors, into queue_limits.discard_granularity, which, like all other members of struct queue_limits, is in bytes. For snapshots with a chunk size smaller than 512 sectors, e.g. the 16 KiB (32-sector) chunks created by the syzkaller reproducer, the resulting granularity is smaller than a single sector. When dm_calculate_queue_limits() then stacks the target limits into the table limits: blk_stack_limits(limits, &ti_limits, 0) queue_limit_discard_alignment() truncates that granularity to zero sectors and sector_div() divides by zero: Oops: divide error: 0000 [#1] SMP KASAN NOPTI RIP: 0010:queue_limit_discard_alignment block/blk-settings.c:645 [inline] RIP: 0010:blk_stack_limits+0x1105/0x2d00 block/blk-settings.c:916 Call Trace: dm_calculate_queue_limits+0x70c/0x1510 drivers/md/dm-table.c:1823 dm_setup_md_queue+0x199/0x4f0 drivers/md/dm.c:2618 table_load+0x4fd/0x8a0 drivers/md/dm-ioctl.c:1661 The divide fault itself only became reachable when commit e924da7d6622 ("block: Drop granularity check in queue_limit_discard_alignment()") removed the check for a sub-sector granularity in v6.13. Kernels before that returned a zero discard alignment instead of faulting, but they still advertised a granularity 512 times too small. Use the same chunk_size << SECTOR_SHIFT conversion that io_overlaps_chunk() already applies to turn chunk_size into bytes. Fixes: 2e6023850e17 ("dm snapshot: add optional discard support features") Reported-by: syzbot+1288484b13bf6af3620d@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=1288484b13bf6af3620d Cc: stable@vger.kernel.org Signed-off-by: wuyuehong Signed-off-by: Mikulas Patocka --- drivers/md/dm-snap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c index 797c432594d8..31676ee05514 100644 --- a/drivers/md/dm-snap.c +++ b/drivers/md/dm-snap.c @@ -2414,7 +2414,7 @@ static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits) snap = snap_src; /* All discards are split on chunk_size boundary */ - limits->discard_granularity = snap->store->chunk_size; + limits->discard_granularity = snap->store->chunk_size << SECTOR_SHIFT; limits->max_hw_discard_sectors = snap->store->chunk_size; up_read(&_origins_lock); From 4fe2c2baad01da64d47f1a0c8fc093df994bcef5 Mon Sep 17 00:00:00 2001 From: Chen Cheng Date: Fri, 30 Oct 2026 17:21:28 +0800 Subject: [PATCH 06/34] dm snapshot: split discards on chunk boundaries dm-snapshot did not set ti->max_discard_granularity when it allows discards, so a discard could reach the target unaligned, or spanning several chunks. Set the flag, so dm splits discards on chunk boundaries. Fixes: 2e6023850e17 ("dm snapshot: add optional discard support features") Cc: stable@vger.kernel.org Signed-off-by: Chen Cheng Signed-off-by: Mikulas Patocka --- drivers/md/dm-snap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c index 31676ee05514..e49427bdbeef 100644 --- a/drivers/md/dm-snap.c +++ b/drivers/md/dm-snap.c @@ -1358,8 +1358,10 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) ti->private = s; ti->num_flush_bios = num_flush_bios; - if (s->discard_zeroes_cow) + if (s->discard_zeroes_cow) { ti->num_discard_bios = (s->discard_passdown_origin ? 2 : 1); + ti->max_discard_granularity = true; + } ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk); /* Add snapshot to the list of snapshots for this origin */ From 73174b4bb6fcd0e6cf973c9402a0fae2e567ad9f Mon Sep 17 00:00:00 2001 From: Chen Cheng Date: Fri, 30 Oct 2026 17:21:29 +0800 Subject: [PATCH 07/34] dm snapshot: fix discard writing into the next COW chunk zero_exception() zeroes one chunk of the COW device and takes the start sector from bio->bi_iter.bi_sector. remap_exception() has already set that to the COW chunk start plus the offset inside the chunk. That offset is needed for reads and writes, but for a discard it is wrong: the zeroing window is shifted, so the head of the target chunk keeps its old data and the tail lands in the next COW chunk, which holds a different exception. io_overlaps_chunk() should have caught this, but it only compares the length. "maps to entire chunks" in snapshot.rst also means the discard has to start on a chunk boundary. A discard that is not a full chunk has a second problem: after remap_exception() it is sent to the COW device, where it hits a chunk that still backs a live exception. After a discard the contents are undefined, but the exception stays in the table and keeps pointing at that chunk, so a later read returns whatever the discard left instead of the snapshot's data. Reproducer: truncate -s 4M o.img c.img o=$(losetup -f --show o.img) c=$(losetup -f --show c.img) dmsetup create snap --table \ "0 8192 snapshot $o $c P 512 1 discard_zeroes_cow" # write two chunks, this creates two exceptions head -c 256K /dev/zero | tr '\000' '\252' > pat dd if=pat of=/dev/mapper/snap bs=256K seek=1 count=1 conv=notrunc dd if=pat of=/dev/mapper/snap bs=256K seek=2 count=1 conv=notrunc # shift the snapshot by 8 sectors, so a discard that is aligned on # top is unaligned inside the snapshot dmsetup create top --table "0 8184 linear /dev/mapper/snap 8" blkdiscard -o 262144 -l 262144 /dev/mapper/top sync echo 3 > /proc/sys/vm/drop_caches # chunk 1 dd if=/dev/mapper/snap bs=512 skip=512 count=8 | od -An -tx1 -N1 # chunk 2 dd if=/dev/mapper/snap bs=512 skip=1024 count=8 | od -An -tx1 -N1 Before: aa 00 <- chunk 2 was zeroed although it was not discarded After: aa aa <- nothing written, a discard is only a hint Fix it by requiring the discard to start on a chunk boundary in io_overlaps_chunk(), and by finishing a discard that is not a full chunk with bio_endio() instead of passing it down to the COW device. Fixes: 2e6023850e17 ("dm snapshot: add optional discard support features") Cc: stable@vger.kernel.org Signed-off-by: Chen Cheng Signed-off-by: Mikulas Patocka --- drivers/md/dm-snap.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c index e49427bdbeef..6968e806eb84 100644 --- a/drivers/md/dm-snap.c +++ b/drivers/md/dm-snap.c @@ -1948,8 +1948,8 @@ static void zero_exception(struct dm_snapshot *s, struct dm_exception *e, static bool io_overlaps_chunk(struct dm_snapshot *s, struct bio *bio) { - return bio->bi_iter.bi_size == - (s->store->chunk_size << SECTOR_SHIFT); + return bio->bi_iter.bi_size == (s->store->chunk_size << SECTOR_SHIFT) && + !(bio->bi_iter.bi_sector & s->store->chunk_mask); } static int snapshot_map(struct dm_target *ti, struct bio *bio) @@ -2009,11 +2009,13 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio) e = dm_lookup_exception(&s->complete, chunk); if (e) { remap_exception(s, e, bio, chunk); - if (unlikely(bio_op(bio) == REQ_OP_DISCARD) && - io_overlaps_chunk(s, bio)) { + if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) { dm_exception_table_unlock(&lock); up_read(&s->lock); - zero_exception(s, e, bio, chunk); + if (io_overlaps_chunk(s, bio)) + zero_exception(s, e, bio, chunk); + else + bio_endio(bio); r = DM_MAPIO_SUBMITTED; /* discard is not issued */ goto out; } From bd93c4e59c5132282c01f142806e307c7380e7ea Mon Sep 17 00:00:00 2001 From: xietangxin Date: Sat, 31 Oct 2026 15:15:08 +0800 Subject: [PATCH 08/34] dm-crypt: reject integrity_key_size with non-authenc AEAD ciphers When integrity_key_size parameter is paired with a pure AEAD cipher that is not an authenc composite, crypt_ctr_auth_cipher() returns 0 early without allocating cc->authenc_key, but cc->key_mac_size remains non-zero because CRYPT_KEY_MAC_SIZE_SET was set during option parsing. As a result crypt_copy_authenckey() dereferences NULL crashes the kernel. Reject the contradictory configuration explicitly instead of silently returning 0, so userspace gets a clear -EINVAL. Fixes: 4441686b24a1 ("dm-crypt: Allow to specify the integrity key size as option") Cc: stable@vger.kernel.org Reported-by: syzbot+ff132cb52bce4f44ecc9@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=ff132cb52bce4f44ecc9 Assisted-by: LLM Signed-off-by: xietangxin Signed-off-by: Mikulas Patocka --- drivers/md/dm-crypt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index a7655bba7ce3..8853e9af1c3d 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -2847,8 +2847,11 @@ static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) char *start, *end, *mac_alg = NULL; struct crypto_ahash *mac; - if (!strstarts(cipher_api, "authenc(")) + if (!strstarts(cipher_api, "authenc(")) { + if (test_bit(CRYPT_KEY_MAC_SIZE_SET, &cc->cipher_flags)) + return -EINVAL; return 0; + } start = strchr(cipher_api, '('); end = strchr(cipher_api, ','); From bcb78d49620f666b4a9d62997691e8d031fd4ebc Mon Sep 17 00:00:00 2001 From: Yingjie Wang <1075151112@qq.com> Date: Tue, 3 Nov 2026 00:34:29 +0800 Subject: [PATCH 09/34] dm thin: avoid wrapping no_space_timeout set_pool_mode() converts the no_space_timeout module parameter with READ_ONCE(no_space_timeout_secs) * HZ. The usual arithmetic conversions evaluate the product as unsigned int, so it can wrap before being stored in the unsigned long delay. With HZ=1000, 4294968 seconds becomes 704 jiffies: a pool that runs out of data space switches to error_if_no_space and fails queued writes with -ENOSPC after about 0.7 seconds. Widen the value before the multiplication and cap it at MAX_JIFFY_OFFSET / HZ, so large timeouts no longer wrap into short delays. On 32-bit kernels, values beyond the supported jiffies range saturate at the largest whole-second delay not exceeding MAX_JIFFY_OFFSET. The timer wheel's existing handling of very long delays is unchanged. Fixes: 80c578930ce7 ("dm thin: add 'no_space_timeout' dm-thin-pool module param") Cc: stable@vger.kernel.org Signed-off-by: Yingjie Wang <1075151112@qq.com> Signed-off-by: Mikulas Patocka --- drivers/md/dm-thin.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c index 358ed77ffb2b..9287ee5c00a4 100644 --- a/drivers/md/dm-thin.c +++ b/drivers/md/dm-thin.c @@ -2542,7 +2542,9 @@ static void set_pool_mode(struct pool *pool, enum pool_mode new_mode) struct pool_c *pt = pool->ti->private; bool needs_check = dm_pool_metadata_needs_check(pool->pmd); enum pool_mode old_mode = get_pool_mode(pool); - unsigned long no_space_timeout = READ_ONCE(no_space_timeout_secs) * HZ; + unsigned long no_space_timeout = + min_t(unsigned long, READ_ONCE(no_space_timeout_secs), + MAX_JIFFY_OFFSET / HZ) * HZ; /* * Never allow the pool to transition to PM_WRITE mode if user From 8f611fe94a781c6fb658488cc7eab4e4923e6df6 Mon Sep 17 00:00:00 2001 From: "shao.mingyin@zte.com.cn" Date: Tue, 3 Nov 2026 18:53:40 +0800 Subject: [PATCH 10/34] dm mpath: fix buffer overflow in parse_hw_handler() parse_hw_handler() builds the hw_handler parameter string in a buffer sized with len = 4; plus one byte per argument. Those four bytes were meant for the decimal form of the parameter count, three digits plus the terminating NUL. dm_read_arg_group() accepts up to 1024 arguments and the count written is 'hw_argc - 1', so a count of 1000 or more needs four digits plus the NUL and sprintf() writes one byte past the allocation. Size the buffer from the actual string length instead. Fixes: 2bfd2e1337f0 ("[SCSI] scsi_dh: Use scsi_dh_set_params() in multipath.") Cc: stable@vger.kernel.org Signed-off-by: Shao Mingyin Signed-off-by: Mikulas Patocka --- drivers/md/dm-mpath.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-mpath.c b/drivers/md/dm-mpath.c index ae9318f5f21b..2f871a23a8cb 100644 --- a/drivers/md/dm-mpath.c +++ b/drivers/md/dm-mpath.c @@ -1085,8 +1085,9 @@ static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) if (hw_argc > 1) { char *p; - int i, j, len = 4; + int i, j, len; + len = snprintf(NULL, 0, "%d", hw_argc - 1) + 1; for (i = 0; i <= hw_argc - 2; i++) len += strlen(as->argv[i]) + 1; p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); From 0550d3b14e008b033b02cfce668008ec6addffe4 Mon Sep 17 00:00:00 2001 From: "shao.mingyin@zte.com.cn" Date: Tue, 3 Nov 2026 18:53:42 +0800 Subject: [PATCH 11/34] dm log userspace: drop the device reference on error userspace_ctr() takes a reference on the log device with dm_get_device(). When the integrated-flush workqueue allocation fails it jumps to the shared error path, which frees the log context without dropping that reference, so the device stays busy and cannot be removed. userspace_dtr() drops the reference correctly; do the same here. Fixes: 5066a4df1f42 ("dm log userspace: allow mark requests to piggyback on flush requests") Cc: stable@vger.kernel.org Signed-off-by: Shao Mingyin Signed-off-by: Mikulas Patocka --- drivers/md/dm-log-userspace-base.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-log-userspace-base.c b/drivers/md/dm-log-userspace-base.c index 3ca35aa4e057..48daa41728d6 100644 --- a/drivers/md/dm-log-userspace-base.c +++ b/drivers/md/dm-log-userspace-base.c @@ -294,9 +294,11 @@ static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti, } r = dm_get_device(ti, devices_rdata, dm_table_get_mode(ti->table), &lc->log_dev); - if (r) + if (r) { DMERR("Failed to register %s with device-mapper", devices_rdata); + goto out; + } } if (lc->integrated_flush) { @@ -315,6 +317,8 @@ static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti, out: kfree(devices_rdata); if (r) { + if (lc->log_dev) + dm_put_device(lc->ti, lc->log_dev); mempool_exit(&lc->flush_entry_pool); kfree(lc); kfree(ctr_str); From 9bb1d14877f4929be17f7fdbeb4cfdf5025d464b Mon Sep 17 00:00:00 2001 From: Kyrylo Biryuk Date: Tue, 3 Nov 2026 23:04:37 +0100 Subject: [PATCH 12/34] dm zoned: back off when reclaim cannot make progress When a reclaim pass cannot make progress, dmz_reclaim_work() still ends with dmz_schedule_reclaim(). If the target is idle, dmz_should_reclaim() keeps returning true, the work is queued again with no delay and the worker spins on the same failing pass. This patch handles two such cases: - dmz_do_reclaim() fails with -ENOSPC because no free zone is available to copy the data into, for example on a target without cache zones that has no free sequential zone left. The next patch also makes this the expected result when an idle target with cache zones has random zones but no free sequential zone. - dmz_do_reclaim() finds no zone it can lock for reclaim and returns -EBUSY. Since commit 90a9b8693f1b ("dm zoned: reclaim random zones when idle"), dmz_should_reclaim() returns true on an idle target whenever the total number of random (and cache) zones is non-zero, whether or not any of them holds data. Each pass then also runs dmz_check_dev(), which checks every device for media events (a TEST UNIT READY on SCSI disks). The -EBUSY loop lasts as long as the target stays idle; it ends when a pass finishes while the target is busy and has enough free zones. On three 27 TB host-managed SATA drives used as md RAID5 members (7.2.10 with the module built out of tree, all random zones free, target idle for 300 seconds, kprobes attached, two other drives on the controller being zero-filled), each member's SCSI device counted about 108,000 requests per second with the stock module and the system used about 915 s of CPU time, against about 46 s with this series. libata completes TEST UNIT READY in software, so these counters do not measure commands sent to the drives; the cost is CPU time. When a pass fails with -ENOSPC or -EBUSY, retrying at once is unlikely to make progress: re-arm the idle poll instead, so that reclaim is retried after DMZ_IDLE_PERIOD unless a zone allocation or a "reclaim" message queues it earlier. On a busy target, zone allocations can restart reclaim immediately when free space is low enough to require reclaim. Re-arming also keeps the poll running after a complete idle drain. The poll can still stop after a pass that ends while reclaim is not needed; that is unchanged. Skip the device check for these two results: they normally mean that there is no zone to copy into or no zone to reclaim, not that a device failed, and the I/O paths check the devices themselves. Use queue_delayed_work() so that work an allocation has already queued is not postponed. A pass that fails with an I/O error, for example on an unreadable block of a device that stays online, is still retried at once while the target is idle; this patch does not change that. Tested on 7.4-rc1 with this patch and the next one, on three emulated host-managed drives (scsi_debug, 1 GiB, 32 MiB zones) each under dm-zoned as md RAID5 members, all random zones free and the target idle for 300 seconds. With the stock module each member's SCSI device counted about 70.2 million requests and the reclaim workers used 726 s of CPU. With this series: 75 requests per member, 30 passes per worker (one per DMZ_IDLE_PERIOD) and 0.08 s of CPU. Data written during the tests read back with matching checksums, and no kernel warnings were logged. Fixes: 3b1a94c88b79 ("dm zoned: drive-managed zoned block device target") Fixes: 90a9b8693f1b ("dm zoned: reclaim random zones when idle") Cc: stable@vger.kernel.org Signed-off-by: Kyrylo Biryuk Reviewed-by: Damien Le Moal Reviewed-by: Hannes Reinecke Signed-off-by: Mikulas Patocka --- drivers/md/dm-zoned-reclaim.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/md/dm-zoned-reclaim.c b/drivers/md/dm-zoned-reclaim.c index c041413c729e..077a8a50197f 100644 --- a/drivers/md/dm-zoned-reclaim.c +++ b/drivers/md/dm-zoned-reclaim.c @@ -537,6 +537,16 @@ static void dmz_reclaim_work(struct work_struct *work) dmz_nr_rnd_zones(zmd, zrc->dev_idx)); ret = dmz_do_reclaim(zrc); + if (ret == -ENOSPC || ret == -EBUSY) { + /* + * No zone to copy into (-ENOSPC) or no zone to reclaim + * (-EBUSY): retrying at once cannot make progress and would + * spin. Poll again after the idle period instead, without + * postponing work that an allocation has already queued. + */ + queue_delayed_work(zrc->wq, &zrc->work, DMZ_IDLE_PERIOD); + return; + } if (ret && ret != -EINTR) { if (!dmz_check_dev(zmd)) return; From dab527d83462a618d99bfc96370ce45ae3428706 Mon Sep 17 00:00:00 2001 From: Kyrylo Biryuk Date: Tue, 3 Nov 2026 23:04:38 +0100 Subject: [PATCH 13/34] dm zoned: do not reclaim a random zone into another random zone dmz_reclaim_rnd_data() falls back to a random destination zone when no free sequential zone is available and the target has cache zones. When the target is idle and the cache zone list is empty, dmz_get_rnd_zone_for_reclaim() selects random zones of the zoned device as well, so a random zone is then copied into another random zone. That frees nothing: the next pass picks the next random zone and copies it again, for as long as the target stays idle. Seen on a two-device target (256 GiB regular cache device plus a 27 TB host-managed drive) used as an md RAID5 member: after a full rebuild every chunk was mapped and no sequential zone was free, and the idle drive kept reading and writing while the free zone counters in "dmsetup status" did not change. Only switch to a random destination zone when the zone being reclaimed is a cache zone: moving it to the zoned device still frees cache space. This depends on "dm zoned: back off when reclaim cannot make progress": without it, dmz_reclaim_work() would retry the resulting -ENOSPC at once, and run dmz_check_dev(), for as long as the target stays idle. Tested on 7.4-rc1 together with the previous patch, on a two-device target (a 512 MiB RAM-backed regular cache device plus a 1 GiB scsi_debug host-managed drive) filled until no sequential zone was free, then idle for 60 seconds: the stock module made 877 random-to-random copies; with this series it made none, and each pass failed with -ENOSPC once per DMZ_IDLE_PERIOD without a device check. Fixes: c5c788595292 ("dm zoned: start reclaim with sequential zones") Cc: stable@vger.kernel.org Reviewed-by: Damien Le Moal Reviewed-by: Hannes Reinecke Signed-off-by: Kyrylo Biryuk Signed-off-by: Mikulas Patocka --- drivers/md/dm-zoned-reclaim.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-zoned-reclaim.c b/drivers/md/dm-zoned-reclaim.c index 077a8a50197f..152cb8788114 100644 --- a/drivers/md/dm-zoned-reclaim.c +++ b/drivers/md/dm-zoned-reclaim.c @@ -288,7 +288,13 @@ static int dmz_reclaim_rnd_data(struct dmz_reclaim *zrc, struct dm_zone *dzone) again: szone = dmz_alloc_zone(zmd, zrc->dev_idx, alloc_flags | DMZ_ALLOC_RECLAIM); - if (!szone && alloc_flags == DMZ_ALLOC_SEQ && dmz_nr_cache_zones(zmd)) { + /* + * If no free sequential zone is available, a cache zone can still be + * moved into a free random zone of the zoned device, which frees cache + * space. Do not do this for a random zone: moving it into another + * random zone frees nothing and, on an idle target, repeats forever. + */ + if (!szone && alloc_flags == DMZ_ALLOC_SEQ && dmz_is_cache(dzone)) { alloc_flags = DMZ_ALLOC_RND; goto again; } From 875a7161fe1574205293c36f7bbd0815023e48a1 Mon Sep 17 00:00:00 2001 From: Sinan Polat Date: Mon, 2 Nov 2026 20:21:51 +0000 Subject: [PATCH 14/34] dm-integrity: don't recalculate with HMAC because of a dirty bitmap Commit 5c02406428d5 ("dm integrity: conditionally disable "recalculate" feature") refuses a superblock with SB_FLAG_RECALCULATING if a keyed hash is used without "legacy_recalculate". Otherwise someone who can write to the device, but does not know the key, could modify data and have the kernel compute valid tags for it. dm_integrity_resume() can still start the same recalculation in J or D mode: if SB_FLAG_DIRTY_BITMAP is set and the bitmap is not clear, or log2_blocks_per_bitmap_bit does not match, it sets SB_FLAG_RECALCULATING and recalc_sector = 0. The superblock is not authenticated unless fix_hmac and journal_mac are both used, so the flag can be set offline. Resume also runs on the first activation. Apply the constructor's policy there. If recalculation is not allowed, log an error and keep the existing tags, so modified blocks fail verification. This also avoids queueing recalc_work on a NULL recalc_wq when internal_hash is not used. Fixes: 5c02406428d5 ("dm integrity: conditionally disable "recalculate" feature") Cc: stable@vger.kernel.org Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Sinan Polat Signed-off-by: Mikulas Patocka --- drivers/md/dm-integrity.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/md/dm-integrity.c b/drivers/md/dm-integrity.c index 802b377613b0..1659a11a1a29 100644 --- a/drivers/md/dm-integrity.c +++ b/drivers/md/dm-integrity.c @@ -3888,6 +3888,24 @@ static bool superblock_changed(struct dm_integrity_c *ic) memcmp(ic->sb->salt, ic->sb_copy->salt, SALT_SIZE) != 0; } +/* + * The superblock is authenticated only if fix_hmac and journal_mac are used, + * so SB_FLAG_DIRTY_BITMAP may have been set by someone who doesn't know the + * key. Apply the policy that the constructor applies to SB_FLAG_RECALCULATING. + */ +static bool dm_integrity_may_recalc_dirty_bitmap(struct dm_integrity_c *ic) +{ + if (!ic->internal_hash) { + DMERR("Dirty bitmap found, but internal_hash is not used"); + return false; + } + if (dm_integrity_disable_recalculate(ic)) { + DMERR("Dirty bitmap found, but HMAC recalculation requires \"legacy_recalculate\""); + return false; + } + return true; +} + static void dm_integrity_resume(struct dm_target *ti) { struct dm_integrity_c *ic = ti->private; @@ -3977,8 +3995,10 @@ static void dm_integrity_resume(struct dm_target *ti) if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit && block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR)) || ic->reset_recalculate_flag) { - ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING); - ic->sb->recalc_sector = cpu_to_le64(0); + if (dm_integrity_may_recalc_dirty_bitmap(ic)) { + ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING); + ic->sb->recalc_sector = cpu_to_le64(0); + } } init_journal(ic, 0, ic->journal_sections, 0); replay_journal(ic); From 406d576cc457203cf86eac7f17839b2efec81db4 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Thu, 5 Nov 2026 16:11:37 +0100 Subject: [PATCH 15/34] dm-sysfs: fix misbehavior if kobject_init_and_add fails If kobject_init_and_add fails, we must call kobject_put (see the comment at kobject_init_and_add). Before calling kobject_init_and_add, we must zero the kobject (see https://lwn.net/Articles/52085/). If the first call to dm_sysfs_init failed, it may be called again, and that second call would see an uninitialized kobject. Cc: stable@vger.kernel.org Fixes: 784aae735d9b ("dm: add name and uuid to sysfs") Signed-off-by: Mikulas Patocka Reported-by: Wentao Liang --- drivers/md/dm-sysfs.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/md/dm-sysfs.c b/drivers/md/dm-sysfs.c index 22bc70923a83..c9f81231df7b 100644 --- a/drivers/md/dm-sysfs.c +++ b/drivers/md/dm-sysfs.c @@ -128,9 +128,20 @@ static const struct kobj_type dm_ktype = { */ int dm_sysfs_init(struct mapped_device *md) { - return kobject_init_and_add(dm_kobject(md), &dm_ktype, - &disk_to_dev(dm_disk(md))->kobj, - "%s", "dm"); + struct kobject *kobj = dm_kobject(md); + int r; + + /* The kobject must be zeroed before calling kobject_init */ + memset(kobj, 0, sizeof(struct kobject)); + r = kobject_init_and_add(kobj, &dm_ktype, + &disk_to_dev(dm_disk(md))->kobj, + "%s", "dm"); + if (unlikely(r)) { + /* Weird semantics - we must call kobject_put on failure */ + dm_sysfs_exit(md); + return r; + } + return 0; } /* From c4965effbe15f837a8089e79c0fc17ab19363cce Mon Sep 17 00:00:00 2001 From: Yaohan Li Date: Wed, 4 Nov 2026 17:15:42 +0800 Subject: [PATCH 16/34] dm: avoid double-dropping the zone reset submission reference On an error in __send_zone_reset_all(), the submission reference held by the dm_io can be decremented there and again by the caller's error path. If another clone is still outstanding, the second drop can release the dm_io before that clone completes. Only drop the submission reference in __send_zone_reset_all() on success. On failure, leave the reference for the caller's existing error handling to release once. The error path was reproduced under KASAN with an injected GFP_NOIO allocation failure while another zone-reset clone remained in flight. With the change, the same failure was reported normally, without the observed use-after-free, and ordinary zone resets continued to work. Fixes: 81e7706345f06 ("dm: handle REQ_OP_ZONE_RESET_ALL") Cc: stable@vger.kernel.org Assisted-by: ZCode:GLM-4 Signed-off-by: Yaohan Li Signed-off-by: Mikulas Patocka --- drivers/md/dm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 1881d86111fb..109b0133f860 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -1929,8 +1929,13 @@ static blk_status_t __send_zone_reset_all(struct clone_info *ci) break; } - /* Release the reference that alloc_io() took for submission. */ - atomic_sub(1, &ci->io->io_count); + /* + * Release the reference that alloc_io() took for submission. On error, + * that reference was not consumed: dm_split_and_process_bio() releases + * it on the error path, so do not release it here a second time. + */ + if (sts == BLK_STS_OK) + atomic_dec(&ci->io->io_count); return sts; } From ae310e4ad872a42f4707a596e66417f2aaa52826 Mon Sep 17 00:00:00 2001 From: Kirtikumar Anandrao Ramchandani Date: Thu, 5 Nov 2026 20:32:23 +0100 Subject: [PATCH 17/34] dm: Reject table setup after queue or disk teardown A failed table load can leave a mapped device in a state where its queue has already been initialized or its disk has been removed, while md->type is still DM_TYPE_NONE. A later table load then calls blk_mq_init_allocated_queue() on the same queue. If del_gendisk() exited the old hardware contexts, blk_mq_exit_hctx() dereferences their cleared flush queue pointer. Reject setup attempts after queue initialization or disk removal. A failed request queue initialization still leaves md->tag_set NULL and can be retried. Set the request-based disk operations only after that initialization succeeds, so an unsuccessful attempt cannot leave a bio-based retry with request-based operations. Reported-by: syzbot+5307e3bcacec2ce4c2f0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=5307e3bcacec2ce4c2f0 Cc: stable@vger.kernel.org Signed-off-by: Kirtikumar Anandrao Ramchandani Signed-off-by: Mikulas Patocka --- drivers/md/dm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 109b0133f860..12c26f42ce51 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -2611,13 +2611,17 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) WARN_ON_ONCE(type == DM_TYPE_NONE); + /* A failed setup may have left the queue initialized or the disk dead. */ + if (md->tag_set || test_bit(GD_DEAD, &md->disk->state)) + return -ENODEV; + if (type == DM_TYPE_REQUEST_BASED) { - md->disk->fops = &dm_rq_blk_dops; r = dm_mq_init_request_queue(md, t); if (r) { DMERR("Cannot initialize queue for request-based dm mapped device"); return r; } + md->disk->fops = &dm_rq_blk_dops; } r = dm_calculate_queue_limits(t, &limits); From df8daa4d916c88ef7888c738e0eb3b6f11714a6f Mon Sep 17 00:00:00 2001 From: Kirtikumar Anandrao Ramchandani Date: Thu, 5 Nov 2026 20:48:07 +0100 Subject: [PATCH 18/34] dm cache: validate the discard block size before narrowing The discard block size doubles until the origin fits within the discard bitset limit. too_many_discard_blocks() passes this sector_t size to sector_div(), whose divisor is only 32 bits. Once the size exceeds that range, the division uses a truncated divisor, which can become zero. Use a 64-bit divisor when checking the block count. The selected size is stored in a u32, so reject sizes that cannot be represented before the assignment and before calculating the bitset size. This prevents a truncated zero size from reaching dm_sector_div_up(). An earlier proposal checks whether the mapped length exceeds the origin device. This fix also covers an origin device whose valid size requires a discard block size beyond the field's range. Reported-by: syzbot+c8564fc0a96e3ecbe4d4@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=c8564fc0a96e3ecbe4d4 Cc: stable@vger.kernel.org Fixes: 08b184514f65 ("dm cache: revert 'prevent corruption caused by discard_block_size > cache_block_size'") Signed-off-by: Kirtikumar Anandrao Ramchandani Signed-off-by: Mikulas Patocka --- drivers/md/dm-cache-target.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c index b562bf59124c..809184371629 100644 --- a/drivers/md/dm-cache-target.c +++ b/drivers/md/dm-cache-target.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -2383,9 +2384,7 @@ static int create_cache_policy(struct cache *cache, struct cache_args *ca, static bool too_many_discard_blocks(sector_t discard_block_size, sector_t origin_size) { - (void) sector_div(origin_size, discard_block_size); - - return origin_size > MAX_DISCARD_BLOCKS; + return div64_u64(origin_size, discard_block_size) > MAX_DISCARD_BLOCKS; } static sector_t calculate_discard_block_size(sector_t cache_block_size, @@ -2422,6 +2421,7 @@ static int cache_create(struct cache_args *ca, struct cache **result) struct cache *cache; struct dm_target *ti = ca->ti; dm_block_t origin_blocks; + sector_t discard_block_size; struct dm_cache_metadata *cmd; bool may_format = ca->features.mode == CM_WRITE; @@ -2521,9 +2521,15 @@ static int cache_create(struct cache_args *ca, struct cache **result) } clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size)); - cache->discard_block_size = + discard_block_size = calculate_discard_block_size(cache->sectors_per_block, cache->origin_sectors); + if (discard_block_size > U32_MAX) { + *error = "Discard block size exceeds 32 bits"; + r = -EOVERFLOW; + goto bad; + } + cache->discard_block_size = discard_block_size; cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors, cache->discard_block_size)); cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks)); From ae5b40accbc06f93e6a513e0e9a07fd32b197e49 Mon Sep 17 00:00:00 2001 From: Yingjie Wang Date: Thu, 5 Nov 2026 15:49:27 +0000 Subject: [PATCH 19/34] dm mpath: cap queue_if_no_path_timeout_secs enable_nopath_timeout() arms the no-path timer with jiffies + queue_if_no_path_timeout_secs * HZ. The module parameter is an unbounded unsigned long, so the delay can exceed what mod_timer() handles: a delay above LONG_MAX is treated as already expired, and values above ULONG_MAX / HZ also wrap in the multiplication. The timer then clears queue_if_no_path right away, and I/O queued after the last path fails gets -EIO instead of waiting for the configured time. On x86-64 with HZ=1000, 9223372036854776 seconds failed a write after 124 ms while one second less remained queued throughout a 4.2-second observation window. A value of 18446744073709552 seconds wrapped to 384 jiffies and failed the write after 455 ms. On 32-bit kernels with HZ=1000 the delay passes LONG_MAX above 2147483 seconds, a little under 25 days. Cap the seconds value at the largest whole-second delay not exceeding MAX_JIFFY_OFFSET before multiplying by HZ. This prevents signed-delta overflow and multiplication wrap. The timer wheel's existing handling of very long delays is unchanged. Fixes: be240ff5e402 ("dm mpath: Add timeout mechanism for queue_if_no_path") Cc: stable@vger.kernel.org Signed-off-by: Yingjie Wang Signed-off-by: Mikulas Patocka --- drivers/md/dm-mpath.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-mpath.c b/drivers/md/dm-mpath.c index 2f871a23a8cb..d2e94b96d2e4 100644 --- a/drivers/md/dm-mpath.c +++ b/drivers/md/dm-mpath.c @@ -797,7 +797,8 @@ static void queue_if_no_path_timeout_work(struct timer_list *t) static void enable_nopath_timeout(struct multipath *m) { unsigned long queue_if_no_path_timeout = - READ_ONCE(queue_if_no_path_timeout_secs) * HZ; + min_t(unsigned long, READ_ONCE(queue_if_no_path_timeout_secs), + MAX_JIFFY_OFFSET / HZ) * HZ; lockdep_assert_held(&m->lock); From 6fed9e26810f0b466dbcebe35a73d1fd48793ff5 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Thu, 5 Nov 2026 08:27:08 +0100 Subject: [PATCH 20/34] dm-crypt: defer async write submission to write thread With no_write_workqueue, kcryptd_crypt_write_io_submit() submits the bio directly, even when called from kcryptd_async_done(). An async cipher may run this callback in softirq or with BH disabled, where submit_bio_noacct() must not be called because it can sleep. Submit inline only when async is false. Async completions are sent to the write thread, as is already done for submit_from_crypt_cpus. Use the async flag rather than in_interrupt(), since callback context depends on the cipher driver. Sync ciphers still submit inline. Tested with cryptd in QEMU (x86_64 and arm64) with CONFIG_DEBUG_ATOMIC_SLEEP, and on a P2020 running 6.18.55 without it. In QEMU: - 4k writes logging "sleeping function called from invalid context": 100/100 unpatched, 0/155 patched. - nr_requests=4 on a loop device: 0/6 unpatched runs completed, 4/4 patched runs completed. On the P2020, a warning in blk_mq_run_hw_queue() appeared in the 6th unpatched run and did not appear in 30 patched runs. Fixes: 39d42fa96ba1 ("dm crypt: add flags to optionally bypass kcryptd workqueues") Cc: stable@vger.kernel.org Signed-off-by: Josef Schlehofer Signed-off-by: Mikulas Patocka --- drivers/md/dm-crypt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index 8853e9af1c3d..c0552a300e62 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -1982,8 +1982,9 @@ static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async) /* crypt_convert should have filled the clone bio */ BUG_ON(io->ctx.iter_out.bi_size); - if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || - test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { + if (likely(!async) && + (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags) || + test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { dm_submit_bio_remap(io->base_bio, clone); return; } From d12afc2f9ed6e14019fed9c02a74ad699915dc85 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:38:45 +0100 Subject: [PATCH 21/34] dm-cache: fix NULL pointer dereference on open failure When __begin_transaction_flags in metadata_open fails, dm_cache_metadata_close is called. dm_cache_metadata_close attempts to execute list_del(&cmd->list);, but the "cmd" object is not on any list yet, thus it crashes with NULL pointer dereference. Fix this by initializing cmd->list after cmd is allocated. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: 9b1cc9f251af ("dm cache: share cache-metadata object across inactive and active DM tables") --- drivers/md/dm-cache-metadata.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/md/dm-cache-metadata.c b/drivers/md/dm-cache-metadata.c index 179b76173255..bd501fb0e5c5 100644 --- a/drivers/md/dm-cache-metadata.c +++ b/drivers/md/dm-cache-metadata.c @@ -766,6 +766,7 @@ static struct dm_cache_metadata *metadata_open(struct block_device *bdev, return ERR_PTR(-ENOMEM); } + INIT_LIST_HEAD(&cmd->list); cmd->version = metadata_version; refcount_set(&cmd->ref_count, 1); init_rwsem(&cmd->root_lock); From 421420758be348cb90715bfaa26e1211a12ff1d0 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:39:54 +0100 Subject: [PATCH 22/34] dm-cache: fix ending a cursor that never began If dm_bitset_cursor_begin fails, dm_array_cursor_end(&cmd->hint_cursor) is called. However, cmd->hint_cursor was only initialized if the variable hints_valid is true. Thus, we must also destroy hint_cursor only if hints_valid is true. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: 629d0a8a1a10 ("dm cache metadata: add "metadata2" feature") --- drivers/md/dm-cache-metadata.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-cache-metadata.c b/drivers/md/dm-cache-metadata.c index bd501fb0e5c5..461044dca367 100644 --- a/drivers/md/dm-cache-metadata.c +++ b/drivers/md/dm-cache-metadata.c @@ -1433,7 +1433,8 @@ static int __load_mappings(struct dm_cache_metadata *cmd, from_cblock(cmd->cache_blocks), &cmd->dirty_cursor); if (r) { - dm_array_cursor_end(&cmd->hint_cursor); + if (hints_valid) + dm_array_cursor_end(&cmd->hint_cursor); dm_array_cursor_end(&cmd->mapping_cursor); return r; } From 62ee9c09366cad69410330087d990e469dd92314 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:41:56 +0100 Subject: [PATCH 23/34] dm-thin: fix data corruption if discard races with regular I/O In the function break_up_discard_bio, if a discard hits the same block as regular I/O, it was intended to skip the discard for the affected range. However, the skip code only advances "data_begin", it doesn't advance "virt_begin". So, the data_begin and virt_begin variables become unsynchronized. process_prepared_discard_passdown_pt1 calculates "data_end = m->data_block + (m->virt_end - m->virt_begin);" - if m->data_block is incremented and m->virt_begin isn't, it will overshoot the intended discard range. Cc: stable@vger.kernel.org Fixes: e2dd8aca2d76 ("dm bio prison v1: improve concurrent IO performance") Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 --- drivers/md/dm-thin.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c index 9287ee5c00a4..b42f7488e1f3 100644 --- a/drivers/md/dm-thin.c +++ b/drivers/md/dm-thin.c @@ -1703,6 +1703,7 @@ static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t (void) build_key(tc->td, PHYSICAL, data_begin, data_begin + len, &data_key); if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) { /* contention, we'll give up with this range */ + virt_begin += len; data_begin += len; continue; } From dfb95816cf6f2f2660af2ec64c65dc204c60b5e7 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:43:14 +0100 Subject: [PATCH 24/34] dm-thin: fix leak if __set_snapshot_details fails If __set_snapshot_details fails, the variable td is leaked - this prevents any other snapshot with the same id from being created. Fix this by removing it from the list and freeing it. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: 991d9fa02da0 ("dm: add thin provisioning target") --- drivers/md/dm-thin-metadata.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c index e60e1326376a..03df0b1aab78 100644 --- a/drivers/md/dm-thin-metadata.c +++ b/drivers/md/dm-thin-metadata.c @@ -1225,10 +1225,13 @@ static int __create_snap(struct dm_pool_metadata *pmd, __close_device(td); if (r) - goto bad; + goto bad2; return 0; +bad2: + list_del(&td->list); + kfree(td); bad: dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root); dm_btree_remove(&pmd->details_info, pmd->details_root, From c557fa19a83efad1e20eb733df821b745a4589ff Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:44:19 +0100 Subject: [PATCH 25/34] dm-thin: fix parsing of feature arguments "struct dm_arg _args" limits the number of arguments to 4, but there are 5 possible arguments that can be specified. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: 787a996cb251 ("dm thin: add error_if_no_space feature") --- drivers/md/dm-thin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c index b42f7488e1f3..0b65c1b09f47 100644 --- a/drivers/md/dm-thin.c +++ b/drivers/md/dm-thin.c @@ -3149,7 +3149,7 @@ static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf, const char *arg_name; static const struct dm_arg _args[] = { - {0, 4, "Invalid number of pool feature arguments"}, + {0, 5, "Invalid number of pool feature arguments"}, }; /* From 624d146d97c5fbe7200ef4b2dd5350ecf57d6b79 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:46:24 +0100 Subject: [PATCH 26/34] dm persistent data: test return value of node_copy There were 3 places where the return value of node_copy was not tested. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: c671ffa55d8b ("dm btree remove: change a bunch of BUG_ON() calls to proper errors") --- drivers/md/persistent-data/dm-btree-remove.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/md/persistent-data/dm-btree-remove.c b/drivers/md/persistent-data/dm-btree-remove.c index 2357ff9b53f3..18f53c672202 100644 --- a/drivers/md/persistent-data/dm-btree-remove.c +++ b/drivers/md/persistent-data/dm-btree-remove.c @@ -253,7 +253,9 @@ static int __rebalance2(struct dm_btree_info *info, struct btree_node *parent, /* * Merge */ - node_copy(left, right, -nr_right); + ret = node_copy(left, right, -nr_right); + if (unlikely(ret)) + return ret; left->header.nr_entries = cpu_to_le32(nr_left + nr_right); delete_at(parent, r->index); @@ -315,13 +317,16 @@ static int delete_center_node(struct dm_btree_info *info, struct btree_node *par { uint32_t max_entries = le32_to_cpu(left->header.max_entries); unsigned int shift = min(max_entries - nr_left, nr_center); + int ret; if (nr_left + shift > max_entries) { DMERR("node shift out of bounds"); return -EINVAL; } - node_copy(left, center, -shift); + ret = node_copy(left, center, -shift); + if (unlikely(ret)) + return ret; left->header.nr_entries = cpu_to_le32(nr_left + shift); if (shift != nr_center) { @@ -333,7 +338,9 @@ static int delete_center_node(struct dm_btree_info *info, struct btree_node *par } node_shift(right, shift); - node_copy(center, right, shift); + ret = node_copy(center, right, shift); + if (unlikely(ret)) + return ret; right->header.nr_entries = cpu_to_le32(nr_right + shift); } *key_ptr(parent, r->index) = right->keys[0]; From fa887f2ebee7aff97eb87b3365939616ade09774 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:48:02 +0100 Subject: [PATCH 27/34] dm persistent data: don't ignore writeback errors An error returned by ie_cache_writeback could be silently overwritten by another successful ie_cache_writeback. This commit fixes disk_ll_commit, so that it doesn't ignore the error and returns it immediatelly. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: 6b06dd5a9722 ("dm space map disk: cache a small number of index entries") --- drivers/md/persistent-data/dm-space-map-common.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/md/persistent-data/dm-space-map-common.c b/drivers/md/persistent-data/dm-space-map-common.c index 22a551c407da..833aefb0ab99 100644 --- a/drivers/md/persistent-data/dm-space-map-common.c +++ b/drivers/md/persistent-data/dm-space-map-common.c @@ -1190,17 +1190,20 @@ static dm_block_t disk_ll_max_entries(struct ll_disk *ll) static int disk_ll_commit(struct ll_disk *ll) { - int r = 0; + int r; unsigned int i; for (i = 0; i < IE_CACHE_SIZE; i++) { struct ie_cache *iec = ll->ie_cache + i; - if (iec->valid && iec->dirty) + if (iec->valid && iec->dirty) { r = ie_cache_writeback(ll, iec); + if (unlikely(r)) + return r; + } } - return r; + return 0; } int sm_ll_new_disk(struct ll_disk *ll, struct dm_transaction_manager *tm) From b27523a0781aae0b51ac13f29dc65373d14b8434 Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Fri, 6 Nov 2026 19:49:33 +0100 Subject: [PATCH 28/34] dm persistent data: don't leak bufio buffer if find_leaf fails If find_leaf fails, the function dm_btree_cursor_begin would return an error, but it would keep the dm-bufio buffers held. This would result in a memory leak and a warning when releasing the dm-bufio cache. This commit fixes it, so that dm_btree_cursor_begin releases the buffers on error. Signed-off-by: Mikulas Patocka Assisted-by: Claude:claude-opus-5 Cc: stable@vger.kernel.org Fixes: 7d111c81fa29 ("dm btree: introduce cursor api") --- drivers/md/persistent-data/dm-btree.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/md/persistent-data/dm-btree.c b/drivers/md/persistent-data/dm-btree.c index dd02eee4a23c..abfbbab8c7c6 100644 --- a/drivers/md/persistent-data/dm-btree.c +++ b/drivers/md/persistent-data/dm-btree.c @@ -1582,7 +1582,13 @@ int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root, if (r) return r; - return find_leaf(c); + r = find_leaf(c); + if (unlikely(r)) { + dm_btree_cursor_end(c); + return r; + } + + return 0; } EXPORT_SYMBOL_GPL(dm_btree_cursor_begin); From c834161ded1a141a58d5f03b24f6894291222c96 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 6 Nov 2026 19:59:22 +0100 Subject: [PATCH 29/34] dm: fix use-after-free when dm_get_device() widens the open mode There's an UAF bug in dm_get_device(): when a table construction calls dm_get_device() on the same device twice with widen access mode (e.g., read-only first then read-write), the second dm_get_device() calls upgrade_mode(), which reopens the device in upgraded mode, making previously returned dm_dev* a stale pointer. Fixing approaches: - Add per-mode views (dm_dev_ref) from a per-device dm_dev_internal to multiple table_devices. - Keep reusing the dm_dev_ref when the requested mode is included, i.e., a read-only reference can reuse the read-write opened device within a table. - Maintain a canonical block_device pointer in dm_dev_internal for handling the DM_TABLE_DEPS ioctl. Also tweak the way of mode sharing check in find_ref() to "(x & mask) == mask". The result stays the same to the previous upgrade_mode() guard "(x | mask) == x". Pros: keep the relationship between dm_dev_internal and table_device: one dm_dev_internal per device, regardless the mode, so the output of DMM_TABLE_DEPS ioctl unchanged. Cons: extra dm_dev_ref allocation. Note that now the table_device's count not necessarily mean the number tables referecing this pair, since we're allowing multiple references within a table. Signed-off-by: Ming Hung Tsai Signed-off-by: Mikulas Patocka Co-Authored-By: Claude Opus 5 Fixes: 86f1152b117a ("dm: allow active and inactive tables to share dm_devs") Cc: stable@vger.kernel.org --- drivers/md/dm-ioctl.c | 2 +- drivers/md/dm-table.c | 146 +++++++++++++++++++++++------------------- drivers/md/dm.c | 2 +- drivers/md/dm.h | 18 +++++- 4 files changed, 100 insertions(+), 68 deletions(-) diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c index 8f945f0aeda5..4a7cb0926c17 100644 --- a/drivers/md/dm-ioctl.c +++ b/drivers/md/dm-ioctl.c @@ -1794,7 +1794,7 @@ static void retrieve_deps(struct dm_table *table, deps->count = count; count = 0; list_for_each_entry(dd, dm_table_get_devices(table), list) - deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev); + deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev); param->data_size = param->data_start + needed; } diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c index a483f49dd4ce..db1ec4ae8e56 100644 --- a/drivers/md/dm-table.c +++ b/drivers/md/dm-table.c @@ -165,14 +165,16 @@ int dm_table_create(struct dm_table **result, blk_mode_t mode, static void free_devices(struct list_head *devices, struct mapped_device *md) { - struct list_head *tmp, *next; + struct dm_dev_internal *dd, *dd_next; + struct dm_dev_ref *ref, *ref_next; - list_for_each_safe(tmp, next, devices) { - struct dm_dev_internal *dd = - list_entry(tmp, struct dm_dev_internal, list); - DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s", - dm_device_name(md), dd->dm_dev->name); - dm_put_table_device(md, dd->dm_dev); + list_for_each_entry_safe(dd, dd_next, devices, list) { + list_for_each_entry_safe(ref, ref_next, &dd->refs, list) { + DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s", + dm_device_name(md), ref->dm_dev->name); + dm_put_table_device(md, ref->dm_dev); + kfree(ref); + } kfree(dd); } } @@ -218,12 +220,27 @@ static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev) struct dm_dev_internal *dd; list_for_each_entry(dd, l, list) - if (dd->dm_dev->bdev->bd_dev == dev) + if (dd->bdev->bd_dev == dev) return dd; return NULL; } +/* + * See if the device is already open with a mode that covers the + * requested one. + */ +static struct dm_dev_ref *find_ref(struct dm_dev_internal *dd, blk_mode_t mode) +{ + struct dm_dev_ref *ref; + + list_for_each_entry(ref, &dd->refs, list) + if ((ref->dm_dev->mode & mode) == mode) + return ref; + + return NULL; +} + /* * If possible, this checks an area of a destination device is invalid. */ @@ -303,31 +320,6 @@ static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev, return 0; } -/* - * This upgrades the mode on an already open dm_dev, being - * careful to leave things as they were if we fail to reopen the - * device and not to touch the existing bdev field in case - * it is accessed concurrently. - */ -static int upgrade_mode(struct dm_dev_internal *dd, blk_mode_t new_mode, - struct mapped_device *md) -{ - int r; - struct dm_dev *old_dev, *new_dev; - - old_dev = dd->dm_dev; - - r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev, - dd->dm_dev->mode | new_mode, &new_dev); - if (r) - return r; - - dd->dm_dev = new_dev; - dm_put_table_device(md, old_dev); - - return 0; -} - /* * Note: the __ref annotation is because this function can call the __init * marked early_lookup_bdev when called during early boot code from dm-init.c. @@ -360,7 +352,9 @@ EXPORT_SYMBOL(dm_devt_from_path); /* * Add a device to the list, or just increment the usage count if - * it's already present. + * it's already present with a mode that covers the requested one. + * Otherwise open the device again with the requested mode, so the + * dm_devs returned earlier stay valid. */ int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode, struct dm_dev **result) @@ -368,6 +362,7 @@ int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode, int r; dev_t dev; struct dm_dev_internal *dd; + struct dm_dev_ref *ref; struct dm_table *t = ti->table; BUG_ON(!t); @@ -380,30 +375,46 @@ int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode, return -EINVAL; dd = find_device(&t->devices, dev); - if (!dd) { + if (dd) { + ref = find_ref(dd, mode); + if (ref) { + refcount_inc(&ref->count); + goto out; + } + } else { dd = kmalloc_obj(*dd); if (!dd) return -ENOMEM; - - r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev); - if (r) { - kfree(dd); - return r; - } - - refcount_set(&dd->count, 1); - list_add(&dd->list, &t->devices); - goto out; - - } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) { - r = upgrade_mode(dd, mode, t->md); - if (r) - return r; + INIT_LIST_HEAD(&dd->refs); } - refcount_inc(&dd->count); + + ref = kmalloc_obj(*ref); + if (!ref) { + r = -ENOMEM; + goto bad; + } + + r = dm_get_table_device(t->md, dev, mode, &ref->dm_dev); + if (r) { + kfree(ref); + goto bad; + } + refcount_set(&ref->count, 1); + + /* A device in the list always has at least one ref */ + if (list_empty(&dd->refs)) { + dd->bdev = ref->dm_dev->bdev; + list_add(&dd->list, &t->devices); + } + list_add_tail(&ref->list, &dd->refs); out: - *result = dd->dm_dev; + *result = ref->dm_dev; return 0; + +bad: + if (list_empty(&dd->refs)) + kfree(dd); + return r; } EXPORT_SYMBOL(dm_get_device); @@ -453,23 +464,28 @@ static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev, */ void dm_put_device(struct dm_target *ti, struct dm_dev *d) { - int found = 0; struct list_head *devices = &ti->table->devices; struct dm_dev_internal *dd; + struct dm_dev_ref *ref; - list_for_each_entry(dd, devices, list) { - if (dd->dm_dev == d) { - found = 1; - break; - } - } - if (!found) { - DMERR("%s: device %s not in table devices list", - dm_device_name(ti->table->md), d->name); + list_for_each_entry(dd, devices, list) + list_for_each_entry(ref, &dd->refs, list) + if (ref->dm_dev == d) + goto found; + + DMERR("%s: device %s not in table devices list", + dm_device_name(ti->table->md), d->name); + return; + +found: + if (!refcount_dec_and_test(&ref->count)) return; - } - if (refcount_dec_and_test(&dd->count)) { - dm_put_table_device(ti->table->md, d); + + dm_put_table_device(ti->table->md, d); + list_del(&ref->list); + kfree(ref); + + if (list_empty(&dd->refs)) { list_del(&dd->list); kfree(dd); } diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 12c26f42ce51..a73355c0125c 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -1588,7 +1588,7 @@ static void __send_empty_flush(struct clone_info *ci) */ clone = alloc_tio(ci, NULL, 0, &len, GFP_NOIO); atomic_add(1, &ci->io->io_count); - bio_set_dev(clone, dd->dm_dev->bdev); + bio_set_dev(clone, dd->bdev); clone->bi_end_io = clone_endio; dm_submit_bio_remap(clone, NULL); } diff --git a/drivers/md/dm.h b/drivers/md/dm.h index 7a795979ec72..34f58060243f 100644 --- a/drivers/md/dm.h +++ b/drivers/md/dm.h @@ -37,10 +37,26 @@ /* * List of devices that a metadevice uses and should open/close. + * There is one dm_dev_internal per device. It holds one dm_dev_ref + * for each mode that the targets opened the device with. */ struct dm_dev_internal { struct list_head list; - refcount_t count; + struct block_device *bdev; + struct list_head refs; +}; + +/* + * One open of a device with one mode, shared by the targets of a table. + * dm_get_device() reuses a dm_dev_ref whose mode covers the requested + * mode, and adds a new one only when none does. So the dm_dev_refs of + * a device have different modes, and a table holds each table_device + * at most once. table_device->count is still the number of tables that + * use the table_device. + */ +struct dm_dev_ref { + struct list_head list; + refcount_t count; /* dm_get_device() calls that returned dm_dev */ struct dm_dev *dm_dev; }; From 8ba1ade3e6b595b9091040feaa3341d7e45ba95d Mon Sep 17 00:00:00 2001 From: Zhao Feng Date: Mon, 9 Nov 2026 10:37:53 +0800 Subject: [PATCH 30/34] dm crypt: fix use of uninitialized percpu_counter in ctr error path If percpu_counter_init() fails in crypt_ctr() (e.g. when the underlying percpu allocation fails under memory pressure or fault injection), the error path jumps to "bad", which calls crypt_dtr(). crypt_dtr() then evaluates WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0) and calls percpu_counter_destroy() on a counter that was never initialized, because __percpu_counter_init_many() returns -ENOMEM before calling raw_spin_lock_init() on fbc->lock. With lockdep enabled this triggers "INFO: trying to register non-static key" in __percpu_counter_sum(), as reported by syzbot. The console additionally shows "crypt: Unknown error (-ENOMEM)" because this failure path sets no ti->error. Move percpu_counter_init() to right after the allocation of cc, before ti->private is set and before any "goto bad" can happen. On failure, set ti->error and free cc directly, so that crypt_dtr() is only ever called with an initialized counter. Reported-by: syzbot+4b232570d625662eaab4@syzkaller.appspotmail.com Closes: https://syzbot.org/bug?extid=4b232570d625662eaab4 Fixes: 5059353df86e ("dm crypt: limit the number of allocated pages") Cc: stable@vger.kernel.org Signed-off-by: Zhao Feng Signed-off-by: Mikulas Patocka --- drivers/md/dm-crypt.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c index c0552a300e62..02038a9cd21d 100644 --- a/drivers/md/dm-crypt.c +++ b/drivers/md/dm-crypt.c @@ -3233,6 +3233,14 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) ti->error = "Cannot allocate encryption context"; return -ENOMEM; } + + ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); + if (ret < 0) { + ti->error = "Cannot initialize page counter"; + kfree_sensitive(cc); + return ret; + } + cc->key_size = key_size; cc->sector_size = (1 << SECTOR_SHIFT); cc->sector_shift = 0; @@ -3244,10 +3252,6 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) crypt_calculate_pages_per_client(); spin_unlock(&dm_crypt_clients_lock); - ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); - if (ret < 0) - goto bad; - /* Optional parameters need to be read before cipher constructor */ if (argc > 5) { ret = crypt_ctr_optional(ti, argc - 5, &argv[5]); From 47930d1236d2988cdc580c10d3af21b6b02d7ece Mon Sep 17 00:00:00 2001 From: Weijiang Zhou Date: Wed, 11 Nov 2026 20:24:42 +0100 Subject: [PATCH 31/34] dm-verity: don't restart or panic on errors during system shutdown Commit 252bd1256396 ("dm verity: skip verity work if I/O error when system is shutting down") stopped I/O errors during shutdown from triggering a restart, since devices may legitimately be gone by then. Errors that reach verity_handle_err(), e.g. through a failed hash block read followed by a failed FEC attempt, are not covered and still call kernel_restart() or panic(). During a reboot that is both wrong (nothing is corrupt) and harmful: it races with the reboot already in progress and replaces its reason with "dm-verity device corrupted", which bootloaders such as Android's treat as a verity failure and switch the device to EIO mode. Apply the same shutdown check in verity_handle_err(). Corruption is still logged, recorded in hash_failed and reported via uevent, and the I/O still fails. Signed-off-by: Hongyu Jin Signed-off-by: weijiang.zhou Signed-off-by: Mikulas Patocka Fixes: 252bd1256396 ("dm verity: skip verity work if I/O error when system is shutting down") Cc: stable@vger.kernel.org --- drivers/md/dm-verity-target.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index 6d589867623c..451c75e30a8f 100644 --- a/drivers/md/dm-verity-target.c +++ b/drivers/md/dm-verity-target.c @@ -170,6 +170,15 @@ static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level, *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits); } +/* + * Skip verity work in response to I/O error when system is shutting down. + */ +static inline bool verity_is_system_shutting_down(void) +{ + return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF + || system_state == SYSTEM_RESTART; +} + /* * Handle verification errors. */ @@ -219,6 +228,15 @@ out: if (v->mode == DM_VERITY_MODE_LOGGING) return 0; + /* + * Devices may already be quiesced (e.g. UFS set offline by its + * shutdown hook), so a failure here does not imply corruption. Fail + * the I/O but do not restart or panic, which would also override the + * reboot reason of the shutdown already in progress. + */ + if (verity_is_system_shutting_down()) + return 1; + if (v->mode == DM_VERITY_MODE_RESTART) kernel_restart("dm-verity device corrupted"); @@ -593,15 +611,6 @@ error: return r; } -/* - * Skip verity work in response to I/O error when system is shutting down. - */ -static inline bool verity_is_system_shutting_down(void) -{ - return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF - || system_state == SYSTEM_RESTART; -} - static void restart_io_error(struct work_struct *w) { kernel_restart("dm-verity device has I/O error"); From e5e03918fbec31f0c896f4f43c2bd2aadf35d43d Mon Sep 17 00:00:00 2001 From: Mikulas Patocka Date: Wed, 11 Nov 2026 18:51:26 +0100 Subject: [PATCH 32/34] dm-integrity: don't attempt to commit journal on disabled device If the device is disabled due to an error, we shouldn't attempt to flush the journal. We jump straight to the label release_flush_bios, that will call do_endio on all queued bios. do_endio will terminate the bios with the error. This fixes WARN_ON in the function pad_uncommitted reported by syzkaller. Reported-by: syzbot+94dac3c75477e184f0b0@syzkaller.appspotmail.com Closes: https://syzbot.org/bug?extid=94dac3c75477e184f0b0 Fixes: 7eada909bfd7 ("dm: add integrity target") Cc: stable@vger.kernel.org Signed-off-by: Mikulas Patocka --- drivers/md/dm-integrity.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/md/dm-integrity.c b/drivers/md/dm-integrity.c index 1659a11a1a29..6968e38888db 100644 --- a/drivers/md/dm-integrity.c +++ b/drivers/md/dm-integrity.c @@ -2908,6 +2908,11 @@ static void integrity_commit(struct work_struct *w) spin_lock_irq(&ic->endio_wait.lock); flushes = bio_list_get(&ic->flush_bio_list); + if (unlikely(dm_integrity_failed(ic))) { + wake_up_locked(&ic->endio_wait); + spin_unlock_irq(&ic->endio_wait.lock); + goto release_flush_bios; + } if (unlikely(ic->mode != 'J')) { spin_unlock_irq(&ic->endio_wait.lock); dm_integrity_flush_buffers(ic, true); @@ -5429,7 +5434,6 @@ try_smaller_buffer: r = create_journal(ic, &ti->error); if (r) goto bad; - } if (ic->mode == 'B') { From 46af2c975958119290ddcd984a98491966123a7b Mon Sep 17 00:00:00 2001 From: Li Youhong Date: Thu, 12 Nov 2026 17:12:05 +0800 Subject: [PATCH 33/34] dm-raid: fix use-after-free in raid_dtr() raid_dtr() currently flushes event_work only if work_pending() is true. This is insufficient because once a worker has dequeued the work, the PENDING bit is cleared while do_table_event() is still running. In that window raid_set_free() can free the raid_set, leading to a use-after-free when do_table_event() accesses rs->ti->table. Flush event_work whenever it has been initialized, so raid_set is not freed while the event handler is still running. The .func guard skips the flush for mddev instances where INIT_WORK has not run yet. Fixes: db29d79b34d9 ("dm-raid: delay flushing event_work() after reconfig_mutex is released") Cc: stable@vger.kernel.org Signed-off-by: Li Youhong Signed-off-by: Mikulas Patocka --- drivers/md/dm-raid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/md/dm-raid.c b/drivers/md/dm-raid.c index b973181bdce2..e44d92462aff 100644 --- a/drivers/md/dm-raid.c +++ b/drivers/md/dm-raid.c @@ -3334,7 +3334,7 @@ static void raid_dtr(struct dm_target *ti) rs->md.dm_gendisk = NULL; mddev_unlock(&rs->md); - if (work_pending(&rs->md.event_work)) + if (rs->md.event_work.func) flush_work(&rs->md.event_work); raid_set_free(rs); } From e92cd41e8e5ee5616c6c7cd4378661d942f0a7c0 Mon Sep 17 00:00:00 2001 From: Daejun Park Date: Thu, 12 Nov 2026 10:35:29 +0900 Subject: [PATCH 34/34] dm: fail a REQ_PREFLUSH bio with data when its preflush fails For a bio with REQ_PREFLUSH and data, dm core may send an empty flush first. If the flush fails, __dm_io_complete() still reissues the bio without REQ_PREFLUSH and drops the error. When that write succeeds, the XFS log keeps running and fsync() can return 0. End the bio with the flush error instead of writing the data, as blk-mq does. Fixes: 6a8736d10cb4 ("dm: relax ordering of bio-based flush implementation") Cc: stable@vger.kernel.org Signed-off-by: Daejun Park Acked-by: Tejun Heo Signed-off-by: Mikulas Patocka --- drivers/md/dm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index a73355c0125c..38eaae8ac9b4 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -983,7 +983,7 @@ static void __dm_io_complete(struct dm_io *io, bool first_stage) if (requeued) return; - if (unlikely(requeue_flush_with_data)) { + if (unlikely(requeue_flush_with_data) && !io_error) { /* * Preflush done for flush with data, reissue * without REQ_PREFLUSH. @@ -991,7 +991,7 @@ static void __dm_io_complete(struct dm_io *io, bool first_stage) bio->bi_opf &= ~REQ_PREFLUSH; queue_io(md, bio); } else { - /* done with normal IO or empty flush */ + /* done with normal IO, empty flush or failed preflush */ if (io_error) bio->bi_status = io_error; bio_endio(bio);