GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — mountinfo.Mounted: add fast path for Linux using openat2 by kolyshkin · Pull Request #29 · moby/sys

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Sep 8, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

A new (as of Linux 5.6) openat2(2) system call can be used to check
whether a path is a mount point or not, and it even works for bind
mounts!

This relies on the fact that openat2() with RESOLVE_NO_XDEV option
won't cross the filesystem boundary (that includes bind mounts).
For directories, we try to open a parent directory (..), and for
files, a directory the file is in.

The error from openat2 is handled in the following way:

  • EXDEV: the argument is definitely a mount point.
  • no error: the argument is definitely not a mount point.

For all the other errors (most probably ENOSYS or EPERM), fall
back to existing methods (mountedByStat, mountedByMountinfo).

Add a couple of tests:

  • TestMountedBy prepares a few mounts, and checks that various
    mountedBy* functions work as expected. Requires root.

  • TestMountedByOpenat2VsMountinfo reads the mountinfo and then tries
    to use mountedByOpenat2() for each mount, ignoring EPERM as the test
    might not be run as root. AFAIK this is the only way to test the
    new functionality if one is not root.

v2 changes:

  • drop our own openat2 wrapper;
  • add O_PATH to openat2 flags;
  • add O_CLOEXEC to all open* calls;
  • rm socket and symlink special cases;
  • rm some debug from tests.

v3 changes:

  • add normalization of path to mountedByMountinfo (moved from mount.Mounted: normalize the path argument #28).

v4 changes:

  • get rid of dir/file distinction in mountedByOpenat2 to simplify the code;
  • remove the second argument from mountedByStat and mountedByOpenat2;
  • treat ENOENT as "not mounted" by all mountedBy* functions

Fixes: #25
Fixes: #27

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kolyshkin
mountinfo.Mounted: return true for /
14267b8
Let's assume that the root directory is always mounted
(I can't imagine a case when it's not).

This should speed up Mounted("/") case.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
kolyshkin force-pushed the openat2 branch 3 times, most recently from fd49532 to aa19c08 Compare September 8, 2020 20:55
@kolyshkin
kolyshkin requested review from cpuguy83 and thaJeztah September 8, 2020 23:19
@kolyshkin

kolyshkin commented Sep 8, 2020

Copy link
Copy Markdown
Collaborator Author

@cpuguy83 @AkihiroSuda @thaJeztah @cyphar PTAL

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cyphar
cyphar reviewed Sep 9, 2020
View reviewed changes
Comment thread mountinfo/mounted_linux.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah

thaJeztah commented Sep 9, 2020

Copy link
Copy Markdown
Member

Looks like https://go-review.googlesource.com/c/sys/+/253057/ was merged (golang/sys@eff7692) (thanks @tklauser!), so this PR can remove the temporary wrapper

tklauser reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cyphar
cyphar reviewed Sep 9, 2020
View reviewed changes
Comment thread mountinfo/mounted_linux.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

cyphar
cyphar reviewed Sep 9, 2020
View reviewed changes
Comment thread mountinfo/mounted_linux.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

cyphar
cyphar reviewed Sep 9, 2020
View reviewed changes
Comment thread mountinfo/openat2_linux.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

cyphar
cyphar reviewed Sep 9, 2020
View reviewed changes
Comment thread mountinfo/openat2_linux.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

kolyshkin added 2 commits September 9, 2020 13:17
@kolyshkin
mountinfo: bump golang.org/x/sys
abf73b4
... to a version that includes openat2 implementation
(see https://golang.org/cl/253057).

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
ci: add golang 1.15.x
ecf1ea8
... and drop 1.13.x

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
kolyshkin force-pushed the openat2 branch 2 times, most recently from 2f60725 to 4ec1bfd Compare September 9, 2020 20:39
@kolyshkin

kolyshkin commented Sep 9, 2020

Copy link
Copy Markdown
Collaborator Author

v2 changes (thanks to @cyphar for review):

  • drop our own openat2 wrapper;
  • add O_PATH to openat2 flags;
  • add O_CLOEXEC to all open* calls;
  • rm socket and symlink special cases;
  • rm some debug from tests.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kolyshkin

kolyshkin commented Sep 11, 2020

Copy link
Copy Markdown
Collaborator Author

@cpuguy83 @thaJeztah PTAL. I am mostly interested in any test cases I might be missing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kolyshkin kolyshkin mentioned this pull request Sep 11, 2020
mount.Mounted: normalize the path argument #28
Closed
@kolyshkin
kolyshkin force-pushed the openat2 branch from e18253b to 3444206 Compare September 11, 2020 19:04
@kolyshkin

kolyshkin commented Sep 11, 2020

Copy link
Copy Markdown
Collaborator Author

v3: add path normalization to mountedByMountinfo

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cyphar
cyphar reviewed Sep 12, 2020
View reviewed changes
Comment thread mountinfo/mounted_linux.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@cyphar

cyphar commented Sep 12, 2020

Copy link
Copy Markdown
Contributor

Aside from the two nits, LGTM.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cpuguy83
cpuguy83 approved these changes Sep 14, 2020
View reviewed changes

@cpuguy83 cpuguy83 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

@cyphar ❤️ for openat2. Love it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@cpuguy83

cpuguy83 commented Sep 14, 2020

Copy link
Copy Markdown
Member

Downside is I don't think we can test this in CI without a custom VM.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kolyshkin
mountinfo.Mounted: optimize on Linux with openat2
78374f6
A new (as of Linux 5.6) openat2(2) system call can be used to check
whether a path is a mount point or not, and it even works for bind
mounts!

This relies on the fact that openat2() with RESOLVE_NO_XDEV flag
won't cross the filesystem boundary (that includes bind mounts).

If EXDEV error is received, it means the argument is definitely a mount.
If no error is received, it means the argument is definitely not a
mount. For all the other errors (most probably ENOSYS or EPERM), fall
back to existing methods (mountedByStat,  mountedByMountinfo).

Add a couple of tests:

* TestMountedBy prepares a few mounts, and checks that various
  mountedBy* functions work as expected. Requires root.

* TestMountedByOpenat2VsMountinfo reads the mountinfo and then tries
  to use mountedByOpenat2() for each mount, ignoring EPERM as the test
  might not be run as root. AFAIK this is the only way to test the
  new functionality if one is not root.

v2 changes:
 - drop our own openat2 wrapper;
 - add O_PATH to openat2 flags;
 - add O_CLOEXEC to all open* calls;
 - rm socket and symlink special cases.

v3:
 - drop the stat parameter, remove the dir/file distinction;
 - use openat2 for dirfd as well, so we can bail out earlier.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin

kolyshkin commented Sep 14, 2020

Copy link
Copy Markdown
Collaborator Author

Downside is I don't think we can test this in CI without a custom VM.

Should be relatively easy to do, @AkihiroSuda did it for runc using vagrant on Travis CI. I might take a look at it later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kolyshkin added 2 commits September 14, 2020 15:20
@kolyshkin
mountinfo: treat ENOENT as "not mounted"
0c38c02
The positive side effect from this is we're returning earlier, not
falling back on a slow path.

The downside is this might not be entirely true, as a mounted path can
be overshadowed by another mount, but still present in mountinfo.

As the mount is inaccessible anyway, there is nothing that can be done
with it, so let's treat it as non-mount.

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
mountinfo.mountedByMountinfo: normalize path
682f2bc
Currently, the path provided to mountinfo.Mounted() must be
 - an absolute path;
 - with all symlinks resolved;
 - cleaned.

Otherwise, the function may return a false negative (in case this is
a bind mount and the kernel is too old to have openat2) -- i.e. it
will return "not mounted" while the path is in fact mounted.

This commit removes the above limitation by doing a path normalization.
This should slow things down but it's not affecting any of the fast
paths, so the slowdown will only be seen in the "bind mount and older
kernel" case (which is already pretty slow since we have to parse
mountinfo).

v2: as in mountedByOpenat2 and mountedByStat, treat ENOENT as "not
mounted".

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin
kolyshkin force-pushed the openat2 branch from 3444206 to 682f2bc Compare September 14, 2020 22:35
@kolyshkin

kolyshkin commented Sep 14, 2020

Copy link
Copy Markdown
Collaborator Author

v4 changes:

  • get rid of dir/file distinction in mountedByOpenat2 to simplify the code;
  • remove the second argument from mountedByStat and mountedByOpenat2;
  • treat ENOENT as "not mounted" by all mountedBy* functions
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kolyshkin
ci: run tests under Fedora 32
9c884dc
This should make it possible to test the recently added mountedByOpenat2
functionality, which requires Linux kernel 5.6+.

Alas, even the latest Ubuntu release available on Travis, Focal (20.04),
has a kernel that's not new enough, so we have to setup a vagrant box.

The machinery is mostly stolen from the runc repo at
https://github.com/opencontainers/runc/

Signed-off-by: Kir Kolyshkin <[email protected]>
@kolyshkin

kolyshkin commented Sep 15, 2020

Copy link
Copy Markdown
Collaborator Author

I might take a look at it later

Added 🎉

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kolyshkin

kolyshkin commented Sep 15, 2020

Copy link
Copy Markdown
Collaborator Author

aaand it's a pass! https://travis-ci.org/github/moby/sys/builds/727240869#L4099

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

thaJeztah
thaJeztah approved these changes Sep 15, 2020
View reviewed changes

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread mountinfo/mounted_linux_test.go
return
}

func cleanupMounts(t *testing.T, dir string, mounts []string) {

@thaJeztah thaJeztah Sep 15, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

(not for this PR, but just thinking out loud); we should have a look at the testing.Cleanup() option that's now available in Go 1.13+

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread mountinfo/mounted_linux_test.go
}

checked := false
for _, m := range mounts {

@thaJeztah thaJeztah Sep 15, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

(not a blocker, just thinking out loud as well) We could use subtests for these for easier debugging

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@thaJeztah

thaJeztah commented Sep 15, 2020

Copy link
Copy Markdown
Member

I think @cyphar's comments were addressed, so I'll go ahead and merge <3

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah
thaJeztah merged commit bbb236c into moby:master Sep 15, 2020
@thaJeztah thaJeztah mentioned this pull request Sep 18, 2020
mount: some refactor and improved GoDoc about Windows support #31
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@cpuguy83 cpuguy83 cpuguy83 approved these changes
@thaJeztah thaJeztah thaJeztah approved these changes
+1 more reviewer
@cyphar cyphar cyphar left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Mounted: add path resolution Implement Mounted() optimization using openat2(RESOLVE_NO_XDEV)

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@kolyshkin @thaJeztah @cyphar @cpuguy83
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.