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

From: Michael Ellerman
Date: Tue Aug 15 2023 - 03:22:57 EST


Christophe Leroy <christophe.leroy@xxxxxxxxxx> writes:
> 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
>>> @@ -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