Skip to content

Commit

Permalink
proc: Allow interrupting generation of /proc/pid/mount{s,info}
Browse files Browse the repository at this point in the history
In some cases, generating /proc/pid/mount{s,info} can take a long time. This change allows the process to be interrupted.

Reported-by: [email protected]
FUTURE_COPYBARA_INTEGRATE_REVIEW=#11341 from avagin:mountinfo-int 679c77e
PiperOrigin-RevId: 712652641
  • Loading branch information
avagin authored and gvisor-bot committed Jan 7, 2025
1 parent 7af7cb0 commit 895fdc9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 2 additions & 4 deletions pkg/sentry/fsimpl/proc/task_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,7 @@ func (i *mountInfoData) Generate(ctx context.Context, buf *bytes.Buffer) error {
return nil
}
defer i.fs.SafeDecRef(ctx, rootDir)
i.task.Kernel().VFS().GenerateProcMountInfo(ctx, rootDir, buf)
return nil
return i.task.Kernel().VFS().GenerateProcMountInfo(ctx, rootDir, buf)
}

// mountsData is used to implement /proc/[pid]/mounts.
Expand Down Expand Up @@ -1233,8 +1232,7 @@ func (i *mountsData) Generate(ctx context.Context, buf *bytes.Buffer) error {
return nil
}
defer i.fs.SafeDecRef(ctx, rootDir)
i.task.Kernel().VFS().GenerateProcMounts(ctx, rootDir, buf)
return nil
return i.task.Kernel().VFS().GenerateProcMounts(ctx, rootDir, buf)
}

// +stateify savable
Expand Down
12 changes: 10 additions & 2 deletions pkg/sentry/vfs/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ func (mnt *Mount) Root() *Dentry {
// GenerateProcMounts emits the contents of /proc/[pid]/mounts for vfs to buf.
//
// Preconditions: taskRootDir.Ok().
func (vfs *VirtualFilesystem) GenerateProcMounts(ctx context.Context, taskRootDir VirtualDentry, buf *bytes.Buffer) {
func (vfs *VirtualFilesystem) GenerateProcMounts(ctx context.Context, taskRootDir VirtualDentry, buf *bytes.Buffer) error {
rootMnt := taskRootDir.mount

vfs.lockMounts()
Expand All @@ -1361,6 +1361,9 @@ func (vfs *VirtualFilesystem) GenerateProcMounts(ctx context.Context, taskRootDi
sort.Slice(mounts, func(i, j int) bool { return mounts[i].ID < mounts[j].ID })

for _, mnt := range mounts {
if ctx.Interrupted() {
return linuxerr.ErrInterrupted
}
// Get the path to this mount relative to task root.
mntRootVD := VirtualDentry{
mount: mnt,
Expand Down Expand Up @@ -1404,13 +1407,14 @@ func (vfs *VirtualFilesystem) GenerateProcMounts(ctx context.Context, taskRootDi
// is allowed.
fmt.Fprintf(buf, "%s %s %s %s %d %d\n", "none", path, mnt.fs.FilesystemType().Name(), opts, 0, 0)
}
return nil
}

// GenerateProcMountInfo emits the contents of /proc/[pid]/mountinfo for vfs to
// buf.
//
// Preconditions: taskRootDir.Ok().
func (vfs *VirtualFilesystem) GenerateProcMountInfo(ctx context.Context, taskRootDir VirtualDentry, buf *bytes.Buffer) {
func (vfs *VirtualFilesystem) GenerateProcMountInfo(ctx context.Context, taskRootDir VirtualDentry, buf *bytes.Buffer) error {
rootMnt := taskRootDir.mount

vfs.lockMounts()
Expand All @@ -1431,6 +1435,9 @@ func (vfs *VirtualFilesystem) GenerateProcMountInfo(ctx context.Context, taskRoo

creds := auth.CredentialsFromContext(ctx)
for _, mnt := range mounts {
if ctx.Interrupted() {
return linuxerr.ErrInterrupted
}
// Get the path to this mount relative to task root.
mntRootVD := VirtualDentry{
mount: mnt,
Expand Down Expand Up @@ -1530,6 +1537,7 @@ func (vfs *VirtualFilesystem) GenerateProcMountInfo(ctx context.Context, taskRoo
// (11) Superblock options, and final newline.
fmt.Fprintf(buf, "%s\n", superBlockOpts(pathFromRoot, mnt))
}
return nil
}

// manglePath replaces ' ', '\t', '\n', and '\\' with their octal equivalents.
Expand Down

0 comments on commit 895fdc9

Please sign in to comment.