1.\" 2.\" Copyright 2000 Massachusetts Institute of Technology 3.\" 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 14.\" warranty. 15.\" 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 27.\" SUCH DAMAGE. 28.\" 29.Dd August 4, 2025 30.Dt SHM_OPEN 2 31.Os 32.Sh NAME 33.Nm memfd_create , shm_create_largepage , shm_open , shm_rename, shm_unlink 34.Nd "shared memory object operations" 35.Sh LIBRARY 36.Lb libc 37.Sh SYNOPSIS 38.In sys/types.h 39.In sys/mman.h 40.In fcntl.h 41.Ft int 42.Fn memfd_create "const char *name" "unsigned int flags" 43.Ft int 44.Fo shm_create_largepage 45.Fa "const char *path" 46.Fa "int flags" 47.Fa "int psind" 48.Fa "int alloc_policy" 49.Fa "mode_t mode" 50.Fc 51.Ft int 52.Fn shm_open "const char *path" "int flags" "mode_t mode" 53.Ft int 54.Fn shm_rename "const char *path_from" "const char *path_to" "int flags" 55.Ft int 56.Fn shm_unlink "const char *path" 57.Sh DESCRIPTION 58The 59.Fn shm_open 60function opens (or optionally creates) a 61POSIX 62shared memory object named 63.Fa path . 64The 65.Fa flags 66argument contains a subset of the flags used by 67.Xr open 2 . 68An access mode of either 69.Dv O_RDONLY 70or 71.Dv O_RDWR 72must be included in 73.Fa flags . 74The optional flags 75.Dv O_CREAT , 76.Dv O_EXCL , 77.Dv O_TRUNC , 78and 79.Dv O_CLOFORK 80may also be specified. 81.Pp 82If 83.Dv O_CREAT 84is specified, 85then a new shared memory object named 86.Fa path 87will be created if it does not exist. 88In this case, 89the shared memory object is created with mode 90.Fa mode 91subject to the process' umask value. 92If both the 93.Dv O_CREAT 94and 95.Dv O_EXCL 96flags are specified and a shared memory object named 97.Fa path 98already exists, 99then 100.Fn shm_open 101will fail with 102.Er EEXIST . 103.Pp 104Newly created objects start off with a size of zero. 105If an existing shared memory object is opened with 106.Dv O_RDWR 107and the 108.Dv O_TRUNC 109flag is specified, 110then the shared memory object will be truncated to a size of zero. 111The size of the object can be adjusted via 112.Xr ftruncate 2 113and queried via 114.Xr fstat 2 . 115.Pp 116The new descriptor is set to close during 117.Xr execve 2 118system calls; 119see 120.Xr close 2 121and 122.Xr fcntl 2 . 123.Pp 124The constant 125.Dv SHM_ANON 126may be used for the 127.Fa path 128argument to 129.Fn shm_open . 130In this case, an anonymous, unnamed shared memory object is created. 131Since the object has no name, 132it cannot be removed via a subsequent call to 133.Fn shm_unlink , 134or moved with a call to 135.Fn shm_rename . 136Instead, 137the shared memory object will be garbage collected when the last reference to 138the shared memory object is removed. 139The shared memory object may be shared with other processes by sharing the 140file descriptor via 141.Xr fork 2 142or 143.Xr sendmsg 2 . 144Attempting to open an anonymous shared memory object with 145.Dv O_RDONLY 146will fail with 147.Er EINVAL . 148All other flags are ignored. 149.Pp 150The 151.Fn shm_create_largepage 152function behaves similarly to 153.Fn shm_open , 154except that the 155.Dv O_CREAT 156flag is implicitly specified, and the returned 157.Dq largepage 158object is always backed by aligned, physically contiguous chunks of memory. 159This ensures that the object can be mapped using so-called 160.Dq superpages , 161which can improve application performance in some workloads by reducing the 162number of translation lookaside buffer (TLB) entries required to access a 163mapping of the object, 164and by reducing the number of page faults performed when accessing a mapping. 165This happens automatically for all largepage objects. 166.Pp 167An existing largepage object can be opened using the 168.Fn shm_open 169function. 170Largepage shared memory objects behave slightly differently from non-largepage 171objects: 172.Bl -bullet -offset indent 173.It 174Memory for a largepage object is allocated when the object is 175extended using the 176.Xr ftruncate 2 177system call, whereas memory for regular shared memory objects is allocated 178lazily and may be paged out to a swap device when not in use. 179.It 180The size of a mapping of a largepage object must be a multiple of the 181underlying large page size. 182Most attributes of such a mapping can only be modified at the granularity 183of the large page size. 184For example, when using 185.Xr munmap 2 186to unmap a portion of a largepage object mapping, or when using 187.Xr mprotect 2 188to adjust protections of a mapping of a largepage object, the starting address 189must be large page size-aligned, and the length of the operation must be a 190multiple of the large page size. 191If not, the corresponding system call will fail and set 192.Va errno 193to 194.Er EINVAL . 195.El 196.Pp 197The 198.Fa psind 199argument to 200.Fn shm_create_largepage 201specifies the size of large pages used to back the object. 202This argument is an index into the page sizes array returned by 203.Xr getpagesizes 3 . 204In particular, all large pages backing a largepage object must be of the 205same size. 206For example, on a system with large page sizes of 2MB and 1GB, a 2GB largepage 207object will consist of either 1024 2MB pages, or 2 1GB pages, depending on 208the value specified for the 209.Fa psind 210argument. 211The 212.Fa alloc_policy 213parameter specifies what happens when an attempt to use 214.Xr ftruncate 2 215to allocate memory for the object fails. 216The following values are accepted: 217.Bl -tag -offset indent -width SHM_ 218.It Dv SHM_LARGEPAGE_ALLOC_DEFAULT 219If the (non-blocking) memory allocation fails because there is insufficient free 220contiguous memory, the kernel will attempt to defragment physical memory and 221try another allocation. 222The subsequent allocation may or may not succeed. 223If this subsequent allocation also fails, 224.Xr ftruncate 2 225will fail and set 226.Va errno 227to 228.Er ENOMEM . 229.It Dv SHM_LARGEPAGE_ALLOC_NOWAIT 230If the memory allocation fails, 231.Xr ftruncate 2 232will fail and set 233.Va errno 234to 235.Er ENOMEM . 236.It Dv SHM_LARGEPAGE_ALLOC_HARD 237The kernel will attempt defragmentation until the allocation succeeds, 238or an unblocked signal is delivered to the thread. 239However, it is possible for physical memory to be fragmented such that the 240allocation will never succeed. 241.El 242.Pp 243The 244.Dv FIOSSHMLPGCNF 245and 246.Dv FIOGSHMLPGCNF 247.Xr ioctl 2 248commands can be used with a largepage shared memory object to get and set 249largepage object parameters. 250Both commands operate on the following structure: 251.Bd -literal 252struct shm_largepage_conf { 253 int psind; 254 int alloc_policy; 255}; 256 257.Ed 258The 259.Dv FIOGSHMLPGCNF 260command populates this structure with the current values of these parameters, 261while the 262.Dv FIOSSHMLPGCNF 263command modifies the largepage object. 264Currently only the 265.Va alloc_policy 266parameter may be modified. 267Internally, 268.Fn shm_create_largepage 269works by creating a regular shared memory object using 270.Fn shm_open , 271and then converting it into a largepage object using the 272.Dv FIOSSHMLPGCNF 273ioctl command. 274.Pp 275The 276.Fn shm_rename 277system call atomically removes a shared memory object named 278.Fa path_from 279and relinks it at 280.Fa path_to . 281If another object is already linked at 282.Fa path_to , 283that object will be unlinked, unless one of the following flags are provided: 284.Bl -tag -offset indent -width Er 285.It Er SHM_RENAME_EXCHANGE 286Atomically exchange the shms at 287.Fa path_from 288and 289.Fa path_to . 290.It Er SHM_RENAME_NOREPLACE 291Return an error if an shm exists at 292.Fa path_to , 293rather than unlinking it. 294.El 295.Pp 296The 297.Fn shm_unlink 298system call removes a shared memory object named 299.Fa path . 300.Pp 301The 302.Fn memfd_create 303function creates an anonymous shared memory object, identical to that created 304by 305.Fn shm_open 306when 307.Dv SHM_ANON 308is specified. 309Newly created objects start off with a size of zero. 310The size of the new object must be adjusted via 311.Xr ftruncate 2 . 312.Pp 313The 314.Fa name 315argument must not be 316.Dv NULL , 317but it may be an empty string. 318The length of the 319.Fa name 320argument may not exceed 321.Dv NAME_MAX 322minus six characters for the prefix 323.Dq memfd: , 324which will be prepended. 325The 326.Fa name 327argument is intended solely for debugging purposes and will never be used by the 328kernel to identify a memfd. 329Names are therefore not required to be unique. 330.Pp 331The following 332.Fa flags 333may be specified to 334.Fn memfd_create : 335.Bl -tag -width MFD_ALLOW_SEALING 336.It Dv MFD_CLOEXEC 337Set 338.Dv FD_CLOEXEC 339on the resulting file descriptor. 340.It Dv MFD_ALLOW_SEALING 341Allow adding seals to the resulting file descriptor using the 342.Dv F_ADD_SEALS 343.Xr fcntl 2 344command. 345.It Dv MFD_HUGETLB 346This flag is currently unsupported. 347.El 348.Sh RETURN VALUES 349If successful, 350.Fn memfd_create 351and 352.Fn shm_open 353both return a non-negative integer, 354and 355.Fn shm_rename 356and 357.Fn shm_unlink 358return zero. 359All functions return -1 on failure, and set 360.Va errno 361to indicate the error. 362.Sh COMPATIBILITY 363The 364.Fn shm_create_largepage 365and 366.Fn shm_rename 367functions are 368.Fx 369extensions, as is support for the 370.Dv SHM_ANON 371value in 372.Fn shm_open . 373.Pp 374The 375.Fa path , 376.Fa path_from , 377and 378.Fa path_to 379arguments do not necessarily represent a pathname (although they do in 380most other implementations). 381Two processes opening the same 382.Fa path 383are guaranteed to access the same shared memory object if and only if 384.Fa path 385begins with a slash 386.Pq Ql \&/ 387character. 388.Pp 389Only the 390.Dv O_RDONLY , 391.Dv O_RDWR , 392.Dv O_CREAT , 393.Dv O_EXCL , 394and 395.Dv O_TRUNC 396flags may be used in portable programs. 397.Pp 398POSIX 399specifications state that the result of using 400.Xr open 2 , 401.Xr read 2 , 402or 403.Xr write 2 404on a shared memory object, or on the descriptor returned by 405.Fn shm_open , 406is undefined. 407However, the 408.Fx 409kernel implementation explicitly includes support for 410.Xr read 2 411and 412.Xr write 2 . 413.Pp 414.Fx 415also supports zero-copy transmission of data from shared memory 416objects with 417.Xr sendfile 2 . 418.Pp 419Neither shared memory objects nor their contents persist across reboots. 420.Pp 421Writes do not extend shared memory objects, so 422.Xr ftruncate 2 423must be called before any data can be written. 424See 425.Sx EXAMPLES . 426.Sh EXAMPLES 427This example fails without the call to 428.Xr ftruncate 2 : 429.Bd -literal -compact 430 431 uint8_t buffer[getpagesize()]; 432 ssize_t len; 433 int fd; 434 435 fd = shm_open(SHM_ANON, O_RDWR | O_CREAT, 0600); 436 if (fd < 0) 437 err(EX_OSERR, "%s: shm_open", __func__); 438 if (ftruncate(fd, getpagesize()) < 0) 439 err(EX_IOERR, "%s: ftruncate", __func__); 440 len = pwrite(fd, buffer, getpagesize(), 0); 441 if (len < 0) 442 err(EX_IOERR, "%s: pwrite", __func__); 443 if (len != getpagesize()) 444 errx(EX_IOERR, "%s: pwrite length mismatch", __func__); 445.Ed 446.Sh ERRORS 447.Fn memfd_create 448fails with these error codes for these conditions: 449.Bl -tag -width Er 450.It Bq Er EBADF 451The 452.Fa name 453argument was NULL. 454.It Bq Er EINVAL 455The 456.Fa name 457argument was too long. 458.Pp 459An invalid or unsupported flag was included in 460.Fa flags . 461.It Bq Er EMFILE 462The process has already reached its limit for open file descriptors. 463.It Bq Er ENFILE 464The system file table is full. 465.It Bq Er ENOSYS 466In 467.Fa memfd_create , 468.Dv MFD_HUGETLB 469was specified in 470.Fa flags , 471and this system does not support forced hugetlb mappings. 472.El 473.Pp 474.Fn shm_open 475fails with these error codes for these conditions: 476.Bl -tag -width Er 477.It Bq Er EINVAL 478A flag other than 479.Dv O_RDONLY , 480.Dv O_RDWR , 481.Dv O_CREAT , 482.Dv O_EXCL , 483or 484.Dv O_TRUNC 485was included in 486.Fa flags . 487.It Bq Er EMFILE 488The process has already reached its limit for open file descriptors. 489.It Bq Er ENFILE 490The system file table is full. 491.It Bq Er EINVAL 492.Dv O_RDONLY 493was specified while creating an anonymous shared memory object via 494.Dv SHM_ANON . 495.It Bq Er EFAULT 496The 497.Fa path 498argument points outside the process' allocated address space. 499.It Bq Er ENAMETOOLONG 500The entire pathname exceeds 1023 characters. 501.It Bq Er EINVAL 502The 503.Fa path 504does not begin with a slash 505.Pq Ql \&/ 506character. 507.It Bq Er ENOENT 508.Dv O_CREAT 509is not specified and the named shared memory object does not exist. 510.It Bq Er EEXIST 511.Dv O_CREAT 512and 513.Dv O_EXCL 514are specified and the named shared memory object does exist. 515.It Bq Er EACCES 516The required permissions (for reading or reading and writing) are denied. 517.It Bq Er ECAPMODE 518The process is running in capability mode (see 519.Xr capsicum 4 ) 520and attempted to create a named shared memory object. 521.El 522.Pp 523.Fn shm_create_largepage 524can fail for the reasons listed above. 525It also fails with these error codes for the following conditions: 526.Bl -tag -width Er 527.It Bq Er ENOTTY 528The kernel does not support large pages on the current platform. 529.El 530.Pp 531The following errors are defined for 532.Fn shm_rename : 533.Bl -tag -width Er 534.It Bq Er EFAULT 535The 536.Fa path_from 537or 538.Fa path_to 539argument points outside the process' allocated address space. 540.It Bq Er ENAMETOOLONG 541The entire pathname exceeds 1023 characters. 542.It Bq Er ENOENT 543The shared memory object at 544.Fa path_from 545does not exist. 546.It Bq Er EACCES 547The required permissions are denied. 548.It Bq Er EEXIST 549An shm exists at 550.Fa path_to , 551and the 552.Dv SHM_RENAME_NOREPLACE 553flag was provided. 554.El 555.Pp 556.Fn shm_unlink 557fails with these error codes for these conditions: 558.Bl -tag -width Er 559.It Bq Er EFAULT 560The 561.Fa path 562argument points outside the process' allocated address space. 563.It Bq Er ENAMETOOLONG 564The entire pathname exceeds 1023 characters. 565.It Bq Er ENOENT 566The named shared memory object does not exist. 567.It Bq Er EACCES 568The required permissions are denied. 569.Fn shm_unlink 570requires write permission to the shared memory object. 571.El 572.Sh SEE ALSO 573.Xr posixshmcontrol 1 , 574.Xr close 2 , 575.Xr fstat 2 , 576.Xr ftruncate 2 , 577.Xr ioctl 2 , 578.Xr mmap 2 , 579.Xr munmap 2 , 580.Xr sendfile 2 581.Sh STANDARDS 582The 583.Fn memfd_create 584function is expected to be compatible with the Linux system call of the same 585name. 586.Pp 587The 588.Fn shm_open 589and 590.Fn shm_unlink 591functions are believed to conform to 592.St -p1003.1b-93 . 593.Sh HISTORY 594The 595.Fn memfd_create 596function appeared in 597.Fx 13.0 . 598.Pp 599The 600.Fn shm_open 601and 602.Fn shm_unlink 603functions first appeared in 604.Fx 4.3 . 605The functions were reimplemented as system calls using shared memory objects 606directly rather than files in 607.Fx 8.0 . 608.Pp 609.Fn shm_rename 610first appeared in 611.Fx 13.0 612as a 613.Fx 614extension. 615.Sh AUTHORS 616.An Garrett A. Wollman Aq Mt wollman@FreeBSD.org 617(C library support and this manual page) 618.Pp 619.An Matthew Dillon Aq Mt dillon@FreeBSD.org 620.Pq Dv MAP_NOSYNC 621.Pp 622.An Matthew Bryan Aq Mt matthew.bryan@isilon.com 623.Pq Dv shm_rename implementation 624