blob: 547aac7696cd0485a2c2fa1f511afae3fbd4256c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Badari Pulavartycbe37d02005-06-21 17:14:52 -070064#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Badari Pulavartycbe37d02005-06-21 17:14:52 -070069#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Badari Pulavartycbe37d02005-06-21 17:14:52 -070074#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 */
Nick Piggin676165a2006-04-10 11:21:48 +100077#define PG_buddy 19 /* Page is free, on buddy lists */
78
79#define PG_uncached 20 /* Page has been mapped as uncached */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * Global page accounting. One instance per CPU. Only unsigned longs are
83 * allowed.
Nick Pigginb09eb1c2006-01-06 00:11:21 -080084 *
85 * - Fields can be modified with xxx_page_state and xxx_page_state_zone at
86 * any time safely (which protects the instance from modification by
87 * interrupt.
88 * - The __xxx_page_state variants can be used safely when interrupts are
89 * disabled.
90 * - The __xxx_page_state variants can be used if the field is only
Nick Piggin9d414152006-03-22 00:08:06 -080091 * modified from process context and protected from preemption, or only
92 * modified from interrupt context. In this case, the field should be
93 * commented here.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95struct page_state {
96 unsigned long nr_dirty; /* Dirty writeable pages */
97 unsigned long nr_writeback; /* Pages under writeback */
98 unsigned long nr_unstable; /* NFS unstable pages */
99 unsigned long nr_page_table_pages;/* Pages used for pagetables */
Nick Pigginb09eb1c2006-01-06 00:11:21 -0800100 unsigned long nr_mapped; /* mapped into pagetables.
101 * only modified from process context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 unsigned long nr_slab; /* In slab */
103#define GET_PAGE_STATE_LAST nr_slab
104
105 /*
106 * The below are zeroed by get_page_state(). Use get_full_page_state()
107 * to add up all these.
108 */
109 unsigned long pgpgin; /* Disk reads */
110 unsigned long pgpgout; /* Disk writes */
111 unsigned long pswpin; /* swap reads */
112 unsigned long pswpout; /* swap writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Nick Piggin9328b8f2006-01-06 00:11:10 -0800114 unsigned long pgalloc_high; /* page allocations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned long pgalloc_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800116 unsigned long pgalloc_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long pgalloc_dma;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned long pgfree; /* page freeings */
120 unsigned long pgactivate; /* pages moved inactive->active */
121 unsigned long pgdeactivate; /* pages moved active->inactive */
122
123 unsigned long pgfault; /* faults (major+minor) */
124 unsigned long pgmajfault; /* faults (major only) */
Nick Piggin9328b8f2006-01-06 00:11:10 -0800125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long pgrefill_high; /* inspected in refill_inactive_zone */
127 unsigned long pgrefill_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800128 unsigned long pgrefill_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned long pgrefill_dma;
130
131 unsigned long pgsteal_high; /* total highmem pages reclaimed */
132 unsigned long pgsteal_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800133 unsigned long pgsteal_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned long pgsteal_dma;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long pgscan_kswapd_high;/* total highmem pages scanned */
137 unsigned long pgscan_kswapd_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800138 unsigned long pgscan_kswapd_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unsigned long pgscan_kswapd_dma;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned long pgscan_direct_high;/* total highmem pages scanned */
142 unsigned long pgscan_direct_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800143 unsigned long pgscan_direct_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long pgscan_direct_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Nick Piggin9328b8f2006-01-06 00:11:10 -0800146 unsigned long pginodesteal; /* pages reclaimed via inode freeing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 unsigned long slabs_scanned; /* slab objects scanned */
148 unsigned long kswapd_steal; /* pages reclaimed by kswapd */
149 unsigned long kswapd_inodesteal;/* reclaimed via kswapd inode freeing */
150 unsigned long pageoutrun; /* kswapd's calls to page reclaim */
151 unsigned long allocstall; /* direct reclaim calls */
152
153 unsigned long pgrotated; /* pages rotated to tail of the LRU */
KAMEZAWA Hiroyukiedfbe2b2005-05-01 08:58:37 -0700154 unsigned long nr_bounce; /* pages for bounce buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157extern void get_page_state(struct page_state *ret);
Martin Hicksc07e02d2005-09-03 15:55:11 -0700158extern void get_page_state_node(struct page_state *ret, int node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159extern void get_full_page_state(struct page_state *ret);
Nick Piggina74609f2006-01-06 00:11:20 -0800160extern unsigned long read_page_state_offset(unsigned long offset);
161extern void mod_page_state_offset(unsigned long offset, unsigned long delta);
162extern void __mod_page_state_offset(unsigned long offset, unsigned long delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164#define read_page_state(member) \
Nick Piggina74609f2006-01-06 00:11:20 -0800165 read_page_state_offset(offsetof(struct page_state, member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167#define mod_page_state(member, delta) \
Nick Piggina74609f2006-01-06 00:11:20 -0800168 mod_page_state_offset(offsetof(struct page_state, member), (delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Nick Piggina74609f2006-01-06 00:11:20 -0800170#define __mod_page_state(member, delta) \
171 __mod_page_state_offset(offsetof(struct page_state, member), (delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Nick Piggina74609f2006-01-06 00:11:20 -0800173#define inc_page_state(member) mod_page_state(member, 1UL)
174#define dec_page_state(member) mod_page_state(member, 0UL - 1)
175#define add_page_state(member,delta) mod_page_state(member, (delta))
176#define sub_page_state(member,delta) mod_page_state(member, 0UL - (delta))
177
178#define __inc_page_state(member) __mod_page_state(member, 1UL)
179#define __dec_page_state(member) __mod_page_state(member, 0UL - 1)
180#define __add_page_state(member,delta) __mod_page_state(member, (delta))
181#define __sub_page_state(member,delta) __mod_page_state(member, 0UL - (delta))
182
183#define page_state(member) (*__page_state(offsetof(struct page_state, member)))
184
185#define state_zone_offset(zone, member) \
186({ \
Nick Piggin9328b8f2006-01-06 00:11:10 -0800187 unsigned offset; \
188 if (is_highmem(zone)) \
189 offset = offsetof(struct page_state, member##_high); \
190 else if (is_normal(zone)) \
191 offset = offsetof(struct page_state, member##_normal); \
192 else if (is_dma32(zone)) \
193 offset = offsetof(struct page_state, member##_dma32); \
194 else \
195 offset = offsetof(struct page_state, member##_dma); \
Nick Piggina74609f2006-01-06 00:11:20 -0800196 offset; \
197})
198
199#define __mod_page_state_zone(zone, member, delta) \
200 do { \
201 __mod_page_state_offset(state_zone_offset(zone, member), (delta)); \
202 } while (0)
203
204#define mod_page_state_zone(zone, member, delta) \
205 do { \
206 mod_page_state_offset(state_zone_offset(zone, member), (delta)); \
Nick Piggin9328b8f2006-01-06 00:11:10 -0800207 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * Manipulation of page state flags
211 */
212#define PageLocked(page) \
213 test_bit(PG_locked, &(page)->flags)
214#define SetPageLocked(page) \
215 set_bit(PG_locked, &(page)->flags)
216#define TestSetPageLocked(page) \
217 test_and_set_bit(PG_locked, &(page)->flags)
218#define ClearPageLocked(page) \
219 clear_bit(PG_locked, &(page)->flags)
220#define TestClearPageLocked(page) \
221 test_and_clear_bit(PG_locked, &(page)->flags)
222
223#define PageError(page) test_bit(PG_error, &(page)->flags)
224#define SetPageError(page) set_bit(PG_error, &(page)->flags)
225#define ClearPageError(page) clear_bit(PG_error, &(page)->flags)
226
227#define PageReferenced(page) test_bit(PG_referenced, &(page)->flags)
228#define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
229#define ClearPageReferenced(page) clear_bit(PG_referenced, &(page)->flags)
230#define TestClearPageReferenced(page) test_and_clear_bit(PG_referenced, &(page)->flags)
231
232#define PageUptodate(page) test_bit(PG_uptodate, &(page)->flags)
233#ifndef SetPageUptodate
234#define SetPageUptodate(page) set_bit(PG_uptodate, &(page)->flags)
235#endif
236#define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
237
238#define PageDirty(page) test_bit(PG_dirty, &(page)->flags)
239#define SetPageDirty(page) set_bit(PG_dirty, &(page)->flags)
240#define TestSetPageDirty(page) test_and_set_bit(PG_dirty, &(page)->flags)
241#define ClearPageDirty(page) clear_bit(PG_dirty, &(page)->flags)
Nick Piggin242e5462005-09-03 15:54:50 -0700242#define __ClearPageDirty(page) __clear_bit(PG_dirty, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#define TestClearPageDirty(page) test_and_clear_bit(PG_dirty, &(page)->flags)
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define PageLRU(page) test_bit(PG_lru, &(page)->flags)
Nick Piggin8d438f92006-03-22 00:07:59 -0800246#define SetPageLRU(page) set_bit(PG_lru, &(page)->flags)
247#define ClearPageLRU(page) clear_bit(PG_lru, &(page)->flags)
Nick Piggin67453912006-03-22 00:08:00 -0800248#define __ClearPageLRU(page) __clear_bit(PG_lru, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250#define PageActive(page) test_bit(PG_active, &(page)->flags)
251#define SetPageActive(page) set_bit(PG_active, &(page)->flags)
252#define ClearPageActive(page) clear_bit(PG_active, &(page)->flags)
Nick Piggin67453912006-03-22 00:08:00 -0800253#define __ClearPageActive(page) __clear_bit(PG_active, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255#define PageSlab(page) test_bit(PG_slab, &(page)->flags)
Nick Pigginf205b2f2006-03-22 00:08:02 -0800256#define __SetPageSlab(page) __set_bit(PG_slab, &(page)->flags)
257#define __ClearPageSlab(page) __clear_bit(PG_slab, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259#ifdef CONFIG_HIGHMEM
Badari Pulavartycbe37d02005-06-21 17:14:52 -0700260#define PageHighMem(page) is_highmem(page_zone(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#else
262#define PageHighMem(page) 0 /* needed to optimize away at compile time */
263#endif
264
265#define PageChecked(page) test_bit(PG_checked, &(page)->flags)
266#define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
267#define ClearPageChecked(page) clear_bit(PG_checked, &(page)->flags)
268
269#define PageReserved(page) test_bit(PG_reserved, &(page)->flags)
270#define SetPageReserved(page) set_bit(PG_reserved, &(page)->flags)
271#define ClearPageReserved(page) clear_bit(PG_reserved, &(page)->flags)
272#define __ClearPageReserved(page) __clear_bit(PG_reserved, &(page)->flags)
273
274#define SetPagePrivate(page) set_bit(PG_private, &(page)->flags)
275#define ClearPagePrivate(page) clear_bit(PG_private, &(page)->flags)
276#define PagePrivate(page) test_bit(PG_private, &(page)->flags)
277#define __SetPagePrivate(page) __set_bit(PG_private, &(page)->flags)
278#define __ClearPagePrivate(page) __clear_bit(PG_private, &(page)->flags)
279
280#define PageWriteback(page) test_bit(PG_writeback, &(page)->flags)
281#define SetPageWriteback(page) \
282 do { \
283 if (!test_and_set_bit(PG_writeback, \
284 &(page)->flags)) \
285 inc_page_state(nr_writeback); \
286 } while (0)
287#define TestSetPageWriteback(page) \
288 ({ \
289 int ret; \
290 ret = test_and_set_bit(PG_writeback, \
291 &(page)->flags); \
292 if (!ret) \
293 inc_page_state(nr_writeback); \
294 ret; \
295 })
296#define ClearPageWriteback(page) \
297 do { \
298 if (test_and_clear_bit(PG_writeback, \
299 &(page)->flags)) \
300 dec_page_state(nr_writeback); \
301 } while (0)
302#define TestClearPageWriteback(page) \
303 ({ \
304 int ret; \
305 ret = test_and_clear_bit(PG_writeback, \
306 &(page)->flags); \
307 if (ret) \
308 dec_page_state(nr_writeback); \
309 ret; \
310 })
311
312#define PageNosave(page) test_bit(PG_nosave, &(page)->flags)
313#define SetPageNosave(page) set_bit(PG_nosave, &(page)->flags)
314#define TestSetPageNosave(page) test_and_set_bit(PG_nosave, &(page)->flags)
315#define ClearPageNosave(page) clear_bit(PG_nosave, &(page)->flags)
316#define TestClearPageNosave(page) test_and_clear_bit(PG_nosave, &(page)->flags)
317
318#define PageNosaveFree(page) test_bit(PG_nosave_free, &(page)->flags)
319#define SetPageNosaveFree(page) set_bit(PG_nosave_free, &(page)->flags)
320#define ClearPageNosaveFree(page) clear_bit(PG_nosave_free, &(page)->flags)
321
Nick Piggin676165a2006-04-10 11:21:48 +1000322#define PageBuddy(page) test_bit(PG_buddy, &(page)->flags)
323#define __SetPageBuddy(page) __set_bit(PG_buddy, &(page)->flags)
324#define __ClearPageBuddy(page) __clear_bit(PG_buddy, &(page)->flags)
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#define PageMappedToDisk(page) test_bit(PG_mappedtodisk, &(page)->flags)
327#define SetPageMappedToDisk(page) set_bit(PG_mappedtodisk, &(page)->flags)
328#define ClearPageMappedToDisk(page) clear_bit(PG_mappedtodisk, &(page)->flags)
329
330#define PageReclaim(page) test_bit(PG_reclaim, &(page)->flags)
331#define SetPageReclaim(page) set_bit(PG_reclaim, &(page)->flags)
332#define ClearPageReclaim(page) clear_bit(PG_reclaim, &(page)->flags)
333#define TestClearPageReclaim(page) test_and_clear_bit(PG_reclaim, &(page)->flags)
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#define PageCompound(page) test_bit(PG_compound, &(page)->flags)
Nick Piggin5e9dace2006-03-22 00:08:01 -0800336#define __SetPageCompound(page) __set_bit(PG_compound, &(page)->flags)
337#define __ClearPageCompound(page) __clear_bit(PG_compound, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339#ifdef CONFIG_SWAP
340#define PageSwapCache(page) test_bit(PG_swapcache, &(page)->flags)
341#define SetPageSwapCache(page) set_bit(PG_swapcache, &(page)->flags)
342#define ClearPageSwapCache(page) clear_bit(PG_swapcache, &(page)->flags)
343#else
344#define PageSwapCache(page) 0
345#endif
346
347#define PageUncached(page) test_bit(PG_uncached, &(page)->flags)
348#define SetPageUncached(page) set_bit(PG_uncached, &(page)->flags)
349#define ClearPageUncached(page) clear_bit(PG_uncached, &(page)->flags)
350
351struct page; /* forward declaration */
352
353int test_clear_page_dirty(struct page *page);
354int test_clear_page_writeback(struct page *page);
355int test_set_page_writeback(struct page *page);
356
357static inline void clear_page_dirty(struct page *page)
358{
359 test_clear_page_dirty(page);
360}
361
362static inline void set_page_writeback(struct page *page)
363{
364 test_set_page_writeback(page);
365}
366
367#endif /* PAGE_FLAGS_H */