Skip to contentSkip to conversation

mm: vmscan: fix the page state calculation in too_many_isolated

PATCH In review Vinayak Menon 5 participants 30 messages Jan 14, 2015 – Feb 12, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: vmscan: fix the page state calculation in too_many_isolated

It is observed that sometimes multiple tasks get blocked for long in the congestion_wait loop below, in shrink_inactive_list. This is because of vm_stat values not being synced.

(__schedule) from [<c0a03328>]
(schedule_timeout) from [<c0a04940>]
(io_schedule_timeout) from [<c01d585c>]
(congestion_wait) from [<c01cc9d8>]
(shrink_inactive_list) from [<c01cd034>]
(shrink_zone) from [<c01cdd08>]
(try_to_free_pages) from [<c01c442c>]
(__alloc_pages_nodemask) from [<c01f1884>]
(new_slab) from [<c09fcf60>]
(__slab_alloc) from [<c01f1a6c>]

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

This patch uses zone_page_state_snapshot instead, but restricts its usage to avoid performance penalty.

---
 mm/vmscan.c | 56 +++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 19 deletions(-)
mm/vmscan.c +37 -19
index 5e8772b..266551f 100644
@@ -1392,6 +1392,32 @@ int isolate_lru_page(struct page *page)
 	return ret;
 }
 
+static int __too_many_isolated(struct zone *zone, int file,
+	struct scan_control *sc, int safe)
+{
+	unsigned long inactive, isolated;
+
+	if (safe) {
+		inactive = zone_page_state_snapshot(zone,
+				NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state_snapshot(zone,
+				NR_ISOLATED_ANON + file);
+	} else {
+		inactive = zone_page_state(zone, NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state(zone, NR_ISOLATED_ANON + file);
+	}
+
+	/*
+	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
+	 * won't get blocked by normal direct-reclaimers, forming a circular
+	 * deadlock.
+	 */
+	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
+		inactive >>= 3;
+
+	return isolated > inactive;
+}
+
 /*
  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
  * then get resheduled. When there are massive number of tasks doing page
@@ -1400,33 +1426,22 @@ int isolate_lru_page(struct page *page)
  * unnecessary swapping, thrashing and OOM.
  */
 static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc)
+		struct scan_control *sc, int safe)
 {
-	unsigned long inactive, isolated;
-
 	if (current_is_kswapd())
 		return 0;
 
 	if (!global_reclaim(sc))
 		return 0;
 
-	if (file) {
-		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
-		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
-	} else {
-		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
-		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
+	if (unlikely(__too_many_isolated(zone, file, sc, 0))) {
+		if (safe)
+			return __too_many_isolated(zone, file, sc, safe);
+		else
+			return 1;
 	}
 
-	/*
-	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
-	 * won't get blocked by normal direct-reclaimers, forming a circular
-	 * deadlock.
-	 */
-	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
-		inactive >>= 3;
-
-	return isolated > inactive;
+	return 0;
 }
 
 static noinline_for_stack void
@@ -1516,15 +1531,18 @@ shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
 	unsigned long nr_immediate = 0;
 	isolate_mode_t isolate_mode = 0;
 	int file = is_file_lru(lru);
+	int safe = 0;
 	struct zone *zone = lruvec_zone(lruvec);
 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 
-	while (unlikely(too_many_isolated(zone, file, sc))) {
+	while (unlikely(too_many_isolated(zone, file, sc, safe))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);
 
 		/* We are about to die and free our memory. Return now. */
 		if (fatal_signal_pending(current))
 			return SWAP_CLUSTER_MAX;
+
+		safe = 1;
 	}
 
 	lru_add_drain();
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation
#1 lore raw

On Wed 14-01-15 17:06:59, Vinayak Menon wrote:
[...]

Quoted 10 lines

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

Not syncing for such a long time doesn't sound right. I am not familiar with the vmstat syncing but sysctl_stat_interval is HZ so it should happen much more often that every 4 seconds.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw
Quoted 13 lines · Michal Hocko

On Wed 14-01-15 17:06:59, Vinayak Menon wrote:
[...]

Quoted 10 lines

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

Quoted 4 lines

Not syncing for such a long time doesn't sound right. I am not familiar with the vmstat syncing but sysctl_stat_interval is HZ so it should happen much more often that every 4 seconds.

Though the interval is HZ, since the vmstat_work is declared as a deferrable work, IIUC the timer trigger can be deferred to the
next non-defferable timer expiry on the CPU which is in idle. This results in the vmstat syncing on an idle CPU delayed by seconds. May be in most cases this behavior is fine, except in cases like this. Even in usual cases were the timer triggers in 1-2 secs, is it fine to let the tasks in reclaim path wait that long unnecessarily when there isn't any real congestion?

Signature · 8 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw
Quoted 19 lines · Vinayak Menon
Quoted 13 lines · Michal Hocko

On Wed 14-01-15 17:06:59, Vinayak Menon wrote:
[...]

Quoted 10 lines

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

Quoted 4 lines

Not syncing for such a long time doesn't sound right. I am not familiar with the vmstat syncing but sysctl_stat_interval is HZ so it should happen much more often that every 4 seconds.

Quoted 5 lines

Though the interval is HZ, since the vmstat_work is declared as a deferrable work, IIUC the timer trigger can be deferred to the next non-defferable timer expiry on the CPU which is in idle. This results in the vmstat syncing on an idle CPU delayed by seconds. May be in most cases this behavior is fine, except in cases like this.

I am not sure I understand the above because CPU being idle doesn't seem important AFAICS. Anyway I have checked the current code which has changed quite recently by 7cc36bbddde5 (vmstat: on-demand vmstat workers V8). Let's CC Christoph (the thread starts here:
http://thread.gmane.org/gmane.linux.kernel.mm/127229).

__round_jiffies_relative can easily make timeout 2HZ from 1HZ. Now we have vmstat_shepherd which waits to be queued and then wait to run. When it runs finally it only queues per-cpu vmstat_work which can also end up being 2HZ for some CPUs. So we can indeed have 4 seconds spent just for queuing. Not even mentioning work item latencies. Especially when workers are overloaded e.g. by fs work items and no additional workers cannot be created e.g. due to memory pressure so they are processed only by the workqueue rescuer. And latencies would grow a lot.

We have seen an issue where rescuer had to process thousands of work items because all workers where blocked on memory allocation - see
http://thread.gmane.org/gmane.linux.kernel/1816452 - which is mainline already 008847f66c38 (workqueue: allow rescuer thread to do more work.) the patch reduces the latency considerably but it doesn't remove it completely.

If the time between two syncs is large then the per-cpu drift might be really large as well. Isn't this going to hurt other places where we rely on stats as well?

In this particular case the reclaimers are throttled because they see too many isolated pages which was just a result of the per-cpu drift and small LRU list. The system seems to be under serious memory pressure already because there is basically no file cache. If the reclaimers wouldn't be throttled they would fail the reclaim probably and trigger OOM killer which might help to resolve the situation. Reclaimers are throttled instead.

Why cannot we simply update the global counters from vmstat_shepherd directly? Sure we would access remote CPU counters but that wouldn't happen often. That still wouldn't be enough because there is the workqueue latency. So why cannot we do that whole shepherd thing from a kernel thread instead? If the NUMA zone->pageset->pcp thing has to be done locally then it could have per-node kthread rather than being part of the unrelated vmstat code.
I might be missing obvious things because I still haven't digested the code completely but this whole thing seems overly complicated and I do not see a good reason for that. If the primary motivation was cpu isolation then kthreads would sound like a better idea because you can play with the explicit affinity from the userspace.

Quoted 3 lines

Even in usual cases were the timer triggers in 1-2 secs, is it fine to let the tasks in reclaim path wait that long unnecessarily when there isn't any real congestion?

But how do we find that out?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

On Fri 16-01-15 16:49:22, Michal Hocko wrote:
[...]

Quoted 2 lines

Why cannot we simply update the global counters from vmstat_shepherd directly?

OK, I should have checked the updating paths... This would be racy, so update from remote is not an option without additional trickery (like retries etc.) :/

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw
Quoted 8 lines · Michal Hocko

On Fri 16-01-15 16:49:22, Michal Hocko wrote:
[...]

Quoted 2 lines

Why cannot we simply update the global counters from vmstat_shepherd directly?

OK, I should have checked the updating paths... This would be racy, so update from remote is not an option without additional trickery (like retries etc.) :/

You can do that if you have a way to ensure that the other cpu does not access the counter. F.e. if the other cpu is staying in user space all the time or it is guaranteed to be idle.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
In reply to Michal Hocko
Quoted 26 lines · Michal Hocko
Quoted 19 lines · Vinayak Menon
Quoted 13 lines · Michal Hocko

On Wed 14-01-15 17:06:59, Vinayak Menon wrote:
[...]

Quoted 10 lines

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

Quoted 4 lines

Not syncing for such a long time doesn't sound right. I am not familiar with the vmstat syncing but sysctl_stat_interval is HZ so it should happen much more often that every 4 seconds.

Quoted 5 lines

Though the interval is HZ, since the vmstat_work is declared as a deferrable work, IIUC the timer trigger can be deferred to the next non-defferable timer expiry on the CPU which is in idle. This results in the vmstat syncing on an idle CPU delayed by seconds. May be in most cases this behavior is fine, except in cases like this.

Quoted 6 lines

I am not sure I understand the above because CPU being idle doesn't seem important AFAICS. Anyway I have checked the current code which has changed quite recently by 7cc36bbddde5 (vmstat: on-demand vmstat workers V8). Let's CC Christoph (the thread starts here:
http://thread.gmane.org/gmane.linux.kernel.mm/127229).

I will try to explain the exact observations. All the cases which I had encountered, had similar symptoms. In one of the cases, it was CPU3 alone which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the list. Since deferrable timers can't wake up the CPU, the vmstat sync for this CPU was deferred for a long time i.e. till the expiry of next non-deferrable timer. The issue was caught because, one of the tasks which was in reclaim path and in the congestion_wait loop had an associated watchdog, which resulted in a panic after 4secs. So 4 secs is actually the watchdog expiry, and the time we can get blocked in the congestion loop can be even more.

Signature · 8 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw
Quoted 4 lines · Vinayak Menon

which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the

We can remove the deferrrable flag now since the vmstat threads are only activated as necessary with the recent changes. Looks like this could fix your issue?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 11 lines · Christoph Lameter
Quoted 4 lines · Vinayak Menon

which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the

We can remove the deferrrable flag now since the vmstat threads are only activated as necessary with the recent changes. Looks like this could fix your issue?

Yes, this should fix my issue.
But I think we may need the fix in too_many_isolated, since there can still be a delay of few seconds (HZ by default and even more because of reasons pointed out by Michal) which will result in reclaimers unnecessarily entering congestion_wait. No ?

Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation
#9 lore raw
Quoted 8 lines · Vinayak Menon
Quoted 7 lines · Christoph Lameter
Quoted 4 lines · Vinayak Menon

which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the

Quoted 6 lines
Quoted 4 lines

We can remove the deferrrable flag now since the vmstat threads are only activated as necessary with the recent changes. Looks like this could fix your issue?

Yes, this should fix my issue.

Does it? Because I would prefer not getting into un-synced state much more than playing around one specific place which shows the problems right now.

Quoted 4 lines

But I think we may need the fix in too_many_isolated, since there can still be a delay of few seconds (HZ by default and even more because of reasons pointed out by Michal) which will result in reclaimers unnecessarily entering congestion_wait. No ?

I think we can solve this as well. We can stick vmstat_shepherd into a kernel thread with a loop with the configured timeout and then create a mask of CPUs which need the update and run vmstat_update from IPI context (smp_call_function_many).
We would have to drop cond_resched from refresh_cpu_vm_stats of course. The nr_zones x NR_VM_ZONE_STAT_ITEMS in the IPI context shouldn't be excessive but I haven't measured that so I might be easily wrong.

Anyway, that should work more reliably than the current scheme and should help to reduce pointless wakeups which the original patchset was addressing. Or am I missing something?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw
Quoted 34 lines · Michal Hocko
Quoted 8 lines · Vinayak Menon
Quoted 7 lines · Christoph Lameter
Quoted 4 lines · Vinayak Menon

which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the

Quoted 6 lines
Quoted 4 lines

We can remove the deferrrable flag now since the vmstat threads are only activated as necessary with the recent changes. Looks like this could fix your issue?

Yes, this should fix my issue.

Does it? Because I would prefer not getting into un-synced state much more than playing around one specific place which shows the problems right now.

Quoted 4 lines

But I think we may need the fix in too_many_isolated, since there can still be a delay of few seconds (HZ by default and even more because of reasons pointed out by Michal) which will result in reclaimers unnecessarily entering congestion_wait. No ?

I think we can solve this as well. We can stick vmstat_shepherd into a kernel thread with a loop with the configured timeout and then create a mask of CPUs which need the update and run vmstat_update from IPI context (smp_call_function_many).
We would have to drop cond_resched from refresh_cpu_vm_stats of course. The nr_zones x NR_VM_ZONE_STAT_ITEMS in the IPI context shouldn't be excessive but I haven't measured that so I might be easily wrong.

Quoted 3 lines

Anyway, that should work more reliably than the current scheme and should help to reduce pointless wakeups which the original patchset was addressing. Or am I missing something?

Maybe to further reduce wakeups, a CPU could check and update its counters before going idle? (unless that already happens)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw
In reply to Michal Hocko
Quoted 4 lines · Michal Hocko

I think we can solve this as well. We can stick vmstat_shepherd into a kernel thread with a loop with the configured timeout and then create a mask of CPUs which need the update and run vmstat_update from IPI context (smp_call_function_many).

Please do not run the vmstat_updates concurrently. They update shared cachelines and therefore can cause bouncing cachelines if run concurrently on multiple cpus.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#12 lore raw
Quoted 10 lines · Christoph Lameter
Quoted 4 lines · Michal Hocko

I think we can solve this as well. We can stick vmstat_shepherd into a kernel thread with a loop with the configured timeout and then create a mask of CPUs which need the update and run vmstat_update from IPI context (smp_call_function_many).

Please do not run the vmstat_updates concurrently. They update shared cachelines and therefore can cause bouncing cachelines if run concurrently on multiple cpus.

Would you preffer to call smp_call_function_single on each CPU which needs an update? That would make vmstat_shepherd slower but that is not a big deal, is it?

Anyway I am wondering whether the cache line bouncing between vmstat_update instances is a big deal in the real life. Updating shared counters whould bounce with many CPUs but this is an operation which is not done often. Also all the CPUs would have update the same counters all the time and I am not sure this happens that often. Do you have a load where this would be measurable?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#13 lore raw
Quoted 7 lines · Michal Hocko
Quoted 3 lines

Please do not run the vmstat_updates concurrently. They update shared cachelines and therefore can cause bouncing cachelines if run concurrently on multiple cpus.

Would you preffer to call smp_call_function_single on each CPU which needs an update? That would make vmstat_shepherd slower but that is not a big deal, is it?

Run it from the timer interrupt as usual from a work request? Those are staggered.

Quoted 6 lines

Anyway I am wondering whether the cache line bouncing between vmstat_update instances is a big deal in the real life. Updating shared counters whould bounce with many CPUs but this is an operation which is not done often. Also all the CPUs would have update the same counters all the time and I am not sure this happens that often. Do you have a load where this would be measurable?

Concurrent page faults update lots of counters concurrently. But will those trigger the smp_call_function?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw
Quoted 6 lines · Christoph Lameter
Quoted 4 lines · Michal Hocko
Quoted 3 lines

Please do not run the vmstat_updates concurrently. They update shared cachelines and therefore can cause bouncing cachelines if run concurrently on multiple cpus.

Quoted 6 lines
Quoted 3 lines

Would you preffer to call smp_call_function_single on each CPU which needs an update? That would make vmstat_shepherd slower but that is not a big deal, is it?

Run it from the timer interrupt as usual from a work request? Those are staggered.

I am not following. The idea was to run vmstat_shepherd in a kernel thread and waking up as per defined timeout and then check need_update for each CPU and call smp_call_function_single to refresh the timer rather than building a mask and then calling sm_call_function_many to reduce paralel contention on the shared counters.

Quoted 8 lines
Quoted 6 lines

Anyway I am wondering whether the cache line bouncing between vmstat_update instances is a big deal in the real life. Updating shared counters whould bounce with many CPUs but this is an operation which is not done often. Also all the CPUs would have update the same counters all the time and I am not sure this happens that often. Do you have a load where this would be measurable?

Concurrent page faults update lots of counters concurrently.

True

Quoted 1 line

But will those trigger the smp_call_function?

The smp_call_function was meant to be called only from the vmstat_shepherd context which does happen "rarely". Or am I missing your point here?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#15 lore raw
Quoted 5 lines · Michal Hocko

I am not following. The idea was to run vmstat_shepherd in a kernel thread and waking up as per defined timeout and then check need_update for each CPU and call smp_call_function_single to refresh the timer rather than building a mask and then calling sm_call_function_many to reduce paralel contention on the shared counters.

Thats ok.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw
Quoted 9 lines · Christoph Lameter
Quoted 5 lines · Michal Hocko

I am not following. The idea was to run vmstat_shepherd in a kernel thread and waking up as per defined timeout and then check need_update for each CPU and call smp_call_function_single to refresh the timer rather than building a mask and then calling sm_call_function_many to reduce paralel contention on the shared counters.

Thats ok.

OK, I will put that on my todo list and try to find some time to implement it.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#17 lore raw
In reply to Christoph Lameter
Quoted 10 lines · Christoph Lameter
Quoted 4 lines · Vinayak Menon

which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the

We can remove the deferrrable flag now since the vmstat threads are only activated as necessary with the recent changes. Looks like this could fix your issue?

OK, I have checked the history and the deferrable behavior has been introduced by 39bf6270f524 (VM statistics: Make timer deferrable) which hasn't offered any numbers which would justify the change. So I think it would be a good idea to revert this one as it can clearly cause issues.

Could you retest with this change? It still wouldn't help with the highly overloaded workqueues but that sounds like a bigger change and this one sounds like quite safe to me so it is a good start.
---

#18 lore raw
Quoted 4 lines · Michal Hocko
Quoted 1 line

From 12d00a8066e336d3e1311600b50fa9b588798448 Mon Sep 17 00:00:00 2001

From: Michal Hocko <[email protected]>
Date: Mon, 26 Jan 2015 18:07:51 +0100
Subject: [PATCH] vmstat: Do not use deferrable delayed work for vmstat_update

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#19 lore raw
In reply to Michal Hocko
Quoted 15 lines · On Mon, 26 Jan 2015 18:28:32 +0100 Michal Hocko <[email protected]> wrote:

Subject: [PATCH] vmstat: Do not use deferrable delayed work for vmstat_update

Vinayak Menon has reported that excessive number of tasks was throttled in the direct reclaim inside too_many_isolated because NR_ISOLATED_FILE was relatively high compared to NR_INACTIVE_FILE. However it turned out that the real number of NR_ISOLATED_FILE was 0 and the per-cpu vm_stat_diff wasn't transfered into the global counter.

vmstat_work which is responsible for the sync is defined as deferrable delayed work which means that the defined timeout doesn't wake up an idle CPU. A CPU might stay in an idle state for a long time and general effort is to keep such a CPU in this state as long as possible which might lead to all sorts of troubles for vmstat consumers as can be seen with the excessive direct reclaim throttling.

Quoted 5 lines

This patch basically reverts 39bf6270f524 (VM statistics: Make timer deferrable) but it shouldn't cause any problems for idle CPUs because only CPUs with an active per-cpu drift are woken up since 7cc36bbddde5 (vmstat: on-demand vmstat workers v8) and CPUs which are idle for a longer time shouldn't have per-cpu drift.

So do we drop mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated.patch?

From: Vinayak Menon <[email protected]>
Subject: mm: vmscan: fix the page state calculation in too_many_isolated

It is observed that sometimes multiple tasks get blocked for long in the congestion_wait loop below, in shrink_inactive_list. This is because of vm_stat values not being synced.

(__schedule) from [<c0a03328>]
(schedule_timeout) from [<c0a04940>]
(io_schedule_timeout) from [<c01d585c>]
(congestion_wait) from [<c01cc9d8>]
(shrink_inactive_list) from [<c01cd034>]
(shrink_zone) from [<c01cdd08>]
(try_to_free_pages) from [<c01c442c>]
(__alloc_pages_nodemask) from [<c01f1884>]
(new_slab) from [<c09fcf60>]
(__slab_alloc) from [<c01f1a6c>]

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

This patch uses zone_page_state_snapshot instead, but restricts its usage to avoid performance penalty.

The vmstat sync interval is HZ (sysctl_stat_interval), but since the vmstat_work is declared as a deferrable work, the timer trigger can be deferred to the next non-defferable timer expiry on the CPU which is in idle. This results in the vmstat syncing on an idle CPU being delayed by seconds. May be in most cases this behavior is fine, except in cases like this.

Cc Johannes Weiner <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>

---

 mm/vmscan.c |   56 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 37 insertions(+), 19 deletions(-)
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated +37 -19
diff -puN mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated mm/vmscan.c
@@ -1401,6 +1401,32 @@ int isolate_lru_page(struct page *page)
 	return ret;
 }
 
+static int __too_many_isolated(struct zone *zone, int file,
+	struct scan_control *sc, int safe)
+{
+	unsigned long inactive, isolated;
+
+	if (safe) {
+		inactive = zone_page_state_snapshot(zone,
+				NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state_snapshot(zone,
+				NR_ISOLATED_ANON + file);
+	} else {
+		inactive = zone_page_state(zone, NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state(zone, NR_ISOLATED_ANON + file);
+	}
+
+	/*
+	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
+	 * won't get blocked by normal direct-reclaimers, forming a circular
+	 * deadlock.
+	 */
+	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
+		inactive >>= 3;
+
+	return isolated > inactive;
+}
+
 /*
  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
  * then get resheduled. When there are massive number of tasks doing page
@@ -1409,33 +1435,22 @@ int isolate_lru_page(struct page *page)
  * unnecessary swapping, thrashing and OOM.
  */
 static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc)
+		struct scan_control *sc, int safe)
 {
-	unsigned long inactive, isolated;
-
 	if (current_is_kswapd())
 		return 0;
 
 	if (!global_reclaim(sc))
 		return 0;
 
-	if (file) {
-		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
-		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
-	} else {
-		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
-		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
+	if (unlikely(__too_many_isolated(zone, file, sc, 0))) {
+		if (safe)
+			return __too_many_isolated(zone, file, sc, safe);
+		else
+			return 1;
 	}
 
-	/*
-	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
-	 * won't get blocked by normal direct-reclaimers, forming a circular
-	 * deadlock.
-	 */
-	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
-		inactive >>= 3;
-
-	return isolated > inactive;
+	return 0;
 }
 
 static noinline_for_stack void
@@ -1525,15 +1540,18 @@ shrink_inactive_list(unsigned long nr_to
 	unsigned long nr_immediate = 0;
 	isolate_mode_t isolate_mode = 0;
 	int file = is_file_lru(lru);
+	int safe = 0;
 	struct zone *zone = lruvec_zone(lruvec);
 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 
-	while (unlikely(too_many_isolated(zone, file, sc))) {
+	while (unlikely(too_many_isolated(zone, file, sc, safe))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);
 
 		/* We are about to die and free our memory. Return now. */
 		if (fatal_signal_pending(current))
 			return SWAP_CLUSTER_MAX;
+
+		safe = 1;
 	}
 
 	lru_add_drain();

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw

On Mon 26-01-15 14:11:59, Andrew Morton wrote:
[...]

Quoted 2 lines

So do we drop mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated.patch?

I would like to see a confirmation from Vinayak that this really helped in his test case first and only then drop the above patch. I really think that we shouldn't spread hacks around the code just workaround inefficiency in the vmstat code. We already have two places which need a special treatment and who knows how many more will show up later.

Even with this patch applied we have other issues related to the overloaded workqueues as mentioned earlier but those should be fixed by a separate fixe(s).

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#21 lore raw
In reply to Michal Hocko
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Christoph Lameter
Quoted 4 lines · Vinayak Menon

which had not updated the vmstat_diff. This CPU was in idle for around 30 secs. When I looked at the tvec base for this CPU, the timer associated with vmstat_update had its expiry time less than current jiffies. This timer had its deferrable flag set, and was tied to the next non-deferrable timer in the

We can remove the deferrrable flag now since the vmstat threads are only activated as necessary with the recent changes. Looks like this could fix your issue?

Quoted 8 lines

OK, I have checked the history and the deferrable behavior has been introduced by 39bf6270f524 (VM statistics: Make timer deferrable) which hasn't offered any numbers which would justify the change. So I think it would be a good idea to revert this one as it can clearly cause issues.

Could you retest with this change? It still wouldn't help with the highly overloaded workqueues but that sounds like a bigger change and this one sounds like quite safe to me so it is a good start.

Sure, I can retest.
Even without highly overloaded workqueues, there can be a delay of HZ in updating the counters. This means reclaim path can be blocked for a second or more, when there aren't really any isolated pages. So we need the fix in too_many_isolated also right ?

Signature · 8 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#22 lore raw

On Tue 27-01-15 16:03:57, Vinayak Menon wrote:
[...]

Quoted 1 line

Sure, I can retest.

Thanks!

Quoted 4 lines

Even without highly overloaded workqueues, there can be a delay of HZ in updating the counters. This means reclaim path can be blocked for a second or more, when there aren't really any isolated pages. So we need the fix in too_many_isolated also right ?

Is this a big deal though? What you are hitting is certainly a corner case. I assume your system is trashing heavily already with so few pages on the file LRU list.

Anyway as mentioned in other email I would rather see vmstat data more reliable than spread hacks to the code where we see immediate issues.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#23 lore raw
In reply to Michal Hocko
Quoted 8 lines · Michal Hocko

__round_jiffies_relative can easily make timeout 2HZ from 1HZ. Now we have vmstat_shepherd which waits to be queued and then wait to run. When it runs finally it only queues per-cpu vmstat_work which can also end up being 2HZ for some CPUs. So we can indeed have 4 seconds spent just for queuing. Not even mentioning work item latencies. Especially when workers are overloaded e.g. by fs work items and no additional workers cannot be created e.g. due to memory pressure so they are processed only by the workqueue rescuer. And latencies would grow a lot.

Here is a small fix to ensure that the 4 seconds interval does not happen:

Subject: vmstat: Reduce time interval to stat update on idle cpu

It was noted that the vm stat shepherd runs every 2 seconds and that the vmstat update is then scheduled 2 seconds in the future.

This yields an interval of double the time interval which is not desired.

Change the shepherd so that it does not delay the vmstat update on the other cpu. We stil have to use schedule_delayed_work since we are using a delayed_work_struct but we can set the delay to 0.

mm/vmstat.c +2 -2
Index: linux/mm/vmstat.c
===================================================================
@@ -1435,8 +1435,8 @@ static void vmstat_shepherd(struct work_
 		if (need_update(cpu) &&
 			cpumask_test_and_clear_cpu(cpu, cpu_stat_off))
 
-			schedule_delayed_work_on(cpu, &per_cpu(vmstat_work, cpu),
-				__round_jiffies_relative(sysctl_stat_interval, cpu));
+			schedule_delayed_work_on(cpu,
+				&per_cpu(vmstat_work, cpu), 0);
 
 	put_online_cpus();
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by
#24 lore raw

On Thu 29-01-15 11:32:43, Christoph Lameter wrote:
[...]

Quoted 8 lines

Subject: vmstat: Reduce time interval to stat update on idle cpu

It was noted that the vm stat shepherd runs every 2 seconds and that the vmstat update is then scheduled 2 seconds in the future.

This yields an interval of double the time interval which is not desired.

Quoted 6 lines

Change the shepherd so that it does not delay the vmstat update on the other cpu. We stil have to use schedule_delayed_work since we are using a delayed_work_struct but we can set the delay to 0.

Quoted 16 lines

Index: linux/mm/vmstat.c
===================================================================

mm/vmstat.c +2 -2
@@ -1435,8 +1435,8 @@ static void vmstat_shepherd(struct work_
 		if (need_update(cpu) &&
 			cpumask_test_and_clear_cpu(cpu, cpu_stat_off))
 
-			schedule_delayed_work_on(cpu, &per_cpu(vmstat_work, cpu),
-				__round_jiffies_relative(sysctl_stat_interval, cpu));
+			schedule_delayed_work_on(cpu,
+				&per_cpu(vmstat_work, cpu), 0);
 
 	put_online_cpus();
 
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#25 lore raw
In reply to Vinayak Menon
Quoted 26 lines · On Wed, 14 Jan 2015 17:06:59 +0530 Vinayak Menon <[email protected]>…

It is observed that sometimes multiple tasks get blocked for long in the congestion_wait loop below, in shrink_inactive_list. This is because of vm_stat values not being synced.

(__schedule) from [<c0a03328>]
(schedule_timeout) from [<c0a04940>]
(io_schedule_timeout) from [<c01d585c>]
(congestion_wait) from [<c01cc9d8>]
(shrink_inactive_list) from [<c01cd034>]
(shrink_zone) from [<c01cdd08>]
(try_to_free_pages) from [<c01c442c>]
(__alloc_pages_nodemask) from [<c01f1884>]
(new_slab) from [<c09fcf60>]
(__slab_alloc) from [<c01f1a6c>]

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

Quoted 2 lines

This patch uses zone_page_state_snapshot instead, but restricts its usage to avoid performance penalty.

Seems reasonable.

Quoted 11 lines

...

(quoted diff) +1 -0
@@ -1516,15 +1531,18 @@ shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
 	unsigned long nr_immediate = 0;
 	isolate_mode_t isolate_mode = 0;
 	int file = is_file_lru(lru);
+	int safe = 0;
 	struct zone *zone = lruvec_zone(lruvec);
 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 

Review shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, · line 1546

Quoted 10 lines
(quoted diff) +3 -1
-	while (unlikely(too_many_isolated(zone, file, sc))) {
+	while (unlikely(too_many_isolated(zone, file, sc, safe))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);
 
 		/* We are about to die and free our memory. Return now. */
 		if (fatal_signal_pending(current))
 			return SWAP_CLUSTER_MAX;
+
+		safe = 1;
 	}

But here and under the circumstances you describe, we'll call congestion_wait() a single time. That shouldn't have occurred.

So how about we put the fallback logic into too_many_isolated() itself?

From: Andrew Morton <[email protected]>
Subject: mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix

Move the zone_page_state_snapshot() fallback logic into too_many_isolated(), so shrink_inactive_list() doesn't incorrectly call congestion_wait().

Cc Johannes Weiner <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Vinayak Menon <[email protected]>
Cc Vladimir Davydov <[email protected]>

---

 mm/vmscan.c |   23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix +11 -12
diff -puN mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix mm/vmscan.c
@@ -1402,7 +1402,7 @@ int isolate_lru_page(struct page *page)
 }
 
 static int __too_many_isolated(struct zone *zone, int file,
-	struct scan_control *sc, int safe)
+			       struct scan_control *sc, int safe)
 {
 	unsigned long inactive, isolated;
 
@@ -1435,7 +1435,7 @@ static int __too_many_isolated(struct zo
  * unnecessary swapping, thrashing and OOM.
  */
 static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc, int safe)
+			     struct scan_control *sc)
 {
 	if (current_is_kswapd())
 		return 0;
@@ -1443,12 +1443,14 @@ static int too_many_isolated(struct zone
 	if (!global_reclaim(sc))
 		return 0;
 
-	if (unlikely(__too_many_isolated(zone, file, sc, 0))) {
-		if (safe)
-			return __too_many_isolated(zone, file, sc, safe);
-		else
-			return 1;
-	}
+	/*
+	 * __too_many_isolated(safe=0) is fast but inaccurate, because it
+	 * doesn't account for the vm_stat_diff[] counters.  So if it looks
+	 * like too_many_isolated() is about to return true, fall back to the
+	 * slower, more accurate zone_page_state_snapshot().
+	 */
+	if (unlikely(__too_many_isolated(zone, file, sc, 0)))
+		return __too_many_isolated(zone, file, sc, safe);
 
 	return 0;
 }
@@ -1540,18 +1542,15 @@ shrink_inactive_list(unsigned long nr_to
 	unsigned long nr_immediate = 0;
 	isolate_mode_t isolate_mode = 0;
 	int file = is_file_lru(lru);
-	int safe = 0;
 	struct zone *zone = lruvec_zone(lruvec);
 	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 
-	while (unlikely(too_many_isolated(zone, file, sc, safe))) {
+	while (unlikely(too_many_isolated(zone, file, sc))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);
 
 		/* We are about to die and free our memory. Return now. */
 		if (fatal_signal_pending(current))
 			return SWAP_CLUSTER_MAX;
-
-		safe = 1;
 	}
 
 	lru_add_drain();

_

#26 lore raw
Quoted 58 lines · Andrew Morton
Quoted 26 lines · On Wed, 14 Jan 2015 17:06:59 +0530 Vinayak Menon <[email protected]>…

It is observed that sometimes multiple tasks get blocked for long in the congestion_wait loop below, in shrink_inactive_list. This is because of vm_stat values not being synced.

(__schedule) from [<c0a03328>]
(schedule_timeout) from [<c0a04940>]
(io_schedule_timeout) from [<c01d585c>]
(congestion_wait) from [<c01cc9d8>]
(shrink_inactive_list) from [<c01cd034>]
(shrink_zone) from [<c01cdd08>]
(try_to_free_pages) from [<c01c442c>]
(__alloc_pages_nodemask) from [<c01f1884>]
(new_slab) from [<c09fcf60>]
(__slab_alloc) from [<c01f1a6c>]

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

Quoted 2 lines

This patch uses zone_page_state_snapshot instead, but restricts its usage to avoid performance penalty.

Seems reasonable.

Quoted 11 lines

...

mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix +1 -0
@@ -1516,15 +1531,18 @@ shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
  	unsigned long nr_immediate = 0;
  	isolate_mode_t isolate_mode = 0;
  	int file = is_file_lru(lru);
+	int safe = 0;
  	struct zone *zone = lruvec_zone(lruvec);
  	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, · line 1546

Quoted 10 lines
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix +3 -1
-	while (unlikely(too_many_isolated(zone, file, sc))) {
+	while (unlikely(too_many_isolated(zone, file, sc, safe))) {
  		congestion_wait(BLK_RW_ASYNC, HZ/10);
 
  		/* We are about to die and free our memory. Return now. */
  		if (fatal_signal_pending(current))
  			return SWAP_CLUSTER_MAX;
+
+		safe = 1;
  	}

But here and under the circumstances you describe, we'll call congestion_wait() a single time. That shouldn't have occurred.

Quoted 3 lines

So how about we put the fallback logic into too_many_isolated() itself?

congestion_wait was allowed to run once as an optimization, considering that __too_many_isolated (unsafe and faster) can be correct in returning true most of the time. So we avoid calling the safe version, in most of the cases. But I agree that we should not call congestion_wait unnecessarily even in those rare cases. So this looks correct to me.

Quoted 84 lines

From: Andrew Morton <[email protected]>
Subject: mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix

Move the zone_page_state_snapshot() fallback logic into too_many_isolated(), so shrink_inactive_list() doesn't incorrectly call congestion_wait().

Cc Johannes Weiner <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Vinayak Menon <[email protected]>
Cc Vladimir Davydov <[email protected]>

---

  mm/vmscan.c |   23 +++++++++++------------
  1 file changed, 11 insertions(+), 12 deletions(-)

diff -puN mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix mm/vmscan.c

mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix +11 -12
@@ -1402,7 +1402,7 @@ int isolate_lru_page(struct page *page)
  }
 
  static int __too_many_isolated(struct zone *zone, int file,
-	struct scan_control *sc, int safe)
+			       struct scan_control *sc, int safe)
  {
  	unsigned long inactive, isolated;
 
@@ -1435,7 +1435,7 @@ static int __too_many_isolated(struct zo
   * unnecessary swapping, thrashing and OOM.
   */
  static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc, int safe)
+			     struct scan_control *sc)
  {
  	if (current_is_kswapd())
  		return 0;
@@ -1443,12 +1443,14 @@ static int too_many_isolated(struct zone
  	if (!global_reclaim(sc))
  		return 0;
 
-	if (unlikely(__too_many_isolated(zone, file, sc, 0))) {
-		if (safe)
-			return __too_many_isolated(zone, file, sc, safe);
-		else
-			return 1;
-	}
+	/*
+	 * __too_many_isolated(safe=0) is fast but inaccurate, because it
+	 * doesn't account for the vm_stat_diff[] counters.  So if it looks
+	 * like too_many_isolated() is about to return true, fall back to the
+	 * slower, more accurate zone_page_state_snapshot().
+	 */
+	if (unlikely(__too_many_isolated(zone, file, sc, 0)))
+		return __too_many_isolated(zone, file, sc, safe);
 
  	return 0;
  }
@@ -1540,18 +1542,15 @@ shrink_inactive_list(unsigned long nr_to
  	unsigned long nr_immediate = 0;
  	isolate_mode_t isolate_mode = 0;
  	int file = is_file_lru(lru);
-	int safe = 0;
  	struct zone *zone = lruvec_zone(lruvec);
  	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
 
-	while (unlikely(too_many_isolated(zone, file, sc, safe))) {
+	while (unlikely(too_many_isolated(zone, file, sc))) {
  		congestion_wait(BLK_RW_ASYNC, HZ/10);
 
  		/* We are about to die and free our memory. Return now. */
  		if (fatal_signal_pending(current))
  			return SWAP_CLUSTER_MAX;
-
-		safe = 1;
  	}
 
  	lru_add_drain();

_

Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation
#27 lore raw
In reply to Andrew Morton
Quoted 48 lines · Andrew Morton

From: Andrew Morton <[email protected]>
Subject: mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix

Move the zone_page_state_snapshot() fallback logic into too_many_isolated(), so shrink_inactive_list() doesn't incorrectly call congestion_wait().

Cc Johannes Weiner <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Vinayak Menon <[email protected]>
Cc Vladimir Davydov <[email protected]>

---

  mm/vmscan.c |   23 +++++++++++------------
  1 file changed, 11 insertions(+), 12 deletions(-)

diff -puN mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix mm/vmscan.c

mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix +2 -6
@@ -1402,7 +1402,7 @@ int isolate_lru_page(struct page *page)
  }
 
  static int __too_many_isolated(struct zone *zone, int file,
-	struct scan_control *sc, int safe)
+			       struct scan_control *sc, int safe)
  {
  	unsigned long inactive, isolated;
 
@@ -1435,7 +1435,7 @@ static int __too_many_isolated(struct zo
   * unnecessary swapping, thrashing and OOM.
   */
  static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc, int safe)
+			     struct scan_control *sc)
  {
  	if (current_is_kswapd())
  		return 0;
@@ -1443,12 +1443,14 @@ static int too_many_isolated(struct zone
  	if (!global_reclaim(sc))
  		return 0;
 
-	if (unlikely(__too_many_isolated(zone, file, sc, 0))) {
-		if (safe)
-			return __too_many_isolated(zone, file, sc, safe);
-		else

mm/vmscan.c static int too_many_isolated(struct zone · line 1453

Quoted 10 lines
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated-fix +8 -2
-			return 1;
-	}
+	/*
+	 * __too_many_isolated(safe=0) is fast but inaccurate, because it
+	 * doesn't account for the vm_stat_diff[] counters.  So if it looks
+	 * like too_many_isolated() is about to return true, fall back to the
+	 * slower, more accurate zone_page_state_snapshot().
+	 */
+	if (unlikely(__too_many_isolated(zone, file, sc, 0)))
+		return __too_many_isolated(zone, file, sc, safe);

Just noticed now that, in the above statement it should be "1", instead of "safe". "safe" is not declared.

Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation
maintainer
#28 lore raw

Did we end up deciding to merge this, or is
http://ozlabs.org/~akpm/mmots/broken-out/vmstat-do-not-use-deferrable-delayed-work-for-vmstat_update.patch
a sufficient fix?

From: Vinayak Menon <[email protected]>
Subject: mm: vmscan: fix the page state calculation in too_many_isolated

It is observed that sometimes multiple tasks get blocked for long in the congestion_wait loop below, in shrink_inactive_list. This is because of vm_stat values not being synced.

(__schedule) from [<c0a03328>]
(schedule_timeout) from [<c0a04940>]
(io_schedule_timeout) from [<c01d585c>]
(congestion_wait) from [<c01cc9d8>]
(shrink_inactive_list) from [<c01cd034>]
(shrink_zone) from [<c01cdd08>]
(try_to_free_pages) from [<c01c442c>]
(__alloc_pages_nodemask) from [<c01f1884>]
(new_slab) from [<c09fcf60>]
(__slab_alloc) from [<c01f1a6c>]

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

This patch uses zone_page_state_snapshot instead, but restricts its usage to avoid performance penalty.

The vmstat sync interval is HZ (sysctl_stat_interval), but since the vmstat_work is declared as a deferrable work, the timer trigger can be deferred to the next non-defferable timer expiry on the CPU which is in idle. This results in the vmstat syncing on an idle CPU being delayed by seconds. May be in most cases this behavior is fine, except in cases like this.

[[email protected]: move zone_page_state_snapshot() fallback logic into too_many_isolated()]
Cc Johannes Weiner <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>

---

 mm/vmscan.c |   51 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)
mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated +34 -17
diff -puN mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated mm/vmscan.c
@@ -1363,6 +1363,32 @@ int isolate_lru_page(struct page *page)
 	return ret;
 }
 
+static int __too_many_isolated(struct zone *zone, int file,
+			       struct scan_control *sc, int safe)
+{
+	unsigned long inactive, isolated;
+
+	if (safe) {
+		inactive = zone_page_state_snapshot(zone,
+				NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state_snapshot(zone,
+				NR_ISOLATED_ANON + file);
+	} else {
+		inactive = zone_page_state(zone, NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state(zone, NR_ISOLATED_ANON + file);
+	}
+
+	/*
+	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
+	 * won't get blocked by normal direct-reclaimers, forming a circular
+	 * deadlock.
+	 */
+	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
+		inactive >>= 3;
+
+	return isolated > inactive;
+}
+
 /*
  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
  * then get resheduled. When there are massive number of tasks doing page
@@ -1371,33 +1397,24 @@ int isolate_lru_page(struct page *page)
  * unnecessary swapping, thrashing and OOM.
  */
 static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc)
+			     struct scan_control *sc)
 {
-	unsigned long inactive, isolated;
-
 	if (current_is_kswapd())
 		return 0;
 
 	if (!global_reclaim(sc))
 		return 0;
 
-	if (file) {
-		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
-		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
-	} else {
-		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
-		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
-	}
-
 	/*
-	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
-	 * won't get blocked by normal direct-reclaimers, forming a circular
-	 * deadlock.
+	 * __too_many_isolated(safe=0) is fast but inaccurate, because it
+	 * doesn't account for the vm_stat_diff[] counters.  So if it looks
+	 * like too_many_isolated() is about to return true, fall back to the
+	 * slower, more accurate zone_page_state_snapshot().
 	 */
-	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
-		inactive >>= 3;
+	if (unlikely(__too_many_isolated(zone, file, sc, 0)))
+		return __too_many_isolated(zone, file, sc, 1);
 
-	return isolated > inactive;
+	return 0;
 }
 
 static noinline_for_stack void

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#29 lore raw
Quoted 4 lines · Andrew Morton

Did we end up deciding to merge this, or is
http://ozlabs.org/~akpm/mmots/broken-out/vmstat-do-not-use-deferrable-delayed-work-for-vmstat_update.patch
a sufficient fix?

I think Michal wanted to have the general vmstat worker fix from elsewhere in the thread tested, if it solves the problem by itself, without this patch.

Quoted 137 lines

From: Vinayak Menon <[email protected]>
Subject: mm: vmscan: fix the page state calculation in too_many_isolated

It is observed that sometimes multiple tasks get blocked for long in the congestion_wait loop below, in shrink_inactive_list. This is because of vm_stat values not being synced.

(__schedule) from [<c0a03328>]
(schedule_timeout) from [<c0a04940>]
(io_schedule_timeout) from [<c01d585c>]
(congestion_wait) from [<c01cc9d8>]
(shrink_inactive_list) from [<c01cd034>]
(shrink_zone) from [<c01cdd08>]
(try_to_free_pages) from [<c01c442c>]
(__alloc_pages_nodemask) from [<c01f1884>]
(new_slab) from [<c09fcf60>]
(__slab_alloc) from [<c01f1a6c>]

In one such instance, zone_page_state(zone, NR_ISOLATED_FILE) had returned 14, zone_page_state(zone, NR_INACTIVE_FILE) returned 92, and GFP_IOFS was set, and this resulted in too_many_isolated returning true. But one of the CPU's pageset vm_stat_diff had NR_ISOLATED_FILE as "-14". So the actual isolated count was zero. As there weren't any more updates to NR_ISOLATED_FILE and vmstat_update deffered work had not been scheduled yet, 7 tasks were spinning in the congestion wait loop for around 4 seconds, in the direct reclaim path.

This patch uses zone_page_state_snapshot instead, but restricts its usage to avoid performance penalty.

The vmstat sync interval is HZ (sysctl_stat_interval), but since the vmstat_work is declared as a deferrable work, the timer trigger can be deferred to the next non-defferable timer expiry on the CPU which is in idle. This results in the vmstat syncing on an idle CPU being delayed by seconds. May be in most cases this behavior is fine, except in cases like this.

[[email protected]: move zone_page_state_snapshot() fallback logic into too_many_isolated()]
Cc Johannes Weiner <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>

---

 mm/vmscan.c |   51 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)

diff -puN mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated mm/vmscan.c

mm/vmscan.c renamed from mm/vmscan.c~mm-vmscan-fix-the-page-state-calculation-in-too_many_isolated +34 -17
@@ -1363,6 +1363,32 @@ int isolate_lru_page(struct page *page)
 	return ret;
 }
 
+static int __too_many_isolated(struct zone *zone, int file,
+			       struct scan_control *sc, int safe)
+{
+	unsigned long inactive, isolated;
+
+	if (safe) {
+		inactive = zone_page_state_snapshot(zone,
+				NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state_snapshot(zone,
+				NR_ISOLATED_ANON + file);
+	} else {
+		inactive = zone_page_state(zone, NR_INACTIVE_ANON + 2 * file);
+		isolated = zone_page_state(zone, NR_ISOLATED_ANON + file);
+	}
+
+	/*
+	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
+	 * won't get blocked by normal direct-reclaimers, forming a circular
+	 * deadlock.
+	 */
+	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
+		inactive >>= 3;
+
+	return isolated > inactive;
+}
+
 /*
  * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
  * then get resheduled. When there are massive number of tasks doing page
@@ -1371,33 +1397,24 @@ int isolate_lru_page(struct page *page)
  * unnecessary swapping, thrashing and OOM.
  */
 static int too_many_isolated(struct zone *zone, int file,
-		struct scan_control *sc)
+			     struct scan_control *sc)
 {
-	unsigned long inactive, isolated;
-
 	if (current_is_kswapd())
 		return 0;
 
 	if (!global_reclaim(sc))
 		return 0;
 
-	if (file) {
-		inactive = zone_page_state(zone, NR_INACTIVE_FILE);
-		isolated = zone_page_state(zone, NR_ISOLATED_FILE);
-	} else {
-		inactive = zone_page_state(zone, NR_INACTIVE_ANON);
-		isolated = zone_page_state(zone, NR_ISOLATED_ANON);
-	}
-
 	/*
-	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
-	 * won't get blocked by normal direct-reclaimers, forming a circular
-	 * deadlock.
+	 * __too_many_isolated(safe=0) is fast but inaccurate, because it
+	 * doesn't account for the vm_stat_diff[] counters.  So if it looks
+	 * like too_many_isolated() is about to return true, fall back to the
+	 * slower, more accurate zone_page_state_snapshot().
 	 */
-	if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS)
-		inactive >>= 3;
+	if (unlikely(__too_many_isolated(zone, file, sc, 0)))
+		return __too_many_isolated(zone, file, sc, 1);
 
-	return isolated > inactive;
+	return 0;
 }
 
 static noinline_for_stack void

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>