xref: /linux/include/linux/pagevec.h (revision 2da68a77)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * include/linux/pagevec.h
4  *
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.
7  */
8 
9 #ifndef _LINUX_PAGEVEC_H
10 #define _LINUX_PAGEVEC_H
11 
12 #include <linux/xarray.h>
13 
14 /* 15 pointers + header align the pagevec structure to a power of two */
15 #define PAGEVEC_SIZE	15
16 
17 struct page;
18 struct folio;
19 struct address_space;
20 
21 /* Layout must match folio_batch */
22 struct pagevec {
23 	unsigned char nr;
24 	bool percpu_pvec_drained;
25 	struct page *pages[PAGEVEC_SIZE];
26 };
27 
28 void __pagevec_release(struct pagevec *pvec);
29 unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
30 		struct address_space *mapping, pgoff_t *index, pgoff_t end,
31 		xa_mark_t tag);
32 static inline unsigned pagevec_lookup_tag(struct pagevec *pvec,
33 		struct address_space *mapping, pgoff_t *index, xa_mark_t tag)
34 {
35 	return pagevec_lookup_range_tag(pvec, mapping, index, (pgoff_t)-1, tag);
36 }
37 
38 static inline void pagevec_init(struct pagevec *pvec)
39 {
40 	pvec->nr = 0;
41 	pvec->percpu_pvec_drained = false;
42 }
43 
44 static inline void pagevec_reinit(struct pagevec *pvec)
45 {
46 	pvec->nr = 0;
47 }
48 
49 static inline unsigned pagevec_count(struct pagevec *pvec)
50 {
51 	return pvec->nr;
52 }
53 
54 static inline unsigned pagevec_space(struct pagevec *pvec)
55 {
56 	return PAGEVEC_SIZE - pvec->nr;
57 }
58 
59 /*
60  * Add a page to a pagevec.  Returns the number of slots still available.
61  */
62 static inline unsigned pagevec_add(struct pagevec *pvec, struct page *page)
63 {
64 	pvec->pages[pvec->nr++] = page;
65 	return pagevec_space(pvec);
66 }
67 
68 static inline void pagevec_release(struct pagevec *pvec)
69 {
70 	if (pagevec_count(pvec))
71 		__pagevec_release(pvec);
72 }
73 
74 /**
75  * struct folio_batch - A collection of folios.
76  *
77  * The folio_batch is used to amortise the cost of retrieving and
78  * operating on a set of folios.  The order of folios in the batch may be
79  * significant (eg delete_from_page_cache_batch()).  Some users of the
80  * folio_batch store "exceptional" entries in it which can be removed
81  * by calling folio_batch_remove_exceptionals().
82  */
83 struct folio_batch {
84 	unsigned char nr;
85 	bool percpu_pvec_drained;
86 	struct folio *folios[PAGEVEC_SIZE];
87 };
88 
89 /* Layout must match pagevec */
90 static_assert(sizeof(struct pagevec) == sizeof(struct folio_batch));
91 static_assert(offsetof(struct pagevec, pages) ==
92 		offsetof(struct folio_batch, folios));
93 
94 /**
95  * folio_batch_init() - Initialise a batch of folios
96  * @fbatch: The folio batch.
97  *
98  * A freshly initialised folio_batch contains zero folios.
99  */
100 static inline void folio_batch_init(struct folio_batch *fbatch)
101 {
102 	fbatch->nr = 0;
103 	fbatch->percpu_pvec_drained = false;
104 }
105 
106 static inline unsigned int folio_batch_count(struct folio_batch *fbatch)
107 {
108 	return fbatch->nr;
109 }
110 
111 static inline unsigned int fbatch_space(struct folio_batch *fbatch)
112 {
113 	return PAGEVEC_SIZE - fbatch->nr;
114 }
115 
116 /**
117  * folio_batch_add() - Add a folio to a batch.
118  * @fbatch: The folio batch.
119  * @folio: The folio to add.
120  *
121  * The folio is added to the end of the batch.
122  * The batch must have previously been initialised using folio_batch_init().
123  *
124  * Return: The number of slots still available.
125  */
126 static inline unsigned folio_batch_add(struct folio_batch *fbatch,
127 		struct folio *folio)
128 {
129 	fbatch->folios[fbatch->nr++] = folio;
130 	return fbatch_space(fbatch);
131 }
132 
133 static inline void folio_batch_release(struct folio_batch *fbatch)
134 {
135 	pagevec_release((struct pagevec *)fbatch);
136 }
137 
138 void folio_batch_remove_exceptionals(struct folio_batch *fbatch);
139 #endif /* _LINUX_PAGEVEC_H */
140