Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1365310

[PATCH 01/11] mm/slab: hold a slab_mutex when calling __kmem_cache_shrink()

Path csiph.com!aioe.org!bofh.it!news.nic.it!robomod
From js1304@gmail.com
Newsgroups linux.kernel
Subject [PATCH 01/11] mm/slab: hold a slab_mutex when calling __kmem_cache_shrink()
Date Mon, 28 Mar 2016 07:30:01 +0200
Message-ID <rhxUt-2rs-1@gated-at.bofh.it> (permalink)
References <rhxUt-2rs-3@gated-at.bofh.it>
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wvTqOR2eel/7h6W1pRZE8apJamWf3NnxyTre3kYyptw=; b=h5H/p6zPwFipzCqYLKGQvNVq8kmAfsVkOE1mCaC+45dglf+IW9ueqV6/what1XLSvS TLv/s4UUw2c5sG6w7IvmOYa3P9Sk421dfEBNXaPxhyZHtNM0CqdV2RCXyOD9dc932vvz Rct2sGEXfRvnHa2kWcexJh6n2lAMSX+aP34gQ/JixpVSeeTnCdHMR0vuUdTsRrOenJst Bf+icZiHUvohp1+JerTswrpSmB/xzQKdTb4Bp8svQ8zIc8bFGox0I2zLvRJg1wl3XQZT eRg4Rmvq5vuOkjGhnBwSDNeY3oAt/QZMudvzgZ6CwSxT7gbWfw5do+MmL3fk8DJkxngS Fbyw==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wvTqOR2eel/7h6W1pRZE8apJamWf3NnxyTre3kYyptw=; b=X+UdOkM1a0t8bxTqyC3ZZYa8Jd+lXgT+Eakd1hgrtIKCl8biXKXU9wSsecP2Z35qtA LIRXdKUsXQ8LcEtK3ZWp3dOkyVVx6o9pLk4cikhDtn65lC828gRb1o+yKpupFejTzGKf M9Fp4tUTWX/Jb+2oBFGyBZAvtyVVFbGoeB6+4CaomWfPF022+ny11292LVE02MK/uTkh wMdYI3Qy9/cTamORIR/et99Bii5fOZYJqvdAxxTMVAD8VC6zT30X1HCbcBApYnXlUtAB mYK4hlmCwly11teBS89nrx/nBLqe7LMxgVrCWGe7zZ0T/mO3NUqd0aYhNKwXeAtnjr/X qcyw==
X-Gm-Message-State AD7BkJI1I4P7ur3Obx3UOYlxh0QgCU1U0PZAyfHvC7StK6QP9rNP+1VtIxyMt2d6mrenxQ==
X-Received by 10.98.72.218 with SMTP id q87mr39994468pfi.117.1459142835224; Sun, 27 Mar 2016 22:27:15 -0700 (PDT)
X-Google-Original-From iamjoonsoo.kim@lge.com
X-Mailer git-send-email 1.9.1
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 59
Organization linux.* mail to news gateway
X-Original-Cc Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Jesper Dangaard Brouer <brouer@redhat.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Joonsoo Kim <iamjoonsoo.kim@lge.com>
X-Original-Date Mon, 28 Mar 2016 14:26:51 +0900
X-Original-Message-ID <1459142821-20303-2-git-send-email-iamjoonsoo.kim@lge.com>
X-Original-References <1459142821-20303-1-git-send-email-iamjoonsoo.kim@lge.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1365310

Show key headers only | View raw


From: Joonsoo Kim <iamjoonsoo.kim@lge.com>

Major kmem_cache metadata in slab subsystem is synchronized with
the slab_mutex. In SLAB, if some of them is changed, node's shared
array cache would be freed and re-populated. If __kmem_cache_shrink()
is called at the same time, it will call drain_array() with n->shared
without holding node lock so problem can happen.

We can fix this small theoretical race condition by holding node lock
in drain_array(), but, holding a slab_mutex in kmem_cache_shrink()
looks more appropriate solution because stable state would make things
less error-prone and this is not performance critical path.

In addtion, annotate on SLAB functions.

Signed-off-by: Joonsoo Kim <iamjoonsoo.kim@lge.com>
---
 mm/slab.c        | 2 ++
 mm/slab_common.c | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/mm/slab.c b/mm/slab.c
index a53a0f6..043606a 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -2218,6 +2218,7 @@ static void do_drain(void *arg)
 	ac->avail = 0;
 }
 
+/* Should be called with slab_mutex to prevent from freeing shared array */
 static void drain_cpu_caches(struct kmem_cache *cachep)
 {
 	struct kmem_cache_node *n;
@@ -3871,6 +3872,7 @@ skip_setup:
  * Drain an array if it contains any elements taking the node lock only if
  * necessary. Note that the node listlock also protects the array_cache
  * if drain_array() is used on the shared array.
+ * Should be called with slab_mutex to prevent from freeing shared array.
  */
 static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
 			 struct array_cache *ac, int force, int node)
diff --git a/mm/slab_common.c b/mm/slab_common.c
index a65dad7..5bed565 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -755,7 +755,11 @@ int kmem_cache_shrink(struct kmem_cache *cachep)
 	get_online_cpus();
 	get_online_mems();
 	kasan_cache_shrink(cachep);
+
+	mutex_lock(&slab_mutex);
 	ret = __kmem_cache_shrink(cachep, false);
+	mutex_unlock(&slab_mutex);
+
 	put_online_mems();
 	put_online_cpus();
 	return ret;
-- 
1.9.1

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH 01/11] mm/slab: hold a slab_mutex when calling __kmem_cache_shrink() js1304@gmail.com - 2016-03-28 07:30 +0200
  Re: [PATCH 01/11] mm/slab: hold a slab_mutex when calling  __kmem_cache_shrink() Christoph Lameter <cl@linux.com> - 2016-03-29 03:00 +0200
    Re: [PATCH 01/11] mm/slab: hold a slab_mutex when calling  __kmem_cache_shrink() Joonsoo Kim <iamjoonsoo.kim@lge.com> - 2016-03-30 10:20 +0200
  Re: [PATCH 01/11] mm/slab: hold a slab_mutex when calling  __kmem_cache_shrink() Nikolay Borisov <kernel@kyup.com> - 2016-03-31 13:00 +0200
    Re: [PATCH 01/11] mm/slab: hold a slab_mutex when calling  __kmem_cache_shrink() Joonsoo Kim <iamjoonsoo.kim@lge.com> - 2016-04-01 04:20 +0200

csiph-web