From patchwork Mon Dec 2 03:28:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13889938 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 456FFD49798 for ; Mon, 2 Dec 2024 03:28:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4577F6B0099; Sun, 1 Dec 2024 22:28:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 408306B009A; Sun, 1 Dec 2024 22:28:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 209B26B009B; Sun, 1 Dec 2024 22:28:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F3A706B0099 for ; Sun, 1 Dec 2024 22:28:41 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A2A1C120373 for ; Mon, 2 Dec 2024 03:28:41 +0000 (UTC) X-FDA: 82848586380.25.775F306 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf25.hostedemail.com (Postfix) with ESMTP id 95F90A000A for ; Mon, 2 Dec 2024 03:28:31 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZIqpIJ7Z; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3ZilNZwYKCC8jfkSLZRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yuzhao.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3ZilNZwYKCC8jfkSLZRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yuzhao.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733110109; a=rsa-sha256; cv=none; b=vkzYUGUytBRRyNKzcBepmkxPFiCOTwpxOJH7ENNKuASyBAfCGmT4M++chhaKAOIFwFyxfS pVC1f/EiToltqk5zWYf78sSf+UMEBbQFNxV4jPnWa/iiHqO9dP34q4bhcnLBftwxeKlDbf e92fY0d4cxN4NAHx3MNCADVzvw1Ofv8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZIqpIJ7Z; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3ZilNZwYKCC8jfkSLZRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yuzhao.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3ZilNZwYKCC8jfkSLZRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yuzhao.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733110109; 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=tAZ7EXnKaWj8C19Di4ja72nho7QoBFM1H+8+YKhOaNc=; b=F92LFK5QMQyaBHfmlvPbWZxMf5A53XpVMDfg4qwxnKkrzLeBytJMSqyGBG1v3cIVqPVi79 GQSuBIae4jEYUUxnoyYfdUAYMOcnEjdc3uhdpDndeYgB9VWJC1V+3MbChl1KHv1Q7VF2yY 0eOAvXlgCp0bNrqut5WymBYn57HO780= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee9e92a577so866782a91.0 for ; Sun, 01 Dec 2024 19:28:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733110118; x=1733714918; 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=tAZ7EXnKaWj8C19Di4ja72nho7QoBFM1H+8+YKhOaNc=; b=ZIqpIJ7ZGxWqsHMqqiJGpWRjOd+/IA74x6QRxppRAFIMSeF9zeiAncQXbrtLD047HO e3OGmYN3tm4vF/mK+ijOA5ANVvZzis0bZQiu9MM/Yf1ksMVrsnxixjGJSpr6mh8AN/St 3bFSndcRFpMujjm2KUqv8L1WUWUf7DfyWkWZlX+2cYL4aOCke6ZXM6T8fABidA/SIy7/ 2kwbJR1/WJ+nrydRHRit0N1ODoewpQREvkHrpCtuLRWmC4F5Vu2CctvsoDbS7lNII1RZ VVmG6WXzT5y8xwDSrxEuUt4J/Pxe5sQ4zwvOFfHZ+V+YMDk0WBLpUZWmFernEeCrf2T/ DEuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733110118; x=1733714918; 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=tAZ7EXnKaWj8C19Di4ja72nho7QoBFM1H+8+YKhOaNc=; b=aMYA54oltoH+YqgIflkGOLkEcooBuetfeCJleQ0gAbx9xxXnlR8eWhx6okLfDEOcct aePsH0OGl+7pP0Y/JxFALetRQ5hTZG3gKtyc+EOoiO8QVyVU/srzGbOzC/hn1/4XAFLS uIp63p387gBjiBWsWxZ6fQTuZZD8T0oM/X6U79oVoliLninKrGRrtQvxb2pTyaqhu+GI mVDpgsgtzrooicoWBt5aGJm/+8yraQ8PRW5fNlWQyHy3Xw0UNBDJ62bxVwPTyfux1lvU 43AW9+84tQw0tkQDYuAWwezyR2am/NHkX8i7WDth72DVsZRRvCNB2a3aXrgoLaSsTf3k oarQ== X-Gm-Message-State: AOJu0YwEjBiP4jY8a2vnlsKA72+k1xhDwyg5dznr+WoeEINFbalqHJ1w 62vwjQXiIk6F34oY1dcivE5rrTPd/q/9Kikn7Gb1TkCvzuNM0ebUKuzuWZ5jNKAYcETVtCIj5bp +5w== X-Google-Smtp-Source: AGHT+IEv//P/jMaTg8qmYerNeCdqWyYAtY0F42kcb0T+44U79mT8shI+AxtZNoulfnzTgxMAL0SEqCiFUhE= X-Received: from pjbqd4.prod.google.com ([2002:a17:90b:3cc4:b0:2ea:c64d:fccb]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5186:b0:2ee:9229:e4bd with SMTP id 98e67ed59e1d1-2ee9229e5d9mr9120161a91.2.1733110118614; Sun, 01 Dec 2024 19:28:38 -0800 (PST) Date: Sun, 1 Dec 2024 20:28:21 -0700 In-Reply-To: <20241202032823.2741019-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241202032823.2741019-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241202032823.2741019-5-yuzhao@google.com> Subject: [PATCH mm-unstable v1 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-Stat-Signature: q71hffn4hzfu4ou8riape3c4mpo6rfa9 X-Rspam-User: X-Rspamd-Queue-Id: 95F90A000A X-Rspamd-Server: rspam08 X-HE-Tag: 1733110111-945105 X-HE-Meta: U2FsdGVkX1+rIU/x9YA94u/VswJweGKNoWBWtE4qvTHKeXc3CWGQwnb2nA0mE8OuLxKfFot+C1hW/qpWAaQYoMlupM31GEC8rYu7CgbLI30bRRrrWrUzwtpKTtGW77RECzK7qyKAoBJs8fmKNrjSEi0ivTGB8eQLhaXUKvU91qi+XTlO3Tqo7H/mawqK8el0V3TXIzqC2x/6sIYovP/G4oYf+JNHTllDr6MHakTrN1PIXfvS2McvWvMKM4PWzP+FYp1yuXwoJ1FesLJJszwNEAwuSrxKp0Srm7Ags/xsQxgu0mz6b3KJ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GdYYsJNsdnQd6iS3roEPOXwaCpoMDDt7rTl+5GfeC2RqnhbAqGKnJ7H1sfcmfPT461zX+mj3Hw3cBDSZoddOiD0sqtTm1/FQHKPc5mByXRCzc9L4XaEzaXUCuRhj4zccDzdPdnzOPL7YQHtnwJjtNrWkJ+eVhsi1vDw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.002814, 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..307ae8b03d0d 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->refaulted = pos->total = 0; pos->gain = gain; + + 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;