stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Michal Hocko <mhocko@suse.cz>,
	Li Zefan <lizefan@huawei.com>, Jiri Slaby <jslaby@suse.cz>
Subject: [PATCH 3.13 21/22] cgroup: protect modifications to cgroup_idr with cgroup_mutex
Date: Mon, 31 Mar 2014 21:08:51 -0700	[thread overview]
Message-ID: <20140401040708.267451128@linuxfoundation.org> (raw)
In-Reply-To: <20140401040703.045139933@linuxfoundation.org>

3.13-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Li Zefan <lizefan@huawei.com>

commit 0ab02ca8f887908152d1a96db5130fc661d36a1e upstream.

Setup cgroupfs like this:
  # mount -t cgroup -o cpuacct xxx /cgroup
  # mkdir /cgroup/sub1
  # mkdir /cgroup/sub2

Then run these two commands:
  # for ((; ;)) { mkdir /cgroup/sub1/tmp && rmdir /mnt/sub1/tmp; } &
  # for ((; ;)) { mkdir /cgroup/sub2/tmp && rmdir /mnt/sub2/tmp; } &

After seconds you may see this warning:

------------[ cut here ]------------
WARNING: CPU: 1 PID: 25243 at lib/idr.c:527 sub_remove+0x87/0x1b0()
idr_remove called for id=6 which is not allocated.
...
Call Trace:
 [<ffffffff8156063c>] dump_stack+0x7a/0x96
 [<ffffffff810591ac>] warn_slowpath_common+0x8c/0xc0
 [<ffffffff81059296>] warn_slowpath_fmt+0x46/0x50
 [<ffffffff81300aa7>] sub_remove+0x87/0x1b0
 [<ffffffff810f3f02>] ? css_killed_work_fn+0x32/0x1b0
 [<ffffffff81300bf5>] idr_remove+0x25/0xd0
 [<ffffffff810f2bab>] cgroup_destroy_css_killed+0x5b/0xc0
 [<ffffffff810f4000>] css_killed_work_fn+0x130/0x1b0
 [<ffffffff8107cdbc>] process_one_work+0x26c/0x550
 [<ffffffff8107eefe>] worker_thread+0x12e/0x3b0
 [<ffffffff81085f96>] kthread+0xe6/0xf0
 [<ffffffff81570bac>] ret_from_fork+0x7c/0xb0
---[ end trace 2d1577ec10cf80d0 ]---

It's because allocating/removing cgroup ID is not properly synchronized.

The bug was introduced when we converted cgroup_ida to cgroup_idr.
While synchronization is already done inside ida_simple_{get,remove}(),
users are responsible for concurrent calls to idr_{alloc,remove}().

tj: Refreshed on top of b58c89986a77 ("cgroup: fix error return from
cgroup_create()").

[mhocko@suse.cz: ported to 3.12]
Fixes: 4e96ee8e981b ("cgroup: convert cgroup_ida to cgroup_idr")
Cc: <stable@vger.kernel.org> #3.12+
Reported-by: Michal Hocko <mhocko@suse.cz>
Signed-off-by: Li Zefan <lizefan@huawei.com>
Signed-off-by: Michal Hocko <mhocko@suse.cz>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 include/linux/cgroup.h |    2 ++
 kernel/cgroup.c        |   26 +++++++++++++-------------
 2 files changed, 15 insertions(+), 13 deletions(-)

--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -169,6 +169,8 @@ struct cgroup {
 	 *
 	 * The ID of the root cgroup is always 0, and a new cgroup
 	 * will be assigned with a smallest available ID.
+	 *
+	 * Allocating/Removing ID must be protected by cgroup_mutex.
 	 */
 	int id;
 
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -4363,16 +4363,6 @@ static long cgroup_create(struct cgroup
 	rcu_assign_pointer(cgrp->name, name);
 
 	/*
-	 * Temporarily set the pointer to NULL, so idr_find() won't return
-	 * a half-baked cgroup.
-	 */
-	cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
-	if (cgrp->id < 0) {
-		err = -ENOMEM;
-		goto err_free_name;
-	}
-
-	/*
 	 * Only live parents can have children.  Note that the liveliness
 	 * check isn't strictly necessary because cgroup_mkdir() and
 	 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
@@ -4381,7 +4371,7 @@ static long cgroup_create(struct cgroup
 	 */
 	if (!cgroup_lock_live_group(parent)) {
 		err = -ENODEV;
-		goto err_free_id;
+		goto err_free_name;
 	}
 
 	/* Grab a reference on the superblock so the hierarchy doesn't
@@ -4391,6 +4381,16 @@ static long cgroup_create(struct cgroup
 	 * fs */
 	atomic_inc(&sb->s_active);
 
+	/*
+	 * Temporarily set the pointer to NULL, so idr_find() won't return
+	 * a half-baked cgroup.
+	 */
+	cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
+	if (cgrp->id < 0) {
+		err = -ENOMEM;
+		goto err_unlock;
+ 	}
+
 	init_cgroup_housekeeping(cgrp);
 
 	dentry->d_fsdata = cgrp;
@@ -4491,11 +4491,11 @@ err_free_all:
 			ss->css_free(css);
 		}
 	}
+	idr_remove(&root->cgroup_idr, cgrp->id);
+err_unlock:
 	mutex_unlock(&cgroup_mutex);
 	/* Release the reference count that we took on the superblock */
 	deactivate_super(sb);
-err_free_id:
-	idr_remove(&root->cgroup_idr, cgrp->id);
 err_free_name:
 	kfree(rcu_dereference_raw(cgrp->name));
 err_free_cgrp:



  parent reply	other threads:[~2014-04-01  4:08 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-01  4:08 [PATCH 3.13 00/22] 3.13.9-stable review Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 01/22] ext4: atomically set inode->i_flags in ext4_set_inode_flags() Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 02/22] rcuwalk: recheck mount_lock after mountpoint crossing attempts Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 03/22] Input: mousedev - fix race when creating mixed device Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 04/22] Input: synaptics - add manual min/max quirk Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 05/22] Input: synaptics - add manual min/max quirk for ThinkPad X240 Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 06/22] Input: cypress_ps2 - dont report as a button pads Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 07/22] xen/balloon: flush persistent kmaps in correct position Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 08/22] Revert "xen: properly account for _PAGE_NUMA during xen pte translations" Greg Kroah-Hartman
2014-04-10 10:05   ` Steven Noonan
2014-04-10 10:08     ` David Vrabel
2014-04-01  4:08 ` [PATCH 3.13 09/22] i2c: cpm: Fix build by adding of_address.h and of_irq.h Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 10/22] drm/i915: Undo gtt scratch pte unmapping again Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 11/22] x86: fix boot on uniprocessor systems Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 12/22] make prepend_name() work correctly when called with negative *buflen Greg Kroah-Hartman
2014-04-09 20:17   ` Sasha Levin
2014-04-01  4:08 ` [PATCH 3.13 13/22] net: mvneta: rename MVNETA_GMAC2_PSC_ENABLE to MVNETA_GMAC2_PCS_ENABLE Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 14/22] net: mvneta: fix usage as a module on RGMII configurations Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 15/22] random32: avoid attempt to late reseed if in the middle of seeding Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 16/22] resizable namespace.c hashes Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 17/22] keep shadowed vfsmounts together Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 18/22] dont bother with propagate_mnt() unless the target is shared Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 19/22] switch mnt_hash to hlist Greg Kroah-Hartman
2014-04-01  4:08 ` [PATCH 3.13 20/22] mm: close PageTail race Greg Kroah-Hartman
2014-04-01  4:08 ` Greg Kroah-Hartman [this message]
2014-04-01  4:08 ` [PATCH 3.13 22/22] netfilter: nf_conntrack_dccp: fix skb_header_pointer API usages Greg Kroah-Hartman
2014-04-02  0:03 ` [PATCH 3.13 00/22] 3.13.9-stable review Guenter Roeck
2014-04-03 22:45   ` Greg Kroah-Hartman
2014-04-04 13:30     ` Shuah Khan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140401040708.267451128@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=mhocko@suse.cz \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).