Skip to contentSkip to conversation

arm: Correct virt_addr_valid

PATCH Superseded Laura Abbott 3 participants 6 messages Dec 16, 2013 – Dec 18, 2013

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 arm: Correct virt_addr_valid

The definition of virt_addr_valid is that virt_addr_valid should return true if and only if virt_to_page returns a valid pointer. The current definition of virt_addr_valid only checks against the virtual address range. There's no guarantee that just because a virtual address falls bewteen PAGE_OFFSET and high_memory the associated physical memory has a valid backing struct page. Follow the example of other architectures and convert to pfn_valid to verify that the virtual address is actually valid. The check for an address between PAGE_OFFSET and high_memory is still necessary as vmalloc/highmem addresses are not valid with virt_to_page.

Cc Russell King <[email protected]>
Cc Will Deacon <[email protected]>
Cc Nicolas Pitre <[email protected]>
---
 arch/arm/include/asm/memory.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/arm/include/asm/memory.h +2 -1
index 9ecccc8..6211df0 100644
@@ -350,7 +350,8 @@ static inline __deprecated void *bus_to_virt(unsigned long x)
 #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
 
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#define virt_addr_valid(kaddr)	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory)
+#define virt_addr_valid(kaddr)	(pfn_valid(__pa(kaddr) >> PAGE_SHIFT) && \
+	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory))
 
 #endif
 
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation
#1 lore raw

PATCH v2 2/2 arm64: Correct virt_addr_valid

The definition of virt_addr_valid is that virt_addr_valid should return true if and only if virt_to_page returns a valid pointer. The current definition of virt_addr_valid only checks against the virtual address range. There's no guarantee that just because a virtual address falls bewteen PAGE_OFFSET and high_memory the associated physical memory has a valid backing struct page. Follow the example of other architectures and convert to pfn_valid to verify that the virtual address is actually valid.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Nicolas Pitre <[email protected]>
---
 arch/arm64/include/asm/memory.h |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
arch/arm64/include/asm/memory.h +3 -2
index 3776217..b8ec776 100644
@@ -146,8 +146,9 @@ static inline void *phys_to_virt(phys_addr_t x)
 #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
 
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#define	virt_addr_valid(kaddr)	(((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
-				 ((void *)(kaddr) < (void *)high_memory))
+#define virt_addr_valid(kaddr)	(pfn_valid(__pa(kaddr) >> PAGE_SHIFT) && \
+	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory))
+
 
 #endif
 
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation
maintainer
#2 lore raw

PATCH v2 2/2 arm64: Correct virt_addr_valid

arch/arm64/include/asm/memory.h static inline void *phys_to_virt(phys_addr_t x) · line 151

Quoted 13 lines · Laura Abbott
arch/arm64/include/asm/memory.h +3 -2
index 3776217..b8ec776 100644
@@ -146,8 +146,9 @@ static inline void *phys_to_virt(phys_addr_t x)
 #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
 
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#define	virt_addr_valid(kaddr)	(((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
-				 ((void *)(kaddr) < (void *)high_memory))
+#define virt_addr_valid(kaddr)	(pfn_valid(__pa(kaddr) >> PAGE_SHIFT) && \
+	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory))
+

I still think the original patch was fine for arm64, no need for additional checks since we don't have highmem and __pa() is always linear.

Signature · 1 line
Catalin
#3 lore raw

PATCH v2 2/2 arm64: Correct virt_addr_valid

Quoted 8 lines · Catalin Marinas
Quoted 7 lines · Laura Abbott
arch/arm64/include/asm/memory.h +0 -0
index 3776217..b8ec776 100644
@@ -146,8 +146,9 @@ static inline void *phys_to_virt(phys_addr_t x)
  #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
 
Quoted 11 lines

arch/arm64/include/asm/memory.h static inline void *phys_to_virt(phys_addr_t x) · line 151

Quoted 6 lines
arch/arm64/include/asm/memory.h +3 -2
  #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#define	virt_addr_valid(kaddr)	(((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
-				 ((void *)(kaddr) < (void *)high_memory))
+#define virt_addr_valid(kaddr)	(pfn_valid(__pa(kaddr) >> PAGE_SHIFT) && \
+	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory))
+

I still think the original patch was fine for arm64, no need for additional checks since we don't have highmem and __pa() is always linear.

Okay, do you want to go ahead and just take the previous version then?

Laura

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by The Linux Foundation
maintainer
#4 lore raw

PATCH v2 2/2 arm64: Correct virt_addr_valid

Quoted 20 lines · Laura Abbott
Quoted 8 lines · Catalin Marinas
Quoted 7 lines · Laura Abbott
arch/arm64/include/asm/memory.h +0 -0
index 3776217..b8ec776 100644
@@ -146,8 +146,9 @@ static inline void *phys_to_virt(phys_addr_t x)
  #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
 
Quoted 10 lines

arch/arm64/include/asm/memory.h static inline void *phys_to_virt(phys_addr_t x) · line 151

Quoted 6 lines
arch/arm64/include/asm/memory.h +3 -2
  #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#define	virt_addr_valid(kaddr)	(((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
-				 ((void *)(kaddr) < (void *)high_memory))
+#define virt_addr_valid(kaddr)	(pfn_valid(__pa(kaddr) >> PAGE_SHIFT) && \
+	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory))
+

I still think the original patch was fine for arm64, no need for additional checks since we don't have highmem and __pa() is always linear.

Quoted 1 line

Okay, do you want to go ahead and just take the previous version then?

I already did (while Will is away ;)). Thanks.

Signature · 1 line
Catalin
#5 lore raw

PATCH v2 1/2 arm: Correct virt_addr_valid

In reply to Laura Abbott
Quoted 24 lines · Laura Abbott

The definition of virt_addr_valid is that virt_addr_valid should return true if and only if virt_to_page returns a valid pointer. The current definition of virt_addr_valid only checks against the virtual address range. There's no guarantee that just because a virtual address falls bewteen PAGE_OFFSET and high_memory the associated physical memory has a valid backing struct page. Follow the example of other architectures and convert to pfn_valid to verify that the virtual address is actually valid. The check for an address between PAGE_OFFSET and high_memory is still necessary as vmalloc/highmem addresses are not valid with virt_to_page.

Cc Russell King <[email protected]>
Cc Will Deacon <[email protected]>
Cc Nicolas Pitre <[email protected]>
---
 arch/arm/include/asm/memory.h |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
arch/arm/include/asm/memory.h +0 -0
index 9ecccc8..6211df0 100644

arch/arm/include/asm/memory.h static inline __deprecated void *bus_to_virt(unsigned long x) · line 354

Quoted 6 lines
arch/arm/include/asm/memory.h +2 -1
@@ -350,7 +350,8 @@ static inline __deprecated void *bus_to_virt(unsigned long x)
 #define ARCH_PFN_OFFSET		PHYS_PFN_OFFSET
 
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#define virt_addr_valid(kaddr)	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory)
+#define virt_addr_valid(kaddr)	(pfn_valid(__pa(kaddr) >> PAGE_SHIFT) && \
+	((unsigned long)(kaddr) >= PAGE_OFFSET && (unsigned long)(kaddr) < (unsigned long)high_memory))

Can you change the order of the check please, so that we do the quicker lowmem range checks before the pfn_valid check? I know I backed down on the latter being slow, but it's still slower than what we had before and changing the order of the conjunction is easy to do.

Cheers,

Will