Cache pgalloc.MemoryFile in mm.MemoryManager.

This just saves an interface method call and indirection through kernel.Kernel
for a frequently-used resource.

PiperOrigin-RevId: 581318869
This commit is contained in:
Jamie Liu
2023-11-10 11:28:46 -08:00
committed by gVisor bot
parent cf9d55bb6e
commit b042aeefb7
4 changed files with 17 additions and 13 deletions
+2
View File
@@ -32,6 +32,7 @@ func NewMemoryManager(p platform.Platform, mfp pgalloc.MemoryFileProvider, sleep
return &MemoryManager{
p: p,
mfp: mfp,
mf: mfp.MemoryFile(),
haveASIO: p.SupportsAddressSpaceIO(),
privateRefs: &privateRefs{},
users: atomicbitops.FromInt32(1),
@@ -75,6 +76,7 @@ func (mm *MemoryManager) Fork(ctx context.Context) (*MemoryManager, error) {
mm2 := &MemoryManager{
p: mm.p,
mfp: mm.mfp,
mf: mm.mf,
haveASIO: mm.haveASIO,
layout: mm.layout,
privateRefs: mm.privateRefs,
+5
View File
@@ -61,6 +61,11 @@ type MemoryManager struct {
p platform.Platform
mfp pgalloc.MemoryFileProvider
// mf is the cached result of mfp.MemoryFile().
//
// mf is immutable.
mf *pgalloc.MemoryFile `state:"nosave"`
// haveASIO is the cached result of p.SupportsAddressSpaceIO(). Aside from
// eliminating an indirect call in the hot I/O path, this makes
// MemoryManager.asioEnabled() a leaf function, allowing it to be inlined.
+7 -9
View File
@@ -215,7 +215,6 @@ func (mm *MemoryManager) getPMAsInternalLocked(ctx context.Context, vseg vmaIter
}
atomic.StoreUintptr(&vma.lastFault, uintptr(ar.Start))
mf := mm.mfp.MemoryFile()
// Limit the range we allocate to ar, aligned to privateAllocUnit.
maskAR := privateAligned(ar)
didUnmapAS := false
@@ -241,7 +240,7 @@ func (mm *MemoryManager) getPMAsInternalLocked(ctx context.Context, vseg vmaIter
if vma.mappable == nil {
// Private anonymous mappings get pmas by allocating.
allocAR := optAR.Intersect(maskAR)
fr, err := mf.Allocate(uint64(allocAR.Length()), opts)
fr, err := mm.mf.Allocate(uint64(allocAR.Length()), opts)
if err != nil {
return pstart, pgap, err
}
@@ -252,9 +251,9 @@ func (mm *MemoryManager) getPMAsInternalLocked(ctx context.Context, vseg vmaIter
}
mm.addRSSLocked(allocAR)
mm.incPrivateRef(fr)
mf.IncRef(fr, memCgID)
mm.mf.IncRef(fr, memCgID)
pseg, pgap = mm.pmas.Insert(pgap, allocAR, pma{
file: mf,
file: mm.mf,
off: fr.Start,
translatePerms: hostarch.AnyAccess,
effectivePerms: vma.effectivePerms,
@@ -374,7 +373,7 @@ func (mm *MemoryManager) getPMAsInternalLocked(ctx context.Context, vseg vmaIter
return pstart, pseg.PrevGap(), err
}
// Copy contents.
fr, err := mf.Allocate(uint64(copyAR.Length()), pgalloc.AllocOpts{
fr, err := mm.mf.Allocate(uint64(copyAR.Length()), pgalloc.AllocOpts{
Kind: usage.Anonymous,
Mode: pgalloc.AllocateAndWritePopulate,
MemCgID: memCgID,
@@ -410,8 +409,8 @@ func (mm *MemoryManager) getPMAsInternalLocked(ctx context.Context, vseg vmaIter
}
oldpma.file.DecRef(pseg.fileRange())
mm.incPrivateRef(fr)
mf.IncRef(fr, memCgID)
oldpma.file = mf
mm.mf.IncRef(fr, memCgID)
oldpma.file = mm.mf
oldpma.off = fr.Start
oldpma.translatePerms = hostarch.AnyAccess
oldpma.effectivePerms = vma.effectivePerms
@@ -975,9 +974,8 @@ func (mm *MemoryManager) decPrivateRef(fr memmap.FileRange) {
refSet.MergeAdjacent(fr)
mm.privateRefs.mu.Unlock()
mf := mm.mfp.MemoryFile()
for _, fr := range freed {
mf.DecRef(fr)
mm.mf.DecRef(fr)
}
}
+3 -4
View File
@@ -37,9 +37,8 @@ func (mm *MemoryManager) InvalidateUnsavable(ctx context.Context) error {
// beforeSave is invoked by stateify.
func (mm *MemoryManager) beforeSave() {
mf := mm.mfp.MemoryFile()
for pseg := mm.pmas.FirstSegment(); pseg.Ok(); pseg = pseg.NextSegment() {
if pma := pseg.ValuePtr(); pma.file != mf {
if pma := pseg.ValuePtr(); pma.file != mm.mf {
// InvalidateUnsavable should have caused all such pmas to be
// invalidated.
panic(fmt.Sprintf("Can't save pma %#v with non-MemoryFile of type %T:\n%s", pseg.Range(), pma.file, mm))
@@ -49,10 +48,10 @@ func (mm *MemoryManager) beforeSave() {
// afterLoad is invoked by stateify.
func (mm *MemoryManager) afterLoad() {
mm.mf = mm.mfp.MemoryFile()
mm.haveASIO = mm.p.SupportsAddressSpaceIO()
mf := mm.mfp.MemoryFile()
for pseg := mm.pmas.FirstSegment(); pseg.Ok(); pseg = pseg.NextSegment() {
pseg.ValuePtr().file = mf
pseg.ValuePtr().file = mm.mf
}
}