]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
page cache: Convert hole search to XArray
authorMatthew Wilcox <willy@infradead.org>
Tue, 21 Nov 2017 19:07:06 +0000 (14:07 -0500)
committerMatthew Wilcox <willy@infradead.org>
Sun, 21 Oct 2018 14:46:33 +0000 (10:46 -0400)
The page cache offers the ability to search for a miss in the previous or
next N locations.  Rather than teach the XArray about the page cache's
definition of a miss, use xas_prev() and xas_next() to search the page
array.  This should be more efficient as it does not have to start the
lookup from the top for each index.

Signed-off-by: Matthew Wilcox <willy@infradead.org>
fs/nfs/blocklayout/blocklayout.c
include/linux/pagemap.h
mm/filemap.c
mm/readahead.c

index 06cb0c1d9aee37dff885f511fa4f8da6f5ec1914..d3781cd983f623657f961ddf0c2bc36227eb12f2 100644 (file)
@@ -896,7 +896,7 @@ static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
        end = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
        if (end != inode->i_mapping->nrpages) {
                rcu_read_lock();
-               end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
+               end = page_cache_next_miss(mapping, idx + 1, ULONG_MAX);
                rcu_read_unlock();
        }
 
index b1bd2186e6d2bdc9428580184f4baa0ae51ec287..cf9ad413eee9e22bd947bf29631e41b6cd0d888b 100644 (file)
@@ -241,9 +241,9 @@ static inline gfp_t readahead_gfp_mask(struct address_space *x)
 
 typedef int filler_t(void *, struct page *);
 
-pgoff_t page_cache_next_hole(struct address_space *mapping,
+pgoff_t page_cache_next_miss(struct address_space *mapping,
                             pgoff_t index, unsigned long max_scan);
-pgoff_t page_cache_prev_hole(struct address_space *mapping,
+pgoff_t page_cache_prev_miss(struct address_space *mapping,
                             pgoff_t index, unsigned long max_scan);
 
 #define FGP_ACCESSED           0x00000001
index 4de14e75c4ec98f3f4c6fa37244664b78c1e24c8..714d3d0f60f55000a60889921c3ea8ca9bab0242 100644 (file)
@@ -1326,86 +1326,76 @@ int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
 }
 
 /**
- * page_cache_next_hole - find the next hole (not-present entry)
- * @mapping: mapping
- * @index: index
- * @max_scan: maximum range to search
- *
- * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the
- * lowest indexed hole.
- *
- * Returns: the index of the hole if found, otherwise returns an index
- * outside of the set specified (in which case 'return - index >=
- * max_scan' will be true). In rare cases of index wrap-around, 0 will
- * be returned.
- *
- * page_cache_next_hole may be called under rcu_read_lock. However,
- * like radix_tree_gang_lookup, this will not atomically search a
- * snapshot of the tree at a single point in time. For example, if a
- * hole is created at index 5, then subsequently a hole is created at
- * index 10, page_cache_next_hole covering both indexes may return 10
- * if called under rcu_read_lock.
+ * page_cache_next_miss() - Find the next gap in the page cache.
+ * @mapping: Mapping.
+ * @index: Index.
+ * @max_scan: Maximum range to search.
+ *
+ * Search the range [index, min(index + max_scan - 1, ULONG_MAX)] for the
+ * gap with the lowest index.
+ *
+ * This function may be called under the rcu_read_lock.  However, this will
+ * not atomically search a snapshot of the cache at a single point in time.
+ * For example, if a gap is created at index 5, then subsequently a gap is
+ * created at index 10, page_cache_next_miss covering both indices may
+ * return 10 if called under the rcu_read_lock.
+ *
+ * Return: The index of the gap if found, otherwise an index outside the
+ * range specified (in which case 'return - index >= max_scan' will be true).
+ * In the rare case of index wrap-around, 0 will be returned.
  */
-pgoff_t page_cache_next_hole(struct address_space *mapping,
+pgoff_t page_cache_next_miss(struct address_space *mapping,
                             pgoff_t index, unsigned long max_scan)
 {
-       unsigned long i;
+       XA_STATE(xas, &mapping->i_pages, index);
 
-       for (i = 0; i < max_scan; i++) {
-               struct page *page;
-
-               page = radix_tree_lookup(&mapping->i_pages, index);
-               if (!page || xa_is_value(page))
+       while (max_scan--) {
+               void *entry = xas_next(&xas);
+               if (!entry || xa_is_value(entry))
                        break;
-               index++;
-               if (index == 0)
+               if (xas.xa_index == 0)
                        break;
        }
 
-       return index;
+       return xas.xa_index;
 }
-EXPORT_SYMBOL(page_cache_next_hole);
+EXPORT_SYMBOL(page_cache_next_miss);
 
 /**
- * page_cache_prev_hole - find the prev hole (not-present entry)
- * @mapping: mapping
- * @index: index
- * @max_scan: maximum range to search
- *
- * Search backwards in the range [max(index-max_scan+1, 0), index] for
- * the first hole.
- *
- * Returns: the index of the hole if found, otherwise returns an index
- * outside of the set specified (in which case 'index - return >=
- * max_scan' will be true). In rare cases of wrap-around, ULONG_MAX
- * will be returned.
- *
- * page_cache_prev_hole may be called under rcu_read_lock. However,
- * like radix_tree_gang_lookup, this will not atomically search a
- * snapshot of the tree at a single point in time. For example, if a
- * hole is created at index 10, then subsequently a hole is created at
- * index 5, page_cache_prev_hole covering both indexes may return 5 if
- * called under rcu_read_lock.
+ * page_cache_prev_miss() - Find the next gap in the page cache.
+ * @mapping: Mapping.
+ * @index: Index.
+ * @max_scan: Maximum range to search.
+ *
+ * Search the range [max(index - max_scan + 1, 0), index] for the
+ * gap with the highest index.
+ *
+ * This function may be called under the rcu_read_lock.  However, this will
+ * not atomically search a snapshot of the cache at a single point in time.
+ * For example, if a gap is created at index 10, then subsequently a gap is
+ * created at index 5, page_cache_prev_miss() covering both indices may
+ * return 5 if called under the rcu_read_lock.
+ *
+ * Return: The index of the gap if found, otherwise an index outside the
+ * range specified (in which case 'index - return >= max_scan' will be true).
+ * In the rare case of wrap-around, ULONG_MAX will be returned.
  */
-pgoff_t page_cache_prev_hole(struct address_space *mapping,
+pgoff_t page_cache_prev_miss(struct address_space *mapping,
                             pgoff_t index, unsigned long max_scan)
 {
-       unsigned long i;
-
-       for (i = 0; i < max_scan; i++) {
-               struct page *page;
+       XA_STATE(xas, &mapping->i_pages, index);
 
-               page = radix_tree_lookup(&mapping->i_pages, index);
-               if (!page || xa_is_value(page))
+       while (max_scan--) {
+               void *entry = xas_prev(&xas);
+               if (!entry || xa_is_value(entry))
                        break;
-               index--;
-               if (index == ULONG_MAX)
+               if (xas.xa_index == ULONG_MAX)
                        break;
        }
 
-       return index;
+       return xas.xa_index;
 }
-EXPORT_SYMBOL(page_cache_prev_hole);
+EXPORT_SYMBOL(page_cache_prev_miss);
 
 /**
  * find_get_entry - find and get a page cache entry
index de657077d41d63996806fdead358bbeedfbf59ae..fc4dd364b37a67646845b235dec9240c6731cc20 100644 (file)
@@ -336,7 +336,7 @@ static pgoff_t count_history_pages(struct address_space *mapping,
        pgoff_t head;
 
        rcu_read_lock();
-       head = page_cache_prev_hole(mapping, offset - 1, max);
+       head = page_cache_prev_miss(mapping, offset - 1, max);
        rcu_read_unlock();
 
        return offset - 1 - head;
@@ -425,7 +425,7 @@ ondemand_readahead(struct address_space *mapping,
                pgoff_t start;
 
                rcu_read_lock();
-               start = page_cache_next_hole(mapping, offset + 1, max_pages);
+               start = page_cache_next_miss(mapping, offset + 1, max_pages);
                rcu_read_unlock();
 
                if (!start || start - offset > max_pages)