diff options
author | gVisor bot <gvisor-bot@google.com> | 2020-12-11 12:28:33 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-12-11 12:28:33 +0000 |
commit | 29acd2ab481e386db3dff7de135432afcaacd7a9 (patch) | |
tree | 393a35362d33080112958407561eeb462e75c163 | |
parent | 9b65ed17ec2728d8ac3b362fca44825d8be08659 (diff) | |
parent | 73eccab91ec58aafd1ffdf038993b78a812ba30f (diff) |
Merge release-20201208.0-26-g73eccab91 (automated)
-rw-r--r-- | pkg/sentry/kernel/semaphore/semaphore.go | 53 | ||||
-rw-r--r-- | pkg/sentry/kernel/semaphore/semaphore_state_autogen.go | 3 | ||||
-rw-r--r-- | pkg/sentry/syscalls/linux/sys_sem.go | 7 |
3 files changed, 60 insertions, 3 deletions
diff --git a/pkg/sentry/kernel/semaphore/semaphore.go b/pkg/sentry/kernel/semaphore/semaphore.go index 31198d772..3dd3953b3 100644 --- a/pkg/sentry/kernel/semaphore/semaphore.go +++ b/pkg/sentry/kernel/semaphore/semaphore.go @@ -52,6 +52,9 @@ type Registry struct { mu sync.Mutex `state:"nosave"` semaphores map[int32]*Set lastIDUsed int32 + // indexes maintains a mapping between a set's index in virtual array and + // its identifier. + indexes map[int32]int32 } // Set represents a set of semaphores that can be operated atomically. @@ -113,6 +116,7 @@ func NewRegistry(userNS *auth.UserNamespace) *Registry { return &Registry{ userNS: userNS, semaphores: make(map[int32]*Set), + indexes: make(map[int32]int32), } } @@ -163,6 +167,9 @@ func (r *Registry) FindOrCreate(ctx context.Context, key, nsems int32, mode linu } // Apply system limits. + // + // Map semaphores and map indexes in a registry are of the same size, + // check map semaphores only here for the system limit. if len(r.semaphores) >= setsMax { return nil, syserror.EINVAL } @@ -192,6 +199,23 @@ func (r *Registry) IPCInfo() *linux.SemInfo { } } +// HighestIndex returns the index of the highest used entry in +// the kernel's array. +func (r *Registry) HighestIndex() int32 { + r.mu.Lock() + defer r.mu.Unlock() + + // By default, highest used index is 0 even though + // there is no semaphroe set. + var highestIndex int32 + for index := range r.indexes { + if index > highestIndex { + highestIndex = index + } + } + return highestIndex +} + // RemoveID removes set with give 'id' from the registry and marks the set as // dead. All waiters will be awakened and fail. func (r *Registry) RemoveID(id int32, creds *auth.Credentials) error { @@ -202,6 +226,11 @@ func (r *Registry) RemoveID(id int32, creds *auth.Credentials) error { if set == nil { return syserror.EINVAL } + index, found := r.findIndexByID(id) + if !found { + // Inconsistent state. + panic(fmt.Sprintf("unable to find an index for ID: %d", id)) + } set.mu.Lock() defer set.mu.Unlock() @@ -213,6 +242,7 @@ func (r *Registry) RemoveID(id int32, creds *auth.Credentials) error { } delete(r.semaphores, set.ID) + delete(r.indexes, index) set.destroy() return nil } @@ -236,6 +266,11 @@ func (r *Registry) newSet(ctx context.Context, key int32, owner, creator fs.File continue } if r.semaphores[id] == nil { + index, found := r.findFirstAvailableIndex() + if !found { + panic("unable to find an available index") + } + r.indexes[index] = id r.lastIDUsed = id r.semaphores[id] = set set.ID = id @@ -263,6 +298,24 @@ func (r *Registry) findByKey(key int32) *Set { return nil } +func (r *Registry) findIndexByID(id int32) (int32, bool) { + for k, v := range r.indexes { + if v == id { + return k, true + } + } + return 0, false +} + +func (r *Registry) findFirstAvailableIndex() (int32, bool) { + for index := int32(0); index < setsMax; index++ { + if _, present := r.indexes[index]; !present { + return index, true + } + } + return 0, false +} + func (r *Registry) totalSems() int { totalSems := 0 for _, v := range r.semaphores { diff --git a/pkg/sentry/kernel/semaphore/semaphore_state_autogen.go b/pkg/sentry/kernel/semaphore/semaphore_state_autogen.go index aed6cfa29..e0578836c 100644 --- a/pkg/sentry/kernel/semaphore/semaphore_state_autogen.go +++ b/pkg/sentry/kernel/semaphore/semaphore_state_autogen.go @@ -15,6 +15,7 @@ func (r *Registry) StateFields() []string { "userNS", "semaphores", "lastIDUsed", + "indexes", } } @@ -25,6 +26,7 @@ func (r *Registry) StateSave(stateSinkObject state.Sink) { stateSinkObject.Save(0, &r.userNS) stateSinkObject.Save(1, &r.semaphores) stateSinkObject.Save(2, &r.lastIDUsed) + stateSinkObject.Save(3, &r.indexes) } func (r *Registry) afterLoad() {} @@ -33,6 +35,7 @@ func (r *Registry) StateLoad(stateSourceObject state.Source) { stateSourceObject.Load(0, &r.userNS) stateSourceObject.Load(1, &r.semaphores) stateSourceObject.Load(2, &r.lastIDUsed) + stateSourceObject.Load(3, &r.indexes) } func (s *Set) StateTypeName() string { diff --git a/pkg/sentry/syscalls/linux/sys_sem.go b/pkg/sentry/syscalls/linux/sys_sem.go index a1601676f..a62a6b3b5 100644 --- a/pkg/sentry/syscalls/linux/sys_sem.go +++ b/pkg/sentry/syscalls/linux/sys_sem.go @@ -150,9 +150,10 @@ func Semctl(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Syscal buf := args[3].Pointer() r := t.IPCNamespace().SemaphoreRegistry() info := r.IPCInfo() - _, err := info.CopyOut(t, buf) - // TODO(gvisor.dev/issue/137): Return the index of the highest used entry. - return 0, nil, err + if _, err := info.CopyOut(t, buf); err != nil { + return 0, nil, err + } + return uintptr(r.HighestIndex()), nil, nil case linux.SEM_INFO, linux.SEM_STAT, |