c - 由简单的 kthread 代码产生的 Linux 内核 Oops(调用跟踪)

标签 c linux-kernel

Operating System: Fedora 26 Alpha (Server)

Kernel Version: 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 模块名称

`

[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 模块名称

`

[ 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 ]---

`

I feel little bit difficulty in reading and understanding this kernel call trace.

Why Kernel Oops or kernel panic here, is it a fault during kernel threading ?

最佳答案

伙计,来吧,你首先是如何编译这个的?

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

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

线程中的循环是while (1),这是设计错误的:没有办法停止它。这绝对不是内核线程的运行方式。

我强烈建议你在用户空间学习基础知识

关于c - 由简单的 kthread 代码产生的 Linux 内核 Oops(调用跟踪),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804083/

相关文章:

c - Sudoku C 递归函数无法正常工作

c - 如何在 Linux 内核中跟踪传出 ICMP 数据包的代码路径?

Linux内核模块相关的理解?

linux - Docker: "From"指令的好处?

c - 如何从C中的行中获取第一个和最后一个单词?

C,尽管有互斥锁,但在循环中初始化的 pthreads 没有正确执行分配的函数

c - 在txt文件中查找字符串并显示一行

c - 错误段错误(核心转储)

linux - 当 linux 内核崩溃时,内核堆栈和调用跟踪如何记录到控制台?

linux - 在 linux 编译中错过 asm/types.h