21af88b718aaac534d0507ebe95ff9c3ddc1c9d7
[linux-2.6-microblaze.git] / mm / page_isolation.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/mm/page_isolation.c
4  */
5
6 #include <linux/mm.h>
7 #include <linux/page-isolation.h>
8 #include <linux/pageblock-flags.h>
9 #include <linux/memory.h>
10 #include <linux/hugetlb.h>
11 #include <linux/page_owner.h>
12 #include <linux/migrate.h>
13 #include "internal.h"
14
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/page_isolation.h>
17
18 static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
19 {
20         struct zone *zone;
21         unsigned long flags;
22         int ret = -EBUSY;
23
24         zone = page_zone(page);
25
26         spin_lock_irqsave(&zone->lock, flags);
27
28         /*
29          * We assume the caller intended to SET migrate type to isolate.
30          * If it is already set, then someone else must have raced and
31          * set it before us.  Return -EBUSY
32          */
33         if (is_migrate_isolate_page(page))
34                 goto out;
35
36         /*
37          * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
38          * We just check MOVABLE pages.
39          */
40         if (!has_unmovable_pages(zone, page, 0, migratetype, isol_flags)) {
41                 unsigned long nr_pages;
42                 int mt = get_pageblock_migratetype(page);
43
44                 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
45                 zone->nr_isolate_pageblock++;
46                 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
47                                                                         NULL);
48
49                 __mod_zone_freepage_state(zone, -nr_pages, mt);
50                 ret = 0;
51         }
52
53 out:
54         spin_unlock_irqrestore(&zone->lock, flags);
55         if (!ret)
56                 drain_all_pages(zone);
57         return ret;
58 }
59
60 static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
61 {
62         struct zone *zone;
63         unsigned long flags, nr_pages;
64         bool isolated_page = false;
65         unsigned int order;
66         unsigned long pfn, buddy_pfn;
67         struct page *buddy;
68
69         zone = page_zone(page);
70         spin_lock_irqsave(&zone->lock, flags);
71         if (!is_migrate_isolate_page(page))
72                 goto out;
73
74         /*
75          * Because freepage with more than pageblock_order on isolated
76          * pageblock is restricted to merge due to freepage counting problem,
77          * it is possible that there is free buddy page.
78          * move_freepages_block() doesn't care of merge so we need other
79          * approach in order to merge them. Isolation and free will make
80          * these pages to be merged.
81          */
82         if (PageBuddy(page)) {
83                 order = page_order(page);
84                 if (order >= pageblock_order) {
85                         pfn = page_to_pfn(page);
86                         buddy_pfn = __find_buddy_pfn(pfn, order);
87                         buddy = page + (buddy_pfn - pfn);
88
89                         if (pfn_valid_within(buddy_pfn) &&
90                             !is_migrate_isolate_page(buddy)) {
91                                 __isolate_free_page(page, order);
92                                 isolated_page = true;
93                         }
94                 }
95         }
96
97         /*
98          * If we isolate freepage with more than pageblock_order, there
99          * should be no freepage in the range, so we could avoid costly
100          * pageblock scanning for freepage moving.
101          */
102         if (!isolated_page) {
103                 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
104                 __mod_zone_freepage_state(zone, nr_pages, migratetype);
105         }
106         set_pageblock_migratetype(page, migratetype);
107         zone->nr_isolate_pageblock--;
108 out:
109         spin_unlock_irqrestore(&zone->lock, flags);
110         if (isolated_page) {
111                 post_alloc_hook(page, order, __GFP_MOVABLE);
112                 __free_pages(page, order);
113         }
114 }
115
116 static inline struct page *
117 __first_valid_page(unsigned long pfn, unsigned long nr_pages)
118 {
119         int i;
120
121         for (i = 0; i < nr_pages; i++) {
122                 struct page *page;
123
124                 page = pfn_to_online_page(pfn + i);
125                 if (!page)
126                         continue;
127                 return page;
128         }
129         return NULL;
130 }
131
132 /**
133  * start_isolate_page_range() - make page-allocation-type of range of pages to
134  * be MIGRATE_ISOLATE.
135  * @start_pfn:          The lower PFN of the range to be isolated.
136  * @end_pfn:            The upper PFN of the range to be isolated.
137  *                      start_pfn/end_pfn must be aligned to pageblock_order.
138  * @migratetype:        Migrate type to set in error recovery.
139  * @flags:              The following flags are allowed (they can be combined in
140  *                      a bit mask)
141  *                      MEMORY_OFFLINE - isolate to offline (!allocate) memory
142  *                                       e.g., skip over PageHWPoison() pages
143  *                      REPORT_FAILURE - report details about the failure to
144  *                      isolate the range
145  *
146  * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
147  * the range will never be allocated. Any free pages and pages freed in the
148  * future will not be allocated again. If specified range includes migrate types
149  * other than MOVABLE or CMA, this will fail with -EBUSY. For isolating all
150  * pages in the range finally, the caller have to free all pages in the range.
151  * test_page_isolated() can be used for test it.
152  *
153  * There is no high level synchronization mechanism that prevents two threads
154  * from trying to isolate overlapping ranges. If this happens, one thread
155  * will notice pageblocks in the overlapping range already set to isolate.
156  * This happens in set_migratetype_isolate, and set_migratetype_isolate
157  * returns an error. We then clean up by restoring the migration type on
158  * pageblocks we may have modified and return -EBUSY to caller. This
159  * prevents two threads from simultaneously working on overlapping ranges.
160  *
161  * Return: the number of isolated pageblocks on success and -EBUSY if any part
162  * of range cannot be isolated.
163  */
164 int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
165                              unsigned migratetype, int flags)
166 {
167         unsigned long pfn;
168         unsigned long undo_pfn;
169         struct page *page;
170         int nr_isolate_pageblock = 0;
171
172         BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
173         BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
174
175         for (pfn = start_pfn;
176              pfn < end_pfn;
177              pfn += pageblock_nr_pages) {
178                 page = __first_valid_page(pfn, pageblock_nr_pages);
179                 if (page) {
180                         if (set_migratetype_isolate(page, migratetype, flags)) {
181                                 undo_pfn = pfn;
182                                 goto undo;
183                         }
184                         nr_isolate_pageblock++;
185                 }
186         }
187         return nr_isolate_pageblock;
188 undo:
189         for (pfn = start_pfn;
190              pfn < undo_pfn;
191              pfn += pageblock_nr_pages) {
192                 struct page *page = pfn_to_online_page(pfn);
193                 if (!page)
194                         continue;
195                 unset_migratetype_isolate(page, migratetype);
196         }
197
198         return -EBUSY;
199 }
200
201 /*
202  * Make isolated pages available again.
203  */
204 void undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
205                             unsigned migratetype)
206 {
207         unsigned long pfn;
208         struct page *page;
209
210         BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
211         BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
212
213         for (pfn = start_pfn;
214              pfn < end_pfn;
215              pfn += pageblock_nr_pages) {
216                 page = __first_valid_page(pfn, pageblock_nr_pages);
217                 if (!page || !is_migrate_isolate_page(page))
218                         continue;
219                 unset_migratetype_isolate(page, migratetype);
220         }
221 }
222 /*
223  * Test all pages in the range is free(means isolated) or not.
224  * all pages in [start_pfn...end_pfn) must be in the same zone.
225  * zone->lock must be held before call this.
226  *
227  * Returns the last tested pfn.
228  */
229 static unsigned long
230 __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
231                                   int flags)
232 {
233         struct page *page;
234
235         while (pfn < end_pfn) {
236                 if (!pfn_valid_within(pfn)) {
237                         pfn++;
238                         continue;
239                 }
240                 page = pfn_to_page(pfn);
241                 if (PageBuddy(page))
242                         /*
243                          * If the page is on a free list, it has to be on
244                          * the correct MIGRATE_ISOLATE freelist. There is no
245                          * simple way to verify that as VM_BUG_ON(), though.
246                          */
247                         pfn += 1 << page_order(page);
248                 else if ((flags & MEMORY_OFFLINE) && PageHWPoison(page))
249                         /* A HWPoisoned page cannot be also PageBuddy */
250                         pfn++;
251                 else
252                         break;
253         }
254
255         return pfn;
256 }
257
258 /* Caller should ensure that requested range is in a single zone */
259 int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
260                         int isol_flags)
261 {
262         unsigned long pfn, flags;
263         struct page *page;
264         struct zone *zone;
265
266         /*
267          * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
268          * are not aligned to pageblock_nr_pages.
269          * Then we just check migratetype first.
270          */
271         for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
272                 page = __first_valid_page(pfn, pageblock_nr_pages);
273                 if (page && !is_migrate_isolate_page(page))
274                         break;
275         }
276         page = __first_valid_page(start_pfn, end_pfn - start_pfn);
277         if ((pfn < end_pfn) || !page)
278                 return -EBUSY;
279         /* Check all pages are free or marked as ISOLATED */
280         zone = page_zone(page);
281         spin_lock_irqsave(&zone->lock, flags);
282         pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn, isol_flags);
283         spin_unlock_irqrestore(&zone->lock, flags);
284
285         trace_test_pages_isolated(start_pfn, end_pfn, pfn);
286
287         return pfn < end_pfn ? -EBUSY : 0;
288 }
289
290 struct page *alloc_migrate_target(struct page *page, unsigned long private)
291 {
292         return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
293 }