Skip to contentSkip to conversation

arm64: Further fixes to the linear address checking

PATCH Applied Catalin Marinas 3 participants 9 messages Feb 1, 2021 – Feb 2, 2021

Subsystems (2)
lore
Share & tools
lore

Catalin Marinas

author maintainer
#0 lore raw

cover 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

author maintainer
#1 lore raw

PATCH 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.

Fixes 519ea6f1c82f ("arm64: Fix kernel address detection of __is_lm_address()")
Cc <[email protected]> # 5.4.x
Cc Will Deacon <[email protected]>
Cc Vincenzo Frascino <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
---
 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
index 99d7e1494aaa..3c1aaa522cbd 100644
@@ -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
index 67a9ba9eaa96..cde44c13dda1 100644
@@ -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);
author maintainer
#2 lore raw

PATCH 2/2 arm64: Use simpler arithmetics for the linear map macros

In reply to Catalin Marinas

Because 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.

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 +2 -2
index 3c1aaa522cbd..ff4732785c32 100644
@@ -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

#3 lore raw

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 using PAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.

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
index 3c1aaa522cbd..ff4732785c32 100644

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

#4 lore raw

Re: [PATCH 2/2] arm64: Use simpler arithmetics for the linear map macros

Quoted 2 lines · Ard Biesheuvel

Why still subtract PAGE_OFFSET on 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

author maintainer
#5 lore raw

Re: [PATCH 2/2] arm64: Use simpler arithmetics for the linear map macros

In reply to Ard Biesheuvel
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 using PAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.

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
index 3c1aaa522cbd..ff4732785c32 100644
@@ -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?

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
#6 lore raw

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 using PAGE_OFFSET. Since these macros only accept untagged addresses, use a simple subtract operation.

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
index 3c1aaa522cbd..ff4732785c32 100644
@@ -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?

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_END but it wouldn't cover the addr < PAGE_OFFSET case. 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

author maintainer Applied
#8 lore raw
In reply to Catalin Marinas
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 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).

[...]

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