Re: [PATCH] mm/memcg: remove useless check on page->mem_cgroup

Johannes Weiner
2020-07-31T15:18:01Z

On Fri, Jul 31, 2020 at 11:34:40AM +0800, Alex Shi wrote:
> Since readahead page will be charged on memcg too. We don't need to
> check this exception now. Rmove them is safe as all user pages are
> charged before use.
>
> Signed-off-by: Alex Shi <alex.shi@linux.alibaba.com>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Michal Hocko <mhocko@kernel.org>
> Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: cgroups@vger.kernel.org
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
> ---
> mm/memcontrol.c | 21 ++++-----------------
> 1 file changed, 4 insertions(+), 17 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index e84c2b5596f2..9e44ae22d591 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -1322,12 +1322,7 @@ struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgd
> }
>
> memcg = page->mem_cgroup;
> - /*
> - * Swapcache readahead pages are added to the LRU - and
> - * possibly migrated - before they are charged.
> - */
> - if (!memcg)
> - memcg = root_mem_cgroup;
> + VM_BUG_ON_PAGE(!memcg, page);
>
> mz = mem_cgroup_page_nodeinfo(memcg, page);
> lruvec = &mz->lruvec;
> @@ -6897,10 +6892,8 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
> if (newpage->mem_cgroup)
> return;
>
> - /* Swapcache readahead pages can get replaced before being charged */
> memcg = oldpage->mem_cgroup;
> - if (!memcg)
> - return;
> + VM_BUG_ON_PAGE(!memcg, oldpage);
>
> /* Force-charge the new page. The old one will be freed soon */
> nr_pages = thp_nr_pages(newpage);
> @@ -7094,10 +7087,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
> return;
>
> memcg = page->mem_cgroup;
> -
> - /* Readahead page, never charged */
> - if (!memcg)
> - return;
> + VM_BUG_ON_PAGE(!memcg, page);
>
> /*
> * In case the memcg owning these pages has been offlined and doesn't
> @@ -7158,10 +7148,7 @@ int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
> return 0;
>
> memcg = page->mem_cgroup;
> -
> - /* Readahead page, never charged */
> - if (!memcg)
> - return 0;
> + VM_BUG_ON_PAGE(!memcg, page);
>
> if (!entry.val) {
> memcg_memory_event(memcg, MEMCG_SWAP_FAIL);

Uncharged readahead pages are gone, but I'm not 100% sure uncharged
pages in general are gone. ISTR that the !page->mem_cgroup check in
mem_cgroup_uncharge() prevented a crash - although that is of course a
much broader interface, whereas the ones you change should only apply
to LRU pages (which are hopefully all charged).

Nevertheless, to avoid unnecessary crashes if we discover that we've
been wrong, how about leaving the branches for now, but adding a (new)
VM_WARN_ON_ONCE_PAGE() to them?