Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp110026pxy; Tue, 4 May 2021 20:08:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFDcY0OySaTCjXcX/zPlbHjx/WPuuWjRobwq9biiJL2jYVMg193w0SavyWPxW265+ts7wZ X-Received: by 2002:a17:906:38c5:: with SMTP id r5mr24180920ejd.230.1620184117432; Tue, 04 May 2021 20:08:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620184117; cv=none; d=google.com; s=arc-20160816; b=0zJ2fZUylK9MUwdbor7zSxU17qSbeG5NchofECvmkp/BuF7eZ7TP6yp65XZyYDoth7 ot7dCBbVsg3qijSJMziAE1xcV8yQTSVfYUayfhq8tPeYJFhs4pB2u+6a6iidSOBNe47v oZOyV5E+vimwZxhC/ErmqHcVeB9DZg7xdDVaj3ACctDnYaIM/4wEfBcwK3ASoGekhhzW n0yu3wQH45E8Xr3taffO7wmcFcvnDeTe7JGS8iDqqXnRbyiPfeiEBpa0OfUsZgyAqe/4 jTFdDNMlxbpVN3VXNJJhmQB6KcM4OepEGtf1fIGZQE4FdQkvkWsZ5wImXO7fq2mNmS6k N6jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=O8nA00ni6lNsqnWOdLM7s679eOLE4MZHEuCAj6DehK4=; b=B+ascqmmdlbU09Eax9ITMEmLDVW9Eu3DbyLUAuAV6GvQzFSvI6Rq3JYGH4hVXJDQPa SVaWZ0AF8fUmG1JXLYH5vGsF5qI0KFa+pGHTWWCLslzAVydVhUCoWsHZhzjTedk99ofR U2YeQTZa3IXDp0nmvIa+oX3kj3vR/h+6VbMh1QH+DxzfCd5Ajx3AB4zUb5EHhpaF6/Y3 5+tUetCjJt9nt131oWxgrWNjHQn65G6GUyYRa7OTuo7YKUoYGcHX1xHQpYEsXmbp6IDk +yhTwKxApco511GvLLm3DD3jrgkq0FdDHl0MV/wv4x6YYNTBXL5TegSbkNOSyFTqracJ pxUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FzKTQSAD; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bo9si4477635ejb.184.2021.05.04.20.08.11; Tue, 04 May 2021 20:08:37 -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=@google.com header.s=20161025 header.b=FzKTQSAD; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230483AbhEECxI (ORCPT + 99 others); Tue, 4 May 2021 22:53:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230079AbhEECxI (ORCPT ); Tue, 4 May 2021 22:53:08 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC32CC061574 for ; Tue, 4 May 2021 19:52:12 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id lp4so52468pjb.1 for ; Tue, 04 May 2021 19:52:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=O8nA00ni6lNsqnWOdLM7s679eOLE4MZHEuCAj6DehK4=; b=FzKTQSADNf9GMiSJqYlX8A+RMeHzC6trj8E7AwMr/snkhpP3QQ/LKjWO45WWdBchMT qyryQtqD2xVmzvsqCYXL7l25X4gxSJX3Mt4fbLQILIc8B/+NrMAyWny894RHIeg260Mh XJ+Iec+T0OZE5Duy5+QkmELbZOaR4Blni3FXD1scgwx4tYo1Z2ApyWo4AjcmT3zuuyJU uFR0oe2t45Hy6PYWdVPKzuo7sRadTihVrIoy6LDup8xpfYo1jMEgO/zbygbtqLJixzPn VnmUpvIOmQGkff0RNohhDw+jCCieZLnjaVYVNvF+ey+n9c0IOFttYkE4uJ4QZob53xGr VUBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=O8nA00ni6lNsqnWOdLM7s679eOLE4MZHEuCAj6DehK4=; b=BuQrmoB1m4I++cx/4LCXdnMeW+pYgBZJ2/m8J2xhwPcAz9w+fng6qRt5/5BatBsnU2 vt5jPUFci9NwH8/Y7v6DvhHkcIfaAoV797N4rS75sMhZgMjsJZ8EqvTZFrJa7QJuCnih z2AdoDvywAxhq+2LHyqcYmVISBqXxdsUSlhfEY3//XVZh0a4K222UDNPW9aaK32aaFcx W0GqaftirIXl3usMixYMdWF5ncK56cZQ/DxKpi/DJo8PZw0KxRu9eQXfRgdvmlXiK4VN XJ5d+EkbIDvo6iYmZNWX/D+xgV6NuIchcRUZLX3x1nhbnV0jN+YOURkBIy0fVrFV4iGY bkcw== X-Gm-Message-State: AOAM532HAJnR3Z2ZIwYvETIF8xxGugoKRSBLBkbxyYn4cBFOTAkCqAHy oM4f772LPgy4HCaiOXLLT3DLTQ== X-Received: by 2002:a17:90a:f2d3:: with SMTP id gt19mr8983022pjb.52.1620183132131; Tue, 04 May 2021 19:52:12 -0700 (PDT) Received: from [2620:15c:17:3:34c9:704d:575a:e347] ([2620:15c:17:3:34c9:704d:575a:e347]) by smtp.gmail.com with ESMTPSA id d63sm6130047pjk.10.2021.05.04.19.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 May 2021 19:52:11 -0700 (PDT) Date: Tue, 4 May 2021 19:52:10 -0700 (PDT) From: David Rientjes To: Vlastimil Babka cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Joonsoo Kim , Christoph Lameter , Pekka Enberg , "Paul E . McKenney" Subject: Re: [PATCH 5.13] mm, slub: move slub_debug static key enabling outside slab_mutex In-Reply-To: Message-ID: <96862325-1ef1-b255-37f4-e22777a4eb78@google.com> References: <20210504120019.26791-1-vbabka@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 4 May 2021, Vlastimil Babka wrote: > On 5/4/21 2:00 PM, Vlastimil Babka wrote: > > Paul E. McKenney reported [1] that commit 1f0723a4c0df ("mm, slub: enable > > slub_debug static key when creating cache with explicit debug flags") results > > in the lockdep complaint: > > > > ====================================================== > > WARNING: possible circular locking dependency detected > > 5.12.0+ #15 Not tainted > > ------------------------------------------------------ > > rcu_torture_sta/109 is trying to acquire lock: > > ffffffff96063cd0 (cpu_hotplug_lock){++++}-{0:0}, at: static_key_enable+0x9/0x20 > > > > but task is already holding lock: > > ffffffff96173c28 (slab_mutex){+.+.}-{3:3}, at: kmem_cache_create_usercopy+0x2d/0x250 > > > > which lock already depends on the new lock. > > > > the existing dependency chain (in reverse order) is: > > > > -> #1 (slab_mutex){+.+.}-{3:3}: > > lock_acquire+0xb9/0x3a0 > > __mutex_lock+0x8d/0x920 > > slub_cpu_dead+0x15/0xf0 > > cpuhp_invoke_callback+0x17a/0x7c0 > > cpuhp_invoke_callback_range+0x3b/0x80 > > _cpu_down+0xdf/0x2a0 > > cpu_down+0x2c/0x50 > > device_offline+0x82/0xb0 > > remove_cpu+0x1a/0x30 > > torture_offline+0x80/0x140 > > torture_onoff+0x147/0x260 > > kthread+0x10a/0x140 > > ret_from_fork+0x22/0x30 > > > > -> #0 (cpu_hotplug_lock){++++}-{0:0}: > > check_prev_add+0x8f/0xbf0 > > __lock_acquire+0x13f0/0x1d80 > > lock_acquire+0xb9/0x3a0 > > cpus_read_lock+0x21/0xa0 > > static_key_enable+0x9/0x20 > > __kmem_cache_create+0x38d/0x430 > > kmem_cache_create_usercopy+0x146/0x250 > > kmem_cache_create+0xd/0x10 > > rcu_torture_stats+0x79/0x280 > > kthread+0x10a/0x140 > > ret_from_fork+0x22/0x30 > > > > other info that might help us debug this: > > > > Possible unsafe locking scenario: > > > > CPU0 CPU1 > > ---- ---- > > lock(slab_mutex); > > lock(cpu_hotplug_lock); > > lock(slab_mutex); > > lock(cpu_hotplug_lock); > > > > *** DEADLOCK *** > > > > 1 lock held by rcu_torture_sta/109: > > #0: ffffffff96173c28 (slab_mutex){+.+.}-{3:3}, at: kmem_cache_create_usercopy+0x2d/0x250 > > > > stack backtrace: > > CPU: 3 PID: 109 Comm: rcu_torture_sta Not tainted 5.12.0+ #15 > > Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 > > Call Trace: > > dump_stack+0x6d/0x89 > > check_noncircular+0xfe/0x110 > > ? lock_is_held_type+0x98/0x110 > > check_prev_add+0x8f/0xbf0 > > __lock_acquire+0x13f0/0x1d80 > > lock_acquire+0xb9/0x3a0 > > ? static_key_enable+0x9/0x20 > > ? mark_held_locks+0x49/0x70 > > cpus_read_lock+0x21/0xa0 > > ? static_key_enable+0x9/0x20 > > static_key_enable+0x9/0x20 > > __kmem_cache_create+0x38d/0x430 > > kmem_cache_create_usercopy+0x146/0x250 > > ? rcu_torture_stats_print+0xd0/0xd0 > > kmem_cache_create+0xd/0x10 > > rcu_torture_stats+0x79/0x280 > > ? rcu_torture_stats_print+0xd0/0xd0 > > kthread+0x10a/0x140 > > ? kthread_park+0x80/0x80 > > ret_from_fork+0x22/0x30 > > > > This is because there's one order of locking from the hotplug callbacks: > > > > lock(cpu_hotplug_lock); // from hotplug machinery itself > > lock(slab_mutex); // in e.g. slab_mem_going_offline_callback() > > > > And commit 1f0723a4c0df made the reverse sequence possible: > > lock(slab_mutex); // in kmem_cache_create_usercopy() > > lock(cpu_hotplug_lock); // kmem_cache_open() -> static_key_enable() > > > > The simplest fix is to move static_key_enable() to a place before slab_mutex is > > taken. That means kmem_cache_create_usercopy() in mm/slab_common.c which is not > > ideal for SLUB-specific code, but the #ifdef CONFIG_SLUB_DEBUG makes it > > at least self-contained and obvious. > > > > [1] https://lore.kernel.org/lkml/20210502171827.GA3670492@paulmck-ThinkPad-P17-Gen-1/ > > > > Reported-and-tested-by: Paul E. McKenney > > Forgot: > Fixes: 1f0723a4c0df ("mm, slub: enable slub_debug static key when creating cache > with explicit debug flags") > Acked-by: David Rientjes Nice concise fix!