Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp210790pxu; Tue, 1 Dec 2020 09:23:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJwmNMZr9aiZ+2jf7NLwnP5mAbsqtX14HqgS38DtXW8gSdUJTq2szi/Ui9dSvrF4eX8B8Rsr X-Received: by 2002:a17:906:8046:: with SMTP id x6mr4003510ejw.189.1606843385696; Tue, 01 Dec 2020 09:23:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606843385; cv=none; d=google.com; s=arc-20160816; b=ZOovz7K4eJ/KlhJ1CFwELLxzBS0659rhGX6JHBpk6S39BXsx1pI7xudfU1gsPz4OPS kCO4BxXhlRBbc/Wz7ie3rl+SR9T56k1PntcPDCuYsuN7tMXyqwNWePepbo2hqbn0+n7E 4vmG/tgBKmcIOL9Z56FCV7gZkN/ZsXnoOCJDd7Ib1Y5+spbIEXDYn/tbRKQ6tuciGotB RW2ekdGk8c52Bwq+q5FpILyfJJSlAnFKg5g/6JoqOb4m8B4rIFW2/kyj9KjdGqWnLocg TfVJRKvizljWWhCqTWZXEvebS3/ItxKRTJ6lB1FMcjGqG2PbKYDPkB6vTFrZt521Oihr bNmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=LQKIIhxcjoKVEtwnQ+W/fhUOvKOLq2pB3RUeQtzqXE0=; b=DKbYEvqdlWdLGxT2MaZggOaQw1+JM9h1D1JaGPq5PKscGj0/qm7OeqFarLXOpgnoTI PPPK61lNHtOIT9IDJbSBQUeVTO2EW2vM65uylGUXQehkZTQ77/3q1jgqtwx9iKiwI33X AkilqfbgUlVYg+58+YH2f5vS4JZ5O/r4mF5mEUBo/W1kTPpeGVjLe5A63mF7Bj7DSaj/ HBMt/ok/Ue9ZEv39FJTT06L474DQ+n6ok1rJgk7g8REjQTIoRGxcP5thZCeUlYCcPqL7 NWAXMsL8W/yJhWcxWp5CYTIcm5k2p7dkfzVpVKSt6cO7wwVHZ7PbGczUJjHCMzI9Dsix XFwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mHJRr5IQ; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ec19si332794ejb.669.2020.12.01.09.22.39; Tue, 01 Dec 2020 09:23:05 -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=@gmail.com header.s=20161025 header.b=mHJRr5IQ; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730377AbgLART7 (ORCPT + 99 others); Tue, 1 Dec 2020 12:19:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726716AbgLART6 (ORCPT ); Tue, 1 Dec 2020 12:19:58 -0500 Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4438C0613CF for ; Tue, 1 Dec 2020 09:19:17 -0800 (PST) Received: by mail-ed1-x542.google.com with SMTP id y22so4431043edv.1 for ; Tue, 01 Dec 2020 09:19:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=LQKIIhxcjoKVEtwnQ+W/fhUOvKOLq2pB3RUeQtzqXE0=; b=mHJRr5IQ0LM2xeUQGg8kKZ4FHj8Yn4VIVKEcX3t0MtTupgfvFybzy3Rph+eEuHpjon rPote0nXx21oj8cJmXYSEHIwjLihOrFdZziNCckU5Agi0jt2ryCcBRmPZpxrnmaxR1vS F8AaUCJsQJWIhBOjahpFJ49UB/wv398rJbPufSwaGUjnU4FNSfkFFxlgRpttTbwZp1JP +MUQMUR5ToW0ImCOa96JISZY9+0PhIE1IFDJkIhBSO2gpu3kZ89gILYkghqm7wgzkWmg we5dc/k7I5r73vYF3lqnzLokNA+zJ5DhIa7h1sxMMes3Hv1+gRVvkxULSzam2t8G9MVh Ba1Q== 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=LQKIIhxcjoKVEtwnQ+W/fhUOvKOLq2pB3RUeQtzqXE0=; b=VATzIA98d4QLELBtFFqPVfLTCOdQBd1/XvxtdlbQ1oE+o+tkDQqqZy5UO/g2U8JHoz qulmavcnPkTyv8VPro0IHFZtjnOTJm/SZ2LkakDRDcc5eTsLBKYDKxzmXbmAmEuo2tcU LrL11lYRIa4bVe8/IeXW8HnDSGa21mtrGl10MWWyZU6AtGkIQPZ9CKulFXBgt7rEVGtj ZqHkE0UL5ho3Xqmb42aTPcUijcwdfocoSyK1XRCnYb6MoBQk1NiiZHgTq4qcjXsCS+IK py0kW71ltt5Y2nc172CuWhDSJzk93hBqRJf5BtB56ohTR2+6+3+CXKyRg4ymUXc9/Dpt 6YMQ== X-Gm-Message-State: AOAM533dIHVl9jm+Mw/6AMd2wT14HmsZHYqnagJLp97zbMfQGYAh46T/ C7cHw+mZNHIN56OfNNMMDM8C2w9Ou7iLC9FQwmI= X-Received: by 2002:a05:6402:1c8a:: with SMTP id cy10mr4016563edb.151.1606843156527; Tue, 01 Dec 2020 09:19:16 -0800 (PST) MIME-Version: 1.0 References: <20201130184514.551950-1-shy828301@gmail.com> In-Reply-To: From: Yang Shi Date: Tue, 1 Dec 2020 09:19:04 -0800 Message-ID: Subject: Re: [PATCH] mm: list_lru: hold nlru lock to avoid reading transient negative nr_items To: Kirill Tkhai Cc: Vladimir Davydov , Roman Gushchin , Shakeel Butt , Andrew Morton , Linux MM , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 1, 2020 at 2:09 AM Kirill Tkhai wrote: > > On 30.11.2020 21:45, Yang Shi wrote: > > When investigating a slab cache bloat problem, significant amount of > > negative dentry cache was seen, but confusingly they neither got shrunk > > by reclaimer (the host has very tight memory) nor be shrunk by dropping > > cache. The vmcore shows there are over 14M negative dentry objects on lru, > > but tracing result shows they were even not scanned at all. The further > > investigation shows the memcg's vfs shrinker_map bit is not set. So the > > reclaimer or dropping cache just skip calling vfs shrinker. So we have > > to reboot the hosts to get the memory back. > > > > I didn't manage to come up with a reproducer in test environment, and the > > problem can't be reproduced after rebooting. But it seems there is race > > between shrinker map bit clear and reparenting by code inspection. The > > hypothesis is elaborated as below. > > > > The memcg hierarchy on our production environment looks like: > > root > > / \ > > system user > > > > The main workloads are running under user slice's children, and it creates > > and removes memcg frequently. So reparenting happens very often under user > > slice, but no task is under user slice directly. > > > > So with the frequent reparenting and tight memory pressure, the below > > hypothetical race condition may happen: > > > > CPU A CPU B CPU C > > reparent > > dst->nr_items == 0 > > shrinker: > > total_objects == 0 > > add src->nr_items to dst > > set_bit > > retrun SHRINK_EMPTY > > clear_bit > > list_lru_del() > > reparent again > > dst->nr_items may go negative > > due to current list_lru_del() > > on CPU C > > The second run of shrinker: > > read nr_items without any > > synchronization, so it may > > see intermediate negative > > nr_items then total_objects > > may return 0 conincidently > > > > keep the bit cleared > > dst->nr_items != 0 > > skip set_bit > > add scr->nr_item to dst > > Good catch, Yang. Thanks for investigating this. > > But I agree with Roman it's better to fix that in rare-called place > (memcg_drain_list_lru_node()), than in hot place (list_lru_count_one()). Yes, agreed. Will incarnate Roman's proposal in v2. > > Also, I'd added to description of new patch a reference to memcg_offline_kmem(), > because this is the place, where child->kmemcg_id is rewritten, and > this is the reason of lru's nr_items may become negative. Sure. > > > After this point dst->nr_item may never go zero, so reparenting will not > > set shrinker_map bit anymore. And since there is no task under user > > slice directly, so no new object will be added to its lru to set the > > shrinker map bit either. That bit is kept cleared forever. > > > > How does list_lru_del() race with reparenting? It is because > > reparenting replaces childen's kmemcg_id to parent's without protecting > > from nlru->lock, so list_lru_del() may see parent's kmemcg_id but > > actually deleting items from child's lru, but dec'ing parent's nr_items, > > so the parent's nr_items may go negative as commit > > 2788cf0c401c268b4819c5407493a8769b7007aa ("memcg: reparent list_lrus and > > free kmemcg_id on css offline") says. > > > > Can we move kmemcg_id replacement after reparenting? No, because the > > race with list_lru_del() may result in negative src->nr_items, but it > > will never be fixed. So the shrinker may never return SHRINK_EMPTY then > > keep the shrinker map bit set always. The shrinker will be always > > called for nonsense. > > > > Can we synchronize list_lru_del() and reparenting? Yes, it could be > > done. But it seems we need introduce a new lock or use nlru->lock. But > > it sounds complicated to move kmemcg_id replacement code under nlru->lock. > > And list_lru_del() may be called quite often to exacerbate some hot > > path, i.e. dentry kill. > > > > So, it sounds acceptable to synchronize reading nr_items to avoid seeing > > intermediate negative nr_items given the simplicity and it is typically > > just called by shrinkers when counting the freeable objects. > > > > The patch is tested with some shrinker intensive workloads, no > > noticeable regression is soptted. > > > > Cc: Vladimir Davydov > > Cc: Kirill Tkhai > > Cc: Roman Gushchin > > Cc: Shakeel Butt > > Signed-off-by: Yang Shi > > --- > > mm/list_lru.c | 11 +++++++++-- > > 1 file changed, 9 insertions(+), 2 deletions(-) > > > > diff --git a/mm/list_lru.c b/mm/list_lru.c > > index 5aa6e44bc2ae..5c128a7710ff 100644 > > --- a/mm/list_lru.c > > +++ b/mm/list_lru.c > > @@ -178,10 +178,17 @@ unsigned long list_lru_count_one(struct list_lru *lru, > > struct list_lru_one *l; > > unsigned long count; > > > > - rcu_read_lock(); > > + /* > > + * Since list_lru_{add,del} may be called under an IRQ-safe lock, > > + * we have to use IRQ-safe primitives here to avoid deadlock. > > + * > > + * Hold the lock to prevent from seeing transient negative > > + * nr_items value. > > + */ > > + spin_lock_irq(&nlru->lock); > > l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); > > count = READ_ONCE(l->nr_items); > > - rcu_read_unlock(); > > + spin_unlock_irq(&nlru->lock); > > > > return count; > > } > > > >