Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1158422pxy; Fri, 23 Apr 2021 01:23:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfrG3irYlkPh13QKJGfEUhYQBaEYh+v8iffGYDptbwsP5SLGod3fGh7hRYl6L+C69cKE7D X-Received: by 2002:a65:6a4c:: with SMTP id o12mr2640746pgu.371.1619166187140; Fri, 23 Apr 2021 01:23:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619166187; cv=none; d=google.com; s=arc-20160816; b=JRpasBrGN9amBADoksSOa5Ta5imK31PmIsJ87/ax3xr5cAWAUcXJddNeHo1V+Lb9Vx RmQ3sNf7ltpR3se4p8PggUJQYXamkecS5MV3OXVYsXeEcBMD2j+tVc97vvG9gVdp2iex Yh9FWfkbgs/6JdatqNzHGFVeHlUxNZmQgLwr9CaWgz1Xsu4sMdik3Z7mj04xXeuiwJn2 hHuuGqAEBIpSrs6dZ6dT9cKruZY3Brr7mLutVSni9rADiQYqQ/T/c13mCDpCO7ra94V6 0Y1/D2AxXQtzkawUqCzsWe+P4+L1DLXCOcq+NqVRHLE7oyiGrh2CF2pnSuSjo6uffPnG 6klg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=NQ/C0zDNyAPIDZw2+/amw4VenmRaKiNB4o9V4vurBKA=; b=Pp7Yq1tNPzETyKW9Ed8037vY9aI68a0aiZWAxw8kwwSS2ohRoosXAyElUXiuOMD6RK 5Qbhcjulvh/ma8c2oikULDw9WFOZRvWAS8JPPr6IMSMCB/nuJx8d0B1ADYUls8tfOV4H MN8o7rzzhEEvgPDEk0TTf8G9Fu9jjWLxkTfgPNKSVBFVRCaAszCvQ5dZV/gzdyNABgry NSNhRrvYX9/T8J+LlGCwzP+E7zqYJqoINnPQMJA7KC2LP+YbukfG/RgrwhByjrtEK33Y ea8HDFqcDXGzwXXRssUi/IcSc9MVCx3epzF0mAaEMqdVU4KPz9sHFo5usDe2KR/RSrE1 Gn0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=RQCCyoYS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h1si5955260plb.375.2021.04.23.01.22.54; Fri, 23 Apr 2021 01:23:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=RQCCyoYS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230192AbhDWIVq (ORCPT + 99 others); Fri, 23 Apr 2021 04:21:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbhDWIVp (ORCPT ); Fri, 23 Apr 2021 04:21:45 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C44DC061574 for ; Fri, 23 Apr 2021 01:21:09 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id em21-20020a17090b0155b029014e204a81e6so4107399pjb.1 for ; Fri, 23 Apr 2021 01:21:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NQ/C0zDNyAPIDZw2+/amw4VenmRaKiNB4o9V4vurBKA=; b=RQCCyoYSmuhQtzrxiVl5azTu5daw1SMebUqJjeMDDlxszJIDwPCUnf1u1A6e4dsu+b c05o3xH6h9XOJ+10E5rKtioC3LghHIoVTmB0YvSSxPzu1Tibi6mYLv5u59CFajC/CNyf RUibK1/Ov7Uj1q4BqCIoFMn7cp9ksmasU9CDhlXlmIghVa1a73AiKn1j37AlR2W4iHwm CxJeAPMhGLEDqv42QLZjnfQ7TdzuJ3IYppLE59SCR+7tqFkhHgZraxSSt2UFZkz+KxeS ecbBmCBXrKz5qRS//tBjrUdQaUJ7c15RlRHxCOg/vX1RJGW5dQlxF9r04B32L0CrNw4O z/Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NQ/C0zDNyAPIDZw2+/amw4VenmRaKiNB4o9V4vurBKA=; b=HuOeThFtlfMpT+Z/zPUyxYMr0MR8iJ0h+DQQ9VOmW2WrRSGtpDPtEiZ+BKQWpT21gp gVFiXLDr7Uj5YsDgqfUtKoKKCQJYM0lYxcQl6TsUinZPOgC5tVYn7BEF9tDrDQiGxRJq o340WXe1qKfRe/lBb0Vn7swu/cK5HUsQLiZ2XrqtmBB+azx2u5hVOpzq3MmMoYmU3y8N 6CSwfeEB+r1ktwbx/LQmaHqE59JUEJXir2/HZH1UDG9qMsHcrbJZz2dFF/8M2dfOr3Wt vDTDYmOSW/2VUFKYCYfZSiy5XBiVq/j2YiHjGI4MWdbbGLyQkzONfZLJFFD7P3E4XQrp rc0Q== X-Gm-Message-State: AOAM5325VMLZ+PtLVqdOa8nipnOnnNX8/K71iq3lpEkkiJzmvn0DxlqX Bh3/P0cAGQx4b6x5ZQjFy/KeHCH+BvrWWz10ApkFrKKBd88= X-Received: by 2002:a17:90b:88f:: with SMTP id bj15mr4504534pjb.147.1619166068961; Fri, 23 Apr 2021 01:21:08 -0700 (PDT) MIME-Version: 1.0 References: <20210421062644.68331-1-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Fri, 23 Apr 2021 16:20:32 +0800 Message-ID: Subject: Re: [External] Re: [PATCH] mm: memcontrol: fix root_mem_cgroup charging To: Roman Gushchin Cc: Michal Hocko , Johannes Weiner , Andrew Morton , Shakeel Butt , Vladimir Davydov , LKML , Linux Memory Management List , Xiongchun duan , fam.zheng@bytedance.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 23, 2021 at 2:53 AM Roman Gushchin wrote: > > On Thu, Apr 22, 2021 at 11:47:05AM +0800, Muchun Song wrote: > > On Thu, Apr 22, 2021 at 8:57 AM Roman Gushchin wrote: > > > > > > On Wed, Apr 21, 2021 at 09:39:03PM +0800, Muchun Song wrote: > > > > On Wed, Apr 21, 2021 at 9:03 PM Michal Hocko wrote: > > > > > > > > > > On Wed 21-04-21 17:50:06, Muchun Song wrote: > > > > > > On Wed, Apr 21, 2021 at 3:34 PM Michal Hocko wrote: > > > > > > > > > > > > > > On Wed 21-04-21 14:26:44, 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 > > > > > > > > page_counter_uncharge(&memcg->memory) > > > > > > > > > > > > > > > > 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. > > > > > > > > > > > > > > The changelog doesn't explain the fix and why you have chosen to charge > > > > > > > kmem objects to root memcg and left all other try_charge users intact. > > > > > > > > > > > > The object cgroup is special (because the page can reparent). Only the > > > > > > user of objcg APIs should be fixed. > > > > > > > > > > > > > The reason is likely that those are not reparented now but that just > > > > > > > adds an inconsistency. > > > > > > > > > > > > > > Is there any reason you haven't simply matched obj_cgroup_uncharge_pages > > > > > > > to check for the root memcg and bail out early? > > > > > > > > > > > > Because obj_cgroup_uncharge_pages() uncharges pages from the > > > > > > root memcg unconditionally. Why? Because some pages can be > > > > > > reparented to root memcg, in order to ensure the correctness of > > > > > > page counter of root memcg. We have to uncharge pages from > > > > > > root memcg. So we do not check whether the page belongs to > > > > > > the root memcg when it uncharges. > > > > > > > > > > I am not sure I follow. Let me ask differently. Wouldn't you > > > > > achieve the same if you simply didn't uncharge root memcg in > > > > > obj_cgroup_charge_pages? > > > > > > > > I'm afraid not. Some pages should uncharge root memcg, some > > > > pages should not uncharge root memcg. But all those pages belong > > > > to the root memcg. We cannot distinguish between the two. > > > > > > > > I believe Roman is very familiar with this mechanism (objcg APIs). > > > > > > > > Hi Roman, > > > > > > > > Any thoughts on this? > > > > > > First, unfortunately we do export the root's counter on cgroup v1: > > > /sys/fs/cgroup/memory/memory.kmem.usage_in_bytes > > > But we don't ignore these counters for the root mem cgroup, so there > > > are no bugs here. (Otherwise, please, reproduce it). So it's all about > > > the potential warning in page_counter_cancel(). > > > > Right. > > > > > > > > The patch looks technically correct to me. Not sure about __try_charge() > > > naming, we never use "__" prefix to do something with the root_mem_cgroup. > > > > > > The commit message should be more clear and mention the following: > > > 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. > > > > Very clear. Thanks. > > > > > > > > But honestly I'm not sure the problem is worth the time spent on the fix > > > and the discussion. It's a small race and it's generally hard to trigger > > > a kernel allocation racing with a cgroup deletion and then you need *a lot* > > > of such races and then maybe there will be a single warning printed without > > > *any* other consequences. > > > > I agree the race is very small. Since the fix is easy, but a little confusing > > to someone. I want to hear other people's suggestions on whether to fix it. > > I'm not opposing the idea to fix this issue. But, __please__, make sure you > include all necessary information into the commit log. Got it. Thanks Roman. > > Thanks!