Received: by 10.213.65.68 with SMTP id h4csp47423imn; Tue, 27 Mar 2018 16:07:28 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/mp/c6EYaucj9ZjqN+XVRLeBHJX8HcMbZZIEKaqJFjH25G43iDgj88dpp8La3AgDolldoE X-Received: by 10.99.149.83 with SMTP id t19mr797545pgn.433.1522192048802; Tue, 27 Mar 2018 16:07:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522192048; cv=none; d=google.com; s=arc-20160816; b=Vp2hr1ztT1/lN6dUIOddutOfTOLxqvFNPQ/yJuwZT9llYpXauhl9PkRukG37Jf3/LD a2veTaf2ehXy1WpNdjZTGpiJmuWzHoFttU+hdT60U0cLlQ2Nc5Jx5vvuOJf566jk+n7d YajpLsdzQMrF/dGsJ+gJ8CYFyzZhFxtZVBvd74kCBd9CaAGlUWapaWnKj8mCXMK/Jn/m EOKnzxHY0n6C1ZlwJUQngZFtmd9UroxI6RXhUYgAVfq7Wi/fmElOKXq5LVhvK/2jV/2a DJ2ZEoat2TbEaOnD5UEu7Ps+TKlY+m8YAVniTXz5cANEK8UByUuT6giTEx7bcOHg+wHl bzMw== 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=Tscfg1TJJV81qrwaq8TgYuKBM2M1q93MSEBcsi8Kk28=; b=gT78zBAGak1Ed0zo8NrOcVyrUlu6h1NgaayM47+59RcddjVPA9RKP/Q1cFWdvbND8J ynljd3Q7DDijNh7oyNIgMRmF4WdPBUhKLtwhkTX82Vmc3XzYPJPjXsNnKKd7U9GkP5gz h5f9jP6LkouvmAMtZCWEfPi9cltOP4iT4TXELVEPyunxPlCcjoweHnWbtLJXOr3VYTbV UbGa8LzKSsMb7sM3SxVjfqm8iaLFBKmIGue7lZ//PlYqxbOQLlOjHW+xvWou2bAforvN 6KLXUubROL8Vkz9enuFJ7mNZmHHOjGgsiSpg84RglLz+vuml4BKNBn4WECW4ZqRKaMJf CJfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dI3CYCOY; 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 i12-v6si2194023plk.508.2018.03.27.16.07.13; Tue, 27 Mar 2018 16:07:28 -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=dI3CYCOY; 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 S1752384AbeC0XGM (ORCPT + 99 others); Tue, 27 Mar 2018 19:06:12 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:41245 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752123AbeC0XGK (ORCPT ); Tue, 27 Mar 2018 19:06:10 -0400 Received: by mail-pl0-f65.google.com with SMTP id b7-v6so371486plr.8 for ; Tue, 27 Mar 2018 16:06:10 -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=Tscfg1TJJV81qrwaq8TgYuKBM2M1q93MSEBcsi8Kk28=; b=dI3CYCOYlwoxpFXymqnGHpu//6EIpKO/x3MPF3/V7DNiYXWv1IfaMphbR2088a95aA 7EtyGRO7ffEf2kyYaUjVQtBPfhVBsJi4IEuNiFkZXrPTqlqPY8CLKZvEwMXPPXlSbfmU hj5GXDp4Ov6JEe+N5/kkR5hdq/qI3ffPhzIKJam2oblEcFQj3K9ZDSjxiISqc/E55I83 eicHnW4QqyhIICQaGGlIdHH+XePcE1eihgyxREWzgVGgB6JQ9i/O1GdY2eJRot45wOU5 k9CYPwKxoEof+fuExsHQ/P3wtnUX+ij/MK1CwimiZN0VfdQJhY8NF0XygkYgiDjDcXwU jnYg== 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=Tscfg1TJJV81qrwaq8TgYuKBM2M1q93MSEBcsi8Kk28=; b=LN2hPlPQ0bSmE2Le39yjvQQPQoTHikCIj3FG6nRS/CAMuNuhX1jAbrdtRv4JRK4H5b g0V5yIQy7gpF1tg+R6aOa/Nq9fFMZvdlAoMaIAaWfA6SL5scsedOvrX+sI+NatpnC/kq inq5GavEHsuAfm1iLLB7ACiFDceyWR0ag4fejU287zOnc1U2amiVyGApYnDjdVViQI7K tdAB9FSq6F1YRc65+V+dQuggZWaC0TWiPpz5PUHFAUCJo+VBXwP/gYP+z07ds5FvtUxs emgj4JekQ0Nf3ZBRgA8KXDwLgpNp4CG/AM+mWl+7bYz9jpaVjxBdzyMiWqenF2sRAyuk WjBA== X-Gm-Message-State: AElRT7G+3VCJlA+kJ76XvTjgvd79irYDdgLfnWamZy9EnC2wZsgSXc+b 6FaHPeaTn2OV4/W9lkQRDSGUPw== X-Received: by 2002:a17:902:24c7:: with SMTP id l7-v6mr1203000plg.320.1522191969854; Tue, 27 Mar 2018 16:06:09 -0700 (PDT) Received: from shakeelb.mtv.corp.google.com ([2620:15c:2cb:201:3a5f:3a4f:fa44:6b63]) by smtp.gmail.com with ESMTPSA id y15sm4483221pfb.37.2018.03.27.16.06.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Mar 2018 16:06:08 -0700 (PDT) From: Shakeel Butt To: Andrey Ryabinin , Vladimir Davydov , Alexander Potapenko , Greg Thelen , Dmitry Vyukov , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Subject: [PATCH] slab, slub: skip unnecessary kasan_cache_shutdown() Date: Tue, 27 Mar 2018 16:06:03 -0700 Message-Id: <20180327230603.54721-1-shakeelb@google.com> X-Mailer: git-send-email 2.17.0.rc1.321.gba9d0f2565-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The kasan quarantine is designed to delay freeing slab objects to catch use-after-free. The quarantine can be large (several percent of machine memory size). When kmem_caches are deleted related objects are flushed from the quarantine but this requires scanning the entire quarantine which can be very slow. We have seen the kernel busily working on this while holding slab_mutex and badly affecting cache_reaper, slabinfo readers and memcg kmem cache creations. It can easily reproduced by following script: yes . | head -1000000 | xargs stat > /dev/null for i in `seq 1 10`; do seq 500 | (cd /cg/memory && xargs mkdir) seq 500 | xargs -I{} sh -c 'echo $BASHPID > \ /cg/memory/{}/tasks && exec stat .' > /dev/null seq 500 | (cd /cg/memory && xargs rmdir) done The busy stack: kasan_cache_shutdown shutdown_cache memcg_destroy_kmem_caches mem_cgroup_css_free css_free_rwork_fn process_one_work worker_thread kthread ret_from_fork This patch is based on the observation that if the kmem_cache to be destroyed is empty then there should not be any objects of this cache in the quarantine. Without the patch the script got stuck for couple of hours. With the patch the script completed within a second. Signed-off-by: Shakeel Butt --- mm/kasan/kasan.c | 3 ++- mm/slab.c | 12 ++++++++++++ mm/slab.h | 1 + mm/slub.c | 11 +++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index 49fffb0ca83b..135ce2838c89 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -382,7 +382,8 @@ void kasan_cache_shrink(struct kmem_cache *cache) void kasan_cache_shutdown(struct kmem_cache *cache) { - quarantine_remove_cache(cache); + if (!__kmem_cache_empty(cache)) + quarantine_remove_cache(cache); } size_t kasan_metadata_size(struct kmem_cache *cache) diff --git a/mm/slab.c b/mm/slab.c index 9212c64bb705..b59f2cdf28d1 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -2291,6 +2291,18 @@ static int drain_freelist(struct kmem_cache *cache, return nr_freed; } +bool __kmem_cache_empty(struct kmem_cache *s) +{ + int node; + struct kmem_cache_node *n; + + for_each_kmem_cache_node(s, node, n) + if (!list_empty(&n->slabs_full) || + !list_empty(&n->slabs_partial)) + return false; + return true; +} + int __kmem_cache_shrink(struct kmem_cache *cachep) { int ret = 0; diff --git a/mm/slab.h b/mm/slab.h index e8981e811c45..68bdf498da3b 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -166,6 +166,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size, SLAB_TEMPORARY | \ SLAB_ACCOUNT) +bool __kmem_cache_empty(struct kmem_cache *); int __kmem_cache_shutdown(struct kmem_cache *); void __kmem_cache_release(struct kmem_cache *); int __kmem_cache_shrink(struct kmem_cache *); diff --git a/mm/slub.c b/mm/slub.c index 1edc8d97c862..44aa7847324a 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3707,6 +3707,17 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n) discard_slab(s, page); } +bool __kmem_cache_empty(struct kmem_cache *s) +{ + int node; + struct kmem_cache_node *n; + + for_each_kmem_cache_node(s, node, n) + if (n->nr_partial || slabs_node(s, node)) + return false; + return true; +} + /* * Release all resources used by a slab cache. */ -- 2.17.0.rc1.321.gba9d0f2565-goog