From patchwork Sun Sep 29 11:26:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingxiang Zeng X-Patchwork-Id: 13815073 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 60D99CF6499 for ; Sun, 29 Sep 2024 11:26:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CEB836B00AA; Sun, 29 Sep 2024 07:26:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C72EE6B00DE; Sun, 29 Sep 2024 07:26:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3B3D6B00F5; Sun, 29 Sep 2024 07:26:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 966856B00AA for ; Sun, 29 Sep 2024 07:26:54 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3B9CF1A1995 for ; Sun, 29 Sep 2024 11:26:54 +0000 (UTC) X-FDA: 82617548748.01.CE741E5 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf28.hostedemail.com (Postfix) with ESMTP id 49B29C000D for ; Sun, 29 Sep 2024 11:26:52 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eZTLkq79; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of jingxiangzeng.cas@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=jingxiangzeng.cas@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727609120; a=rsa-sha256; cv=none; b=ahjtx7ZvZlYyp1tQQAGp8iUDB9HsVBOCtP7UqhQj7AlRA4RzplK6vWt8/XsVdt73vG62Et LZ0uCTm4axpf/eK/VubZES5h5rs1ZfDOPQLG1RdPdT6pq2eFQtFblaulR10FoxbM3kjQtP hfCe2LzsEisSPgkDgWrB6hL6Duej6Ec= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eZTLkq79; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of jingxiangzeng.cas@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=jingxiangzeng.cas@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727609120; 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=s6EbTKSSIMrdfxOrdGxKRCffCAwNII6oo6Oi258NhPg=; b=lUcDF32PSLykHUZF1HcEB8mspFWtcAYxs4S4FnCpvwsIGdUusT3VvJISDnXYR9Z8NB/MyC SUMEIWMZPmpsQs4jU0Q49cJF0TNpD9h2LJD6k4e10WJNx8cJLadOhnkd7wNwST3KpZlQFq gyCabmdFqbeq+g35+ngTvgmi/mgP4AI= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71971d2099cso2828958b3a.2 for ; Sun, 29 Sep 2024 04:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727609210; x=1728214010; 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=s6EbTKSSIMrdfxOrdGxKRCffCAwNII6oo6Oi258NhPg=; b=eZTLkq79Wm1tO/AG6tUQDbDoqolFh7ZG86N/xz9oB5ax1AgCi8LMoKGkls4gFAUftu U0H4pf4xt5UI1qpJge+sDuIeU/LF+m2Cw9zIxd2IIN8icCbGLy0fKfsr4rsLepEUfX7S uyckeFGQIVSQ1fi+Y2fZACH0+fTPrv8sezPrO3DSMQoCulYIizwdRc7nZLH0z5XJPehQ QgjNLBmf3Y06vwuCSdCZYXBbJNxw+vP3sYSDdLhrDrs9gMF29lFOU0wlvg4j9j2F8uBW MXfAa+ju4cV5stjsuyQe48WtKFirgzJLbqmzd5ZsxIetS0zwJiVi+aQ/jSggiweQIZUZ TwKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727609210; x=1728214010; 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=s6EbTKSSIMrdfxOrdGxKRCffCAwNII6oo6Oi258NhPg=; b=WKEnkHXVZ9GuqWqpYORNsO/9RvqXp0I0mgHKI/OvHa53u7DcJq4j6UDjYu95DT9B7O v5astgQ+U08LCGrtF3ccI0CJS/D9ZSAzOn2zBPDmus7e59JPqJSErJXsnqY0ZtQiDl7f sRtcKiOVM/xYIw+JIxz5zzGKbks+9Nqa5uMq8H2TCDf5h3GOeAc6jP6T9ZroL5pnTfJj HF9mcmNRrxbruPHGGycVCN1znL9pSLJ8s9kY7IpdFnJadtnQe6ASGeiS2Mjmj+N2G4sm 16FmixBLJ+Xy7YtjqmbaEfnONEp4eJqT5H+jDwWRmGyTENiSDyc1e/6RDo4/HIZzKQ+B ze6w== X-Gm-Message-State: AOJu0YwYoyqOo8/Tjfx7nAa72VnCBxGAyokrxYohzYA+1m2RRPJjUS/7 hZ2yFlEkoFPv6+ayEEFoqyqJXa8h6T2ftWO2ioS5owVGNolSogpL8NSU6fj0 X-Google-Smtp-Source: AGHT+IEtls1x7x5bfCwpZ45fSytgtheV/Rz4GiQc6IgJezi0A6QuxJcbNwmvdoGp5oSD89kR4NbrWA== X-Received: by 2002:a05:6a00:2383:b0:717:8568:8cee with SMTP id d2e1a72fcca58-71b25f3b880mr14027759b3a.12.1727609210251; Sun, 29 Sep 2024 04:26:50 -0700 (PDT) Received: from localhost.localdomain ([14.116.239.36]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b2652b4f6sm4564656b3a.174.2024.09.29.04.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 04:26:49 -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] [PATCH v4] mm/vmscan: wake up flushers conditionally to avoid cgroup OOM Date: Sun, 29 Sep 2024 19:26:42 +0800 Message-ID: <20240929112642.68087-1-jingxiangzeng.cas@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 49B29C000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 1ox1gae8e5sy9b7jdpbeq9a5u1s8pkgj X-HE-Tag: 1727609212-841395 X-HE-Meta: U2FsdGVkX19HQENyi0PUiy/bfUmCMcbEktpvJArS6+g9ejOMbujPR+MmA7/R1kL68C3K9wjLoxNyoshJVYph8Y0GwS3aZR6qBD2EpmwXudggW/y1Uf91B+hpCjYqNUXWqvZntoXz1MvJOl91m7ccquvPAmxlmiA+wi8Fbmx1yAEFblf7dNYoYkmeG3e3Yk5spY8LWkUT1s1JTeHMpzDfK9aC7tALShs/nIMSKaiD42HbPHAmjzLjkmEYPPRVmCgBhLQQBKVV8Lwk4ZEfCP2M5NoV+1OppuwqlvrVksWu20sXxRg0vFh37K+PqbsK7FjGM7O0Mr1+34ncSDD0ZKYT1BcU8zfD/tIZr6lS5Xo58zd/PtYWQzfFBaFuYVbf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.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: 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. --- Changes from v3: - Avoid taking lock and reduce overhead on folio isolation by checking the right flags and rework wake up condition, fixing the performance regression reported by Chris Li. [Chris Li, Kairui Song] - Move the wake up check to try_to_shrink_lruvec to cover kswapd case as well, and update comments. [Kairui Song] - Link to v3: https://lore.kernel.org/all/20240924121358.30685-1-jingxiangzeng.cas@gmail.com/ Changes from v2: - Acquire the lock before calling the folio_check_dirty_writeback function. [Wei Xu, Jingxiang Zeng] - 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. [Wei Xu, Jingxiang Zeng] - Link to v1: https://lore.kernel.org/all/20240829102543.189453-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 --- mm/vmscan.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index dc7a285b256b..2a5c2fe81467 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4291,6 +4291,7 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c int tier_idx) { bool success; + bool dirty, writeback; int gen = folio_lru_gen(folio); int type = folio_is_file_lru(folio); int zone = folio_zonenum(folio); @@ -4336,9 +4337,14 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c return true; } + dirty = folio_test_dirty(folio); + writeback = folio_test_writeback(folio); + if (type == LRU_GEN_FILE && dirty && !writeback) + sc->nr.unqueued_dirty += delta; + /* 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) || writeback || + (type == LRU_GEN_FILE && dirty)) { gen = folio_inc_gen(lruvec, folio, true); list_move(&folio->lru, &lrugen->folios[gen][type][zone]); return true; @@ -4454,7 +4460,7 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, MAX_LRU_BATCH, scanned, skipped, isolated, type ? LRU_INACTIVE_FILE : LRU_INACTIVE_ANON); - + sc->nr.taken += scanned; /* * There might not be eligible folios due to reclaim_idx. Check the * remaining to prevent livelock if it's not making progress. @@ -4796,6 +4802,13 @@ static bool try_to_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) cond_resched(); } + /* + * If too many file cache in the coldest generation can't be evicted + * due to being dirty, wake up the flusher. + */ + if (sc->nr.unqueued_dirty && sc->nr.unqueued_dirty == sc->nr.taken) + wakeup_flusher_threads(WB_REASON_VMSCAN); + /* whether this lruvec should be rotated */ return nr_to_scan < 0; }