Skip to content

mountinfo: use statx in Mounted and MountedFast - #281

Open
kolyshkin wants to merge 1 commit into
moby:mainfrom
kolyshkin:mounted-statx
Open

kolyshkin wants to merge 1 commit into
moby:mainfrom
kolyshkin:mounted-statx

Conversation

@kolyshkin

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. Tested on Linux 7.3 (as user) and 7.2 (as root).

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).

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.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
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.

1 participant