Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3996631pxu; Mon, 30 Nov 2020 15:14:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxHvmF+ufVzEh2AakKKnw4DmjH2rwfuRli1xzWOI77VoSA5wX9T0OdbBp4GIt1e9fqj09bv X-Received: by 2002:a17:906:318f:: with SMTP id 15mr130277ejy.183.1606778047278; Mon, 30 Nov 2020 15:14:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606778047; cv=none; d=google.com; s=arc-20160816; b=s1y9/8fETzYs5DPMU97EZlHNvd5WJr29w2sEmLC+96qe39XYLL6I48YNIzt7T9gcl7 jU21zEmnqdy8viaJsXZMwaAmouMortKtO90bHIBcZTYgIoICSaEZuzHhfZpTPgNbzSkx GKzYShH/2vXo8Ugg6UDoEQBr9WLawuYv0o/YreyFYQyvl71+q2sp8Ef4NzGHqe4MELnl aASSCJVKTIyG+BfWqZA0FaFwyJ8ONZ1apuk7S9l9Rd3cRjOJML0RkI5cRucK2/VUVyvG Abs8rx6RK8q9lCeZvjWtx3gBjmtYEb+F5lKgTBXE1DzhpQZqXU8GSxlDpMR/JKP+KPp9 OJkg== 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=VPiFGQkiqWAFPJXQUHUTo1ORgX89o0bCN92vawl5snc=; b=k3G4KOC70frP/FBdf81mJnLtu0zd49M0/bX4sHEks98OpgIBkLZUj+mzhIXelLx7EL VW0veMoAjuMw5b6SvHiu4BxKBvNHxbd/KuUiA+N9h7zW4yLq3FHwgKq/bQlZaXep+cXN a7URfQfHC4LkeoOIsMPLb2Yv+w0iGaoMJ6XoSQ9fA/SMNbhtRTyihI+AASRt8gc9bzq6 mq3LzytHvCde1XTP9ip+6tn6LMKZGaEItdt2ySaGcZInwict6zGU5z8DuaXzCT/YDMaK BLEm+tBAh7ULx5FZuZNvLtpOnsHLF2Dk4v8zMJ4gHpvReZlG4BnK/2obtsW0SsZlRK/p gifQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KgRqSq+m; 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 c2si11993107eds.117.2020.11.30.15.13.44; Mon, 30 Nov 2020 15:14:07 -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=KgRqSq+m; 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 S1730885AbgK3W6Q (ORCPT + 99 others); Mon, 30 Nov 2020 17:58:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728648AbgK3W6Q (ORCPT ); Mon, 30 Nov 2020 17:58:16 -0500 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F26ADC0613D4 for ; Mon, 30 Nov 2020 14:57:35 -0800 (PST) Received: by mail-ej1-x644.google.com with SMTP id 7so25295156ejm.0 for ; Mon, 30 Nov 2020 14:57:35 -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=VPiFGQkiqWAFPJXQUHUTo1ORgX89o0bCN92vawl5snc=; b=KgRqSq+mb9FeUCYdNxy5lVl/TeDwcTk3FhwJ04jq285EVz2Qrm56pUpjxUUcl0JlG4 CoH8pcNtTuSGqyF3oELr/YGrlWhw9WbyxstlzH0jtLC86akm5F3/ni8pfQaehPgcfoiZ PPazu2lIKQbNgn6v6gqY+jsCJCHsmC6YL63W2FKz9gFWcDMyfL+MsNT1O3um3pLlO9Sh HR2JAcX7kimDAyyYSn7pgVlEHzSQ5QOTiBQljDm2W3qTzkfPn4Rdog+b0xW/VhGK5DLV pMKnzA0Mj6wVzkiOkEhfbCJX5MVQbBb/vz612jFpTIzpiXYLd9s05ff8MOPRIwiWKm5g W4AA== 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=VPiFGQkiqWAFPJXQUHUTo1ORgX89o0bCN92vawl5snc=; b=ExspSkfESR8PtIWMMz0oE/gB/H4Q2G5JBwa5OHCRHhbEquiLxGFGePIHgC8JjnRxT1 RLG4yNDzzBWfEEl+dwAfY7jZBMko8Ftf4V2WV8Nbr2MJhFz9Viuy1kYzc8Ij9BPnR8qL GAo/r/ytRtXzoqOrdh5W8AVxJtlmA414DnRQByi5UZqY9I0KNBRph2r/BD1vkm6tyVAj kROGuk+suLsvqhgsG/fywJ+pbao4A+hA52tJQC/RYJ11qgDPA8XVpdrAq1ZZ0K5pIUKB r2g2wuYoqQnJ4iZqMc7wDCtU9MU4oWhGhTGQKro/QmhzvcX4nflkB9dOKj7Ohbk1ZmGq j+mw== X-Gm-Message-State: AOAM530rQ957ijjccXxEc9PHloN5gWxVk2ECA0pXFN92Q8VNaYptcSIo 1EVdRwVg7S6wdNOM6KCCj/z4z82lUnhfSHJM5mM= X-Received: by 2002:a17:906:24c3:: with SMTP id f3mr81171ejb.238.1606777054774; Mon, 30 Nov 2020 14:57:34 -0800 (PST) MIME-Version: 1.0 References: <20201130184514.551950-1-shy828301@gmail.com> <20201130200936.GA1354703@carbon.DHCP.thefacebook.com> <20201130225255.GF840171@carbon.dhcp.thefacebook.com> In-Reply-To: <20201130225255.GF840171@carbon.dhcp.thefacebook.com> From: Yang Shi Date: Mon, 30 Nov 2020 14:57:23 -0800 Message-ID: Subject: Re: [PATCH] mm: list_lru: hold nlru lock to avoid reading transient negative nr_items To: Roman Gushchin Cc: Vladimir Davydov , Kirill Tkhai , 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 Mon, Nov 30, 2020 at 2:53 PM Roman Gushchin wrote: > > On Mon, Nov 30, 2020 at 12:57:47PM -0800, Yang Shi wrote: > > On Mon, Nov 30, 2020 at 12:09 PM Roman Gushchin wrote: > > > > > > On Mon, Nov 30, 2020 at 10:45:14AM -0800, 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 > > Btw, I think I have a simpler explanation: > > A (0 objects) > | > B (N objects) > > Let's say the reparenting races with the deletion of a single slab object. > list_lru_del() can see parent's lru list and substract 1 from nr_items == 0, > setting A's nr_items to -1 (the item is actually still in B's list). > > memcg_drain_list_lru_node() will check !dst->nr_items && src->nr_items > !-1 && N => 0 and not set the bit. But now we have (N-1) objects in A's list > and the shrinker bit not set. Yes, this is the exact race I elaborated in the commit log. > > My proposed fix should resolve it. Alternatively, we maybe can check if > dst->nr_items <= 0 and only then set the bit, but it seems to be an unnecessary > optimization. Yes, I think "src->nr_items != 0" is good enough. > > Thanks!