blob: 6b63679ce8a1a3895242eabe9f41fd251ab358a1 [file] [log] [blame]
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001#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 */
13struct 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
Al Viro31168482008-11-22 17:33:24 +000020void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +030021
22#ifdef CONFIG_SPARSEMEM
23static inline void __init page_cgroup_init_flatmem(void)
24{
25}
26extern void __init page_cgroup_init(void);
27#else
28void __init page_cgroup_init_flatmem(void);
29static inline void __init page_cgroup_init(void)
30{
31}
32#endif
33
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070034struct page_cgroup *lookup_page_cgroup(struct page *page);
35
36enum {
37 /* flags for mem_cgroup */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -080038 PCG_LOCK, /* Lock for pc->mem_cgroup and following bits. */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070039 PCG_CACHE, /* charged as cache */
40 PCG_USED, /* this object is in use. */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -080041 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 */
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -070044 PCG_FILE_MAPPED, /* page is accounted as "mapped" */
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -080045 /* No lock in page_cgroup */
46 PCG_ACCT_LRU, /* page has been accounted for (under lru_lock) */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070047};
48
49#define TESTPCGFLAG(uname, lname) \
50static inline int PageCgroup##uname(struct page_cgroup *pc) \
51 { return test_bit(PCG_##lname, &pc->flags); }
52
53#define SETPCGFLAG(uname, lname) \
54static inline void SetPageCgroup##uname(struct page_cgroup *pc)\
55 { set_bit(PCG_##lname, &pc->flags); }
56
57#define CLEARPCGFLAG(uname, lname) \
58static inline void ClearPageCgroup##uname(struct page_cgroup *pc) \
59 { clear_bit(PCG_##lname, &pc->flags); }
60
Balbir Singh4b3bde42009-09-23 15:56:32 -070061#define TESTCLEARPCGFLAG(uname, lname) \
62static inline int TestClearPageCgroup##uname(struct page_cgroup *pc) \
63 { return test_and_clear_bit(PCG_##lname, &pc->flags); }
64
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070065/* Cache flag is set only once (at allocation) */
66TESTPCGFLAG(Cache, CACHE)
Balbir Singh4b3bde42009-09-23 15:56:32 -070067CLEARPCGFLAG(Cache, CACHE)
68SETPCGFLAG(Cache, CACHE)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070069
70TESTPCGFLAG(Used, USED)
71CLEARPCGFLAG(Used, USED)
Balbir Singh4b3bde42009-09-23 15:56:32 -070072SETPCGFLAG(Used, USED)
73
74SETPCGFLAG(AcctLRU, ACCT_LRU)
75CLEARPCGFLAG(AcctLRU, ACCT_LRU)
76TESTPCGFLAG(AcctLRU, ACCT_LRU)
77TESTCLEARPCGFLAG(AcctLRU, ACCT_LRU)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070078
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -070079
80SETPCGFLAG(FileMapped, FILE_MAPPED)
81CLEARPCGFLAG(FileMapped, FILE_MAPPED)
82TESTPCGFLAG(FileMapped, FILE_MAPPED)
83
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -070084SETPCGFLAG(Migration, MIGRATION)
85CLEARPCGFLAG(Migration, MIGRATION)
86TESTPCGFLAG(Migration, MIGRATION)
87
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070088static inline void lock_page_cgroup(struct page_cgroup *pc)
89{
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -080090 /*
91 * Don't take this lock in IRQ context.
92 * This lock is for pc->mem_cgroup, USED, CACHE, MIGRATION
93 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070094 bit_spin_lock(PCG_LOCK, &pc->flags);
95}
96
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070097static inline void unlock_page_cgroup(struct page_cgroup *pc)
98{
99 bit_spin_unlock(PCG_LOCK, &pc->flags);
100}
101
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -0800102static inline void move_lock_page_cgroup(struct page_cgroup *pc,
103 unsigned long *flags)
104{
105 /*
106 * We know updates to pc->flags of page cache's stats are from both of
107 * usual context or IRQ context. Disable IRQ to avoid deadlock.
108 */
109 local_irq_save(*flags);
110 bit_spin_lock(PCG_MOVE_LOCK, &pc->flags);
111}
112
113static inline void move_unlock_page_cgroup(struct page_cgroup *pc,
114 unsigned long *flags)
115{
116 bit_spin_unlock(PCG_MOVE_LOCK, &pc->flags);
117 local_irq_restore(*flags);
118}
119
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700120#else /* CONFIG_CGROUP_MEM_RES_CTLR */
121struct page_cgroup;
122
Al Viro31168482008-11-22 17:33:24 +0000123static inline void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700124{
125}
126
127static inline struct page_cgroup *lookup_page_cgroup(struct page *page)
128{
129 return NULL;
130}
KAMEZAWA Hiroyuki94b6da52008-10-22 14:15:05 -0700131
132static inline void page_cgroup_init(void)
133{
134}
135
KAMEZAWA Hiroyukica371c02009-06-12 10:33:53 +0300136static inline void __init page_cgroup_init_flatmem(void)
137{
138}
139
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700140#endif
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800141
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800142#include <linux/swap.h>
Jaswinder Singh Rajput97572752009-09-20 16:20:44 +0530143
144#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Daisuke Nishimura02491442010-03-10 15:22:17 -0800145extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
146 unsigned short old, unsigned short new);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700147extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
148extern unsigned short lookup_swap_cgroup(swp_entry_t ent);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800149extern int swap_cgroup_swapon(int type, unsigned long max_pages);
150extern void swap_cgroup_swapoff(int type);
151#else
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800152
153static inline
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700154unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800155{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700156 return 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800157}
158
159static inline
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700160unsigned short lookup_swap_cgroup(swp_entry_t ent)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800161{
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700162 return 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800163}
164
165static inline int
166swap_cgroup_swapon(int type, unsigned long max_pages)
167{
168 return 0;
169}
170
171static inline void swap_cgroup_swapoff(int type)
172{
173 return;
174}
175
176#endif
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700177#endif