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=-7.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no 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 41564C64E7B for ; Tue, 1 Dec 2020 20:12:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8074A2151B for ; Tue, 1 Dec 2020 20:12:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZApU1Pr1" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8074A2151B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 69C646B0036; Tue, 1 Dec 2020 15:12:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 64B936B005D; Tue, 1 Dec 2020 15:12:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 565958D0001; Tue, 1 Dec 2020 15:12:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0026.hostedemail.com [216.40.44.26]) by kanga.kvack.org (Postfix) with ESMTP id 3FD826B0036 for ; Tue, 1 Dec 2020 15:12:24 -0500 (EST) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0B4D7181AEF1E for ; Tue, 1 Dec 2020 20:12:24 +0000 (UTC) X-FDA: 77545810608.01.mine48_301053c273ad Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id CDAF11004ED74 for ; Tue, 1 Dec 2020 20:12:23 +0000 (UTC) X-HE-Tag: mine48_301053c273ad X-Filterd-Recvd-Size: 8550 Received: from mail-ed1-f66.google.com (mail-ed1-f66.google.com [209.85.208.66]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Tue, 1 Dec 2020 20:12:23 +0000 (UTC) Received: by mail-ed1-f66.google.com with SMTP id r5so1899946eda.12 for ; Tue, 01 Dec 2020 12:12:23 -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=IdV9WVEGceglx0cHy6mJTuBEPbUl8WbSuNfkxhy/hFI=; b=ZApU1Pr1AK5m//dB82jNhtcePYVRdsMambAex44ZT//E3HokkRxe0KnZMzVqrYLXy4 094p3ntx6uM2pZ7jQmpO/ghxk/vJdryp8ubgahnvJDCQD72yWSr8MhQnEcPRqMUGO7Ts 3NddvLIGhVCdQfmVEfjk1ohJr7sl1Qp4OpvGYBH02nZbqIH3lKs8QxfIORTQvoWfIyjn oyuoniM63fRz2tmiUksrrIL3lTw9ccGn8Xm0hbb1EWZKU/enHzJ42raBb9fv5H/6p0q/ /fFO+IZoNi1+OU7TAUxJ8e740BDo8C4ayZQRfX/jB05SzUbg6MSpxhuWU9lM8vRIhF9v s0HA== 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=IdV9WVEGceglx0cHy6mJTuBEPbUl8WbSuNfkxhy/hFI=; b=cS+dBMK4jx4MjSDsETEgzd4roVpeA5d6U9cLyoHF/bGEsu9/tDye02IpcwNa0XBFXu bNyUV75Qh7BSufxFzRXG7Z+dTLwAtdjvIDTZmtvLWkaHxf96Moz6jPGmR5SnlLldAdxx TuT6CvLhRqwDxLzAY1Dv/wcvrQX8XicG2gzEbOfzfAqteMba5sRnIggJQdvqnCbyTpi/ TVH/b1x5CkqkATvvMN0Vmn0qgflsrxAKh7lm9VAfq+tGO6peyUnvJ0Ej2HUNE7NHDGAn IQ0VLKvKJdAIak0D4ZUVAgAjjNHLDeVoF7S/dvbooNK0hwJTU9l4B7S4T1cXZtvDYvJU hxhA== X-Gm-Message-State: AOAM531apG+n1uqjwHGUKk+qVfoP0/xnAhCPGhWQk1Ys2NJB/Nbpvyf/ YoVOwTxquEFJKFVbsn8S8IsqPuO8V+0n8PRVWo0= X-Google-Smtp-Source: ABdhPJyATAeb+MblLObtQdU7abWws/96CS4xUE/lb4MGbR6iWKI5njKri/U4o1oQW8hb3nqLLscPzOQ+uNPdzurJ3XY= X-Received: by 2002:a05:6402:746:: with SMTP id p6mr4859514edy.313.1606853542105; Tue, 01 Dec 2020 12:12:22 -0800 (PST) MIME-Version: 1.0 References: <20201201174449.51435-1-shy828301@gmail.com> <20201201195232.GC1375014@carbon.DHCP.thefacebook.com> In-Reply-To: <20201201195232.GC1375014@carbon.DHCP.thefacebook.com> From: Yang Shi Date: Tue, 1 Dec 2020 12:12:07 -0800 Message-ID: Subject: Re: [v2 PATCH] mm: list_lru: set shrinker map bit when child nr_items is not zero To: Roman Gushchin Cc: Kirill Tkhai , Vladimir Davydov , Shakeel Butt , Andrew Morton , Linux MM , Linux Kernel Mailing List , stable Content-Type: text/plain; charset="UTF-8" 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 Tue, Dec 1, 2020 at 11:52 AM Roman Gushchin wrote: > > On Tue, Dec 01, 2020 at 09:44:49AM -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 > > child memcg offline > > replace child's kmemcg_id to > > parent's (in memcg_offline_kmem()) > > list_lru_del() > > see parent's kmemcg_id > > dec dst->nr_items > > reparent again > > dst->nr_items may go negative > > due to concurrent 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 > > > > 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. > > > > Since it is impossible that dst->nr_items goes negative and > > src->nr_items goes zero at the same time, so it seems we could set the > > shrinker map bit iff src->nr_items != 0. We could synchronize > > list_lru_count_one() and reparenting with nlru->lock, but it seems > > checking src->nr_items in reparenting is the simplest and avoids lock > > contention. > > > > Fixes: fae91d6d8be5 ("mm/list_lru.c: set bit in memcg shrinker bitmap on first list_lru item appearance") > > Suggested-by: Roman Gushchin > > Cc: Vladimir Davydov > > Cc: Kirill Tkhai > > Cc: Shakeel Butt > > Cc: v4.19+ > > Signed-off-by: Yang Shi > > Hi Yang! > > Code-wise it looks good to me. Thank you for updating! > > I think the commit log can be simplified a bit: you don't really need 3 CPUs > to reproduce the problem. Also, IMO the section about fixing the problem by > introducing an additional synchronization can be dropped, but it's up to you. Yes, don't have to have 3 CPUs. Will simplify the commit log. > > With the updated commit log, please feel to add > Reviewed-by: Roman Gushchin . Thanks! > > Thank you!