arm64: Further fixes to the linear address checking
Subsystems (2)
Catalin Marinas
maintainercover arm64: Further fixes to the linear address checking
There some history to virt_addr_valid() that goes back to 5.4. It seems to have been ok prior to this kernel, however, the VA reshuffling in commit 14c127c957c1 ("arm64: mm: Flip kernel VA space") broke it. The subsequent fix 68dd8ef32162 ("arm64: memory: Fix virt_addr_valid() using __is_lm_address()") partially addressed it but it left virt_addr_valid() returning true for user addresses (or NULL).
The recent commit 519ea6f1c82f ("arm64: Fix kernel address detection of __is_lm_address()") fixed the NULL checking but broke the virt_addr_valid() macro for tagged addresses.
The first patch attempts to fix virt_addr_valid() macro for tagged addresses. The second patch replaces the bitwise operations with a subtract. The resulting image is slightly smaller.
Catalin Marinas (2):
arm64: Do not pass tagged addresses to __is_lm_address()
arm64: Use simpler arithmetics for the linear map macros
arch/arm64/include/asm/memory.h | 6 +++--- arch/arm64/mm/physaddr.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Catalin Marinas
maintainerPATCH 1/2 arm64: Do not pass tagged addresses to __is_lm_address()
Commit 519ea6f1c82f ("arm64: Fix kernel address detection of __is_lm_address()") fixed the incorrect validation of addresses below PAGE_OFFSET. However, it no longer allowed tagged addresses to be passed to virt_addr_valid().
Fix this by explicitly resetting the pointer tag prior to invoking __is_lm_address(). This is consistent with the __lm_to_phys() macro.
--- arch/arm64/include/asm/memory.h | 2 +- arch/arm64/mm/physaddr.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/memory.h +1 -1
@@ -332,7 +332,7 @@ static inline void *phys_to_virt(phys_addr_t x)#endif /* !CONFIG_SPARSEMEM_VMEMMAP || CONFIG_DEBUG_VIRTUAL */ #define virt_addr_valid (addr) ({ \ - __typeof__(addr) __addr = addr; \ + __typeof__(addr) __addr = __tag_reset(addr); \ __is_lm_address(__addr) && pfn_valid(virt_to_pfn(__addr)); \ })
arch/arm64/mm/physaddr.c +1 -1
@@ -9,7 +9,7 @@phys_addr_t __virt_to_phys (unsigned long x) { -WARN (!__is_lm_address (x), +WARN (!__is_lm_address (__tag_reset (x)),"virt_to_phys used for non-linear address: %pK (%pS)\n" , (void *)x, (void *)x);
Catalin Marinas
maintainerPATCH 2/2 arm64: Use simpler arithmetics for the linear map macros
In reply to Catalin MarinasBecause of the tagged addresses, the __is_lm_address() and __lm_to_phys() macros grew to some harder to understand bitwise operations using PAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.
--- arch/arm64/include/asm/memory.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/memory.h +2 -2
@@ -251,9 +251,9 @@ static inline const void *__tag_set(const void *addr, u8 tag)* lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the * kernel's TTBR1 address range. */ -#define __is_lm_address (addr) (((u64)(addr) ^ PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET)) +#define __is_lm_address (addr) (((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET)) -#define __lm_to_phys (addr) (((addr) & ~PAGE_OFFSET) + PHYS_OFFSET) +#define __lm_to_phys (addr) (((addr) - PAGE_OFFSET) + PHYS_OFFSET)#define __kimg_to_phys (addr) ((addr) - kimage_voffset)#define __virt_to_phys_nodebug (x) ({ \
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 2/2] arm64: Use simpler arithmetics for the linear map macros
Quoted 19 lines · Catalin Marinas
Because of the tagged addresses, the
__is_lm_address()and__lm_to_phys()macros grew to some harder to understand bitwise operations usingPAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.Signed-off-by Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Mark Rutland <[email protected]>--- arch/arm64/include/asm/memory.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/include/asm/memory.h +0
-0
arch/arm64/include/asm/memory.h static inline const void *__tag_set(const void *addr, u8 tag) · line 255
Quoted 6 lines
arch/arm64/include/asm/memory.h +1
-1@@ -251,9 +251,9 @@ static inline const void *__tag_set(const void *addr, u8 tag)* lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the * kernel's TTBR1 address range. */ -#define __is_lm_address (addr) (((u64)(addr) ^ PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET)) +#define __is_lm_address (addr) (((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))
Why still subtract PAGE_OFFSET on both sides of the comparison here?
arch/arm64/include/asm/memory.h static inline const void *__tag_set(const void *addr, u8 tag) · line 259
Quoted 5 lines
arch/arm64/include/asm/memory.h +1
-1-#define __lm_to_phys (addr) (((addr) & ~PAGE_OFFSET) + PHYS_OFFSET) +#define __lm_to_phys (addr) (((addr) - PAGE_OFFSET) + PHYS_OFFSET)#define __kimg_to_phys (addr) ((addr) - kimage_voffset)#define __virt_to_phys_nodebug (x) ({ \
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 2/2] arm64: Use simpler arithmetics for the linear map macros
Quoted 2 lines · Ard Biesheuvel
Why still subtract
PAGE_OFFSETon both sides of the comparison here?
Because otherwise you'd have to use 'addr' twice in a macro, and write it something like
#define __is_lm_address(addr) \
(((u64)(addr) >= PAGE_OFFSET) && < ((u64)(addr) < PAGE_END))which then would require you to do a local variable in a statement expression to avoid the double evaluation..
Linus
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Catalin Marinas
maintainerRe: [PATCH 2/2] arm64: Use simpler arithmetics for the linear map macros
In reply to Ard BiesheuvelQuoted 14 lines · Ard Biesheuvel
Quoted 13 lines · Catalin Marinas
Because of the tagged addresses, the
__is_lm_address()and__lm_to_phys()macros grew to some harder to understand bitwise operations usingPAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.Signed-off-by Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Mark Rutland <[email protected]>--- arch/arm64/include/asm/memory.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines
arch/arm64/include/asm/memory.h static inline const void *__tag_set(const void *addr, u8 tag) · line 254
Quoted 10 lines
arch/arm64/include/asm/memory.h +1
-1@@ -251,9 +251,9 @@ static inline const void *__tag_set(const void *addr, u8 tag)* lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the * kernel's TTBR1 address range. */ -#define __is_lm_address (addr) (((u64)(addr) ^ PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET)) +#define __is_lm_address (addr) (((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))Why still subtract
PAGE_OFFSETon both sides of the comparison here?
Not sure what you mean (Linus replied on the local variable aspect).
With signed arithmetics, the above would be equivalent to addr < PAGE_END but it wouldn't cover the addr < PAGE_OFFSET case. So we rely on the unsigned wrap-around to detect this.
Signature · 6 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 2/2] arm64: Use simpler arithmetics for the linear map macros
Quoted 29 lines · Catalin Marinas
Quoted 14 lines · Ard Biesheuvel
Quoted 13 lines · Catalin Marinas
Because of the tagged addresses, the
__is_lm_address()and__lm_to_phys()macros grew to some harder to understand bitwise operations usingPAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.Signed-off-by Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Mark Rutland <[email protected]>--- arch/arm64/include/asm/memory.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 12 lines
arch/arm64/include/asm/memory.h static inline const void *__tag_set(const void *addr, u8 tag) · line 254
Quoted 10 lines
arch/arm64/include/asm/memory.h +1
-1@@ -251,9 +251,9 @@ static inline const void *__tag_set(const void *addr, u8 tag)* lives in the [PAGE_OFFSET, PAGE_END) interval at the bottom of the * kernel's TTBR1 address range. */ -#define __is_lm_address (addr) (((u64)(addr) ^ PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET)) +#define __is_lm_address (addr) (((u64)(addr) - PAGE_OFFSET) < (PAGE_END - PAGE_OFFSET))Why still subtract
PAGE_OFFSETon both sides of the comparison here?
Quoted 6 lines
Not sure what you mean (Linus replied on the local variable aspect).
With signed arithmetics, the above would be equivalent to addr <
PAGE_ENDbut it wouldn't cover the addr <PAGE_OFFSETcase. So we rely on the unsigned wrap-around to detect this.
Never mind, I just got confused. These changes look fine.
For the series,
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 4 lines · Catalin Marinas
The first patch attempts to fix
virt_addr_valid()macro for tagged addresses. The second patch replaces the bitwise operations with a subtract. The resulting image is slightly smaller.
Thanks, this looks very sane to me.
Linus
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 8 lines · Catalin Marinas
There some history to
virt_addr_valid()that goes back to 5.4. It seems to have been ok prior to this kernel, however, the VA reshuffling in commit14c127c957c1("arm64: mm: Flip kernel VA space") broke it. The subsequent fix68dd8ef32162("arm64: memory: Fixvirt_addr_valid()using__is_lm_address()") partially addressed it but it leftvirt_addr_valid()returning true for user addresses (or NULL).[...]
Applied to arm64 (for-next/fixes), thanks!
[1/2] arm64: Do not pass tagged addresses to __is_lm_address()
https://git.kernel.org/arm64/c/91cb2c8b072e [2/2] arm64: Use simpler arithmetics for the linear map macros https://git.kernel.org/arm64/c/22cd5edb2d9c
Signature · 7 lines
Catalin _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel