Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions pkg/sentry/kernel/fd_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (f *FDTable) saveDescriptorTable() map[int32]descriptor {
m := make(map[int32]descriptor)
f.mu.Lock()
defer f.mu.Unlock()
f.ForEach(context.Background(), func(fd int32, file *vfs.FileDescription, flags FDFlags) bool {
f.forEach(context.Background(), func(fd int32, file *vfs.FileDescription, flags FDFlags) bool {
m[fd] = descriptor{
file: file,
flags: flags,
Expand Down Expand Up @@ -172,10 +172,19 @@ func (f *FDTable) forEachUpTo(ctx context.Context, maxFd int32, fn func(fd int32
})
}

// ForEach iterates over all non-nil files upto maxFd in sorted order.
// ForEach acquires f.mu and iterates over all non-nil files upto maxFd in sorted order.
func (f *FDTable) ForEach(ctx context.Context, fn func(fd int32, file *vfs.FileDescription, flags FDFlags) bool) {
f.mu.Lock()
defer f.mu.Unlock()
f.forEach(ctx, fn)
}

// forEach iterates over all non-nil files upto maxFd in sorted order.
//
// It is the caller's responsibility to acquire an appropriate lock.
func (f *FDTable) ForEach(ctx context.Context, fn func(fd int32, file *vfs.FileDescription, flags FDFlags) bool) {
//
// +checklocks:f.mu
func (f *FDTable) forEach(ctx context.Context, fn func(fd int32, file *vfs.FileDescription, flags FDFlags) bool) {
f.forEachUpTo(ctx, MaxFdLimit, fn)
}

Expand All @@ -187,7 +196,7 @@ func (f *FDTable) String() string {
f.mu.Lock()
// Can't release f.mu from defer, because vfsObj.PathnameWithDeleted
// should not be called under the fdtable mutex.
f.ForEach(ctx, func(fd int32, file *vfs.FileDescription, flags FDFlags) bool {
f.forEach(ctx, func(fd int32, file *vfs.FileDescription, flags FDFlags) bool {
if file != nil {
file.IncRef()
files[fd] = file
Expand Down Expand Up @@ -427,7 +436,7 @@ func (f *FDTable) GetFDs(ctx context.Context) []int32 {
f.mu.Lock()
defer f.mu.Unlock()
fds := make([]int32, 0, int(f.fdBitmap.GetNumOnes()))
f.ForEach(ctx, func(fd int32, _ *vfs.FileDescription, _ FDFlags) bool {
f.forEach(ctx, func(fd int32, _ *vfs.FileDescription, _ FDFlags) bool {
fds = append(fds, fd)
return true
})
Expand Down Expand Up @@ -489,7 +498,7 @@ func (f *FDTable) RemoveIf(ctx context.Context, cond func(*vfs.FileDescription,
var files []*vfs.FileDescription

f.mu.Lock()
f.ForEach(ctx, func(fd int32, file *vfs.FileDescription, flags FDFlags) bool {
f.forEach(ctx, func(fd int32, file *vfs.FileDescription, flags FDFlags) bool {
if cond(file, flags) {
// Clear from table.
if df := f.set(fd, nil, FDFlags{}); df != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ func (k *Kernel) pauseTimeLocked(ctx context.Context) {
// This means we'll iterate FDTables shared by multiple tasks repeatedly,
// but ktime.Timer.Pause is idempotent so this is harmless.
if t.fdTable != nil {
t.fdTable.ForEach(ctx, func(_ int32, fd *vfs.FileDescription, _ FDFlags) bool {
t.fdTable.forEach(ctx, func(_ int32, fd *vfs.FileDescription, _ FDFlags) bool { // +checklocksignore
if tfd, ok := fd.Impl().(*timerfd.TimerFileDescription); ok {
tfd.PauseTimer()
}
Expand Down Expand Up @@ -1699,7 +1699,7 @@ func (k *Kernel) resumeTimeLocked(ctx context.Context) {
}
}
if t.fdTable != nil {
t.fdTable.ForEach(ctx, func(_ int32, fd *vfs.FileDescription, _ FDFlags) bool {
t.fdTable.forEach(ctx, func(_ int32, fd *vfs.FileDescription, _ FDFlags) bool { // +checklocksignore
if tfd, ok := fd.Impl().(*timerfd.TimerFileDescription); ok {
tfd.ResumeTimer()
}
Expand Down
Loading