Received: by 2002:a25:ce4d:0:0:0:0:0 with SMTP id x74csp11853ybe; Mon, 20 Jul 2020 08:56:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwg6tNInkMQtLUX9yCZb+ciIPbpftpg5pRwFhdOaevAe1PR9dhnBHObOLrYaWw9iosDxVrj X-Received: by 2002:a17:906:269a:: with SMTP id t26mr3844526ejc.286.1595260608632; Mon, 20 Jul 2020 08:56:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595260608; cv=none; d=google.com; s=arc-20160816; b=cfqSq8Tjg2ng5yX30DQxCoCGoZw3n3uhkk4/pk+FMlXOsgk+BWKAxO+PW7W8qqlAhG wDoOf7dWZ9dYA8/4imaNdJwv57RJ3eiMYnFmUiDwm9SzfMwUQrYzESnF/BP35nWmKBMf 2Xh+R/zwQV3in6aWaNpc6G1Cedm3rXk+INrYqUOqPAtLYdICZUur2JbOV+qEXtLdEApU 8l3ssnvQ25/7nqyweGCY5TKChqOEyt/+ongD+rd5J0GNM6mVPpBsHf7W7BekhZCCbTbu AO+AgiwJH3M0ZLPzV/5cuarJXRGcJnNxuEn2pawMWHSCZ2ADY5hdNG4zDHHiGnxnKxUB O+aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=fuyy0zLiX9oqGaNGjgjh+XXT41N87KSvgPNDfMsD0N4=; b=jY7FxYlCw+RHupfzG9wyhqvWt/QPqCBPzFXwbpL9p8LVOSZC00x5xHB2g7HtrVXDB9 q5CcgZ8haZWwTcK9tvFwawfymcbOuWMitkJqMmwUI5Ewaa2LjlHs2CO0uA9IXgUi0oLc O/d9n58Bh+/JooKO1JK68ebTnP+g/H3i4fX5l92VhqGEG3qCaDQHFU0FjO45WoxAiJW/ F8ykx0epybtXeUfdA+8NMpg3TWX8i9l8SjOehMowOO28OOcb6eSw/dxaROTSZ5dl+R68 +nQrK0OPprgxl+1Ho1YLITijz3aiCs5lt/vbFbZMBEGbDg/ASwQx89TRtLY1K0nMn4OP Np8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZecZm1YU; dkim=neutral (no key) header.i=@vger.kernel.org header.s=2020e header.b=ohg76ZgG; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bt2si10987652ejb.597.2020.07.20.08.56.24; Mon, 20 Jul 2020 08:56:48 -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=@linutronix.de header.s=2020 header.b=ZecZm1YU; dkim=neutral (no key) header.i=@vger.kernel.org header.s=2020e header.b=ohg76ZgG; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731539AbgGTP4S (ORCPT + 99 others); Mon, 20 Jul 2020 11:56:18 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:58762 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730307AbgGTP4H (ORCPT ); Mon, 20 Jul 2020 11:56:07 -0400 From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1595260565; 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=fuyy0zLiX9oqGaNGjgjh+XXT41N87KSvgPNDfMsD0N4=; b=ZecZm1YU70HSXnGpMlZe9VTWjCaEHdsnteUyILUtdbVSezQ/LavSa+TajTUy5KRZ9C3onc hMDNuKBipOpId9Jil8SHeSTr0qElVmULMjmmkQ+IjmE3pn5Dg0j/K9VTlhchhm9Wl4tXCd v22Txi3y+7yNBG9ASOHc4xJ9TR6SJ5agxXBdTEaSlYx8/FynKdBS2KWsOE7Om748qruIak py3DwiZwK/fl3WhWeLdLtiYdOyWFDg9kA0cc55hVYMTeKHm7ZxgZrvxddzWIa3XcKbNa3P p33KscI4xTzQEmdZKq757dpfWdo8Sc9YHbHYEsdq04rEDZOpYfdnWqRc/iXoxQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1595260565; 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=fuyy0zLiX9oqGaNGjgjh+XXT41N87KSvgPNDfMsD0N4=; b=ohg76ZgGDwrBYFBW0kVzECDbTJf5R9GJy9xLHaOPNpUmMnY4vYlkCahnBj5+50STdHp1Va q3TWG4k4jOMYpECQ== To: Peter Zijlstra , Ingo Molnar , Will Deacon Cc: Thomas Gleixner , "Paul E. McKenney" , "Sebastian A. Siewior" , Steven Rostedt , LKML , "Ahmed S. Darwish" Subject: [PATCH v4 07/24] lockdep: Add preemption enabled/disabled assertion APIs Date: Mon, 20 Jul 2020 17:55:13 +0200 Message-Id: <20200720155530.1173732-8-a.darwish@linutronix.de> In-Reply-To: <20200720155530.1173732-1-a.darwish@linutronix.de> References: <20200519214547.352050-1-a.darwish@linutronix.de> <20200720155530.1173732-1-a.darwish@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Asserting that preemption is enabled or disabled is a critical sanity check. Developers are usually reluctant to add such a check in a fastpath as reading the preemption count can be costly. Extend the lockdep API with macros asserting that preemption is disabled or enabled. If lockdep is disabled, or if the underlying architecture does not support kernel preemption, this assert has no runtime overhead. References: f54bb2ec02c8 ("locking/lockdep: Add IRQs disabled/enabled assertion APIs: ...") Signed-off-by: Ahmed S. Darwish --- include/linux/lockdep.h | 19 +++++++++++++++++++ lib/Kconfig.debug | 1 + 2 files changed, 20 insertions(+) diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h index 7aafba0ddcf9..39a35699d0d6 100644 --- a/include/linux/lockdep.h +++ b/include/linux/lockdep.h @@ -549,6 +549,22 @@ do { \ WARN_ON_ONCE(debug_locks && !this_cpu_read(hardirq_context)); \ } while (0) +#define lockdep_assert_preemption_enabled() \ +do { \ + WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \ + debug_locks && \ + (preempt_count() != 0 || \ + !this_cpu_read(hardirqs_enabled))); \ +} while (0) + +#define lockdep_assert_preemption_disabled() \ +do { \ + WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \ + debug_locks && \ + (preempt_count() == 0 && \ + this_cpu_read(hardirqs_enabled))); \ +} while (0) + #else # define might_lock(lock) do { } while (0) # define might_lock_read(lock) do { } while (0) @@ -557,6 +573,9 @@ do { \ # define lockdep_assert_irqs_enabled() do { } while (0) # define lockdep_assert_irqs_disabled() do { } while (0) # define lockdep_assert_in_irq() do { } while (0) + +# define lockdep_assert_preemption_enabled() do { } while (0) +# define lockdep_assert_preemption_disabled() do { } while (0) #endif #ifdef CONFIG_PROVE_RAW_LOCK_NESTING diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 9ad9210d70a1..5379931ba3b5 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1117,6 +1117,7 @@ config PROVE_LOCKING select DEBUG_RWSEMS select DEBUG_WW_MUTEX_SLOWPATH select DEBUG_LOCK_ALLOC + select PREEMPT_COUNT if !ARCH_NO_PREEMPT select TRACE_IRQFLAGS default n help -- 2.20.1