Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1663781ybk; Sat, 16 May 2020 19:23:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwuhT7BNqGS9SsSMSTU5bZ2T71GhatkTpRFcVYLsrweAcETdTdxDQCiAO2CXncM3M05WI26 X-Received: by 2002:a50:9a86:: with SMTP id p6mr8651878edb.153.1589682182603; Sat, 16 May 2020 19:23:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589682182; cv=none; d=google.com; s=arc-20160816; b=DPW4bAfXprsYEWRlmYfgvYbiXNVdJO1VPRST+OCZHAFCVKgBRAeVmDzcIG9T8XuTUg wn+gA1/zwQhCVh/j/gPadI9Yz22hG+A/iFcW0f5JAN49Qasc02qDzt/gYDFJIKyrJsON HPHX8Uhhazpce+VrhbwLJLeHYkUA/c8YGdGtWIzyaLaQvlt+mxws1HE3LpSIUDtuIN+9 0msiWdF1DnzLKyPO1f5ksafKZ3Hx/cHreYSuy67AW1cOLG6owThkPeD4jRDj85Z/pDMO EMAo0h6jbJRCbI1sD4LhX1HYpgzrdH8Mej7jq2l/L+CPkSdiXCqo9XbhvyrA5xDSn0gj DXkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:in-reply-to:cc:references:message-id :date:subject:mime-version:from:content-transfer-encoding :dkim-signature; bh=reILY0Vqckz9zOXZI1PQPYQDzXOJemhFua89GW65fy0=; b=L1KDCRlnfL1pOCOgbRH/qZFOLmo2C2ctKcVVpSiz6ulxqRaaOiMthLAIKnVEzsRuV3 j709zgK3f/Txm9ERelUsXbj1HzyjvhHGRneF09Nge6Bog0YvTaxD0kEB+RpdD+SQeJdE k+c1z0n6xXsglVKdXygP2oUi7rvzVF1RlDxv4D40850w7NCj268lkeOYQJj5F9OOip+V NQwg7RWpy1ZxxlvU4dWvYA2GvPpJ8ohHfuakxg4TB37GXuFwqGan4GdDgUkx8H7Y4R2h QKmItzgAxtGR+gclwOFGwHXIpG0BXj1Ck1IAtil722zypenfVTLd8mu/LDnrZL2WB4HT aw3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=pIAwb2eo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v22si64170ejh.271.2020.05.16.19.22.37; Sat, 16 May 2020 19:23:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=pIAwb2eo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726947AbgEQCTQ (ORCPT + 99 others); Sat, 16 May 2020 22:19:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726880AbgEQCTP (ORCPT ); Sat, 16 May 2020 22:19:15 -0400 Received: from mail-qv1-xf44.google.com (mail-qv1-xf44.google.com [IPv6:2607:f8b0:4864:20::f44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27646C061A0C for ; Sat, 16 May 2020 19:19:15 -0700 (PDT) Received: by mail-qv1-xf44.google.com with SMTP id a4so3107189qvj.3 for ; Sat, 16 May 2020 19:19:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=reILY0Vqckz9zOXZI1PQPYQDzXOJemhFua89GW65fy0=; b=pIAwb2eoYglLwoftFfZRHOH1Iauxv47DwFt8guUDUzepmhBtdWKnf6tj+cBDhU9OTF kECfnujvTJKr8YeroaSRIiYeVy5KG310PnOC4OsyxQQpkXo6JV8KUh6wHeidXXdnYJLi 2urTKHfOv2+fh2Z/drc7aLYPMpFOhLMO8thHY7q6ZR2EHH3E3rk2TZMBw375QmdTfjhS WVxV5sKNACulZvEq6O83Mxk6dZiv9ieiAuoy2dCn3CpaWCc0E4KZvCOUTZwVthhcZqmp hwBOf1zuiZPtXjLHkbZSyw6nLiwrG/RWBgQdUOmCje4sXZHYx8G88VLOW5OwZabR568J jUkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=reILY0Vqckz9zOXZI1PQPYQDzXOJemhFua89GW65fy0=; b=RretUece9frA990hspM3i0i/u3/JHbVSuRWzpnvcSTJfgyISprao8sg3jCFPssjNQ7 yW8a2v5oAh2RtG1QpB9mb1Hf62Dyf6FzHZv1BHt8Kn8mjU92cUMQqF/9SXqzHFuUJPGW IpS5Bnlm585+PAK4o2tG8sWg+rLIXnt6xnk36Ip7JRk0Gck1f/4qOFMuGA35rbPRvkLv OWuI8Oro9D50v/G8GzJcB4RWGKMnZQpf8l7euMyq8Cp/lz68wwaxmvssrlxF1kdwPLUq IpoGEMyrYfEj4+ntvBAkosNuifXNf884KP2JdqzX8/qt+jarqwotR1k0Gpby3D6ZS91o WKsg== X-Gm-Message-State: AOAM531C78UAmhLIbFuPdyQ+8QxB075YMVnvtvX4Gjcf/qfmhyMxqlqB 8MNao+6CDdSrE0w0AbR8fv5eZg== X-Received: by 2002:a0c:f590:: with SMTP id k16mr7536598qvm.81.1589681954242; Sat, 16 May 2020 19:19:14 -0700 (PDT) Received: from [192.168.1.183] (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id p2sm5057375qkm.41.2020.05.16.19.19.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 16 May 2020 19:19:13 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable From: Qian Cai Mime-Version: 1.0 (1.0) Subject: Re: [PATCH v2 3/4] mm/slub: Fix another circular locking dependency in slab_attr_store() Date: Sat, 16 May 2020 22:19:12 -0400 Message-Id: References: <20200427235621.7823-4-longman@redhat.com> Cc: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Johannes Weiner , Michal Hocko , Vladimir Davydov , linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Juri Lelli In-Reply-To: <20200427235621.7823-4-longman@redhat.com> To: Waiman Long X-Mailer: iPhone Mail (17E262) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Apr 27, 2020, at 7:56 PM, Waiman Long wrote: >=20 > It turns out that switching from slab_mutex to memcg_cache_ids_sem in > slab_attr_store() does not completely eliminate circular locking dependenc= y > as shown by the following lockdep splat when the system is shut down: >=20 > [ 2095.079697] Chain exists of: > [ 2095.079697] kn->count#278 --> memcg_cache_ids_sem --> slab_mutex > [ 2095.079697] > [ 2095.090278] Possible unsafe locking scenario: > [ 2095.090278] > [ 2095.096227] CPU0 CPU1 > [ 2095.100779] ---- ---- > [ 2095.105331] lock(slab_mutex); > [ 2095.108486] lock(memcg_cache_ids_sem); > [ 2095.114961] lock(slab_mutex); > [ 2095.120649] lock(kn->count#278); > [ 2095.124068] > [ 2095.124068] *** DEADLOCK *** Can you show the full splat? >=20 > To eliminate this possibility, we have to use trylock to acquire > memcg_cache_ids_sem. Unlikely slab_mutex which can be acquired in > many places, the memcg_cache_ids_sem write lock is only acquired > in memcg_alloc_cache_id() to double the size of memcg_nr_cache_ids. > So the chance of successive calls to memcg_alloc_cache_id() within > a short time is pretty low. As a result, we can retry the read lock > acquisition a few times if the first attempt fails. >=20 > Signed-off-by: Waiman Long The code looks a bit hacky and probably not that robust. Since it is the shu= tdown path which is not all that important without lockdep, maybe you could d= rop this single patch for now until there is a better solution? > --- > include/linux/memcontrol.h | 1 + > mm/memcontrol.c | 5 +++++ > mm/slub.c | 25 +++++++++++++++++++++++-- > 3 files changed, 29 insertions(+), 2 deletions(-) >=20 > diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h > index d275c72c4f8e..9285f14965b1 100644 > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -1379,6 +1379,7 @@ extern struct workqueue_struct *memcg_kmem_cache_wq;= > extern int memcg_nr_cache_ids; > void memcg_get_cache_ids(void); > void memcg_put_cache_ids(void); > +int memcg_tryget_cache_ids(void); >=20 > /* > * Helper macro to loop through all memcg-specific caches. Callers must st= ill > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 5beea03dd58a..9fa8535ff72a 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -279,6 +279,11 @@ void memcg_get_cache_ids(void) > down_read(&memcg_cache_ids_sem); > } >=20 > +int memcg_tryget_cache_ids(void) > +{ > + return down_read_trylock(&memcg_cache_ids_sem); > +} > + > void memcg_put_cache_ids(void) > { > up_read(&memcg_cache_ids_sem); > diff --git a/mm/slub.c b/mm/slub.c > index 44cb5215c17f..cf2114ca27f7 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -34,6 +34,7 @@ > #include > #include > #include > +#include >=20 > #include >=20 > @@ -5572,6 +5573,7 @@ static ssize_t slab_attr_store(struct kobject *kobj,= > !list_empty(&s->memcg_params.children)) { > struct kmem_cache *c, **pcaches; > int idx, max, cnt =3D 0; > + int retries =3D 3; > size_t size, old =3D s->max_attr_size; > struct memcg_cache_array *arr; >=20 > @@ -5585,9 +5587,28 @@ static ssize_t slab_attr_store(struct kobject *kobj= , > old =3D cmpxchg(&s->max_attr_size, size, len); > } while (old !=3D size); >=20 > - memcg_get_cache_ids(); > - max =3D memcg_nr_cache_ids; > + /* > + * To avoid the following circular lock chain > + * > + * kn->count#278 --> memcg_cache_ids_sem --> slab_mutex > + * > + * We need to use trylock to acquire memcg_cache_ids_sem. > + * > + * Since the write lock is acquired only in > + * memcg_alloc_cache_id() to double the size of > + * memcg_nr_cache_ids. The chance of successive > + * memcg_alloc_cache_id() calls within a short time is > + * very low except at the beginning where the number of > + * memory cgroups is low. So we retry a few times to get > + * the memcg_cache_ids_sem read lock. > + */ > + while (!memcg_tryget_cache_ids()) { > + if (retries-- <=3D 0) > + return -EBUSY; > + msleep(100); > + } >=20 > + max =3D memcg_nr_cache_ids; > pcaches =3D kmalloc_array(max, sizeof(void *), GFP_KERNEL); > if (!pcaches) { > memcg_put_cache_ids();