1MEMFD_CREATE(2) Linux Programmer's Manual MEMFD_CREATE(2)
2
3
4
6 memfd_create - create an anonymous file
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <sys/mman.h>
11
12 int memfd_create(const char *name, unsigned int flags);
13
15 memfd_create() creates an anonymous file and returns a file descriptor
16 that refers to it. The file behaves like a regular file, and so can be
17 modified, truncated, memory-mapped, and so on. However, unlike a regu‐
18 lar file, it lives in RAM and has a volatile backing storage. Once all
19 references to the file are dropped, it is automatically released.
20 Anonymous memory is used for all backing pages of the file. Therefore,
21 files created by memfd_create() have the same semantics as other anony‐
22 mous memory allocations such as those allocated using mmap(2) with the
23 MAP_ANONYMOUS flag.
24
25 The initial size of the file is set to 0. Following the call, the file
26 size should be set using ftruncate(2). (Alternatively, the file may be
27 populated by calls to write(2) or similar.)
28
29 The name supplied in name is used as a filename and will be displayed
30 as the target of the corresponding symbolic link in the directory
31 /proc/self/fd/. The displayed name is always prefixed with memfd: and
32 serves only for debugging purposes. Names do not affect the behavior
33 of the file descriptor, and as such multiple files can have the same
34 name without any side effects.
35
36 The following values may be bitwise ORed in flags to change the behav‐
37 ior of memfd_create():
38
39 MFD_CLOEXEC
40 Set the close-on-exec (FD_CLOEXEC) flag on the new file descrip‐
41 tor. See the description of the O_CLOEXEC flag in open(2) for
42 reasons why this may be useful.
43
44 MFD_ALLOW_SEALING
45 Allow sealing operations on this file. See the discussion of
46 the F_ADD_SEALS and F_GET_SEALS operations in fcntl(2), and also
47 NOTES, below. The initial set of seals is empty. If this flag
48 is not set, the initial set of seals will be F_SEAL_SEAL, mean‐
49 ing that no other seals can be set on the file.
50
51 MFD_HUGETLB (since Linux 4.14)
52 The anonymous file will be created in the hugetlbfs filesystem
53 using huge pages. See the Linux kernel source file Documenta‐
54 tion/admin-guide/mm/hugetlbpage.rst for more information about
55 hugetlbfs. Specifying both MFD_HUGETLB and MFD_ALLOW_SEALING in
56 flags is supported since Linux 4.16.
57
58 MFD_HUGE_2MB, MFD_HUGE_1GB, ...
59 Used in conjunction with MFD_HUGETLB to select alternative
60 hugetlb page sizes (respectively, 2