fb482cf438daa1709b8f380df22303d506186664
[platform/upstream/kernel-adaptation-pc.git] / mm / page_isolation.c
1 /*
2  * linux/mm/page_isolation.c
3  */
4
5 #include <linux/mm.h>
6 #include <linux/page-isolation.h>
7 #include <linux/pageblock-flags.h>
8 #include <linux/memory.h>
9 #include "internal.h"
10
11 int set_migratetype_isolate(struct page *page)
12 {
13         struct zone *zone;
14         unsigned long flags, pfn;
15         struct memory_isolate_notify arg;
16         int notifier_ret;
17         int ret = -EBUSY;
18
19         zone = page_zone(page);
20
21         spin_lock_irqsave(&zone->lock, flags);
22
23         pfn = page_to_pfn(page);
24         arg.start_pfn = pfn;
25         arg.nr_pages = pageblock_nr_pages;
26         arg.pages_found = 0;
27
28         /*
29          * It may be possible to isolate a pageblock even if the
30          * migratetype is not MIGRATE_MOVABLE. The memory isolation
31          * notifier chain is used by balloon drivers to return the
32          * number of pages in a range that are held by the balloon
33          * driver to shrink memory. If all the pages are accounted for
34          * by balloons, are free, or on the LRU, isolation can continue.
35          * Later, for example, when memory hotplug notifier runs, these
36          * pages reported as "can be isolated" should be isolated(freed)
37          * by the balloon driver through the memory notifier chain.
38          */
39         notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
40         notifier_ret = notifier_to_errno(notifier_ret);
41         if (notifier_ret)
42                 goto out;
43         /*
44          * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
45          * We just check MOVABLE pages.
46          */
47         if (!has_unmovable_pages(zone, page, arg.pages_found))
48                 ret = 0;
49
50         /*
51          * immobile means "not-on-lru" paes. If immobile is larger than
52          * removable-by-driver pages reported by notifier, we'll fail.
53          */
54
55 out:
56         if (!ret) {
57                 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
58                 move_freepages_block(zone, page, MIGRATE_ISOLATE);
59         }
60
61         spin_unlock_irqrestore(&zone->lock, flags);
62         if (!ret)
63                 drain_all_pages();
64         return ret;
65 }
66
67 void unset_migratetype_isolate(struct page *page, unsigned migratetype)
68 {
69         struct zone *zone;
70         unsigned long flags;
71         zone = page_zone(page);
72         spin_lock_irqsave(&zone->lock, flags);
73         if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
74                 goto out;
75         set_pageblock_migratetype(page, migratetype);
76         move_freepages_block(zone, page, migratetype);
77 out:
78         spin_unlock_irqrestore(&zone->lock, flags);
79 }
80
81 static inline struct page *
82 __first_valid_page(unsigned long pfn, unsigned long nr_pages)
83 {
84         int i;
85         for (i = 0; i < nr_pages; i++)
86                 if (pfn_valid_within(pfn + i))
87                         break;
88         if (unlikely(i == nr_pages))
89                 return NULL;
90         return pfn_to_page(pfn + i);
91 }
92
93 /*
94  * start_isolate_page_range() -- make page-allocation-type of range of pages
95  * to be MIGRATE_ISOLATE.
96  * @start_pfn: The lower PFN of the range to be isolated.
97  * @end_pfn: The upper PFN of the range to be isolated.
98  * @migratetype: migrate type to set in error recovery.
99  *
100  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
101  * the range will never be allocated. Any free pages and pages freed in the
102  * future will not be allocated again.
103  *
104  * start_pfn/end_pfn must be aligned to pageblock_order.
105  * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
106  */
107 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
108                              unsigned migratetype)
109 {
110         unsigned long pfn;
111         unsigned long undo_pfn;
112         struct page *page;
113
114         BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
115         BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
116
117         for (pfn = start_pfn;
118              pfn < end_pfn;
119              pfn += pageblock_nr_pages) {
120                 page = __first_valid_page(pfn, pageblock_nr_pages);
121                 if (page && set_migratetype_isolate(page)) {
122                         undo_pfn = pfn;
123                         goto undo;
124                 }
125         }
126         return 0;
127 undo:
128         for (pfn = start_pfn;
129              pfn < undo_pfn;
130              pfn += pageblock_nr_pages)
131                 unset_migratetype_isolate(pfn_to_page(pfn), migratetype);
132
133         return -EBUSY;
134 }
135
136 /*
137  * Make isolated pages available again.
138  */
139 int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
140                             unsigned migratetype)
141 {
142         unsigned long pfn;
143         struct page *page;
144         BUG_ON((start_pfn) & (pageblock_nr_pages - 1));
145         BUG_ON((end_pfn) & (pageblock_nr_pages - 1));
146         for (pfn = start_pfn;
147              pfn < end_pfn;
148              pfn += pageblock_nr_pages) {
149                 page = __first_valid_page(pfn, pageblock_nr_pages);
150                 if (!page || get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
151                         continue;
152                 unset_migratetype_isolate(page, migratetype);
153         }
154         return 0;
155 }
156 /*
157  * Test all pages in the range is free(means isolated) or not.
158  * all pages in [start_pfn...end_pfn) must be in the same zone.
159  * zone->lock must be held before call this.
160  *
161  * Returns 1 if all pages in the range are isolated.
162  */
163 static int
164 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn)
165 {
166         struct page *page;
167
168         while (pfn < end_pfn) {
169                 if (!pfn_valid_within(pfn)) {
170                         pfn++;
171                         continue;
172                 }
173                 page = pfn_to_page(pfn);
174                 if (PageBuddy(page))
175                         pfn += 1 << page_order(page);
176                 else if (page_count(page) == 0 &&
177                                 page_private(page) == MIGRATE_ISOLATE)
178                         pfn += 1;
179                 else
180                         break;
181         }
182         if (pfn < end_pfn)
183                 return 0;
184         return 1;
185 }
186
187 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn)
188 {
189         unsigned long pfn, flags;
190         struct page *page;
191         struct zone *zone;
192         int ret;
193
194         /*
195          * Note: pageblock_nr_page != MAX_ORDER. Then, chunks of free page
196          * is not aligned to pageblock_nr_pages.
197          * Then we just check pagetype fist.
198          */
199         for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
200                 page = __first_valid_page(pfn, pageblock_nr_pages);
201                 if (page && get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
202                         break;
203         }
204         page = __first_valid_page(start_pfn, end_pfn - start_pfn);
205         if ((pfn < end_pfn) || !page)
206                 return -EBUSY;
207         /* Check all pages are free or Marked as ISOLATED */
208         zone = page_zone(page);
209         spin_lock_irqsave(&zone->lock, flags);
210         ret = __test_page_isolated_in_pageblock(start_pfn, end_pfn);
211         spin_unlock_irqrestore(&zone->lock, flags);
212         return ret ? 0 : -EBUSY;
213 }