Skip to contentSkip to conversation

arm64: trap illegal translations in __virt_to_phys()

PATCH In review Miles Chen 2 participants 5 messages Jan 7, 2019 – Jan 9, 2019

Subsystems (2)
lore
Share & tools
lore

Miles Chen

author
#0 lore raw

PATCH 1/1 arm64: trap illegal translations in __virt_to_phys()

Current __virt_to_phys() only print warning messages for non-linear addresses. It's hard to catch all warnings by those messages. So add a VIRTUAL_BUG_ON() to trap all non-linear and non-symbol addresses (e.g., stack addresses)

Tested by pass stack addresses and symbol addresses to __pa(). Result:
stack addresses: kernel BUG()
symbol addresses: kernel warning message

Maybe we should trap all non-linear address translations in the future.

---
 arch/arm64/mm/physaddr.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/mm/physaddr.c +5 -0
index 67a9ba9eaa96..f6b935dad19c 100644
@@ -14,6 +14,11 @@ phys_addr_t __virt_to_phys(unsigned long x)
 	      (void *)x,
 	      (void *)x);
 
+	/* trap all non-linear and non-symbol addresses */
+	VIRTUAL_BUG_ON(!__is_lm_address(x) &&
+			(x < (unsigned long)KERNEL_START ||
+			 x > (unsigned long)KERNEL_END));
+
 	return __virt_to_phys_nodebug(x);
 }
 EXPORT_SYMBOL(__virt_to_phys);
Signature · 1 line
2.18.0
reviewer
#1 lore raw
Quoted 2 lines · Miles Chen

Current __virt_to_phys() only print warning messages for non-linear addresses. It's hard to catch all warnings by those messages.

Why? Are you seeing a large number of warnings somewhere?

Quoted 5 lines

So add a VIRTUAL_BUG_ON() to trap all non-linear and non-symbol addresses (e.g., stack addresses)

Tested by pass stack addresses and symbol addresses to __pa(). Result:
stack addresses: kernel BUG()

Either:

* Stacks are vmap'd, and __is_lm_address(stack_addr) is false. We'll produce a WARNING() today (and return a junk physical address).

* Stacks are linear mapped, and cannot be distinguished from other linear mapped addresses. The physical address will be valid.

... so I don't understand why you need to change this.

Quoted 1 line

symbol addresses: kernel warning message

That should already be the case today, since the kernel image is mapped separately from the linear map, so __is_lm_address(symbol_addr) should be false.

Quoted 13 lines

Maybe we should trap all non-linear address translations in the future.

---
 arch/arm64/mm/physaddr.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/mm/physaddr.c +0 -0
index 67a9ba9eaa96..f6b935dad19c 100644

arch/arm64/mm/physaddr.c phys_addr_t __virt_to_phys(unsigned long x) · line 20

Quoted 7 lines
arch/arm64/mm/physaddr.c +4 -0
@@ -14,6 +14,11 @@ phys_addr_t __virt_to_phys(unsigned long x)
 	      (void *)x,
 	      (void *)x);
 
+	/* trap all non-linear and non-symbol addresses */
+	VIRTUAL_BUG_ON(!__is_lm_address(x) &&
+			(x < (unsigned long)KERNEL_START ||
+			 x > (unsigned long)KERNEL_END));

The KERNEL_START and KERNEL_END definitions refer to the kernel image, not the linear map, so it doesn't make any sense to permit those here.

It is *not* valid to call __virt_to_phys() with a symbol address. We only support those in __virt_to_phys_nodebug() so that broken code has a chance of stumbling on.

If you want the kernel to die immediately when it hits a warning here, please set panic_on_warn.

Thanks,
Mark.

author
#2 lore raw
Quoted 5 lines · Mark Rutland
Quoted 2 lines · Miles Chen

Current __virt_to_phys() only print warning messages for non-linear addresses. It's hard to catch all warnings by those messages.

Why? Are you seeing a large number of warnings somewhere?

Official kernel works fine. I saw some cases in our internal branch and we're fixing them.

Quoted 9 lines
Quoted 6 lines

So add a VIRTUAL_BUG_ON() to trap all non-linear and non-symbol addresses (e.g., stack addresses)

Tested by pass stack addresses and symbol addresses to __pa(). Result:
stack addresses: kernel BUG()

Either:

Quoted 7 lines

* Stacks are vmap'd, and __is_lm_address(stack_addr) is false. We'll produce a WARNING() today (and return a junk physical address).

* Stacks are linear mapped, and cannot be distinguished from other linear mapped addresses. The physical address will be valid.

... so I don't understand why you need to change this.

For the first case: for vmap'd stack, __pa() returns a junk physical address and it might be easier to debug this incorrect address translation by a BUG() call instead of monitoring the warning log.

Like __phys_addr_symbol() does. __phys_addr_symbol() uses VIRTUAL_BUG_ON() for non symbol addresses.

Quoted 40 lines
Quoted 2 lines

symbol addresses: kernel warning message

That should already be the case today, since the kernel image is mapped separately from the linear map, so __is_lm_address(symbol_addr) should be false.

Quoted 13 lines

Maybe we should trap all non-linear address translations in the future.

---
 arch/arm64/mm/physaddr.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/mm/physaddr.c +0 -0
index 67a9ba9eaa96..f6b935dad19c 100644

arch/arm64/mm/physaddr.c phys_addr_t __virt_to_phys(unsigned long x) · line 20

Quoted 7 lines
arch/arm64/mm/physaddr.c +4 -0
@@ -14,6 +14,11 @@ phys_addr_t __virt_to_phys(unsigned long x)
 	      (void *)x,
 	      (void *)x);
 
+	/* trap all non-linear and non-symbol addresses */
+	VIRTUAL_BUG_ON(!__is_lm_address(x) &&
+			(x < (unsigned long)KERNEL_START ||
+			 x > (unsigned long)KERNEL_END));

The KERNEL_START and KERNEL_END definitions refer to the kernel image, not the linear map, so it doesn't make any sense to permit those here.

It is *not* valid to call __virt_to_phys() with a symbol address. We only support those in __virt_to_phys_nodebug() so that broken code has a chance of stumbling on.

If you want the kernel to die immediately when it hits a warning here, please set panic_on_warn.

Thanks,
Mark.

reviewer
#3 lore raw
Quoted 22 lines · Miles Chen
Quoted 5 lines · Mark Rutland
Quoted 2 lines · Miles Chen

Current __virt_to_phys() only print warning messages for non-linear addresses. It's hard to catch all warnings by those messages.

Why? Are you seeing a large number of warnings somewhere?

Official kernel works fine. I saw some cases in our internal branch and we're fixing them.

Quoted 12 lines
Quoted 6 lines

So add a VIRTUAL_BUG_ON() to trap all non-linear and non-symbol addresses (e.g., stack addresses)

Tested by pass stack addresses and symbol addresses to __pa(). Result:
stack addresses: kernel BUG()

Either:

* Stacks are vmap'd, and __is_lm_address(stack_addr) is false. We'll produce a WARNING() today (and return a junk physical address).

Quoted 8 lines
Quoted 4 lines

* Stacks are linear mapped, and cannot be distinguished from other linear mapped addresses. The physical address will be valid.

... so I don't understand why you need to change this.

For the first case: for vmap'd stack, __pa() returns a junk physical address and it might be easier to debug this incorrect address translation by a BUG() call instead of monitoring the warning log.

I think that's an argument for upgrading the existing WARN() to a BUG(), rather than adding a separate VIRTUAL_BUG_ON().

However, there are cases where the junk physical address is not used to perform an access, and the WARN() is more helpful.

You can set panic_on_warn to get an immediate panic() when the WARN() fires. Is there some reason that approach doesn't work for you?

Thanks,
Mark.

author
#4 lore raw
Quoted 32 lines · Mark Rutland
Quoted 22 lines · Miles Chen
Quoted 5 lines · Mark Rutland
Quoted 2 lines · Miles Chen

Current __virt_to_phys() only print warning messages for non-linear addresses. It's hard to catch all warnings by those messages.

Why? Are you seeing a large number of warnings somewhere?

Official kernel works fine. I saw some cases in our internal branch and we're fixing them.

Quoted 12 lines
Quoted 6 lines

So add a VIRTUAL_BUG_ON() to trap all non-linear and non-symbol addresses (e.g., stack addresses)

Tested by pass stack addresses and symbol addresses to __pa(). Result:
stack addresses: kernel BUG()

Either:

* Stacks are vmap'd, and __is_lm_address(stack_addr) is false. We'll produce a WARNING() today (and return a junk physical address).

Quoted 8 lines
Quoted 4 lines

* Stacks are linear mapped, and cannot be distinguished from other linear mapped addresses. The physical address will be valid.

... so I don't understand why you need to change this.

For the first case: for vmap'd stack, __pa() returns a junk physical address and it might be easier to debug this incorrect address translation by a BUG() call instead of monitoring the warning log.

Quoted 5 lines

I think that's an argument for upgrading the existing WARN() to a BUG(), rather than adding a separate VIRTUAL_BUG_ON().

However, there are cases where the junk physical address is not used to perform an access, and the WARN() is more helpful.

got it.

Quoted 3 lines

You can set panic_on_warn to get an immediate panic() when the WARN() fires. Is there some reason that approach doesn't work for you?

panic_on_warn works fine. thanks for your comment.

cheers,
Miles

Quoted 3 lines

Thanks,
Mark.