tools/testing/selftests/cgroup/test_zswap.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/cgroup/test_zswap.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/cgroup/test_zswap.c- Extension
.c- Size
- 21396 bytes
- Lines
- 846
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/limits.hunistd.hstdio.hsignal.herrno.hfcntl.hsys/sysinfo.hstring.hsys/wait.hsys/mman.hsys/random.hkselftest.hcgroup_util.h
Detected Declarations
struct no_kmem_bypass_child_argsstruct incomp_child_argsstruct zswap_testfunction read_intfunction set_min_free_kbfunction read_min_free_kbfunction get_zswap_stored_pagesfunction get_cg_wb_countfunction get_zswpoutfunction allocate_and_read_bytesfunction allocate_bytesfunction wait_for_writebackfunction test_zswap_usagefunction test_swapin_nozswapfunction test_zswapinfunction attempt_writebackfunction test_zswap_writeback_onefunction test_zswap_writebackfunction test_zswap_writeback_enabledfunction test_zswap_writeback_disabledfunction test_no_invasive_cgroup_shrinkfunction no_kmem_bypass_childfunction cd08d80ecdacfunction allocate_random_and_waitfunction get_zswap_incompfunction pagesfunction check_zswap_enabledfunction main
Annotated Snippet
struct no_kmem_bypass_child_args {
size_t target_alloc_bytes;
size_t child_allocated;
};
static int no_kmem_bypass_child(const char *cgroup, void *arg)
{
struct no_kmem_bypass_child_args *values = arg;
void *allocation;
allocation = malloc(values->target_alloc_bytes);
if (!allocation) {
values->child_allocated = true;
return -1;
}
for (long i = 0; i < values->target_alloc_bytes; i += page_size)
((char *)allocation)[i] = 'a';
values->child_allocated = true;
pause();
free(allocation);
return 0;
}
/*
* When pages owned by a memcg are pushed to zswap by kswapd, they should be
* charged to that cgroup. This wasn't the case before commit
* cd08d80ecdac("mm: correctly charge compressed memory to its memcg").
*
* The test first allocates memory in a memcg, then raises min_free_kbytes to
* a very high value so that the allocation falls below low wm, then makes
* another allocation to trigger kswapd that should push the memcg-owned pages
* to zswap and verifies that the zswap pages are correctly charged.
*
* To be run on a VM with at most 4G of memory.
*/
static int test_no_kmem_bypass(const char *root)
{
size_t min_free_kb_high, min_free_kb_low, min_free_kb_original;
struct no_kmem_bypass_child_args *values;
size_t trigger_allocation_size;
int wait_child_iteration = 0;
long stored_pages_threshold;
struct sysinfo sys_info;
int ret = KSFT_FAIL;
int child_status;
char *test_group = NULL;
pid_t child_pid;
/* Read sys info and compute test values accordingly */
if (sysinfo(&sys_info) != 0)
return KSFT_FAIL;
if (sys_info.totalram > 5000000000)
return KSFT_SKIP;
values = mmap(0, sizeof(struct no_kmem_bypass_child_args), PROT_READ |
PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (values == MAP_FAILED)
return KSFT_FAIL;
if (read_min_free_kb(&min_free_kb_original))
return KSFT_FAIL;
min_free_kb_high = sys_info.totalram / 2000;
min_free_kb_low = sys_info.totalram / 500000;
values->target_alloc_bytes = (sys_info.totalram - min_free_kb_high * 1000) +
sys_info.totalram * 5 / 100;
stored_pages_threshold = sys_info.totalram / 5 / page_size;
trigger_allocation_size = sys_info.totalram / 20;
/* Set up test memcg */
test_group = cg_name(root, "kmem_bypass_test");
if (!test_group)
goto out;
/* Spawn memcg child and wait for it to allocate */
set_min_free_kb(min_free_kb_low);
if (cg_create(test_group))
goto out;
values->child_allocated = false;
child_pid = cg_run_nowait(test_group, no_kmem_bypass_child, values);
if (child_pid < 0)
goto out;
while (!values->child_allocated && wait_child_iteration++ < 10000)
usleep(1000);
/* Try to wakeup kswapd and let it push child memory to zswap */
set_min_free_kb(min_free_kb_high);
for (int i = 0; i < 20; i++) {
size_t stored_pages;
char *trigger_allocation = malloc(trigger_allocation_size);
if (!trigger_allocation)
break;
Annotation
- Immediate include surface: `linux/limits.h`, `unistd.h`, `stdio.h`, `signal.h`, `errno.h`, `fcntl.h`, `sys/sysinfo.h`, `string.h`.
- Detected declarations: `struct no_kmem_bypass_child_args`, `struct incomp_child_args`, `struct zswap_test`, `function read_int`, `function set_min_free_kb`, `function read_min_free_kb`, `function get_zswap_stored_pages`, `function get_cg_wb_count`, `function get_zswpout`, `function allocate_and_read_bytes`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.