From patchwork Thu Dec 22 04:19:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13079364 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 24BC9C4167B for ; Thu, 22 Dec 2022 04:19:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0432494000A; Wed, 21 Dec 2022 23:19:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F0E15940007; Wed, 21 Dec 2022 23:19:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CEAFB94000A; Wed, 21 Dec 2022 23:19:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BB86B940007 for ; Wed, 21 Dec 2022 23:19:48 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 98E491A058F for ; Thu, 22 Dec 2022 04:19:48 +0000 (UTC) X-FDA: 80268638856.10.AC1356F Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf28.hostedemail.com (Postfix) with ESMTP id 10C41C0007 for ; Thu, 22 Dec 2022 04:19:46 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LGK8h9wz; spf=pass (imf28.hostedemail.com: domain of 34tqjYwYKCEsB7Cun1t11tyr.p1zyv07A-zzx8npx.14t@flex--yuzhao.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=34tqjYwYKCEsB7Cun1t11tyr.p1zyv07A-zzx8npx.14t@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671682787; a=rsa-sha256; cv=none; b=2M5hSaRuihNBlfyxXUnIhpYuuEd9Y88/0MJLB24MWT4LruEEmJfNWF8SmuP2VtSq4QE+Pi qfKvxV+ZfOidVMLs3axlhhRLNun4zY612eUJxQudEo5pTowL+9LiQmJBZS1C9hUqAiO90B gE75bD+9THaNZf8Uze01sXXWLE0/2zg= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LGK8h9wz; spf=pass (imf28.hostedemail.com: domain of 34tqjYwYKCEsB7Cun1t11tyr.p1zyv07A-zzx8npx.14t@flex--yuzhao.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=34tqjYwYKCEsB7Cun1t11tyr.p1zyv07A-zzx8npx.14t@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=1671682787; 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=nzLJiZ4QM5Uh21mWC41LC4GBB5ZaSED6XDHCtugxc08=; b=gVmyn+XU1S7Syc2fbDi3neqHRhliJWi+fTtsVnIDeFWDALkrDME8Y0EKJP1TS7qariFpBR 1jaq+HITLKR/PI3WLAYXwIEkXl6q1ntulXilVOAy2mjsVuyj6aeC37/rpWbjmDPeYqyLk2 t9ZNrfrfs6/hmvtHEvXCdCQqyddFJTM= Received: by mail-yb1-f201.google.com with SMTP id r7-20020a25c107000000b006ff55ac0ee7so669396ybf.15 for ; Wed, 21 Dec 2022 20:19:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=nzLJiZ4QM5Uh21mWC41LC4GBB5ZaSED6XDHCtugxc08=; b=LGK8h9wzmfZbkub7vTd8RNWpNWLSzm7G1OuZlNkyjd72d/DuTcDHYUL4xBQcaqX+6a iBARPo3dG0EKbr43vnaB9kXhiLdIVarVSxPRMSmvLc3GrMSsLYLgsfAVBTIA/B44vjLs so6T2/KZM+3aQXkpITzI4UM8KbocN8LznaSbs2lDpTf74F8ww3eWoj7GwxhNNeTZDBGS 3JuGx5KWL6WL3jJU6O7VPlUhEW4+O/Q3JOV/j8eDtLU8oYPgWtWgLX5JetQCIv39UVGk 62+J38ROwufIeFkVog6el5JN4Yi8QoJ0KVmDw0YykY5v+DyL1rqOiHGd579YyrOxuB05 c1Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nzLJiZ4QM5Uh21mWC41LC4GBB5ZaSED6XDHCtugxc08=; b=FPoMiOuo5BojukYZwEluYSzEWXHnMXZVquUzhvgRXSEbPu8Osl+4L4luo3erYWpBTO scwoHoegwQynr9GYrjzz+U/op9Vz6E/f84xOQ6oPvlGBT2IVNcKQKZyr63nMFYsYYxH/ S37cb65Ad0ICqE3LzND9upFTn1b4n7CnAunlm1kT50FAQ6WEkAlmUQFZWzRefsrUNTpw veuJ9kZBotpT8+69BKpLNAPZ+JXeLOW1q92yMkCrWewwPqo3XbT2wshjWSIznqTDdZu/ MQXgPuzDM+HnRDoVscv+4FdeSCNJEokcs0NNmFpzz0LS1+jrUYjm/QKWdLslykUjvAZ6 uJnw== X-Gm-Message-State: AFqh2kpiNBnNexGWWXtatEHLHZ7DgySfHOS0W2R6UVf/IqR3FhrF5Z/b Gt67EtdNfiSNrGAkdPLw31L5JUE3iOM= X-Google-Smtp-Source: AMrXdXu349F+GjLv+LsWuQvHnJ0vl+EFKxOrB01QlRFTB17yje2mwF78wvgJ6L9LCsoyXjHckCuxD+e8kGc= X-Received: from yuzhao.bld.corp.google.com ([2620:15c:183:200:a463:5f7b:440e:5c77]) (user=yuzhao job=sendgmr) by 2002:a05:690c:c95:b0:36c:aaa6:e571 with SMTP id cm21-20020a05690c0c9500b0036caaa6e571mr312608ywb.467.1671682786236; Wed, 21 Dec 2022 20:19:46 -0800 (PST) Date: Wed, 21 Dec 2022 21:19:02 -0700 In-Reply-To: <20221222041905.2431096-1-yuzhao@google.com> Message-Id: <20221222041905.2431096-5-yuzhao@google.com> Mime-Version: 1.0 References: <20221222041905.2431096-1-yuzhao@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH mm-unstable v3 4/8] mm: multi-gen LRU: remove aging fairness safeguard From: Yu Zhao To: Andrew Morton Cc: Johannes Weiner , Jonathan Corbet , Michael Larabel , Michal Hocko , Mike Rapoport , Roman Gushchin , Suren Baghdasaryan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-mm@google.com, Yu Zhao X-Rspam-User: X-Rspamd-Queue-Id: 10C41C0007 X-Rspamd-Server: rspam01 X-Stat-Signature: j1a6fbugp6wsetumy5t318uu9jdkqj3s X-HE-Tag: 1671682786-149337 X-HE-Meta: U2FsdGVkX1/A3FMrpUrWhd75+RVlykDLEtbB5uEn5d2sYKoNILgoCgtiowbIYZEHJwMvNBWeVsK79YejdOOU4JgHbX3JchNHTMWh2rjx4D2sgXmyUm2GshNvYHR/W9Bmida3l0mvcNUReeHfU1Fmw8OPgS3neFApdSha3jerxlh9qCy5vLtUWJmgo/RyI8iJxRlFq5H4DDWRzIsTfNqct3okWRuH5wP07LomjY37lKJtLNQcEV9lk43zX8iC2CBAlAQ2f6HGAWJCecyKOhBgGLxfTzoaT0N2mrx+fDeVZnHTp+2bHvwTqPPkJ4ydBSxGYR6f/IjyFzo6IPAgU97g1BPRRAtMuBrxeHKjKWNLJ9tKj5jm4joAJ3F4cwkmrAzSFHjtax3+jc8NMPk2G0DQoDm56KkilGYC/A5fWun62c2g4Kuf1FtBHm9lv67auv9qdI38m2AEDMVGYisuWdmwoOIrbymrm+GyuTAQmJknuUBMcI5JMMiPN57nLF2zob+bSDOdnej2KrH1K5zsXTgXEmrrQLNmVj81fXDTNlXca40I9fU7VvlMlPnod++MpVZ9Qsb6dLdDAZPCxP3tUHtTa8c1gFcIWgzjDwQBUWeZPmSnORX2aweQ5D9MulMGK+1Jv+2YYktLviXeaSVBZ9qDG/NNEaeawrfNJ/az6OhvPZ27DW8iUpX0CNSbzIOg7j0qxDEbmpFB1AafntfAFdvr6s4Un2Kc8/r9etyFipI8XmgQfbwVaQoyf3YBr100/Fu8OGatv373bwp8flbF3vhB/wvOxykHsQIkul4W6f2f7TLCsZyuwMET5Z+3AghGYyMR6cxoVsfAl+tQcKHPQFUFw6DSBP0rYHXKofko2qkYsqiJm+BOZo8/mwBORyawotxyirOj7Pq11lk/B4yv7Z+S5HogE1mflliDexKwKb+OLfISF0m8Op9lfi5tvaKO4Adcn7eYu0m4pj7KLk+Qimb B0zDGwIX CsUXUiOPGmb6H+U1VjfE7/eLHBcC7oq+QulM0e4GZrZgMpHuSahzzuaoultF6Lppe0Xy5QL/v5Ukm+CJBEhCRI93sDM9xAyzR8etwdO1eHqcQ9vGRhSZY2IJgm34vu44AVhoLbdAxK60OEbW2OL7cQ61X+8t1glq4nSRPJX3pdyx6nk6HySzAmamhDW0t4X19rlF4UMEVpxC5kIYKfhnlW0GU4y922AKjMDaudArzV6gHR4g650tCGALLCyk/aYhYHGJaeA5VysOevj2bDuGHqgtHUcLoLUtCIyKX7SbTNU0l22Z2fB2t4Xd4IkTYT5uBordNh1E0/zDzxH/GrrToRvqBdLrqy9kSB/ow0z4mA3vgukkNfkAVaSgRMA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Recall that the aging produces the youngest generation: first it scans for accessed folios and updates their gen counters; then it increments lrugen->max_seq. The current aging fairness safeguard for kswapd uses two passes to ensure the fairness to multiple eligible memcgs. On the first pass, which is shared with the eviction, it checks whether all eligible memcgs are low on cold folios. If so, it requires a second pass, on which it ages all those memcgs at the same time. With memcg LRU, the aging, while ensuring eventual fairness, will run when necessary. Therefore the current aging fairness safeguard for kswapd will not be needed. Note that memcg LRU only applies to global reclaim. For memcg reclaim, the aging can be unfair to different memcgs, i.e., their lrugen->max_seq can be incremented at different paces. Signed-off-by: Yu Zhao --- mm/vmscan.c | 150 +++++++++++++++++++++++++--------------------------- 1 file changed, 71 insertions(+), 79 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 49d7c103906a..65cc82208b6e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -137,7 +137,6 @@ struct scan_control { #ifdef CONFIG_LRU_GEN /* help kswapd make better choices among multiple memcgs */ - unsigned int memcgs_need_aging:1; unsigned long last_reclaimed; #endif @@ -4468,7 +4467,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, return true; } -static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, unsigned long *min_seq, +static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, struct scan_control *sc, bool can_swap, unsigned long *nr_to_scan) { int gen, type, zone; @@ -4477,6 +4476,13 @@ static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, unsig unsigned long total = 0; struct lru_gen_folio *lrugen = &lruvec->lrugen; struct mem_cgroup *memcg = lruvec_memcg(lruvec); + DEFINE_MIN_SEQ(lruvec); + + /* whether this lruvec is completely out of cold folios */ + if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) { + *nr_to_scan = 0; + return true; + } for (type = !can_swap; type < ANON_AND_FILE; type++) { unsigned long seq; @@ -4505,8 +4511,6 @@ static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, unsig * stalls when the number of generations reaches MIN_NR_GENS. Hence, the * ideal number of generations is MIN_NR_GENS+1. */ - if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) - return true; if (min_seq[!can_swap] + MIN_NR_GENS < max_seq) return false; @@ -4525,40 +4529,54 @@ static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, unsig return false; } -static bool age_lruvec(struct lruvec *lruvec, struct scan_control *sc, unsigned long min_ttl) +static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc) { - bool need_aging; - unsigned long nr_to_scan; - int swappiness = get_swappiness(lruvec, sc); + int gen, type, zone; + unsigned long total = 0; + bool can_swap = get_swappiness(lruvec, sc); + struct lru_gen_folio *lrugen = &lruvec->lrugen; struct mem_cgroup *memcg = lruvec_memcg(lruvec); DEFINE_MAX_SEQ(lruvec); DEFINE_MIN_SEQ(lruvec); + for (type = !can_swap; type < ANON_AND_FILE; type++) { + unsigned long seq; + + for (seq = min_seq[type]; seq <= max_seq; seq++) { + gen = lru_gen_from_seq(seq); + + for (zone = 0; zone < MAX_NR_ZONES; zone++) + total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); + } + } + + /* whether the size is big enough to be helpful */ + return mem_cgroup_online(memcg) ? (total >> sc->priority) : total; +} + +static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc, + unsigned long min_ttl) +{ + int gen; + unsigned long birth; + struct mem_cgroup *memcg = lruvec_memcg(lruvec); + DEFINE_MIN_SEQ(lruvec); + VM_WARN_ON_ONCE(sc->memcg_low_reclaim); + /* see the comment on lru_gen_folio */ + gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]); + birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); + + if (time_is_after_jiffies(birth + min_ttl)) + return false; + + if (!lruvec_is_sizable(lruvec, sc)) + return false; + mem_cgroup_calculate_protection(NULL, memcg); - if (mem_cgroup_below_min(NULL, memcg)) - return false; - - need_aging = should_run_aging(lruvec, max_seq, min_seq, sc, swappiness, &nr_to_scan); - - if (min_ttl) { - int gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]); - unsigned long birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); - - if (time_is_after_jiffies(birth + min_ttl)) - return false; - - /* the size is likely too small to be helpful */ - if (!nr_to_scan && sc->priority != DEF_PRIORITY) - return false; - } - - if (need_aging) - try_to_inc_max_seq(lruvec, max_seq, sc, swappiness, false); - - return true; + return !mem_cgroup_below_min(NULL, memcg); } /* to protect the working set of the last N jiffies */ @@ -4567,46 +4585,32 @@ static unsigned long lru_gen_min_ttl __read_mostly; static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) { struct mem_cgroup *memcg; - bool success = false; unsigned long min_ttl = READ_ONCE(lru_gen_min_ttl); VM_WARN_ON_ONCE(!current_is_kswapd()); sc->last_reclaimed = sc->nr_reclaimed; - /* - * To reduce the chance of going into the aging path, which can be - * costly, optimistically skip it if the flag below was cleared in the - * eviction path. This improves the overall performance when multiple - * memcgs are available. - */ - if (!sc->memcgs_need_aging) { - sc->memcgs_need_aging = true; - return; - } - - set_mm_walk(pgdat); - - memcg = mem_cgroup_iter(NULL, NULL, NULL); - do { - struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); - - if (age_lruvec(lruvec, sc, min_ttl)) - success = true; - - cond_resched(); - } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); - - clear_mm_walk(); - /* check the order to exclude compaction-induced reclaim */ - if (success || !min_ttl || sc->order) + if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY) return; + memcg = mem_cgroup_iter(NULL, NULL, NULL); + do { + struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); + + if (lruvec_is_reclaimable(lruvec, sc, min_ttl)) { + mem_cgroup_iter_break(NULL, memcg); + return; + } + + cond_resched(); + } while ((memcg = mem_cgroup_iter(NULL, memcg, NULL))); + /* * The main goal is to OOM kill if every generation from all memcgs is * younger than min_ttl. However, another possibility is all memcgs are - * either below min or empty. + * either too small or below min. */ if (mutex_trylock(&oom_lock)) { struct oom_control oc = { @@ -5114,34 +5118,28 @@ static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swap * reclaim. */ static unsigned long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, - bool can_swap, bool *need_aging) + bool can_swap) { unsigned long nr_to_scan; struct mem_cgroup *memcg = lruvec_memcg(lruvec); DEFINE_MAX_SEQ(lruvec); - DEFINE_MIN_SEQ(lruvec); if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg) || (mem_cgroup_below_low(sc->target_mem_cgroup, memcg) && !sc->memcg_low_reclaim)) return 0; - *need_aging = should_run_aging(lruvec, max_seq, min_seq, sc, can_swap, &nr_to_scan); - if (!*need_aging) + if (!should_run_aging(lruvec, max_seq, sc, can_swap, &nr_to_scan)) return nr_to_scan; /* skip the aging path at the default priority */ if (sc->priority == DEF_PRIORITY) - goto done; - - /* leave the work to lru_gen_age_node() */ - if (current_is_kswapd()) - return 0; - - if (try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false)) return nr_to_scan; -done: - return min_seq[!can_swap] + MIN_NR_GENS <= max_seq ? nr_to_scan : 0; + + try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, false); + + /* skip this lruvec as it's low on cold folios */ + return 0; } static unsigned long get_nr_to_reclaim(struct scan_control *sc) @@ -5160,9 +5158,7 @@ static unsigned long get_nr_to_reclaim(struct scan_control *sc) static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) { struct blk_plug plug; - bool need_aging = false; unsigned long scanned = 0; - unsigned long reclaimed = sc->nr_reclaimed; unsigned long nr_to_reclaim = get_nr_to_reclaim(sc); lru_add_drain(); @@ -5183,13 +5179,13 @@ static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc else swappiness = 0; - nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness, &need_aging); + nr_to_scan = get_nr_to_scan(lruvec, sc, swappiness); if (!nr_to_scan) - goto done; + break; delta = evict_folios(lruvec, sc, swappiness); if (!delta) - goto done; + break; scanned += delta; if (scanned >= nr_to_scan) @@ -5201,10 +5197,6 @@ static void lru_gen_shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc cond_resched(); } - /* see the comment in lru_gen_age_node() */ - if (sc->nr_reclaimed - reclaimed >= MIN_LRU_BATCH && !need_aging) - sc->memcgs_need_aging = false; -done: clear_mm_walk(); blk_finish_plug(&plug);