2 .\" Copyright 2000 Massachusetts Institute of Technology
4 .\" Permission to use, copy, modify, and distribute this software and
5 .\" its documentation for any purpose and without fee is hereby
6 .\" granted, provided that both the above copyright notice and this
7 .\" permission notice appear in all copies, that both the above
8 .\" copyright notice and this permission notice appear in all
9 .\" supporting documentation, and that the name of M.I.T. not be used
10 .\" in advertising or publicity pertaining to distribution of the
11 .\" software without specific, written prior permission. M.I.T. makes
12 .\" no representations about the suitability of this software for any
13 .\" purpose. It is provided "as is" without express or implied
16 .\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 .\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 .\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 .\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 .\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 .\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .Nm memfd_create , shm_create_largepage , shm_open , shm_rename, shm_unlink
36 .Nd "shared memory object operations"
44 .Fn memfd_create "const char *name" "unsigned int flags"
46 .Fo shm_create_largepage
47 .Fa "const char *path"
50 .Fa "int alloc_policy"
54 .Fn shm_open "const char *path" "int flags" "mode_t mode"
56 .Fn shm_rename "const char *path_from" "const char *path_to" "int flags"
58 .Fn shm_unlink "const char *path"
62 function opens (or optionally creates) a
64 shared memory object named
68 argument contains a subset of the flags used by
70 An access mode of either
81 may also be specified.
86 then a new shared memory object named
88 will be created if it does not exist.
90 the shared memory object is created with mode
92 subject to the process' umask value.
97 flags are specified and a shared memory object named
105 Newly created objects start off with a size of zero.
106 If an existing shared memory object is opened with
111 then the shared memory object will be truncated to a size of zero.
112 The size of the object can be adjusted via
117 The new descriptor is set to close during
131 In this case, an anonymous, unnamed shared memory object is created.
132 Since the object has no name,
133 it cannot be removed via a subsequent call to
135 or moved with a call to
138 the shared memory object will be garbage collected when the last reference to
139 the shared memory object is removed.
140 The shared memory object may be shared with other processes by sharing the
145 Attempting to open an anonymous shared memory object with
149 All other flags are ignored.
152 .Fn shm_create_largepage
153 function behaves similarly to
157 flag is implicitly specified, and the returned
159 object is always backed by aligned, physically contiguous chunks of memory.
160 This ensures that the object can be mapped using so-called
162 which can improve application performance in some workloads by reducing the
163 number of translation lookaside buffer (TLB) entries required to access a
164 mapping of the object,
165 and by reducing the number of page faults performed when accessing a mapping.
166 This happens automatically for all largepage objects.
168 An existing largepage object can be opened using the
171 Largepage shared memory objects behave slightly differently from non-largepage
173 .Bl -bullet -offset indent
175 Memory for a largepage object is allocated when the object is
178 system call, whereas memory for regular shared memory objects is allocated
179 lazily and may be paged out to a swap device when not in use.
181 The size of a mapping of a largepage object must be a multiple of the
182 underlying large page size.
183 Most attributes of such a mapping can only be modified at the granularity
184 of the large page size.
185 For example, when using
187 to unmap a portion of a largepage object mapping, or when using
189 to adjust protections of a mapping of a largepage object, the starting address
190 must be large page size-aligned, and the length of the operation must be a
191 multiple of the large page size.
192 If not, the corresponding system call will fail and set
201 .Fn shm_create_largepage
202 specifies the size of large pages used to back the object.
203 This argument is an index into the page sizes array returned by
205 In particular, all large pages backing a largepage object must be of the
207 For example, on a system with large page sizes of 2MB and 1GB, a 2GB largepage
208 object will consist of either 1024 2MB pages, or 2 1GB pages, depending on
209 the value specified for the
214 parameter specifies what happens when an attempt to use
216 to allocate memory for the object fails.
217 The following values are accepted:
218 .Bl -tag -offset indent -width SHM_
219 .It Dv SHM_LARGEPAGE_ALLOC_DEFAULT
220 If the (non-blocking) memory allocation fails because there is insufficient free
221 contiguous memory, the kernel will attempt to defragment physical memory and
222 try another allocation.
223 The subsequent allocation may or may not succeed.
224 If this subsequent allocation also fails,
230 .It Dv SHM_LARGEPAGE_ALLOC_NOWAIT
231 If the memory allocation fails,
237 .It Dv SHM_LARGEPAGE_ALLOC_HARD
238 The kernel will attempt defragmentation until the allocation succeeds,
239 or an unblocked signal is delivered to the thread.
240 However, it is possible for physical memory to be fragmented such that the
241 allocation will never succeed.
249 commands can be used with a largepage shared memory object to get and set
250 largepage object parameters.
251 Both commands operate on the following structure:
253 struct shm_largepage_conf {
261 command populates this structure with the current values of these parameters,
264 command modifies the largepage object.
267 parameter may be modified.
269 .Fn shm_create_largepage
270 works by creating a regular shared memory object using
272 and then converting it into a largepage object using the
278 system call atomically removes a shared memory object named
282 If another object is already linked at
284 that object will be unlinked, unless one of the following flags are provided:
285 .Bl -tag -offset indent -width Er
286 .It Er SHM_RENAME_EXCHANGE
287 Atomically exchange the shms at
291 .It Er SHM_RENAME_NOREPLACE
292 Return an error if an shm exists at
294 rather than unlinking it.
299 system call removes a shared memory object named
304 function creates an anonymous shared memory object, identical to that created
310 Newly created objects start off with a size of zero.
311 The size of the new object must be adjusted via
318 but it may be an empty string.
321 argument may not exceed
323 minus six characters for the prefix
325 which will be prepended.
328 argument is intended solely for debugging purposes and will never be used by the
329 kernel to identify a memfd.
330 Names are therefore not required to be unique.
336 .Bl -tag -width MFD_ALLOW_SEALING
340 on the resulting file descriptor.
341 .It Dv MFD_ALLOW_SEALING
342 Allow adding seals to the resulting file descriptor using the
347 This flag is currently unsupported.
354 both return a non-negative integer,
360 All functions return -1 on failure, and set
362 to indicate the error.
365 .Fn shm_create_largepage
370 extensions, as is support for the
380 arguments do not necessarily represent a pathname (although they do in
381 most other implementations).
382 Two processes opening the same
384 are guaranteed to access the same shared memory object if and only if
397 flags may be used in portable programs.
400 specifications state that the result of using
405 on a shared memory object, or on the descriptor returned by
410 kernel implementation explicitly includes support for
416 also supports zero-copy transmission of data from shared memory
420 Neither shared memory objects nor their contents persist across reboots.
422 Writes do not extend shared memory objects, so
424 must be called before any data can be written.
428 This example fails without the call to
430 .Bd -literal -compact
432 uint8_t buffer[getpagesize()];
436 fd = shm_open(SHM_ANON, O_RDWR | O_CREAT, 0600);
438 err(EX_OSERR, "%s: shm_open", __func__);
439 if (ftruncate(fd, getpagesize()) < 0)
440 err(EX_IOERR, "%s: ftruncate", __func__);
441 len = pwrite(fd, buffer, getpagesize(), 0);
443 err(EX_IOERR, "%s: pwrite", __func__);
444 if (len != getpagesize())
445 errx(EX_IOERR, "%s: pwrite length mismatch", __func__);
449 fails with these error codes for these conditions:
458 argument was too long.
460 An invalid or unsupported flag was included in
463 The process has already reached its limit for open file descriptors.
465 The system file table is full.
472 and this system does not support forced hugetlb mappings.
476 fails with these error codes for these conditions:
489 The process has already reached its limit for open file descriptors.
491 The system file table is full.
494 was specified while creating an anonymous shared memory object via
499 argument points outside the process' allocated address space.
500 .It Bq Er ENAMETOOLONG
501 The entire pathname exceeds 1023 characters.
505 does not begin with a slash
510 is not specified and the named shared memory object does not exist.
515 are specified and the named shared memory object does exist.
517 The required permissions (for reading or reading and writing) are denied.
519 The process is running in capability mode (see
521 and attempted to create a named shared memory object.
524 .Fn shm_create_largepage
525 can fail for the reasons listed above.
526 It also fails with these error codes for the following conditions:
529 The kernel does not support large pages on the current platform.
532 The following errors are defined for
540 argument points outside the process' allocated address space.
541 .It Bq Er ENAMETOOLONG
542 The entire pathname exceeds 1023 characters.
544 The shared memory object at
548 The required permissions are denied.
553 .Dv SHM_RENAME_NOREPLACE
558 fails with these error codes for these conditions:
563 argument points outside the process' allocated address space.
564 .It Bq Er ENAMETOOLONG
565 The entire pathname exceeds 1023 characters.
567 The named shared memory object does not exist.
569 The required permissions are denied.
571 requires write permission to the shared memory object.
574 .Xr posixshmcontrol 1 ,
585 function is expected to be compatible with the Linux system call of the same
592 functions are believed to conform to
604 functions first appeared in
606 The functions were reimplemented as system calls using shared memory objects
607 directly rather than files in
617 .An Garrett A. Wollman Aq Mt wollman@FreeBSD.org
618 (C library support and this manual page)
620 .An Matthew Dillon Aq Mt dillon@FreeBSD.org
623 .An Matthew Bryan Aq Mt matthew.bryan@isilon.com
624 .Pq Dv shm_rename implementation