MCPcopy Index your code
hub / github.com/google/gvisor / NewMemoryFile

Function NewMemoryFile

pkg/sentry/pgalloc/pgalloc.go:423–466  ·  view source on GitHub ↗

NewMemoryFile creates a MemoryFile backed by the given file. If NewMemoryFile succeeds, ownership of file is transferred to the returned MemoryFile.

(file *os.File, opts MemoryFileOpts)

Source from the content-addressed store, hash-verified

421// NewMemoryFile succeeds, ownership of file is transferred to the returned
422// MemoryFile.
423func NewMemoryFile(file *os.File, opts MemoryFileOpts) (*MemoryFile, error) {
424 switch opts.DelayedEviction {
425 case DelayedEvictionDefault:
426 opts.DelayedEviction = DelayedEvictionEnabled
427 case DelayedEvictionDisabled, DelayedEvictionManual:
428 opts.UseHostMemcgPressure = false
429 case DelayedEvictionEnabled:
430 // ok
431 default:
432 return nil, fmt.Errorf("invalid MemoryFileOpts.DelayedEviction: %v", opts.DelayedEviction)
433 }
434
435 // Truncate the file to 0 bytes first to ensure that it's empty.
436 if err := file.Truncate(0); err != nil {
437 return nil, err
438 }
439 f := &MemoryFile{
440 opts: opts,
441 file: file,
442 }
443 f.initFields()
444
445 if f.opts.DelayedEviction == DelayedEvictionEnabled && f.opts.UseHostMemcgPressure {
446 stop, err := hostmm.NotifyCurrentMemcgPressureCallback(func() {
447 f.mu.Lock()
448 startedAny := f.startEvictionsLocked()
449 f.mu.Unlock()
450 if startedAny {
451 log.Debugf("pgalloc.MemoryFile performing evictions due to memcg pressure")
452 }
453 }, "low")
454 if err != nil {
455 return nil, fmt.Errorf("failed to configure memcg pressure level notifications: %v", err)
456 }
457 f.stopNotifyPressure = stop
458 }
459
460 go f.releaserMain() // S/R-SAFE: f.mu
461
462 if !opts.DisableIMAWorkAround {
463 IMAWorkAroundForMemFile(file.Fd())
464 }
465 return f, nil
466}
467
468func (f *MemoryFile) initFields() {
469 // Initially, all pages are void.

Callers 5

ContextFunction · 0.92
createMemoryFileFunction · 0.92
createMemoryFileFunction · 0.92
createPrivateMemoryFileFunction · 0.92
createMemoryFileFunction · 0.92

Calls 10

initFieldsMethod · 0.95
startEvictionsLockedMethod · 0.95
releaserMainMethod · 0.95
DebugfFunction · 0.92
IMAWorkAroundForMemFileFunction · 0.85
ErrorfMethod · 0.65
TruncateMethod · 0.65
LockMethod · 0.65
UnlockMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…