Skip to contentSkip to conversation

dm-cache: fix ending a cursor that never began

PATCH In review Mikulas Patocka 2 participants 2 messages Nov 6 – Nov 11 Device mapper

lore
Share & tools
lore

Mikulas Patocka

author maintainer
#0 lore raw

PATCH 1/1 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.

Assisted-by Claude:claude-opus-5
Fixes 629d0a8a1a10 ("dm cache metadata: add "metadata2" feature")
---
 drivers/md/dm-cache-metadata.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/dm-cache-metadata.c +2 -1
Index: linux-2.6/drivers/md/dm-cache-metadata.c
===================================================================
@@ -1433,7 +1433,8 @@ static int __load_mappings(struct dm_cac
 					   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;
 		}
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Mikulas Patocka

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.

Assisted-by Claude:claude-opus-5
Fixes 629d0a8a1a10 ("dm cache metadata: add "metadata2" feature")
---
 drivers/md/dm-cache-metadata.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Index: linux-2.6/drivers/md/dm-cache-metadata.c
===================================================================

drivers/md/dm-cache-metadata.c static int __load_mappings(struct dm_cac · line 1440

Quoted 9 lines
drivers/md/dm-cache-metadata.c +2 -1
@@ -1433,7 +1433,8 @@ static int __load_mappings(struct dm_cac
                                           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;
                }

This corner case occurs when the metadata is not clean and the dirty bitset gets corrupted. Although calling dm_array_cursor_end() is safe because hint_cursor is zero-initialized during dm_cache_metadata allocation, adding an explicit check is a reasonable safeguard.