/*
  * Lookup the page table entry for a virtual address in a specific pgd.
- * Return a pointer to the entry, the level of the mapping, and the effective
- * NX and RW bits of all page table levels.
+ * Return a pointer to the entry (or NULL if the entry does not exist),
+ * the level of the entry, and the effective NX and RW bits of all
+ * page table levels.
  */
 pte_t *lookup_address_in_pgd_attr(pgd_t *pgd, unsigned long address,
                                  unsigned int *level, bool *nx, bool *rw)
        pud_t *pud;
        pmd_t *pmd;
 
-       *level = PG_LEVEL_NONE;
+       *level = PG_LEVEL_256T;
        *nx = false;
        *rw = true;
 
        if (pgd_none(*pgd))
                return NULL;
 
+       *level = PG_LEVEL_512G;
        *nx |= pgd_flags(*pgd) & _PAGE_NX;
        *rw &= pgd_flags(*pgd) & _PAGE_RW;
 
        if (p4d_none(*p4d))
                return NULL;
 
-       *level = PG_LEVEL_512G;
        if (p4d_leaf(*p4d) || !p4d_present(*p4d))
                return (pte_t *)p4d;
 
+       *level = PG_LEVEL_1G;
        *nx |= p4d_flags(*p4d) & _PAGE_NX;
        *rw &= p4d_flags(*p4d) & _PAGE_RW;
 
        if (pud_none(*pud))
                return NULL;
 
-       *level = PG_LEVEL_1G;
        if (pud_leaf(*pud) || !pud_present(*pud))
                return (pte_t *)pud;
 
+       *level = PG_LEVEL_2M;
        *nx |= pud_flags(*pud) & _PAGE_NX;
        *rw &= pud_flags(*pud) & _PAGE_RW;
 
        if (pmd_none(*pmd))
                return NULL;
 
-       *level = PG_LEVEL_2M;
        if (pmd_leaf(*pmd) || !pmd_present(*pmd))
                return (pte_t *)pmd;
 
+       *level = PG_LEVEL_4K;
        *nx |= pmd_flags(*pmd) & _PAGE_NX;
        *rw &= pmd_flags(*pmd) & _PAGE_RW;
 
-       *level = PG_LEVEL_4K;
-
        return pte_offset_kernel(pmd, address);
 }
 
  * Lookup the page table entry for a virtual address. Return a pointer
  * to the entry and the level of the mapping.
  *
- * Note: We return pud and pmd either when the entry is marked large
- * or when the present bit is not set. Otherwise we would return a
- * pointer to a nonexisting mapping.
+ * Note: the function returns p4d, pud or pmd either when the entry is marked
+ * large or when the present bit is not set. Otherwise it returns NULL.
  */
 pte_t *lookup_address(unsigned long address, unsigned int *level)
 {