3 * This tests memfd interactions with get_user_pages(). We require the
4 * fuse_mnt.c program to provide a fake direct-IO FUSE mount-point for us. This
5 * file-system delays _all_ reads by 1s and forces direct-IO. This means, any
6 * read() on files in that file-system will pin the receive-buffer pages for at
7 * least 1s via get_user_pages().
9 * We use this trick to race ADD_SEALS against a write on a memfd object. The
10 * ADD_SEALS must fail if the memfd pages are still pinned. Note that we use
11 * the read() syscall with our memory-mapped memfd object as receive buffer to
12 * force the kernel to write into our memfd object.
16 #define __EXPORTED_HEADERS__
21 #include <linux/falloc.h>
22 #include <linux/fcntl.h>
23 #include <linux/memfd.h>
31 #include <sys/syscall.h>
35 #define MFD_DEF_SIZE 8192
36 #define STACK_SIZE 65535
38 static int sys_memfd_create(const char *name
,
41 return syscall(__NR_memfd_create
, name
, flags
);
44 static int mfd_assert_new(const char *name
, loff_t sz
, unsigned int flags
)
48 fd
= sys_memfd_create(name
, flags
);
50 printf("memfd_create(\"%s\", %u) failed: %m\n",
55 r
= ftruncate(fd
, sz
);
57 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz
);
64 static __u64
mfd_assert_get_seals(int fd
)
68 r
= fcntl(fd
, F_GET_SEALS
);
70 printf("GET_SEALS(%d) failed: %m\n", fd
);
77 static void mfd_assert_has_seals(int fd
, __u64 seals
)
81 s
= mfd_assert_get_seals(fd
);
83 printf("%llu != %llu = GET_SEALS(%d)\n",
84 (unsigned long long)seals
, (unsigned long long)s
, fd
);
89 static void mfd_assert_add_seals(int fd
, __u64 seals
)
94 s
= mfd_assert_get_seals(fd
);
95 r
= fcntl(fd
, F_ADD_SEALS
, seals
);
97 printf("ADD_SEALS(%d, %llu -> %llu) failed: %m\n",
98 fd
, (unsigned long long)s
, (unsigned long long)seals
);
103 static int mfd_busy_add_seals(int fd
, __u64 seals
)
108 r
= fcntl(fd
, F_GET_SEALS
);
114 r
= fcntl(fd
, F_ADD_SEALS
, seals
);
115 if (r
< 0 && errno
!= EBUSY
) {
116 printf("ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\n",
117 fd
, (unsigned long long)s
, (unsigned long long)seals
);
124 static void *mfd_assert_mmap_shared(int fd
)
130 PROT_READ
| PROT_WRITE
,
134 if (p
== MAP_FAILED
) {
135 printf("mmap() failed: %m\n");
142 static void *mfd_assert_mmap_private(int fd
)
148 PROT_READ
| PROT_WRITE
,
152 if (p
== MAP_FAILED
) {
153 printf("mmap() failed: %m\n");
160 static int global_mfd
= -1;
161 static void *global_p
= NULL
;
163 static int sealing_thread_fn(void *arg
)
168 * This thread first waits 200ms so any pending operation in the parent
169 * is correctly started. After that, it tries to seal @global_mfd as
170 * SEAL_WRITE. This _must_ fail as the parent thread has a read() into
171 * that memory mapped object still ongoing.
172 * We then wait one more second and try sealing again. This time it
173 * must succeed as there shouldn't be anyone else pinning the pages.
176 /* wait 200ms for FUSE-request to be active */
179 /* unmount mapping before sealing to avoid i_mmap_writable failures */
180 munmap(global_p
, MFD_DEF_SIZE
);
182 /* Try sealing the global file; expect EBUSY or success. Current
183 * kernels will never succeed, but in the future, kernels might
184 * implement page-replacements or other fancy ways to avoid racing
186 r
= mfd_busy_add_seals(global_mfd
, F_SEAL_WRITE
);
188 printf("HURRAY! This kernel fixed GUP races!\n");
190 /* wait 1s more so the FUSE-request is done */
193 /* try sealing the global file again */
194 mfd_assert_add_seals(global_mfd
, F_SEAL_WRITE
);
200 static pid_t
spawn_sealing_thread(void)
205 stack
= malloc(STACK_SIZE
);
207 printf("malloc(STACK_SIZE) failed: %m\n");
211 pid
= clone(sealing_thread_fn
,
213 SIGCHLD
| CLONE_FILES
| CLONE_FS
| CLONE_VM
,
216 printf("clone() failed: %m\n");
223 static void join_sealing_thread(pid_t pid
)
225 waitpid(pid
, NULL
, 0);
228 int main(int argc
, char **argv
)
230 static const char zero
[MFD_DEF_SIZE
];
237 printf("error: please pass path to file in fuse_mnt mount-point\n");
241 /* open FUSE memfd file for GUP testing */
242 printf("opening: %s\n", argv
[1]);
243 fd
= open(argv
[1], O_RDONLY
| O_CLOEXEC
);
245 printf("cannot open(\"%s\"): %m\n", argv
[1]);
249 /* create new memfd-object */
250 mfd
= mfd_assert_new("kern_memfd_fuse",
252 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
254 /* mmap memfd-object for writing */
255 p
= mfd_assert_mmap_shared(mfd
);
257 /* pass mfd+mapping to a separate sealing-thread which tries to seal
258 * the memfd objects with SEAL_WRITE while we write into it */
261 pid
= spawn_sealing_thread();
263 /* Use read() on the FUSE file to read into our memory-mapped memfd
264 * object. This races the other thread which tries to seal the
266 * If @fd is on the memfd-fake-FUSE-FS, the read() is delayed by 1s.
267 * This guarantees that the receive-buffer is pinned for 1s until the
268 * data is written into it. The racing ADD_SEALS should thus fail as
269 * the pages are still pinned. */
270 r
= read(fd
, p
, MFD_DEF_SIZE
);
272 printf("read() failed: %m\n");
275 printf("unexpected EOF on read()\n");
279 was_sealed
= mfd_assert_get_seals(mfd
) & F_SEAL_WRITE
;
281 /* Wait for sealing-thread to finish and verify that it
282 * successfully sealed the file after the second try. */
283 join_sealing_thread(pid
);
284 mfd_assert_has_seals(mfd
, F_SEAL_WRITE
);
286 /* *IF* the memfd-object was sealed at the time our read() returned,
287 * then the kernel did a page-replacement or canceled the read() (or
288 * whatever magic it did..). In that case, the memfd object is still
290 * In case the memfd-object was *not* sealed, the read() was successfull
291 * and the memfd object must *not* be all zero.
292 * Note that in real scenarios, there might be a mixture of both, but
293 * in this test-cases, we have explicit 200ms delays which should be
294 * enough to avoid any in-flight writes. */
296 p
= mfd_assert_mmap_private(mfd
);
297 if (was_sealed
&& memcmp(p
, zero
, MFD_DEF_SIZE
)) {
298 printf("memfd sealed during read() but data not discarded\n");
300 } else if (!was_sealed
&& !memcmp(p
, zero
, MFD_DEF_SIZE
)) {
301 printf("memfd sealed after read() but data discarded\n");
308 printf("fuse: DONE\n");