1 year ago

#210923

test-img

CAO-Wuhui

How to find the virtual address of a page (including vmalloc situation) in linux kernel?

There is page_address() in linux kernel to get the virtual address of a page. However I find that it is not work when a page is allocted by vmalloc() in following demo.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/slab.h>

MODULE_LICENSE("GPL");\
char *p1,*p2;

static void check_vmalloc(const void *addr)
{
    if(is_vmalloc_addr(addr))
    {
        struct page *p=vmalloc_to_page(addr);
        printk("%lx from vmalloc. PFN %lu. VirtualAddr %lx",(unsigned long)addr,page_to_pfn(p),page_address(p));
        if(is_vmalloc_addr(page_address(p)))
        {
            printk("VirtualAddr %lx is vmalloc",page_address(p));
        }
        else
        {
            printk("VirtualAddr %lx is not vmalloc",page_address(p));
        }
    }
    else
    {
        printk("%lx not from vmalloc\n",(unsigned long)addr);
    }
}

static int __init lkm_example_init(void) {
    printk("pid %d\n",current->pid);

    p1=vmalloc(10);
    *p1=123;
    check_vmalloc(p1);

    p2=kmalloc(10,GFP_KERNEL);
    *p2=123;
    check_vmalloc(p2);
    
    return 0;
}

static void __exit lkm_example_exit(void) {
 printk("Goodbye\n");
 vfree(p1);
 kfree(p2);
}

module_init(lkm_example_init);
module_exit(lkm_example_exit);

output

So, if I want to find the virtual address of a page which is allocated by vmalloc(), what should I do? And could you tell me how to judge whether a page is allocated by vmalloc()?

c

memory

memory-management

linux-kernel

virtual-address-space

0 Answers

Your Answer

Accepted video resources