2

我能够 insmod hello.ko 没有任何错误消息。但我的理解是不应该工作,因为 linux 内核堆栈只有 4KB 大小。看来我的理解有些差距。谁能帮我弄清楚我错过了什么?

#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");


int popo()
{
    int a[10*1024*1024]={0};
            printk(KERN_WARNING "popo\n");
    }
    static int __init my_init()
    {
      printk(KERN_WARNING "Hello world!\n");
  popo();
  return 0;
}

static void my_exit()
{
  printk(KERN_WARNING "bye!\n");
}

module_init(my_init);
module_exit(my_exit);
4

1 回答 1

2

@Eugene:确实。我将本地缓冲区大小设置为 10240 字节,然后 memset() 它;在“制作”时收到警告:

...
<...>/kstk.c: In function ‘func’:
<...>/kstk.c:14:1: warning: the frame size of 10244 bytes is larger than 1024 bytes [-Wframe-larger-than=]
  Building modules, stage 2.
Building with KERNELRELEASE = 3.2.0-30-generic-pae
...

gcc 发出“Wframe-larger-than=”警告;但这只是一个警告!

从 gcc 手册页:

" -Wframe-larger-than=len
           Warn if the size of a function frame is larger than len bytes.  The computation done to determine the stack frame size is approximate
           and not conservative.  The actual requirements may be somewhat greater than len even if you do not get a warning.  In addition, any
           space allocated via "alloca", variable-length arrays, or related constructs is not included by the compiler when determining whether or
           not to issue a warning.
"

然后我为 ARM 编译了 ko 并运行了一个模拟的 QEMU/ARM VM。猜猜看:它惊人地崩溃了!有许多 printk 的:第一和最后几行 printk 复制如下:

ARM /myprj $ insmod ./kstk.ko 
[   13.967453] All okay!
[   13.967508] Hello, world
[   13.971181] Unable to handle kernel paging request at virtual address 0d890cc0
[   13.972465] pgd = 80004000
[   13.972966] [0d890cc0] *pgd=00000000
[   13.973831] Internal error: Oops: 5 [#1] SMP
[   13.974649] Modules linked in: kstk(O)
[   13.975656] CPU: 0    Tainted: G           O  (3.2.21 #1)
[   13.976893] PC is at do_page_fault+0xbc/0x3c4
[   13.977648] LR is at do_page_fault+0x74/0x3c4
[   13.978373] pc : [<804165a4>]    lr : [<8041655c>]    psr: 20000193
[   13.978386] sp : 8e056058  ip : 005e4312  fp : 8e0560e4
[   13.980465] r10: 0d890c00  r9 : 2ab2906c  r8 : 8e056058
[   13.981366] r7 : 00000005  r6 : 0d890cc0  r5 : 8e056018  r4 : 8e0561b0
[   13.982471] r3 : 20000193  r2 : 005e4311  r1 : 8e056000  r0 : 8e056000
[   13.983608] Flags: nzCv  IRQs off  FIQs on  Mode SVC_32  ISA ARM  Segment kernel
[   13.984836] Control: 10c53c7d  Table: 6e05006a  DAC: 00000015
[   13.985768] Process nfsiod (pid: 342, stack limit = 0x8e0542f0)
[   13.986753] Stack: (0x8e056058 to 0x8e056000)
[   13.987920] [<804165a4>] (do_page_fault+0xbc/0x3c4) from [<80416958>] (do_translation_fault+0xac/0xb0)
[   13.989377] [<80416958>] (do_translation_fault+0xac/0xb0) from [<800084b8>] (do_DataAbort+0x48/0xac)
[   13.991682] [<800084b8>] (do_DataAbort+0x48/0xac) from [<8041495c>] (__dabt_svc+0x3c/0x60)
[   13.992351] Exception stack(0x8e0561b0 to 0x8e0561f8)
[   13.992850] 61a0:                                     8e056000 8e056000 005e4311 20000193
[   13.993863] 61c0: 8e056350 8e056038 0d890cc0 00000005 8e0561f8 2ab2906c 0d890c00 8e056284
[   13.994673] 61e0: 005e4312 8e0561f8 8041655c 804165a4 20000193 ffffffff

--snip--

       17.321313] Exception stack(0x8e055d80 to 0x8e055dc8)
    [   17.321962] 5d80: 00000004 00000000 00000003 00000002 8f943c00 00000040 8420804c 84208044
    [   17.322979] 5da0: 00000001 8f943cf8 8e055e24 8e055e5c 8e055d28 8e055e08 00000000 80038024
    [   17.323989] 5dc0: 60000113 ffffffff
    [   17.324468] [<80414a6c>] (__und_svc+0x4c/0x80) from [<80038024>] (do_exit+0x4d0/0x6ec)
    [   17.325468] [<80038024>] (do_exit+0x4d0/0x6ec) from [<800141b0>] (die+0x144/0x1f8)
    [   17.326417] [<800141b0>] (die+0x144/0x1f8) from [<8001cd7c>] (__do_kernel_fault+0x84/0x94)
    [   17.327447] [<8001cd7c>] (__do_kernel_fault+0x84/0x94) from [<804165ec>] (do_page_fault+0x104/0x3c4)
    [   17.328577] [<804165ec>] (do_page_fault+0x104/0x3c4) from [<80416958>] (do_translation_fault+0xac/0xb0)
    [   17.329863] [<80416958>] (do_translation_fault+0xac/0xb0) from [<800084b8>] (do_DataAbort+0x48/0xac)
    [   17.331175] Code: e24cb004 e92d4000 e8bd4000 e5901278 (e5914008) 
    [   17.331939] ---[ end trace f1c8dff59d91e3cd ]---
    [   17.332521] Fixing recursive fault but reboot is needed!
    [   17.333493] BUG: scheduling while atomic: insmod/476/0x00000003
    [   17.334656] Modules linked in: kstk(O)
    [   17.335455] 
    [   17.335779] Pid: 342, comm:               nfsiod
    [   17.336706] CPU: 0    Tainted: G      D    O  (3.2.21 #1)
    [   17.337778] PC is at 0x2ad4ff68
    [   17.338415] LR is at 0x2abca3a0
    [   17.339060] pc : [<2ad4ff68>]    lr : [<2abca3a0>]    psr: 20000010
    [   17.339081] sp : 7e93cc38  ip : 00000000  fp : 7e93ccbc
    [   17.341184] r10: 40000000  r9 : 2ab2906c  r8 : 00000001
    [   17.341682] r7 : 2aae2650  r6 : 2ab28e00  r5 : 000000c0  r4 : 2ac68000
    [   17.342743] r3 : fffffff8  r2 : 2adade14  r1 : 2adade04  r0 : 2ac68090
    [   17.344019] Flags: nzCv  IRQs on  FIQs on  Mode USER_32  ISA ARM  Segment user
    [   17.345280] Control: 10c53c7d  Table: 6e05006a  DAC: 00000015

道德:开发人员必须注意所有警告!:)

于 2012-09-21T03:42:06.793 回答