Linux 堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6270945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:23:31  来源:igfitidea点击:

Linux Stack Sizes

linuxlinux-kernellinux-device-driver

提问by John Ulvr

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

我正在寻找对 linux 内核中堆栈的良好描述,但我发现很难找到任何有用的东西。

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).

我知道大多数系统的堆栈限制为 4k,其他系统限制为 8k。我假设每个内核线程/下半部分都有自己的堆栈。我还听说如果中断发生,它会使用当前线程的堆栈,但我找不到任何关于此的文档。我正在寻找的是堆栈是如何分配的,如果有任何好的调试例程(我怀疑某个特定问题的堆栈溢出,我想知道是否可以将内核编译为警察堆栈大小等)。

回答by vhallac

For processes, you can control the stack size of processes via ulimitcommand (-soption). For threads, the default stack size varies a lot, but you can control it via a call to pthread_attr_setstacksize()(assuming you are using pthreads).

对于进程,您可以通过ulimit命令(-s选项)控制进程的堆栈大小。对于线程,默认堆栈大小变化很大,但您可以通过调用来控制它pthread_attr_setstacksize()(假设您使用的是 pthreads)。

As for the interrupt using the userland stack, I somewhat doubt it, as accessing userland memory is a kind of a hassle from the kernel, especially from an interrupt routine. But I don't know for sure.

至于使用用户态堆栈的中断,我有点怀疑,因为访问用户态内存是内核的一种麻烦,尤其是中断例程。但我不确定。

回答by caf

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_SIZEmacro defines the (architecture-dependent) per-thread kernel stack size.

文档稀缺的原因是它是一个非常依赖于架构的领域。代码确实是最好的文档——例如,THREAD_SIZE宏定义了(依赖于架构的)每线程内核堆栈大小。

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

堆栈在alloc_thread_stack_node(). 中的堆栈指针在struct task_struct中更新dup_task_struct(),作为克隆线程的一部分被调用。

The kernel does check for kernel stack overflows, by placing a canary value STACK_END_MAGICat 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 handlerwhich prints the message Thread overran stack, or stack corruptedafter the Oops message if the stack canary has been clobbered.

内核通过在堆栈末尾放置一个 canary 值来STACK_END_MAGIC检查内核堆栈溢出。在页面错误处理程序中,如果内核空间中发生错误,则检查此金丝雀 - 例如请参见x86 错误处理程序Thread overran stack, or stack corrupted如果堆栈金丝雀已被破坏,它会在 Oops 消息之后打印消息。

Of course this won't trigger on allstack 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.

当然,这不会在所有堆栈溢出时触发,只有那些破坏堆栈金丝雀的溢出才会触发。但是,您应该始终能够从 Oops 输出中判断您是否遇到了堆栈溢出 - 如果堆栈指针低于task->stack

回答by Miles Rout

You can determine the process stack size with the ulimitcommand. I get 8192 KiB on my system:

您可以使用该ulimit命令确定进程堆栈大小。我的系统上有 8192 KiB:

$ ulimit -s
8192