Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp215340lql; Fri, 12 Apr 2024 08:18:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVVcOWhEJSxS/CdyVv125sLLUFweSIobJoYv4rfDn3xcfrM2HGwILM3LuNEbI8Xp/MbLH4AY4W+7ZQR5Ic4/3XLeMPBbjhkAfICszfSGw== X-Google-Smtp-Source: AGHT+IEV2czWpyeHMaDLiW4j+Xve7rBNoL71yTCrUAExO2qjmaovDkgJ6IWh8TkEGuIeR8w9n27Z X-Received: by 2002:a17:90a:7781:b0:2a5:b95a:8622 with SMTP id v1-20020a17090a778100b002a5b95a8622mr3078475pjk.36.1712935108792; Fri, 12 Apr 2024 08:18:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712935108; cv=pass; d=google.com; s=arc-20160816; b=irXiEXp4h9AYZ9VU+FzrzY5R5yMAAEazw49z3dA/ljsuqnh9VpsWdGWRQVKxD6HEYX 7UlhDozN7ZyETL79O2nGnCCRo8wsE4OtFPMr8HFmz+JWEnqxGq6JxZJRHiHuuIsUDKaL cgzuOG2CwVXR27iEnYegJ256f1nwNP0Ns8gL6H8PjUSGQz1lV5QK7c5Nzxod5cJ3TGz9 D3jwt5u4aq0cJX6LKqQGK5t/SN4R2RAFf10QsJztg70SuFVlmGl5EHNVTqzxMIBVIXwC xuQr+qjaYHVI+wE8JFsr/L9MTMuNgaJgJCFIor7YBJPSWeH69cfjWTzf3Ot6z73JoV/0 mDWg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=HGrS0ep8w1RchUHEPvwc/RWL92h4nIga3/g5zkm0l5Y=; fh=F5ke6QXkG8DTssze7G7p0tXWfODuAYd4HUPkGOUNf5g=; b=q90du/3pat+HuKWYS6plX0qhJ2eh10mtfeGFDn8Y3LBGNL88W3Mlj6wZBpn6UzElrf JRKgfGNyVcJsOre8Ui2lSHbudTdwfjz6SAF4E6tW3ugSoD/0dZTBXanSHIEmC3vRIVMx a8p/n4nW35a6zwcyjeW40hD++TcW9k2AsC8ftI395Y4C52KFiaykNlOQhIq4h8CLvr9F kcwsZdvr+q5qCmcE+DW/JuheZCwavcWd/7Qk6MfpNTE+G2mcDxTaeCNKfFYbWPRMwDtg BuF/TnS7zkxY/zOxSXxVPE1nggA8ql7TjSssVsG2NtE1+MdBO5EhfVCbItsRMTtNP9yb oBqQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=kSD04Gyl; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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-142923-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142923-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ie18-20020a17090b401200b002a2874a8c2esi5522460pjb.77.2024.04.12.08.18.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 08:18:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142923-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=kSD04Gyl; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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-142923-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142923-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 6C1C7283FEA for ; Fri, 12 Apr 2024 15:18:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AD39585270; Fri, 12 Apr 2024 15:14:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kSD04Gyl"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wpRW7SMg" 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 A2CE51487D0; Fri, 12 Apr 2024 15:14:32 +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=1712934874; cv=none; b=uEv1itx7b4kRNLkhJSnZ8DWXgFH53E5ILTi/iTgUosclN04YGQbQizyIO2cLfNpq/JueYZwhF7E3qrVDnJCOqZHNGsU+l1MCJkE4VFmuQwkbH7D05yP5voP/ly0KSDSabhE3HtF9YIxCHD8onR2ARGcx+33oP35ditg0rfra2Vw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934874; c=relaxed/simple; bh=kHeilL8r9aPCtvAAX1xuOhqoKus/0tiWAAvBeA5cm9A=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=pXC1WHfVr/gkkODbdohtZPLDHAhbBSlhW5O5H+V25q7HC8xQGR175YnOB3LrTp2jFHRZdQlUAnqY4Cj8gOMUjX9E4BqPxBi03F1vTy/oWmgTQZRNQx631pEjkvuQnuKmetpl6/d9erz5j77uiiNG4LYaLW8e3IXAZ9e02B9fz3g= 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=kSD04Gyl; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wpRW7SMg; 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 Date: Fri, 12 Apr 2024 15:14:29 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712934870; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HGrS0ep8w1RchUHEPvwc/RWL92h4nIga3/g5zkm0l5Y=; b=kSD04GylaF08VAq7Ps8+fjWCXj7PVhGZ93m/svtc92gPdBMwrbb2alHxkn6h4kh0eksEZ4 TCsU9pnqLUWVZqqOC9B6bYVfh4+zyzeFafdIsPFLLleXc6hvDqFFNyxMOA88qzxIM8iWE+ +9sUfk+qf9LNoTenY17uNj76ZzTIjeEEK4VvJNoBQ3S3aB5D6FVZJGmQn2noAEdu6XzZ/6 gbNbDWx+uBPoMl4O1yWgk/EH/UVJiDhasvh09crzFcAspeXLuw6zwLd6pWRRH3Tc1liJxe 0KmC4UB9/V7xDzG2BRree6D/cHWyURK93oX2Cn2VSGhVzBeLTn0noDceDHWEOA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712934870; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HGrS0ep8w1RchUHEPvwc/RWL92h4nIga3/g5zkm0l5Y=; b=wpRW7SMgLrj5o5EEoVu4btua3514wH2+sEmnJjji9lqYCVvUJtQ2cxK43pPvlwsUEBMwS6 0Hdgdi16Lh6uhqDQ== From: "tip-bot2 for Bitao Hu" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: irq/core] watchdog/softlockup: Low-overhead detection of interrupt storm Cc: Bitao Hu , Thomas Gleixner , Douglas Anderson , Liu Song , x86@kernel.org, linux-kernel@vger.kernel.org, maz@kernel.org In-Reply-To: <20240411074134.30922-5-yaoma@linux.alibaba.com> References: <20240411074134.30922-5-yaoma@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <171293486962.10875.16894940902796299113.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit The following commit has been merged into the irq/core branch of tip: Commit-ID: d7037381d00286aa4beb631c401da761ee564c94 Gitweb: https://git.kernel.org/tip/d7037381d00286aa4beb631c401da761ee564c94 Author: Bitao Hu AuthorDate: Thu, 11 Apr 2024 15:41:33 +08:00 Committer: Thomas Gleixner CommitterDate: Fri, 12 Apr 2024 17:08:05 +02:00 watchdog/softlockup: Low-overhead detection of interrupt storm The following softlockup is caused by interrupt storm, but it cannot be identified from the call tree. Because the call tree is just a snapshot and doesn't fully capture the behavior of the CPU during the soft lockup. watchdog: BUG: soft lockup - CPU#28 stuck for 23s! [fio:83921] ... Call trace: __do_softirq+0xa0/0x37c __irq_exit_rcu+0x108/0x140 irq_exit+0x14/0x20 __handle_domain_irq+0x84/0xe0 gic_handle_irq+0x80/0x108 el0_irq_naked+0x50/0x58 Therefore, it is necessary to report CPU utilization during the softlockup_threshold period (report once every sample_period, for a total of 5 reportings), like this: watchdog: BUG: soft lockup - CPU#28 stuck for 23s! [fio:83921] CPU#28 Utilization every 4s during lockup: #1: 0% system, 0% softirq, 100% hardirq, 0% idle #2: 0% system, 0% softirq, 100% hardirq, 0% idle #3: 0% system, 0% softirq, 100% hardirq, 0% idle #4: 0% system, 0% softirq, 100% hardirq, 0% idle #5: 0% system, 0% softirq, 100% hardirq, 0% idle ... This is helpful in determining whether an interrupt storm has occurred or in identifying the cause of the softlockup. The criteria for determination are as follows: a. If the hardirq utilization is high, then interrupt storm should be considered and the root cause cannot be determined from the call tree. b. If the softirq utilization is high, then the call might not necessarily point at the root cause. c. If the system utilization is high, then analyzing the root cause from the call tree is possible in most cases. The mechanism requires a considerable amount of global storage space when configured for the maximum number of CPUs. Therefore, adding a SOFTLOCKUP_DETECTOR_INTR_STORM Kconfig knob that defaults to "yes" if the max number of CPUs is <= 128. Signed-off-by: Bitao Hu Signed-off-by: Thomas Gleixner Reviewed-by: Douglas Anderson Reviewed-by: Liu Song Link: https://lore.kernel.org/r/20240411074134.30922-5-yaoma@linux.alibaba.com --- kernel/watchdog.c | 99 ++++++++++++++++++++++++++++++++++++++++++++- lib/Kconfig.debug | 14 ++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/kernel/watchdog.c b/kernel/watchdog.c index d7b2125..ef8ebd3 100644 --- a/kernel/watchdog.c +++ b/kernel/watchdog.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include #include @@ -35,6 +37,8 @@ static DEFINE_MUTEX(watchdog_mutex); # define WATCHDOG_HARDLOCKUP_DEFAULT 0 #endif +#define NUM_SAMPLE_PERIODS 5 + unsigned long __read_mostly watchdog_enabled; int __read_mostly watchdog_user_enabled = 1; static int __read_mostly watchdog_hardlockup_user_enabled = WATCHDOG_HARDLOCKUP_DEFAULT; @@ -333,6 +337,96 @@ __setup("watchdog_thresh=", watchdog_thresh_setup); static void __lockup_detector_cleanup(void); +#ifdef CONFIG_SOFTLOCKUP_DETECTOR_INTR_STORM +enum stats_per_group { + STATS_SYSTEM, + STATS_SOFTIRQ, + STATS_HARDIRQ, + STATS_IDLE, + NUM_STATS_PER_GROUP, +}; + +static const enum cpu_usage_stat tracked_stats[NUM_STATS_PER_GROUP] = { + CPUTIME_SYSTEM, + CPUTIME_SOFTIRQ, + CPUTIME_IRQ, + CPUTIME_IDLE, +}; + +static DEFINE_PER_CPU(u16, cpustat_old[NUM_STATS_PER_GROUP]); +static DEFINE_PER_CPU(u8, cpustat_util[NUM_SAMPLE_PERIODS][NUM_STATS_PER_GROUP]); +static DEFINE_PER_CPU(u8, cpustat_tail); + +/* + * We don't need nanosecond resolution. A granularity of 16ms is + * sufficient for our precision, allowing us to use u16 to store + * cpustats, which will roll over roughly every ~1000 seconds. + * 2^24 ~= 16 * 10^6 + */ +static u16 get_16bit_precision(u64 data_ns) +{ + return data_ns >> 24LL; /* 2^24ns ~= 16.8ms */ +} + +static void update_cpustat(void) +{ + int i; + u8 util; + u16 old_stat, new_stat; + struct kernel_cpustat kcpustat; + u64 *cpustat = kcpustat.cpustat; + u8 tail = __this_cpu_read(cpustat_tail); + u16 sample_period_16 = get_16bit_precision(sample_period); + + kcpustat_cpu_fetch(&kcpustat, smp_processor_id()); + + for (i = 0; i < NUM_STATS_PER_GROUP; i++) { + old_stat = __this_cpu_read(cpustat_old[i]); + new_stat = get_16bit_precision(cpustat[tracked_stats[i]]); + util = DIV_ROUND_UP(100 * (new_stat - old_stat), sample_period_16); + __this_cpu_write(cpustat_util[tail][i], util); + __this_cpu_write(cpustat_old[i], new_stat); + } + + __this_cpu_write(cpustat_tail, (tail + 1) % NUM_SAMPLE_PERIODS); +} + +static void print_cpustat(void) +{ + int i, group; + u8 tail = __this_cpu_read(cpustat_tail); + u64 sample_period_second = sample_period; + + do_div(sample_period_second, NSEC_PER_SEC); + + /* + * Outputting the "watchdog" prefix on every line is redundant and not + * concise, and the original alarm information is sufficient for + * positioning in logs, hence here printk() is used instead of pr_crit(). + */ + printk(KERN_CRIT "CPU#%d Utilization every %llus during lockup:\n", + smp_processor_id(), sample_period_second); + + for (i = 0; i < NUM_SAMPLE_PERIODS; i++) { + group = (tail + i) % NUM_SAMPLE_PERIODS; + printk(KERN_CRIT "\t#%d: %3u%% system,\t%3u%% softirq,\t" + "%3u%% hardirq,\t%3u%% idle\n", i + 1, + __this_cpu_read(cpustat_util[group][STATS_SYSTEM]), + __this_cpu_read(cpustat_util[group][STATS_SOFTIRQ]), + __this_cpu_read(cpustat_util[group][STATS_HARDIRQ]), + __this_cpu_read(cpustat_util[group][STATS_IDLE])); + } +} + +static void report_cpu_status(void) +{ + print_cpustat(); +} +#else +static inline void update_cpustat(void) { } +static inline void report_cpu_status(void) { } +#endif + /* * Hard-lockup warnings should be triggered after just a few seconds. Soft- * lockups can have false positives under extreme conditions. So we generally @@ -364,7 +458,7 @@ static void set_sample_period(void) * and hard thresholds) to increment before the * hardlockup detector generates a warning */ - sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5); + sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / NUM_SAMPLE_PERIODS); watchdog_update_hrtimer_threshold(sample_period); } @@ -504,6 +598,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) */ period_ts = READ_ONCE(*this_cpu_ptr(&watchdog_report_ts)); + update_cpustat(); + /* Reset the interval when touched by known problematic code. */ if (period_ts == SOFTLOCKUP_DELAY_REPORT) { if (unlikely(__this_cpu_read(softlockup_touch_sync))) { @@ -539,6 +635,7 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer) pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", smp_processor_id(), duration, current->comm, task_pid_nr(current)); + report_cpu_status(); print_modules(); print_irqtrace_events(current); if (regs) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index c63a5fb..1f0ce71 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1029,6 +1029,20 @@ config SOFTLOCKUP_DETECTOR chance to run. The current stack trace is displayed upon detection and the system will stay locked up. +config SOFTLOCKUP_DETECTOR_INTR_STORM + bool "Detect Interrupt Storm in Soft Lockups" + depends on SOFTLOCKUP_DETECTOR && IRQ_TIME_ACCOUNTING + select GENERIC_IRQ_STAT_SNAPSHOT + default y if NR_CPUS <= 128 + help + Say Y here to enable the kernel to detect interrupt storm + during "soft lockups". + + "soft lockups" can be caused by a variety of reasons. If one is + caused by an interrupt storm, then the storming interrupts will not + be on the callstack. To detect this case, it is necessary to report + the CPU stats and the interrupt counts during the "soft lockups". + config BOOTPARAM_SOFTLOCKUP_PANIC bool "Panic (Reboot) On Soft Lockups" depends on SOFTLOCKUP_DETECTOR