From patchwork Mon May 18 17:35:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 225798 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=-6.8 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 DD5A2C433DF for ; Mon, 18 May 2020 17:54:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B623E2086A for ; Mon, 18 May 2020 17:54:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589824499; bh=eMgKjlrSqZbzw2cAvXs6l4AlTWhoYN7FjTDp/GmESNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=Yt4WX16I+h9/KPYUb/4EbzIkb7hVna3SK4AIiheLZ1HsPvhCzx4E4e+NLqrsceYaH yxRd/hNJeuCJvfZdsqu7E7tKLlYX7GyRuCks10Gs7XQ3fEWhxVHfn6VTZHYCWwNH4s UPD48GxcOb3lJhNMwkLJHUy75HZ0SuZ29xARVpy4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730437AbgERRyz (ORCPT ); Mon, 18 May 2020 13:54:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:59928 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731372AbgERRyx (ORCPT ); Mon, 18 May 2020 13:54:53 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 2A584207C4; Mon, 18 May 2020 17:54:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589824492; bh=eMgKjlrSqZbzw2cAvXs6l4AlTWhoYN7FjTDp/GmESNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IFWI3Kx8vzckXE+npCRIPv2PD30ws6bagcJY766/GxcQlcXojvJLBKotyC88wTgVl bIVmgKD59AFFip1myjEZ38Sa/ZF+v9vE3tpaHCaJWoF8jFDgHQjQv8Xl5VawWP+reh S4LitunQiCOX7I10YABktS6Hev0p+Mb0ordaYpQw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yang Yingliang , Zefan Li , Tejun Heo , Jakub Kicinski Subject: [PATCH 5.4 032/147] netprio_cgroup: Fix unlimited memory leak of v2 cgroups Date: Mon, 18 May 2020 19:35:55 +0200 Message-Id: <20200518173518.206897083@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518173513.009514388@linuxfoundation.org> References: <20200518173513.009514388@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Zefan Li [ Upstream commit 090e28b229af92dc5b40786ca673999d59e73056 ] If systemd is configured to use hybrid mode which enables the use of both cgroup v1 and v2, systemd will create new cgroup on both the default root (v2) and netprio_cgroup hierarchy (v1) for a new session and attach task to the two cgroups. If the task does some network thing then the v2 cgroup can never be freed after the session exited. One of our machines ran into OOM due to this memory leak. In the scenario described above when sk_alloc() is called cgroup_sk_alloc() thought it's in v2 mode, so it stores the cgroup pointer in sk->sk_cgrp_data and increments the cgroup refcnt, but then sock_update_netprioidx() thought it's in v1 mode, so it stores netprioidx value in sk->sk_cgrp_data, so the cgroup refcnt will never be freed. Currently we do the mode switch when someone writes to the ifpriomap cgroup control file. The easiest fix is to also do the switch when a task is attached to a new cgroup. Fixes: bd1060a1d671 ("sock, cgroup: add sock->sk_cgroup") Reported-by: Yang Yingliang Tested-by: Yang Yingliang Signed-off-by: Zefan Li Acked-by: Tejun Heo Signed-off-by: Jakub Kicinski Signed-off-by: Greg Kroah-Hartman --- net/core/netprio_cgroup.c | 2 ++ 1 file changed, 2 insertions(+) --- a/net/core/netprio_cgroup.c +++ b/net/core/netprio_cgroup.c @@ -236,6 +236,8 @@ static void net_prio_attach(struct cgrou struct task_struct *p; struct cgroup_subsys_state *css; + cgroup_sk_alloc_disable(); + cgroup_taskset_for_each(p, css, tset) { void *v = (void *)(unsigned long)css->cgroup->id;