Skip to content

mountinfo: add MountOf, Submounts, Info helpers - #283

Draft
kolyshkin wants to merge 5 commits into
moby:mainfrom
kolyshkin:statmount
Draft

kolyshkin wants to merge 5 commits into
moby:mainfrom
kolyshkin:statmount

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Draft: waiting for Listmount/Statmount in golang.org/x/sys, see https://go-review.googlesource.com/c/sys/+/863921. Until then, the first commit adds a replace to a local x/sys checkout, so CI fails. Once the CL is merged, that commit will be replaced by an x/sys bump (which requires Go 1.26).

This adds a new API for the most common use of this package (besides Mounted): finding the mount a given path belongs to, and its properties (such as propagation). Today, users do it by GetMounts(ParentsFilter(path)) plus choosing the longest mount point (moby, containerd, cri-o, nri, buildx), or by SingleEntryFilter (which returns the bottom entry when several mounts are stacked), and then parse Optional by hand.

  • MountOf(path) returns the mount the path belongs to (the topmost one, if mounts are stacked):
    • Linux 6.13+: statx(STATX_MNT_ID_UNIQUE) + statmount(2), without reading mountinfo;
    • Linux 5.8+: statx(STATX_MNT_ID) + mountinfo lookup by mount ID;
    • otherwise (and on BSDs): ParentsFilter + longest mount point.
  • Info gets UniqueID and ParentUniqueID fields (filled in when statmount is used).
  • Info gets PeerGroup, Master, PropagateFrom and Unbindable methods to parse Optional.
  • Submounts(path) (using listmount(2) on a subtree), for users which unmount everything under a path (containerd, buildah, snapshotters).
  • An internal statmount backend converts statmount(2) results to Info so that it is identical to what is parsed from mountinfo; this is tested against mountinfo for all mounts.

GetMounts is intentionally not switched to listmount/statmount: for a full table, it is 2–2.7x slower than parsing mountinfo.

Benchmark (ns/op, Linux 7.2, 4 CPUs; with 1034 mounts, the path is on the last one, like a rootfs of a just started container):

34 mounts 1034 mounts 1034 mounts + 4x mount/umount loop
MountOf (statmount) 1660 1914 2812
statx + mount ID 25456 704318 941112
ParentsFilter 29406 717812 977935

Related: #281 (use statx in Mounted).

This is a temporary replace until Listmount and Statmount are merged
into golang.org/x/sys.

Note that current x/sys requires Go 1.26, so go mod tidy bumped the
go directive.

Signed-off-by: Kir Kolyshkin <[email protected]>
Add an internal implementation of getting mount information using
listmount(2) and statmount(2), converting the results to Info in a way
that it is identical to what is parsed from /proc/self/mountinfo
(this mimics show_mountinfo from the kernel's fs/proc_namespace.c).

Since some statmount fields (fs subtype, sb source) are only available
since Linux 6.13, statmount is only considered supported if it returns
all the needed information.

Add UniqueID and ParentUniqueID fields to Info. These are only filled
in when statmount is used.

Add a test comparing the results with mountinfo parsing, and a
benchmark. Note that for getting all mounts, statmount is slower than
parsing mountinfo (47.7 vs 27.3 µs for 33 mounts), so it is not used
by GetMounts. The intended use is for targeted queries.

Signed-off-by: Kir Kolyshkin <[email protected]>
Many users need to find the mount a given path belongs to (and its
properties, e.g. propagation or source). Currently this is done by
GetMounts(ParentsFilter(path)) and choosing an entry with the longest
mount point (moby, containerd, cri-o, nri, buildx etc.), or by
SingleEntryFilter (which returns the bottommost entry when several
mounts are stacked on the same mount point).

Add MountOf which does it faster and more correctly:
 - on Linux 6.13+, statx(STATX_MNT_ID_UNIQUE) + statmount;
 - on Linux 5.8+, statx(STATX_MNT_ID) + mountinfo search by mount ID;
 - otherwise (and on BSDs), ParentsFilter + longest mount point,
   choosing the topmost one if several mounts are stacked.

With statmount, the cost of MountOf does not depend on the number of
mounts, while the mountinfo-based approaches are O(n) and are further
slowed down by concurrent mounts/unmounts (as on a busy container host).
Benchmark results (ns/op, Linux 7.2, 4 CPUs; with 1034 mounts, the path
is on the last one, like a rootfs of a just started container):

                       34 mounts      1034 mounts   1034 mounts +
                                                    4x mount/umount loop
  MountOf (statmount)       1660             1914                2812
  statx + mount ID         25456           704318              941112
  ParentsFilter            29406           717812              977935

Signed-off-by: Kir Kolyshkin <[email protected]>
Many users parse the Optional field by hand to figure out mount
propagation (moby, cri-o, nri, k8sd, rootlesskit, u-root), sometimes
incorrectly (e.g. strings.Contains(Optional, "shared:1") also
matches "shared:12").

Add PeerGroup, Master, PropagateFrom and Unbindable methods to Info.
These do not allocate.

Together with MountOf, this gives a simple way to find out the
propagation of a mount a given path belongs to.

Add a unit test, and a test for real mounts (requires root).

Signed-off-by: Kir Kolyshkin <[email protected]>
Many users need to find all mounts on and under a given path, usually
to unmount them (containerd, buildah, nerdbox, werf, snapshotters).
Currently this is done by GetMounts(PrefixFilter(path)) (which requires
a normalized path), or by a custom filter.

Add Submounts which returns the same result as
GetMounts(PrefixFilter(path)), except path does not have to be
normalized. On Linux 6.13+, if path is a mount point, it uses statx,
listmount and statmount, and avoids reading mountinfo. Otherwise
(including the case when path is not a mount point, as then the
subtree to look at might be as large as the whole mount table), it
falls back to using PrefixFilter.

Benchmark results (ns/op, Linux 7.2, 4 CPUs; the path is a mount point
with 10 submounts, like a container rootfs):

                       1044 mounts   1044 mounts +
                                     4x mount/umount loop
  Submounts                  22876          32059
  PrefixFilter              631660         782542

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin kolyshkin changed the title mountinfo: add MountOf (using statmount), Info propagation helpers mountinfo: add MountOf, Submounts, Info helpers Nov 18, 2026
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