Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4868683pxu; Thu, 10 Dec 2020 07:23:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJzM8bm0Qejb9d8K0xBOuSWSMJ75O9UzpjafcvPzo/qd1O9xoxBXyq6BIFwdbTrIAbxh6MHE X-Received: by 2002:a17:906:edb2:: with SMTP id sa18mr6597637ejb.264.1607613823103; Thu, 10 Dec 2020 07:23:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607613823; cv=none; d=google.com; s=arc-20160816; b=MgchslhBudns98xjX7GPSxfdIisTdGGTeeyKBhP7fwiVWQ77OZVCa//w68XeHsDAxz h4jM56bUzvApqIbTJWk/MzbEdcmdKBJSclB7RaVJIYHOMV10B0XN+xPNxpQEA8MJisOr o5mdFFTJmu14v3O3/e3hYm1+aWlOUVJZZll3tyB1pxdnpSJhrGl2HAbNdkTM/QOxK4HU yjUaz1eoKdVXlODE+o1qmwTTGJqM7YHmbmy+M3CzRm9RBF3UnG+sR/tukac/j5cKKnpH U75GZn3jMKBTDcjic6zoKwffmgXJZVUorqVauldIPRTXvasuMFuI0L/9usnQy1hXY4UQ TS4Q== 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=T8TYr8U3ruZBc+gFJ9Co9TnsVkvfLvOx2ydXd1OODM0=; b=dAo65UJ+UePbCrY3Ay44cMR2lR/GG4IUD2Vk9yh/+hbxqzd//f61fXqWBlcbU/2xRH XJHLAOAi+L8womFiDCBKjkY5yg+1qL/3MtRT0YVQRgH3hyhQD2VNRZf2tkMyzQ3n+pn8 cXMpveuN4vgjZgF0CboPHb2RSPL8HRVM50JG0P9Ku8YX6zCkgTLzYFIBSCys9fA23IxV H601L0jCnMLj3jjxORrPcHgT/ea79X2CLK7nBsffOqVM9qNsKE5t+juz4qa5YwUf3jZm ZOrAOj/J4uc18b4iY1wwUSoYYKQ2kAuomL3qlNtqWuQKi4LsTL+imv89Yxi/LYOt4LhH kjXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=WU51H6Ml; 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=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p23si2803964eja.133.2020.12.10.07.23.18; Thu, 10 Dec 2020 07:23:43 -0800 (PST) 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=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=WU51H6Ml; 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=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391575AbgLJPRG (ORCPT + 99 others); Thu, 10 Dec 2020 10:17:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389902AbgLJPQT (ORCPT ); Thu, 10 Dec 2020 10:16:19 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB068C0613CF for ; Thu, 10 Dec 2020 07:15:38 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id c198so4961266wmd.0 for ; Thu, 10 Dec 2020 07:15:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=T8TYr8U3ruZBc+gFJ9Co9TnsVkvfLvOx2ydXd1OODM0=; b=WU51H6MlxI70ZfH97X4nphb6PeCVXPQICtS2n8J7/oPyV0L/0dPwJpf89kQhONT5mc Gy4hnVV19Gw9BelWveMRbSKPCjt/mTw3X634a71iuCnPSDKAOq8sdcyET5/iHIsedXRK B8pxA/hTm5+yQzRzC5qVvkrTUb3DybjHXGd1tew3jYlJYqQOrSBfiU4DY43CCS7EICXg As1YITr4s7ajmG2pPGGHBNDmPcVG/ltzv75N08tmGh/IbyknAhYkhG13J1WarMTFfCvN qIVfao7RoPIA4SVTrd/p/1ILgz2/Pu5+IStfEkTU5CoJlgZkajUAbYsJVV+1+Vmu8vdW 40Pw== 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=T8TYr8U3ruZBc+gFJ9Co9TnsVkvfLvOx2ydXd1OODM0=; b=K82phaUgALRhuYdDO8J7UY6FKmeJ8g+QuCrY/mqYdwKQgzijA6dQ9PdQs/72DstH1J 6GG6/7XUvAyy+wjxSKQ4gyo3s0JcEF6XW9iVVhroBzuJLvqKrJTNZXvZ1thyOXkuRg7U cj0U1nw9s0Y51ZVaKwMkxjwouktdDOecHN12VbgW56K82doFCohmFZNoMVeC3KFYNK8K /B0EgNVcFjE1HTnrVLira6xS99LGIhs03au6sCWxfcL/7u96qMttESmcZfWrDUJJDNO6 /G+cGWWExf9WFeDWog2c/plQTK0TAxBVqVEDZbbPlXBeCfQuKFg61lfK5u46PlEYYgb1 zawA== X-Gm-Message-State: AOAM5322/Sy8AKin01IZgLKv6Up0Sb/JyV60IbhBjs//eB5J2iZtsYck v6tt7KSd3Ry//viJuZtxisk68g== X-Received: by 2002:a1c:c305:: with SMTP id t5mr8890217wmf.63.1607613337568; Thu, 10 Dec 2020 07:15:37 -0800 (PST) Received: from localhost (p4fdabc80.dip0.t-ipconnect.de. [79.218.188.128]) by smtp.gmail.com with ESMTPSA id z2sm10467139wml.23.2020.12.10.07.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Dec 2020 07:15:36 -0800 (PST) Date: Thu, 10 Dec 2020 16:13:31 +0100 From: Johannes Weiner To: Yang Shi Cc: Kirill Tkhai , Roman Gushchin , Shakeel Butt , Dave Chinner , Michal Hocko , Andrew Morton , Linux MM , Linux FS-devel Mailing List , Linux Kernel Mailing List Subject: Re: [PATCH 6/9] mm: vmscan: use per memcg nr_deferred of shrinker Message-ID: <20201210151331.GD264602@cmpxchg.org> References: <20201202182725.265020-1-shy828301@gmail.com> <20201202182725.265020-7-shy828301@gmail.com> <49464720-675d-5144-043c-eba6852a9c06@virtuozzo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 09, 2020 at 09:32:37AM -0800, Yang Shi wrote: > On Wed, Dec 9, 2020 at 7:42 AM Kirill Tkhai wrote: > > > > On 08.12.2020 20:13, Yang Shi wrote: > > > On Thu, Dec 3, 2020 at 3:40 AM Kirill Tkhai wrote: > > >> > > >> On 02.12.2020 21:27, Yang Shi wrote: > > >>> Use per memcg's nr_deferred for memcg aware shrinkers. The shrinker's nr_deferred > > >>> will be used in the following cases: > > >>> 1. Non memcg aware shrinkers > > >>> 2. !CONFIG_MEMCG > > >>> 3. memcg is disabled by boot parameter > > >>> > > >>> Signed-off-by: Yang Shi > > >>> --- > > >>> mm/vmscan.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++---- > > >>> 1 file changed, 82 insertions(+), 6 deletions(-) > > >>> > > >>> diff --git a/mm/vmscan.c b/mm/vmscan.c > > >>> index cba0bc8d4661..d569fdcaba79 100644 > > >>> --- a/mm/vmscan.c > > >>> +++ b/mm/vmscan.c > > >>> @@ -203,6 +203,12 @@ static DECLARE_RWSEM(shrinker_rwsem); > > >>> static DEFINE_IDR(shrinker_idr); > > >>> static int shrinker_nr_max; > > >>> > > >>> +static inline bool is_deferred_memcg_aware(struct shrinker *shrinker) > > >>> +{ > > >>> + return (shrinker->flags & SHRINKER_MEMCG_AWARE) && > > >>> + !mem_cgroup_disabled(); > > >>> +} > > >>> + > > >>> static int prealloc_memcg_shrinker(struct shrinker *shrinker) > > >>> { > > >>> int id, ret = -ENOMEM; > > >>> @@ -271,7 +277,58 @@ static bool writeback_throttling_sane(struct scan_control *sc) > > >>> #endif > > >>> return false; > > >>> } > > >>> + > > >>> +static inline long count_nr_deferred(struct shrinker *shrinker, > > >>> + struct shrink_control *sc) > > >>> +{ > > >>> + bool per_memcg_deferred = is_deferred_memcg_aware(shrinker) && sc->memcg; > > >>> + struct memcg_shrinker_deferred *deferred; > > >>> + struct mem_cgroup *memcg = sc->memcg; > > >>> + int nid = sc->nid; > > >>> + int id = shrinker->id; > > >>> + long nr; > > >>> + > > >>> + if (!(shrinker->flags & SHRINKER_NUMA_AWARE)) > > >>> + nid = 0; > > >>> + > > >>> + if (per_memcg_deferred) { > > >>> + deferred = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_deferred, > > >>> + true); > > >> > > >> My comment is about both 5/9 and 6/9 patches. > > > > > > Sorry for the late reply, I don't know why Gmail filtered this out to spam. > > > > > >> > > >> shrink_slab_memcg() races with mem_cgroup_css_online(). A visibility of CSS_ONLINE flag > > >> in shrink_slab_memcg()->mem_cgroup_online() does not guarantee that you will see > > >> memcg->nodeinfo[nid]->shrinker_deferred != NULL in count_nr_deferred(). This may occur > > >> because of processor reordering on !x86 (there is no a common lock or memory barriers). > > >> > > >> Regarding to shrinker_map this is not a problem due to map check in shrink_slab_memcg(). > > >> The map can't be NULL there. > > >> > > >> Regarding to shrinker_deferred you should prove either this is not a problem too, > > >> or to add proper synchronization (maybe, based on barriers) or to add some similar check > > >> (maybe, in shrink_slab_memcg() too). > > > > > > It seems shrink_slab_memcg() might see shrinker_deferred as NULL > > > either due to the same reason. I don't think there is a guarantee it > > > won't happen. > > > > > > We just need guarantee CSS_ONLINE is seen after shrinker_maps and > > > shrinker_deferred are allocated, so I'm supposed barriers before > > > "css->flags |= CSS_ONLINE" should work. > > > > > > So the below patch may be ok: > > > > > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > > > index df128cab900f..9f7fb0450d69 100644 > > > --- a/mm/memcontrol.c > > > +++ b/mm/memcontrol.c > > > @@ -5539,6 +5539,12 @@ static int mem_cgroup_css_online(struct > > > cgroup_subsys_state *css) > > > return -ENOMEM; > > > } > > > > > > > > > + /* > > > + * Barrier for CSS_ONLINE, so that shrink_slab_memcg() sees > > > shirnker_maps > > > + * and shrinker_deferred before CSS_ONLINE. > > > + */ > > > + smp_mb(); > > > + > > > /* Online state pins memcg ID, memcg ID pins CSS */ > > > refcount_set(&memcg->id.ref, 1); > > > css_get(css); > > > > smp barriers synchronize data access from different cpus. They should go in a pair. > > In case of you add the smp barrier into mem_cgroup_css_online(), we should also > > add one more smp barrier in another place, which we want to synchonize with this. > > Also, every place should contain a comment referring to its pair: "Pairs with...". > > Thanks, I think you are correct. Looked into it further, it seems the > race pattern looks like: > > CPU A CPU B > store shrinker_maps pointer load CSS_ONLINE > store CSS_ONLINE load shrinker_maps pointer > > By checking the memory-barriers document, it seems we need write > barrier/read barrier pair as below: > > CPU A CPU B > store shrinker_maps pointer load CSS_ONLINE > > store CSS_ONLINE load shrinker_maps pointer > > > So, the patch should look like: > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index df128cab900f..489c0a84f82b 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -5539,6 +5539,13 @@ static int mem_cgroup_css_online(struct > cgroup_subsys_state *css) > return -ENOMEM; > } > > + /* > + * Barrier for CSS_ONLINE, so that shrink_slab_memcg() sees > shirnker_maps > + * and shrinker_deferred before CSS_ONLINE. It pairs with the > read barrier > + * in shrink_slab_memcg(). > + */ > + smp_wmb(); Is there a reason why the shrinker allocations aren't done in .css_alloc()? That would take care of all necessary ordering: #0 css = ->css_alloc() list_add_tail_rcu(css, parent->children) rcu_assign_pointer() ->css_online(css) css->flags |= CSS_ONLINE #1 memcg = mem_cgroup_iter() list_entry_rcu() rcu_dereference() shrink_slab(.., memcg) RCU ensures that once the cgroup shows up in the reclaim cgroup it's also fully allocated. > /* Online state pins memcg ID, memcg ID pins CSS */ > refcount_set(&memcg->id.ref, 1); > css_get(css); > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 9d2a6485e982..fc9bda576d98 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -603,13 +603,15 @@ static unsigned long shrink_slab_memcg(gfp_t > gfp_mask, int nid, > if (!mem_cgroup_online(memcg)) > return 0; ...then we should be able to delete this online check here entirely: A not-yet online cgroup is guaranteed to have a shrinker map, just with no bits set. The shrinker code handles that just fine. An offlined cgroup will eventually have an empty bitmap as the called shrinkers return SHRINK_EMPTY. This could also be shortcut by clearing the bit in memcg_drain_list_lru_node() the same way we set it in the parent when we move all objects upward, but seems correct as-is.