Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1831222imm; Mon, 3 Sep 2018 10:33:09 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdbgbgo8GqYlrsORZ785YmlCHH4vFaUIAyPpbBmG9tFoqXCmEr5QluI4oTCeVE6AKbH5egU+ X-Received: by 2002:a17:902:7e09:: with SMTP id b9-v6mr1076098plm.221.1535995989351; Mon, 03 Sep 2018 10:33:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535995989; cv=none; d=google.com; s=arc-20160816; b=Fm02iVWZ8vR2T54yAA7BhH7YAtZhVWbK3kFJSUuv+L30lWKmTFGfv0cZYLANY9KDE4 LP2NehIZQM4BYmjxpw5SdmVtGH1L0wT0B4ywbeNu3xTjmIxlxWrWAHHW4+yh8uzA0NJ5 7Ggrf4yh5hTDiRHZ+S/5PoH/de6tuKH9XYvVi1hRhhFFSq5OSAxUEH1lGD40Uxu65Rwo VeThj21HhFiYzq1pMDp5sqmzl7u9N+cJcfmZKSERoECEnHBlK7BBdbrK0ZJpyKBS4wn+ u8vLeo10GfxL8lYBZf6pCYTdGKYDAy8qJHZu8+b0a2O1LPy3o7s2CEPW/vEyzzr8tf1f pLlA== 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:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=Nv1TekUlaxicq1Wn5RjwvDE1gfdrsguzgtj7+lH2bag=; b=cuGnM4R/GtLfwb5Tsadx0dWqyPx30U+7ytx3Ku+og+5d1Muv0YjzBa/7UtWkrah2lm qCM4XAEiayjHAOijxcFu84E8dZEiqhNIZRUklwma0ifKzNcD/+SZu3Q7ZpnGEqFfScs+ POHabd87PkbvAdAGGS+i9lYh9nimOJctzcTUl8u0pQCbK6Q4yztk9XsOLyrt8Q1nwusN FmS/vuMNRvkxkYaRKAkPRBtL596hxf15+OWqBw0EQ9tyscCC7llnwLIL9k8HKMAwtfbJ KFCGbuutyV758z0kg/JY4xtY1GdoomNoAKScxKuM+dcANY3hk6AmmQ/2BsxtwzWO3Lxg eBzA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c11-v6si11444086pgd.620.2018.09.03.10.32.54; Mon, 03 Sep 2018 10:33:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728971AbeICVwK (ORCPT + 99 others); Mon, 3 Sep 2018 17:52:10 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:46000 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728731AbeICVsR (ORCPT ); Mon, 3 Sep 2018 17:48:17 -0400 Received: from localhost (ip-213-127-74-90.ip.prioritytelecom.net [213.127.74.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id E487AD1B; Mon, 3 Sep 2018 17:19:24 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kirill Tkhai , Johannes Weiner , Vladimir Davydov , Michal Hocko , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.14 076/165] memcg: remove memcg_cgroup::id from IDR on mem_cgroup_css_alloc() failure Date: Mon, 3 Sep 2018 18:56:02 +0200 Message-Id: <20180903165658.905484831@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180903165655.003605184@linuxfoundation.org> References: <20180903165655.003605184@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kirill Tkhai [ Upstream commit 7e97de0b033bcac4fa9a35cef72e0c06e6a22c67 ] In case of memcg_online_kmem() failure, memcg_cgroup::id remains hashed in mem_cgroup_idr even after memcg memory is freed. This leads to leak of ID in mem_cgroup_idr. This patch adds removal into mem_cgroup_css_alloc(), which fixes the problem. For better readability, it adds a generic helper which is used in mem_cgroup_alloc() and mem_cgroup_id_put_many() as well. Link: http://lkml.kernel.org/r/152354470916.22460.14397070748001974638.stgit@localhost.localdomain Fixes 73f576c04b94 ("mm: memcontrol: fix cgroup creation failure after many small jobs") Signed-off-by: Kirill Tkhai Acked-by: Johannes Weiner Acked-by: Vladimir Davydov Cc: Michal Hocko Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- mm/memcontrol.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4110,6 +4110,14 @@ static struct cftype mem_cgroup_legacy_f static DEFINE_IDR(mem_cgroup_idr); +static void mem_cgroup_id_remove(struct mem_cgroup *memcg) +{ + if (memcg->id.id > 0) { + idr_remove(&mem_cgroup_idr, memcg->id.id); + memcg->id.id = 0; + } +} + static void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n) { VM_BUG_ON(atomic_read(&memcg->id.ref) <= 0); @@ -4120,8 +4128,7 @@ static void mem_cgroup_id_put_many(struc { VM_BUG_ON(atomic_read(&memcg->id.ref) < n); if (atomic_sub_and_test(n, &memcg->id.ref)) { - idr_remove(&mem_cgroup_idr, memcg->id.id); - memcg->id.id = 0; + mem_cgroup_id_remove(memcg); /* Memcg ID pins CSS */ css_put(&memcg->css); @@ -4258,8 +4265,7 @@ static struct mem_cgroup *mem_cgroup_all idr_replace(&mem_cgroup_idr, memcg, memcg->id.id); return memcg; fail: - if (memcg->id.id > 0) - idr_remove(&mem_cgroup_idr, memcg->id.id); + mem_cgroup_id_remove(memcg); __mem_cgroup_free(memcg); return NULL; } @@ -4318,6 +4324,7 @@ mem_cgroup_css_alloc(struct cgroup_subsy return &memcg->css; fail: + mem_cgroup_id_remove(memcg); mem_cgroup_free(memcg); return ERR_PTR(-ENOMEM); }