From patchwork Fri Dec 6 00:31:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896158 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 61843E77171 for ; Fri, 6 Dec 2024 00:31:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF8766B00E6; Thu, 5 Dec 2024 19:31:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7FE06B00ED; Thu, 5 Dec 2024 19:31:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B84B56B0109; Thu, 5 Dec 2024 19:31:45 -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 96A0B6B00E6 for ; Thu, 5 Dec 2024 19:31:45 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 55671141811 for ; Fri, 6 Dec 2024 00:31:45 +0000 (UTC) X-FDA: 82862655414.01.26F8104 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf26.hostedemail.com (Postfix) with ESMTP id 39E8214000F for ; Fri, 6 Dec 2024 00:31:31 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gMeesYfD; spf=pass (imf26.hostedemail.com: domain of 37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733445096; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=h1mvjlX5nT1rLjjtVWZn8iR4hG3sSfAtSE80GuqS3ww=; b=SffSIRKoaopFCv+g0cKWa5JzmY2iuMxTLdGCOHY1JBIe3apFjAaajS2rLVeRctiQObK0Oa Fv3qCHEs2OZHxKOuFmHEkdkw/ak6zmat4iILy1q5a206UcnRrSSOU+WnRm4fDyWB4jIQCs fvltowzO30bEMZM12g8epaYI5Ixh2lY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445096; a=rsa-sha256; cv=none; b=ZeAnA3pDiyUAs4d1uTLfwgIGKVRlcln/3O+IECRCX89RaxiedrrOsYUFDWPdCr64fmD6X5 Ir6L9+V1+S++3EXaUbWbxOWgtMRRy1lY8rx/4PmKxDCOu6sKk3qe7WNjfdXxif7nM0eBuJ 4WqcuoWKRY9tpzZr6bzarHdOm7e5SBE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gMeesYfD; spf=pass (imf26.hostedemail.com: domain of 37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-724fc3f60c1so2317528b3a.2 for ; Thu, 05 Dec 2024 16:31:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445102; x=1734049902; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=h1mvjlX5nT1rLjjtVWZn8iR4hG3sSfAtSE80GuqS3ww=; b=gMeesYfDm07sqWFuQl6FpgsS8fYNno3t9XUvik/r4pnZGO+1OPQ6ijOyR4eLMdZkNw 5rpP6ogU8ch1AikpeQMsJYWz76QE0sCEaWkSip7NxsC1dXO9V0HFPR3xzH0mNVzSAKSs UUrkrTFfPnLoqCk4mIFGlP4vNnxuLShSz1+Yu7HU90VR5WsaAdqU3ewhyynjmDuwEyo7 GcWZ/dth+XYO4R4lwa5RvMteBqri86QZu+YZwPS7HgzjByRZEZvaeeyHzFcZ1IonYoyL XSj7IKQRzm67Fx+Dcdq4CEDZUwZThFhCZRphIRxZcPotV4X6lJg00prXqJs/4dYtTK6o 2/5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445102; x=1734049902; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=h1mvjlX5nT1rLjjtVWZn8iR4hG3sSfAtSE80GuqS3ww=; b=a+rY6tJXruaUZGKGK6lQtv4v4Jj6tf5qwL/4KzYy/5FO+GGrl650SWPvhbfTBPkbbi PnCxx7jym6QPjps2PRkh2RHgn+fmnGtNNBwP5UBj3VMQ2/3UbbkbJMHE83z9qVRdbwcb bM1raqBEfFq2Ycv9nm2tYWcdl4ac8cxcmcd0B9nzdM1rCVk9ZXnRkLX2DwL3tT/gN0R1 DV/ePD7rdbzKWbsm2fxh+Yqa+uf1vVkwcgpimOKDCycSSd84JJrUp7rY+Rjk45qld+qz W3a2RxL7D/Lf0oRLrhMOf4v5cJOAJN2tGsp9441LkZLYytr0SjFfETTQm+WZ1daLBuAV y+Aw== X-Gm-Message-State: AOJu0YxBauImdh5eH3tSa5oov9fRtUjeQrmqGE11QNBpCiJ6R0YjP+6J 9r3A5QvknRgImy2kthlC+aamcKT9h/o3R/OFtuZRvYiN194HZFbu2v88uE/MWB3QEAuLcmPUInI qOg== X-Google-Smtp-Source: AGHT+IHDpfbWWJjycwRdIOATm4/TSA+FDknfDh4D0XImpwFKpP+OCJtRJF+zzzhrB+mJ306ByBPns1yu/E8= X-Received: from pfbim19.prod.google.com ([2002:a05:6a00:8d93:b0:725:3321:1f0c]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4fc7:b0:725:9d1d:3462 with SMTP id d2e1a72fcca58-725b81c1481mr1987282b3a.25.1733445102450; Thu, 05 Dec 2024 16:31:42 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:24 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-5-yuzhao@google.com> Subject: [PATCH mm-unstable v2 4/6] mm/mglru: rework type selection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , David Stevens , Kalesh Singh X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 39E8214000F X-Stat-Signature: 44yb1fzf1t17eiexox7ioo3xmbpbi8rj X-Rspam-User: X-HE-Tag: 1733445091-374490 X-HE-Meta: U2FsdGVkX19fdOb8DwvJmkZmHZiaqQZ5bwn23TkuIsAsWoIj83JSox8XUp6jBgeCVuSz1H2mB6qUAnwrhamG1WuRSyxOWikYppAFirBOZGs6r0zCgqW5SQsHqDdP6cxxalFe4rezGygtPM21gCrTY8GZ6S2b6sp6+YRwIYFB+6y1SrRg0b+hvx70WC+YVGuUbyHfxax7lzI+DCKuBSiUAdInwo0w9yXHoFBkHOFMMiwWtyFloZ1YyMeUAY6GZpM2R46M5mSH+YNIr8n9Mn83dkZE4QTSXLxLJNghtf83GfKMaoP/G2GRdH28rYTJ6giBjYXhEsNh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OWuS2j+qNvk69YC/yPd0KBwvm2m/OuKlWbh2eHaTYgHcYe6QB7l81mCgk23W+0eEGuBO8wY/3JtAkAR+85zaVp5QxYNU5vD7aMDhOHVzXiHMNauOGJLLtuxWj3uaJlKmFfB+WIeoA926+sNQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.011265, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With anon and file min_seq being able to move independently, rework type selection so that it is based on the total refaults from all tiers of each type. Also allow a type to be selected until that type reaches MIN_NR_GENS, and therefore abs_diff(min_seq[0],min_seq[1]) now can be 2 (MAX_NR_GENS-MIN_NR_GENS) instead of 1. Since some tiers of a selected type can have higher refaults than the first tier of the other type, use a less larger gain factor 2:3 instead of 1:2, in order for those tiers in the selected type to be better protected. As an intermediate step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. Reported-by: David Stevens Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/vmscan.c | 82 +++++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 56 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 00a5aff3db42..02b01ae2bdbb 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3089,15 +3089,20 @@ struct ctrl_pos { static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain, struct ctrl_pos *pos) { + int i; struct lru_gen_folio *lrugen = &lruvec->lrugen; int hist = lru_hist_from_seq(lrugen->min_seq[type]); - pos->refaulted = lrugen->avg_refaulted[type][tier] + - atomic_long_read(&lrugen->refaulted[hist][type][tier]); - pos->total = lrugen->avg_total[type][tier] + - lrugen->protected[hist][type][tier] + - atomic_long_read(&lrugen->evicted[hist][type][tier]); pos->gain = gain; + pos->refaulted = pos->total = 0; + + for (i = tier % MAX_NR_TIERS; i <= min(tier, MAX_NR_TIERS - 1); i++) { + pos->refaulted += lrugen->avg_refaulted[type][i] + + atomic_long_read(&lrugen->refaulted[hist][type][i]); + pos->total += lrugen->avg_total[type][i] + + lrugen->protected[hist][type][i] + + atomic_long_read(&lrugen->evicted[hist][type][i]); + } } static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) @@ -4480,13 +4485,13 @@ static int get_tier_idx(struct lruvec *lruvec, int type) struct ctrl_pos sp, pv; /* - * To leave a margin for fluctuations, use a larger gain factor (1:2). + * To leave a margin for fluctuations, use a larger gain factor (2:3). * This value is chosen because any other tier would have at least twice * as many refaults as the first tier. */ - read_ctrl_pos(lruvec, type, 0, 1, &sp); + read_ctrl_pos(lruvec, type, 0, 2, &sp); for (tier = 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, 2, &pv); + read_ctrl_pos(lruvec, type, tier, 3, &pv); if (!positive_ctrl_err(&sp, &pv)) break; } @@ -4494,68 +4499,34 @@ static int get_tier_idx(struct lruvec *lruvec, int type) return tier - 1; } -static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx) +static int get_type_to_scan(struct lruvec *lruvec, int swappiness) { - int type, tier; struct ctrl_pos sp, pv; - int gain[ANON_AND_FILE] = { swappiness, MAX_SWAPPINESS - swappiness }; + if (!swappiness) + return LRU_GEN_FILE; + + if (swappiness == MAX_SWAPPINESS) + return LRU_GEN_ANON; /* - * Compare the first tier of anon with that of file to determine which - * type to scan. Also need to compare other tiers of the selected type - * with the first tier of the other type to determine the last tier (of - * the selected type) to evict. + * Compare the sum of all tiers of anon with that of file to determine + * which type to scan. */ - read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp); - read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv); - type = positive_ctrl_err(&sp, &pv); + read_ctrl_pos(lruvec, LRU_GEN_ANON, MAX_NR_TIERS, swappiness, &sp); + read_ctrl_pos(lruvec, LRU_GEN_FILE, MAX_NR_TIERS, MAX_SWAPPINESS - swappiness, &pv); - read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp); - for (tier = 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, gain[type], &pv); - if (!positive_ctrl_err(&sp, &pv)) - break; - } - - *tier_idx = tier - 1; - - return type; + return positive_ctrl_err(&sp, &pv); } static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness, int *type_scanned, struct list_head *list) { int i; - int type; - int tier = -1; - DEFINE_MIN_SEQ(lruvec); - - /* - * Try to make the obvious choice first, and if anon and file are both - * available from the same generation, - * 1. Interpret swappiness 1 as file first and MAX_SWAPPINESS as anon - * first. - * 2. If !__GFP_IO, file first since clean pagecache is more likely to - * exist than clean swapcache. - */ - if (!swappiness) - type = LRU_GEN_FILE; - else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE]) - type = LRU_GEN_ANON; - else if (swappiness == 1) - type = LRU_GEN_FILE; - else if (swappiness == MAX_SWAPPINESS) - type = LRU_GEN_ANON; - else if (!(sc->gfp_mask & __GFP_IO)) - type = LRU_GEN_FILE; - else - type = get_type_to_scan(lruvec, swappiness, &tier); + int type = get_type_to_scan(lruvec, swappiness); for_each_evictable_type(i, swappiness) { int scanned; - - if (tier < 0) - tier = get_tier_idx(lruvec, type); + int tier = get_tier_idx(lruvec, type); *type_scanned = type; @@ -4564,7 +4535,6 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw return scanned; type = !type; - tier = -1; } return 0;