Linux Kernel Oops(Call Trace)由简单的kthread代码生成

时间:2017-08-21 19:09:59

标签: c linux-kernel

  

操作系统:Fedora 26 Alpha(服务器)

     

内核版本:Linux kerneldev 4.13.0-rc5#1 SMP Tue Aug 15 19:53:44 IST 2017 x86_64 x86_64 x86_64 GNU / Linux

[root@kerneldev thread_k]# cat kernel_t.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/sched.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Neeraj Pal <neerajpal09@gmail.com>");
MODULE_DESCRIPTION("Working Sample kernel Thread code");


static struct task_struct *t;
static int t_f(void *unused)
{
    while(1)
    {
        printk(KERN_ALERT "Hello world\n");
        ssleep(5);
    }

    printk(KERN_ALERT "Stopping thread\n");
    do_exit(0);
    return 0;
}

static int __init init_thread(void)
{
    printk(KERN_INFO "Thread creating ...\n");
    t = kthread_create(t_f,NULL,"mythread");
    if(t)
    {
        printk(KERN_INFO "Thread Created Sucessfully\n");
        wake_up_process(t);
    }
    else
    {
        printk(KERN_ALERT "Thread Creation Failed\n");
        return 0;
    }
}

static void __exit cleanup_thread(void)
{
    printk(KERN_INFO "Cleaning up ...\n");
}

module_init(init_thread)
module_exit(cleanup_thread)

内核日志:

  • insmod module_name

`

[root@kerneldev thread_k]# dmesg
[ 3983.796253] Disabling lock debugging due to kernel taint
[ 3983.796270] kernel_t: module verification failed: signature and/or required key missing - tainting kernel
[ 4731.985703] Thread creating ...
[ 4731.986047] Thread Created Sucessfully
[ 4755.392992] Cleaning up ...
[ 4965.415410] Thread creating ...
[ 4965.415905] Thread Created Sucessfully
[ 4965.415909] do_init_module: 'kernel_t'->init suspiciously returned 1, it should follow 0/-E convention
               do_init_module: loading module anyway...
[ 4965.415911] CPU: 1 PID: 4465 Comm: insmod Tainted: P           OE   4.13.0-rc5 #1
[ 4965.415912] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1.fc26 04/01/2014
[ 4965.415913] Call Trace:
[ 4965.415920]  dump_stack+0x63/0x8b
[ 4965.415923]  do_init_module+0x8d/0x1e9
[ 4965.415926]  load_module+0x21bd/0x2b10
[ 4965.415929]  SYSC_finit_module+0xfc/0x120
[ 4965.415931]  ? SYSC_finit_module+0xfc/0x120
[ 4965.415934]  SyS_finit_module+0xe/0x10
[ 4965.415937]  entry_SYSCALL_64_fastpath+0x1a/0xa5
[ 4965.415939] RIP: 0033:0x7fab36d717a9
[ 4965.415940] RSP: 002b:00007ffcbf3e9478 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[ 4965.415941] RAX: ffffffffffffffda RBX: 000000230f9fd980 RCX: 00007fab36d717a9
[ 4965.415942] RDX: 0000000000000000 RSI: 000000230f7f545c RDI: 0000000000000003
[ 4965.415943] RBP: 00007ffcbf3e9470 R08: 0000000000000000 R09: 00007fab37033e80
[ 4965.415943] R10: 0000000000000003 R11: 0000000000000246 R12: 000000231178d010
[ 4965.415944] R13: 000000231178e2f0 R14: 00007ffcbf3e9410 R15: 0000000000000006
[ 4965.416151] Hello world
[ 4970.523214] Hello world
[ 4975.643344] Hello world
[ 4980.763177] Hello world
[ 4985.883245] Hello world
[ 4991.003233] Hello world
[ 4996.123130] Hello world
[ 5001.243217] Hello world
[ 5006.363091] Hello world
[ 5011.483207] Hello world
[ 5016.603131] Hello world
[ 5021.723079] Hello world
[ 5026.843170] Hello world
[ 5031.963064] Hello world
[ 5037.083047] Hello world
[ 5042.203140] Hello world
[ 5047.323058] Hello world
[ 5052.443102] Hello world
[ 5057.563049] Hello world
[ 5062.683030] Hello world
[ 5067.803101] Hello world
[ 5072.923216] Hello world
[ 5078.043363] Hello world
[ 5083.163032] Hello world
[ 5088.282956] Hello world
[ 5093.403124] Hello world

`

  • rmmod module_name

`

[ 5093.987294] Cleaning up ...
[ 5098.522923] BUG: unable to handle kernel paging request at ffffffffc05eb023
[ 5098.522955] IP: 0xffffffffc05eb023
[ 5098.522979] PGD 21e0c067 
[ 5098.522980] P4D 21e0c067 
[ 5098.522989] PUD 21e0e067 
[ 5098.522997] PMD 31a5e067 
[ 5098.523006] PTE 0

[ 5098.523027] Oops: 0010 [#1] SMP
[ 5098.523037] Modules linked in: ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 xt_conntrack ip_set nfnetlink ebtable_nat ebtable_broute bridge stp llc ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_raw ip6table_security iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_raw iptable_security ebtable_filter ebtables ip6table_filter ip6_tables snd_hda_codec_generic snd_hda_intel snd_hda_codec snd_hda_core snd_hwdep snd_pcm crct10dif_pclmul snd_timer crc32_pclmul 8139too ppdev ghash_clmulni_intel snd parport_pc floppy 8139cp soundcore pcspkr i2c_piix4 parport mii virtio_balloon nfsd auth_rpcgss nfs_acl lockd grace sunrpc xfs virtio_console libcrc32c qxl crc32c_intel drm_kms_helper ttm serio_raw drm virtio_pci virtio_ring virtio
[ 5098.523454]  ata_generic pata_acpi qemu_fw_cfg [last unloaded: kernel_t]
[ 5098.523641] CPU: 0 PID: 4467 Comm: mythread Tainted: P           OE   4.13.0-rc5 #1
[ 5098.523822] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1.fc26 04/01/2014
[ 5098.524024] task: ffff9c69b1af2640 task.stack: ffffb45740338000
[ 5098.524206] RIP: 0010:0xffffffffc05eb023
[ 5098.524378] RSP: 0018:ffffb4574033bef8 EFLAGS: 00010246
[ 5098.524552] RAX: 0000000000000000 RBX: ffffffffc05ec024 RCX: 0000000000000000
[ 5098.524731] RDX: 0000000000000000 RSI: 0000000000000246 RDI: 0000000000000246
[ 5098.524936] RBP: ffffb4574033bf00 R08: 000000000000027e R09: ffff9c69b44300c0
[ 5098.525154] R10: ffffb4574033bee0 R11: 0000000000000281 R12: ffff9c69df7c61c0
[ 5098.525360] R13: ffffb45741e7fbc8 R14: 0000000000000000 R15: ffff9c69b1af2640
[ 5098.525568] FS:  0000000000000000(0000) GS:ffff9c69ffc00000(0000) knlGS:0000000000000000
[ 5098.525781] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5098.526002] CR2: ffffffffc05eb023 CR3: 000000005f5ac000 CR4: 00000000001406f0
[ 5098.526226] Call Trace:
[ 5098.526446]  kthread+0x125/0x140
[ 5098.526664]  ? kthread_park+0x60/0x60
[ 5098.526882]  ret_from_fork+0x25/0x30
[ 5098.527111] Code:  Bad RIP value.
[ 5098.527338] RIP: 0xffffffffc05eb023 RSP: ffffb4574033bef8
[ 5098.527564] CR2: ffffffffc05eb023
[ 5098.527787] ---[ end trace d4fb567953037a51 ]---

`

  

我觉得阅读和理解这个内核调用跟踪有点困难。

     

为什么Kernel Oops或内核恐慌,这是内核线程中的错误吗?

1 个答案:

答案 0 :(得分:1)

伙计,cmon,你是怎么编写这个的?

你的init函数没有成功案例的显式return语句并返回垃圾

退出时

只允许模块卸载,而您创建的线程则保持不变。然后当它执行的代码被删除(在卸载模块之后),你得到了崩溃

线程中的循环是while (1),这在设计上是错误的:没有办法阻止它。并且它绝对不是内核线程应该如何运作。

我强烈建议您使用用户空间的基础知识