From nobody Mon Feb 9 01:16:51 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