我实现了一个内核函数,我遇到内核恐慌问题,显示页面分配失败或oom-manager被调用有时会在我尝试运行我的函数非常多次时杀死进程(否则它很好)。为了调试这个,我开始编写一个虚拟程序,它最小化了我的实际程序。我的虚拟程序打开并关闭一个文件(就是这样!)。但是当我的模块加载如下时,我正在运行这个函数很多次
static int dummy(void)
{
struct file *fd;
mm_segment_t fs;
fs = get_fs();
set_fs(KERNEL_DS);
fd = filp_open(“x/y/z/a.txt”,O_RDONLY,0);
if(!fd || IS_ERR(fd)){
return -1;
}
//close file
filp_close(fd,0);
set_fs(fs);
return 0;
}
//我的内核模块加载时调用的init模块函数
static int dummy_init(void)
{
int i, ret;
for(i=0;i < 10000000;++i) {
ret = dummy();
if(ret == -1)
return 0;
}
return 0;
}
现在当我执行上面的代码时,我有“页面分配失败”或者有时会调用oom-manager并且它会终止进程。当我试图减少执行时 从10 ^ 7到10 ^ 6没有问题。所以这就是问题..对于每次迭代,只需打开一个文件并在函数终止之前使用filp_close()关闭它。所以我不明白是什么问题。此外,我看到VFS:file-max limit警告,因此我将限制增加到非常大的数量。虽然我每次都关闭文件,但为什么达到文件最大限制也没有意义。
下面是堆栈跟踪。
[ 609.118491] vmtoolsd invoked oom-killer: gfp_mask=0x201da, order=0, oom_score_adj=0
[ 609.120014] vmtoolsd cpuset=session-c2.scope mems_allowed=0
[ 609.121145] CPU: 0 PID: 2194 Comm: vmtoolsd Tainted: GF O 3.10.87 #2
[ 609.122574] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/20/2014
[ 609.124694] 0000000000000000 ffff88002f287978 ffffffff816b4c84 ffff88002f2879c0
[ 609.126353] ffffffff816b376a 0000000000000206 0000000000000001 ffff8800359e8000
[ 609.127952] 0000000000000000 000000000007d336 0000000000000036 00000000000201da
[ 609.129549] Call Trace:
[ 609.130146] [<ffffffff816b4c84>] dump_stack+0x19/0x1b
[ 609.131162] [<ffffffff816b376a>] dump_header.isra.9+0x7b/0x1a2
[ 609.132323] [<ffffffff8113884d>] oom_kill_process+0x1dd/0x330
[ 609.133471] [<ffffffff812a9105>] ? security_capable_noaudit+0x15/0x20
[ 609.134751] [<ffffffff81138fc4>] out_of_memory+0x464/0x4b0
[ 609.135847] [<ffffffff8113edf8>] __alloc_pages_nodemask+0x9d8/0xa20
[ 609.137097] [<ffffffff8117909e>] alloc_pages_current+0xae/0x160
[ 609.138257] [<ffffffff81135367>] __page_cache_alloc+0x97/0xb0
[ 609.139436] [<ffffffff81136dc4>] filemap_fault+0x174/0x3c0
[ 609.140528] [<ffffffff8115953c>] __do_fault+0x6c/0x620
[ 609.141552] [<ffffffff8115cd34>] handle_pte_fault+0x444/0x940
[ 609.142696] [<ffffffff8115e0fe>] handle_mm_fault+0x2fe/0x690
[ 609.144058] [<ffffffff811a8b00>] ? poll_schedule_timeout+0x70/0x70
[ 609.145878] [<ffffffff816be872>] __do_page_fault+0x1a2/0x580
[ 609.147099] [<ffffffff8108e955>] ? task_sched_runtime+0x55/0xa0
[ 609.148574] [<ffffffff81092185>] ? cputime_adjust+0x25/0x110
[ 609.149878] [<ffffffff81092841>] ? thread_group_cputime_adjusted+0x41/0x50
[ 609.151458] [<ffffffff81019ce9>] ? read_tsc+0x9/0x20
[ 609.152613] [<ffffffff816bec7c>] do_page_fault+0x2c/0x50
[ 609.153860] [<ffffffff816baef8>] page_fault+0x28/0x30
[ 609.155025] Mem-Info:
[ 609.155532] Node 0 DMA per-cpu:
[ 609.156268] CPU 0: hi: 0, btch: 1 usd: 0
[ 609.157309] Node 0 DMA32 per-cpu:
[ 609.158063] CPU 0: hi: 186, btch: 31 usd: 54
[ 609.159030] active_anon:0 inactive_anon:0 isolated_anon:0
[ 609.159030] active_file:19 inactive_file:81 isolated_file:0
[ 609.159030] unevictable:8 dirty:0 writeback:7 unstable:0
[ 609.159030] free:11595 slab_reclaimable:4672 slab_unreclaimable:216463
[ 609.159030] mapped:10 shmem:8 pagetables:7505 bounce:0
[ 609.159030] free_cma:0
[ 609.165085] Node 0 DMA free:4108kB min:788kB low:984kB high:1180kB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:15988kB managed:15904kB mlocked:0kB dirty:0kB writeback:0kB mapped:0kB shmem:0kB slab_reclaimable:72kB slab_unreclaimable:11668kB kernel_stack:0kB pagetables:12kB unstable:0kB bounce:0kB free_cma:0kB writeback_tmp:0kB pages_scanned:0 all_unreclaimable? yes
[ 609.173628] lowmem_reserve[]: 0 832 832 832
[ 609.174687] Node 0 DMA32 free:42272kB min:42340kB low:52924kB high:63508kB active_anon:0kB inactive_anon:0kB active_file:76kB inactive_file:324kB unevictable:32kB isolated(anon):0kB isolated(file):0kB present:1032064kB managed:852376kB mlocked:32kB dirty:0kB writeback:28kB mapped:40kB shmem:32kB slab_reclaimable:18616kB slab_unreclaimable:854184kB kernel_stack:6048kB pagetables:30008kB unstable:0kB bounce:0kB free_cma:0kB writeback_tmp:0kB pages_scanned:120 all_unreclaimable? yes
[ 609.182737] lowmem_reserve[]: 0 0 0 0
[ 609.183616] Node 0 DMA: 7*4kB (UM) 6*8kB (U) 12*16kB (UM) 6*32kB (UM) 9*64kB (M) 6*128kB (UM) 1*256kB (U) 0*512kB 0*1024kB 1*2048kB (R) 0*4096kB = 4108kB
[ 609.186932] Node 0 DMA32: 1988*4kB (UEM) 3018*8kB (UEM) 379*16kB (M) 0*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 1*4096kB (R) = 42256kB
[ 609.190003] Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
[ 609.191646] 128 total pagecache pages
[ 609.192376] 5 pages in swap cache
[ 609.193043] Swap cache stats: add 125434, delete 125429, find 2279/2765
[ 609.194314] Free swap = 565836kB
[ 609.194965] Total swap = 1046524kB
[ 609.197207] 262143 pages RAM
[ 609.197816] 11016 pages reserved
[ 609.198461] 263966 pages shared
[ 609.199090] 237462 pages non-shared