From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 A4A0F1A2556 for ; Tue, 13 Aug 2024 16:59:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568382; cv=none; b=AtHaZI0xsFAsLqjxZ+5Tpb316AhVRxcsqtQetj2qCyM4VsWC3l6ORopA0SR0OvifBDzUOMFzR4sG8CSQ/Xv2uv8S8inVF3ICIaS4DFm6CW1W2Ir0saBz7oPjImnK+lA+8q35Ixt6c1Aqw91JDg7eJh9abHBuS0VCSHZGmM1SZ+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568382; c=relaxed/simple; bh=KnjEyofW4VxEK8ywkwzm58LdL5VEXo3D8VuQsrfiLNg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mnq7sOkf4YD+ZtHYl+HGERjH0DxXkSr8ulFa6lAr+cNHe+eWtKZlZT6GOn2i5h2o87AtRhaO+6Mj9a9NYwzyKuFHwRcskyibQP3dB3TCeqB83I8d2mECkoP/yzSNNQGGhu+0Vtt635bXPHBbAUcu481mqo1D9SF5Loq8x6QGvoU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TeySKkt+; arc=none smtp.client-ip=209.85.128.202 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TeySKkt+" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6688c44060fso132242117b3.2 for ; Tue, 13 Aug 2024 09:59:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568378; x=1724173178; 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=N9o3rrXvjK3EuUuaBsq4Dq2VUkpZajOoCi9sWYw3qPg=; b=TeySKkt+g8I8y09Vzu8l6crYWFqw6/5TfYOxNg03nJzsYBTD98Wnru5a09LfdN0Lej uf7oTJlXw3z6Xm3yTxbkOvStcw2iVFEr4MIpnM1GnRMrTbSBlEevdsVDsm74SPT5iRkl SnnVJBsbvCQJIscv42gfR2WKFACl40IaTy3nmlkcKvWmWlbys8I/w3uE58md6Xk7DmL6 MILFjl91/JOT+7E92jfbgbjU+P4q9paNoOWF6S5kypuQOrZPdCPKsmjPZs9vjXYKYlUP M/B/bmgYgxKfg701hrdaImNwBM9x9GYK+NtBEmOnnCMtxfO6dNiIS4/4gzbBuDZkFHON 9NXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568378; x=1724173178; 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=N9o3rrXvjK3EuUuaBsq4Dq2VUkpZajOoCi9sWYw3qPg=; b=swvGewa9MBWRtNcDl+EcEe1HUoWgqspOC60EBU65I/pjDJMhWsvOHZNze0tIF8xi/T j7jii+Q6Ja0/qYfhLYd7nUAbsJ14v2xc5HRtfW5nKGvn/7QAEKoFA2CFMcPcS0iikZc7 ewV2xPjVIvz03Q2lKG4K3W2bKj0Qk9loNYU7RApK+fQk05vnbvpJRTn0yQFmJ798w+dW MXINHwSVtuOdDY7FdiFAcPxmcSbM94Vcx4HdFWPCCq+hPxYhWZ3e64Tq+6sSd1PZNv2C Pj1tjGfreQHizvrBSsan+r8l3SSQ9R+rqwR/7EANhcQUyLvLpA13fi6Z0HWlk7U9T3pZ gMJg== X-Forwarded-Encrypted: i=1; AJvYcCV8r2TnVlZoi1IEMn8JVV74Xqo4TVWVQbsIJZXm5yXshVqyAK0l9kzaMwj3D7IJVO3MDtoYDa/C6UlEc1qH5ZTl0eSPihPbwxoQSrr9 X-Gm-Message-State: AOJu0YwWb8MM3IMRajvy5iRqc9uYxBm9PkkerY2/COTYne59ss/dOMjd S+7fBzoLUp0ENbPe2D4dw64Oqnwu1Uid5IzWT4oy5O80/dqfe9z21zEHL88RylQI1h6j8YZgfK1 /TMBDIw== X-Google-Smtp-Source: AGHT+IFcT1PaXJoLmzamp9fHH9zjFph1X3u8jmQRIW7x8w/hZfTh+8gA42aT7LBM4EO5qJIAh6kA4AaV1iY6 X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a81:8506:0:b0:691:2f66:4af9 with SMTP id 00721157ae682-6ac98b677b4mr1967b3.4.1723568377681; Tue, 13 Aug 2024 09:59:37 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:12 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-2-yuanchu@google.com> Subject: [PATCH v3 1/7] mm: aggregate working set information into histograms From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hierarchically aggregate all memcgs' MGLRU generations and their page counts into working set page age histograms. The histograms break down the system's working set per-node, per-anon/file. The sysfs interfaces are as follows: /sys/devices/system/node/nodeX/workingset_report/page_age A per-node page age histogram, showing an aggregate of the node's lruvecs. The information is extracted from MGLRU's per-generation page counters. Reading this file causes a hierarchical aging of all lruvecs, scanning pages and creates a new generation in each lruvec. For example: 1000 anon=3D0 file=3D0 2000 anon=3D0 file=3D0 100000 anon=3D5533696 file=3D5566464 18446744073709551615 anon=3D0 file=3D0 /sys/devices/system/node/nodeX/workingset_report/page_age_interval A comma separated list of time in milliseconds that configures what the page age histogram uses for aggregation. Signed-off-by: Yuanchu Xie Change-Id: I88fb6183f511f5a697c91a14619ee8e71e98e553 --- drivers/base/node.c | 6 + include/linux/mmzone.h | 9 + include/linux/workingset_report.h | 79 ++++++ mm/Kconfig | 9 + mm/Makefile | 1 + mm/internal.h | 4 + mm/memcontrol.c | 2 + mm/mm_init.c | 2 + mm/mmzone.c | 2 + mm/vmscan.c | 10 +- mm/workingset_report.c | 451 ++++++++++++++++++++++++++++++ 11 files changed, 571 insertions(+), 4 deletions(-) create mode 100644 include/linux/workingset_report.h create mode 100644 mm/workingset_report.c diff --git a/drivers/base/node.c b/drivers/base/node.c index eb72580288e6..ba5b8720dbfa 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include =20 static const struct bus_type node_subsys =3D { .name =3D "node", @@ -626,6 +628,7 @@ static int register_node(struct node *node, int num) } else { hugetlb_register_node(node); compaction_register_node(node); + wsr_init_sysfs(node); } =20 return error; @@ -642,6 +645,9 @@ void unregister_node(struct node *node) { hugetlb_unregister_node(node); compaction_unregister_node(node); + wsr_remove_sysfs(node); + wsr_destroy_lruvec(mem_cgroup_lruvec(NULL, NODE_DATA(node->dev.id))); + wsr_destroy_pgdat(NODE_DATA(node->dev.id)); node_remove_accesses(node); node_remove_caches(node); device_unregister(&node->dev); diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 41458892bc8a..57d485f73363 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -24,6 +24,7 @@ #include #include #include +#include =20 /* Free memory management - zoned buddy allocator. */ #ifndef CONFIG_ARCH_FORCE_MAX_ORDER @@ -633,6 +634,9 @@ struct lruvec { struct lru_gen_mm_state mm_state; #endif #endif /* CONFIG_LRU_GEN */ +#ifdef CONFIG_WORKINGSET_REPORT + struct wsr_state wsr; +#endif /* CONFIG_WORKINGSET_REPORT */ #ifdef CONFIG_MEMCG struct pglist_data *pgdat; #endif @@ -1405,6 +1409,11 @@ typedef struct pglist_data { struct lru_gen_memcg memcg_lru; #endif =20 +#ifdef CONFIG_WORKINGSET_REPORT + struct mutex wsr_update_mutex; + struct wsr_report_bins __rcu *wsr_page_age_bins; +#endif + CACHELINE_PADDING(_pad2_); =20 /* Per-node vmstats */ diff --git a/include/linux/workingset_report.h b/include/linux/workingset_r= eport.h new file mode 100644 index 000000000000..d7c2ee14ec87 --- /dev/null +++ b/include/linux/workingset_report.h @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_WORKINGSET_REPORT_H +#define _LINUX_WORKINGSET_REPORT_H + +#include +#include + +struct mem_cgroup; +struct pglist_data; +struct node; +struct lruvec; + +#ifdef CONFIG_WORKINGSET_REPORT + +#define WORKINGSET_REPORT_MIN_NR_BINS 2 +#define WORKINGSET_REPORT_MAX_NR_BINS 32 + +#define WORKINGSET_INTERVAL_MAX ((unsigned long)-1) +#define ANON_AND_FILE 2 + +struct wsr_report_bin { + unsigned long idle_age; + unsigned long nr_pages[ANON_AND_FILE]; +}; + +struct wsr_report_bins { + /* excludes the WORKINGSET_INTERVAL_MAX bin */ + unsigned long nr_bins; + /* last bin contains WORKINGSET_INTERVAL_MAX */ + unsigned long idle_age[WORKINGSET_REPORT_MAX_NR_BINS]; + struct rcu_head rcu; +}; + +struct wsr_page_age_histo { + unsigned long timestamp; + struct wsr_report_bin bins[WORKINGSET_REPORT_MAX_NR_BINS]; +}; + +struct wsr_state { + /* breakdown of workingset by page age */ + struct mutex page_age_lock; + struct wsr_page_age_histo *page_age; +}; + +void wsr_init_lruvec(struct lruvec *lruvec); +void wsr_destroy_lruvec(struct lruvec *lruvec); +void wsr_init_pgdat(struct pglist_data *pgdat); +void wsr_destroy_pgdat(struct pglist_data *pgdat); +void wsr_init_sysfs(struct node *node); +void wsr_remove_sysfs(struct node *node); + +/* + * Returns true if the wsr is configured to be refreshed. + * The next refresh time is stored in refresh_time. + */ +bool wsr_refresh_report(struct wsr_state *wsr, struct mem_cgroup *root, + struct pglist_data *pgdat); +#else +static inline void wsr_init_lruvec(struct lruvec *lruvec) +{ +} +static inline void wsr_destroy_lruvec(struct lruvec *lruvec) +{ +} +static inline void wsr_init_pgdat(struct pglist_data *pgdat) +{ +} +static inline void wsr_destroy_pgdat(struct pglist_data *pgdat) +{ +} +static inline void wsr_init_sysfs(struct node *node) +{ +} +static inline void wsr_remove_sysfs(struct node *node) +{ +} +#endif /* CONFIG_WORKINGSET_REPORT */ + +#endif /* _LINUX_WORKINGSET_REPORT_H */ diff --git a/mm/Kconfig b/mm/Kconfig index b72e7d040f78..951b4e7300d2 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1263,6 +1263,15 @@ config IOMMU_MM_DATA config EXECMEM bool =20 +config WORKINGSET_REPORT + bool "Working set reporting" + depends on LRU_GEN && SYSFS + help + Report system and per-memcg working set to userspace. + + This option exports stats and events giving the user more insight + into its memory working set. + source "mm/damon/Kconfig" =20 endmenu diff --git a/mm/Makefile b/mm/Makefile index d2915f8c9dc0..62cef72ce7f8 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -98,6 +98,7 @@ obj-$(CONFIG_TRANSPARENT_HUGEPAGE) +=3D huge_memory.o khu= gepaged.o obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.o obj-$(CONFIG_MEMCG_V1) +=3D memcontrol-v1.o obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o +obj-$(CONFIG_WORKINGSET_REPORT) +=3D workingset_report.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) +=3D swap_cgroup.o endif diff --git a/mm/internal.h b/mm/internal.h index b4d86436565b..f7d790f5d41c 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -406,11 +406,15 @@ extern unsigned long highest_memmap_pfn; /* * in mm/vmscan.c: */ +struct scan_control; bool isolate_lru_page(struct page *page); bool folio_isolate_lru(struct folio *folio); void putback_lru_page(struct page *page); void folio_putback_lru(struct folio *folio); extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state = reason); +bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, bool can= _swap, + bool force_scan); +void set_task_reclaim_state(struct task_struct *task, struct reclaim_state= *rs); =20 /* * in mm/rmap.c: diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f29157288b7d..5d16fd9d2c3f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -61,6 +61,7 @@ #include #include #include +#include #include "internal.h" #include #include @@ -3504,6 +3505,7 @@ static void free_mem_cgroup_per_node_info(struct mem_= cgroup *memcg, int node) if (!pn) return; =20 + wsr_destroy_lruvec(&pn->lruvec); free_percpu(pn->lruvec_stats_percpu); kfree(pn->lruvec_stats); kfree(pn); diff --git a/mm/mm_init.c b/mm/mm_init.c index 75c3bd42799b..d8fe19fc1cef 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "internal.h" #include "slab.h" #include "shuffle.h" @@ -1378,6 +1379,7 @@ static void __meminit pgdat_init_internals(struct pgl= ist_data *pgdat) =20 pgdat_page_ext_init(pgdat); lruvec_init(&pgdat->__lruvec); + wsr_init_pgdat(pgdat); } =20 static void __meminit zone_init_internals(struct zone *zone, enum zone_typ= e idx, int nid, diff --git a/mm/mmzone.c b/mm/mmzone.c index c01896eca736..477cd5ac1d78 100644 --- a/mm/mmzone.c +++ b/mm/mmzone.c @@ -90,6 +90,8 @@ void lruvec_init(struct lruvec *lruvec) */ list_del(&lruvec->lists[LRU_UNEVICTABLE]); =20 + wsr_init_lruvec(lruvec); + lru_gen_init_lruvec(lruvec); } =20 diff --git a/mm/vmscan.c b/mm/vmscan.c index cfa839284b92..8ab1b456d2cf 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -56,6 +56,7 @@ #include #include #include +#include =20 #include #include @@ -270,8 +271,7 @@ static int sc_swappiness(struct scan_control *sc, struc= t mem_cgroup *memcg) } #endif =20 -static void set_task_reclaim_state(struct task_struct *task, - struct reclaim_state *rs) +void set_task_reclaim_state(struct task_struct *task, struct reclaim_state= *rs) { /* Check for an overwrite */ WARN_ON_ONCE(rs && task->reclaim_state); @@ -3857,8 +3857,8 @@ static bool inc_max_seq(struct lruvec *lruvec, unsign= ed long seq, return success; } =20 -static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) +bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, bool can= _swap, + bool force_scan) { bool success; struct lru_gen_mm_walk *walk; @@ -5631,6 +5631,8 @@ 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 + wsr_init_sysfs(NULL); + 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); =20 diff --git a/mm/workingset_report.c b/mm/workingset_report.c new file mode 100644 index 000000000000..a4dcf62fcd96 --- /dev/null +++ b/mm/workingset_report.c @@ -0,0 +1,451 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "internal.h" + +void wsr_init_pgdat(struct pglist_data *pgdat) +{ + mutex_init(&pgdat->wsr_update_mutex); + RCU_INIT_POINTER(pgdat->wsr_page_age_bins, NULL); +} + +void wsr_destroy_pgdat(struct pglist_data *pgdat) +{ + struct wsr_report_bins __rcu *bins; + + mutex_lock(&pgdat->wsr_update_mutex); + bins =3D rcu_replace_pointer(pgdat->wsr_page_age_bins, NULL, + lockdep_is_held(&pgdat->wsr_update_mutex)); + kfree_rcu(bins, rcu); + mutex_unlock(&pgdat->wsr_update_mutex); + mutex_destroy(&pgdat->wsr_update_mutex); +} + +void wsr_init_lruvec(struct lruvec *lruvec) +{ + struct wsr_state *wsr =3D &lruvec->wsr; + + memset(wsr, 0, sizeof(*wsr)); + mutex_init(&wsr->page_age_lock); +} + +void wsr_destroy_lruvec(struct lruvec *lruvec) +{ + struct wsr_state *wsr =3D &lruvec->wsr; + + mutex_destroy(&wsr->page_age_lock); + kfree(wsr->page_age); + memset(wsr, 0, sizeof(*wsr)); +} + +static int workingset_report_intervals_parse(char *src, + struct wsr_report_bins *bins) +{ + int err =3D 0, i =3D 0; + char *cur, *next =3D strim(src); + + if (*next =3D=3D '\0') + return 0; + + while ((cur =3D strsep(&next, ","))) { + unsigned int interval; + + err =3D kstrtouint(cur, 0, &interval); + if (err) + goto out; + + bins->idle_age[i] =3D msecs_to_jiffies(interval); + if (i > 0 && bins->idle_age[i] <=3D bins->idle_age[i - 1]) { + err =3D -EINVAL; + goto out; + } + + if (++i =3D=3D WORKINGSET_REPORT_MAX_NR_BINS) { + err =3D -ERANGE; + goto out; + } + } + + if (i && i < WORKINGSET_REPORT_MIN_NR_BINS - 1) { + err =3D -ERANGE; + goto out; + } + + bins->nr_bins =3D i; + bins->idle_age[i] =3D WORKINGSET_INTERVAL_MAX; +out: + return err ?: i; +} + +static unsigned long get_gen_start_time(const struct lru_gen_folio *lrugen, + unsigned long seq, + unsigned long max_seq, + unsigned long curr_timestamp) +{ + int younger_gen; + + if (seq =3D=3D max_seq) + return curr_timestamp; + younger_gen =3D lru_gen_from_seq(seq + 1); + return READ_ONCE(lrugen->timestamps[younger_gen]); +} + +static void collect_page_age_type(const struct lru_gen_folio *lrugen, + struct wsr_report_bin *bin, + unsigned long max_seq, unsigned long min_seq, + unsigned long curr_timestamp, int type) +{ + unsigned long seq; + + for (seq =3D max_seq; seq + 1 > min_seq; seq--) { + int gen, zone; + unsigned long gen_end, gen_start, size =3D 0; + + gen =3D lru_gen_from_seq(seq); + + for (zone =3D 0; zone < MAX_NR_ZONES; zone++) + size +=3D max( + READ_ONCE(lrugen->nr_pages[gen][type][zone]), + 0L); + + gen_start =3D get_gen_start_time(lrugen, seq, max_seq, + curr_timestamp); + gen_end =3D READ_ONCE(lrugen->timestamps[gen]); + + while (bin->idle_age !=3D WORKINGSET_INTERVAL_MAX && + time_before(gen_end + bin->idle_age, curr_timestamp)) { + unsigned long gen_in_bin =3D (long)gen_start - + (long)curr_timestamp + + (long)bin->idle_age; + unsigned long gen_len =3D (long)gen_start - (long)gen_end; + + if (!gen_len) + break; + if (gen_in_bin) { + unsigned long split_bin =3D + size / gen_len * gen_in_bin; + + bin->nr_pages[type] +=3D split_bin; + size -=3D split_bin; + } + gen_start =3D curr_timestamp - bin->idle_age; + bin++; + } + bin->nr_pages[type] +=3D size; + } +} + +/* + * proportionally aggregate Multi-gen LRU bins into a working set report + * MGLRU generations: + * current time + * | max_seq timestamp + * | | max_seq - 1 timestamp + * | | | unbounded + * | | | | + * -------------------------------- + * | max_seq | ... | ... | min_seq + * -------------------------------- + * + * Bins: + * + * current time + * | current - idle_age[0] + * | | current - idle_age[1] + * | | | unbounded + * | | | | + * ------------------------------ + * | bin 0 | ... | ... | bin n-1 + * ------------------------------ + * + * Assume the heuristic that pages are in the MGLRU generation + * through uniform accesses, so we can aggregate them + * proportionally into bins. + */ +static void collect_page_age(struct wsr_page_age_histo *page_age, + const struct lruvec *lruvec) +{ + int type; + const struct lru_gen_folio *lrugen =3D &lruvec->lrugen; + unsigned long curr_timestamp =3D jiffies; + unsigned long max_seq =3D READ_ONCE((lruvec)->lrugen.max_seq); + unsigned long min_seq[ANON_AND_FILE] =3D { + READ_ONCE(lruvec->lrugen.min_seq[LRU_GEN_ANON]), + READ_ONCE(lruvec->lrugen.min_seq[LRU_GEN_FILE]), + }; + struct wsr_report_bin *bin =3D &page_age->bins[0]; + + for (type =3D 0; type < ANON_AND_FILE; type++) + collect_page_age_type(lrugen, bin, max_seq, min_seq[type], + curr_timestamp, type); +} + +/* First step: hierarchically scan child memcgs. */ +static void refresh_scan(struct wsr_state *wsr, struct mem_cgroup *root, + struct pglist_data *pgdat) +{ + struct mem_cgroup *memcg; + unsigned int flags; + struct reclaim_state rs =3D { 0 }; + + set_task_reclaim_state(current, &rs); + flags =3D memalloc_noreclaim_save(); + + memcg =3D mem_cgroup_iter(root, NULL, NULL); + do { + struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); + unsigned long max_seq =3D READ_ONCE((lruvec)->lrugen.max_seq); + + /* + * setting can_swap=3Dtrue and force_scan=3Dtrue ensures + * proper workingset stats when the system cannot swap. + */ + try_to_inc_max_seq(lruvec, max_seq, true, true); + cond_resched(); + } while ((memcg =3D mem_cgroup_iter(root, memcg, NULL))); + + memalloc_noreclaim_restore(flags); + set_task_reclaim_state(current, NULL); +} + +/* Second step: aggregate child memcgs into the page age histogram. */ +static void refresh_aggregate(struct wsr_page_age_histo *page_age, + struct mem_cgroup *root, + struct pglist_data *pgdat) +{ + struct mem_cgroup *memcg; + struct wsr_report_bin *bin; + + for (bin =3D page_age->bins; + bin->idle_age !=3D WORKINGSET_INTERVAL_MAX; bin++) { + bin->nr_pages[0] =3D 0; + bin->nr_pages[1] =3D 0; + } + /* the last used bin has idle_age =3D=3D WORKINGSET_INTERVAL_MAX. */ + bin->nr_pages[0] =3D 0; + bin->nr_pages[1] =3D 0; + + memcg =3D mem_cgroup_iter(root, NULL, NULL); + do { + struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); + + collect_page_age(page_age, lruvec); + cond_resched(); + } while ((memcg =3D mem_cgroup_iter(root, memcg, NULL))); + WRITE_ONCE(page_age->timestamp, jiffies); +} + +static void copy_node_bins(struct pglist_data *pgdat, + struct wsr_page_age_histo *page_age) +{ + struct wsr_report_bins *node_page_age_bins; + int i =3D 0; + + rcu_read_lock(); + node_page_age_bins =3D rcu_dereference(pgdat->wsr_page_age_bins); + if (!node_page_age_bins) + goto nocopy; + for (i =3D 0; i < node_page_age_bins->nr_bins; ++i) + page_age->bins[i].idle_age =3D node_page_age_bins->idle_age[i]; + +nocopy: + page_age->bins[i].idle_age =3D WORKINGSET_INTERVAL_MAX; + rcu_read_unlock(); +} + +bool wsr_refresh_report(struct wsr_state *wsr, struct mem_cgroup *root, + struct pglist_data *pgdat) +{ + struct wsr_page_age_histo *page_age; + + if (!READ_ONCE(wsr->page_age)) + return false; + + refresh_scan(wsr, root, pgdat); + mutex_lock(&wsr->page_age_lock); + page_age =3D READ_ONCE(wsr->page_age); + if (page_age) { + copy_node_bins(pgdat, page_age); + refresh_aggregate(page_age, root, pgdat); + } + mutex_unlock(&wsr->page_age_lock); + return !!page_age; +} +EXPORT_SYMBOL_GPL(wsr_refresh_report); + +static struct pglist_data *kobj_to_pgdat(struct kobject *kobj) +{ + int nid =3D IS_ENABLED(CONFIG_NUMA) ? kobj_to_dev(kobj)->id : + first_memory_node; + + return NODE_DATA(nid); +} + +static struct wsr_state *kobj_to_wsr(struct kobject *kobj) +{ + return &mem_cgroup_lruvec(NULL, kobj_to_pgdat(kobj))->wsr; +} + +static ssize_t page_age_intervals_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct wsr_report_bins *bins; + int len =3D 0; + struct pglist_data *pgdat =3D kobj_to_pgdat(kobj); + + rcu_read_lock(); + bins =3D rcu_dereference(pgdat->wsr_page_age_bins); + if (bins) { + int i; + int nr_bins =3D bins->nr_bins; + + for (i =3D 0; i < bins->nr_bins; ++i) { + len +=3D sysfs_emit_at( + buf, len, "%u", + jiffies_to_msecs(bins->idle_age[i])); + if (i + 1 < nr_bins) + len +=3D sysfs_emit_at(buf, len, ","); + } + } + len +=3D sysfs_emit_at(buf, len, "\n"); + rcu_read_unlock(); + + return len; +} + +static ssize_t page_age_intervals_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *src, size_t len) +{ + struct wsr_report_bins *bins =3D NULL, __rcu *old; + char *buf =3D NULL; + int err =3D 0; + struct pglist_data *pgdat =3D kobj_to_pgdat(kobj); + + buf =3D kstrdup(src, GFP_KERNEL); + if (!buf) { + err =3D -ENOMEM; + goto failed; + } + + bins =3D + kzalloc(sizeof(struct wsr_report_bins), GFP_KERNEL); + + if (!bins) { + err =3D -ENOMEM; + goto failed; + } + + err =3D workingset_report_intervals_parse(buf, bins); + if (err < 0) + goto failed; + + if (err =3D=3D 0) { + kfree(bins); + bins =3D NULL; + } + + mutex_lock(&pgdat->wsr_update_mutex); + old =3D rcu_replace_pointer(pgdat->wsr_page_age_bins, bins, + lockdep_is_held(&pgdat->wsr_update_mutex)); + mutex_unlock(&pgdat->wsr_update_mutex); + kfree_rcu(old, rcu); + kfree(buf); + return len; +failed: + kfree(bins); + kfree(buf); + + return err; +} + +static struct kobj_attribute page_age_intervals_attr =3D + __ATTR_RW(page_age_intervals); + +static ssize_t page_age_show(struct kobject *kobj, struct kobj_attribute *= attr, + char *buf) +{ + struct wsr_report_bin *bin; + int ret =3D 0; + struct wsr_state *wsr =3D kobj_to_wsr(kobj); + + + mutex_lock(&wsr->page_age_lock); + if (!wsr->page_age) + wsr->page_age =3D + kzalloc(sizeof(struct wsr_page_age_histo), GFP_KERNEL); + mutex_unlock(&wsr->page_age_lock); + + wsr_refresh_report(wsr, NULL, kobj_to_pgdat(kobj)); + + mutex_lock(&wsr->page_age_lock); + if (!wsr->page_age) + goto unlock; + for (bin =3D wsr->page_age->bins; + bin->idle_age !=3D WORKINGSET_INTERVAL_MAX; bin++) + ret +=3D sysfs_emit_at(buf, ret, "%u anon=3D%lu file=3D%lu\n", + jiffies_to_msecs(bin->idle_age), + bin->nr_pages[0] * PAGE_SIZE, + bin->nr_pages[1] * PAGE_SIZE); + + ret +=3D sysfs_emit_at(buf, ret, "%lu anon=3D%lu file=3D%lu\n", + WORKINGSET_INTERVAL_MAX, + bin->nr_pages[0] * PAGE_SIZE, + bin->nr_pages[1] * PAGE_SIZE); + +unlock: + mutex_unlock(&wsr->page_age_lock); + return ret; +} + +static struct kobj_attribute page_age_attr =3D __ATTR_RO(page_age); + +static struct attribute *workingset_report_attrs[] =3D { + &page_age_intervals_attr.attr, &page_age_attr.attr, NULL +}; + +static const struct attribute_group workingset_report_attr_group =3D { + .name =3D "workingset_report", + .attrs =3D workingset_report_attrs, +}; + +void wsr_init_sysfs(struct node *node) +{ + struct kobject *kobj =3D node ? &node->dev.kobj : mm_kobj; + struct wsr_state *wsr; + + if (IS_ENABLED(CONFIG_NUMA) && !node) + return; + + wsr =3D kobj_to_wsr(kobj); + + if (sysfs_create_group(kobj, &workingset_report_attr_group)) + pr_warn("Workingset report failed to create sysfs files\n"); +} +EXPORT_SYMBOL_GPL(wsr_init_sysfs); + +void wsr_remove_sysfs(struct node *node) +{ + struct kobject *kobj =3D &node->dev.kobj; + struct wsr_state *wsr; + + if (IS_ENABLED(CONFIG_NUMA) && !node) + return; + + wsr =3D kobj_to_wsr(kobj); + sysfs_remove_group(kobj, &workingset_report_attr_group); +} +EXPORT_SYMBOL_GPL(wsr_remove_sysfs); --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 D54591A2C01 for ; Tue, 13 Aug 2024 16:59:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568382; cv=none; b=tWxuU4LWi+a6J3nTEoSWRiQDjbcMPDawRL1JN1oTz5a7IG2vKGBFickMMRg9+LobEB1fj+5dQoBBisLNeOTCnv2dMehhsxEyZE6rj9ffC6YdwaZ7K1cMcRrXE6glkP9aFe6ZrCEBmRdmIZb79bcWzTyIUgN9QBfzJWHFUiR/MlQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568382; c=relaxed/simple; bh=FnqAQTPwgBVVzDcbida/hFCCtbq3A4I5BvukZxTAxKI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jj9HNT3GZqI7WHPfFBk/g+/MxEvsdROc4MVCoYEnlaG9RF4mKx4hQLQF43LXOPsXiDbCJVIc3afYW6EG+vzN4bOOoWVT3Cz1ovG0IjvOXkwIbO49TYYU9WIF87U8R3ux3VdVukyuXpXF7jssqkUJaAUHzuU7zu12xIU/OieFKgs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cf6TjRSD; arc=none smtp.client-ip=209.85.128.201 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cf6TjRSD" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-664fc7c4e51so116144807b3.3 for ; Tue, 13 Aug 2024 09:59:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568380; x=1724173180; 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=Hqd1jKx+q5wvRHFsKe7RtJaTNM99zPu8YKm8b61Swh4=; b=cf6TjRSD7hGi1Z7Qu23XIG7vOAiRc4PCOA0hLBhX5rg3pFiqeJto/+2dl2U1fEBCYI 3Won33Z1AGzKo2aSR5vWaq23zzlSFoGFFa7elnDhiXcQ1/vRt7Hv7drl/fwyi+SZ3Aq7 R63j0YsMj/uct3npuDxpuH0oSbbf7c9nu5otOOiaYrcnJwAeuzAOrJ4ieOwVD9oTSaWq wlQM36pGs6XohQSi17G8MNmqceVsglySsmvk4QW0TniIK4vrekUW5U+9jmTQcq0rYJ+S poJuF4sX8Q2nnyhdkD6joiLR6V1mWT6lKPgESp7azd0Fu9njgBPuABW/i+pGPXJcNoY/ Y6TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568380; x=1724173180; 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=Hqd1jKx+q5wvRHFsKe7RtJaTNM99zPu8YKm8b61Swh4=; b=OZDbEIprO+UvH6NKfDEVdkrZqhTbYP7AvHs4dlDWK6m3sfgd2jGalLvdZceFKKmBx4 ta/cxp2fJUdBFuUkKLGLUgmKXqv1O6cna2oQxnJAmkuUtB2es3oF8EZjov7bls7V+QlW 1cz4tv6ZKYNLBNyU8o49sZkPVo0/nLw8kRsDumNdBOWMmD5fL+iMxIl/In1ANgnBR+7w QIiUgsGZXKT98fhcBdvz3s/s/mULxPtuA88G4DpW+F+UKLMxGMGGpas73BXp3gEOfSpm bHSWQBjAdD/wLgfx045UCpbF7Tv+7aX/bawED20UJOwd2ql5nrgMsC5t7oN6bux46Gg5 OT/A== X-Forwarded-Encrypted: i=1; AJvYcCUNh/g8puuTfWbDeZKEwfp4qx47i1NVRxm7e10029RVtwza6XMJsw356SLGNgcfrWt9R/zpvbIQOXiHb5xLx6VQCJR1xaFurVSNDt0M X-Gm-Message-State: AOJu0YypvRaoU2FAQdcqkUESCypYGmjXxDI6Wq+T7Nl59P5fLcyXwdU3 usdnwvBgcoPSPFFvtb/8O84o3hPO/WWUERkmi/pkr0j/w7rCo+gC52xSYHWYAplwIVYT1kW5imu ABa+K5w== X-Google-Smtp-Source: AGHT+IHcXqA8D3zvHAIQfVpeQAXseyth/6ohOvX1sAG9G57GDA3x2krpy7DDbUSjGWDUUw5XpHr8rLgkLnqj X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a0d:ce43:0:b0:62f:f535:f41 with SMTP id 00721157ae682-6ac9bd76ef6mr1297b3.9.1723568379605; Tue, 13 Aug 2024 09:59:39 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:13 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-3-yuanchu@google.com> Subject: [PATCH v3 2/7] mm: use refresh interval to rate-limit workingset report aggregation From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The refresh interval is a rate limiting factor to workingset page age histogram reads. When a workingset report is generated, a timestamp is noted, and the same report will be read until it expires beyond the refresh interval, at which point a new report is generated. Sysfs interface /sys/devices/system/node/nodeX/workingset_report/refresh_interval time in milliseconds specifying how long the report is valid for Signed-off-by: Yuanchu Xie Change-Id: Iabacec2845a7fac7280ffc5f4faed55186cfa7a2 --- include/linux/workingset_report.h | 1 + mm/workingset_report.c | 84 +++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/include/linux/workingset_report.h b/include/linux/workingset_r= eport.h index d7c2ee14ec87..8bae6a600410 100644 --- a/include/linux/workingset_report.h +++ b/include/linux/workingset_report.h @@ -37,6 +37,7 @@ struct wsr_page_age_histo { }; =20 struct wsr_state { + unsigned long refresh_interval; /* breakdown of workingset by page age */ struct mutex page_age_lock; struct wsr_page_age_histo *page_age; diff --git a/mm/workingset_report.c b/mm/workingset_report.c index a4dcf62fcd96..fe553c0a653e 100644 --- a/mm/workingset_report.c +++ b/mm/workingset_report.c @@ -195,7 +195,8 @@ static void collect_page_age(struct wsr_page_age_histo = *page_age, =20 /* First step: hierarchically scan child memcgs. */ static void refresh_scan(struct wsr_state *wsr, struct mem_cgroup *root, - struct pglist_data *pgdat) + struct pglist_data *pgdat, + unsigned long refresh_interval) { struct mem_cgroup *memcg; unsigned int flags; @@ -208,12 +209,15 @@ static void refresh_scan(struct wsr_state *wsr, struc= t mem_cgroup *root, do { struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); unsigned long max_seq =3D READ_ONCE((lruvec)->lrugen.max_seq); + int gen =3D lru_gen_from_seq(max_seq); + unsigned long birth =3D READ_ONCE(lruvec->lrugen.timestamps[gen]); =20 /* * setting can_swap=3Dtrue and force_scan=3Dtrue ensures * proper workingset stats when the system cannot swap. */ - try_to_inc_max_seq(lruvec, max_seq, true, true); + if (time_is_before_jiffies(birth + refresh_interval)) + try_to_inc_max_seq(lruvec, max_seq, true, true); cond_resched(); } while ((memcg =3D mem_cgroup_iter(root, memcg, NULL))); =20 @@ -270,17 +274,25 @@ bool wsr_refresh_report(struct wsr_state *wsr, struct= mem_cgroup *root, struct pglist_data *pgdat) { struct wsr_page_age_histo *page_age; + unsigned long refresh_interval =3D READ_ONCE(wsr->refresh_interval); =20 if (!READ_ONCE(wsr->page_age)) return false; =20 - refresh_scan(wsr, root, pgdat); + if (!refresh_interval) + return false; + mutex_lock(&wsr->page_age_lock); page_age =3D READ_ONCE(wsr->page_age); - if (page_age) { - copy_node_bins(pgdat, page_age); - refresh_aggregate(page_age, root, pgdat); - } + if (!page_age) + goto unlock; + if (page_age->timestamp && + time_is_after_jiffies(page_age->timestamp + refresh_interval)) + goto unlock; + refresh_scan(wsr, root, pgdat, refresh_interval); + copy_node_bins(pgdat, page_age); + refresh_aggregate(page_age, root, pgdat); +unlock: mutex_unlock(&wsr->page_age_lock); return !!page_age; } @@ -299,6 +311,52 @@ static struct wsr_state *kobj_to_wsr(struct kobject *k= obj) return &mem_cgroup_lruvec(NULL, kobj_to_pgdat(kobj))->wsr; } =20 +static ssize_t refresh_interval_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct wsr_state *wsr =3D kobj_to_wsr(kobj); + unsigned int interval =3D READ_ONCE(wsr->refresh_interval); + + return sysfs_emit(buf, "%u\n", jiffies_to_msecs(interval)); +} + +static ssize_t refresh_interval_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t len) +{ + unsigned int interval; + int err; + struct wsr_state *wsr =3D kobj_to_wsr(kobj); + + err =3D kstrtouint(buf, 0, &interval); + if (err) + return err; + + mutex_lock(&wsr->page_age_lock); + if (interval && !wsr->page_age) { + struct wsr_page_age_histo *page_age =3D + kzalloc(sizeof(struct wsr_page_age_histo), GFP_KERNEL); + + if (!page_age) { + err =3D -ENOMEM; + goto unlock; + } + wsr->page_age =3D page_age; + } + if (!interval && wsr->page_age) { + kfree(wsr->page_age); + wsr->page_age =3D NULL; + } + + WRITE_ONCE(wsr->refresh_interval, msecs_to_jiffies(interval)); +unlock: + mutex_unlock(&wsr->page_age_lock); + return err ?: len; +} + +static struct kobj_attribute refresh_interval_attr =3D + __ATTR_RW(refresh_interval); + static ssize_t page_age_intervals_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -382,13 +440,6 @@ static ssize_t page_age_show(struct kobject *kobj, str= uct kobj_attribute *attr, int ret =3D 0; struct wsr_state *wsr =3D kobj_to_wsr(kobj); =20 - - mutex_lock(&wsr->page_age_lock); - if (!wsr->page_age) - wsr->page_age =3D - kzalloc(sizeof(struct wsr_page_age_histo), GFP_KERNEL); - mutex_unlock(&wsr->page_age_lock); - wsr_refresh_report(wsr, NULL, kobj_to_pgdat(kobj)); =20 mutex_lock(&wsr->page_age_lock); @@ -414,7 +465,10 @@ static ssize_t page_age_show(struct kobject *kobj, str= uct kobj_attribute *attr, static struct kobj_attribute page_age_attr =3D __ATTR_RO(page_age); =20 static struct attribute *workingset_report_attrs[] =3D { - &page_age_intervals_attr.attr, &page_age_attr.attr, NULL + &refresh_interval_attr.attr, + &page_age_intervals_attr.attr, + &page_age_attr.attr, + NULL }; =20 static const struct attribute_group workingset_report_attr_group =3D { --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 C84FE1A2C39 for ; Tue, 13 Aug 2024 16:59:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568385; cv=none; b=Ae60kwI63Tl+hrCxei+Csl4626Ru6oHsHE82N7TSwgkDQRstOCOwNJpQghahUcue/z7rp5+S7sitYYwNIUhRtGs7stxiTk6lVKMOa3gaWXV4rF0VvPdZL3yfDz6/aARn52LGcKso4z/wwA2wIsHjiYZJMmJgFQgUMc4YZ2QtdyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568385; c=relaxed/simple; bh=uUt4ZAicTtOcUFBajc2Rcm/DS6stfUhYzd+Acgyt79g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cvWyVTDZ4zt7mWO93BLZUDxvcxHX7lj5aSoa2wRJ+CHZVinWPfuSb8ZyzHkk6NNeItBogzuLyM4ZzK7kYzoRP1yU0ABAwK8wB6FhiUksd6e0V98+9CvveS1OvwVwdF/c1OLHheMSIdSZnemM58e0HhjsU7aYtfILmpZHja75YnY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IlJ+ydgf; arc=none smtp.client-ip=209.85.128.201 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IlJ+ydgf" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-666010fb35cso1071527b3.0 for ; Tue, 13 Aug 2024 09:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568382; x=1724173182; 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=I1JUyOR+BauD0beWrpDUPXKr0GpTowWtPDSwnxHbi4c=; b=IlJ+ydgfsQVGD8V5EZalojt0t8crnWOVXavzywaQ7Jf5nNZbWQktI6u+EEWwF3OcBi PzZFmPY9kXHnCTqhgo9g6R1R88vbfDsMpaDEDHz1v9AVFAmJRjeWSUFQgl2xu/zXWwri qYGYYLfuHxweY+fWH6CNZf97W3w6dPIbIYoBPXHGSoTZfwK62XrATImy+GfwasCNV7Rt fhNGrdRUk8NnSyjNhO1x0MTz9Cys0+MNPZ/hbRCiQoePfPYGo2OGEaMhtK7oZiLQJyjT uhkSfMIR0ivCHBGotLCpQUVa1/AUBje7cxFnexmDIT2V/n6CLqVTc9s+RWzcI+orO26B HG2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568382; x=1724173182; 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=I1JUyOR+BauD0beWrpDUPXKr0GpTowWtPDSwnxHbi4c=; b=wd8oDD593MELyw21tcfSIEH3pISP2UWdktZPtORtRGjwBJ9u+CQNyBFiasfPlhmTPs DpqQpYjatB7FnhQiICeViY5gA9bbN3sTjoiTSBg8t+0Go2HZThvGogGiSXMcPOSeB8d9 eShv7Mv5LtvbWg+xUXlIiHV6OGr8yA7TRKesTZRuwZN5OGZQbMzeUdlZMHppldbJ5bCW 99QftfpbJEm6BRfTixVkyiDtpqmJuP7vhwBdVsvYZ58NiF+HFrjTIsBFJlG/tOFsrLkr WL3Mz236Tb79kyQ0ilc0oYBgLpPV2swJx+M7xF3zG+3qtjpJ7p2lKQdN+Jcjq2WqbOp5 7I8A== X-Forwarded-Encrypted: i=1; AJvYcCVZ7e7w2mtESOQYB4fwv2mLORGyKW0ti2LivJi1vmS4Yb8u33zfzD4GL2xqh8PHrq0eDjDkp8sTWsZV/EXsFIUnJ4Eu1QnjPYweL1Gf X-Gm-Message-State: AOJu0Yxv6D3WKQ4D3tZyvnE9We3GSOah8VDF8TtBG2CI41v6ap/jEJB5 7LMoHdG9cyMtHEvdaRFLkJRb3AaLsPz+8IlHx3hj9139JdeWUW6SXedcfYRWiiYqhTtsKW//t/N GUkk2NA== X-Google-Smtp-Source: AGHT+IGF6gcvOUuK0qRwt2F4MDLyEzr3H7dooXhW2VhDhQ7vMG1kJd5bUtwNOeRY1xrvYnGAuUjldLTxpbDJ X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a0d:ce01:0:b0:6a1:741b:b723 with SMTP id 00721157ae682-6a9ea8e5238mr545567b3.5.1723568381863; Tue, 13 Aug 2024 09:59:41 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:14 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-4-yuanchu@google.com> Subject: [PATCH v3 3/7] mm: report workingset during memory pressure driven scanning From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a node reaches its low watermarks and wakes up kswapd, notify all userspace programs waiting on the workingset page age histogram of the memory pressure, so a userspace agent can read the workingset report in time and make policy decisions, such as logging, oom-killing, or migration. Sysfs interface: /sys/devices/system/node/nodeX/workingset_report/report_threshold time in milliseconds that specifies how often the userspace agent can be notified for node memory pressure. Signed-off-by: Yuanchu Xie Change-Id: Ib1fdb726da921e8d467822a99da8f384c6181951 --- include/linux/workingset_report.h | 4 +++ mm/internal.h | 12 ++++++++ mm/vmscan.c | 46 +++++++++++++++++++++++++++++++ mm/workingset_report.c | 43 ++++++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/include/linux/workingset_report.h b/include/linux/workingset_r= eport.h index 8bae6a600410..2ec8b927b200 100644 --- a/include/linux/workingset_report.h +++ b/include/linux/workingset_report.h @@ -37,7 +37,11 @@ struct wsr_page_age_histo { }; =20 struct wsr_state { + unsigned long report_threshold; unsigned long refresh_interval; + + struct kernfs_node *page_age_sys_file; + /* breakdown of workingset by page age */ struct mutex page_age_lock; struct wsr_page_age_histo *page_age; diff --git a/mm/internal.h b/mm/internal.h index f7d790f5d41c..19af882c506e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -416,6 +416,18 @@ bool try_to_inc_max_seq(struct lruvec *lruvec, unsigne= d long seq, bool can_swap, bool force_scan); void set_task_reclaim_state(struct task_struct *task, struct reclaim_state= *rs); =20 +#ifdef CONFIG_WORKINGSET_REPORT +/* + * in mm/wsr.c + */ +void notify_workingset(struct mem_cgroup *memcg, struct pglist_data *pgdat= ); +#else +static inline void notify_workingset(struct mem_cgroup *memcg, + struct pglist_data *pgdat) +{ +} +#endif + /* * in mm/rmap.c: */ diff --git a/mm/vmscan.c b/mm/vmscan.c index 8ab1b456d2cf..b07fd2016c75 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2574,6 +2574,15 @@ static bool can_age_anon_pages(struct pglist_data *p= gdat, return can_demote(pgdat->node_id, sc); } =20 +#ifdef CONFIG_WORKINGSET_REPORT +static void try_to_report_workingset(struct pglist_data *pgdat, struct sca= n_control *sc); +#else +static inline void try_to_report_workingset(struct pglist_data *pgdat, + struct scan_control *sc) +{ +} +#endif + #ifdef CONFIG_LRU_GEN =20 #ifdef CONFIG_LRU_GEN_ENABLED @@ -4000,6 +4009,8 @@ static void lru_gen_age_node(struct pglist_data *pgda= t, struct scan_control *sc) =20 set_initial_priority(pgdat, sc); =20 + try_to_report_workingset(pgdat, sc); + memcg =3D mem_cgroup_iter(NULL, NULL, NULL); do { struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); @@ -5640,6 +5651,38 @@ static int __init init_lru_gen(void) }; late_initcall(init_lru_gen); =20 +#ifdef CONFIG_WORKINGSET_REPORT +static void try_to_report_workingset(struct pglist_data *pgdat, + struct scan_control *sc) +{ + struct mem_cgroup *memcg =3D sc->target_mem_cgroup; + struct wsr_state *wsr =3D &mem_cgroup_lruvec(memcg, pgdat)->wsr; + unsigned long threshold =3D READ_ONCE(wsr->report_threshold); + + if (sc->priority =3D=3D DEF_PRIORITY) + return; + + if (!threshold) + return; + + if (!mutex_trylock(&wsr->page_age_lock)) + return; + + if (!wsr->page_age) { + mutex_unlock(&wsr->page_age_lock); + return; + } + + if (time_is_after_jiffies(wsr->page_age->timestamp + threshold)) { + mutex_unlock(&wsr->page_age_lock); + return; + } + + mutex_unlock(&wsr->page_age_lock); + notify_workingset(memcg, pgdat); +} +#endif /* CONFIG_WORKINGSET_REPORT */ + #else /* !CONFIG_LRU_GEN */ =20 static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_contro= l *sc) @@ -6191,6 +6234,9 @@ static void shrink_zones(struct zonelist *zonelist, s= truct scan_control *sc) if (zone->zone_pgdat =3D=3D last_pgdat) continue; last_pgdat =3D zone->zone_pgdat; + + if (!sc->proactive) + try_to_report_workingset(zone->zone_pgdat, sc); shrink_node(zone->zone_pgdat, sc); } =20 diff --git a/mm/workingset_report.c b/mm/workingset_report.c index fe553c0a653e..801ac8e5c1da 100644 --- a/mm/workingset_report.c +++ b/mm/workingset_report.c @@ -311,6 +311,33 @@ static struct wsr_state *kobj_to_wsr(struct kobject *k= obj) return &mem_cgroup_lruvec(NULL, kobj_to_pgdat(kobj))->wsr; } =20 +static ssize_t report_threshold_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct wsr_state *wsr =3D kobj_to_wsr(kobj); + unsigned int threshold =3D READ_ONCE(wsr->report_threshold); + + return sysfs_emit(buf, "%u\n", jiffies_to_msecs(threshold)); +} + +static ssize_t report_threshold_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t len) +{ + unsigned int threshold; + struct wsr_state *wsr =3D kobj_to_wsr(kobj); + + if (kstrtouint(buf, 0, &threshold)) + return -EINVAL; + + WRITE_ONCE(wsr->report_threshold, msecs_to_jiffies(threshold)); + + return len; +} + +static struct kobj_attribute report_threshold_attr =3D + __ATTR_RW(report_threshold); + static ssize_t refresh_interval_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -465,6 +492,7 @@ static ssize_t page_age_show(struct kobject *kobj, stru= ct kobj_attribute *attr, static struct kobj_attribute page_age_attr =3D __ATTR_RO(page_age); =20 static struct attribute *workingset_report_attrs[] =3D { + &report_threshold_attr.attr, &refresh_interval_attr.attr, &page_age_intervals_attr.attr, &page_age_attr.attr, @@ -486,8 +514,13 @@ void wsr_init_sysfs(struct node *node) =20 wsr =3D kobj_to_wsr(kobj); =20 - if (sysfs_create_group(kobj, &workingset_report_attr_group)) + if (sysfs_create_group(kobj, &workingset_report_attr_group)) { pr_warn("Workingset report failed to create sysfs files\n"); + return; + } + + wsr->page_age_sys_file =3D + kernfs_walk_and_get(kobj->sd, "workingset_report/page_age"); } EXPORT_SYMBOL_GPL(wsr_init_sysfs); =20 @@ -500,6 +533,14 @@ void wsr_remove_sysfs(struct node *node) return; =20 wsr =3D kobj_to_wsr(kobj); + kernfs_put(wsr->page_age_sys_file); sysfs_remove_group(kobj, &workingset_report_attr_group); } EXPORT_SYMBOL_GPL(wsr_remove_sysfs); + +void notify_workingset(struct mem_cgroup *memcg, struct pglist_data *pgdat) +{ + struct wsr_state *wsr =3D &mem_cgroup_lruvec(memcg, pgdat)->wsr; + + kernfs_notify(wsr->page_age_sys_file); +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 EF1941A2561 for ; Tue, 13 Aug 2024 16:59:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568386; cv=none; b=KDLgko48uw8A6N4bCLYOBO8HEr1pO0DS3IGzN3YWNYLxiNx6aHsu+7m5MD8VoitTq8JG8Xu0z7/lHSDA7NI/yrOriVmrVoyRdNI0HTah45s/UJIcJLDjgkY80b5nCLYzrRI1qYsWu/50R+gkG8D885MhNToRzh6Ab+rGXc75Ue8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568386; c=relaxed/simple; bh=v4o0k/hvkZp0NOHjP3zwNZqrNqtVb/d9j5ZzED2dby8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mpRC6638J0Taz9gEgXgoFjOuBTH/p9iady6o9JLwMVVk2oC3jhNbbjmT0+tujWa1UjGOARgW8VZBlsc+Gq5KeyJZ8GIDIqLffE77kgI0MRn74AZzL3JXEWWERJhW51aLW82cMwsqOPhAE45PA85uYRvDoiiUVK9a9ij0BcAFHEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gjPGFElB; arc=none smtp.client-ip=209.85.219.201 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gjPGFElB" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e02a4de4f4eso9998386276.1 for ; Tue, 13 Aug 2024 09:59:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568384; x=1724173184; 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=nX5Or29rWGSYnqI8Mal7bT79wmdNbU4VM0EJZ849BDE=; b=gjPGFElBOXW8hFo0GgXOLyAozJnNvOMIU8QgVLuHjvCQmysB982i4f4fdkHRfabKw4 TXhj1UbH34oRXb8glQlAjD0o/x1u+PXiccyH1uPOtVxIRjd6YTUsjktE9yg/KCvUkC5o nBI18micNsWLBTmmnh8bjmTUCmuPkYiQsPZLbZBYdOHFxWwG5HPVSKVRcIeh0mRNWr9B iDyl6anDgnNU4ph+QQK0oDiXViUnSgCo5meneHYxyXcxWjoEpk9SZS60nNjfmmzXJfVt yyFY/slaWzfogMkaZg7joAFYXxDNdnm/NI9TkMDeVzuBdDb6/fO4KtOLB4wFn+le7qv2 hBTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568384; x=1724173184; 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=nX5Or29rWGSYnqI8Mal7bT79wmdNbU4VM0EJZ849BDE=; b=PTHwQm0E0fmpfmmNwL8iy4g+R25mDUmpjOt2uJDaIIhT8e4gIjIDS2eFW0kUbmm5o6 uuBRIZ3dip2Ysqs8ox1dPeiKPzWxB2NLdNqp+LvDpnya6ejJIxC0wQ+yGk1AFYg3GJUO smRjiUA4trHeAxJtDZnwrbAuyaLASZ0pW3rhu8KBVTK4IAIfFg9MqR+7LYWBmVU6y6rp n0Eq+PITOG3umEER4Jy1n6osPPyz9ad2GARWjg8RII5y7z8jfg1eL7r2Hqbk7nRs/gS8 fcLPi9ssVy28BLNSVMElus8K68VYDP79QdK/q8fyMaDV5lUN2ewL/lnuWeOXjJYInWxr Yuyg== X-Forwarded-Encrypted: i=1; AJvYcCXI0m3LYcF7ljsW5JRy3NEkhmfX6tIK4xZGHnqbhq6k33XpFzOq0T+Jt85UxuUPxU7vtPkb/dDnQQnRyVM=@vger.kernel.org X-Gm-Message-State: AOJu0YxENAnHRSP66u1bAYQzUygUj5bu1nPjcmW21FuuH+Xjry1YjIEl wA9L2n/8WMjnal/txC8c3AJmMzcFmqXtEqJXTyIbMVfqA2BKE1n/pQiDz5HZxYS/WGWq4S+dK+c AjqC1uw== X-Google-Smtp-Source: AGHT+IEDV077DvpPZgcSOtEG+50DqdAd9HxWks1Jm8Rh/HY1wmqFnkHTld2tYhmnQaOc9LEXewCUzyWTHQqf X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a25:a444:0:b0:e05:6e45:84b6 with SMTP id 3f1490d57ef6-e1155b79891mr171276.8.1723568383787; Tue, 13 Aug 2024 09:59:43 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:15 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-5-yuanchu@google.com> Subject: [PATCH v3 4/7] mm: extend working set reporting to memcgs From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Break down the system-wide working set reporting into per-memcg reports, which aggregages its children hierarchically. The per-node working set reporting histograms and refresh/report threshold files are presented as memcg files, showing a report containing all the nodes. The per-node page age interval is configurable in sysfs and not available per-memcg, while the refresh interval and report threshold are configured per-memcg. Memcg interface: /sys/fs/cgroup/.../memory.workingset.page_age The memcg equivalent of the sysfs workingset page age histogram breaks down the workingset of this memcg and its children into page age intervals. Each node is prefixed with a node header and a newline. Non-proactive direct reclaim on this memcg can also wake up userspace agents that are waiting on this file. e.g. N0 1000 anon=3D0 file=3D0 2000 anon=3D0 file=3D0 3000 anon=3D0 file=3D0 4000 anon=3D0 file=3D0 5000 anon=3D0 file=3D0 18446744073709551615 anon=3D0 file=3D0 /sys/fs/cgroup/.../memory.workingset.refresh_interval The memcg equivalent of the sysfs refresh interval. A per-node number of how much time a page age histogram is valid for, in milliseconds. e.g. echo N0=3D2000 > memory.workingset.refresh_interval /sys/fs/cgroup/.../memory.workingset.report_threshold The memcg equivalent of the sysfs report threshold. A per-node number of how often userspace agent waiting on the page age histogram can be woken up, in milliseconds. e.g. echo N0=3D1000 > memory.workingset.report_threshold Signed-off-by: Yuanchu Xie Change-Id: I6c8a39cddd248c031c85f144787c9044912f3c21 --- include/linux/memcontrol.h | 21 ++++ include/linux/workingset_report.h | 6 +- mm/internal.h | 2 + mm/memcontrol.c | 178 +++++++++++++++++++++++++++++- mm/workingset_report.c | 12 +- 5 files changed, 214 insertions(+), 5 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 0e5bf25d324f..6d67f1072943 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -315,6 +315,11 @@ struct mem_cgroup { spinlock_t event_list_lock; #endif /* CONFIG_MEMCG_V1 */ =20 +#ifdef CONFIG_WORKINGSET_REPORT + /* memory.workingset.page_age file */ + struct cgroup_file workingset_page_age_file; +#endif + struct mem_cgroup_per_node *nodeinfo[]; }; =20 @@ -1063,6 +1068,16 @@ static inline void memcg_memory_event_mm(struct mm_s= truct *mm, =20 void split_page_memcg(struct page *head, int old_order, int new_order); =20 +static inline struct cgroup_file * +mem_cgroup_page_age_file(struct mem_cgroup *memcg) +{ +#ifdef CONFIG_WORKINGSET_REPORT + return &memcg->workingset_page_age_file; +#else + return NULL; +#endif +} + #else /* CONFIG_MEMCG */ =20 #define MEM_CGROUP_ID_SHIFT 0 @@ -1470,6 +1485,12 @@ void count_memcg_event_mm(struct mm_struct *mm, enum= vm_event_item idx) static inline void split_page_memcg(struct page *head, int old_order, int = new_order) { } + +static inline struct cgroup_file * +mem_cgroup_page_age_file(struct mem_cgroup *memcg) +{ + return NULL; +} #endif /* CONFIG_MEMCG */ =20 /* diff --git a/include/linux/workingset_report.h b/include/linux/workingset_r= eport.h index 2ec8b927b200..ae412d408037 100644 --- a/include/linux/workingset_report.h +++ b/include/linux/workingset_report.h @@ -9,6 +9,7 @@ struct mem_cgroup; struct pglist_data; struct node; struct lruvec; +struct cgroup_file; =20 #ifdef CONFIG_WORKINGSET_REPORT =20 @@ -40,7 +41,10 @@ struct wsr_state { unsigned long report_threshold; unsigned long refresh_interval; =20 - struct kernfs_node *page_age_sys_file; + union { + struct kernfs_node *page_age_sys_file; + struct cgroup_file *page_age_cgroup_file; + }; =20 /* breakdown of workingset by page age */ struct mutex page_age_lock; diff --git a/mm/internal.h b/mm/internal.h index 19af882c506e..cc54358ed582 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -421,6 +421,8 @@ void set_task_reclaim_state(struct task_struct *task, s= truct reclaim_state *rs); * in mm/wsr.c */ void notify_workingset(struct mem_cgroup *memcg, struct pglist_data *pgdat= ); +int workingset_report_intervals_parse(char *src, + struct wsr_report_bins *bins); #else static inline void notify_workingset(struct mem_cgroup *memcg, struct pglist_data *pgdat) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5d16fd9d2c3f..56848dc94d48 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4318,6 +4318,162 @@ static ssize_t memory_reclaim(struct kernfs_open_fi= le *of, char *buf, return nbytes; } =20 +#ifdef CONFIG_WORKINGSET_REPORT +static int memory_ws_refresh_interval_show(struct seq_file *m, void *v) +{ + int nid; + struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); + + for_each_node_state(nid, N_MEMORY) { + struct wsr_state *wsr =3D + &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; + + seq_printf(m, "N%d=3D%u ", nid, + jiffies_to_msecs(READ_ONCE(wsr->refresh_interval))); + } + seq_putc(m, '\n'); + + return 0; +} + +static ssize_t memory_wsr_threshold_parse(char *buf, size_t nbytes, + unsigned int *nid_out, + unsigned int *msecs) +{ + char *node, *threshold; + unsigned int nid; + int err; + + buf =3D strstrip(buf); + threshold =3D buf; + node =3D strsep(&threshold, "=3D"); + + if (*node !=3D 'N') + return -EINVAL; + + err =3D kstrtouint(node + 1, 0, &nid); + if (err) + return err; + + if (nid >=3D nr_node_ids || !node_state(nid, N_MEMORY)) + return -EINVAL; + + err =3D kstrtouint(threshold, 0, msecs); + if (err) + return err; + + *nid_out =3D nid; + + return nbytes; +} + +static ssize_t memory_ws_refresh_interval_write(struct kernfs_open_file *o= f, + char *buf, size_t nbytes, + loff_t off) +{ + unsigned int nid, msecs; + struct wsr_state *wsr; + struct mem_cgroup *memcg =3D mem_cgroup_from_css(of_css(of)); + ssize_t ret =3D memory_wsr_threshold_parse(buf, nbytes, &nid, &msecs); + + if (ret < 0) + return ret; + + wsr =3D &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; + + mutex_lock(&wsr->page_age_lock); + if (msecs && !wsr->page_age) { + struct wsr_page_age_histo *page_age =3D + kzalloc(sizeof(struct wsr_page_age_histo), GFP_KERNEL); + + if (!page_age) { + ret =3D -ENOMEM; + goto unlock; + } + wsr->page_age =3D page_age; + } + if (!msecs && wsr->page_age) { + kfree(wsr->page_age); + wsr->page_age =3D NULL; + } + + WRITE_ONCE(wsr->refresh_interval, msecs_to_jiffies(msecs)); +unlock: + mutex_unlock(&wsr->page_age_lock); + return ret; +} + +static int memory_ws_report_threshold_show(struct seq_file *m, void *v) +{ + int nid; + struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); + + for_each_node_state(nid, N_MEMORY) { + struct wsr_state *wsr =3D + &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; + + seq_printf(m, "N%d=3D%u ", nid, + jiffies_to_msecs(READ_ONCE(wsr->report_threshold))); + } + seq_putc(m, '\n'); + + return 0; +} + +static ssize_t memory_ws_report_threshold_write(struct kernfs_open_file *o= f, + char *buf, size_t nbytes, + loff_t off) +{ + unsigned int nid, msecs; + struct wsr_state *wsr; + struct mem_cgroup *memcg =3D mem_cgroup_from_css(of_css(of)); + ssize_t ret =3D memory_wsr_threshold_parse(buf, nbytes, &nid, &msecs); + + if (ret < 0) + return ret; + + wsr =3D &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; + WRITE_ONCE(wsr->report_threshold, msecs_to_jiffies(msecs)); + return ret; +} + +static int memory_ws_page_age_show(struct seq_file *m, void *v) +{ + int nid; + struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); + + for_each_node_state(nid, N_MEMORY) { + struct wsr_state *wsr =3D + &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; + struct wsr_report_bin *bin; + + if (!READ_ONCE(wsr->page_age)) + continue; + + wsr_refresh_report(wsr, memcg, NODE_DATA(nid)); + mutex_lock(&wsr->page_age_lock); + if (!wsr->page_age) + goto unlock; + seq_printf(m, "N%d\n", nid); + for (bin =3D wsr->page_age->bins; + bin->idle_age !=3D WORKINGSET_INTERVAL_MAX; bin++) + seq_printf(m, "%u anon=3D%lu file=3D%lu\n", + jiffies_to_msecs(bin->idle_age), + bin->nr_pages[0] * PAGE_SIZE, + bin->nr_pages[1] * PAGE_SIZE); + + seq_printf(m, "%lu anon=3D%lu file=3D%lu\n", WORKINGSET_INTERVAL_MAX, + bin->nr_pages[0] * PAGE_SIZE, + bin->nr_pages[1] * PAGE_SIZE); + +unlock: + mutex_unlock(&wsr->page_age_lock); + } + + return 0; +} +#endif + static struct cftype memory_files[] =3D { { .name =3D "current", @@ -4386,7 +4542,27 @@ static struct cftype memory_files[] =3D { .flags =3D CFTYPE_NS_DELEGATABLE, .write =3D memory_reclaim, }, - { } /* terminate */ +#ifdef CONFIG_WORKINGSET_REPORT + { + .name =3D "workingset.refresh_interval", + .flags =3D CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE, + .seq_show =3D memory_ws_refresh_interval_show, + .write =3D memory_ws_refresh_interval_write, + }, + { + .name =3D "workingset.report_threshold", + .flags =3D CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE, + .seq_show =3D memory_ws_report_threshold_show, + .write =3D memory_ws_report_threshold_write, + }, + { + .name =3D "workingset.page_age", + .flags =3D CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE, + .file_offset =3D offsetof(struct mem_cgroup, workingset_page_age_file), + .seq_show =3D memory_ws_page_age_show, + }, +#endif + {} /* terminate */ }; =20 struct cgroup_subsys memory_cgrp_subsys =3D { diff --git a/mm/workingset_report.c b/mm/workingset_report.c index 801ac8e5c1da..333e51e3ee12 100644 --- a/mm/workingset_report.c +++ b/mm/workingset_report.c @@ -37,9 +37,12 @@ void wsr_destroy_pgdat(struct pglist_data *pgdat) void wsr_init_lruvec(struct lruvec *lruvec) { struct wsr_state *wsr =3D &lruvec->wsr; + struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); =20 memset(wsr, 0, sizeof(*wsr)); mutex_init(&wsr->page_age_lock); + if (memcg && !mem_cgroup_is_root(memcg)) + wsr->page_age_cgroup_file =3D mem_cgroup_page_age_file(memcg); } =20 void wsr_destroy_lruvec(struct lruvec *lruvec) @@ -51,8 +54,8 @@ void wsr_destroy_lruvec(struct lruvec *lruvec) memset(wsr, 0, sizeof(*wsr)); } =20 -static int workingset_report_intervals_parse(char *src, - struct wsr_report_bins *bins) +int workingset_report_intervals_parse(char *src, + struct wsr_report_bins *bins) { int err =3D 0, i =3D 0; char *cur, *next =3D strim(src); @@ -542,5 +545,8 @@ void notify_workingset(struct mem_cgroup *memcg, struct= pglist_data *pgdat) { struct wsr_state *wsr =3D &mem_cgroup_lruvec(memcg, pgdat)->wsr; =20 - kernfs_notify(wsr->page_age_sys_file); + if (mem_cgroup_is_root(memcg)) + kernfs_notify(wsr->page_age_sys_file); + else + cgroup_file_notify(wsr->page_age_cgroup_file); } --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 0E8061A38C7 for ; Tue, 13 Aug 2024 16:59:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568388; cv=none; b=d/d5sif7PS5qECBLUKlvJqKQzCHi3PBxYEDWhciTJlJk8WMFJLtqmbXiKVrUuY4IDRAmPLSnHN38eu+DWYcRc92aUPQBWxqxok7uIe2USCf1lPTPaRn9Ey2P1E5AiOXOGgZ0UKjHjkJUTDLCD2tFwSqo+9taGJorlYGmsQQELtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568388; c=relaxed/simple; bh=rsGrKoXAc8FxIjTMM2Zmrw32QSBwJqh9XHRtMaAmFmg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fi6FgQjZJstz+JjHp4DkZceOH8QCYTTcS4Q9RDcQzlRDb/gU8NMZfFAhVrGf7Y+T+BVo+Ivo3b2QvqVibyWxMhyJBCfn9b7bJrYsxbUxTV1+zDz1GYX9nLdIfRHBDmSMEwpboWRxfyQ9hzLUeeBO+jdIKpPS7ofgXd6lBPweDKM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sdVUSwHF; arc=none smtp.client-ip=209.85.128.202 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sdVUSwHF" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-664b7a67ad4so130375627b3.2 for ; Tue, 13 Aug 2024 09:59:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568386; x=1724173186; 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=xpAcupGObnfzDmBhsss97bo400nzNISpyaH5VDbdw9g=; b=sdVUSwHF3qHrznTWsQNsRWoAg+ZoL/j1lsJWLrQ2fd9/zLssXVbaflnedvOJOld+ex u4O1jNfIFWanWEYKgjcGnnc+seAexTiXIQZHnbva28G05/TWUsb/TVBKw6tJxAV0mC2J yYQTYdNiPpzFYxDi3E/WtVOiZGZ41PeQXRyGdeb4EPFo+vQouHLFLfSULYmCQSSsJFbR Z9IkI947SsfZ+fXm4fvVLXXsfa083BvMF03fd/7nffF4x1ZQH6A53ZIzdqrl1bN6pbHA oYAyMYB+G9uy/z85p3BWX+HZ559VpycoanOIPbIrqDcVr9WTM7bB5/HDT8NRvWy5+VoS AX4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568386; x=1724173186; 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=xpAcupGObnfzDmBhsss97bo400nzNISpyaH5VDbdw9g=; b=kWbjJPp5D4WZaDckpXjx3Krn8g1xxKoChttf3DbhuvjGN3s29A9OFrSZRvgjc3qT0S vfNGPrE3uIbj0eSfI32uzzgGXNO8I6XTbwkYxnC/BYnTbNdZyKlQp1mnHXut3deXfsdg i3bfeOE18bPH4omtVHQGKkObR7HSRt/31lQab0NejPZ16nkH6Mc5SnYcIMpic1WBJblx V8FT7BULIeKH9AK0GwUNVp4lg0TcXi/9E3UOD0rD6fCfyqIvGyoDDlaRa12EA+ypfkU3 zEUDbzkHR/Xg+Ocr73MYHdg/6IBTJvzmMAxlrGl9t7gjZW8ggerUEi1n/nRJNcqEjbRI U0fA== X-Forwarded-Encrypted: i=1; AJvYcCXiz86TZxkMSrbutb8aWnXlllvkIBqIUm7aufNFowt2KQBzCfL+zM++JhEuFdPCcIKS99cUHXlw0tcqG2Y=@vger.kernel.org X-Gm-Message-State: AOJu0YxlYX5cyFO08d9byF+3pW6zzDqRNLH8LDM4DItHDCdpU7z0UYJJ IGwiqmkrzECC7fgaYhMIOL1MzIKGVmBo+I6hOfKGqgPviY5kU431RnFYd8HVDO2y5/ZXdaoqCp3 IB/GVRQ== X-Google-Smtp-Source: AGHT+IHuzPFm66066+PM+qeXLjE3MUrZ+OKfquaNrkkMfQw82fR/11BL+BIef/h89u4WvUPnZWHI7Qy55Ias X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a0d:c607:0:b0:630:e8a:8a15 with SMTP id 00721157ae682-6ac8feff7e5mr7727b3.0.1723568385881; Tue, 13 Aug 2024 09:59:45 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:16 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-6-yuanchu@google.com> Subject: [PATCH v3 5/7] mm: add kernel aging thread for workingset reporting From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For reliable and timely aging on memcgs, one has to read the page age histograms on time. A kernel thread makes it easier by aging memcgs with valid refresh_interval when they can be refreshed, and also reduces the latency of any userspace consumers of the page age histogram. The kerne aging thread is gated behind CONFIG_WORKINGSET_REPORT_AGING. Debugging stats may be added in the future for when aging cannot keep up with the configured refresh_interval. Signed-off-by: Yuanchu Xie Change-Id: Ia34a5d6b7eb91e39fab5f2d4ca2f18a9c370890b --- include/linux/workingset_report.h | 11 ++- mm/Kconfig | 6 ++ mm/Makefile | 1 + mm/memcontrol.c | 8 +- mm/workingset_report.c | 15 +++- mm/workingset_report_aging.c | 127 ++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 mm/workingset_report_aging.c diff --git a/include/linux/workingset_report.h b/include/linux/workingset_r= eport.h index ae412d408037..9294023db5a8 100644 --- a/include/linux/workingset_report.h +++ b/include/linux/workingset_report.h @@ -63,7 +63,16 @@ void wsr_remove_sysfs(struct node *node); * The next refresh time is stored in refresh_time. */ bool wsr_refresh_report(struct wsr_state *wsr, struct mem_cgroup *root, - struct pglist_data *pgdat); + struct pglist_data *pgdat, unsigned long *refresh_time); + +#ifdef CONFIG_WORKINGSET_REPORT_AGING +void wsr_wakeup_aging_thread(void); +#else /* CONFIG_WORKINGSET_REPORT_AGING */ +static inline void wsr_wakeup_aging_thread(void) +{ +} +#endif /* CONFIG_WORKINGSET_REPORT_AGING */ + #else static inline void wsr_init_lruvec(struct lruvec *lruvec) { diff --git a/mm/Kconfig b/mm/Kconfig index 951b4e7300d2..c1dfece5b71c 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1272,6 +1272,12 @@ config WORKINGSET_REPORT This option exports stats and events giving the user more insight into its memory working set. =20 +config WORKINGSET_REPORT_AGING + bool "Workingset report kernel aging thread" + depends on WORKINGSET_REPORT + help + Performs aging on memcgs with their configured refresh intervals. + source "mm/damon/Kconfig" =20 endmenu diff --git a/mm/Makefile b/mm/Makefile index 62cef72ce7f8..4a64b2454b6a 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -99,6 +99,7 @@ obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.o obj-$(CONFIG_MEMCG_V1) +=3D memcontrol-v1.o obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o obj-$(CONFIG_WORKINGSET_REPORT) +=3D workingset_report.o +obj-$(CONFIG_WORKINGSET_REPORT_AGING) +=3D workingset_report_aging.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) +=3D swap_cgroup.o endif diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 56848dc94d48..52123b6cc9ce 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4373,12 +4373,12 @@ static ssize_t memory_ws_refresh_interval_write(str= uct kernfs_open_file *of, { unsigned int nid, msecs; struct wsr_state *wsr; + unsigned long old_interval; struct mem_cgroup *memcg =3D mem_cgroup_from_css(of_css(of)); ssize_t ret =3D memory_wsr_threshold_parse(buf, nbytes, &nid, &msecs); =20 if (ret < 0) return ret; - wsr =3D &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; =20 mutex_lock(&wsr->page_age_lock); @@ -4397,9 +4397,13 @@ static ssize_t memory_ws_refresh_interval_write(stru= ct kernfs_open_file *of, wsr->page_age =3D NULL; } =20 + old_interval =3D READ_ONCE(wsr->refresh_interval); WRITE_ONCE(wsr->refresh_interval, msecs_to_jiffies(msecs)); unlock: mutex_unlock(&wsr->page_age_lock); + if (ret > 0 && msecs && + (!old_interval || jiffies_to_msecs(old_interval) > msecs)) + wsr_wakeup_aging_thread(); return ret; } =20 @@ -4450,7 +4454,7 @@ static int memory_ws_page_age_show(struct seq_file *m= , void *v) if (!READ_ONCE(wsr->page_age)) continue; =20 - wsr_refresh_report(wsr, memcg, NODE_DATA(nid)); + wsr_refresh_report(wsr, memcg, NODE_DATA(nid), NULL); mutex_lock(&wsr->page_age_lock); if (!wsr->page_age) goto unlock; diff --git a/mm/workingset_report.c b/mm/workingset_report.c index 333e51e3ee12..6f64d89fe70d 100644 --- a/mm/workingset_report.c +++ b/mm/workingset_report.c @@ -274,7 +274,7 @@ static void copy_node_bins(struct pglist_data *pgdat, } =20 bool wsr_refresh_report(struct wsr_state *wsr, struct mem_cgroup *root, - struct pglist_data *pgdat) + struct pglist_data *pgdat, unsigned long *refresh_time) { struct wsr_page_age_histo *page_age; unsigned long refresh_interval =3D READ_ONCE(wsr->refresh_interval); @@ -291,10 +291,14 @@ bool wsr_refresh_report(struct wsr_state *wsr, struct= mem_cgroup *root, goto unlock; if (page_age->timestamp && time_is_after_jiffies(page_age->timestamp + refresh_interval)) - goto unlock; + goto time; refresh_scan(wsr, root, pgdat, refresh_interval); copy_node_bins(pgdat, page_age); refresh_aggregate(page_age, root, pgdat); + +time: + if (refresh_time) + *refresh_time =3D page_age->timestamp + refresh_interval; unlock: mutex_unlock(&wsr->page_age_lock); return !!page_age; @@ -357,6 +361,7 @@ static ssize_t refresh_interval_store(struct kobject *k= obj, unsigned int interval; int err; struct wsr_state *wsr =3D kobj_to_wsr(kobj); + unsigned long old_interval =3D 0; =20 err =3D kstrtouint(buf, 0, &interval); if (err) @@ -378,9 +383,13 @@ static ssize_t refresh_interval_store(struct kobject *= kobj, wsr->page_age =3D NULL; } =20 + old_interval =3D READ_ONCE(wsr->refresh_interval); WRITE_ONCE(wsr->refresh_interval, msecs_to_jiffies(interval)); unlock: mutex_unlock(&wsr->page_age_lock); + if (!err && interval && + (!old_interval || jiffies_to_msecs(old_interval) > interval)) + wsr_wakeup_aging_thread(); return err ?: len; } =20 @@ -470,7 +479,7 @@ static ssize_t page_age_show(struct kobject *kobj, stru= ct kobj_attribute *attr, int ret =3D 0; struct wsr_state *wsr =3D kobj_to_wsr(kobj); =20 - wsr_refresh_report(wsr, NULL, kobj_to_pgdat(kobj)); + wsr_refresh_report(wsr, NULL, kobj_to_pgdat(kobj), NULL); =20 mutex_lock(&wsr->page_age_lock); if (!wsr->page_age) diff --git a/mm/workingset_report_aging.c b/mm/workingset_report_aging.c new file mode 100644 index 000000000000..91ad5020778a --- /dev/null +++ b/mm/workingset_report_aging.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Workingset report kernel aging thread + * + * Performs aging on behalf of memcgs with their configured refresh interv= al. + * While a userspace program can periodically read the page age breakdown + * per-memcg and trigger aging, the kernel performing aging is less overhe= ad, + * more consistent, and more reliable for the use case where every memcg s= hould + * be aged according to their refresh interval. + */ +#define pr_fmt(fmt) "workingset report aging: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static DECLARE_WAIT_QUEUE_HEAD(aging_wait); +static bool refresh_pending; + +static bool do_aging_node(int nid, unsigned long *next_wake_time) +{ + struct mem_cgroup *memcg; + bool should_wait =3D true; + struct pglist_data *pgdat =3D NODE_DATA(nid); + + memcg =3D mem_cgroup_iter(NULL, NULL, NULL); + do { + struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, pgdat); + struct wsr_state *wsr =3D &lruvec->wsr; + unsigned long refresh_time; + + /* use returned time to decide when to wake up next */ + if (wsr_refresh_report(wsr, memcg, pgdat, &refresh_time)) { + if (should_wait) { + should_wait =3D false; + *next_wake_time =3D refresh_time; + } else if (time_before(refresh_time, *next_wake_time)) { + *next_wake_time =3D refresh_time; + } + } + + cond_resched(); + } while ((memcg =3D mem_cgroup_iter(NULL, memcg, NULL))); + + return should_wait; +} + +static int do_aging(void *unused) +{ + while (!kthread_should_stop()) { + int nid; + long timeout_ticks; + unsigned long next_wake_time; + bool should_wait =3D true; + + WRITE_ONCE(refresh_pending, false); + for_each_node_state(nid, N_MEMORY) { + unsigned long node_next_wake_time; + + if (do_aging_node(nid, &node_next_wake_time)) + continue; + if (should_wait) { + should_wait =3D false; + next_wake_time =3D node_next_wake_time; + } else if (time_before(node_next_wake_time, + next_wake_time)) { + next_wake_time =3D node_next_wake_time; + } + } + + if (should_wait) { + wait_event_interruptible(aging_wait, refresh_pending); + continue; + } + + /* sleep until next aging */ + timeout_ticks =3D next_wake_time - jiffies; + if (timeout_ticks > 0 && + timeout_ticks !=3D MAX_SCHEDULE_TIMEOUT) { + schedule_timeout_idle(timeout_ticks); + continue; + } + } + return 0; +} + +/* Invoked when refresh_interval shortens or changes to a non-zero value. = */ +void wsr_wakeup_aging_thread(void) +{ + WRITE_ONCE(refresh_pending, true); + wake_up_interruptible(&aging_wait); +} + +static struct task_struct *aging_thread; + +static int aging_init(void) +{ + struct task_struct *task; + + task =3D kthread_run(do_aging, NULL, "kagingd"); + + if (IS_ERR(task)) { + pr_err("Failed to create aging kthread\n"); + return PTR_ERR(task); + } + + aging_thread =3D task; + pr_info("module loaded\n"); + return 0; +} + +static void aging_exit(void) +{ + kthread_stop(aging_thread); + aging_thread =3D NULL; + pr_info("module unloaded\n"); +} + +module_init(aging_init); +module_exit(aging_exit); --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 F37301A3BC2 for ; Tue, 13 Aug 2024 16:59:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568391; cv=none; b=Lz/zlyQz+JYnuyQRleWvO99dKD8XlEr49m2IsqFDF6gQ7pLDZNQelp7zTDOH2/qzjeR6bm9QHh1ChRgjjyT6/lCMIxyLsKCMaCy30Xt4waUojDkJ0zwlnWTShbbl+ntM3HggOL0icyrBzLh40v6/3GShydQXfMSbDvMyqtRMkRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568391; c=relaxed/simple; bh=/x4Lpox/ByoRUL0boaGu5hLpoQbBGf9FAOcg7NXF8NQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fwU1nnsn2clZdv6Bs7kKyMhnqlP/mUO/rbMDMFD67wi9IVzgrlsQPd/kEEMRStPOQEmKM2LUqWYjDAC+ghpwPf0rF5vuIQ2nkuVhR5NYc2+rQtihse0XUfpIcJPlwadLu3apaid6/JzI24sGtQ167OybbqFtqiImgRdU7HX/hRg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LpfS40q1; arc=none smtp.client-ip=209.85.128.202 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LpfS40q1" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-690404fd230so119982477b3.3 for ; Tue, 13 Aug 2024 09:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568388; x=1724173188; 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=nmNRGoZ1KbCR4Ovo2GA2r3+/VwovN+l6r9v92Jg3Hew=; b=LpfS40q1EPr/kpM/dfTWbyNgAtyB6SswqBM7Joo7PGXerh+FukD4EC6SY5ePFModRc +0uo9wXrHbH2yxKCIrDLGVLM4eazGkoHEmsHe6ahfnOhoXovAURgATrXcOpwpMzPo78K A9LLTk7V8QihbWGVYyjB5BifKuMtGIFzLZTXm2BVdVY5B1v845m0Pa32s7lJhyEXG3Ox GtdI1vlH4IM3kY57TSytgs0sfLPeaIwo5sVvjX2KDN8nHRxxRGiYLEflBynz3j3Jahx6 nxEusg8FBg/0u4WJiTfs3FuN573g17assaEAzJfeVD8rKvEnhUsYtogSt+e3a666sCof nV7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568388; x=1724173188; 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=nmNRGoZ1KbCR4Ovo2GA2r3+/VwovN+l6r9v92Jg3Hew=; b=fr/cXFb3Kgb7WazmCLBLilpGaoBJLWJVZ2xsB660sT3btXmpct1VA9MFRgxFKtt7PR eSzd3rRkJTJAecPqm6ceyXzUYnXPEJ0P3rbFdWiBWNVgHrLhM4aPzP84D/qsTyxIaZYB K12ceY3YocBpaMXB+abNJFvV+GT6vckm9tmCOkJ1uqKLnKXEJMAu0BEDckB0GoVLIayh 5EoNzvbp+Eq6gW6ekqw2c11lO5jUmyGj5a7l83vgqvLa8D9pZcbIfghxfz+0tttUVB/R /98E7d5+124jZvUZqd8O8gQlS9L0Cxcg9Hcqc8HTRGdSDQ57+bbLHU8Jm3awPsrlayTr 9j9Q== X-Forwarded-Encrypted: i=1; AJvYcCV0uwXyqSiUhRFPH8HV1YFgn8aRM+B6VDkjPDj3gh2t5STkvrVsHxwI8qWD+CG13lVEXuV8QyhbyPghTKtLvuWN8q5hYCBiE2h/swSJ X-Gm-Message-State: AOJu0YziKAZs/ZHMf9GaRd5tjBikE8JR+GyG6nSHOuvmYN+UOAmRyFnH iNjhn6stbETn92aWzyFWlEHaqUCxFUAWpgv8Yz1bzTfCb4SjklpM0wAaArAWidvdiUuA2M5GgWz nb662Rw== X-Google-Smtp-Source: AGHT+IHDb1zAvYhwkH2Nz7owaIopIGytMqAr45V8NRtzWfOlB9K6iqc0dkiowScYN9kSuCEnjpoAnU+/LUcr X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a0d:e586:0:b0:61b:e103:804d with SMTP id 00721157ae682-6ac91bde66dmr2347b3.0.1723568387967; Tue, 13 Aug 2024 09:59:47 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:17 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-7-yuanchu@google.com> Subject: [PATCH v3 6/7] selftest: test system-wide workingset reporting From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A basic test that verifies the working set size of a simple memory accessor. It should work with or without the aging thread. When running tests with run_vmtests.sh, file workingset report testing requires an environment variable WORKINGSET_REPORT_TEST_FILE_PATH to store a temporary file, which is passed into the test invocation as a parameter. Signed-off-by: Yuanchu Xie Change-Id: I1a55364164b9a67934f8500aaf77df4372edaa55 --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + tools/testing/selftests/mm/run_vmtests.sh | 5 + .../testing/selftests/mm/workingset_report.c | 306 ++++++++++++++++ .../testing/selftests/mm/workingset_report.h | 39 +++ .../selftests/mm/workingset_report_test.c | 330 ++++++++++++++++++ 6 files changed, 684 insertions(+) create mode 100644 tools/testing/selftests/mm/workingset_report.c create mode 100644 tools/testing/selftests/mm/workingset_report.h create mode 100644 tools/testing/selftests/mm/workingset_report_test.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftest= s/mm/.gitignore index da030b43e43b..e5cd0085ab74 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -51,3 +51,4 @@ hugetlb_madv_vs_map mseal_test seal_elf droppable +workingset_report_test diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/= mm/Makefile index 7b8a5def54a1..8d1a7d24ecd1 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -77,6 +77,7 @@ TEST_GEN_FILES +=3D hugetlb_fault_after_madv TEST_GEN_FILES +=3D hugetlb_madv_vs_map TEST_GEN_FILES +=3D hugetlb_dio TEST_GEN_FILES +=3D droppable +TEST_GEN_FILES +=3D workingset_report_test =20 ifneq ($(ARCH),arm64) TEST_GEN_FILES +=3D soft-dirty @@ -135,6 +136,8 @@ $(TEST_GEN_FILES): vm_util.c thp_settings.c $(OUTPUT)/uffd-stress: uffd-common.c $(OUTPUT)/uffd-unit-tests: uffd-common.c =20 +$(OUTPUT)/workingset_report_test: workingset_report.c + ifeq ($(ARCH),x86_64) BINARIES_32 :=3D $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) BINARIES_64 :=3D $(patsubst %,$(OUTPUT)/%,$(BINARIES_64)) diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/self= tests/mm/run_vmtests.sh index 03ac4f2e1cce..c4a81f17a3a3 100755 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -75,6 +75,8 @@ separated by spaces: read-only VMAs - mdwe test prctl(PR_SET_MDWE, ...) +- workingset_report + test workingset reporting =20 example: ./run_vmtests.sh -t "hmm mmap ksm" EOF @@ -453,6 +455,9 @@ CATEGORY=3D"mkdirty" run_test ./mkdirty =20 CATEGORY=3D"mdwe" run_test ./mdwe_test =20 +CATEGORY=3D"workingset_report" run_test ./workingset_report_test \ + "${WORKINGSET_REPORT_TEST_FILE_PATH}" + echo "SUMMARY: PASS=3D${count_pass} SKIP=3D${count_skip} FAIL=3D${count_fa= il}" | tap_prefix echo "1..${count_total}" | tap_output =20 diff --git a/tools/testing/selftests/mm/workingset_report.c b/tools/testing= /selftests/mm/workingset_report.c new file mode 100644 index 000000000000..ee4dda5c371d --- /dev/null +++ b/tools/testing/selftests/mm/workingset_report.c @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "workingset_report.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#define SYSFS_NODE_ONLINE "/sys/devices/system/node/online" +#define PROC_DROP_CACHES "/proc/sys/vm/drop_caches" + +/* Returns read len on success, or -errno on failure. */ +static ssize_t read_text(const char *path, char *buf, size_t max_len) +{ + ssize_t len; + int fd, err; + size_t bytes_read =3D 0; + + if (!max_len) + return -EINVAL; + + fd =3D open(path, O_RDONLY); + if (fd < 0) + return -errno; + + while (bytes_read < max_len - 1) { + len =3D read(fd, buf + bytes_read, max_len - 1 - bytes_read); + + if (len <=3D 0) + break; + bytes_read +=3D len; + } + + buf[bytes_read] =3D '\0'; + + err =3D -errno; + close(fd); + return len < 0 ? err : bytes_read; +} + +/* Returns written len on success, or -errno on failure. */ +static ssize_t write_text(const char *path, const char *buf, ssize_t max_l= en) +{ + int fd, len, err; + size_t bytes_written =3D 0; + + fd =3D open(path, O_WRONLY | O_APPEND); + if (fd < 0) + return -errno; + + while (bytes_written < max_len) { + len =3D write(fd, buf + bytes_written, max_len - bytes_written); + + if (len < 0) + break; + bytes_written +=3D len; + } + + err =3D -errno; + close(fd); + return len < 0 ? err : bytes_written; +} + +static long read_num(const char *path) +{ + char buf[21]; + + if (read_text(path, buf, sizeof(buf)) <=3D 0) + return -1; + return (long)strtoul(buf, NULL, 10); +} + +static int write_num(const char *path, unsigned long n) +{ + char buf[21]; + + sprintf(buf, "%lu", n); + if (write_text(path, buf, strlen(buf)) < 0) + return -1; + return 0; +} + +long sysfs_get_refresh_interval(int nid) +{ + char file[128]; + + snprintf(file, sizeof(file), + "/sys/devices/system/node/node%d/workingset_report/refresh_interval", + nid); + return read_num(file); +} + +int sysfs_set_refresh_interval(int nid, long interval) +{ + char file[128]; + + snprintf(file, sizeof(file), + "/sys/devices/system/node/node%d/workingset_report/refresh_interval", + nid); + return write_num(file, interval); +} + +int sysfs_get_page_age_intervals_str(int nid, char *buf, int len) +{ + char path[128]; + + snprintf(path, sizeof(path), + "/sys/devices/system/node/node%d/workingset_report/page_age_intervals", + nid); + return read_text(path, buf, len); + +} + +int sysfs_set_page_age_intervals_str(int nid, const char *buf, int len) +{ + char path[128]; + + snprintf(path, sizeof(path), + "/sys/devices/system/node/node%d/workingset_report/page_age_intervals", + nid); + return write_text(path, buf, len); +} + +int sysfs_set_page_age_intervals(int nid, const char *const intervals[], + int nr_intervals) +{ + char file[128]; + char buf[1024]; + int i; + int err, len =3D 0; + + for (i =3D 0; i < nr_intervals; ++i) { + err =3D snprintf(buf + len, sizeof(buf) - len, "%s", intervals[i]); + + if (err < 0) + return err; + len +=3D err; + + if (i < nr_intervals - 1) { + err =3D snprintf(buf + len, sizeof(buf) - len, ","); + if (err < 0) + return err; + len +=3D err; + } + } + + snprintf(file, sizeof(file), + "/sys/devices/system/node/node%d/workingset_report/page_age_intervals", + nid); + return write_text(file, buf, len); +} + +int get_nr_nodes(void) +{ + char buf[22]; + char *found; + + if (read_text(SYSFS_NODE_ONLINE, buf, sizeof(buf)) <=3D 0) + return -1; + found =3D strstr(buf, "-"); + if (found) + return (int)strtoul(found + 1, NULL, 10) + 1; + return (long)strtoul(buf, NULL, 10) + 1; +} + +int drop_pagecache(void) +{ + return write_num(PROC_DROP_CACHES, 1); +} + +ssize_t sysfs_page_age_read(int nid, char *buf, size_t len) + +{ + char file[128]; + + snprintf(file, sizeof(file), + "/sys/devices/system/node/node%d/workingset_report/page_age", + nid); + return read_text(file, buf, len); +} + +/* + * Finds the first occurrence of "N\n" + * Modifies buf to terminate before the next occurrence of "N". + * Returns a substring of buf starting after "N\n" + */ +char *page_age_split_node(char *buf, int nid, char **next) +{ + char node_str[5]; + char *found; + int node_str_len; + + node_str_len =3D snprintf(node_str, sizeof(node_str), "N%u\n", nid); + + /* find the node prefix first */ + found =3D strstr(buf, node_str); + if (!found) { + ksft_print_msg("cannot find '%s' in page_idle_age", node_str); + return NULL; + } + found +=3D node_str_len; + + *next =3D strchr(found, 'N'); + if (*next) + *(*next - 1) =3D '\0'; + + return found; +} + +ssize_t page_age_read(const char *buf, const char *interval, int pagetype) +{ + static const char * const type[ANON_AND_FILE] =3D { "anon=3D", "file=3D" = }; + char *found; + + found =3D strstr(buf, interval); + if (!found) { + ksft_print_msg("cannot find %s in page_age", interval); + return -1; + } + found =3D strstr(found, type[pagetype]); + if (!found) { + ksft_print_msg("cannot find %s in page_age", type[pagetype]); + return -1; + } + found +=3D strlen(type[pagetype]); + return (long)strtoul(found, NULL, 10); +} + +static const char *TEMP_FILE =3D "/tmp/workingset_selftest"; +void cleanup_file_workingset(void) +{ + remove(TEMP_FILE); +} + +int alloc_file_workingset(void *arg) +{ + int err =3D 0; + char *ptr; + int fd; + int ppid; + char *mapped; + size_t size =3D (size_t)arg; + size_t page_size =3D getpagesize(); + + ppid =3D getppid(); + + fd =3D open(TEMP_FILE, O_RDWR | O_CREAT); + if (fd < 0) { + err =3D -errno; + ksft_perror("failed to open temp file\n"); + goto cleanup; + } + + if (fallocate(fd, 0, 0, size) < 0) { + err =3D -errno; + ksft_perror("fallocate"); + goto cleanup; + } + + mapped =3D (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, + fd, 0); + if (mapped =3D=3D NULL) { + err =3D -errno; + ksft_perror("mmap"); + goto cleanup; + } + + while (getppid() =3D=3D ppid) { + sync(); + for (ptr =3D mapped; ptr < mapped + size; ptr +=3D page_size) + *ptr =3D *ptr ^ 0xFF; + } + +cleanup: + cleanup_file_workingset(); + return err; +} + +int alloc_anon_workingset(void *arg) +{ + char *buf, *ptr; + int ppid =3D getppid(); + size_t size =3D (size_t)arg; + size_t page_size =3D getpagesize(); + + buf =3D malloc(size); + + if (!buf) { + ksft_print_msg("cannot allocate anon workingset"); + exit(1); + } + + while (getppid() =3D=3D ppid) { + for (ptr =3D buf; ptr < buf + size; ptr +=3D page_size) + *ptr =3D *ptr ^ 0xFF; + } + + free(buf); + return 0; +} diff --git a/tools/testing/selftests/mm/workingset_report.h b/tools/testing= /selftests/mm/workingset_report.h new file mode 100644 index 000000000000..c5c281e4069b --- /dev/null +++ b/tools/testing/selftests/mm/workingset_report.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef WORKINGSET_REPORT_H_ +#define WORKINGSET_REPORT_H_ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include +#include +#include +#include +#include + +#define PAGETYPE_ANON 0 +#define PAGETYPE_FILE 1 +#define ANON_AND_FILE 2 + +int get_nr_nodes(void); +int drop_pagecache(void); + +long sysfs_get_refresh_interval(int nid); +int sysfs_set_refresh_interval(int nid, long interval); + +int sysfs_get_page_age_intervals_str(int nid, char *buf, int len); +int sysfs_set_page_age_intervals_str(int nid, const char *buf, int len); + +int sysfs_set_page_age_intervals(int nid, const char *const intervals[], + int nr_intervals); + +char *page_age_split_node(char *buf, int nid, char **next); +ssize_t sysfs_page_age_read(int nid, char *buf, size_t len); +ssize_t page_age_read(const char *buf, const char *interval, int pagetype); + +int alloc_file_workingset(void *arg); +void cleanup_file_workingset(void); +int alloc_anon_workingset(void *arg); + +#endif /* WORKINGSET_REPORT_H_ */ diff --git a/tools/testing/selftests/mm/workingset_report_test.c b/tools/te= sting/selftests/mm/workingset_report_test.c new file mode 100644 index 000000000000..89ff4e9d746e --- /dev/null +++ b/tools/testing/selftests/mm/workingset_report_test.c @@ -0,0 +1,330 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "workingset_report.h" + +#include +#include +#include +#include + +#include "../clone3/clone3_selftests.h" + +#define REFRESH_INTERVAL 5000 +#define MB(x) (x << 20) + +static void sleep_ms(int milliseconds) +{ + struct timespec ts; + + ts.tv_sec =3D milliseconds / 1000; + ts.tv_nsec =3D (milliseconds % 1000) * 1000000; + nanosleep(&ts, NULL); +} + +/* + * Checks if two given values differ by less than err% of their sum. + */ +static inline int values_close(long a, long b, int err) +{ + return labs(a - b) <=3D (a + b) / 100 * err; +} + +static const char * const PAGE_AGE_INTERVALS[] =3D { + "6000", "10000", "15000", "18446744073709551615", +}; +#define NR_PAGE_AGE_INTERVALS (ARRAY_SIZE(PAGE_AGE_INTERVALS)) + +static int set_page_age_intervals_all_nodes(const char *intervals, int nr_= nodes) +{ + int i; + + for (i =3D 0; i < nr_nodes; ++i) { + int err =3D sysfs_set_page_age_intervals_str( + i, &intervals[i * 1024], strlen(&intervals[i * 1024])); + + if (err < 0) + return err; + } + return 0; +} + +static int get_page_age_intervals_all_nodes(char *intervals, int nr_nodes) +{ + int i; + + for (i =3D 0; i < nr_nodes; ++i) { + int err =3D sysfs_get_page_age_intervals_str( + i, &intervals[i * 1024], 1024); + + if (err < 0) + return err; + } + return 0; +} + +static int set_refresh_interval_all_nodes(const long *interval, int nr_nod= es) +{ + int i; + + for (i =3D 0; i < nr_nodes; ++i) { + int err =3D sysfs_set_refresh_interval(i, interval[i]); + + if (err < 0) + return err; + } + return 0; +} + +static int get_refresh_interval_all_nodes(long *interval, int nr_nodes) +{ + int i; + + for (i =3D 0; i < nr_nodes; ++i) { + long val =3D sysfs_get_refresh_interval(i); + + if (val < 0) + return val; + interval[i] =3D val; + } + return 0; +} + +static pid_t clone_and_run(int fn(void *arg), void *arg) +{ + pid_t pid; + + struct __clone_args args =3D { + .exit_signal =3D SIGCHLD, + }; + + pid =3D sys_clone3(&args, sizeof(struct __clone_args)); + + if (pid =3D=3D 0) + exit(fn(arg)); + + return pid; +} + +static int read_workingset(int pagetype, int nid, + unsigned long page_age[NR_PAGE_AGE_INTERVALS]) +{ + int i, err; + char buf[4096]; + + err =3D sysfs_page_age_read(nid, buf, sizeof(buf)); + if (err < 0) + return err; + + for (i =3D 0; i < NR_PAGE_AGE_INTERVALS; ++i) { + err =3D page_age_read(buf, PAGE_AGE_INTERVALS[i], pagetype); + if (err < 0) + return err; + page_age[i] =3D err; + } + + return 0; +} + +static ssize_t read_interval_all_nodes(int pagetype, int interval) +{ + int i, err; + unsigned long page_age[NR_PAGE_AGE_INTERVALS]; + ssize_t ret =3D 0; + int nr_nodes =3D get_nr_nodes(); + + for (i =3D 0; i < nr_nodes; ++i) { + err =3D read_workingset(pagetype, i, page_age); + if (err < 0) + return err; + + ret +=3D page_age[interval]; + } + + return ret; +} + +#define TEST_SIZE MB(500l) + +static int run_test(int f(void)) +{ + int i, err, test_result; + long *old_refresh_intervals; + long *new_refresh_intervals; + char *old_page_age_intervals; + int nr_nodes =3D get_nr_nodes(); + + if (nr_nodes <=3D 0) { + ksft_print_msg("failed to get nr_nodes\n"); + return KSFT_FAIL; + } + + old_refresh_intervals =3D calloc(nr_nodes, sizeof(long)); + new_refresh_intervals =3D calloc(nr_nodes, sizeof(long)); + old_page_age_intervals =3D calloc(nr_nodes, 1024); + + if (!(old_refresh_intervals && new_refresh_intervals && + old_page_age_intervals)) { + ksft_print_msg("failed to allocate memory for intervals\n"); + return KSFT_FAIL; + } + + err =3D get_refresh_interval_all_nodes(old_refresh_intervals, nr_nodes); + if (err < 0) { + ksft_print_msg("failed to read refresh interval\n"); + return KSFT_FAIL; + } + + err =3D get_page_age_intervals_all_nodes(old_page_age_intervals, nr_nodes= ); + if (err < 0) { + ksft_print_msg("failed to read page age interval\n"); + return KSFT_FAIL; + } + + for (i =3D 0; i < nr_nodes; ++i) + new_refresh_intervals[i] =3D REFRESH_INTERVAL; + + for (i =3D 0; i < nr_nodes; ++i) { + err =3D sysfs_set_page_age_intervals(i, PAGE_AGE_INTERVALS, + NR_PAGE_AGE_INTERVALS - 1); + if (err < 0) { + ksft_print_msg("failed to set page age interval\n"); + test_result =3D KSFT_FAIL; + goto fail; + } + } + + err =3D set_refresh_interval_all_nodes(new_refresh_intervals, nr_nodes); + if (err < 0) { + ksft_print_msg("failed to set refresh interval\n"); + test_result =3D KSFT_FAIL; + goto fail; + } + + sync(); + drop_pagecache(); + + test_result =3D f(); + +fail: + err =3D set_refresh_interval_all_nodes(old_refresh_intervals, nr_nodes); + if (err < 0) { + ksft_print_msg("failed to restore refresh interval\n"); + test_result =3D KSFT_FAIL; + } + err =3D set_page_age_intervals_all_nodes(old_page_age_intervals, nr_nodes= ); + if (err < 0) { + ksft_print_msg("failed to restore page age interval\n"); + test_result =3D KSFT_FAIL; + } + return test_result; +} + +static char *file_test_path; +static int test_file(void) +{ + ssize_t ws_size_ref, ws_size_test; + int ret =3D KSFT_FAIL, i; + pid_t pid =3D 0; + + if (!file_test_path) { + ksft_print_msg("Set a path to test file workingset\n"); + return KSFT_SKIP; + } + + ws_size_ref =3D read_interval_all_nodes(PAGETYPE_FILE, 0); + if (ws_size_ref < 0) + goto cleanup; + + pid =3D clone_and_run(alloc_file_workingset, (void *)TEST_SIZE); + if (pid < 0) + goto cleanup; + + read_interval_all_nodes(PAGETYPE_FILE, 0); + sleep_ms(REFRESH_INTERVAL); + + for (i =3D 0; i < 3; ++i) { + sleep_ms(REFRESH_INTERVAL); + ws_size_test =3D read_interval_all_nodes(PAGETYPE_FILE, 0); + ws_size_test +=3D read_interval_all_nodes(PAGETYPE_FILE, 1); + if (ws_size_test < 0) + goto cleanup; + + if (!values_close(ws_size_test - ws_size_ref, TEST_SIZE, 10)) { + ksft_print_msg( + "file working set size difference too large: actual=3D%ld, expected=3D= %ld\n", + ws_size_test - ws_size_ref, TEST_SIZE); + goto cleanup; + } + } + ret =3D KSFT_PASS; + +cleanup: + if (pid > 0) + kill(pid, SIGKILL); + cleanup_file_workingset(); + return ret; +} + +static int test_anon(void) +{ + ssize_t ws_size_ref, ws_size_test; + pid_t pid =3D 0; + int ret =3D KSFT_FAIL, i; + + ws_size_ref =3D read_interval_all_nodes(PAGETYPE_ANON, 0); + ws_size_ref +=3D read_interval_all_nodes(PAGETYPE_ANON, 1); + if (ws_size_ref < 0) + goto cleanup; + + pid =3D clone_and_run(alloc_anon_workingset, (void *)TEST_SIZE); + if (pid < 0) + goto cleanup; + + sleep_ms(REFRESH_INTERVAL); + read_interval_all_nodes(PAGETYPE_ANON, 0); + + for (i =3D 0; i < 5; ++i) { + sleep_ms(REFRESH_INTERVAL); + ws_size_test =3D read_interval_all_nodes(PAGETYPE_ANON, 0); + ws_size_test +=3D read_interval_all_nodes(PAGETYPE_ANON, 1); + if (ws_size_test < 0) + goto cleanup; + + if (!values_close(ws_size_test - ws_size_ref, TEST_SIZE, 10)) { + ksft_print_msg( + "anon working set size difference too large: actual=3D%ld, expected=3D= %ld\n", + ws_size_test - ws_size_ref, TEST_SIZE); + goto cleanup; + } + } + ret =3D KSFT_PASS; + +cleanup: + if (pid > 0) + kill(pid, SIGKILL); + return ret; +} + + +#define T(x) { x, #x } +struct workingset_test { + int (*fn)(void); + const char *name; +} tests[] =3D { + T(test_anon), + T(test_file), +}; +#undef T + +int main(int argc, char **argv) +{ + int i, err; + + if (argc > 1) + file_test_path =3D argv[1]; + + for (i =3D 0; i < ARRAY_SIZE(tests); i++) { + err =3D run_test(tests[i].fn); + ksft_test_result_code(err, tests[i].name, NULL); + } + return 0; +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Sun Feb 8 00:11:54 2026 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 0BF2A1A4F03 for ; Tue, 13 Aug 2024 16:59:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568392; cv=none; b=boH6RNCB92Zh/oE7bckA2NyjN1uwA7oJOpcjUzDOr02GJSFMJI0iSo0//AKoBQ9YbWi5vnV+ltORJjKoAAqRtBceRevsqBz7ItqeycdqhJCuxNwgt59CdoedvGi95QGoaAhqfV/hw4hldn0FlKYIlm7gs4IHCoO1j0nTrlNtdG0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723568392; c=relaxed/simple; bh=E3sZsNR1YDqdKFQzjzHncZWFfHtHPWAP/JNt5fBaZxE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HUrDKeYGwg6pmLLdNKvIQJkderdT95819MTjCOyQEUvVaau4IpR+Kra2S9hZZFJBcWirR4l8NCqfM7SGAdZs/8jExsPn9x7vpRCN34eoJXX9zQgDJYTLk9xNWIBtrSx6E/Aoqx8caXsKcu4RbYM7aQlhQl7NmtzDoDSe/Gt/MUE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--yuanchu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OKSwEPTo; arc=none smtp.client-ip=209.85.219.202 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--yuanchu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OKSwEPTo" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0be7c74d79so8484950276.0 for ; Tue, 13 Aug 2024 09:59:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723568390; x=1724173190; 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=hOP5SFFIvPyG4OwQTVSroxGri5wlFhobHFeL7lq0KZY=; b=OKSwEPToulZl0T2izwfPaRPN6vFy2Ki+eztr4Ofu2n8anxMZpvueHA5TRSepOjckvw 1XtpUP0K8pBKDvotcZUpnntALkCe71c55Tqw/8ZKotBE9RMfvgIdy9LhC10xUtnfNSnB N0sk325a+O6S0fXbk3TZSjmJm7lMYfm7OH198gJN3n/c6X/izQ5V78tdnrdI7t+Rwa+m TxsNRby1cdFkSkCJFyMlAPGmtQT/P378e4dvuyiQHg7+XBBUemWA3pa51eSLgr2GpXYi IdWh/dIygaeDLTHrC4BMZvLfl9JGdS2U1QVH52BA44N77y7ggOICaqqEea/p/B3NRCf2 dBdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723568390; x=1724173190; 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=hOP5SFFIvPyG4OwQTVSroxGri5wlFhobHFeL7lq0KZY=; b=msZsbH+MXcXRiEFXSDyvJD/2W+3A/qduYraOjzdcs0RTffNrOWIfOZpNifEB/jpTCv UH2NLgfTNza7IolmaY11q0QG5CemkvwpKUQoXRcgYxMLc8mYcjGcY+tgs48EJzt2y00s dQfdN8v83lep/O0GWErUSGokvWVnCRInI1IpgdgAAb703ET2d1AYEpHLDbad/klT/Kh2 i3dNmFiIwjXj9whlkyXbz+QsgFpSs++3w4jdQji079Fg/I9KrXVloSKLJeSu+EHF5VHJ J4ae7weyXZr+nTkRy4M2vWJ3xSmcvXm+xrdGi/cmA/HlYQNvSw9RDxFOMAiCRaIb4zI3 uNdw== X-Forwarded-Encrypted: i=1; AJvYcCWwawLiQQjbKR64xCskTzh92Qac/6WIiRZtN6zxD9puRvTR/pCfMhRM70yYKjnh/QjorJLV7/jE/UgdX081DGGSDPSgt5Ng7It/73q0 X-Gm-Message-State: AOJu0YydsSzt+uV4W/NwxWBRYXuM8yCt9aSGny/V5jzMhqbVL5T//M4G OEfAiBSFypvyhallXAeychIG/YST4ZhxEr3k6aA0MtDhcToxkbxHXZiNMAZviOh0PGQbxmGdMvh WZ2/1DQ== X-Google-Smtp-Source: AGHT+IECAXtBsp42GLKJ5N1BoyX/FjeEZsFBUtplL8hab3FEghl0VDuEX9j5TFSLO+qQ7fOJRfdfV8wWTHGk X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:b50c:66e8:6532:a371]) (user=yuanchu job=sendgmr) by 2002:a25:7450:0:b0:e05:fc91:8935 with SMTP id 3f1490d57ef6-e1155a4ac06mr3055276.3.1723568389892; Tue, 13 Aug 2024 09:59:49 -0700 (PDT) Date: Tue, 13 Aug 2024 09:56:18 -0700 In-Reply-To: <20240813165619.748102-1-yuanchu@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240813165619.748102-1-yuanchu@google.com> X-Mailer: git-send-email 2.46.0.76.ge559c4bf1a-goog Message-ID: <20240813165619.748102-8-yuanchu@google.com> Subject: [PATCH v3 7/7] Docs/admin-guide/mm/workingset_report: document sysfs and memcg interfaces From: Yuanchu Xie To: David Hildenbrand , "Aneesh Kumar K.V" , Khalid Aziz , Henry Huang , Yu Zhao , Dan Williams , Gregory Price , Huang Ying , Andrew Morton , Lance Yang , Randy Dunlap , Muhammad Usama Anjum Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Shuah Khan , Yosry Ahmed , Matthew Wilcox , Sudarshan Rajagopalan , Kairui Song , "Michael S. Tsirkin" , Vasily Averin , Nhat Pham , Miaohe Lin , Qi Zheng , Abel Wu , "Vishal Moola (Oracle)" , Kefeng Wang , Yuanchu Xie , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add workingset reporting documentation for better discoverability of its sysfs and memcg interfaces. Also document the required kernel config to enable workingset reporting. Change-Id: Ib9dfc9004473baa6ef26ca7277d220b6199517de Signed-off-by: Yuanchu Xie --- Documentation/admin-guide/mm/index.rst | 1 + .../admin-guide/mm/workingset_report.rst | 105 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 Documentation/admin-guide/mm/workingset_report.rst diff --git a/Documentation/admin-guide/mm/index.rst b/Documentation/admin-g= uide/mm/index.rst index 8b35795b664b..61a2a347fc91 100644 --- a/Documentation/admin-guide/mm/index.rst +++ b/Documentation/admin-guide/mm/index.rst @@ -41,4 +41,5 @@ the Linux memory management. swap_numa transhuge userfaultfd + workingset_report zswap diff --git a/Documentation/admin-guide/mm/workingset_report.rst b/Documenta= tion/admin-guide/mm/workingset_report.rst new file mode 100644 index 000000000000..ddcc0c33a8df --- /dev/null +++ b/Documentation/admin-guide/mm/workingset_report.rst @@ -0,0 +1,105 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Workingset Report +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Workingset report provides a view of memory coldness in user-defined +time intervals, i.e. X bytes are Y milliseconds cold. It breaks down +the user pages in the system per-NUMA node, per-memcg, for both +anonymous and file pages into histograms that look like: +:: + + 1000 anon=3D137368 file=3D24530 + 20000 anon=3D34342 file=3D0 + 30000 anon=3D353232 file=3D333608 + 40000 anon=3D407198 file=3D206052 + 9223372036854775807 anon=3D4925624 file=3D892892 + +The workingset reports can be used to drive proactive reclaim, by +identifying the number of cold bytes in a memcg, then writing to +``memory.reclaim``. + +Quick start +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Build the kernel with the following configurations. The report relies +on Multi-gen LRU for page coldness. + +* ``CONFIG_LRU_GEN=3Dy`` +* ``CONFIG_LRU_GEN_ENABLED=3Dy`` +* ``CONFIG_WORKINGSET_REPORT=3Dy`` + +Optionally, the aging kernel daemon can be enabled with the following +configuration. +* ``CONFIG_WORKINGSET_REPORT_AGING=3Dy`` + +Sysfs interfaces +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +``/sys/devices/system/node/nodeX/workingset_report/page_age`` provides +a per-node page age histogram, showing an aggregate of the node's lruvecs. +Reading this file causes a hierarchical aging of all lruvecs, scanning +pages and creates a new Multi-gen LRU generation in each lruvec. +For example: +:: + + 1000 anon=3D0 file=3D0 + 2000 anon=3D0 file=3D0 + 100000 anon=3D5533696 file=3D5566464 + 18446744073709551615 anon=3D0 file=3D0 + +``/sys/devices/system/node/nodeX/workingset_report/page_age_intervals`` +is a comma separated list of time in milliseconds that configures what +the page age histogram uses for aggregation. For the above histogram, +the intervals are: +:: + 1000,2000,100000 + +``/sys/devices/system/node/nodeX/workingset_report/refresh_interval`` +defines the amount of time the report is valid for in milliseconds. +When a report is still valid, reading the ``page_age`` file shows +the existing valid report, instead of generating a new one. + +``/sys/devices/system/node/nodeX/workingset_report/report_threshold`` +specifies how often the userspace agent can be notified for node +memory pressure, in milliseconds. When a node reaches its low +watermarks and wakes up kswapd, programs waiting on ``page_age`` are +woken up so they can read the histogram and make policy decisions. + +Memcg interface +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +While ``page_age_interval`` is defined per-node in sysfs, ``page_age``, +``refresh_interval`` and ``report_threshold`` are available per-memcg. + +``/sys/fs/cgroup/.../memory.workingset.page_age`` +The memcg equivalent of the sysfs workingset page age histogram +breaks down the workingset of this memcg and its children into +page age intervals. Each node is prefixed with a node header and +a newline. Non-proactive direct reclaim on this memcg can also +wake up userspace agents that are waiting on this file. +e.g. +:: + + N0 + 1000 anon=3D0 file=3D0 + 2000 anon=3D0 file=3D0 + 3000 anon=3D0 file=3D0 + 4000 anon=3D0 file=3D0 + 5000 anon=3D0 file=3D0 + 18446744073709551615 anon=3D0 file=3D0 + +``/sys/fs/cgroup/.../memory.workingset.refresh_interval`` +The memcg equivalent of the sysfs refresh interval. A per-node +number of how much time a page age histogram is valid for, in +milliseconds. +e.g. +:: + + echo N0=3D2000 > memory.workingset.refresh_interval + +``/sys/fs/cgroup/.../memory.workingset.report_threshold`` +The memcg equivalent of the sysfs report threshold. A per-node +number of how often userspace agent waiting on the page age +histogram can be woken up, in milliseconds. +e.g. +:: + + echo N0=3D1000 > memory.workingset.report_threshold --=20 2.46.0.76.ge559c4bf1a-goog