Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp877424pxb; Fri, 22 Apr 2022 13:14:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwl1JX7wthn6HiafGAuZ1lgpMs8HOLGgaULodgparyBRwqqKPehLbmXcTw1eypAxuMT4fzH X-Received: by 2002:aa7:8d47:0:b0:4f6:a7f9:1ead with SMTP id s7-20020aa78d47000000b004f6a7f91eadmr6774125pfe.42.1650658478299; Fri, 22 Apr 2022 13:14:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650658478; cv=none; d=google.com; s=arc-20160816; b=mS3sCCrOzVoOen0ssNqhYMvApV6BJr2JRkQIomOmu+7Qttfk1n7ZM/x9d4/3wmmBo5 VheeeV/ZRN4YMJIyG83CjePrOD28AQ/J/Y2fzyyA43U108ExRrFXUPNdXJmnIDXAGQeQ ijXIz2R3139XEARneZBVI9iwkk5Sj2s6+14WgjswPeuvUE5stFqsUZw3SmoO8htAmQPn OVEY81mqyUCLAENVUbmxQrjotMowyWew2aQ6mp+nluGeuBCD7xulUxVtyznGkg/58PsT w1R4ZvHl8P4vcH9BUA7pCgpr5M9QgWxnLtF0y1bxGLqkgvc74Vxl0sJ6hiWgen7VQzkA N8iA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=QiB2o+WdMsJeAeDDSCARTXP2m3be3V0si7VrlcKUU9o=; b=qK5oWpvC5kAcjwmH1WdpTcxjSNbX9IODinKxgDIVPQ3mpWBcawYaolhXfb8c6/+XXA Ug4zSm/gACnbYx9qKckA+2X0JxKZMtXFCzmOZI6BIVKrknzbpRS8oy8AOxKWTuKfRWi7 YDhjZ3Y6vN0Ktg/rjfwe4iF5WiaQLRDNK8c+x8GsXQxZaK8Wb5xi3Uewdiy8j450l04B cRV2qQwx27gWdyx/qwDhyaJFv8vc4ug3kYJgriGuSOs7XWquGO/f6FQgBtHX2o+0RUwp hQq+nMgYBcMb0G61vjVaZdPwpgRJHgsZD5d8pD6WM/1qX5F1q8vfW8zbLOkmnYldFCRd QSbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=EpVOrHjP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id l5-20020a17090a598500b001c64f3d4750si11461360pji.161.2022.04.22.13.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 13:14:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=EpVOrHjP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2DEC91B5D6C; Fri, 22 Apr 2022 12:16:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446525AbiDVKaW (ORCPT + 99 others); Fri, 22 Apr 2022 06:30:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351711AbiDVKaV (ORCPT ); Fri, 22 Apr 2022 06:30:21 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C467D54BDA for ; Fri, 22 Apr 2022 03:27:27 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id k29so6979728pgm.12 for ; Fri, 22 Apr 2022 03:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=QiB2o+WdMsJeAeDDSCARTXP2m3be3V0si7VrlcKUU9o=; b=EpVOrHjPnfUPosjTlIQ1i2uTS3VaVjSTEsn9fvxTlMiqlmD5AwJQeVwCDiQcArIIKN G0U4XrIyJIN3CfVCUIrKzrqmbxecuVOE+Ns+XyT6sLIQq3Sz+vUfV3qF4jkcNVStV+HD ohqFPfNeLvtVze2xs3540Uag578+Sa0Tio3U23Oj9fuHVhxC10HtbglTic+F4nkj+rnJ uNZ4AmDYyFqaAMKue+LnbfyyCvEFjw3OFO08wu8B27tOJZSsok3aK5Esq1bRVszqWR8L yJ2z08rjpeL6YiYDefHLoElqOMY1TQYX5c4B0SjdTEwSs9+LPZm8GPtYD9zlA2w7ZbTc jLKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=QiB2o+WdMsJeAeDDSCARTXP2m3be3V0si7VrlcKUU9o=; b=Fq5H5mZfldbO2aMezKQp3DJnMPx+d41uA0bO2E8Aldw25P8qhJd245bmhr/DuSijJ3 l5sAc7sE7FBjSU7nrvi5WVEfi1QHWWQ/rgyFVip6pRWM5X0sM7X7a1ZmGR4KkbSsjH9L fhSznu2Wu3MB5L4O1uMMsVij02yeme1PXHR5b8o2QXm8Xiq7jW8CDavMBxzke6Key5y3 IOfKzdRFEDkAYoUDapw5ZoPAN2M5HowwdXqSQAWiELxKFNl6TpgRV+MRGzv4WmB1AE2c QekJauvdLQ+1DM4/v70a721YJzFWezXoyu8ec2Q5wgGtCmjmWtr5yTaijBlcUzwuDSF4 deOQ== X-Gm-Message-State: AOAM530SzbizoZJFnukf9uXPv3UexWjD7AqdIA5DSnr09T63Ei+bNe6n TiQ36tMYr3+ot7dpTblFejxzAQ== X-Received: by 2002:a05:6a02:206:b0:399:3c9:f465 with SMTP id bh6-20020a056a02020600b0039903c9f465mr3409854pgb.388.1650623247325; Fri, 22 Apr 2022 03:27:27 -0700 (PDT) Received: from localhost ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id t38-20020a634626000000b0039cc30b7c93sm1779592pga.82.2022.04.22.03.27.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 03:27:27 -0700 (PDT) Date: Fri, 22 Apr 2022 18:27:22 +0800 From: Muchun Song To: Roman Gushchin Cc: Waiman Long , Johannes Weiner , Michal Hocko , Shakeel Butt , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, "Matthew Wilcox (Oracle)" , Yang Shi , Vlastimil Babka Subject: Re: [PATCH] mm/memcg: Free percpu stats memory of dying memcg's Message-ID: References: <20220421145845.1044652-1-longman@redhat.com> <112a4d7f-bc53-6e59-7bb8-6fecb65d045d@redhat.com> <58c41f14-356e-88dd-54aa-dc6873bf80ff@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 21, 2022 at 07:59:00PM -0700, Roman Gushchin wrote: > On Thu, Apr 21, 2022 at 02:46:00PM -0400, Waiman Long wrote: > > On 4/21/22 13:59, Roman Gushchin wrote: > > > On Thu, Apr 21, 2022 at 01:28:20PM -0400, Waiman Long wrote: > > > > On 4/21/22 12:33, Roman Gushchin wrote: > > > > > On Thu, Apr 21, 2022 at 10:58:45AM -0400, Waiman Long wrote: > > > > > > For systems with large number of CPUs, the majority of the memory > > > > > > consumed by the mem_cgroup structure is actually the percpu stats > > > > > > memory. When a large number of memory cgroups are continuously created > > > > > > and destroyed (like in a container host), it is possible that more > > > > > > and more mem_cgroup structures remained in the dying state holding up > > > > > > increasing amount of percpu memory. > > > > > > > > > > > > We can't free up the memory of the dying mem_cgroup structure due to > > > > > > active references in some other places. However, the percpu stats memory > > > > > > allocated to that mem_cgroup is a different story. > > > > > > > > > > > > This patch adds a new percpu_stats_disabled variable to keep track of > > > > > > the state of the percpu stats memory. If the variable is set, percpu > > > > > > stats update will be disabled for that particular memcg. All the stats > > > > > > update will be forward to its parent instead. Reading of the its percpu > > > > > > stats will return 0. > > > > > > > > > > > > The flushing and freeing of the percpu stats memory is a multi-step > > > > > > process. The percpu_stats_disabled variable is set when the memcg is > > > > > > being set to offline state. After a grace period with the help of RCU, > > > > > > the percpu stats data are flushed and then freed. > > > > > > > > > > > > This will greatly reduce the amount of memory held up by dying memory > > > > > > cgroups. > > > > > > > > > > > > By running a simple management tool for container 2000 times per test > > > > > > run, below are the results of increases of percpu memory (as reported > > > > > > in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat. > > > > > Hi Waiman! > > > > > > > > > > I've been proposing the same idea some time ago: > > > > > https://lore.kernel.org/all/20190312223404.28665-7-guro@fb.com/T/ . > > > > > > > > > > However I dropped it with the thinking that with many other fixes > > > > > preventing the accumulation of the dying cgroups it's not worth the added > > > > > complexity and a potential cpu overhead. > > > > > > > > > > I think it ultimately comes to the number of dying cgroups. If it's low, > > > > > memory savings are not worth the cpu overhead. If it's high, they are. > > > > > I hope long-term to drive it down significantly (with lru-pages reparenting > > > > > being the first major milestone), but it might take a while. > > > > > > > > > > I don't have a strong opinion either way, just want to dump my thoughts > > > > > on this. > > > > I have quite a number of customer cases complaining about increasing percpu > > > > memory usages. The number of dying memcg's can go to tens of thousands. From > > > > my own investigation, I believe that those dying memcg's are not freed > > > > because they are pinned down by references in the page structure. I am aware > > > > that we support the use of objcg in the page structure which will allow easy > > > > reparenting, but most pages don't do that and it is not easy to do this > > > > conversion and it may take quite a while to do that. > > > The big question is whether there is a memory pressure on those systems. > > > If yes, and the number of dying cgroups is growing, it's worth investigating. > > > It might be due to the sharing of pagecache pages and this will be ultimately > > > fixed with implementing of the pagecache reparenting. But it also might be due > > > to other bugs, which are fixable, so it would be great to understand. > > > > > > Pagecache reparenting will probably fix the problem that I have seen. Is > > someone working on this? > > Some time ago Muchun posted patches based on the reusing of the obj_cgroup API. > Yep. It is here: https://lore.kernel.org/all/20220216115132.52602-1-songmuchun@bytedance.com/. > I'm not strictly against this approach, but in my opinion it's not the best. > I suggested to use lru vectors as an intermediate objects. In theory, it might I remember this. > allow to avoid bumping reference counters for all charged pages at all: live > cgroups will be protected by being live, dying cgroups will only need > a temporarily protection while lru vectors and associated pages are reparenting. > > There are pros and cons: > + cgroup reference counting becomes simpler and more debuggable > + potential perf wins from fewer operations with live cgroups css refcounters > = I hope to see code simplifications (but not guaranteed) > - deleting cgroups becomes more expensive, but the cost can be spread to > asynchronous workers > > Idk if Muchun tried to implement it. If not, I might try myself. > Yep. I have implemented a initial version recently. I'll do some stability tests and send it out ASAP. Thanks Roman.