diff --git a/pkg/sentry/fsutil/BUILD b/pkg/sentry/fsutil/BUILD index 4a3b1624f..6d57175b9 100644 --- a/pkg/sentry/fsutil/BUILD +++ b/pkg/sentry/fsutil/BUILD @@ -1,11 +1,26 @@ load("//tools:defs.bzl", "go_library", "go_test") load("//tools/go_generics:defs.bzl", "go_template_instance") +load("//pkg/sync/locking:locking.bzl", "declare_mutex") package( default_applicable_licenses = ["//:license"], licenses = ["notice"], ) +declare_mutex( + name = "refs_mutex", + out = "refs_mutex.go", + package = "fsutil", + prefix = "refs", +) + +declare_mutex( + name = "maps_mutex", + out = "maps_mutex.go", + package = "fsutil", + prefix = "maps", +) + go_template_instance( name = "dirty_set_impl", out = "dirty_set_impl.go", @@ -70,6 +85,8 @@ go_library( "host_file_mapper.go", "host_file_mapper_state.go", "host_file_mapper_unsafe.go", + "maps_mutex.go", + "refs_mutex.go", ], visibility = ["//pkg/sentry:internal"], deps = [ @@ -88,6 +105,7 @@ go_library( "//pkg/sentry/usage", "//pkg/state", "//pkg/sync", + "//pkg/sync/locking", "//pkg/usermem", "//pkg/waiter", "@org_golang_x_sys//unix:go_default_library", diff --git a/pkg/sentry/fsutil/host_file_mapper.go b/pkg/sentry/fsutil/host_file_mapper.go index 7849069da..456bf9b53 100644 --- a/pkg/sentry/fsutil/host_file_mapper.go +++ b/pkg/sentry/fsutil/host_file_mapper.go @@ -22,7 +22,6 @@ import ( "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/safemem" "gvisor.dev/gvisor/pkg/sentry/memmap" - "gvisor.dev/gvisor/pkg/sync" ) // HostFileMapper caches mappings of an arbitrary host file descriptor. It is @@ -35,13 +34,13 @@ type HostFileMapper struct { // size and alignment chunkSize, and caches mappings of the file on a chunk // granularity. - refsMu sync.Mutex `state:"nosave"` + refsMu refsMutex `state:"nosave"` // refs maps chunk start offsets to the sum of reference counts for all // pages in that chunk. refs is protected by refsMu. refs map[uint64]int32 - mapsMu sync.Mutex `state:"nosave"` + mapsMu mapsMutex `state:"nosave"` // mappings maps chunk start offsets to mappings of those chunks, // obtained by calling unix.Mmap. mappings is protected by diff --git a/pkg/sentry/kernel/BUILD b/pkg/sentry/kernel/BUILD index e78262e66..a1b5da7a8 100644 --- a/pkg/sentry/kernel/BUILD +++ b/pkg/sentry/kernel/BUILD @@ -36,6 +36,13 @@ declare_mutex( prefix = "task", ) +declare_mutex( + name = "task_work_mutex", + out = "task_work_mutex.go", + package = "kernel", + prefix = "taskWork", +) + declare_mutex( name = "cgroup_mutex", out = "cgroup_mutex.go", @@ -261,6 +268,7 @@ go_library( "task_syscall.go", "task_usermem.go", "task_work.go", + "task_work_mutex.go", "taskset_mutex.go", "thread_group.go", "thread_group_timer_mutex.go", diff --git a/pkg/sentry/kernel/fasync/BUILD b/pkg/sentry/kernel/fasync/BUILD index e4517b2da..33c74e84e 100644 --- a/pkg/sentry/kernel/fasync/BUILD +++ b/pkg/sentry/kernel/fasync/BUILD @@ -1,13 +1,32 @@ load("//tools:defs.bzl", "go_library") +load("//pkg/sync/locking:locking.bzl", "declare_mutex") package( default_applicable_licenses = ["//:license"], licenses = ["notice"], ) +declare_mutex( + name = "reg_mutex", + out = "reg_mutex.go", + package = "fasync", + prefix = "reg", +) + +declare_mutex( + name = "file_mutex", + out = "file_mutex.go", + package = "fasync", + prefix = "file", +) + go_library( name = "fasync", - srcs = ["fasync.go"], + srcs = [ + "fasync.go", + "file_mutex.go", + "reg_mutex.go", + ], visibility = ["//:sandbox"], deps = [ "//pkg/abi/linux", @@ -16,6 +35,7 @@ go_library( "//pkg/sentry/kernel/auth", "//pkg/sentry/vfs", "//pkg/sync", + "//pkg/sync/locking", "//pkg/waiter", ], ) diff --git a/pkg/sentry/kernel/fasync/fasync.go b/pkg/sentry/kernel/fasync/fasync.go index b682f5898..c8c7e6325 100644 --- a/pkg/sentry/kernel/fasync/fasync.go +++ b/pkg/sentry/kernel/fasync/fasync.go @@ -21,7 +21,6 @@ import ( "gvisor.dev/gvisor/pkg/sentry/kernel" "gvisor.dev/gvisor/pkg/sentry/kernel/auth" "gvisor.dev/gvisor/pkg/sentry/vfs" - "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/waiter" ) @@ -67,12 +66,12 @@ type FileAsync struct { // through the registration action itself. // // Lock ordering: regMu, mu. - regMu sync.Mutex `state:"nosave"` + regMu regMutex `state:"nosave"` // mu protects all following fields. // // Lock ordering: e.mu, mu. - mu sync.Mutex `state:"nosave"` + mu fileMutex `state:"nosave"` requester *auth.Credentials registered bool // signal is the signal to deliver upon I/O being available. diff --git a/pkg/sentry/kernel/task.go b/pkg/sentry/kernel/task.go index b9e1eb52e..97516c6c6 100644 --- a/pkg/sentry/kernel/task.go +++ b/pkg/sentry/kernel/task.go @@ -74,7 +74,7 @@ type Task struct { taskWorkCount atomicbitops.Int32 // taskWorkMu protects taskWork. - taskWorkMu sync.Mutex `state:"nosave"` + taskWorkMu taskWorkMutex `state:"nosave"` // taskWork is a queue of work to be executed before resuming user execution. // It is similar to the task_work mechanism in Linux. diff --git a/pkg/sentry/pgalloc/BUILD b/pkg/sentry/pgalloc/BUILD index 090f0e143..d0829dd69 100644 --- a/pkg/sentry/pgalloc/BUILD +++ b/pkg/sentry/pgalloc/BUILD @@ -1,11 +1,26 @@ load("//tools:defs.bzl", "go_library", "go_test") load("//tools/go_generics:defs.bzl", "go_template_instance") +load("//pkg/sync/locking:locking.bzl", "declare_mutex") package( default_applicable_licenses = ["//:license"], licenses = ["notice"], ) +declare_mutex( + name = "memory_file_mutex", + out = "memory_file_mutex.go", + package = "pgalloc", + prefix = "memoryFile", +) + +declare_mutex( + name = "mappings_mutex", + out = "mappings_mutex.go", + package = "pgalloc", + prefix = "mappings", +) + go_template_instance( name = "evictable_range", out = "evictable_range.go", @@ -78,6 +93,8 @@ go_library( "context.go", "evictable_range.go", "evictable_range_set.go", + "mappings_mutex.go", + "memory_file_mutex.go", "pgalloc.go", "pgalloc_unsafe.go", "reclaim_set.go", @@ -101,6 +118,7 @@ go_library( "//pkg/state", "//pkg/state/wire", "//pkg/sync", + "//pkg/sync/locking", "//pkg/usermem", "@org_golang_x_sys//unix:go_default_library", ], diff --git a/pkg/sentry/pgalloc/pgalloc.go b/pkg/sentry/pgalloc/pgalloc.go index 3757fc3eb..30b714c1e 100644 --- a/pkg/sentry/pgalloc/pgalloc.go +++ b/pkg/sentry/pgalloc/pgalloc.go @@ -96,7 +96,7 @@ type MemoryFile struct { // file is the backing file. The file pointer is immutable. file *os.File - mu sync.Mutex + mu memoryFileMutex // usage maps each page in the file to metadata for that page. Pages for // which no segment exists in usage are both unallocated (not in use) and @@ -152,7 +152,7 @@ type MemoryFile struct { // only requires *either* holding mappingsMu or using atomic memory // operations. This allows MemoryFile.MapInternal to avoid locking in the // common case where chunk mappings already exist. - mappingsMu sync.Mutex + mappingsMu mappingsMutex mappings atomic.Value // destroyed is set by Destroy to instruct the reclaimer goroutine to diff --git a/pkg/sentry/usage/BUILD b/pkg/sentry/usage/BUILD index 5fac80c28..e142a7698 100644 --- a/pkg/sentry/usage/BUILD +++ b/pkg/sentry/usage/BUILD @@ -1,16 +1,25 @@ load("//tools:defs.bzl", "go_library") +load("//pkg/sync/locking:locking.bzl", "declare_rwmutex") package( default_applicable_licenses = ["//:license"], licenses = ["notice"], ) +declare_rwmutex( + name = "memory_mutex", + out = "memory_mutex.go", + package = "usage", + prefix = "memory", +) + go_library( name = "usage", srcs = [ "cpu.go", "io.go", "memory.go", + "memory_mutex.go", "memory_unsafe.go", "usage.go", ], @@ -22,6 +31,7 @@ go_library( "//pkg/bits", "//pkg/memutil", "//pkg/sync", + "//pkg/sync/locking", "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/sentry/usage/memory.go b/pkg/sentry/usage/memory.go index 9966bd1e1..88c5a0f94 100644 --- a/pkg/sentry/usage/memory.go +++ b/pkg/sentry/usage/memory.go @@ -22,7 +22,6 @@ import ( "gvisor.dev/gvisor/pkg/atomicbitops" "gvisor.dev/gvisor/pkg/bits" "gvisor.dev/gvisor/pkg/memutil" - "gvisor.dev/gvisor/pkg/sync" ) // MemoryKind represents a type of memory used by the application. @@ -118,7 +117,7 @@ type RTMemoryStats struct { // MemoryLocked is Memory with access methods. type MemoryLocked struct { - mu sync.RWMutex + mu memoryRWMutex // memoryStats records the memory stats. memoryStats // RTMemoryStats records the memory stats that need to be exposed through