page_cgroup.c 11.8 KB
Newer Older
1 2 3 4 5 6
#include <linux/mm.h>
#include <linux/mmzone.h>
#include <linux/bootmem.h>
#include <linux/bit_spinlock.h>
#include <linux/page_cgroup.h>
#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>
13

14
static void __meminit init_page_cgroup(struct page_cgroup *pc, unsigned long id)
15 16
{
	pc->flags = 0;
17
	set_page_cgroup_array_id(pc, id);
18
	pc->mem_cgroup = NULL;
K
KAMEZAWA Hiroyuki 已提交
19
	INIT_LIST_HEAD(&pc->lru);
20 21 22 23 24 25
}
static unsigned long total_usage;

#if !defined(CONFIG_SPARSEMEM)


A
Al Viro 已提交
26
void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
	pgdat->node_page_cgroup = NULL;
}

struct page_cgroup *lookup_page_cgroup(struct page *page)
{
	unsigned long pfn = page_to_pfn(page);
	unsigned long offset;
	struct page_cgroup *base;

	base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
	if (unlikely(!base))
		return NULL;

	offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
	return base + offset;
}

45 46 47 48 49 50 51 52 53 54 55 56 57
struct page *lookup_cgroup_page(struct page_cgroup *pc)
{
	unsigned long pfn;
	struct page *page;
	pg_data_t *pgdat;

	pgdat = NODE_DATA(page_cgroup_array_id(pc));
	pfn = pc - pgdat->node_page_cgroup + pgdat->node_start_pfn;
	page = pfn_to_page(pfn);
	VM_BUG_ON(pc != lookup_page_cgroup(page));
	return page;
}

58 59 60 61 62 63 64 65 66
static int __init alloc_node_page_cgroup(int nid)
{
	struct page_cgroup *base, *pc;
	unsigned long table_size;
	unsigned long start_pfn, nr_pages, index;

	start_pfn = NODE_DATA(nid)->node_start_pfn;
	nr_pages = NODE_DATA(nid)->node_spanned_pages;

67 68 69
	if (!nr_pages)
		return 0;

70
	table_size = sizeof(struct page_cgroup) * nr_pages;
71 72 73 74

	base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
			table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
	if (!base)
75 76 77
		return -ENOMEM;
	for (index = 0; index < nr_pages; index++) {
		pc = base + index;
78
		init_page_cgroup(pc, nid);
79 80 81 82 83 84
	}
	NODE_DATA(nid)->node_page_cgroup = base;
	total_usage += table_size;
	return 0;
}

85
void __init page_cgroup_init_flatmem(void)
86 87 88 89
{

	int nid, fail;

90
	if (mem_cgroup_disabled())
91 92
		return;

93 94 95 96 97 98
	for_each_online_node(nid)  {
		fail = alloc_node_page_cgroup(nid);
		if (fail)
			goto fail;
	}
	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
99 100
	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
	" don't want memory cgroups\n");
101 102
	return;
fail:
103 104
	printk(KERN_CRIT "allocation of page_cgroup failed.\n");
	printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
105 106 107 108 109 110 111 112 113 114
	panic("Out of memory");
}

#else /* CONFIG_FLAT_NODE_MEM_MAP */

struct page_cgroup *lookup_page_cgroup(struct page *page)
{
	unsigned long pfn = page_to_pfn(page);
	struct mem_section *section = __pfn_to_section(pfn);

115 116
	if (!section->page_cgroup)
		return NULL;
117 118 119
	return section->page_cgroup + pfn;
}

120 121 122 123 124 125 126 127 128 129 130 131 132
struct page *lookup_cgroup_page(struct page_cgroup *pc)
{
	struct mem_section *section;
	struct page *page;
	unsigned long nr;

	nr = page_cgroup_array_id(pc);
	section = __nr_to_section(nr);
	page = pfn_to_page(pc - section->page_cgroup);
	VM_BUG_ON(pc != lookup_page_cgroup(page));
	return page;
}

133 134 135 136
static void *__init_refok alloc_page_cgroup(size_t size, int nid)
{
	void *addr = NULL;

137
	addr = alloc_pages_exact_nid(nid, size, GFP_KERNEL | __GFP_NOWARN);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	if (addr)
		return addr;

	if (node_state(nid, N_HIGH_MEMORY))
		addr = vmalloc_node(size, nid);
	else
		addr = vmalloc(size);

	return addr;
}

#ifdef CONFIG_MEMORY_HOTPLUG
static void free_page_cgroup(void *addr)
{
	if (is_vmalloc_addr(addr)) {
		vfree(addr);
	} else {
		struct page *page = virt_to_page(addr);
156 157 158 159 160
		size_t table_size =
			sizeof(struct page_cgroup) * PAGES_PER_SECTION;

		BUG_ON(PageReserved(page));
		free_pages_exact(addr, table_size);
161 162 163 164
	}
}
#endif

165
static int __init_refok init_section_page_cgroup(unsigned long pfn)
166 167
{
	struct page_cgroup *base, *pc;
168
	struct mem_section *section;
169
	unsigned long table_size;
170
	unsigned long nr;
171 172
	int nid, index;

173 174 175 176 177 178 179 180
	nr = pfn_to_section_nr(pfn);
	section = __nr_to_section(nr);

	if (section->page_cgroup)
		return 0;

	nid = page_to_nid(pfn_to_page(pfn));
	table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
181 182
	base = alloc_page_cgroup(table_size, nid);

183 184 185 186 187 188
	/*
	 * The value stored in section->page_cgroup is (base - pfn)
	 * and it does not point to the memory block allocated above,
	 * causing kmemleak false positives.
	 */
	kmemleak_not_leak(base);
189 190 191 192 193 194 195 196

	if (!base) {
		printk(KERN_ERR "page cgroup allocation failure\n");
		return -ENOMEM;
	}

	for (index = 0; index < PAGES_PER_SECTION; index++) {
		pc = base + index;
197
		init_page_cgroup(pc, nr);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	}

	section->page_cgroup = base - pfn;
	total_usage += table_size;
	return 0;
}
#ifdef CONFIG_MEMORY_HOTPLUG
void __free_page_cgroup(unsigned long pfn)
{
	struct mem_section *ms;
	struct page_cgroup *base;

	ms = __pfn_to_section(pfn);
	if (!ms || !ms->page_cgroup)
		return;
	base = ms->page_cgroup + pfn;
214 215
	free_page_cgroup(base);
	ms->page_cgroup = NULL;
216 217
}

A
Al Viro 已提交
218
int __meminit online_page_cgroup(unsigned long start_pfn,
219 220 221 222 223 224
			unsigned long nr_pages,
			int nid)
{
	unsigned long start, end, pfn;
	int fail = 0;

225
	start = start_pfn & ~(PAGES_PER_SECTION - 1);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);

	for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
		if (!pfn_present(pfn))
			continue;
		fail = init_section_page_cgroup(pfn);
	}
	if (!fail)
		return 0;

	/* rollback */
	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
		__free_page_cgroup(pfn);

	return -ENOMEM;
}

A
Al Viro 已提交
243
int __meminit offline_page_cgroup(unsigned long start_pfn,
244 245 246 247
		unsigned long nr_pages, int nid)
{
	unsigned long start, end, pfn;

248
	start = start_pfn & ~(PAGES_PER_SECTION - 1);
249 250 251 252 253 254 255 256
	end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);

	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
		__free_page_cgroup(pfn);
	return 0;

}

A
Al Viro 已提交
257
static int __meminit page_cgroup_callback(struct notifier_block *self,
258 259 260 261 262 263 264 265 266 267 268 269 270
			       unsigned long action, void *arg)
{
	struct memory_notify *mn = arg;
	int ret = 0;
	switch (action) {
	case MEM_GOING_ONLINE:
		ret = online_page_cgroup(mn->start_pfn,
				   mn->nr_pages, mn->status_change_nid);
		break;
	case MEM_OFFLINE:
		offline_page_cgroup(mn->start_pfn,
				mn->nr_pages, mn->status_change_nid);
		break;
271
	case MEM_CANCEL_ONLINE:
272 273 274 275 276 277
	case MEM_GOING_OFFLINE:
		break;
	case MEM_ONLINE:
	case MEM_CANCEL_OFFLINE:
		break;
	}
278

279
	return notifier_from_errno(ret);
280 281 282 283 284 285 286 287 288
}

#endif

void __init page_cgroup_init(void)
{
	unsigned long pfn;
	int fail = 0;

289
	if (mem_cgroup_disabled())
290 291
		return;

292 293 294 295 296 297
	for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
		if (!pfn_present(pfn))
			continue;
		fail = init_section_page_cgroup(pfn);
	}
	if (fail) {
298
		printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
299 300 301 302 303
		panic("Out of memory");
	} else {
		hotplug_memory_notifier(page_cgroup_callback, 0);
	}
	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
304 305
	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you don't"
	" want memory cgroups\n");
306 307
}

A
Al Viro 已提交
308
void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
309 310 311 312 313
{
	return;
}

#endif
314 315 316 317 318 319 320 321


#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP

static DEFINE_MUTEX(swap_cgroup_mutex);
struct swap_cgroup_ctrl {
	struct page **map;
	unsigned long length;
322
	spinlock_t	lock;
323 324 325 326 327
};

struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];

struct swap_cgroup {
328
	unsigned short		id;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
};
#define SC_PER_PAGE	(PAGE_SIZE/sizeof(struct swap_cgroup))
#define SC_POS_MASK	(SC_PER_PAGE - 1)

/*
 * SwapCgroup implements "lookup" and "exchange" operations.
 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
 * against SwapCache. At swap_free(), this is accessed directly from swap.
 *
 * This means,
 *  - we have no race in "exchange" when we're accessed via SwapCache because
 *    SwapCache(and its swp_entry) is under lock.
 *  - When called via swap_free(), there is no user of this entry and no race.
 * Then, we don't need lock around "exchange".
 *
 * TODO: we can push these buffers out to HIGHMEM.
 */

/*
 * allocate buffer for swap_cgroup.
 */
static int swap_cgroup_prepare(int type)
{
	struct page *page;
	struct swap_cgroup_ctrl *ctrl;
	unsigned long idx, max;

	ctrl = &swap_cgroup_ctrl[type];

	for (idx = 0; idx < ctrl->length; idx++) {
		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
		if (!page)
			goto not_enough_page;
		ctrl->map[idx] = page;
	}
	return 0;
not_enough_page:
	max = idx;
	for (idx = 0; idx < max; idx++)
		__free_page(ctrl->map[idx]);

	return -ENOMEM;
}

373 374 375 376 377 378 379
/**
 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
 * @end: swap entry to be cmpxchged
 * @old: old id
 * @new: new id
 *
 * Returns old id at success, 0 at failure.
L
Lucas De Marchi 已提交
380
 * (There is no mem_cgroup using 0 as its id)
381 382 383 384 385 386 387 388 389 390 391
 */
unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
					unsigned short old, unsigned short new)
{
	int type = swp_type(ent);
	unsigned long offset = swp_offset(ent);
	unsigned long idx = offset / SC_PER_PAGE;
	unsigned long pos = offset & SC_POS_MASK;
	struct swap_cgroup_ctrl *ctrl;
	struct page *mappage;
	struct swap_cgroup *sc;
392 393
	unsigned long flags;
	unsigned short retval;
394 395 396 397 398 399

	ctrl = &swap_cgroup_ctrl[type];

	mappage = ctrl->map[idx];
	sc = page_address(mappage);
	sc += pos;
400 401 402 403
	spin_lock_irqsave(&ctrl->lock, flags);
	retval = sc->id;
	if (retval == old)
		sc->id = new;
404
	else
405 406 407
		retval = 0;
	spin_unlock_irqrestore(&ctrl->lock, flags);
	return retval;
408 409
}

410 411 412 413 414
/**
 * swap_cgroup_record - record mem_cgroup for this swp_entry.
 * @ent: swap entry to be recorded into
 * @mem: mem_cgroup to be recorded
 *
415 416
 * Returns old value at success, 0 at failure.
 * (Of course, old value can be 0.)
417
 */
418
unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
419 420 421 422 423 424 425 426
{
	int type = swp_type(ent);
	unsigned long offset = swp_offset(ent);
	unsigned long idx = offset / SC_PER_PAGE;
	unsigned long pos = offset & SC_POS_MASK;
	struct swap_cgroup_ctrl *ctrl;
	struct page *mappage;
	struct swap_cgroup *sc;
427
	unsigned short old;
428
	unsigned long flags;
429 430 431 432 433 434

	ctrl = &swap_cgroup_ctrl[type];

	mappage = ctrl->map[idx];
	sc = page_address(mappage);
	sc += pos;
435 436 437 438
	spin_lock_irqsave(&ctrl->lock, flags);
	old = sc->id;
	sc->id = id;
	spin_unlock_irqrestore(&ctrl->lock, flags);
439 440 441 442 443 444 445 446

	return old;
}

/**
 * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
 * @ent: swap entry to be looked up.
 *
447
 * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
448
 */
449
unsigned short lookup_swap_cgroup(swp_entry_t ent)
450 451 452 453 454 455 456 457
{
	int type = swp_type(ent);
	unsigned long offset = swp_offset(ent);
	unsigned long idx = offset / SC_PER_PAGE;
	unsigned long pos = offset & SC_POS_MASK;
	struct swap_cgroup_ctrl *ctrl;
	struct page *mappage;
	struct swap_cgroup *sc;
458
	unsigned short ret;
459 460 461 462 463

	ctrl = &swap_cgroup_ctrl[type];
	mappage = ctrl->map[idx];
	sc = page_address(mappage);
	sc += pos;
464
	ret = sc->id;
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	return ret;
}

int swap_cgroup_swapon(int type, unsigned long max_pages)
{
	void *array;
	unsigned long array_size;
	unsigned long length;
	struct swap_cgroup_ctrl *ctrl;

	if (!do_swap_account)
		return 0;

	length = ((max_pages/SC_PER_PAGE) + 1);
	array_size = length * sizeof(void *);

	array = vmalloc(array_size);
	if (!array)
		goto nomem;

	memset(array, 0, array_size);
	ctrl = &swap_cgroup_ctrl[type];
	mutex_lock(&swap_cgroup_mutex);
	ctrl->length = length;
	ctrl->map = array;
490
	spin_lock_init(&ctrl->lock);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
	if (swap_cgroup_prepare(type)) {
		/* memory shortage */
		ctrl->map = NULL;
		ctrl->length = 0;
		vfree(array);
		mutex_unlock(&swap_cgroup_mutex);
		goto nomem;
	}
	mutex_unlock(&swap_cgroup_mutex);

	return 0;
nomem:
	printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
	printk(KERN_INFO
		"swap_cgroup can be disabled by noswapaccount boot option\n");
	return -ENOMEM;
}

void swap_cgroup_swapoff(int type)
{
	int i;
	struct swap_cgroup_ctrl *ctrl;

	if (!do_swap_account)
		return;

	mutex_lock(&swap_cgroup_mutex);
	ctrl = &swap_cgroup_ctrl[type];
	if (ctrl->map) {
		for (i = 0; i < ctrl->length; i++) {
			struct page *page = ctrl->map[i];
			if (page)
				__free_page(page);
		}
		vfree(ctrl->map);
		ctrl->map = NULL;
		ctrl->length = 0;
	}
	mutex_unlock(&swap_cgroup_mutex);
}

#endif