1 #ifndef __LINUX_PAGE_CGROUP_H
2 #define __LINUX_PAGE_CGROUP_H
4 #ifdef CONFIG_CGROUP_MEM_RES_CTLR
5 #include <linux/bit_spinlock.h>
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.
15 struct mem_cgroup *mem_cgroup;
17 struct list_head lru; /* per cgroup LRU list */
20 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
22 #ifdef CONFIG_SPARSEMEM
23 static inline void __init page_cgroup_init_flatmem(void)
26 extern void __init page_cgroup_init(void);
28 void __init page_cgroup_init_flatmem(void);
29 static inline void __init page_cgroup_init(void)
34 struct page_cgroup *lookup_page_cgroup(struct page *page);
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 /* No lock in page_cgroup */
46 PCG_ACCT_LRU, /* page has been accounted for (under lru_lock) */
49 #define TESTPCGFLAG(uname, lname) \
50 static inline int PageCgroup##uname(struct page_cgroup *pc) \
51 { return test_bit(PCG_##lname, &pc->flags); }
53 #define SETPCGFLAG(uname, lname) \
54 static inline void SetPageCgroup##uname(struct page_cgroup *pc)\
55 { set_bit(PCG_##lname, &pc->flags); }
57 #define CLEARPCGFLAG(uname, lname) \
58 static inline void ClearPageCgroup##uname(struct page_cgroup *pc) \
59 { clear_bit(PCG_##lname, &pc->flags); }
61 #define TESTCLEARPCGFLAG(uname, lname) \
62 static inline int TestClearPageCgroup##uname(struct page_cgroup *pc) \
63 { return test_and_clear_bit(PCG_##lname, &pc->flags); }
65 /* Cache flag is set only once (at allocation) */
66 TESTPCGFLAG(Cache, CACHE)
67 CLEARPCGFLAG(Cache, CACHE)
68 SETPCGFLAG(Cache, CACHE)
70 TESTPCGFLAG(Used, USED)
71 CLEARPCGFLAG(Used, USED)
72 SETPCGFLAG(Used, USED)
74 SETPCGFLAG(AcctLRU, ACCT_LRU)
75 CLEARPCGFLAG(AcctLRU, ACCT_LRU)
76 TESTPCGFLAG(AcctLRU, ACCT_LRU)
77 TESTCLEARPCGFLAG(AcctLRU, ACCT_LRU)
80 SETPCGFLAG(FileMapped, FILE_MAPPED)
81 CLEARPCGFLAG(FileMapped, FILE_MAPPED)
82 TESTPCGFLAG(FileMapped, FILE_MAPPED)
84 SETPCGFLAG(Migration, MIGRATION)
85 CLEARPCGFLAG(Migration, MIGRATION)
86 TESTPCGFLAG(Migration, MIGRATION)
88 static inline int page_cgroup_nid(struct page_cgroup *pc)
90 return page_to_nid(pc->page);
93 static inline enum zone_type page_cgroup_zid(struct page_cgroup *pc)
95 return page_zonenum(pc->page);
98 static inline void lock_page_cgroup(struct page_cgroup *pc)
101 * Don't take this lock in IRQ context.
102 * This lock is for pc->mem_cgroup, USED, CACHE, MIGRATION
104 bit_spin_lock(PCG_LOCK, &pc->flags);
107 static inline void unlock_page_cgroup(struct page_cgroup *pc)
109 bit_spin_unlock(PCG_LOCK, &pc->flags);
112 static inline int page_is_cgroup_locked(struct page_cgroup *pc)
114 return bit_spin_is_locked(PCG_LOCK, &pc->flags);
117 static inline void move_lock_page_cgroup(struct page_cgroup *pc,
118 unsigned long *flags)
121 * We know updates to pc->flags of page cache's stats are from both of
122 * usual context or IRQ context. Disable IRQ to avoid deadlock.
124 local_irq_save(*flags);
125 bit_spin_lock(PCG_MOVE_LOCK, &pc->flags);
128 static inline void move_unlock_page_cgroup(struct page_cgroup *pc,
129 unsigned long *flags)
131 bit_spin_unlock(PCG_MOVE_LOCK, &pc->flags);
132 local_irq_restore(*flags);
135 #else /* CONFIG_CGROUP_MEM_RES_CTLR */
138 static inline void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
142 static inline struct page_cgroup *lookup_page_cgroup(struct page *page)
147 static inline void page_cgroup_init(void)
151 static inline void __init page_cgroup_init_flatmem(void)
157 #include <linux/swap.h>
159 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
160 extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
161 unsigned short old, unsigned short new);
162 extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
163 extern unsigned short lookup_swap_cgroup(swp_entry_t ent);
164 extern int swap_cgroup_swapon(int type, unsigned long max_pages);
165 extern void swap_cgroup_swapoff(int type);
169 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
175 unsigned short lookup_swap_cgroup(swp_entry_t ent)
181 swap_cgroup_swapon(int type, unsigned long max_pages)
186 static inline void swap_cgroup_swapoff(int type)