Thread (9 messages) flat view 9 messages, 4 authors, 2023-08-23

Re: [PATCH] powerpc: Make virt_to_pfn() a static inline

From: Michael Ellerman <mpe@ellerman.id.au>
Date: 2023-08-15 07:22:57
Also in: kvm, lkml

Christophe Leroy [off-list ref] writes:
Le 14/08/2023 à 14:37, Michael Ellerman a écrit :
quoted
Linus Walleij [off-list ref] writes:
quoted
Making virt_to_pfn() a static inline taking a strongly typed
(const void *) makes the contract of a passing a pointer of that
type to the function explicit and exposes any misuse of the
macro virt_to_pfn() acting polymorphic and accepting many types
such as (void *), (unitptr_t) or (unsigned long) as arguments
without warnings.
...
quoted
diff --git a/arch/powerpc/include/asm/page.h b/arch/powerpc/include/asm/page.h
index f2b6bf5687d0..9ee4b6d4a82a 100644
--- a/arch/powerpc/include/asm/page.h
+++ b/arch/powerpc/include/asm/page.h
@@ -233,6 +224,25 @@ extern long long virt_phys_offset;
  #endif
  #endif
  
+#ifndef __ASSEMBLY__
+static inline unsigned long virt_to_pfn(const void *kaddr)
+{
+	return __pa(kaddr) >> PAGE_SHIFT;
+}
+
+static inline const void *pfn_to_kaddr(unsigned long pfn)
+{
+	return (const void *)(((unsigned long)__va(pfn)) << PAGE_SHIFT);
Any reason to do it this way rather than:

+       return __va(pfn << PAGE_SHIFT);
Even cleaner:

	return __va(PFN_PHYS(pfn));
PFN_PHYS() includes a cast to phys_addr_t before shifting, so it's not
entirely equivalent.

But if phys_addr_t is larger than unsinged long then that cast is
important. Which makes me wonder how/if pfn_to_kaddr() has been working
until now for CONFIG_PHYS_ADDR_T_64BIT=y.

cheers
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help