diff --git a/pkg/sentry/fsimpl/gofer/gofer.go b/pkg/sentry/fsimpl/gofer/gofer.go index efdeeabbb..ee8430f2d 100644 --- a/pkg/sentry/fsimpl/gofer/gofer.go +++ b/pkg/sentry/fsimpl/gofer/gofer.go @@ -113,13 +113,18 @@ type dentryCache struct { // SetDentryCacheSize sets the size of the global gofer dentry cache. func SetDentryCacheSize(size int) { - globalDentryCache.mu.Lock() - defer globalDentryCache.mu.Unlock() - globalDentryCache.maxCachedDentries = uint64(size) + if size < 0 { + return + } + if globalDentryCache != nil { + log.Warningf("Global dentry cache has already been initialized. Ignoring subsequent attempt.") + return + } + globalDentryCache = &dentryCache{maxCachedDentries: uint64(size)} } // globalDentryCache is a global cache of dentries across all gofers. -var globalDentryCache dentryCache +var globalDentryCache *dentryCache // Valid values for "trans" mount option. const transportModeFD = "fd" @@ -494,13 +499,11 @@ func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.Virt } // Did the user configure a global dentry cache? - globalDentryCache.mu.Lock() - if globalDentryCache.maxCachedDentries >= 1 { - fs.dentryCache = &globalDentryCache + if globalDentryCache != nil { + fs.dentryCache = globalDentryCache } else { fs.dentryCache = &dentryCache{maxCachedDentries: defaultMaxCachedDentries} } - globalDentryCache.mu.Unlock() fs.vfsfs.Init(vfsObj, &fstype, fs) diff --git a/runsc/config/flags.go b/runsc/config/flags.go index 1b6f965c3..dee52a3ed 100644 --- a/runsc/config/flags.go +++ b/runsc/config/flags.go @@ -84,7 +84,7 @@ func RegisterFlags(flagSet *flag.FlagSet) { flagSet.Bool("cgroupfs", false, "Automatically mount cgroupfs.") flagSet.Bool("ignore-cgroups", false, "don't configure cgroups.") flagSet.Int("fdlimit", -1, "Specifies a limit on the number of host file descriptors that can be open. Applies separately to the sentry and gofer. Note: each file in the sandbox holds more than one host FD open.") - flagSet.Int("dcache", 0, "Set the global gofer direct cache size. This acts as a coarse-grained control on the number of host FDs simultaneously open by the sentry. If zero, per-mount caches are used.") + flagSet.Int("dcache", -1, "Set the global dentry cache size. This acts as a coarse-grained control on the number of host FDs simultaneously open by the sentry. If negative, per-mount caches are used.") // Flags that control sandbox runtime behavior: network related. flagSet.Var(networkTypePtr(NetworkSandbox), "network", "specifies which network to use: sandbox (default), host, none. Using network inside the sandbox is more secure because it's isolated from the host network.")