1 // SPDX-License-Identifier: GPL-2.0
4 * This tests memfd interactions with get_user_pages(). We require the
5 * fuse_mnt.c program to provide a fake direct-IO FUSE mount-point for us. This
6 * file-system delays _all_ reads by 1s and forces direct-IO. This means, any
7 * read() on files in that file-system will pin the receive-buffer pages for at
8 * least 1s via get_user_pages().
10 * We use this trick to race ADD_SEALS against a write on a memfd object. The
11 * ADD_SEALS must fail if the memfd pages are still pinned. Note that we use
12 * the read() syscall with our memory-mapped memfd object as receive buffer to
13 * force the kernel to write into our memfd object.
17 #define __EXPORTED_HEADERS__
22 #include <linux/falloc.h>
24 #include <linux/memfd.h>
32 #include <sys/syscall.h>
38 #define MFD_DEF_SIZE 8192
39 #define STACK_SIZE 65536
41 static size_t mfd_def_size
= MFD_DEF_SIZE
;
43 static int mfd_assert_new(const char *name
, loff_t sz
, unsigned int flags
)
47 fd
= sys_memfd_create(name
, flags
);
49 printf("memfd_create(\"%s\", %u) failed: %m\n",
54 r
= ftruncate(fd
, sz
);
56 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz
);
63 static __u64
mfd_assert_get_seals(int fd
)
67 r
= fcntl(fd
, F_GET_SEALS
);
69 printf("GET_SEALS(%d) failed: %m\n", fd
);
76 static void mfd_assert_has_seals(int fd
, __u64 seals
)
80 s
= mfd_assert_get_seals(fd
);
82 printf("%llu != %llu = GET_SEALS(%d)\n",
83 (unsigned long long)seals
, (unsigned long long)s
, fd
);
88 static void mfd_assert_add_seals(int fd
, __u64 seals
)
93 s
= mfd_assert_get_seals(fd
);
94 r
= fcntl(fd
, F_ADD_SEALS
, seals
);
96 printf("ADD_SEALS(%d, %llu -> %llu) failed: %m\n",
97 fd
, (unsigned long long)s
, (unsigned long long)seals
);
102 static int mfd_busy_add_seals(int fd
, __u64 seals
)
107 r
= fcntl(fd
, F_GET_SEALS
);
113 r
= fcntl(fd
, F_ADD_SEALS
, seals
);
114 if (r
< 0 && errno
!= EBUSY
) {
115 printf("ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\n",
116 fd
, (unsigned long long)s
, (unsigned long long)seals
);
123 static void *mfd_assert_mmap_shared(int fd
)
129 PROT_READ
| PROT_WRITE
,
133 if (p
== MAP_FAILED
) {
134 printf("mmap() failed: %m\n");
141 static void *mfd_assert_mmap_private(int fd
)
147 PROT_READ
| PROT_WRITE
,
151 if (p
== MAP_FAILED
) {
152 printf("mmap() failed: %m\n");
159 static int global_mfd
= -1;
160 static void *global_p
= NULL
;
162 static int sealing_thread_fn(void *arg
)
167 * This thread first waits 200ms so any pending operation in the parent
168 * is correctly started. After that, it tries to seal @global_mfd as
169 * SEAL_WRITE. This _must_ fail as the parent thread has a read() into
170 * that memory mapped object still ongoing.
171 * We then wait one more second and try sealing again. This time it
172 * must succeed as there shouldn't be anyone else pinning the pages.
175 /* wait 200ms for FUSE-request to be active */
178 /* unmount mapping before sealing to avoid i_mmap_writable failures */
179 munmap(global_p
, mfd_def_size
);
181 /* Try sealing the global file; expect EBUSY or success. Current
182 * kernels will never succeed, but in the future, kernels might
183 * implement page-replacements or other fancy ways to avoid racing
185 r
= mfd_busy_add_seals(global_mfd
, F_SEAL_WRITE
);
187 printf("HURRAY! This kernel fixed GUP races!\n");
189 /* wait 1s more so the FUSE-request is done */
192 /* try sealing the global file again */
193 mfd_assert_add_seals(global_mfd
, F_SEAL_WRITE
);
199 static pid_t
spawn_sealing_thread(void)
204 stack
= malloc(STACK_SIZE
);
206 printf("malloc(STACK_SIZE) failed: %m\n");
210 pid
= clone(sealing_thread_fn
,
212 SIGCHLD
| CLONE_FILES
| CLONE_FS
| CLONE_VM
,
215 printf("clone() failed: %m\n");
222 static void join_sealing_thread(pid_t pid
)
224 waitpid(pid
, NULL
, 0);
227 int main(int argc
, char **argv
)
236 printf("error: please pass path to file in fuse_mnt mount-point\n");
241 if (!strcmp(argv
[2], "hugetlbfs")) {
242 unsigned long hpage_size
= default_huge_page_size();
245 printf("Unable to determine huge page size\n");
250 mfd_def_size
= hpage_size
* 2;
252 printf("Unknown option: %s\n", argv
[2]);
257 zero
= calloc(sizeof(*zero
), mfd_def_size
);
259 /* open FUSE memfd file for GUP testing */
260 printf("opening: %s\n", argv
[1]);
261 fd
= open(argv
[1], O_RDONLY
| O_CLOEXEC
);
263 printf("cannot open(\"%s\"): %m\n", argv
[1]);
267 /* create new memfd-object */
268 mfd
= mfd_assert_new("kern_memfd_fuse",
270 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
272 /* mmap memfd-object for writing */
273 p
= mfd_assert_mmap_shared(mfd
);
275 /* pass mfd+mapping to a separate sealing-thread which tries to seal
276 * the memfd objects with SEAL_WRITE while we write into it */
279 pid
= spawn_sealing_thread();
281 /* Use read() on the FUSE file to read into our memory-mapped memfd
282 * object. This races the other thread which tries to seal the
284 * If @fd is on the memfd-fake-FUSE-FS, the read() is delayed by 1s.
285 * This guarantees that the receive-buffer is pinned for 1s until the
286 * data is written into it. The racing ADD_SEALS should thus fail as
287 * the pages are still pinned. */
288 r
= read(fd
, p
, mfd_def_size
);
290 printf("read() failed: %m\n");
293 printf("unexpected EOF on read()\n");
297 was_sealed
= mfd_assert_get_seals(mfd
) & F_SEAL_WRITE
;
299 /* Wait for sealing-thread to finish and verify that it
300 * successfully sealed the file after the second try. */
301 join_sealing_thread(pid
);
302 mfd_assert_has_seals(mfd
, F_SEAL_WRITE
);
304 /* *IF* the memfd-object was sealed at the time our read() returned,
305 * then the kernel did a page-replacement or canceled the read() (or
306 * whatever magic it did..). In that case, the memfd object is still
308 * In case the memfd-object was *not* sealed, the read() was successfull
309 * and the memfd object must *not* be all zero.
310 * Note that in real scenarios, there might be a mixture of both, but
311 * in this test-cases, we have explicit 200ms delays which should be
312 * enough to avoid any in-flight writes. */
314 p
= mfd_assert_mmap_private(mfd
);
315 if (was_sealed
&& memcmp(p
, zero
, mfd_def_size
)) {
316 printf("memfd sealed during read() but data not discarded\n");
318 } else if (!was_sealed
&& !memcmp(p
, zero
, mfd_def_size
)) {
319 printf("memfd sealed after read() but data discarded\n");
326 printf("fuse: DONE\n");