Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

arm64: mm: Add double logical invert to pte accessors

Page table entries on ARM64 are 64 bits, and some pte functions such as
pte_dirty return a bitwise-and of a flag with the pte value. If the
flag to be tested resides in the upper 32 bits of the pte, then we run
into the danger of the result being dropped if downcast.

For example:
gather_stats(page, md, pte_dirty(*pte), 1);
where pte_dirty(*pte) is downcast to an int.

This patch adds a double logical invert to all the pte_ accessors to
ensure predictable downcasting.

Signed-off-by: Steve Capper <steve.capper@linaro.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>

authored by

Steve Capper and committed by
Catalin Marinas
84fe6826 e306dfd0

+5 -5
+5 -5
arch/arm64/include/asm/pgtable.h
··· 136 136 /* 137 137 * The following only work if pte_present(). Undefined behaviour otherwise. 138 138 */ 139 - #define pte_present(pte) (pte_val(pte) & (PTE_VALID | PTE_PROT_NONE)) 140 - #define pte_dirty(pte) (pte_val(pte) & PTE_DIRTY) 141 - #define pte_young(pte) (pte_val(pte) & PTE_AF) 142 - #define pte_special(pte) (pte_val(pte) & PTE_SPECIAL) 143 - #define pte_write(pte) (pte_val(pte) & PTE_WRITE) 139 + #define pte_present(pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) 140 + #define pte_dirty(pte) (!!(pte_val(pte) & PTE_DIRTY)) 141 + #define pte_young(pte) (!!(pte_val(pte) & PTE_AF)) 142 + #define pte_special(pte) (!!(pte_val(pte) & PTE_SPECIAL)) 143 + #define pte_write(pte) (!!(pte_val(pte) & PTE_WRITE)) 144 144 #define pte_exec(pte) (!(pte_val(pte) & PTE_UXN)) 145 145 146 146 #define pte_valid_user(pte) \