Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6824883rdb; Fri, 15 Dec 2023 09:13:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJOe+BYMr6UrrkUBV1Hop8I6Nt1XMjOrZpmAsFWIv0AZuBoHoKUSvkgwXkm76StfM2egXj X-Received: by 2002:a17:90a:bd8a:b0:28b:3895:c73b with SMTP id z10-20020a17090abd8a00b0028b3895c73bmr595730pjr.77.1702660386446; Fri, 15 Dec 2023 09:13:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702660386; cv=none; d=google.com; s=arc-20160816; b=S20d/ZSSi2u0qSjAeByP+veUCFScO4Wdt2CycuuEv4DxzlXOoEM25FhR9/fFmkF7iC TFIJZoHFapOq/KS4ZLMg3bp7+AppMIKG2gKgYJjvIx1m/xt3zGmRm4fnbZYkUxSNiOKM QpJlOuoAYWMbBFq0qPSzVPAYat0393BS55jNrYB33Rp7B2e45OcbM2IWXLoEeruSOerk oSLLxG7AOKHfz5lMXe3JmWhheKQ6Q6Qpy681vyrZiLXqH5076nKUO20Kz5MkM3FVf/su rBcW/V+BjuF9WsNZ/8K2E/2FnFxWSxziGdBOOBRIymRBbMlBen4e4lTee43U6XICbt5h QKDg== ARC-Message-Signature: i=1; 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=4hXWC+KQ48HdWABG8tICGwil7n9P1nfuL/9s+0V/nEk=; fh=mRMHo6FEBd+e0N/0wwmt9BaEcwCnNTE8npQn9DDRWX4=; b=QgGlcomRtWKlQm/7mi/kWRGLo2251dSvBVWcAUjHMzTR5yiwEkj99LniNyqCPh7dbu 9bxxxTEixDheGNzF4Mc9hRTYZMasuZZTeCeHgFNupfdUTtsfEvsPoVQ55iMzHdcOVouj Mq2m7iNxbTwXTLHLUAz2C/R1nnikflm1lQ+SqcTE4pWZECRXkCkt+4O2G3GUj60+bwFP uTV4pvxTort9SQ8F41xE2dAlVZYvTeZtub+duBEceCFjnwvUdcGdUjJDUTFCa16mgVNo aOw7jlcv6mb8KfZi2CCxCqFhtIJ27yZwf2Lk8QhBtMsLAk4X/qoqlBu5QTlYfGeSanYf O9Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZY3DOSR5; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel+bounces-1365-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1365-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s3-20020a17090a948300b0028ae97e2467si4627625pjo.31.2023.12.15.09.13.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 09:13:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1365-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZY3DOSR5; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel+bounces-1365-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1365-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8E3DDB22AFE for ; Fri, 15 Dec 2023 17:11:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 580A741876; Fri, 15 Dec 2023 17:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZY3DOSR5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uwH1vhsj" X-Original-To: linux-kernel@vger.kernel.org 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 854E03FE2A; Fri, 15 Dec 2023 17:10:34 +0000 (UTC) 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=1702660232; 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=4hXWC+KQ48HdWABG8tICGwil7n9P1nfuL/9s+0V/nEk=; b=ZY3DOSR51N4hU9NBb4MoCr/OWzpWMWXwCQFexNPapBTZqqNF2KIIZRFSuRTUGfddjVU+qB k7DMO7EfFAkOfhGSKD4sey6cj6mYZ1Mj/Q8o2c/bJH+yG0FUIhjDluzJq8GG+jZE/CW+Hr coXQDRnc/TV+Bvi+34b4Eef8lTMtevJfed6q/fWlGoC2fUzEHJWvineyRtJG3PBZteochR N5plHUqHizXQz7z8I4lq+Qf4H0JW83hrHsg88ybxsssLJPIMuzgr9cbfsLEj+WIeqFtF68 jGXN1AVYdk+p0oJ6kiGF7anFUH7Iz2siXXI0N/oJgf6Tv58rjSIWhPBQRWXxsg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1702660232; 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=4hXWC+KQ48HdWABG8tICGwil7n9P1nfuL/9s+0V/nEk=; b=uwH1vhsj09Tb2R70oCOs1K5DC7+atNXD5r4Ii29g2VFnHjntTLEOaITCNiXdxZBKy4oRe4 DjOHaUfmhOrgUKBw== To: linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "David S. Miller" , 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 net-next 02/24] locking/local_lock: Add local nested BH locking infrastructure. Date: Fri, 15 Dec 2023 18:07:21 +0100 Message-ID: <20231215171020.687342-3-bigeasy@linutronix.de> In-Reply-To: <20231215171020.687342-1-bigeasy@linutronix.de> References: <20231215171020.687342-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. 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 706c4b65d9449..303caac7fbbbc 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, 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_LOCK_GUARD_1(local_lock_nested_bh, local_lock_t, + local_lock_nested_bh(_T->lock), + local_unlock_nested_bh(_T->lock)) + #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 dc2844b071c2c..881732fa0df8c 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -657,6 +657,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) @@ -670,6 +672,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.43.0