Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3487261ybz; Mon, 27 Apr 2020 16:58:55 -0700 (PDT) X-Google-Smtp-Source: APiQypKLbdFHu5yeJK2TYV4sUcskca8GnjmPK1UBmiMFIfrp5N/olhUZIsGhQbhj3RtiUj+ZZdH6 X-Received: by 2002:a17:906:2f8f:: with SMTP id w15mr22624907eji.255.1588031935379; Mon, 27 Apr 2020 16:58:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588031935; cv=none; d=google.com; s=arc-20160816; b=jjoloWNq3jY+3fdDf7GGL5cPG8yvi0YCVnozA050b1+Yk/uVTM7wX1TZLanTimL9NE 8FqocjI9yPQXrQ8MtyIRZIkzgi5qJQH/yCx60mjqvghNE8y6MU7b8PKBNnHBcXDR6rHX SU4GkmNBwdFIZApIsnc6kuuVhf4GhHU9vppPh+o3Lgp5Ma0NFAuFu/iaou00Iv0amck9 qob58jnH0QyhbBUQwIcj8ca9pWxvcDvw/ie2EkTiWEvbk9pzNgbeKfw8O2vA0hXwLeQq oaMB/tJIG5KLPIKYNmvCjk1sV/ZwDOOtZHA5eWgiGn6zH4jr/Tjs0TjfO0MqPDok12cu qANw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=hKMGvop5RUwWSaAIrxGf06+2WG92KPK2um2wWjE0aqI=; b=yZE5TkdnQGbpvQLPwYlxFie4+eBudPpydLhkTDb3NRqt2yMgKAO08t9pyCax0RRQ4n QQIqQIVMvq0mHy6OpUmPsjmSeFjEmRQuobzTrjbRxg5bfWBlzzcoOmQ2VbJev9KJ5TX1 oofyqMJiFxDZA0GsdTQ+5hVm2v3psC57jAlyLXKt9PPOpL5kuPhw/qoZCWpvPb837sg7 mhHqgi+DZhge5uTLxa0R6XHTuanaJIlmNVpJBw00BMXu8B8bQZyESaZNyowFdU4Zef52 9Iu4KYMdw77sDrMJX1Hewmw/XABnfdikNu8bzVTi+SXl2gQjdBW0tNjN2oy0INBkwo88 82lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FgUaCwP5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h21si758459ejc.152.2020.04.27.16.58.32; Mon, 27 Apr 2020 16:58:55 -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=@redhat.com header.s=mimecast20190719 header.b=FgUaCwP5; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726450AbgD0X5A (ORCPT + 99 others); Mon, 27 Apr 2020 19:57:00 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:28323 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726328AbgD0X4w (ORCPT ); Mon, 27 Apr 2020 19:56:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1588031810; 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=hKMGvop5RUwWSaAIrxGf06+2WG92KPK2um2wWjE0aqI=; b=FgUaCwP5/4VVeP3CbMFUJg5qVFyi5aOIZ+d0dW4mbXXR4u7MmWFDuCvFdr7yJZPKMaSpBc rZ33PARjnls7yRfjBV9zNPEqnm60/Z/5LBXMAyQlwmjD92KSgUSptL2Yw05q8L7IQ/7wVy 6aX1kAX+53fNi1jCA6qX+3uIREXwyVM= 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-447-iDM7_ISSM26hLWkH4Npq8w-1; Mon, 27 Apr 2020 19:56:48 -0400 X-MC-Unique: iDM7_ISSM26hLWkH4Npq8w-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 7A2A7180F122; Mon, 27 Apr 2020 23:56:46 +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 3293560BE2; Mon, 27 Apr 2020 23:56:45 +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 3/4] mm/slub: Fix another circular locking dependency in slab_attr_store() Date: Mon, 27 Apr 2020 19:56:20 -0400 Message-Id: <20200427235621.7823-4-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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It turns out that switching from slab_mutex to memcg_cache_ids_sem in slab_attr_store() does not completely eliminate circular locking dependency as shown by the following lockdep splat when the system is shut down: [ 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 *** 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. Signed-off-by: Waiman Long --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 5 +++++ mm/slub.c | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) 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); /* * Helper macro to loop through all memcg-specific caches. Callers must still 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); } +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 #include @@ -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 = 0; + int retries = 3; size_t size, old = s->max_attr_size; struct memcg_cache_array *arr; @@ -5585,9 +5587,28 @@ static ssize_t slab_attr_store(struct kobject *kobj, old = cmpxchg(&s->max_attr_size, size, len); } while (old != size); - memcg_get_cache_ids(); - max = 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-- <= 0) + return -EBUSY; + msleep(100); + } + max = memcg_nr_cache_ids; pcaches = kmalloc_array(max, sizeof(void *), GFP_KERNEL); if (!pcaches) { memcg_put_cache_ids(); -- 2.18.1