c - 奇怪的 printk 对 linux 堆栈大小测试的影响

标签 c linux linux-kernel printk

I am trying to test linux kernel stack size in 64 bit. 

我发现了这种奇怪的行为。 我写了下面的代码来使内核崩溃,但奇怪的是 只有当 printk 被取消注释时它才会崩溃, 否则运行良好,没有错误/警告!

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/kernel.h>

    static int __init crash_stack_init(void)
    {
        long arr[1024];
        long *a;

        a = &arr[0];

        //printk("%p\n", &arr[0]);

        return 0;
    }

        enter code here

    static void __exit crash_stack_exit(void)
    {
    }

    module_init(crash_stack_init);
    module_exit(crash_stack_exit);


Here is the "make" output without the printk,

make -C /lib/modules/4.4.0-53-generic/build M=/home/naveenvc/work/ker/crash_stack modules make[1]: Entering directory '/usr/src/linux-headers-4.4.0-53-generic' CC [M] /home/naveenvc/work/ker/crash_stack/crash_stack.o Building modules, stage 2. MODPOST 1 modules CC /home/naveenvc/work/ker/crash_stack/crash_stack.mod.o LD [M] /home/naveenvc/work/ker/crash_stack/crash_stack.ko make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-53-generic'

And make output with printk,

make -C /lib/modules/4.4.0-53-generic/build M=/home/naveenvc/work/ker/crash_stack modules make[1]: Entering directory '/usr/src/linux-headers-4.4.0-53-generic' CC [M] /home/naveenvc/work/ker/crash_stack/crash_stack.o > /home/naveenvc/work/ker/crash_stack/crash_stack.c: In function ‘crash_stack_init’: /home/naveenvc/work/ker/crash_stack/crash_stack.c:14:1: warning: the frame size of 8200 bytes is larger than 1024 bytes [-Wframe-larger-than=] } ^
Building modules, stage 2. MODPOST 1 modules CC
/home/naveenvc/work/ker/crash_stack/crash_stack.mod.o LD [M] /home/naveenvc/work/ker/crash_stack/crash_stack.ko make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-53-generic'

这可能是什么原因造成的?

最佳答案

堆栈大小已被详细记录,上面不是正确的测试方法,特别是在使用大页面支持堆栈的旧内核中,上述代码会跳转到下一个堆栈。

注释掉 prink 的 func __crash_stack_init 是一个叶函数——它不调用任何东西,所以编译器确切地知道局部变量会发生什么。特别是在这段代码中,它发现不需要完整的数组,因此它没有被分配。然而,对 printk 的调用传递了 arr。编译器不知道 func 将如何处理它,因此它必须在堆栈上保留 1024 * sizeof(long),这会导致警告。

堆栈在几年前达到了 16KB,您可以从这里开始阅读 https://lwn.net/Articles/600644/

关于c - 奇怪的 printk 对 linux 堆栈大小测试的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47733342/

相关文章:

c - 为什么我会在这个 C 程序中得到这个运行时错误?请告诉我哪里出了问题

linux - 嵌入式linux如何通过usb gadget检测文件被修改&更新文件

linux - Linux/GTK 上存在哪些应用程序帮助系统(如 chm 文件)?

sockets - 套接字对和成对的未命名管道之间有什么区别吗?

c++ - 循环不停止!? (C++编程)

c - 我不明白 execlp() 在 Linux 中的工作原理

c - 从 argv[] 获取特定的字符值并创建一个新的 const char

java - 在 Cubieboard 平台内运行的 Java 代码中执行终端命令获取输出

c - Linux 内核 splice() 是零拷贝吗?

linux - kmalloc 中 GFP_USER 标志的用途是什么?