Skip to content

btrfs-progs: fix btrfs_path leak, part 2

Qu Wenruo linux-btrfs 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest12 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/12btrfs-progs: kernel-shared: use BTRFS_PATH_AUTO_RELEASE() for on-stack pathskernel-shared/extent-tree.c kernel-shared/file.c kernel-shared/free-space-cache.c +4 more+45 -780 replies
2/12btrfs-progs: common, convert, image: use BTRFS_PATH_AUTO_RELEASE() for on-stack pathscommon/clear-cache.c common/root-tree-utils.c convert/main.c +5 more+41 -810 replies
3/12btrfs-progs: mkfs: use BTRFS_PATH_AUTO_RELEASE() for on-stack pathsmkfs/main.c mkfs/rootdir.c+26 -520 replies
4/12btrfs-progs: tune: use BTRFS_PATH_AUTO_RELEASE() for on-stack pathstune/change-csum.c tune/change-uuid.c tune/convert-remap-tree.c +1 more+39 -800 replies
5/12btrfs-progs: cmds: use BTRFS_PATH_AUTO_RELEASE() for on-stack pathscmds/device.c cmds/inspect-dump-tree.c cmds/rescue-chunk-recover.c +3 more+78 -1290 replies
6/12btrfs-progs: check: use BTRFS_PATH_AUTO_RELEASE() for repair, qgroup-verify and mode-commoncheck/mode-common.c check/qgroup-verify.c check/repair.c+62 -1070 replies
7/12btrfs-progs: check: use BTRFS_PATH_AUTO_RELEASE() for lowmem modecheck/mode-lowmem.c+66 -1360 replies
8/12btrfs-progs: check: use BTRFS_PATH_AUTO_RELEASE() for original modecheck/main.c+71 -1470 replies
9/12btrfs-progs: check: fix fixup_extent_flags() retry pathcheck/main.c+9 -80 replies
10/12btrfs-progs: fix missing transaction commit in truncate_free_ino_items()common/clear-cache.c+6 -20 replies
11/12btrfs-progs: dump-tree: fix exit code and leak on open failurecmds/inspect-dump-tree.c+3 -20 replies
12/12btrfs-progs: check: return error if fill_csum_tree_from_fs() fails to read a rootcheck/mode-common.c+4 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 00/12] btrfs-progs: fix btrfs_path leak, part 2

This is the part 2, focusing on the on-stack btrfs_path.

Unlike heap-allocated btrfs_path, which needs proper btrfs_free_path() call, or the btrfs_path itself along with the held ebs will leak.

The on-stack one only leaks the held ebs, but still we should fix them.

Just by the amount of code, one should know this is largely generated by LLM, although I spent more time drafting the plan and review the final patch.

There are some extra notes other than just converting existing "btrfs_path" on-stack declaration to BTRFS_PATH_AUTO_RELEASE:

- If a label is only for cleanup, remove the label when possible As a lot of call sites can directly return without jumping to the cleanup label.

Also cleanup some "ret = x; return ret;" patterns.

- Keep dedicated btrfs_release_path() if needed
As our C code is not fully aligning the path lifespan with the scope, we still need to manually release path halfway for certain call sites.

E.g. before calling btrfs_commit_transaction(), btrfs_abort_transaction(), btrfs_search_slot(), close_ctree() or before lowmem mode modifying the tree.

Releasing the path before btrfs_commit_transaction() is to follow the kernel convension. There are a lot of existing call sites breaking it, so here we do not intentionally fix them.

This will require a dedicated change in the future.

- Reduce the lifespan of btrfs_path when possible
E.g. the path is only utilized inside a loop or if () branch, declare path there.

This applies to patch 01~08, which are all straightforward conversion with minor cleanups.

After that, the code review process exposed some other bugs in the touched functions, which are all pretty simple and low severity ones. Patch 09~12 are to fix those minor bugs.

Qu Wenruo (12):
btrfs-progs: kernel-shared: use BTRFS_PATH_AUTO_RELEASE() for on-stack
paths
btrfs-progs: common, convert, image: use BTRFS_PATH_AUTO_RELEASE() for
on-stack paths
btrfs-progs: mkfs: use BTRFS_PATH_AUTO_RELEASE() for on-stack paths
btrfs-progs: tune: use BTRFS_PATH_AUTO_RELEASE() for on-stack paths
btrfs-progs: cmds: use BTRFS_PATH_AUTO_RELEASE() for on-stack paths
btrfs-progs: check: use BTRFS_PATH_AUTO_RELEASE() for repair,
qgroup-verify and mode-common
btrfs-progs: check: use BTRFS_PATH_AUTO_RELEASE() for lowmem mode
btrfs-progs: check: use BTRFS_PATH_AUTO_RELEASE() for original mode
btrfs-progs: check: fix fixup_extent_flags() retry path
btrfs-progs: fix missing transaction commit in
truncate_free_ino_items()
btrfs-progs: dump-tree: fix exit code and leak on open failure
btrfs-progs: check: return error if fill_csum_tree_from_fs() fails to
read a root

 check/main.c                     | 235 +++++++++++--------------------
 check/mode-common.c              | 141 +++++++------------
 check/mode-lowmem.c              | 202 +++++++++-----------------
 check/qgroup-verify.c            |  29 ++--
 check/repair.c                   |   5 +-
 cmds/device.c                    |  11 +-
 cmds/inspect-dump-tree.c         |  11 +-
 cmds/rescue-chunk-recover.c      |  43 ++----
 cmds/rescue-fix-data-checksum.c  |  10 +-
 cmds/rescue.c                    |   7 +-
 cmds/restore.c                   | 126 +++++++----------
 common/clear-cache.c             |  79 ++++-------
 common/root-tree-utils.c         |  10 +-
 convert/main.c                   |   9 +-
 convert/source-ext2.c            |   3 +-
 convert/source-fs.c              |   3 +-
 image/common.c                   |   2 +-
 image/image-create.c             |   2 +-
 image/image-restore.c            |  20 +--
 kernel-shared/extent-tree.c      |  62 +++-----
 kernel-shared/file.c             |   3 +-
 kernel-shared/free-space-cache.c |  24 ++--
 kernel-shared/inode.c            |   8 +-
 kernel-shared/print-tree.c       |   9 +-
 kernel-shared/volumes.c          |  15 +-
 mkfs/main.c                      |  42 ++----
 mkfs/rootdir.c                   |  36 ++---
 tests/btree-test.c               |   2 +-
 tune/change-csum.c               |  50 ++-----
 tune/change-uuid.c               |  38 ++---
 tune/convert-remap-tree.c        |  12 +-
 tune/quota.c                     |  19 ++-
 32 files changed, 447 insertions(+), 821 deletions(-)

--
2.56.0