Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3996449pxu; Mon, 30 Nov 2020 15:13:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJyP2xtf8lDmvEZaAEU7fM30/do54KSrRBK8L6gZJwjUSSFhUzCXry2i9RY416HjxWBOh/DW X-Received: by 2002:a17:906:1c8e:: with SMTP id g14mr143469ejh.5.1606778032025; Mon, 30 Nov 2020 15:13:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606778032; cv=none; d=google.com; s=arc-20160816; b=GzWOAssqHaUY4OnBB3Okr1PzWloqTVgnUsoVkRCG8PbChVVxtPIO++eGmVSI4FW7y6 wXZS+RGvc3F6V/lApE2rvIpt501F3G8OAtlEPLNeFNAWJXRMqWQRUUkO4+H356hvxqHU tOwLFOfi36IomF761QYtoEU1xJeoIsdZdYa7O89jJ/z2rfHNiUX6Vm0dXApFBXZSsfrB e+lOLP573sksC7Z1TzeRXGua6CEeG2P9h917Xj1vCmXuWnLr3QoIH53MKLYzTKcrGBCo 0iRXNNAaKvRm/zjSApxoZdfDiUxafe5eamqJ7WLaUqDzkcceGYXnzrWyzP5w9d8n27hn IYUA== 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=XxnkwRTZ0Nf3UlU1Niuq+4jZM/RsNgxDizkMSh8t26E=; b=asveeyTzKYMAGHN6JPtNb+bBaJt6SP95LF1pPWs/HcPm7x0SJECv3wmL8CvayHCrdB lDu2kKCulIJMqCgEw+LuqH6wDaSJMNR21X82klfmTIoeSy4IGEw4YXMNFF4ApVzZnrAN 4FNkaAqatqiCTlxW5MnIGJLd2/veRckAtw81nqx7Y8KyM58lZr00lx9LCa+K8r68lzj4 0d6aIjd3ooRbdI0sFRw1f/RRu6uJ9B3HbUTQd0xK/sZbW4WDC6x39uPOIsXDtO1ttgfY ie+Rk/RJA3BXu2sJfsJmozdy31epyufGR4k7TezQPJwfzaVKur/nEv3GABPqy8RA0QHK zmAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EIHwEmeW; 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 u11si171667eja.365.2020.11.30.15.13.29; Mon, 30 Nov 2020 15:13:52 -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=EIHwEmeW; 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 S1730863AbgK3Wy5 (ORCPT + 99 others); Mon, 30 Nov 2020 17:54:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730855AbgK3Wy4 (ORCPT ); Mon, 30 Nov 2020 17:54:56 -0500 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBA30C0613D2 for ; Mon, 30 Nov 2020 14:54:15 -0800 (PST) Received: by mail-ej1-x643.google.com with SMTP id x16so18113110ejj.7 for ; Mon, 30 Nov 2020 14:54:15 -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=XxnkwRTZ0Nf3UlU1Niuq+4jZM/RsNgxDizkMSh8t26E=; b=EIHwEmeWSB5eXKcF/tL7BDtRHh4/fYCyLEG8DrWDPF1RZoPEe+WmkQtjHAO4E303VD +/vrrVDC9WYna4ZCBRsx3paaRu3M+1M4o99wqfyMUNYuRjUT7gU2bT458ryuxzv0PY5f LcHGCfp7iAvKHJRwJD3BVMYkYqwW4AOXSaKZEWb6UHVlLg9E1A2kkJlPYhX3udfXzOqI rTUVVhT0TG5hFwh3uBNRnZ8gHl39runVO4sOpPg61+/4J6vayMniSLFHYPBYhGoIyPYL tJfTS0PEuZRxTOzUWqfQTcFcMbh1MuGIEPNxDzpZN+DzO33sYozAL4x9Xrh163Edh4Fa LbUQ== 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=XxnkwRTZ0Nf3UlU1Niuq+4jZM/RsNgxDizkMSh8t26E=; b=Ape12ydUXpSxkHr/wL8NRPBUWqamLurkvmNWEtmF08WUjDmvaROof8yFEE+k43tuqs 173BLkP+ftjZM0VEuY4WsqDziXBirjSsBPBXEZFv2S0pRBnRLQpv7WJy06EZDWHOAmjQ Y7KbOE370Gou8qPrauMClLc/+cQiVyQrWTbuEliJqpVmB5ni4rgbEYU/56+qVg38563V 8aEKLzNvIr94/ZFidMzAvu4M8O5ktjWJaoflXlKT7I5BbsAYuSNt07aLIg+dDUD5JLY6 aMjzKXgypYZiO3lvs39yZdSD/wc2gvhPrFQLNaEdU7VeXZGzAr+FgwJbQDsGK4WdNWqu 5G4w== X-Gm-Message-State: AOAM531/Tqkjh+Mv/92raiixkUHyx32AsW4+AzcHHH4eIp1Ss3ENHQZ0 B9cE62bS0yX3eDBbcyoAEh1Ofr9JOMrlTffDI3A= X-Received: by 2002:a17:906:24c3:: with SMTP id f3mr69817ejb.238.1606776854418; Mon, 30 Nov 2020 14:54:14 -0800 (PST) MIME-Version: 1.0 References: <20201130184514.551950-1-shy828301@gmail.com> <20201130200936.GA1354703@carbon.DHCP.thefacebook.com> <20201130223347.GE840171@carbon.dhcp.thefacebook.com> In-Reply-To: <20201130223347.GE840171@carbon.dhcp.thefacebook.com> From: Yang Shi Date: Mon, 30 Nov 2020 14:54:02 -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:33 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 > > > > > > > > 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. > > Also note that since the introduction of the slab reparenting, list_lru_from_kmem() > can return the parent lru. Do you mean slab charge reparenting or lru reparenting? I think list_lru_from_kmem() can return the parent lru since lru reparenting. > > > > > > > > > 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. > > > > > > Hi Yang! > > > > > > It's really tricky, thank you for digging in! It's a perfect analysis! > > > > > > I wonder though, if it's better to just always set the shrinker bit on reparenting > > > if we do reparent some items? Then we'll avoid adding new synchronization > > > to the hot path. What do you think? > > > > Thanks a lot for the suggestion. I was thinking about the same > > approach too, but I thought src->nr_items may go zero due to > > concurrent list_lru_del() at the first place. But I just rethought the > > whole thing, it seems impossible that dst->nr_items goes negative and > > src->nr_items goes zero at the same time. > > Even if it would be possible, it seems less scary: the next reparenting > will likely set the bit. So we'll not get into the permanently bad state. Unfortunately, no. Once the race happens, reparenting won't set the bit anymore since dst->nr_items won't go zero because the shrinker will not be called. > > > list_lru_del() should just > > see either dst or src, it can't manipulate both lists simultaneously. > > So I think your suggestion should work. I will incarnate your > > suggestion in v2. > > > > > > > > -- > > > > > > @@ -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,9 +545,8 @@ 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) > > > memcg_set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru)); > > > src->nr_items = 0; > > > > > > > > > -- > > > > > > Btw, it seems that the bug is quite old. I wonder why we haven't seen it before? > > > Any ideas? > > > > It is not new, but not that old from my point of view. The > > shrinker_map thing was introduced since v4.19, I bet pre-v4.19 kernel > > may still dominate in production environment. And, it needs some > > conditions (i.e. nr_inode + nr_dentry == 0 coincidently, and there is > > not task under dst memcg directly, etc) to trigger, so it seems > > unlikely to hit. > > > > And the consequence may be not noticeable to the most people at all. > > We happened to see frequent OOMs on a couple of small machines (32G > > memory w/o swap, but most memory was consumed by anonymous pages) > > recently and they were already up for long time (almost 300 days), > > then the investigation leads to this race condition. > > I agree that most users will unlikely notice it. > > But https://www.spinics.net/lists/cgroups/msg27295.html looks very similar > and can be caused by the same problem. Once you'll have v2, let's ask > them to test it too. Yeah, may be the same root cause. > > Thanks!