我正在做一个Linux内核模块来清理进程缓存
以下是我用来做的代码。
static void clear_process_cache(struct task_struct *p)
{
struct mm_struct *mm;
struct vm_area_struct *vma;
struct page *page;
char *my_page_address;
unsigned long uaddr, paddr;
long res;
unsigned int level;
pte_t *pte;
mm = p->mm;
for (vma = mm->mmap; vma; vma = vma->vm_next) {
for(uaddr = vma->vm_start; uaddr < vma->vm_end; uaddr += PAGE_SIZE) {
down_read(&p->mm->mmap_sem);
res = get_user_pages(p, mm, uaddr, 1, 0, 1, &page, NULL);
if (res == 1) {
my_page_address = kmap(page);
paddr = (unsigned long)page_address(page);
pte = lookup_address(paddr, &level);
if (pte && (pte_val(*pte) &_PAGE_PRESENT)) {
clflush_cache_range(my_page_address, PAGE_SIZE);
}
kunmap(page);
put_page(page);
}
up_read(&p->mm->mmap_sem);
}
}
}
static void clear_process_cache(struct task_struct *p)
{
struct mm_struct *mm;
struct vm_area_struct *vma;
struct page *page;
char *my_page_address;
unsigned long uaddr, paddr;
long res;
unsigned int level;
pte_t *pte;
mm = p->mm;
for (vma = mm->mmap; vma; vma = vma->vm_next) {
for(uaddr = vma->vm_start; uaddr < vma->vm_end; uaddr += PAGE_SIZE) {
down_read(&p->mm->mmap_sem);
res = get_user_pages(p, mm, uaddr, 1, 0, 1, &page, NULL);
if (res == 1) {
my_page_address = kmap(page);
paddr = (unsigned long)page_address(page);
pte = lookup_address(paddr, &level);
if (pte && (pte_val(*pte) &_PAGE_PRESENT)) {
clflush_cache_range(my_page_address, PAGE_SIZE);
}
kunmap(page);
put_page(page);
}
up_read(&p->mm->mmap_sem);
}
}
}
当密集调用代码时,Linux内核崩溃了 我检查了我的代码,但无法找到导致内核崩溃的原因 你想帮忙吗,还是有其他高性能的方法吗?
这是崩溃转储。
[ 391.693385] general protection fault: 0000 [#1] SMP
[ 391.694435] Modules linked in: xt_CHECKSUM iptable_mangle ipt_MASQUERADE nf_nat_masquerade_ipv4 iptable_nat nf_nat_ipv4 nf_nat nf_conntrack_ipv4 nf_defrag_ipv4 xt_conntrack nf_conntrack ipt_REJECT nf_reject_ipv4 xt_tcpudp bridge stp llc ebtable_filter ebtables ip6table_filter ip6_tables iptable_filter ip_tables x_tables vmw_vsock_vmci_transport vsock kvm_intel kvm irqbypass vmw_balloon input_leds joydev serio_raw shpchp vmw_vmci i2c_piix4 mac_hid ib_iser rdma_cm iw_cm ib_cm ib_sa ib_mad ib_core ib_addr iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi autofs4 btrfs raid10 raid456 async_raid6_recov async_memcpy async_pq async_xor async_tx xor raid6_pq libcrc32c raid1 raid0 multipath linear crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel aes_x86_64 lrw gf128mul glue_helper vmwgfx ablk_helper
[ 391.702930] cryptd ttm drm_kms_helper syscopyarea psmouse sysfillrect pata_acpi sysimgblt mptspi fb_sys_fops mptscsih drm mptbase vmxnet3 scsi_transport_spi floppy fjes
[ 391.705034] CPU: 3 PID: 1716 Comm: java Not tainted 4.4.131 #4
[ 391.706080] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/28/2017
[ 391.708180] task: ffff88042607c600 ti: ffff8804292b8000 task.ti: ffff8804292b8000
[ 391.709244] RIP: 0010:[<ffffffff811a34dc>] [<ffffffff811a34dc>] put_compound_page+0x5c/0x1b0
[ 391.710358] RSP: 0000:ffff8804292bbcc8 EFLAGS: 00210202
[ 391.711439] RAX: 00d0a78b4c535441 RBX: ffffffff810dc4f9 RCX: 000507e043713000
[ 391.712523] RDX: ffff8804292bbd44 RSI: 000507e043713000 RDI: ffffffff810dc4f9
[ 391.713586] RBP: ffff8804292bbcd8 R08: ffff880002213cf0 R09: 00003ffffffff000
[ 391.714653] R10: 0000000000000080 R11: 0000000000000000 R12: 00d0a78b4c535440
[ 391.715712] R13: 0000160000000000 R14: ffff8804292bbd88 R15: ffffffff810dc4f9
[ 391.716764] FS: 00007fb138d5b700(0000) GS:ffff88042d6c0000(0000) knlGS:0000000000000000
[ 391.717829] CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
[ 391.718877] CR2: 0000000000000000 CR3: 00000000351d1000 CR4: 00000000001606f0
[ 391.719972] Stack:
[ 391.720993] ffffffff810dc4f9 ffff880000000000 ffff8804292bbcf0 ffffffff811a364d
[ 391.722055] ffff8804292bbdc8 ffff8804292bbdf8 ffffffff8102e21e ffff8804292bbd48
[ 391.723122] 0000000000000000 ffff88042607c600 ffff880429e6ac00 ffff880425e9f388
[ 391.724165] Call Trace:
[ 391.725190] [<ffffffff810dc4f9>] ? vprintk_default+0x29/0x40
[ 391.726222] [<ffffffff811a364d>] put_page+0x1d/0x50
[ 391.727259] [<ffffffff8102e21e>] clear_process_cache+0x11e/0x1f0
[ 391.728298] [<ffffffff810dc4f9>] ? vprintk_default+0x29/0x40
[ 391.729318] [<ffffffff811918d0>] ? printk+0x5a/0x76
[ 391.730328] [<ffffffff8102e93d>] do_signal+0x20d/0x770
[ 391.731310] [<ffffffff81193459>] ? unlock_page+0x69/0x70
[ 391.732297] [<ffffffff811972c0>] ? __probe_kernel_read+0x40/0x90
[ 391.733271] [<ffffffff8106d3c3>] ? bad_area+0x43/0x50
[ 391.734220] [<ffffffff810034fc>] exit_to_usermode_loop+0x8c/0xd0
[ 391.735143] [<ffffffff81003c26>] prepare_exit_to_usermode+0x26/0x30
[ 391.736062] [<ffffffff8185184e>] retint_user+0x8/0x34
[ 391.736941] Code: ff 5b 41 5c 5d c3 48 89 df e8 01 f6 ff ff 48 89 df 31 f6 e8 17 76 ff ff 5b 41 5c 5d c3 48 8b 47 20 4c 8d 60 ff a8 01 4c 0f 44 e7 <41> f6 44 24 08 01 74 08 49 8b 04 24 a8 80 74 1a 48 8b 43 20 a8
[ 391.739698] RIP [<ffffffff811a34dc>] put_compound_page+0x5c/0x1b0
[ 391.740571] RSP <ffff8804292bbcc8>