change page order in kernel space

58 Views Asked by At

I have a kernel module that works on data that is:

  • allocated by the kernel
  • page aligned
  • the data "mapping" is arbitrary

I allocate the memory in kernel space with kvmalloc(). For userspace representation i use vm_insert_page() to create the correct ordered representation. But i could not find a method with that i can "insert" or "remap" or "reorder" page mapping within kernel space. Are there methods do the same as vm_insert_page() for kernelspace mappings?

1

There are 1 best solutions below

0
eddy On BEST ANSWER

ok this seems to work:

static int __init test_init_fs(void)
{
    int rv = 0;

    size_t size = 5*1024*1024; /* 5 MiB*/
    void* mem = vzalloc(size);

    struct page **pages = kcalloc(5, sizeof(struct page *), GFP_KERNEL);

    pr_info("alloced\n");
    
    pages[0] = vmalloc_to_page(mem + 0 * PAGE_SIZE);
    pages[1] = vmalloc_to_page(mem + 6 * PAGE_SIZE);
    pages[2] = vmalloc_to_page(mem + 2 * PAGE_SIZE);
    pages[3] = vmalloc_to_page(mem + 1 * PAGE_SIZE);
    pages[4] = vmalloc_to_page(mem + 8 * PAGE_SIZE);

    pr_info("got all pages\n");

    void* new_mapping = vmap(pages,5, VM_MAP, PAGE_KERNEL);

    pr_info("new mapping created\n");

    void* buffer = vzalloc(5*PAGE_SIZE);

    memcpy(buffer,new_mapping,5*PAGE_SIZE);

    vunmap(new_mapping);
    
    pr_info("unmapped\n");

    vfree(mem);

    return rv;
}