Re: [PATCH] resource: re-factor page_is_ram()

From: Dan Williams
Date: Mon Jul 18 2022 - 14:00:30 EST


David Hildenbrand wrote:
> On 01.06.22 18:32, Vaibhav Jain wrote:
> > Presently page_is_ram() relies on walk_system_ram_range() that performs a walk
> > on kernel iomem resources hierarchy with a dummy callback __is_ram(). Before
> > calling find_next_iomem_res(), walk_system_ram_range() does some book-keeping
> > which can be avoided for page_is_ram() use-case.
> >
> > Hence this patch proposes to update page_is_ram() to directly call
> > find_next_iomem_res() with minimal book-keeping needed.
> >
> > To avoid allocating a 'struct resource' the patch also updates
> > find_next_iomem_res() to not return -EINVAL in case 'res == NULL'. Instead
> > out 'struct resource *res' is only populated when its not NULL.
> >
> > Signed-off-by: Vaibhav Jain <vaibhav@xxxxxxxxxxxxx>
> > ---
> > kernel/resource.c | 19 ++++++++-----------
> > 1 file changed, 8 insertions(+), 11 deletions(-)
> >
> > diff --git a/kernel/resource.c b/kernel/resource.c
> > index 34eaee179689..ecf6b9a50adc 100644
> > --- a/kernel/resource.c
> > +++ b/kernel/resource.c
> > @@ -311,7 +311,7 @@ EXPORT_SYMBOL(release_resource);
> > *
> > * If a resource is found, returns 0 and @*res is overwritten with the part
> > * of the resource that's within [@start..@end]; if none is found, returns
> > - * -ENODEV. Returns -EINVAL for invalid parameters.
> > + * -ENODEV.
> > *
>
> There is still another -EINVAL in that function ...
>
> > * @start: start address of the resource searched for
> > * @end: end address of same resource
> > @@ -328,9 +328,6 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end,
> > {
> > struct resource *p;
> >
> > - if (!res)
> > - return -EINVAL;
> > -
> > if (start >= end)
> > return -EINVAL;
>
> As all callers guarantee that, we might just remove it.
>
> >
> > @@ -356,7 +353,7 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end,
> > break;
> > }
> >
> > - if (p) {
> > + if (p && res) {
> > /* copy data */
> > *res = (struct resource) {
> > .start = max(start, p->start),
> > @@ -474,18 +471,18 @@ int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
> > return ret;
> > }
> >
> > -static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
> > -{
> > - return 1;
> > -}
> > -
> > /*
> > * This generic page_is_ram() returns true if specified address is
> > * registered as System RAM in iomem_resource list.
> > */
> > int __weak page_is_ram(unsigned long pfn)
> > {
> > - return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
> > + const resource_size_t pfn_res = PFN_PHYS(pfn);
> > +
> > + return find_next_iomem_res(pfn_res,
> > + pfn_res + 1,
> > + IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
> > + IORES_DESC_NONE, NULL) == 0;

I tend to agree with David that this change makes the page_is_ram()
harder to read. I think the problem is that the "next" nature of
find_next_iomem_res() is meant to be handled by the caller. So it really
should be called find_iomem_res().

> > }
> > EXPORT_SYMBOL_GPL(page_is_ram);
> >
>
> What about
>
> a) A cleanup patch upfront that removes both -EINVAL cases from
> find_next_iomem_res() followed by

...a patch to rename find_next_iomem_res()

>
> b) The actual change to page_is_ram()
>
> ?