Re: [PATCH 3/7] mm: Add an 'end' parameter to find_get_entries

From: Jan Kara
Date: Fri Aug 21 2020 - 14:06:37 EST


On Fri 21-08-20 17:33:06, Matthew Wilcox wrote:
> On Fri, Aug 21, 2020 at 06:07:59PM +0200, Jan Kara wrote:
> > On Wed 19-08-20 16:05:51, Matthew Wilcox (Oracle) wrote:
> > > This simplifies the callers and leads to a more efficient implementation
> > > since the XArray has this functionality already.
> > >
> > > Signed-off-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>
> >
> > The patch looks good to me. Just I'd note that you could drop some:
> >
> > if (index >= end)
> > break;
> >
> > checks in shmem_undo_range() as well.
>
> Oh yes, missed a couple ;-) Thanks, I'll add.
>
> > In the past I was considering moving find_get_entries() to the same API as
> > find_get_pages_range() has (which is essentially what you do now, but I
> > also had 'start' to be a pgoff_t * so that we can return there where the
> > iteration ended in the range). But in the end I've decided the churn is not
> > worth the few removed lines and didn't push the patch in the end. What you
> > did in this patch seems to be a reasonable middle-ground :)
>
> I did look at that, but since we're returning the indices, we don't _need_
> to update the index here.
>
> I have some other ideas for this family of interfaces, but I'm trying
> to get the THP work off my plate before getting distracted by that ;-)

I have one thing which I wanted to do for a long time but never got to it.
IMHO the pagevec abstraction makes the loops unnecessarily complex. I'd
rather have helpers like:

for_each_mapping_page(mapping, page, start, end)

or

for_each_mapping_entry(mapping, entry, index, start, end)

and hide all the pagevec magic inside those. And it's even not that hard to
do including the handling of premature exit from the loop - sample
userspace code is attached...

Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR
#include <stdio.h>

#define PAGEVEC_SIZE 15

struct pagevec {
unsigned char nr;
int vals[PAGEVEC_SIZE];
};

struct pagevec_iter {
unsigned char idx;
int last_index;
struct pagevec pvec;
};

void pagevec_release(struct pagevec *pvec)
{
int i;

for (i = 0; i < pvec->nr; i++)
printf("Freeing val %d\n", pvec->vals[i]);
pvec->nr = 0;
}

int pagevec_lookup(struct pagevec *pvec, void *mapping, int *start, int end)
{
int i;

for (i = 0; i < PAGEVEC_SIZE && *start < 29; i++) {
pvec->vals[pvec->nr++] = 100 + *start;
(*start)++;
}
return pvec->nr;
}

static inline int get_next_pvec_val(struct pagevec_iter *pvec_i, void *mapping,
int end)
{
if (pvec_i->idx >= pvec_i->pvec.nr) {
pagevec_release(&pvec_i->pvec);
if (!pagevec_lookup(&pvec_i->pvec, mapping, &pvec_i->last_index,
end))
return 0;
pvec_i->idx = 0;
}
return pvec_i->pvec.vals[pvec_i->idx++];
}

void pagevec_release_iter(struct pagevec_iter *pvec_i)
{
pagevec_release(&pvec_i->pvec);
}

#define for_each_page(mapping, page, start, end) \
for (struct pagevec_iter pvec_i \
__attribute__ ((cleanup (pagevec_release_iter))) = { \
.idx = 0, .last_index = start, .pvec = { .nr = 0 } }; \
(page = get_next_pvec_val(&pvec_i, mapping, end));)

int main(void)
{
int page;

for_each_page(NULL, page, 5, 32) {
printf("Seeing val %d\n", page);
if (page > 122)
break;
}
return 0;
}