blob: 0c076d58c6760751a3e82fda67c71fc02c7cc83a [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>
Andrew Mortonf886ed42006-06-23 02:03:06 -070010#include <linux/types.h>
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/pgtable.h>
13
14/*
15 * Various page->flags bits:
16 *
17 * PG_reserved is set for special pages, which can never be swapped out. Some
18 * of them might not even exist (eg empty_bad_page)...
19 *
20 * The PG_private bitflag is set if page->private contains a valid value.
21 *
22 * During disk I/O, PG_locked is used. This bit is set before I/O and
23 * reset when I/O completes. page_waitqueue(page) is a wait queue of all tasks
24 * waiting for the I/O on this page to complete.
25 *
26 * PG_uptodate tells whether the page's contents is valid. When a read
27 * completes, the page becomes uptodate, unless a disk I/O error happened.
28 *
29 * For choosing which pages to swap out, inode pages carry a PG_referenced bit,
30 * which is set any time the system accesses that page through the (mapping,
31 * index) hash table. This referenced bit, together with the referenced bit
32 * in the page tables, is used to manipulate page->age and move the page across
33 * the active, inactive_dirty and inactive_clean lists.
34 *
35 * Note that the referenced bit, the page->lru list_head and the active,
36 * inactive_dirty and inactive_clean lists are protected by the
37 * zone->lru_lock, and *NOT* by the usual PG_locked bit!
38 *
39 * PG_error is set to indicate that an I/O error occurred on this page.
40 *
41 * PG_arch_1 is an architecture specific page state bit. The generic code
42 * guarantees that this bit is cleared for a page when it first is entered into
43 * the page cache.
44 *
45 * PG_highmem pages are not permanently mapped into the kernel virtual address
46 * space, they need to be kmapped separately for doing IO on the pages. The
47 * struct page (these bits with information) are always mapped into kernel
48 * address space...
49 */
50
51/*
52 * Don't use the *_dontuse flags. Use the macros. Otherwise you'll break
Andy Whitcroft91fc8ab2006-04-10 22:53:01 -070053 * locked- and dirty-page accounting.
54 *
55 * The page flags field is split into two parts, the main flags area
56 * which extends from the low bits upwards, and the fields area which
57 * extends from the high bits downwards.
58 *
59 * | FIELD | ... | FLAGS |
60 * N-1 ^ 0
61 * (N-FLAGS_RESERVED)
62 *
63 * The fields area is reserved for fields mapping zone, node and SPARSEMEM
64 * section. The boundry between these two areas is defined by
65 * FLAGS_RESERVED which defines the width of the fields section
66 * (see linux/mmzone.h). New flags must _not_ overlap with this area.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68#define PG_locked 0 /* Page is locked. Don't touch. */
69#define PG_error 1
70#define PG_referenced 2
71#define PG_uptodate 3
72
73#define PG_dirty 4
74#define PG_lru 5
75#define PG_active 6
76#define PG_slab 7 /* slab debug (Suparna wants this) */
77
Badari Pulavartycbe37d02005-06-21 17:14:52 -070078#define PG_checked 8 /* kill me in 2.5.<early>. */
79#define PG_arch_1 9
80#define PG_reserved 10
81#define PG_private 11 /* Has something at ->private */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Badari Pulavartycbe37d02005-06-21 17:14:52 -070083#define PG_writeback 12 /* Page is under writeback */
84#define PG_nosave 13 /* Used for system suspend/resume */
85#define PG_compound 14 /* Part of a compound page */
86#define PG_swapcache 15 /* Swap page: swp_entry_t in private */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Badari Pulavartycbe37d02005-06-21 17:14:52 -070088#define PG_mappedtodisk 16 /* Has blocks allocated on-disk */
89#define PG_reclaim 17 /* To be reclaimed asap */
90#define PG_nosave_free 18 /* Free, should not be written */
Nick Piggin676165a2006-04-10 11:21:48 +100091#define PG_buddy 19 /* Page is free, on buddy lists */
92
Andrew Mortonf886ed42006-06-23 02:03:06 -070093
94#if (BITS_PER_LONG > 32)
95/*
96 * 64-bit-only flags build down from bit 31
97 *
98 * 32 bit -------------------------------| FIELDS | FLAGS |
99 * 64 bit | FIELDS | ?????? FLAGS |
100 * 63 32 0
101 */
102#define PG_uncached 31 /* Page has been mapped as uncached */
103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * Global page accounting. One instance per CPU. Only unsigned longs are
107 * allowed.
Nick Pigginb09eb1c2006-01-06 00:11:21 -0800108 *
109 * - Fields can be modified with xxx_page_state and xxx_page_state_zone at
110 * any time safely (which protects the instance from modification by
111 * interrupt.
112 * - The __xxx_page_state variants can be used safely when interrupts are
113 * disabled.
114 * - The __xxx_page_state variants can be used if the field is only
Nick Piggin9d414152006-03-22 00:08:06 -0800115 * modified from process context and protected from preemption, or only
116 * modified from interrupt context. In this case, the field should be
117 * commented here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119struct page_state {
120 unsigned long nr_dirty; /* Dirty writeable pages */
121 unsigned long nr_writeback; /* Pages under writeback */
122 unsigned long nr_unstable; /* NFS unstable pages */
123 unsigned long nr_page_table_pages;/* Pages used for pagetables */
Nick Pigginb09eb1c2006-01-06 00:11:21 -0800124 unsigned long nr_mapped; /* mapped into pagetables.
125 * only modified from process context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long nr_slab; /* In slab */
127#define GET_PAGE_STATE_LAST nr_slab
128
129 /*
130 * The below are zeroed by get_page_state(). Use get_full_page_state()
131 * to add up all these.
132 */
133 unsigned long pgpgin; /* Disk reads */
134 unsigned long pgpgout; /* Disk writes */
135 unsigned long pswpin; /* swap reads */
136 unsigned long pswpout; /* swap writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Nick Piggin9328b8f2006-01-06 00:11:10 -0800138 unsigned long pgalloc_high; /* page allocations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unsigned long pgalloc_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800140 unsigned long pgalloc_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned long pgalloc_dma;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned long pgfree; /* page freeings */
144 unsigned long pgactivate; /* pages moved inactive->active */
145 unsigned long pgdeactivate; /* pages moved active->inactive */
146
147 unsigned long pgfault; /* faults (major+minor) */
148 unsigned long pgmajfault; /* faults (major only) */
Nick Piggin9328b8f2006-01-06 00:11:10 -0800149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long pgrefill_high; /* inspected in refill_inactive_zone */
151 unsigned long pgrefill_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800152 unsigned long pgrefill_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned long pgrefill_dma;
154
155 unsigned long pgsteal_high; /* total highmem pages reclaimed */
156 unsigned long pgsteal_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800157 unsigned long pgsteal_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long pgsteal_dma;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned long pgscan_kswapd_high;/* total highmem pages scanned */
161 unsigned long pgscan_kswapd_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800162 unsigned long pgscan_kswapd_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned long pgscan_kswapd_dma;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 unsigned long pgscan_direct_high;/* total highmem pages scanned */
166 unsigned long pgscan_direct_normal;
Nick Piggin9328b8f2006-01-06 00:11:10 -0800167 unsigned long pgscan_direct_dma32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned long pgscan_direct_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Nick Piggin9328b8f2006-01-06 00:11:10 -0800170 unsigned long pginodesteal; /* pages reclaimed via inode freeing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 unsigned long slabs_scanned; /* slab objects scanned */
172 unsigned long kswapd_steal; /* pages reclaimed by kswapd */
173 unsigned long kswapd_inodesteal;/* reclaimed via kswapd inode freeing */
174 unsigned long pageoutrun; /* kswapd's calls to page reclaim */
175 unsigned long allocstall; /* direct reclaim calls */
176
177 unsigned long pgrotated; /* pages rotated to tail of the LRU */
KAMEZAWA Hiroyukiedfbe2b2005-05-01 08:58:37 -0700178 unsigned long nr_bounce; /* pages for bounce buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181extern void get_page_state(struct page_state *ret);
Martin Hicksc07e02d2005-09-03 15:55:11 -0700182extern void get_page_state_node(struct page_state *ret, int node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183extern void get_full_page_state(struct page_state *ret);
Nick Piggina74609f2006-01-06 00:11:20 -0800184extern unsigned long read_page_state_offset(unsigned long offset);
185extern void mod_page_state_offset(unsigned long offset, unsigned long delta);
186extern void __mod_page_state_offset(unsigned long offset, unsigned long delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188#define read_page_state(member) \
Nick Piggina74609f2006-01-06 00:11:20 -0800189 read_page_state_offset(offsetof(struct page_state, member))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191#define mod_page_state(member, delta) \
Nick Piggina74609f2006-01-06 00:11:20 -0800192 mod_page_state_offset(offsetof(struct page_state, member), (delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Nick Piggina74609f2006-01-06 00:11:20 -0800194#define __mod_page_state(member, delta) \
195 __mod_page_state_offset(offsetof(struct page_state, member), (delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Nick Piggina74609f2006-01-06 00:11:20 -0800197#define inc_page_state(member) mod_page_state(member, 1UL)
198#define dec_page_state(member) mod_page_state(member, 0UL - 1)
199#define add_page_state(member,delta) mod_page_state(member, (delta))
200#define sub_page_state(member,delta) mod_page_state(member, 0UL - (delta))
201
202#define __inc_page_state(member) __mod_page_state(member, 1UL)
203#define __dec_page_state(member) __mod_page_state(member, 0UL - 1)
204#define __add_page_state(member,delta) __mod_page_state(member, (delta))
205#define __sub_page_state(member,delta) __mod_page_state(member, 0UL - (delta))
206
207#define page_state(member) (*__page_state(offsetof(struct page_state, member)))
208
209#define state_zone_offset(zone, member) \
210({ \
Nick Piggin9328b8f2006-01-06 00:11:10 -0800211 unsigned offset; \
212 if (is_highmem(zone)) \
213 offset = offsetof(struct page_state, member##_high); \
214 else if (is_normal(zone)) \
215 offset = offsetof(struct page_state, member##_normal); \
216 else if (is_dma32(zone)) \
217 offset = offsetof(struct page_state, member##_dma32); \
218 else \
219 offset = offsetof(struct page_state, member##_dma); \
Nick Piggina74609f2006-01-06 00:11:20 -0800220 offset; \
221})
222
223#define __mod_page_state_zone(zone, member, delta) \
224 do { \
225 __mod_page_state_offset(state_zone_offset(zone, member), (delta)); \
226 } while (0)
227
228#define mod_page_state_zone(zone, member, delta) \
229 do { \
230 mod_page_state_offset(state_zone_offset(zone, member), (delta)); \
Nick Piggin9328b8f2006-01-06 00:11:10 -0800231 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * Manipulation of page state flags
235 */
236#define PageLocked(page) \
237 test_bit(PG_locked, &(page)->flags)
238#define SetPageLocked(page) \
239 set_bit(PG_locked, &(page)->flags)
240#define TestSetPageLocked(page) \
241 test_and_set_bit(PG_locked, &(page)->flags)
242#define ClearPageLocked(page) \
243 clear_bit(PG_locked, &(page)->flags)
244#define TestClearPageLocked(page) \
245 test_and_clear_bit(PG_locked, &(page)->flags)
246
247#define PageError(page) test_bit(PG_error, &(page)->flags)
248#define SetPageError(page) set_bit(PG_error, &(page)->flags)
249#define ClearPageError(page) clear_bit(PG_error, &(page)->flags)
250
251#define PageReferenced(page) test_bit(PG_referenced, &(page)->flags)
252#define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
253#define ClearPageReferenced(page) clear_bit(PG_referenced, &(page)->flags)
254#define TestClearPageReferenced(page) test_and_clear_bit(PG_referenced, &(page)->flags)
255
256#define PageUptodate(page) test_bit(PG_uptodate, &(page)->flags)
257#ifndef SetPageUptodate
258#define SetPageUptodate(page) set_bit(PG_uptodate, &(page)->flags)
259#endif
260#define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
261
262#define PageDirty(page) test_bit(PG_dirty, &(page)->flags)
263#define SetPageDirty(page) set_bit(PG_dirty, &(page)->flags)
264#define TestSetPageDirty(page) test_and_set_bit(PG_dirty, &(page)->flags)
265#define ClearPageDirty(page) clear_bit(PG_dirty, &(page)->flags)
Nick Piggin242e5462005-09-03 15:54:50 -0700266#define __ClearPageDirty(page) __clear_bit(PG_dirty, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#define TestClearPageDirty(page) test_and_clear_bit(PG_dirty, &(page)->flags)
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#define PageLRU(page) test_bit(PG_lru, &(page)->flags)
Nick Piggin8d438f92006-03-22 00:07:59 -0800270#define SetPageLRU(page) set_bit(PG_lru, &(page)->flags)
271#define ClearPageLRU(page) clear_bit(PG_lru, &(page)->flags)
Nick Piggin67453912006-03-22 00:08:00 -0800272#define __ClearPageLRU(page) __clear_bit(PG_lru, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274#define PageActive(page) test_bit(PG_active, &(page)->flags)
275#define SetPageActive(page) set_bit(PG_active, &(page)->flags)
276#define ClearPageActive(page) clear_bit(PG_active, &(page)->flags)
Nick Piggin67453912006-03-22 00:08:00 -0800277#define __ClearPageActive(page) __clear_bit(PG_active, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279#define PageSlab(page) test_bit(PG_slab, &(page)->flags)
Nick Pigginf205b2f2006-03-22 00:08:02 -0800280#define __SetPageSlab(page) __set_bit(PG_slab, &(page)->flags)
281#define __ClearPageSlab(page) __clear_bit(PG_slab, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283#ifdef CONFIG_HIGHMEM
Badari Pulavartycbe37d02005-06-21 17:14:52 -0700284#define PageHighMem(page) is_highmem(page_zone(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#else
286#define PageHighMem(page) 0 /* needed to optimize away at compile time */
287#endif
288
289#define PageChecked(page) test_bit(PG_checked, &(page)->flags)
290#define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
291#define ClearPageChecked(page) clear_bit(PG_checked, &(page)->flags)
292
293#define PageReserved(page) test_bit(PG_reserved, &(page)->flags)
294#define SetPageReserved(page) set_bit(PG_reserved, &(page)->flags)
295#define ClearPageReserved(page) clear_bit(PG_reserved, &(page)->flags)
296#define __ClearPageReserved(page) __clear_bit(PG_reserved, &(page)->flags)
297
298#define SetPagePrivate(page) set_bit(PG_private, &(page)->flags)
299#define ClearPagePrivate(page) clear_bit(PG_private, &(page)->flags)
300#define PagePrivate(page) test_bit(PG_private, &(page)->flags)
301#define __SetPagePrivate(page) __set_bit(PG_private, &(page)->flags)
302#define __ClearPagePrivate(page) __clear_bit(PG_private, &(page)->flags)
303
304#define PageWriteback(page) test_bit(PG_writeback, &(page)->flags)
305#define SetPageWriteback(page) \
306 do { \
307 if (!test_and_set_bit(PG_writeback, \
308 &(page)->flags)) \
309 inc_page_state(nr_writeback); \
310 } while (0)
311#define TestSetPageWriteback(page) \
312 ({ \
313 int ret; \
314 ret = test_and_set_bit(PG_writeback, \
315 &(page)->flags); \
316 if (!ret) \
317 inc_page_state(nr_writeback); \
318 ret; \
319 })
320#define ClearPageWriteback(page) \
321 do { \
322 if (test_and_clear_bit(PG_writeback, \
323 &(page)->flags)) \
324 dec_page_state(nr_writeback); \
325 } while (0)
326#define TestClearPageWriteback(page) \
327 ({ \
328 int ret; \
329 ret = test_and_clear_bit(PG_writeback, \
330 &(page)->flags); \
331 if (ret) \
332 dec_page_state(nr_writeback); \
333 ret; \
334 })
335
336#define PageNosave(page) test_bit(PG_nosave, &(page)->flags)
337#define SetPageNosave(page) set_bit(PG_nosave, &(page)->flags)
338#define TestSetPageNosave(page) test_and_set_bit(PG_nosave, &(page)->flags)
339#define ClearPageNosave(page) clear_bit(PG_nosave, &(page)->flags)
340#define TestClearPageNosave(page) test_and_clear_bit(PG_nosave, &(page)->flags)
341
342#define PageNosaveFree(page) test_bit(PG_nosave_free, &(page)->flags)
343#define SetPageNosaveFree(page) set_bit(PG_nosave_free, &(page)->flags)
344#define ClearPageNosaveFree(page) clear_bit(PG_nosave_free, &(page)->flags)
345
Nick Piggin676165a2006-04-10 11:21:48 +1000346#define PageBuddy(page) test_bit(PG_buddy, &(page)->flags)
347#define __SetPageBuddy(page) __set_bit(PG_buddy, &(page)->flags)
348#define __ClearPageBuddy(page) __clear_bit(PG_buddy, &(page)->flags)
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#define PageMappedToDisk(page) test_bit(PG_mappedtodisk, &(page)->flags)
351#define SetPageMappedToDisk(page) set_bit(PG_mappedtodisk, &(page)->flags)
352#define ClearPageMappedToDisk(page) clear_bit(PG_mappedtodisk, &(page)->flags)
353
354#define PageReclaim(page) test_bit(PG_reclaim, &(page)->flags)
355#define SetPageReclaim(page) set_bit(PG_reclaim, &(page)->flags)
356#define ClearPageReclaim(page) clear_bit(PG_reclaim, &(page)->flags)
357#define TestClearPageReclaim(page) test_and_clear_bit(PG_reclaim, &(page)->flags)
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#define PageCompound(page) test_bit(PG_compound, &(page)->flags)
Nick Piggin5e9dace2006-03-22 00:08:01 -0800360#define __SetPageCompound(page) __set_bit(PG_compound, &(page)->flags)
361#define __ClearPageCompound(page) __clear_bit(PG_compound, &(page)->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363#ifdef CONFIG_SWAP
364#define PageSwapCache(page) test_bit(PG_swapcache, &(page)->flags)
365#define SetPageSwapCache(page) set_bit(PG_swapcache, &(page)->flags)
366#define ClearPageSwapCache(page) clear_bit(PG_swapcache, &(page)->flags)
367#else
368#define PageSwapCache(page) 0
369#endif
370
371#define PageUncached(page) test_bit(PG_uncached, &(page)->flags)
372#define SetPageUncached(page) set_bit(PG_uncached, &(page)->flags)
373#define ClearPageUncached(page) clear_bit(PG_uncached, &(page)->flags)
374
375struct page; /* forward declaration */
376
377int test_clear_page_dirty(struct page *page);
378int test_clear_page_writeback(struct page *page);
379int test_set_page_writeback(struct page *page);
380
381static inline void clear_page_dirty(struct page *page)
382{
383 test_clear_page_dirty(page);
384}
385
386static inline void set_page_writeback(struct page *page)
387{
388 test_set_page_writeback(page);
389}
390
391#endif /* PAGE_FLAGS_H */