0

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?

eddy
  • 488
  • 5
  • 18
  • so after hours of searching i think that `vm_map_ram` might be a possibility reorder .. or better to create a new ordered mapping of the memory – eddy Aug 29 '22 at 19:49
  • note to myself for future tryout: 1. use `vmalloc_to_page` or `vmalloc_to_pfn` get either the pages or the pfn of the "old" mapping. 2. reorder the pages or pfns. 3. then use `vm_map_ram` `vmap` or `vmap_pfn` to create the new mapping. at least – eddy Nov 17 '22 at 09:48
  • another note to myself: https://github.com/torvalds/linux/blob/master/arch/x86/hyperv/ivm.c#L366 try this when you have time – eddy Dec 20 '22 at 19:14

1 Answers1

0

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;
}
eddy
  • 488
  • 5
  • 18