Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

memcg: remove a redundant check

Because of remove refcnt patch, it's very rare case to that
mem_cgroup_charge_common() is called against a page which is accounted.

mem_cgroup_charge_common() is called when.
1. a page is added into file cache.
2. an anon page is _newly_ mapped.

A racy case is that a newly-swapped-in anonymous page is referred from
prural threads in do_swap_page() at the same time.
(a page is not Locked when mem_cgroup_charge() is called from do_swap_page.)

Another case is shmem. It charges its page before calling add_to_page_cache().
Then, mem_cgroup_charge_cache() is called twice. This case is handled in
mem_cgroup_cache_charge(). But this check may be too hacky...

Signed-off-by : KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Pavel Emelyanov <xemul@openvz.org>
Cc: Li Zefan <lizf@cn.fujitsu.com>
Cc: Hugh Dickins <hugh@veritas.com>
Cc: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Cc: Paul Menage <menage@google.com>
Cc: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

KAMEZAWA Hiroyuki and committed by
Linus Torvalds
accf163e b76734e5

+25 -28
+25 -28
mm/memcontrol.c
··· 536 536 if (mem_cgroup_subsys.disabled) 537 537 return 0; 538 538 539 - /* 540 - * Should page_cgroup's go to their own slab? 541 - * One could optimize the performance of the charging routine 542 - * by saving a bit in the page_flags and using it as a lock 543 - * to see if the cgroup page already has a page_cgroup associated 544 - * with it 545 - */ 546 - retry: 547 - lock_page_cgroup(page); 548 - pc = page_get_page_cgroup(page); 549 - /* 550 - * The page_cgroup exists and 551 - * the page has already been accounted. 552 - */ 553 - if (unlikely(pc)) { 554 - VM_BUG_ON(pc->page != page); 555 - VM_BUG_ON(!pc->mem_cgroup); 556 - unlock_page_cgroup(page); 557 - goto done; 558 - } 559 - unlock_page_cgroup(page); 560 - 561 539 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask); 562 540 if (unlikely(pc == NULL)) 563 541 goto err; ··· 596 618 lock_page_cgroup(page); 597 619 if (unlikely(page_get_page_cgroup(page))) { 598 620 unlock_page_cgroup(page); 599 - /* 600 - * Another charge has been added to this page already. 601 - * We take lock_page_cgroup(page) again and read 602 - * page->cgroup, increment refcnt.... just retry is OK. 603 - */ 604 621 res_counter_uncharge(&mem->res, PAGE_SIZE); 605 622 css_put(&mem->css); 606 623 kmem_cache_free(page_cgroup_cache, pc); 607 - goto retry; 624 + goto done; 608 625 } 609 626 page_assign_page_cgroup(page, pc); 610 627 ··· 638 665 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, 639 666 gfp_t gfp_mask) 640 667 { 668 + /* 669 + * Corner case handling. This is called from add_to_page_cache() 670 + * in usual. But some FS (shmem) precharges this page before calling it 671 + * and call add_to_page_cache() with GFP_NOWAIT. 672 + * 673 + * For GFP_NOWAIT case, the page may be pre-charged before calling 674 + * add_to_page_cache(). (See shmem.c) check it here and avoid to call 675 + * charge twice. (It works but has to pay a bit larger cost.) 676 + */ 677 + if (!(gfp_mask & __GFP_WAIT)) { 678 + struct page_cgroup *pc; 679 + 680 + lock_page_cgroup(page); 681 + pc = page_get_page_cgroup(page); 682 + if (pc) { 683 + VM_BUG_ON(pc->page != page); 684 + VM_BUG_ON(!pc->mem_cgroup); 685 + unlock_page_cgroup(page); 686 + return 0; 687 + } 688 + unlock_page_cgroup(page); 689 + } 690 + 641 691 if (unlikely(!mm)) 642 692 mm = &init_mm; 693 + 643 694 return mem_cgroup_charge_common(page, mm, gfp_mask, 644 695 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); 645 696 }