From nobody Wed Dec 17 21:39:23 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDDBE15EFA1 for ; Tue, 31 Dec 2024 04:35:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619757; cv=none; b=Ih5Zft/0OQZSFmUNKfA//sx7qtJM1XPfnrIbihtkizwBd9NhLcJXptfHGcUzqIIfnCWUKKEAWw+gVjGGHc/fZJ7WCHp9BXuDXkFifhYyIM68yPoelk+QkTlm8V0uvAzu9JT+Yod5CLe1Hn0l5A2i8w1Hlumhtzx/Wlf/2QQCDEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735619757; c=relaxed/simple; bh=q1WnSZgXmLYzutr+EunR5Q1Z3UubbeTqmVf960GbNbY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a5PwxkSMkKxWgU9qPihf2Ja0t0tGMCc56ftmbQFyEKen0TauCzyQ4M2TyjMDEQJc+EBsWF7pHvzW1pzpHtqdf/w/k36qivjRpLV44ix8gsDpbtzRFE1LCeVRGssV1xANgGcGIM11KGSMymClua4GZXjEC8P4fhDN0CMar7XiF/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vHVzVgMG; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--yuzhao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vHVzVgMG" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef79d9c692so20669045a91.0 for ; Mon, 30 Dec 2024 20:35:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735619755; x=1736224555; darn=vger.kernel.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=lq6dqRxgSDV3CyACy0+VqeWU26YT+ayL2zVnCSsSGnY=; b=vHVzVgMGSbY0RpOhzx+S09mSI4V1wJWg0LLsEPnf9R4qvH5ODvu1LDwp/5I9H+sgTa zUZs6ZcPuFL4qeb/mMtjPeTvoTsTq75XsYbvUDLb5fCYvnZv1x1iSlyD8bYASQQvVZrC a9pch3Gv8TNPaClqXJ2SvS8Sa1nVsQC0C42F6ll3vxJ/nGlM+k0bv//O9TRoY06WlzOV DtLlwVhD5l75D/COE+frz4lG6LAWgGHDELRh2EUdu7CmUttHScoE9sH2kcXpbYlW1o3Z FNrURFAYy6y0url5tL45Pjl5q4ig27sI/so9uo3HR2XvhewANAYYSLmEhR277EVHUFua r2Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735619755; x=1736224555; 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=lq6dqRxgSDV3CyACy0+VqeWU26YT+ayL2zVnCSsSGnY=; b=CkxMzv/l6WJ1wTrMzrjjYyMVr+oEDoF1wpmY2vIpN87IpVCqDVY9awL5HdQF1saRxS R6g8esSNkAcHa4ANyGosSb14IqRI37gmfRXTDSzrf7GBgamwe2Mme7hJQZF2rjxLFDkZ hf2O54erJuH8L4HIjqutsVeOcCQwXpOi6IRPpfLqDUIliyxXzieb0AfEg55zP0bspg/e ZvISttuDy0DBEIYxYgU7pJwpFu/GbiccnS5dOFFEIAcKQX1vMIMvY7sfH+RcK8YXZd38 HCTSUs54/AVavB/5HsieZL9ckn2shU/0A9zGsfMnEzAsr1JjBb5cve4YyrwwmFEVtrdP AdTQ== X-Forwarded-Encrypted: i=1; AJvYcCX5EeCQdUfvf1mPdIl3p6b43uXCVY9CcS9rDGdiyMYB2fjenFWn4x6eXvgVPvuvfhmK5+grYUrAtxdFx1c=@vger.kernel.org X-Gm-Message-State: AOJu0YycZou/mFfQMFNApn/bZZqJEGXNzvxwA6LW0AokcKMXWCkL84rY yHzswE27H5fOIa3W/XYUfgURr+aj1C2RhhpiDHjA5ScTnqPKWiG9M9bZBdSI/FZ6hdiTm2d2t8j AaQ== X-Google-Smtp-Source: AGHT+IHeZyv9yGkkGSeakHyyxsc/LoAVc3WVdX/rKDrbygxdcMqZjnwE7jl/rLSwE6a6nOYHrpf3ke3Jfww= X-Received: from pfd7.prod.google.com ([2002:a05:6a00:a807:b0:727:2d74:d385]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:430d:b0:725:db34:6a8c with SMTP id d2e1a72fcca58-72abddbd4f7mr55420073b3a.13.1735619755260; Mon, 30 Dec 2024 20:35:55 -0800 (PST) Date: Mon, 30 Dec 2024 21:35:35 -0700 In-Reply-To: <20241231043538.4075764-1-yuzhao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241231043538.4075764-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241231043538.4075764-5-yuzhao@google.com> Subject: [PATCH mm-unstable v4 4/7] 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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, regardless of whether that type has a larger min_seq or not, to accommodate extreme swappiness. 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 f767e3d34e73..a33221298fd0 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3093,15 +3093,20 @@ struct ctrl_pos { static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int g= ain, struct ctrl_pos *pos) { + int i; struct lru_gen_folio *lrugen =3D &lruvec->lrugen; int hist =3D lru_hist_from_seq(lrugen->min_seq[type]); =20 - pos->refaulted =3D lrugen->avg_refaulted[type][tier] + - atomic_long_read(&lrugen->refaulted[hist][type][tier]); - pos->total =3D lrugen->avg_total[type][tier] + - lrugen->protected[hist][type][tier] + - atomic_long_read(&lrugen->evicted[hist][type][tier]); pos->gain =3D gain; + pos->refaulted =3D pos->total =3D 0; + + for (i =3D tier % MAX_NR_TIERS; i <=3D min(tier, MAX_NR_TIERS - 1); i++) { + pos->refaulted +=3D lrugen->avg_refaulted[type][i] + + atomic_long_read(&lrugen->refaulted[hist][type][i]); + pos->total +=3D lrugen->avg_total[type][i] + + lrugen->protected[hist][type][i] + + atomic_long_read(&lrugen->evicted[hist][type][i]); + } } =20 static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) @@ -4501,13 +4506,13 @@ static int get_tier_idx(struct lruvec *lruvec, int = type) struct ctrl_pos sp, pv; =20 /* - * 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 =3D 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; } @@ -4515,68 +4520,34 @@ static int get_tier_idx(struct lruvec *lruvec, int = type) return tier - 1; } =20 -static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *ti= er_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] =3D { swappiness, MAX_SWAPPINESS - swappiness }; =20 + if (!swappiness) + return LRU_GEN_FILE; + + if (swappiness =3D=3D 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 =3D 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 - swappi= ness, &pv); =20 - read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp); - for (tier =3D 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, gain[type], &pv); - if (!positive_ctrl_err(&sp, &pv)) - break; - } - - *tier_idx =3D tier - 1; - - return type; + return positive_ctrl_err(&sp, &pv); } =20 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 =3D -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 =3D LRU_GEN_FILE; - else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE]) - type =3D LRU_GEN_ANON; - else if (swappiness =3D=3D 1) - type =3D LRU_GEN_FILE; - else if (swappiness =3D=3D MAX_SWAPPINESS) - type =3D LRU_GEN_ANON; - else if (!(sc->gfp_mask & __GFP_IO)) - type =3D LRU_GEN_FILE; - else - type =3D get_type_to_scan(lruvec, swappiness, &tier); + int type =3D get_type_to_scan(lruvec, swappiness); =20 for_each_evictable_type(i, swappiness) { int scanned; - - if (tier < 0) - tier =3D get_tier_idx(lruvec, type); + int tier =3D get_tier_idx(lruvec, type); =20 *type_scanned =3D type; =20 @@ -4585,7 +4556,6 @@ static int isolate_folios(struct lruvec *lruvec, stru= ct scan_control *sc, int sw return scanned; =20 type =3D !type; - tier =3D -1; } =20 return 0; --=20 2.47.1.613.gc27f4b7a9f-goog