From patchwork Mon Apr 27 23:56:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 11513401 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 26F5C15AB for ; Mon, 27 Apr 2020 23:56:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DE4AE2076A for ; Mon, 27 Apr 2020 23:56:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Bx3jM5Mc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DE4AE2076A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C81278E0001; Mon, 27 Apr 2020 19:56:52 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C2E5F8E0009; Mon, 27 Apr 2020 19:56:52 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A30358E0001; Mon, 27 Apr 2020 19:56:52 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0251.hostedemail.com [216.40.44.251]) by kanga.kvack.org (Postfix) with ESMTP id 7E8CA8E0007 for ; Mon, 27 Apr 2020 19:56:52 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 3C90152B7 for ; Mon, 27 Apr 2020 23:56:52 +0000 (UTC) X-FDA: 76755297864.23.clam51_762610d9a1d31 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,longman@redhat.com,,RULES_HIT:30054,0,RBL:205.139.110.120:@redhat.com:.lbl8.mailshell.net-66.10.201.10 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: clam51_762610d9a1d31 X-Filterd-Recvd-Size: 5735 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by imf17.hostedemail.com (Postfix) with ESMTP for ; Mon, 27 Apr 2020 23:56:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588031811; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=qCIbPd4z+LsfKk+IqgZJljs6Wparbj+tB3otWihkQ10=; b=Bx3jM5Mcv+ejkUcUJsMHp79kfoeQ1CXgBM2qcLexgKkcbfv0crGVy2cIzoYIv9XjS7PV7X 11Bq/FxJpXNOhv6UsiY53pOrChnuycYdYdaXvxluVITKJ5nSNJaHpPkZu8/f+uh9XfaITA HPhXhABHKa0paY6B7YFgFVsdq9FelbM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-116-t_m2IxUVOyyRbN5EYA7WrQ-1; Mon, 27 Apr 2020 19:56:49 -0400 X-MC-Unique: t_m2IxUVOyyRbN5EYA7WrQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E899C180F12C; Mon, 27 Apr 2020 23:56:47 +0000 (UTC) Received: from llong.com (ovpn-112-176.rdu2.redhat.com [10.10.112.176]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A89D60C81; Mon, 27 Apr 2020 23:56:46 +0000 (UTC) From: Waiman Long To: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Johannes Weiner , Michal Hocko , Vladimir Davydov Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Juri Lelli , Qian Cai , Waiman Long Subject: [PATCH v2 4/4] mm/slub: Fix sysfs shrink circular locking dependency Date: Mon, 27 Apr 2020 19:56:21 -0400 Message-Id: <20200427235621.7823-5-longman@redhat.com> In-Reply-To: <20200427235621.7823-1-longman@redhat.com> References: <20200427235621.7823-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: A lockdep splat is observed by echoing "1" to the shrink sysfs file and then shutting down the system: [ 167.473392] Chain exists of: [ 167.473392] kn->count#279 --> mem_hotplug_lock.rw_sem --> slab_mutex [ 167.473392] [ 167.484323] Possible unsafe locking scenario: [ 167.484323] [ 167.490273] CPU0 CPU1 [ 167.494825] ---- ---- [ 167.499376] lock(slab_mutex); [ 167.502530] lock(mem_hotplug_lock.rw_sem); [ 167.509356] lock(slab_mutex); [ 167.515044] lock(kn->count#279); [ 167.518462] [ 167.518462] *** DEADLOCK *** It is because of the get_online_cpus() and get_online_mems() calls in kmem_cache_shrink() invoked via the shrink sysfs file. To fix that, we have to use trylock to get the memory and cpu hotplug read locks. Since hotplug events are rare, it should be fine to refuse a kmem caches shrink operation when some hotplug events are in progress. Signed-off-by: Waiman Long --- include/linux/memory_hotplug.h | 2 ++ mm/memory_hotplug.c | 5 +++++ mm/slub.c | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 93d9ada74ddd..4ec4b0a2f0fa 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -231,6 +231,7 @@ extern void get_page_bootmem(unsigned long ingo, struct page *page, void get_online_mems(void); void put_online_mems(void); +int tryget_online_mems(void); void mem_hotplug_begin(void); void mem_hotplug_done(void); @@ -274,6 +275,7 @@ static inline int try_online_node(int nid) static inline void get_online_mems(void) {} static inline void put_online_mems(void) {} +static inline int tryget_online_mems(void) { return 1; } static inline void mem_hotplug_begin(void) {} static inline void mem_hotplug_done(void) {} diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index fc0aad0bc1f5..38f9ccec9259 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -59,6 +59,11 @@ void get_online_mems(void) percpu_down_read(&mem_hotplug_lock); } +int tryget_online_mems(void) +{ + return percpu_down_read_trylock(&mem_hotplug_lock); +} + void put_online_mems(void) { percpu_up_read(&mem_hotplug_lock); diff --git a/mm/slub.c b/mm/slub.c index cf2114ca27f7..c4977ac3271b 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -5343,10 +5343,20 @@ static ssize_t shrink_show(struct kmem_cache *s, char *buf) static ssize_t shrink_store(struct kmem_cache *s, const char *buf, size_t length) { - if (buf[0] == '1') - kmem_cache_shrink(s); - else + if (buf[0] != '1') return -EINVAL; + + if (!cpus_read_trylock()) + return -EBUSY; + if (!tryget_online_mems()) { + length = -EBUSY; + goto cpus_unlock_out; + } + kasan_cache_shrink(s); + __kmem_cache_shrink(s); + put_online_mems(); +cpus_unlock_out: + cpus_read_unlock(); return length; } SLAB_ATTR(shrink); @@ -5654,7 +5664,8 @@ static ssize_t slab_attr_store(struct kobject *kobj, for (idx = 0; idx < cnt; idx++) { c = pcaches[idx]; - attribute->store(c, buf, len); + if (attribute->store(c, buf, len) == -EBUSY) + err = -EBUSY; percpu_ref_put(&c->memcg_params.refcnt); } kfree(pcaches);