mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
sentry/socket: use lockdep mutexes
PiperOrigin-RevId: 448382489
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user