Remove linux.EpollEvent.Fd.

glibc defines struct epoll_event in such a way that epoll_event.data.fd exists.
However, the kernel's definition of struct epoll_event makes epoll_event.data
an opaque uint64, so naming half of it "fd" just introduces confusion. Remove
the Fd field, and make Data a [2]int32 to compensate.

Also add required padding to linux.EpollEvent on ARM64.

PiperOrigin-RevId: 295250424
This commit is contained in:
gVisor bot 2020-02-14 16:19:00 -08:00
parent 5baf9dc2fb
commit a5069f820f
6 changed files with 61 additions and 19 deletions

View File

@ -17,6 +17,8 @@ go_library(
"dev.go",
"elf.go",
"epoll.go",
"epoll_amd64.go",
"epoll_arm64.go",
"errors.go",
"eventfd.go",
"exec.go",

View File

@ -14,13 +14,6 @@
package linux
// EpollEvent is equivalent to struct epoll_event from epoll(2).
type EpollEvent struct {
Events uint32
Fd int32
Data int32
}
// Event masks.
const (
EPOLLIN = 0x1

View File

@ -0,0 +1,25 @@
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linux
// EpollEvent is equivalent to struct epoll_event from epoll(2).
type EpollEvent struct {
Events uint32
// Linux makes struct epoll_event::data a __u64. We represent it as
// [2]int32 because, on amd64, Linux also makes struct epoll_event
// __attribute__((packed)), such that there is no padding between Events
// and Data.
Data [2]int32
}

View File

@ -0,0 +1,24 @@
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linux
// EpollEvent is equivalent to struct epoll_event from epoll(2).
type EpollEvent struct {
Events uint32
// Linux makes struct epoll_event a __u64, necessitating 4 bytes of padding
// here.
_ int32
Data [2]int32
}

View File

@ -83,8 +83,7 @@ func EpollCtl(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Sysc
}
mask = waiter.EventMaskFromLinux(e.Events)
data[0] = e.Fd
data[1] = e.Data
data = e.Data
}
// Perform the requested operations.

View File

@ -85,8 +85,8 @@ type epollInterest struct {
ready bool
epollInterestEntry
// userData is the epoll_data_t associated with this epollInterest.
// userData is protected by epoll.mu.
// userData is the struct epoll_event::data associated with this
// epollInterest. userData is protected by epoll.mu.
userData [2]int32
}
@ -157,7 +157,7 @@ func (ep *EpollInstance) Seek(ctx context.Context, offset int64, whence int32) (
// AddInterest implements the semantics of EPOLL_CTL_ADD.
//
// Preconditions: A reference must be held on file.
func (ep *EpollInstance) AddInterest(file *FileDescription, num int32, mask uint32, userData [2]int32) error {
func (ep *EpollInstance) AddInterest(file *FileDescription, num int32, event linux.EpollEvent) error {
// Check for cyclic polling if necessary.
subep, _ := file.impl.(*EpollInstance)
if subep != nil {
@ -183,12 +183,12 @@ func (ep *EpollInstance) AddInterest(file *FileDescription, num int32, mask uint
}
// Register interest in file.
mask |= linux.EPOLLERR | linux.EPOLLRDHUP
mask := event.Events | linux.EPOLLERR | linux.EPOLLRDHUP
epi := &epollInterest{
epoll: ep,
key: key,
mask: mask,
userData: userData,
userData: event.Data,
}
ep.interest[key] = epi
wmask := waiter.EventMaskFromLinux(mask)
@ -236,7 +236,7 @@ func (ep *EpollInstance) mightPollRecursive(ep2 *EpollInstance, remainingRecursi
// ModifyInterest implements the semantics of EPOLL_CTL_MOD.
//
// Preconditions: A reference must be held on file.
func (ep *EpollInstance) ModifyInterest(file *FileDescription, num int32, mask uint32, userData [2]int32) error {
func (ep *EpollInstance) ModifyInterest(file *FileDescription, num int32, event linux.EpollEvent) error {
ep.interestMu.Lock()
defer ep.interestMu.Unlock()
@ -250,13 +250,13 @@ func (ep *EpollInstance) ModifyInterest(file *FileDescription, num int32, mask u
}
// Update epi for the next call to ep.ReadEvents().
mask := event.Events | linux.EPOLLERR | linux.EPOLLRDHUP
ep.mu.Lock()
epi.mask = mask
epi.userData = userData
epi.userData = event.Data
ep.mu.Unlock()
// Re-register with the new mask.
mask |= linux.EPOLLERR | linux.EPOLLRDHUP
file.EventUnregister(&epi.waiter)
wmask := waiter.EventMaskFromLinux(mask)
file.EventRegister(&epi.waiter, wmask)
@ -363,8 +363,7 @@ func (ep *EpollInstance) ReadEvents(events []linux.EpollEvent) int {
// Report ievents.
events[i] = linux.EpollEvent{
Events: ievents.ToLinux(),
Fd: epi.userData[0],
Data: epi.userData[1],
Data: epi.userData,
}
i++
if i == len(events) {