Go to main content

man pages section 2: System Calls

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

memcntl(2)

Name

memcntl - memory management control

Synopsis

#include <sys/types.h>
#include <sys/mman.h>

int memcntl(void *addr, size_t len, int cmd, void * arg,
     int attr, int mask);

Description

The memcntl() function allows the calling process to apply a variety of control operations over the address space identified by the mappings established for the address range [addr, addr + len).

The addr argument must be a multiple of the pagesize as returned by sysconf(3C). The scope of the control operations can be further defined with additional selection criteria (in the form of attributes) according to the bit pattern contained in attr.

The following attributes specify page mapping selection criteria:

SHARED

Page is mapped shared.

PRIVATE

Page is mapped private.

The following attributes specify page protection selection criteria. The selection criteria are constructed by a bitwise OR operation on the attribute bits and must match exactly.

PROT_READ

Page can be read.

PROT_WRITE

Page can be written.

PROT_EXEC

Page can be executed.

The following criteria may also be specified:

PROC_TEXT

Process text.

PROC_DATA

Process data.

The PROC_TEXT attribute specifies all privately mapped segments with read and execute permission, and the PROC_DATA attribute specifies all privately mapped segments with write permission.

Selection criteria can be used to describe various abstract memory objects within the address space on which to operate. If an operation shall not be constrained by the selection criteria, attr must have the value 0.

The operation to be performed is identified by the argument cmd. The symbolic names for the operations are defined in <sys/mman.h> as follows:

MC_LOCK

Lock in memory all pages in the range with attributes attr. A given page may be locked multiple times through different mappings; however, within a given mapping, page locks do not nest. Multiple lock operations on the same address in the same process will all be removed with a single unlock operation. A page locked in one process and mapped in another (or visible through a different mapping in the locking process) is locked in memory as long as the locking process does neither an implicit nor explicit unlock operation. If a locked mapping is removed, or a page is deleted through file removal or truncation, an unlock operation is implicitly performed. If a writable MAP_PRIVATE page in the address range is changed, the lock will be transferred to the private page.

The arg argument is not used, but must be 0 to ensure compatibility with potential future enhancements.

This command has no effect on segments created with shmget_osm(2).

MC_LOCKAS

Lock in memory all pages mapped by the address space with attributes attr. The addr and len arguments are not used, but must be NULL and 0 respectively, to ensure compatibility with potential future enhancements. The arg argument is a bit pattern built from the flags:

MCL_CURRENT

Lock current mappings.

MCL_FUTURE

Lock future mappings.

The value of arg determines whether the pages to be locked are those currently mapped by the address space, those that will be mapped in the future, or both. If MCL_FUTURE is specified, then all mappings subsequently added to the address space will be locked, provided sufficient memory is available.

This command has no effect on segments created with shmget_osm(2).

MC_LOCK_GRANULE

For each granule in the requested region which is not already locked, it allocates locked-down memory for that granule and enables access to the region by all processes attached to the shared memory segment. The newly allocated memory will be entirely zeroed. The arg, attr, and mask arguments must be NULL, 0, and 0, respectively, to ensure compatibility with potential future enhancements.

Since this operation allocates locked memory, it is subjected to the project.max-locked-memory and zone.max-locked-memory resource controls, and the overall availability of memory on the system.

The operation may be split into multiple parts; if this occurs, and a later part fails, any earlier parts which were not previously locked when the operation started will be unlocked before MC_LOCK_GRANULE returns.

MC_SYNC

Write to their backing storage locations all modified pages in the range with attributes attr. Optionally, invalidate cache copies. The backing storage for a modified MAP_SHARED mapping is the file the page is mapped to; the backing storage for a modified MAP_PRIVATE mapping is its swap area. The arg argument is a bit pattern built from the flags used to control the behavior of the operation:

MS_ASYNC

Perform asynchronous writes.

MS_SYNC

Perform synchronous writes.

MS_INVALIDATE

Invalidate mappings.

MS_ASYNC Return immediately once all write operations are scheduled; with MS_SYNC the function will not return until all write operations are completed.

MS_INVALIDATE Invalidate all cached copies of data in memory, so that further references to the pages will be obtained by the system from their backing storage locations. This operation should be used by applications that require a memory object to be in a known state.

This command has no effect on segments created with shmget_osm(2).

MC_UNLOCK

Unlock all pages in the range with attributes attr. The arg argument is not used, but must be 0 to ensure compatibility with potential future enhancements.

This command has no effect on segments created with shmget_osm(2).

MC_UNLOCKAS

Remove address space memory locks and locks on all pages in the address space with attributes attr. The addr, len, and arg arguments are not used, but must be NULL, 0 and 0, respectively, to ensure compatibility with potential future enhancements.

This command has no effect on segments created with shmget_osm(2).

MC_CORE_PRUNE_IN

Include the specified address range in the coredump of the process.

The addr and len arguments specify the address range which the user wants to have included in the coredump.

The arg, attr, and mask arguments must all be set to 0, when using this command.

MC_CORE_PRUNE_OUT

Exclude the specified address range from the coredump of the process.

The addr and len arguments specify the address range which the user wants to have included in the coredump.

The arg, attr, and mask arguments must all be set to 0, when using this command.

MC_CORE_UNPRUNE

Use the default coredump settings for the specified address range. The default coredump settings can be found by running coreadm command.

The addr and len arguments specify the address range which the user wants to use as default settings.

The arg, attr, and mask arguments must all be set to 0, when using this command.

MC_CORE_QUERY

Fill up the character array passed in the arg argument with the prune policies applied to the address range on a per page basis. Each entry in the array will have exactly one of the following values.

MCQ_DEFAULT

Indicates that the corresponding address range uses default settings.

MCQ_PRUNE_IN

Indicates that the corresponding address range will be included in the coredump of the process.

MCQ_PRUNE_OUT

Indicates that the corresponding address range will be excluded from the coredump of the process.

The caller needs to pass an allocated character array, whose length will be equal to the number of pages the caller is intend to query. It is solely the caller's responsibility to ensure enough memory has been allocated to accommodate all the requested pages.

The attr and mask arguments must be set to 0, when using this command.

MC_UNLOCK_GRANULE

For each granule in the requested region which is allocated and locked, access to the granule is disabled, the granule's allocated pages are unlocked, and the granule's memory is freed. The arg, attr, and mask arguments must be NULL, 0, and 0, respectively, to ensure compatibility with potential future enhancements.

If this operation covers more than a single segment, it may succeed on earlier segments before returning failure. If so, the earlier segments will be left unlocked, since their underlying data has been thrown away.

MC_RESERVE_AS

Reserve the Virtual Address range specified by addr and len. If successful, the reservation prevents the kernel from using the Reserved VA range for memory map operations that do not specify fixed addresses (e.g. library mappings).

The Reserved VA range(s) is 'reserved' for later use by memory mapping operations that specify an address range contained within the Reserved VA range starting at the addr argument to mmap or the shmaddr argument to shmat. The reservation can also be left in place to prevent any other usage of the VA range. For more information, see the mmap(2) and shmat(2) man pages.

The Reserved VA range can be anywhere in the address space that does not already have a mapping. This includes regions like the memory hole that may not be valid for actual memory maps.

A fixed address memory mapping operation can freely use the 'valid' portions of the Reserved VA space automatically. However, if the mapping range is larger than the Reserved VA range and include other mappings, the fixed address operation will fail. (mmap(2) with the MAP_FIXED flag set is the exception as it will indiscriminately unmap all mappings in its mapping range.) For guaranteed success, it is essential that the Reserved VA range is equal to or larger than the fixed address mapping range.

Please note that the mapfile Empty Segment directive is recommended for use to create the VA Reservation (For more information, see Oracle Solaris 11.4 Linkers and Libraries Guide for details). The MC_RESERVE_AS memcntl command issued from an application occurs after process startup which could mean that library mappings could have claimed the desired address range(s) needed for the Reservation.

When a program is compiled with the Mapfile Empty Segment directive, a VA Reservation is created starting at the specified Empty Segment Address for the specified Length.

Unlike MC_RESERVE_AS, the VA Reservation Empty Segment directive should always succeed as the Reservation occurs very early on in the process startup and the only potential conflict is with the program text and data and the link-editor would have caught the conflict during the link-edit. See ld(1) and the Oracle Solaris 11.4 Linkers and Libraries Guide for more details.

MC_UNRESERVE_AS

Unreserve any Reserved Virtual Address range(s) in the address range specified by addr and len.

If not unreserved via MC_UNRESERVE_AS, the Virtual Address Reservations are persistent and lasts until the process exits or executes a different program.

Mapping ranges that are freed (munmap(2) & shmdt(2)) that had previously mapped on top of a Reserved VA range are Re-Reserved.

MC_HAT_ADVISE

Advise system how a region of user-mapped memory will be accessed. The arg argument is interpreted as a “struct memcntl_mha *”. The following members are defined in a struct memcntl_mha:

uint_t mha_cmd;
uint_t mha_flags;
size_t mha_pagesize;

The accepted values for mha_cmd are:

MHA_MAPSIZE_VA
MHA_MAPSIZE_STACK
MHA_MAPSIZE_BSSBRK

The mha_flags member is reserved for future use and must always be set to 0. The mha_pagesize member must be a valid size as obtained from getpagesizes(3C) or the constant value 0 to allow the system to choose an appropriate hardware address translation mapping size.

MHA_MAPSIZE_VA sets the preferred hardware address translation mapping size of the region of memory from addr to addr + len. Both addr and len must be aligned to an mha_pagesize boundary. The entire virtual address region from addr to addr + len must not have any holes. Permissions within each mha_pagesize–aligned portion of the region must be consistent. When a size of 0 is specified, the system selects an appropriate size based on the size and alignment of the memory region, type of processor, and other considerations.

MHA_MAPSIZE_STACK sets the preferred hardware address translation mapping size of the process main thread stack segment. The addr and len arguments must be NULL and 0, respectively.

MHA_MAPSIZE_BSSBRK sets the preferred hardware address translation mapping size of the process heap. The addr and len arguments must be NULL and 0, respectively. See the NOTES section of the ppgsz(1) manual page for additional information on process heap alignment.

The attr argument must be 0 for all MC_HAT_ADVISE operations.

MC_ENABLE_ADI

Available on platforms that support Application Data Integrity. Enable ADI on all pages in the range.

The arg, attr, and mask arguments must be NULL, 0, and 0 respectively, to ensure compatibility with potential future enhancements.

ADI may be enabled on anonymous memory that is mapped PRIVATE or SHARED and all forms of shared memory attached via shmat(2) except for DISM.

ADI may only be enabled (or disabled) over the entire range of an ISM segment.

For a memory segment created with shmget_osm(2), ADI may only be enabled (or disabled) over a range that starts and ends at a multiple of the segment's granule size.

When ADI is initially enabled on a region of memory, the ADI versions for the region are undefined until they are explicitly set by the application.

MC_DISABLE_ADI

Available on platforms that support Application Data Integrity. Disable ADI on all pages in the range.

The arg, attr, and mask arguments must be NULL, 0, and 0 respectively, to ensure compatibility with potential future enhancements.

The mask argument must be 0; it is reserved for future use.

Locks established with the lock operations are not inherited by a child process after fork(2). The memcntl() function fails if it attempts to lock more memory than a system-specific limit.

Due to the potential impact on system resources, the operations MC_LOCKAS, MC_LOCK, MC_UNLOCKAS, and MC_UNLOCK are restricted to privileged processes.

Requests from MC_CORE_PRUNE_IN and MC_CORE_PRUNE_OUT will be honored only if the core file content specified by coreadm includes the prune directive. This directive is included in the default set of content descriptors in coreadm command.

Usage

The memcntl() function subsumes the operations of plock(3C).

MC_HAT_ADVISE is intended to improve performance of applications that use large amounts of memory on processors that support multiple hardware address translation mapping sizes; however, it should be used with care. Not all processors support all sizes with equal efficiency. Use of larger sizes may also introduce extra overhead that could reduce performance or available memory. Using large sizes for one application may reduce available resources for other applications and result in slower system wide performance.

Return Values

Upon successful completion, memcntl() returns 0; otherwise, it returns −1 and sets errno to indicate an error.

Errors

The memcntl() function will fail if:

EACCES

MC_LOCK_GRANULE or MC_UNLOCK_GRANULE was specified, and the requested range covered a non-writable memory segment.

EAGAIN

When the selection criteria match, some or all of the memory identified by the operation could not be locked when MC_LOCK or MC_LOCKAS was specified, some or all mappings in the address range [addr, addr + len) are locked for I/O when MC_HAT_ADVISE was specified, or the system has insufficient resources when MC_HAT_ADVISE was specified.

The cmd is MC_LOCK or MC_LOCKAS and locking the memory identified by this operation would exceed a limit or resource control on locked memory.

The cmd is MC_ENABLE_ADI and the memory identified by this operation would exceed a limit or resource control on ADI memory or the total amount of system memory is temporarily insufficient to allocate ADI metadata.

The cmd is MC_LOCK_GRANULE and the memory identified by this operation would exceed a limit or resource control.

EBUSY

When the selection criteria match, some or all of the addresses in the range [addr, addr + len) are locked and MC_SYNC with the MS_INVALIDATE option was specified.

EINVAL

The addr argument specifies invalid selection criteria or is not a multiple of the page size as returned by sysconf(3C).

The addr and/or len argument does not have the value 0 when MC_LOCKAS or MC_UNLOCKAS is specified.

The arg argument is not valid for the function specified.

The mha_pagesize or mha_cmd member is invalid.

MC_HAT_ADVISE is specified and not all pages in the specified region have the same access permissions within the given size boundaries.

MC_HAT_ADVISE is specified for a region of shared memory either created with shmget_osm(2) or attached with the SHM_SHARE_MMU or SHM_PAGEABLE attribute.

A prune request was made for an address range, parts of which are not backed by actual segments in the address range.

MC_LOCK_GRANULE or MC_UNLOCK_GRANULE was specified, and either arg was non-NULL, attr was non-zero, mask was non-zero, or the addr or len arguments were not multiples of the segment's granule size.

MC_ENABLE_ADI or MC_DISABLE_ADI is specified for a memory segment which was created with shmget_osm(2), and the range start or ends at a non-granule multiple.

MC_ENABLE_ADI or MC_DISABLE_ADI is specified for a region of ISM and the addr and/or len argument do not cover the entire segment.

MC_ENABLE_ADI or MC_DISABLE_ADI is specified and either arg was non-NULL, attr was non-zero, or mask was non-zero.

ENOMEM

When the selection criteria match, some or all of the addresses in the range [addr, addr + len) are invalid for the address space of a process or specify one or more pages which are not mapped.

MC_LOCK_GRANULE was specified, and there was not enough available memory in the system for the request.

ENOSYS

MC_LOCK_GRANULE or MC_UNLOCK_GRANULE was specified, and the requested range covered a memory segment which was not created with shmget_osm(2).

MC_ENABLE_ADI or MC_DISABLE_ADI was specified, and the requested range covered a memory segment that does not support ADI.

ENOTSUP

MC_ENABLE_ADI or MC_DISABLE_ADI was specified, and the ADI feature is not supported by the platform.

MC_ENABLE_ADI or MC_DISABLE_ADI was specified, and the caller is a 32-bit process.

EPERM

The {PRIV_PROC_LOCK_MEMORY} privilege is not asserted in the effective set of the calling process and MC_LOCK, MC_LOCKAS, MC_UNLOCK, or MC_UNLOCKAS was specified.

EFAULT

The arg argument points to an illegal address.

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
MT-Level
MT-Safe

See Also

ld(1), ppgsz(1), fork(2), mmap(2), mprotect(2), shmget_osm(2), getpagesizes(3C), mlock(3C), mlockall(3C), msync(3C), plock(3C), sysconf(3C), attributes(7), privileges(7)

Oracle Solaris 11.4 Linkers and Libraries Guide