Linux: How to Lock The Pages of a Process in Memory

Memory-Debuging: How to get locked pages information in user space/kernel space in linux

For each page in memory the is flag assigned to it, Virtual memory page is locked using mlock, mlockall() etc API, it assigned the VM_LOCKED flag to page.

Two options to know the locked pages detail:

  1. Use cat /sys/kernel/debug/page_owner >> page_owner.txt
    To enable debug: kernel menuconfig PAGE_OWNER=y
    add "page_owner=on" to boot cmdline.
    cat /sys/kernel/debug/page_owner >> page_owner.txt

  2. In linux source code type: /tool/vm/page-types.c, then
    compile it and iterate through all pid entries in /proc/ and use the following option for application:
    ./test -p $PID -L >> test_output.txt.
    It will give you all page details with flags, then you can find locked pages in memory.

`mlockall` to another process

I write a new system call in order to fill the gap.

I attached the patch to the kernel-mm mailing list, you can find it here

Why would we need to lock a process's address space in RAM?

It ensures the memory is always in RAM and never moved to the swap disk. This makes accessing those memory locations much faster as disks are extremely slow compared to RAM.

In a realtime system (linux is NOT a RTS btw!) you need extremely low latencies so a memory access resulting in a disk access is usually not acceptable inside time-critical code.

How do segments from a single memory page behave when mlock'd?

If it locks or unlocks, it does so for the entire page. There's no smaller granularity in the CPU (disregarding x86 segmentation, which is effectively disabled). The OS does not check each and every memory access either (would be prohibitively slow like going to computers of the 80's).

Does the kernel implement a mechanism where the page remains locked as long as the number of mlocks exceeds the number of unlocks?

No:

Memory locks do not stack, that is, pages which have been locked several times by calls to mlock() or mlockall() will be unlocked by a single call to munlock() for the corresponding range or by munlockall().



Related Topics



Leave a reply



Submit