Documentation ¶
Overview ¶
Package shm implements sysv shared memory segments.
Known missing features:
- SHM_LOCK/SHM_UNLOCK are no-ops. The sentry currently doesn't implement memory locking in general.
- SHM_HUGETLB and related flags for shmget(2) are ignored. There's no easy way to implement hugetlb support on a per-map basis, and it has no impact on correctness.
- SHM_NORESERVE for shmget(2) is ignored, the sentry doesn't implement swap so it's meaningless to reserve space for swap.
- No per-process segment size enforcement. This feature probably isn't used much anyways, since Linux sets the per-process limits to the system-wide limits by default.
Lock ordering: mm.mappingMu -> shm registry lock -> shm lock
Index ¶
- Constants
- type AttachOpts
- type ID
- type Key
- type Registry
- func (r *Registry) FindByID(id ID) *Shm
- func (r *Registry) FindOrCreate(ctx context.Context, pid int32, key Key, size uint64, mode linux.FileMode, ...) (*Shm, error)
- func (r *Registry) IPCInfo() *linux.ShmParams
- func (r *Registry) Release(ctx context.Context)
- func (r *Registry) ShmInfo() *linux.ShmInfo
- func (r *Registry) StateFields() []string
- func (r *Registry) StateLoad(stateSourceObject state.Source)
- func (r *Registry) StateSave(stateSinkObject state.Sink)
- func (r *Registry) StateTypeName() string
- type Shm
- func (s *Shm) AddMapping(ctx context.Context, _ memmap.MappingSpace, _ hostarch.AddrRange, _ uint64, ...) error
- func (s *Shm) ConfigureAttach(ctx context.Context, addr hostarch.Addr, opts AttachOpts) (memmap.MMapOpts, error)
- func (*Shm) CopyMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, hostarch.AddrRange, ...) error
- func (s *Shm) DecRef(ctx context.Context)
- func (s *Shm) DeviceID() uint64
- func (s *Shm) EffectiveSize() uint64
- func (s *Shm) IPCStat(ctx context.Context) (*linux.ShmidDS, error)
- func (s *Shm) InodeID() uint64
- func (s *Shm) InvalidateUnsavable(ctx context.Context) error
- func (s *Shm) MappedName(ctx context.Context) string
- func (s *Shm) MarkDestroyed(ctx context.Context)
- func (s *Shm) Msync(context.Context, memmap.MappableRange) error
- func (s *Shm) RemoveMapping(ctx context.Context, _ memmap.MappingSpace, _ hostarch.AddrRange, _ uint64, ...)
- func (s *Shm) Set(ctx context.Context, ds *linux.ShmidDS) error
- func (s *Shm) StateFields() []string
- func (s *Shm) StateLoad(stateSourceObject state.Source)
- func (s *Shm) StateSave(stateSinkObject state.Sink)
- func (s *Shm) StateTypeName() string
- func (s *Shm) Translate(ctx context.Context, required, optional memmap.MappableRange, ...) ([]memmap.Translation, error)
- type ShmRefs
- func (r *ShmRefs) DecRef(destroy func())
- func (r *ShmRefs) IncRef()
- func (r *ShmRefs) InitRefs()
- func (r *ShmRefs) LeakMessage() string
- func (r *ShmRefs) LogRefs() bool
- func (r *ShmRefs) ReadRefs() int64
- func (r *ShmRefs) RefType() string
- func (r *ShmRefs) StateFields() []string
- func (r *ShmRefs) StateLoad(stateSourceObject state.Source)
- func (r *ShmRefs) StateSave(stateSinkObject state.Sink)
- func (r *ShmRefs) StateTypeName() string
- func (r *ShmRefs) TryIncRef() bool
Constants ¶
const ShmenableLogging = true
enableLogging indicates whether reference-related events should be logged (with stack traces). This is false by default and should only be set to true for debugging purposes, as it can generate an extremely large amount of output and drastically degrade performance.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AttachOpts ¶
AttachOpts describes various flags passed to shmat(2).
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry tracks all shared memory segments in an IPC namespace. The registry provides the mechanisms for creating and finding segments, and reporting global shm parameters.
+stateify savable
func NewRegistry ¶
func NewRegistry(userNS *auth.UserNamespace) *Registry
NewRegistry creates a new shm registry.
func (*Registry) FindByID ¶
FindByID looks up a segment given an ID.
FindByID returns a reference on Shm.
func (*Registry) FindOrCreate ¶
func (r *Registry) FindOrCreate(ctx context.Context, pid int32, key Key, size uint64, mode linux.FileMode, private, create, exclusive bool) (*Shm, error)
FindOrCreate looks up or creates a segment in the registry. It's functionally analogous to open(2).
FindOrCreate returns a reference on Shm.
func (*Registry) IPCInfo ¶
IPCInfo reports global parameters for sysv shared memory segments on this system. See shmctl(IPC_INFO).
func (*Registry) Release ¶
Release drops the self-reference of each active shm segment in the registry. It is called when the kernel.IPCNamespace containing r is being destroyed.
func (*Registry) ShmInfo ¶
ShmInfo reports linux-specific global parameters for sysv shared memory segments on this system. See shmctl(SHM_INFO).
func (*Registry) StateFields ¶
func (*Registry) StateTypeName ¶
type Shm ¶
type Shm struct { // ShmRefs tracks the number of references to this segment. // // A segment holds a reference to itself until it is marked for // destruction. // // In addition to direct users, the MemoryManager will hold references // via MappingIdentity. ShmRefs // ID is the kernel identifier for this segment. Immutable. ID ID // contains filtered or unexported fields }
Shm represents a single shared memory segment.
Shm segments are backed directly by an allocation from platform memory. Segments are always mapped as a whole, greatly simplifying how mappings are tracked. However note that mremap and munmap calls may cause the vma for a segment to become fragmented; which requires special care when unmapping a segment. See mm/shm.go.
Segments persist until they are explicitly marked for destruction via MarkDestroyed().
Shm implements memmap.Mappable and memmap.MappingIdentity.
+stateify savable
var Shmobj *Shm
obj is used to customize logging. Note that we use a pointer to T so that we do not copy the entire object when passed as a format parameter.
func (*Shm) AddMapping ¶
func (s *Shm) AddMapping(ctx context.Context, _ memmap.MappingSpace, _ hostarch.AddrRange, _ uint64, _ bool) error
AddMapping implements memmap.Mappable.AddMapping.
func (*Shm) ConfigureAttach ¶
func (s *Shm) ConfigureAttach(ctx context.Context, addr hostarch.Addr, opts AttachOpts) (memmap.MMapOpts, error)
ConfigureAttach creates an mmap configuration for the segment with the requested attach options.
Postconditions: The returned MMapOpts are valid only as long as a reference continues to be held on s.
func (*Shm) CopyMapping ¶
func (*Shm) CopyMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, hostarch.AddrRange, uint64, bool) error
CopyMapping implements memmap.Mappable.CopyMapping.
func (*Shm) EffectiveSize ¶
EffectiveSize returns the size of the underlying shared memory segment. This may be larger than the requested size at creation, due to rounding to page boundaries.
func (*Shm) InvalidateUnsavable ¶
InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
func (*Shm) MappedName ¶
MappedName implements memmap.MappingIdentity.MappedName.
func (*Shm) MarkDestroyed ¶
MarkDestroyed marks a segment for destruction. The segment is actually destroyed once it has no references. MarkDestroyed may be called multiple times, and is safe to call after a segment has already been destroyed. See shmctl(IPC_RMID).
func (*Shm) Msync ¶
Msync implements memmap.MappingIdentity.Msync. Msync is a no-op for shm segments.
func (*Shm) RemoveMapping ¶
func (s *Shm) RemoveMapping(ctx context.Context, _ memmap.MappingSpace, _ hostarch.AddrRange, _ uint64, _ bool)
RemoveMapping implements memmap.Mappable.RemoveMapping.
func (*Shm) StateFields ¶
func (*Shm) StateTypeName ¶
func (*Shm) Translate ¶
func (s *Shm) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error)
Translate implements memmap.Mappable.Translate.
type ShmRefs ¶
type ShmRefs struct {
// contains filtered or unexported fields
}
Refs implements refs.RefCounter. It keeps a reference count using atomic operations and calls the destructor when the count reaches zero.
+stateify savable
func (*ShmRefs) DecRef ¶
func (r *ShmRefs) DecRef(destroy func())
DecRef implements refs.RefCounter.DecRef.
Note that speculative references are counted here. Since they were added prior to real references reaching zero, they will successfully convert to real references. In other words, we see speculative references only in the following case:
A: TryIncRef [speculative increase => sees non-negative references] B: DecRef [real decrease] A: TryIncRef [transform speculative to real]
func (*ShmRefs) InitRefs ¶
func (r *ShmRefs) InitRefs()
InitRefs initializes r with one reference and, if enabled, activates leak checking.
func (*ShmRefs) LeakMessage ¶
LeakMessage implements refsvfs2.CheckedObject.LeakMessage.
func (*ShmRefs) ReadRefs ¶
ReadRefs returns the current number of references. The returned count is inherently racy and is unsafe to use without external synchronization.