Skip to content

mountinfo: use statx in Mounted and MountedFast - #281

Merged
thaJeztah merged 1 commit into
moby:mainfrom
kolyshkin:mounted-statx
Dec 1, 2026
Merged

thaJeztah merged 1 commit into
moby:mainfrom
kolyshkin:mounted-statx

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Since Linux v5.8, statx(2) reports the STATX_ATTR_MOUNT_ROOT attribute, which tells whether a path is the root of a mount. It works for all kinds of mounts, including bind mounts, and takes a single syscall.

This PR makes it the first method tried by Mounted and MountedFast:

  • The path is only made absolute and clean (filepath.Abs), not fully normalized: the kernel resolves symlinks itself, and filepath.EvalSymlinks (an lstat per path component) was the most expensive part of Mounted. .. is still handled lexically, as before.
  • AT_NO_AUTOMOUNT is used so that checking an automount point does not trigger the automount; this is consistent with the existing methods (verified with /sys/kernel/debug/tracing on tracefs).
  • If statx fails for any reason (old kernel, seccomp, non-existent path) or the kernel does not report STATX_ATTR_MOUNT_ROOT, the existing chain (openat2 → stat → mountinfo) is used, so returned errors are unchanged.

Result: Mounted is about 10x faster (523 vs 5056 ns/op for a path with 6 components) 👯.

Tests: mountedByStatx is added to TestMountedBy (which covers bind mounts of dirs, files and sockets, symlinks, etc. when run as root), and a new TestMountedByStatxVsMountinfo checks it against every entry in mountinfo.

This comment was marked as resolved.

This comment was marked as resolved.

Comment thread mountinfo/mounted_linux.go Resolved

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

This comment was marked as resolved.

@vvoland

vvoland commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Looks like Copilot reviews are worth addressing?

@thaJeztah

Copy link
Copy Markdown
Member
=== RUN   TestMountedRootChroot
    mounted_linux_test.go:558: child failed: exit status 2
        === RUN   TestMountedRootChroot
        --- PASS: TestMountedRootChroot (0.00s)
        PASS
        error setting GOCOVERDIR: bad os.MkdirTemp return: stat /tmp: no such file or directory
        warning: GOCOVERDIR not set, no coverage data emitted
--- FAIL: TestMountedRootChroot (0.00s)

@thaJeztah
thaJeztah self-requested a review November 24, 2026 21:27
@thaJeztah

Copy link
Copy Markdown
Member

@kolyshkin can you have a look at the failure?

@kolyshkin
kolyshkin marked this pull request as draft November 25, 2026 06:15
@kolyshkin

kolyshkin commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

@kolyshkin can you have a look at the failure?

yup, that was due to go test -coverprofile which sets GOCOVERDIR=/tmp/..... and the lack of /tmp inside chroot in TestMountedRootChroot. Should be fixed now, sorry it took me so long, holidays and other stuff.

@kolyshkin
kolyshkin marked this pull request as ready for review November 28, 2026 20:51
@kolyshkin

Copy link
Copy Markdown
Collaborator Author

@vvoland @thaJeztah PTAL

This comment was marked as resolved.

Since Linux v5.8, statx(2) reports STATX_ATTR_MOUNT_ROOT attribute,
which tells whether the path is a root of a mount. This works for all
kinds of mounts, including bind mounts, and requires a single syscall.

Use it as the first method in Mounted and MountedFast, falling back to
existing methods (openat2, stat, mountinfo) if statx fails for any
reason, or the kernel does not report STATX_ATTR_MOUNT_ROOT. The errors
are not returned from statx to keep the existing error semantics
(those come from normalizePath used by the fallback code). The only
exception is a magic link such as /proc/self/fd/N pointing to an
unlinked file: statx follows it, while normalizePath returns ENOENT.

Since the path is not normalized, it may resolve to root (e.g. a symlink
to /), which is always mounted but may not be a mount root (in chroot).
Handle this case by comparing the path's device, inode, and mount ID
with those of root, if statx says it is not a mount root. The mount ID
is needed to tell root from the same directory seen via a bind mount of
its parent.

Since the kernel resolves symlinks, there is no need to normalize the
path (which is relatively costly, as filepath.EvalSymlinks does lstat
on every path component), only to make it absolute and clean (so that
".." is handled the same way as before).

AT_NO_AUTOMOUNT is used to avoid triggering an automount, which is
consistent with the other methods (and avoids side effects).

As a result, Mounted is about 10x faster (523 vs 5056 ns/op on a
path with 6 components).

Add the new method to the tests. In TestMountedBy*VsMountinfo, skip
mounts hidden by an overmount (as their entries remain in mountinfo).
Add test cases for a symlink to root in chroot, and for a bind-mounted
alias of root in chroot.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin

Copy link
Copy Markdown
Collaborator Author

Addressed the last round of copilot reviews.

I'd like to cut a new mountinfo release once this is merged.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The Linux mount-detection changes have no active review issues.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@kolyshkin

Copy link
Copy Markdown
Collaborator Author

@vvoland @thaJeztah PTAL

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thx!

@thaJeztah
thaJeztah merged commit 115ac58 into moby:main Dec 1, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants