LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mprotect() return codes (https://www.linuxquestions.org/questions/programming-9/mprotect-return-codes-320707/)

tim_l 05-06-2005 08:38 AM

mprotect() return codes
 
I'm getting an ENOMEM error from a call to mprotect(), which from the man pages means "Internal Kernel structures couldn't be allocated", which I don't really follow - too obtuse for me.

Because the memory that I'm trying to change the permissions on exists already, I'm not trying to create a new space so what is mprotect doing? And why does it fail?

Thanks,

Tim

rjlee 05-07-2005 07:37 PM

You probably want to pass the protection parameters to mmap() in the first place.

If you're not using mmap(), do be aware that a) this violates the POSIX standard and b) mprotect() works on the entire page (usually around 4kb) that happens to contain the address range you pass in; it's mostly used by the kernel internally, and by mmap().

The problem is that mprotect() uses a kernel-level function that allocates memory in the kernel-equivalent of the heap. Kernel memory allocation is subject to a number of different rules about where in RAM a block of memory can be allocated. If there doesn't happen to be any memory free in the right place, the allocation will fail. (For user-level memory, this would trigger the entire memory page to be swapped out and the memory allocation to be re-tried, but the kernel is primarily concerned with speed and so doesn't normally do this.)

The upshot of this is that allocation of kernel memory isn't guarenteed, and any function that requires allocating kernel memory, like mprotect(), can fail on memory, even if you technically have memory free in your system.

I would put this in a while loop, with an exit condition on timeout or when not returning ENOMEM. It might also help you to usleep() or schedule during the loop, as this will at least give the kernel a chance to swap out some RAM.

tim_l 05-09-2005 07:38 AM

Thanks for the reply.

Thing is, I'm using mprotect() to (try and) change the access rights of the current executing process, the memory the program itself is loaded into, so will waiting around actually be of any benefit? Because surely the kernel has already allocated the memory in the right place?

I was a little perplexed by the whole thing because I've tried the program on two machines, on two different operating systems and there hasn't been a problem. But on a third machine I keep getting this ENOMEM.


All times are GMT -5. The time now is 11:29 AM.