remove libdss from Makefile
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / mm / page_isolation.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
a5d76b54
KH
2/*
3 * linux/mm/page_isolation.c
4 */
5
a5d76b54
KH
6#include <linux/mm.h>
7#include <linux/page-isolation.h>
8#include <linux/pageblock-flags.h>
ee6f509c 9#include <linux/memory.h>
c8721bbb 10#include <linux/hugetlb.h>
83358ece 11#include <linux/page_owner.h>
8b913238 12#include <linux/migrate.h>
a5d76b54
KH
13#include "internal.h"
14
0f0848e5
JK
15#define CREATE_TRACE_POINTS
16#include <trace/events/page_isolation.h>
17
c5b4e1b0
NH
18static int set_migratetype_isolate(struct page *page,
19 bool skip_hwpoisoned_pages)
ee6f509c
MK
20{
21 struct zone *zone;
22 unsigned long flags, pfn;
23 struct memory_isolate_notify arg;
24 int notifier_ret;
25 int ret = -EBUSY;
26
27 zone = page_zone(page);
28
29 spin_lock_irqsave(&zone->lock, flags);
30
31 pfn = page_to_pfn(page);
32 arg.start_pfn = pfn;
33 arg.nr_pages = pageblock_nr_pages;
34 arg.pages_found = 0;
35
36 /*
37 * It may be possible to isolate a pageblock even if the
38 * migratetype is not MIGRATE_MOVABLE. The memory isolation
39 * notifier chain is used by balloon drivers to return the
40 * number of pages in a range that are held by the balloon
41 * driver to shrink memory. If all the pages are accounted for
42 * by balloons, are free, or on the LRU, isolation can continue.
43 * Later, for example, when memory hotplug notifier runs, these
44 * pages reported as "can be isolated" should be isolated(freed)
45 * by the balloon driver through the memory notifier chain.
46 */
47 notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
48 notifier_ret = notifier_to_errno(notifier_ret);
49 if (notifier_ret)
50 goto out;
51 /*
52 * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
53 * We just check MOVABLE pages.
54 */
b023f468
WC
55 if (!has_unmovable_pages(zone, page, arg.pages_found,
56 skip_hwpoisoned_pages))
ee6f509c
MK
57 ret = 0;
58
59 /*
ac34dcd2 60 * immobile means "not-on-lru" pages. If immobile is larger than
ee6f509c
MK
61 * removable-by-driver pages reported by notifier, we'll fail.
62 */
63
64out:
65 if (!ret) {
2139cbe6 66 unsigned long nr_pages;
d1ce749a 67 int migratetype = get_pageblock_migratetype(page);
2139cbe6 68
a458431e 69 set_pageblock_migratetype(page, MIGRATE_ISOLATE);
ad53f92e 70 zone->nr_isolate_pageblock++;
02aa0cdd
VB
71 nr_pages = move_freepages_block(zone, page, MIGRATE_ISOLATE,
72 NULL);
2139cbe6 73
d1ce749a 74 __mod_zone_freepage_state(zone, -nr_pages, migratetype);
ee6f509c
MK
75 }
76
77 spin_unlock_irqrestore(&zone->lock, flags);
78 if (!ret)
ec25af84 79 drain_all_pages(zone);
ee6f509c
MK
80 return ret;
81}
82
c5b4e1b0 83static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
ee6f509c
MK
84{
85 struct zone *zone;
2139cbe6 86 unsigned long flags, nr_pages;
e3a2713c 87 bool isolated_page = false;
3c605096 88 unsigned int order;
76741e77 89 unsigned long pfn, buddy_pfn;
3c605096 90 struct page *buddy;
2139cbe6 91
ee6f509c
MK
92 zone = page_zone(page);
93 spin_lock_irqsave(&zone->lock, flags);
bbf9ce97 94 if (!is_migrate_isolate_page(page))
ee6f509c 95 goto out;
3c605096
JK
96
97 /*
98 * Because freepage with more than pageblock_order on isolated
99 * pageblock is restricted to merge due to freepage counting problem,
100 * it is possible that there is free buddy page.
101 * move_freepages_block() doesn't care of merge so we need other
102 * approach in order to merge them. Isolation and free will make
103 * these pages to be merged.
104 */
105 if (PageBuddy(page)) {
106 order = page_order(page);
107 if (order >= pageblock_order) {
76741e77
VB
108 pfn = page_to_pfn(page);
109 buddy_pfn = __find_buddy_pfn(pfn, order);
110 buddy = page + (buddy_pfn - pfn);
3c605096 111
13ad59df 112 if (pfn_valid_within(buddy_pfn) &&
1ae7013d 113 !is_migrate_isolate_page(buddy)) {
3c605096 114 __isolate_free_page(page, order);
e3a2713c 115 isolated_page = true;
3c605096
JK
116 }
117 }
118 }
119
120 /*
121 * If we isolate freepage with more than pageblock_order, there
122 * should be no freepage in the range, so we could avoid costly
123 * pageblock scanning for freepage moving.
124 */
125 if (!isolated_page) {
02aa0cdd 126 nr_pages = move_freepages_block(zone, page, migratetype, NULL);
3c605096
JK
127 __mod_zone_freepage_state(zone, nr_pages, migratetype);
128 }
a458431e 129 set_pageblock_migratetype(page, migratetype);
ad53f92e 130 zone->nr_isolate_pageblock--;
ee6f509c
MK
131out:
132 spin_unlock_irqrestore(&zone->lock, flags);
83358ece 133 if (isolated_page) {
46f24fd8 134 post_alloc_hook(page, order, __GFP_MOVABLE);
e3a2713c 135 __free_pages(page, order);
83358ece 136 }
ee6f509c
MK
137}
138
a5d76b54
KH
139static inline struct page *
140__first_valid_page(unsigned long pfn, unsigned long nr_pages)
141{
142 int i;
2ce13640
MH
143
144 for (i = 0; i < nr_pages; i++) {
145 struct page *page;
146
147 if (!pfn_valid_within(pfn + i))
148 continue;
149 page = pfn_to_online_page(pfn + i);
150 if (!page)
151 continue;
152 return page;
153 }
154 return NULL;
a5d76b54
KH
155}
156
157/*
158 * start_isolate_page_range() -- make page-allocation-type of range of pages
159 * to be MIGRATE_ISOLATE.
160 * @start_pfn: The lower PFN of the range to be isolated.
161 * @end_pfn: The upper PFN of the range to be isolated.
0815f3d8 162 * @migratetype: migrate type to set in error recovery.
a5d76b54
KH
163 *
164 * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in
165 * the range will never be allocated. Any free pages and pages freed in the
166 * future will not be allocated again.
167 *
168 * start_pfn/end_pfn must be aligned to pageblock_order.
169 * Returns 0 on success and -EBUSY if any part of range cannot be isolated.
170 */
0815f3d8 171int start_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
b023f468 172 unsigned migratetype, bool skip_hwpoisoned_pages)
a5d76b54
KH
173{
174 unsigned long pfn;
175 unsigned long undo_pfn;
176 struct page *page;
177
fec174d6
NH
178 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
179 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
a5d76b54
KH
180
181 for (pfn = start_pfn;
182 pfn < end_pfn;
183 pfn += pageblock_nr_pages) {
184 page = __first_valid_page(pfn, pageblock_nr_pages);
b023f468
WC
185 if (page &&
186 set_migratetype_isolate(page, skip_hwpoisoned_pages)) {
a5d76b54
KH
187 undo_pfn = pfn;
188 goto undo;
189 }
190 }
191 return 0;
192undo:
193 for (pfn = start_pfn;
dbc0e4ce 194 pfn < undo_pfn;
2ce13640
MH
195 pfn += pageblock_nr_pages) {
196 struct page *page = pfn_to_online_page(pfn);
197 if (!page)
198 continue;
199 unset_migratetype_isolate(page, migratetype);
200 }
a5d76b54
KH
201
202 return -EBUSY;
203}
204
205/*
206 * Make isolated pages available again.
207 */
0815f3d8
MN
208int undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn,
209 unsigned migratetype)
a5d76b54
KH
210{
211 unsigned long pfn;
212 struct page *page;
6f8d2b8a
WX
213
214 BUG_ON(!IS_ALIGNED(start_pfn, pageblock_nr_pages));
215 BUG_ON(!IS_ALIGNED(end_pfn, pageblock_nr_pages));
216
a5d76b54
KH
217 for (pfn = start_pfn;
218 pfn < end_pfn;
219 pfn += pageblock_nr_pages) {
220 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 221 if (!page || !is_migrate_isolate_page(page))
a5d76b54 222 continue;
0815f3d8 223 unset_migratetype_isolate(page, migratetype);
a5d76b54
KH
224 }
225 return 0;
226}
227/*
228 * Test all pages in the range is free(means isolated) or not.
229 * all pages in [start_pfn...end_pfn) must be in the same zone.
230 * zone->lock must be held before call this.
231 *
ec3b6882 232 * Returns the last tested pfn.
a5d76b54 233 */
fea85cff 234static unsigned long
b023f468
WC
235__test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,
236 bool skip_hwpoisoned_pages)
a5d76b54
KH
237{
238 struct page *page;
239
240 while (pfn < end_pfn) {
241 if (!pfn_valid_within(pfn)) {
242 pfn++;
243 continue;
244 }
245 page = pfn_to_page(pfn);
aa016d14 246 if (PageBuddy(page))
435b405c 247 /*
aa016d14
VB
248 * If the page is on a free list, it has to be on
249 * the correct MIGRATE_ISOLATE freelist. There is no
250 * simple way to verify that as VM_BUG_ON(), though.
435b405c 251 */
a5d76b54 252 pfn += 1 << page_order(page);
aa016d14
VB
253 else if (skip_hwpoisoned_pages && PageHWPoison(page))
254 /* A HWPoisoned page cannot be also PageBuddy */
b023f468 255 pfn++;
a5d76b54
KH
256 else
257 break;
258 }
fea85cff
JK
259
260 return pfn;
a5d76b54
KH
261}
262
b9eb6319 263/* Caller should ensure that requested range is in a single zone */
b023f468
WC
264int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
265 bool skip_hwpoisoned_pages)
a5d76b54 266{
6c1b7f68 267 unsigned long pfn, flags;
a5d76b54 268 struct page *page;
6c1b7f68 269 struct zone *zone;
a5d76b54 270
a5d76b54 271 /*
85dbe706
TC
272 * Note: pageblock_nr_pages != MAX_ORDER. Then, chunks of free pages
273 * are not aligned to pageblock_nr_pages.
274 * Then we just check migratetype first.
a5d76b54
KH
275 */
276 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
277 page = __first_valid_page(pfn, pageblock_nr_pages);
bbf9ce97 278 if (page && !is_migrate_isolate_page(page))
a5d76b54
KH
279 break;
280 }
a70dcb96
GS
281 page = __first_valid_page(start_pfn, end_pfn - start_pfn);
282 if ((pfn < end_pfn) || !page)
a5d76b54 283 return -EBUSY;
85dbe706 284 /* Check all pages are free or marked as ISOLATED */
a70dcb96 285 zone = page_zone(page);
6c1b7f68 286 spin_lock_irqsave(&zone->lock, flags);
fea85cff 287 pfn = __test_page_isolated_in_pageblock(start_pfn, end_pfn,
b023f468 288 skip_hwpoisoned_pages);
75d5932f
CK
289
290 if (pfn < end_pfn) {
323f553b
OE
291 pr_info_ratelimited("%s: page of pfn %lu is not isolated\n", __func__, pfn);
292 /* __dump_page(pfn_to_page(pfn), "isolation failure"); */
75d5932f
CK
293 }
294
6c1b7f68 295 spin_unlock_irqrestore(&zone->lock, flags);
fea85cff 296
0f0848e5
JK
297 trace_test_pages_isolated(start_pfn, end_pfn, pfn);
298
fea85cff 299 return pfn < end_pfn ? -EBUSY : 0;
a5d76b54 300}
723a0644
MK
301
302struct page *alloc_migrate_target(struct page *page, unsigned long private,
303 int **resultp)
304{
8b913238 305 return new_page_nodemask(page, numa_node_id(), &node_states[N_MEMORY]);
723a0644 306}