Limits on resources in Linux
Last updated
Was this helpful?
Last updated
Was this helpful?
Each process in the system uses certain amount of different resources like files, CPU time, memory and so on.
Such resources are not infinite and each process and we should have an instrument to manage it. Sometimes it is useful to know current limits for a certain resource or to change its value. In this post we will consider such instruments that allow us to get information about limits for a process and increase or decrease such limits.
We will start from userspace view and then we will look how it is implemented in the Linux kernel.
There are three main fundamental to manage resource limit for a process:
getrlimit
setrlimit
prlimit
The first two allows a process to read and set limits on a system resource. The last one is extension for previous functions. The prlimit
allows to set and read the resource limits of a process specified by . Definitions of these functions looks:
The getrlimit
is:
The setrlimit
is:
And the definition of the prlimit
is:
In the first two cases, functions takes two parameters:
resource
- represents resource type (we will see available types later);
rlim
- combination of soft
and hard
limits.
There are two types of limits:
soft
hard
The first provides actual limit for a resource of a process. The second is a ceiling value of a soft
limit and can be set only by superuser. So, soft
limit can never exceed related hard
limit.
Both these values are combined in the rlimit
structure:
The last one function looks a little bit complex and takes 4
arguments. Besides resource
argument, it takes:
pid
- specifies an ID of a process on which the prlimit
should be executed;
new_limit
- provides new limits values if it is not NULL
;
old_limit
- current soft
and hard
limits will be placed here if it is not NULL
.
For example:
Here we can see prlimit64
, but not the prlimit
. The fact is that we see underlying system call here instead of library call.
Now let's look at list of available resources:
RLIMIT_CPU
CPU time limit given in seconds
RLIMIT_FSIZE
the maximum size of files that a process may create
RLIMIT_DATA
the maximum size of the process's data segment
RLIMIT_STACK
the maximum size of the process stack in bytes
RLIMIT_CORE
RLIMIT_RSS
the number of bytes that can be allocated for a process in RAM
RLIMIT_NPROC
the maximum number of processes that can be created by a user
RLIMIT_NOFILE
the maximum number of a file descriptor that can be opened by a process
RLIMIT_MEMLOCK
RLIMIT_AS
the maximum size of virtual memory in bytes.
RLIMIT_LOCKS
RLIMIT_SIGPENDING
RLIMIT_MSGQUEUE
RLIMIT_NICE
RLIMIT_RTPRIO
maximum real-time priority value
RLIMIT_RTTIME
maximum number of microseconds that a process may be scheduled under real-time scheduling policy without making blocking system call
If you're looking into source code of open source projects, you will note that reading or updating of a resource limit is quite widely used operation.
We've just saw a little bit about resources limits related stuff in the userspace, now let's look at the same system calls in the Linux kernel.
Both implementation of getrlimit
system call and setrlimit
looks similar. Both they execute do_prlimit
function that is core implementation of the prlimit
system call and copy from/to given rlimit
from/to userspace:
The getrlimit
:
and setrlimit
:
First of all the do_prlimit
function executes a check that the given resource is valid:
and in a failure case returns -EINVAL
error. After this check will pass successfully and new limits was passed as non NULL
value, two following checks:
We need to do this because prlimit
system call allows us to update limits of another task by the given pid. As task list is locked, we take the rlimit
instance that is responsible for the given resource limit of the given process:
where the tsk->signal->rlim
is just array of struct rlimit
that represents certain resources. And if the new_rlim
is not NULL
we just update its value. If old_rlim
is not NULL
we fill it:
That's all.
Exactly prlimit
function is used by util. We can verify this with the help of util.
the maximum size of a file.
the maximum number of bytes of memory that may be locked into RAM by .
the maximum number and locking related calls
maximum number of that may be queued for a user of the calling process
the number of bytes that can be allocated for
the maximum value that can be set by a process
For example:
Or :
Implementations of these system calls are defined in the kernel source code file.
check that the given soft
limit does not exceed hard
limit and in a case when the given resource is the maximum number of a file descriptors that hard limit is not greater than sysctl_nr_open
value. The value of the sysctl_nr_open
can be found via :
After all of these checks we lock tasklist
to be sure that handlers related things will not be destroyed while we updating limits for a given resource:
This is the end of the second part that describes implementation of the system calls in the Linux kernel. If you have questions or suggestions, ping me on Twitter , drop me an , or just create an .
Please note that English is not my first language and I am really sorry for any inconvenience. If you find any mistakes please send me PR to .