mm: don't miss the last page because of round-off error
Subsystems (3)
RFC PATCH 1/1 mm: don't miss the last page because of round-off error
I've noticed, that dying memory cgroups are often pinned in memory by a single pagecache page. Even under moderate memory pressure they sometimes stayed in such state for a long time. That looked strange.
My investigation showed that the problem is caused by applying the LRU pressure balancing math:
scan = div64_u64(scan * fraction[lru], denominator),
where
denominator = fraction[anon] + fraction[file] + 1.
Because fraction[lru] is always less than denominator, if the initial scan size is 1, the result is always 0.
This means the last page is not scanned and has no chances to be reclaimed.
Fix this by skipping the balancing logic if the initial scan count is 1.
In practice this change significantly improves the speed of dying cgroups reclaim.
--- mm/vmscan.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
mm/vmscan.c +5 -2
@@ -2287,9 +2287,12 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,/* * Scan types proportional to swappiness and * their relative recent reclaim efficiency. +* Make sure we don't miss the last page +* because of a round-off error. */ - scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);break ;case SCAN_FILE :case SCAN_ANON :
Signature · 1 line
2.17.1
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);
Wouldn't we be better off doing a div_round_up? ie:
scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);
although i'd rather hide that in a new macro in math64.h than opencode it here.
Quoted 9 lines · Matthew Wilcox
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);Wouldn't we be better off doing a
div_round_up? ie:
Quoted 4 lines
scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);although i'd rather hide that in a new macro in
math64.hthan opencode it here.
Good idea! Will do in v2.
Thanks!
Quoted 7 lines · Matthew Wilcox
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);
Quoted 6 lines
Wouldn't we be better off doing a
div_round_up? ie:scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);although i'd rather hide that in a new macro in
math64.hthan opencode it here.
All numbers here should be up to nr_pages * 200 and fit into unsigned long. I see no reason for u64. If they overflow then u64 wouldn't help either.
There is macro DIV_ROUND_UP in kernel.h
Quoted 10 lines · Konstantin Khlebnikov
Quoted 9 lines · Matthew Wilcox
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);Wouldn't we be better off doing a
div_round_up? ie:
Quoted 7 lines
Quoted 4 lines
scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);although i'd rather hide that in a new macro in
math64.hthan opencode it here.All numbers here should be up to
nr_pages* 200 and fit into unsigned long. I see no reason for u64. If they overflow then u64 wouldn't help either.
Shaohua added the div64 usage initially, adding him to the cc.
Quoted 1 line
There is macro
DIV_ROUND_UPinkernel.h
Indeed there is.
Johannes Weiner
maintainerQuoted 10 lines · Konstantin Khlebnikov
Quoted 9 lines · Matthew Wilcox
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);Wouldn't we be better off doing a
div_round_up? ie:
Quoted 7 lines
Quoted 4 lines
scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);although i'd rather hide that in a new macro in
math64.hthan opencode it here.All numbers here should be up to
nr_pages* 200 and fit into unsigned long. I see no reason for u64. If they overflow then u64 wouldn't help either.
It is nr_pages * 200 * recent_scanned, where recent_scanned can be up to four times of what's on the LRUs. That can overflow a u32 with even small amounts of memory.
Quoted 16 lines · Johannes Weiner
Quoted 15 lines · Konstantin Khlebnikov
Quoted 13 lines · Matthew Wilcox
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);Wouldn't we be better off doing a
div_round_up? ie:scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);although i'd rather hide that in a new macro in
math64.hthan opencode it here.
Quoted 6 lines
Quoted 2 lines
All numbers here should be up to
nr_pages* 200 and fit into unsigned long. I see no reason for u64. If they overflow then u64 wouldn't help either.It is
nr_pages* 200 *recent_scanned, whererecent_scannedcan be up to four times of what's on the LRUs. That can overflow a u32 with even small amounts of memory.
Ah, this thing is inverted because it aims to proportional reactivation rate rather than the proportional pressure to reclaimable pages. That's not obvious. I suppose this should be in comment above it.
Well, at least denominator should fit into unsigned long. So full 64/64 division is redundant.
Quoted 24 lines · Konstantin Khlebnikov
Quoted 16 lines · Johannes Weiner
Quoted 15 lines · Konstantin Khlebnikov
Quoted 13 lines · Matthew Wilcox
mm/vmscan.c static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg, · line 2295
Quoted 5 lines · Roman Gushchin
mm/vmscan.c +3
-2- scan =div64_u64 (scan * fraction[file], - denominator); +if (scan >1 ) + scan =div64_u64 (scan * fraction[file], + denominator);Wouldn't we be better off doing a
div_round_up? ie:scan = div64_u64(scan * fraction[file] + denominator - 1, denominator);although i'd rather hide that in a new macro in
math64.hthan opencode it here.Quoted 6 lines
Quoted 2 lines
All numbers here should be up to
nr_pages* 200 and fit into unsigned long. I see no reason for u64. If they overflow then u64 wouldn't help either.It is
nr_pages* 200 *recent_scanned, whererecent_scannedcan be up to four times of what's on the LRUs. That can overflow a u32 with even small amounts of memory.
Quoted 6 lines
Ah, this thing is inverted because it aims to proportional reactivation rate rather than the proportional pressure to reclaimable pages. That's not obvious. I suppose this should be in comment above it.
Well, at least denominator should fit into unsigned long. So full 64/64 division is redundant.
In any case it's not related to the original issue and should be treated separately. I'd like to keep the patch simple to make backporting to stable easy.
All refactorings can be done separately, if necessarily.
Thanks!