Commit b947cee4 authored by Jan Kara's avatar Jan Kara Committed by Linus Torvalds

mm: implement find_get_pages_range()

Implement a variant of find_get_pages() that stops iterating at given
index.  This may be substantial performance gain if the mapping is
sparse.  See following commit for details.  Furthermore lots of users of
this function (through pagevec_lookup()) actually want a range lookup
and all of them are currently open-coding this.

Also create corresponding pagevec_lookup_range() function.

Link: http://lkml.kernel.org/r/20170726114704.7626-4-jack@suse.czSigned-off-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d72dc8a2
...@@ -353,8 +353,16 @@ struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset); ...@@ -353,8 +353,16 @@ struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset);
unsigned find_get_entries(struct address_space *mapping, pgoff_t start, unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
unsigned int nr_entries, struct page **entries, unsigned int nr_entries, struct page **entries,
pgoff_t *indices); pgoff_t *indices);
unsigned find_get_pages(struct address_space *mapping, pgoff_t *start, unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
unsigned int nr_pages, struct page **pages); pgoff_t end, unsigned int nr_pages,
struct page **pages);
static inline unsigned find_get_pages(struct address_space *mapping,
pgoff_t *start, unsigned int nr_pages,
struct page **pages)
{
return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
pages);
}
unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start, unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
unsigned int nr_pages, struct page **pages); unsigned int nr_pages, struct page **pages);
unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index, unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
......
...@@ -27,8 +27,17 @@ unsigned pagevec_lookup_entries(struct pagevec *pvec, ...@@ -27,8 +27,17 @@ unsigned pagevec_lookup_entries(struct pagevec *pvec,
pgoff_t start, unsigned nr_entries, pgoff_t start, unsigned nr_entries,
pgoff_t *indices); pgoff_t *indices);
void pagevec_remove_exceptionals(struct pagevec *pvec); void pagevec_remove_exceptionals(struct pagevec *pvec);
unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping, unsigned pagevec_lookup_range(struct pagevec *pvec,
pgoff_t *start, unsigned nr_pages); struct address_space *mapping,
pgoff_t *start, pgoff_t end, unsigned nr_pages);
static inline unsigned pagevec_lookup(struct pagevec *pvec,
struct address_space *mapping,
pgoff_t *start, unsigned nr_pages)
{
return pagevec_lookup_range(pvec, mapping, start, (pgoff_t)-1,
nr_pages);
}
unsigned pagevec_lookup_tag(struct pagevec *pvec, unsigned pagevec_lookup_tag(struct pagevec *pvec,
struct address_space *mapping, pgoff_t *index, int tag, struct address_space *mapping, pgoff_t *index, int tag,
unsigned nr_pages); unsigned nr_pages);
......
...@@ -1557,24 +1557,29 @@ unsigned find_get_entries(struct address_space *mapping, ...@@ -1557,24 +1557,29 @@ unsigned find_get_entries(struct address_space *mapping,
} }
/** /**
* find_get_pages - gang pagecache lookup * find_get_pages_range - gang pagecache lookup
* @mapping: The address_space to search * @mapping: The address_space to search
* @start: The starting page index * @start: The starting page index
* @end: The final page index (inclusive)
* @nr_pages: The maximum number of pages * @nr_pages: The maximum number of pages
* @pages: Where the resulting pages are placed * @pages: Where the resulting pages are placed
* *
* find_get_pages() will search for and return a group of up to * find_get_pages_range() will search for and return a group of up to @nr_pages
* @nr_pages pages in the mapping. The pages are placed at @pages. * pages in the mapping starting at index @start and up to index @end
* find_get_pages() takes a reference against the returned pages. * (inclusive). The pages are placed at @pages. find_get_pages_range() takes
* a reference against the returned pages.
* *
* The search returns a group of mapping-contiguous pages with ascending * The search returns a group of mapping-contiguous pages with ascending
* indexes. There may be holes in the indices due to not-present pages. * indexes. There may be holes in the indices due to not-present pages.
* We also update @start to index the next page for the traversal. * We also update @start to index the next page for the traversal.
* *
* find_get_pages() returns the number of pages which were found. * find_get_pages_range() returns the number of pages which were found. If this
* number is smaller than @nr_pages, the end of specified range has been
* reached.
*/ */
unsigned find_get_pages(struct address_space *mapping, pgoff_t *start, unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
unsigned int nr_pages, struct page **pages) pgoff_t end, unsigned int nr_pages,
struct page **pages)
{ {
struct radix_tree_iter iter; struct radix_tree_iter iter;
void **slot; void **slot;
...@@ -1586,6 +1591,9 @@ unsigned find_get_pages(struct address_space *mapping, pgoff_t *start, ...@@ -1586,6 +1591,9 @@ unsigned find_get_pages(struct address_space *mapping, pgoff_t *start,
rcu_read_lock(); rcu_read_lock();
radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, *start) { radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, *start) {
struct page *head, *page; struct page *head, *page;
if (iter.index > end)
break;
repeat: repeat:
page = radix_tree_deref_slot(slot); page = radix_tree_deref_slot(slot);
if (unlikely(!page)) if (unlikely(!page))
...@@ -1621,15 +1629,25 @@ unsigned find_get_pages(struct address_space *mapping, pgoff_t *start, ...@@ -1621,15 +1629,25 @@ unsigned find_get_pages(struct address_space *mapping, pgoff_t *start,
} }
pages[ret] = page; pages[ret] = page;
if (++ret == nr_pages) if (++ret == nr_pages) {
break; *start = pages[ret - 1]->index + 1;
goto out;
}
} }
/*
* We come here when there is no page beyond @end. We take care to not
* overflow the index @start as it confuses some of the callers. This
* breaks the iteration when there is page at index -1 but that is
* already broken anyway.
*/
if (end == (pgoff_t)-1)
*start = (pgoff_t)-1;
else
*start = end + 1;
out:
rcu_read_unlock(); rcu_read_unlock();
if (ret)
*start = pages[ret - 1]->index + 1;
return ret; return ret;
} }
......
...@@ -946,29 +946,35 @@ void pagevec_remove_exceptionals(struct pagevec *pvec) ...@@ -946,29 +946,35 @@ void pagevec_remove_exceptionals(struct pagevec *pvec)
} }
/** /**
* pagevec_lookup - gang pagecache lookup * pagevec_lookup_range - gang pagecache lookup
* @pvec: Where the resulting pages are placed * @pvec: Where the resulting pages are placed
* @mapping: The address_space to search * @mapping: The address_space to search
* @start: The starting page index * @start: The starting page index
* @end: The final page index
* @nr_pages: The maximum number of pages * @nr_pages: The maximum number of pages
* *
* pagevec_lookup() will search for and return a group of up to @nr_pages pages * pagevec_lookup_range() will search for and return a group of up to @nr_pages
* in the mapping. The pages are placed in @pvec. pagevec_lookup() takes a * pages in the mapping starting from index @start and upto index @end
* (inclusive). The pages are placed in @pvec. pagevec_lookup() takes a
* reference against the pages in @pvec. * reference against the pages in @pvec.
* *
* The search returns a group of mapping-contiguous pages with ascending * The search returns a group of mapping-contiguous pages with ascending
* indexes. There may be holes in the indices due to not-present pages. We * indexes. There may be holes in the indices due to not-present pages. We
* also update @start to index the next page for the traversal. * also update @start to index the next page for the traversal.
* *
* pagevec_lookup() returns the number of pages which were found. * pagevec_lookup_range() returns the number of pages which were found. If this
* number is smaller than @nr_pages, the end of specified range has been
* reached.
*/ */
unsigned pagevec_lookup(struct pagevec *pvec, struct address_space *mapping, unsigned pagevec_lookup_range(struct pagevec *pvec,
pgoff_t *start, unsigned nr_pages) struct address_space *mapping, pgoff_t *start, pgoff_t end,
unsigned nr_pages)
{ {
pvec->nr = find_get_pages(mapping, start, nr_pages, pvec->pages); pvec->nr = find_get_pages_range(mapping, start, end, nr_pages,
pvec->pages);
return pagevec_count(pvec); return pagevec_count(pvec);
} }
EXPORT_SYMBOL(pagevec_lookup); EXPORT_SYMBOL(pagevec_lookup_range);
unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping, unsigned pagevec_lookup_tag(struct pagevec *pvec, struct address_space *mapping,
pgoff_t *index, int tag, unsigned nr_pages) pgoff_t *index, int tag, unsigned nr_pages)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment