diff --git a/pkg/procid/BUILD b/pkg/hosttid/BUILD similarity index 53% rename from pkg/procid/BUILD rename to pkg/hosttid/BUILD index 9124b5b05..97a84d216 100644 --- a/pkg/procid/BUILD +++ b/pkg/hosttid/BUILD @@ -4,37 +4,37 @@ load("//tools/nogo:defs.bzl", "nogo_facts") package(licenses = ["notice"]) nogo_facts( - name = "procid_impl", - srcs = ["procid.go"], - output = "procid_impl.s", + name = "hosttid_impl", + srcs = ["hosttid.go"], + output = "hosttid_impl.s", template = select_arch( - amd64 = "procid_amd64.s", - arm64 = "procid_arm64.s", + amd64 = "hosttid_amd64.s", + arm64 = "hosttid_arm64.s", ), ) arch_genrule( - name = "procid_impl_arch", - src = ":procid_impl", - template = "procid_impl_%s.s", + name = "hosttid_impl_arch", + src = ":hosttid_impl", + template = "hosttid_impl_%s.s", ) go_library( - name = "procid", + name = "hosttid", srcs = [ - "procid.go", - ":procid_impl_arch", + "hosttid.go", + ":hosttid_impl_arch", ], visibility = ["//visibility:public"], ) go_test( - name = "procid_test", + name = "hosttid_test", size = "small", srcs = [ - "procid_test.go", + "hosttid_test.go", ], - library = ":procid", + library = ":hosttid", deps = [ "//pkg/sync", "@org_golang_x_sys//unix:go_default_library", @@ -42,13 +42,13 @@ go_test( ) go_test( - name = "procid_net_test", + name = "hosttid_net_test", size = "small", srcs = [ - "procid_net_test.go", - "procid_test.go", + "hosttid_net_test.go", + "hosttid_test.go", ], - library = ":procid", + library = ":hosttid", deps = [ "//pkg/sync", "@org_golang_x_sys//unix:go_default_library", diff --git a/pkg/procid/procid.go b/pkg/hosttid/hosttid.go similarity index 67% rename from pkg/procid/procid.go rename to pkg/hosttid/hosttid.go index 55a16a80a..a556b1941 100644 --- a/pkg/procid/procid.go +++ b/pkg/hosttid/hosttid.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package procid provides a way to get the current system thread identifier. -package procid +// Package hosttid provides the Current function. +package hosttid import ( "runtime" @@ -22,7 +22,9 @@ import ( // Dummy references for facts. const _ = runtime.Compiler -// Current returns the current system thread identifier. +// Current returns the caller's host thread ID. Unless runtime.LockOSThread() +// is in effect, this function is inherently racy since the Go runtime may +// migrate the calling goroutine to another thread at any time. // -// Precondition: This should only be called with the runtime OS thread locked. +// Current is equivalent to unix.Gettid(), but faster. func Current() uint64 diff --git a/pkg/procid/procid_amd64.s b/pkg/hosttid/hosttid_amd64.s similarity index 100% rename from pkg/procid/procid_amd64.s rename to pkg/hosttid/hosttid_amd64.s diff --git a/pkg/procid/procid_arm64.s b/pkg/hosttid/hosttid_arm64.s similarity index 100% rename from pkg/procid/procid_arm64.s rename to pkg/hosttid/hosttid_arm64.s diff --git a/pkg/procid/procid_net_test.go b/pkg/hosttid/hosttid_net_test.go similarity index 97% rename from pkg/procid/procid_net_test.go rename to pkg/hosttid/hosttid_net_test.go index b628e2285..ec0b98767 100644 --- a/pkg/procid/procid_net_test.go +++ b/pkg/hosttid/hosttid_net_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package procid +package hosttid // This file is just to force the inclusion of the "net" package, which will // make the test binary a cgo one. diff --git a/pkg/procid/procid_test.go b/pkg/hosttid/hosttid_test.go similarity index 99% rename from pkg/procid/procid_test.go rename to pkg/hosttid/hosttid_test.go index a08110b35..7bb7142d7 100644 --- a/pkg/procid/procid_test.go +++ b/pkg/hosttid/hosttid_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package procid +package hosttid import ( "os" diff --git a/pkg/sentry/platform/kvm/BUILD b/pkg/sentry/platform/kvm/BUILD index 878c8553f..97140924f 100644 --- a/pkg/sentry/platform/kvm/BUILD +++ b/pkg/sentry/platform/kvm/BUILD @@ -92,9 +92,9 @@ go_library( "//pkg/context", "//pkg/cpuid", "//pkg/hostarch", + "//pkg/hosttid", "//pkg/log", "//pkg/metric", - "//pkg/procid", "//pkg/ring0", "//pkg/ring0/pagetables", "//pkg/seccomp", diff --git a/pkg/sentry/platform/kvm/machine.go b/pkg/sentry/platform/kvm/machine.go index 93b3e2e72..b0ee55c1d 100644 --- a/pkg/sentry/platform/kvm/machine.go +++ b/pkg/sentry/platform/kvm/machine.go @@ -25,9 +25,9 @@ import ( "gvisor.dev/gvisor/pkg/abi/linux" "gvisor.dev/gvisor/pkg/atomicbitops" "gvisor.dev/gvisor/pkg/hostarch" + "gvisor.dev/gvisor/pkg/hosttid" "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/metric" - "gvisor.dev/gvisor/pkg/procid" "gvisor.dev/gvisor/pkg/ring0" "gvisor.dev/gvisor/pkg/ring0/pagetables" "gvisor.dev/gvisor/pkg/seccomp" @@ -447,7 +447,7 @@ func (m *machine) Destroy() { func (m *machine) Get() *vCPU { m.mu.RLock() runtime.LockOSThread() - tid := procid.Current() + tid := hosttid.Current() // Check for an exact match. if c := m.vCPUsByTID[tid]; c != nil { @@ -467,7 +467,7 @@ func (m *machine) Get() *vCPU { runtime.UnlockOSThread() m.mu.Lock() runtime.LockOSThread() - tid = procid.Current() + tid = hosttid.Current() // Recheck for an exact match. if c := m.vCPUsByTID[tid]; c != nil { diff --git a/pkg/sentry/platform/ptrace/BUILD b/pkg/sentry/platform/ptrace/BUILD index 0d2e1ef73..27b202593 100644 --- a/pkg/sentry/platform/ptrace/BUILD +++ b/pkg/sentry/platform/ptrace/BUILD @@ -27,8 +27,8 @@ go_library( "//pkg/context", "//pkg/cpuid", "//pkg/hostarch", + "//pkg/hosttid", "//pkg/log", - "//pkg/procid", "//pkg/safecopy", "//pkg/seccomp", "//pkg/sentry/arch", diff --git a/pkg/sentry/platform/ptrace/subprocess.go b/pkg/sentry/platform/ptrace/subprocess.go index 45405d651..3d23f78a6 100644 --- a/pkg/sentry/platform/ptrace/subprocess.go +++ b/pkg/sentry/platform/ptrace/subprocess.go @@ -22,8 +22,8 @@ import ( "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/abi/linux" "gvisor.dev/gvisor/pkg/hostarch" + "gvisor.dev/gvisor/pkg/hosttid" "gvisor.dev/gvisor/pkg/log" - "gvisor.dev/gvisor/pkg/procid" "gvisor.dev/gvisor/pkg/sentry/arch" "gvisor.dev/gvisor/pkg/sentry/memmap" "gvisor.dev/gvisor/pkg/sentry/platform" @@ -518,7 +518,7 @@ func (s *subprocess) switchToApp(c *context, ac *arch.Context64) bool { fpState := ac.FloatingPointData() // Grab our thread from the pool. - currentTID := int32(procid.Current()) + currentTID := int32(hosttid.Current()) t := s.sysemuThreads.lookupOrCreate(currentTID, s.newThread) // Reset necessary registers. @@ -627,7 +627,7 @@ func (s *subprocess) syscall(sysno uintptr, args ...arch.SyscallArgument) (uintp // Grab a thread. runtime.LockOSThread() defer runtime.UnlockOSThread() - currentTID := int32(procid.Current()) + currentTID := int32(hosttid.Current()) t := s.syscallThreads.lookupOrCreate(currentTID, s.newThread) return t.syscallIgnoreInterrupt(&t.initRegs, sysno, args...) diff --git a/pkg/sentry/platform/ptrace/subprocess_linux.go b/pkg/sentry/platform/ptrace/subprocess_linux.go index 513346c42..901ad9213 100644 --- a/pkg/sentry/platform/ptrace/subprocess_linux.go +++ b/pkg/sentry/platform/ptrace/subprocess_linux.go @@ -22,8 +22,8 @@ import ( "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/abi/linux" + "gvisor.dev/gvisor/pkg/hosttid" "gvisor.dev/gvisor/pkg/log" - "gvisor.dev/gvisor/pkg/procid" "gvisor.dev/gvisor/pkg/seccomp" "gvisor.dev/gvisor/pkg/sentry/arch" ) @@ -210,7 +210,7 @@ func forkStub(flags uintptr, instrs []linux.BPFInstruction) (*thread, error) { func (s *subprocess) createStub() (*thread, error) { // There's no need to lock the runtime thread here, as this can only be // called from a context that is already locked. - currentTID := int32(procid.Current()) + currentTID := int32(hosttid.Current()) t := s.syscallThreads.lookupOrCreate(currentTID, s.newThread) // Pass the expected PPID to the child via R15.