1 /* SPDX-License-Identifier: GPL-2.0 */
3 * include/linux/pagevec.h
5 * In many places it is efficient to batch an operation up against multiple
6 * pages. A pagevec is a multipage container which is used for that.
9 #ifndef _LINUX_PAGEVEC_H
10 #define _LINUX_PAGEVEC_H
12 #include <linux/xarray.h>
14 /* 15 pointers + header align the pagevec structure to a power of two */
15 #define PAGEVEC_SIZE 15
21 /* Layout must match folio_batch */
24 bool percpu_pvec_drained;
25 struct page *pages[PAGEVEC_SIZE];
28 void __pagevec_release(struct pagevec *pvec);
29 void __pagevec_lru_add(struct pagevec *pvec);
30 unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
31 struct address_space *mapping, pgoff_t *index, pgoff_t end,
33 static inline unsigned pagevec_lookup_tag(struct pagevec *pvec,
34 struct address_space *mapping, pgoff_t *index, xa_mark_t tag)
36 return pagevec_lookup_range_tag(pvec, mapping, index, (pgoff_t)-1, tag);
39 static inline void pagevec_init(struct pagevec *pvec)
42 pvec->percpu_pvec_drained = false;
45 static inline void pagevec_reinit(struct pagevec *pvec)
50 static inline unsigned pagevec_count(struct pagevec *pvec)
55 static inline unsigned pagevec_space(struct pagevec *pvec)
57 return PAGEVEC_SIZE - pvec->nr;
61 * Add a page to a pagevec. Returns the number of slots still available.
63 static inline unsigned pagevec_add(struct pagevec *pvec, struct page *page)
65 pvec->pages[pvec->nr++] = page;
66 return pagevec_space(pvec);
69 static inline void pagevec_release(struct pagevec *pvec)
71 if (pagevec_count(pvec))
72 __pagevec_release(pvec);
76 * struct folio_batch - A collection of folios.
78 * The folio_batch is used to amortise the cost of retrieving and
79 * operating on a set of folios. The order of folios in the batch may be
80 * significant (eg delete_from_page_cache_batch()). Some users of the
81 * folio_batch store "exceptional" entries in it which can be removed
82 * by calling folio_batch_remove_exceptionals().
86 bool percpu_pvec_drained;
87 struct folio *folios[PAGEVEC_SIZE];
90 /* Layout must match pagevec */
91 static_assert(sizeof(struct pagevec) == sizeof(struct folio_batch));
92 static_assert(offsetof(struct pagevec, pages) ==
93 offsetof(struct folio_batch, folios));
96 * folio_batch_init() - Initialise a batch of folios
97 * @fbatch: The folio batch.
99 * A freshly initialised folio_batch contains zero folios.
101 static inline void folio_batch_init(struct folio_batch *fbatch)
104 fbatch->percpu_pvec_drained = false;
107 static inline unsigned int folio_batch_count(struct folio_batch *fbatch)
112 static inline unsigned int fbatch_space(struct folio_batch *fbatch)
114 return PAGEVEC_SIZE - fbatch->nr;
118 * folio_batch_add() - Add a folio to a batch.
119 * @fbatch: The folio batch.
120 * @folio: The folio to add.
122 * The folio is added to the end of the batch.
123 * The batch must have previously been initialised using folio_batch_init().
125 * Return: The number of slots still available.
127 static inline unsigned folio_batch_add(struct folio_batch *fbatch,
130 fbatch->folios[fbatch->nr++] = folio;
131 return fbatch_space(fbatch);
134 static inline void folio_batch_release(struct folio_batch *fbatch)
136 pagevec_release((struct pagevec *)fbatch);
139 void folio_batch_remove_exceptionals(struct folio_batch *fbatch);
140 #endif /* _LINUX_PAGEVEC_H */