sentry/socket: use lockdep mutexes

PiperOrigin-RevId: 448382489
This commit is contained in:
Andrei Vagin
2022-05-12 18:18:15 -07:00
committed by gVisor bot
parent db16575cb5
commit d44862f672
4 changed files with 56 additions and 15 deletions
+37
View File
@@ -3,6 +3,39 @@ load("//tools/go_generics:defs.bzl", "go_template_instance")
package(licenses = ["notice"])
go_template_instance(
name = "queue_mutex",
out = "queue_mutex.go",
package = "transport",
prefix = "queue",
substrs = {
"genericMark": "unixQueue",
},
template = "//pkg/sync/locking:generic_mutex",
)
go_template_instance(
name = "stream_queue_receiver_mutex",
out = "stream_queue_receiver_mutex.go",
package = "transport",
prefix = "streamQueueReceiver",
substrs = {
"genericMark": "streamQueueReceiver",
},
template = "//pkg/sync/locking:generic_mutex",
)
go_template_instance(
name = "endpoint_mutex",
out = "endpoint_mutex.go",
package = "transport",
prefix = "endpoint",
substrs = {
"genericMark": "unixEndpoint",
},
template = "//pkg/sync/locking:generic_mutex",
)
go_template_instance(
name = "transport_message_list",
out = "transport_message_list.go",
@@ -44,13 +77,16 @@ go_library(
"connectioned_state.go",
"connectionless.go",
"connectionless_state.go",
"endpoint_mutex.go",
"host.go",
"host_connected_endpoint_refs.go",
"host_iovec.go",
"host_unsafe.go",
"queue.go",
"queue_mutex.go",
"queue_refs.go",
"save_restore.go",
"stream_queue_receiver_mutex.go",
"transport_message_list.go",
"unix.go",
],
@@ -70,6 +106,7 @@ go_library(
"//pkg/sentry/inet",
"//pkg/sentry/uniqueid",
"//pkg/sync",
"//pkg/sync/locking",
"//pkg/syserr",
"//pkg/tcpip",
"//pkg/tcpip/buffer",
@@ -23,12 +23,18 @@ import (
"gvisor.dev/gvisor/pkg/fdnotifier"
"gvisor.dev/gvisor/pkg/lisafs"
"gvisor.dev/gvisor/pkg/sentry/uniqueid"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/syserr"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/waiter"
)
type locker interface {
Lock()
Unlock()
NestedLock()
NestedUnlock()
}
// A ConnectingEndpoint is a connectioned unix endpoint that is attempting to
// establish a bidirectional connection with a BoundEndpoint.
type ConnectingEndpoint interface {
@@ -51,7 +57,7 @@ type ConnectingEndpoint interface {
// Locker protects the following methods. While locked, only the holder of
// the lock can change the return value of the protected methods.
sync.Locker
locker
// Connected returns true iff the ConnectingEndpoint is in the connected
// state. ConnectingEndpoints can only be connected to a single endpoint,
@@ -292,27 +298,27 @@ func (e *connectionedEndpoint) BidirectionalConnect(ctx context.Context, ce Conn
// Do a dance to safely acquire locks on both endpoints.
if e.id < ce.ID() {
e.Lock()
ce.Lock()
ce.NestedLock()
} else {
ce.Lock()
e.Lock()
e.NestedLock()
}
// Check connecting state.
if ce.Connected() {
e.Unlock()
e.NestedUnlock()
ce.Unlock()
return syserr.ErrAlreadyConnected
}
if ce.ListeningLocked() {
e.Unlock()
e.NestedUnlock()
ce.Unlock()
return syserr.ErrInvalidEndpointState
}
// Check bound state.
if !e.ListeningLocked() {
e.Unlock()
e.NestedUnlock()
ce.Unlock()
return syserr.ErrConnectionRefused
}
@@ -363,7 +369,7 @@ func (e *connectionedEndpoint) BidirectionalConnect(ctx context.Context, ce Conn
}
// Notify can deadlock if we are holding these locks.
e.Unlock()
e.NestedUnlock()
ce.Unlock()
// Notify on both ends.
@@ -373,9 +379,9 @@ func (e *connectionedEndpoint) BidirectionalConnect(ctx context.Context, ce Conn
return nil
default:
// Busy; return EAGAIN per spec.
ne.Close(ctx)
e.Unlock()
e.NestedUnlock()
ce.Unlock()
ne.Close(ctx)
return syserr.ErrTryAgain
}
}
+1 -2
View File
@@ -16,7 +16,6 @@ package transport
import (
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/syserr"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
@@ -32,7 +31,7 @@ type queue struct {
ReaderQueue *waiter.Queue
WriterQueue *waiter.Queue
mu sync.Mutex `state:"nosave"`
mu queueMutex `state:"nosave"`
closed bool
unread bool
used int64
+2 -3
View File
@@ -20,7 +20,6 @@ import (
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/lisafs"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/syserr"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
@@ -423,7 +422,7 @@ func (q *queueReceiver) Release(ctx context.Context) {
type streamQueueReceiver struct {
queueReceiver
mu sync.Mutex `state:"nosave"`
mu streamQueueReceiverMutex `state:"nosave"`
buffer []byte
control ControlMessages
addr tcpip.FullAddress
@@ -765,7 +764,7 @@ type baseEndpoint struct {
//
// See the lock ordering comment in package kernel/epoll regarding when
// this lock can safely be held.
sync.Mutex `state:"nosave"`
endpointMutex `state:"nosave"`
// receiver allows Messages to be received.
receiver Receiver