From patchwork Tue Aug 13 16:56:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuanchu Xie X-Patchwork-Id: 13762348 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E80BC52D7C for ; Tue, 13 Aug 2024 16:59:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75A406B00A0; Tue, 13 Aug 2024 12:59:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C0546B00A1; Tue, 13 Aug 2024 12:59:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4726D6B00A2; Tue, 13 Aug 2024 12:59:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 247DE6B00A0 for ; Tue, 13 Aug 2024 12:59:49 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D0A7A1209C6 for ; Tue, 13 Aug 2024 16:59:48 +0000 (UTC) X-FDA: 82447834056.08.ABB3E4D Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf14.hostedemail.com (Postfix) with ESMTP id F318A10001C for ; Tue, 13 Aug 2024 16:59:46 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=a74FgDtV; spf=pass (imf14.hostedemail.com: domain of 3AZG7ZgcKCE4EAq3sxAw44w1u.s421y3AD-220Bqs0.47w@flex--yuanchu.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3AZG7ZgcKCE4EAq3sxAw44w1u.s421y3AD-220Bqs0.47w@flex--yuanchu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723568315; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xpAcupGObnfzDmBhsss97bo400nzNISpyaH5VDbdw9g=; b=KVX6ujKXm6HZmsAWPFeN8HR5DcOTOjFVR/5Su1xESe12ZEuBdNZid2tPAgLcIdPDbH/y2k ynJ/UNtgZCdeKKclupGgi4f99+wZfGVl7I6HiBVWQ0cUeBNrJUgLMYePRAKfAdv17xYBeI aA37neZJ6lTjZi7TBa9cpvkW0f9dzf4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723568315; a=rsa-sha256; cv=none; b=fy7Z+B7KZXQM87uASEzjLqYeLoDdeiAuBATpUz++29A62ooYgz7dTjpDxI3t/xBc0l8kVa YLQuSDKB1uke20isWki4alyEU7iR7aBqgd0HBZC7Rq3gpSCLCI379CpBiCif00XFDVRENS 8Eof2SEEcwm8aJFsNmf9SxaXA8azEDk= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=a74FgDtV; spf=pass (imf14.hostedemail.com: domain of 3AZG7ZgcKCE4EAq3sxAw44w1u.s421y3AD-220Bqs0.47w@flex--yuanchu.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3AZG7ZgcKCE4EAq3sxAw44w1u.s421y3AD-220Bqs0.47w@flex--yuanchu.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-664b7a67ad4so130375617b3.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=kvack.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=a74FgDtV5G37gItFnoOROBRIS5WkHjgUD1lRsXYB1hFhalXJP3/hFmNegXdU3Ut8Lj DYuTBTGd4R1Mb2nunIIBQ70zvnpRpn6LBEOoBdjD7GAOswWRZwfuhkUdUfVUMLRS3yzm MRj2dZMvPRBjV8naUAQcauD5kMk+keCg/eh7ee6SjbVaqwRVM/FOSl+3F6RQ1BuN2KwG vs//wtXaoYVmnJIYszFAZ3414C7b/F3jhYlYBnchHoOxdRC3g/Wse0oWIpGTuvPSlrCA ldryMtYwWAEK/cPmZmjP+cAzEak4K72YH5j4SxsXKOLsKbr5MrOYl72WA/p9KrZ+XPUe 0blA== 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=kDaM5WY1qFO0eHH0xll59U3SWnPNn1+NdwLJ/oNd33bc8H+ICSScxLOARIPbZ7HKL1 KRwCqs1Uy9jiatiJ9Z3tBizB4Z73flpBgRsgsx9ryywC3te32yYkzSph9yqzOMBn7uQa ojKEeMSpmCehpvlWMwY3d2CNBqsrRSwgKR6Qc7lGYKMqRtRwqg7fBrRBod7Jx9isiBhR PT0VNbelQoWkTdExC/uA1ZFC+0pTFzcFseaVL8xKsOWX07ZETXe4aCZqTdPLO7aBArqc oZsq+ZDMPcbSX2xQ80TYLIV4l8iOOSSPCC/M2hh8z5c3TycGtieGmuwQvecGmRDujo6E 01+w== X-Forwarded-Encrypted: i=1; AJvYcCXFnCCn5F1tvPq+uPgrQo5ohVrUcA/2vgyk2+p+6xBOiTgtmcusLGuEl/NyEeYP4wciJQhzkqmWaQ==@kvack.org X-Gm-Message-State: AOJu0YwLIHzi+wqXexiki2BTBa8WgfTRLWEkTA+VuTgCOJbTEGh0FkBS l4FUA9HE6poBhLJkj0M0C/souFQuqNDYC2kYlX7jUNNyFnVMK4NORMAV4yoAPBD2/AMQkO9CQIA yuXUrXg== 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> 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 X-St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KyRSAyTfq5 S9ve5vTV 8sjG5Ci8cf8opBJZT8FKUGqt8CG0GSAJ9aF+y+axbh9ww4p0IYs2buaOdd7JnpGw36fnLRWjULCqD9Amx5pHsU+KqtpxMHhWZOwE499a6q9+tWzaBjGf4pN7cbLALM/LeLD7K+KS2S99HPMisgo7WmXJMAajzF5Atx2KPgpmDd9zSN5CEA9oagHv4dXy7pooBqxkWO93MQLoqfy2ki0+tP402t1zzCyajUUxfEaq1FER2+MQJwdVPIOrKyQwNnEHsyK8f4+pK2QPIs40MNIoSotL4bb9c7NzEzC+3zpMjG0FUQH8ftBc9eOomng9jXkfeCLrSUeyN9SSwXw5sdmWt85liKvdHUZ/l2kHdM88C85/cmOPuk9hS/8z7O1pnRVdrQbo+pZIwRlFUwqLGjTEpzphCxUZujvuQoO3eauWT2raY1PYEaYFYaSy1RXJswAoR53kkyPANqRcXJUyYm7hUOmDJ2uimbbkREDIH6ngko4NBTrfbh7NBB0KuBzekn22Nfvp/3AxtEFtAsOE5emYOS3ikFRIwfrJc8GDGyROCPWXZKNiH6G2RuIBBarTA0EREx5SA1BR4Hd2cgGXfqmKygb2Ol0AdbWZQbtnF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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_report.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. +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" 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) += page_counter.o obj-$(CONFIG_MEMCG_V1) += memcontrol-v1.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o obj-$(CONFIG_WORKINGSET_REPORT) += workingset_report.o +obj-$(CONFIG_WORKINGSET_REPORT_AGING) += workingset_report_aging.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) += 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(struct kernfs_open_file *of, { unsigned int nid, msecs; struct wsr_state *wsr; + unsigned long old_interval; struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); ssize_t ret = memory_wsr_threshold_parse(buf, nbytes, &nid, &msecs); if (ret < 0) return ret; - wsr = &mem_cgroup_lruvec(memcg, NODE_DATA(nid))->wsr; mutex_lock(&wsr->page_age_lock); @@ -4397,9 +4397,13 @@ static ssize_t memory_ws_refresh_interval_write(struct kernfs_open_file *of, wsr->page_age = NULL; } + old_interval = 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; } @@ -4450,7 +4454,7 @@ static int memory_ws_page_age_show(struct seq_file *m, void *v) if (!READ_ONCE(wsr->page_age)) continue; - 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, } 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 = 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 = 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 *kobj, unsigned int interval; int err; struct wsr_state *wsr = kobj_to_wsr(kobj); + unsigned long old_interval = 0; err = kstrtouint(buf, 0, &interval); if (err) @@ -378,9 +383,13 @@ static ssize_t refresh_interval_store(struct kobject *kobj, wsr->page_age = NULL; } + old_interval = 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; } @@ -470,7 +479,7 @@ static ssize_t page_age_show(struct kobject *kobj, struct kobj_attribute *attr, int ret = 0; struct wsr_state *wsr = kobj_to_wsr(kobj); - wsr_refresh_report(wsr, NULL, kobj_to_pgdat(kobj)); + wsr_refresh_report(wsr, NULL, kobj_to_pgdat(kobj), NULL); 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 interval. + * While a userspace program can periodically read the page age breakdown + * per-memcg and trigger aging, the kernel performing aging is less overhead, + * more consistent, and more reliable for the use case where every memcg should + * 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 = true; + struct pglist_data *pgdat = NODE_DATA(nid); + + memcg = mem_cgroup_iter(NULL, NULL, NULL); + do { + struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); + struct wsr_state *wsr = &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 = false; + *next_wake_time = refresh_time; + } else if (time_before(refresh_time, *next_wake_time)) { + *next_wake_time = refresh_time; + } + } + + cond_resched(); + } while ((memcg = 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 = 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 = false; + next_wake_time = node_next_wake_time; + } else if (time_before(node_next_wake_time, + next_wake_time)) { + next_wake_time = node_next_wake_time; + } + } + + if (should_wait) { + wait_event_interruptible(aging_wait, refresh_pending); + continue; + } + + /* sleep until next aging */ + timeout_ticks = next_wake_time - jiffies; + if (timeout_ticks > 0 && + timeout_ticks != 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 = kthread_run(do_aging, NULL, "kagingd"); + + if (IS_ERR(task)) { + pr_err("Failed to create aging kthread\n"); + return PTR_ERR(task); + } + + aging_thread = task; + pr_info("module loaded\n"); + return 0; +} + +static void aging_exit(void) +{ + kthread_stop(aging_thread); + aging_thread = NULL; + pr_info("module unloaded\n"); +} + +module_init(aging_init); +module_exit(aging_exit);