Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm persistent data: leaked new block in __shadow_block() on
  error
--

commit 03aa675fd4998658022986a6e963e9c3d3429569
Author: Denis V. Lunev <[email protected]>

dm persistent data: put back the reference a failed shadow dropped

This commit restores the original block's reference count via
dm_sm_inc_block() when __shadow_block() fails to read the original block
or write-lock the new block.

> diff --git a/drivers/md/persistent-data/dm-transaction-manager.c 
> b/drivers/md/persistent-data/dm-transaction-manager.c
> index 7401add01d12e..cf203ebb5110e 100644
> --- a/drivers/md/persistent-data/dm-transaction-manager.c
> +++ b/drivers/md/persistent-data/dm-transaction-manager.c
> @@ -303,7 +303,7 @@ static int __shadow_block(struct dm_transaction_manager 
> *tm, dm_block_t orig,
>  
>       r = dm_bm_read_lock(tm->bm, orig, v, &orig_block);
>       if (r < 0)
> -             return r;
> +             goto out_put_orig;
>  
>       /*
>        * It would be tempting to use dm_bm_unlock_move here, but some
> @@ -315,13 +315,18 @@ static int __shadow_block(struct dm_transaction_manager 
> *tm, dm_block_t orig,
>       r = dm_bm_write_lock_zero(tm->bm, new, v, result);
>       if (r) {
>               dm_bm_unlock(orig_block);
> -             return r;
> +             goto out_put_orig;
>       }
>  
>       memcpy(dm_block_data(*result), dm_block_data(orig_block),
>              dm_bm_block_size(tm->bm));
>  
>       dm_bm_unlock(orig_block);
> +     return 0;
> +
> +out_put_orig:
> +     /* The caller goes on using orig when the shadow fails. */
> +     dm_sm_inc_block(tm->sm, orig);
>       return r;
>  }

[Severity: Medium]
Does this leak the newly allocated block new in the space map when
__shadow_block() fails?

__shadow_block() allocates new via dm_sm_new_block(tm->sm, &new) with a
reference count of 1 before decrementing orig:

drivers/md/persistent-data/dm-transaction-manager.c:__shadow_block() {
        r = dm_sm_new_block(tm->sm, &new);
        if (r < 0)
                return r;

        r = dm_sm_dec_block(tm->sm, orig);
        if (r < 0)
                return r;
    ...
}

Unlike dm_tm_new_block(), which calls dm_sm_dec_block(tm->sm, new_block)
on failure, neither out_put_orig nor the dm_sm_dec_block(tm->sm, orig)
error path releases new.  Once the target recovers from the transient
failure and commits a later transaction, new remains permanently allocated
in the on-disk space map with nothing referencing it.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to