Skip to contentSkip to conversation

Reduce latencies and improve overall reclaim efficiency v1

PATCH In review Mel Gorman 8 participants 66 messages Sep 6, 2010 – Oct 29, 2010

Subsystems (9)
lore
Share & tools
lore

Mel Gorman

author
#0 lore raw

cover Reduce latencies and improve overall reclaim efficiency v1

There have been numerous reports of stalls that pointed at the problem being somewhere in the VM. There are multiple roots to the problems which means dealing with any of the root problems in isolation is tricky to justify on their own and they would still need integration testing. This patch series gathers together three different patch sets which in combination should tackle some of the root causes of latency problems being reported.

The first patch improves vmscan latency by tracking when pages get reclaimed by shrink_inactive_list. For this series, the most important results is being able to calculate the scanning/reclaim ratio as a measure of the amount of work being done by page reclaim.

Patches 2 and 3 account for the time spent in congestion_wait() and avoids calling going to sleep on congestion when it is unnecessary. This is expected to reduce stalls in situations where the system is under memory pressure but not due to congestion.

Patches 4-8 were originally developed by Kosaki Motohiro but reworked for this series. It has been noted that lumpy reclaim is far too aggressive and trashes the system somewhat. As SLUB uses high-order allocations, a large cost incurred by lumpy reclaim will be noticeable. It was also reported during transparent hugepage support testing that lumpy reclaim was trashing the system and these patches should mitigate that problem without disabling lumpy reclaim.

Patches 9-10 revisit avoiding filesystem writeback from direct reclaim. This has been reported as being a potential cause of stack overflow but it can also result in poor IO patterns increasing reclaim latencies.

There are patches similar to 9-10 already in mmotm but Andrew had concerns about their impact. Hence, I revisisted them as the last part of this series for re-evaluation.

I ran a number of tests with monitoring on X86, X86-64 and PPC64. Each machine had 3G of RAM and the CPUs were

X86:    Intel P4 2-core
X86-64: AMD Phenom 4-core
PPC64:  PPC970MP

Each used a single disk and the onboard IO controller. Dirty ratio was left at 20. I'm just going to report for X86-64 and PPC64 in a vague attempt to keep this report short. Four kernels were tested each based on v2.6.36-rc3

traceonly-v1r5: Patches 1 and 2 to instrument vmscan reclaims and congestion_wait
nocongest-v1r5: Patches 1-3 for testing wait_iff_congestion
lowlumpy-v1r5:  Patches 1-8 to test if lumpy reclaim is better
nodirect-v1r5:  Patches 1-10 to disable filesystem writeback for better IO

The tests run were as follows

kernbench
compile-based benchmark. Smoke test performance

iozone
Smoke test performance, isn't putting the system under major stress

sysbench
OLTP read-only benchmark. Will be re-run in the future as read-write

micro-mapped-file-stream
This is a micro-benchmark from Johannes Weiner that accesses a large sparse-file through mmap(). It was configured to run in only single-CPU mode but can be indicative of how well page reclaim identifies suitable pages.

stress-highalloc
Tries to allocate huge pages under heavy load.

kernbench, iozone and sysbench did not report any performance regression on any machine and as they did not put the machine under memory pressure the main paths this series deals with were not exercised. sysbench will be re-run in the future with read-write testing as it is sensitive to writeback performance under memory pressure. It is an oversight that it didn't happen for this test.

X86-64 micro-mapped-file-stream
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5

pgalloc_dma                       2631.00 (   0.00%)      2483.00 (  -5.96%)      2375.00 ( -10.78%)      2467.00 (  -6.65%)
pgalloc_dma32                  2840528.00 (   0.00%)   2841510.00 (   0.03%)   2841391.00 (   0.03%)   2842308.00 (   0.06%)
pgalloc_normal                       0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pgsteal_dma                       1383.00 (   0.00%)      1182.00 ( -17.01%)      1177.00 ( -17.50%)      1181.00 ( -17.10%)
pgsteal_dma32                  2237658.00 (   0.00%)   2236581.00 (  -0.05%)   2219885.00 (  -0.80%)   2234527.00 (  -0.14%)
pgsteal_normal                       0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pgscan_kswapd_dma                 3006.00 (   0.00%)      1400.00 (-114.71%)      1547.00 ( -94.31%)      1347.00 (-123.16%)
pgscan_kswapd_dma32            4206487.00 (   0.00%)   3343082.00 ( -25.83%)   3425728.00 ( -22.79%)   3304369.00 ( -27.30%)
pgscan_kswapd_normal                 0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pgscan_direct_dma                  629.00 (   0.00%)      1793.00 (  64.92%)      1643.00 (  61.72%)      1868.00 (  66.33%)
pgscan_direct_dma32             506741.00 (   0.00%)   1402557.00 (  63.87%)   1330777.00 (  61.92%)   1448345.00 (  65.01%)
pgscan_direct_normal                 0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pageoutrun                       15449.00 (   0.00%)     15555.00 (   0.68%)     15319.00 (  -0.85%)     15963.00 (   3.22%)
allocstall                         152.00 (   0.00%)       941.00 (  83.85%)       967.00 (  84.28%)       729.00 (  79.15%)

These are just the raw figures taken from /proc/vmstat. It's a rough measure of reclaim activity. Note that allocstall counts are higher because we are entering direct reclaim more often as a result of not sleeping in congestion. In itself, it's not necessarily a bad thing. It's easier to get a view of what happened from the vmscan tracepoint report.

FTrace Reclaim Statistics: vmscan
micro-traceonly-v1r5-micromicro-nocongest-v1r5-micromicro-lowlumpy-v1r5-micromicro-nodirect-v1r5-micro
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5
Direct reclaims 152 941 967 729
Direct reclaim pages scanned 507377 1404350 1332420 1450213
Direct reclaim pages reclaimed 10968 72042 77186 41097

Direct reclaim write file async I/O              0          0          0          0 
Direct reclaim write anon async I/O              0          0          0          0 
Direct reclaim write file sync I/O               0          0          0          0 
Direct reclaim write anon sync I/O               0          0          0          0 
Wake kswapd requests                        127195     241025     254825     188846 
Kswapd wakeups                                   6          1          1          1 
Kswapd pages scanned                       4210101    3345122    3427915    3306356 
Kswapd pages reclaimed                     2228073    2165721    2143876    2194611 
Kswapd reclaim write file async I/O              0          0          0          0 
Kswapd reclaim write anon async I/O              0          0          0          0 
Kswapd reclaim write file sync I/O               0          0          0          0 
Kswapd reclaim write anon sync I/O               0          0          0          0 
Time stalled direct reclaim (seconds)         7.60       3.03       3.24       3.43 
Time kswapd awake (seconds)                  12.46       9.46       9.56       9.40 
Total pages scanned                        4717478   4749472   4760335   4756569
Total pages reclaimed                      2239041   2237763   2221062   2235708
%age total pages scanned/reclaimed          47.46%    47.12%    46.66%    47.00%
%age total pages scanned/written             0.00%     0.00%     0.00%     0.00%
%age  file pages scanned/written             0.00%     0.00%     0.00%     0.00%
Percentage Time Spent Direct Reclaim        43.80%    21.38%    22.34%    23.46%
Percentage Time kswapd Awake                79.92%    79.56%    79.20%    80.48%

What is interesting here for nocongest in particular is that while direct reclaim scans more pages, the overall number of pages scanned remains the same and the ratio of pages scanned to pages reclaimed is more or less the same. In other words, while we are sleeping less, reclaim is not doing more work and in fact, direct reclaim and kswapd is awake for less time. Overall, the series reduces reclaim work.

FTrace Reclaim Statistics: congestion_wait

Direct number congest     waited               148          0          0          0 
Direct time   congest     waited            8376ms        0ms        0ms        0ms 
Direct full   congest     waited               127          0          0          0 
Direct number conditional waited                 0        711        693        627 
Direct time   conditional waited               0ms        0ms        0ms        0ms 
Direct full   conditional waited               127          0          0          0 
KSwapd number congest     waited                38         11         12         14 
KSwapd time   congest     waited            3236ms      548ms      576ms      576ms 
KSwapd full   congest     waited                31          3          3          2 

KSwapd number conditional waited 0 0 0 0
KSwapd time conditional waited 0ms 0ms 0ms 0ms
KSwapd full conditional waited 31 3 3 2

The vanilla kernel spent 8 seconds asleep in direct reclaim and no time at all asleep with the patches.

MMTests Statistics: duration

User/Sys Time Running Test (seconds)          9.75     11.14     11.26     11.19
Total Elapsed Time (seconds)                 15.59     11.89     12.07     11.68

And overall, the tests complete significantly faster. Indicators are that reclaim did less work and the test completed faster with fewer stalls. Seems good.

PPC64 micro-mapped-file-stream
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5

pgalloc_dma                    3027144.00 (   0.00%)   3025080.00 (  -0.07%)   3025463.00 (  -0.06%)   3026037.00 (  -0.04%)
pgalloc_normal                       0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pgsteal_dma                    2399696.00 (   0.00%)   2399540.00 (  -0.01%)   2399592.00 (  -0.00%)   2399570.00 (  -0.01%)
pgsteal_normal                       0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pgscan_kswapd_dma              3690319.00 (   0.00%)   2883661.00 ( -27.97%)   2852314.00 ( -29.38%)   3008323.00 ( -22.67%)
pgscan_kswapd_normal                 0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pgscan_direct_dma              1224036.00 (   0.00%)   1975664.00 (  38.04%)   2012185.00 (  39.17%)   1907869.00 (  35.84%)
pgscan_direct_normal                 0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)         0.00 (   0.00%)
pageoutrun                       15170.00 (   0.00%)     14636.00 (  -3.65%)     14664.00 (  -3.45%)     16027.00 (   5.35%)
allocstall                         712.00 (   0.00%)      1906.00 (  62.64%)      1912.00 (  62.76%)      2027.00 (  64.87%)

Similar trends to x86-64. allocstalls are up but it's not necessarily bad.

FTrace Reclaim Statistics: vmscan
micro-traceonly-v1r5-micromicro-nocongest-v1r5-micromicro-lowlumpy-v1r5-micromicro-nodirect-v1r5-micro
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5
Direct reclaims 712 1906 1904 2021
Direct reclaim pages scanned 1224100 1975664 2010015 1906767
Direct reclaim pages reclaimed 79215 218292 202719 209388

Direct reclaim write file async I/O              0          0          0          0 
Direct reclaim write anon async I/O              0          0          0          0 
Direct reclaim write file sync I/O               0          0          0          0 
Direct reclaim write anon sync I/O               0          0          0          0 
Wake kswapd requests                       1154724     805852     767944     848063 
Kswapd wakeups                                   3          2          2          2 
Kswapd pages scanned                       3690799    2884173    2852026    3008835 
Kswapd pages reclaimed                     2320481    2181248    2195908    2189076 
Kswapd reclaim write file async I/O              0          0          0          0 
Kswapd reclaim write anon async I/O              0          0          0          0 
Kswapd reclaim write file sync I/O               0          0          0          0 
Kswapd reclaim write anon sync I/O               0          0          0          0 

Time stalled direct reclaim (seconds) 21.02 7.19 7.72 6.76
Time kswapd awake (seconds) 39.55 25.31 24.88 24.83

Total pages scanned                        4914899   4859837   4862041   4915602
Total pages reclaimed                      2399696   2399540   2398627   2398464
%age total pages scanned/reclaimed          48.82%    49.37%    49.33%    48.79%
%age total pages scanned/written             0.00%     0.00%     0.00%     0.00%
%age  file pages scanned/written             0.00%     0.00%     0.00%     0.00%
Percentage Time Spent Direct Reclaim        43.44%    19.64%    20.77%    18.43%
Percentage Time kswapd Awake                87.36%    81.94%    81.84%    81.28%

Again, a similar trend that the congestion_wait changes mean that direct reclaim scans more pages but the overall number of pages scanned is very similar and the ratio of scanning/reclaimed remains roughly similar. Once again, reclaim is not doing more work, but spends less time in direct reclaim and with kswapd awake.

What is interesting here for nocongest in particular is that while direct reclaim scans more pages, the overall number of pages scanned remains the same and the ratio of pages scanned to pages reclaimed is more or less the same. In other words, while we are sleeping less, reclaim is not doing more work and in fact, direct reclaim and kswapd is awake for less time. Overall, the series reduces reclaim work.

FTrace Reclaim Statistics: congestion_wait

Direct number congest     waited               499          0          0          0 
Direct time   congest     waited           22700ms        0ms        0ms        0ms 
Direct full   congest     waited               421          0          0          0 
Direct number conditional waited                 0       1214       1242       1290 
Direct time   conditional waited               0ms        4ms        0ms        0ms 
Direct full   conditional waited               421          0          0          0 
KSwapd number congest     waited               257        103         94        104 
KSwapd time   congest     waited           22116ms     7344ms     7476ms     7528ms 
KSwapd full   congest     waited               203         57         59         56 
KSwapd number conditional waited                 0          0          0          0 
KSwapd time   conditional waited               0ms        0ms        0ms        0ms 
KSwapd full   conditional waited               203         57         59         56 

The vanilla kernel spent 22 seconds asleep in direct reclaim and no time at all asleep with the patches. which is a big improvement. The time kswapd spent congest waited was also reduced by a large factor.

MMTests Statistics: duration

User/Sys Time Running Test (seconds)         27.37     29.42     29.45     29.91
Total Elapsed Time (seconds)                 45.27     30.89     30.40     30.55

And the test again completed far faster.

X86-64 STRESS-HIGHALLOC
stress-highalloc stress-highalloc stress-highalloc stress-highalloc
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5

Pass 1          84.00 ( 0.00%)    84.00 ( 0.00%)    80.00 (-4.00%)    72.00 (-12.00%)
Pass 2          94.00 ( 0.00%)    94.00 ( 0.00%)    89.00 (-5.00%)    88.00 (-6.00%)
At Rest         95.00 ( 0.00%)    95.00 ( 0.00%)    95.00 ( 0.00%)    92.00 (-3.00%)

Success figures start dropping off for lowlumpy and nodirect. This ordinarily would be a concern but the rest of the report paints a better picture.

FTrace Reclaim Statistics: vmscan
stress-highalloc stress-highalloc stress-highalloc stress-highalloc
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5
Direct reclaims 838 1189 1323 1197
Direct reclaim pages scanned 182207 168696 146310 133117
Direct reclaim pages reclaimed 84208 81706 80442 54879
Direct reclaim write file async I/O 538 619 839 0
Direct reclaim write anon async I/O 36403 32892 44126 22085
Direct reclaim write file sync I/O 88 108 1 0

Direct reclaim write anon sync I/O           19107      15514        871          0 
Wake kswapd requests                          7761        827        865       6502 
Kswapd wakeups                                 749        733        658        614 
Kswapd pages scanned                       6400676    6871918    6875056    3126591 
Kswapd pages reclaimed                     3122126    3376919    3001799    1669300 
Kswapd reclaim write file async I/O          58199      67175      28483        925 
Kswapd reclaim write anon async I/O        1740452    1851455    1680964     186578 
Kswapd reclaim write file sync I/O               0          0          0          0 
Kswapd reclaim write anon sync I/O               0          0          0          0 
Time stalled direct reclaim (seconds)      3864.84    4426.77    3108.85     254.08 
Time kswapd awake (seconds)                1792.00    2130.10    1890.76     343.37 
Total pages scanned                        6582883   7040614   7021366   3259708
Total pages reclaimed                      3206334   3458625   3082241   1724179
%age total pages scanned/reclaimed          48.71%    49.12%    43.90%    52.89%
%age total pages scanned/written            28.18%    27.95%    25.00%     6.43%
%age  file pages scanned/written             0.89%     0.96%     0.42%     0.03%
Percentage Time Spent Direct Reclaim        53.38%    56.75%    47.80%     8.44%
Percentage Time kswapd Awake                35.35%    37.88%    43.97%    23.01%

Scanned/reclaimed ratios again look good. The Scanned/written ratios look very good for the nodirect patches showing that the writeback is happening more in the flusher threads and less from direct reclaim. The expectation is that the IO should be more efficient and indeed the time spent in direct reclaim is massively reduced by the full series and kswapd spends a little less time awake.

Overall, indications here are that things are moving much faster.

FTrace Reclaim Statistics: congestion_wait

Direct number congest     waited              1060          1          0          0 
Direct time   congest     waited           63664ms      100ms        0ms        0ms 
Direct full   congest     waited               617          1          0          0 
Direct number conditional waited                 0       1650        866        838 
Direct time   conditional waited               0ms    20296ms     1916ms    17652ms 
Direct full   conditional waited               617          1          0          0 
KSwapd number congest     waited               399          0        466         12 
KSwapd time   congest     waited           33376ms        0ms    33048ms      968ms 
KSwapd full   congest     waited               318          0        312          9 
KSwapd number conditional waited                 0          0          0          0 
KSwapd time   conditional waited               0ms        0ms        0ms        0ms 
KSwapd full   conditional waited               318          0        312          9 

The sleep times for congest wait get interesting here. congestion_wait() times are dropped to almost zero but wait_iff_congested() is detecting when there is in fact congestion or too much writeback and still going to sleep. Overall the times are reduced though - from 63ish seconds to about 20. We are still backing off but less aggressively.

MMTests Statistics: duration

User/Sys Time Running Test (seconds)       3375.95   3374.04   3395.56   2756.97
Total Elapsed Time (seconds)               5068.80   5623.06   4300.45   1492.09

Oddly, the nocongest patches took longer to complete the test but the overall series reduces the test time by almost an hour or about in one third of the time. I also looked at the latency figures when allocating huge pages and got this

http://www.csn.ul.ie/~mel/postings/vmscanreduce-20100609/highalloc-interlatency-hydra-mean.ps

So it looks like the latencies in general are reduced. The full series reduces latency by massive amounts but there is also a hint why nocongest was slower overall. Its latencies were lower up to the point where 72% of memory was allocated with huge pages. After the latencies were higher but this problem is resolved later in the series.

PPC64 STRESS-HIGHALLOC

                traceonly-v1r5    nocongest-v1r5     lowlumpy-v1r5     nodirect-v1r5
Pass 1          27.00 ( 0.00%)    38.00 (11.00%)    31.00 ( 4.00%)    43.00 (16.00%)
Pass 2          41.00 ( 0.00%)    43.00 ( 2.00%)    33.00 (-8.00%)    55.00 (14.00%)
At Rest         84.00 ( 0.00%)    83.00 (-1.00%)    84.00 ( 0.00%)    85.00 ( 1.00%)

Success rates there are *way* up particularly considering that the 16MB huge pages on PPC64 mean that it's always much harder to allocate them.

FTrace Reclaim Statistics: vmscan
stress-highalloc stress-highalloc stress-highalloc stress-highalloc
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5
Direct reclaims 461 426 547 915

Direct reclaim pages scanned                193118     171811     143647     138334 
Direct reclaim pages reclaimed              130100     108863      65954      63043 
Direct reclaim write file async I/O            442        293        748          0 
Direct reclaim write anon async I/O          52948      45149      29910       9949 
Direct reclaim write file sync I/O              34        154          0          0 
Direct reclaim write anon sync I/O           33128      27267        119          0 
Wake kswapd requests                           302        282        306        233 
Kswapd wakeups                                 154        146        123        132 
Kswapd pages scanned                      13019861   12506267    3409775    3072689 
Kswapd pages reclaimed                     4839299    4782393    1908499    1723469 
Kswapd reclaim write file async I/O          77348      77785      14580        214 
Kswapd reclaim write anon async I/O        2878272    2840643     428083     142755 
Kswapd reclaim write file sync I/O               0          0          0          0 
Kswapd reclaim write anon sync I/O               0          0          0          0 
Time stalled direct reclaim (seconds)      7692.01    7473.31    1044.76     217.31 
Time kswapd awake (seconds)                7332.64    7171.23    1059.70     357.02 
Total pages scanned                       13212979  12678078   3553422   3211023
Total pages reclaimed                      4969399   4891256   1974453   1786512
%age total pages scanned/reclaimed          37.61%    38.58%    55.56%    55.64%
%age total pages scanned/written            23.02%    23.59%    13.32%     4.76%
%age  file pages scanned/written             0.59%     0.62%     0.43%     0.01%
Percentage Time Spent Direct Reclaim        42.66%    43.22%    26.30%     6.59%
Percentage Time kswapd Awake                82.06%    82.08%    45.82%    21.87%

Initially, it looks like the scanned/reclaimed ratios are much higher and that's a bad thing. However, the number of pages scanned is reduced by around 75% and the times spent in direct reclaim and with kswapd are *massively* reduced. Overall the VM seems to be doing a lot less work.

FTrace Reclaim Statistics: congestion_wait

Direct number congest     waited               811         23         38          0 
Direct time   congest     waited           40272ms      512ms     1496ms        0ms 
Direct full   congest     waited               484          4         14          0 
Direct number conditional waited                 0        703        345       1281 
Direct time   conditional waited               0ms    22776ms     1312ms    10428ms 
Direct full   conditional waited               484          4         14          0 
KSwapd number congest     waited                 1          0          6          6 
KSwapd time   congest     waited             100ms        0ms      124ms      404ms 
KSwapd full   congest     waited                 1          0          1          2 

KSwapd number conditional waited 0 0 0 0
KSwapd time conditional waited 0ms 0ms 0ms 0ms
KSwapd full conditional waited 1 0 1 2

Not as dramatic a story here but the time spent asleep is reduced and we can still see what wait_iff_congested is going to sleep when necessary.

MMTests Statistics: duration

User/Sys Time Running Test (seconds)      10340.18   9818.41   2927.13   3078.91
Total Elapsed Time (seconds)               8936.19   8736.59   2312.71   1632.74

The time to complete this test goes way down. Take the allocation success rates - we are allocating 16% more memory as huge pages in less than a fifth of the time and this is reflected in the allocation latency data

http://www.csn.ul.ie/~mel/postings/vmscanreduce-20100609/highalloc-interlatency-powyah-mean.ps

I recognise that this is a weighty series but the desktop latency and other stall issues are a tricky topic. There are multiple root causes as to what might be causing them but I believe this series kicks a number of them. I think the congestion_wait changes will also impact Dave Chinner's fs-mark test that showed up in the minute-long livelock report but I'm hoping the filesystem people that were complaining about latencies in the VM could test this series with their respective workloads.

 .../trace/postprocess/trace-vmscan-postprocess.pl  |   39 +++-
 include/linux/backing-dev.h                        |    2 +-
 include/trace/events/vmscan.h                      |   44 ++++-
 include/trace/events/writeback.h                   |   35 +++
 mm/backing-dev.c                                   |   71 ++++++-
 mm/page_alloc.c                                    |    4 +-
 mm/vmscan.c                                        |  253 +++++++++++++++-----
 7 files changed, 368 insertions(+), 80 deletions(-)

--
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>

author
#1 lore raw

PATCH 1/10 tracing, vmscan: Add trace events for LRU list shrinking

This patch adds a trace event for shrink_inactive_list(). It can be used to determine how many pages were reclaimed and for non-lumpy reclaim where exactly the pages were reclaimed from.

---
 .../trace/postprocess/trace-vmscan-postprocess.pl  |   39 +++++++++++++-----
 include/trace/events/vmscan.h                      |   42 ++++++++++++++++++++
 mm/vmscan.c                                        |    6 +++
 3 files changed, 77 insertions(+), 10 deletions(-)
Documentation/trace/postprocess/trace-vmscan-postprocess.pl +29 -10
index 1b55146..b3e73dd 100644
@@ -46,7 +46,7 @@ use constant HIGH_KSWAPD_LATENCY		=> 20;
 use constant HIGH_KSWAPD_REWAKEUP		=> 21;
 use constant HIGH_NR_SCANNED			=> 22;
 use constant HIGH_NR_TAKEN			=> 23;
-use constant HIGH_NR_RECLAIM			=> 24;
+use constant HIGH_NR_RECLAIMED			=> 24;
 use constant HIGH_NR_CONTIG_DIRTY		=> 25;
 
 my %perprocesspid;
@@ -58,11 +58,13 @@ my $opt_read_procstat;
 my $total_wakeup_kswapd;
 my ($total_direct_reclaim, $total_direct_nr_scanned);
 my ($total_direct_latency, $total_kswapd_latency);
+my ($total_direct_nr_reclaimed);
 my ($total_direct_writepage_file_sync, $total_direct_writepage_file_async);
 my ($total_direct_writepage_anon_sync, $total_direct_writepage_anon_async);
 my ($total_kswapd_nr_scanned, $total_kswapd_wake);
 my ($total_kswapd_writepage_file_sync, $total_kswapd_writepage_file_async);
 my ($total_kswapd_writepage_anon_sync, $total_kswapd_writepage_anon_async);
+my ($total_kswapd_nr_reclaimed);
 
 # Catch sigint and exit on request
 my $sigint_report = 0;
@@ -104,7 +106,7 @@ my $regex_kswapd_wake_default = 'nid=([0-9]*) order=([0-9]*)';
 my $regex_kswapd_sleep_default = 'nid=([0-9]*)';
 my $regex_wakeup_kswapd_default = 'nid=([0-9]*) zid=([0-9]*) order=([0-9]*)';
 my $regex_lru_isolate_default = 'isolate_mode=([0-9]*) order=([0-9]*) nr_requested=([0-9]*) nr_scanned=([0-9]*) nr_taken=([0-9]*) contig_taken=([0-9]*) contig_dirty=([0-9]*) contig_failed=([0-9]*)';
-my $regex_lru_shrink_inactive_default = 'lru=([A-Z_]*) nr_scanned=([0-9]*) nr_reclaimed=([0-9]*) priority=([0-9]*)';
+my $regex_lru_shrink_inactive_default = 'nid=([0-9]*) zid=([0-9]*) nr_scanned=([0-9]*) nr_reclaimed=([0-9]*) priority=([0-9]*) flags=([A-Z_|]*)';
 my $regex_lru_shrink_active_default = 'lru=([A-Z_]*) nr_scanned=([0-9]*) nr_rotated=([0-9]*) priority=([0-9]*)';
 my $regex_writepage_default = 'page=([0-9a-f]*) pfn=([0-9]*) flags=([A-Z_|]*)';
 
@@ -203,8 +205,8 @@ $regex_lru_shrink_inactive = generate_traceevent_regex(
 			"vmscan/mm_vmscan_lru_shrink_inactive",
 			$regex_lru_shrink_inactive_default,
 			"nid", "zid",
-			"lru",
-			"nr_scanned", "nr_reclaimed", "priority");
+			"nr_scanned", "nr_reclaimed", "priority",
+			"flags");
 $regex_lru_shrink_active = generate_traceevent_regex(
 			"vmscan/mm_vmscan_lru_shrink_active",
 			$regex_lru_shrink_active_default,
@@ -375,6 +377,16 @@ EVENT_PROCESS:
 			my $nr_contig_dirty = $7;
 			$perprocesspid{$process_pid}->{HIGH_NR_SCANNED} += $nr_scanned;
 			$perprocesspid{$process_pid}->{HIGH_NR_CONTIG_DIRTY} += $nr_contig_dirty;
+		} elsif ($tracepoint eq "mm_vmscan_lru_shrink_inactive") {
+			$details = $5;
+			if ($details !~ /$regex_lru_shrink_inactive/o) {
+				print "WARNING: Failed to parse mm_vmscan_lru_shrink_inactive as expected\n";
+				print "         $details\n";
+				print "         $regex_lru_shrink_inactive/o\n";
+				next;
+			}
+			my $nr_reclaimed = $4;
+			$perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED} += $nr_reclaimed;
 		} elsif ($tracepoint eq "mm_vmscan_writepage") {
 			$details = $5;
 			if ($details !~ /$regex_writepage/o) {
@@ -464,8 +476,8 @@ sub dump_stats {
 
 	# Print out process activity
 	printf("\n");
-	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s %8s\n", "Process", "Direct",  "Wokeup", "Pages",   "Pages",   "Pages",     "Time");
-	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s %8s\n", "details", "Rclms",   "Kswapd", "Scanned", "Sync-IO", "ASync-IO",  "Stalled");
+	printf("%-" . $max_strlen . "s %8s %10s   %8s %8s  %8s %8s %8s %8s\n", "Process", "Direct",  "Wokeup", "Pages",   "Pages",   "Pages",   "Pages",     "Time");
+	printf("%-" . $max_strlen . "s %8s %10s   %8s %8s  %8s %8s %8s %8s\n", "details", "Rclms",   "Kswapd", "Scanned", "Rclmed",  "Sync-IO", "ASync-IO",  "Stalled");
 	foreach $process_pid (keys %stats) {
 
 		if (!$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN}) {
@@ -475,6 +487,7 @@ sub dump_stats {
 		$total_direct_reclaim += $stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN};
 		$total_wakeup_kswapd += $stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
 		$total_direct_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
+		$total_direct_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
 		$total_direct_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
 		$total_direct_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
 		$total_direct_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
@@ -489,11 +502,12 @@ sub dump_stats {
 			$index++;
 		}
 
-		printf("%-" . $max_strlen . "s %8d %10d   %8u   %8u %8u %8.3f",
+		printf("%-" . $max_strlen . "s %8d %10d   %8u %8u  %8u %8u %8.3f",
 			$process_pid,
 			$stats{$process_pid}->{MM_VMSCAN_DIRECT_RECLAIM_BEGIN},
 			$stats{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD},
 			$stats{$process_pid}->{HIGH_NR_SCANNED},
+			$stats{$process_pid}->{HIGH_NR_RECLAIMED},
 			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
 			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC},
 			$this_reclaim_delay / 1000);
@@ -529,8 +543,8 @@ sub dump_stats {
 
 	# Print out kswapd activity
 	printf("\n");
-	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s\n", "Kswapd",   "Kswapd",  "Order",     "Pages",   "Pages",  "Pages");
-	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s\n", "Instance", "Wakeups", "Re-wakeup", "Scanned", "Sync-IO", "ASync-IO");
+	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s\n", "Kswapd",   "Kswapd",  "Order",     "Pages",   "Pages",   "Pages",  "Pages");
+	printf("%-" . $max_strlen . "s %8s %10s   %8s   %8s %8s %8s\n", "Instance", "Wakeups", "Re-wakeup", "Scanned", "Rclmed",  "Sync-IO", "ASync-IO");
 	foreach $process_pid (keys %stats) {
 
 		if (!$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE}) {
@@ -539,16 +553,18 @@ sub dump_stats {
 
 		$total_kswapd_wake += $stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE};
 		$total_kswapd_nr_scanned += $stats{$process_pid}->{HIGH_NR_SCANNED};
+		$total_kswapd_nr_reclaimed += $stats{$process_pid}->{HIGH_NR_RECLAIMED};
 		$total_kswapd_writepage_file_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
 		$total_kswapd_writepage_anon_sync += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
 		$total_kswapd_writepage_file_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
 		$total_kswapd_writepage_anon_async += $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC};
 
-		printf("%-" . $max_strlen . "s %8d %10d   %8u   %8i %8u",
+		printf("%-" . $max_strlen . "s %8d %10d   %8u %8u  %8i %8u",
 			$process_pid,
 			$stats{$process_pid}->{MM_VMSCAN_KSWAPD_WAKE},
 			$stats{$process_pid}->{HIGH_KSWAPD_REWAKEUP},
 			$stats{$process_pid}->{HIGH_NR_SCANNED},
+			$stats{$process_pid}->{HIGH_NR_RECLAIMED},
 			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC},
 			$stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} + $stats{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_ASYNC});
 
@@ -579,6 +595,7 @@ sub dump_stats {
 	print "\nSummary\n";
 	print "Direct reclaims:     			$total_direct_reclaim\n";
 	print "Direct reclaim pages scanned:		$total_direct_nr_scanned\n";
+	print "Direct reclaim pages reclaimed:		$total_direct_nr_reclaimed\n";
 	print "Direct reclaim write file sync I/O:	$total_direct_writepage_file_sync\n";
 	print "Direct reclaim write anon sync I/O:	$total_direct_writepage_anon_sync\n";
 	print "Direct reclaim write file async I/O:	$total_direct_writepage_file_async\n";
@@ -588,6 +605,7 @@ sub dump_stats {
 	print "\n";
 	print "Kswapd wakeups:				$total_kswapd_wake\n";
 	print "Kswapd pages scanned:			$total_kswapd_nr_scanned\n";
+	print "Kswapd pages reclaimed:			$total_kswapd_nr_reclaimed\n";
 	print "Kswapd reclaim write file sync I/O:	$total_kswapd_writepage_file_sync\n";
 	print "Kswapd reclaim write anon sync I/O:	$total_kswapd_writepage_anon_sync\n";
 	print "Kswapd reclaim write file async I/O:	$total_kswapd_writepage_file_async\n";
@@ -612,6 +630,7 @@ sub aggregate_perprocesspid() {
 		$perprocess{$process}->{MM_VMSCAN_WAKEUP_KSWAPD} += $perprocesspid{$process_pid}->{MM_VMSCAN_WAKEUP_KSWAPD};
 		$perprocess{$process}->{HIGH_KSWAPD_REWAKEUP} += $perprocesspid{$process_pid}->{HIGH_KSWAPD_REWAKEUP};
 		$perprocess{$process}->{HIGH_NR_SCANNED} += $perprocesspid{$process_pid}->{HIGH_NR_SCANNED};
+		$perprocess{$process}->{HIGH_NR_RECLAIMED} += $perprocesspid{$process_pid}->{HIGH_NR_RECLAIMED};
 		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_SYNC};
 		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_ANON_SYNC};
 		$perprocess{$process}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC} += $perprocesspid{$process_pid}->{MM_VMSCAN_WRITEPAGE_FILE_ASYNC};
include/trace/events/vmscan.h +42 -0

66 lines not on this page · raw message

mm/vmscan.c +6 -0
index c391c32..652650f 100644
@@ -1359,6 +1359,12 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 	__count_zone_vm_events(PGSTEAL, zone, nr_reclaimed);
 
 	putback_lru_pages(zone, sc, nr_anon, nr_file, &page_list);
+
+	trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id,
+		zone_idx(zone),
+		nr_scanned, nr_reclaimed,
+		priority,
+		trace_shrink_flags(file, sc->lumpy_reclaim_mode));
 	return nr_reclaimed;
 }
 
Signature · 7 lines
1.7.1

--
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>
author
#2 lore raw

PATCH 2/10 writeback: Account for time spent congestion_waited

In reply to Mel Gorman

There is strong evidence to indicate a lot of time is being spent in congestion_wait(), some of it unnecessarily. This patch adds a tracepoint for congestion_wait to record when congestion_wait() was called, how long the timeout was for and how long it actually slept.

---
 include/trace/events/writeback.h |   28 ++++++++++++++++++++++++++++
 mm/backing-dev.c                 |    5 +++++
 2 files changed, 33 insertions(+), 0 deletions(-)
include/trace/events/writeback.h +28 -0
index f345f66..275d477 100644
@@ -153,6 +153,34 @@ DEFINE_WBC_EVENT(wbc_balance_dirty_written);
 DEFINE_WBC_EVENT(wbc_balance_dirty_wait);
 DEFINE_WBC_EVENT(wbc_writepage);
 
+DECLARE_EVENT_CLASS(writeback_congest_waited_template,
+
+	TP_PROTO(unsigned int usec_timeout, unsigned int usec_delayed),
+
+	TP_ARGS(usec_timeout, usec_delayed),
+
+	TP_STRUCT__entry(
+		__field(	unsigned int,	usec_timeout	)
+		__field(	unsigned int,	usec_delayed	)
+	),
+
+	TP_fast_assign(
+		__entry->usec_timeout	= usec_timeout;
+		__entry->usec_delayed	= usec_delayed;
+	),
+
+	TP_printk("usec_timeout=%u usec_delayed=%u",
+			__entry->usec_timeout,
+			__entry->usec_delayed)
+);
+
+DEFINE_EVENT(writeback_congest_waited_template, writeback_congestion_wait,
+
+	TP_PROTO(unsigned int usec_timeout, unsigned int usec_delayed),
+
+	TP_ARGS(usec_timeout, usec_delayed)
+);
+
 #endif /* _TRACE_WRITEBACK_H */
 
 /* This part must be outside protection */
mm/backing-dev.c +5 -0
index eaa4a5b..298975a 100644
@@ -759,12 +759,17 @@ EXPORT_SYMBOL(set_bdi_congested);
 long congestion_wait(int sync, long timeout)
 {
 	long ret;
+	unsigned long start = jiffies;
 	DEFINE_WAIT(wait);
 	wait_queue_head_t *wqh = &congestion_wqh[sync];
 
 	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
 	ret = io_schedule_timeout(timeout);
 	finish_wait(wqh, &wait);
+
+	trace_writeback_congestion_wait(jiffies_to_usecs(timeout),
+					jiffies_to_usecs(jiffies - start));
+
 	return ret;
 }
 EXPORT_SYMBOL(congestion_wait);
Signature · 7 lines
1.7.1

--
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>
author
#3 lore raw

PATCH 3/10 writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

In reply to Mel Gorman

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

---
 include/linux/backing-dev.h      |    2 +-
 include/trace/events/writeback.h |    7 ++++
 mm/backing-dev.c                 |   66 ++++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c                  |    4 +-
 mm/vmscan.c                      |   26 ++++++++++++--
 5 files changed, 96 insertions(+), 9 deletions(-)
include/linux/backing-dev.h +1 -1
index 35b0074..f1b402a 100644
@@ -285,7 +285,7 @@ enum {
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
 void set_bdi_congested(struct backing_dev_info *bdi, int sync);
 long congestion_wait(int sync, long timeout);
-
+long wait_iff_congested(struct zone *zone, int sync, long timeout);
 
 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
 {
include/trace/events/writeback.h +7 -0
index 275d477..eeaf1f5 100644
@@ -181,6 +181,13 @@ DEFINE_EVENT(writeback_congest_waited_template, writeback_congestion_wait,
 	TP_ARGS(usec_timeout, usec_delayed)
 );
 
+DEFINE_EVENT(writeback_congest_waited_template, writeback_wait_iff_congested,
+
+	TP_PROTO(unsigned int usec_timeout, unsigned int usec_delayed),
+
+	TP_ARGS(usec_timeout, usec_delayed)
+);
+
 #endif /* _TRACE_WRITEBACK_H */
 
 /* This part must be outside protection */
mm/backing-dev.c +64 -2

87 lines not on this page · raw message

mm/page_alloc.c +2 -2
index a9649f4..641900a 100644
@@ -1893,7 +1893,7 @@ __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
 			preferred_zone, migratetype);
 
 		if (!page && gfp_mask & __GFP_NOFAIL)
-			congestion_wait(BLK_RW_ASYNC, HZ/50);
+			wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
 	} while (!page && (gfp_mask & __GFP_NOFAIL));
 
 	return page;
@@ -2081,7 +2081,7 @@ rebalance:
 	pages_reclaimed += did_some_progress;
 	if (should_alloc_retry(gfp_mask, order, pages_reclaimed)) {
 		/* Wait for some write requests to complete then retry */
-		congestion_wait(BLK_RW_ASYNC, HZ/50);
+		wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
 		goto rebalance;
 	}
 
mm/vmscan.c +22 -4
index 652650f..eabe987 100644
@@ -1341,7 +1341,7 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
-		congestion_wait(BLK_RW_ASYNC, HZ/10);
+		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
 
 		/*
 		 * The attempt at page out may have made some
@@ -1913,10 +1913,28 @@ static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
 			sc->may_writepage = 1;
 		}
 
-		/* Take a nap, wait for some writeback to complete */
+		/* Take a nap if congested, wait for some writeback */
 		if (!sc->hibernation_mode && sc->nr_scanned &&
-		    priority < DEF_PRIORITY - 2)
-			congestion_wait(BLK_RW_ASYNC, HZ/10);
+		    priority < DEF_PRIORITY - 2) {
+			struct zone *active_zone = NULL;
+			unsigned long max_writeback = 0;
+			for_each_zone_zonelist(zone, z, zonelist,
+					gfp_zone(sc->gfp_mask)) {
+				unsigned long writeback;
+
+				/* Initialise for first zone */
+				if (active_zone == NULL)
+					active_zone = zone;
+
+				writeback = zone_page_state(zone, NR_WRITEBACK);
+				if (writeback > max_writeback) {
+					max_writeback = writeback;
+					active_zone = zone;
+				}
+			}
+
+			wait_iff_congested(active_zone, BLK_RW_ASYNC, HZ/10);
+		}
 	}
 
 out:
Signature · 7 lines
1.7.1

--
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

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 88 lines · Mel Gorman

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

---
 include/linux/backing-dev.h      |    2 +-
 include/trace/events/writeback.h |    7 ++++
 mm/backing-dev.c                 |   66 ++++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c                  |    4 +-
 mm/vmscan.c                      |   26 ++++++++++++--
 5 files changed, 96 insertions(+), 9 deletions(-)
include/linux/backing-dev.h +1 -1
index 35b0074..f1b402a 100644
@@ -285,7 +285,7 @@ enum {
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
 void set_bdi_congested(struct backing_dev_info *bdi, int sync);
 long congestion_wait(int sync, long timeout);
-
+long wait_iff_congested(struct zone *zone, int sync, long timeout);
 
 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
 {
include/trace/events/writeback.h +7 -0
index 275d477..eeaf1f5 100644
@@ -181,6 +181,13 @@ DEFINE_EVENT(writeback_congest_waited_template, writeback_congestion_wait,
 	TP_ARGS(usec_timeout, usec_delayed)
 );
 
+DEFINE_EVENT(writeback_congest_waited_template, writeback_wait_iff_congested,
+
+	TP_PROTO(unsigned int usec_timeout, unsigned int usec_delayed),
+
+	TP_ARGS(usec_timeout, usec_delayed)
+);
+
 #endif /* _TRACE_WRITEBACK_H */
 
 /* This part must be outside protection */
mm/backing-dev.c +5 -2
index 298975a..94b5433 100644
@@ -724,6 +724,7 @@ static wait_queue_head_t congestion_wqh[2] = {
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
 	};
+static atomic_t nr_bdi_congested[2];
 
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 {
@@ -731,7 +732,8 @@ void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 	wait_queue_head_t *wqh = &congestion_wqh[sync];
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	clear_bit(bit, &bdi->state);
+	if (test_and_clear_bit(bit, &bdi->state))
+		atomic_dec(&nr_bdi_congested[sync]);
 	smp_mb__after_clear_bit();
 	if (waitqueue_active(wqh))
 		wake_up(wqh);
@@ -743,7 +745,8 @@ void set_bdi_congested(struct backing_dev_info *bdi, int sync)
 	enum bdi_state bit;
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	set_bit(bit, &bdi->state);
+	if (!test_and_set_bit(bit, &bdi->state))
+		atomic_inc(&nr_bdi_congested[sync]);
 }
 EXPORT_SYMBOL(set_bdi_congested);
 

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 781

Quoted 5 lines
mm/backing-dev.c +2 -0
@@ -774,3 +777,62 @@ long congestion_wait(int sync, long timeout)
 }
 EXPORT_SYMBOL(congestion_wait);
 
+/**
+ * congestion_wait - wait for a backing_dev to become uncongested

wait_iff_congested

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 789

Quoted 8 lines
mm/backing-dev.c +8 -0
+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion.  If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point

and

Quoted 16 lines
mm/backing-dev.c +16 -0
+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 10 lines
mm/backing-dev.c +10 -0
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

I am not sure this is best.

1. Without considering various speed class storage, could we fix it as half of inactive?
2. Isn't there any writeback throttling on above layer? Do we care of it in here?

Just out of curiosity.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
author
#5 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 16 lines · Minchan Kim
Quoted 15 lines · Mel Gorman

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

Quoted 7 lines
Quoted 5 lines

---
<SNIP>
+/**
+ * congestion_wait - wait for a backing_dev to become uncongested

wait_iff_congested

Fixed, thanks.

Quoted 10 lines
Quoted 8 lines

+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion. If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point

and

Why and? "or" makes sense because we avoid sleeping on either condition.

Quoted 20 lines
Quoted 20 lines

+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

mm/backing-dev.c +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 6 lines
mm/backing-dev.c +6 -0
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

I am not sure this is best.

I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

Quoted 1 line

1. Without considering various speed class storage, could we fix it as half of inactive?

We don't really have a good means of identifying speed classes of storage. Worse, we are considering on a zone-basis here, not a BDI basis. The pages being written back in the zone could be backed by anything so we cannot make decisions based on BDI speed.

Quoted 2 lines

2. Isn't there any writeback throttling on above layer? Do we care of it in here?

There are but congestion_wait() and now wait_iff_congested() are part of that. We can see from the figures in the leader that congestion_wait() is sleeping more than is necessary or smart.

Quoted 2 lines

Just out of curiosity.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 9 lines · Mel Gorman
Quoted 8 lines · Minchan Kim
Quoted 8 lines

+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion. If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point

Quoted 4 lines
Quoted 2 lines

and

Why and? "or" makes sense because we avoid sleeping on either condition.

if (nr_bdi_congested[sync]) == 0) {
        if (writeback < inactive / 2) {
                cond_resched();
                ..
                goto out
        }
}

for avoiding sleeping, above two condition should meet. So I thought "and" is make sense.
Am I missing something?

Quoted 38 lines
Quoted 21 lines
Quoted 21 lines

+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

mm/backing-dev.c +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 6 lines
mm/backing-dev.c +6 -0
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

I am not sure this is best.

I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

Quoted 3 lines

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Why I pointed out is that you added new rule and I hope let others know this change since they have a good idea or any opinions.
I think it's a one of roles as reviewer.

Quoted 7 lines
Quoted 2 lines

1. Without considering various speed class storage, could we fix it as half of inactive?

We don't really have a good means of identifying speed classes of storage. Worse, we are considering on a zone-basis here, not a BDI basis. The pages being written back in the zone could be backed by anything so we cannot make decisions based on BDI speed.

True. So it's why I have below question.
As you said, we don't have enough information in vmscan. So I am not sure how effective such semi-sensible decision is.

I think best is to throttle in page-writeback well.
But I am not a expert about that and don't have any idea. Sorry. So I can't insist on my nitpick. If others don't have any objection, I don't mind this, either.

Wu, Do you have any opinion?

Quoted 15 lines
Quoted 3 lines

2. Isn't there any writeback throttling on above layer? Do we care of it in here?

There are but congestion_wait() and now wait_iff_congested() are part of that. We can see from the figures in the leader that congestion_wait() is sleeping more than is necessary or smart.

Quoted 2 lines

Just out of curiosity.

Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
author
#7 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 23 lines · Minchan Kim
Quoted 9 lines · Mel Gorman
Quoted 8 lines · Minchan Kim
Quoted 8 lines

+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion. If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point

Quoted 4 lines
Quoted 2 lines

and

Why and? "or" makes sense because we avoid sleeping on either condition.

if (nr_bdi_congested[sync]) == 0) {
        if (writeback < inactive / 2) {
                cond_resched();
                ..
                goto out
        }
}
Quoted 1 line

for avoiding sleeping, above two condition should meet.

This is a terrible comment that is badly written. Is this any clearer?

/**
* wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a zone to complete writes
* @zone: A zone to consider the number of being being written back from
* @sync: SYNC or ASYNC IO
* @timeout: timeout in jiffies
*
* In the event of a congested backing_dev (any backing_dev) or a given @zone
* having a large number of pages in writeback, this waits for up to @timeout
* jiffies for either a BDI to exit congestion or a write to complete.
*
* If there is no congestion and few pending writes, then cond_resched()
* is called to yield the processor if necessary but otherwise does not
* sleep.
*/

Quoted 38 lines
Quoted 38 lines
Quoted 21 lines
Quoted 21 lines

+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

mm/backing-dev.c +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 6 lines
mm/backing-dev.c +6 -0
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

I am not sure this is best.

I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

Quoted 7 lines
Quoted 3 lines

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Yes, I'm trying to add a new rule about throttling in the page allocator and from vmscan. As you can see from the results in the leader, we are currently sleeping more than we need to.

Quoted 4 lines

Why I pointed out is that you added new rule and I hope let others know this change since they have a good idea or any opinions.
I think it's a one of roles as reviewer.

Of course.

Quoted 12 lines
Quoted 7 lines
Quoted 2 lines

1. Without considering various speed class storage, could we fix it as half of inactive?

We don't really have a good means of identifying speed classes of storage. Worse, we are considering on a zone-basis here, not a BDI basis. The pages being written back in the zone could be backed by anything so we cannot make decisions based on BDI speed.

True. So it's why I have below question.
As you said, we don't have enough information in vmscan. So I am not sure how effective such semi-sensible decision is.

What additional metrics would you apply than the ones I used in the leader mail?

Quoted 1 line

I think best is to throttle in page-writeback well.

I do not think there is a problem as such in page writeback throttling. The problem is that we are going to sleep without any congestion or without writes in progress. We sleep for a full timeout in this case for no reason and this is what I'm trying to avoid.

Quoted 1 line

But I am not a expert about that and don't have any idea. Sorry.

Don't be, this is something that needs thinking about!

Quoted 25 lines

So I can't insist on my nitpick. If others don't have any objection, I don't mind this, either.

Wu, Do you have any opinion?

Quoted 15 lines
Quoted 3 lines

2. Isn't there any writeback throttling on above layer? Do we care of it in here?

There are but congestion_wait() and now wait_iff_congested() are part of that. We can see from the figures in the leader that congestion_wait() is sleeping more than is necessary or smart.

Quoted 2 lines

Just out of curiosity.

Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
Signature · 2 lines
Kind regards,
Minchan Kim
Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
#8 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 28 lines · Mel Gorman
Quoted 23 lines · Minchan Kim
Quoted 9 lines · Mel Gorman
Quoted 8 lines · Minchan Kim
Quoted 8 lines

+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion. If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point

Quoted 4 lines
Quoted 2 lines

and

Why and? "or" makes sense because we avoid sleeping on either condition.

if (nr_bdi_congested[sync]) == 0) {
        if (writeback < inactive / 2) {
                cond_resched();
                ..
                goto out
        }
}
Quoted 1 line

for avoiding sleeping, above two condition should meet.

This is a terrible comment that is badly written. Is this any clearer?

Quoted 14 lines

/**
* wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a zone to complete writes
* @zone: A zone to consider the number of being being written back from
* @sync: SYNC or ASYNC IO
* @timeout: timeout in jiffies
*
* In the event of a congested backing_dev (any backing_dev) or a given @zone
* having a large number of pages in writeback, this waits for up to @timeout
* jiffies for either a BDI to exit congestion or a write to complete.
*
* If there is no congestion and few pending writes, then cond_resched()
* is called to yield the processor if necessary but otherwise does not
* sleep.
*/

Looks good.

Quoted 43 lines
Quoted 43 lines
Quoted 42 lines
Quoted 22 lines
Quoted 22 lines

+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

mm/backing-dev.c +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 6 lines
mm/backing-dev.c +6 -0
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

I am not sure this is best.

I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

Quoted 7 lines
Quoted 3 lines

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Yes, I'm trying to add a new rule about throttling in the page allocator and from vmscan. As you can see from the results in the leader, we are currently sleeping more than we need to.

I can see the about avoiding congestion_wait but can't find about (writeback < incative / 2) hueristic result.

Quoted 16 lines
Quoted 5 lines

Why I pointed out is that you added new rule and I hope let others know this change since they have a good idea or any opinions.
I think it's a one of roles as reviewer.

Of course.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

1. Without considering various speed class storage, could we fix it as half of inactive?

We don't really have a good means of identifying speed classes of storage. Worse, we are considering on a zone-basis here, not a BDI basis. The pages being written back in the zone could be backed by anything so we cannot make decisions based on BDI speed.

Quoted 7 lines
Quoted 4 lines

True. So it's why I have below question.
As you said, we don't have enough information in vmscan. So I am not sure how effective such semi-sensible decision is.

What additional metrics would you apply than the ones I used in the leader mail?

effectiveness of (writeback < inactive / 2) heuristic.

Quoted 7 lines
Quoted 2 lines

I think best is to throttle in page-writeback well.

I do not think there is a problem as such in page writeback throttling. The problem is that we are going to sleep without any congestion or without writes in progress. We sleep for a full timeout in this case for no reason and this is what I'm trying to avoid.

Yes. I agree.
Just my concern is heuristic accuarcy I mentioned. In your previous verstion, you don't add the heuristic. But suddenly you added it in this version.
So I think you have any clue to add it in this version. Please, write down cause and data if you have.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
author
#9 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 47 lines · Minchan Kim
Quoted 47 lines
Quoted 46 lines
Quoted 39 lines
Quoted 22 lines
Quoted 22 lines

<SNIP>

+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

mm/backing-dev.c +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 6 lines
mm/backing-dev.c +6 -0
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

I am not sure this is best.

I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

Quoted 3 lines

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Quoted 7 lines
Quoted 3 lines

Yes, I'm trying to add a new rule about throttling in the page allocator and from vmscan. As you can see from the results in the leader, we are currently sleeping more than we need to.

I can see the about avoiding congestion_wait but can't find about (writeback < incative / 2) hueristic result.

See the leader and each of the report sections entitled "FTrace Reclaim Statistics: congestion_wait". It provides a measure of how sleep times are affected.

"congest waited" are waits due to calling congestion_wait. "conditional waited" are those related to wait_iff_congested(). As you will see from the reports, sleep times are reduced overall while callers of wait_iff_congested() still go to sleep. The reports entitled "FTrace Reclaim Statistics: vmscan" show how reclaim is behaving and indicators so far are that reclaim is not hurt by introducing wait_iff_congested().

Quoted 21 lines
Quoted 21 lines
Quoted 5 lines

Why I pointed out is that you added new rule and I hope let others know this change since they have a good idea or any opinions.
I think it's a one of roles as reviewer.

Of course.

Quoted 12 lines
Quoted 7 lines
Quoted 2 lines

1. Without considering various speed class storage, could we fix it as half of inactive?

We don't really have a good means of identifying speed classes of storage. Worse, we are considering on a zone-basis here, not a BDI basis. The pages being written back in the zone could be backed by anything so we cannot make decisions based on BDI speed.

True. So it's why I have below question.
As you said, we don't have enough information in vmscan. So I am not sure how effective such semi-sensible decision is.

Quoted 5 lines
Quoted 2 lines

What additional metrics would you apply than the ones I used in the leader mail?

effectiveness of (writeback < inactive / 2) heuristic.

Define effectiveness.

In the reports I gave, I reported on the sleep times and whether the full timeout was slept or not. Sleep times are reduced while not negatively impacting reclaim.

Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

I think best is to throttle in page-writeback well.

I do not think there is a problem as such in page writeback throttling. The problem is that we are going to sleep without any congestion or without writes in progress. We sleep for a full timeout in this case for no reason and this is what I'm trying to avoid.

Yes. I agree.
Just my concern is heuristic accuarcy I mentioned. In your previous verstion, you don't add the heuristic.

In the previous version, I also changed all callers to congestion_wait(). V1 simply was not that great a patch and Johannes pointed out that I wasn't measuring the scanning/reclaim ratios to see how reclaim was impacted. The reports now include this data and things are looking better.

Quoted 4 lines

But suddenly you added it in this version.
So I think you have any clue to add it in this version. Please, write down cause and data if you have.

The leader has a large amount of data on how this and the other patches affected results for a good variety of workloads.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 45 lines · Mel Gorman
Quoted 44 lines · Minchan Kim
Quoted 44 lines
Quoted 44 lines
Quoted 39 lines
Quoted 22 lines
Quoted 22 lines

<SNIP>

+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

(quoted diff) +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+   long ret;
+   unsigned long start = jiffies;
+   DEFINE_WAIT(wait);
+   wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+   /*
+    * If there is no congestion, check the amount of writeback. If there
+    * is no significant writeback and no congestion, just cond_resched
+    */
+   if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+           unsigned long inactive, writeback;
+
+           inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+                           zone_page_state(zone, NR_INACTIVE_ANON);
+           writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines
Quoted 6 lines

+
+           /*
+            * If less than half the inactive list is being written back,
+            * reclaim might as well continue
+            */
+           if (writeback < inactive / 2) {

I am not sure this is best.

I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

Quoted 3 lines

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time

author
#11 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 35 lines · Minchan Kim
Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 11 lines

<SNIP>
I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Quoted 7 lines
Quoted 3 lines

Yes, I'm trying to add a new rule about throttling in the page allocator and from vmscan. As you can see from the results in the leader, we are currently sleeping more than we need to.

I can see the about avoiding congestion_wait but can't find about (writeback < incative / 2) hueristic result.

See the leader and each of the report sections entitled "FTrace Reclaim Statistics: congestion_wait". It provides a measure of how sleep times are affected.

Quoted 6 lines

"congest waited" are waits due to calling congestion_wait. "conditional waited" are those related to wait_iff_congested(). As you will see from the reports, sleep times are reduced overall while callers of wait_iff_congested() still go to sleep. The reports entitled "FTrace Reclaim Statistics: vmscan" show how reclaim is behaving and indicators so far are that reclaim is not hurt by introducing wait_iff_congested().

Quoted 4 lines

I saw the result.
It was a result about effectiveness _both_ nr_bdi_congested and (writeback < inactive/2).
What I mean is just effectiveness (writeback < inactive/2) _alone_.

I didn't measured it because such a change means that wait_iff_congested() ignored BDI congestion. If we were reclaiming on a NUMA machine for example, it could mean that a BDI gets flooded with requests if we only checked the ratios of one zone if little writeback was happening in that zone at the time. It did not seem like a good idea to ignore congestion.

Quoted 3 lines

If we remove (writeback < inactive / 2) check and unconditionally return, how does the behavior changed?

Based on just the workload Johannes sent, scanning and completion times both increased without any improvement in the scanning/reclaim ratio (a bad result) hence why this logic was introduced to back off where there is some writeback taking place even if the BDI is not congested.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 41 lines · Mel Gorman
Quoted 35 lines · Minchan Kim
Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 11 lines

<SNIP>
I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Quoted 7 lines
Quoted 3 lines

Yes, I'm trying to add a new rule about throttling in the page allocator and from vmscan. As you can see from the results in the leader, we are currently sleeping more than we need to.

I can see the about avoiding congestion_wait but can't find about (writeback < incative / 2) hueristic result.

See the leader and each of the report sections entitled "FTrace Reclaim Statistics: congestion_wait". It provides a measure of how sleep times are affected.

Quoted 6 lines

"congest waited" are waits due to calling congestion_wait. "conditional waited" are those related to wait_iff_congested(). As you will see from the reports, sleep times are reduced overall while callers of wait_iff_congested() still go to sleep. The reports entitled "FTrace Reclaim Statistics: vmscan" show how reclaim is behaving and indicators so far are that reclaim is not hurt by introducing wait_iff_congested().

Quoted 4 lines

I saw  the result.
It was a result about effectiveness _both_ nr_bdi_congested and (writeback < inactive/2).
What I mean is just effectiveness (writeback < inactive/2) _alone_.

Quoted 5 lines

I didn't measured it because such a change means that wait_iff_congested() ignored BDI congestion. If we were reclaiming on a NUMA machine for example, it could mean that a BDI gets flooded with requests if we only checked the ratios of one zone if little writeback was happening in that zone at the time. It did not seem like a good idea to ignore congestion.

You seem to misunderstand my word.
Sorry for not clear sentence.

I don't mean ignore congestion.
First of all, we should consider congestion of bdi.
My meant is whether we need adding up (nr_writeback < nr_inacive /2) heuristic plus congestion bdi.
It wasn't previous version in your patch but it showed up in this version. So I thought apparently you have any evidence why we should add such heuristic.

Quoted 9 lines
Quoted 4 lines

If we remove (writeback < inactive / 2) check and unconditionally return, how does the behavior changed?

Based on just the workload Johannes sent, scanning and completion times both increased without any improvement in the scanning/reclaim ratio (a bad result) hence why this logic was introduced to back off where there is some writeback taking place even if the BDI is not congested.

Yes. That's what I want. At least, comment of function should have it to understand the logic. In addition, It would be better to add the number to show how it back off well.

Quoted 6 lines

--
Mel Gorman
Part-time Phd Student                          Linux Technology Center University of Limerick                         IBM Dublin Software Lab

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
author
#13 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 48 lines · Minchan Kim
Quoted 41 lines · Mel Gorman
Quoted 35 lines · Minchan Kim
Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 11 lines

<SNIP>
I'm not saying it is. The objective is to identify a situation where sleeping until the next write or congestion clears is pointless. We have already identified that we are not congested so the question is "are we writing a lot at the moment?". The assumption is that if there is a lot of writing going on, we might as well sleep until one completes rather than reclaiming more.

This is the first effort at identifying pointless sleeps. Better ones might be identified in the future but that shouldn't stop us making a semi-sensible decision now.

nr_bdi_congested is no problem since we have used it for a long time. But you added new rule about writeback.

Quoted 7 lines
Quoted 3 lines

Yes, I'm trying to add a new rule about throttling in the page allocator and from vmscan. As you can see from the results in the leader, we are currently sleeping more than we need to.

I can see the about avoiding congestion_wait but can't find about (writeback < incative / 2) hueristic result.

See the leader and each of the report sections entitled "FTrace Reclaim Statistics: congestion_wait". It provides a measure of how sleep times are affected.

Quoted 6 lines

"congest waited" are waits due to calling congestion_wait. "conditional waited" are those related to wait_iff_congested(). As you will see from the reports, sleep times are reduced overall while callers of wait_iff_congested() still go to sleep. The reports entitled "FTrace Reclaim Statistics: vmscan" show how reclaim is behaving and indicators so far are that reclaim is not hurt by introducing wait_iff_congested().

Quoted 4 lines

I saw  the result.
It was a result about effectiveness _both_ nr_bdi_congested and (writeback < inactive/2).
What I mean is just effectiveness (writeback < inactive/2) _alone_.

Quoted 5 lines

I didn't measured it because such a change means that wait_iff_congested() ignored BDI congestion. If we were reclaiming on a NUMA machine for example, it could mean that a BDI gets flooded with requests if we only checked the ratios of one zone if little writeback was happening in that zone at the time. It did not seem like a good idea to ignore congestion.

Quoted 7 lines

You seem to misunderstand my word.
Sorry for not clear sentence.

I don't mean ignore congestion.
First of all, we should consider congestion of bdi.
My meant is whether we need adding up (nr_writeback < nr_inacive /2) heuristic plus congestion bdi.

Early tests indicated "yes".

Quoted 3 lines

It wasn't previous version in your patch but it showed up in this version. So I thought apparently you have any evidence why we should add such heuristic.

Only the feedback from the first patch where Johannes posted a workload that did exhibit a problem. Isolated tests on just that workload led to the (nr_writeback < inactive / 2) change.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

If we remove (writeback < inactive / 2) check and unconditionally return, how does the behavior changed?

Quoted 9 lines
Quoted 4 lines

Based on just the workload Johannes sent, scanning and completion times both increased without any improvement in the scanning/reclaim ratio (a bad result) hence why this logic was introduced to back off where there is some writeback taking place even if the BDI is not congested.

Yes. That's what I want. At least, comment of function should have it to understand the logic. In addition, It would be better to add the number to show how it back off well.

Very well. I'll hold off posting v2 of the series now then because producing such results take many hours and my machines are currently busy. Hopefully I'll have something by Wednesday.

Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
maintainer
#14 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

In reply to Mel Gorman
Quoted 7 lines · On Mon, 6 Sep 2010 11:47:26 +0100 Mel Gorman <[email protected]> wrote:

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

Quoted 8 lines

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

The patch series looks generally good. Would like to see some testing results ;) A few touchups are planned so I'll await v2.

mm/backing-dev.c static wait_queue_head_t congestion_wqh[2] = { · line 727

Quoted 7 lines
mm/backing-dev.c +1 -0
@@ -724,6 +724,7 @@ static wait_queue_head_t congestion_wqh[2] = {
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
 	};
+static atomic_t nr_bdi_congested[2];

Let's remember that a queue can get congested because of reads as well as writes. It's very rare for this to happen - it needs either a zillion read()ing threads or someone going berzerk with O_DIRECT aio, etc. Probably it doesn't matter much, but for memory reclaim purposes read-congestion is somewhat irrelevant and a bit of thought is warranted.

vmscan currently only looks at *write* congestion, but in this patch you secretly change that logic to newly look at write-or-read congestion. Talk to me.

mm/backing-dev.c void clear_bdi_congested(struct backing_dev_info *bdi, int sync) · line 739

Quoted 12 lines
mm/backing-dev.c +2 -1
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 {
@@ -731,7 +732,8 @@ void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 	wait_queue_head_t *wqh = &congestion_wqh[sync];
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	clear_bit(bit, &bdi->state);
+	if (test_and_clear_bit(bit, &bdi->state))
+		atomic_dec(&nr_bdi_congested[sync]);
 	smp_mb__after_clear_bit();
 	if (waitqueue_active(wqh))
 		wake_up(wqh);

Worried. Having a single slow disk getting itself gummed up will affect the entire machine!

There's potential for pathological corner-case problems here. "When I do a big aio read from /dev/MySuckyUsbStick, all my CPUs get pegged in page reclaim!".

What to do?

Of course, we'd very much prefer to know whether a queue which we're interested in for writeback will block when we try to write to it. Much better than looking at all queues.

Important question: which of teh current congestion_wait() call sites are causing appreciable stalls?

I think a more accurate way of implementing this is to be smarter with the may_write_to_queue()->bdi_write_congested() result. If a previous attempt to write off this LRU encountered congestion then fine, call congestion_wait(). But if writeback is not hitting may_write_to_queue()->bdi_write_congested() then that is the time to avoid calling congestion_wait().

In other words, save the bdi_write_congested() result in the zone struct in some fashion and inspect that before deciding to synchronize behind the underlying device's write rate. Not hitting a congested device for this LRU? Then don't wait for congested devices.

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 782

Quoted 7 lines
mm/backing-dev.c +3 -0
@@ -774,3 +777,62 @@ long congestion_wait(int sync, long timeout)
 }
 EXPORT_SYMBOL(congestion_wait);
 
+/**
+ * congestion_wait - wait for a backing_dev to become uncongested
+ * @zone: A zone to consider the number of being being written back from

That comments needs help.

mm/backing-dev.c long congestion_wait(int sync, long timeout)

Quoted 5 lines
mm/backing-dev.c +5 -0
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion.'

write or read congestion!!

Quoted 6 lines

If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point
+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

Document the return value?

Quoted 13 lines
mm/backing-dev.c +13 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 10 lines
mm/backing-dev.c +10 -0
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

This is all getting seriously inaccurate :(

Quoted 9 lines
mm/backing-dev.c +9 -0
+			cond_resched();
+
+			/* In case we scheduled, work out time remaining */
+			ret = timeout - (jiffies - start);
+			if (ret < 0)
+				ret = 0;
+
+			goto out;
+		}

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 834

Quoted 10 lines
mm/backing-dev.c +10 -0
+	}
+
+	/* Sleep until uncongested or a write happens */
+	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
+	ret = io_schedule_timeout(timeout);
+	finish_wait(wqh, &wait);
+
+out:
+	trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout),
+					jiffies_to_usecs(jiffies - start));

Does this tracepoint tell us how often wait_iff_congested() is sleeping versus how often it is returning immediately?

Quoted 25 lines
mm/backing-dev.c +14 -3
+	return ret;
+}
+EXPORT_SYMBOL(wait_iff_congested);
 
...
@@ -1913,10 +1913,28 @@ static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
 			sc->may_writepage = 1;
 		}
 
-		/* Take a nap, wait for some writeback to complete */
+		/* Take a nap if congested, wait for some writeback */
 		if (!sc->hibernation_mode && sc->nr_scanned &&
-		    priority < DEF_PRIORITY - 2)
-			congestion_wait(BLK_RW_ASYNC, HZ/10);
+		    priority < DEF_PRIORITY - 2) {
+			struct zone *active_zone = NULL;
+			unsigned long max_writeback = 0;
+			for_each_zone_zonelist(zone, z, zonelist,
+					gfp_zone(sc->gfp_mask)) {
+				unsigned long writeback;
+
+				/* Initialise for first zone */
+				if (active_zone == NULL)
+					active_zone = zone;

mm/backing-dev.c static unsigned long do_try_to_free_pages(struct zonelist *zonelist, · line 1937

Quoted 10 lines
mm/backing-dev.c +10 -0
+
+				writeback = zone_page_state(zone, NR_WRITEBACK);
+				if (writeback > max_writeback) {
+					max_writeback = writeback;
+					active_zone = zone;
+				}
+			}
+
+			wait_iff_congested(active_zone, BLK_RW_ASYNC, HZ/10);
+		}

Again, we would benefit from more accuracy here. In my above suggestion I'm assuming that the (congestion) result of the most recent attempt to perform writeback is a predictor of the next attempt.

Doing that on a kernel-wide basis would be rather inaccurate on large machines in some scenarios. Storing the state info in the zone would help.

--
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>

author
#15 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 19 lines · Andrew Morton
Quoted 7 lines · On Mon, 6 Sep 2010 11:47:26 +0100 Mel Gorman <[email protected]> wrote:

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

Quoted 8 lines

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

Quoted 2 lines

The patch series looks generally good. Would like to see some testing results ;)

They are all in the leader. They are all based on a test-suite that I'm bound to stick a README on and release one of these days :/

Quoted 2 lines

A few touchups are planned so I'll await v2.

Good plan.

Quoted 5 lines
Quoted 5 lines
mm/backing-dev.c +0 -0
@@ -724,6 +724,7 @@ static wait_queue_head_t congestion_wqh[2] = {
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
Quoted 9 lines

mm/backing-dev.c static wait_queue_head_t congestion_wqh[2] = { · line 727

Quoted 2 lines
mm/backing-dev.c +1 -0
 	};
+static atomic_t nr_bdi_congested[2];

Let's remember that a queue can get congested because of reads as well as writes. It's very rare for this to happen - it needs either a zillion read()ing threads or someone going berzerk with O_DIRECT aio, etc. Probably it doesn't matter much, but for memory reclaim purposes read-congestion is somewhat irrelevant and a bit of thought is warranted.

This is an interesting point and would be well worth digging into if we got a new bug report about stalls under heavy reads.

Quoted 4 lines

vmscan currently only looks at *write* congestion, but in this patch you secretly change that logic to newly look at write-or-read congestion. Talk to me.

vmscan currently only looks at write congestion because it's checking the BLK_RW_ASYNC and all reads will be BLK_RW_SYNC. Currently, this is why we are only looking at write congestion even though it's approximate, right?

Remember, congestion_wait used to be about READ and WRITE but now it's about SYNC and ASYNC.

In the patch, there are separate SYNC and ASYNC nr_bdi_congested counters. wait_iff_congested() is only called for BLK_RW_ASYNC so we still checking write congestion only.

What stupid thing did I miss?

Quoted 5 lines
Quoted 5 lines
mm/backing-dev.c +0 -0
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 {
@@ -731,7 +732,8 @@ void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 	wait_queue_head_t *wqh = &congestion_wqh[sync];
 
Quoted 11 lines

mm/backing-dev.c void clear_bdi_congested(struct backing_dev_info *bdi, int sync) · line 739

Quoted 7 lines
mm/backing-dev.c +2 -1
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	clear_bit(bit, &bdi->state);
+	if (test_and_clear_bit(bit, &bdi->state))
+		atomic_dec(&nr_bdi_congested[sync]);
 	smp_mb__after_clear_bit();
 	if (waitqueue_active(wqh))
 		wake_up(wqh);

Worried. Having a single slow disk getting itself gummed up will affect the entire machine!

This can already happen today. In fact, I think it's one of the sources of desktop stalls during IO from https://bugzilla.kernel.org/show_bug.cgi?id=12309
that you brought up a few weeks back. I was tempted to try resolve it in this patch but thought I was reaching far enough with this series as it was.

Quoted 4 lines

There's potential for pathological corner-case problems here. "When I do a big aio read from /dev/MySuckyUsbStick, all my CPUs get pegged in page reclaim!".

I thought it might be enough to just do a huge backup to an external USB drive. I guess I could make it worse by starting up one copy per CPU thread preferably to more than one slow USB device.

Quoted 6 lines

What to do?

Of course, we'd very much prefer to know whether a queue which we're interested in for writeback will block when we try to write to it. Much better than looking at all queues.

And somehow reconciling the queue being written to with the zone the pages are coming from.

Quoted 3 lines

Important question: which of teh current congestion_wait() call sites are causing appreciable stalls?

This potentially can be found out from the tracepoints if they record the stack trace as well. In this patch, I avoided changing all callers to congestion_wait() and changed a few callers to wait_iff_congested() instead to limit the scope of what was being changed in this cycle.

Quoted 7 lines

I think a more accurate way of implementing this is to be smarter with the may_write_to_queue()->bdi_write_congested() result. If a previous attempt to write off this LRU encountered congestion then fine, call congestion_wait(). But if writeback is not hitting may_write_to_queue()->bdi_write_congested() then that is the time to avoid calling congestion_wait().

I see the logic. If we assume that there is large amounts of anon page reclaim while writeback is happening to a USB device for example, we would avoid a stall in this case. It would still encounter a problem if all the reclaim is from the file LRU and there are a few pages being written to a USB stick. We'll still wait on congestion even though it might not have been necessary and it's why I was counting the number of writeback pages versus the size of the inactive queue and making a decision based on that.

Quoted 5 lines

In other words, save the bdi_write_congested() result in the zone struct in some fashion and inspect that before deciding to synchronize behind the underlying device's write rate. Not hitting a congested device for this LRU? Then don't wait for congested devices.

I think the idea has potential. It will take a fair amount of time to work out the details though. Testing tends to take a *long* time even with automation.

Quoted 10 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 782

Quoted 7 lines
mm/backing-dev.c +3 -0
@@ -774,3 +777,62 @@ long congestion_wait(int sync, long timeout)
 }
 EXPORT_SYMBOL(congestion_wait);
 
+/**
+ * congestion_wait - wait for a backing_dev to become uncongested
+ * @zone: A zone to consider the number of being being written back from

That comments needs help.

Indeed it does. It currently stands as

/**
* wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a zone to complete writes
* @zone: A zone to consider the number of being being written back from
* @sync: SYNC or ASYNC IO
* @timeout: timeout in jiffies
*
* In the event of a congested backing_dev (any backing_dev) or a given zone
* having a large number of pages in writeback, this waits for up to @timeout
* jiffies for either a BDI to exit congestion of the given @sync queue.
*
* If there is no congestion and few pending writes, then cond_resched()
* is called to yield the processor if necessary but otherwise does not
* sleep.

* The return value is 0 if the sleep is for the full timeout. Otherwise,
* it is the number of jiffies that were still remaining when the function
* returned. return_value == timeout implies the function did not sleep. */

Quoted 8 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout)

Quoted 5 lines
mm/backing-dev.c +5 -0
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion.'

write or read congestion!!

I just know I'm going to spot where we wait on read congestion the second I push send and make a fool of myself :(

Quoted 9 lines
Quoted 6 lines

If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point
+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */

Document the return value?

What's the fun in that? :)

I included a blurb on the return value in the updated comment above.

Quoted 17 lines
Quoted 17 lines
mm/backing-dev.c +17 -0
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 6 lines
mm/backing-dev.c +6 -0
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

This is all getting seriously inaccurate :(

We are already woefully inaccurate.

The intention here is to catch where we are not congested but that there is sufficient writeback in the zone to make it worthwhile waiting for some of it to complete. Minimally, we have a reasonable expectation that if writeback is happening that we'll be woken up if we go to sleep on the congestion queue.

i.e. it's not great but it's better than what we have at the moment which can be seen from the micro-mapped-file-stream results in the leader. Time to completion is reduced, sleepy time is reduced while the ratio of scans/writes does not get worse.

Quoted 14 lines
Quoted 14 lines
mm/backing-dev.c +14 -0
+			cond_resched();
+
+			/* In case we scheduled, work out time remaining */
+			ret = timeout - (jiffies - start);
+			if (ret < 0)
+				ret = 0;
+
+			goto out;
+		}
+	}
+
+	/* Sleep until uncongested or a write happens */
+	prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
+	ret = io_schedule_timeout(timeout);
Quoted 9 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 834

Quoted 5 lines
mm/backing-dev.c +5 -0
+	finish_wait(wqh, &wait);
+
+out:
+	trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout),
+					jiffies_to_usecs(jiffies - start));

Does this tracepoint tell us how often wait_iff_congested() is sleeping versus how often it is returning immediately?

Yes. Taking an example from the leader

FTrace Reclaim Statistics: congestion_wait
traceonly-v1r5 nocongest-v1r5 lowlumpy-v1r5 nodirect-v1r5

Direct number congest     waited               499          0          0          0
Direct time   congest     waited           22700ms        0ms        0ms        0ms
Direct full   congest     waited               421          0          0          0
Direct number conditional waited                 0       1214       1242       1290
Direct time   conditional waited               0ms        4ms        0ms        0ms
Direct full   conditional waited               421          0          0          0
KSwapd number congest     waited               257        103         94        104
KSwapd time   congest     waited           22116ms     7344ms     7476ms     7528ms
KSwapd full   congest     waited               203         57         59         56
KSwapd number conditional waited                 0          0          0          0
KSwapd time   conditional waited               0ms        0ms        0ms        0ms
KSwapd full   conditional waited               203         57         59         56

A "full congest waited" is a count of the number of times we slept for more than the timeout. The trace-only kernel reports that direct reclaimers slept the full timeout 421 times and kswapd slept for the full timeout 203 times. The patch (nocongest-v1r5) reduces these counts significantly.

The report is from a script that reads ftrace information. It's similar in operation to what's in Documentation/trace/postprocess/.

Quoted 31 lines
Quoted 31 lines
mm/backing-dev.c +20 -3
+	return ret;
+}
+EXPORT_SYMBOL(wait_iff_congested);
 
...
@@ -1913,10 +1913,28 @@ static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
 			sc->may_writepage = 1;
 		}
 
-		/* Take a nap, wait for some writeback to complete */
+		/* Take a nap if congested, wait for some writeback */
 		if (!sc->hibernation_mode && sc->nr_scanned &&
-		    priority < DEF_PRIORITY - 2)
-			congestion_wait(BLK_RW_ASYNC, HZ/10);
+		    priority < DEF_PRIORITY - 2) {
+			struct zone *active_zone = NULL;
+			unsigned long max_writeback = 0;
+			for_each_zone_zonelist(zone, z, zonelist,
+					gfp_zone(sc->gfp_mask)) {
+				unsigned long writeback;
+
+				/* Initialise for first zone */
+				if (active_zone == NULL)
+					active_zone = zone;
+
+				writeback = zone_page_state(zone, NR_WRITEBACK);
+				if (writeback > max_writeback) {
+					max_writeback = writeback;
+					active_zone = zone;
+				}
Quoted 9 lines

mm/backing-dev.c static unsigned long do_try_to_free_pages(struct zonelist *zonelist, · line 1937

Quoted 4 lines
mm/backing-dev.c +4 -0
+			}
+
+			wait_iff_congested(active_zone, BLK_RW_ASYNC, HZ/10);
+		}

Again, we would benefit from more accuracy here. In my above suggestion I'm assuming that the (congestion) result of the most recent attempt to perform writeback is a predictor of the next attempt.

I suspect you are on to something but it will take me some time to work out the details and to build a setup involving a few USB sticks to trigger that test case. What are the possibilities of starting with this heuristic (in release v2 or v3 of this series) because it improves on what we have today and then trying out different ideas for how and when to call wait_iff_congested() in the next cycle?

Quoted 4 lines

Doing that on a kernel-wide basis would be rather inaccurate on large machines in some scenarios. Storing the state info in the zone would help.

We are already depending on kernel-wide inaccuracy. The series aims to chip away at some of the obvious badness to start with.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

In reply to Mel Gorman
Quoted 117 lines · On Mon, 6 Sep 2010 11:47:26 +0100 Mel Gorman <[email protected]> wrote:

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

---
 include/linux/backing-dev.h      |    2 +-
 include/trace/events/writeback.h |    7 ++++
 mm/backing-dev.c                 |   66 ++++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c                  |    4 +-
 mm/vmscan.c                      |   26 ++++++++++++--
 5 files changed, 96 insertions(+), 9 deletions(-)
include/linux/backing-dev.h +1 -1
index 35b0074..f1b402a 100644
@@ -285,7 +285,7 @@ enum {
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
 void set_bdi_congested(struct backing_dev_info *bdi, int sync);
 long congestion_wait(int sync, long timeout);
-
+long wait_iff_congested(struct zone *zone, int sync, long timeout);
 
 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
 {
include/trace/events/writeback.h +7 -0
index 275d477..eeaf1f5 100644
@@ -181,6 +181,13 @@ DEFINE_EVENT(writeback_congest_waited_template, writeback_congestion_wait,
 	TP_ARGS(usec_timeout, usec_delayed)
 );
 
+DEFINE_EVENT(writeback_congest_waited_template, writeback_wait_iff_congested,
+
+	TP_PROTO(unsigned int usec_timeout, unsigned int usec_delayed),
+
+	TP_ARGS(usec_timeout, usec_delayed)
+);
+
 #endif /* _TRACE_WRITEBACK_H */
 
 /* This part must be outside protection */
mm/backing-dev.c +31 -2
index 298975a..94b5433 100644
@@ -724,6 +724,7 @@ static wait_queue_head_t congestion_wqh[2] = {
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
 	};
+static atomic_t nr_bdi_congested[2];
 
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 {
@@ -731,7 +732,8 @@ void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 	wait_queue_head_t *wqh = &congestion_wqh[sync];
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	clear_bit(bit, &bdi->state);
+	if (test_and_clear_bit(bit, &bdi->state))
+		atomic_dec(&nr_bdi_congested[sync]);
 	smp_mb__after_clear_bit();
 	if (waitqueue_active(wqh))
 		wake_up(wqh);
@@ -743,7 +745,8 @@ void set_bdi_congested(struct backing_dev_info *bdi, int sync)
 	enum bdi_state bit;
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	set_bit(bit, &bdi->state);
+	if (!test_and_set_bit(bit, &bdi->state))
+		atomic_inc(&nr_bdi_congested[sync]);
 }
 EXPORT_SYMBOL(set_bdi_congested);
 
@@ -774,3 +777,62 @@ long congestion_wait(int sync, long timeout)
 }
 EXPORT_SYMBOL(congestion_wait);
 
+/**
+ * congestion_wait - wait for a backing_dev to become uncongested
+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion.  If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point
+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 10 lines
mm/backing-dev.c +10 -0
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
+
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

Hmm..can't we have a way that "find a page which can be just dropped without writeback" rather than sleeping ? I think we can throttole the number of victims for avoidng I/O congestion as pages/tick....if exhausted, ok, we should sleep.

Thanks,
-Kame

--
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>

author
#17 lore raw

Re: [PATCH 03/10] writeback: Do not congestion sleep if there are no congested BDIs or significant writeback

Quoted 125 lines · KAMEZAWA Hiroyuki
Quoted 122 lines · On Mon, 6 Sep 2010 11:47:26 +0100 Mel Gorman <[email protected]> wrote:

If congestion_wait() is called with no BDIs congested, the caller will sleep for the full timeout and this may be an unnecessary sleep. This patch adds a wait_iff_congested() that checks congestion and only sleeps if a BDI is congested or if there is a significant amount of writeback going on in an interesting zone. Else, it calls cond_resched() to ensure the caller is not hogging the CPU longer than its quota but otherwise will not sleep.

This is aimed at reducing some of the major desktop stalls reported during IO. For example, while kswapd is operating, it calls congestion_wait() but it could just have been reclaiming clean page cache pages with no congestion. Without this patch, it would sleep for a full timeout but after this patch, it'll just call schedule() if it has been on the CPU too long. Similar logic applies to direct reclaimers that are not making enough progress.

---
 include/linux/backing-dev.h      |    2 +-
 include/trace/events/writeback.h |    7 ++++
 mm/backing-dev.c                 |   66 ++++++++++++++++++++++++++++++++++++-
 mm/page_alloc.c                  |    4 +-
 mm/vmscan.c                      |   26 ++++++++++++--
 5 files changed, 96 insertions(+), 9 deletions(-)
include/linux/backing-dev.h +1 -1
index 35b0074..f1b402a 100644
@@ -285,7 +285,7 @@ enum {
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync);
 void set_bdi_congested(struct backing_dev_info *bdi, int sync);
 long congestion_wait(int sync, long timeout);
-
+long wait_iff_congested(struct zone *zone, int sync, long timeout);
 
 static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
 {
include/trace/events/writeback.h +7 -0
index 275d477..eeaf1f5 100644
@@ -181,6 +181,13 @@ DEFINE_EVENT(writeback_congest_waited_template, writeback_congestion_wait,
 	TP_ARGS(usec_timeout, usec_delayed)
 );
 
+DEFINE_EVENT(writeback_congest_waited_template, writeback_wait_iff_congested,
+
+	TP_PROTO(unsigned int usec_timeout, unsigned int usec_delayed),
+
+	TP_ARGS(usec_timeout, usec_delayed)
+);
+
 #endif /* _TRACE_WRITEBACK_H */
 
 /* This part must be outside protection */
mm/backing-dev.c +36 -2
index 298975a..94b5433 100644
@@ -724,6 +724,7 @@ static wait_queue_head_t congestion_wqh[2] = {
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
 		__WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
 	};
+static atomic_t nr_bdi_congested[2];
 
 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 {
@@ -731,7 +732,8 @@ void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
 	wait_queue_head_t *wqh = &congestion_wqh[sync];
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	clear_bit(bit, &bdi->state);
+	if (test_and_clear_bit(bit, &bdi->state))
+		atomic_dec(&nr_bdi_congested[sync]);
 	smp_mb__after_clear_bit();
 	if (waitqueue_active(wqh))
 		wake_up(wqh);
@@ -743,7 +745,8 @@ void set_bdi_congested(struct backing_dev_info *bdi, int sync)
 	enum bdi_state bit;
 
 	bit = sync ? BDI_sync_congested : BDI_async_congested;
-	set_bit(bit, &bdi->state);
+	if (!test_and_set_bit(bit, &bdi->state))
+		atomic_inc(&nr_bdi_congested[sync]);
 }
 EXPORT_SYMBOL(set_bdi_congested);
 
@@ -774,3 +777,62 @@ long congestion_wait(int sync, long timeout)
 }
 EXPORT_SYMBOL(congestion_wait);
 
+/**
+ * congestion_wait - wait for a backing_dev to become uncongested
+ * @zone: A zone to consider the number of being being written back from
+ * @sync: SYNC or ASYNC IO
+ * @timeout: timeout in jiffies
+ *
+ * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
+ * write congestion.  If no backing_devs are congested then the number of
+ * writeback pages in the zone are checked and compared to the inactive
+ * list. If there is no sigificant writeback or congestion, there is no point
+ * in sleeping but cond_resched() is called in case the current process has
+ * consumed its CPU quota.
+ */
+long wait_iff_congested(struct zone *zone, int sync, long timeout)
+{
+	long ret;
+	unsigned long start = jiffies;
+	DEFINE_WAIT(wait);
+	wait_queue_head_t *wqh = &congestion_wqh[sync];
+
+	/*
+	 * If there is no congestion, check the amount of writeback. If there
+	 * is no significant writeback and no congestion, just cond_resched
+	 */
+	if (atomic_read(&nr_bdi_congested[sync]) == 0) {
+		unsigned long inactive, writeback;
+
+		inactive = zone_page_state(zone, NR_INACTIVE_FILE) +
+				zone_page_state(zone, NR_INACTIVE_ANON);
+		writeback = zone_page_state(zone, NR_WRITEBACK);
+
Quoted 8 lines

mm/backing-dev.c long congestion_wait(int sync, long timeout) · line 815

Quoted 5 lines
mm/backing-dev.c +5 -0
+		/*
+		 * If less than half the inactive list is being written back,
+		 * reclaim might as well continue
+		 */
+		if (writeback < inactive / 2) {

Hmm..can't we have a way that "find a page which can be just dropped without writeback" rather than sleeping ?

Sure, just scan for clean pages but then younger clean pages would be reclaimed before old dirty pages because we were not waiting on writeback. It's a significant change.

Quoted 3 lines

I think we can throttole the number of victims for avoidng I/O congestion as pages/tick....if exhausted, ok, we should sleep.

I think it would be tricky to throttle based on time effectively. I find it easier to think about throttling in terms of congested device, number of dirty pages in a zone or number of pages currently being written back because these are events that can prevent reclaim taking place.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
author
#18 lore raw

PATCH 4/10 vmscan: Synchronous lumpy reclaim should not call congestion_wait()

In reply to Mel Gorman

From: KOSAKI Motohiro <[email protected]>

congestion_wait() mean "waiting queue congestion is cleared". However, synchronous lumpy reclaim does not need this congestion_wait() as shrink_page_list(PAGEOUT_IO_SYNC) uses wait_on_page_writeback() and it provides the necessary waiting.

---
 mm/vmscan.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
mm/vmscan.c +0 -2
index eabe987..5979850 100644
@@ -1341,8 +1341,6 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
-		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
-
 		/*
 		 * The attempt at page out may have made some
 		 * of the pages active, mark them inactive again.
Signature · 7 lines
1.7.1

--
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>
Reviewed-by on v1 4/10
#19 lore raw

Re: [PATCH 04/10] vmscan: Synchronous lumpy reclaim should not call congestion_wait()

Quoted 9 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

congestion_wait() mean "waiting queue congestion is cleared". However, synchronous lumpy reclaim does not need this congestion_wait() as shrink_page_list(PAGEOUT_IO_SYNC) uses wait_on_page_writeback() and it provides the necessary waiting.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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 Reviewed-by on v1 4/10
#20 lore raw

Re: [PATCH 04/10] vmscan: Synchronous lumpy reclaim should not call congestion_wait()

In reply to Mel Gorman
Quoted 9 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

congestion_wait() mean "waiting queue congestion is cleared". However, synchronous lumpy reclaim does not need this congestion_wait() as shrink_page_list(PAGEOUT_IO_SYNC) uses wait_on_page_writeback() and it provides the necessary waiting.

Quoted 9 lines
---
 mm/vmscan.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
mm/vmscan.c +0 -0
index eabe987..5979850 100644

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1346

Quoted 8 lines
mm/vmscan.c +0 -2
@@ -1341,8 +1341,6 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
-		wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
-
 		/*
 		 * The attempt at page out may have made some
 		 * of the pages active, mark them inactive again.

--
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>

Reviewed-by on v1 4/10
#21 lore raw

Re: [PATCH 04/10] vmscan: Synchronous lumpy reclaim should not call congestion_wait()

In reply to Mel Gorman
Quoted 6 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

congestion_wait() mean "waiting queue congestion is cleared". However, synchronous lumpy reclaim does not need this congestion_wait() as shrink_page_list(PAGEOUT_IO_SYNC) uses wait_on_page_writeback() and it provides the necessary waiting.

--
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>

Reviewed-by on v1 4/10
#22 lore raw

Re: [PATCH 04/10] vmscan: Synchronous lumpy reclaim should not call congestion_wait()

In reply to Mel Gorman
Quoted 9 lines · On Mon, 6 Sep 2010 11:47:27 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

congestion_wait() mean "waiting queue congestion is cleared". However, synchronous lumpy reclaim does not need this congestion_wait() as shrink_page_list(PAGEOUT_IO_SYNC) uses wait_on_page_writeback() and it provides the necessary waiting.

author
#23 lore raw

PATCH 5/10 vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

In reply to Mel Gorman

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

---
 mm/vmscan.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
mm/vmscan.c +3 -1
index 5979850..79bd812 100644
@@ -665,7 +665,9 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		page = lru_to_page(page_list);
 		list_del(&page->lru);
 
-		if (!trylock_page(page))
+		if (sync_writeback == PAGEOUT_IO_SYNC)
+			lock_page(page);
+		else if (!trylock_page(page))
 			goto keep;
 
 		VM_BUG_ON(PageActive(page));
Signature · 7 lines
1.7.1

--
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>
Reviewed-by on v1 5/10
#24 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 8 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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 Acked-by on v1 5/10
#25 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

In reply to Mel Gorman
Quoted 8 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Quoted 9 lines
---
 mm/vmscan.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
mm/vmscan.c +0 -0
index 5979850..79bd812 100644

mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 673

Quoted 10 lines
mm/vmscan.c +3 -1
@@ -665,7 +665,9 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		page = lru_to_page(page_list);
 		list_del(&page->lru);
 
-		if (!trylock_page(page))
+		if (sync_writeback == PAGEOUT_IO_SYNC)
+			lock_page(page);
+		else if (!trylock_page(page))
 			goto keep;
 
 		VM_BUG_ON(PageActive(page));

--
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 on v1 5/10
#26 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

In reply to Mel Gorman
Quoted 8 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

--
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>

Reviewed-by on v1 5/10
#27 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

In reply to Mel Gorman
Quoted 8 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

--
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>

#28 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 5 lines · On Thu, 9 Sep 2010 12:04:48 +0900 KAMEZAWA Hiroyuki…
Quoted 2 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

Quoted 9 lines
Quoted 6 lines

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Ah......but can't this change cause dead lock ??

Thanks,
-Kame

#29 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 12 lines · KAMEZAWA Hiroyuki
Quoted 9 lines · On Thu, 9 Sep 2010 12:04:48 +0900 KAMEZAWA Hiroyuki…
Quoted 6 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Ah......but can't this change cause dead lock ??

You mean the task goes for page allocation while holding some page lock? Seems possible.

Thanks,
Fengguang

#30 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

In reply to KAMEZAWA Hiroyuki
Quoted 12 lines
Quoted 9 lines · On Thu, 9 Sep 2010 12:04:48 +0900 KAMEZAWA Hiroyuki…
Quoted 6 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Ah......but can't this change cause dead lock ??

Yes, this patch is purely crappy. please drop. I guess I was poisoned by poisonous mushroom of Mario Bros.

--
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>

author
#31 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 15 lines · KOSAKI Motohiro
Quoted 15 lines
Quoted 12 lines · On Thu, 9 Sep 2010 12:04:48 +0900 KAMEZAWA Hiroyuki…
Quoted 8 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

Ah......but can't this change cause dead lock ??

Yes, this patch is purely crappy. please drop. I guess I was poisoned by poisonous mushroom of Mario Bros.

Lets be clear on what the exact dead lock conditions are. The ones I had thought about when I felt this patch was ok were;

o We are not holding the LRU lock (or any lock, we just called cond_resched())
o We do not have another page locked because we cannot lock multiple pages
o Kswapd will never be in LUMPY_MODE_SYNC so it is not getting blocked
o lock_page() itself is not allocating anything that we could recurse on

One potential dead lock would be if the direct reclaimer held a page lock and ended up here but is that situation even allowed? I did not think of an obvious example of when this would happen. Similarly, deadlock situations with mmap_sem shouldn't happen unless multiple page locks are being taken.

(prepares to feel foolish)

What did I miss?

Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
#32 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 24 lines · KOSAKI Motohiro
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines · On Thu, 9 Sep 2010 12:04:48 +0900 KAMEZAWA Hiroyuki…
Quoted 8 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

Ah......but can't this change cause dead lock ??

Yes, this patch is purely crappy. please drop. I guess I was poisoned by poisonous mushroom of Mario Bros.

Quoted 7 lines

Lets be clear on what the exact dead lock conditions are. The ones I had thought about when I felt this patch was ok were;

o We are not holding the LRU lock (or any lock, we just called cond_resched())
o We do not have another page locked because we cannot lock multiple pages
o Kswapd will never be in LUMPY_MODE_SYNC so it is not getting blocked
o lock_page() itself is not allocating anything that we could recurse on

True, all.

Quoted 3 lines

One potential dead lock would be if the direct reclaimer held a page lock and ended up here but is that situation even allowed?

example,

__do_fault()
{
(snip)

        if (unlikely(!(ret & VM_FAULT_LOCKED)))
                lock_page(vmf.page);
        else
                VM_BUG_ON(!PageLocked(vmf.page));

        /*
         * Should we do an early C-O-W break?
         */
        page = vmf.page;
        if (flags & FAULT_FLAG_WRITE) {
                if (!(vma->vm_flags & VM_SHARED)) {
                        anon = 1;
                        if (unlikely(anon_vma_prepare(vma))) {
                                ret = VM_FAULT_OOM;
                                goto out;
                        }
                        page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
                                                vma, address);

Afaik, detailed rule is,

o kswapd can call lock_page() because they never take page lock outside vmscan
o if try_lock() is successed, we can call lock_page_nosync() against its page after unlock. because the task have gurantee of no lock taken.
o otherwise, direct reclaimer can't call lock_page(). the task may have a lock already.

I think.

Quoted 8 lines

I did not
think of an obvious example of when this would happen. Similarly, deadlock situations with mmap_sem shouldn't happen unless multiple page locks are being taken.

(prepares to feel foolish)

What did I miss?

--
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>

#33 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 3 lines

Afaik, detailed rule is,

o kswapd can call lock_page() because they never take page lock outside vmscan

s/lock_page()/lock_page_nosync()/

Quoted 4 lines

o if try_lock() is successed, we can call lock_page_nosync() against its page after unlock. because the task have gurantee of no lock taken.
o otherwise, direct reclaimer can't call lock_page(). the task may have a lock already.

Quoted 16 lines

I think.

Quoted 8 lines

I did not
think of an obvious example of when this would happen. Similarly, deadlock situations with mmap_sem shouldn't happen unless multiple page locks are being taken.

(prepares to feel foolish)

What did I miss?

--
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>

author
#34 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

In reply to KOSAKI Motohiro
Quoted 53 lines · KOSAKI Motohiro
Quoted 24 lines · KOSAKI Motohiro
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines · On Thu, 9 Sep 2010 12:04:48 +0900 KAMEZAWA Hiroyuki…
Quoted 8 lines · On Mon, 6 Sep 2010 11:47:28 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

With synchrounous lumpy reclaim, there is no reason to give up to reclaim pages even if page is locked. This patch uses lock_page() instead of trylock_page() in this case.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

Ah......but can't this change cause dead lock ??

Yes, this patch is purely crappy. please drop. I guess I was poisoned by poisonous mushroom of Mario Bros.

Quoted 7 lines

Lets be clear on what the exact dead lock conditions are. The ones I had thought about when I felt this patch was ok were;

o We are not holding the LRU lock (or any lock, we just called cond_resched())
o We do not have another page locked because we cannot lock multiple pages
o Kswapd will never be in LUMPY_MODE_SYNC so it is not getting blocked
o lock_page() itself is not allocating anything that we could recurse on

True, all.

Quoted 3 lines

One potential dead lock would be if the direct reclaimer held a page lock and ended up here but is that situation even allowed?

example,

__do_fault()
{
(snip)

        if (unlikely(!(ret & VM_FAULT_LOCKED)))
                lock_page(vmf.page);
        else
                VM_BUG_ON(!PageLocked(vmf.page));

        /*
         * Should we do an early C-O-W break?
         */
        page = vmf.page;
        if (flags & FAULT_FLAG_WRITE) {
Quoted 9 lines
                if (!(vma->vm_flags & VM_SHARED)) {
                        anon = 1;
                        if (unlikely(anon_vma_prepare(vma))) {
                                ret = VM_FAULT_OOM;
                                goto out;
                        }
                        page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
                                                vma, address);

Correct, this is a problem. I already had dropped the patch but thanks for pointing out a deadlock because I was missing this case. Nothing stops the page being faulted being sent to shrink_page_list() when alloc_page_vma() is called. The deadlock might be hard to hit, but it's there.

Quoted 4 lines

Afaik, detailed rule is,

o kswapd can call lock_page() because they never take page lock outside vmscan

lock_page_nosync as you point out in your next mail. While it can call it, kswapd shouldn't because normally it avoids stalls but it would not deadlock as a result of calling it.

Quoted 4 lines

o if try_lock() is successed, we can call lock_page_nosync() against its page after unlock. because the task have gurantee of no lock taken.
o otherwise, direct reclaimer can't call lock_page(). the task may have a lock already.

I think the safer bet is simply to say "direct reclaimers should not call lock_page() because the fault path could be holding a lock on that page already".

Thanks.

Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
#35 lore raw

Re: [PATCH 05/10] vmscan: Synchrounous lumpy reclaim use lock_page() instead trylock_page()

Quoted 25 lines
Quoted 15 lines

example,

__do_fault()
{
(snip)

        if (unlikely(!(ret & VM_FAULT_LOCKED)))
                lock_page(vmf.page);
        else
                VM_BUG_ON(!PageLocked(vmf.page));

        /*
         * Should we do an early C-O-W break?
         */
        page = vmf.page;
        if (flags & FAULT_FLAG_WRITE) {
Quoted 9 lines
                if (!(vma->vm_flags & VM_SHARED)) {
                        anon = 1;
                        if (unlikely(anon_vma_prepare(vma))) {
                                ret = VM_FAULT_OOM;
                                goto out;
                        }
                        page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
                                                vma, address);
Quoted 4 lines

Correct, this is a problem. I already had dropped the patch but thanks for pointing out a deadlock because I was missing this case. Nothing stops the page being faulted being sent to shrink_page_list() when alloc_page_vma() is called. The deadlock might be hard to hit, but it's there.

Yup, unfortunatelly.

Quoted 7 lines
Quoted 3 lines

Afaik, detailed rule is,

o kswapd can call lock_page() because they never take page lock outside vmscan

lock_page_nosync as you point out in your next mail. While it can call it, kswapd shouldn't because normally it avoids stalls but it would not deadlock as a result of calling it.

Agreed.

Quoted 8 lines
Quoted 4 lines

o if try_lock() is successed, we can call lock_page_nosync() against its page after unlock. because the task have gurantee of no lock taken.
o otherwise, direct reclaimer can't call lock_page(). the task may have a lock already.

I think the safer bet is simply to say "direct reclaimers should not call lock_page() because the fault path could be holding a lock on that page already".

Yup, agreed.

--
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>

author
#36 lore raw

PATCH 6/10 vmscan: Narrow the scenarios lumpy reclaim uses synchrounous reclaim

In reply to Mel Gorman

From: KOSAKI Motohiro <[email protected]>

shrink_page_list() can decide to give up reclaiming a page under a number of conditions such as

1. trylock_page() failure
2. page is unevictable
3. zone reclaim and page is mapped
4. PageWriteback() is true
5. page is swapbacked and swap is full
6. add_to_swap() failure
7. page is dirty and gfpmask don't have GFP_IO, GFP_FS
8. page is pinned
9. IO queue is congested
10. pageout() start IO, but not finished

When lumpy reclaim, all of failure result in entering synchronous lumpy reclaim but this can be unnecessary. In cases (2), (3), (5), (6), (7) and (8), there is no point retrying. This patch causes lumpy reclaim to abort when it is known it will fail.

Case (9) is more interesting. current behavior is,
1. start shrink_page_list(async)
2. found queue_congested()
3. skip pageout write
4. still start shrink_page_list(sync)
5. wait on a lot of pages
6. again, found queue_congested()
7. give up pageout write again

So, it's meaningless time wasting. However, just skipping page reclaim is also not a good as as x86 allocating a huge page needs 512 pages for example. It can have more dirty pages than queue congestion threshold (~=128).

After this patch, pageout() behaves as follows;

- If order > PAGE_ALLOC_COSTLY_ORDER Ignore queue congestion always.
- If order <= PAGE_ALLOC_COSTLY_ORDER skip write page and disable lumpy reclaim.

---
 include/trace/events/vmscan.h |    6 +-
 mm/vmscan.c                   |  122 +++++++++++++++++++++++++---------------
 2 files changed, 79 insertions(+), 49 deletions(-)
include/trace/events/vmscan.h +3 -3
index 14c1586..6f07c44 100644
@@ -25,13 +25,13 @@
 
 #define trace_reclaim_flags(page, sync) ( \
 	(page_is_file_cache(page) ? RECLAIM_WB_FILE : RECLAIM_WB_ANON) | \
-	(sync == PAGEOUT_IO_SYNC ? RECLAIM_WB_SYNC : RECLAIM_WB_ASYNC)   \
+	(sync == LUMPY_MODE_SYNC ? RECLAIM_WB_SYNC : RECLAIM_WB_ASYNC)   \
 	)
 
 #define trace_shrink_flags(file, sync) ( \
-	(sync == PAGEOUT_IO_SYNC ? RECLAIM_WB_MIXED : \
+	(sync == LUMPY_MODE_SYNC ? RECLAIM_WB_MIXED : \
 			(file ? RECLAIM_WB_FILE : RECLAIM_WB_ANON)) |  \
-	(sync == PAGEOUT_IO_SYNC ? RECLAIM_WB_SYNC : RECLAIM_WB_ASYNC) \
+	(sync == LUMPY_MODE_SYNC ? RECLAIM_WB_SYNC : RECLAIM_WB_ASYNC) \
 	)
 
 TRACE_EVENT(mm_vmscan_kswapd_sleep,
mm/vmscan.c +76 -46

280 lines not on this page · raw message

Signature · 7 lines
1.7.1

--
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>
Reviewed-by on v1 6/10
#37 lore raw

Re: [PATCH 06/10] vmscan: Narrow the scenarios lumpy reclaim uses synchrounous reclaim

Quoted 36 lines · On Mon, 6 Sep 2010 11:47:29 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

shrink_page_list() can decide to give up reclaiming a page under a number of conditions such as

1. trylock_page() failure
2. page is unevictable
3. zone reclaim and page is mapped
4. PageWriteback() is true
5. page is swapbacked and swap is full
6. add_to_swap() failure
7. page is dirty and gfpmask don't have GFP_IO, GFP_FS
8. page is pinned
9. IO queue is congested
10. pageout() start IO, but not finished

When lumpy reclaim, all of failure result in entering synchronous lumpy reclaim but this can be unnecessary. In cases (2), (3), (5), (6), (7) and (8), there is no point retrying. This patch causes lumpy reclaim to abort when it is known it will fail.

Case (9) is more interesting. current behavior is,
1. start shrink_page_list(async)
2. found queue_congested()
3. skip pageout write
4. still start shrink_page_list(sync)
5. wait on a lot of pages
6. again, found queue_congested()
7. give up pageout write again

So, it's meaningless time wasting. However, just skipping page reclaim is also not a good as as x86 allocating a huge page needs 512 pages for example. It can have more dirty pages than queue congestion threshold (~=128).

After this patch, pageout() behaves as follows;

Quoted 7 lines

- If order > PAGE_ALLOC_COSTLY_ORDER Ignore queue congestion always.
- If order <= PAGE_ALLOC_COSTLY_ORDER skip write page and disable lumpy reclaim.

seems nice.

author
#38 lore raw

PATCH 7/10 vmscan: Remove dead code in shrink_inactive_list()

In reply to Mel Gorman

From: KOSAKI Motohiro <[email protected]>

After synchrounous lumpy reclaim, the page_list is guaranteed to not have active pages as page activation in shrink_page_list() disables lumpy reclaim. Remove the dead code.

---
 mm/vmscan.c |    8 --------
 1 files changed, 0 insertions(+), 8 deletions(-)
mm/vmscan.c +0 -8
index 21d1153..64f9ca5 100644
@@ -1334,7 +1334,6 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 	unsigned long nr_scanned;
 	unsigned long nr_reclaimed = 0;
 	unsigned long nr_taken;
-	unsigned long nr_active;
 	unsigned long nr_anon;
 	unsigned long nr_file;
 
@@ -1389,13 +1388,6 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
-		/*
-		 * The attempt at page out may have made some
-		 * of the pages active, mark them inactive again.
-		 */
-		nr_active = clear_active_flags(&page_list, NULL);
-		count_vm_events(PGDEACTIVATE, nr_active);
-
 		set_lumpy_reclaim_mode(priority, sc, true);
 		nr_reclaimed += shrink_page_list(&page_list, sc);
 	}
Signature · 1 line
1.7.1
Reviewed-by on v1 7/10
#39 lore raw

Re: [PATCH 07/10] vmscan: Remove dead code in shrink_inactive_list()

Quoted 8 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

After synchrounous lumpy reclaim, the page_list is guaranteed to not have active pages as page activation in shrink_page_list() disables lumpy reclaim. Remove the dead code.

Signature · 2 lines
Kind regards,
Minchan Kim
author
#40 lore raw

PATCH 8/10 vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

In reply to Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +16 -8
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {
-				if (mode == ISOLATE_BOTH &&
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}
 	}
Signature · 7 lines
1.7.1

--
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>
#41 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

Quoted 54 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1077

Quoted 4 lines
mm/vmscan.c +0 -1
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {
-				if (mode == ISOLATE_BOTH &&

Why can we remove ISOLATION_BOTH check?
Is it a intentionall behavior change?

Quoted 13 lines
mm/vmscan.c +5 -2
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}
 	}
Signature · 1 line
1.7.1
Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
author
#42 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

Quoted 52 lines · Minchan Kim
Quoted 51 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
Quoted 9 lines

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1077

Quoted 7 lines
mm/vmscan.c +0 -1
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {
-				if (mode == ISOLATE_BOTH &&

Why can we remove ISOLATION_BOTH check?

Because this is lumpy reclaim and whether we are isolating inactive, active or both doesn't matter. The fact we failed to isolate the page and it has a reference count means that a contiguous allocation in that area will fail.

Quoted 2 lines

Is it a intentionall behavior change?

Yes.

Quoted 10 lines
Quoted 10 lines
mm/vmscan.c +5 -2
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}
 	}
Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
Reviewed-by
#43 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

Quoted 63 lines · Mel Gorman
Quoted 52 lines · Minchan Kim
Quoted 51 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
Quoted 9 lines

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1077

Quoted 7 lines
mm/vmscan.c +0 -1
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {
-				if (mode == ISOLATE_BOTH &&

Why can we remove ISOLATION_BOTH check?

Quoted 8 lines

Because this is lumpy reclaim and whether we are isolating inactive, active or both doesn't matter. The fact we failed to isolate the page and it has a reference count means that a contiguous allocation in that area will fail.

Quoted 2 lines

Is it a intentionall behavior change?

Yes.

It looks good to me.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
Reviewed-by on v1 8/10
#44 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

In reply to Mel Gorman
Quoted 57 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1089

Quoted 10 lines
mm/vmscan.c +5 -3
-				if (mode == ISOLATE_BOTH &&
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}

The many nr_lumpy_failed++ can be moved here:

if (pfn < end_pfn)
nr_lumpy_failed++;

--
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>

author
#45 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

Quoted 71 lines · Wu Fengguang
Quoted 57 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1089

Quoted 10 lines
mm/vmscan.c +5 -3
-				if (mode == ISOLATE_BOTH &&
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}

The many nr_lumpy_failed++ can be moved here:

Quoted 3 lines

if (pfn < end_pfn)
nr_lumpy_failed++;

Because the break stops the loop iterating, is there an advantage to making it a pfn check instead? I might be misunderstanding your suggestion.

Quoted 2 lines

Thanks

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
#46 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

Quoted 72 lines · Mel Gorman
Quoted 71 lines · Wu Fengguang
Quoted 57 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1089

Quoted 10 lines
mm/vmscan.c +5 -3
-				if (mode == ISOLATE_BOTH &&
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}

The many nr_lumpy_failed++ can be moved here:

Quoted 7 lines
Quoted 3 lines

if (pfn < end_pfn)
nr_lumpy_failed++;

Because the break stops the loop iterating, is there an advantage to making it a pfn check instead? I might be misunderstanding your suggestion.

The complete view in my mind is

                for (; pfn < end_pfn; pfn++) {
                        if (failed 1)
                                break;
                        if (failed 2)
                                break;
                        if (failed 3)
                                break;
                }
                if (pfn < end_pfn)
                        nr_lumpy_failed++;

Sure it just reduces several lines of code :)

Thanks,
Fengguang

author
#47 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

Quoted 94 lines · Wu Fengguang
Quoted 72 lines · Mel Gorman
Quoted 71 lines · Wu Fengguang
Quoted 57 lines · Mel Gorman

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

---
 mm/vmscan.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
mm/vmscan.c +11 -5
index 64f9ca5..ff52b46 100644
@@ -1047,14 +1047,18 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn)))
+			if (unlikely(!pfn_valid_within(pfn))) {
+				nr_lumpy_failed++;
 				break;
+			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id))
-				continue;
+			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1062,8 +1066,10 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-					!PageSwapCache(cursor_page))
-				continue;
+			    !PageSwapCache(cursor_page)) {
+				nr_lumpy_failed++;
+				break;
+			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1074,9 +1080,11 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 					nr_lumpy_dirty++;
 				scan++;
 			} else {

mm/vmscan.c static unsigned long isolate_lru_pages(unsigned long nr_to_scan, · line 1089

Quoted 10 lines
mm/vmscan.c +5 -3
-				if (mode == ISOLATE_BOTH &&
-						page_count(cursor_page))
-					nr_lumpy_failed++;
+				/* the page is freed already. */
+				if (!page_count(cursor_page))
+					continue;
+				nr_lumpy_failed++;
+				break;
 			}
 		}

The many nr_lumpy_failed++ can be moved here:

Quoted 7 lines
Quoted 3 lines

if (pfn < end_pfn)
nr_lumpy_failed++;

Because the break stops the loop iterating, is there an advantage to making it a pfn check instead? I might be misunderstanding your suggestion.

The complete view in my mind is

                for (; pfn < end_pfn; pfn++) {
                        if (failed 1)
                                break;
                        if (failed 2)
                                break;
                        if (failed 3)
                                break;
                }
                if (pfn < end_pfn)
                        nr_lumpy_failed++;
Quoted 2 lines

Sure it just reduces several lines of code :)

Fair point. I applied the following patch on top.

mm/vmscan.c +7 -10
index 33d27a4..54df972 100644
@@ -1091,18 +1091,14 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				continue;
 
 			/* Avoid holes within the zone. */
-			if (unlikely(!pfn_valid_within(pfn))) {
-				nr_lumpy_failed++;
+			if (unlikely(!pfn_valid_within(pfn)))
 				break;
-			}
 
 			cursor_page = pfn_to_page(pfn);
 
 			/* Check that we have not crossed a zone boundary. */
-			if (unlikely(page_zone_id(cursor_page) != zone_id)) {
-				nr_lumpy_failed++;
+			if (unlikely(page_zone_id(cursor_page) != zone_id))
 				break;
-			}
 
 			/*
 			 * If we don't have enough swap space, reclaiming of
@@ -1110,10 +1106,8 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 			 * pointless.
 			 */
 			if (nr_swap_pages <= 0 && PageAnon(cursor_page) &&
-			    !PageSwapCache(cursor_page)) {
-				nr_lumpy_failed++;
+			    !PageSwapCache(cursor_page))
 				break;
-			}
 
 			if (__isolate_lru_page(cursor_page, mode, file) == 0) {
 				list_move(&cursor_page->lru, dst);
@@ -1127,10 +1121,13 @@ static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
 				/* the page is freed already. */
 				if (!page_count(cursor_page))
 					continue;
-				nr_lumpy_failed++;
 				break;
 			}
 		}
+
+		/* If we break out of the loop above, lumpy reclaim failed */
+		if (pfn < end_pfn)
+			nr_lumpy_failed++;
 	}
 
 	*scanned = scan;

--
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>

Reviewed-by on v1 8/10
#48 lore raw

Re: [PATCH 08/10] vmscan: isolated_lru_pages() stop neighbour search if neighbour cannot be isolated

In reply to Mel Gorman
Quoted 10 lines · On Mon, 6 Sep 2010 11:47:31 +0100 Mel Gorman <[email protected]> wrote:

From: KOSAKI Motohiro <[email protected]>

isolate_lru_pages() does not just isolate LRU tail pages, but also isolate neighbour pages of the eviction page. The neighbour search does not stop even if neighbours cannot be isolated which is excessive as the lumpy reclaim will no longer result in a successful higher order allocation. This patch stops the PFN neighbour pages if an isolation fails and moves on to the next block.

--
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>

author
#49 lore raw

PATCH 9/10 vmscan: Do not writeback filesystem pages in direct reclaim

In reply to Mel Gorman

When memory is under enough pressure, a process may enter direct reclaim to free pages in the same manner kswapd does. If a dirty page is encountered during the scan, this page is written to backing storage using mapping->writepage. This can result in very deep call stacks, particularly if the target storage or filesystem are complex. It has already been observed on XFS that the stack overflows but the problem is not XFS-specific.

This patch prevents direct reclaim writing back filesystem pages by checking if current is kswapd or the page is anonymous before writing back. If the dirty pages cannot be written back, they are placed back on the LRU lists for either background writing by the BDI threads or kswapd. If in direct lumpy reclaim and dirty pages are encountered, the process will stall for the background flusher before trying to reclaim the pages again.

As the call-chain for writing anonymous pages is not expected to be deep and they are not cleaned by flusher threads, anonymous pages are still written back in direct reclaim.

---
 mm/vmscan.c |   49 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 3 deletions(-)
mm/vmscan.c +46 -3
index ff52b46..408c101 100644
@@ -145,6 +145,9 @@ static DECLARE_RWSEM(shrinker_rwsem);
 #define scanning_global_lru(sc)	(1)
 #endif
 
+/* Direct lumpy reclaim waits up to five seconds for background cleaning */
+#define MAX_SWAP_CLEAN_WAIT 50
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -682,11 +685,13 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  * shrink_page_list() returns the number of reclaimed pages
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
-				      struct scan_control *sc)
+					struct scan_control *sc,
+					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
+	unsigned long nr_dirty = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -785,6 +790,15 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			/*
+			 * Only kswapd can writeback filesystem pages to
+			 * avoid risk of stack overflow
+			 */
+			if (page_is_file_cache(page) && !current_is_kswapd()) {
+				nr_dirty++;
+				goto keep_locked;
+			}
+
 			if (references == PAGEREF_RECLAIM_CLEAN)
 				goto keep_locked;
 			if (!may_enter_fs)
@@ -908,6 +922,8 @@ keep_lumpy:
 	free_page_list(&free_pages);
 
 	list_splice(&ret_pages, page_list);
+
+	*nr_still_dirty = nr_dirty;
 	count_vm_events(PGACTIVATE, pgactivate);
 	return nr_reclaimed;
 }
@@ -1312,6 +1328,10 @@ static inline bool should_reclaim_stall(unsigned long nr_taken,
 	if (sc->lumpy_reclaim_mode == LUMPY_MODE_NONE)
 		return false;
 
+	/* If we cannot writeback, there is no point stalling */
+	if (!sc->may_writepage)
+		return false;
+
 	/* If we have relaimed everything on the isolated list, no stall */
 	if (nr_freed == nr_taken)
 		return false;
@@ -1339,11 +1359,13 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 			struct scan_control *sc, int priority, int file)
 {
 	LIST_HEAD(page_list);
+	LIST_HEAD(putback_list);
 	unsigned long nr_scanned;
 	unsigned long nr_reclaimed = 0;
 	unsigned long nr_taken;
 	unsigned long nr_anon;
 	unsigned long nr_file;
+	unsigned long nr_dirty;
 
 	while (unlikely(too_many_isolated(zone, file, sc))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);
@@ -1392,14 +1414,35 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	spin_unlock_irq(&zone->lru_lock);
 
-	nr_reclaimed = shrink_page_list(&page_list, sc);
+	nr_reclaimed = shrink_page_list(&page_list, sc, &nr_dirty);
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
+		int dirty_retry = MAX_SWAP_CLEAN_WAIT;
 		set_lumpy_reclaim_mode(priority, sc, true);
-		nr_reclaimed += shrink_page_list(&page_list, sc);
+
+		while (nr_reclaimed < nr_taken && nr_dirty && dirty_retry--) {
+			struct page *page, *tmp;
+
+			/* Take off the clean pages marked for activation */
+			list_for_each_entry_safe(page, tmp, &page_list, lru) {
+				if (PageDirty(page) || PageWriteback(page))
+					continue;
+
+				list_del(&page->lru);
+				list_add(&page->lru, &putback_list);
+			}
+
+			wakeup_flusher_threads(laptop_mode ? 0 : nr_dirty);
+			wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
+
+			nr_reclaimed = shrink_page_list(&page_list, sc,
+							&nr_dirty);
+		}
 	}
 
+	list_splice(&putback_list, &page_list);
+
 	local_irq_disable();
 	if (current_is_kswapd())
 		__count_vm_events(KSWAPD_STEAL, nr_reclaimed);
Signature · 7 lines
1.7.1

--
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>
#50 lore raw

Re: [PATCH 09/10] vmscan: Do not writeback filesystem pages in direct reclaim

Mel,

Sorry for being late, I'm doing pretty much prework these days ;)

Quoted 105 lines · Mel Gorman

When memory is under enough pressure, a process may enter direct reclaim to free pages in the same manner kswapd does. If a dirty page is encountered during the scan, this page is written to backing storage using mapping->writepage. This can result in very deep call stacks, particularly if the target storage or filesystem are complex. It has already been observed on XFS that the stack overflows but the problem is not XFS-specific.

This patch prevents direct reclaim writing back filesystem pages by checking if current is kswapd or the page is anonymous before writing back. If the dirty pages cannot be written back, they are placed back on the LRU lists for either background writing by the BDI threads or kswapd. If in direct lumpy reclaim and dirty pages are encountered, the process will stall for the background flusher before trying to reclaim the pages again.

As the call-chain for writing anonymous pages is not expected to be deep and they are not cleaned by flusher threads, anonymous pages are still written back in direct reclaim.

---
 mm/vmscan.c |   49 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 3 deletions(-)
mm/vmscan.c +23 -1
index ff52b46..408c101 100644
@@ -145,6 +145,9 @@ static DECLARE_RWSEM(shrinker_rwsem);
 #define scanning_global_lru(sc)	(1)
 #endif
 
+/* Direct lumpy reclaim waits up to five seconds for background cleaning */
+#define MAX_SWAP_CLEAN_WAIT 50
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -682,11 +685,13 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  * shrink_page_list() returns the number of reclaimed pages
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
-				      struct scan_control *sc)
+					struct scan_control *sc,
+					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
+	unsigned long nr_dirty = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -785,6 +790,15 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			/*
+			 * Only kswapd can writeback filesystem pages to
+			 * avoid risk of stack overflow
+			 */
+			if (page_is_file_cache(page) && !current_is_kswapd()) {
+				nr_dirty++;
+				goto keep_locked;
+			}
+
 			if (references == PAGEREF_RECLAIM_CLEAN)
 				goto keep_locked;
 			if (!may_enter_fs)
@@ -908,6 +922,8 @@ keep_lumpy:
 	free_page_list(&free_pages);
 
 	list_splice(&ret_pages, page_list);
+
+	*nr_still_dirty = nr_dirty;
 	count_vm_events(PGACTIVATE, pgactivate);
 	return nr_reclaimed;
 }
@@ -1312,6 +1328,10 @@ static inline bool should_reclaim_stall(unsigned long nr_taken,
 	if (sc->lumpy_reclaim_mode == LUMPY_MODE_NONE)
 		return false;
 
+	/* If we cannot writeback, there is no point stalling */
+	if (!sc->may_writepage)
+		return false;
+
 	/* If we have relaimed everything on the isolated list, no stall */
 	if (nr_freed == nr_taken)
 		return false;
@@ -1339,11 +1359,13 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 			struct scan_control *sc, int priority, int file)
 {
 	LIST_HEAD(page_list);
+	LIST_HEAD(putback_list);
 	unsigned long nr_scanned;
 	unsigned long nr_reclaimed = 0;
 	unsigned long nr_taken;
 	unsigned long nr_anon;
 	unsigned long nr_file;
+	unsigned long nr_dirty;
 
 	while (unlikely(too_many_isolated(zone, file, sc))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1420

Quoted 8 lines
mm/vmscan.c +1 -1
@@ -1392,14 +1414,35 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	spin_unlock_irq(&zone->lru_lock);
 
-	nr_reclaimed = shrink_page_list(&page_list, sc);
+	nr_reclaimed = shrink_page_list(&page_list, sc, &nr_dirty);
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {

It is possible to OOM if the LRU list is small and/or the storage is slow, so that the flusher cannot clean enough pages before the LRU is fully scanned.

So we may need do waits on dirty/writeback pages on *order-0* direct reclaims, when priority goes rather low (such as < 3).

Quoted 6 lines
mm/vmscan.c +4 -1
+		int dirty_retry = MAX_SWAP_CLEAN_WAIT;
 		set_lumpy_reclaim_mode(priority, sc, true);
-		nr_reclaimed += shrink_page_list(&page_list, sc);
+
+		while (nr_reclaimed < nr_taken && nr_dirty && dirty_retry--) {
+			struct page *page, *tmp;

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,

Quoted 10 lines
mm/vmscan.c +9 -0
+
 
+			/* Take off the clean pages marked for activation */
+			list_for_each_entry_safe(page, tmp, &page_list, lru) {
+				if (PageDirty(page) || PageWriteback(page))
+					continue;
+
+				list_del(&page->lru);
+				list_add(&page->lru, &putback_list);
+			}

nitpick: I guess the above loop is optional code to avoid overheads of shrink_page_list() repeatedly going through some unfreeable pages? Considering this is the slow code path, I'd prefer to keep the code simple than to do such optimizations.

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1436

Quoted 1 line
mm/vmscan.c +1 -0
+			wakeup_flusher_threads(laptop_mode ? 0 : nr_dirty);

how about

                        if (!laptop_mode)
                                wakeup_flusher_threads(nr_dirty);
Quoted 14 lines
mm/vmscan.c +7 -0
+			wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
+
+			nr_reclaimed = shrink_page_list(&page_list, sc,
+							&nr_dirty);
+		}
 	}
 
+	list_splice(&putback_list, &page_list);
+
 	local_irq_disable();
 	if (current_is_kswapd())
 		__count_vm_events(KSWAPD_STEAL, nr_reclaimed);
Signature · 1 line
1.7.1

--
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>

author
#51 lore raw

Re: [PATCH 09/10] vmscan: Do not writeback filesystem pages in direct reclaim

Quoted 4 lines · Wu Fengguang

Mel,

Sorry for being late, I'm doing pretty much prework these days ;)

No worries, I'm all over the place at the moment so cannot lecture on response times :)

Quoted 109 lines · Mel Gorman
Quoted 108 lines

When memory is under enough pressure, a process may enter direct reclaim to free pages in the same manner kswapd does. If a dirty page is encountered during the scan, this page is written to backing storage using mapping->writepage. This can result in very deep call stacks, particularly if the target storage or filesystem are complex. It has already been observed on XFS that the stack overflows but the problem is not XFS-specific.

This patch prevents direct reclaim writing back filesystem pages by checking if current is kswapd or the page is anonymous before writing back. If the dirty pages cannot be written back, they are placed back on the LRU lists for either background writing by the BDI threads or kswapd. If in direct lumpy reclaim and dirty pages are encountered, the process will stall for the background flusher before trying to reclaim the pages again.

As the call-chain for writing anonymous pages is not expected to be deep and they are not cleaned by flusher threads, anonymous pages are still written back in direct reclaim.

---
 mm/vmscan.c |   49 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 3 deletions(-)
mm/vmscan.c +23 -1
index ff52b46..408c101 100644
@@ -145,6 +145,9 @@ static DECLARE_RWSEM(shrinker_rwsem);
 #define scanning_global_lru(sc)	(1)
 #endif
 
+/* Direct lumpy reclaim waits up to five seconds for background cleaning */
+#define MAX_SWAP_CLEAN_WAIT 50
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -682,11 +685,13 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  * shrink_page_list() returns the number of reclaimed pages
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
-				      struct scan_control *sc)
+					struct scan_control *sc,
+					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
+	unsigned long nr_dirty = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -785,6 +790,15 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			/*
+			 * Only kswapd can writeback filesystem pages to
+			 * avoid risk of stack overflow
+			 */
+			if (page_is_file_cache(page) && !current_is_kswapd()) {
+				nr_dirty++;
+				goto keep_locked;
+			}
+
 			if (references == PAGEREF_RECLAIM_CLEAN)
 				goto keep_locked;
 			if (!may_enter_fs)
@@ -908,6 +922,8 @@ keep_lumpy:
 	free_page_list(&free_pages);
 
 	list_splice(&ret_pages, page_list);
+
+	*nr_still_dirty = nr_dirty;
 	count_vm_events(PGACTIVATE, pgactivate);
 	return nr_reclaimed;
 }
@@ -1312,6 +1328,10 @@ static inline bool should_reclaim_stall(unsigned long nr_taken,
 	if (sc->lumpy_reclaim_mode == LUMPY_MODE_NONE)
 		return false;
 
+	/* If we cannot writeback, there is no point stalling */
+	if (!sc->may_writepage)
+		return false;
+
 	/* If we have relaimed everything on the isolated list, no stall */
 	if (nr_freed == nr_taken)
 		return false;
@@ -1339,11 +1359,13 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 			struct scan_control *sc, int priority, int file)
 {
 	LIST_HEAD(page_list);
+	LIST_HEAD(putback_list);
 	unsigned long nr_scanned;
 	unsigned long nr_reclaimed = 0;
 	unsigned long nr_taken;
 	unsigned long nr_anon;
 	unsigned long nr_file;
+	unsigned long nr_dirty;
 
 	while (unlikely(too_many_isolated(zone, file, sc))) {
 		congestion_wait(BLK_RW_ASYNC, HZ/10);
@@ -1392,14 +1414,35 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	spin_unlock_irq(&zone->lru_lock);
 
Quoted 9 lines

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1420

Quoted 5 lines
mm/vmscan.c +1 -1
-	nr_reclaimed = shrink_page_list(&page_list, sc);
+	nr_reclaimed = shrink_page_list(&page_list, sc, &nr_dirty);
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {

It is possible to OOM if the LRU list is small and/or the storage is slow, so that the flusher cannot clean enough pages before the LRU is fully scanned.

To go OOM, nr_reclaimed would have to be 0 and for that, the entire list would have to be dirty or unreclaimable. If that situation happens, is the dirty throttling not also broken?

Quoted 3 lines

So we may need do waits on dirty/writeback pages on *order-0* direct reclaims, when priority goes rather low (such as < 3).

In case this is really necessary, the necessary stalling could be done by removing the check for lumpy reclaim in should_reclaim_stall(). What do you think of the following replacement?

/*
* Returns true if the caller should wait to clean dirty/writeback pages. *
* If we are direct reclaiming for contiguous pages and we do not reclaim
* everything in the list, try again and wait for writeback IO to complete.
* This will stall high-order allocations noticeably. Only do that when really
* need to free the pages under high memory pressure.
*
* Alternatively, if priority is getting high, it may be because there are
* too many dirty pages on the LRU. Rather than returning nr_reclaimed == 0
* and potentially causing an OOM, we stall on writeback.
*/
static inline bool should_reclaim_stall(unsigned long nr_taken,
unsigned long nr_freed, int priority,
struct scan_control *sc)

{
        int stall_priority;

        /* kswapd should not stall on sync IO */
        if (current_is_kswapd())
                return false;

        /* If we cannot writeback, there is no point stalling */
        if (!sc->may_writepage)
                return false;

        /* If we have relaimed everything on the isolated list, no stall */
        if (nr_freed == nr_taken)
                return false;

        /*
         * For high-order allocations, there are two stall thresholds.
         * High-cost allocations stall immediately where as lower
         * order allocations such as stacks require the scanning
         * priority to be much higher before stalling.
         */
        if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
                stall_priority = DEF_PRIORITY;
        else
                stall_priority = DEF_PRIORITY / 3;

        return priority <= stall_priority;
}
Quoted 8 lines
Quoted 7 lines
mm/vmscan.c +5 -1
+		int dirty_retry = MAX_SWAP_CLEAN_WAIT;
 		set_lumpy_reclaim_mode(priority, sc, true);
-		nr_reclaimed += shrink_page_list(&page_list, sc);
+
+		while (nr_reclaimed < nr_taken && nr_dirty && dirty_retry--) {
+			struct page *page, *tmp;
+
Quoted 11 lines

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1434

Quoted 8 lines
mm/vmscan.c +8 -0
+			/* Take off the clean pages marked for activation */
+			list_for_each_entry_safe(page, tmp, &page_list, lru) {
+				if (PageDirty(page) || PageWriteback(page))
+					continue;
+
+				list_del(&page->lru);
+				list_add(&page->lru, &putback_list);
+			}

nitpick: I guess the above loop is optional code to avoid overheads of shrink_page_list() repeatedly going through some unfreeable pages?

Pretty much, if they are to be activated, there is no point trying to reclaim them again. It's unnecessary overhead. A strong motivation for this series is to reduce overheads in the reclaim paths and unnecessary retrying of unfreeable pages.

Quoted 9 lines

Considering this is the slow code path, I'd prefer to keep the code simple than to do such optimizations.

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1436

Quoted 1 line
mm/vmscan.c +1 -0
+			wakeup_flusher_threads(laptop_mode ? 0 : nr_dirty);

how about

                        if (!laptop_mode)
                                wakeup_flusher_threads(nr_dirty);

It's not the same thing. wakeup_flusher_threads(0) in laptop_mode is to clean all pages if some need dirtying. laptop_mode cleans all pages to minimise disk spinups.

Quoted 15 lines
Quoted 14 lines
mm/vmscan.c +7 -0
+			wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
+
+			nr_reclaimed = shrink_page_list(&page_list, sc,
+							&nr_dirty);
+		}
 	}
 
+	list_splice(&putback_list, &page_list);
+
 	local_irq_disable();
 	if (current_is_kswapd())
 		__count_vm_events(KSWAPD_STEAL, nr_reclaimed);
Signature · 1 line
1.7.1
Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
#52 lore raw

Re: [PATCH 09/10] vmscan: Do not writeback filesystem pages in direct reclaim

Quoted 10 lines
Quoted 6 lines

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1420

Quoted 2 lines
mm/vmscan.c +0 -0
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {

It is possible to OOM if the LRU list is small and/or the storage is slow, so that the flusher cannot clean enough pages before the LRU is fully scanned.

To go OOM, nr_reclaimed would have to be 0 and for that, the entire list would have to be dirty or unreclaimable. If that situation happens, is the dirty throttling not also broken?

My worry is, even if the dirty throttling limit is instantly set to 0, it may still take time to knock down the number of dirty pages. Think about 500MB dirty pages waiting to be flushed to a slow USB stick.

Quoted 7 lines
Quoted 3 lines

So we may need do waits on dirty/writeback pages on *order-0* direct reclaims, when priority goes rather low (such as < 3).

In case this is really necessary, the necessary stalling could be done by removing the check for lumpy reclaim in should_reclaim_stall(). What do you think of the following replacement?

I merely want to provide a guarantee, so it may be enough to add this:

        if (nr_freed == nr_taken)
                return false;

+ if (!priority)
+ return true;

This ensures the last full LRU scan will do necessary waits to prevent the OOM.

Quoted 66 lines

/*
* Returns true if the caller should wait to clean dirty/writeback pages. *
* If we are direct reclaiming for contiguous pages and we do not reclaim
* everything in the list, try again and wait for writeback IO to complete.
* This will stall high-order allocations noticeably. Only do that when really
* need to free the pages under high memory pressure.
*
* Alternatively, if priority is getting high, it may be because there are
* too many dirty pages on the LRU. Rather than returning nr_reclaimed == 0
* and potentially causing an OOM, we stall on writeback.
*/
static inline bool should_reclaim_stall(unsigned long nr_taken,
unsigned long nr_freed, int priority,
struct scan_control *sc)

{
        int stall_priority;

        /* kswapd should not stall on sync IO */
        if (current_is_kswapd())
                return false;

        /* If we cannot writeback, there is no point stalling */
        if (!sc->may_writepage)
                return false;

        /* If we have relaimed everything on the isolated list, no stall */
        if (nr_freed == nr_taken)
                return false;

        /*
         * For high-order allocations, there are two stall thresholds.
         * High-cost allocations stall immediately where as lower
         * order allocations such as stacks require the scanning
         * priority to be much higher before stalling.
         */
        if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
                stall_priority = DEF_PRIORITY;
        else
                stall_priority = DEF_PRIORITY / 3;

        return priority <= stall_priority;
}
Quoted 8 lines
Quoted 7 lines
mm/vmscan.c +5 -1
+		int dirty_retry = MAX_SWAP_CLEAN_WAIT;
 		set_lumpy_reclaim_mode(priority, sc, true);
-		nr_reclaimed += shrink_page_list(&page_list, sc);
+
+		while (nr_reclaimed < nr_taken && nr_dirty && dirty_retry--) {
+			struct page *page, *tmp;
+
Quoted 11 lines

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1434

Quoted 8 lines
mm/vmscan.c +8 -0
+			/* Take off the clean pages marked for activation */
+			list_for_each_entry_safe(page, tmp, &page_list, lru) {
+				if (PageDirty(page) || PageWriteback(page))
+					continue;
+
+				list_del(&page->lru);
+				list_add(&page->lru, &putback_list);
+			}

nitpick: I guess the above loop is optional code to avoid overheads of shrink_page_list() repeatedly going through some unfreeable pages?

Quoted 4 lines

Pretty much, if they are to be activated, there is no point trying to reclaim them again. It's unnecessary overhead. A strong motivation for this series is to reduce overheads in the reclaim paths and unnecessary retrying of unfreeable pages.

We do so much waits in this loop, so that users will get upset by the iowait stalls much much more than the CPU overheads.. best option is always to avoid entering this loop in the first place, and if we succeeded on that, these lines of optimizations will be nothing but mind destroyers for newbie developers.

Quoted 10 lines
Quoted 9 lines

Considering this is the slow code path, I'd prefer to keep the code simple than to do such optimizations.

mm/vmscan.c shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone, · line 1436

Quoted 1 line
mm/vmscan.c +1 -0
+			wakeup_flusher_threads(laptop_mode ? 0 : nr_dirty);

how about

                        if (!laptop_mode)
                                wakeup_flusher_threads(nr_dirty);
Quoted 3 lines

It's not the same thing. wakeup_flusher_threads(0) in laptop_mode is to clean all pages if some need dirtying. laptop_mode cleans all pages to minimise disk spinups.

Ah.. that's sure fine. I wonder if the flusher could be more smart to automatically extend the number of pages to write in laptop mode. This could simplify some callers.

Thanks,
Fengguang

--
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>

author
#54 lore raw

Re: [PATCH 09/10] vmscan: Do not writeback filesystem pages in direct reclaim

Quoted 3 lines · Christoph Hellwig

Looks like this once again didn't get merged for 2.6.37. Any reason for that?

There are still concerns as to whether this is a good idea or or whether we are papering over the fact that there are too many dirty pages at the end of the LRU. The tracepoints necessary to track the dirty pages encountered went in this cycle as well as some writeback and congestion-waiting changes. I was waiting for some of the writeback churn to die down before revisiting this. The ideal point to reach is "we hardly ever encounter dirty pages so disabling direct writeback has no impact".

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
author
#55 lore raw

PATCH 10/10 vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

In reply to Mel Gorman

There are a number of cases where pages get cleaned but two of concern to this patch are;
o When dirtying pages, processes may be throttled to clean pages if dirty_ratio is not met.
o Pages belonging to inodes dirtied longer than dirty_writeback_centisecs get cleaned.

The problem for reclaim is that dirty pages can reach the end of the LRU if pages are being dirtied slowly so that neither the throttling or a flusher thread waking periodically cleans them.

Background flush is already cleaning old or expired inodes first but the expire time is too far in the future at the time of page reclaim. To mitigate future problems, this patch wakes flusher threads to clean 4M of data - an amount that should be manageable without causing congestion in many cases.

Ideally, the background flushers would only be cleaning pages belonging to the zone being scanned but it's not clear if this would be of benefit (less IO) or not (potentially less efficient IO if an inode is scattered across multiple zones).

---
 mm/vmscan.c |   32 ++++++++++++++++++++++++++++++--
 1 files changed, 30 insertions(+), 2 deletions(-)
mm/vmscan.c +30 -2
index 408c101..33d27a4 100644
@@ -148,6 +148,18 @@ static DECLARE_RWSEM(shrinker_rwsem);
 /* Direct lumpy reclaim waits up to five seconds for background cleaning */
 #define MAX_SWAP_CLEAN_WAIT 50
 
+/*
+ * When reclaim encounters dirty data, wakeup flusher threads to clean
+ * a maximum of 4M of data.
+ */
+#define MAX_WRITEBACK (4194304UL >> PAGE_SHIFT)
+#define WRITEBACK_FACTOR (MAX_WRITEBACK / SWAP_CLUSTER_MAX)
+static inline long nr_writeback_pages(unsigned long nr_dirty)
+{
+	return laptop_mode ? 0 :
+			min(MAX_WRITEBACK, (nr_dirty * WRITEBACK_FACTOR));
+}
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -686,12 +698,14 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
 					struct scan_control *sc,
+					int file,
 					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
 	unsigned long nr_dirty = 0;
+	unsigned long nr_dirty_seen = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -790,6 +804,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			nr_dirty_seen++;
+
 			/*
 			 * Only kswapd can writeback filesystem pages to
 			 * avoid risk of stack overflow
@@ -923,6 +939,18 @@ keep_lumpy:
 
 	list_splice(&ret_pages, page_list);
 
+	/*
+	 * If reclaim is encountering dirty pages, it may be because
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));
+
 	*nr_still_dirty = nr_dirty;
 	count_vm_events(PGACTIVATE, pgactivate);
 	return nr_reclaimed;
@@ -1414,7 +1442,7 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 
 	spin_unlock_irq(&zone->lru_lock);
 
-	nr_reclaimed = shrink_page_list(&page_list, sc, &nr_dirty);
+	nr_reclaimed = shrink_page_list(&page_list, sc, file, &nr_dirty);
 
 	/* Check if we should syncronously wait for writeback */
 	if (should_reclaim_stall(nr_taken, nr_reclaimed, priority, sc)) {
@@ -1437,7 +1465,7 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
 			wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10);
 
 			nr_reclaimed = shrink_page_list(&page_list, sc,
-							&nr_dirty);
+							file, &nr_dirty);
 		}
 	}
 
Signature · 1 line
1.7.1
#56 lore raw

Re: [PATCH 10/10] vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

Quoted 79 lines · On Mon, 6 Sep 2010 11:47:33 +0100 Mel Gorman <[email protected]> wrote:

There are a number of cases where pages get cleaned but two of concern to this patch are;
o When dirtying pages, processes may be throttled to clean pages if dirty_ratio is not met.
o Pages belonging to inodes dirtied longer than dirty_writeback_centisecs get cleaned.

The problem for reclaim is that dirty pages can reach the end of the LRU if pages are being dirtied slowly so that neither the throttling or a flusher thread waking periodically cleans them.

Background flush is already cleaning old or expired inodes first but the expire time is too far in the future at the time of page reclaim. To mitigate future problems, this patch wakes flusher threads to clean 4M of data - an amount that should be manageable without causing congestion in many cases.

Ideally, the background flushers would only be cleaning pages belonging to the zone being scanned but it's not clear if this would be of benefit (less IO) or not (potentially less efficient IO if an inode is scattered across multiple zones).

---
 mm/vmscan.c |   32 ++++++++++++++++++++++++++++++--
 1 files changed, 30 insertions(+), 2 deletions(-)
mm/vmscan.c +18 -0
index 408c101..33d27a4 100644
@@ -148,6 +148,18 @@ static DECLARE_RWSEM(shrinker_rwsem);
 /* Direct lumpy reclaim waits up to five seconds for background cleaning */
 #define MAX_SWAP_CLEAN_WAIT 50
 
+/*
+ * When reclaim encounters dirty data, wakeup flusher threads to clean
+ * a maximum of 4M of data.
+ */
+#define MAX_WRITEBACK (4194304UL >> PAGE_SHIFT)
+#define WRITEBACK_FACTOR (MAX_WRITEBACK / SWAP_CLUSTER_MAX)
+static inline long nr_writeback_pages(unsigned long nr_dirty)
+{
+	return laptop_mode ? 0 :
+			min(MAX_WRITEBACK, (nr_dirty * WRITEBACK_FACTOR));
+}
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -686,12 +698,14 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
 					struct scan_control *sc,
+					int file,
 					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
 	unsigned long nr_dirty = 0;
+	unsigned long nr_dirty_seen = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -790,6 +804,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			nr_dirty_seen++;
+
 			/*
 			 * Only kswapd can writeback filesystem pages to
 			 * avoid risk of stack overflow
@@ -923,6 +939,18 @@ keep_lumpy:
 
 	list_splice(&ret_pages, page_list);
 
+	/*
+	 * If reclaim is encountering dirty pages, it may be because

mm/vmscan.c keep_lumpy: · line 953

Quoted 10 lines
mm/vmscan.c +10 -0
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));
+

Thank you. Ok, I'll check what happens in memcg.

Can I add

	if (sc->memcg) {
		memcg_check_flusher_wakeup()
	}

or some here ?

Hm, maybe memcg should wake up flusher at starting try_to_free_memory_cgroup_pages().

Thanks,
-Kame

--
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>

author
#57 lore raw

Re: [PATCH 10/10] vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

Quoted 80 lines · KAMEZAWA Hiroyuki
Quoted 77 lines · On Mon, 6 Sep 2010 11:47:33 +0100 Mel Gorman <[email protected]> wrote:

There are a number of cases where pages get cleaned but two of concern to this patch are;
o When dirtying pages, processes may be throttled to clean pages if dirty_ratio is not met.
o Pages belonging to inodes dirtied longer than dirty_writeback_centisecs get cleaned.

The problem for reclaim is that dirty pages can reach the end of the LRU if pages are being dirtied slowly so that neither the throttling or a flusher thread waking periodically cleans them.

Background flush is already cleaning old or expired inodes first but the expire time is too far in the future at the time of page reclaim. To mitigate future problems, this patch wakes flusher threads to clean 4M of data - an amount that should be manageable without causing congestion in many cases.

Ideally, the background flushers would only be cleaning pages belonging to the zone being scanned but it's not clear if this would be of benefit (less IO) or not (potentially less efficient IO if an inode is scattered across multiple zones).

---
 mm/vmscan.c |   32 ++++++++++++++++++++++++++++++--
 1 files changed, 30 insertions(+), 2 deletions(-)
mm/vmscan.c +16 -0
index 408c101..33d27a4 100644
@@ -148,6 +148,18 @@ static DECLARE_RWSEM(shrinker_rwsem);
 /* Direct lumpy reclaim waits up to five seconds for background cleaning */
 #define MAX_SWAP_CLEAN_WAIT 50
 
+/*
+ * When reclaim encounters dirty data, wakeup flusher threads to clean
+ * a maximum of 4M of data.
+ */
+#define MAX_WRITEBACK (4194304UL >> PAGE_SHIFT)
+#define WRITEBACK_FACTOR (MAX_WRITEBACK / SWAP_CLUSTER_MAX)
+static inline long nr_writeback_pages(unsigned long nr_dirty)
+{
+	return laptop_mode ? 0 :
+			min(MAX_WRITEBACK, (nr_dirty * WRITEBACK_FACTOR));
+}
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -686,12 +698,14 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
 					struct scan_control *sc,
+					int file,
 					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
 	unsigned long nr_dirty = 0;
+	unsigned long nr_dirty_seen = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -790,6 +804,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			nr_dirty_seen++;
+
 			/*
 			 * Only kswapd can writeback filesystem pages to
 			 * avoid risk of stack overflow
@@ -923,6 +939,18 @@ keep_lumpy:
 
 	list_splice(&ret_pages, page_list);
 
Quoted 15 lines

mm/vmscan.c keep_lumpy: · line 953

Quoted 12 lines
mm/vmscan.c +12 -0
+	/*
+	 * If reclaim is encountering dirty pages, it may be because
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));
+

Thank you. Ok, I'll check what happens in memcg.

Thanks

Quoted 6 lines

Can I add

	if (sc->memcg) {
		memcg_check_flusher_wakeup()
	}

or some here ?

It seems reasonable.

Quoted 2 lines

Hm, maybe memcg should wake up flusher at starting try_to_free_memory_cgroup_pages().

I'm afraid I cannot make a judgement call on which is the best as I am not very familiar with how cgroups behave in comparison to normal reclaim. There could easily be a follow-on patch though that was cgroup specific?

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
#58 lore raw

Re: [PATCH 10/10] vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

Quoted 108 lines · On Thu, 9 Sep 2010 10:32:11 +0100 Mel Gorman <[email protected]> wrote:
Quoted 80 lines · KAMEZAWA Hiroyuki
Quoted 77 lines · On Mon, 6 Sep 2010 11:47:33 +0100 Mel Gorman <[email protected]> wrote:

There are a number of cases where pages get cleaned but two of concern to this patch are;
o When dirtying pages, processes may be throttled to clean pages if dirty_ratio is not met.
o Pages belonging to inodes dirtied longer than dirty_writeback_centisecs get cleaned.

The problem for reclaim is that dirty pages can reach the end of the LRU if pages are being dirtied slowly so that neither the throttling or a flusher thread waking periodically cleans them.

Background flush is already cleaning old or expired inodes first but the expire time is too far in the future at the time of page reclaim. To mitigate future problems, this patch wakes flusher threads to clean 4M of data - an amount that should be manageable without causing congestion in many cases.

Ideally, the background flushers would only be cleaning pages belonging to the zone being scanned but it's not clear if this would be of benefit (less IO) or not (potentially less efficient IO if an inode is scattered across multiple zones).

---
 mm/vmscan.c |   32 ++++++++++++++++++++++++++++++--
 1 files changed, 30 insertions(+), 2 deletions(-)
mm/vmscan.c +16 -0
index 408c101..33d27a4 100644
@@ -148,6 +148,18 @@ static DECLARE_RWSEM(shrinker_rwsem);
 /* Direct lumpy reclaim waits up to five seconds for background cleaning */
 #define MAX_SWAP_CLEAN_WAIT 50
 
+/*
+ * When reclaim encounters dirty data, wakeup flusher threads to clean
+ * a maximum of 4M of data.
+ */
+#define MAX_WRITEBACK (4194304UL >> PAGE_SHIFT)
+#define WRITEBACK_FACTOR (MAX_WRITEBACK / SWAP_CLUSTER_MAX)
+static inline long nr_writeback_pages(unsigned long nr_dirty)
+{
+	return laptop_mode ? 0 :
+			min(MAX_WRITEBACK, (nr_dirty * WRITEBACK_FACTOR));
+}
+
 static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone,
 						  struct scan_control *sc)
 {
@@ -686,12 +698,14 @@ static noinline_for_stack void free_page_list(struct list_head *free_pages)
  */
 static unsigned long shrink_page_list(struct list_head *page_list,
 					struct scan_control *sc,
+					int file,
 					unsigned long *nr_still_dirty)
 {
 	LIST_HEAD(ret_pages);
 	LIST_HEAD(free_pages);
 	int pgactivate = 0;
 	unsigned long nr_dirty = 0;
+	unsigned long nr_dirty_seen = 0;
 	unsigned long nr_reclaimed = 0;
 
 	cond_resched();
@@ -790,6 +804,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		}
 
 		if (PageDirty(page)) {
+			nr_dirty_seen++;
+
 			/*
 			 * Only kswapd can writeback filesystem pages to
 			 * avoid risk of stack overflow
@@ -923,6 +939,18 @@ keep_lumpy:
 
 	list_splice(&ret_pages, page_list);
 
Quoted 15 lines

mm/vmscan.c keep_lumpy: · line 953

Quoted 12 lines
mm/vmscan.c +12 -0
+	/*
+	 * If reclaim is encountering dirty pages, it may be because
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));
+

Thank you. Ok, I'll check what happens in memcg.

Thanks

Quoted 6 lines

Can I add

	if (sc->memcg) {
		memcg_check_flusher_wakeup()
	}

or some here ?

It seems reasonable.

Quoted 8 lines
Quoted 2 lines

Hm, maybe memcg should wake up flusher at starting try_to_free_memory_cgroup_pages().

I'm afraid I cannot make a judgement call on which is the best as I am not very familiar with how cgroups behave in comparison to normal reclaim. There could easily be a follow-on patch though that was cgroup specific?

Yes, I'd like to make patches when this series is merged. It's not difficult and makes it clear how memcg and flusher works for getting good reviews.

Thanks,
-Kame

--
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>

#59 lore raw

Re: [PATCH 10/10] vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

In reply to Mel Gorman

mm/vmscan.c keep_lumpy: · line 952

Quoted 11 lines
mm/vmscan.c +11 -0
+	/*
+	 * If reclaim is encountering dirty pages, it may be because
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));

wakeup_flusher_threads() works, but seems not the pertinent one.

- locally, it needs some luck to clean the pages that direct reclaim is waiting on
- globally, it cleans up some dirty pages, however some heavy dirtier may quickly create new ones..

So how about taking the approaches in these patches?

- "[PATCH 4/4] vmscan: transfer async file writeback to the flusher"
- "[PATCH 15/17] mm: lower soft dirty limits on memory pressure"

In particular the first patch should work very nicely with memcg, as all pages of an inode typically belong to the same memcg. So doing write-around helps clean lots of dirty pages in the target LRU list in one shot.

Thanks,
Fengguang

--
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>

author
#60 lore raw

Re: [PATCH 10/10] vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

Quoted 14 lines · Wu Fengguang

mm/vmscan.c keep_lumpy: · line 952

Quoted 11 lines
mm/vmscan.c +11 -0
+	/*
+	 * If reclaim is encountering dirty pages, it may be because
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));

wakeup_flusher_threads() works, but seems not the pertinent one.

Quoted 1 line

- locally, it needs some luck to clean the pages that direct reclaim is waiting on

There is a certain amount of luck involved but it's depending on there being a correlation between old inodes and old pages on the LRU list. As long as that correlation is accurate, some relevant pages will get cleaned. Testing on previously released versions of this patch did show that the percentage of dirty pages encountered during reclaim were reduced as a result of this patch.

Quoted 8 lines

- globally, it cleans up some dirty pages, however some heavy dirtier may quickly create new ones..

So how about taking the approaches in these patches?

- "[PATCH 4/4] vmscan: transfer async file writeback to the flusher"
- "[PATCH 15/17] mm: lower soft dirty limits on memory pressure"

There is a lot going on in those patches. It's going to take me a while to figure them out and formulate an opinion.

Quoted 5 lines

In particular the first patch should work very nicely with memcg, as all pages of an inode typically belong to the same memcg. So doing write-around helps clean lots of dirty pages in the target LRU list in one shot.

It might but as there is also a correlation between old dirty inodes and the location of dirty pages, it is tricky to predict if it is better and if so, by how much.

Signature · 3 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab
#61 lore raw

Re: [PATCH 10/10] vmscan: Kick flusher threads to clean pages when reclaim is encountering dirty pages

Quoted 15 lines · Mel Gorman
Quoted 14 lines · Wu Fengguang

mm/vmscan.c keep_lumpy: · line 952

Quoted 11 lines
mm/vmscan.c +11 -0
+	/*
+	 * If reclaim is encountering dirty pages, it may be because
+	 * dirty pages are reaching the end of the LRU even though the
+	 * dirty_ratio may be satisified. In this case, wake flusher
+	 * threads to pro-actively clean up to a maximum of
+	 * 4 * SWAP_CLUSTER_MAX amount of data (usually 1/2MB) unless
+	 * !may_writepage indicates that this is a direct reclaimer in
+	 * laptop mode avoiding disk spin-ups
+	 */
+	if (file && nr_dirty_seen && sc->may_writepage)
+		wakeup_flusher_threads(nr_writeback_pages(nr_dirty));

wakeup_flusher_threads() works, but seems not the pertinent one.

Quoted 7 lines
Quoted 1 line

- locally, it needs some luck to clean the pages that direct reclaim is waiting on

There is a certain amount of luck involved but it's depending on there being a correlation between old inodes and old pages on the LRU list. As long as that correlation is accurate, some relevant pages will get cleaned. Testing on previously released versions of this patch did show that the percentage of dirty pages encountered during reclaim were reduced as a result of this patch.

Yup.

Quoted 11 lines
Quoted 8 lines

- globally, it cleans up some dirty pages, however some heavy dirtier may quickly create new ones..

So how about taking the approaches in these patches?

- "[PATCH 4/4] vmscan: transfer async file writeback to the flusher"
- "[PATCH 15/17] mm: lower soft dirty limits on memory pressure"

There is a lot going on in those patches. It's going to take me a while to figure them out and formulate an opinion.

OK. I also need some time off for doing other works :)

Quoted 9 lines
Quoted 5 lines

In particular the first patch should work very nicely with memcg, as all pages of an inode typically belong to the same memcg. So doing write-around helps clean lots of dirty pages in the target LRU list in one shot.

It might but as there is also a correlation between old dirty inodes and the location of dirty pages, it is tricky to predict if it is better and if so, by how much.

It at least guarantees to clean the one page pageout() is running into :) Others will depend on the locality/sequentiality of the workload. But as the write-around pages are in the same LRU lists, the vmscan code will hit them sooner or later.

Thanks,
Fengguang

author
#62 lore raw
In reply to Mel Gorman

*sigh*

The subject should have been [PATCH 0/10] of course.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
#63 lore raw
In reply to Mel Gorman
Quoted 17 lines

There have been numerous reports of stalls that pointed at the problem being somewhere in the VM. There are multiple roots to the problems which means dealing with any of the root problems in isolation is tricky to justify on their own and they would still need integration testing. This patch series gathers together three different patch sets which in combination should tackle some of the root causes of latency problems being reported.

The first patch improves vmscan latency by tracking when pages get reclaimed by shrink_inactive_list. For this series, the most important results is being able to calculate the scanning/reclaim ratio as a measure of the amount of work being done by page reclaim.

Patches 2 and 3 account for the time spent in congestion_wait() and avoids calling going to sleep on congestion when it is unnecessary. This is expected to reduce stalls in situations where the system is under memory pressure but not due to congestion.

Quoted 7 lines

Patches 4-8 were originally developed by Kosaki Motohiro but reworked for this series. It has been noted that lumpy reclaim is far too aggressive and trashes the system somewhat. As SLUB uses high-order allocations, a large cost incurred by lumpy reclaim will be noticeable. It was also reported during transparent hugepage support testing that lumpy reclaim was trashing the system and these patches should mitigate that problem without disabling lumpy reclaim.

Wow, I'm sorry my lazyness bother you. I'll join to test this patch series ASAP and take a feedback soon.

author
#64 lore raw
Quoted 25 lines · KOSAKI Motohiro
Quoted 17 lines

There have been numerous reports of stalls that pointed at the problem being somewhere in the VM. There are multiple roots to the problems which means dealing with any of the root problems in isolation is tricky to justify on their own and they would still need integration testing. This patch series gathers together three different patch sets which in combination should tackle some of the root causes of latency problems being reported.

The first patch improves vmscan latency by tracking when pages get reclaimed by shrink_inactive_list. For this series, the most important results is being able to calculate the scanning/reclaim ratio as a measure of the amount of work being done by page reclaim.

Patches 2 and 3 account for the time spent in congestion_wait() and avoids calling going to sleep on congestion when it is unnecessary. This is expected to reduce stalls in situations where the system is under memory pressure but not due to congestion.

Quoted 7 lines

Patches 4-8 were originally developed by Kosaki Motohiro but reworked for this series. It has been noted that lumpy reclaim is far too aggressive and trashes the system somewhat. As SLUB uses high-order allocations, a large cost incurred by lumpy reclaim will be noticeable. It was also reported during transparent hugepage support testing that lumpy reclaim was trashing the system and these patches should mitigate that problem without disabling lumpy reclaim.

Quoted 3 lines

Wow, I'm sorry my lazyness bother you. I'll join to test this patch series ASAP and take a feedback soon.

It did not bother me at all. I generally agreed with the direction and it seemed sensible to take them into consideration before patches 9 and 10 in particular and make sure they all played nicely together.

Signature · 9 lines
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

--
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>
#65 lore raw
In reply to Mel Gorman

On Mon, Sep 6, 2010 at 7:47 PM, Mel Gorman <[email protected]> wrote:

<snip>

Quoted 28 lines

These are just the raw figures taken from /proc/vmstat. It's a rough measure of reclaim activity. Note that allocstall counts are higher because we are entering direct reclaim more often as a result of not sleeping in congestion. In itself, it's not necessarily a bad thing. It's easier to get a view of what happened from the vmscan tracepoint report.

FTrace Reclaim Statistics: vmscan micro-traceonly-v1r5-micromicro-nocongest-v1r5-micromicro-lowlumpy-v1r5-micromicro-nodirect-v1r5-micro traceonly-v1r5    nocongest-v1r5     lowlumpy-v1r5     nodirect-v1r5
Direct reclaims                                152        941        967        729
Direct reclaim pages scanned                507377    1404350    1332420    1450213
Direct reclaim pages reclaimed               10968      72042      77186      41097
Direct reclaim write file async I/O              0          0          0          0
Direct reclaim write anon async I/O              0          0          0          0
Direct reclaim write file sync I/O               0          0          0          0
Direct reclaim write anon sync I/O               0          0          0          0
Wake kswapd requests                        127195     241025     254825     188846
Kswapd wakeups                                   6          1          1          1
Kswapd pages scanned                       4210101    3345122    3427915    3306356
Kswapd pages reclaimed                     2228073    2165721    2143876    2194611
Kswapd reclaim write file async I/O              0          0          0          0
Kswapd reclaim write anon async I/O              0          0          0          0
Kswapd reclaim write file sync I/O               0          0          0          0
Kswapd reclaim write anon sync I/O               0          0          0          0
Time stalled direct reclaim (seconds)         7.60       3.03       3.24       3.43
Time kswapd awake (seconds)                  12.46       9.46       9.56       9.40

Quoted 7 lines

Total pages scanned                        4717478   4749472   4760335   4756569 Total pages reclaimed                      2239041   2237763   2221062   2235708 %age total pages scanned/reclaimed          47.46%    47.12%    46.66%    47.00% %age total pages scanned/written             0.00%     0.00%     0.00%     0.00% %age  file pages scanned/written             0.00%     0.00%     0.00%     0.00% Percentage Time Spent Direct Reclaim        43.80%    21.38%    22.34%    23.46% Percentage Time kswapd Awake                79.92%    79.56%    79.20%    80.48%

There is a nitpick about stalled reclaim time.
For example, In direct reclaim

===

       trace_mm_vmscan_direct_reclaim_begin(order,
                               sc.may_writepage,
                               gfp_mask);

       nr_reclaimed = do_try_to_free_pages(zonelist, &sc);

       trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);

===

In this case, Isn't this time accumulated value?
My point is following as.

Process A Process B
direct reclaim begin
do_try_to_free_pages
cond_resched

direct reclaim begin

do_try_to_free_pages

direct reclaim end
direct reclaim end

So A's result includes B's time so total stall time would be bigger than real.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>