如何取消映射和释放highmem页面

时间:2013-11-02 01:32:45

标签: linux linux-kernel module

我在模块的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]高用户页面的地址是: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()应该释放分配的页面。

然而,当我尝试这个时,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 ]---

现在,我该怎么办?

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

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

1 个答案:

答案 0 :(得分:0)

kmap 仅映射其结构正在传递的一个页面。但是,程序应该正确释放页面,因为_free_pages()释放了通过alloc_pages()分配的所有页面。尝试编写基本功能模块,仅分配一个页面(订单1),然后释放它。如果这样做,那么在模块的某个地方,代码就会尝试访问未映射的页面。