From patchwork Wed Jun 21 18:04:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuanchu Xie X-Patchwork-Id: 111264 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp4567182vqr; Wed, 21 Jun 2023 11:41:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5NmmiKupO5yN/Fcz9m/0WBSfL08jWYQ+nYA0686TL1dCUEPA+qumrqnWZDVWfzyJ4yK5+/ X-Received: by 2002:a17:90a:ec14:b0:25b:e4dc:bd06 with SMTP id l20-20020a17090aec1400b0025be4dcbd06mr12416953pjy.18.1687372876199; Wed, 21 Jun 2023 11:41:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687372876; cv=none; d=google.com; s=arc-20160816; b=dr+Th1tC856R3szZZYqhu/SFn8bpWCoZIhWv+1SFMGgoG4pa8snrTI1fR9mfm+9L4d Y+2dBYkCi+n957OnaKVRX3LmWaXLWIIyVonPORybvIGQOA4QcQf97KW/nsUi0oTr3qCt OdcapjT7+1Bqn34igCWd5ywtiJuCtqzlyc2Sr0EnUDj53FGG9RNVsGVXHQ1qKevTubk3 U9tRxB+egxt5RpTQDpIF28JDRqgO3z7UA0XB3diQE3zCM69MHQiSJvOuZtMBnYQIrNEx 1LRFzdYSmlB62GP51Ui86bQOSPhm0wNVDYftV73Icq/MnjeAnQxYBOxiIZJbkZkvVIh2 nlHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=VxhcEKjfe7vCc52XMrwf8C6JOsMy8jrJM8NpygXtz0A=; b=uujLNzAGkkT6nn6LkarLQ5n2V+zawAARnVevVglWTJKYfAAdICLPmu3vUETeg7ABON 8dWW8V+NZ+/SSmu9HRUxrPEfvkWE8x1hhjT9Z777ENT8G+J2+/zHRJqx6ynGxo4MVoXd 2i6C16B8TW07urr/VDs6fVORfKhUO1wamDgiTCX/PmQBTzmwzx4GftrlD/aSZoa55cOK ko4nTP8u2DUdVPEA94+lSRXPOeMptLcAx3wGvVB3noNKFxJ+tLP0WgEYyCWPR91Vxs04 hLKXO+gKqiM86/FBlXr2zF5lFqaFBfij/SNbdqLlbNwLGRypC8dAYtoHRMS93NBZxBGW MBNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=YrxzTYP2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nm4-20020a17090b19c400b002402275fc56si4639130pjb.118.2023.06.21.11.41.02; Wed, 21 Jun 2023 11:41:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=YrxzTYP2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231517AbjFUSRS (ORCPT + 99 others); Wed, 21 Jun 2023 14:17:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231132AbjFUSRJ (ORCPT ); Wed, 21 Jun 2023 14:17:09 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2E4E120 for ; Wed, 21 Jun 2023 11:17:07 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bd69ee0edacso7912064276.3 for ; Wed, 21 Jun 2023 11:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687371427; x=1689963427; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VxhcEKjfe7vCc52XMrwf8C6JOsMy8jrJM8NpygXtz0A=; b=YrxzTYP2Y/JUxuvH4119nDn3m669dvyeu1ioqqJFdsHRVZAj+ZA9EqTpltTHxG50zq qfOTrJc6zM1jH8E4+2c3lCHG6eE3+bnJMfrPN5dNe5p0C2a8FPj+SyHCvquxF7n79tcF VHmZC11UUpY3AyupbmMbK35VBgwGx2dy7P5HJ+del76VoLJE5CBkEqHVxN6YMZmEjiNW Nm/tK449uQplLimDFQlk1RDQFbICohvd+xoAZOn5/kUpZH7auwDHZzdUwPamBHfS4zje DibmTNCjMMTpWrlCnyh4PwuG3h/Y48tI7tHM9kAgnxNARzwXIjzj+7DB8CzJyW06rhtB RT1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687371427; x=1689963427; 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=VxhcEKjfe7vCc52XMrwf8C6JOsMy8jrJM8NpygXtz0A=; b=A9+s6KL317dKQRA3/vWqCsvLV6/lqFJcKMVR70YdiJNHUvDVczQH7gj0lFmpQmFXsd 09GZ5+eqfhfGR5BkdrAoHW34oN+ughkM8FNjZ7AiUoUN8G7FnqE7gNuP9/VGzzcMkvyg F0l60paoQow1k9q0nrDHgXq61BRrd7qsfEjzkwdmprbMFQo5bSc4OgtoXp+A8oO6x8l5 3UsieKPvHaCoavNTQQImxvO7MTyXb2qjGPY5W13M6qbzrcQ01TE5MnZo9wj3usoMLYGR xkn9XbzTbdReGAPCoxzUMB2Qt4srXcRoiWZI7La5YVFJa++o/BDOvDR4Il2pNcc53wSv DO3g== X-Gm-Message-State: AC+VfDwSTXZJvZxyQfyPPuWLS8qIJGe1awFdHwKpIYCrSKPK6HeZ7Z2D Qp+/asJlaBIAgiyj7Yhc/GYujbwMlP8F X-Received: from yuanchu.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:4024]) (user=yuanchu job=sendgmr) by 2002:a25:7710:0:b0:be4:aab4:789b with SMTP id s16-20020a257710000000b00be4aab4789bmr6836965ybc.8.1687371426885; Wed, 21 Jun 2023 11:17:06 -0700 (PDT) Date: Wed, 21 Jun 2023 18:04:50 +0000 In-Reply-To: <20230621180454.973862-1-yuanchu@google.com> Mime-Version: 1.0 References: <20230621180454.973862-1-yuanchu@google.com> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230621180454.973862-3-yuanchu@google.com> Subject: [RFC PATCH v2 2/6] mm: add working set refresh threshold to rate-limit aggregation From: Yuanchu Xie To: Greg Kroah-Hartman , "Rafael J . Wysocki" , "Michael S . Tsirkin" , David Hildenbrand , Jason Wang , Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Yu Zhao , Kefeng Wang , Kairui Song , Yosry Ahmed , Yuanchu Xie , "T . J . Alumbaugh" Cc: Wei Xu , SeongJae Park , Sudarshan Rajagopalan , kai.huang@intel.com, hch@lst.de, jon@nutanix.com, Aneesh Kumar K V , Matthew Wilcox , Vasily Averin , linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-mm@kvack.org, cgroups@vger.kernel.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769338701020738700?= X-GMAIL-MSGID: =?utf-8?q?1769338701020738700?= Refresh threshold is a rate limiting factor to working set histogram reads. When a working set report is generated, a timestamp is noted, and the same report will be read until it expires beyond the refresh threshold, at which point a new report is generated. Signed-off-by: T.J. Alumbaugh Signed-off-by: Yuanchu Xie --- include/linux/mmzone.h | 1 + include/linux/wsr.h | 3 +++ mm/internal.h | 11 +++++++++ mm/vmscan.c | 39 +++++++++++++++++++++++++++++-- mm/wsr.c | 52 +++++++++++++++++++++++++++++++++++++++--- 5 files changed, 101 insertions(+), 5 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 96f0d8f3584e4..bca828a16a46b 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -362,6 +362,7 @@ enum lruvec_flags { #ifndef __GENERATING_BOUNDS_H +struct node; struct lruvec; struct page_vma_mapped_walk; diff --git a/include/linux/wsr.h b/include/linux/wsr.h index fa46b4d61177d..a86105468c710 100644 --- a/include/linux/wsr.h +++ b/include/linux/wsr.h @@ -26,6 +26,8 @@ struct ws_bin { struct wsr { /* protects bins */ struct mutex bins_lock; + unsigned long timestamp; + unsigned long refresh_threshold; struct ws_bin bins[MAX_NR_BINS]; }; @@ -40,6 +42,7 @@ ssize_t wsr_intervals_ms_parse(char *src, struct ws_bin *bins); /* * wsr->bins needs to be locked + * refreshes wsr based on the refresh threshold */ void wsr_refresh(struct wsr *wsr, struct mem_cgroup *root, struct pglist_data *pgdat); diff --git a/mm/internal.h b/mm/internal.h index 88dba0b11f663..ce4757e7f8277 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -186,6 +186,17 @@ int 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); +int get_swappiness(struct lruvec *lruvec, struct scan_control *sc); +bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, + struct scan_control *sc, bool can_swap, + bool force_scan); + +/* + * in mm/wsr.c + */ +void refresh_wsr(struct wsr *wsr, struct mem_cgroup *root, + struct pglist_data *pgdat, struct scan_control *sc, + unsigned long refresh_threshold); /* * in mm/rmap.c: diff --git a/mm/vmscan.c b/mm/vmscan.c index 150e3cd70c65e..66c5df2a7f65b 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3201,7 +3201,7 @@ static struct lruvec *get_lruvec(struct mem_cgroup *memcg, int nid) return &pgdat->__lruvec; } -static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc) +int get_swappiness(struct lruvec *lruvec, struct scan_control *sc) { struct mem_cgroup *memcg = lruvec_memcg(lruvec); struct pglist_data *pgdat = lruvec_pgdat(lruvec); @@ -4402,7 +4402,7 @@ static void inc_max_seq(struct lruvec *lruvec, bool can_swap, bool force_scan) spin_unlock_irq(&lruvec->lru_lock); } -static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, +bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long max_seq, struct scan_control *sc, bool can_swap, bool force_scan) { bool success; @@ -5900,6 +5900,41 @@ static int __init init_lru_gen(void) }; late_initcall(init_lru_gen); +/****************************************************************************** + * working set reporting + ******************************************************************************/ + +#ifdef CONFIG_WSR +void wsr_refresh(struct wsr *wsr, struct mem_cgroup *root, + struct pglist_data *pgdat) +{ + unsigned int flags; + struct scan_control sc = { + .may_writepage = true, + .may_unmap = true, + .may_swap = true, + .reclaim_idx = MAX_NR_ZONES - 1, + .gfp_mask = GFP_KERNEL, + }; + + lockdep_assert_held(&wsr->bins_lock); + + if (wsr->bins->idle_age != -1) { + unsigned long timestamp = READ_ONCE(wsr->timestamp); + unsigned long threshold = READ_ONCE(wsr->refresh_threshold); + + if (time_is_before_jiffies(timestamp + threshold)) { + set_task_reclaim_state(current, &sc.reclaim_state); + flags = memalloc_noreclaim_save(); + refresh_wsr(wsr, root, pgdat, &sc, threshold); + memalloc_noreclaim_restore(flags); + set_task_reclaim_state(current, NULL); + } + } +} + +#endif /* CONFIG_WSR */ + #else /* !CONFIG_LRU_GEN */ static void lru_gen_age_node(struct pglist_data *pgdat, struct scan_control *sc) diff --git a/mm/wsr.c b/mm/wsr.c index 1e4c0ce69caf7..ee295d164461e 100644 --- a/mm/wsr.c +++ b/mm/wsr.c @@ -125,8 +125,9 @@ static void collect_wsr(struct wsr *wsr, const struct lruvec *lruvec) } } -static void refresh_wsr(struct wsr *wsr, struct mem_cgroup *root, - struct pglist_data *pgdat) +void refresh_wsr(struct wsr *wsr, struct mem_cgroup *root, + struct pglist_data *pgdat, struct scan_control *sc, + unsigned long refresh_threshold) { struct ws_bin *bin; struct mem_cgroup *memcg; @@ -146,6 +147,24 @@ static void refresh_wsr(struct wsr *wsr, struct mem_cgroup *root, do { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); + bool can_swap = get_swappiness(lruvec, sc); + unsigned long max_seq = READ_ONCE((lruvec)->lrugen.max_seq); + unsigned long min_seq[ANON_AND_FILE] = { + READ_ONCE(lruvec->lrugen.min_seq[LRU_GEN_ANON]), + READ_ONCE(lruvec->lrugen.min_seq[LRU_GEN_FILE]), + }; + + mem_cgroup_calculate_protection(root, memcg); + if (!mem_cgroup_below_min(root, memcg) && refresh_threshold && + min_seq[!can_swap] + MAX_NR_GENS - 1 > max_seq) { + int gen = lru_gen_from_seq(max_seq); + unsigned long birth = + READ_ONCE(lruvec->lrugen.timestamps[gen]); + + if (time_is_before_jiffies(birth + refresh_threshold)) + try_to_inc_max_seq(lruvec, max_seq, sc, + can_swap, false); + } collect_wsr(wsr, lruvec); @@ -165,6 +184,32 @@ static struct wsr *kobj_to_wsr(struct kobject *kobj) return lruvec_wsr(mem_cgroup_lruvec(NULL, kobj_to_pgdat(kobj))); } + +static ssize_t refresh_ms_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf) +{ + struct wsr *wsr = kobj_to_wsr(kobj); + unsigned long threshold = READ_ONCE(wsr->refresh_threshold); + + return sysfs_emit(buf, "%u\n", jiffies_to_msecs(threshold)); +} + +static ssize_t refresh_ms_store(struct kobject *kobj, struct kobj_attribute *attr, + const char *buf, size_t len) +{ + unsigned int msecs; + struct wsr *wsr = kobj_to_wsr(kobj); + + if (kstrtouint(buf, 0, &msecs)) + return -EINVAL; + + WRITE_ONCE(wsr->refresh_threshold, msecs_to_jiffies(msecs)); + + return len; +} + +static struct kobj_attribute refresh_ms_attr = __ATTR_RW(refresh_ms); + static ssize_t intervals_ms_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -227,7 +272,7 @@ static ssize_t histogram_show(struct kobject *kobj, struct kobj_attribute *attr, mutex_lock(&wsr->bins_lock); - refresh_wsr(wsr, NULL, kobj_to_pgdat(kobj)); + wsr_refresh(wsr, NULL, kobj_to_pgdat(kobj)); for (bin = wsr->bins; bin->idle_age != -1; bin++) len += sysfs_emit_at(buf, len, "%u anon=%lu file=%lu\n", @@ -245,6 +290,7 @@ static ssize_t histogram_show(struct kobject *kobj, struct kobj_attribute *attr, static struct kobj_attribute histogram_attr = __ATTR_RO(histogram); static struct attribute *wsr_attrs[] = { + &refresh_ms_attr.attr, &intervals_ms_attr.attr, &histogram_attr.attr, NULL