From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f68.google.com (mail-pj1-f68.google.com [209.85.216.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A350A383309 for ; Tue, 12 May 2026 03:46:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.68 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778557603; cv=none; b=NVPxlBr7kalpHBdAncEsuOcn0r4vukdD/VLnWz1pJzJX/KeO9yXiOTOfxfPXDUbNSp/dXhMXoHZyIRTCQvmn7cmUnX1fR9n49Z+Y123MXJ3n57NydWIdUK4d2ewJMGi+BzHyg0IZ1urEma/CUXf9N/GDi2b4GLtb5WpqJsjlx5U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778557603; c=relaxed/simple; bh=RoO+OXl1G/hyBMYC73agTB565JL2bkIU4ca3YJ4m92g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X1C7oNWsiE3UYl//+xHXj5T+hirAgOi3yq9IE9FbcFuCOiCiGazHymYCfY4Fhc0pIJ7ahiSf01Av88qQ9kj7qwl6AYeFJavo0rfCjiLEaK/OBoOPjr5CXBVv/pilxHOBDFs3hKbXxjuOrFM0yf7Rggggfu9yhjfeIR1iOAm4YDA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SyRyz5Lv; arc=none smtp.client-ip=209.85.216.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SyRyz5Lv" Received: by mail-pj1-f68.google.com with SMTP id 98e67ed59e1d1-3664df30f53so2237491a91.1 for ; Mon, 11 May 2026 20:46:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778557602; x=1779162402; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fe+pv0uhu+8/9x+lT5zEcF9+ZmOubb2ATbUfZcsmgWA=; b=SyRyz5LvrfZCyw6+olR9412V6iCUoShZZGA5lHRysCBt0daextjaDp6Tbnf+i16Pt5 lh/tmfJnso+x4BlSYf4kYa3xASfE1+GgRMy68UnckVna0YTrWrax3+EwXCuJCu4GiLnK Kt2acT3eMu7VEf2oO3pr1VNOWpOmwE3Io4iRzd3dEiTNR3WTFPSbNcwjMjxcnRKvPfb6 /S/+Qg7wxYBSbsSagu9hAQA5r2oLaWPV6icnTvCPh6gqtF9pBGTvcRoo2LLQpXbLoLv4 yyQ9oSCOHme55gWnS6fTyXPC93K2bcMoB3L4yzS0V6+yi5EIzuNCKNlZHAM19UwDSCGR JwuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778557602; x=1779162402; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fe+pv0uhu+8/9x+lT5zEcF9+ZmOubb2ATbUfZcsmgWA=; b=l/6kcY8TOQyaWr1u/vBxKXDwIZFsE+2++qRXjYMZLXliTQeiv+JALDI6Pyw9K+r2y3 7zwDyyVYEi6Chu8/lQAtTsPCeslUGxe6l4p3R4F9EDlZod7ObPzJENp+TfTFdz9/Zjid NL/wHdyu3K1TOkQ5go2WGhR9wWOtJdUCIk2cjLLPEIOcLLJe+xCXyZPlPBjj551rVx09 cfJe2RcJGIDy1thTnzwEbp5KwTwqliJbzvXrYvhc/H8mK8+rGADYraGdToeFkgDQrzDL 6sjDsgD4kYO28B04RRYk6x+L13wtvZyWucg2eM2fh/HvuVW2GniRk0nVUvJbJTUhv1nn OTHQ== X-Forwarded-Encrypted: i=1; AFNElJ8erUej8de+gNyxNHrsp0dfHzK7bd5IN4aRQONjlBYw4svJEZR+Ye2aiSgFk6bQfeLSHINSRl8b1onjuJ4=@vger.kernel.org X-Gm-Message-State: AOJu0YxG0gmlXETwu36utXyOXN9wj4aLwQNi5n72y+Lx6cyLenYaub3+ Aty0kZSlbbM6pSrRuKkWG7WsLHB9LyD1t+Cn7EdxPj77Dy66YNQBd9f9 X-Gm-Gg: Acq92OHT5JdcZxO4pTcnuDFiN82HeicnHr3iQR41L2QVlOKTC4sQbRx+qXbDuOSpKOp x7YYxC6CvHvMBuyuKJhz+AL+J45D12CseDhmHpoVACra1VPuJL6Apy4k1xuEzcwLLo5S8etnvzT huDLkh6pS3/aIAuKQduNs3aVZZZ/1Mb+JNyoP/JmYpFdvzt/yDJ7wrKtTNA1YRfD/m7qNepy0vd KhlUubKWmATWreSih3Jd7CQfCTnKWdO91uFPVCi6y1x19P/K80k45j1H7a7dZJnEW7yxV2BJ0mG lxjf+RJbZLWK0/bdhqAyshrOh6vOW1MnkjxBiinBUg2su3EGfQ2LPxHJXXT8Pw/lj98MRpOAv5b jrtd54BFBRDYeR0v1e4/xJDBQO0+LFRe7DHuMQWjpFWy7Nd1HQ+pgPPgBnbKCQvJM99GNByZjjo QWLvMDWEJ6oX4N8B75Awkd+nV5FqhShJAWM+0ueCuvdBw7JW1Qvg== X-Received: by 2002:a17:90b:314c:b0:366:19da:832f with SMTP id 98e67ed59e1d1-368b26649d9mr1377619a91.17.1778557601863; Mon, 11 May 2026 20:46:41 -0700 (PDT) Received: from lima-ubuntu.hz.ali.com ([47.246.98.218]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-367d629f40bsm9341207a91.7.2026.05.11.20.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 20:46:41 -0700 (PDT) From: Qing Wang To: harry@kernel.org Cc: akpm@linux-foundation.org, cl@gentwo.org, hao.li@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, rientjes@google.com, roman.gushchin@linux.dev, vbabka@kernel.org, wangqing7171@gmail.com Subject: [PATCH v2] mm/slub: hold cpus_read_lock around flush_rcu_sheaves_on_cache() Date: Tue, 12 May 2026 11:46:35 +0800 Message-Id: <20260512034635.760011-1-wangqing7171@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit flush_rcu_sheaves_on_cache() calls queue_work_on() in a for_each_online_cpu() loop, which requires the cpu to stay online. But cpus_read_lock() is not held in kvfree_rcu_barrier_on_cache() and the set of "online cpus" is subject to change. There are two paths that call flush_rcu_sheaves_on_cache(): // has cpus_read_lock() flush_all_rcu_sheaves() -> flush_rcu_sheaves_on_cache() // no cpus_read_lock() kvfree_rcu_barrier_on_cache() -> flush_rcu_sheaves_on_cache() Fix this by holding cpus_read_lock() in kvfree_rcu_barrier_on_cache(). Why not move cpus_read_lock() from flush_all_rcu_sheaves() into flush_rcu_sheaves_on_cache()? The reason is it would introduce a new lock order (slab_mutex -> cpu_hotplug_lock). The reverse order (cpu_hotplug_lock -> slab_mutex) is established by - cpuhp_setup_state_nocalls(..., slub_cpu_setup, ...) - kmem_cache_destroy() The two orders together would form an AB-BA deadlock. Finally, add lockdep_assert_cpus_held() in flush_rcu_sheaves_on_cache() to catch the same problem in the future. Fixes: 0f35040de593 ("mm/slab: introduce kvfree_rcu_barrier_on_cache() for cache destruction") Signed-off-by: Qing Wang --- Changes in v2: - Deleted the unnecessary comment. - Added "Fixes" field in the commit message. mm/slab_common.c | 6 ++++++ mm/slub.c | 1 + 2 files changed, 7 insertions(+) diff --git a/mm/slab_common.c b/mm/slab_common.c index d5a70a831a2a..0ee5a4189453 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -2110,7 +2110,13 @@ EXPORT_SYMBOL_GPL(kvfree_rcu_barrier); void kvfree_rcu_barrier_on_cache(struct kmem_cache *s) { if (cache_has_sheaves(s)) { + /* + * flush_rcu_sheaves_on_cache() use queue_work_on() and queue_work_on() + * must be called with the CPU hotplug read lock. + */ + cpus_read_lock(); flush_rcu_sheaves_on_cache(s); + cpus_read_unlock(); rcu_barrier(); } diff --git a/mm/slub.c b/mm/slub.c index 161079ac5ba1..2a005d1e3a74 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4024,6 +4024,7 @@ void flush_rcu_sheaves_on_cache(struct kmem_cache *s) struct slub_flush_work *sfw; unsigned int cpu; + lockdep_assert_cpus_held(); mutex_lock(&flush_lock); for_each_online_cpu(cpu) { -- 2.34.1