Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp14226ybt; Tue, 7 Jul 2020 14:41:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNax/IJUfCYZT6wc61TD2ygEitGJSEvLugE8iQlOCIEeeVGsI7FTI6oAGSDWzdwgpMu18L X-Received: by 2002:a05:6402:b79:: with SMTP id cb25mr42666135edb.154.1594158101645; Tue, 07 Jul 2020 14:41:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594158101; cv=none; d=google.com; s=arc-20160816; b=XjnVF3jnlPBk7EcpudVWhJdD1KJPI8lXS1c8+8CNg+s9NdhHlhfa/H8ojd77P8UWfb tQzhpvAFofkdoYCvc/KK/r4dTno+umLxLo2UmpWtN7Dfh1RWbtImYaVcetepR13s9PBi tH0cNu2nRDt/s40aAW7/A0JNZtje0mNVoUSlhDOgTmgJclnkec0WJ+vwMQEiiARn1d6i yUKrJxuxC05OPtC5537V+hsn/nJ+2a0cGY2vb27c0LL027osKDuSixsJqWepwP7xTjNo 78CcVSyEpPZE4WG+gEfxnLJ66Obk/6NKCl82t2VdPaRqj9nLYJ+IKENTh7V2bBEN9niO 8/hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :subject:cc:to:from:date:dkim-signature; bh=86ZO0cDUMoFRuQFL1rAuMjsl3l6Acwgz3uk4qfYDTYw=; b=bm9s+2jP+WSo/IidSHvxIvgIKDC2b/waIay/nmFXe0jUcM/oeQfUd2AFXGb/sl12Z6 HwcCVKJTEl7744w6nQ6gwJtBd1GMPOJiC0PO3MQW7fRVb6AlDzLoSswPeUkEVZ7sRZ25 n0yk1jR6fnaHbrBsKgJNJnRuUfKZEGadfJwn+hquOnINnUdQQaJDmI3qT/LNo/i908r7 3vXz8DgqhkYEWOCaLSieM+/euIz8iWqsBPnKhWTTh7m6q5Sn/ibQ3q1vnIYEP81+pyoE m1GJHF24SytLgPf0h0LSlad46eiCAgjDyrdU/fMHFUhMoxnYVtge2peplJj/HKwfkVhW 3Ung== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=fyRqItPO; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x1si2261517ejj.109.2020.07.07.14.41.18; Tue, 07 Jul 2020 14:41:41 -0700 (PDT) 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=@google.com header.s=20161025 header.b=fyRqItPO; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728509AbgGGVij (ORCPT + 99 others); Tue, 7 Jul 2020 17:38:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728337AbgGGVii (ORCPT ); Tue, 7 Jul 2020 17:38:38 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB4FCC061755 for ; Tue, 7 Jul 2020 14:38:38 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id j80so39650290qke.0 for ; Tue, 07 Jul 2020 14:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=86ZO0cDUMoFRuQFL1rAuMjsl3l6Acwgz3uk4qfYDTYw=; b=fyRqItPO3JMzl5UDYTAG3aDxi1u1Zt9gxagsbBYONe2VYOkTXrvOxV9OxonWeofXTR vnmLUGaW4hRzibnGl/5JFMXpFkWZY0wXAxajpYmGcAKooeoHcbgdVQkp86VuWSjLt4HO ltAXffK1FBIb/mmNBkZz5ugvlYqOCmx1wTUoTQWLrAYWmKQZU79luODl7ifDKTX9E5/0 o3i+4WxTh2YSdP5RrnZmhyj/s+f0Pen/sh0c4p5LOUv2K8qiAx1xnSNKXTs9BDJu022l qiHm03WzbshWw2HbpRFpvbm6Qy+TdL8lmMudjOwWWAy1Ckn7DFDUve5wBZYXenxETYXL C/6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=86ZO0cDUMoFRuQFL1rAuMjsl3l6Acwgz3uk4qfYDTYw=; b=Vtv/gvbV+GEkmGmgSMzEJ9oUb8G9TOK7c4RCDTXADdczH8HNl9DAmG3bzjB4x0JRJk 2yHewqyHYqt0Jy1amB7vXxOQhiH456L/7JiCH0Tjj/1VT9qgVK9SlvQZ3xT/3XF6lhFI nWyd5fqLAMd4vv2OUrqUUgzhUjipsLXb5DQJsXZ2OtZMF7zx5BdyauZynVcPDkfr0wrk pJ553xmpNnC9e/5y4wwlRZFsvrNNNnz5HB0FmieNoW5ul0+Ep+TMlDUnxGaBN1Sp22E0 9uIpOWqt6NthUeHW096JGRpN++v9iH+AoW5rslvFrPCBhOdO0JHOh6btvo2TV5pTk9pd Zd/Q== X-Gm-Message-State: AOAM5307/IyxIcwQ+pnM4rSh26Oq3eSTge3gBEOHtD0OUJnhItjd/XOW RPtaR5Hx8eUU20duw0PpF9oP3ZUO+Ow= X-Received: by 2002:a37:4289:: with SMTP id p131mr24422127qka.28.1594157917451; Tue, 07 Jul 2020 14:38:37 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id o12sm26346467qtl.48.2020.07.07.14.38.35 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Tue, 07 Jul 2020 14:38:36 -0700 (PDT) Date: Tue, 7 Jul 2020 14:38:18 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Johannes Weiner , Alex Shi , Shakeel Butt , Hugh Dickins , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mm/memcg: fix refcount error while moving and swapping Message-ID: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It was hard to keep a test running, moving tasks between memcgs with move_charge_at_immigrate, while swapping: mem_cgroup_id_get_many()'s refcount is discovered to be 0 (supposedly impossible), so it is then forced to REFCOUNT_SATURATED, and after thousands of warnings in quick succession, the test is at last put out of misery by being OOM killed. This is because of the way moved_swap accounting was saved up until the task move gets completed in __mem_cgroup_clear_mc(), deferred from when mem_cgroup_move_swap_account() actually exchanged old and new ids. Concurrent activity can free up swap quicker than the task is scanned, bringing id refcount down 0 (which should only be possible when offlining). Just skip that optimization: do that part of the accounting immediately. Fixes: 615d66c37c75 ("mm: memcontrol: fix memcg id ref counter on swap charge move") Cc: Signed-off-by: Hugh Dickins --- This was frustrating while testing Alex Shi's patches a few weeks ago, and no fault of those. I may have misattributed the "Fixes", which was itself fixing an earlier, which were both backported to v3.19; or maybe it goes back way further than those, I didn't pursue it - not top of the list of user complaints! Certainly goes back before the refcount_add() in v4.20, which replaced a VM_BUG_ON(atomic_read <= 0). mm/memcontrol.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- 5.8-rc4/mm/memcontrol.c 2020-06-28 15:52:13.360672658 -0700 +++ linux/mm/memcontrol.c 2020-07-05 18:11:51.136542439 -0700 @@ -5669,7 +5669,6 @@ static void __mem_cgroup_clear_mc(void) if (!mem_cgroup_is_root(mc.to)) page_counter_uncharge(&mc.to->memory, mc.moved_swap); - mem_cgroup_id_get_many(mc.to, mc.moved_swap); css_put_many(&mc.to->css, mc.moved_swap); mc.moved_swap = 0; @@ -5860,7 +5859,8 @@ put: /* get_mctgt_type() gets the page ent = target.ent; if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { mc.precharge--; - /* we fixup refcnts and charges later. */ + mem_cgroup_id_get_many(mc.to, 1); + /* we fixup other refcnts and charges later. */ mc.moved_swap++; } break;