Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp1310328imm; Wed, 20 Jun 2018 15:43:02 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI3G3Z/RvudrRHX475gRKtTwbj9Xj08RQxwbOhia1jWcAlEn3PdY61ZRDvC0KLaDi7ICTyK X-Received: by 2002:a17:902:301:: with SMTP id 1-v6mr25568680pld.127.1529534582585; Wed, 20 Jun 2018 15:43:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529534582; cv=none; d=google.com; s=arc-20160816; b=mpOLIEfBFu644e417qraJV2TzKhtzWSHxXrgxBRWEenTV6vc+3gOLc/sCu0EviGfD2 3u1ZkDLx2XWteYzddDP7Bd6X680ZCEb2bp1bbc8DddUUatVD4woNKBQdR+rVmOSnNU86 6eVnS8hvRu3J/WfuWcato8Y3hgVq/O/XGXhR3N1VY2MND9w745oiu36Kz3FBdcM1X5Hs FfD3TccsItJqd414RvhZymr6YYOe8bj6FLcr1wC+cmDlALCb5y80UiiQ0qSlaZun5Mi0 i4rJqR8RKa1lPhsR25XHTzYSbz3Ml3BpAtOQJZodOy9bpa+67HeKqmrWlAFZXpWFkpzm DUrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=QDrgQcbxtpv9rkSNMuo6ul0hIWy399jGiJTcOQHJxbw=; b=K/JNQVQVpHLffw3fbUx0o5bQi3a8oqkHCeUSVZluQ413DucmRS5I1j/kl1okClQXMY hv/0/XtdR2a7g/Ftc+7NaQ6zqzNDjJ+f4osJsBZCBdhvjVzsuHH2Q+VhTWdhx8GHw+fI jV7dBgcSg06z7N7ckvcNKCpLSHfRGO7EMB6FxYlnLxtgzC5eXX+YHPoTlawvQEEiOBRP rSbKBo0b/qIXpATtFnY7aOJ7MWKdhQdTcp+gQtGTUvVo4vORg4+9utJ733ajIA9vt+cP acvQ41Jmjv9n5KAH9MQWWpT9yX4vDIxq59JMRB8wfkDR8rjgVFFUwFqr25CIOCzzJMtg WwMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=rKhHbpQ7; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f4-v6si2627610pgs.244.2018.06.20.15.42.48; Wed, 20 Jun 2018 15:43:02 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=rKhHbpQ7; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933821AbeFTWmF (ORCPT + 99 others); Wed, 20 Jun 2018 18:42:05 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:46950 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933803AbeFTWmD (ORCPT ); Wed, 20 Jun 2018 18:42:03 -0400 Received: by mail-pf0-f193.google.com with SMTP id q1-v6so481501pff.13 for ; Wed, 20 Jun 2018 15:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=QDrgQcbxtpv9rkSNMuo6ul0hIWy399jGiJTcOQHJxbw=; b=rKhHbpQ7e0xS+1n0GX57b09+YI8KeexFjVgkDPz39MEiOekRebzHWY4bIc7bsJ4HGg Ydkl2ct+4Zn5HEpHKJqwdtZO7+GJWBGiarFw90EK5d+LqPzb55KIAW6zs/P+vF0TuKhK 8X5WU7TvtEIPs1m71nAs/JQjw0ui9aCtKFcQMdvz39+hvazGuLdCppmJ1WW09sA7VYwg aO4cHyGKyhYH72iGX65FRxm+N7ydf5OzfExGEKYSF/ucYV+RIuQBNYKEPIWaGIAAW1kg RJPabBYEPTuzjXyN+gNrmDw1g4G69NEg0Tyyw1IAopZWHjSnuNHd6h1edNyAS6G3Qam1 vDLQ== 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; bh=QDrgQcbxtpv9rkSNMuo6ul0hIWy399jGiJTcOQHJxbw=; b=Pt82Ja0FaCVNVCL9zZzXGLchWdkL/h2rJC8zyhgvgeOew57KVBKX3woQNKpjf1PvcP S8mAken3RoTDRu0jT5sR2zi9bwDoV/eWb2GCwA0Bp9zo42g58tW5upNknRkXcHsh0t4U /7VvxDIp/JPVEHYpJ4fgd6CMjytdT5f7YsJCzP6vrnR9RGN34TeU8cyc+rIIQA0iSdwR dKZtoP4OXWim4mq0/R0On1+47RkwHh9YBeDxu2fkR8iiu/xG62ZFfprHbm9jVN99IspX mFauLpqCpEEKmLCSvYPBtHMnCJJFcMYoMU3+0vJl2sjE8EOWC8Wn487z9si3VibByRLB wrRA== X-Gm-Message-State: APt69E1hc4/D7bhcpDnHOA41lct5iLY30YKcFtTdzbgPO03+LhvCJDoF A1I7TPfagwWEgGgbyU+Ael8ZNA== X-Received: by 2002:a62:4b16:: with SMTP id y22-v6mr24722884pfa.214.1529534522566; Wed, 20 Jun 2018 15:42:02 -0700 (PDT) Received: from shakeelb.mtv.corp.google.com ([2620:15c:2cb:201:3a5f:3a4f:fa44:6b63]) by smtp.gmail.com with ESMTPSA id e16-v6sm4713236pfn.46.2018.06.20.15.42.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Jun 2018 15:42:01 -0700 (PDT) From: Shakeel Butt To: "Jason A . Donenfeld" Cc: David Rientjes , Christoph Lameter , Pekka Enberg , Joonsoo Kim , Andrew Morton , Andrey Ryabinin , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt , stable@vger.kernel.org Subject: [PATCH] slub: track number of slabs irrespective of CONFIG_SLUB_DEBUG Date: Wed, 20 Jun 2018 15:41:47 -0700 Message-Id: <20180620224147.23777-1-shakeelb@google.com> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For !CONFIG_SLUB_DEBUG, SLUB does not maintain the number of slabs allocated per node for a kmem_cache. Thus, slabs_node() in __kmem_cache_empty(), __kmem_cache_shrink() and __kmem_cache_destroy() will always return 0 for such config. This is wrong and can cause issues for all users of these functions. Infact in [1] Jason has reported a system crash while using SLUB without CONFIG_SLUB_DEBUG. The reason was the usage of slabs_node() by __kmem_cache_empty(). The right solution is to make slabs_node() work even for !CONFIG_SLUB_DEBUG. The commit 0f389ec63077 ("slub: No need for per node slab counters if !SLUB_DEBUG") had put the per node slab counter under CONFIG_SLUB_DEBUG because it was only read through sysfs API and the sysfs API was disabled on !CONFIG_SLUB_DEBUG. However the users of the per node slab counter assumed that it will work in the absence of CONFIG_SLUB_DEBUG. So, make the counter work for !CONFIG_SLUB_DEBUG. Please note that commit f9e13c0a5a33 ("slab, slub: skip unnecessary kasan_cache_shutdown()") exposed this issue but it is present even before. [1] http://lkml.kernel.org/r/CAHmME9rtoPwxUSnktxzKso14iuVCWT7BE_-_8PAC=pGw1iJnQg@mail.gmail.com Fixes: f9e13c0a5a33 ("slab, slub: skip unnecessary kasan_cache_shutdown()") Signed-off-by: Shakeel Butt Suggested-by: David Rientjes Reported-by: Jason A . Donenfeld Cc: Christoph Lameter Cc: Pekka Enberg Cc: Joonsoo Kim Cc: Andrew Morton Cc: Andrey Ryabinin Cc: Cc: Cc: --- mm/slab.h | 2 +- mm/slub.c | 80 +++++++++++++++++++++++++------------------------------ 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index 68bdf498da3b..a6545332cc86 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -473,8 +473,8 @@ struct kmem_cache_node { #ifdef CONFIG_SLUB unsigned long nr_partial; struct list_head partial; -#ifdef CONFIG_SLUB_DEBUG atomic_long_t nr_slabs; +#ifdef CONFIG_SLUB_DEBUG atomic_long_t total_objects; struct list_head full; #endif diff --git a/mm/slub.c b/mm/slub.c index a3b8467c14af..c9c190d54687 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1030,42 +1030,6 @@ static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct list_del(&page->lru); } -/* Tracking of the number of slabs for debugging purposes */ -static inline unsigned long slabs_node(struct kmem_cache *s, int node) -{ - struct kmem_cache_node *n = get_node(s, node); - - return atomic_long_read(&n->nr_slabs); -} - -static inline unsigned long node_nr_slabs(struct kmem_cache_node *n) -{ - return atomic_long_read(&n->nr_slabs); -} - -static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects) -{ - struct kmem_cache_node *n = get_node(s, node); - - /* - * May be called early in order to allocate a slab for the - * kmem_cache_node structure. Solve the chicken-egg - * dilemma by deferring the increment of the count during - * bootstrap (see early_kmem_cache_node_alloc). - */ - if (likely(n)) { - atomic_long_inc(&n->nr_slabs); - atomic_long_add(objects, &n->total_objects); - } -} -static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects) -{ - struct kmem_cache_node *n = get_node(s, node); - - atomic_long_dec(&n->nr_slabs); - atomic_long_sub(objects, &n->total_objects); -} - /* Object debug checks for alloc/free paths */ static void setup_object_debug(struct kmem_cache *s, struct page *page, void *object) @@ -1321,16 +1285,46 @@ slab_flags_t kmem_cache_flags(unsigned int object_size, #define disable_higher_order_debug 0 +#endif /* CONFIG_SLUB_DEBUG */ + static inline unsigned long slabs_node(struct kmem_cache *s, int node) - { return 0; } +{ + struct kmem_cache_node *n = get_node(s, node); + + return atomic_long_read(&n->nr_slabs); +} + static inline unsigned long node_nr_slabs(struct kmem_cache_node *n) - { return 0; } -static inline void inc_slabs_node(struct kmem_cache *s, int node, - int objects) {} -static inline void dec_slabs_node(struct kmem_cache *s, int node, - int objects) {} +{ + return atomic_long_read(&n->nr_slabs); +} -#endif /* CONFIG_SLUB_DEBUG */ +static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects) +{ + struct kmem_cache_node *n = get_node(s, node); + + /* + * May be called early in order to allocate a slab for the + * kmem_cache_node structure. Solve the chicken-egg + * dilemma by deferring the increment of the count during + * bootstrap (see early_kmem_cache_node_alloc). + */ + if (likely(n)) { + atomic_long_inc(&n->nr_slabs); +#ifdef CONFIG_SLUB_DEBUG + atomic_long_add(objects, &n->total_objects); +#endif + } +} +static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects) +{ + struct kmem_cache_node *n = get_node(s, node); + + atomic_long_dec(&n->nr_slabs); +#ifdef CONFIG_SLUB_DEBUG + atomic_long_sub(objects, &n->total_objects); +#endif +} /* * Hooks for other subsystems that check memory allocations. In a typical -- 2.18.0.rc1.244.gcf134e6275-goog