Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

real-time

http://linux.die.net/man/2/mlockall

mlockall() locks all of the calling process's virtual address space into RAM, preventing that memory from being paged to the swap area.

why is this important in real time systems?

like image 693
Aquarius_Girl Avatar asked Mar 22 '12 08:03

Aquarius_Girl


People also ask

What is locked memory?

A memory lock persists until the process that owns the memory explicitly unlocks it. (But process termination and exec cause the virtual memory to cease to exist, which you might say means it isn't locked any more). Memory locks are not inherited by child processes.

What does a process address space contain?

The process address space consists of the linear address range presented to each process and, more importantly, the addresses within this space that the process is allowed to use. Each process is given a flat 32- or 64-bit address space, with the size depending on the architecture.

What is process address space in operating system?

What is an address space? z/OS concepts. The range of virtual addresses that the operating system assigns to a user or separately running program is called an address space. This is the area of contiguous virtual addresses available for executing instructions and storing data.

What is Mlock system call?

mlock() locks pages in the address range starting at addr and continuing for len bytes. All pages that contain a part of the specified address range are guaranteed to be resident in RAM when the call returns successfully; the pages are guaranteed to stay in RAM until later unlocked.


2 Answers

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.

like image 80
ThiefMaster Avatar answered Nov 14 '22 23:11

ThiefMaster


It can be used in real-time applications or high-security data processing for instance. This is a quote from mlockall() documentation:

Real-time applications require deterministic timing, and, like scheduling, paging is one major cause of unexpected program execution delays. Real-time applications will usually also switch to a real-time scheduler with sched_setscheduler(2). Crypto‐ graphic security software often handles critical bytes like passwords or secret keys as data structures. As a result of paging, these secrets could be transferred onto a per‐ sistent swap store medium, where they might be accessible to the enemy long after the security software has erased the secrets in RAM and terminated. (But be aware that the suspend mode on laptops and some desktop computers will save a copy of the system's RAM to disk, regardless of memory locks.)

Real-time processes that are using mlockall() to prevent delays on page faults should reserve enough locked stack pages before entering the time-critical section, so that no page fault can be caused by function calls. This can be achieved by calling a function that allocates a sufficiently large automatic variable (an array) and writes to the mem‐ ory occupied by this array in order to touch these stack pages. This way, enough pages will be mapped for the stack and can be locked into RAM. The dummy writes ensure that not even copy-on-write page faults can occur in the critical section.

like image 44
Danil Onishchenko Avatar answered Nov 14 '22 23:11

Danil Onishchenko