Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3553319pxy; Tue, 4 May 2021 05:02:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyd+tUE32XejqSGQfBXNvcKLPDXirIjZGUQMlDdzB+jA67LeS+BpcSK4Llw3hM6yeo2ej6f X-Received: by 2002:a17:90a:66c3:: with SMTP id z3mr4786524pjl.196.1620129767923; Tue, 04 May 2021 05:02:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620129767; cv=none; d=google.com; s=arc-20160816; b=UqoP0y5EOGQWhDb/Sit6p4kF2ZSN3cwr8nHjXpb9DJcJJ/gc0DfLPhQs6uqS4Iqch1 jBAFcnhNvO06XJQJwDX0eJU7ebyHkVLvzK9RfupXMblFOHHG7ZlgCTmiv/JJtxR5NYq5 I0TtVYWcIdBqMVvnnEfwmlcdFQzN7RoxBBJ6PJGfls0a2rIiTfATtMbyZax9RrCEtzrP 123g/xM2uf+teXlhz0lL767qF0XUrR077RvqjncQGxN593vkuDrN5HOltzRRoswekejI wVNKUL9BVj5nw89x92GqPF6gtdnXcGOZQL17cybHELf20Kq9mLMRxJ7DKTdXSMoT1Wg7 JGwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=bZglNT+eTgeHxJZ3fwVIdrZTY4Ee4BmttnGBkTK9spk=; b=TckOSOl7Av0r9thGkHlPaE1GVgi4PsUmyTDVqdmXq+7ath3Mdehh8sQ8VIO2tvx0yC gxuYCyhTsy3OCtKTMExPOSHr5o26xTrdHRy5mCybd1iSaEVWxmo9AN5KsiBNL4QfyiZh 0x2kNvKnaHzx9dWr9gFqyzpLjo87SQI0yLRfn+Me3QkaK1tqXqLVKZ9pGoX8VGInRPja i3nSsu3t87yGgWiidwtjBQfktMQwHKy0ALpnSCBitQrbseb3cFeCR83qrocsssqkpzPw knV9/eZ4Y2hzy4342wO1CsA06zlzCqV1YvOUSFPOs/8uYPm+GeSVmY0I+js7UDi6cl/4 nr9A== ARC-Authentication-Results: i=1; mx.google.com; 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 e13si3056594pjr.30.2021.05.04.05.02.31; Tue, 04 May 2021 05:02:47 -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; 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 S230236AbhEDMCm (ORCPT + 99 others); Tue, 4 May 2021 08:02:42 -0400 Received: from mx2.suse.de ([195.135.220.15]:45168 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230217AbhEDMCm (ORCPT ); Tue, 4 May 2021 08:02:42 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9D21CB29A; Tue, 4 May 2021 12:01:46 +0000 (UTC) Subject: Re: [PATCH 5.13] mm, slub: move slub_debug static key enabling outside slab_mutex To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, David Rientjes , Joonsoo Kim , Christoph Lameter , Pekka Enberg , "Paul E . McKenney" References: <20210504120019.26791-1-vbabka@suse.cz> From: Vlastimil Babka Message-ID: Date: Tue, 4 May 2021 14:01:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210504120019.26791-1-vbabka@suse.cz> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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")