Skip to contentSkip to conversation

apply page shift to PFN instead of VA in pfn_to_virt

PATCH In review Yan Zhao 5 participants 18 messages Jan 31, 2024 – Feb 29, 2024

Subsystems (4)
lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

cover apply page shift to PFN instead of VA in pfn_to_virt

This is a tiny fix to pfn_to_virt() for some platforms.

The original implementaion of pfn_to_virt() takes PFN instead of PA as the input to macro __va, with PAGE_SHIFT applying to the converted VA, which is not right under most conditions, especially when there's an offset in __va.

Yan Zhao (4):
asm-generic/page.h: apply page shift to PFN instead of VA in
pfn_to_virt
csky: apply page shift to PFN instead of VA in pfn_to_virt
Hexagon: apply page shift to PFN instead of VA in pfn_to_virt
openrisc: apply page shift to PFN instead of VA in pfn_to_virt

 arch/csky/include/asm/page.h     | 2 +-
 arch/hexagon/include/asm/page.h  | 2 +-
 arch/openrisc/include/asm/page.h | 2 +-
 include/asm-generic/page.h       | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

base-commit: 41bccc98fb7931d63d03f326a746ac4d429c1dd3

Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 1/4 asm-generic/page.h: apply page shift to PFN instead of VA in pfn_to_virt

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes 2d78057f0dd4 ("asm-generic/page.h: Make pfn accessors static inlines")
---
 include/asm-generic/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/asm-generic/page.h +1 -1
index 9773582fd96e..4f1265207b9a 100644
@@ -81,7 +81,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #define virt_to_pfn virt_to_pfn
 static inline void *pfn_to_virt(unsigned long pfn)
 {
-	return __va(pfn) << PAGE_SHIFT;
+	return __va(pfn << PAGE_SHIFT);
 }
 #define pfn_to_virt pfn_to_virt
 
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] asm-generic/page.h: apply page shift to PFN instead of VA in pfn_to_virt

Quoted 15 lines · Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes 2d78057f0dd4 ("asm-generic/page.h: Make pfn accessors static inlines")
---
 include/asm-generic/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/asm-generic/page.h +0 -0
index 9773582fd96e..4f1265207b9a 100644

include/asm-generic/page.h static inline unsigned long virt_to_pfn(const void *kaddr) · line 84

Quoted 5 lines
include/asm-generic/page.h +1 -1
@@ -81,7 +81,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 #define virt_to_pfn virt_to_pfn
 static inline void *pfn_to_virt(unsigned long pfn)
 {
-       return __va(pfn) << PAGE_SHIFT;
+       return __va(pfn << PAGE_SHIFT);

Oh, that's a terrible bug; Thx for fixing it.

Quoted 6 lines
include/asm-generic/page.h +0 -1
 }
 #define pfn_to_virt pfn_to_virt
 
--

2.17.1

Signature · 2 lines
Best Regards
 Guo Ren
Reviewed-by on v1 1/4
#3 lore raw

Re: [PATCH 1/4] asm-generic/page.h: apply page shift to PFN instead of VA in pfn_to_virt

In reply to Yan Zhao
Quoted 5 lines · Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes 2d78057f0dd4 ("asm-generic/page.h: Make pfn accessors static inlines")

My bug, obviously. :(

I thought this was already applied with the other fixes, but maybe it was missed?

Yours,
Linus Walleij

author
#4 lore raw

Re: [PATCH 1/4] asm-generic/page.h: apply page shift to PFN instead of VA in pfn_to_virt

Quoted 5 lines · Linus Walleij
Quoted 3 lines · Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Quoted 9 lines
Quoted 2 lines
Fixes 2d78057f0dd4 ("asm-generic/page.h: Make pfn accessors static inlines")

My bug, obviously. :(

I thought this was already applied with the other fixes, but maybe it was missed?

Hi Linus,
The other 3 in csky/hexagon/openrisc should have been applied in
https://lore.kernel.org/all/[email protected]/.

This one in asm-generic is not, because Arnd said he is going to remove the header as a whole soon.

I explained it in the change log :)
"The pfn_to_virt() in asm-generic/page.h is not touched in this patch as it's referenced by page_to_virt() in that header while the whole header is going to be removed as a whole due to no one including it."

Thanks
Yan

author
#5 lore raw

PATCH 2/4 csky: apply page shift to PFN instead of VA in pfn_to_virt

In reply to Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes c1884e1e1164 ("csky: Make pfn accessors static inlines")
---
 arch/csky/include/asm/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/csky/include/asm/page.h +1 -1
index 4a0502e324a6..2c4cc7825a7b 100644
@@ -84,7 +84,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 
 static inline void * pfn_to_virt(unsigned long pfn)
 {
-	return (void *)((unsigned long)__va(pfn) << PAGE_SHIFT);
+	return __va(pfn << PAGE_SHIFT);
 }
 
 #define MAP_NR(x)	PFN_DOWN((unsigned long)(x) - PAGE_OFFSET - \
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] csky: apply page shift to PFN instead of VA in pfn_to_virt

Quoted 15 lines · Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes c1884e1e1164 ("csky: Make pfn accessors static inlines")
---
 arch/csky/include/asm/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/csky/include/asm/page.h +0 -0
index 4a0502e324a6..2c4cc7825a7b 100644

arch/csky/include/asm/page.h static inline unsigned long virt_to_pfn(const void *kaddr) · line 88

Quoted 6 lines
arch/csky/include/asm/page.h +1 -1
@@ -84,7 +84,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 
 static inline void * pfn_to_virt(unsigned long pfn)
 {
-       return (void *)((unsigned long)__va(pfn) << PAGE_SHIFT);
+       return __va(pfn << PAGE_SHIFT);
 }
Quoted 5 lines
arch/csky/include/asm/page.h +0 -1
 #define MAP_NR(x)      PFN_DOWN((unsigned long)(x) - PAGE_OFFSET - \
--

2.17.1

Signature · 2 lines
Best Regards
 Guo Ren
author
#7 lore raw

PATCH 3/4 Hexagon: apply page shift to PFN instead of VA in pfn_to_virt

In reply to Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes d6e81532b10d ("Hexagon: Make pfn accessors statics inlines")
---
 arch/hexagon/include/asm/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/hexagon/include/asm/page.h +1 -1
index 10f1bc07423c..1d341590791a 100644
@@ -135,7 +135,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 
 static inline void *pfn_to_virt(unsigned long pfn)
 {
-	return (void *)((unsigned long)__va(pfn) << PAGE_SHIFT);
+	return __va(pfn << PAGE_SHIFT);
 }
 
 
Signature · 1 line
2.17.1
author
#8 lore raw

PATCH 4/4 openrisc: apply page shift to PFN instead of VA in pfn_to_virt

In reply to Yan Zhao

Apply the page shift to PFN to get physical address for final VA. The macro __va should take physical address instead of PFN as input.

Fixes 232ba1630c66 ("openrisc: Make pfn accessors statics inlines")
---
 arch/openrisc/include/asm/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/openrisc/include/asm/page.h +1 -1
index 44fc1fd56717..55c66f6cb1bd 100644
@@ -79,7 +79,7 @@ static inline unsigned long virt_to_pfn(const void *kaddr)
 
 static inline void * pfn_to_virt(unsigned long pfn)
 {
-	return (void *)((unsigned long)__va(pfn) << PAGE_SHIFT);
+	return __va(pfn << PAGE_SHIFT);
 }
 
 #define virt_to_page(addr) \
Signature · 1 line
2.17.1
Reviewed-by on the v1 series
#9 lore raw
In reply to Yan Zhao
Quoted 6 lines · Yan Zhao

This is a tiny fix to pfn_to_virt() for some platforms.

The original implementaion of pfn_to_virt() takes PFN instead of PA as the input to macro __va, with PAGE_SHIFT applying to the converted VA, which is not right under most conditions, especially when there's an offset in __va.

Ooops that's right, I wonder why I got it wrong.
Arithmetic made it not regress :/
Thank you so much for fixing this Yan!

Arnd: I think you can take most of them through the arch tree.

Yours,
Linus Walleij

maintainer
#10 lore raw
In reply to Yan Zhao
Quoted 8 lines · Yan Zhao

This is a tiny fix to pfn_to_virt() for some platforms.

The original implementaion of pfn_to_virt() takes PFN instead of PA as the input to macro __va, with PAGE_SHIFT applying to the converted VA, which is not right under most conditions, especially when there's an offset in __va.

Quoted 6 lines

Yan Zhao (4):
asm-generic/page.h: apply page shift to PFN instead of VA in
pfn_to_virt
csky: apply page shift to PFN instead of VA in pfn_to_virt
Hexagon: apply page shift to PFN instead of VA in pfn_to_virt
openrisc: apply page shift to PFN instead of VA in pfn_to_virt

Nice catch, this is clearly a correct fix, and I can take the series through the asm-generic tree if we want to take this approach.

I made a couple of interesting observations looking at this patch though:

- this function is only used in architecture specific code on m68k, riscv and s390, though a couple of other architectures have the same definition.

- There is another function that does the same thing called pfn_to_kaddr(), which is defined on arm, arm64, csky, loongarch, mips, nios2, powerpc, s390, sh, sparc and x86, as well as yet another pfn_va() on parisc.

- the asm-generic/page.h file used to be included by h8300, c6x and blackfin, all of which are now gone. It has no users left and can just as well get removed, unless we find a new use for it.

Since it looks like the four broken functions you fix don't have a single caller, maybe it would be better to just remove them all?

How exactly did you notice the function being wrong, did you try to add a user somewhere, or just read through the code?

Arnd

author
#11 lore raw
Quoted 37 lines · Arnd Bergmann
Quoted 8 lines · Yan Zhao

This is a tiny fix to pfn_to_virt() for some platforms.

The original implementaion of pfn_to_virt() takes PFN instead of PA as the input to macro __va, with PAGE_SHIFT applying to the converted VA, which is not right under most conditions, especially when there's an offset in __va.

Quoted 6 lines

Yan Zhao (4):
asm-generic/page.h: apply page shift to PFN instead of VA in
pfn_to_virt
csky: apply page shift to PFN instead of VA in pfn_to_virt
Hexagon: apply page shift to PFN instead of VA in pfn_to_virt
openrisc: apply page shift to PFN instead of VA in pfn_to_virt

Nice catch, this is clearly a correct fix, and I can take the series through the asm-generic tree if we want to take this approach.

I made a couple of interesting observations looking at this patch though:

- this function is only used in architecture specific code on m68k, riscv and s390, though a couple of other architectures have the same definition.

- There is another function that does the same thing called pfn_to_kaddr(), which is defined on arm, arm64, csky, loongarch, mips, nios2, powerpc, s390, sh, sparc and x86, as well as yet another pfn_va() on parisc.

- the asm-generic/page.h file used to be included by h8300, c6x and blackfin, all of which are now gone. It has no users left and can just as well get removed, unless we find a new use for it.

Quoted 7 lines

Since it looks like the four broken functions you fix don't have a single caller, maybe it would be better to just remove them all?

How exactly did you notice the function being wrong, did you try to add a user somewhere, or just read through the code?

I came across them when I was debugging an unexpected kernel page fault on x86, and I was not sure whether page_to_virt() was compiled in asm-generic/page.h or linux/mm.h.
Though finally, it turned out that the one in linux/mm.h was used, which yielded the right result and the unexpected kernel page fault in my case was not related to page_to_virt(), it did lead me to noticing that the pfn_to_virt() in asm-generic/page.h and other 3 archs did not look right.

Yes, unlike virt_to_pfn() which still has a caller in openrisc (among csky, Hexagon, openrisc), pfn_to_virt() now does not have a caller in the 3 archs. Though both virt_to_pfn() and pfn_to_virt() are referenced in asm-generic/page.h, I also not sure if we need to remove the asm-generic/page.h which may serve as a template to future archs ?

So, either way looks good to me :)

maintainer
#12 lore raw
Quoted 14 lines · Yan Zhao
Quoted 5 lines · Arnd Bergmann

On Wed, Jan 31, 2024, at 06:51, Yan Zhao wrote:

How exactly did you notice the function being wrong, did you try to add a user somewhere, or just read through the code?

I came across them when I was debugging an unexpected kernel page fault on x86, and I was not sure whether page_to_virt() was compiled in asm-generic/page.h or linux/mm.h.
Though finally, it turned out that the one in linux/mm.h was used, which yielded the right result and the unexpected kernel page fault in my case was not related to page_to_virt(), it did lead me to noticing that the pfn_to_virt() in asm-generic/page.h and other 3 archs did not look right.

Quoted 7 lines

Yes, unlike virt_to_pfn() which still has a caller in openrisc (among csky, Hexagon, openrisc), pfn_to_virt() now does not have a caller in the 3 archs. Though both virt_to_pfn() and pfn_to_virt() are referenced in asm-generic/page.h, I also not sure if we need to remove the asm-generic/page.h which may serve as a template to future archs ?

So, either way looks good to me :)

I think it's fair to assume we won't need asm-generic/page.h any more, as we likely won't be adding new NOMMU architectures. I can have a look myself at removing any such unused headers in include/asm-generic/, it's probably not the only one.

Can you just send a patch to remove the unused pfn_to_virt() functions?

Arnd

#13 lore raw

Hi Arnd,

Quoted 2 lines · Arnd Bergmann

I think it's fair to assume we won't need asm-generic/page.h any more, as we likely won't be adding new NOMMU architectures.

So you think riscv-nommu (k210) was the last one we will ever see?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
maintainer
#14 lore raw
Quoted 7 lines · Geert Uytterhoeven

Hi Arnd,

Quoted 2 lines · Arnd Bergmann

I think it's fair to assume we won't need asm-generic/page.h any more, as we likely won't be adding new NOMMU architectures.

So you think riscv-nommu (k210) was the last one we will ever see?

Yes. We've already removed half of the nommu architectures (blackfin, avr32, h8300, m32r, microblaze-nommu) over the past couple of years, and the remaining ones are pretty much only there to support existing users.

The only platform one that I see getting real work is esp32 [1], but that is not a new architecture.

Arnd

[1] https://github.com/jcmvbkbc/linux-xtensa/tree/xtensa-6.8-rc2-esp32

author
#15 lore raw
In reply to Arnd Bergmann
Quoted 23 lines · Arnd Bergmann
Quoted 14 lines · Yan Zhao
Quoted 5 lines · Arnd Bergmann

On Wed, Jan 31, 2024, at 06:51, Yan Zhao wrote:

How exactly did you notice the function being wrong, did you try to add a user somewhere, or just read through the code?

I came across them when I was debugging an unexpected kernel page fault on x86, and I was not sure whether page_to_virt() was compiled in asm-generic/page.h or linux/mm.h.
Though finally, it turned out that the one in linux/mm.h was used, which yielded the right result and the unexpected kernel page fault in my case was not related to page_to_virt(), it did lead me to noticing that the pfn_to_virt() in asm-generic/page.h and other 3 archs did not look right.

Quoted 7 lines

Yes, unlike virt_to_pfn() which still has a caller in openrisc (among csky, Hexagon, openrisc), pfn_to_virt() now does not have a caller in the 3 archs. Though both virt_to_pfn() and pfn_to_virt() are referenced in asm-generic/page.h, I also not sure if we need to remove the asm-generic/page.h which may serve as a template to future archs ?

So, either way looks good to me :)

Quoted 7 lines

I think it's fair to assume we won't need asm-generic/page.h any more, as we likely won't be adding new NOMMU architectures. I can have a look myself at removing any such unused headers in include/asm-generic/, it's probably not the only one.

Can you just send a patch to remove the unused pfn_to_virt() functions?

Ok. I'll do it!
BTW: do you think it's also good to keep this fixing series though we'll remove the unused function later?
So if people want to revert the removal some day, they can get right one.

Or maybe I'm just paranoid, and explanation about the fix in the commit message of patch for function removal is enough.

What's your preference? :)

maintainer
#16 lore raw
Quoted 14 lines · Yan Zhao
Quoted 8 lines · Arnd Bergmann

I think it's fair to assume we won't need asm-generic/page.h any more, as we likely won't be adding new NOMMU architectures. I can have a look myself at removing any such unused headers in include/asm-generic/, it's probably not the only one.

Can you just send a patch to remove the unused pfn_to_virt() functions?

Ok. I'll do it!
BTW: do you think it's also good to keep this fixing series though we'll remove the unused function later?
So if people want to revert the removal some day, they can get right one.

Quoted 4 lines

Or maybe I'm just paranoid, and explanation about the fix in the commit message of patch for function removal is enough.

What's your preference? :)

I would just remove it, there is no point in having both pfn_to_kaddr() and pfn_to_virt() when they do the exact same thing aside from this bug.

Just do a single patch for all architectures, no need to have three or four identical ones when I'm going to merge them all through the same tree anyway.

Just make sure you explain in the changelog what the bug was and how you noticed it, in case anyone is ever tempted to bring the function back.

Arnd

author
#17 lore raw
Quoted 24 lines · Arnd Bergmann
Quoted 14 lines · Yan Zhao
Quoted 8 lines · Arnd Bergmann

I think it's fair to assume we won't need asm-generic/page.h any more, as we likely won't be adding new NOMMU architectures. I can have a look myself at removing any such unused headers in include/asm-generic/, it's probably not the only one.

Can you just send a patch to remove the unused pfn_to_virt() functions?

Ok. I'll do it!
BTW: do you think it's also good to keep this fixing series though we'll remove the unused function later?
So if people want to revert the removal some day, they can get right one.

Quoted 4 lines

Or maybe I'm just paranoid, and explanation about the fix in the commit message of patch for function removal is enough.

What's your preference? :)

I would just remove it, there is no point in having both pfn_to_kaddr() and pfn_to_virt() when they do the exact same thing aside from this bug.

Quoted 7 lines

Just do a single patch for all architectures, no need to have three or four identical ones when I'm going to merge them all through the same tree anyway.

Just make sure you explain in the changelog what the bug was and how you noticed it, in case anyone is ever tempted to bring the function back.

Done.
https://lore.kernel.org/all/[email protected]
Thanks for you guidance :)