All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hillf Danton <hdanton@sina.com>
To: Shakeel Butt <shakeelb@google.com>
Cc: Michal Koutny <mkoutny@suse.com>,
	Ivan Babrou <ivan@cloudflare.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Daniel Dao <dqminh@cloudflare.com>
Subject: Re: [PATCH] memcg: sync flush only if periodic flush is delayed
Date: Sun, 13 Mar 2022 10:50:50 +0800	[thread overview]
Message-ID: <20220313025050.1463-1-hdanton@sina.com> (raw)
In-Reply-To: <20220304184040.1304781-1-shakeelb@google.com>

On Fri,  4 Mar 2022 18:40:40 +0000 Shakeel Butt wrote:
> Daniel Dao has reported [1] a regression on workloads that may trigger
> a lot of refaults (anon and file). The underlying issue is that flushing
> rstat is expensive. Although rstat flush are batched with (nr_cpus *
> MEMCG_BATCH) stat updates, it seems like there are workloads which
> genuinely do stat updates larger than batch value within short amount of
> time. Since the rstat flush can happen in the performance critical
> codepaths like page faults, such workload can suffer greatly.
> 
> This patch fixes this regression by making the rstat flushing
> conditional in the performance critical codepaths. More specifically,
> the kernel relies on the async periodic rstat flusher to flush the stats
> and only if the periodic flusher is delayed by more than twice the
> amount of its normal time window then the kernel allows rstat flushing
> from the performance critical codepaths.
> 
> Now the question: what are the side-effects of this change? The worst
> that can happen is the refault codepath will see 4sec old lruvec stats
> and may cause false (or missed) activations of the refaulted page which
> may under-or-overestimate the workingset size. Though that is not very
> concerning as the kernel can already miss or do false activations.
> 
> There are two more codepaths whose flushing behavior is not changed by
> this patch and we may need to come to them in future. One is the
> writeback stats used by dirty throttling and second is the deactivation
> heuristic in the reclaim. For now keeping an eye on them and if there is
> report of regression due to these codepaths, we will reevaluate then.
> 
> Link: https://lore.kernel.org/all/CA+wXwBSyO87ZX5PVwdHm-=dBjZYECGmfnydUicUyrQqndgX2MQ@mail.gmail.com [1]
> Fixes: 1f828223b799 ("memcg: flush lruvec stats in the refault")
> Signed-off-by: Shakeel Butt <shakeelb@google.com>
> Reported-by: Daniel Dao <dqminh@cloudflare.com>
> Cc: <stable@vger.kernel.org>
> ---
>  include/linux/memcontrol.h |  5 +++++
>  mm/memcontrol.c            | 12 +++++++++++-
>  mm/workingset.c            |  2 +-
>  3 files changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
> index a68dce3873fc..89b14729d59f 100644
> --- a/include/linux/memcontrol.h
> +++ b/include/linux/memcontrol.h
> @@ -1012,6 +1012,7 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
>  }
>  
>  void mem_cgroup_flush_stats(void);
> +void mem_cgroup_flush_stats_delayed(void);
>  
>  void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
>  			      int val);
> @@ -1455,6 +1456,10 @@ static inline void mem_cgroup_flush_stats(void)
>  {
>  }
>  
> +static inline void mem_cgroup_flush_stats_delayed(void)
> +{
> +}
> +
>  static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
>  					    enum node_stat_item idx, int val)
>  {
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index f79bb3f25ce4..edfb337e6948 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -587,6 +587,9 @@ static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);
>  static DEFINE_SPINLOCK(stats_flush_lock);
>  static DEFINE_PER_CPU(unsigned int, stats_updates);
>  static atomic_t stats_flush_threshold = ATOMIC_INIT(0);
> +static u64 flush_next_time;
> +
> +#define FLUSH_TIME (2UL*HZ)
>  
>  /*
>   * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can
> @@ -637,6 +640,7 @@ static void __mem_cgroup_flush_stats(void)
>  	if (!spin_trylock_irqsave(&stats_flush_lock, flag))
>  		return;
>  
> +	flush_next_time = jiffies_64 + 2*FLUSH_TIME;
>  	cgroup_rstat_flush_irqsafe(root_mem_cgroup->css.cgroup);
>  	atomic_set(&stats_flush_threshold, 0);
>  	spin_unlock_irqrestore(&stats_flush_lock, flag);
> @@ -648,10 +652,16 @@ void mem_cgroup_flush_stats(void)
>  		__mem_cgroup_flush_stats();
>  }
>  
> +void mem_cgroup_flush_stats_delayed(void)
> +{
> +	if (rstat_flush_time && time_after64(jiffies_64, flush_next_time))
> +		mem_cgroup_flush_stats();
> +}
> +
>  static void flush_memcg_stats_dwork(struct work_struct *w)
>  {
>  	__mem_cgroup_flush_stats();
> -	queue_delayed_work(system_unbound_wq, &stats_flush_dwork, 2UL*HZ);
> +	queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
>  }
>  

Given flush_next_time is updated only by the periodic flusher, flushing
stats is effectively disabled for page faults as sync flush can hardly
run before the periodic one.

If that is the case, turn the sync flush into a signal that requests
flushing stats sooner.

Only for thoughts now.

Hillf

--- x/mm/memcontrol.c
+++ y/mm/memcontrol.c
@@ -587,9 +587,9 @@ static DECLARE_DEFERRABLE_WORK(stats_flu
 static DEFINE_SPINLOCK(stats_flush_lock);
 static DEFINE_PER_CPU(unsigned int, stats_updates);
 static atomic_t stats_flush_threshold = ATOMIC_INIT(0);
-static u64 flush_next_time;
 
-#define FLUSH_TIME (2UL*HZ)
+#define FLUSH_STATS_TICK (2UL*HZ)
+static unsigned long flush_stats_tick = FLUSH_STATS_TICK;
 
 /*
  * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can
@@ -640,7 +640,6 @@ static void __mem_cgroup_flush_stats(voi
 	if (!spin_trylock_irqsave(&stats_flush_lock, flag))
 		return;
 
-	flush_next_time = jiffies_64 + 2*FLUSH_TIME;
 	cgroup_rstat_flush_irqsafe(root_mem_cgroup->css.cgroup);
 	atomic_set(&stats_flush_threshold, 0);
 	spin_unlock_irqrestore(&stats_flush_lock, flag);
@@ -654,14 +653,15 @@ void mem_cgroup_flush_stats(void)
 
 void mem_cgroup_flush_stats_delayed(void)
 {
-	if (time_after64(jiffies_64, flush_next_time))
-		mem_cgroup_flush_stats();
+	/* s/mem_cgroup_flush_stats_delayed/mem_cgroup_signal_flush_stats/ */
+	flush_stats_tick /= 2;
 }
 
 static void flush_memcg_stats_dwork(struct work_struct *w)
 {
 	__mem_cgroup_flush_stats();
-	queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
+	queue_delayed_work(system_unbound_wq, &stats_flush_dwork, flush_stats_tick);
+	flush_stats_tick = FLUSH_STATS_TICK;
 }
 
 /**


      parent reply	other threads:[~2022-03-13  2:51 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 18:40 [PATCH] memcg: sync flush only if periodic flush is delayed Shakeel Butt
2022-03-04 18:40 ` Shakeel Butt
2022-03-04 18:53 ` Ivan Babrou
2022-03-07  2:44 ` Andrew Morton
2022-03-07  3:06   ` Shakeel Butt
2022-03-11 16:00 ` Michal Koutný
2022-03-11 16:00   ` Michal Koutný
2022-03-12 19:07   ` Shakeel Butt
2022-03-12 19:07     ` Shakeel Butt
2022-03-14 12:57     ` Michal Koutný
2022-03-14 12:57       ` Michal Koutný
2022-03-14 12:57     ` Michal Koutný
2022-03-16 16:26       ` Shakeel Butt
2022-03-16 16:26         ` Shakeel Butt
2022-03-13  2:50 ` Hillf Danton [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220313025050.1463-1-hdanton@sina.com \
    --to=hdanton@sina.com \
    --cc=akpm@linux-foundation.org \
    --cc=dqminh@cloudflare.com \
    --cc=ivan@cloudflare.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mkoutny@suse.com \
    --cc=shakeelb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.