github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/fsutil/frame_ref_set.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fsutil
    16  
    17  import (
    18  	"math"
    19  
    20  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/memmap"
    21  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/usage"
    22  )
    23  
    24  // FrameRefSegInfo holds reference count and memory cgroup id of the segment.
    25  type FrameRefSegInfo struct {
    26  	// refs indicates the reference count of the segment.
    27  	refs uint64
    28  	// memCgID is the memory cgroup id of the first task which touches the
    29  	// segment. This will not be changed over the lifetime of the segment.
    30  	memCgID uint32
    31  }
    32  
    33  // FrameRefSetFunctions implements segment.Functions for FrameRefSet.
    34  type FrameRefSetFunctions struct{}
    35  
    36  // MinKey implements segment.Functions.MinKey.
    37  func (FrameRefSetFunctions) MinKey() uint64 {
    38  	return 0
    39  }
    40  
    41  // MaxKey implements segment.Functions.MaxKey.
    42  func (FrameRefSetFunctions) MaxKey() uint64 {
    43  	return math.MaxUint64
    44  }
    45  
    46  // ClearValue implements segment.Functions.ClearValue.
    47  func (FrameRefSetFunctions) ClearValue(val *FrameRefSegInfo) {
    48  }
    49  
    50  // Merge implements segment.Functions.Merge.
    51  func (FrameRefSetFunctions) Merge(_ memmap.FileRange, val1 FrameRefSegInfo, _ memmap.FileRange, val2 FrameRefSegInfo) (FrameRefSegInfo, bool) {
    52  	if val1 != val2 {
    53  		return FrameRefSegInfo{}, false
    54  	}
    55  	return val1, true
    56  }
    57  
    58  // Split implements segment.Functions.Split.
    59  func (FrameRefSetFunctions) Split(_ memmap.FileRange, val FrameRefSegInfo, _ uint64) (FrameRefSegInfo, FrameRefSegInfo) {
    60  	return val, val
    61  }
    62  
    63  // IncRefAndAccount adds a reference on the range fr. All newly inserted segments
    64  // are accounted as host page cache memory mappings. The new segments will be
    65  // associated with the memCgID, if the segment already exists then the memCgID
    66  // will not be changed.
    67  func (frSet *FrameRefSet) IncRefAndAccount(fr memmap.FileRange, memCgID uint32) {
    68  	seg, gap := frSet.Find(fr.Start)
    69  	for {
    70  		switch {
    71  		case seg.Ok() && seg.Start() < fr.End:
    72  			seg = frSet.Isolate(seg, fr)
    73  			seg.ValuePtr().refs++
    74  			seg, gap = seg.NextNonEmpty()
    75  		case gap.Ok() && gap.Start() < fr.End:
    76  			newRange := gap.Range().Intersect(fr)
    77  			usage.MemoryAccounting.Inc(newRange.Length(), usage.Mapped, memCgID)
    78  			frInfo := FrameRefSegInfo{refs: 1, memCgID: memCgID}
    79  			seg, gap = frSet.InsertWithoutMerging(gap, newRange, frInfo).NextNonEmpty()
    80  		default:
    81  			frSet.MergeAdjacent(fr)
    82  			return
    83  		}
    84  	}
    85  }
    86  
    87  // DecRefAndAccount removes a reference on the range fr and untracks segments
    88  // that are removed from memory accounting.
    89  func (frSet *FrameRefSet) DecRefAndAccount(fr memmap.FileRange) {
    90  	seg := frSet.FindSegment(fr.Start)
    91  
    92  	for seg.Ok() && seg.Start() < fr.End {
    93  		seg = frSet.Isolate(seg, fr)
    94  		if old := seg.ValuePtr().refs; old == 1 {
    95  			usage.MemoryAccounting.Dec(seg.Range().Length(), usage.Mapped, seg.ValuePtr().memCgID)
    96  			seg = frSet.Remove(seg).NextSegment()
    97  		} else {
    98  			seg.ValuePtr().refs--
    99  			seg = seg.NextSegment()
   100  		}
   101  	}
   102  	frSet.MergeAdjacent(fr)
   103  }