tools/testing/selftests/memfd/fuse_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/memfd/fuse_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/memfd/fuse_test.c- Extension
.c- Size
- 7714 bytes
- Lines
- 332
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
Dependency Surface
errno.hinttypes.hlimits.hlinux/falloc.hfcntl.hlinux/memfd.hlinux/types.hsched.hstdio.hstdlib.hsignal.hstring.hsys/mman.hsys/stat.hsys/syscall.hsys/wait.hunistd.hcommon.h
Detected Declarations
function mfd_assert_newfunction mfd_assert_get_sealsfunction mfd_assert_has_sealsfunction mfd_assert_add_sealsfunction mfd_busy_add_sealsfunction sealing_thread_fnfunction spawn_sealing_threadfunction join_sealing_threadfunction main
Annotated Snippet
if (!strcmp(argv[2], "hugetlbfs")) {
unsigned long hpage_size = default_huge_page_size();
if (!hpage_size) {
printf("Unable to determine huge page size\n");
abort();
}
hugetlbfs_test = 1;
mfd_def_size = hpage_size * 2;
} else {
printf("Unknown option: %s\n", argv[2]);
abort();
}
}
zero = calloc(sizeof(*zero), mfd_def_size);
/* open FUSE memfd file for GUP testing */
printf("opening: %s\n", argv[1]);
fd = open(argv[1], O_RDONLY | O_CLOEXEC);
if (fd < 0) {
printf("cannot open(\"%s\"): %m\n", argv[1]);
abort();
}
/* create new memfd-object */
mfd = mfd_assert_new("kern_memfd_fuse",
mfd_def_size,
MFD_CLOEXEC | MFD_ALLOW_SEALING);
/* mmap memfd-object for writing */
p = mfd_assert_mmap_shared(mfd);
/* pass mfd+mapping to a separate sealing-thread which tries to seal
* the memfd objects with SEAL_WRITE while we write into it */
global_mfd = mfd;
global_p = p;
pid = spawn_sealing_thread();
/* Use read() on the FUSE file to read into our memory-mapped memfd
* object. This races the other thread which tries to seal the
* memfd-object.
* If @fd is on the memfd-fake-FUSE-FS, the read() is delayed by 1s.
* This guarantees that the receive-buffer is pinned for 1s until the
* data is written into it. The racing ADD_SEALS should thus fail as
* the pages are still pinned. */
r = read(fd, p, mfd_def_size);
if (r < 0) {
printf("read() failed: %m\n");
abort();
} else if (!r) {
printf("unexpected EOF on read()\n");
abort();
}
was_sealed = mfd_assert_get_seals(mfd) & F_SEAL_WRITE;
/* Wait for sealing-thread to finish and verify that it
* successfully sealed the file after the second try. */
join_sealing_thread(pid);
mfd_assert_has_seals(mfd, F_SEAL_WRITE);
/* *IF* the memfd-object was sealed at the time our read() returned,
* then the kernel did a page-replacement or canceled the read() (or
* whatever magic it did..). In that case, the memfd object is still
* all zero.
* In case the memfd-object was *not* sealed, the read() was successful
* and the memfd object must *not* be all zero.
* Note that in real scenarios, there might be a mixture of both, but
* in this test-cases, we have explicit 200ms delays which should be
* enough to avoid any in-flight writes. */
p = mfd_assert_mmap_private(mfd);
if (was_sealed && memcmp(p, zero, mfd_def_size)) {
printf("memfd sealed during read() but data not discarded\n");
abort();
} else if (!was_sealed && !memcmp(p, zero, mfd_def_size)) {
printf("memfd sealed after read() but data discarded\n");
abort();
}
close(mfd);
close(fd);
printf("fuse: DONE\n");
free(zero);
return 0;
}
Annotation
- Immediate include surface: `errno.h`, `inttypes.h`, `limits.h`, `linux/falloc.h`, `fcntl.h`, `linux/memfd.h`, `linux/types.h`, `sched.h`.
- Detected declarations: `function mfd_assert_new`, `function mfd_assert_get_seals`, `function mfd_assert_has_seals`, `function mfd_assert_add_seals`, `function mfd_busy_add_seals`, `function sealing_thread_fn`, `function spawn_sealing_thread`, `function join_sealing_thread`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.