From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9B8AC433B4 for ; Mon, 10 May 2021 00:54:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 36071613D2 for ; Mon, 10 May 2021 00:54:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 36071613D2 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4745B6B0070; Sun, 9 May 2021 20:54:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 424406B0071; Sun, 9 May 2021 20:54:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C6656B0072; Sun, 9 May 2021 20:54:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 104FA6B0070 for ; Sun, 9 May 2021 20:54:06 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id C198F9426 for ; Mon, 10 May 2021 00:54:05 +0000 (UTC) X-FDA: 78123499650.37.73532C4 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf26.hostedemail.com (Postfix) with ESMTP id 0D69F40002ED for ; Mon, 10 May 2021 00:53:51 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id F1A90613CD; Mon, 10 May 2021 00:54:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1620608044; bh=b1iyFhP93PLYe183MwIZhu4SLDJGahzFM+EHwvxRKxQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Xx8+8MR6Ik0YNRnih52LHJJp7wdu+H2nY5NpKdl7wRS9vhP0EsyqskBnuzF4pRXDW l3ytroX/P9+QKvPZeQeWxoRNs1fYsyJbkECfwYH9W1q5aMKbLGWCylXyV+NgQ2n4Or HDJO0G93SEYnifPBEPt7KacT6mOLfIyTJlkKX4DQ= Date: Sun, 9 May 2021 17:54:03 -0700 From: Andrew Morton To: Muchun Song Cc: guro@fb.com, hannes@cmpxchg.org, mhocko@kernel.org, shakeelb@google.com, vdavydov.dev@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, zhengqi.arch@bytedance.com Subject: Re: [PATCH v2] mm: memcontrol: fix root_mem_cgroup charging Message-Id: <20210509175403.ec3f33f293ed69f2a9b275a6@linux-foundation.org> In-Reply-To: <20210425075410.19255-1-songmuchun@bytedance.com> References: <20210425075410.19255-1-songmuchun@bytedance.com> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=Xx8+8MR6; dmarc=none; spf=pass (imf26.hostedemail.com: domain of akpm@linux-foundation.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0D69F40002ED X-Stat-Signature: npyjubu67tuwn5rtjjqq9n9ogjsb31qt Received-SPF: none (linux-foundation.org>: No applicable sender policy available) receiver=imf26; identity=mailfrom; envelope-from=""; helo=mail.kernel.org; client-ip=198.145.29.99 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620608031-934785 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Sun, 25 Apr 2021 15:54:10 +0800 Muchun Song wrote: > The below scenario can cause the page counters of the root_mem_cgroup > to be out of balance. > > CPU0: CPU1: > > objcg = get_obj_cgroup_from_current() > obj_cgroup_charge_pages(objcg) > memcg_reparent_objcgs() > // reparent to root_mem_cgroup > WRITE_ONCE(iter->memcg, parent) > // memcg == root_mem_cgroup > memcg = get_mem_cgroup_from_objcg(objcg) > // do not charge to the root_mem_cgroup > try_charge(memcg) > > obj_cgroup_uncharge_pages(objcg) > memcg = get_mem_cgroup_from_objcg(objcg) > // uncharge from the root_mem_cgroup > refill_stock(memcg) > drain_stock(memcg) > page_counter_uncharge(&memcg->memory) > > get_obj_cgroup_from_current() never returns a root_mem_cgroup's objcg, > so we never explicitly charge the root_mem_cgroup. And it's not > going to change. It's all about a race when we got an obj_cgroup > pointing at some non-root memcg, but before we were able to charge it, > the cgroup was gone, objcg was reparented to the root and so we're > skipping the charging. Then we store the objcg pointer and later use > to uncharge the root_mem_cgroup. > > This can cause the page counter to be less than the actual value. > Although we do not display the value (mem_cgroup_usage) so there > shouldn't be any actual problem, but there is a WARN_ON_ONCE in > the page_counter_cancel(). Who knows if it will trigger? So it > is better to fix it. > It sounds like Roman will be acking this, but some additional reviewer attention would be helpful, please.