From patchwork Fri Jan 10 03:17:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: 76824143@qq.com X-Patchwork-Id: 13933552 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 DEF93E77188 for ; Fri, 10 Jan 2025 03:18:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60C156B0093; Thu, 9 Jan 2025 22:18:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BC456B0095; Thu, 9 Jan 2025 22:18:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 484196B0096; Thu, 9 Jan 2025 22:18:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2461E6B0093 for ; Thu, 9 Jan 2025 22:18:08 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 98EE6AF4C5 for ; Fri, 10 Jan 2025 03:18:07 +0000 (UTC) X-FDA: 82990083414.04.C834574 Received: from out203-205-221-149.mail.qq.com (out203-205-221-149.mail.qq.com [203.205.221.149]) by imf18.hostedemail.com (Postfix) with ESMTP id 5C61F1C0003 for ; Fri, 10 Jan 2025 03:18:05 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=qq.com header.s=s201512 header.b=N2yyL5a1; dmarc=pass (policy=quarantine) header.from=qq.com; spf=pass (imf18.hostedemail.com: domain of 76824143@qq.com designates 203.205.221.149 as permitted sender) smtp.mailfrom=76824143@qq.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736479086; a=rsa-sha256; cv=none; b=nsX5jjpici8zHEHc2KbV/kQH/ZtKEBi3x/Bb4SuDRz6dnjKkM70Ny2v0lMdsg7RTSJ+Lv/ G7/1pCZ2uKOPKXGE5vyrpwFs1StzYSwPTv5ehvitHLYTjyi67OnFso6E8c8StcmijF5/PY 6zmGLQr7bCJGyMLSFVP9U8oyZ739HPU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=qq.com header.s=s201512 header.b=N2yyL5a1; dmarc=pass (policy=quarantine) header.from=qq.com; spf=pass (imf18.hostedemail.com: domain of 76824143@qq.com designates 203.205.221.149 as permitted sender) smtp.mailfrom=76824143@qq.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736479086; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=N6YYrMJwxHYDu/WlEpF+nAd2hhef33Uup3BdVQ9xsLo=; b=Lh2T4pbzTBTrW37XMrQCedI5Dl33B4Kl2L72JLQPbekvlidblhiPNqp68oJUO8ON6CXLt3 FAZnYF72/LpNYcaKA194vabX0evnJfS/IHFs+3zuOFPGI7XXIOSz2r1kR8y07sc6JM2WkK uSNUlKY/gXy1g3TS8bUG9p78FmCZdxo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1736479071; bh=N6YYrMJwxHYDu/WlEpF+nAd2hhef33Uup3BdVQ9xsLo=; h=From:To:Cc:Subject:Date; b=N2yyL5a1xlg3d9hug17Lc2eZznid5Rq3ueygjo5eIUZMJvbVN9735c3tHjbnLbm/I MwBXdcy59m+olNmqB8rRsY3qoghYSD7dQu0xCPvdINN+Q8O4f4Eqvo2eIjMkMBC+vu 2ooAoLcqGgWjOH1F5LSU3xwlSQNAO7PXOtDBcR3g= Received: from localhost.localdomain ([116.128.244.171]) by newxmesmtplogicsvrszb16-1.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 uldU/t6vx+515GB18naRquRaSSaPIrwgpQEZZOTpY= X-QQ-XMRINFO: OWPUhxQsoeAVDbp3OJHYyFg= From: 76824143@qq.com To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, Hao Zhang Subject: [PATCH] mm/vmscan: extract calculated pressure balance as a function Date: Fri, 10 Jan 2025 11:17:47 +0800 X-OQ-MSGID: <20250110031747.81202-1-76824143@qq.com> X-Mailer: git-send-email 2.15.0 X-Stat-Signature: p4frxjcq6m66eyezbyecet1m4wpeqpib X-Rspam-User: X-Rspamd-Queue-Id: 5C61F1C0003 X-Rspamd-Server: rspam08 X-HE-Tag: 1736479085-270721 X-HE-Meta: U2FsdGVkX18CxhGr1uW20FG5Th9duW5rSLXUEIgUOdeK7+8bgakJUBJ8dqdT7N8CbO6OLQMhR3UDgGJLxgr34SQwNz4V27X0Gcb/B1x0LS9NrVkAWlL9qNCh76nKVkH6xEoz2BncGIJ5BUMeZAKsl+y0sBeh29CBhauALyBSzQVFxYxpthgcDeSCaXiuZw4vC0OgxezCeQfAOoSTdo6BM3qHBQ7W6XKa7pBLMKf/BfizrGcNAq+sXTlOU8d65DUGa5sq3xKFo+UlmAsz4U9ttxURUVZqqE2ToNUYXjKkdnE8cjP2T08r8VzIW3Rdp5NHhdM+cq7vYHrA3NxqkSoxaslTaouzy6MVkrrWi3bZx3LFgzo1AyUXkfqXYqUYEz1xiy+umalj7M8IaClM9BBvmSNL9ftwwqeMYsLux1uvtLDm3bWsNrMkKwmZTOP/DLS/98nnM9IRqvZrtmR7OjrX726e9047BUDH48MQN6ODcvoc1vIFQgnvZKV4C0n2abF/supFud4vDQz5Vu8GfvQFClB337YQ7R39SZZMOFnksKpIm/PpZOsClDOkt23Y7/ZRwfVO2ZLHj2bOBxw/Q8w/DiAgMicf4cRzAndDTCjCZo1A/Ud2JxyGmlR63kqs8IcBM5hiFKBigAeTelvXoiK6EoJLVpGkSpdAGkAr2729DHFFOu3pCIfNJR4PdakANrtvx5I+W9KuTiMcr2/+lb51PNbxd0n7DAN5y4TsEi2+dpWarBDKgxam2T5FXTzAcR4TD4Uzgb3JxL/1VRGQtQ+Ig2a8aa07ViMsy4fCmqqGq3a9RhoYpYuM2pRHoUgCN/WWaxl5yii0IRYnH4+ihINMZaRRZD3GshkE2tZAxb+JmnZI9uWjg0cwW+52DCFDooi2nugOHSkPdSqHEa+486wG1T70FvMwNW/Qy/1Uzv001GQ8W9lt+VcnZaZktsAPe2GN25dQK38xm+cwBWeJyST nr9He4nR YgWVLZx3EQ5+2aq2NF7yc6M4whH/FM9LzS2anx4UUmtG7ay7WdRv45XUY8Xn9iDBF+T0U0XFlfmLrYOu2a7iC5vq53BqiKrTXxKQUoxE13VLEDcSE43d0H/foQhtbVQZKmK9DoF6LnwFQ/eZ/lkh5hFWGI+Z/JXi4RzL6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.213193, 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: Hao Zhang The pressure balance calculation is only used in the fraction scenario. Extract into functions to avoid unnecessary calculations. This doesn't change current behaviour. Signed-off-by: Hao Zhang --- mm/vmscan.c | 73 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 30 deletions(-) base-commit: eea6e4b4dfb8859446177c32961c96726d0117be diff --git a/mm/vmscan.c b/mm/vmscan.c index 9a859b7d18d7..97754406dd2e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2367,6 +2367,43 @@ static void prepare_scan_control(pg_data_t *pgdat, struct scan_control *sc) } } +static void calculate_pressure_balance(struct scan_control *sc, int swappiness, + u64 *fraction, u64 *denominator) +{ + unsigned long anon_cost, file_cost, total_cost; + unsigned long ap, fp; + + /* + * Calculate the pressure balance between anon and file pages. + * + * The amount of pressure we put on each LRU is inversely + * proportional to the cost of reclaiming each list, as + * determined by the share of pages that are refaulting, times + * the relative IO cost of bringing back a swapped out + * anonymous page vs reloading a filesystem page (swappiness). + * + * Although we limit that influence to ensure no list gets + * left behind completely: at least a third of the pressure is + * applied, before swappiness. + * + * With swappiness at 100, anon and file have equal IO cost. + */ + total_cost = sc->anon_cost + sc->file_cost; + anon_cost = total_cost + sc->anon_cost; + file_cost = total_cost + sc->file_cost; + total_cost = anon_cost + file_cost; + + ap = swappiness * (total_cost + 1); + ap /= anon_cost + 1; + + fp = (MAX_SWAPPINESS - swappiness) * (total_cost + 1); + fp /= file_cost + 1; + + fraction[WORKINGSET_ANON] = ap; + fraction[WORKINGSET_FILE] = fp; + *denominator = ap + fp; +} + /* * Determine how aggressively the anon and file LRU lists should be * scanned. @@ -2379,12 +2416,11 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, { struct pglist_data *pgdat = lruvec_pgdat(lruvec); struct mem_cgroup *memcg = lruvec_memcg(lruvec); - unsigned long anon_cost, file_cost, total_cost; int swappiness = sc_swappiness(sc, memcg); u64 fraction[ANON_AND_FILE]; + bool calculated = false; u64 denominator = 0; /* gcc */ enum scan_balance scan_balance; - unsigned long ap, fp; enum lru_list lru; /* If we have no swap space, do not bother scanning anon folios. */ @@ -2433,35 +2469,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, } scan_balance = SCAN_FRACT; - /* - * Calculate the pressure balance between anon and file pages. - * - * The amount of pressure we put on each LRU is inversely - * proportional to the cost of reclaiming each list, as - * determined by the share of pages that are refaulting, times - * the relative IO cost of bringing back a swapped out - * anonymous page vs reloading a filesystem page (swappiness). - * - * Although we limit that influence to ensure no list gets - * left behind completely: at least a third of the pressure is - * applied, before swappiness. - * - * With swappiness at 100, anon and file have equal IO cost. - */ - total_cost = sc->anon_cost + sc->file_cost; - anon_cost = total_cost + sc->anon_cost; - file_cost = total_cost + sc->file_cost; - total_cost = anon_cost + file_cost; - ap = swappiness * (total_cost + 1); - ap /= anon_cost + 1; - - fp = (MAX_SWAPPINESS - swappiness) * (total_cost + 1); - fp /= file_cost + 1; - - fraction[0] = ap; - fraction[1] = fp; - denominator = ap + fp; out: for_each_evictable_lru(lru) { bool file = is_file_lru(lru); @@ -2544,6 +2552,11 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, /* Scan lists relative to size */ break; case SCAN_FRACT: + if (!calculated) { + calculate_pressure_balance(sc, swappiness, fraction, &denominator); + calculated = true; + } + /* * Scan types proportional to swappiness and * their relative recent reclaim efficiency.