Skip to contentSkip to conversation

nohz: fix idle accounting in NO_HZ kernels

PATCH Superseded Hidetoshi Seto 3 participants 6 messages Mar 24, 2014 – Mar 25, 2014

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover nohz: fix idle accounting in NO_HZ kernels

Hi all,

This patch set (based on v3.14-rc7) will fix an issue that idle/iowait of /proc/stat can go backward. Originally reported by Tetsuo and Fernando at last year, Mar 2013.

Reviews are welcome.

Thanks,
H.Seto

Hidetoshi Seto (2):
nohz: use seqlock to avoid race on idle time stats nohz, procfs: introduce get_cpu_idle/iowait_time_coarse

 fs/proc/stat.c           |   16 ++---
 include/linux/tick.h     |    5 ++
 kernel/time/tick-sched.c |  171 ++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 161 insertions(+), 31 deletions(-)
#1 lore raw

PATCH 1/2 nohz: use seqlock to avoid race on idle time stats

This patch is 1/2 of patch set to fix an issue that idle/iowait of /proc/stat can go backward. Originally reported by Tetsuo and Fernando at last year, Mar 2013.

[BACKGROUNDS]: idle accounting on NO_HZ

If NO_HZ is enabled, cpu stops tick interrupt for itself before go sleep to be idle. It means that time stats of the sleeping cpu will not be updated in tick interrupt. Instead when cpu wakes up, it updates time stats by calculating idle duration time from timestamp at entering idle and current time as exiting idle.

OTOH, it can happen that there are some kind of observer who want to know how long the sleeping cpu have been idle. Imagine that userland runs top command or application who read /proc/stats. Therefore kernel provides get_cpu_{idle,iowait}_time_us() function for user to obtain current idle time stats of such sleeping cpu. This function reads time stats and timestamp at entering idle, and then return current idle time by adding duration calculated from timestamp and current time.

There are 2 problems:

[PROBLEM 1]: there is no exclusive control.

It is easy to understand that there are 2 different cpu - an observing cpu where running a program observing idle cpu's stat and an observed cpu where performing idle. It means race can happen if observed cpu wakes up while it is observed. Observer can accidentally add calculated duration time (say delta) to time stats which is just updated by woken cpu. Soon correct idle time is returned in next turn, so it will result in backward time. Therefore readers must be excluded by writer.

Then time stats are updated by woken cpu so there are only one writer, right? No, unfortunately no. I'm not sure why are they, but in some reason the function get_cpu_{idle,iowait}_time_us() has ability to update sleeping cpu's time stats from observing cpu. From grep of today's kernel tree, this feature is used by cpufreq module. Calling this function with this feature in periodically manner works like emulating tick for sleeping cpu.

In summary there are races between multiple writer and multiple reader but no exclusive control on this idle time stats dataset.

[PROBLEM 2]: broken iowait accounting.

As historical nature, cpu's idle time was accounted as either idle or iowait depending on the presence of tasks blocked by I/O. No one complain about it for a long time. However:

Quoted 4 lines

Still trying to wrap my head around it, but conceptually get_cpu_iowait_time_us() doesn't make any kind of sense. iowait isn't per cpu since effectively tasks that aren't running aren't assigned a cpu (as Oleg already pointed out).

-- Peter Zijlstra

Now some kernel folks realized that accounting iowait as per-cpu does not make sense in SMP world. When we were in traditional UP era, cpu is considered to be waiting I/O if it is idle while nr_iowait > 0. But in these days with SMP systems, tasks easily migrate from a cpu where they issued an I/O to another cpu where they are queued after I/O completion.

Back to NO_HZ mechanism. Totally terrible thing here is that observer need to handle duration "delta" without knowing that nr_iowait of sleeping cpu can be changed easily by migration even if cpu is sleeping. So it can happen that observer temporary assign delta as iowait at 1st place, then blocked tasks are migrated all, and at last in 2nd turn observer assign delta as idle. You will see that delta have moved from iowait to idle.

In summary iowait accounting has fundamental problem and needs to be precisely reworked. It implies that some user interfaces might be replaced completely.

[WHAT THIS PATCH PROPOSED]: fix problem 1 first.

To fix problem 1, this patch adds seqlock for NO_HZ idle accounting to avoid possible races between multiple reader/writer.

And to cope with problem 2, I add comments to note that there is known issues.

Thanks,
H.Seto

References:
First report from Fernando:
[RFC] iowait/idle time accounting hiccups in NOHZ kernels
https://lkml.org/lkml/2013/3/18/962
Steps to reproduce guided by Tetsuo:

    https://lkml.org/lkml/2013/4/1/201

1st patchset from Frederic:
[PATCH 0/4] nohz: Fix racy sleeptime stats
https://lkml.org/lkml/2013/8/8/638
[PATCH RESEND 0/4] nohz: Fix racy sleeptime stats
https://lkml.org/lkml/2013/8/16/274

2nd patchset from Frederic:
[RFC PATCH 0/5] nohz: Fix racy sleeptime stats v2
https://lkml.org/lkml/2013/10/19/86

Cc Frederic Weisbecker <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Arjan van de Ven <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 include/linux/tick.h     |    1 +
 kernel/time/tick-sched.c |   92 +++++++++++++++++++++++++++++++++++----------
 2 files changed, 72 insertions(+), 21 deletions(-)
include/linux/tick.h +1 -0
index b84773c..f6f4ac1 100644
@@ -62,6 +62,7 @@ struct tick_sched {
 	unsigned long			idle_calls;
 	unsigned long			idle_sleeps;
 	int				idle_active;
+	seqlock_t			idle_sleeptime_seq;
 	ktime_t				idle_entrytime;
 	ktime_t				idle_waketime;
 	ktime_t				idle_exittime;
kernel/time/tick-sched.c +71 -21

150 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH 1/2] nohz: use seqlock to avoid race on idle time stats

Hi Hidetoshi,

The patch looks good to me except the comments around the monotonicity of the return value of the idle stats observer. I am unable to relate them to the dependency on nr_iowait_cpu.

I see that when the reader queries for the idle stats and calls get_cpu_idle_time_us(), the nr_iowait_cpu might be 0. When he later queries get_cpu_iowait_time_us(), it may be >0 . Hence we will be accounting for the idle time in both idle time and iowait time. This is definitely a problem. But I do not understand what this has got to do with the monotonicity of the time
returned. This is just for my understanding.

Thanks!

Regards
Preeti U Murthy
On 3/24/14, Hidetoshi Seto <[email protected]> wrote:

<snip>

Quoted 102 lines

+ * Known bug: Return value is not monotonic in case if @last_update_time
+ * is NULL and therefore update is not performed. Because it includes
+ * cputime which is not determined idle or iowait so not accounted yet.
+ *
* This function returns -1 if NOHZ is not enabled.
*/
u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)

kernel/time/tick-sched.c +19 -7
@@ -467,16 +489,28 @@ u64 get_cpu_idle_time_us(int cpu, u64
 *last_update_time)
 
 	now = ktime_get();
 	if (last_update_time) {
-		update_ts_time_stats(cpu, ts, now, last_update_time);
-		idle = ts->idle_sleeptime;
+		update_ts_time_stats(cpu, ts, now, &idle, NULL, 0);
+		*last_update_time = ktime_to_us(now);
 	} else {
-		if (ts->idle_active && !nr_iowait_cpu(cpu)) {
-			ktime_t delta = ktime_sub(now, ts->idle_entrytime);
+		unsigned int seq;
 
-			idle = ktime_add(ts->idle_sleeptime, delta);
-		} else {
+		do {
+			seq = read_seqbegin(&ts->idle_sleeptime_seq);
 			idle = ts->idle_sleeptime;
-		}
+			/*
+			 * FIXME: At this point, delta is determined neither
+			 * idle nor iowait. This function temporarily treat
+			 * this delta depending on the value of nr_iowait
+			 * when this function reaches here. It will result
+			 * in non-monotonic return value. So user of this
+			 * function must not expect monotonicity here.
+			 */
+			if (ts->idle_active && !nr_iowait_cpu(cpu)
+			    && (ktime_compare(now, ts->idle_entrytime) > 0)) {
+				ktime_t delta = ktime_sub(now, ts->idle_entrytime);
+				idle = ktime_add(ts->idle_sleeptime, delta);
+			}
+		} while (read_seqretry(&ts->idle_sleeptime_seq, seq));
 	}
 
kernel/time/tick-sched.c +23 -7
 	return ktime_to_us(idle);
@@ -496,6 +530,10 @@ EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  * This time is measured via accounting rather than sampling,
  * and is as accurate as ktime_get() is.
  *
+ * Known bug: Return value is not monotonic in case if @last_update_time
+ * is NULL and therefore update is not performed. Because it includes
+ * cputime which is not determined idle or iowait so not accounted yet.
+ *
  * This function returns -1 if NOHZ is not enabled.
  */
 u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
@@ -508,16 +546,28 @@ u64 get_cpu_iowait_time_us(int cpu, u64
 *last_update_time)
 
 	now = ktime_get();
 	if (last_update_time) {
-		update_ts_time_stats(cpu, ts, now, last_update_time);
-		iowait = ts->iowait_sleeptime;
+		update_ts_time_stats(cpu, ts, now, NULL, &iowait, 0);
+		*last_update_time = ktime_to_us(now);
 	} else {
-		if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
-			ktime_t delta = ktime_sub(now, ts->idle_entrytime);
+		unsigned int seq;
 
-			iowait = ktime_add(ts->iowait_sleeptime, delta);
-		} else {
+		do {
+			seq = read_seqbegin(&ts->idle_sleeptime_seq);
 			iowait = ts->iowait_sleeptime;
-		}
+			/*
+			 * FIXME: At this point, delta is determined neither
+			 * idle nor iowait. This function temporarily treat
+			 * this delta depending on the value of nr_iowait
+			 * when this function reaches here. It will result
+			 * in non-monotonic return value. So user of this
+			 * function must not expect monotonicity here.
+			 */
+			if (ts->idle_active && nr_iowait_cpu(cpu) > 0
+			    && (ktime_compare(now, ts->idle_entrytime) > 0)) {
+				ktime_t delta = ktime_sub(now, ts->idle_entrytime);
+				iowait = ktime_add(ts->iowait_sleeptime, delta);
+			}
+		} while (read_seqretry(&ts->idle_sleeptime_seq, seq));
 	}
 

return ktime_to_us(iowait);
--
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw

Re: [PATCH 1/2] nohz: use seqlock to avoid race on idle time stats

Quoted 6 lines · Preeti Murthy

Hi Hidetoshi,

The patch looks good to me except the comments around the monotonicity of the return value of the idle stats observer. I am unable to relate them to the dependency on nr_iowait_cpu.

Quoted 7 lines

I see that when the reader queries for the idle stats and calls get_cpu_idle_time_us(), the nr_iowait_cpu might be 0. When he later queries get_cpu_iowait_time_us(), it may be >0 . Hence we will be accounting for the idle time in both idle time and iowait time. This is definitely a problem. But I do not understand what this has got to do with the monotonicity of the time
returned. This is just for my understanding.

Thank you for your comment!

Ah yes, I think I could write better comments around here to clarify the monotonicity problem. (It will be happy if someone can give me such better sentence for here :-D)

One important point is that readers do not update idle stats when they use these function. i.e.

given:
idle stats: idle=1000, iowait=100
stamp at idle entry: entry=50
nr tasks waiting io: nr_iowait=1

1st reader:
query @ now=60

     idle=1000
     iowait=110 (=100+(60-50))

(here nr_iowait changed to 0)

2nd reader:
query @ now=70

     idle=1020 (=1000+(70-50))
     iowait=100

So you will see iowait is decreased from 110 to 100.

I hope this short story helps you.

Thanks,
H.Seto

#4 lore raw

Re: [PATCH 1/2] nohz: use seqlock to avoid race on idle time stats

Quoted 34 lines · Hidetoshi Seto
Quoted 6 lines · Preeti Murthy

Hi Hidetoshi,

The patch looks good to me except the comments around the monotonicity of the return value of the idle stats observer. I am unable to relate them to the dependency on nr_iowait_cpu.

Quoted 7 lines

I see that when the reader queries for the idle stats and calls get_cpu_idle_time_us(), the nr_iowait_cpu might be 0. When he later queries get_cpu_iowait_time_us(), it may be >0 . Hence we will be accounting for the idle time in both idle time and iowait time. This is definitely a problem. But I do not understand what this has got to do with the monotonicity of the time
returned. This is just for my understanding.

Thank you for your comment!

Ah yes, I think I could write better comments around here to clarify the monotonicity problem. (It will be happy if someone can give me such better sentence for here :-D)

One important point is that readers do not update idle stats when they use these function. i.e.

given:
idle stats: idle=1000, iowait=100
stamp at idle entry: entry=50
nr tasks waiting io: nr_iowait=1

1st reader:

   query @ now=60
     idle=1000
     iowait=110 (=100+(60-50))
Quoted 8 lines

(here nr_iowait changed to 0)

2nd reader:

   query @ now=70
     idle=1020 (=1000+(70-50))
     iowait=100

So you will see iowait is decreased from 110 to 100.

Ah ok! I get it. Please do add my Reviewed-by to it.

Quoted 2 lines

I hope this short story helps you.

Thanks!

Regards
Preeti U Murthy

Quoted 4 lines

Thanks,
H.Seto

#5 lore raw

PATCH 2/2 nohz, procfs: introduce get_cpu_idle/iowait_time_coarse

In reply to Hidetoshi Seto

This patch is 2/2 of patch set to fix an issue that idle/iowait of /proc/stat can go backward. Originally reported by Tetsuo and Fernando at last year, Mar 2013.

Now it is clear that get_cpu_{idle,iowait}_time_us() is not monotonic. Using this for /proc/stats will cause troubles in innocent userland who believe these counters are definitely monotonic.

Given that:

- If observer (= reader of /proc/stats) want to avoid backwarding, it must update time stats for next observer. It means observer determine delta to idle/iowait and account it for sleeping cpu.

- Usually the number of observers will not so many (i.e. running top on few console or hiring few monitor program will be enough for average system admin), but we must predict the worst case. In short, make effort to reduce lock contention.

- The resolution required in /proc/stats is tick-level, not us.

This patch introduces new function get_cpu_idle/iowait_time_coarse() that guarantees monotonic return value but resolution is low.

Tricks are here:

- At first this function obtain latest time stats and calculate "delta" which indicates duration from time when the time stats is updated last time to current time.

- If delta is less than tick length, use obtained time stats as if it was sampled in tick interrupt recently happened.

- If delta is greater than tick, perform update of time stats as if it emulates tick for sleeping observed cpu.

- As the result the rate of updating time stats by observer is limited to once per tick. In other words, in case if there is observer who are monitoring sleeping cpu, we leave tick emulation job during idle to the observer.

I confirmed this patch fix the monotonicity of /proc/stats, by running reproducer and stressor for a day. The rate of reproduce is different for different system, but in my case, running "git gc" on kernel source repository aside of checker works fine.

Thanks,
H.Seto

Cc Frederic Weisbecker <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Arjan van de Ven <[email protected]>
Cc Oleg Nesterov <[email protected]>
---
 fs/proc/stat.c           |   16 +++------
 include/linux/tick.h     |    4 ++
 kernel/time/tick-sched.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 89 insertions(+), 10 deletions(-)
fs/proc/stat.c +6 -10
index 6f599c6..3dbe282 100644
@@ -45,32 +45,28 @@ static cputime64_t get_iowait_time(int cpu)
 
 static u64 get_idle_time(int cpu)
 {
-	u64 idle, idle_time = -1ULL;
+	u64 idle = -1ULL;
 
 	if (cpu_online(cpu))
-		idle_time = get_cpu_idle_time_us(cpu, NULL);
+		idle = get_cpu_idle_time_coarse(cpu);
 
-	if (idle_time == -1ULL)
+	if (idle == -1ULL)
 		/* !NO_HZ or cpu offline so we can rely on cpustat.idle */
 		idle = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
-	else
-		idle = usecs_to_cputime64(idle_time);
 
 	return idle;
 }
 
 static u64 get_iowait_time(int cpu)
 {
-	u64 iowait, iowait_time = -1ULL;
+	u64 iowait = -1ULL;
 
 	if (cpu_online(cpu))
-		iowait_time = get_cpu_iowait_time_us(cpu, NULL);
+		iowait = get_cpu_iowait_time_coarse(cpu);
 
-	if (iowait_time == -1ULL)
+	if (iowait == -1ULL)
 		/* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
 		iowait = kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
-	else
-		iowait = usecs_to_cputime64(iowait_time);
 
 	return iowait;
 }
include/linux/tick.h +4 -0
index f6f4ac1..3b4674d 100644
@@ -140,6 +140,8 @@ extern void tick_nohz_irq_exit(void);
 extern ktime_t tick_nohz_get_sleep_length(void);
 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
+extern u64 get_cpu_idle_time_coarse(int cpu);
+extern u64 get_cpu_iowait_time_coarse(int cpu);
 
 # else /* !CONFIG_NO_HZ_COMMON */
 static inline int tick_nohz_tick_stopped(void)
@@ -158,6 +160,8 @@ static inline ktime_t tick_nohz_get_sleep_length(void)
 }
 static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
 static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
+static inline u64 get_cpu_idle_time_coarse(int cpu) { return -1; }
+static inline u64 get_cpu_iowait_time_coarse(int cpu) { return -1; }
 # endif /* !CONFIG_NO_HZ_COMMON */
 
 #ifdef CONFIG_NO_HZ_FULL
kernel/time/tick-sched.c +79 -0

85 lines not on this page · raw message

Signature · 1 line
1.7.1