adding patch proposed to resolve kbug 12309
[kernel-bfs] / kernel-power-2.6.28 / debian / patches / issue_12309_0.diff
1 Fix "system goes unresponsive under memory pressure and lots of
2 dirty/writeback pages" bug.
3
4         http://lkml.org/lkml/2010/4/4/86
5
6 In the above thread, Andreas Mohr described that
7
8         Invoking any command locked up for minutes (note that I'm
9         talking about attempted additional I/O to the _other_,
10         _unaffected_ main system HDD - such as loading some shell
11         binaries -, NOT the external SSD18M!!).
12
13 This happens when the two conditions are both meet:
14  - under memory pressure
15  - writing heavily to a slow device
16
17 OOM also happens in Andreas' system. The OOM trace shows that 3
18 processes are stuck in wait_on_page_writeback() in the direct reclaim
19 path. One in do_fork() and the other two in unix_stream_sendmsg(). They
20 are blocked on this condition:
21
22         (sc->order && priority < DEF_PRIORITY - 2)
23
24 which was introduced in commit 78dc583d (vmscan: low order lumpy reclaim
25 also should use PAGEOUT_IO_SYNC) one year ago. That condition may be too
26 permissive. In Andreas' case, 512MB/1024 = 512KB. If the direct reclaim
27 for the order-1 fork() allocation runs into a range of 512KB
28 hard-to-reclaim LRU pages, it will be stalled.
29
30 It's a severe problem in three ways.
31
32 Firstly, it can easily happen in daily desktop usage.  vmscan priority
33 can easily go below (DEF_PRIORITY - 2) on _local_ memory pressure. Even
34 if the system has 50% globally reclaimable pages, it still has good
35 opportunity to have 0.1% sized hard-to-reclaim ranges. For example, a
36 simple dd can easily create a big range (up to 20%) of dirty pages in
37 the LRU lists. And order-1 to order-3 allocations are more than common
38 with SLUB. Try "grep -v '1 :' /proc/slabinfo" to get the list of high
39 order slab caches. For example, the order-1 radix_tree_node slab cache
40 may stall applications at swap-in time; the order-3 inode cache on most
41 filesystems may stall applications when trying to read some file; the
42 order-2 proc_inode_cache may stall applications when trying to open a
43 /proc file.
44
45 Secondly, once triggered, it will stall unrelated processes (not doing IO
46 at all) in the system. This "one slow USB device stalls the whole system"
47 avalanching effect is very bad.
48
49 Thirdly, once stalled, the stall time could be intolerable long for the
50 users.  When there are 20MB queued writeback pages and USB 1.1 is
51 writing them in 1MB/s, wait_on_page_writeback() will stuck for up to 20
52 seconds.  Not to mention it may be called multiple times.
53
54 So raise the bar to only enable PAGEOUT_IO_SYNC when priority goes below
55 DEF_PRIORITY/3, or 6.25% LRU size. As the default dirty throttle ratio is
56  20%, it will hardly be triggered by pure dirty pages. We'd better treat
57 PAGEOUT_IO_SYNC as some last resort workaround -- its stall time is so
58 uncomfortably long (easily goes beyond 1s).
59
60 The bar is only raised for (order < PAGE_ALLOC_COSTLY_ORDER) allocations,
61 which are easy to satisfy in 1TB memory boxes. So, although 6.25% of
62 memory could be an awful lot of pages to scan on a system with 1TB of
63 memory, it won't really have to busy scan that much.
64
65 Andreas tested an older version of this patch and reported that it
66 mostly fixed his problem. Mel Gorman helped improve it and KOSAKI
67 Motohiro will fix it further in the next patch.
68
69 Reported-by: Andreas Mohr <andi@lisas.de>
70 Reviewed-by: Minchan Kim <minchan.kim@gmail.com>
71 Signed-off-by: Mel Gorman <mel@csn.ul.ie>
72 Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
73 ---
74
75 Index: kernel-2.6.28/mm/vmscan.c
76 ===================================================================
77 --- kernel-2.6.28.orig/mm/vmscan.c
78 +++ kernel-2.6.28/mm/vmscan.c
79 @@ -1024,6 +1024,47 @@ int isolate_lru_page(struct page *page)
80  }
81  
82  /*
83 + * Returns true if the caller should wait to clean dirty/writeback pages.
84 + *
85 + * If we are direct reclaiming for contiguous pages and we do not reclaim
86 + * everything in the list, try again and wait for writeback IO to complete.
87 + * This will stall high-order allocations noticeably. Only do that when really
88 + * need to free the pages under high memory pressure.
89 + */
90 +static inline bool should_reclaim_stall(unsigned long nr_taken,
91 +                                       unsigned long nr_freed,
92 +                                       int priority,
93 +                                       struct scan_control *sc)
94 +{
95 +       int lumpy_stall_priority;
96 +
97 +       /* kswapd should not stall on sync IO */
98 +       if (current_is_kswapd())
99 +               return false;
100 +
101 +       /* Only stall on lumpy reclaim */
102 +       if (!sc->lumpy_reclaim_mode)
103 +               return false;
104 +
105 +       /* If we have relaimed everything on the isolated list, no stall */
106 +       if (nr_freed == nr_taken)
107 +               return false;
108 +
109 +       /*
110 +        * For high-order allocations, there are two stall thresholds.
111 +        * High-cost allocations stall immediately where as lower
112 +        * order allocations such as stacks require the scanning
113 +        * priority to be much higher before stalling.
114 +        */
115 +       if (sc->order > PAGE_ALLOC_COSTLY_ORDER)
116 +               lumpy_stall_priority = DEF_PRIORITY;
117 +       else
118 +               lumpy_stall_priority = DEF_PRIORITY / 3;
119 +
120 +       return priority <= lumpy_stall_priority;
121 +}
122 +
123 +/*
124   * shrink_inactive_list() is a helper for shrink_zone().  It returns the number
125   * of reclaimed pages
126   */
127 @@ -1088,15 +1129,8 @@ static unsigned long shrink_inactive_lis
128                 nr_scanned += nr_scan;
129                 nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC);
130  
131 -               /*
132 -                * If we are direct reclaiming for contiguous pages and we do
133 -                * not reclaim everything in the list, try again and wait
134 -                * for IO to complete. This will stall high-order allocations
135 -                * but that should be acceptable to the caller
136 -                */
137 -               if (nr_freed < nr_taken && !current_is_kswapd() &&
138 -                                       sc->order > PAGE_ALLOC_COSTLY_ORDER) {
139 -                       congestion_wait(WRITE, HZ/10);
140 +        /* Check if we should syncronously wait for writeback */
141 +               if (should_reclaim_stall(nr_taken, nr_freed, priority, sc)) {
142  
143                         /*
144                          * The attempt at page out may have made some