2

I am doing a page table walk. When I am getting ready to update the kernel I get an error:

kernel/sys.c: In function ‘__do_sys_get_page_info’:
kernel/sys.c:2745:23: error: passing argument 1 of ‘pud_offset’ from incompatible pointer type [-Werror=incompatible-pointer-types]
      pud = pud_offset(pgd, vmpage);
                       ^
In file included from ./include/linux/mm.h:99:0,
                 from kernel/sys.c:19:
./arch/x86/include/asm/pgtable.h:905:22: note: expected ‘p4d_t * {aka struct <anonymous> *}’ but argument is of type ‘pgd_t * {aka struct <anonymous> *}’
 static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
                      ^

This is where the code is being used:

....
int loc, ref, dirty;
struct vm_area_struct *vma;
unsigned long vmpage;
struct mm_struct *task_mm = task->mm;
if ((task_mm && task_mm->mmap))
{
    int i;
    pgd_t *pgd;
    pud_t *pud;
    pmd_t *pmd;
    pte_t *ptep, pte;

    vma = task_mm->mmap;

    while (vma)
    {
        for (vmpage = vma->vm_start, i = 1; vmpage < vma->vm_end; vmpage += PAGE_SIZE, i++)
        {
            pgd = pgd_offset(task_mm, vmpage);
            if (pgd_none(*pgd) || pgd_bad(*pgd))
                return 0;

            pud = pud_offset(pgd, vmpage);
            if (pud_none(*pud) || pud_bad(*pud))
                return 0;

            pmd = pmd_offset(pud, vmpage);
            if (pmd_none(*pmd) || pmd_bad(*pmd))
                return 0;

            ptep = pte_offset_kernel(pmd, vmpage);
            if (!ptep)
                return 0;

            pte = *ptep;
            ...
}

I looked up the errors and notes but didn't find anything about resolving this problem. Is this a well known problem or am I doing something wrong?

updated*

Jinzu
  • 1,325
  • 2
  • 10
  • 22
  • What is the type of `vmpage` variable? Looks like the width of this type is less then width of the address type. – Tsyvarev Nov 07 '19 at 12:04
  • I updated the code also I added the other warning I got. Warning about `vmpage` being unused. But I don't see how I haven't used it. – Jinzu Nov 07 '19 at 16:52
  • Got it. Exression `int i = 1, vmpage = vma->vm_start;` actually **defines two variables** of type `int`: `i` and `vmpage`. This declaration hides declaration of `vmpage` before the `for` loop. This is why you get warning `unused variable ‘vmpage’` and warnings about "shift counts". – Tsyvarev Nov 07 '19 at 20:32
  • I edited the code. It is saying error because of the `pud_offset` function. – Jinzu Nov 08 '19 at 17:20

1 Answers1

4

I recently got the same problem and I found that just like pgd_offset and pud_offset, there is a p4d_offset. Put it between pgd and pud:

pgd_t *pgd;
p4d_t* p4d;
pud_t *pud;
pmd_t *pmd;
pte_t *ptep, pte;

...

pgd = pgd_offset(task_mm, vmpage);
if (pgd_none(*pgd) || pgd_bad(*pgd))
    return 0;

p4d = p4d_offset(pgd, vmpage);
if (p4d_none(*p4d) || p4d_bad(*p4d))
    return 0;

pud = pud_offset(p4d, vmpage);
if (pud_none(*pud) || pud_bad(*pud))
    return 0;

...

Edit: Here is some information about the additional level: Five-level page tables.

It has been implemented in kernel version 4.11.

Jimbei
  • 395
  • 3
  • 11