btrfs: fix memory leaks after failure to lookup checksums during inode logging
PATCH 1/1 btrfs: fix memory leaks after failure to lookup checksums during inode logging
From: Filipe Manana <[email protected]>
While logging an inode, at copy_items(), if we fail to lookup the checksums for an extent we release the destination path, free the ins_data array and then return immediately. However a previous iteration of the for loop may have added checksums to the ordered_sums list, in which case we leak the memory used by them.
So fix this by making sure we iterate the ordered_sums list and free all its checksums before returning.
--- fs/btrfs/tree-log.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
fs/btrfs/tree-log.c +2 -6
@@ -4035,11 +4035,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, fs_info->csum_root, ds + cs, ds + cs + cl -1 , &ordered_sums, 0); -if (ret) { -btrfs_release_path (dst_path); -kfree (ins_data); -return ret; - } +if (ret) +break ; } } }
@@ -4052,7 +4049,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,* we have to do this after the loop above to avoid changing the * log tree while trying to change the log tree. */ - ret =0 ;while (!list_empty (&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, struct btrfs_ordered_sum,
Signature · 1 line
2.26.2
Quoted 19 lines · On 29/07/2020 11:18, [email protected] wrote:
fs/btrfs/tree-log.c +2
-5@@ -4035,11 +4035,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, fs_info->csum_root, ds + cs, ds + cs + cl -1 , &ordered_sums, 0); -if (ret) { -btrfs_release_path (dst_path); -kfree (ins_data); -return ret; - } +if (ret) +break ; } } }
fs/btrfs/tree-log.c static noinline int copy_items(struct btrfs_trans_handle *trans, · line 4053
Quoted 6 lines
fs/btrfs/tree-log.c +0
-1@@ -4052,7 +4049,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,* we have to do this after the loop above to avoid changing the * log tree while trying to change the log tree. */ - ret =0 ;while (!list_empty (&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
Isn't there a potential that ret get overridden by this hunk:
while (!list_empty(&ordered_sums)) {
struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
struct btrfs_ordered_sum,
list);
if (!ret)
ret = log_csums(trans, inode, log, sums);
list_del(&sums->list);
kfree(sums);
}
return ret;and we're potentially returning 0 instead of the -ENOMEM from 'btrfs_lookup_csums_range'?
Maybe we should do something like this:
while (!list_empty(&ordered_sums)) {
struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
struct btrfs_ordered_sum,
list);
if (!ret)
ret2 = log_csums(trans, inode, log, sums);
list_del(&sums->list);
kfree(sums);
}
return ret2 ? ret2 : ret;Thanks,
Johannes
Quoted 30 lines · Johannes Thumshirn
Quoted 19 lines · On 29/07/2020 11:18, [email protected] wrote:
fs/btrfs/tree-log.c +2
-5@@ -4035,11 +4035,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, fs_info->csum_root, ds + cs, ds + cs + cl -1 , &ordered_sums, 0); -if (ret) { -btrfs_release_path (dst_path); -kfree (ins_data); -return ret; - } +if (ret) +break ; } } }fs/btrfs/tree-log.c static noinline int copy_items(struct btrfs_trans_handle *trans, · line 4053
Quoted 6 lines
fs/btrfs/tree-log.c +0
-1@@ -4052,7 +4049,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,* we have to do this after the loop above to avoid changing the * log tree while trying to change the log tree. */ - ret =0 ;while (!list_empty (&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,Isn't there a potential that ret get overridden by this hunk:
Quoted 9 lines
while (!list_empty(&ordered_sums)) {
struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
struct btrfs_ordered_sum,
list);
if (!ret)
ret = log_csums(trans, inode, log, sums);
list_del(&sums->list);
kfree(sums);
}Nop, when ret is non-zero it never gets overwritten.
Quoted 4 lines
return ret;and we're potentially returning 0 instead of the
-ENOMEMfrom 'btrfs_lookup_csums_range'?
Nop, for the same reason mentioned before.
Thanks.
Quoted 17 lines
Maybe we should do something like this:
while (!list_empty(&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, struct btrfs_ordered_sum, list); if (!ret) ret2 = log_csums(trans, inode, log, sums); list_del(&sums->list); kfree(sums); } return ret2 ? ret2 : ret;Thanks,
Johannes
Johannes Thumshirn
Reviewed-byQuoted 42 lines · Filipe Manana
Quoted 30 lines · Johannes Thumshirn
Quoted 19 lines · On 29/07/2020 11:18, [email protected] wrote:
fs/btrfs/tree-log.c +2
-5@@ -4035,11 +4035,8 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, fs_info->csum_root, ds + cs, ds + cs + cl -1 , &ordered_sums, 0); -if (ret) { -btrfs_release_path (dst_path); -kfree (ins_data); -return ret; - } +if (ret) +break ; } } }fs/btrfs/tree-log.c static noinline int copy_items(struct btrfs_trans_handle *trans, · line 4053
Quoted 6 lines
fs/btrfs/tree-log.c +0
-1@@ -4052,7 +4049,6 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,* we have to do this after the loop above to avoid changing the * log tree while trying to change the log tree. */ - ret =0 ;while (!list_empty (&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,Isn't there a potential that ret get overridden by this hunk:
Quoted 9 lines
while (!list_empty(&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, struct btrfs_ordered_sum, list); if (!ret) ret = log_csums(trans, inode, log, sums); list_del(&sums->list); kfree(sums); }
Quoted 1 line
Nop, when ret is non-zero it never gets overwritten.
You're absolutely right, I'm sorry.
Quoted 8 lines · On Wed, Jul 29, 2020 at 10:17:50AM +0100, [email protected] wrote:
From: Filipe Manana <[email protected]>
While logging an inode, at
copy_items(), if we fail to lookup the checksums for an extent we release the destination path, free theins_dataarray and then return immediately. However a previous iteration of the for loop may have added checksums to theordered_sumslist, in which case we leak the memory used by them.
Quoted 5 lines
So fix this by making sure we iterate the
ordered_sumslist and free all its checksums before returning.Fixes 3650860b90cc2a ("Btrfs: remove almost all of the BUG()'s from tree-log.c")Signed-off-by Filipe Manana <[email protected]>
Added to misc-next, thanks.