当char dev打开时rmmod

时间:2017-06-27 09:35:52

标签: c++ c linux-kernel

当设备在文件描述符(另一个C ++程序)中打开时,我尝试使用rmmod删除内核模块。当我关闭C ++程序时,我有这个:

Unable to handle kernel paging request at virtual address 7f023588
pgd = 80004000
[7f023588] *pgd=388aa811, *pte=00000000, *ppte=00000000
Internal error: Oops: 7 [#2] PREEMPT SMP ARM
Modules linked in: [last unloaded: dev]
CPU: 2 PID: 1272 Comm: helloWorld Tainted: P      D W  O    4.1.38-fslc+gee67fc7 #4
Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree)
task: a8c42d00 ti: a89f8000 task.ti: a89f8000
PC is at filp_close+0x2c/0x8c
LR is at put_files_struct+0xb4/0x10c
pc : [<8013266c>]    lr : [<80150f28>]    psr: 20070113
sp : a89f9dc0  ip : a89f9de0  fp : a89f9ddc
r10: a89f9edc  r9 : a8ada8c0  r8 : 00000000
r7 : a89f4d00  r6 : a89f4d00  r5 : a8832f00  r4 : 00000001
r3 : 7f023554  r2 : a8832f00  r1 : a89f4d00  r0 : a8832f00
Flags: nzCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment user
Control: 10c53c7d  Table: 38a0404a  DAC: 00000015
Process helloWorld (pid: 1272, stack limit = 0xa89f8210)
Stack: (0xa89f9dc0 to 0xa89fa000)

有可能解决这个问题吗?怎么样?

1 个答案:

答案 0 :(得分:2)

在打开文件时阻止内核模块卸载的正确方法是设置.owner结构的file_operations字段:

static struct file_operations my_ops = {
    .owner = THIS_MODULE,
    //...
};

注意:在try_module_get(THIS_MODULE)之前立即卸载模块时,module_put(THIS_MODULE)try_module_get()的手动使用受竞争条件的约束电话,所以进一步的代码可以取消映射。