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=-3.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED 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 00DAEC433B4 for ; Fri, 23 Apr 2021 08:21:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ADFE261209 for ; Fri, 23 Apr 2021 08:21:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241292AbhDWIVq (ORCPT ); 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-Google-Smtp-Source: ABdhPJyA4QzVqfKaLyIGnfT8rOC4yH+HNaEwhpK9D7gfbFaje8oux2w/1t+LWvsce4SvE8vAWI2uTvngUrAaNvSgL/E= 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! 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=-3.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED 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 3EE79C433ED for ; Fri, 23 Apr 2021 08:21:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8B6946128A for ; Fri, 23 Apr 2021 08:21:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8B6946128A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 00D888D0005; Fri, 23 Apr 2021 04:21:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFE218D0001; Fri, 23 Apr 2021 04:21:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC5D28D0005; Fri, 23 Apr 2021 04:21:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0156.hostedemail.com [216.40.44.156]) by kanga.kvack.org (Postfix) with ESMTP id BD9D98D0001 for ; Fri, 23 Apr 2021 04:21:11 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 7E56D180AE7E2 for ; Fri, 23 Apr 2021 08:21:11 +0000 (UTC) X-FDA: 78062936742.38.295347C Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf04.hostedemail.com (Postfix) with ESMTP id 8060D3C1 for ; Fri, 23 Apr 2021 08:21:07 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id e2so20551171plh.8 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=jA5NVkIvPceTdxrVu90B8b0HeA4q1h+WByRXzIrHfdbO0PoHSoxngdBH1gDwI9UqJi sSgJ6aHGDB3i+tS2jm6aNXcSvRhkF64NNzEfDNOugKquyTdBYQmbiTA7kkZ8kVDZoNyk MF5XSoLtUBij/Jq+6+vL1t01hwXqrVRpA/lMAY2tBfOkuUxF6A7sI4ulGENahbaMFomS oZDMPrpdf8ZZwOgNXI5qbJWBaHMZLpSm+q1T2JcaRR8K6hIjzQRtONDynDA+/H0IKyMp ZkMhoWPP6Td1hD9bG2FT2/CQvbsFO1TSw2nG9lU0o+kQ5tJ/9yvaqmkIVBCmArKuTeT3 u2xQ== X-Gm-Message-State: AOAM530dpW96uBCt33FkpA8ub8DWBsRcxXMX7RuzN1xyxPcTN8tKfhoE crOkQfJDHXzuyJeblhrzYb+0FH7OdF7ZzQ2bDP6Hdg== X-Google-Smtp-Source: ABdhPJyA4QzVqfKaLyIGnfT8rOC4yH+HNaEwhpK9D7gfbFaje8oux2w/1t+LWvsce4SvE8vAWI2uTvngUrAaNvSgL/E= 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" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8060D3C1 X-Stat-Signature: y5yfx746hg5scc8ouq5ys8h81gzunj64 Received-SPF: none (bytedance.com>: No applicable sender policy available) receiver=imf04; identity=mailfrom; envelope-from=""; helo=mail-pl1-f182.google.com; client-ip=209.85.214.182 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619166067-312770 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 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!