0

我在模块的 init 方法中分配并映射了 highmem 页面:

highmem_page = alloc_pages( GFP_HIGHUSER, 3 );
if ( ! highmem_page ) printk(KERN_ERR "Couldn't allocate highmem page with order : 3\n" );
  else {
    highmem_page_address = kmap( highmem_page );
    if ( ! highmem_page_address ) printk( KERN_ERR "Couldn't map highmem pages.\n");
    else {
      printk( KERN_ERR "Address for highuser pages is : %lx. Order: %d.\n", 
            highmem_page_address,
            3
           );
     }
  }

哪个运行良好并在日志中生成输出。通过dmesg

[4065.975025] highuser 页面的地址是:ffff88002c5d0000。订单:3。

现在,在退出时,我正在做这样的事情:

kunmap(highmem_page); //will unmap only ? can it free too? highly doubtful!
__free_pages( highmem_page, 3 ); //will free the pages?

我认为kunmap()应该从内核的虚拟地址空间中取消映射 highmem 页面。我的理解在这里吗?然后__free_pages()应该释放分配的页面。

但是,当我对其进行 rmmod 时,会 dmesg显示一些我没有预料到的内容:

[ 4109.529834] BUG: unable to handle kernel paging request at ffffeb880002c5dc
[ 4109.529845] IP: [<ffffffff8111e999>] __free_pages+0x9/0x40
[ 4109.529856] PGD 0 
[ 4109.529861] Oops: 0000 [#1] SMP 
[ 4109.529866] CPU 0 
[ 4109.529869] Modules linked in: prob4(O-) 
...
[ 4109.529943] 
[ 4109.529948] Pid: 5060, comm: rmmod Tainted: G           O 3.2.6 #4 TOSHIBA T20             /T20             
[ 4109.529956] RIP: 0010:[<ffffffff8111e999>]  [<ffffffff8111e999>] __free_pages+0x9/0x40
[ 4109.529963] RSP: 0018:ffff88002c5b5e78  EFLAGS: 00010216
[ 4109.529967] RAX: 000001880002c5c0 RBX: ffffea0000b17400 RCX: 0000000000000024
[ 4109.529970] RDX: 0000000000000000 RSI: 0000000000000006 RDI: ffffeb880002c5c0
[ 4109.529974] RBP: ffff88002c5b5e78 R08: 0000000000000000 R09: 0000000000000005
[ 4109.529978] R10: ffffea0000b18018 R11: 0000000000000000 R12: ffffffffa024f2f8
[ 4109.529981] R13: ffff88002c5b5f18 R14: 00007fffe3342480 R15: 0000000000000001
[ 4109.529986] FS:  00007f927f6d4700(0000) GS:ffff88005f400000(0000) knlGS:0000000000000000
[ 4109.529990] CS:  0010 DS: 0000 ES: 0000 CR0: 000000008005003b
[ 4109.529993] CR2: ffffeb880002c5dc CR3: 000000002c537000 CR4: 00000000000006f0
[ 4109.529997] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[ 4109.530000] DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
[ 4109.530005] Process rmmod (pid: 5060, threadinfo ffff88002c5b4000, task ffff88005b0416e0)
[ 4109.530008] Stack:
[ 4109.530010]  ffff88002c5b5ea8 ffffffff8111ea14 ffff88002c5b5e98 ffffffff00000006
[ 4109.530017]  ffff88002c5b5ec8 0000000000000007 ffff88002c5b5ec8 ffffffffa024d049
[ 4109.530024]  0000000000000880 ffffffffa024f000 ffff88002c5b5f78 ffffffff810a4cde
[ 4109.530031] Call Trace:
[ 4109.530037]  [<ffffffff8111ea14>] free_pages+0x44/0x50
[ 4109.530043]  [<ffffffffa024d049>] my_pager_exit+0x49/0x1000 [prob4]
[ 4109.530051]  [<ffffffff810a4cde>] sys_delete_module+0x19e/0x270
[ 4109.530059]  [<ffffffff81645102>] system_call_fastpath+0x16/0x1b
[ 4109.530062] Code: 04 25 d8 05 01 00 8b 4d b8 48 8b 55 c0 e9 fe fe ff ff 31 d2 48 89 de e8 06 c6 ff ff e9 6d ff ff ff 90 55 48 89 e5 66 66 66 66 90 <8b> 47 1c f0 ff 4f 1c 0f 94 c0 84 c0 74 09 85 f6 74 0d e8 60 d8 
[ 4109.530120] RIP  [<ffffffff8111e999>] __free_pages+0x9/0x40
[ 4109.530125]  RSP <ffff88002c5b5e78>
[ 4109.530127] CR2: ffffeb880002c5dc
[ 4109.530132] ---[ end trace 2dcb27dca5b2d882 ]---

ffffeb880002c5dc在我的代码中没有。为什么要去那里?

而现在,我该怎么办?

R# rmmod prob4
ERROR: Removing 'prob4': Device or resource busy

由于没有发生卸载。

那么,如何从 HIGHMEM 取消映射和释放分配的页面?

编辑:

顺便说一句,同样的问题在https://unix.stackexchange.com/questions/98498/how-to-unmap-and-free-the-highmem-pages上没有产生任何结果,这就是我在这里问它的原因。而且,这个问题包含与编程相关的实际错误。就这样。

4

1 回答 1

0

您分配多个连续的物理页面,然后只映射第一个。如果您的内核模块从其他两个模块访问内存,可能会发生奇怪的事情。

这通常在整个内核中完成的方式是,如果您需要多个页面,您将有一个循环,您可以在其中:

1. call alloc_page to get a physical page.
2. map it with kmap.

发布时,需要遍历所有页面并:

1. call kunmap.
2. call free_page.

您也可以考虑使用好的旧 kmalloc,如果您只需要少量内存,它可以节省一些麻烦。

于 2013-11-02T10:40:30.757 回答