use atomicbitops, not sync/atomic, in refsvfs2

Since refsvfs2 are used in netstack, we should use atomicbitops to avoid
breaking 32-bit builds.

On 64-bit builds there is no performance difference.

PiperOrigin-RevId: 439687980
This commit is contained in:
Kevin Krakauer
2022-04-05 15:16:01 -07:00
committed by gVisor bot
parent bfd3f716ab
commit 019e0c9301
18 changed files with 27 additions and 9 deletions
+1
View File
@@ -88,6 +88,7 @@ go_library(
marshal = True,
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/cleanup",
"//pkg/context",
"//pkg/fdchannel",
+9 -9
View File
@@ -18,8 +18,8 @@ package refs_template
import (
"fmt"
"sync/atomic"
"gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/refsvfs2"
)
@@ -57,13 +57,13 @@ type Refs struct {
// Speculative references are used for TryIncRef, to avoid a CompareAndSwap
// loop. See IncRef, DecRef and TryIncRef for details of how these fields are
// used.
refCount int64
refCount atomicbitops.AlignedAtomicInt64
}
// InitRefs initializes r with one reference and, if enabled, activates leak
// checking.
func (r *Refs) InitRefs() {
atomic.StoreInt64(&r.refCount, 1)
r.refCount.Store(1)
refsvfs2.Register(r)
}
@@ -85,14 +85,14 @@ func (r *Refs) LogRefs() bool {
// ReadRefs returns the current number of references. The returned count is
// inherently racy and is unsafe to use without external synchronization.
func (r *Refs) ReadRefs() int64 {
return atomic.LoadInt64(&r.refCount)
return r.refCount.Load()
}
// IncRef implements refs.RefCounter.IncRef.
//
//go:nosplit
func (r *Refs) IncRef() {
v := atomic.AddInt64(&r.refCount, 1)
v := r.refCount.Add(1)
if enableLogging {
refsvfs2.LogIncRef(r, v)
}
@@ -110,14 +110,14 @@ func (r *Refs) IncRef() {
//go:nosplit
func (r *Refs) TryIncRef() bool {
const speculativeRef = 1 << 32
if v := atomic.AddInt64(&r.refCount, speculativeRef); int32(v) == 0 {
if v := r.refCount.Add(speculativeRef); int32(v) == 0 {
// This object has already been freed.
atomic.AddInt64(&r.refCount, -speculativeRef)
r.refCount.Add(-speculativeRef)
return false
}
// Turn into a real reference.
v := atomic.AddInt64(&r.refCount, -speculativeRef+1)
v := r.refCount.Add(-speculativeRef + 1)
if enableLogging {
refsvfs2.LogTryIncRef(r, v)
}
@@ -137,7 +137,7 @@ func (r *Refs) TryIncRef() bool {
//
//go:nosplit
func (r *Refs) DecRef(destroy func()) {
v := atomic.AddInt64(&r.refCount, -1)
v := r.refCount.Add(-1)
if enableLogging {
refsvfs2.LogDecRef(r, v)
}
+1
View File
@@ -30,6 +30,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/bitmap",
"//pkg/context",
"//pkg/coverage",
+1
View File
@@ -28,6 +28,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/log",
+1
View File
@@ -46,6 +46,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/hostarch",
+1
View File
@@ -29,6 +29,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fdnotifier",
+2
View File
@@ -103,6 +103,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fspath",
@@ -134,6 +135,7 @@ go_test(
deps = [
":kernfs",
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fspath",
+1
View File
@@ -26,6 +26,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/refsvfs2",
+1
View File
@@ -80,6 +80,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/hostarch",
+1
View File
@@ -24,6 +24,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/coverage",
"//pkg/errors/linuxerr",
+1
View File
@@ -56,6 +56,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fspath",
+1
View File
@@ -220,6 +220,7 @@ go_library(
"//pkg/abi",
"//pkg/abi/linux",
"//pkg/abi/linux/errno",
"//pkg/atomicbitops",
"//pkg/bitmap",
"//pkg/bits",
"//pkg/bpf",
+1
View File
@@ -27,6 +27,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/hostarch",
+1
View File
@@ -38,6 +38,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fspath",
+1
View File
@@ -57,6 +57,7 @@ go_library(
visibility = ["//:sandbox"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fdnotifier",
+1
View File
@@ -94,6 +94,7 @@ go_library(
visibility = ["//pkg/sentry:internal"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/fd",
+1
View File
@@ -25,6 +25,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/abi/linux",
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/log",
+1
View File
@@ -71,6 +71,7 @@ go_library(
imports = ["gvisor.dev/gvisor/pkg/tcpip/buffer"],
visibility = ["//visibility:public"],
deps = [
"//pkg/atomicbitops",
"//pkg/log",
"//pkg/rand",
"//pkg/refsvfs2",