Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3890611pxb; Tue, 17 Nov 2020 06:18:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJdion9hP6F3i5EWNv5l/eQJmxa5+yKd7QhOJXk+oWyi6i4WJ2bE7TaJcSsYcd7te7TBoL X-Received: by 2002:aa7:ce8d:: with SMTP id y13mr7558021edv.65.1605622690414; Tue, 17 Nov 2020 06:18:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605622690; cv=none; d=google.com; s=arc-20160816; b=FZHezLjWgPgYOZG3BULlxHH4zNWFjk+Ci3QWVasndGplE3Ljnx169wwVx7OrXC5KKR 2TOz2YF0bgeDsHBQEGCwCX6B98QACeLyez606kWItTgNlEKN1ocqL1YinIdTqzbbaMgM MoAriX9lNLupT69B9sG5QKLr+lJh7c6sXFXDpH2Elsoipm5CWkEYdDVWTsVs5m13PUui o5G42NrMwwYV1us4G4OE5ce6LdnXQJutPh9/YSZLQBGV7ckSvEswcmcicLYwwpYID6Ry rTz6VQ790YyqD+yIbVdnVVV3+F2oCO4tqDKyHvNLYQkKMVDcOcZA7WeArBvUAOY5dzRY 6bmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=v++rXuBJXgqFwYD+Oi4NSMGWabIYHT4Dv4+/w+Zio8A=; b=CUfXFhfi9bX+xCr5Q4Zzk/T4Jl6aIoN947UbYRIX/Fgt3iLfGLi704vQRXAaCMqb8M d0x/fUE3890qJ24sgb9TvkXybkdgr9v41P0nKYg9+zhGOSJxHtwaBPitlQKwyNeG1sBH u5bMAIXSEZyuNiZgFp8vZcvzd9191SqgEHakAjN7BRGmeFHFKklX35J9/GXTcv21Joy9 Ve8rkS49p1T/6cn703rKmZHgJdWTF4Kizdzhc/6SV92b0OwPaNw8eF4JW5b4lsH9vDoM qhs9uwT3ptdlVqa7/+TCBne5cep/WYyRDUwGicH/xafHSVrA1L0+U+OFJkuznZ1lDr5G HAcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yyLFQK0T; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r19si13776748ejr.323.2020.11.17.06.17.42; Tue, 17 Nov 2020 06:18:10 -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=@kernel.org header.s=default header.b=yyLFQK0T; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731997AbgKQNbH (ORCPT + 99 others); Tue, 17 Nov 2020 08:31:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:40074 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731304AbgKQNas (ORCPT ); Tue, 17 Nov 2020 08:30:48 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0A48920867; Tue, 17 Nov 2020 13:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605619846; bh=6+JiWbjiMpRAJIAOZZHvDe3aPZI7EpxydlFtgIX8Fxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yyLFQK0TK5EOP1ftX53QjoIcHsZZfT3qbx3X/n+jhyzpNaBifV0YEPAwvru/6poxV UGoBPKxmgz0HHliJV5IeRNUwVlJH7NX0ZFpO1T2TsC9PYE4wG8V6etk7EiZ8GPBm78 L3u+upDejeUG8TzJaPWb3U7Oe1+QVUCnU6UrkgQY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, ltp@lists.linux.it, Roman Gushchin , Andrew Morton , Shakeel Butt , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Johannes Weiner , Michal Hocko , Linus Torvalds , Sasha Levin , Richard Palethorpe Subject: [PATCH 5.9 003/255] mm: memcg: link page counters to root if use_hierarchy is false Date: Tue, 17 Nov 2020 14:02:23 +0100 Message-Id: <20201117122139.098694275@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201117122138.925150709@linuxfoundation.org> References: <20201117122138.925150709@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Roman Gushchin [ Upstream commit 8de15e920dc85d1705ab9c202c95d56845bc2d48 ] Richard reported a warning which can be reproduced by running the LTP madvise6 test (cgroup v1 in the non-hierarchical mode should be used): WARNING: CPU: 0 PID: 12 at mm/page_counter.c:57 page_counter_uncharge (mm/page_counter.c:57 mm/page_counter.c:50 mm/page_counter.c:156) Modules linked in: CPU: 0 PID: 12 Comm: kworker/0:1 Not tainted 5.9.0-rc7-22-default #77 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.13.0-48-gd9c812d-rebuilt.opensuse.org 04/01/2014 Workqueue: events drain_local_stock RIP: 0010:page_counter_uncharge (mm/page_counter.c:57 mm/page_counter.c:50 mm/page_counter.c:156) Call Trace: __memcg_kmem_uncharge (mm/memcontrol.c:3022) drain_obj_stock (./include/linux/rcupdate.h:689 mm/memcontrol.c:3114) drain_local_stock (mm/memcontrol.c:2255) process_one_work (./arch/x86/include/asm/jump_label.h:25 ./include/linux/jump_label.h:200 ./include/trace/events/workqueue.h:108 kernel/workqueue.c:2274) worker_thread (./include/linux/list.h:282 kernel/workqueue.c:2416) kthread (kernel/kthread.c:292) ret_from_fork (arch/x86/entry/entry_64.S:300) The problem occurs because in the non-hierarchical mode non-root page counters are not linked to root page counters, so the charge is not propagated to the root memory cgroup. After the removal of the original memory cgroup and reparenting of the object cgroup, the root cgroup might be uncharged by draining a objcg stock, for example. It leads to an eventual underflow of the charge and triggers a warning. Fix it by linking all page counters to corresponding root page counters in the non-hierarchical mode. Please note, that in the non-hierarchical mode all objcgs are always reparented to the root memory cgroup, even if the hierarchy has more than 1 level. This patch doesn't change it. The patch also doesn't affect how the hierarchical mode is working, which is the only sane and truly supported mode now. Thanks to Richard for reporting, debugging and providing an alternative version of the fix! Fixes: bf4f059954dc ("mm: memcg/slab: obj_cgroup API") Reported-by: Signed-off-by: Roman Gushchin Signed-off-by: Andrew Morton Reviewed-by: Shakeel Butt Reviewed-by: Michal Koutný Acked-by: Johannes Weiner Cc: Michal Hocko Cc: Link: https://lkml.kernel.org/r/20201026231326.3212225-1-guro@fb.com Debugged-by: Richard Palethorpe Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/memcontrol.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 9eefdb9cc2303..de51787831728 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5298,7 +5298,13 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) memcg->swappiness = mem_cgroup_swappiness(parent); memcg->oom_kill_disable = parent->oom_kill_disable; } - if (parent && parent->use_hierarchy) { + if (!parent) { + page_counter_init(&memcg->memory, NULL); + page_counter_init(&memcg->swap, NULL); + page_counter_init(&memcg->memsw, NULL); + page_counter_init(&memcg->kmem, NULL); + page_counter_init(&memcg->tcpmem, NULL); + } else if (parent->use_hierarchy) { memcg->use_hierarchy = true; page_counter_init(&memcg->memory, &parent->memory); page_counter_init(&memcg->swap, &parent->swap); @@ -5306,11 +5312,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) page_counter_init(&memcg->kmem, &parent->kmem); page_counter_init(&memcg->tcpmem, &parent->tcpmem); } else { - page_counter_init(&memcg->memory, NULL); - page_counter_init(&memcg->swap, NULL); - page_counter_init(&memcg->memsw, NULL); - page_counter_init(&memcg->kmem, NULL); - page_counter_init(&memcg->tcpmem, NULL); + page_counter_init(&memcg->memory, &root_mem_cgroup->memory); + page_counter_init(&memcg->swap, &root_mem_cgroup->swap); + page_counter_init(&memcg->memsw, &root_mem_cgroup->memsw); + page_counter_init(&memcg->kmem, &root_mem_cgroup->kmem); + page_counter_init(&memcg->tcpmem, &root_mem_cgroup->tcpmem); /* * Deeper hierachy with use_hierarchy == false doesn't make * much sense so let cgroup subsystem know about this -- 2.27.0