shm

package
v0.0.0-...-ff2c174 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2021 License: Apache-2.0, MIT Imports: 17 Imported by: 0

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

View Source
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

type AttachOpts struct {
	Execute  bool
	Readonly bool
	Remap    bool
}

AttachOpts describes various flags passed to shmat(2).

type ID

type ID int32

ID represents the opaque handle for a shm segment. Analogous to an fd.

type Key

type Key int32

Key represents a shm segment key. Analogous to a file name.

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

func (r *Registry) FindByID(id ID) *Shm

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

func (r *Registry) IPCInfo() *linux.ShmParams

IPCInfo reports global parameters for sysv shared memory segments on this system. See shmctl(IPC_INFO).

func (*Registry) Release

func (r *Registry) Release(ctx context.Context)

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

func (r *Registry) ShmInfo() *linux.ShmInfo

ShmInfo reports linux-specific global parameters for sysv shared memory segments on this system. See shmctl(SHM_INFO).

func (*Registry) StateFields

func (r *Registry) StateFields() []string

func (*Registry) StateLoad

func (r *Registry) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*Registry) StateSave

func (r *Registry) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*Registry) StateTypeName

func (r *Registry) StateTypeName() string

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

CopyMapping implements memmap.Mappable.CopyMapping.

func (*Shm) DecRef

func (s *Shm) DecRef(ctx context.Context)

DecRef drops a reference on s.

Precondition: Caller must not hold s.mu.

func (*Shm) DeviceID

func (s *Shm) DeviceID() uint64

DeviceID implements memmap.MappingIdentity.DeviceID.

func (*Shm) EffectiveSize

func (s *Shm) EffectiveSize() uint64

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) IPCStat

func (s *Shm) IPCStat(ctx context.Context) (*linux.ShmidDS, error)

IPCStat returns information about a shm. See shmctl(IPC_STAT).

func (*Shm) InodeID

func (s *Shm) InodeID() uint64

InodeID implements memmap.MappingIdentity.InodeID.

func (*Shm) InvalidateUnsavable

func (s *Shm) InvalidateUnsavable(ctx context.Context) error

InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.

func (*Shm) MappedName

func (s *Shm) MappedName(ctx context.Context) string

MappedName implements memmap.MappingIdentity.MappedName.

func (*Shm) MarkDestroyed

func (s *Shm) MarkDestroyed(ctx context.Context)

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) Set

func (s *Shm) Set(ctx context.Context, ds *linux.ShmidDS) error

Set modifies attributes for a segment. See shmctl(IPC_SET).

func (*Shm) StateFields

func (s *Shm) StateFields() []string

func (*Shm) StateLoad

func (s *Shm) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*Shm) StateSave

func (s *Shm) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*Shm) StateTypeName

func (s *Shm) StateTypeName() string

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) IncRef

func (r *ShmRefs) IncRef()

IncRef implements refs.RefCounter.IncRef.

func (*ShmRefs) InitRefs

func (r *ShmRefs) InitRefs()

InitRefs initializes r with one reference and, if enabled, activates leak checking.

func (*ShmRefs) LeakMessage

func (r *ShmRefs) LeakMessage() string

LeakMessage implements refsvfs2.CheckedObject.LeakMessage.

func (*ShmRefs) LogRefs

func (r *ShmRefs) LogRefs() bool

LogRefs implements refsvfs2.CheckedObject.LogRefs.

func (*ShmRefs) ReadRefs

func (r *ShmRefs) ReadRefs() int64

ReadRefs returns the current number of references. The returned count is inherently racy and is unsafe to use without external synchronization.

func (*ShmRefs) RefType

func (r *ShmRefs) RefType() string

RefType implements refsvfs2.CheckedObject.RefType.

func (*ShmRefs) StateFields

func (r *ShmRefs) StateFields() []string

func (*ShmRefs) StateLoad

func (r *ShmRefs) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*ShmRefs) StateSave

func (r *ShmRefs) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*ShmRefs) StateTypeName

func (r *ShmRefs) StateTypeName() string

func (*ShmRefs) TryIncRef

func (r *ShmRefs) TryIncRef() bool

TryIncRef implements refs.RefCounter.TryIncRef.

To do this safely without a loop, a speculative reference is first acquired on the object. This allows multiple concurrent TryIncRef calls to distinguish other TryIncRef calls from genuine references held.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL