[PATCH] mm: page_alloc less atomics
[linux-2.6.git] / include / linux / page-flags.h
1 /*
2  * Macros for manipulating and testing page->flags
3  */
4
5 #ifndef PAGE_FLAGS_H
6 #define PAGE_FLAGS_H
7
8 #include <linux/percpu.h>
9 #include <linux/cache.h>
10 #include <asm/pgtable.h>
11
12 /*
13  * Various page->flags bits:
14  *
15  * PG_reserved is set for special pages, which can never be swapped out. Some
16  * of them might not even exist (eg empty_bad_page)...
17  *
18  * The PG_private bitflag is set if page->private contains a valid value.
19  *
20  * During disk I/O, PG_locked is used. This bit is set before I/O and
21  * reset when I/O completes. page_waitqueue(page) is a wait queue of all tasks
22  * waiting for the I/O on this page to complete.
23  *
24  * PG_uptodate tells whether the page's contents is valid.  When a read
25  * completes, the page becomes uptodate, unless a disk I/O error happened.
26  *
27  * For choosing which pages to swap out, inode pages carry a PG_referenced bit,
28  * which is set any time the system accesses that page through the (mapping,
29  * index) hash table.  This referenced bit, together with the referenced bit
30  * in the page tables, is used to manipulate page->age and move the page across
31  * the active, inactive_dirty and inactive_clean lists.
32  *
33  * Note that the referenced bit, the page->lru list_head and the active,
34  * inactive_dirty and inactive_clean lists are protected by the
35  * zone->lru_lock, and *NOT* by the usual PG_locked bit!
36  *
37  * PG_error is set to indicate that an I/O error occurred on this page.
38  *
39  * PG_arch_1 is an architecture specific page state bit.  The generic code
40  * guarantees that this bit is cleared for a page when it first is entered into
41  * the page cache.
42  *
43  * PG_highmem pages are not permanently mapped into the kernel virtual address
44  * space, they need to be kmapped separately for doing IO on the pages.  The
45  * struct page (these bits with information) are always mapped into kernel
46  * address space...
47  */
48
49 /*
50  * Don't use the *_dontuse flags.  Use the macros.  Otherwise you'll break
51  * locked- and dirty-page accounting.  The top eight bits of page->flags are
52  * used for page->zone, so putting flag bits there doesn't work.
53  */
54 #define PG_locked                0      /* Page is locked. Don't touch. */
55 #define PG_error                 1
56 #define PG_referenced            2
57 #define PG_uptodate              3
58
59 #define PG_dirty                 4
60 #define PG_lru                   5
61 #define PG_active                6
62 #define PG_slab                  7      /* slab debug (Suparna wants this) */
63
64 #define PG_checked               8      /* kill me in 2.5.<early>. */
65 #define PG_arch_1                9
66 #define PG_reserved             10
67 #define PG_private              11      /* Has something at ->private */
68
69 #define PG_writeback            12      /* Page is under writeback */
70 #define PG_nosave               13      /* Used for system suspend/resume */
71 #define PG_compound             14      /* Part of a compound page */
72 #define PG_swapcache            15      /* Swap page: swp_entry_t in private */
73
74 #define PG_mappedtodisk         16      /* Has blocks allocated on-disk */
75 #define PG_reclaim              17      /* To be reclaimed asap */
76 #define PG_nosave_free          18      /* Free, should not be written */
77 #define PG_uncached             19      /* Page has been mapped as uncached */
78
79 /*
80  * Global page accounting.  One instance per CPU.  Only unsigned longs are
81  * allowed.
82  *
83  * - Fields can be modified with xxx_page_state and xxx_page_state_zone at
84  * any time safely (which protects the instance from modification by
85  * interrupt.
86  * - The __xxx_page_state variants can be used safely when interrupts are
87  * disabled.
88  * - The __xxx_page_state variants can be used if the field is only
89  * modified from process context, or only modified from interrupt context.
90  * In this case, the field should be commented here.
91  */
92 struct page_state {
93         unsigned long nr_dirty;         /* Dirty writeable pages */
94         unsigned long nr_writeback;     /* Pages under writeback */
95         unsigned long nr_unstable;      /* NFS unstable pages */
96         unsigned long nr_page_table_pages;/* Pages used for pagetables */
97         unsigned long nr_mapped;        /* mapped into pagetables.
98                                          * only modified from process context */
99         unsigned long nr_slab;          /* In slab */
100 #define GET_PAGE_STATE_LAST nr_slab
101
102         /*
103          * The below are zeroed by get_page_state().  Use get_full_page_state()
104          * to add up all these.
105          */
106         unsigned long pgpgin;           /* Disk reads */
107         unsigned long pgpgout;          /* Disk writes */
108         unsigned long pswpin;           /* swap reads */
109         unsigned long pswpout;          /* swap writes */
110
111         unsigned long pgalloc_high;     /* page allocations */
112         unsigned long pgalloc_normal;
113         unsigned long pgalloc_dma32;
114         unsigned long pgalloc_dma;
115
116         unsigned long pgfree;           /* page freeings */
117         unsigned long pgactivate;       /* pages moved inactive->active */
118         unsigned long pgdeactivate;     /* pages moved active->inactive */
119
120         unsigned long pgfault;          /* faults (major+minor) */
121         unsigned long pgmajfault;       /* faults (major only) */
122
123         unsigned long pgrefill_high;    /* inspected in refill_inactive_zone */
124         unsigned long pgrefill_normal;
125         unsigned long pgrefill_dma32;
126         unsigned long pgrefill_dma;
127
128         unsigned long pgsteal_high;     /* total highmem pages reclaimed */
129         unsigned long pgsteal_normal;
130         unsigned long pgsteal_dma32;
131         unsigned long pgsteal_dma;
132
133         unsigned long pgscan_kswapd_high;/* total highmem pages scanned */
134         unsigned long pgscan_kswapd_normal;
135         unsigned long pgscan_kswapd_dma32;
136         unsigned long pgscan_kswapd_dma;
137
138         unsigned long pgscan_direct_high;/* total highmem pages scanned */
139         unsigned long pgscan_direct_normal;
140         unsigned long pgscan_direct_dma32;
141         unsigned long pgscan_direct_dma;
142
143         unsigned long pginodesteal;     /* pages reclaimed via inode freeing */
144         unsigned long slabs_scanned;    /* slab objects scanned */
145         unsigned long kswapd_steal;     /* pages reclaimed by kswapd */
146         unsigned long kswapd_inodesteal;/* reclaimed via kswapd inode freeing */
147         unsigned long pageoutrun;       /* kswapd's calls to page reclaim */
148         unsigned long allocstall;       /* direct reclaim calls */
149
150         unsigned long pgrotated;        /* pages rotated to tail of the LRU */
151         unsigned long nr_bounce;        /* pages for bounce buffers */
152 };
153
154 extern void get_page_state(struct page_state *ret);
155 extern void get_page_state_node(struct page_state *ret, int node);
156 extern void get_full_page_state(struct page_state *ret);
157 extern unsigned long read_page_state_offset(unsigned long offset);
158 extern void mod_page_state_offset(unsigned long offset, unsigned long delta);
159 extern void __mod_page_state_offset(unsigned long offset, unsigned long delta);
160
161 #define read_page_state(member) \
162         read_page_state_offset(offsetof(struct page_state, member))
163
164 #define mod_page_state(member, delta)   \
165         mod_page_state_offset(offsetof(struct page_state, member), (delta))
166
167 #define __mod_page_state(member, delta) \
168         __mod_page_state_offset(offsetof(struct page_state, member), (delta))
169
170 #define inc_page_state(member)          mod_page_state(member, 1UL)
171 #define dec_page_state(member)          mod_page_state(member, 0UL - 1)
172 #define add_page_state(member,delta)    mod_page_state(member, (delta))
173 #define sub_page_state(member,delta)    mod_page_state(member, 0UL - (delta))
174
175 #define __inc_page_state(member)        __mod_page_state(member, 1UL)
176 #define __dec_page_state(member)        __mod_page_state(member, 0UL - 1)
177 #define __add_page_state(member,delta)  __mod_page_state(member, (delta))
178 #define __sub_page_state(member,delta)  __mod_page_state(member, 0UL - (delta))
179
180 #define page_state(member) (*__page_state(offsetof(struct page_state, member)))
181
182 #define state_zone_offset(zone, member)                                 \
183 ({                                                                      \
184         unsigned offset;                                                \
185         if (is_highmem(zone))                                           \
186                 offset = offsetof(struct page_state, member##_high);    \
187         else if (is_normal(zone))                                       \
188                 offset = offsetof(struct page_state, member##_normal);  \
189         else if (is_dma32(zone))                                        \
190                 offset = offsetof(struct page_state, member##_dma32);   \
191         else                                                            \
192                 offset = offsetof(struct page_state, member##_dma);     \
193         offset;                                                         \
194 })
195
196 #define __mod_page_state_zone(zone, member, delta)                      \
197  do {                                                                   \
198         __mod_page_state_offset(state_zone_offset(zone, member), (delta)); \
199  } while (0)
200
201 #define mod_page_state_zone(zone, member, delta)                        \
202  do {                                                                   \
203         mod_page_state_offset(state_zone_offset(zone, member), (delta)); \
204  } while (0)
205
206 /*
207  * Manipulation of page state flags
208  */
209 #define PageLocked(page)                \
210                 test_bit(PG_locked, &(page)->flags)
211 #define SetPageLocked(page)             \
212                 set_bit(PG_locked, &(page)->flags)
213 #define TestSetPageLocked(page)         \
214                 test_and_set_bit(PG_locked, &(page)->flags)
215 #define ClearPageLocked(page)           \
216                 clear_bit(PG_locked, &(page)->flags)
217 #define TestClearPageLocked(page)       \
218                 test_and_clear_bit(PG_locked, &(page)->flags)
219
220 #define PageError(page)         test_bit(PG_error, &(page)->flags)
221 #define SetPageError(page)      set_bit(PG_error, &(page)->flags)
222 #define ClearPageError(page)    clear_bit(PG_error, &(page)->flags)
223
224 #define PageReferenced(page)    test_bit(PG_referenced, &(page)->flags)
225 #define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
226 #define ClearPageReferenced(page)       clear_bit(PG_referenced, &(page)->flags)
227 #define TestClearPageReferenced(page) test_and_clear_bit(PG_referenced, &(page)->flags)
228
229 #define PageUptodate(page)      test_bit(PG_uptodate, &(page)->flags)
230 #ifndef SetPageUptodate
231 #define SetPageUptodate(page)   set_bit(PG_uptodate, &(page)->flags)
232 #endif
233 #define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
234
235 #define PageDirty(page)         test_bit(PG_dirty, &(page)->flags)
236 #define SetPageDirty(page)      set_bit(PG_dirty, &(page)->flags)
237 #define TestSetPageDirty(page)  test_and_set_bit(PG_dirty, &(page)->flags)
238 #define ClearPageDirty(page)    clear_bit(PG_dirty, &(page)->flags)
239 #define __ClearPageDirty(page)  __clear_bit(PG_dirty, &(page)->flags)
240 #define TestClearPageDirty(page) test_and_clear_bit(PG_dirty, &(page)->flags)
241
242 #define PageLRU(page)           test_bit(PG_lru, &(page)->flags)
243 #define SetPageLRU(page)        set_bit(PG_lru, &(page)->flags)
244 #define ClearPageLRU(page)      clear_bit(PG_lru, &(page)->flags)
245 #define __ClearPageLRU(page)    __clear_bit(PG_lru, &(page)->flags)
246
247 #define PageActive(page)        test_bit(PG_active, &(page)->flags)
248 #define SetPageActive(page)     set_bit(PG_active, &(page)->flags)
249 #define ClearPageActive(page)   clear_bit(PG_active, &(page)->flags)
250 #define __ClearPageActive(page) __clear_bit(PG_active, &(page)->flags)
251
252 #define PageSlab(page)          test_bit(PG_slab, &(page)->flags)
253 #define SetPageSlab(page)       set_bit(PG_slab, &(page)->flags)
254 #define ClearPageSlab(page)     clear_bit(PG_slab, &(page)->flags)
255 #define TestClearPageSlab(page) test_and_clear_bit(PG_slab, &(page)->flags)
256 #define TestSetPageSlab(page)   test_and_set_bit(PG_slab, &(page)->flags)
257
258 #ifdef CONFIG_HIGHMEM
259 #define PageHighMem(page)       is_highmem(page_zone(page))
260 #else
261 #define PageHighMem(page)       0 /* needed to optimize away at compile time */
262 #endif
263
264 #define PageChecked(page)       test_bit(PG_checked, &(page)->flags)
265 #define SetPageChecked(page)    set_bit(PG_checked, &(page)->flags)
266 #define ClearPageChecked(page)  clear_bit(PG_checked, &(page)->flags)
267
268 #define PageReserved(page)      test_bit(PG_reserved, &(page)->flags)
269 #define SetPageReserved(page)   set_bit(PG_reserved, &(page)->flags)
270 #define ClearPageReserved(page) clear_bit(PG_reserved, &(page)->flags)
271 #define __ClearPageReserved(page)       __clear_bit(PG_reserved, &(page)->flags)
272
273 #define SetPagePrivate(page)    set_bit(PG_private, &(page)->flags)
274 #define ClearPagePrivate(page)  clear_bit(PG_private, &(page)->flags)
275 #define PagePrivate(page)       test_bit(PG_private, &(page)->flags)
276 #define __SetPagePrivate(page)  __set_bit(PG_private, &(page)->flags)
277 #define __ClearPagePrivate(page) __clear_bit(PG_private, &(page)->flags)
278
279 #define PageWriteback(page)     test_bit(PG_writeback, &(page)->flags)
280 #define SetPageWriteback(page)                                          \
281         do {                                                            \
282                 if (!test_and_set_bit(PG_writeback,                     \
283                                 &(page)->flags))                        \
284                         inc_page_state(nr_writeback);                   \
285         } while (0)
286 #define TestSetPageWriteback(page)                                      \
287         ({                                                              \
288                 int ret;                                                \
289                 ret = test_and_set_bit(PG_writeback,                    \
290                                         &(page)->flags);                \
291                 if (!ret)                                               \
292                         inc_page_state(nr_writeback);                   \
293                 ret;                                                    \
294         })
295 #define ClearPageWriteback(page)                                        \
296         do {                                                            \
297                 if (test_and_clear_bit(PG_writeback,                    \
298                                 &(page)->flags))                        \
299                         dec_page_state(nr_writeback);                   \
300         } while (0)
301 #define TestClearPageWriteback(page)                                    \
302         ({                                                              \
303                 int ret;                                                \
304                 ret = test_and_clear_bit(PG_writeback,                  \
305                                 &(page)->flags);                        \
306                 if (ret)                                                \
307                         dec_page_state(nr_writeback);                   \
308                 ret;                                                    \
309         })
310
311 #define PageNosave(page)        test_bit(PG_nosave, &(page)->flags)
312 #define SetPageNosave(page)     set_bit(PG_nosave, &(page)->flags)
313 #define TestSetPageNosave(page) test_and_set_bit(PG_nosave, &(page)->flags)
314 #define ClearPageNosave(page)           clear_bit(PG_nosave, &(page)->flags)
315 #define TestClearPageNosave(page)       test_and_clear_bit(PG_nosave, &(page)->flags)
316
317 #define PageNosaveFree(page)    test_bit(PG_nosave_free, &(page)->flags)
318 #define SetPageNosaveFree(page) set_bit(PG_nosave_free, &(page)->flags)
319 #define ClearPageNosaveFree(page)               clear_bit(PG_nosave_free, &(page)->flags)
320
321 #define PageMappedToDisk(page)  test_bit(PG_mappedtodisk, &(page)->flags)
322 #define SetPageMappedToDisk(page) set_bit(PG_mappedtodisk, &(page)->flags)
323 #define ClearPageMappedToDisk(page) clear_bit(PG_mappedtodisk, &(page)->flags)
324
325 #define PageReclaim(page)       test_bit(PG_reclaim, &(page)->flags)
326 #define SetPageReclaim(page)    set_bit(PG_reclaim, &(page)->flags)
327 #define ClearPageReclaim(page)  clear_bit(PG_reclaim, &(page)->flags)
328 #define TestClearPageReclaim(page) test_and_clear_bit(PG_reclaim, &(page)->flags)
329
330 #define PageCompound(page)      test_bit(PG_compound, &(page)->flags)
331 #define __SetPageCompound(page) __set_bit(PG_compound, &(page)->flags)
332 #define __ClearPageCompound(page) __clear_bit(PG_compound, &(page)->flags)
333
334 #ifdef CONFIG_SWAP
335 #define PageSwapCache(page)     test_bit(PG_swapcache, &(page)->flags)
336 #define SetPageSwapCache(page)  set_bit(PG_swapcache, &(page)->flags)
337 #define ClearPageSwapCache(page) clear_bit(PG_swapcache, &(page)->flags)
338 #else
339 #define PageSwapCache(page)     0
340 #endif
341
342 #define PageUncached(page)      test_bit(PG_uncached, &(page)->flags)
343 #define SetPageUncached(page)   set_bit(PG_uncached, &(page)->flags)
344 #define ClearPageUncached(page) clear_bit(PG_uncached, &(page)->flags)
345
346 struct page;    /* forward declaration */
347
348 int test_clear_page_dirty(struct page *page);
349 int test_clear_page_writeback(struct page *page);
350 int test_set_page_writeback(struct page *page);
351
352 static inline void clear_page_dirty(struct page *page)
353 {
354         test_clear_page_dirty(page);
355 }
356
357 static inline void set_page_writeback(struct page *page)
358 {
359         test_set_page_writeback(page);
360 }
361
362 #endif  /* PAGE_FLAGS_H */