Linux default stack size

Linux default stack size

Let me talk about thread memory related things, mainly the following:

  • All threads in the process share the same address space.
  • Any variable declared as static/extern or heap variable can be read and written by all threads in the process.
  • The only private storage that a thread really has is processor registers.
  • The thread stack can be shared with other threads by exposing the stack address.

In applications with large amounts of data, sometimes we need to allocate a large memory block in the stack space or allocate many small memory blocks, but the maximum value of the thread’s stack space is already set when the thread is created. If the size of the stack exceeds a certain value, the system will access unauthorized memory blocks. There is no doubt that it will definitely be a segfault.

When pthread_create() creates a thread, if you do not specify the size of the allocated stack, the system will allocate the default value. The method for viewing through the command is as follows:

The above unit is Kb, so the thread default stack size is 8M.

Can also be passed under the terminalulimit -s value Used to reset the stack size.

In general, the default stack size is 8388608, the minimum stack size is 16384, and the unit is bytes. In some embedded systems, if the memory is not very large, the default value will cause problems. If the memory is insufficient, pthread_create() will return 12, which is defined as follows:

02. Set thread stack function

You can use pthread_attr_getstacksize() and pthread_attr_setstacksize() to get and set the thread’s stack space.

Sample code to view the thread stack size is as follows:

The sample code for setting the thread stack size is as follows:

Источник

Default stack size for pthreads

As I understand, the default stack size for a pthread on Linux is 16K. I am getting strange results on my 64-bit Ubuntu install.

I’m quite sure the stack size is not «8388608». What could be wrong?

2 Answers 2

Actually, your virtual stack size is 8388608 bytes (8 MB). Of course, it’s natural to conclude that this can’t be right, because that’s a ridiculously large amount of memory for every thread to consume for its stack when 99% of the time a couple of KB is probably all they need.

The good news is that your thread only uses the amount of physical memory that it actually needs. This is one of the magical powers that your OS gets from using the hardware Memory Management Unit (MMU) in your processor. Here’s what happens:

Читайте также:  Express vpn для linux

The OS allocates 8 MB of virtual memory for your stack by setting up the MMU’s page tables for your thread. This requires very little RAM to hold the page table entries only.

When your thread runs and tries to access a virtual address on the stack that doesn’t have a physical page assigned to it yet, a hardware exception called a «page fault» is triggered by the MMU.

The CPU core responds to the page fault exception by switching to a privileged execution mode (which has its own stack) and calling the page fault exception handler function inside the kernel.

The kernel allocates a page of physical RAM to that virtual memory page and returns back to the user space thread.

The user space thread sees none of that work. From its point of view, it just uses the stack as if the memory was there all along. Meanwhile, the stack automatically grows (or doesn’t) to meet the thread’s needs.

The MMU is a key part of the hardware of today’s computer systems. In particular, it’s responsible for a lot of the «magic» in the system, so I highly recommend learning more about what the MMU does, and about virtual memory in general. Also, if your application is performance sensitive and deals with a significant amount of data, you should understand how the TLB (the MMU’s page table cache) works and how you can restructure your data or your algorithms to maximize your TLB hit rate.

Источник

Linux Stack Sizes

I’m looking for a good description of stacks within the linux kernel, but I’m finding it surprisingly difficult to find anything useful.

I know that stacks are limited to 4k for most systems, and 8k for others. I’m assuming that each kernel thread / bottom half has its own stack. I’ve also heard that if an interrupt goes off, it uses the current thread’s stack, but I can’t find any documentation on any of this. What I’m looking for is how the stacks are allocated, if there’s any good debugging routines for them (I’m suspecting a stack overflow for a particular problem, and I’d like to know if its possible to compile the kernel to police stack sizes, etc).

3 Answers 3

The reason that documentation is scarce is that it’s an area that’s quite architecture-dependent. The code is really the best documentation — for example, the THREAD_SIZE macro defines the (architecture-dependent) per-thread kernel stack size.

The stacks are allocated in alloc_thread_stack_node() . The stack pointer in the struct task_struct is updated in dup_task_struct() , which is called as part of cloning a thread.

The kernel does check for kernel stack overflows, by placing a canary value STACK_END_MAGIC at the end of the stack. In the page fault handler, if a fault in kernel space occurs this canary is checked — see for example the x86 fault handler which prints the message Thread overran stack, or stack corrupted after the Oops message if the stack canary has been clobbered.

Читайте также:  Visual studio code golang настройка linux

Of course this won’t trigger on all stack overruns, only the ones that clobber the stack canary. However, you should always be able to tell from the Oops output if you’ve suffered a stack overrun — that’s the case if the stack pointer is below task->stack .

Источник

Что такое Stack Size (размер стека) в UNIX-like

Понятие «стека» (Stack) имеет довольно обширное понятие в вычислительной технике. Существуют такие виды стеков как «Программный стек», «Аппаратный стек» и «Сетевой стек», но Stack Size (размер стека) обычно подразумевает тематику именно «Программного стека» и имеет прямое отношение к оперативной памяти.

У каждого процесса есть свой стек, который размещается в оперативной памяти и используется для передачи параметров и хранения локальных переменных программы. Каждый раз, когда процесс обращается к подпрограмме или функции, в стек отправляется новый фрейм, частью которого является указатель на базу предыдущего фрейма, который дает возможность вернуться из вызова функции.

Обычно в Linux Stack Size (размер стека) равен 10240 kb (10M), управляется конфигурационным файлом /etc/security/limits.conf и командой ulimit -s StackSize , где StackSize размер в kb.

Существует также понятие soft (мягкий) лимит и hard (жесткий) лимит на размер стека. Когда достигнут предел soft (мягкий) лимита, то приложению будет позволено увеличить размер стека до планки hard лимита. По-умолчанию жесткий лимит в Linux не ограничен, проверить можно командой:

Многие программы обычно способны к расширению стека (stack expansion), но в некоторых случаях расширение стека может быть невозможно, что может послужить причиной завершения приложения с многочисленными ошибками.

Для примера чувствительных к размеру стека приложений можно взять веб-сервер Apache с его модулем mod_fcgid и вики-движок MediaWiki. Так например в описании директивы ThreadStackSize веб-сервера Apache сказано, что в большинстве случаев размер стека определённый операционной системой является разумным, но в некоторых случаях требует корректировки:

  • На платформах (например, HP-UX), с относительно небольшим размером стека может произойти сбой в работе сторонних модулей Apache. В таком случае увеличение размера стека через параметр ThreadStackSize может решить проблему.
  • На платформах с довольно большим размером стека по умолчанию, снижение размера стека через параметр ThreadStackSize можно снизить расход оперативной памяти.

Для нормальной работы вики-движка MediaWiki есть особые рекомендации для веб-сервера Apache, включая требования к StackSize не ниже чем 8М.

Параметры ядра на StackSize можно изменять, но в разумных пределах и с контролем влияния настройки на общую производительность отдельно взятой машины имхо популярный в сети Интернет способ экономии РАМы за счёт размера стека не всегда может дать положительный результат.

Уменьшение размера стека (StackSize) безусловно снизит общее потребление оперативной памяти, но в вместе с тем может снизить стабильность системы и не обязательно даст желаемый прирост производительности. Если хоть на один байт размер стека окажется меньше необходимого, то некоторые процессы могут обрываться с многочисленными ошибками.

Читайте также:  Android running windows programs

Кроме стабильности нужно помнить и о безопасности. Приложение, которое работает на сервере может быть хорошо защищено само по себе, но экспериментами с параметрами стека мы можем широко распахнуть дверь перед злоумышленниками.

Рекомендуемый контент

А тут же ж мог быть рекомендуемый контент от гугла 🙂 Для отображения рекомендуемого контента необходимо в браузере разрешить выполнение JavaScript скриптов, включая скрипты с доменов googlesyndication.com и doubleclick.net

Вы не любите рекламу!? Напрасно!:) На нашем сайте она вовсе ненавязчивая, а потому для нашего сайта можете полностью отключить AdBlock (uBlock/uBlock Origin/NoScript) и прочие блокировщики рекламы! AdBlock/uBlock может препятствовать нормальной работе системы поиска по сайту, отображению рекомендуемого контента и прочих сервисов Google. Рекомендуем полностью отключить блокировщик рекламы и скриптов, а также разрешить фреймы (aka iframe).

Источник

Why on modern Linux, the default stack size is so huge — 8MB (even 10 on some distributions)

For example, on OSX, it’s even less than 512k.

Is there any recommended size, having in mind, that the app does not use recursion and does not allocate a lot of stack variables?
I know the question is too broad and it highly depends on the usage, but still wanted to ask, as I was wondering if there’s some hidden/internal/system reason behind this huge number.

I was wondering, as I intend to change the stack size to 512 KiB in my app — this still sounds like a huge number for this, but it’s much smaller than 8MiB — and will lead to significantly decreased virtual memory of the process, as I have a lot of threads (I/O).

I also know this doesn’t really hurt, well explained here: Default stack size for pthreads

2 Answers 2

As others have said, and as is mentioned in the link you provide in your question, having an 8MiB stack doesn’t hurt anything (apart from consuming address space — on a 64-bit system that won’t matter).

Linux has used 8MiB stacks for a very long time; the change was introduced in version 1.3.7 of the kernel, in July 1995. Back then it was presented as introducing a limit, previously there wasn’t one:

Limit the stack by to some sane default: root can always increase this limit if needed.. 8MB seems reasonable.

On Linux, the stack limit also affects the size of program arguments and the environment, which are limited to one quarter of the stack limit; the kernel enforces a minimum of 32 pages for the arguments and environment.

For threads, if the stack limit ( RLIMIT_STACK ) is unlimited, pthread_create applies its own limits to new threads’ stacks — and on most architectures, that’s less than 8MiB.

Источник

Оцените статью