I want to lock the memory to physical RAM in C with mlock and munlock, but I'm unsure about the correct usage.
Allow me to explain in a step by step scenario:
Let's assume that I dynamically allocate a pointer using calloc:
char * data = (char *)calloc(12, sizeof(char*));
Should I do mlock right after that?
Let's also assume that I later attempt to resize the memory block with realloc:
(char *)realloc(data, 100 * sizeof(char*));
Note the above increase amount ( 100 ) is random and sometimes i will decrease the memory block.
Should I first do munlock and then mlock again to address the changes made?
Also when I want to free the pointer data later, should I munlock first?
I hope someone can please explain the correct steps to me so I can understand better.
From the POSIX specification of mlock() and munlock():
The
mlock()function shall cause those whole pages containing any part of the address space of the process starting at addressaddrand continuing forlenbytes to be memory-resident until unlocked or until the process exits or execs another process image. The implementation may require thataddrbe a multiple of{PAGESIZE}.The
munlock()function shall unlock those whole pages containing any part of the address space of the process starting at addressaddrand continuing forlenbytes, regardless of how many timesmlock()has been called by the process for any of the pages in the specified range. The implementation may require thataddrbe a multiple of{PAGESIZE}.
Note that:
addr to be a multiple of page sizemunlock doesn't use any reference counting to track lock lifetimeThis make it almost impossible to use them with pointers returned by malloc/calloc/realloc as they can:
You should use mmap instead or any other OS-specific mechanism. For example Linux has mremap which allows you to "reallocate" memory. Whatever you use, make sure mlock behavior is well-defined for it. From Linux man pages:
If the memory segment specified by
old_addressandold_sizeis locked (usingmlock(2)or similar), then this lock is maintained when the segment is resized and/or relocated. As a consequence, the amount of memory locked by the process may change.
Note Nate Eldredge's comment below:
Another problem with using
reallocwith locked memory is that the data will be copied to the new location before you have a chance to find out where it is and lock it. If your purpose in usingmlockis to ensure that sensitive data never gets written out to swap, this creates a window of time where that might happen.
TL;DR
Memory locking doesn't mix with general-purpose memory allocation using the C language runtime.
Memory locking does mix with page-oriented virtual memory mapping OS-level APIs.
The above hold unless special circumstances arise (that's my way out of this :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With