0

I'm writing a kernel module that works with page table entries, in order to implement a new page replacement policy. I want to get a list of page table entries owned by a specific process. So far I have retrieved a linked list of vma structures which basically carry information about pages owned by a process. There is a function called walk_page_vma which takes the pointer to the vma and gives back the page tables, it has been defined in mm/pagewalk.c also declared in linux/mm.h. Therefore, I have included linux/mm.h in my code.

    process_pid = -1;
    struct task_struct* task_list;
    size_t process_counter = 0;
    for_each_process(task_list) {
        if (strcmp(task_list->comm, process_name) == 0){
            process_pid = task_list->pid;

            pr_info("found %s pid = %d \n", process_name, process_pid);
            struct vm_area_struct *mmap = task_list->mm->mmap;
            while(mmap != NULL){
                struct mm_walk walk;
                int res = walk_page_vma(mmap, &walk);
                if (res == 0) {
                    printk("walked successfully\n");
                } else {
                    printk("failed to walk!\n");
                }
                mmap = mmap->vm_next;
            }
            // break;
        }
            // pr_info("== %s [%d]\n", task_list->comm, task_list->pid);
            ++process_counter;
    }

    if (process_pid){
       // pr_info("found %s pid = %d \n", process_name, process_pid);
    } else {
        pr_info("couldn't find %s pid. exiting! \n", process_name);
    }
    // printk(KERN_INFO "== Number of process: %zu\n", process_counter);

At building time, it throws a warning saying

WARNING: "walk_page_vma" [/home/myusername/Projects/ProjectModule/my_module.ko] undefined!

and is unable to load it when calling insmode.

Reza
  • 11
  • 3
  • What kernel version?!?! `walk_page_vma` does not exists for example in kernel [v.3.19.8](https://elixir.bootlin.com/linux/v3.19.8/ident/walk_page_vma). Does your code compile with no errors without calling `walk_page_vma`? – KamilCuk Jul 10 '18 at 14:07
  • 1
    @KamilCuk I'm working on [v.4.15.24](https://elixir.bootlin.com/linux/v4.15.2/ident/walk_page_vma) and it compiles with no errors. I was not aware of the **export** topic, and since `walk_page_vma` is not an _exported symbol_ , it cannot be accessed from a loadable module. – Reza Jul 10 '18 at 14:36

1 Answers1

2

walk_page_vma is not exported and so it can't be used in a dynamically loadable module. You must export it by patching the kernel (be aware that such a change will be refused by upstream devs) or compile your code as "built-in".

smeso
  • 4,165
  • 18
  • 27
  • Thanks for the explanation! I was not aware of *export*. First, I'll try to avoid patching the kernel; but if couldn't implement the needed function using exported ones, then I'll do the patch. you saved me a huge amount of time and effort. – Reza Jul 10 '18 at 14:42