繁体   English   中英

刷新缓存和TLB不起作用:flush_cache_mm(mm)/ flush_tlb_mm(mm)

[英]Flushing the cache and TLB does not work : flush_cache_mm(mm)/flush_tlb_mm(mm)

清空缓存和TLB无效。 以下内核模块接收到一个pid,并使用该ID刷新进程的tlb / cache条目。

我在Linux内核中修改了handle_mm_fault()函数,并在其中添加了一行,以打印出pid和导致页面的地址if(current->pid == target_process) ....参见下面注释掉的行。 但是,当我运行测试时,没有看到由该过程引起的任何页面错误。 我正在使用在x86-64位处理器上运行的linux v3.11.6。

#include <linux/module.h>
#include <linux/mm.h>
#include <asm/tlbflush.h>
#include <asm/tlb.h>

static int pid = -1;
module_param(pid, int, 0444);

void do_init(void) {
    struct task_struct *task;
    struct vm_area_struct *next_vma;
    struct mm_struct *mm;

    if (pid == -1)
        return;

    task = pid_task(find_vpid(pid), PIDTYPE_PID);

    //target_process = pid;

    if (!task) {
        printk("Could not find the task struct for process id %d\n", pid);
        return;
    } else {
        printk("Found the task <%s>\n", task->comm);
    }

    mm = task->mm;

    if (!mm) {
        printk("Could not find the mmap struct for process id %d\n", pid);
        return;
    }

    printk("****Start_brk = %lu\n", mm->start_brk);
    down_read(&mm->mmap_sem);

    next_vma = find_vma(mm, mm->start_brk);
    flush_cache_mm(mm); 
    flush_tlb_mm(mm); 
    up_read(&mm->mmap_sem); 

}

int init_module(void) {
    printk("Inserting the module!!!\n");
    do_init();
    return 0;
}

void cleanup_module(void) {
    printk("Module removed!!!\n");
}

MODULE_LICENSE("GPL");

清空缓存和TLB无效。

实际上它可能会起作用,但是TLB刷新的效果不会出现页面错误。 刷新TLB后,来自目标进程的内存访问将导致TLB丢失和TLB重新填充。 在最常见的x86 / x86_64和更常见的ARM中, 内存管理单元将在硬件中重新填充TLB,而无需来自OS的活动代码。 在笔芯中,MMU将加载页表并进行遍历。 在更特殊的SPARC或MIPS中,tlb填充可以由软件完成,而不能由pagefault进行。

如果要在访问某些内存区域时强制页面错误,则可以(从用户空间)使用mprotect syscall( 检查其man ,只需在标志中设置无读取(PROT_READ)和写入(PROT_WRITE)访问权限。

如果要在内核空间中执行相同的操作,则可以检查mprotect的实现: SYSCALL_DEFINE3(mm / mprotect.c中的mprotect并执行相同的操作...

 unsigned long vm_flags = calc_vm_prot_bits(..flags..); // set flags
 down_write(&current->mm->mmap_sem); // lock mm
 vma = find_vma(current->mm, ..address..); // find needed VMA
 // check for errors...
 vma->vm_flags = newflags;
 vma->vm_page_prot = pgprot_modify(...) // update vm flags, or just call mprotect_fixup
 up_write(&current->mm->mmap_sem); // unlock mm

^^^该代码未经测试,无效,任何人都不应使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM