linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: mm: check virtual addr in virt_to_page() if CONFIG_DEBUG_VIRTUAL=y
@ 2019-04-15 17:36 Miles Chen
  2019-04-16 11:30 ` Catalin Marinas
  0 siblings, 1 reply; 2+ messages in thread
From: Miles Chen @ 2019-04-15 17:36 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, linux-kernel, linux-mediatek, wsd_upstream,
	Miles Chen, Ard Biesheuvel

This change uses the original virt_to_page() (the one with __pa()) to
check the given virtual address if CONFIG_DEBUG_VIRTUAL=y.

Recently, I worked on a bug: a driver passes a symbol address to
dma_map_single() and the virt_to_page() (called by dma_map_single())
does not work for non-linear addresses after commit 9f2875912dac
("arm64: mm: restrict virt_to_page() to the linear mapping").

I tried to trap the bug by enabling CONFIG_DEBUG_VIRTUAL but it
did not work - bacause the commit removes the __pa() from
virt_to_page() but CONFIG_DEBUG_VIRTUAL checks the virtual address
in __pa()/__virt_to_phys().

A simple solution is to use the original virt_to_page()
(the one with__pa()) if CONFIG_DEBUG_VIRTUAL=y.

Signed-off-by: Miles Chen <miles.chen@mediatek.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 arch/arm64/include/asm/memory.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
index 290195168bb3..2cb8248fa2c8 100644
--- a/arch/arm64/include/asm/memory.h
+++ b/arch/arm64/include/asm/memory.h
@@ -302,7 +302,7 @@ static inline void *phys_to_virt(phys_addr_t x)
  */
 #define ARCH_PFN_OFFSET		((unsigned long)PHYS_PFN_OFFSET)
 
-#ifndef CONFIG_SPARSEMEM_VMEMMAP
+#if !defined(CONFIG_SPARSEMEM_VMEMMAP) || defined(CONFIG_DEBUG_VIRTUAL)
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
 #define _virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
 #else
-- 
2.18.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] arm64: mm: check virtual addr in virt_to_page() if CONFIG_DEBUG_VIRTUAL=y
  2019-04-15 17:36 [PATCH] arm64: mm: check virtual addr in virt_to_page() if CONFIG_DEBUG_VIRTUAL=y Miles Chen
@ 2019-04-16 11:30 ` Catalin Marinas
  0 siblings, 0 replies; 2+ messages in thread
From: Catalin Marinas @ 2019-04-16 11:30 UTC (permalink / raw)
  To: Miles Chen
  Cc: Will Deacon, linux-arm-kernel, linux-kernel, linux-mediatek,
	wsd_upstream, Ard Biesheuvel

On Tue, Apr 16, 2019 at 01:36:36AM +0800, Miles Chen wrote:
> This change uses the original virt_to_page() (the one with __pa()) to
> check the given virtual address if CONFIG_DEBUG_VIRTUAL=y.
> 
> Recently, I worked on a bug: a driver passes a symbol address to
> dma_map_single() and the virt_to_page() (called by dma_map_single())
> does not work for non-linear addresses after commit 9f2875912dac
> ("arm64: mm: restrict virt_to_page() to the linear mapping").
> 
> I tried to trap the bug by enabling CONFIG_DEBUG_VIRTUAL but it
> did not work - bacause the commit removes the __pa() from
> virt_to_page() but CONFIG_DEBUG_VIRTUAL checks the virtual address
> in __pa()/__virt_to_phys().
> 
> A simple solution is to use the original virt_to_page()
> (the one with__pa()) if CONFIG_DEBUG_VIRTUAL=y.
> 
> Signed-off-by: Miles Chen <miles.chen@mediatek.com>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  arch/arm64/include/asm/memory.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h
> index 290195168bb3..2cb8248fa2c8 100644
> --- a/arch/arm64/include/asm/memory.h
> +++ b/arch/arm64/include/asm/memory.h
> @@ -302,7 +302,7 @@ static inline void *phys_to_virt(phys_addr_t x)
>   */
>  #define ARCH_PFN_OFFSET		((unsigned long)PHYS_PFN_OFFSET)
>  
> -#ifndef CONFIG_SPARSEMEM_VMEMMAP
> +#if !defined(CONFIG_SPARSEMEM_VMEMMAP) || defined(CONFIG_DEBUG_VIRTUAL)
>  #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
>  #define _virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
>  #else

IIUC, this shouldn't change the behaviour of virt_addr_valid(). The
patch looks fine to me.

Acked-by: Catalin Marinas <catalin.marinas@arm.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-16 11:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-15 17:36 [PATCH] arm64: mm: check virtual addr in virt_to_page() if CONFIG_DEBUG_VIRTUAL=y Miles Chen
2019-04-16 11:30 ` Catalin Marinas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).