From patchwork Tue Nov 19 06:08:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: liuye X-Patchwork-Id: 13879434 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 9649DD4921C for ; Tue, 19 Nov 2024 06:09:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E7C96B007B; Tue, 19 Nov 2024 01:09:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 297C86B0082; Tue, 19 Nov 2024 01:09:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 160566B0083; Tue, 19 Nov 2024 01:09:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id ED8D76B007B for ; Tue, 19 Nov 2024 01:09:01 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9E7EEA0160 for ; Tue, 19 Nov 2024 06:09:01 +0000 (UTC) X-FDA: 82801815054.12.742F27C Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by imf27.hostedemail.com (Postfix) with ESMTP id 49C8E40007 for ; Tue, 19 Nov 2024 06:08:05 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of liuye@kylinos.cn designates 124.126.103.232 as permitted sender) smtp.mailfrom=liuye@kylinos.cn; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731996338; 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:in-reply-to:references:references; bh=fd1VfSjn4y6ztcnHxlBJcYYXTAzKD5JgwiraJkzm7aU=; b=S1eNQUAkLKDFeOnu2elLNsf90NNL1LMMMScQMrWEdG3jpxrmqJP180ZitUUMa0hQ8mlsML +5pT1rdCRinscVnRnaGWAKpx5roEIO6Fwx69ndU7mzSIKemvF5nvAA9OQHkcQiVEJ34w6b hD6CdPaRpaHynGZfiTx8a+YrvXwNMjM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=pass (imf27.hostedemail.com: domain of liuye@kylinos.cn designates 124.126.103.232 as permitted sender) smtp.mailfrom=liuye@kylinos.cn; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731996338; a=rsa-sha256; cv=none; b=H2rJsqLb5+B8LRwmnHXPDoJ7qFXVDv1fEY3FFaV3QacLpFS2584RxsRX+m0TplO3dc6M/8 ND+oPpzailKqXuwFyVCU08bm9BNR/Y6o9ZG07iAGHUkTXgys+2ihry0W5AfmXnLiJ89uz1 Gy/wTrbg+A6wuh4m59Iw5gU90vEbOu4= X-UUID: bd19bab8a63c11efa216b1d71e6e1362-20241119 X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_TXT HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_DIGIT_LEN HR_SJ_LANG, HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE HR_SJ_PHRASE_LEN, HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME IP_TRUSTED, SRC_TRUSTED, DN_TRUSTED, SA_EXISTED, SN_EXISTED SPF_NOPASS, DKIM_NOPASS, DMARC_NOPASS, CIE_BAD, CIE_GOOD CIE_GOOD_SPF, GTI_FG_BS, GTI_RG_INFO, GTI_C_BU, AMN_T1 AMN_GOOD, AMN_C_TI, AMN_C_BU, ABX_MISS_RDNS X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.38,REQID:5617c3a8-1e88-4be5-a607-a85dc1a63a69,IP:25, URL:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:10 X-CID-INFO: VERSION:1.1.38,REQID:5617c3a8-1e88-4be5-a607-a85dc1a63a69,IP:25,UR L:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:10 X-CID-META: VersionHash:82c5f88,CLOUDID:d0843e6f27936c32b8e6a54dcef86a00,BulkI D:2411191402458XN0IKTY,BulkQuantity:1,Recheck:0,SF:841|24|17|19|44|66|102, TC:nil,Content:0,EDM:-3,IP:-2,URL:0,File:nil,RT:nil,Bulk:40,QS:nil,BEC:nil ,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_FSD,TF_CID_SPAM_FSI,TF_CID_SPAM_SNR,TF_CID_SPAM_FAS X-UUID: bd19bab8a63c11efa216b1d71e6e1362-20241119 X-User: liuye@kylinos.cn Received: from localhost.localdomain [(39.156.73.13)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA with TLSv1.3 TLS_AES_256_GCM_SHA384 256/256) with ESMTP id 311379331; Tue, 19 Nov 2024 14:08:47 +0800 From: liuye To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, liuye@kylinos.cn Subject: [PATCH v2 RESEND] mm/vmscan: Fix hard LOCKUP in function isolate_lru_folios Date: Tue, 19 Nov 2024 14:08:42 +0800 Message-Id: <20241119060842.274072-1-liuye@kylinos.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240919021443.9170-1-liuye@kylinos.cn> References: <20240919021443.9170-1-liuye@kylinos.cn> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 49C8E40007 X-Stat-Signature: zpjb9qbfi17nmcujnuwhxbbzffg3skye X-Rspam-User: X-HE-Tag: 1731996485-695908 X-HE-Meta: U2FsdGVkX19C6fDThrzDuJlSsCzDZkYMnolyMNqmwfhB31kMIAA62SMc4r/EzzJRTKrA2MXs3CD3g8ZV+2UVmOw8SFFiH2RBPG3OeVLczKtlu6RUGjrz2eg5HEC7oWkmF2ZP2kyJSC9082JbYytKjcvL+1PNSWm/eVI4a7R/Bc1YBRf/gq0X8Tr1l5hXl5p+/96lFXHHBxXTcJQFdbJui26tGF/QCm75kiWiS42j+UwxdZWYidXe+iHtbc96Z5xJGCOMPsHI7DklVj7MZNSPLixj1MZ6C//Ilgq0ACsFh5cY0nY2TITcrSQvUc+4lZ3MHyh7jq1Vh//XRmeNq4MTCSEp+FPgVTsPpVrwRUM4DzWo6k3wwDOaGLwyEeXVSCIWlr+4J3vZK45ai0x20NKWAEbpnNfjeSOHCsaTUZovRjMKdDUhEA1tDgJgk8XJMTnieULghpF+NxHkY5POZceeLZHEwjtTcJM/KuioBcY5kdO0nj+Cj6gHPQPTMPP4xg7fcRJtULJ/uWwH0+rj3la9r4YXZV0Ow+y/VvpghD1n7EgSqsfuGEKafuW9iZXwHK7xuwih60LPcqRIgZbs61qc5286MEXpYNEz/Ph29VNRg+2MHTWglpJ2pb6+aqzDGicL9U+gZKDRqiy+8vBR1No8ckWU9AxpGyZ3lXx0NYuvpA7fo4ScaE5njyOkEtE9R9s6TYJu7oQU+M3qLyf+pDVlNSUATXLajmOCE+HUWNWU8exm08NARKzEibErCKyeiaK1eO20Lm2KmkJdlIlrxdbFCpzb8YQo1GlMOMKq16pZJ1XH6ROfEdPMRRo85/HMZqjC+WKyNRWAlV7Xt6Y0DXqUnvvgHI63GwMh0UZhpXc97UkR7/6JwIcuxOTMCyJ6So0n7j1VmR5YATeI9UGNWTe2ehApQK+N/bDOjbbkGIRlgbhHtTgiKNrQupaP3bRQIxa1yzACMMbLnckSA+y2foO O3uuAWLk vbSdtXIWYpylcgJu+0njwgsZSkBTNvUsJbLKv/3wr/1JIpKnhkf7nKUmpSa8IGGUm7CNQhZV9JahnhBG9Zt48aHEEZAW2S2YAJ+VPlxU2YhHlkDEcaQD1+XToDg== 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: List-Subscribe: List-Unsubscribe: This fixes the following hard lockup in function isolate_lru_folios when memory reclaim.If the LRU mostly contains ineligible folios May trigger watchdog. watchdog: Watchdog detected hard LOCKUP on cpu 173 RIP: 0010:native_queued_spin_lock_slowpath+0x255/0x2a0 Call Trace: _raw_spin_lock_irqsave+0x31/0x40 folio_lruvec_lock_irqsave+0x5f/0x90 folio_batch_move_lru+0x91/0x150 lru_add_drain_per_cpu+0x1c/0x40 process_one_work+0x17d/0x350 worker_thread+0x27b/0x3a0 kthread+0xe8/0x120 ret_from_fork+0x34/0x50 ret_from_fork_asm+0x1b/0x30 lruvec->lru_lock owner: PID: 2865 TASK: ffff888139214d40 CPU: 40 COMMAND: "kswapd0" #0 [fffffe0000945e60] crash_nmi_callback at ffffffffa567a555 #1 [fffffe0000945e68] nmi_handle at ffffffffa563b171 #2 [fffffe0000945eb0] default_do_nmi at ffffffffa6575920 #3 [fffffe0000945ed0] exc_nmi at ffffffffa6575af4 #4 [fffffe0000945ef0] end_repeat_nmi at ffffffffa6601dde [exception RIP: isolate_lru_folios+403] RIP: ffffffffa597df53 RSP: ffffc90006fb7c28 RFLAGS: 00000002 RAX: 0000000000000001 RBX: ffffc90006fb7c60 RCX: ffffea04a2196f88 RDX: ffffc90006fb7c60 RSI: ffffc90006fb7c60 RDI: ffffea04a2197048 RBP: ffff88812cbd3010 R8: ffffea04a2197008 R9: 0000000000000001 R10: 0000000000000000 R11: 0000000000000001 R12: ffffea04a2197008 R13: ffffea04a2197048 R14: ffffc90006fb7de8 R15: 0000000003e3e937 ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 #5 [ffffc90006fb7c28] isolate_lru_folios at ffffffffa597df53 #6 [ffffc90006fb7cf8] shrink_active_list at ffffffffa597f788 #7 [ffffc90006fb7da8] balance_pgdat at ffffffffa5986db0 #8 [ffffc90006fb7ec0] kswapd at ffffffffa5987354 #9 [ffffc90006fb7ef8] kthread at ffffffffa5748238 crash> Scenario: User processe are requesting a large amount of memory and keep page active. Then a module continuously requests memory from ZONE_DMA32 area. Memory reclaim will be triggered due to ZONE_DMA32 watermark alarm reached. However pages in the LRU(active_anon) list are mostly from the ZONE_NORMAL area. Reproduce: Terminal 1: Construct to continuously increase pages active(anon). mkdir /tmp/memory mount -t tmpfs -o size=1024000M tmpfs /tmp/memory dd if=/dev/zero of=/tmp/memory/block bs=4M tail /tmp/memory/block Terminal 2: vmstat -a 1 active will increase. procs ---memory--- ---swap-- ---io---- -system-- ---cpu--- ... r b swpd free inact active si so bi bo 1 0 0 1445623076 45898836 83646008 0 0 0 1 0 0 1445623076 43450228 86094616 0 0 0 1 0 0 1445623076 41003480 88541364 0 0 0 1 0 0 1445623076 38557088 90987756 0 0 0 1 0 0 1445623076 36109688 93435156 0 0 0 1 0 0 1445619552 33663256 95881632 0 0 0 1 0 0 1445619804 31217140 98327792 0 0 0 1 0 0 1445619804 28769988 100774944 0 0 0 1 0 0 1445619804 26322348 103222584 0 0 0 1 0 0 1445619804 23875592 105669340 0 0 0 cat /proc/meminfo | head Active(anon) increase. MemTotal: 1579941036 kB MemFree: 1445618500 kB MemAvailable: 1453013224 kB Buffers: 6516 kB Cached: 128653956 kB SwapCached: 0 kB Active: 118110812 kB Inactive: 11436620 kB Active(anon): 115345744 kB Inactive(anon): 945292 kB When the Active(anon) is 115345744 kB, insmod module triggers the ZONE_DMA32 watermark. perf record -e vmscan:mm_vmscan_lru_isolate -aR perf script isolate_mode=0 classzone=1 order=1 nr_requested=32 nr_scanned=2 nr_skipped=2 nr_taken=0 lru=active_anon isolate_mode=0 classzone=1 order=1 nr_requested=32 nr_scanned=0 nr_skipped=0 nr_taken=0 lru=active_anon isolate_mode=0 classzone=1 order=0 nr_requested=32 nr_scanned=28835844 nr_skipped=28835844 nr_taken=0 lru=active_anon isolate_mode=0 classzone=1 order=1 nr_requested=32 nr_scanned=28835844 nr_skipped=28835844 nr_taken=0 lru=active_anon isolate_mode=0 classzone=1 order=0 nr_requested=32 nr_scanned=29 nr_skipped=29 nr_taken=0 lru=active_anon isolate_mode=0 classzone=1 order=0 nr_requested=32 nr_scanned=0 nr_skipped=0 nr_taken=0 lru=active_anon See nr_scanned=28835844. 28835844 * 4k = 115343376KB approximately equal to 115345744 kB. If increase Active(anon) to 1000G then insmod module triggers the ZONE_DMA32 watermark. hard lockup will occur. In my device nr_scanned = 0000000003e3e937 when hard lockup. Convert to memory size 0x0000000003e3e937 * 4KB = 261072092 KB. [ffffc90006fb7c28] isolate_lru_folios at ffffffffa597df53 ffffc90006fb7c30: 0000000000000020 0000000000000000 ffffc90006fb7c40: ffffc90006fb7d40 ffff88812cbd3000 ffffc90006fb7c50: ffffc90006fb7d30 0000000106fb7de8 ffffc90006fb7c60: ffffea04a2197008 ffffea0006ed4a48 ffffc90006fb7c70: 0000000000000000 0000000000000000 ffffc90006fb7c80: 0000000000000000 0000000000000000 ffffc90006fb7c90: 0000000000000000 0000000000000000 ffffc90006fb7ca0: 0000000000000000 0000000003e3e937 ffffc90006fb7cb0: 0000000000000000 0000000000000000 ffffc90006fb7cc0: 8d7c0b56b7874b00 ffff88812cbd3000 About the Fixes: Why did it take eight years to be discovered? The problem requires the following conditions to occur: 1. The device memory should be large enough. 2. Pages in the LRU(active_anon) list are mostly from the ZONE_NORMAL area. 3. The memory in ZONE_DMA32 needs to reach the watermark. If the memory is not large enough, or if the usage design of ZONE_DMA32 area memory is reasonable, this problem is difficult to detect. notes: The problem is most likely to occur in ZONE_DMA32 and ZONE_NORMAL, but other suitable scenarios may also trigger the problem. Fixes: b2e18757f2c9 ("mm, vmscan: begin reclaiming pages on a per-node basis") --- V1->V2 : Adjust code format and add scenario description, reproduction method. --- Signed-off-by: liuye --- include/linux/swap.h | 1 + mm/vmscan.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index f3e0ac20c2e8..187715eec3cb 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -223,6 +223,7 @@ enum { }; #define SWAP_CLUSTER_MAX 32UL +#define SWAP_CLUSTER_MAX_SKIPPED (SWAP_CLUSTER_MAX << 10) #define COMPACT_CLUSTER_MAX SWAP_CLUSTER_MAX /* Bit flag in swap_map */ diff --git a/mm/vmscan.c b/mm/vmscan.c index 28ba2b06fc7d..0bdfae413b4c 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1657,6 +1657,7 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan, unsigned long nr_skipped[MAX_NR_ZONES] = { 0, }; unsigned long skipped = 0; unsigned long scan, total_scan, nr_pages; + unsigned long max_nr_skipped = 0; LIST_HEAD(folios_skipped); total_scan = 0; @@ -1671,9 +1672,12 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan, nr_pages = folio_nr_pages(folio); total_scan += nr_pages; - if (folio_zonenum(folio) > sc->reclaim_idx) { + /* Using max_nr_skipped to prevent hard LOCKUP*/ + if (max_nr_skipped < SWAP_CLUSTER_MAX_SKIPPED && + (folio_zonenum(folio) > sc->reclaim_idx)) { nr_skipped[folio_zonenum(folio)] += nr_pages; move_to = &folios_skipped; + max_nr_skipped++; goto move; }