2 #include <linux/mmzone.h>
3 #include <linux/bootmem.h>
4 #include <linux/bit_spinlock.h>
5 #include <linux/page_cgroup.h>
6 #include <linux/hash.h>
7 #include <linux/slab.h>
8 #include <linux/memory.h>
9 #include <linux/vmalloc.h>
10 #include <linux/cgroup.h>
11 #include <linux/swapops.h>
12 #include <linux/kmemleak.h>
14 static unsigned long total_usage
;
16 static void page_cgroup_lock_init(struct page_cgroup
*pc
, int nr_pages
)
18 #ifdef CONFIG_PREEMPT_RT_BASE
19 for (; nr_pages
; nr_pages
--, pc
++)
20 spin_lock_init(&pc
->pcg_lock
);
24 #if !defined(CONFIG_SPARSEMEM)
27 void __meminit
pgdat_page_cgroup_init(struct pglist_data
*pgdat
)
29 pgdat
->node_page_cgroup
= NULL
;
32 struct page_cgroup
*lookup_page_cgroup(struct page
*page
)
34 unsigned long pfn
= page_to_pfn(page
);
36 struct page_cgroup
*base
;
38 base
= NODE_DATA(page_to_nid(page
))->node_page_cgroup
;
39 #ifdef CONFIG_DEBUG_VM
41 * The sanity checks the page allocator does upon freeing a
42 * page can reach here before the page_cgroup arrays are
43 * allocated when feeding a range of pages to the allocator
44 * for the first time during bootup or memory hotplug.
49 offset
= pfn
- NODE_DATA(page_to_nid(page
))->node_start_pfn
;
53 static int __init
alloc_node_page_cgroup(int nid
)
55 struct page_cgroup
*base
;
56 unsigned long table_size
;
57 unsigned long nr_pages
;
59 nr_pages
= NODE_DATA(nid
)->node_spanned_pages
;
63 table_size
= sizeof(struct page_cgroup
) * nr_pages
;
65 base
= memblock_virt_alloc_try_nid_nopanic(
66 table_size
, PAGE_SIZE
, __pa(MAX_DMA_ADDRESS
),
67 BOOTMEM_ALLOC_ACCESSIBLE
, nid
);
70 NODE_DATA(nid
)->node_page_cgroup
= base
;
71 total_usage
+= table_size
;
72 page_cgroup_lock_init(base
, nr_pages
);
76 void __init
page_cgroup_init_flatmem(void)
81 if (mem_cgroup_disabled())
84 for_each_online_node(nid
) {
85 fail
= alloc_node_page_cgroup(nid
);
89 printk(KERN_INFO
"allocated %ld bytes of page_cgroup\n", total_usage
);
90 printk(KERN_INFO
"please try 'cgroup_disable=memory' option if you"
91 " don't want memory cgroups\n");
94 printk(KERN_CRIT
"allocation of page_cgroup failed.\n");
95 printk(KERN_CRIT
"please try 'cgroup_disable=memory' boot option\n");
96 panic("Out of memory");
99 #else /* CONFIG_FLAT_NODE_MEM_MAP */
101 struct page_cgroup
*lookup_page_cgroup(struct page
*page
)
103 unsigned long pfn
= page_to_pfn(page
);
104 struct mem_section
*section
= __pfn_to_section(pfn
);
105 #ifdef CONFIG_DEBUG_VM
107 * The sanity checks the page allocator does upon freeing a
108 * page can reach here before the page_cgroup arrays are
109 * allocated when feeding a range of pages to the allocator
110 * for the first time during bootup or memory hotplug.
112 if (!section
->page_cgroup
)
115 return section
->page_cgroup
+ pfn
;
118 static void *__meminit
alloc_page_cgroup(size_t size
, int nid
)
120 gfp_t flags
= GFP_KERNEL
| __GFP_ZERO
| __GFP_NOWARN
;
123 addr
= alloc_pages_exact_nid(nid
, size
, flags
);
125 kmemleak_alloc(addr
, size
, 1, flags
);
129 if (node_state(nid
, N_HIGH_MEMORY
))
130 addr
= vzalloc_node(size
, nid
);
132 addr
= vzalloc(size
);
137 static int __meminit
init_section_page_cgroup(unsigned long pfn
, int nid
)
139 struct mem_section
*section
;
140 struct page_cgroup
*base
;
141 unsigned long table_size
;
143 section
= __pfn_to_section(pfn
);
145 if (section
->page_cgroup
)
148 table_size
= sizeof(struct page_cgroup
) * PAGES_PER_SECTION
;
149 base
= alloc_page_cgroup(table_size
, nid
);
152 * The value stored in section->page_cgroup is (base - pfn)
153 * and it does not point to the memory block allocated above,
154 * causing kmemleak false positives.
156 kmemleak_not_leak(base
);
159 printk(KERN_ERR
"page cgroup allocation failure\n");
163 page_cgroup_lock_init(base
, PAGES_PER_SECTION
);
166 * The passed "pfn" may not be aligned to SECTION. For the calculation
167 * we need to apply a mask.
169 pfn
&= PAGE_SECTION_MASK
;
170 section
->page_cgroup
= base
- pfn
;
171 total_usage
+= table_size
;
174 #ifdef CONFIG_MEMORY_HOTPLUG
175 static void free_page_cgroup(void *addr
)
177 if (is_vmalloc_addr(addr
)) {
180 struct page
*page
= virt_to_page(addr
);
182 sizeof(struct page_cgroup
) * PAGES_PER_SECTION
;
184 BUG_ON(PageReserved(page
));
186 free_pages_exact(addr
, table_size
);
190 void __free_page_cgroup(unsigned long pfn
)
192 struct mem_section
*ms
;
193 struct page_cgroup
*base
;
195 ms
= __pfn_to_section(pfn
);
196 if (!ms
|| !ms
->page_cgroup
)
198 base
= ms
->page_cgroup
+ pfn
;
199 free_page_cgroup(base
);
200 ms
->page_cgroup
= NULL
;
203 int __meminit
online_page_cgroup(unsigned long start_pfn
,
204 unsigned long nr_pages
,
207 unsigned long start
, end
, pfn
;
210 start
= SECTION_ALIGN_DOWN(start_pfn
);
211 end
= SECTION_ALIGN_UP(start_pfn
+ nr_pages
);
215 * In this case, "nid" already exists and contains valid memory.
216 * "start_pfn" passed to us is a pfn which is an arg for
217 * online__pages(), and start_pfn should exist.
219 nid
= pfn_to_nid(start_pfn
);
220 VM_BUG_ON(!node_state(nid
, N_ONLINE
));
223 for (pfn
= start
; !fail
&& pfn
< end
; pfn
+= PAGES_PER_SECTION
) {
224 if (!pfn_present(pfn
))
226 fail
= init_section_page_cgroup(pfn
, nid
);
232 for (pfn
= start
; pfn
< end
; pfn
+= PAGES_PER_SECTION
)
233 __free_page_cgroup(pfn
);
238 int __meminit
offline_page_cgroup(unsigned long start_pfn
,
239 unsigned long nr_pages
, int nid
)
241 unsigned long start
, end
, pfn
;
243 start
= SECTION_ALIGN_DOWN(start_pfn
);
244 end
= SECTION_ALIGN_UP(start_pfn
+ nr_pages
);
246 for (pfn
= start
; pfn
< end
; pfn
+= PAGES_PER_SECTION
)
247 __free_page_cgroup(pfn
);
252 static int __meminit
page_cgroup_callback(struct notifier_block
*self
,
253 unsigned long action
, void *arg
)
255 struct memory_notify
*mn
= arg
;
258 case MEM_GOING_ONLINE
:
259 ret
= online_page_cgroup(mn
->start_pfn
,
260 mn
->nr_pages
, mn
->status_change_nid
);
263 offline_page_cgroup(mn
->start_pfn
,
264 mn
->nr_pages
, mn
->status_change_nid
);
266 case MEM_CANCEL_ONLINE
:
267 offline_page_cgroup(mn
->start_pfn
,
268 mn
->nr_pages
, mn
->status_change_nid
);
270 case MEM_GOING_OFFLINE
:
273 case MEM_CANCEL_OFFLINE
:
277 return notifier_from_errno(ret
);
282 void __init
page_cgroup_init(void)
287 if (mem_cgroup_disabled())
290 for_each_node_state(nid
, N_MEMORY
) {
291 unsigned long start_pfn
, end_pfn
;
293 start_pfn
= node_start_pfn(nid
);
294 end_pfn
= node_end_pfn(nid
);
296 * start_pfn and end_pfn may not be aligned to SECTION and the
297 * page->flags of out of node pages are not initialized. So we
298 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
300 for (pfn
= start_pfn
;
302 pfn
= ALIGN(pfn
+ 1, PAGES_PER_SECTION
)) {
307 * Nodes's pfns can be overlapping.
308 * We know some arch can have a nodes layout such as
309 * -------------pfn-------------->
310 * N0 | N1 | N2 | N0 | N1 | N2|....
312 if (pfn_to_nid(pfn
) != nid
)
314 if (init_section_page_cgroup(pfn
, nid
))
318 hotplug_memory_notifier(page_cgroup_callback
, 0);
319 printk(KERN_INFO
"allocated %ld bytes of page_cgroup\n", total_usage
);
320 printk(KERN_INFO
"please try 'cgroup_disable=memory' option if you "
321 "don't want memory cgroups\n");
324 printk(KERN_CRIT
"try 'cgroup_disable=memory' boot option\n");
325 panic("Out of memory");
328 void __meminit
pgdat_page_cgroup_init(struct pglist_data
*pgdat
)
336 #ifdef CONFIG_MEMCG_SWAP
338 static DEFINE_MUTEX(swap_cgroup_mutex
);
339 struct swap_cgroup_ctrl
{
341 unsigned long length
;
345 static struct swap_cgroup_ctrl swap_cgroup_ctrl
[MAX_SWAPFILES
];
350 #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
353 * SwapCgroup implements "lookup" and "exchange" operations.
354 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
355 * against SwapCache. At swap_free(), this is accessed directly from swap.
358 * - we have no race in "exchange" when we're accessed via SwapCache because
359 * SwapCache(and its swp_entry) is under lock.
360 * - When called via swap_free(), there is no user of this entry and no race.
361 * Then, we don't need lock around "exchange".
363 * TODO: we can push these buffers out to HIGHMEM.
367 * allocate buffer for swap_cgroup.
369 static int swap_cgroup_prepare(int type
)
372 struct swap_cgroup_ctrl
*ctrl
;
373 unsigned long idx
, max
;
375 ctrl
= &swap_cgroup_ctrl
[type
];
377 for (idx
= 0; idx
< ctrl
->length
; idx
++) {
378 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
380 goto not_enough_page
;
381 ctrl
->map
[idx
] = page
;
386 for (idx
= 0; idx
< max
; idx
++)
387 __free_page(ctrl
->map
[idx
]);
392 static struct swap_cgroup
*lookup_swap_cgroup(swp_entry_t ent
,
393 struct swap_cgroup_ctrl
**ctrlp
)
395 pgoff_t offset
= swp_offset(ent
);
396 struct swap_cgroup_ctrl
*ctrl
;
397 struct page
*mappage
;
398 struct swap_cgroup
*sc
;
400 ctrl
= &swap_cgroup_ctrl
[swp_type(ent
)];
404 mappage
= ctrl
->map
[offset
/ SC_PER_PAGE
];
405 sc
= page_address(mappage
);
406 return sc
+ offset
% SC_PER_PAGE
;
410 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
411 * @ent: swap entry to be cmpxchged
415 * Returns old id at success, 0 at failure.
416 * (There is no mem_cgroup using 0 as its id)
418 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent
,
419 unsigned short old
, unsigned short new)
421 struct swap_cgroup_ctrl
*ctrl
;
422 struct swap_cgroup
*sc
;
424 unsigned short retval
;
426 sc
= lookup_swap_cgroup(ent
, &ctrl
);
428 spin_lock_irqsave(&ctrl
->lock
, flags
);
434 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
439 * swap_cgroup_record - record mem_cgroup for this swp_entry.
440 * @ent: swap entry to be recorded into
441 * @id: mem_cgroup to be recorded
443 * Returns old value at success, 0 at failure.
444 * (Of course, old value can be 0.)
446 unsigned short swap_cgroup_record(swp_entry_t ent
, unsigned short id
)
448 struct swap_cgroup_ctrl
*ctrl
;
449 struct swap_cgroup
*sc
;
453 sc
= lookup_swap_cgroup(ent
, &ctrl
);
455 spin_lock_irqsave(&ctrl
->lock
, flags
);
458 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
464 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
465 * @ent: swap entry to be looked up.
467 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
469 unsigned short lookup_swap_cgroup_id(swp_entry_t ent
)
471 return lookup_swap_cgroup(ent
, NULL
)->id
;
474 int swap_cgroup_swapon(int type
, unsigned long max_pages
)
477 unsigned long array_size
;
478 unsigned long length
;
479 struct swap_cgroup_ctrl
*ctrl
;
481 if (!do_swap_account
)
484 length
= DIV_ROUND_UP(max_pages
, SC_PER_PAGE
);
485 array_size
= length
* sizeof(void *);
487 array
= vzalloc(array_size
);
491 ctrl
= &swap_cgroup_ctrl
[type
];
492 mutex_lock(&swap_cgroup_mutex
);
493 ctrl
->length
= length
;
495 spin_lock_init(&ctrl
->lock
);
496 if (swap_cgroup_prepare(type
)) {
497 /* memory shortage */
500 mutex_unlock(&swap_cgroup_mutex
);
504 mutex_unlock(&swap_cgroup_mutex
);
508 printk(KERN_INFO
"couldn't allocate enough memory for swap_cgroup.\n");
510 "swap_cgroup can be disabled by swapaccount=0 boot option\n");
514 void swap_cgroup_swapoff(int type
)
517 unsigned long i
, length
;
518 struct swap_cgroup_ctrl
*ctrl
;
520 if (!do_swap_account
)
523 mutex_lock(&swap_cgroup_mutex
);
524 ctrl
= &swap_cgroup_ctrl
[type
];
526 length
= ctrl
->length
;
529 mutex_unlock(&swap_cgroup_mutex
);
532 for (i
= 0; i
< length
; i
++) {
533 struct page
*page
= map
[i
];