Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1587349pxb; Tue, 8 Feb 2022 23:14:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwQTm2sebdhg7T7bPpTA7wZdAHg9MW+K7uJ6RXnGpmIOBiP/iuk8ZhtZE9oMSxKM2g5qVXr X-Received: by 2002:a17:906:b848:: with SMTP id ga8mr733361ejb.268.1644390863311; Tue, 08 Feb 2022 23:14:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644390863; cv=none; d=google.com; s=arc-20160816; b=rTYAgPHbcQbsD8jO+S+t3sFbtO/jRG0qkAkc5cfn5EBrdhluWNKpDHbwFjzrx13AIk oGptyUk8pAY+DgaZKIaQhLKgXSLcCD1zHsJbi2tzGqPVQj4lO/F9jq2pcddXzLCMHUir +o37n//AVjKrG6aA8kuMWbDZgXDcaWrQSTZANdQHX1rqDr1BEGS/+m202zPPwTKBC6/t eCcfSWVMXuJ+Z3fVvvkvot/g7nHm+bEcVjnp3f84GnONa3heyK/UCGS6TA7B35lj9Nnf t1cvt5F9G4sV/TWRS9xOyjF9qAXJxTOw7DroRvejOJucOfLbozZo5fG+Xemg4a9eD/yh KMnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :references:mime-version:message-id:in-reply-to:date:dkim-signature; bh=gVDBJVPByyZFfRkdZeqZdQGSh6CIxqnMnG9KKvEiV/c=; b=kSvVViRXWu2I++pEnzRlE5fJ93P5kG0a2ARD6VceJVnDQkOHg9cLgG9wXIPe7rf2pj yTXrPVldeELdyN2gZqr8PvgQqhE3sZescbE15UZnvi/k6sq68Uqppz2VAijZS/+Sc/QS 01nWg1Vf8QLqcXWAqFBDR74OuzeJy2WTPyEMZVOd6sCWyy0Km4NPEahTF5UpLTAPMx5C d5xejKM3OJlB/QFuOFAXMfwTljVAzAzi8t00eoOSn64LMd6n3yO2A974Er1UGFeAvsHC AGJ1PNWI7Ed1Tk68rZBs5yd3RJ55A+i51xwr+2KzUiNlSY2fcywZ+a2kNiQi+aSksH2A sWGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="k1U/Rnjg"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hr4si13221683ejc.94.2022.02.08.23.13.58; Tue, 08 Feb 2022 23:14:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="k1U/Rnjg"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349376AbiBHIUs (ORCPT + 99 others); Tue, 8 Feb 2022 03:20:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349031AbiBHITs (ORCPT ); Tue, 8 Feb 2022 03:19:48 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BA23C03FEE4 for ; Tue, 8 Feb 2022 00:19:43 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id z15-20020a25bb0f000000b00613388c7d99so34028744ybg.8 for ; Tue, 08 Feb 2022 00:19:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=gVDBJVPByyZFfRkdZeqZdQGSh6CIxqnMnG9KKvEiV/c=; b=k1U/RnjgN44585TzVZG62xdMrgfViRuIbfO6WMgZwLuBnY0x8i2V2JNe1CSsECYKpK tz30HjiqxAf+qea5BIW2tWIMCZFvq2VYri8UeZ8hxC9342EbBvEGx9S/TvlAKIv5xq24 2KDvJ47TRSND65HiyVnOsQwuCGDopg/ITjzFDCTzywhBsZl53/mrG73AmEXt0/+vBG4J PPc9aYlDMPfNehvVFf0t1V/K1TRrDcmEjBh7XCJEvZhTE5Xcpp9qH9ugkqkS0uxqOO1Y SZRhev3Q/t8CIj0Rrl+8p5P9L8O03yqHNT12sJLoZ9rTejJhasYfHm+tsABB0IaN5TVS x00A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=gVDBJVPByyZFfRkdZeqZdQGSh6CIxqnMnG9KKvEiV/c=; b=nABMwrO+utSwIibKBbVbKdpZhGOhnvaXxR2Fenb0v67a5bhJB+bFtTjtRp/TLYOvwF TozXs+xLeeT7n0IOyd9Fjsvch7iljEHtIPP4Ql2kFHFgQNTjPjG4Ag80P4pNu6oNjXM0 4S/NGyfUzsxLJSDEMkVr503mRaQFwDm+idt/AQ5naVueiFeWpVhF7s8zcDBVUpK3EL7e xq1dd7soB1RwPQUALydU6qCaFlEFKR8CtQ202VYK4hZSZRB6rRq24R5oi0FatAb5RW2l +AKqgfRUL/gwu3Uoxi0GoA4drkHYORE86Nr86VIsJkEz3OAJE7MIeVms4w8Qcc9XXDoa 6lJg== X-Gm-Message-State: AOAM533GjxFqSs2i/sBYlPJwMkMh2KfCbwUlzddaKgtmWsVm+ahuo8QM pIWRI7Pd0zJEHWK82FpdJuBzfge5gqw= X-Received: from yuzhao.bld.corp.google.com ([2620:15c:183:200:5f31:19c3:21f5:7300]) (user=yuzhao job=sendgmr) by 2002:a81:b54a:: with SMTP id c10mr3609142ywk.283.1644308382745; Tue, 08 Feb 2022 00:19:42 -0800 (PST) Date: Tue, 8 Feb 2022 01:19:01 -0700 In-Reply-To: <20220208081902.3550911-1-yuzhao@google.com> Message-Id: <20220208081902.3550911-12-yuzhao@google.com> Mime-Version: 1.0 References: <20220208081902.3550911-1-yuzhao@google.com> X-Mailer: git-send-email 2.35.0.263.gb82422642f-goog Subject: [PATCH v7 11/12] mm: multigenerational LRU: debugfs interface From: Yu Zhao To: Andrew Morton , Johannes Weiner , Mel Gorman , Michal Hocko Cc: Andi Kleen , Aneesh Kumar , Barry Song <21cnbao@gmail.com>, Catalin Marinas , Dave Hansen , Hillf Danton , Jens Axboe , Jesse Barnes , Jonathan Corbet , Linus Torvalds , Matthew Wilcox , Michael Larabel , Mike Rapoport , Rik van Riel , Vlastimil Babka , Will Deacon , Ying Huang , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, page-reclaim@google.com, x86@kernel.org, Yu Zhao , Brian Geffon , Jan Alexander Steffens , Oleksandr Natalenko , Steven Barrett , Suleiman Souhlal , Daniel Byrne , Donald Carr , "=?UTF-8?q?Holger=20Hoffst=C3=A4tte?=" , Konstantin Kharlamov , Shuang Zhai , Sofia Trinh Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add /sys/kernel/debug/lru_gen for working set estimation and proactive reclaim. These features are required to optimize job scheduling (bin packing) in data centers [1][2]. Compared with the page table-based approach and the PFN-based approach, e.g., mm/damon/[vp]addr.c, this lruvec-based approach has the following advantages: 1) It offers better choices because it's aware of memcgs, NUMA nodes, shared mappings and unmapped page cache. 2) It's more scalable because it's O(nr_hot_pages), whereas the PFN-based approach is O(nr_total_pages). Add /sys/kernel/debug/lru_gen_full for debugging. [1] https://research.google/pubs/pub48551/ [2] https://www.cs.cmu.edu/~dskarlat/publications/tmo_asplos22.pdf Signed-off-by: Yu Zhao Acked-by: Brian Geffon Acked-by: Jan Alexander Steffens (heftig) Acked-by: Oleksandr Natalenko Acked-by: Steven Barrett Acked-by: Suleiman Souhlal Tested-by: Daniel Byrne Tested-by: Donald Carr Tested-by: Holger Hoffst=C3=A4tte Tested-by: Konstantin Kharlamov Tested-by: Shuang Zhai Tested-by: Sofia Trinh --- include/linux/nodemask.h | 1 + mm/vmscan.c | 353 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 354 insertions(+) diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h index 567c3ddba2c4..90840c459abc 100644 --- a/include/linux/nodemask.h +++ b/include/linux/nodemask.h @@ -486,6 +486,7 @@ static inline int num_node_state(enum node_states state= ) #define first_online_node 0 #define first_memory_node 0 #define next_online_node(nid) (MAX_NUMNODES) +#define next_memory_node(nid) (MAX_NUMNODES) #define nr_node_ids 1U #define nr_online_nodes 1U =20 diff --git a/mm/vmscan.c b/mm/vmscan.c index 4d37d63668b5..3dfa938a4c4a 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -52,6 +52,8 @@ #include #include #include +#include +#include =20 #include #include @@ -5220,6 +5222,354 @@ static struct attribute_group lru_gen_attr_group = =3D { .attrs =3D lru_gen_attrs, }; =20 +/*************************************************************************= ***** + * debugfs interface + *************************************************************************= *****/ + +static void *lru_gen_seq_start(struct seq_file *m, loff_t *pos) +{ + struct mem_cgroup *memcg; + loff_t nr_to_skip =3D *pos; + + m->private =3D kvmalloc(PATH_MAX, GFP_KERNEL); + if (!m->private) + return ERR_PTR(-ENOMEM); + + memcg =3D mem_cgroup_iter(NULL, NULL, NULL); + do { + int nid; + + for_each_node_state(nid, N_MEMORY) { + if (!nr_to_skip--) + return get_lruvec(memcg, nid); + } + } while ((memcg =3D mem_cgroup_iter(NULL, memcg, NULL))); + + return NULL; +} + +static void lru_gen_seq_stop(struct seq_file *m, void *v) +{ + if (!IS_ERR_OR_NULL(v)) + mem_cgroup_iter_break(NULL, lruvec_memcg(v)); + + kvfree(m->private); + m->private =3D NULL; +} + +static void *lru_gen_seq_next(struct seq_file *m, void *v, loff_t *pos) +{ + int nid =3D lruvec_pgdat(v)->node_id; + struct mem_cgroup *memcg =3D lruvec_memcg(v); + + ++*pos; + + nid =3D next_memory_node(nid); + if (nid =3D=3D MAX_NUMNODES) { + memcg =3D mem_cgroup_iter(NULL, memcg, NULL); + if (!memcg) + return NULL; + + nid =3D first_memory_node; + } + + return get_lruvec(memcg, nid); +} + +static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruve= c, + unsigned long max_seq, unsigned long *min_seq, + unsigned long seq) +{ + int i; + int type, tier; + int hist =3D lru_hist_from_seq(seq); + struct lru_gen_struct *lrugen =3D &lruvec->lrugen; + + for (tier =3D 0; tier < MAX_NR_TIERS; tier++) { + seq_printf(m, " %10d", tier); + for (type =3D 0; type < ANON_AND_FILE; type++) { + unsigned long n[3] =3D {}; + + if (seq =3D=3D max_seq) { + n[0] =3D READ_ONCE(lrugen->avg_refaulted[type][tier]); + n[1] =3D READ_ONCE(lrugen->avg_total[type][tier]); + + seq_printf(m, " %10luR %10luT %10lu ", n[0], n[1], n[2]); + } else if (seq =3D=3D min_seq[type] || NR_HIST_GENS > 1) { + n[0] =3D atomic_long_read(&lrugen->refaulted[hist][type][tier]); + n[1] =3D atomic_long_read(&lrugen->evicted[hist][type][tier]); + if (tier) + n[2] =3D READ_ONCE(lrugen->promoted[hist][type][tier - 1]); + + seq_printf(m, " %10lur %10lue %10lup", n[0], n[1], n[2]); + } else + seq_puts(m, " 0 0 0 "); + } + seq_putc(m, '\n'); + } + + seq_puts(m, " "); + for (i =3D 0; i < NR_MM_STATS; i++) { + if (seq =3D=3D max_seq && NR_HIST_GENS =3D=3D 1) + seq_printf(m, " %10lu%c", READ_ONCE(lruvec->mm_state.stats[hist][i]), + toupper(MM_STAT_CODES[i])); + else if (seq !=3D max_seq && NR_HIST_GENS > 1) + seq_printf(m, " %10lu%c", READ_ONCE(lruvec->mm_state.stats[hist][i]), + MM_STAT_CODES[i]); + else + seq_puts(m, " 0 "); + } + seq_putc(m, '\n'); +} + +static int lru_gen_seq_show(struct seq_file *m, void *v) +{ + unsigned long seq; + bool full =3D !debugfs_real_fops(m->file)->write; + struct lruvec *lruvec =3D v; + struct lru_gen_struct *lrugen =3D &lruvec->lrugen; + int nid =3D lruvec_pgdat(lruvec)->node_id; + struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); + DEFINE_MAX_SEQ(lruvec); + DEFINE_MIN_SEQ(lruvec); + + if (nid =3D=3D first_memory_node) { + const char *path =3D memcg ? m->private : ""; + +#ifdef CONFIG_MEMCG + if (memcg) + cgroup_path(memcg->css.cgroup, m->private, PATH_MAX); +#endif + seq_printf(m, "memcg %5hu %s\n", mem_cgroup_id(memcg), path); + } + + seq_printf(m, " node %5d\n", nid); + + if (!full) + seq =3D min_seq[TYPE_ANON]; + else if (max_seq >=3D MAX_NR_GENS) + seq =3D max_seq - MAX_NR_GENS + 1; + else + seq =3D 0; + + for (; seq <=3D max_seq; seq++) { + int gen, type, zone; + unsigned int msecs; + + gen =3D lru_gen_from_seq(seq); + msecs =3D jiffies_to_msecs(jiffies - READ_ONCE(lrugen->timestamps[gen]))= ; + + seq_printf(m, " %10lu %10u", seq, msecs); + + for (type =3D 0; type < ANON_AND_FILE; type++) { + long size =3D 0; + + if (seq < min_seq[type]) { + seq_puts(m, " -0 "); + continue; + } + + for (zone =3D 0; zone < MAX_NR_ZONES; zone++) + size +=3D READ_ONCE(lrugen->nr_pages[gen][type][zone]); + + seq_printf(m, " %10lu ", max(size, 0L)); + } + + seq_putc(m, '\n'); + + if (full) + lru_gen_seq_show_full(m, lruvec, max_seq, min_seq, seq); + } + + return 0; +} + +static const struct seq_operations lru_gen_seq_ops =3D { + .start =3D lru_gen_seq_start, + .stop =3D lru_gen_seq_stop, + .next =3D lru_gen_seq_next, + .show =3D lru_gen_seq_show, +}; + +static int run_aging(struct lruvec *lruvec, unsigned long seq, struct scan= _control *sc, + bool can_swap, bool full_scan) +{ + DEFINE_MAX_SEQ(lruvec); + + if (seq =3D=3D max_seq) + try_to_inc_max_seq(lruvec, max_seq, sc, can_swap, full_scan); + + return seq > max_seq ? -EINVAL : 0; +} + +static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct s= can_control *sc, + int swappiness, unsigned long nr_to_reclaim) +{ + struct blk_plug plug; + int err =3D -EINTR; + DEFINE_MAX_SEQ(lruvec); + + if (max_seq < seq + MIN_NR_GENS) + return -EINVAL; + + sc->nr_reclaimed =3D 0; + + blk_start_plug(&plug); + + while (!signal_pending(current)) { + DEFINE_MIN_SEQ(lruvec); + + if (seq < min_seq[!swappiness] || sc->nr_reclaimed >=3D nr_to_reclaim || + !evict_folios(lruvec, sc, swappiness, NULL)) { + err =3D 0; + break; + } + + cond_resched(); + } + + blk_finish_plug(&plug); + + return err; +} + +static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq, + struct scan_control *sc, int swappiness, unsigned long opt) +{ + struct lruvec *lruvec; + int err =3D -EINVAL; + struct mem_cgroup *memcg =3D NULL; + + if (!mem_cgroup_disabled()) { + rcu_read_lock(); + memcg =3D mem_cgroup_from_id(memcg_id); +#ifdef CONFIG_MEMCG + if (memcg && !css_tryget(&memcg->css)) + memcg =3D NULL; +#endif + rcu_read_unlock(); + + if (!memcg) + goto done; + } + if (memcg_id !=3D mem_cgroup_id(memcg)) + goto done; + + if (nid < 0 || nid >=3D MAX_NUMNODES || !node_state(nid, N_MEMORY)) + goto done; + + lruvec =3D get_lruvec(memcg, nid); + + if (swappiness < 0) + swappiness =3D get_swappiness(memcg); + else if (swappiness > 200) + goto done; + + switch (cmd) { + case '+': + err =3D run_aging(lruvec, seq, sc, swappiness, opt); + break; + case '-': + err =3D run_eviction(lruvec, seq, sc, swappiness, opt); + break; + } +done: + mem_cgroup_put(memcg); + + return err; +} + +static ssize_t lru_gen_seq_write(struct file *file, const char __user *src= , + size_t len, loff_t *pos) +{ + void *buf; + char *cur, *next; + unsigned int flags; + int err =3D 0; + struct scan_control sc =3D { + .may_writepage =3D true, + .may_unmap =3D true, + .may_swap =3D true, + .reclaim_idx =3D MAX_NR_ZONES - 1, + .gfp_mask =3D GFP_KERNEL, + }; + + buf =3D kvmalloc(len + 1, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + if (copy_from_user(buf, src, len)) { + kvfree(buf); + return -EFAULT; + } + + next =3D buf; + next[len] =3D '\0'; + + sc.reclaim_state.mm_walk =3D alloc_mm_walk(); + if (!sc.reclaim_state.mm_walk) { + kvfree(buf); + return -ENOMEM; + } + + flags =3D memalloc_noreclaim_save(); + set_task_reclaim_state(current, &sc.reclaim_state); + + while ((cur =3D strsep(&next, ",;\n"))) { + int n; + int end; + char cmd; + unsigned int memcg_id; + unsigned int nid; + unsigned long seq; + unsigned int swappiness =3D -1; + unsigned long opt =3D -1; + + cur =3D skip_spaces(cur); + if (!*cur) + continue; + + n =3D sscanf(cur, "%c %u %u %lu %n %u %n %lu %n", &cmd, &memcg_id, &nid, + &seq, &end, &swappiness, &end, &opt, &end); + if (n < 4 || cur[end]) { + err =3D -EINVAL; + break; + } + + err =3D run_cmd(cmd, memcg_id, nid, seq, &sc, swappiness, opt); + if (err) + break; + } + + set_task_reclaim_state(current, NULL); + memalloc_noreclaim_restore(flags); + + free_mm_walk(sc.reclaim_state.mm_walk); + kvfree(buf); + + return err ? : len; +} + +static int lru_gen_seq_open(struct inode *inode, struct file *file) +{ + return seq_open(file, &lru_gen_seq_ops); +} + +static const struct file_operations lru_gen_rw_fops =3D { + .open =3D lru_gen_seq_open, + .read =3D seq_read, + .write =3D lru_gen_seq_write, + .llseek =3D seq_lseek, + .release =3D seq_release, +}; + +static const struct file_operations lru_gen_ro_fops =3D { + .open =3D lru_gen_seq_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D seq_release, +}; + /*************************************************************************= ***** * initialization *************************************************************************= *****/ @@ -5289,6 +5639,9 @@ static int __init init_lru_gen(void) if (sysfs_create_group(mm_kobj, &lru_gen_attr_group)) pr_err("lru_gen: failed to create sysfs group\n"); =20 + debugfs_create_file("lru_gen", 0644, NULL, NULL, &lru_gen_rw_fops); + debugfs_create_file("lru_gen_full", 0444, NULL, NULL, &lru_gen_ro_fops); + return 0; }; late_initcall(init_lru_gen); --=20 2.35.0.263.gb82422642f-goog