Skip to contentSkip to conversation

Fix dm_array_cursor skipping and error handling

PATCH In review Ming-Hung Tsai 2 participants 7 messages Dec 3, 2024 – Dec 10, 2024 Device mapper

lore
Share & tools
lore
#0 lore raw

cover Fix dm_array_cursor skipping and error handling

This patch series addresses a positioning bug in dm_array_cursor_skip() and two other issues regarding error handling.

Ming-Hung Tsai (3):
dm array: fix releasing a faulty array block twice in
dm_array_cursor_end
dm array: fix unreleased btree blocks on closing a faulty array cursor dm array: fix cursor index when skipping across block boundaries

 drivers/md/persistent-data/dm-array.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/3 dm array: fix releasing a faulty array block twice in dm_array_cursor_end

When dm_bm_read_lock() fails due to locking or checksum errors, it releases the faulty block implicitly while leaving an invalid output pointer behind. The caller of dm_bm_read_lock() should not operate on this invalid dm_block pointer, or it will lead to undefined result. For example, the dm_array_cursor incorrectly caches the invalid pointer on reading a faulty array block, causing a double release in dm_array_cursor_end(), then hitting the BUG_ON in dm-bufio cache_put().

Reproduce steps:

1. initialize a cache device

dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" dmsetup create cdata --table "0 65536 linear /dev/sdc 8192" dmsetup create corig --table "0 524288 linear /dev/sdc $262144" dd if=/dev/zero of=/dev/mapper/cmeta bs=4k count=1
dmsetup create cache --table "0 524288 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 writethrough smq 0"

2. wipe the second array block offline

dmsteup remove cache cmeta cdata corig
mapping_root=$(dd if=/dev/sdc bs=1c count=8 skip=192 \
2>/dev/null | hexdump -e '1/8 "%u\n"')
ablock=$(dd if=/dev/sdc bs=1c count=8 skip=$((4096*mapping_root+2056)) \
2>/dev/null | hexdump -e '1/8 "%u\n"')
dd if=/dev/zero of=/dev/sdc bs=4k count=1 seek=$ablock

3. try reopen the cache device

dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" dmsetup create cdata --table "0 65536 linear /dev/sdc 8192" dmsetup create corig --table "0 524288 linear /dev/sdc $262144" dmsetup create cache --table "0 524288 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 writethrough smq 0"

Kernel logs:

(snip)
device-mapper: array: array_block_check failed: blocknr 0 != wanted 10
device-mapper: block manager: array validator check failed for block 10
device-mapper: array: get_ablock failed
device-mapper: cache metadata: dm_array_cursor_next for mapping failed

------------[ cut here ]------------
kernel BUG at drivers/md/dm-bufio.c:638!

Fix by setting the cached block pointer to NULL on errors.

Fixes fdd1315aa5f0 ("dm array: introduce cursor api")
---
 drivers/md/persistent-data/dm-array.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/persistent-data/dm-array.c +7 -2
index 157c9bd2fed7..c46a374f7459 100644
@@ -917,23 +917,28 @@ static int load_ablock(struct dm_array_cursor *c)
 	if (c->block)
 		unlock_ablock(c->info, c->block);
 
-	c->block = NULL;
-	c->ab = NULL;
 	c->index = 0;
 
 	r = dm_btree_cursor_get_value(&c->cursor, &key, &value_le);
 	if (r) {
 		DMERR("dm_btree_cursor_get_value failed");
 		dm_btree_cursor_end(&c->cursor);
+		goto out;
 
 	} else {
 		r = get_ablock(c->info, le64_to_cpu(value_le), &c->block, &c->ab);
 		if (r) {
 			DMERR("get_ablock failed");
 			dm_btree_cursor_end(&c->cursor);
+			goto out;
 		}
 	}
 
+	return 0;
+
+out:
+	c->block = NULL;
+	c->ab = NULL;
 	return r;
 }
 
Signature · 1 line
2.47.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] dm array: fix releasing a faulty array block twice in dm_array_cursor_end

Reviewed-by [email protected]
Quoted 93 lines · Ming-Hung Tsai

When dm_bm_read_lock() fails due to locking or checksum errors, it releases the faulty block implicitly while leaving an invalid output pointer behind. The caller of dm_bm_read_lock() should not operate on this invalid dm_block pointer, or it will lead to undefined result. For example, the dm_array_cursor incorrectly caches the invalid pointer on reading a faulty array block, causing a double release in dm_array_cursor_end(), then hitting the BUG_ON in dm-bufio cache_put().

Reproduce steps:

1. initialize a cache device

dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" dmsetup create cdata --table "0 65536 linear /dev/sdc 8192" dmsetup create corig --table "0 524288 linear /dev/sdc $262144" dd if=/dev/zero of=/dev/mapper/cmeta bs=4k count=1
dmsetup create cache --table "0 524288 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 writethrough smq 0"

2. wipe the second array block offline

dmsteup remove cache cmeta cdata corig
mapping_root=$(dd if=/dev/sdc bs=1c count=8 skip=192 \
2>/dev/null | hexdump -e '1/8 "%u\n"')
ablock=$(dd if=/dev/sdc bs=1c count=8 skip=$((4096*mapping_root+2056)) \
2>/dev/null | hexdump -e '1/8 "%u\n"')
dd if=/dev/zero of=/dev/sdc bs=4k count=1 seek=$ablock

3. try reopen the cache device

dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" dmsetup create cdata --table "0 65536 linear /dev/sdc 8192" dmsetup create corig --table "0 524288 linear /dev/sdc $262144" dmsetup create cache --table "0 524288 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 128 2 metadata2 writethrough smq 0"

Kernel logs:

(snip)
device-mapper: array: array_block_check failed: blocknr 0 != wanted 10
device-mapper: block manager: array validator check failed for block 10
device-mapper: array: get_ablock failed
device-mapper: cache metadata: dm_array_cursor_next for mapping failed

------------[ cut here ]------------
kernel BUG at drivers/md/dm-bufio.c:638!

Fix by setting the cached block pointer to NULL on errors.

Fixes fdd1315aa5f0 ("dm array: introduce cursor api")
---
 drivers/md/persistent-data/dm-array.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/persistent-data/dm-array.c +7 -3
index 157c9bd2fed7..c46a374f7459 100644
@@ -917,23 +917,28 @@ static int load_ablock(struct dm_array_cursor *c)
        if (c->block)
                unlock_ablock(c->info, c->block);
 
-       c->block = NULL;
-       c->ab = NULL;
        c->index = 0;
 
        r = dm_btree_cursor_get_value(&c->cursor, &key, &value_le);
        if (r) {
                DMERR("dm_btree_cursor_get_value failed");
                dm_btree_cursor_end(&c->cursor);
+               goto out;
 
        } else {
                r = get_ablock(c->info, le64_to_cpu(value_le), &c->block, &c->ab);
                if (r) {
                        DMERR("get_ablock failed");
                        dm_btree_cursor_end(&c->cursor);
+                       goto out;
                }
        }
 
+       return 0;
+
+out:
+       c->block = NULL;
+       c->ab = NULL;
        return r;
 }
 
--

2.47.0

#3 lore raw

PATCH 2/3 dm array: fix unreleased btree blocks on closing a faulty array cursor

In reply to Ming-Hung Tsai

The cached block pointer in dm_array_cursor might be NULL if it reaches an unreadable array block, or the array is empty. Therefore, dm_array_cursor_end() should call dm_btree_cursor_end() unconditionally, to prevent leaving unreleased btree blocks.

Fixes fdd1315aa5f0 ("dm array: introduce cursor api")
---
 drivers/md/persistent-data/dm-array.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/md/persistent-data/dm-array.c +3 -3
index c46a374f7459..1c3af717ee4f 100644
@@ -961,10 +961,10 @@ EXPORT_SYMBOL_GPL(dm_array_cursor_begin);
 
 void dm_array_cursor_end(struct dm_array_cursor *c)
 {
-	if (c->block) {
+	if (c->block)
 		unlock_ablock(c->info, c->block);
-		dm_btree_cursor_end(&c->cursor);
-	}
+
+	dm_btree_cursor_end(&c->cursor);
 }
 EXPORT_SYMBOL_GPL(dm_array_cursor_end);
 
Signature · 1 line
2.47.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] dm array: fix unreleased btree blocks on closing a faulty array cursor

Reviewed-by [email protected]
Quoted 33 lines · Ming-Hung Tsai

The cached block pointer in dm_array_cursor might be NULL if it reaches an unreadable array block, or the array is empty. Therefore, dm_array_cursor_end() should call dm_btree_cursor_end() unconditionally, to prevent leaving unreleased btree blocks.

Fixes fdd1315aa5f0 ("dm array: introduce cursor api")
---
 drivers/md/persistent-data/dm-array.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/md/persistent-data/dm-array.c +3 -4
index c46a374f7459..1c3af717ee4f 100644
@@ -961,10 +961,10 @@ EXPORT_SYMBOL_GPL(dm_array_cursor_begin);
 
 void dm_array_cursor_end(struct dm_array_cursor *c)
 {
-       if (c->block) {
+       if (c->block)
                unlock_ablock(c->info, c->block);
-               dm_btree_cursor_end(&c->cursor);
-       }
+
+       dm_btree_cursor_end(&c->cursor);
 }
 EXPORT_SYMBOL_GPL(dm_array_cursor_end);
 
--

2.47.0

#5 lore raw

PATCH 3/3 dm array: fix cursor index when skipping across block boundaries

In reply to Ming-Hung Tsai

dm_array_cursor_skip() seeks to the target position by loading array blocks iteratively until the specified number of entries to skip is reached. When seeking across block boundaries, it uses dm_array_cursor_next() to step into the next block. dm_array_cursor_skip() must first move the cursor index to the end of the current block; otherwise, the cursor position could incorrectly remain in the same block, causing the actual number of skipped entries to be much smaller than expected.

This bug affects cache resizing in v2 metadata and could lead to data loss if the fast device is shrunk during the first-time resume. For example:

1. create a cache metadata consists of 32768 blocks, with a dirty block assigned to the second bitmap block. cache_restore v1.0 is required.

cat <<EOF >> cmeta.xml
<superblock uuid="" block_size="64" nr_cache_blocks="32768" \
policy="smq" hint_width="4">
<mappings>

    <mapping cache_block="32767" origin_block="0" dirty="true"/>

</mappings>
</superblock>
EOF
dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" cache_restore -i cmeta.xml -o /dev/mapper/cmeta --metadata-version=2

2. bring up the cache while attempt to discard all the blocks belonging to the second bitmap block (block# 32576 to 32767). The last command is expected to fail, but it actually succeeds.

dmsetup create cdata --table "0 2084864 linear /dev/sdc 8192" dmsetup create corig --table "0 65536 linear /dev/sdc 2105344" dmsetup create cache --table "0 65536 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 64 2 metadata2 writeback smq \
2 migration_threshold 0"

Fixes 9b696229aa7d ("dm persistent data: add cursor skip functions to the cursor APIs")
---
 drivers/md/persistent-data/dm-array.c | 1 +
 1 file changed, 1 insertion(+)
drivers/md/persistent-data/dm-array.c +1 -0
index 1c3af717ee4f..b293da766fb1 100644
@@ -1004,6 +1004,7 @@ int dm_array_cursor_skip(struct dm_array_cursor *c, uint32_t count)
 		}
 
 		count -= remaining;
+		c->index += (remaining - 1);
 		r = dm_array_cursor_next(c);
 
 	} while (!r);
Signature · 1 line
2.47.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] dm array: fix cursor index when skipping across block boundaries

Reviewed-by [email protected]
Quoted 59 lines · Ming-Hung Tsai

dm_array_cursor_skip() seeks to the target position by loading array blocks iteratively until the specified number of entries to skip is reached. When seeking across block boundaries, it uses dm_array_cursor_next() to step into the next block. dm_array_cursor_skip() must first move the cursor index to the end of the current block; otherwise, the cursor position could incorrectly remain in the same block, causing the actual number of skipped entries to be much smaller than expected.

This bug affects cache resizing in v2 metadata and could lead to data loss if the fast device is shrunk during the first-time resume. For example:

1. create a cache metadata consists of 32768 blocks, with a dirty block assigned to the second bitmap block. cache_restore v1.0 is required.

cat <<EOF >> cmeta.xml
<superblock uuid="" block_size="64" nr_cache_blocks="32768" \
policy="smq" hint_width="4">
<mappings>

    <mapping cache_block="32767" origin_block="0" dirty="true"/>

</mappings>
</superblock>
EOF
dmsetup create cmeta --table "0 8192 linear /dev/sdc 0" cache_restore -i cmeta.xml -o /dev/mapper/cmeta --metadata-version=2

2. bring up the cache while attempt to discard all the blocks belonging to the second bitmap block (block# 32576 to 32767). The last command is expected to fail, but it actually succeeds.

dmsetup create cdata --table "0 2084864 linear /dev/sdc 8192" dmsetup create corig --table "0 65536 linear /dev/sdc 2105344" dmsetup create cache --table "0 65536 cache /dev/mapper/cmeta \
/dev/mapper/cdata /dev/mapper/corig 64 2 metadata2 writeback smq \
2 migration_threshold 0"

Fixes 9b696229aa7d ("dm persistent data: add cursor skip functions to the cursor APIs")
---
 drivers/md/persistent-data/dm-array.c | 1 +
 1 file changed, 1 insertion(+)
drivers/md/persistent-data/dm-array.c +1 -1
index 1c3af717ee4f..b293da766fb1 100644
@@ -1004,6 +1004,7 @@ int dm_array_cursor_skip(struct dm_array_cursor *c, uint32_t count)
                }
 
                count -= remaining;
+               c->index += (remaining - 1);
                r = dm_array_cursor_next(c);
 
        } while (!r);
--

2.47.0