From nobody Wed Dec 17 21:00:58 2025 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 576291CFAF for ; Sat, 4 May 2024 07:30:57 +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=1714807859; cv=none; b=GYJxnUnQGX8LmxBsKUK0bE0EtefFVofK5ey1twqxgYftjHNI123LlQQcVnBnycmFf8kJn/BRWBxmviHx+HceoVhk0cm22a+F/R6TDvO7YMGlZHZFq+Mi85olN0Pd1y4jv+F16Ws6haiZfJ1SdrWo80fD+9WcWsQ+VBmAa4CLRxo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714807859; c=relaxed/simple; bh=behUX3jjuMClKmnueKgHMjdB/LO1/p0zjg69wS3+fro=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=J6gb0sbM02vRu3NB6yN74zJbZfyIAXLVyMv2w6s0lwPY/yCCzSany+bvoPIAickNy95rmuFwvdtJCKPaHFLHSQ7JzQjTR0FDep6acfLmmOCiFVcul6KAqjLqt5TfMI6YAuGSoVCxhbT2Mh7YR4P+3TEBjx1xXOp0xF3R4catmOw= 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=4OXVLhDZ; 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="4OXVLhDZ" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-de604ccb373so733317276.2 for ; Sat, 04 May 2024 00:30:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714807856; x=1715412656; 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=rp6y1AtpkHfeXuTgpbngprvPTppmrZY5FskXOV8DSWk=; b=4OXVLhDZ6NRYxuizriSi6cA4LGUsT5EbRG6qIZnzqCIoO/0RXr7nelf6Nhi1kFwIxn DCDmsM4l3s/kEvUKtoTQuRXEG1MPUS52fpLTCnfmO3u8uKKBaASuAZCXTU/t8cgcCc5v qgCPYl8FRoxOMmI78zYJdGdyCBsV55IckaNZAvFYkXjbh3/qlOYZYPxgB7X4Fz0fsP/S NtPEC8F+9NFFJcyHqG3NcZfcxqNYkZlKwhJ1aiOn75zT2i12/pfuqr57uD9PHV4UWVmV OF/GrS+xIqA6ZzWZdKhRpHgtrEK/Da+BMcoqtUn6tsCYXEDPSJQmaqU5mUUyo4Ow9cmT 6JUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714807856; x=1715412656; 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=rp6y1AtpkHfeXuTgpbngprvPTppmrZY5FskXOV8DSWk=; b=CMA3ZcmvKtqX2ZS6I6Y3bVck8AqXfNyKAwgtH/APbM75GMxjMAs4n/Nn3N+rCA4E0Q UanGhPvCCdej5/1G2bLW4TPSSgSYeamEj3T8zxQR+qCrPR7F4N71wdX8iq6sazv8s7x0 JMQdTYi5SqNLi2BhVNQdQCLYb5w5T8lRhYJJaV0H6BfYDq3p+6fB5Y/kpHiE99kBUEVK Ctgot3gzu6FkJ0upyFF1zP9aJWBBhIiwGZSBFNw+idtJ8b9H9H/QaYNTgmLwzOWLNA7x yom/finLdTVBAYXWktx8JKce3n/BEnF6+/tmSx2bOOs0pr2M6AhKBXHcNk7KqGSAanYi mfEw== X-Forwarded-Encrypted: i=1; AJvYcCXIPE+rtLtPEEZAs79OmLsKlnFWNnzEBUs/YYVtJ/iTtD3io2uInjGsxvOmEHCqB2rkMCuPH0HP6qNZFNjNQbNgx9vz4jQ4EU12ht1d X-Gm-Message-State: AOJu0YzT0ddqAY9i9s7sOIgLFrO1AMdFPqy73AFIsfRVp0Q3qIYmc7s9 T/vEardUjn/lazlyYdYH4FYMajGSAqkOggHMQ3tKrKiVC50mC8k5upp+esmICCbDOZUodosj6eM u93q+kw== X-Google-Smtp-Source: AGHT+IGU6CJeYeCi16Op/dGx3kAMymE/Mcfr7yxitn52cAsdXe0smsbTYAlXgGjRedM+9wSAtFV6UoqmPn5w X-Received: from yuanchu-desktop.svl.corp.google.com ([2620:15c:2a3:200:da8f:bd07:9977:eb21]) (user=yuanchu job=sendgmr) by 2002:a05:6902:1242:b0:de6:1603:2dd5 with SMTP id t2-20020a056902124200b00de616032dd5mr680541ybu.9.1714807856331; Sat, 04 May 2024 00:30:56 -0700 (PDT) Date: Sat, 4 May 2024 00:30:10 -0700 In-Reply-To: <20240504073011.4000534-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: <20240504073011.4000534-1-yuanchu@google.com> X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog Message-ID: <20240504073011.4000534-7-yuanchu@google.com> Subject: [PATCH v1 6/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 Cc: Kalesh Singh , Wei Xu , David Rientjes , Greg Kroah-Hartman , "Rafael J. Wysocki" , Andrew Morton , 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 --- 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 212f203b10b9..1e6aa1bd63f2 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1270,6 +1270,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 57093657030d..7caae7f2d6cf 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -93,6 +93,7 @@ obj-$(CONFIG_TRANSPARENT_HUGEPAGE) +=3D huge_memory.o khu= gepaged.o obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.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 c6c0d2772279..6ada26da6de6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7060,12 +7060,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); @@ -7084,9 +7084,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 @@ -7137,7 +7141,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 5a9bf3ebb914..46bb9469d5b3 100644 --- a/mm/workingset_report.c +++ b/mm/workingset_report.c @@ -258,7 +258,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); @@ -275,10 +275,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; @@ -341,6 +345,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) @@ -362,9 +367,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 @@ -454,7 +463,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.45.0.rc1.225.g2a3ae87e7f-goog