From patchwork Tue Sep 24 12:13:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingxiang Zeng X-Patchwork-Id: 13810877 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 12FC5CF9C6F for ; Tue, 24 Sep 2024 12:14:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 572B06B0085; Tue, 24 Sep 2024 08:14:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5237A6B0088; Tue, 24 Sep 2024 08:14:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4112A6B0089; Tue, 24 Sep 2024 08:14:11 -0400 (EDT) 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 234B56B0085 for ; Tue, 24 Sep 2024 08:14:11 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C13AE1A1AAE for ; Tue, 24 Sep 2024 12:14:10 +0000 (UTC) X-FDA: 82599523860.26.5545245 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id EA68910001A for ; Tue, 24 Sep 2024 12:14:07 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mhIxmKH1; spf=pass (imf14.hostedemail.com: domain of jingxiangzeng.cas@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=jingxiangzeng.cas@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727179929; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ij5K39dJNQXaF5oy09H6p72Y/tNdpzczJu65QWM5JEs=; b=lDM1U1lVYQv5mfiiJNrKSlowxsLpO+T4+AqHQt8a9ALXGIRWGiOSCIUToiLrn373mpicWr zPDy0tSTnPy5PDREtrW1dzenEGnrbliQvcoet1PbWuHUT/sWazYrxmp7fa02XZ8yqx9+WP yG92wSJHn6Hlj2jAGW5ik1eCh1BM6Yw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727179929; a=rsa-sha256; cv=none; b=TyuIHSb+TRjQc1Lcfj/PH6bJIdfrJgp44+UY809HV7x0Dg0x1gAQMKdLa/gCLQIOpewF0s OhqDKpNcghcbPNIcjVvtydL9pp97x47rCWrj13Rjli+eC1q9oNTdf+2xG+GPewvesK+x03 U790sNsZm06HwrSAiSA1sq9pezgCPaE= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mhIxmKH1; spf=pass (imf14.hostedemail.com: domain of jingxiangzeng.cas@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=jingxiangzeng.cas@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2053a0bd0a6so53923795ad.3 for ; Tue, 24 Sep 2024 05:14:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727180046; x=1727784846; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ij5K39dJNQXaF5oy09H6p72Y/tNdpzczJu65QWM5JEs=; b=mhIxmKH1zaHdbRgX5B+lrQqGNFKKTgCCCVIF/lPU7YPJybWKlvMxx3u+MUxIIlzsSM H/LFVEQnI/7f+pZWr1AgLKxu8ca1j0wQF5RPP4p1uJxhR7P9XsdiRPz/+dadaSKzWC1M GtYJbN22tQrCRO350p2oLgvjmgdqDG71BoFVa9EAhMLdNzerf8TvUsqGYBpYkKV+I65g vkT/M/ZY09+xjcgdax7lfUyv+hYem97eVNsEGBpcWrgDx4oyxrqITyx8wQSwq+lmuVRj 40dgavBO0hp6ZZMfpZnAQE/ki4KU1yYgYJMr+TplC6vVOVNlpTp53iwZwTVvy5SWGxN7 LOAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727180046; x=1727784846; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ij5K39dJNQXaF5oy09H6p72Y/tNdpzczJu65QWM5JEs=; b=Yah/2ieWUMCrLdiizBxOwoOWBJWBYkC4nFYIhUdfO3HPQmuyU5W92xzUiCDYlyzkKj gKG+ZRQScSThT5dG8LeLOtJRS70Ge0KB2m1CFw4eT6akbsYCIFCBpEyE/4X5/DGvx9/I FrOa88HGHjw6jzYGTBg3RNpOjVOUgF2GRkUWJJCo5/oVWZTC692cr5U7J/jLen9lK4Xu 0n4VyQOMh5D6ureJg1RhcfaEmoSUc1lpy4fGUgIwvfw54aLSVqvzpy07Di4uVV9IlODY rDW82JAfkviA1KYcDxAqcIP4eoSHsfHHGY6qEqcmuDIYAZi/whZ/YLVwOHb0SJE+h8Ck lJjQ== X-Gm-Message-State: AOJu0YxSx57G3WtDn7XggOKarT6DiFM9tPORF1giZajOVbr6SnmjlFaM 0TVgIDCI89GyzerGDKcZAqoiiAv6zYD+gIJ/4vPmCI2LMmJJv/vqqwDR340t X-Google-Smtp-Source: AGHT+IFl2YN+Zdem7CJHs6GwVNYDS5112EOg21IFqM3FwWmvFL3Hw4hVFLA+svs4H5kW/Q1BtNZyPA== X-Received: by 2002:a17:903:2309:b0:205:627c:8001 with SMTP id d9443c01a7336-208d8303cd2mr194458185ad.5.1727180045897; Tue, 24 Sep 2024 05:14:05 -0700 (PDT) Received: from localhost.localdomain ([14.116.239.35]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20af1867c1dsm9430045ad.303.2024.09.24.05.14.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2024 05:14:05 -0700 (PDT) From: Jingxiang Zeng To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, kasong@tencent.com, linuszeng@tencent.com, linux-kernel@vger.kernel.org, tjmercier@google.com, weixugc@google.com, yuzhao@google.com Subject: [PATCH V3] mm/vmscan: wake up flushers conditionally to avoid cgroup OOM Date: Tue, 24 Sep 2024 20:13:58 +0800 Message-ID: <20240924121358.30685-1-jingxiangzeng.cas@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: EA68910001A X-Stat-Signature: j15cdbku6ca6uyxok68w1hcd88eoaa1z X-HE-Tag: 1727180047-47921 X-HE-Meta: U2FsdGVkX1/oAoZ38Sk7SQmYy1VlWrsDVTrpsMlYQMX5pr7FT3CWX1weFWb/XDEbI/6Lv90Of297QmKa6j1px6mc2V9fEUER/bLRhV3b8gmpN1NngJO8iN5CD2fIoivctoM+EoopG8O2jFwN+Tbm06Rn9HHADf7QcRtkflIrpTBOBA+ACcDvgiJOqpzqC3BxjQOaGg+Cuu1LjDAahf8+mTWtu9Wa49Xx78LFuDXSsjn7xs/usPOLmES7h5MmhYWKUUJMScpv5aHToCGzSJLaj99zQa1nZktqApsiQrbAZhXkJVVvEJVMbWOJeikvp6YuFJImZqSB+CxLT1WkF2UqxRxzrAex8WyYZtl12aEcKbokCMgt87jrLnIPojwwo6kZ2KehMZp5o3wK+g2IUHMhi49wFIcvy6TAgVue0j9GStNvl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edence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Zeng Jingxiang Commit 14aa8b2d5c2e ("mm/mglru: don't sync disk for each aging cycle") removed the opportunity to wake up flushers during the MGLRU page reclamation process can lead to an increased likelihood of triggering OOM when encountering many dirty pages during reclamation on MGLRU. This leads to premature OOM if there are too many dirty pages in cgroup: Killed dd invoked oom-killer: gfp_mask=0x101cca(GFP_HIGHUSER_MOVABLE|__GFP_WRITE), order=0, oom_score_adj=0 Call Trace: dump_stack_lvl+0x5f/0x80 dump_stack+0x14/0x20 dump_header+0x46/0x1b0 oom_kill_process+0x104/0x220 out_of_memory+0x112/0x5a0 mem_cgroup_out_of_memory+0x13b/0x150 try_charge_memcg+0x44f/0x5c0 charge_memcg+0x34/0x50 __mem_cgroup_charge+0x31/0x90 filemap_add_folio+0x4b/0xf0 __filemap_get_folio+0x1a4/0x5b0 ? srso_return_thunk+0x5/0x5f ? __block_commit_write+0x82/0xb0 ext4_da_write_begin+0xe5/0x270 generic_perform_write+0x134/0x2b0 ext4_buffered_write_iter+0x57/0xd0 ext4_file_write_iter+0x76/0x7d0 ? selinux_file_permission+0x119/0x150 ? srso_return_thunk+0x5/0x5f ? srso_return_thunk+0x5/0x5f vfs_write+0x30c/0x440 ksys_write+0x65/0xe0 __x64_sys_write+0x1e/0x30 x64_sys_call+0x11c2/0x1d50 do_syscall_64+0x47/0x110 entry_SYSCALL_64_after_hwframe+0x76/0x7e memory: usage 308224kB, limit 308224kB, failcnt 2589 swap: usage 0kB, limit 9007199254740988kB, failcnt 0 ... file_dirty 303247360 file_writeback 0 ... oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=test, mems_allowed=0,oom_memcg=/test,task_memcg=/test,task=dd,pid=4404,uid=0 Memory cgroup out of memory: Killed process 4404 (dd) total-vm:10512kB, anon-rss:1152kB, file-rss:1824kB, shmem-rss:0kB, UID:0 pgtables:76kB oom_score_adj:0 The flusher wake up was removed to decrease SSD wearing, but if we are seeing all dirty folios at the tail of an LRU, not waking up the flusher could lead to thrashing easily. So wake it up when a mem cgroups is about to OOM due to dirty caches. Link: https://lkml.kernel.org/r/20240829102543.189453-1-jingxiangzeng.cas@gmail.com Link: https://lkml.kernel.org/r/20240913084506.3606292-1-jingxiangzeng.cas@gmail.com Fixes: 14aa8b2d5c2e ("mm/mglru: don't sync disk for each aging cycle") Signed-off-by: Zeng Jingxiang Signed-off-by: Kairui Song Cc: T.J. Mercier Cc: Wei Xu Cc: Yu Zhao --- Changes from v2: - Acquire the lock before calling the folio_check_dirty_writeback function. - Link to v2: https://lore.kernel.org/all/20240913084506.3606292-1-jingxiangzeng.cas@gmail.com/ Changes from v1: - Add code to count the number of unqueued_dirty in the sort_folio function. - Link to v1: https://lore.kernel.org/all/20240829102543.189453-1-jingxiangzeng.cas@gmail.com/ --- mm/vmscan.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 749cdc110c74..12c285a96353 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4290,6 +4290,8 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c int delta = folio_nr_pages(folio); int refs = folio_lru_refs(folio); int tier = lru_tier_from_refs(refs); + bool dirty = folio_test_dirty(folio); + bool writeback = folio_test_writeback(folio); struct lru_gen_folio *lrugen = &lruvec->lrugen; VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio); @@ -4330,8 +4332,10 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c } /* waiting for writeback */ - if (folio_test_locked(folio) || folio_test_writeback(folio) || - (type == LRU_GEN_FILE && folio_test_dirty(folio))) { + if (folio_test_locked(folio) || dirty || + (type == LRU_GEN_FILE && writeback)) { + if (type == LRU_GEN_FILE && dirty && !writeback) + sc->nr.unqueued_dirty += delta; gen = folio_inc_gen(lruvec, folio, true); list_move(&folio->lru, &lrugen->folios[gen][type][zone]); return true; @@ -4448,6 +4452,7 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, scanned, skipped, isolated, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); + sc->nr.taken += isolated; /* * There might not be eligible folios due to reclaim_idx. Check the * remaining to prevent livelock if it's not making progress. @@ -4920,6 +4925,13 @@ static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc if (try_to_shrink_lruvec(lruvec, sc)) lru_gen_rotate_memcg(lruvec, MEMCG_LRU_YOUNG); + /* + * If too many pages in the coldest generation that cannot + * be isolated, wake up flusher. + */ + if (sc->nr.unqueued_dirty > sc->nr.taken) + wakeup_flusher_threads(WB_REASON_VMSCAN); + clear_mm_walk(); blk_finish_plug(&plug);