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

From: Christophe Leroy
Date: Mon Aug 14 2023 - 10:30:17 EST




Le 14/08/2023 à 14:37, Michael Ellerman a écrit :
> Linus Walleij <linus.walleij@xxxxxxxxxx> writes:
>> 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.
> ...
>> 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
>> @@ -9,6 +9,7 @@
>> #ifndef __ASSEMBLY__
>> #include <linux/types.h>
>> #include <linux/kernel.h>
>> +#include <linux/bug.h>
>> #else
>> #include <asm/types.h>
>> #endif
>> @@ -119,16 +120,6 @@ extern long long virt_phys_offset;
>> #define ARCH_PFN_OFFSET ((unsigned long)(MEMORY_START >> PAGE_SHIFT))
>> #endif
>>
>> -#define virt_to_pfn(kaddr) (__pa(kaddr) >> PAGE_SHIFT)
>> -#define virt_to_page(kaddr) pfn_to_page(virt_to_pfn(kaddr))
>> -#define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT)
>> -
>> -#define virt_addr_valid(vaddr) ({ \
>> - unsigned long _addr = (unsigned long)vaddr; \
>> - _addr >= PAGE_OFFSET && _addr < (unsigned long)high_memory && \
>> - pfn_valid(virt_to_pfn(_addr)); \
>> -})
>> -
>> /*
>> * On Book-E parts we need __va to parse the device tree and we can't
>> * determine MEMORY_START until then. However we can determine PHYSICAL_START
>> @@ -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));

>
> Seems to be equivalent and much cleaner?
>
> cheers