[PATCH] powerpc: Make virt_to_pfn() a static inline
Michael Ellerman
mpe at ellerman.id.au
Tue Aug 15 17:21:52 AEST 2023
Christophe Leroy <christophe.leroy at csgroup.eu> writes:
> Le 14/08/2023 à 14:37, Michael Ellerman a écrit :
>> Linus Walleij <linus.walleij at linaro.org> 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
More information about the Linuxppc-dev
mailing list