page_cgroup.c 12.0 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 15 16 17 18

static unsigned long total_usage;

#if !defined(CONFIG_SPARSEMEM)


A
Al Viro 已提交
19
void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
20 21 22 23 24 25 26 27 28 29 30
{
	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;
31 32 33 34 35 36 37
#ifdef CONFIG_DEBUG_VM
	/*
	 * The sanity checks the page allocator does upon freeing a
	 * page can reach here before the page_cgroup arrays are
	 * allocated when feeding a range of pages to the allocator
	 * for the first time during bootup or memory hotplug.
	 */
38 39
	if (unlikely(!base))
		return NULL;
40
#endif
41 42 43 44 45 46
	offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
	return base + offset;
}

static int __init alloc_node_page_cgroup(int nid)
{
47
	struct page_cgroup *base;
48
	unsigned long table_size;
49
	unsigned long nr_pages;
50 51

	nr_pages = NODE_DATA(nid)->node_spanned_pages;
52 53 54
	if (!nr_pages)
		return 0;

55
	table_size = sizeof(struct page_cgroup) * nr_pages;
56

57 58 59
	base = memblock_virt_alloc_try_nid_nopanic(
			table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
			BOOTMEM_ALLOC_ACCESSIBLE, nid);
60
	if (!base)
61 62 63 64 65 66
		return -ENOMEM;
	NODE_DATA(nid)->node_page_cgroup = base;
	total_usage += table_size;
	return 0;
}

67
void __init page_cgroup_init_flatmem(void)
68 69 70 71
{

	int nid, fail;

72
	if (mem_cgroup_disabled())
73 74
		return;

75 76 77 78 79 80
	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);
81 82
	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
	" don't want memory cgroups\n");
83 84
	return;
fail:
85 86
	printk(KERN_CRIT "allocation of page_cgroup failed.\n");
	printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
87 88 89 90 91 92 93 94 95
	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);
96 97 98 99 100 101 102
#ifdef CONFIG_DEBUG_VM
	/*
	 * The sanity checks the page allocator does upon freeing a
	 * page can reach here before the page_cgroup arrays are
	 * allocated when feeding a range of pages to the allocator
	 * for the first time during bootup or memory hotplug.
	 */
103 104
	if (!section->page_cgroup)
		return NULL;
105
#endif
106 107 108
	return section->page_cgroup + pfn;
}

109
static void *__meminit alloc_page_cgroup(size_t size, int nid)
110
{
111
	gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
112 113
	void *addr = NULL;

114 115 116
	addr = alloc_pages_exact_nid(nid, size, flags);
	if (addr) {
		kmemleak_alloc(addr, size, 1, flags);
117
		return addr;
118
	}
119 120

	if (node_state(nid, N_HIGH_MEMORY))
121
		addr = vzalloc_node(size, nid);
122
	else
123
		addr = vzalloc(size);
124 125 126 127

	return addr;
}

128
static int __meminit init_section_page_cgroup(unsigned long pfn, int nid)
129
{
130
	struct mem_section *section;
131
	struct page_cgroup *base;
132 133
	unsigned long table_size;

134
	section = __pfn_to_section(pfn);
135 136 137 138 139

	if (section->page_cgroup)
		return 0;

	table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
140 141
	base = alloc_page_cgroup(table_size, nid);

142 143 144 145 146 147
	/*
	 * 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);
148 149 150 151 152 153

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

154 155 156 157 158
	/*
	 * The passed "pfn" may not be aligned to SECTION.  For the calculation
	 * we need to apply a mask.
	 */
	pfn &= PAGE_SECTION_MASK;
159 160 161 162 163
	section->page_cgroup = base - pfn;
	total_usage += table_size;
	return 0;
}
#ifdef CONFIG_MEMORY_HOTPLUG
164 165 166 167 168 169 170 171 172 173 174 175 176 177
static void free_page_cgroup(void *addr)
{
	if (is_vmalloc_addr(addr)) {
		vfree(addr);
	} else {
		struct page *page = virt_to_page(addr);
		size_t table_size =
			sizeof(struct page_cgroup) * PAGES_PER_SECTION;

		BUG_ON(PageReserved(page));
		free_pages_exact(addr, table_size);
	}
}

178
static void __free_page_cgroup(unsigned long pfn)
179 180 181 182 183 184 185 186
{
	struct mem_section *ms;
	struct page_cgroup *base;

	ms = __pfn_to_section(pfn);
	if (!ms || !ms->page_cgroup)
		return;
	base = ms->page_cgroup + pfn;
187 188
	free_page_cgroup(base);
	ms->page_cgroup = NULL;
189 190
}

191 192 193
static int __meminit online_page_cgroup(unsigned long start_pfn,
				unsigned long nr_pages,
				int nid)
194 195 196 197
{
	unsigned long start, end, pfn;
	int fail = 0;

198 199
	start = SECTION_ALIGN_DOWN(start_pfn);
	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
200

201 202 203 204 205 206 207 208 209 210
	if (nid == -1) {
		/*
		 * In this case, "nid" already exists and contains valid memory.
		 * "start_pfn" passed to us is a pfn which is an arg for
		 * online__pages(), and start_pfn should exist.
		 */
		nid = pfn_to_nid(start_pfn);
		VM_BUG_ON(!node_state(nid, N_ONLINE));
	}

211 212 213
	for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
		if (!pfn_present(pfn))
			continue;
214
		fail = init_section_page_cgroup(pfn, nid);
215 216 217 218 219 220 221 222 223 224 225
	}
	if (!fail)
		return 0;

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

	return -ENOMEM;
}

226 227
static int __meminit offline_page_cgroup(unsigned long start_pfn,
				unsigned long nr_pages, int nid)
228 229 230
{
	unsigned long start, end, pfn;

231 232
	start = SECTION_ALIGN_DOWN(start_pfn);
	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
233 234 235 236 237 238 239

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

}

A
Al Viro 已提交
240
static int __meminit page_cgroup_callback(struct notifier_block *self,
241 242 243 244 245 246 247 248 249 250 251 252 253
			       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;
254
	case MEM_CANCEL_ONLINE:
255 256 257
		offline_page_cgroup(mn->start_pfn,
				mn->nr_pages, mn->status_change_nid);
		break;
258 259 260 261 262 263
	case MEM_GOING_OFFLINE:
		break;
	case MEM_ONLINE:
	case MEM_CANCEL_OFFLINE:
		break;
	}
264

265
	return notifier_from_errno(ret);
266 267 268 269 270 271 272
}

#endif

void __init page_cgroup_init(void)
{
	unsigned long pfn;
273
	int nid;
274

275
	if (mem_cgroup_disabled())
276 277
		return;

278
	for_each_node_state(nid, N_MEMORY) {
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		unsigned long start_pfn, end_pfn;

		start_pfn = node_start_pfn(nid);
		end_pfn = node_end_pfn(nid);
		/*
		 * start_pfn and end_pfn may not be aligned to SECTION and the
		 * page->flags of out of node pages are not initialized.  So we
		 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
		 */
		for (pfn = start_pfn;
		     pfn < end_pfn;
                     pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {

			if (!pfn_valid(pfn))
				continue;
			/*
			 * Nodes's pfns can be overlapping.
			 * We know some arch can have a nodes layout such as
			 * -------------pfn-------------->
			 * N0 | N1 | N2 | N0 | N1 | N2|....
			 */
			if (pfn_to_nid(pfn) != nid)
				continue;
			if (init_section_page_cgroup(pfn, nid))
				goto oom;
		}
305
	}
306
	hotplug_memory_notifier(page_cgroup_callback, 0);
307
	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
308 309 310 311 312 313
	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
			 "don't want memory cgroups\n");
	return;
oom:
	printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
	panic("Out of memory");
314 315
}

A
Al Viro 已提交
316
void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
317 318 319 320 321
{
	return;
}

#endif
322 323


A
Andrew Morton 已提交
324
#ifdef CONFIG_MEMCG_SWAP
325 326 327 328 329

static DEFINE_MUTEX(swap_cgroup_mutex);
struct swap_cgroup_ctrl {
	struct page **map;
	unsigned long length;
330
	spinlock_t	lock;
331 332
};

333
static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
334 335

struct swap_cgroup {
336
	unsigned short		id;
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 373 374 375 376 377 378 379
};
#define SC_PER_PAGE	(PAGE_SIZE/sizeof(struct swap_cgroup))

/*
 * 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;
}

380 381 382 383 384 385
static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
					struct swap_cgroup_ctrl **ctrlp)
{
	pgoff_t offset = swp_offset(ent);
	struct swap_cgroup_ctrl *ctrl;
	struct page *mappage;
386
	struct swap_cgroup *sc;
387 388 389 390 391 392

	ctrl = &swap_cgroup_ctrl[swp_type(ent)];
	if (ctrlp)
		*ctrlp = ctrl;

	mappage = ctrl->map[offset / SC_PER_PAGE];
393 394
	sc = page_address(mappage);
	return sc + offset % SC_PER_PAGE;
395 396
}

397 398
/**
 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
W
Wanpeng Li 已提交
399
 * @ent: swap entry to be cmpxchged
400 401 402 403
 * @old: old id
 * @new: new id
 *
 * Returns old id at success, 0 at failure.
L
Lucas De Marchi 已提交
404
 * (There is no mem_cgroup using 0 as its id)
405 406 407 408 409 410
 */
unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
					unsigned short old, unsigned short new)
{
	struct swap_cgroup_ctrl *ctrl;
	struct swap_cgroup *sc;
411 412
	unsigned long flags;
	unsigned short retval;
413

414
	sc = lookup_swap_cgroup(ent, &ctrl);
415

416 417 418 419
	spin_lock_irqsave(&ctrl->lock, flags);
	retval = sc->id;
	if (retval == old)
		sc->id = new;
420
	else
421 422 423
		retval = 0;
	spin_unlock_irqrestore(&ctrl->lock, flags);
	return retval;
424 425
}

426 427 428
/**
 * swap_cgroup_record - record mem_cgroup for this swp_entry.
 * @ent: swap entry to be recorded into
W
Wanpeng Li 已提交
429
 * @id: mem_cgroup to be recorded
430
 *
431 432
 * Returns old value at success, 0 at failure.
 * (Of course, old value can be 0.)
433
 */
434
unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
435 436 437
{
	struct swap_cgroup_ctrl *ctrl;
	struct swap_cgroup *sc;
438
	unsigned short old;
439
	unsigned long flags;
440

441
	sc = lookup_swap_cgroup(ent, &ctrl);
442

443 444 445 446
	spin_lock_irqsave(&ctrl->lock, flags);
	old = sc->id;
	sc->id = id;
	spin_unlock_irqrestore(&ctrl->lock, flags);
447 448 449 450 451

	return old;
}

/**
452
 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
453 454
 * @ent: swap entry to be looked up.
 *
455
 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
456
 */
457
unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
458
{
459
	return lookup_swap_cgroup(ent, NULL)->id;
460 461 462 463 464 465 466 467 468 469 470 471
}

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;

472
	length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
473 474
	array_size = length * sizeof(void *);

475
	array = vzalloc(array_size);
476 477 478 479 480 481 482
	if (!array)
		goto nomem;

	ctrl = &swap_cgroup_ctrl[type];
	mutex_lock(&swap_cgroup_mutex);
	ctrl->length = length;
	ctrl->map = array;
483
	spin_lock_init(&ctrl->lock);
484 485 486 487 488
	if (swap_cgroup_prepare(type)) {
		/* memory shortage */
		ctrl->map = NULL;
		ctrl->length = 0;
		mutex_unlock(&swap_cgroup_mutex);
489
		vfree(array);
490 491 492 493 494 495 496 497
		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
498
		"swap_cgroup can be disabled by swapaccount=0 boot option\n");
499 500 501 502 503
	return -ENOMEM;
}

void swap_cgroup_swapoff(int type)
{
504 505
	struct page **map;
	unsigned long i, length;
506 507 508 509 510 511 512
	struct swap_cgroup_ctrl *ctrl;

	if (!do_swap_account)
		return;

	mutex_lock(&swap_cgroup_mutex);
	ctrl = &swap_cgroup_ctrl[type];
513 514 515 516 517 518 519 520 521
	map = ctrl->map;
	length = ctrl->length;
	ctrl->map = NULL;
	ctrl->length = 0;
	mutex_unlock(&swap_cgroup_mutex);

	if (map) {
		for (i = 0; i < length; i++) {
			struct page *page = map[i];
522 523 524
			if (page)
				__free_page(page);
		}
525
		vfree(map);
526 527 528 529
	}
}

#endif