From 5d8cf31346376eb7c6a93bad3eab7666f145fa0e Mon Sep 17 00:00:00 2001 From: Adin Scannell Date: Fri, 14 Dec 2018 18:03:43 -0800 Subject: Move fdnotifier package to reduce internal confusion. PiperOrigin-RevId: 225632398 Change-Id: I909e7e2925aa369adc28e844c284d9a6108e85ce --- pkg/fdnotifier/BUILD | 14 +++ pkg/fdnotifier/fdnotifier.go | 200 ++++++++++++++++++++++++++++++++++ pkg/fdnotifier/poll_unsafe.go | 74 +++++++++++++ pkg/sentry/fs/fdpipe/BUILD | 4 +- pkg/sentry/fs/fdpipe/pipe.go | 2 +- pkg/sentry/fs/fdpipe/pipe_test.go | 2 +- pkg/sentry/fs/host/BUILD | 4 +- pkg/sentry/fs/host/descriptor.go | 2 +- pkg/sentry/fs/host/descriptor_test.go | 2 +- pkg/sentry/fs/host/file.go | 2 +- pkg/sentry/fs/host/socket.go | 2 +- pkg/sentry/fs/host/socket_test.go | 2 +- pkg/sentry/kernel/eventfd/BUILD | 2 +- pkg/sentry/kernel/eventfd/eventfd.go | 2 +- pkg/sentry/socket/hostinet/BUILD | 2 +- pkg/sentry/socket/hostinet/socket.go | 2 +- pkg/waiter/fdnotifier/BUILD | 14 --- pkg/waiter/fdnotifier/fdnotifier.go | 200 ---------------------------------- pkg/waiter/fdnotifier/poll_unsafe.go | 74 ------------- 19 files changed, 303 insertions(+), 303 deletions(-) create mode 100644 pkg/fdnotifier/BUILD create mode 100644 pkg/fdnotifier/fdnotifier.go create mode 100644 pkg/fdnotifier/poll_unsafe.go delete mode 100644 pkg/waiter/fdnotifier/BUILD delete mode 100644 pkg/waiter/fdnotifier/fdnotifier.go delete mode 100644 pkg/waiter/fdnotifier/poll_unsafe.go (limited to 'pkg') diff --git a/pkg/fdnotifier/BUILD b/pkg/fdnotifier/BUILD new file mode 100644 index 000000000..27d378d5b --- /dev/null +++ b/pkg/fdnotifier/BUILD @@ -0,0 +1,14 @@ +load("//tools/go_stateify:defs.bzl", "go_library") + +package(licenses = ["notice"]) # Apache 2.0 + +go_library( + name = "fdnotifier", + srcs = [ + "fdnotifier.go", + "poll_unsafe.go", + ], + importpath = "gvisor.googlesource.com/gvisor/pkg/fdnotifier", + visibility = ["//:sandbox"], + deps = ["//pkg/waiter"], +) diff --git a/pkg/fdnotifier/fdnotifier.go b/pkg/fdnotifier/fdnotifier.go new file mode 100644 index 000000000..624b1a0c5 --- /dev/null +++ b/pkg/fdnotifier/fdnotifier.go @@ -0,0 +1,200 @@ +// Copyright 2018 Google LLC +// +// 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 fdnotifier contains an adapter that translates IO events (e.g., a +// file became readable/writable) from native FDs to the notifications in the +// waiter package. It uses epoll in edge-triggered mode to receive notifications +// for registered FDs. +package fdnotifier + +import ( + "fmt" + "sync" + "syscall" + + "gvisor.googlesource.com/gvisor/pkg/waiter" +) + +type fdInfo struct { + queue *waiter.Queue + waiting bool +} + +// notifier holds all the state necessary to issue notifications when IO events +// occur in the observed FDs. +type notifier struct { + // epFD is the epoll file descriptor used to register for io + // notifications. + epFD int + + // mu protects fdMap. + mu sync.Mutex + + // fdMap maps file descriptors to their notification queues and waiting + // status. + fdMap map[int32]*fdInfo +} + +// newNotifier creates a new notifier object. +func newNotifier() (*notifier, error) { + epfd, err := syscall.EpollCreate1(0) + if err != nil { + return nil, err + } + + w := ¬ifier{ + epFD: epfd, + fdMap: make(map[int32]*fdInfo), + } + + go w.waitAndNotify() // S/R-SAFE: no waiter exists during save / load. + + return w, nil +} + +// waitFD waits on mask for fd. The fdMap mutex must be hold. +func (n *notifier) waitFD(fd int32, fi *fdInfo, mask waiter.EventMask) error { + if !fi.waiting && mask == 0 { + return nil + } + + e := syscall.EpollEvent{ + Events: uint32(mask) | -syscall.EPOLLET, + Fd: fd, + } + + switch { + case !fi.waiting && mask != 0: + if err := syscall.EpollCtl(n.epFD, syscall.EPOLL_CTL_ADD, int(fd), &e); err != nil { + return err + } + fi.waiting = true + case fi.waiting && mask == 0: + syscall.EpollCtl(n.epFD, syscall.EPOLL_CTL_DEL, int(fd), nil) + fi.waiting = false + case fi.waiting && mask != 0: + if err := syscall.EpollCtl(n.epFD, syscall.EPOLL_CTL_MOD, int(fd), &e); err != nil { + return err + } + } + + return nil +} + +// addFD adds an FD to the list of FDs observed by n. +func (n *notifier) addFD(fd int32, queue *waiter.Queue) { + n.mu.Lock() + defer n.mu.Unlock() + + // Panic if we're already notifying on this FD. + if _, ok := n.fdMap[fd]; ok { + panic(fmt.Sprintf("File descriptor %v added twice", fd)) + } + + // We have nothing to wait for at the moment. Just add it to the map. + n.fdMap[fd] = &fdInfo{queue: queue} +} + +// updateFD updates the set of events the fd needs to be notified on. +func (n *notifier) updateFD(fd int32) error { + n.mu.Lock() + defer n.mu.Unlock() + + if fi, ok := n.fdMap[fd]; ok { + return n.waitFD(fd, fi, fi.queue.Events()) + } + + return nil +} + +// RemoveFD removes an FD from the list of FDs observed by n. +func (n *notifier) removeFD(fd int32) { + n.mu.Lock() + defer n.mu.Unlock() + + // Remove from map, then from epoll object. + n.waitFD(fd, n.fdMap[fd], 0) + delete(n.fdMap, fd) +} + +// hasFD returns true if the fd is in the list of observed FDs. +func (n *notifier) hasFD(fd int32) bool { + n.mu.Lock() + defer n.mu.Unlock() + + _, ok := n.fdMap[fd] + return ok +} + +// waitAndNotify run is its own goroutine and loops waiting for io event +// notifications from the epoll object. Once notifications arrive, they are +// dispatched to the registered queue. +func (n *notifier) waitAndNotify() error { + e := make([]syscall.EpollEvent, 100) + for { + v, err := epollWait(n.epFD, e, -1) + if err == syscall.EINTR { + continue + } + + if err != nil { + return err + } + + n.mu.Lock() + for i := 0; i < v; i++ { + if fi, ok := n.fdMap[e[i].Fd]; ok { + fi.queue.Notify(waiter.EventMask(e[i].Events)) + } + } + n.mu.Unlock() + } +} + +var shared struct { + notifier *notifier + once sync.Once + initErr error +} + +// AddFD adds an FD to the list of observed FDs. +func AddFD(fd int32, queue *waiter.Queue) error { + shared.once.Do(func() { + shared.notifier, shared.initErr = newNotifier() + }) + + if shared.initErr != nil { + return shared.initErr + } + + shared.notifier.addFD(fd, queue) + return nil +} + +// UpdateFD updates the set of events the fd needs to be notified on. +func UpdateFD(fd int32) error { + return shared.notifier.updateFD(fd) +} + +// RemoveFD removes an FD from the list of observed FDs. +func RemoveFD(fd int32) { + shared.notifier.removeFD(fd) +} + +// HasFD returns true if the FD is in the list of observed FDs. +// +// This should only be used by tests to assert that FDs are correctly registered. +func HasFD(fd int32) bool { + return shared.notifier.hasFD(fd) +} diff --git a/pkg/fdnotifier/poll_unsafe.go b/pkg/fdnotifier/poll_unsafe.go new file mode 100644 index 000000000..8459d4c74 --- /dev/null +++ b/pkg/fdnotifier/poll_unsafe.go @@ -0,0 +1,74 @@ +// Copyright 2018 Google LLC +// +// 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 fdnotifier + +import ( + "syscall" + "unsafe" + + "gvisor.googlesource.com/gvisor/pkg/waiter" +) + +// NonBlockingPoll polls the given FD in non-blocking fashion. It is used just +// to query the FD's current state. +func NonBlockingPoll(fd int32, mask waiter.EventMask) waiter.EventMask { + e := struct { + fd int32 + events int16 + revents int16 + }{ + fd: fd, + events: int16(mask), + } + + for { + n, _, err := syscall.RawSyscall(syscall.SYS_POLL, uintptr(unsafe.Pointer(&e)), 1, 0) + // Interrupted by signal, try again. + if err == syscall.EINTR { + continue + } + // If an error occur we'll conservatively say the FD is ready for + // whatever is being checked. + if err != 0 { + return mask + } + + // If no FDs were returned, it wasn't ready for anything. + if n == 0 { + return 0 + } + + // Otherwise we got the ready events in the revents field. + return waiter.EventMask(e.revents) + } +} + +// epollWait performs a blocking wait on epfd. +// +// Preconditions: +// * len(events) > 0 +func epollWait(epfd int, events []syscall.EpollEvent, msec int) (int, error) { + if len(events) == 0 { + panic("Empty events passed to EpollWait") + } + + // We actually use epoll_pwait with NULL sigmask instead of epoll_wait + // since that is what the Go >= 1.11 runtime prefers. + r, _, e := syscall.Syscall6(syscall.SYS_EPOLL_PWAIT, uintptr(epfd), uintptr(unsafe.Pointer(&events[0])), uintptr(len(events)), uintptr(msec), 0, 0) + if e != 0 { + return 0, e + } + return int(r), nil +} diff --git a/pkg/sentry/fs/fdpipe/BUILD b/pkg/sentry/fs/fdpipe/BUILD index ffe4204bc..8a0937cda 100644 --- a/pkg/sentry/fs/fdpipe/BUILD +++ b/pkg/sentry/fs/fdpipe/BUILD @@ -14,6 +14,7 @@ go_library( visibility = ["//pkg/sentry:internal"], deps = [ "//pkg/fd", + "//pkg/fdnotifier", "//pkg/log", "//pkg/secio", "//pkg/sentry/context", @@ -23,7 +24,6 @@ go_library( "//pkg/sentry/usermem", "//pkg/syserror", "//pkg/waiter", - "//pkg/waiter/fdnotifier", ], ) @@ -37,12 +37,12 @@ go_test( embed = [":fdpipe"], deps = [ "//pkg/fd", + "//pkg/fdnotifier", "//pkg/sentry/context", "//pkg/sentry/context/contexttest", "//pkg/sentry/fs", "//pkg/sentry/usermem", "//pkg/syserror", - "//pkg/waiter/fdnotifier", "@com_github_google_uuid//:go_default_library", ], ) diff --git a/pkg/sentry/fs/fdpipe/pipe.go b/pkg/sentry/fs/fdpipe/pipe.go index bfafff5ec..e3b830747 100644 --- a/pkg/sentry/fs/fdpipe/pipe.go +++ b/pkg/sentry/fs/fdpipe/pipe.go @@ -21,6 +21,7 @@ import ( "syscall" "gvisor.googlesource.com/gvisor/pkg/fd" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/log" "gvisor.googlesource.com/gvisor/pkg/secio" "gvisor.googlesource.com/gvisor/pkg/sentry/context" @@ -30,7 +31,6 @@ import ( "gvisor.googlesource.com/gvisor/pkg/sentry/usermem" "gvisor.googlesource.com/gvisor/pkg/syserror" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) // pipeOperations are the fs.FileOperations of a host pipe. diff --git a/pkg/sentry/fs/fdpipe/pipe_test.go b/pkg/sentry/fs/fdpipe/pipe_test.go index d3f15be6b..7e3ee5257 100644 --- a/pkg/sentry/fs/fdpipe/pipe_test.go +++ b/pkg/sentry/fs/fdpipe/pipe_test.go @@ -22,11 +22,11 @@ import ( "testing" "gvisor.googlesource.com/gvisor/pkg/fd" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/sentry/context/contexttest" "gvisor.googlesource.com/gvisor/pkg/sentry/fs" "gvisor.googlesource.com/gvisor/pkg/sentry/usermem" "gvisor.googlesource.com/gvisor/pkg/syserror" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) func singlePipeFD() (int, error) { diff --git a/pkg/sentry/fs/host/BUILD b/pkg/sentry/fs/host/BUILD index 73d9cc71a..6877eb161 100644 --- a/pkg/sentry/fs/host/BUILD +++ b/pkg/sentry/fs/host/BUILD @@ -27,6 +27,7 @@ go_library( deps = [ "//pkg/abi/linux", "//pkg/fd", + "//pkg/fdnotifier", "//pkg/log", "//pkg/refs", "//pkg/secio", @@ -51,7 +52,6 @@ go_library( "//pkg/tcpip", "//pkg/unet", "//pkg/waiter", - "//pkg/waiter/fdnotifier", ], ) @@ -68,6 +68,7 @@ go_test( embed = [":host"], deps = [ "//pkg/fd", + "//pkg/fdnotifier", "//pkg/sentry/context", "//pkg/sentry/context/contexttest", "//pkg/sentry/fs", @@ -78,6 +79,5 @@ go_test( "//pkg/syserr", "//pkg/tcpip", "//pkg/waiter", - "//pkg/waiter/fdnotifier", ], ) diff --git a/pkg/sentry/fs/host/descriptor.go b/pkg/sentry/fs/host/descriptor.go index 7c9d2b299..554e1693a 100644 --- a/pkg/sentry/fs/host/descriptor.go +++ b/pkg/sentry/fs/host/descriptor.go @@ -19,9 +19,9 @@ import ( "path" "syscall" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/log" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) // descriptor wraps a host fd. diff --git a/pkg/sentry/fs/host/descriptor_test.go b/pkg/sentry/fs/host/descriptor_test.go index 6bc1bd2ae..5dec84ab2 100644 --- a/pkg/sentry/fs/host/descriptor_test.go +++ b/pkg/sentry/fs/host/descriptor_test.go @@ -20,8 +20,8 @@ import ( "syscall" "testing" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) func TestDescriptorRelease(t *testing.T) { diff --git a/pkg/sentry/fs/host/file.go b/pkg/sentry/fs/host/file.go index 975084c86..bc6ee7aa4 100644 --- a/pkg/sentry/fs/host/file.go +++ b/pkg/sentry/fs/host/file.go @@ -19,6 +19,7 @@ import ( "syscall" "gvisor.googlesource.com/gvisor/pkg/fd" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/log" "gvisor.googlesource.com/gvisor/pkg/secio" "gvisor.googlesource.com/gvisor/pkg/sentry/context" @@ -29,7 +30,6 @@ import ( "gvisor.googlesource.com/gvisor/pkg/sentry/usermem" "gvisor.googlesource.com/gvisor/pkg/syserror" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) // fileOperations implements fs.FileOperations for a host file descriptor. diff --git a/pkg/sentry/fs/host/socket.go b/pkg/sentry/fs/host/socket.go index b9e2aa705..be2c3581f 100644 --- a/pkg/sentry/fs/host/socket.go +++ b/pkg/sentry/fs/host/socket.go @@ -19,6 +19,7 @@ import ( "syscall" "gvisor.googlesource.com/gvisor/pkg/fd" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/log" "gvisor.googlesource.com/gvisor/pkg/refs" "gvisor.googlesource.com/gvisor/pkg/sentry/context" @@ -32,7 +33,6 @@ import ( "gvisor.googlesource.com/gvisor/pkg/tcpip" "gvisor.googlesource.com/gvisor/pkg/unet" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) // maxSendBufferSize is the maximum host send buffer size allowed for endpoint. diff --git a/pkg/sentry/fs/host/socket_test.go b/pkg/sentry/fs/host/socket_test.go index 6ddf63a6a..83e8e1b3c 100644 --- a/pkg/sentry/fs/host/socket_test.go +++ b/pkg/sentry/fs/host/socket_test.go @@ -20,6 +20,7 @@ import ( "testing" "gvisor.googlesource.com/gvisor/pkg/fd" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/sentry/context/contexttest" ktime "gvisor.googlesource.com/gvisor/pkg/sentry/kernel/time" "gvisor.googlesource.com/gvisor/pkg/sentry/socket" @@ -28,7 +29,6 @@ import ( "gvisor.googlesource.com/gvisor/pkg/syserr" "gvisor.googlesource.com/gvisor/pkg/tcpip" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) var ( diff --git a/pkg/sentry/kernel/eventfd/BUILD b/pkg/sentry/kernel/eventfd/BUILD index cc1120b4f..d96803fc9 100644 --- a/pkg/sentry/kernel/eventfd/BUILD +++ b/pkg/sentry/kernel/eventfd/BUILD @@ -9,6 +9,7 @@ go_library( visibility = ["//pkg/sentry:internal"], deps = [ "//pkg/abi/linux", + "//pkg/fdnotifier", "//pkg/sentry/context", "//pkg/sentry/fs", "//pkg/sentry/fs/anon", @@ -16,7 +17,6 @@ go_library( "//pkg/sentry/usermem", "//pkg/syserror", "//pkg/waiter", - "//pkg/waiter/fdnotifier", ], ) diff --git a/pkg/sentry/kernel/eventfd/eventfd.go b/pkg/sentry/kernel/eventfd/eventfd.go index 26dc59a85..063a1d5f5 100644 --- a/pkg/sentry/kernel/eventfd/eventfd.go +++ b/pkg/sentry/kernel/eventfd/eventfd.go @@ -22,6 +22,7 @@ import ( "syscall" "gvisor.googlesource.com/gvisor/pkg/abi/linux" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/sentry/context" "gvisor.googlesource.com/gvisor/pkg/sentry/fs" "gvisor.googlesource.com/gvisor/pkg/sentry/fs/anon" @@ -29,7 +30,6 @@ import ( "gvisor.googlesource.com/gvisor/pkg/sentry/usermem" "gvisor.googlesource.com/gvisor/pkg/syserror" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) // EventOperations represents an event with the semantics of Linux's file-based event diff --git a/pkg/sentry/socket/hostinet/BUILD b/pkg/sentry/socket/hostinet/BUILD index c30220a46..b8dceb102 100644 --- a/pkg/sentry/socket/hostinet/BUILD +++ b/pkg/sentry/socket/hostinet/BUILD @@ -17,6 +17,7 @@ go_library( deps = [ "//pkg/abi/linux", "//pkg/binary", + "//pkg/fdnotifier", "//pkg/log", "//pkg/sentry/arch", "//pkg/sentry/context", @@ -34,6 +35,5 @@ go_library( "//pkg/syserr", "//pkg/syserror", "//pkg/waiter", - "//pkg/waiter/fdnotifier", ], ) diff --git a/pkg/sentry/socket/hostinet/socket.go b/pkg/sentry/socket/hostinet/socket.go index 34281cac0..f3ecb6dc3 100644 --- a/pkg/sentry/socket/hostinet/socket.go +++ b/pkg/sentry/socket/hostinet/socket.go @@ -19,6 +19,7 @@ import ( "syscall" "gvisor.googlesource.com/gvisor/pkg/abi/linux" + "gvisor.googlesource.com/gvisor/pkg/fdnotifier" "gvisor.googlesource.com/gvisor/pkg/sentry/context" "gvisor.googlesource.com/gvisor/pkg/sentry/fs" "gvisor.googlesource.com/gvisor/pkg/sentry/fs/fsutil" @@ -32,7 +33,6 @@ import ( "gvisor.googlesource.com/gvisor/pkg/syserr" "gvisor.googlesource.com/gvisor/pkg/syserror" "gvisor.googlesource.com/gvisor/pkg/waiter" - "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier" ) const ( diff --git a/pkg/waiter/fdnotifier/BUILD b/pkg/waiter/fdnotifier/BUILD deleted file mode 100644 index af6baa303..000000000 --- a/pkg/waiter/fdnotifier/BUILD +++ /dev/null @@ -1,14 +0,0 @@ -load("//tools/go_stateify:defs.bzl", "go_library") - -package(licenses = ["notice"]) # Apache 2.0 - -go_library( - name = "fdnotifier", - srcs = [ - "fdnotifier.go", - "poll_unsafe.go", - ], - importpath = "gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier", - visibility = ["//:sandbox"], - deps = ["//pkg/waiter"], -) diff --git a/pkg/waiter/fdnotifier/fdnotifier.go b/pkg/waiter/fdnotifier/fdnotifier.go deleted file mode 100644 index 624b1a0c5..000000000 --- a/pkg/waiter/fdnotifier/fdnotifier.go +++ /dev/null @@ -1,200 +0,0 @@ -// Copyright 2018 Google LLC -// -// 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 fdnotifier contains an adapter that translates IO events (e.g., a -// file became readable/writable) from native FDs to the notifications in the -// waiter package. It uses epoll in edge-triggered mode to receive notifications -// for registered FDs. -package fdnotifier - -import ( - "fmt" - "sync" - "syscall" - - "gvisor.googlesource.com/gvisor/pkg/waiter" -) - -type fdInfo struct { - queue *waiter.Queue - waiting bool -} - -// notifier holds all the state necessary to issue notifications when IO events -// occur in the observed FDs. -type notifier struct { - // epFD is the epoll file descriptor used to register for io - // notifications. - epFD int - - // mu protects fdMap. - mu sync.Mutex - - // fdMap maps file descriptors to their notification queues and waiting - // status. - fdMap map[int32]*fdInfo -} - -// newNotifier creates a new notifier object. -func newNotifier() (*notifier, error) { - epfd, err := syscall.EpollCreate1(0) - if err != nil { - return nil, err - } - - w := ¬ifier{ - epFD: epfd, - fdMap: make(map[int32]*fdInfo), - } - - go w.waitAndNotify() // S/R-SAFE: no waiter exists during save / load. - - return w, nil -} - -// waitFD waits on mask for fd. The fdMap mutex must be hold. -func (n *notifier) waitFD(fd int32, fi *fdInfo, mask waiter.EventMask) error { - if !fi.waiting && mask == 0 { - return nil - } - - e := syscall.EpollEvent{ - Events: uint32(mask) | -syscall.EPOLLET, - Fd: fd, - } - - switch { - case !fi.waiting && mask != 0: - if err := syscall.EpollCtl(n.epFD, syscall.EPOLL_CTL_ADD, int(fd), &e); err != nil { - return err - } - fi.waiting = true - case fi.waiting && mask == 0: - syscall.EpollCtl(n.epFD, syscall.EPOLL_CTL_DEL, int(fd), nil) - fi.waiting = false - case fi.waiting && mask != 0: - if err := syscall.EpollCtl(n.epFD, syscall.EPOLL_CTL_MOD, int(fd), &e); err != nil { - return err - } - } - - return nil -} - -// addFD adds an FD to the list of FDs observed by n. -func (n *notifier) addFD(fd int32, queue *waiter.Queue) { - n.mu.Lock() - defer n.mu.Unlock() - - // Panic if we're already notifying on this FD. - if _, ok := n.fdMap[fd]; ok { - panic(fmt.Sprintf("File descriptor %v added twice", fd)) - } - - // We have nothing to wait for at the moment. Just add it to the map. - n.fdMap[fd] = &fdInfo{queue: queue} -} - -// updateFD updates the set of events the fd needs to be notified on. -func (n *notifier) updateFD(fd int32) error { - n.mu.Lock() - defer n.mu.Unlock() - - if fi, ok := n.fdMap[fd]; ok { - return n.waitFD(fd, fi, fi.queue.Events()) - } - - return nil -} - -// RemoveFD removes an FD from the list of FDs observed by n. -func (n *notifier) removeFD(fd int32) { - n.mu.Lock() - defer n.mu.Unlock() - - // Remove from map, then from epoll object. - n.waitFD(fd, n.fdMap[fd], 0) - delete(n.fdMap, fd) -} - -// hasFD returns true if the fd is in the list of observed FDs. -func (n *notifier) hasFD(fd int32) bool { - n.mu.Lock() - defer n.mu.Unlock() - - _, ok := n.fdMap[fd] - return ok -} - -// waitAndNotify run is its own goroutine and loops waiting for io event -// notifications from the epoll object. Once notifications arrive, they are -// dispatched to the registered queue. -func (n *notifier) waitAndNotify() error { - e := make([]syscall.EpollEvent, 100) - for { - v, err := epollWait(n.epFD, e, -1) - if err == syscall.EINTR { - continue - } - - if err != nil { - return err - } - - n.mu.Lock() - for i := 0; i < v; i++ { - if fi, ok := n.fdMap[e[i].Fd]; ok { - fi.queue.Notify(waiter.EventMask(e[i].Events)) - } - } - n.mu.Unlock() - } -} - -var shared struct { - notifier *notifier - once sync.Once - initErr error -} - -// AddFD adds an FD to the list of observed FDs. -func AddFD(fd int32, queue *waiter.Queue) error { - shared.once.Do(func() { - shared.notifier, shared.initErr = newNotifier() - }) - - if shared.initErr != nil { - return shared.initErr - } - - shared.notifier.addFD(fd, queue) - return nil -} - -// UpdateFD updates the set of events the fd needs to be notified on. -func UpdateFD(fd int32) error { - return shared.notifier.updateFD(fd) -} - -// RemoveFD removes an FD from the list of observed FDs. -func RemoveFD(fd int32) { - shared.notifier.removeFD(fd) -} - -// HasFD returns true if the FD is in the list of observed FDs. -// -// This should only be used by tests to assert that FDs are correctly registered. -func HasFD(fd int32) bool { - return shared.notifier.hasFD(fd) -} diff --git a/pkg/waiter/fdnotifier/poll_unsafe.go b/pkg/waiter/fdnotifier/poll_unsafe.go deleted file mode 100644 index 8459d4c74..000000000 --- a/pkg/waiter/fdnotifier/poll_unsafe.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2018 Google LLC -// -// 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 fdnotifier - -import ( - "syscall" - "unsafe" - - "gvisor.googlesource.com/gvisor/pkg/waiter" -) - -// NonBlockingPoll polls the given FD in non-blocking fashion. It is used just -// to query the FD's current state. -func NonBlockingPoll(fd int32, mask waiter.EventMask) waiter.EventMask { - e := struct { - fd int32 - events int16 - revents int16 - }{ - fd: fd, - events: int16(mask), - } - - for { - n, _, err := syscall.RawSyscall(syscall.SYS_POLL, uintptr(unsafe.Pointer(&e)), 1, 0) - // Interrupted by signal, try again. - if err == syscall.EINTR { - continue - } - // If an error occur we'll conservatively say the FD is ready for - // whatever is being checked. - if err != 0 { - return mask - } - - // If no FDs were returned, it wasn't ready for anything. - if n == 0 { - return 0 - } - - // Otherwise we got the ready events in the revents field. - return waiter.EventMask(e.revents) - } -} - -// epollWait performs a blocking wait on epfd. -// -// Preconditions: -// * len(events) > 0 -func epollWait(epfd int, events []syscall.EpollEvent, msec int) (int, error) { - if len(events) == 0 { - panic("Empty events passed to EpollWait") - } - - // We actually use epoll_pwait with NULL sigmask instead of epoll_wait - // since that is what the Go >= 1.11 runtime prefers. - r, _, e := syscall.Syscall6(syscall.SYS_EPOLL_PWAIT, uintptr(epfd), uintptr(unsafe.Pointer(&events[0])), uintptr(len(events)), uintptr(msec), 0, 0) - if e != 0 { - return 0, e - } - return int(r), nil -} -- cgit v1.2.3