diff --git a/pkg/tcpip/link/channel/BUILD b/pkg/tcpip/link/channel/BUILD index f7746092f..9acaf111e 100644 --- a/pkg/tcpip/link/channel/BUILD +++ b/pkg/tcpip/link/channel/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,12 +6,31 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "channel", + prefix = "endpoint", +) + +declare_rwmutex( + name = "queue_mutex", + out = "queue_mutex.go", + package = "channel", + prefix = "queue", +) + go_library( name = "channel", - srcs = ["channel.go"], + srcs = [ + "channel.go", + "endpoint_mutex.go", + "queue_mutex.go", + ], visibility = ["//visibility:public"], deps = [ "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/link/channel/channel.go b/pkg/tcpip/link/channel/channel.go index 679631827..f08d864e8 100644 --- a/pkg/tcpip/link/channel/channel.go +++ b/pkg/tcpip/link/channel/channel.go @@ -20,7 +20,6 @@ package channel import ( "context" - "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -44,7 +43,7 @@ type NotificationHandle struct { type queue struct { // c is the outbound packet channel. c chan *stack.PacketBuffer - mu sync.RWMutex + mu queueRWMutex // +checklocks:mu notify []*NotificationHandle // +checklocks:mu @@ -142,7 +141,7 @@ type Endpoint struct { LinkEPCapabilities stack.LinkEndpointCapabilities SupportedGSOKind stack.SupportedGSO - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher // +checklocks:mu diff --git a/pkg/tcpip/link/fdbased/BUILD b/pkg/tcpip/link/fdbased/BUILD index 94a69f2d2..6a4b6730b 100644 --- a/pkg/tcpip/link/fdbased/BUILD +++ b/pkg/tcpip/link/fdbased/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_mutex", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,15 +6,39 @@ package( licenses = ["notice"], ) +declare_mutex( + name = "processor_mutex", + out = "processor_mutex.go", + package = "fdbased", + prefix = "processor", +) + +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "fdbased", + prefix = "endpoint", +) + +declare_rwmutex( + name = "injectable_endpoint_mutex", + out = "injectable_endpoint_mutex.go", + package = "fdbased", + prefix = "injectableEndpoint", +) + go_library( name = "fdbased", srcs = [ "endpoint.go", + "endpoint_mutex.go", "endpoint_unsafe.go", + "injectable_endpoint_mutex.go", "mmap.go", "mmap_nonlinux.go", "mmap_unsafe.go", "packet_dispatchers.go", + "processor_mutex.go", "processors.go", "save_restore.go", ], @@ -25,6 +50,7 @@ go_library( "//pkg/rawfile", "//pkg/sleep", "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/hash/jenkins", "//pkg/tcpip/header", diff --git a/pkg/tcpip/link/fdbased/endpoint.go b/pkg/tcpip/link/fdbased/endpoint.go index 04a2d2ddf..8d8cef703 100644 --- a/pkg/tcpip/link/fdbased/endpoint.go +++ b/pkg/tcpip/link/fdbased/endpoint.go @@ -133,7 +133,7 @@ type endpoint struct { inboundDispatchers []linkDispatcher - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher @@ -863,7 +863,7 @@ func (*endpoint) SetOnCloseAction(func()) {} type InjectableEndpoint struct { endpoint - mu sync.RWMutex `state:"nosave"` + mu injectableEndpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher } diff --git a/pkg/tcpip/link/fdbased/processors.go b/pkg/tcpip/link/fdbased/processors.go index 877c5b49e..48b97f381 100644 --- a/pkg/tcpip/link/fdbased/processors.go +++ b/pkg/tcpip/link/fdbased/processors.go @@ -32,7 +32,7 @@ import ( // +stateify savable type processor struct { - mu sync.Mutex `state:"nosave"` + mu processorMutex `state:"nosave"` // +checklocks:mu pkts stack.PacketBufferList diff --git a/pkg/tcpip/link/loopback/BUILD b/pkg/tcpip/link/loopback/BUILD index 7e066cf10..df031c2c9 100644 --- a/pkg/tcpip/link/loopback/BUILD +++ b/pkg/tcpip/link/loopback/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library") package( @@ -5,11 +6,23 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "loopback", + prefix = "endpoint", +) + go_library( name = "loopback", - srcs = ["loopback.go"], + srcs = [ + "endpoint_mutex.go", + "loopback.go", + ], visibility = ["//visibility:public"], deps = [ + "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/link/loopback/loopback.go b/pkg/tcpip/link/loopback/loopback.go index ffab1033a..bee3fb407 100644 --- a/pkg/tcpip/link/loopback/loopback.go +++ b/pkg/tcpip/link/loopback/loopback.go @@ -21,8 +21,6 @@ package loopback import ( - "sync" - "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -34,7 +32,7 @@ const ( // +stateify savable type endpoint struct { - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher // +checklocks:mu diff --git a/pkg/tcpip/link/muxed/BUILD b/pkg/tcpip/link/muxed/BUILD index f37a37c7b..2db29ebae 100644 --- a/pkg/tcpip/link/muxed/BUILD +++ b/pkg/tcpip/link/muxed/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,12 +6,24 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "muxed", + prefix = "endpoint", +) + go_library( name = "muxed", - srcs = ["injectable.go"], + srcs = [ + "endpoint_mutex.go", + "injectable.go", + ], visibility = ["//visibility:public"], deps = [ "//pkg/buffer", + "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/link/muxed/injectable.go b/pkg/tcpip/link/muxed/injectable.go index 224851775..0986fad6a 100644 --- a/pkg/tcpip/link/muxed/injectable.go +++ b/pkg/tcpip/link/muxed/injectable.go @@ -16,8 +16,6 @@ package muxed import ( - "sync" - "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -33,7 +31,7 @@ import ( type InjectableEndpoint struct { routes map[tcpip.Address]stack.InjectableLinkEndpoint - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher } diff --git a/pkg/tcpip/link/pipe/BUILD b/pkg/tcpip/link/pipe/BUILD index 4bfa14795..ebd0fa55f 100644 --- a/pkg/tcpip/link/pipe/BUILD +++ b/pkg/tcpip/link/pipe/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,11 +6,23 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "pipe", + prefix = "endpoint", +) + go_library( name = "pipe", - srcs = ["pipe.go"], + srcs = [ + "endpoint_mutex.go", + "pipe.go", + ], visibility = ["//visibility:public"], deps = [ + "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/link/pipe/pipe.go b/pkg/tcpip/link/pipe/pipe.go index ba1c85103..150dfff05 100644 --- a/pkg/tcpip/link/pipe/pipe.go +++ b/pkg/tcpip/link/pipe/pipe.go @@ -17,8 +17,6 @@ package pipe import ( - "sync" - "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -47,7 +45,7 @@ func New(linkAddr1, linkAddr2 tcpip.LinkAddress, mtu uint32) (*Endpoint, *Endpoi type Endpoint struct { linked *Endpoint - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher // +checklocks:mu diff --git a/pkg/tcpip/link/qdisc/fifo/BUILD b/pkg/tcpip/link/qdisc/fifo/BUILD index 9fabd3e48..c976ea0b1 100644 --- a/pkg/tcpip/link/qdisc/fifo/BUILD +++ b/pkg/tcpip/link/qdisc/fifo/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_mutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,9 +6,17 @@ package( licenses = ["notice"], ) +declare_mutex( + name = "dispatcher_mutex", + out = "dispatcher_mutex.go", + package = "fifo", + prefix = "queueDispatcher", +) + go_library( name = "fifo", srcs = [ + "dispatcher_mutex.go", "fifo.go", "packet_buffer_circular_list.go", ], @@ -16,6 +25,7 @@ go_library( "//pkg/atomicbitops", "//pkg/sleep", "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/stack", ], diff --git a/pkg/tcpip/link/qdisc/fifo/fifo.go b/pkg/tcpip/link/qdisc/fifo/fifo.go index 76d8dbe5b..5a9cd62ed 100644 --- a/pkg/tcpip/link/qdisc/fifo/fifo.go +++ b/pkg/tcpip/link/qdisc/fifo/fifo.go @@ -58,7 +58,7 @@ type discipline struct { type queueDispatcher struct { lower stack.LinkWriter - mu sync.Mutex `state:"nosave"` + mu queueDispatcherMutex `state:"nosave"` // +checklocks:mu queue packetBufferCircularList diff --git a/pkg/tcpip/link/sharedmem/BUILD b/pkg/tcpip/link/sharedmem/BUILD index 79baff878..7009a09cc 100644 --- a/pkg/tcpip/link/sharedmem/BUILD +++ b/pkg/tcpip/link/sharedmem/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,11 +6,27 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "sharedmem", + prefix = "endpoint", +) + +declare_rwmutex( + name = "server_endpoint_mutex", + out = "server_endpoint_mutex.go", + package = "sharedmem", + prefix = "serverEndpoint", +) + go_library( name = "sharedmem", srcs = [ + "endpoint_mutex.go", "queuepair.go", "rx.go", + "server_endpoint_mutex.go", "server_rx.go", "server_tx.go", "sharedmem.go", @@ -29,6 +46,7 @@ go_library( "//pkg/memutil", "//pkg/rawfile", "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/link/sharedmem/pipe", diff --git a/pkg/tcpip/link/sharedmem/sharedmem.go b/pkg/tcpip/link/sharedmem/sharedmem.go index 3c0e0e7c5..6cdffb034 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem.go +++ b/pkg/tcpip/link/sharedmem/sharedmem.go @@ -188,7 +188,7 @@ type endpoint struct { onClosed func(tcpip.Error) `state:"nosave"` // mu protects the following fields. - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // tx is the transmit queue. // +checklocks:mu diff --git a/pkg/tcpip/link/sharedmem/sharedmem_server.go b/pkg/tcpip/link/sharedmem/sharedmem_server.go index 578d6c0b4..15398b723 100644 --- a/pkg/tcpip/link/sharedmem/sharedmem_server.go +++ b/pkg/tcpip/link/sharedmem/sharedmem_server.go @@ -63,7 +63,7 @@ type serverEndpoint struct { onClosed func(tcpip.Error) `state:"nosave"` // mu protects the following fields. - mu sync.RWMutex `state:"nosave"` + mu serverEndpointRWMutex `state:"nosave"` // tx is the transmit queue. // +checklocks:mu diff --git a/pkg/tcpip/link/tun/BUILD b/pkg/tcpip/link/tun/BUILD index b65f4b4e5..d7c190762 100644 --- a/pkg/tcpip/link/tun/BUILD +++ b/pkg/tcpip/link/tun/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library") load("//tools/go_generics:defs.bzl", "go_template_instance") @@ -17,10 +18,18 @@ go_template_instance( }, ) +declare_rwmutex( + name = "device_mutex", + out = "device_mutex.go", + package = "tun", + prefix = "device", +) + go_library( name = "tun", srcs = [ "device.go", + "device_mutex.go", "protocol.go", "tun_endpoint_refs.go", "tun_unsafe.go", @@ -35,6 +44,7 @@ go_library( "//pkg/log", "//pkg/refs", "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/link/channel", diff --git a/pkg/tcpip/link/tun/device.go b/pkg/tcpip/link/tun/device.go index 13c757748..41c184ac2 100644 --- a/pkg/tcpip/link/tun/device.go +++ b/pkg/tcpip/link/tun/device.go @@ -20,7 +20,6 @@ import ( "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/context" "gvisor.dev/gvisor/pkg/errors/linuxerr" - "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/link/channel" @@ -46,7 +45,7 @@ var zeroMAC [6]byte type Device struct { waiter.Queue - mu sync.RWMutex `state:"nosave"` + mu deviceRWMutex `state:"nosave"` endpoint *tunEndpoint notifyHandle *channel.NotificationHandle flags Flags diff --git a/pkg/tcpip/link/veth/BUILD b/pkg/tcpip/link/veth/BUILD index 5108862b4..c0dcf97c5 100644 --- a/pkg/tcpip/link/veth/BUILD +++ b/pkg/tcpip/link/veth/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,12 +6,31 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "veth_mutex", + out = "veth_mutex.go", + package = "veth", + prefix = "veth", +) + +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "veth", + prefix = "endpoint", +) + go_library( name = "veth", - srcs = ["veth.go"], + srcs = [ + "endpoint_mutex.go", + "veth.go", + "veth_mutex.go", + ], visibility = ["//visibility:public"], deps = [ "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/link/veth/veth.go b/pkg/tcpip/link/veth/veth.go index 0cd2c416d..e9e35b510 100644 --- a/pkg/tcpip/link/veth/veth.go +++ b/pkg/tcpip/link/veth/veth.go @@ -16,7 +16,6 @@ package veth import ( - "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -27,7 +26,7 @@ var _ stack.GSOEndpoint = (*Endpoint)(nil) // +stateify savable type veth struct { - mu sync.RWMutex `state:"nosave"` + mu vethRWMutex `state:"nosave"` closed bool backlogQueue chan vethPacket `state:"nosave"` mtu uint32 @@ -73,7 +72,7 @@ type Endpoint struct { veth *veth - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher // linkAddr is the local address of this endpoint. diff --git a/pkg/tcpip/link/waitable/BUILD b/pkg/tcpip/link/waitable/BUILD index 933aae330..6e6ca0bea 100644 --- a/pkg/tcpip/link/waitable/BUILD +++ b/pkg/tcpip/link/waitable/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,14 +6,23 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "waitable", + prefix = "endpoint", +) + go_library( name = "waitable", srcs = [ + "endpoint_mutex.go", "waitable.go", ], visibility = ["//visibility:public"], deps = [ "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/link/waitable/waitable.go b/pkg/tcpip/link/waitable/waitable.go index f74a147d2..56327a5f1 100644 --- a/pkg/tcpip/link/waitable/waitable.go +++ b/pkg/tcpip/link/waitable/waitable.go @@ -37,7 +37,7 @@ var _ stack.LinkEndpoint = (*Endpoint)(nil) type Endpoint struct { dispatchGate sync.Gate - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checklocks:mu dispatcher stack.NetworkDispatcher diff --git a/pkg/tcpip/link/xdp/BUILD b/pkg/tcpip/link/xdp/BUILD index a8a309315..71f5da28c 100644 --- a/pkg/tcpip/link/xdp/BUILD +++ b/pkg/tcpip/link/xdp/BUILD @@ -1,3 +1,4 @@ +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") load("//tools:defs.bzl", "go_library", "go_test") package( @@ -5,16 +6,25 @@ package( licenses = ["notice"], ) +declare_rwmutex( + name = "endpoint_mutex", + out = "endpoint_mutex.go", + package = "xdp", + prefix = "endpoint", +) + go_library( name = "xdp", srcs = [ "endpoint.go", + "endpoint_mutex.go", ], visibility = ["//visibility:public"], deps = [ "//pkg/buffer", "//pkg/rawfile", "//pkg/sync", + "//pkg/sync/locking", "//pkg/tcpip", "//pkg/tcpip/header", "//pkg/tcpip/link/qdisc/fifo", diff --git a/pkg/tcpip/link/xdp/endpoint.go b/pkg/tcpip/link/xdp/endpoint.go index 57bd03734..3955facdc 100644 --- a/pkg/tcpip/link/xdp/endpoint.go +++ b/pkg/tcpip/link/xdp/endpoint.go @@ -53,7 +53,7 @@ type endpoint struct { // TODO(b/341946753): Restore when netstack is savable. closed func(tcpip.Error) `state:"nosave"` - mu sync.RWMutex `state:"nosave"` + mu endpointRWMutex `state:"nosave"` // +checkloks:mu networkDispatcher stack.NetworkDispatcher