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=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham 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 8AC08C433B4 for ; Sun, 11 Apr 2021 06:24:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CA5B3610E9 for ; Sun, 11 Apr 2021 06:24:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CA5B3610E9 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 4A67D8E0002; Sun, 11 Apr 2021 02:24:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 456C78E0001; Sun, 11 Apr 2021 02:24:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D0058E0002; Sun, 11 Apr 2021 02:24:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0092.hostedemail.com [216.40.44.92]) by kanga.kvack.org (Postfix) with ESMTP id 0D3348E0001 for ; Sun, 11 Apr 2021 02:24:49 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B396D941F for ; Sun, 11 Apr 2021 06:24:48 +0000 (UTC) X-FDA: 78019097856.16.2D17B0F Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf13.hostedemail.com (Postfix) with ESMTP id 38576E000104 for ; Sun, 11 Apr 2021 06:24:45 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d8so4676064plh.11 for ; Sat, 10 Apr 2021 23:24:47 -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=ZU/zzhJUZ4M4YMsK8aEsYeoEHlx9HrupSDSHAYxx9F4=; b=a0ap3PRI9gljeJVT0H1HyuuTzKbhoQlydNntQ6LkHaSSzuaJFL81AJO2AW+6bzjvaB yTpY9tpX9glHTcp4igj27sTO8wIvFqdU6yKJYEIUmb56wS3W6EPERoxQ8rYPT+tO6zlV yX2QtoMMu7kD691blmGz2jEUkU750xjlx34wI88Fo0B6MsFZb2Myu7Y7P3zaFLNZ6wEB cEVtE1dIr9y2/qCYNSNA93+/Ro1sTncUeKRTnyBDv6ZgkmOenl9Pg6BDBxE4f1rXPf+c j1Fqv5iKi6QtDkH06kSHR42SHi+4i+E5F7QhJxWC20KGoSxWOkQJRykwzYcqHPlSSHoc CSGg== 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=ZU/zzhJUZ4M4YMsK8aEsYeoEHlx9HrupSDSHAYxx9F4=; b=or/Aq+O93Adtt1wXcs/y2kVWndGn6vCsAptnrjk5azHf7zNOluR7u2Sd+usuD+2S8r KTvKYSgZe9v2ZLd27jhBzlQG8ktlpRQkdczwlTUOsV4wUoZVrey9tHwIp1kqZLh2yjMs EaNo0Vu2TMUesjSnnXVGr9vd0Ejn9lCo+en+R7n/dpJKLIAwoPWcTKMPF7y9u0seoN2U QhItq8a035MG5H7/oAg1tSJnmbMM5s52QWpC5XKtg89LhNqxSuZWGnR/D+QmffiDiNMZ hYQa2s+kzhefL+/WLbdCvjyr3wuUtmACf+rQk0ZskO4bXxH9Ook942/hJwKR3uQWS6Xf uj/A== X-Gm-Message-State: AOAM531bX2tnj0HYwDjL+yAsQV05ATOkavN9Q2M+hdjkiuyVoymh4zTt /cqqOShQzEcAoUGguw9Ux1c3G6dv9NJaAyywMoENzg== X-Google-Smtp-Source: ABdhPJzWbIhNmiud0ovMQll9Xmo28fr5lNxq35QlvcIm31hsXGcAMqYnHwKnbcLhcB2Ls9XV79OQk+HXAdQrGbD5NzE= X-Received: by 2002:a17:90a:901:: with SMTP id n1mr22487174pjn.147.1618122286375; Sat, 10 Apr 2021 23:24:46 -0700 (PDT) MIME-Version: 1.0 References: <20210409122959.82264-1-songmuchun@bytedance.com> <20210409122959.82264-7-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Sun, 11 Apr 2021 14:24:09 +0800 Message-ID: Subject: Re: [External] Re: [RFC PATCH v2 06/18] mm: memcontrol: move the objcg infrastructure out of CONFIG_MEMCG_KMEM To: Johannes Weiner Cc: guro@fb.com, mhocko@kernel.org, akpm@linux-foundation.org, shakeelb@google.com, vdavydov.dev@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, bsingharora@gmail.com, shy828301@gmail.com, alex.shi@linux.alibaba.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 38576E000104 X-Stat-Signature: gtk1bqmo475jq58wz44fqbiohgdd85dk Received-SPF: none (bytedance.com>: No applicable sender policy available) receiver=imf13; identity=mailfrom; envelope-from=""; helo=mail-pl1-f170.google.com; client-ip=209.85.214.170 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1618122285-266862 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 Sat, Apr 10, 2021 at 12:56 AM Johannes Weiner wrote: > > On Fri, Apr 09, 2021 at 08:29:47PM +0800, Muchun Song wrote: > > Because memory allocations pinning memcgs for a long time - it exists > > at a larger scale and is causing recurring problems in the real world: > > page cache doesn't get reclaimed for a long time, or is used by the > > second, third, fourth, ... instance of the same job that was restarted > > into a new cgroup every time. Unreclaimable dying cgroups pile up, > > waste memory, and make page reclaim very inefficient. > > > > We can convert LRU pages and most other raw memcg pins to the objcg > > direction to fix this problem, and then the page->memcg will always > > point to an object cgroup pointer. > > > > Therefore, the infrastructure of objcg no longer only serves > > CONFIG_MEMCG_KMEM. In this patch, we move the infrastructure of the > > objcg out of the scope of the CONFIG_MEMCG_KMEM so that the LRU pages > > can reuse it to charge pages. > > Just an observation on this: > > We actually may want to remove CONFIG_MEMCG_KMEM altogether at this > point. It used to be an optional feature, but nowadays it's not > configurable anymore, and always on unless slob is configured. > > We've also added more than just slab accounting to it, like kernel > stack pages, and it all gets disabled on slob configs just because it > doesn't support slab object tracking. > > We could probably replace CONFIG_MEMCG_KMEM with CONFIG_MEMCG in most > places, and add a couple of !CONFIG_SLOB checks in the slab callbacks. > > But that's beyond the scope of your patch series, so I'm also okay > with this patch here. > > > We know that the LRU pages are not accounted at the root level. But the > > page->memcg_data points to the root_mem_cgroup. So the page->memcg_data > > of the LRU pages always points to a valid pointer. But the root_mem_cgroup > > dose not have an object cgroup. If we use obj_cgroup APIs to charge the > > LRU pages, we should set the page->memcg_data to a root object cgroup. So > > we also allocate an object cgroup for the root_mem_cgroup and introduce > > root_obj_cgroup to cache its value just like root_mem_cgroup. > > > > Signed-off-by: Muchun Song > > Overall, the patch makes sense to me. A few comments below: > > > @@ -252,9 +253,14 @@ struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr) > > return &container_of(vmpr, struct mem_cgroup, vmpressure)->css; > > } > > > > -#ifdef CONFIG_MEMCG_KMEM > > extern spinlock_t css_set_lock; > > > > +static inline bool obj_cgroup_is_root(struct obj_cgroup *objcg) > > +{ > > + return objcg == root_obj_cgroup; > > +} > > This function, and by extension root_obj_cgroup, aren't used by this > patch. Please move them to the patch that adds users for them. OK. Will do. > > > @@ -298,6 +304,20 @@ static void obj_cgroup_release(struct percpu_ref *ref) > > percpu_ref_exit(ref); > > kfree_rcu(objcg, rcu); > > } > > +#else > > +static void obj_cgroup_release(struct percpu_ref *ref) > > +{ > > + struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt); > > + unsigned long flags; > > + > > + spin_lock_irqsave(&css_set_lock, flags); > > + list_del(&objcg->list); > > + spin_unlock_irqrestore(&css_set_lock, flags); > > + > > + percpu_ref_exit(ref); > > + kfree_rcu(objcg, rcu); > > +} > > +#endif > > Having two separate functions for if and else is good when the else > branch is a completely empty dummy function. In this case you end up > duplicating code, so it's better to have just one function and put the > ifdef around the nr_charged_bytes handling in it. Make sense. I will rework the code here. > > > @@ -318,10 +338,14 @@ static struct obj_cgroup *obj_cgroup_alloc(void) > > return objcg; > > } > > > > -static void memcg_reparent_objcgs(struct mem_cgroup *memcg, > > - struct mem_cgroup *parent) > > +static void memcg_reparent_objcgs(struct mem_cgroup *memcg) > > { > > struct obj_cgroup *objcg, *iter; > > + struct mem_cgroup *parent; > > + > > + parent = parent_mem_cgroup(memcg); > > + if (!parent) > > + parent = root_mem_cgroup; > > > > objcg = rcu_replace_pointer(memcg->objcg, NULL, true); > > > > @@ -342,6 +366,27 @@ static void memcg_reparent_objcgs(struct mem_cgroup *memcg, > > percpu_ref_kill(&objcg->refcnt); > > } > > > > +static int memcg_obj_cgroup_alloc(struct mem_cgroup *memcg) > > +{ > > + struct obj_cgroup *objcg; > > + > > + objcg = obj_cgroup_alloc(); > > + if (!objcg) > > + return -ENOMEM; > > + > > + objcg->memcg = memcg; > > + rcu_assign_pointer(memcg->objcg, objcg); > > + > > + return 0; > > +} > > + > > +static void memcg_obj_cgroup_free(struct mem_cgroup *memcg) > > +{ > > + if (unlikely(memcg->objcg)) > > + memcg_reparent_objcgs(memcg); > > +} > > It's confusing to have a 'free' function not free the object it's > called on. > > But rather than search for a fitting name, I think it might be better > to just fold both of these short functions into their only callsites. OK. Will do. > > Also, since memcg->objcg is reparented, and the pointer cleared, on > offlining, when could this ever be non-NULL? This deserves a comment. css_alloc() failed, offlining didn't happen. In this case, memcg->objcg could be non-NULL (Just like memcg_free_kmem() dose). I will move memcg_obj_cgroup_alloc() to the mem_cgroup_css_online() so that we do not need memcg_obj_cgroup_free. > > > @@ -3444,7 +3489,6 @@ static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, > > #ifdef CONFIG_MEMCG_KMEM > > static int memcg_online_kmem(struct mem_cgroup *memcg) > > { > > - struct obj_cgroup *objcg; > > int memcg_id; > > > > if (cgroup_memory_nokmem) > > @@ -3457,14 +3501,6 @@ static int memcg_online_kmem(struct mem_cgroup *memcg) > > if (memcg_id < 0) > > return memcg_id; > > > > - objcg = obj_cgroup_alloc(); > > - if (!objcg) { > > - memcg_free_cache_id(memcg_id); > > - return -ENOMEM; > > - } > > - objcg->memcg = memcg; > > - rcu_assign_pointer(memcg->objcg, objcg); > > - > > static_branch_enable(&memcg_kmem_enabled_key); > > > > memcg->kmemcg_id = memcg_id; > > @@ -3488,7 +3524,7 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) > > if (!parent) > > parent = root_mem_cgroup; > > > > - memcg_reparent_objcgs(memcg, parent); > > + memcg_reparent_objcgs(memcg); > > Since the objcg is no longer tied to kmem, this should move to > mem_cgroup_css_offline() instead. LGTM, will do. Thanks for your all suggestions.