tools/testing/selftests/mm/mlock-random-test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/mlock-random-test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/mlock-random-test.c- Extension
.c- Size
- 6780 bytes
- Lines
- 268
- 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
unistd.hsys/resource.hsys/capability.hsys/mman.hlinux/mman.hfcntl.hstring.hsys/ipc.hsys/shm.htime.hkselftest.hmlock2.h
Detected Declarations
function set_cap_limitsfunction get_proc_locked_vm_sizefunction sizefunction test_mlock_within_limitfunction test_mlock_outof_limitfunction main
Annotated Snippet
if (strstr(line, "VmLck")) {
ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
if (ret <= 0) {
fclose(f);
ksft_exit_fail_msg("sscanf() on VmLck error: %s: %d\n",
line, ret);
}
fclose(f);
return (int)(lock_size << 10);
}
}
fclose(f);
ksft_exit_fail_msg("cannot parse VmLck in /proc/self/status: %s\n", strerror(errno));
return -1;
}
/*
* Get the MMUPageSize of the memory region including input
* address from proc file.
*
* return value: on error case, 0 will be returned.
* Otherwise the page size(in bytes) is returned.
*/
int get_proc_page_size(unsigned long addr)
{
FILE *smaps;
char *line;
unsigned long mmupage_size = 0;
size_t size;
smaps = seek_to_smaps_entry(addr);
if (!smaps)
ksft_exit_fail_msg("Unable to parse /proc/self/smaps\n");
while (getline(&line, &size, smaps) > 0) {
if (!strstr(line, "MMUPageSize")) {
free(line);
line = NULL;
size = 0;
continue;
}
/* found the MMUPageSize of this section */
if (sscanf(line, "MMUPageSize: %8lu kB", &mmupage_size) < 1)
ksft_exit_fail_msg("Unable to parse smaps entry for Size:%s\n",
line);
}
free(line);
if (smaps)
fclose(smaps);
return mmupage_size << 10;
}
/*
* Test mlock/mlock2() on provided memory chunk.
* It expects the mlock/mlock2() to be successful (within rlimit)
*
* With allocated memory chunk [p, p + alloc_size), this
* test will choose start/len randomly to perform mlock/mlock2
* [start, start + len] memory range. The range is within range
* of the allocated chunk.
*
* The memory region size alloc_size is within the rlimit.
* So we always expect a success of mlock/mlock2.
*
* VmLck is assumed to be 0 before this test.
*
* return value: 0 - success
* else: failure
*/
static void test_mlock_within_limit(char *p, int alloc_size)
{
int i;
int ret = 0;
int locked_vm_size = 0;
struct rlimit cur;
int page_size = 0;
getrlimit(RLIMIT_MEMLOCK, &cur);
if (cur.rlim_cur < alloc_size)
ksft_exit_fail_msg("alloc_size[%d] < %u rlimit,lead to mlock failure\n",
alloc_size, (unsigned int)cur.rlim_cur);
srand(time(NULL));
for (i = 0; i < TEST_LOOP; i++) {
/*
* - choose mlock/mlock2 randomly
* - choose lock_size randomly but lock_size < alloc_size
Annotation
- Immediate include surface: `unistd.h`, `sys/resource.h`, `sys/capability.h`, `sys/mman.h`, `linux/mman.h`, `fcntl.h`, `string.h`, `sys/ipc.h`.
- Detected declarations: `function set_cap_limits`, `function get_proc_locked_vm_size`, `function size`, `function test_mlock_within_limit`, `function test_mlock_outof_limit`, `function main`.
- 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.