From patchwork Tue Dec 1 21:25:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 11943825 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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham 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 88C62C64E7B for ; Tue, 1 Dec 2020 21:25:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D6152208FE for ; Tue, 1 Dec 2020 21:25:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="s1gOsSbN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D6152208FE 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 3460B8D0001; Tue, 1 Dec 2020 16:25:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F58D6B0068; Tue, 1 Dec 2020 16:25:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20C8C8D0001; Tue, 1 Dec 2020 16:25:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0228.hostedemail.com [216.40.44.228]) by kanga.kvack.org (Postfix) with ESMTP id 0B6036B005D for ; Tue, 1 Dec 2020 16:25:58 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C2CB2181AC9CB for ; Tue, 1 Dec 2020 21:25:57 +0000 (UTC) X-FDA: 77545995954.22.clam10_3615532273ad Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id A40A718038E87 for ; Tue, 1 Dec 2020 21:25:57 +0000 (UTC) X-HE-Tag: clam10_3615532273ad X-Filterd-Recvd-Size: 8148 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Tue, 1 Dec 2020 21:25:57 +0000 (UTC) Received: by mail-pf1-f175.google.com with SMTP id w187so1999660pfd.5 for ; Tue, 01 Dec 2020 13:25:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6AH1ye9fcQ8nEEAvfgVavcXuDhAsKsHPZtAf/e+wA6w=; b=s1gOsSbNUBq/F4jbyfdn6LKmnFZ7L3RcucNI9foFNgpc7TbE239wWPabc3qM7xGtz5 QEwcvuZFUgbc5HdHqVX2poAQ3dp5kTkBiYqtNI4izLeA3bZZVL8PHjKxi3aMkqcRKcpj y0aRYKU7o6OayWkMoNEjHCsM2dnfV/X77A4Aqm/mnaB7VPc7uJllfHlUONrWAHMa/eLl ZRqBfxg1BzQRNByJsv3tQ26zMEih64PLoBhvb9N2X+QWcIEIbkSJwtBZYRzUTNmhDOPA XNqyAQaWoX/ukxWZ2QxE4g2be8jtmAyWtWYhsfnfZ+bx4RrDl8OWlDxBkZ1K2bRJPpqL L8GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6AH1ye9fcQ8nEEAvfgVavcXuDhAsKsHPZtAf/e+wA6w=; b=Uhb+fRQ7VxtYiiiPGhapWl2KZhQ0ocw5BG+j9dmYBTxEUs6/Y4mKr0Ip9XABhgROYp w0u1990jwh2JkMwh/vQJ5YMgFsX/bkCJJ0mdt+K2US4GZG5xYRD0DhWa7CyH06NyzH2z zMbr3KlLzz1izM+RJWC/IeL250Nj30JBzVRq0J57M/gvrl5eNt+d2olsaMIR584VOM6Q 3h5gBEzncvIJpArXBbjZ09PbNXExtj/lB+42jbq18KblEK2r37dh27i8Q28XAyAy1p+W Ocnb6ZVTxBYvnK6PjPCtWDPmTxnDo0ai/ZP+OY3/FKRk0oAj73eMVfxQ1fndDwWy3gKE tKlg== X-Gm-Message-State: AOAM530gvG5+TKpKWgl25Qg7J9uI04ojiFRteA6Rr/nokJqyc1duCc95 kk0LWg7kRZvK20nlO8RXB44= X-Google-Smtp-Source: ABdhPJzZF8tDdP0ExXOYybudhGqHhffhrCgLWTD49FXQSd5GtdGgJF9fqTo4wtIyI71M11h8o0CBMw== X-Received: by 2002:a65:66d3:: with SMTP id c19mr739264pgw.215.1606857956144; Tue, 01 Dec 2020 13:25:56 -0800 (PST) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id l141sm670433pfd.124.2020.12.01.13.25.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 13:25:55 -0800 (PST) From: Yang Shi To: guro@fb.com, ktkhai@virtuozzo.com, vdavydov.dev@gmail.com, shakeelb@google.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [v3 PATCH] mm: list_lru: set shrinker map bit when child nr_items is not zero Date: Tue, 1 Dec 2020 13:25:53 -0800 Message-Id: <20201201212553.52164-1-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 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: 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 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() between shrinker runs see parent's kmemcg_id dec dst->nr_items reparent again dst->nr_items may go negative due to concurrent list_lru_del() 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. 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 Reviewed-by: Roman Gushchin Cc: Vladimir Davydov Cc: Kirill Tkhai Cc: Shakeel Butt Cc: v4.19+ Signed-off-by: Yang Shi Acked-by: Kirill Tkhai Reviewed-by: Shakeel Butt --- v3: * Revised commit log per Roman's suggestion * Added Roman's reviewed-by tag v2: * Incorporated Roman's suggestion * Incorporated Kirill's suggestion * Changed the subject of patch to get align with the new fix * Added fixes tag mm/list_lru.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/list_lru.c b/mm/list_lru.c index 5aa6e44bc2ae..fe230081690b 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -534,7 +534,6 @@ static void memcg_drain_list_lru_node(struct list_lru *lru, int nid, struct list_lru_node *nlru = &lru->node[nid]; int dst_idx = dst_memcg->kmemcg_id; struct list_lru_one *src, *dst; - bool set; /* * Since list_lru_{add,del} may be called under an IRQ-safe lock, @@ -546,11 +545,12 @@ static void memcg_drain_list_lru_node(struct list_lru *lru, int nid, dst = list_lru_from_memcg_idx(nlru, dst_idx); list_splice_init(&src->list, &dst->list); - set = (!dst->nr_items && src->nr_items); - dst->nr_items += src->nr_items; - if (set) + + if (src->nr_items) { + dst->nr_items += src->nr_items; memcg_set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru)); - src->nr_items = 0; + src->nr_items = 0; + } spin_unlock_irq(&nlru->lock); }