Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2383364ybi; Sun, 9 Jun 2019 10:21:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqyGYkt1NkUF80a8ljC7TCzYVvnxMkS0DIKjZxUFWpSQkVPhDcryJso/PmYVuJFtGU7ym8f+ X-Received: by 2002:a65:5345:: with SMTP id w5mr12249322pgr.110.1560100901455; Sun, 09 Jun 2019 10:21:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560100901; cv=none; d=google.com; s=arc-20160816; b=BIKIf/D754fVodQ0FWvTgPgr32XlMzGat9V2u2NhjUe1kJ6NSxdPXbzUXcKkB6efjt nVJHdB8U+nvS6ol8L0Go+8Ho1lzJnRnYJQTzupI+ZlNf8l+RY4ImFS6u927FGWB21HnT leQmYX8Eed0Quw3aD4yQn9dQvuBnWqsbvpIUrqbX+KKBTS0fKJUjYLaBtiS4l6ZkIREs 7aAqztEp4sw6Owt1eKTGie6o6B/ZROWmH7vxyqekQ9ipZ3+vJcckFs/Um1cAjZqRkj8K EyTAEtdocI7k1WKv8nj7cemy82SsW5hiMsm5p9PitNrc2LLR0rGJDCih4wDZ/N031F5w 6UCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=ACJ30A/Mvxipzw9s6ZSrzImQxdOVyc9S1cfXlCbiESk=; b=vEq8ZJQvgH/Ws6t+DRRYq8IGhy3wvxIINV5A4bMRtrhifC4pIt3W4Iwg9LqlufOJ19 8B8L2WY1PK9IHCB4VcWYKcem8Vyv1DX3s5kF9H3cAaa1kzo92Luvkhc4VlXAt2Z1uOdS DPqQ+RvfFeJwshYJxDYBk//OVjyQRtaoy2g+R5VLKqRmudiHPOcedSL+7yp26uj2i76h +0p7fC5sauoXyP2VvYNWj0nWglUX8TO6aVeaXRG55ogdK2aBZaiIWu5uZ8rcrxSTgVwt 2O9RjI2kL9MJ8niYHMG56rjBaaQiZugVtsw3mBpoRAwZeHaq+/qZgRa01HeqyJdul2z+ yvSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nD6vUOey; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d39si8106321pla.371.2019.06.09.10.21.25; Sun, 09 Jun 2019 10:21:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nD6vUOey; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732422AbfFIRSh (ORCPT + 99 others); Sun, 9 Jun 2019 13:18:37 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:33109 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730714AbfFIRSf (ORCPT ); Sun, 9 Jun 2019 13:18:35 -0400 Received: by mail-lj1-f195.google.com with SMTP id v29so5858687ljv.0 for ; Sun, 09 Jun 2019 10:18:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ACJ30A/Mvxipzw9s6ZSrzImQxdOVyc9S1cfXlCbiESk=; b=nD6vUOeyAeSqCFxG3FLOQ9sFcOVhkFlKok1Jbq9mtVpnkNftXLLZC4JimBJFEnBCBi dtN3r5gTyxfQ7zSuT4H26gAZkPn6N2UmG6/qQy134Tuqy+jc2cI1wHSB0K79P+GHsLP/ MeIEquWhKYto4c5dDtF8Wduuzob3Nugv9F31IxmDUn9zfZCsNp6I/mG7ZaUWbQG5DZpk yN4ECnumY7bmL6uNLh1eVBW6NAujJQd38azs2LUaFypv30RvmTTkZutdQvBEhcM0GEM/ C4O9z64oiUcm6ve/3WoqLC+cCd4Sbya5dfeqHKSPe/uZcdQXEO3uUB+slMCvagy4r3Bp UwkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ACJ30A/Mvxipzw9s6ZSrzImQxdOVyc9S1cfXlCbiESk=; b=nc7o5npXOltpiT374+bUtPLLw9lNNvn2kHaQzN/FXGFznDHwOMzSi4bik7pc7TIEkI 0OXIgtb5Xv35dG0IbtVS5uNlhLzQq+L945z8jTU2Tbl/r/OcbNHhcRA+eTWkxxi8a031 +/rCqRNYX00HTH982vTsxAY2gZ0Dbqhax5GvWrqbI5BzeSp/EwlK8I3O6KaKCL0MlmOb qSzEHuF/SyZR3mMyb8myVrYm7z136K2eX3QVzkqjTi2iXrw0CKShMGj47ZrgJPjF3lYe 8fqBtcHAtseiKnuCRyZcOT/HrcdT7h7htNLuhfLES9XszuVfAbSJV/Hg6/cqKJ+TaRAe w46w== X-Gm-Message-State: APjAAAUAEHYH/K0dujKCUSoOJfAFExXkcT4z5QYvgI6olG0r3GmwXsZh I/AGiKyswT02WJQX0evrS70= X-Received: by 2002:a2e:9654:: with SMTP id z20mr7213303ljh.52.1560100714163; Sun, 09 Jun 2019 10:18:34 -0700 (PDT) Received: from esperanza ([176.120.239.149]) by smtp.gmail.com with ESMTPSA id c8sm1473675ljk.77.2019.06.09.10.18.33 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 09 Jun 2019 10:18:33 -0700 (PDT) Date: Sun, 9 Jun 2019 20:18:31 +0300 From: Vladimir Davydov To: Roman Gushchin Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, Johannes Weiner , Shakeel Butt , Waiman Long Subject: Re: [PATCH v6 10/10] mm: reparent slab memory on cgroup removal Message-ID: <20190609171831.er3wdlmnkhwptqzr@esperanza> References: <20190605024454.1393507-1-guro@fb.com> <20190605024454.1393507-11-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190605024454.1393507-11-guro@fb.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 04, 2019 at 07:44:54PM -0700, Roman Gushchin wrote: > Let's reparent memcg slab memory on memcg offlining. This allows us > to release the memory cgroup without waiting for the last outstanding > kernel object (e.g. dentry used by another application). > > So instead of reparenting all accounted slab pages, let's do reparent > a relatively small amount of kmem_caches. Reparenting is performed as > a part of the deactivation process. > > Since the parent cgroup is already charged, everything we need to do > is to splice the list of kmem_caches to the parent's kmem_caches list, > swap the memcg pointer and drop the css refcounter for each kmem_cache > and adjust the parent's css refcounter. Quite simple. > > Please, note that kmem_cache->memcg_params.memcg isn't a stable > pointer anymore. It's safe to read it under rcu_read_lock() or > with slab_mutex held. > > We can race with the slab allocation and deallocation paths. It's not > a big problem: parent's charge and slab global stats are always > correct, and we don't care anymore about the child usage and global > stats. The child cgroup is already offline, so we don't use or show it > anywhere. > > Local slab stats (NR_SLAB_RECLAIMABLE and NR_SLAB_UNRECLAIMABLE) > aren't used anywhere except count_shadow_nodes(). But even there it > won't break anything: after reparenting "nodes" will be 0 on child > level (because we're already reparenting shrinker lists), and on > parent level page stats always were 0, and this patch won't change > anything. > > Signed-off-by: Roman Gushchin > --- > include/linux/slab.h | 4 ++-- > mm/list_lru.c | 8 +++++++- > mm/memcontrol.c | 14 ++++++++------ > mm/slab.h | 23 +++++++++++++++++------ > mm/slab_common.c | 22 +++++++++++++++++++--- > 5 files changed, 53 insertions(+), 18 deletions(-) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 1b54e5f83342..109cab2ad9b4 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -152,7 +152,7 @@ void kmem_cache_destroy(struct kmem_cache *); > int kmem_cache_shrink(struct kmem_cache *); > > void memcg_create_kmem_cache(struct mem_cgroup *, struct kmem_cache *); > -void memcg_deactivate_kmem_caches(struct mem_cgroup *); > +void memcg_deactivate_kmem_caches(struct mem_cgroup *, struct mem_cgroup *); > > /* > * Please use this macro to create slab caches. Simply specify the > @@ -638,7 +638,7 @@ struct memcg_cache_params { > bool dying; > }; > struct { > - struct mem_cgroup *memcg; > + struct mem_cgroup __rcu *memcg; > struct list_head children_node; > struct list_head kmem_caches_node; > struct percpu_ref refcnt; > diff --git a/mm/list_lru.c b/mm/list_lru.c > index 0f1f6b06b7f3..0b2319897e86 100644 > --- a/mm/list_lru.c > +++ b/mm/list_lru.c > @@ -77,11 +77,15 @@ list_lru_from_kmem(struct list_lru_node *nlru, void *ptr, > if (!nlru->memcg_lrus) > goto out; > > + rcu_read_lock(); > memcg = mem_cgroup_from_kmem(ptr); > - if (!memcg) > + if (!memcg) { > + rcu_read_unlock(); > goto out; > + } > > l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); > + rcu_read_unlock(); > out: > if (memcg_ptr) > *memcg_ptr = memcg; > @@ -131,12 +135,14 @@ bool list_lru_add(struct list_lru *lru, struct list_head *item) > > spin_lock(&nlru->lock); > if (list_empty(item)) { > + rcu_read_lock(); > l = list_lru_from_kmem(nlru, item, &memcg); > list_add_tail(item, &l->list); > /* Set shrinker bit if the first element was added */ > if (!l->nr_items++) > memcg_set_shrinker_bit(memcg, nid, > lru_shrinker_id(lru)); > + rcu_read_unlock(); AFAICS we don't need rcu_read_lock here, because holding nlru->lock guarantees that the cgroup doesn't get freed. If that's correct, I think we better remove __rcu mark and use READ_ONCE for accessing memcg_params.memcg, thus making it the caller's responsibility to ensure the cgroup lifetime. > nlru->nr_items++; > spin_unlock(&nlru->lock); > return true; > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index c097b1fc74ec..0f64a2c06803 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -3209,15 +3209,15 @@ static void memcg_offline_kmem(struct mem_cgroup *memcg) > */ > memcg->kmem_state = KMEM_ALLOCATED; > > - memcg_deactivate_kmem_caches(memcg); > - > - kmemcg_id = memcg->kmemcg_id; > - BUG_ON(kmemcg_id < 0); > - > parent = parent_mem_cgroup(memcg); > if (!parent) > parent = root_mem_cgroup; > > + memcg_deactivate_kmem_caches(memcg, parent); > + > + kmemcg_id = memcg->kmemcg_id; > + BUG_ON(kmemcg_id < 0); > + > /* > * Change kmemcg_id of this cgroup and all its descendants to the > * parent's id, and then move all entries from this cgroup's list_lrus > @@ -3250,7 +3250,6 @@ static void memcg_free_kmem(struct mem_cgroup *memcg) > if (memcg->kmem_state == KMEM_ALLOCATED) { > WARN_ON(!list_empty(&memcg->kmem_caches)); > static_branch_dec(&memcg_kmem_enabled_key); > - WARN_ON(page_counter_read(&memcg->kmem)); > } > } > #else > @@ -4675,6 +4674,9 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) > > /* The following stuff does not apply to the root */ > if (!parent) { > +#ifdef CONFIG_MEMCG_KMEM > + INIT_LIST_HEAD(&memcg->kmem_caches); > +#endif > root_mem_cgroup = memcg; > return &memcg->css; > } > diff --git a/mm/slab.h b/mm/slab.h > index 7ead47cb9338..34bf92382ecd 100644 > --- a/mm/slab.h > +++ b/mm/slab.h > @@ -268,7 +268,7 @@ static inline struct mem_cgroup *memcg_from_slab_page(struct page *page) > > s = READ_ONCE(page->slab_cache); > if (s && !is_root_cache(s)) > - return s->memcg_params.memcg; > + return rcu_dereference(s->memcg_params.memcg); I guess it's worth updating the comment with a few words re cgroup lifetime.