Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp278104lqh; Sat, 4 May 2024 00:31:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCULDVVcIdmMIjc2Ig6Oj28BtTREsXdUJZcM26omN3stErl6xrVqn+XtoBVFUSzBgiLJFDz/JsCNPyHODmDY5dERxYsZkJKj9EBJjsX0lQ== X-Google-Smtp-Source: AGHT+IHARidSulzq7vV9xs8JEa+Dt7bIYZAoYJlWlTuL3+I9S3iHCUBT01IbvDAxfTRCWDHSaLzV X-Received: by 2002:a05:6808:4c1:b0:3c8:6420:4604 with SMTP id a1-20020a05680804c100b003c864204604mr5277734oie.26.1714807918335; Sat, 04 May 2024 00:31:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714807918; cv=pass; d=google.com; s=arc-20160816; b=LHWBDzdEMOD00Kl++9sook6LbjsFCCQu9K7J27p1ZVFjlEB0F8c0gRs8Fp9PTtls9w TwJw3pK9DUXtTJmUs52n5v7Vu7FoBL3cgWwL4xsJ4/oqRwxyfBkvI0VvtGrVcDSaPaks SrBQF+4pBaLlxgazhAfgeLyHxSG991B432JaED3EkWP6DA0B+hsuukRIP4MWrZdy0blh 5VT7KA9/kACVhSZ/3wZmo3U8AHgVXhjIxwIDeLwoT4g8Qd8XuY7+4EaXhFBmn3QPptQB PUlR6BGkU+lODaI8D1WWcQh69KV1QdfOD1S/6nnjKi8wiiXgSOP5bPfu9jydSUu9cT50 MNRw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=xaEmyCMslYMwuxpJOVLJZ4UUBBVY/3Xfmzid12imJ38=; fh=4umCsT9OUPYk2yXfwEqm+BWT3RQ2WRJqE3igcOTm3pU=; b=omZ0L+ucQA1zPVnqbzANGm0+zGH79B5LXCW1Pqpc4+NQhgvyrGkys9OsZx/Dw8kZI4 1gcDHSd/Q6Rl8pia/qAF43dPa8hUnOTCoBbhVzME01T340kntq6Vi8LGBJWGvZ65TGDj ofeGJ15QCJ+RdhntP8yR6dbERWJhk6VzdgAfgR+RyO3ADXidlNBnwZ3e11l6vAYW1kk3 1zoo/+eKPrzFdrppRc71dqpqUtiNB6sBTuXBs3JbcwDJgzl7gFU1qZn7ithJeXpYu+OR ELGdhjz0LbRXPRh/1K7RSiI2iAOx97L2W/+RZyqZUV1aqk5pXTJs3azwtLHuP5CRkFMq yj7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="tVr/XQmT"; arc=pass (i=1 spf=pass spfdomain=flex--yuanchu.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-168537-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168537-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a8-20020a05622a064800b0043ae9ed6b10si5315429qtb.682.2024.05.04.00.31.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 May 2024 00:31:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168537-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="tVr/XQmT"; arc=pass (i=1 spf=pass spfdomain=flex--yuanchu.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-168537-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168537-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E620B1C20EBC for ; Sat, 4 May 2024 07:31:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 654941CAB1; Sat, 4 May 2024 07:30:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tVr/XQmT" 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 9E611182D2 for ; Sat, 4 May 2024 07:30:53 +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=1714807855; cv=none; b=XtUPB6c95YS7wcydCPcE1wGgHiamP77rHjybpGT6IdF4zpcWJjDYf4tNIGBjLt2D6BbDBKHAfMjlAIXaF66NXXJ0Y6uAynHui+TY791zQdKUgHzU1GcvU6DFA+r2TSMsM12k18VBcxYevU6Z9OATJxDLyhnal7NEfs+eXaJsyyM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714807855; c=relaxed/simple; bh=8BYM/0bwfvRiN/1tSxdyuXR2N0rM8FmGTxJpEOr2hEk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FuPP6HxtCfKq/4fWwEpDozQFsedegGmXJMoSA4FjOuCvUaU56O7k3kNgUGWrChI95nHi1lKfWur/RnXApYZ5zfqsM91VXHziOHL1WOYGXCX8RaeQHvXcBxJfrcp/EgfiraUm3ugjsT8XIdvrQHtbpchPMMmphdS63jAjBdLQMLM= 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=tVr/XQmT; 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 Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-ddaf2f115f2so732809276.3 for ; Sat, 04 May 2024 00:30:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714807852; x=1715412652; 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=xaEmyCMslYMwuxpJOVLJZ4UUBBVY/3Xfmzid12imJ38=; b=tVr/XQmTSO43vov5hcBa+jICF9/V5HkyIoGvjGJ86xu0ixLHgupRkWVxJOoSoP18i9 94TcOD06goxs9eV40sZ3HQVefv+aD0SlbrpZ0FoJ7P7LkiG3nWUuHYPINQcfWHUfaIgh PUQDInFzLW2CLZ7EO0ASoiLOk9WGPOzwR7ggGmJKgJnNhr3KesmikmMpEu3PiAYkkoCc myQOXwiUqCGeQyV/r/YIODoIsajgtsvjfDi9J5ewmyquxXlQPkkc5Vtm3O72qNJD+Mh1 Rmo7kHMxQLjQB8VPPT6RiPwnj/mc8Tl8gzrGnCiABinQkKnOtJco8h5gsDi8fEDL1jra GU/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714807852; x=1715412652; 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=xaEmyCMslYMwuxpJOVLJZ4UUBBVY/3Xfmzid12imJ38=; b=Jx+vXEELKG5UfwO/XaVjc2gHMX+O/vQvyLTjaasv6vMb15Ln5DdNV/wGCLOAFwAfAf rcD2Fw22o5wcPKRY3s3ZBJAW8W2ZEn2LrbjKnYsp4+nRO2uhZDivEfmQpi+PhfxdY84O /4tzsvIsBiMGkBUrXhhLZUvCj3fTuQpQWq/b0dV4TEF0psBc7nXVEkUfIUFrPTQTsCer N8tH+yD7w94wKiJbyWvCVtagXHgi0xHQ+ayX40REEbjCFevhWH1uxViRpqmsHqU5GEV2 DmWbb6wsadAIXmY3zru2aEwcgGu3hkKfjSKSnkkUlHq2DujD2B063sRy8DxeMHr52pgK Z7Vw== X-Forwarded-Encrypted: i=1; AJvYcCXh8l0uOAHKCRgvY3pNKHsnyKvxoQCN59z0VfN5vjypSFuXNBrQ+vBWBIt/7YA0Jm3b1pIia559RyPpQlVli2HEbjCKO856kbbg80JB X-Gm-Message-State: AOJu0YwXYacezaTKLPRlcQl0rtBDFzVqZW21bqV+t9OAkPoMHgPUuwKS nxkIq5aenmBiK5+vkAdnXCn8vd+dQh9FjWgTBJ0zPKjh4KsRsnqUjfvzc0iwPGTOmEP9glk7NMB 5Oj9cKQ== 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:c0b:b0:de5:3003:4b83 with SMTP id fs11-20020a0569020c0b00b00de530034b83mr681924ybb.8.1714807852560; Sat, 04 May 2024 00:30:52 -0700 (PDT) Date: Sat, 4 May 2024 00:30:08 -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-5-yuanchu@google.com> Subject: [PATCH v1 4/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 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-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 --- include/linux/workingset_report.h | 4 +++ mm/internal.h | 6 +++++ mm/vmscan.c | 44 +++++++++++++++++++++++++++++++ mm/workingset_report.c | 43 +++++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/include/linux/workingset_report.h b/include/linux/workingset_report.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 { }; 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 151f09c6983e..36480c7ac0dd 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -209,8 +209,14 @@ extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason /* * in mm/wsr.c */ +void notify_workingset(struct mem_cgroup *memcg, struct pglist_data *pgdat); /* Requires wsr->page_age_lock held */ void wsr_refresh_scan(struct lruvec *lruvec, unsigned long refresh_interval); +#else +static inline void notify_workingset(struct mem_cgroup *memcg, + struct pglist_data *pgdat) +{ +} #endif /* diff --git a/mm/vmscan.c b/mm/vmscan.c index b7293baac1dd..1f11b252c15e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2535,6 +2535,15 @@ static bool can_age_anon_pages(struct pglist_data *pgdat, return can_demote(pgdat->node_id, sc); } +#ifdef CONFIG_WORKINGSET_REPORT +static void try_to_report_workingset(struct pglist_data *pgdat, struct scan_control *sc); +#else +static inline void try_to_report_workingset(struct pglist_data *pgdat, + struct scan_control *sc) +{ +} +#endif + #ifdef CONFIG_LRU_GEN #ifdef CONFIG_LRU_GEN_ENABLED @@ -3936,6 +3945,8 @@ static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) if (!min_ttl || sc->order || sc->priority == DEF_PRIORITY) return; + try_to_report_workingset(pgdat, sc); + memcg = mem_cgroup_iter(NULL, NULL, NULL); do { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); @@ -5650,6 +5661,36 @@ void wsr_refresh_scan(struct lruvec *lruvec, unsigned long refresh_interval) } } } + +static void try_to_report_workingset(struct pglist_data *pgdat, + struct scan_control *sc) +{ + struct mem_cgroup *memcg = sc->target_mem_cgroup; + struct wsr_state *wsr = &mem_cgroup_lruvec(memcg, pgdat)->wsr; + unsigned long threshold = READ_ONCE(wsr->report_threshold); + + if (sc->priority == 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 */ @@ -6177,6 +6218,9 @@ static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc) if (zone->zone_pgdat == last_pgdat) continue; last_pgdat = zone->zone_pgdat; + + if (!sc->proactive) + try_to_report_workingset(zone->zone_pgdat, sc); shrink_node(zone->zone_pgdat, sc); } diff --git a/mm/workingset_report.c b/mm/workingset_report.c index 56155acbe7e9..7dcf38525016 100644 --- a/mm/workingset_report.c +++ b/mm/workingset_report.c @@ -295,6 +295,33 @@ static struct wsr_state *kobj_to_wsr(struct kobject *kobj) return &mem_cgroup_lruvec(NULL, kobj_to_pgdat(kobj))->wsr; } +static ssize_t report_threshold_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct wsr_state *wsr = kobj_to_wsr(kobj); + unsigned int threshold = 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 = 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 = + __ATTR_RW(report_threshold); + static ssize_t refresh_interval_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -449,6 +476,7 @@ static ssize_t page_age_show(struct kobject *kobj, struct kobj_attribute *attr, static struct kobj_attribute page_age_attr = __ATTR_RO(page_age); static struct attribute *workingset_report_attrs[] = { + &report_threshold_attr.attr, &refresh_interval_attr.attr, &page_age_intervals_attr.attr, &page_age_attr.attr, @@ -470,8 +498,13 @@ void wsr_init_sysfs(struct node *node) wsr = kobj_to_wsr(kobj); - 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 = + kernfs_walk_and_get(kobj->sd, "workingset_report/page_age"); } EXPORT_SYMBOL_GPL(wsr_init_sysfs); @@ -484,6 +517,14 @@ void wsr_remove_sysfs(struct node *node) return; wsr = 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 = &mem_cgroup_lruvec(memcg, pgdat)->wsr; + + kernfs_notify(wsr->page_age_sys_file); +} -- 2.45.0.rc1.225.g2a3ae87e7f-goog