Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp587509lqp; Wed, 12 Jun 2024 10:03:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVTCbWNQG7+BrLlH00+Fo7bxoEZrPvnMo/2m1PIE9oZ35HVqDF2dv5MM5Ocs9nAtg5Wlw6fo99SNUt++77L8ZF2j48IABBV5dqf5JwReg== X-Google-Smtp-Source: AGHT+IH5EoEm98EZjS6ilYZkg8aYCB133eOxOgSW0VHFjlveEBK8yAuIRH+4Bj7/PbErLkAvPuhg X-Received: by 2002:a17:906:d296:b0:a66:414c:2abf with SMTP id a640c23a62f3a-a6f47f8d11dmr153296666b.26.1718211831646; Wed, 12 Jun 2024 10:03:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718211831; cv=pass; d=google.com; s=arc-20160816; b=k0IAmqDbQKTTkQfT4pHKJzWXaKtBgzDalEuLwDENAmFFbUohrx4SmbOSIAKVTisn1N 5DU5cz9y7Zp13rAca+IiS4q+9xHOuQsJGjbebTQxYvwNgnWgoFNuisGhs7pmojBUXWoh Wq1tB/uv8AaZ5XcHWoJ8UaQ65ERvvYbSlDA7PUbjHJRjj8TLZi76tkY6CVht+l/jWlFV RdIcxQb9+KpW9cqPDERFbTwmvoPKTncW0fqe2XVpK0GpYynR/4YOUW8jNy3dBKBdAnba lWiaWXCubGdSLbtEJGJKyxAy9CMfqHO4uYHgjnfNB60LS2xhWrEe2nPsfgpirdCXMJuT LCdQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=el84tMsbuMc7FXpW45tbOtPU1KGwRdWXM+fk76ycx5o=; fh=pdglhcY7hn43FRujNlflNhJmhVsdAI6S//OyMfKpxfg=; b=nxbrHGgPiUwx78JAK0+n5EJP867YFH+V0dGYSuJ1hw6DCRCCzZ8Is1iVIwBLILHvFA FC1B/j42PWdtWB4WLwZLFebptUoyBnktog8wBJw4nz4hWwzA/anuzk7pQ28rYw+ts+Zw 5uX3JsllDCohcUm7pfhwjxYZ2oEOtwLYp31ySbar9LMagBuPEeT2di+XNE0xm7ULu01Z HS2Ghvq9sK/RPtQr9hYxFWp+0I8IiRpDePi1E1eCgWN9MX1CSfwqmsxIlkv37vGMq5cj kW+jakRcTWekQXgXyl4ngULEJ+Rniacw1gTWXxX5CyfPkV+z1w3l1u+ONtos6k3Wdx+r W+WQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TjId44JJ; dkim=neutral (no key) header.i=@linutronix.de; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-211962-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211962-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f1f41254dsi330472066b.698.2024.06.12.10.03.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 10:03:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211962-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TjId44JJ; dkim=neutral (no key) header.i=@linutronix.de; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-211962-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211962-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 27E4D1F22918 for ; Wed, 12 Jun 2024 17:03:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0B881822ED; Wed, 12 Jun 2024 17:03:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TjId44JJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7qDoWKCH" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29FD728DB3; Wed, 12 Jun 2024 17:03:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718211802; cv=none; b=FIWD735rQdojhubVsLPpTh3+oexoVwXW1DUSSuoRi0041nZrLU3mfhnuvnc+fiXU2+umgjnTfT6iyJnZsxUuvZQju7RcBwc4Eaty8gyjsTrU/F18Wa2x/fsGaOhZk3rUZxgfJpCsXFJt01OjpRcOmXgyEBSLR5pciBIDd8wV1mg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718211802; c=relaxed/simple; bh=iqlgRBii+OGONhIdjUcUpXSfRQsTaMxuuHKIjqsbsOo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nTRCWE/kqf+NoqFDUjIA9coXiy1I98jVjPNSNxiMsPYMnDOgScHu0ig2qOPe3PpsYyd3oumQqRuP8swTXuq/4P9sh9APCoF8eGlIe1qgtzcr2TNXzsvyPY1XmqaUNhH1CbNP0PC9cJyKjXeRnPjnBQhYwTCXSDw5teS42VgL9XA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TjId44JJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7qDoWKCH; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718211793; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=el84tMsbuMc7FXpW45tbOtPU1KGwRdWXM+fk76ycx5o=; b=TjId44JJ5wIauRk1Qoag1N7BvL7mW/BNahbaHR1Gog5CnWolc4S8K9bfumzDHk4PBTVNMF Y2vltzDOYG0xijnbJ2WNES1d3d/la14eJJoyRhVOedFYF9f95w77jfosEYVcVHXdK+z4wZ uK1qo1SCZuO0oMaOHxDgMAAuqLopZkQetSEElrr+j1BbpOXnN61FvOlCucxD1aX8N0Ec2u +Vf9HIUbJ8mSR7aSOBbiLO1/zG0hvNDqh2sflCnor2erX3SVtUoxVdPf4o40OMdWUZpdwF sJSMbwqUh7/rLRcVCm4gTA/d2DFjfpz/mpIQ+NnPJM2JYUE+64pS4QCqbpKybQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718211793; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=el84tMsbuMc7FXpW45tbOtPU1KGwRdWXM+fk76ycx5o=; b=7qDoWKCHTVBYn8DH4GiJMBj0dSAOdrMu1Sn2EXi1ZUFV/CXrD2cYxMbRZ9m8biymlREUdQ JlMZAjgofHnhQ6Bw== To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "David S. Miller" , Daniel Bristot de Oliveira , Boqun Feng , Daniel Borkmann , Eric Dumazet , Frederic Weisbecker , Ingo Molnar , Jakub Kicinski , Paolo Abeni , Peter Zijlstra , Thomas Gleixner , Waiman Long , Will Deacon , Sebastian Andrzej Siewior Subject: [PATCH v6 net-next 02/15] locking/local_lock: Add local nested BH locking infrastructure. Date: Wed, 12 Jun 2024 18:44:28 +0200 Message-ID: <20240612170303.3896084-3-bigeasy@linutronix.de> In-Reply-To: <20240612170303.3896084-1-bigeasy@linutronix.de> References: <20240612170303.3896084-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Add local_lock_nested_bh() locking. It is based on local_lock_t and the naming follows the preempt_disable_nested() example. For !PREEMPT_RT + !LOCKDEP it is a per-CPU annotation for locking assumptions based on local_bh_disable(). The macro is optimized away during compilation. For !PREEMPT_RT + LOCKDEP the local_lock_nested_bh() is reduced to the usual lock-acquire plus lockdep_assert_in_softirq() - ensuring that BH is disabled. For PREEMPT_RT local_lock_nested_bh() acquires the specified per-CPU lock. It does not disable CPU migration because it relies on local_bh_disable() disabling CPU migration. With LOCKDEP it performans the usual lockdep checks as with !PREEMPT_RT. Due to include hell the softirq check has been moved spinlock.c. The intention is to use this locking in places where locking of a per-CPU variable relies on BH being disabled. Instead of treating disabled bottom halves as a big per-CPU lock, PREEMPT_RT can use this to reduce the locking scope to what actually needs protecting. A side effect is that it also documents the protection scope of the per-CPU variables. Acked-by: Peter Zijlstra (Intel) Reviewed-by: Thomas Gleixner Signed-off-by: Sebastian Andrzej Siewior --- include/linux/local_lock.h | 10 ++++++++++ include/linux/local_lock_internal.h | 31 +++++++++++++++++++++++++++++ include/linux/lockdep.h | 3 +++ kernel/locking/spinlock.c | 8 ++++++++ 4 files changed, 52 insertions(+) diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h index 82366a37f4474..091dc0b6bdfb9 100644 --- a/include/linux/local_lock.h +++ b/include/linux/local_lock.h @@ -62,4 +62,14 @@ DEFINE_LOCK_GUARD_1(local_lock_irqsave, local_lock_t __p= ercpu, local_unlock_irqrestore(_T->lock, _T->flags), unsigned long flags) =20 +#define local_lock_nested_bh(_lock) \ + __local_lock_nested_bh(_lock) + +#define local_unlock_nested_bh(_lock) \ + __local_unlock_nested_bh(_lock) + +DEFINE_GUARD(local_lock_nested_bh, local_lock_t __percpu*, + local_lock_nested_bh(_T), + local_unlock_nested_bh(_T)) + #endif diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock= _internal.h index 975e33b793a77..8dd71fbbb6d2b 100644 --- a/include/linux/local_lock_internal.h +++ b/include/linux/local_lock_internal.h @@ -62,6 +62,17 @@ do { \ local_lock_debug_init(lock); \ } while (0) =20 +#define __spinlock_nested_bh_init(lock) \ +do { \ + static struct lock_class_key __key; \ + \ + debug_check_no_locks_freed((void *)lock, sizeof(*lock));\ + lockdep_init_map_type(&(lock)->dep_map, #lock, &__key, \ + 0, LD_WAIT_CONFIG, LD_WAIT_INV, \ + LD_LOCK_NORMAL); \ + local_lock_debug_init(lock); \ +} while (0) + #define __local_lock(lock) \ do { \ preempt_disable(); \ @@ -98,6 +109,15 @@ do { \ local_irq_restore(flags); \ } while (0) =20 +#define __local_lock_nested_bh(lock) \ + do { \ + lockdep_assert_in_softirq(); \ + local_lock_acquire(this_cpu_ptr(lock)); \ + } while (0) + +#define __local_unlock_nested_bh(lock) \ + local_lock_release(this_cpu_ptr(lock)) + #else /* !CONFIG_PREEMPT_RT */ =20 /* @@ -138,4 +158,15 @@ typedef spinlock_t local_lock_t; =20 #define __local_unlock_irqrestore(lock, flags) __local_unlock(lock) =20 +#define __local_lock_nested_bh(lock) \ +do { \ + lockdep_assert_in_softirq_func(); \ + spin_lock(this_cpu_ptr(lock)); \ +} while (0) + +#define __local_unlock_nested_bh(lock) \ +do { \ + spin_unlock(this_cpu_ptr((lock))); \ +} while (0) + #endif /* CONFIG_PREEMPT_RT */ diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 5e51b0de4c4b5..fcc02812bf31e 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -605,6 +605,8 @@ do { \ (!in_softirq() || in_irq() || in_nmi())); \ } while (0) =20 +extern void lockdep_assert_in_softirq_func(void); + #else # define might_lock(lock) do { } while (0) # define might_lock_read(lock) do { } while (0) @@ -618,6 +620,7 @@ do { \ # define lockdep_assert_preemption_enabled() do { } while (0) # define lockdep_assert_preemption_disabled() do { } while (0) # define lockdep_assert_in_softirq() do { } while (0) +# define lockdep_assert_in_softirq_func() do { } while (0) #endif =20 #ifdef CONFIG_PROVE_RAW_LOCK_NESTING diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c index 8475a0794f8c5..438c6086d540e 100644 --- a/kernel/locking/spinlock.c +++ b/kernel/locking/spinlock.c @@ -413,3 +413,11 @@ notrace int in_lock_functions(unsigned long addr) && addr < (unsigned long)__lock_text_end; } EXPORT_SYMBOL(in_lock_functions); + +#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_PREEMPT_RT) +void notrace lockdep_assert_in_softirq_func(void) +{ + lockdep_assert_in_softirq(); +} +EXPORT_SYMBOL(lockdep_assert_in_softirq_func); +#endif --=20 2.45.1