Merge branch 'sfi-release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb...
[pandora-kernel.git] / include / linux / page_cgroup.h
1 #ifndef __LINUX_PAGE_CGROUP_H
2 #define __LINUX_PAGE_CGROUP_H
3
4 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
5 #include <linux/bit_spinlock.h>
6 /*
7  * Page Cgroup can be considered as an extended mem_map.
8  * A page_cgroup page is associated with every page descriptor. The
9  * page_cgroup helps us identify information about the cgroup
10  * All page cgroups are allocated at boot or memory hotplug event,
11  * then the page cgroup for pfn always exists.
12  */
13 struct page_cgroup {
14         unsigned long flags;
15         struct mem_cgroup *mem_cgroup;
16         struct page *page;
17         struct list_head lru;           /* per cgroup LRU list */
18 };
19
20 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
21
22 #ifdef CONFIG_SPARSEMEM
23 static inline void __init page_cgroup_init_flatmem(void)
24 {
25 }
26 extern void __init page_cgroup_init(void);
27 #else
28 void __init page_cgroup_init_flatmem(void);
29 static inline void __init page_cgroup_init(void)
30 {
31 }
32 #endif
33
34 struct page_cgroup *lookup_page_cgroup(struct page *page);
35
36 enum {
37         /* flags for mem_cgroup */
38         PCG_LOCK,  /* Lock for pc->mem_cgroup and following bits. */
39         PCG_CACHE, /* charged as cache */
40         PCG_USED, /* this object is in use. */
41         PCG_MIGRATION, /* under page migration */
42         /* flags for mem_cgroup and file and I/O status */
43         PCG_MOVE_LOCK, /* For race between move_account v.s. following bits */
44         PCG_FILE_MAPPED, /* page is accounted as "mapped" */
45         PCG_FILE_DIRTY, /* page is dirty */
46         PCG_FILE_WRITEBACK, /* page is under writeback */
47         PCG_FILE_UNSTABLE_NFS, /* page is NFS unstable */
48         /* No lock in page_cgroup */
49         PCG_ACCT_LRU, /* page has been accounted for (under lru_lock) */
50 };
51
52 #define TESTPCGFLAG(uname, lname)                       \
53 static inline int PageCgroup##uname(struct page_cgroup *pc)     \
54         { return test_bit(PCG_##lname, &pc->flags); }
55
56 #define SETPCGFLAG(uname, lname)                        \
57 static inline void SetPageCgroup##uname(struct page_cgroup *pc)\
58         { set_bit(PCG_##lname, &pc->flags);  }
59
60 #define CLEARPCGFLAG(uname, lname)                      \
61 static inline void ClearPageCgroup##uname(struct page_cgroup *pc)       \
62         { clear_bit(PCG_##lname, &pc->flags);  }
63
64 #define TESTCLEARPCGFLAG(uname, lname)                  \
65 static inline int TestClearPageCgroup##uname(struct page_cgroup *pc)    \
66         { return test_and_clear_bit(PCG_##lname, &pc->flags);  }
67
68 #define TESTSETPCGFLAG(uname, lname)                    \
69 static inline int TestSetPageCgroup##uname(struct page_cgroup *pc)      \
70         { return test_and_set_bit(PCG_##lname, &pc->flags);  }
71
72 /* Cache flag is set only once (at allocation) */
73 TESTPCGFLAG(Cache, CACHE)
74 CLEARPCGFLAG(Cache, CACHE)
75 SETPCGFLAG(Cache, CACHE)
76
77 TESTPCGFLAG(Used, USED)
78 CLEARPCGFLAG(Used, USED)
79 SETPCGFLAG(Used, USED)
80
81 SETPCGFLAG(AcctLRU, ACCT_LRU)
82 CLEARPCGFLAG(AcctLRU, ACCT_LRU)
83 TESTPCGFLAG(AcctLRU, ACCT_LRU)
84 TESTCLEARPCGFLAG(AcctLRU, ACCT_LRU)
85
86
87 SETPCGFLAG(FileMapped, FILE_MAPPED)
88 CLEARPCGFLAG(FileMapped, FILE_MAPPED)
89 TESTPCGFLAG(FileMapped, FILE_MAPPED)
90
91 SETPCGFLAG(FileDirty, FILE_DIRTY)
92 CLEARPCGFLAG(FileDirty, FILE_DIRTY)
93 TESTPCGFLAG(FileDirty, FILE_DIRTY)
94 TESTCLEARPCGFLAG(FileDirty, FILE_DIRTY)
95 TESTSETPCGFLAG(FileDirty, FILE_DIRTY)
96
97 SETPCGFLAG(FileWriteback, FILE_WRITEBACK)
98 CLEARPCGFLAG(FileWriteback, FILE_WRITEBACK)
99 TESTPCGFLAG(FileWriteback, FILE_WRITEBACK)
100
101 SETPCGFLAG(FileUnstableNFS, FILE_UNSTABLE_NFS)
102 CLEARPCGFLAG(FileUnstableNFS, FILE_UNSTABLE_NFS)
103 TESTPCGFLAG(FileUnstableNFS, FILE_UNSTABLE_NFS)
104 TESTCLEARPCGFLAG(FileUnstableNFS, FILE_UNSTABLE_NFS)
105 TESTSETPCGFLAG(FileUnstableNFS, FILE_UNSTABLE_NFS)
106
107 SETPCGFLAG(Migration, MIGRATION)
108 CLEARPCGFLAG(Migration, MIGRATION)
109 TESTPCGFLAG(Migration, MIGRATION)
110
111 static inline int page_cgroup_nid(struct page_cgroup *pc)
112 {
113         return page_to_nid(pc->page);
114 }
115
116 static inline enum zone_type page_cgroup_zid(struct page_cgroup *pc)
117 {
118         return page_zonenum(pc->page);
119 }
120
121 static inline void lock_page_cgroup(struct page_cgroup *pc)
122 {
123         /*
124          * Don't take this lock in IRQ context.
125          * This lock is for pc->mem_cgroup, USED, CACHE, MIGRATION
126          */
127         bit_spin_lock(PCG_LOCK, &pc->flags);
128 }
129
130 static inline void unlock_page_cgroup(struct page_cgroup *pc)
131 {
132         bit_spin_unlock(PCG_LOCK, &pc->flags);
133 }
134
135 static inline int page_is_cgroup_locked(struct page_cgroup *pc)
136 {
137         return bit_spin_is_locked(PCG_LOCK, &pc->flags);
138 }
139
140 static inline void move_lock_page_cgroup(struct page_cgroup *pc,
141         unsigned long *flags)
142 {
143         /*
144          * We know updates to pc->flags of page cache's stats are from both of
145          * usual context or IRQ context. Disable IRQ to avoid deadlock.
146          */
147         local_irq_save(*flags);
148         bit_spin_lock(PCG_MOVE_LOCK, &pc->flags);
149 }
150
151 static inline void move_unlock_page_cgroup(struct page_cgroup *pc,
152         unsigned long *flags)
153 {
154         bit_spin_unlock(PCG_MOVE_LOCK, &pc->flags);
155         local_irq_restore(*flags);
156 }
157
158 #else /* CONFIG_CGROUP_MEM_RES_CTLR */
159 struct page_cgroup;
160
161 static inline void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
162 {
163 }
164
165 static inline struct page_cgroup *lookup_page_cgroup(struct page *page)
166 {
167         return NULL;
168 }
169
170 static inline void page_cgroup_init(void)
171 {
172 }
173
174 static inline void __init page_cgroup_init_flatmem(void)
175 {
176 }
177
178 #endif
179
180 #include <linux/swap.h>
181
182 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
183 extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
184                                         unsigned short old, unsigned short new);
185 extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
186 extern unsigned short lookup_swap_cgroup(swp_entry_t ent);
187 extern int swap_cgroup_swapon(int type, unsigned long max_pages);
188 extern void swap_cgroup_swapoff(int type);
189 #else
190
191 static inline
192 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
193 {
194         return 0;
195 }
196
197 static inline
198 unsigned short lookup_swap_cgroup(swp_entry_t ent)
199 {
200         return 0;
201 }
202
203 static inline int
204 swap_cgroup_swapon(int type, unsigned long max_pages)
205 {
206         return 0;
207 }
208
209 static inline void swap_cgroup_swapoff(int type)
210 {
211         return;
212 }
213
214 #endif
215 #endif