From patchwork Mon Jun 12 07:26:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Whitchurch X-Patchwork-Id: 13275720 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DA24C7EE2E for ; Mon, 12 Jun 2023 07:26:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6C298E0002; Mon, 12 Jun 2023 03:26:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1B966B0074; Mon, 12 Jun 2023 03:26:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0C598E0002; Mon, 12 Jun 2023 03:26:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 92FC76B0072 for ; Mon, 12 Jun 2023 03:26:58 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 653CBA012E for ; Mon, 12 Jun 2023 07:26:58 +0000 (UTC) X-FDA: 80893264116.21.C79575E Received: from smtp1.axis.com (smtp1.axis.com [195.60.68.17]) by imf28.hostedemail.com (Postfix) with ESMTP id 0B1B4C000A for ; Mon, 12 Jun 2023 07:26:55 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=axis.com header.s=axis-central1 header.b=HK1KUxK1; dmarc=pass (policy=none) header.from=axis.com; spf=pass (imf28.hostedemail.com: domain of Vincent.Whitchurch@axis.com designates 195.60.68.17 as permitted sender) smtp.mailfrom=Vincent.Whitchurch@axis.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686554816; h=from:from:sender: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: references:dkim-signature; bh=GQRdgktJSgoBra8xnN5+347fk+XcPhSW44tQp4xuYhs=; b=TU3wdklvlzb9GoUF9+VZrkeyy0u1D9wnDKb4HbseaWS3FrpOH1dKC/LZ7KSLhM9iOWouC6 y95dZg5eOQl6zHvJ0RO/+blXmxWDkr3JbFWnR2Oxzxp7gCvnvAfgeSvi0v7wkf7NoinOYE 196KHcm9Vib6unW5XX3V/d1n/TZkJFI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=axis.com header.s=axis-central1 header.b=HK1KUxK1; dmarc=pass (policy=none) header.from=axis.com; spf=pass (imf28.hostedemail.com: domain of Vincent.Whitchurch@axis.com designates 195.60.68.17 as permitted sender) smtp.mailfrom=Vincent.Whitchurch@axis.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686554816; a=rsa-sha256; cv=none; b=LYIAI0rbWu45I97qCYhfEVr4xHrB2zeOUrB4ilJbOR/FLb29pKgueWVHq/b5Wm2oij79EJ +RDyL1ejdUxQWL7c0D+2Kw0nIfnuMxMgWVt9+D7WAWNrcKhjqmlPWPMOQEcybRoaxwni3+ Qt7Xk+YylNtPvDr1NadNoUxydKv+A9g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1686554816; x=1718090816; h=from:date:subject:mime-version:content-transfer-encoding: message-id:to:cc; bh=GQRdgktJSgoBra8xnN5+347fk+XcPhSW44tQp4xuYhs=; b=HK1KUxK10RBoSyFHJDZHKfuC9DFKKqBqwFb7SIU82ZtoEM4EX3pHaUFp 4Fv94AqvlpoTyfxyDWMsiyZR+U4ZzDwDO3ZaLFc88bwh7FEuVKMIwpwO7 rVjTj3vRzK/XrrNK2yacUm3x7pX4uoBl01antADyU4uOnjGdAV2XdlWxW WBUZWYQUYpGlO4/EC8sn2bRK7+HEvE1zHjSaLMosELsu1ThjrU/VEX0S6 qUMR8wxZDD3zTYcra3rMxlKG5foffjKp/YVtOiSmKyNiQKOVFaWysDjsc 9tu+sGadhI16pSJem7Yce1nB3x3KMDnzVPf4/vkn8P9LeT50qcYWfwjF8 g==; From: Vincent Whitchurch Date: Mon, 12 Jun 2023 09:26:48 +0200 Subject: [PATCH v2] watchdog/mm: Allow dumping memory info in pretimeout MIME-Version: 1.0 Message-ID: <20230608-pretimeout-oom-v2-1-581f0ad0e4f3@axis.com> X-B4-Tracking: v=1; b=H4sIALfIhmQC/3WNQQ6CMBAAv0L27Jq2VARP/sNwKGWRPcCSFgmG8 HcLd48zyWQ2iBSYIjyyDQItHFnGBOaSge/d+CbkNjEYZXJVqBKnQDMPJJ8ZRQasKm1Lm5dOtx2 kqHGRsAlu9P2RdSLroVPV8Xp+XnXinuMs4XtuF33Yv4dFo8abNd5XWhWmvT/dyvHqZYB63/cf3 3VwpsIAAAA= To: Wim Van Sebroeck , Guenter Roeck , Andrew Morton CC: , , , , Vincent Whitchurch X-Mailer: b4 0.12.2 X-Rspamd-Queue-Id: 0B1B4C000A X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 5xt7rrazgqrygzgisjdpg1c44x6c14qj X-HE-Tag: 1686554815-288064 X-HE-Meta: U2FsdGVkX19a7coS3gekVSkhroxlspu53TI7oXc39wZvtSvnPfnK7okOB4pyOns+zje6qNYlJ5nnLHNsUldYnAtz1KFHVfxwzMaGy9wYniNfDkSArfEmNxI94F8Z5oxlCLq6Q7PJ17f4YvN/V+HBKuuPpOiNElq9Uv8VwfMZkPApfSQYX+hnyfin9m3+vN1LYMOIiibzrGOMwY3+l15aoDBcAtnZVOnk4Z891JYtwgsp+TxDrSUQ6OtKLXV6iL7rQQ7wDNTTLmgKPiGr/Hx1002gwe62EAXFTkIPgNgYpb9OIdPFbOPxSuGw7t65/DeSr6G8LgoMxTeASi8wTWEX2EbQ4zygCB7xvsrABt8PacctY0ICJzo6tOIuNWo8Rugp4OVSGMDZpO8R35LhSnVINeKc8FcdTjamYFVMVbaWG9m9uhdkxjQbbShryBKswUiYH08hVBXHpo0L5CsXuNPacsnK6lZtdXGOb0XH1nWUnaB2hXEY8FRWruMOePa9bK98zYnXkm9CnivOzeDxJ2ycCoLUiPBR+vnPWybwhvEgcycyM9hcaosqXGwRUt1o+m0V5UEJUu7NUKQuw+y6XTx+l4XIriU1QauniiePlNqFCQ5IImwSL1QYoBjZX0XCm86J2k0eNqf2ei/b1Tkuh7G0a7XuG61ASpRKfAd9SaOQ1BeGkEtf5OaThy54hvJTziOjJLiJUV4mFBu4uiBfPkqakuPGDihkOrCF2sjwKVYJHadlViZAjow8vNweSDviZ92NJTo1Py3vgjiAqR/YHZlUyyNACdGnu+0tSKGWdkmP0y4vDlMpxZAElBxcHk91lnVBEMdRDCVC7/cXLvvrhnU9VtBtDmi99xpsdW7n9uPd+rMV6buyGhtv78s4Q+AHNXX+KG5uhEf+l/IR9EkZhkOE9kIT6NYM+XqG9qde/273EZU23ucmulQXvCBHLBQ/2t8svi4M9oPfVzWActOHuTa F7Ga1EFt 0nXiHA9/NUf3rmxU0YiU++k4bWT/5k+bJkQaIHNAZAT/e9aYNZwzD9iRsNMjepNjRdHYt+w3GLL3Z0k0AefzmcFfFYW04NIRnfOZAdO5qpM568nK3Ud0nXelo9Z0Af/Zwjpz20eX/YFhzDs5o9kQu1LslMAu0OnxWueiRI24CbLToIyI8Fu2G0fdpcHnMvDkAtbTC0SUMn4tGoO8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On my (embedded) systems, the most common cause of hitting the watchdog (pre)timeout is due to thrashing. Diagnosing these problems is hard without knowing the memory state at the point of the watchdog hit. In order to make this information available, add a module parameter to the watchdog pretimeout panic governor to ask it to dump memory info and the OOM task list (using a new helper in the OOM code) before triggering the panic. Signed-off-by: Vincent Whitchurch --- Changes in v2: - Add missing static to fix warning reported by kernel test robot. - Export __show_mem to fix error reported by kernel test robot. - Link to v1: https://lore.kernel.org/r/20230608-pretimeout-oom-v1-1-542cc91062d7@axis.com --- drivers/watchdog/pretimeout_panic.c | 15 +++++++++++ include/linux/oom.h | 5 ++++ include/linux/sched/task.h | 5 ++++ lib/show_mem.c | 1 + mm/oom_kill.c | 54 ++++++++++++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 1 deletion(-) --- base-commit: 9561de3a55bed6bdd44a12820ba81ec416e705a7 change-id: 20230608-pretimeout-oom-99148438a1df Best regards, diff --git a/drivers/watchdog/pretimeout_panic.c b/drivers/watchdog/pretimeout_panic.c index 2cc3c41d2be5b..52d686fa541c7 100644 --- a/drivers/watchdog/pretimeout_panic.c +++ b/drivers/watchdog/pretimeout_panic.c @@ -5,10 +5,15 @@ #include #include +#include +#include #include #include "watchdog_pretimeout.h" +static unsigned long dump_min_rss_bytes; +module_param(dump_min_rss_bytes, ulong, 0644); + /** * pretimeout_panic - Panic on watchdog pretimeout event * @wdd - watchdog_device @@ -17,6 +22,16 @@ */ static void pretimeout_panic(struct watchdog_device *wdd) { + /* + * Since the root cause is not certain to be low memory, only print + * tasks with RSS above a configurable limit, to avoid losing + * potentially more important messages from the log. + */ + if (dump_min_rss_bytes) { + show_mem(SHOW_MEM_FILTER_NODES, NULL); + oom_dump_tasks(DIV_ROUND_UP(dump_min_rss_bytes, PAGE_SIZE)); + } + panic("watchdog pretimeout event\n"); } diff --git a/include/linux/oom.h b/include/linux/oom.h index 7d0c9c48a0c54..1451fe2c38d78 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -52,6 +52,9 @@ struct oom_control { /* Used to print the constraint info. */ enum oom_constraint constraint; + + bool dump_trylock; + unsigned long dump_min_rss_pages; }; extern struct mutex oom_lock; @@ -102,6 +105,8 @@ long oom_badness(struct task_struct *p, extern bool out_of_memory(struct oom_control *oc); +extern void oom_dump_tasks(unsigned long min_rss_pages); + extern void exit_oom_victim(void); extern int register_oom_notifier(struct notifier_block *nb); diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index e0f5ac90a228b..e8a68b2a3e829 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -183,6 +183,11 @@ static inline void task_lock(struct task_struct *p) spin_lock(&p->alloc_lock); } +static inline int task_trylock(struct task_struct *p) +{ + return spin_trylock(&p->alloc_lock); +} + static inline void task_unlock(struct task_struct *p) { spin_unlock(&p->alloc_lock); diff --git a/lib/show_mem.c b/lib/show_mem.c index 1485c87be9354..cf90d1c5182b7 100644 --- a/lib/show_mem.c +++ b/lib/show_mem.c @@ -35,3 +35,4 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages)); #endif } +EXPORT_SYMBOL_GPL(__show_mem); diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 044e1eed720ee..0fad1c6d3c90c 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -149,6 +149,30 @@ struct task_struct *find_lock_task_mm(struct task_struct *p) return t; } +/* + * Identical to the above, except that we avoid tasks which we can't lock, to + * avoid deadlocks when called from an interrupt handler. + */ +static struct task_struct *find_trylock_task_mm(struct task_struct *p) +{ + struct task_struct *t; + + rcu_read_lock(); + + for_each_thread(p, t) { + if (!task_trylock(t)) + continue; + if (likely(t->mm)) + goto found; + task_unlock(t); + } + t = NULL; +found: + rcu_read_unlock(); + + return t; +} + /* * order == -1 means the oom kill is required by sysrq, otherwise only * for display purposes. @@ -390,15 +414,26 @@ static int dump_task(struct task_struct *p, void *arg) if (!is_memcg_oom(oc) && !oom_cpuset_eligible(p, oc)) return 0; - task = find_lock_task_mm(p); + task = oc->dump_trylock ? find_trylock_task_mm(p) : + find_lock_task_mm(p); if (!task) { /* * All of p's threads have already detached their mm's. There's * no need to report them; they can't be oom killed anyway. + * + * Or we got here from an interrupt and the task lock is + * locked, in which case we're forced to ignore this task to + * avoid deadlocks. */ return 0; } + if (oc->dump_min_rss_pages && + get_mm_rss(task->mm) < oc->dump_min_rss_pages) { + task_unlock(task); + return 0; + } + pr_info("[%7d] %5d %5d %8lu %8lu %8ld %8lu %5hd %s\n", task->pid, from_kuid(&init_user_ns, task_uid(task)), task->tgid, task->mm->total_vm, get_mm_rss(task->mm), @@ -437,6 +472,23 @@ static void dump_tasks(struct oom_control *oc) } } +void oom_dump_tasks(unsigned long min_rss_pages) +{ + const gfp_t gfp_mask = GFP_KERNEL; + struct oom_control oc = { + .zonelist = node_zonelist(first_memory_node, gfp_mask), + .nodemask = NULL, + .memcg = NULL, + .gfp_mask = gfp_mask, + .order = -1, + .dump_min_rss_pages = min_rss_pages, + .dump_trylock = in_interrupt(), + }; + + dump_tasks(&oc); +} +EXPORT_SYMBOL_GPL(oom_dump_tasks); + static void dump_oom_summary(struct oom_control *oc, struct task_struct *victim) { /* one line summary of the oom killer context. */