Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp852991pxk; Thu, 17 Sep 2020 19:07:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9jaTxOn+Yy99zKx+vWsVJmR9cGgyTZaD+udMy7BGzcCY7Hi5L3oz3gL89kLNA/G6GQL8D X-Received: by 2002:a05:6402:1386:: with SMTP id b6mr36032258edv.296.1600394844484; Thu, 17 Sep 2020 19:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600394844; cv=none; d=google.com; s=arc-20160816; b=J6KnE/hkoAz6H1E4ZT70HbHIoMMGL0iyS6pJYH38+zeXVLgeezVfvqIpmSqhPLEWWv uq7ZLJ7XvADjw5XfB4WQIl9Nepp2wYYg4X6KW6/hjAwJInKdFJyC5F9WlUIvh7CBQlrP FmUQ5YRiFcS17D7HaEz8kcI5wfgsGFS826Gg24BAbwbAhPFsEs9uIR2VDOXmRe8hS9U8 Sfpd3JMASN/PIK4aMvH68ScONT3CmzHYEcjcnDm1+22S4wwjda/xJz43sGBmtoyhKDyc DnjZwH2m6N2MZsoXi2vkKA3NQLgmeyYVLkbvU3hK+Q4YGpHnMUz2bM2r1zWAKL0A7nY0 hPdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=52d5awlvAF4pICoySffzpIEaYqMXxilbCEIQs8Odn90=; b=0p5e9i5iruc1WG4VKMwWqIoF9yNWv4r33VluQeIkJW70rDJtCV0VtoBAQW8g1dgdQk PDzyUYLfVzq2Cc1C+W0/cLLdwTuK4s5UHfovAzDvGoCqJSYKzSKvFtpSKTylWH2oeOwJ XjI/Q7wNA3vJvZ1C49wzaHT5XCSljM7RZI+IC4gkCO5DSX4BwxOn3TkIYm3BbCbafNpB Kgo8QhmngVl5hGPoQdpwTql0eIQaq3HUZ4uBS8Hprl9WkaFYLx+dKJXv8U7FE7HbuvyX hxBn5QQptTvAKuXfPf1vIEpHAZ4LuIXpl6sAzLHRVgKPeJFOhgL4OrNIoko2/kbrmjUD v7UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=WelqxrFK; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z12si1270398edm.560.2020.09.17.19.07.01; Thu, 17 Sep 2020 19:07:24 -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=@kernel.org header.s=default header.b=WelqxrFK; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727175AbgIRCDv (ORCPT + 99 others); Thu, 17 Sep 2020 22:03:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:50402 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727146AbgIRCDq (ORCPT ); Thu, 17 Sep 2020 22:03:46 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 01D23208DB; Fri, 18 Sep 2020 02:03:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394625; bh=g7DBNqWBuOKWr4X+V2/6xXBxhvc+dSxZRRcjZFFx+iM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WelqxrFKo8Dl8Arz0yt1KsxhNebDsuuiEH3xFhOe5AYarrrW/51FAjTwb8hgkrDKC 3MFFss1+80cLzekulwJDiFdlqP5R5d05Y1BiEAVgv4MfhMLugdMJOba2TvtCaQDsrA H6uk17ucuyNY+dplQeCdI/hsTid9vmJLuN20Uy9A= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Waiman Long , Peter Zijlstra , Ingo Molnar , Sasha Levin Subject: [PATCH AUTOSEL 5.4 126/330] locking/lockdep: Decrement IRQ context counters when removing lock chain Date: Thu, 17 Sep 2020 21:57:46 -0400 Message-Id: <20200918020110.2063155-126-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Waiman Long [ Upstream commit b3b9c187dc2544923a601733a85352b9ddaba9b3 ] There are currently three counters to track the IRQ context of a lock chain - nr_hardirq_chains, nr_softirq_chains and nr_process_chains. They are incremented when a new lock chain is added, but they are not decremented when a lock chain is removed. That causes some of the statistic counts reported by /proc/lockdep_stats to be incorrect. IRQ Fix that by decrementing the right counter when a lock chain is removed. Since inc_chains() no longer accesses hardirq_context and softirq_context directly, it is moved out from the CONFIG_TRACE_IRQFLAGS conditional compilation block. Fixes: a0b0fd53e1e6 ("locking/lockdep: Free lock classes that are no longer in use") Signed-off-by: Waiman Long Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Link: https://lkml.kernel.org/r/20200206152408.24165-2-longman@redhat.com Signed-off-by: Sasha Levin --- kernel/locking/lockdep.c | 40 +++++++++++++++++------------- kernel/locking/lockdep_internals.h | 6 +++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 9ab1a965c3b92..bca0f7f71cde4 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -2302,18 +2302,6 @@ static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, return 0; } -static void inc_chains(void) -{ - if (current->hardirq_context) - nr_hardirq_chains++; - else { - if (current->softirq_context) - nr_softirq_chains++; - else - nr_process_chains++; - } -} - #else static inline int check_irq_usage(struct task_struct *curr, @@ -2321,13 +2309,27 @@ static inline int check_irq_usage(struct task_struct *curr, { return 1; } +#endif /* CONFIG_TRACE_IRQFLAGS */ -static inline void inc_chains(void) +static void inc_chains(int irq_context) { - nr_process_chains++; + if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT) + nr_hardirq_chains++; + else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT) + nr_softirq_chains++; + else + nr_process_chains++; } -#endif /* CONFIG_TRACE_IRQFLAGS */ +static void dec_chains(int irq_context) +{ + if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT) + nr_hardirq_chains--; + else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT) + nr_softirq_chains--; + else + nr_process_chains--; +} static void print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv) @@ -2847,7 +2849,7 @@ static inline int add_chain_cache(struct task_struct *curr, hlist_add_head_rcu(&chain->entry, hash_head); debug_atomic_inc(chain_lookup_misses); - inc_chains(); + inc_chains(chain->irq_context); return 1; } @@ -3600,7 +3602,8 @@ lock_used: static inline unsigned int task_irq_context(struct task_struct *task) { - return 2 * !!task->hardirq_context + !!task->softirq_context; + return LOCK_CHAIN_HARDIRQ_CONTEXT * !!task->hardirq_context + + LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context; } static int separate_irq_context(struct task_struct *curr, @@ -4805,6 +4808,8 @@ recalc: return; /* Overwrite the chain key for concurrent RCU readers. */ WRITE_ONCE(chain->chain_key, chain_key); + dec_chains(chain->irq_context); + /* * Note: calling hlist_del_rcu() from inside a * hlist_for_each_entry_rcu() loop is safe. @@ -4826,6 +4831,7 @@ recalc: } *new_chain = *chain; hlist_add_head_rcu(&new_chain->entry, chainhashentry(chain_key)); + inc_chains(new_chain->irq_context); #endif } diff --git a/kernel/locking/lockdep_internals.h b/kernel/locking/lockdep_internals.h index 18d85aebbb57f..a525368b8cf61 100644 --- a/kernel/locking/lockdep_internals.h +++ b/kernel/locking/lockdep_internals.h @@ -106,6 +106,12 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ = #define STACK_TRACE_HASH_SIZE 16384 #endif +/* + * Bit definitions for lock_chain.irq_context + */ +#define LOCK_CHAIN_SOFTIRQ_CONTEXT (1 << 0) +#define LOCK_CHAIN_HARDIRQ_CONTEXT (1 << 1) + #define MAX_LOCKDEP_CHAINS (1UL << MAX_LOCKDEP_CHAINS_BITS) #define MAX_LOCKDEP_CHAIN_HLOCKS (MAX_LOCKDEP_CHAINS*5) -- 2.25.1