From patchwork Wed Jan 2 21:06:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 10746687 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E3C5691E for ; Wed, 2 Jan 2019 21:07:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D2CC827F81 for ; Wed, 2 Jan 2019 21:07:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C3A3028334; Wed, 2 Jan 2019 21:07:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 62EED27F81 for ; Wed, 2 Jan 2019 21:07:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728324AbfABVHG (ORCPT ); Wed, 2 Jan 2019 16:07:06 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:44068 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727139AbfABVHG (ORCPT ); Wed, 2 Jan 2019 16:07:06 -0500 Received: by mail-pf1-f195.google.com with SMTP id u6so15679015pfh.11; Wed, 02 Jan 2019 13:07:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cSmS7eJBfjXNLPRvyNeOJYXNbNeoNdUM0nWaIDy4Ibw=; b=C+vEHrhYuPG12OzL+mDELEPQMHPT1C/3slsxBSCZfAzhC9Cr5ltvan/qlBmB3D3rXl N1wn9axhCejAotUp989UobvsvRlHaSKgxA/4X1jvKwl7mPNZJDQccnuAMaWbVYLDnhe3 S16T/raq/50kqbMJ/h3hlCBOPAht3coth9+Jb3jlzwQ9JcZv43oDIQpPfP7d9fSR4FZM mldH1RimXJ4xF/8hK9xkzUd7zYafzEK/GwT9z1lMLTerIxTJdDOUHqTrGR3eeAI4zGl4 t+uUdvdi8GbkUgY8iwM6+Kn5gGPLY/pa1ZnZwFnO56byjc7gNs7d46GxSC1OW7bZ1ALz /WoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=cSmS7eJBfjXNLPRvyNeOJYXNbNeoNdUM0nWaIDy4Ibw=; b=R0aLe+vNzWaX6gVkHIp2xda+Jo3lVYAW985SrJfGYIj2kO9JxN1N6lPgqXt+fa9iwI SO9WIpvOwqG2uTT1WTqCRIGvGtsAPqID0RK8qSITL3zDdDaarGiKzFj2wlBvsW0rDx5d zk/seQUuYYUrLVoo2hV9fDuJsPVOkR0jqW7TdAXj5T94myi8KxVxxe/KfjQCSDdLIVZB 4w3LM7ZIQR42qDA7S7wDrrWUw6+xGFiU7mPD4lhx2uCMIHDbPgYW6XwPFfOHbqymgV9Z YbneoO6NOJWNRFDu7p4c8uHC2lMdPe8lDMKQqtx+PGAYOPyzAK5zTZ0t54U+YwNpyfFr wYiA== X-Gm-Message-State: AJcUukcO9fOfdCPv3ujzPCuRQctXv+V4tJ/fwJbV9wsVpyGscC4Ziadc Jg5s/iegRocyfFy5MYbKD1C1kFqT X-Google-Smtp-Source: ALg8bN6PDemVzXU0hjF9wMUFWNpYNsIkqNSfwSrNbB+RFghWK84Kfk13kW6S0bsoBfze8m0S2AEfgw== X-Received: by 2002:a63:a064:: with SMTP id u36mr14913654pgn.145.1546463224823; Wed, 02 Jan 2019 13:07:04 -0800 (PST) Received: from localhost.localdomain ([2620:0:1008:fd00:78b4:3a07:980a:287b]) by smtp.gmail.com with ESMTPSA id 22sm67637324pgd.85.2019.01.02.13.07.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 Jan 2019 13:07:03 -0800 (PST) From: Andrei Vagin To: Alexander Viro , David Howells Cc: linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, Andrei Vagin , Li Zefan Subject: [PATCH vfs/for-next v4] cgroup: fix top cgroup refcnt leak Date: Wed, 2 Jan 2019 13:06:59 -0800 Message-Id: <20190102210659.3125-1-avagin@gmail.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190102200223.GI2217@ZenIV.linux.org.uk> References: <20190102200223.GI2217@ZenIV.linux.org.uk> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It looks like the c6b3d5bcd67c ("cgroup: fix top cgroup refcnt leak") commit was reverted by mistake. $ mkdir /tmp/cgroup $ mkdir /tmp/cgroup2 $ mount -t cgroup -o none,name=test test /tmp/cgroup $ mount -t cgroup -o none,name=test test /tmp/cgroup2 $ umount /tmp/cgroup $ umount /tmp/cgroup2 $ cat /proc/self/cgroup | grep test 12:name=test:/ You can see the test cgroup was not freed. Cc: Li Zefan Fixes: aea3f2676c83 ("kernfs, sysfs, cgroup, intel_rdt: Support fs_context") Signed-off-by: Andrei Vagin --- v2: clean up code and add the vfs/for-next tag v3: fix a reference leak when kernfs_node_dentry fails v4: call deactivate_locked_super() in a error case v5: don't dereference fc->root after dput() kernel/cgroup/cgroup.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index fb0717696895..53b730cf1f7b 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -2019,7 +2019,7 @@ int cgroup_do_get_tree(struct fs_context *fc) ret = kernfs_get_tree(fc); if (ret < 0) - goto out_cgrp; + return ret; /* * In non-init cgroup namespace, instead of root cgroup's dentry, @@ -2038,19 +2038,30 @@ int cgroup_do_get_tree(struct fs_context *fc) mutex_unlock(&cgroup_mutex); nsdentry = kernfs_node_dentry(cgrp->kn, fc->root->d_sb); - if (IS_ERR(nsdentry)) - return PTR_ERR(nsdentry); + if (IS_ERR(nsdentry)) { + ret = PTR_ERR(nsdentry); + goto out_cgrp; + } dput(fc->root); fc->root = nsdentry; } ret = 0; - if (ctx->kfc.new_sb_created) - goto out_cgrp; - apply_cgroup_root_flags(ctx->flags); - return 0; + if (!ctx->kfc.new_sb_created) + apply_cgroup_root_flags(ctx->flags); out_cgrp: + if (!ctx->kfc.new_sb_created) + cgroup_put(&ctx->root->cgrp); + + if (unlikely(ret)) { + struct super_block *sb = fc->root->d_sb; + + dput(fc->root); + deactivate_locked_super(sb); + fc->root = NULL; + } + return ret; }