Message ID | 20240204145154.11069-1-yaoma@linux.alibaba.com |
---|---|
Headers |
Return-Path: <linux-kernel+bounces-51731-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp387390dyb; Sun, 4 Feb 2024 06:52:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IGF9D8k6hdIYgPzSOg4RcdXRp4Rw90DUn8zZFUWo6PqDRFDFMZsnW1MXzDTbs6GW38gTlMw X-Received: by 2002:a17:906:31cc:b0:a37:7057:5b3c with SMTP id f12-20020a17090631cc00b00a3770575b3cmr1374920ejf.50.1707058359097; Sun, 04 Feb 2024 06:52:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707058359; cv=pass; d=google.com; s=arc-20160816; b=YY4SDC3DqrhtlHrDlsyyWI6stkN0L5rfREgxJ/szcKmIf6iN+6WifCEQbpqAfPSIhW cfvuguRqEY+0naE2zMAHbHDCtaHiSpZlQMH36woZQglrCklBH7Xeb5oABtXCS3snzQqq m7b1C7VJSsSQj7JVuBlGfrElB+bF5bxTgdDXbn0mIDDuWppJMvrsyVnXwqlLRQ/7GZtI 9NDPZeMpvY4JZoH/9aqGN+PuVS4+ah2u1gJbHVSb7crXMzqW9BuwDznSXzB6vf3K/PYj mbZ7+YTzke6Pu5nW+QTgs9ZgiMIs4G/Gb0b09iWkq1sAyDXZ18/iu/SOcCfH2NolK1z+ d4qQ== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=Qvz8jSNlW0u04VWtHyitrAixmhwD3hvNY7pKyKG/fR0=; fh=Dz9HkuhCnUoOQK2t2IjqD02ofAt0RIIDloTkkc5+/lg=; b=cos/zojG6S2J1saqu2eA0MHcDEJJsSNmlZMOllGHCi+/IlFm4ud6wRfZgQIr02F7pp X5R4K09lrkqPFImZs19PY59U8eGckIgWaoJrJ8QPVk70Asn7iZxV6ltnQwC7FNyiTWqQ h/RmIJY9WwEMm86DkyUxwOBEwDduhYSq72++ZQE3+8d9Xxnyk93K0L0SSm5nv4k6dnH9 6P04QgdI1jUvIAiRyv5X6CJLJPio6tYzDmGzfZw1nZv1TD5UZFaWnGsZIcNVF86TZ2nB OHYCquycvCx12squlB9J300a+jqOKp03xsquSZfyBLDc2BgcE2KzO2Wc9aB6EhcBUcKB iWNw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=TXX8Cq8F; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-51731-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51731-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com X-Forwarded-Encrypted: i=1; AJvYcCWfZ5bYHp7cqavrFn/DJvU/2QIigKGJmpxKV9J9spUGtzfsLby5HpvJirLGZwu2apyHNZLbxUfdYNPuVmd3/sI+78KpAw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s18-20020a170906bc5200b00a36b79090cesi2945717ejv.81.2024.02.04.06.52.38 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Feb 2024 06:52:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-51731-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.alibaba.com header.s=default header.b=TXX8Cq8F; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dkim=pass dkdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-51731-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-51731-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.alibaba.com 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 9ACE91F22256 for <ouuuleilei@gmail.com>; Sun, 4 Feb 2024 14:52:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD3AB2260B; Sun, 4 Feb 2024 14:52:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="TXX8Cq8F" Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (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 25B2122307 for <linux-kernel@vger.kernel.org>; Sun, 4 Feb 2024 14:52:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707058329; cv=none; b=L6kZW7fKgIi6WUFpFAZomBzatNcHD5Zq1y0YA7tRiNfdjWOYgNilp+FmXGre/RLzvSGBZY/FGP9X7fvVWgKIfi85gsBE2J44vuIvVi8ywTe1a627YS+ndlC/oT57WlX7SF1dRv4We1PPBHYWnfHHQ4j+ZLNru0MAiBpoaOVYliw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707058329; c=relaxed/simple; bh=0JcXOqJwgvaXB3q5MnlBmpdl74RAVdFizWUXVAVwj+4=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=BJoccEoZ90fEhXO4qQS/tfeYOwhHB9nm6illtRBucOW7N1vP/aHzVw4oBBug5JDwzNaXjVbqXm5ARE6DLK+YWJBheN9xfe3oz24BmT2fovM+35fAubEabVt2HvnJ2gHqOxkWRp8FSwP+GbldATmWqC3RBfxIh2EZjrf1egJtTVE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=TXX8Cq8F; arc=none smtp.client-ip=115.124.30.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1707058319; h=From:To:Subject:Date:Message-Id:MIME-Version:Content-Type; bh=Qvz8jSNlW0u04VWtHyitrAixmhwD3hvNY7pKyKG/fR0=; b=TXX8Cq8F8NkvI5mKGUIA5HisvdVDFtzstalTSNCNFgMl2tEwhdLq8Iehuaatgjckrv3XA31Xuk6VGsvMI1x/RzQsvgqqhf0EXU82ovyD+E90MQjmhdjcaQtaIy2/pcu7c8DhuqfSD9Lz8YNQa/iCwKDMVUMvVcgyxm4cwRpyvr4= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045170;MF=yaoma@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0W00xOyM_1707058317; Received: from localhost.localdomain(mailfrom:yaoma@linux.alibaba.com fp:SMTPD_---0W00xOyM_1707058317) by smtp.aliyun-inc.com; Sun, 04 Feb 2024 22:51:59 +0800 From: Bitao Hu <yaoma@linux.alibaba.com> To: dianders@chromium.org, akpm@linux-foundation.org, pmladek@suse.com, kernelfans@gmail.com, liusong@linux.alibaba.com Cc: linux-kernel@vger.kernel.org, yaoma@linux.alibaba.com Subject: [PATCHv4 0/3] *** Detect interrupt storm in softlockup *** Date: Sun, 4 Feb 2024 22:51:51 +0800 Message-Id: <20240204145154.11069-1-yaoma@linux.alibaba.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789980426041174049 X-GMAIL-MSGID: 1789980426041174049 |
Series |
*** Detect interrupt storm in softlockup ***
|
|
Message
Bitao Hu
Feb. 4, 2024, 2:51 p.m. UTC
Hi, guys. I have implemented a low-overhead method for detecting interrupt storm in softlockup. Please review it, all comments are welcome. Changes from v3 to v4: - Renaming some variable and function names to make the code logic more readable. - Change the code location to avoid predeclaring. - Just swap rather than a double loop in tabulate_irq_count. - Since nr_irqs has the potential to grow at runtime, bounds-check logic has been implemented. - Add SOFTLOCKUP_DETECTOR_INTR_STORM Kconfig knob. Changes from v2 to v3: - From Liu Song, using enum instead of macro for cpu_stats, shortening the name 'idx_to_stat' to 'stats', adding 'get_16bit_precesion' instead of using right shift operations, and using 'struct irq_counts'. - From kernel robot test, using '__this_cpu_read' and '__this_cpu_write' instead of accessing to an per-cpu array directly, in order to avoid this warning. 'sparse: incorrect type in initializer (different modifiers)' Changes from v1 to v2: - From Douglas, optimize the memory of cpustats. With the maximum number of CPUs, that's now this. 2 * 8192 * 4 + 1 * 8192 * 5 * 4 + 1 * 8192 = 237,568 bytes. - From Liu Song, refactor the code format and add necessary comments. - From Douglas, use interrupt counts instead of interrupt time to determine the cause of softlockup. - Remove the cmdline parameter added in PATCHv1. Bitao Hu (3): watchdog/softlockup: low-overhead detection of interrupt watchdog/softlockup: report the most frequent interrupts watchdog/softlockup: add SOFTLOCKUP_DETECTOR_INTR_STORM Kconfig knob kernel/watchdog.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 13 +++ 2 files changed, 260 insertions(+)
Comments
在 2024/2/4 22:51, Bitao Hu 写道: > When the watchdog determines that the current soft lockup is due > to an interrupt storm based on CPU utilization, reporting the > most frequent interrupts could be good enough for further > troubleshooting. > > Below is an example of interrupt storm. The call tree does not > provide useful information, but we can analyze which interrupt > caused the soft lockup by comparing the counts of interrupts. > > [ 2987.488075] watchdog: BUG: soft lockup - CPU#9 stuck for 23s! [kworker/9:1:214] > [ 2987.488607] CPU#9 Utilization every 4s during lockup: > [ 2987.488941] #1: 0% system, 0% softirq, 100% hardirq, 0% idle > [ 2987.489357] #2: 0% system, 0% softirq, 100% hardirq, 0% idle > [ 2987.489771] #3: 0% system, 0% softirq, 100% hardirq, 0% idle > [ 2987.490186] #4: 0% system, 0% softirq, 100% hardirq, 0% idle > [ 2987.490601] #5: 0% system, 0% softirq, 100% hardirq, 0% idle > [ 2987.491034] CPU#9 Detect HardIRQ Time exceeds 50%. Most frequent HardIRQs: > [ 2987.491493] #1: 330985 irq#7(IPI) > [ 2987.491743] #2: 5000 irq#10(arch_timer) > [ 2987.492039] #3: 9 irq#91(nvme0q2) > [ 2987.492318] #4: 3 irq#118(virtio1-output.12) > ... > [ 2987.492728] Call trace: > [ 2987.492729] __do_softirq+0xa8/0x364 > > Signed-off-by: Bitao Hu <yaoma@linux.alibaba.com> > --- > kernel/watchdog.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 156 insertions(+) > > diff --git a/kernel/watchdog.c b/kernel/watchdog.c > index 7b121e166b81..d31120c3e9d1 100644 > --- a/kernel/watchdog.c > +++ b/kernel/watchdog.c > @@ -19,6 +19,9 @@ > #include <linux/kernel_stat.h> > #include <linux/math64.h> > #include <linux/module.h> > +#include <linux/irq.h> > +#include <linux/irqdesc.h> > +#include <linux/bitops.h> > #include <linux/sysctl.h> > #include <linux/tick.h> > #include <linux/sched/clock.h> > @@ -412,13 +415,153 @@ static void print_cpustat(void) > } > } > > +#define HARDIRQ_PERCENT_THRESH 50 > +#define NUM_HARDIRQ_REPORT 5 > +static DECLARE_BITMAP(softlockup_hardirq_cpus, CONFIG_NR_CPUS); > +static DEFINE_PER_CPU(u32 *, hardirq_counts); > +static DEFINE_PER_CPU(int, actual_nr_irqs); > +struct irq_counts { > + int irq; > + u32 counts; > +}; > + > +/* Tabulate the most frequent interrupts. */ > +static void tabulate_irq_count(struct irq_counts *irq_counts, int irq, u32 counts, int rank) > +{ > + int i; > + struct irq_counts new_count = {irq, counts}; > + > + for (i = 0; i < rank; i++) { > + if (counts > irq_counts[i].counts) > + swap(new_count, irq_counts[i]); > + } > +} > + > +/* > + * If the hardirq time exceeds HARDIRQ_PERCENT_THRESH% of the sample_period, > + * then the cause of softlockup might be interrupt storm. In this case, it > + * would be useful to start interrupt counting. > + */ > +static bool need_counting_irqs(void) > +{ > + int tail = __this_cpu_read(cpustat_tail); > + u8 util; > + > + tail = (tail + NUM_HARDIRQ_REPORT - 1) % NUM_HARDIRQ_REPORT; > + util = __this_cpu_read(cpustat_util[tail][STATS_HARDIRQ]); > + return util > HARDIRQ_PERCENT_THRESH; > +} > + > +static void start_counting_irqs(void) > +{ > + int i; > + u32 *counts = __this_cpu_read(hardirq_counts); > + int cpu = smp_processor_id(); > + struct irq_desc *desc; > + > + if (!test_bit(cpu, softlockup_hardirq_cpus)) { > + /* > + * nr_irqs has the potential to grow at runtime. We should read > + * it and store locally to avoid array out-of-bounds access. > + */ > + __this_cpu_write(actual_nr_irqs, nr_irqs); > + counts = kmalloc_array(__this_cpu_read(actual_nr_irqs), > + sizeof(u32), > + GFP_ATOMIC); > + if (!counts) > + return; > + for (i = 0; i < __this_cpu_read(actual_nr_irqs); i++) { > + desc = irq_to_desc(i); > + if (!desc) > + continue; > + counts[i] = desc->kstat_irqs ? > + *this_cpu_ptr(desc->kstat_irqs) : 0; > + } > + __this_cpu_write(hardirq_counts, counts); > + set_bit(cpu, softlockup_hardirq_cpus); > + } > +} > + > +static void stop_counting_irqs(void) > +{ > + u32 *counts = __this_cpu_read(hardirq_counts); > + int cpu = smp_processor_id(); > + > + if (test_bit(cpu, softlockup_hardirq_cpus)) { > + kfree(counts); > + counts = NULL; > + __this_cpu_write(hardirq_counts, counts); > + clear_bit(cpu, softlockup_hardirq_cpus); > + } > +} > + > +static void print_irq_counts(void) > +{ > + int i; > + struct irq_desc *desc; > + u32 counts_diff; > + u32 *counts = __this_cpu_read(hardirq_counts); > + int cpu = smp_processor_id(); > + struct irq_counts irq_counts_sorted[NUM_HARDIRQ_REPORT] = { > + {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, > + }; > + > + if (test_bit(cpu, softlockup_hardirq_cpus)) { > + for_each_irq_desc(i, desc) { > + if (!desc) > + continue; > + /* > + * We need to bounds-check in case someone on a different CPU > + * expanded nr_irqs. > + */ > + if (i < __this_cpu_read(actual_nr_irqs)) > + counts_diff = desc->kstat_irqs ? > + *this_cpu_ptr(desc->kstat_irqs) - counts[i] : 0; > + else > + counts_diff = desc->kstat_irqs ? > + *this_cpu_ptr(desc->kstat_irqs) : 0; > + tabulate_irq_count(irq_counts_sorted, i, counts_diff, > + NUM_HARDIRQ_REPORT); > + } > + /* > + * We do not want the "watchdog: " prefix on every line, > + * hence we use "printk" instead of "pr_crit". > + */ > + printk(KERN_CRIT "CPU#%d Detect HardIRQ Time exceeds %d%%. Most frequent HardIRQs:\n", > + smp_processor_id(), HARDIRQ_PERCENT_THRESH); > + for (i = 0; i < NUM_HARDIRQ_REPORT; i++) { > + if (irq_counts_sorted[i].irq == -1) > + break; > + desc = irq_to_desc(irq_counts_sorted[i].irq); > + if (desc && desc->action) > + printk(KERN_CRIT "\t#%u: %-10u\tirq#%d(%s)\n", > + i+1, irq_counts_sorted[i].counts, > + irq_counts_sorted[i].irq, desc->action->name); > + else > + printk(KERN_CRIT "\t#%u: %-10u\tirq#%d\n", > + i+1, irq_counts_sorted[i].counts, > + irq_counts_sorted[i].irq); > + } > + /* > + * If the hardirq time is less than HARDIRQ_PERCENT_THRESH% in the last > + * sample_period, then we suspect the interrupt storm might be subsiding. > + */ > + if (!need_counting_irqs()) > + stop_counting_irqs(); > + } > +} > + > static void report_cpu_status(void) > { > print_cpustat(); > + print_irq_counts(); > } > #else > static inline void update_cpustat(void) { } > static inline void report_cpu_status(void) { } > +static inline bool need_counting_irqs(void) { return false; } > +static inline void start_counting_irqs(void) { } > +static inline void stop_counting_irqs(void) { } > #endif > > /* > @@ -522,6 +665,18 @@ static int is_softlockup(unsigned long touch_ts, > unsigned long now) > { > if ((watchdog_enabled & WATCHDOG_SOFTOCKUP_ENABLED) && watchdog_thresh) { > + /* > + * If period_ts has not been updated during a sample_period, then > + * in the subsequent few sample_periods, period_ts might also not > + * be updated, which could indicate a potential softlockup. In > + * this case, if we suspect the cause of the potential softlockup > + * might be interrupt storm, then we need to count the interrupts > + * to find which interrupt is storming. > + */ > + if (time_after_eq(now, period_ts + get_softlockup_thresh() / 5) && > + need_counting_irqs()) > + start_counting_irqs(); > + > /* Warn about unreasonable delays. */ > if (time_after(now, period_ts + get_softlockup_thresh())) > return now - touch_ts; > @@ -544,6 +699,7 @@ static DEFINE_PER_CPU(struct cpu_stop_work, softlockup_stop_work); > static int softlockup_fn(void *data) > { > update_touch_ts(); > + stop_counting_irqs(); > complete(this_cpu_ptr(&softlockup_completion)); > > return 0; Looks good. Here is a personal suggestion: within a function body, if there are variable declarations and declarations with assignments, it is recommended to place the declarations with assignments after those that are simply declarations, as this looks neater. Reviewed-by: Liu Song <liusong@linux.alibaba.com>