[Concept,16/33] boot: Close the filesystem after reading a bootflow

Message ID 20260416023021.626949-17-sjg@u-boot.org
State New
Headers
Series Fix memory leaks and test pollution in sandbox tests |

Commit Message

Simon Glass April 16, 2026, 2:29 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

bootdev_find_in_blk() calls fs_set_blk_dev_with_part() to probe the
partition and leaves the mount active for the bootmeth. Most bootmeths
call fs_size()/fs_read() (which close the fs via fs_legacy), and those
that do their own lookups re-mount via bootmeth_setup_fs(). But every
call chain ends up with one mount still open - nothing calls fs_close()
to release it. For ext4 that leaks the jbd2 journal, revoke tables and
mballoc state every time a partition is scanned - hundreds of KB across
the test suite.

Call fs_close() after bootmeth_read_bootflow(). It is a no-op if the
fs was already closed (fs_type reset to FS_TYPE_ANY).

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 boot/bootdev-uclass.c | 9 +++++++++
 1 file changed, 9 insertions(+)
  

Patch

diff --git a/boot/bootdev-uclass.c b/boot/bootdev-uclass.c
index 72f9a315c04..faed1f3e5df 100644
--- a/boot/bootdev-uclass.c
+++ b/boot/bootdev-uclass.c
@@ -191,6 +191,15 @@  int bootdev_find_in_blk(struct udevice *dev, struct udevice *blk,
 	log_debug("using method %s\n", bflow->method->name);
 	ret = bootmeth_read_bootflow(bflow->method, bflow);
 	log_debug("method %s returned ret=%d\n", bflow->method->name, ret);
+
+	/*
+	 * Close any filesystem left mounted by the bootmeth. This is a no-op
+	 * if the last fs operation already closed, but ensures a dangling
+	 * mount (e.g. from fs_set_blk_dev_with_part() above when the
+	 * bootmeth performed no fs_size()/fs_read()) is released.
+	 */
+	fs_close();
+
 	if (ret)
 		return log_msg_ret("method", ret);