]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/mm/swap.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
6 | */ | |
7 | ||
8 | /* | |
183ff22b | 9 | * This file contains the default values for the operation of the |
1da177e4 | 10 | * Linux VM subsystem. Fine-tuning documentation can be found in |
57043247 | 11 | * Documentation/admin-guide/sysctl/vm.rst. |
1da177e4 LT |
12 | * Started 18.12.91 |
13 | * Swap aging added 23.2.95, Stephen Tweedie. | |
14 | * Buffermem limits added 12.3.98, Rik van Riel. | |
15 | */ | |
16 | ||
17 | #include <linux/mm.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/kernel_stat.h> | |
20 | #include <linux/swap.h> | |
21 | #include <linux/mman.h> | |
22 | #include <linux/pagemap.h> | |
23 | #include <linux/pagevec.h> | |
24 | #include <linux/init.h> | |
b95f1b31 | 25 | #include <linux/export.h> |
1da177e4 | 26 | #include <linux/mm_inline.h> |
1da177e4 | 27 | #include <linux/percpu_counter.h> |
3565fce3 | 28 | #include <linux/memremap.h> |
1da177e4 LT |
29 | #include <linux/percpu.h> |
30 | #include <linux/cpu.h> | |
31 | #include <linux/notifier.h> | |
e0bf68dd | 32 | #include <linux/backing-dev.h> |
66e1707b | 33 | #include <linux/memcontrol.h> |
5a0e3ad6 | 34 | #include <linux/gfp.h> |
a27bb332 | 35 | #include <linux/uio.h> |
822fc613 | 36 | #include <linux/hugetlb.h> |
33c3fc71 | 37 | #include <linux/page_idle.h> |
b01b2141 | 38 | #include <linux/local_lock.h> |
8cc621d2 | 39 | #include <linux/buffer_head.h> |
1da177e4 | 40 | |
64d6519d LS |
41 | #include "internal.h" |
42 | ||
c6286c98 MG |
43 | #define CREATE_TRACE_POINTS |
44 | #include <trace/events/pagemap.h> | |
45 | ||
ea0ffd0c | 46 | /* How many pages do we try to swap or page in/out together? As a power of 2 */ |
1da177e4 | 47 | int page_cluster; |
ea0ffd0c | 48 | const int page_cluster_max = 31; |
1da177e4 | 49 | |
c2bc1681 | 50 | /* Protecting only lru_rotate.fbatch which requires disabling interrupts */ |
b01b2141 IM |
51 | struct lru_rotate { |
52 | local_lock_t lock; | |
c2bc1681 | 53 | struct folio_batch fbatch; |
b01b2141 IM |
54 | }; |
55 | static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = { | |
56 | .lock = INIT_LOCAL_LOCK(lock), | |
57 | }; | |
58 | ||
59 | /* | |
82ac64d8 | 60 | * The following folio batches are grouped together because they are protected |
b01b2141 IM |
61 | * by disabling preemption (and interrupts remain enabled). |
62 | */ | |
82ac64d8 | 63 | struct cpu_fbatches { |
b01b2141 | 64 | local_lock_t lock; |
70dea534 | 65 | struct folio_batch lru_add; |
7a3dbfe8 | 66 | struct folio_batch lru_deactivate_file; |
85cd7791 | 67 | struct folio_batch lru_deactivate; |
cec394ba | 68 | struct folio_batch lru_lazyfree; |
a4a921aa | 69 | #ifdef CONFIG_SMP |
3a44610b | 70 | struct folio_batch activate; |
a4a921aa | 71 | #endif |
b01b2141 | 72 | }; |
82ac64d8 | 73 | static DEFINE_PER_CPU(struct cpu_fbatches, cpu_fbatches) = { |
b01b2141 IM |
74 | .lock = INIT_LOCAL_LOCK(lock), |
75 | }; | |
902aaed0 | 76 | |
f1ee018b MWO |
77 | static void __page_cache_release(struct folio *folio, struct lruvec **lruvecp, |
78 | unsigned long *flagsp) | |
b221385b | 79 | { |
188e8cae | 80 | if (folio_test_lru(folio)) { |
f1ee018b MWO |
81 | folio_lruvec_relock_irqsave(folio, lruvecp, flagsp); |
82 | lruvec_del_folio(*lruvecp, folio); | |
188e8cae | 83 | __folio_clear_lru_flags(folio); |
b221385b | 84 | } |
f1ee018b MWO |
85 | |
86 | /* | |
87 | * In rare cases, when truncation or holepunching raced with | |
88 | * munlock after VM_LOCKED was cleared, Mlocked may still be | |
89 | * found set here. This does not indicate a problem, unless | |
90 | * "unevictable_pgs_cleared" appears worryingly large. | |
91 | */ | |
188e8cae MWO |
92 | if (unlikely(folio_test_mlocked(folio))) { |
93 | long nr_pages = folio_nr_pages(folio); | |
b109b870 | 94 | |
188e8cae MWO |
95 | __folio_clear_mlocked(folio); |
96 | zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages); | |
b109b870 HD |
97 | count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages); |
98 | } | |
91807063 AA |
99 | } |
100 | ||
f1ee018b MWO |
101 | /* |
102 | * This path almost never happens for VM activity - pages are normally freed | |
103 | * in batches. But it gets used by networking - and for compound pages. | |
104 | */ | |
105 | static void page_cache_release(struct folio *folio) | |
106 | { | |
107 | struct lruvec *lruvec = NULL; | |
108 | unsigned long flags; | |
109 | ||
110 | __page_cache_release(folio, &lruvec, &flags); | |
111 | if (lruvec) | |
112 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
113 | } | |
114 | ||
79a48287 | 115 | void __folio_put(struct folio *folio) |
1da177e4 | 116 | { |
79a48287 | 117 | if (unlikely(folio_is_zone_device(folio))) { |
9f100e3b | 118 | free_zone_device_folio(folio); |
79a48287 MWO |
119 | return; |
120 | } else if (folio_test_hugetlb(folio)) { | |
2542b1ac MWO |
121 | free_huge_folio(folio); |
122 | return; | |
123 | } | |
124 | ||
125 | page_cache_release(folio); | |
593a10da | 126 | folio_undo_large_rmappable(folio); |
2542b1ac MWO |
127 | mem_cgroup_uncharge(folio); |
128 | free_unref_page(&folio->page, folio_order(folio)); | |
91807063 | 129 | } |
8d29c703 | 130 | EXPORT_SYMBOL(__folio_put); |
70b50f94 | 131 | |
1d7ea732 | 132 | /** |
7682486b RD |
133 | * put_pages_list() - release a list of pages |
134 | * @pages: list of pages threaded on page->lru | |
1d7ea732 | 135 | * |
988c69f1 | 136 | * Release a list of pages which are strung together on page.lru. |
1d7ea732 AZ |
137 | */ |
138 | void put_pages_list(struct list_head *pages) | |
139 | { | |
24835f89 | 140 | struct folio_batch fbatch; |
b555895c | 141 | struct folio *folio, *next; |
988c69f1 | 142 | |
24835f89 | 143 | folio_batch_init(&fbatch); |
b555895c | 144 | list_for_each_entry_safe(folio, next, pages, lru) { |
24835f89 | 145 | if (!folio_put_testzero(folio)) |
988c69f1 | 146 | continue; |
2f166704 MWO |
147 | if (folio_test_hugetlb(folio)) { |
148 | free_huge_folio(folio); | |
988c69f1 MWO |
149 | continue; |
150 | } | |
2f58e5de | 151 | /* LRU flag must be clear because it's passed using the lru */ |
24835f89 MWO |
152 | if (folio_batch_add(&fbatch, folio) > 0) |
153 | continue; | |
154 | free_unref_folios(&fbatch); | |
1d7ea732 | 155 | } |
988c69f1 | 156 | |
24835f89 MWO |
157 | if (fbatch.nr) |
158 | free_unref_folios(&fbatch); | |
3cd018b4 | 159 | INIT_LIST_HEAD(pages); |
1d7ea732 AZ |
160 | } |
161 | EXPORT_SYMBOL(put_pages_list); | |
162 | ||
c2bc1681 MWO |
163 | typedef void (*move_fn_t)(struct lruvec *lruvec, struct folio *folio); |
164 | ||
70dea534 | 165 | static void lru_add_fn(struct lruvec *lruvec, struct folio *folio) |
7d80dd09 MWO |
166 | { |
167 | int was_unevictable = folio_test_clear_unevictable(folio); | |
168 | long nr_pages = folio_nr_pages(folio); | |
169 | ||
170 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); | |
171 | ||
7d80dd09 MWO |
172 | /* |
173 | * Is an smp_mb__after_atomic() still required here, before | |
188e8cae | 174 | * folio_evictable() tests the mlocked flag, to rule out the possibility |
7d80dd09 | 175 | * of stranding an evictable folio on an unevictable LRU? I think |
e0650a41 | 176 | * not, because __munlock_folio() only clears the mlocked flag |
188e8cae | 177 | * while the LRU lock is held. |
7d80dd09 MWO |
178 | * |
179 | * (That is not true of __page_cache_release(), and not necessarily | |
99fbb6bf | 180 | * true of folios_put(): but those only clear the mlocked flag after |
188e8cae | 181 | * folio_put_testzero() has excluded any other users of the folio.) |
7d80dd09 MWO |
182 | */ |
183 | if (folio_evictable(folio)) { | |
184 | if (was_unevictable) | |
185 | __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages); | |
186 | } else { | |
187 | folio_clear_active(folio); | |
188 | folio_set_unevictable(folio); | |
189 | /* | |
190 | * folio->mlock_count = !!folio_test_mlocked(folio)? | |
e0650a41 | 191 | * But that leaves __mlock_folio() in doubt whether another |
7d80dd09 MWO |
192 | * actor has already counted the mlock or not. Err on the |
193 | * safe side, underestimate, let page reclaim fix it, rather | |
194 | * than leaving a page on the unevictable LRU indefinitely. | |
195 | */ | |
196 | folio->mlock_count = 0; | |
197 | if (!was_unevictable) | |
198 | __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages); | |
199 | } | |
200 | ||
201 | lruvec_add_folio(lruvec, folio); | |
202 | trace_mm_lru_insertion(folio); | |
203 | } | |
204 | ||
c2bc1681 | 205 | static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn) |
902aaed0 HH |
206 | { |
207 | int i; | |
6168d0da | 208 | struct lruvec *lruvec = NULL; |
3dd7ae8e | 209 | unsigned long flags = 0; |
902aaed0 | 210 | |
c2bc1681 MWO |
211 | for (i = 0; i < folio_batch_count(fbatch); i++) { |
212 | struct folio *folio = fbatch->folios[i]; | |
3dd7ae8e | 213 | |
f1ee018b | 214 | folio_lruvec_relock_irqsave(folio, &lruvec, &flags); |
c2bc1681 | 215 | move_fn(lruvec, folio); |
fc574c23 | 216 | |
c2bc1681 | 217 | folio_set_lru(folio); |
902aaed0 | 218 | } |
c2bc1681 | 219 | |
6168d0da AS |
220 | if (lruvec) |
221 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
99fbb6bf | 222 | folios_put(fbatch); |
d8505dee SL |
223 | } |
224 | ||
c2bc1681 MWO |
225 | static void folio_batch_add_and_move(struct folio_batch *fbatch, |
226 | struct folio *folio, move_fn_t move_fn) | |
3dd7ae8e | 227 | { |
c2bc1681 MWO |
228 | if (folio_batch_add(fbatch, folio) && !folio_test_large(folio) && |
229 | !lru_cache_disabled()) | |
230 | return; | |
231 | folio_batch_move_lru(fbatch, move_fn); | |
232 | } | |
575ced1c | 233 | |
c2bc1681 MWO |
234 | static void lru_move_tail_fn(struct lruvec *lruvec, struct folio *folio) |
235 | { | |
575ced1c MWO |
236 | if (!folio_test_unevictable(folio)) { |
237 | lruvec_del_folio(lruvec, folio); | |
238 | folio_clear_active(folio); | |
239 | lruvec_add_folio_tail(lruvec, folio); | |
240 | __count_vm_events(PGROTATED, folio_nr_pages(folio)); | |
3dd7ae8e SL |
241 | } |
242 | } | |
243 | ||
1da177e4 | 244 | /* |
575ced1c MWO |
245 | * Writeback is about to end against a folio which has been marked for |
246 | * immediate reclaim. If it still appears to be reclaimable, move it | |
247 | * to the tail of the inactive list. | |
c7c7b80c | 248 | * |
575ced1c | 249 | * folio_rotate_reclaimable() must disable IRQs, to prevent nasty races. |
1da177e4 | 250 | */ |
575ced1c | 251 | void folio_rotate_reclaimable(struct folio *folio) |
1da177e4 | 252 | { |
575ced1c | 253 | if (!folio_test_locked(folio) && !folio_test_dirty(folio) && |
33dfe920 | 254 | !folio_test_unevictable(folio)) { |
c2bc1681 | 255 | struct folio_batch *fbatch; |
ac6aadb2 MS |
256 | unsigned long flags; |
257 | ||
575ced1c | 258 | folio_get(folio); |
33dfe920 | 259 | if (!folio_test_clear_lru(folio)) { |
260 | folio_put(folio); | |
261 | return; | |
262 | } | |
263 | ||
b01b2141 | 264 | local_lock_irqsave(&lru_rotate.lock, flags); |
c2bc1681 MWO |
265 | fbatch = this_cpu_ptr(&lru_rotate.fbatch); |
266 | folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn); | |
b01b2141 | 267 | local_unlock_irqrestore(&lru_rotate.lock, flags); |
ac6aadb2 | 268 | } |
1da177e4 LT |
269 | } |
270 | ||
0538a82c JW |
271 | void lru_note_cost(struct lruvec *lruvec, bool file, |
272 | unsigned int nr_io, unsigned int nr_rotated) | |
3e2f41f1 | 273 | { |
0538a82c JW |
274 | unsigned long cost; |
275 | ||
276 | /* | |
277 | * Reflect the relative cost of incurring IO and spending CPU | |
278 | * time on rotations. This doesn't attempt to make a precise | |
279 | * comparison, it just says: if reloads are about comparable | |
280 | * between the LRU lists, or rotations are overwhelmingly | |
281 | * different between them, adjust scan balance for CPU work. | |
282 | */ | |
283 | cost = nr_io * SWAP_CLUSTER_MAX + nr_rotated; | |
284 | ||
7cf111bc JW |
285 | do { |
286 | unsigned long lrusize; | |
287 | ||
6168d0da AS |
288 | /* |
289 | * Hold lruvec->lru_lock is safe here, since | |
290 | * 1) The pinned lruvec in reclaim, or | |
291 | * 2) From a pre-LRU page during refault (which also holds the | |
292 | * rcu lock, so would be safe even if the page was on the LRU | |
293 | * and could move simultaneously to a new lruvec). | |
294 | */ | |
295 | spin_lock_irq(&lruvec->lru_lock); | |
7cf111bc | 296 | /* Record cost event */ |
96f8bf4f | 297 | if (file) |
0538a82c | 298 | lruvec->file_cost += cost; |
7cf111bc | 299 | else |
0538a82c | 300 | lruvec->anon_cost += cost; |
7cf111bc JW |
301 | |
302 | /* | |
303 | * Decay previous events | |
304 | * | |
305 | * Because workloads change over time (and to avoid | |
306 | * overflow) we keep these statistics as a floating | |
307 | * average, which ends up weighing recent refaults | |
308 | * more than old ones. | |
309 | */ | |
310 | lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) + | |
311 | lruvec_page_state(lruvec, NR_ACTIVE_ANON) + | |
312 | lruvec_page_state(lruvec, NR_INACTIVE_FILE) + | |
313 | lruvec_page_state(lruvec, NR_ACTIVE_FILE); | |
314 | ||
315 | if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) { | |
316 | lruvec->file_cost /= 2; | |
317 | lruvec->anon_cost /= 2; | |
318 | } | |
6168d0da | 319 | spin_unlock_irq(&lruvec->lru_lock); |
7cf111bc | 320 | } while ((lruvec = parent_lruvec(lruvec))); |
3e2f41f1 KM |
321 | } |
322 | ||
0538a82c | 323 | void lru_note_cost_refault(struct folio *folio) |
96f8bf4f | 324 | { |
0995d7e5 | 325 | lru_note_cost(folio_lruvec(folio), folio_is_file_lru(folio), |
0538a82c | 326 | folio_nr_pages(folio), 0); |
96f8bf4f JW |
327 | } |
328 | ||
3a44610b | 329 | static void folio_activate_fn(struct lruvec *lruvec, struct folio *folio) |
1da177e4 | 330 | { |
f2d27392 MWO |
331 | if (!folio_test_active(folio) && !folio_test_unevictable(folio)) { |
332 | long nr_pages = folio_nr_pages(folio); | |
744ed144 | 333 | |
f2d27392 MWO |
334 | lruvec_del_folio(lruvec, folio); |
335 | folio_set_active(folio); | |
336 | lruvec_add_folio(lruvec, folio); | |
337 | trace_mm_lru_activate(folio); | |
4f98a2fe | 338 | |
21e330fc SB |
339 | __count_vm_events(PGACTIVATE, nr_pages); |
340 | __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE, | |
341 | nr_pages); | |
1da177e4 | 342 | } |
eb709b0d SL |
343 | } |
344 | ||
345 | #ifdef CONFIG_SMP | |
3a44610b | 346 | static void folio_activate_drain(int cpu) |
f2d27392 | 347 | { |
82ac64d8 | 348 | struct folio_batch *fbatch = &per_cpu(cpu_fbatches.activate, cpu); |
f2d27392 | 349 | |
3a44610b MWO |
350 | if (folio_batch_count(fbatch)) |
351 | folio_batch_move_lru(fbatch, folio_activate_fn); | |
5fbc4616 CM |
352 | } |
353 | ||
018ee47f | 354 | void folio_activate(struct folio *folio) |
eb709b0d | 355 | { |
33dfe920 | 356 | if (!folio_test_active(folio) && !folio_test_unevictable(folio)) { |
3a44610b | 357 | struct folio_batch *fbatch; |
eb709b0d | 358 | |
f2d27392 | 359 | folio_get(folio); |
33dfe920 | 360 | if (!folio_test_clear_lru(folio)) { |
361 | folio_put(folio); | |
362 | return; | |
363 | } | |
364 | ||
82ac64d8 MWO |
365 | local_lock(&cpu_fbatches.lock); |
366 | fbatch = this_cpu_ptr(&cpu_fbatches.activate); | |
3a44610b | 367 | folio_batch_add_and_move(fbatch, folio, folio_activate_fn); |
82ac64d8 | 368 | local_unlock(&cpu_fbatches.lock); |
eb709b0d SL |
369 | } |
370 | } | |
371 | ||
372 | #else | |
3a44610b | 373 | static inline void folio_activate_drain(int cpu) |
eb709b0d SL |
374 | { |
375 | } | |
376 | ||
018ee47f | 377 | void folio_activate(struct folio *folio) |
eb709b0d | 378 | { |
6168d0da | 379 | struct lruvec *lruvec; |
eb709b0d | 380 | |
f2d27392 | 381 | if (folio_test_clear_lru(folio)) { |
e809c3fe | 382 | lruvec = folio_lruvec_lock_irq(folio); |
3a44610b | 383 | folio_activate_fn(lruvec, folio); |
6168d0da | 384 | unlock_page_lruvec_irq(lruvec); |
f2d27392 | 385 | folio_set_lru(folio); |
6168d0da | 386 | } |
1da177e4 | 387 | } |
eb709b0d | 388 | #endif |
1da177e4 | 389 | |
76580b65 | 390 | static void __lru_cache_activate_folio(struct folio *folio) |
059285a2 | 391 | { |
70dea534 | 392 | struct folio_batch *fbatch; |
059285a2 MG |
393 | int i; |
394 | ||
82ac64d8 MWO |
395 | local_lock(&cpu_fbatches.lock); |
396 | fbatch = this_cpu_ptr(&cpu_fbatches.lru_add); | |
b01b2141 | 397 | |
059285a2 | 398 | /* |
70dea534 MWO |
399 | * Search backwards on the optimistic assumption that the folio being |
400 | * activated has just been added to this batch. Note that only | |
401 | * the local batch is examined as a !LRU folio could be in the | |
059285a2 | 402 | * process of being released, reclaimed, migrated or on a remote |
70dea534 MWO |
403 | * batch that is currently being drained. Furthermore, marking |
404 | * a remote batch's folio active potentially hits a race where | |
405 | * a folio is marked active just after it is added to the inactive | |
059285a2 MG |
406 | * list causing accounting errors and BUG_ON checks to trigger. |
407 | */ | |
70dea534 MWO |
408 | for (i = folio_batch_count(fbatch) - 1; i >= 0; i--) { |
409 | struct folio *batch_folio = fbatch->folios[i]; | |
059285a2 | 410 | |
70dea534 | 411 | if (batch_folio == folio) { |
76580b65 | 412 | folio_set_active(folio); |
059285a2 MG |
413 | break; |
414 | } | |
415 | } | |
416 | ||
82ac64d8 | 417 | local_unlock(&cpu_fbatches.lock); |
059285a2 MG |
418 | } |
419 | ||
ac35a490 YZ |
420 | #ifdef CONFIG_LRU_GEN |
421 | static void folio_inc_refs(struct folio *folio) | |
422 | { | |
423 | unsigned long new_flags, old_flags = READ_ONCE(folio->flags); | |
424 | ||
425 | if (folio_test_unevictable(folio)) | |
426 | return; | |
427 | ||
428 | if (!folio_test_referenced(folio)) { | |
429 | folio_set_referenced(folio); | |
430 | return; | |
431 | } | |
432 | ||
433 | if (!folio_test_workingset(folio)) { | |
434 | folio_set_workingset(folio); | |
435 | return; | |
436 | } | |
437 | ||
438 | /* see the comment on MAX_NR_TIERS */ | |
439 | do { | |
440 | new_flags = old_flags & LRU_REFS_MASK; | |
441 | if (new_flags == LRU_REFS_MASK) | |
442 | break; | |
443 | ||
444 | new_flags += BIT(LRU_REFS_PGOFF); | |
445 | new_flags |= old_flags & ~LRU_REFS_MASK; | |
446 | } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); | |
447 | } | |
448 | #else | |
449 | static void folio_inc_refs(struct folio *folio) | |
450 | { | |
451 | } | |
452 | #endif /* CONFIG_LRU_GEN */ | |
453 | ||
21db296a MWO |
454 | /** |
455 | * folio_mark_accessed - Mark a folio as having seen activity. | |
456 | * @folio: The folio to mark. | |
457 | * | |
458 | * This function will perform one of the following transitions: | |
1da177e4 | 459 | * |
21db296a MWO |
460 | * * inactive,unreferenced -> inactive,referenced |
461 | * * inactive,referenced -> active,unreferenced | |
462 | * * active,unreferenced -> active,referenced | |
eb39d618 | 463 | * |
21db296a MWO |
464 | * When a newly allocated folio is not yet visible, so safe for non-atomic ops, |
465 | * __folio_set_referenced() may be substituted for folio_mark_accessed(). | |
1da177e4 | 466 | */ |
76580b65 | 467 | void folio_mark_accessed(struct folio *folio) |
1da177e4 | 468 | { |
ac35a490 YZ |
469 | if (lru_gen_enabled()) { |
470 | folio_inc_refs(folio); | |
471 | return; | |
472 | } | |
473 | ||
76580b65 MWO |
474 | if (!folio_test_referenced(folio)) { |
475 | folio_set_referenced(folio); | |
476 | } else if (folio_test_unevictable(folio)) { | |
a1100a74 FW |
477 | /* |
478 | * Unevictable pages are on the "LRU_UNEVICTABLE" list. But, | |
479 | * this list is never rotated or maintained, so marking an | |
914c32e4 | 480 | * unevictable page accessed has no effect. |
a1100a74 | 481 | */ |
76580b65 | 482 | } else if (!folio_test_active(folio)) { |
059285a2 | 483 | /* |
3a44610b | 484 | * If the folio is on the LRU, queue it for activation via |
82ac64d8 | 485 | * cpu_fbatches.activate. Otherwise, assume the folio is in a |
3a44610b | 486 | * folio_batch, mark it active and it'll be moved to the active |
059285a2 MG |
487 | * LRU on the next drain. |
488 | */ | |
76580b65 MWO |
489 | if (folio_test_lru(folio)) |
490 | folio_activate(folio); | |
059285a2 | 491 | else |
76580b65 MWO |
492 | __lru_cache_activate_folio(folio); |
493 | folio_clear_referenced(folio); | |
494 | workingset_activation(folio); | |
1da177e4 | 495 | } |
76580b65 MWO |
496 | if (folio_test_idle(folio)) |
497 | folio_clear_idle(folio); | |
1da177e4 | 498 | } |
76580b65 | 499 | EXPORT_SYMBOL(folio_mark_accessed); |
1da177e4 | 500 | |
f04e9ebb | 501 | /** |
0d31125d MWO |
502 | * folio_add_lru - Add a folio to an LRU list. |
503 | * @folio: The folio to be added to the LRU. | |
2329d375 | 504 | * |
0d31125d | 505 | * Queue the folio for addition to the LRU. The decision on whether |
2329d375 | 506 | * to add the page to the [in]active [file|anon] list is deferred until the |
82ac64d8 | 507 | * folio_batch is drained. This gives a chance for the caller of folio_add_lru() |
0d31125d | 508 | * have the folio added to the active list using folio_mark_accessed(). |
f04e9ebb | 509 | */ |
0d31125d | 510 | void folio_add_lru(struct folio *folio) |
1da177e4 | 511 | { |
70dea534 | 512 | struct folio_batch *fbatch; |
6058eaec | 513 | |
70dea534 MWO |
514 | VM_BUG_ON_FOLIO(folio_test_active(folio) && |
515 | folio_test_unevictable(folio), folio); | |
0d31125d | 516 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
6058eaec | 517 | |
ec1c86b2 YZ |
518 | /* see the comment in lru_gen_add_folio() */ |
519 | if (lru_gen_enabled() && !folio_test_unevictable(folio) && | |
520 | lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) | |
521 | folio_set_active(folio); | |
522 | ||
0d31125d | 523 | folio_get(folio); |
82ac64d8 MWO |
524 | local_lock(&cpu_fbatches.lock); |
525 | fbatch = this_cpu_ptr(&cpu_fbatches.lru_add); | |
70dea534 | 526 | folio_batch_add_and_move(fbatch, folio, lru_add_fn); |
82ac64d8 | 527 | local_unlock(&cpu_fbatches.lock); |
1da177e4 | 528 | } |
0d31125d | 529 | EXPORT_SYMBOL(folio_add_lru); |
1da177e4 | 530 | |
00501b53 | 531 | /** |
681ecf63 MWO |
532 | * folio_add_lru_vma() - Add a folio to the appropate LRU list for this VMA. |
533 | * @folio: The folio to be added to the LRU. | |
534 | * @vma: VMA in which the folio is mapped. | |
00501b53 | 535 | * |
681ecf63 MWO |
536 | * If the VMA is mlocked, @folio is added to the unevictable list. |
537 | * Otherwise, it is treated the same way as folio_add_lru(). | |
00501b53 | 538 | */ |
681ecf63 | 539 | void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma) |
00501b53 | 540 | { |
681ecf63 | 541 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
00501b53 | 542 | |
2fbb0c10 | 543 | if (unlikely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED)) |
96f97c43 | 544 | mlock_new_folio(folio); |
2fbb0c10 | 545 | else |
681ecf63 | 546 | folio_add_lru(folio); |
00501b53 JW |
547 | } |
548 | ||
31560180 | 549 | /* |
7a3dbfe8 | 550 | * If the folio cannot be invalidated, it is moved to the |
31560180 MK |
551 | * inactive list to speed up its reclaim. It is moved to the |
552 | * head of the list, rather than the tail, to give the flusher | |
553 | * threads some time to write it out, as this is much more | |
554 | * effective than the single-page writeout from reclaim. | |
278df9f4 | 555 | * |
7a3dbfe8 MWO |
556 | * If the folio isn't mapped and dirty/writeback, the folio |
557 | * could be reclaimed asap using the reclaim flag. | |
278df9f4 | 558 | * |
7a3dbfe8 MWO |
559 | * 1. active, mapped folio -> none |
560 | * 2. active, dirty/writeback folio -> inactive, head, reclaim | |
561 | * 3. inactive, mapped folio -> none | |
562 | * 4. inactive, dirty/writeback folio -> inactive, head, reclaim | |
278df9f4 MK |
563 | * 5. inactive, clean -> inactive, tail |
564 | * 6. Others -> none | |
565 | * | |
7a3dbfe8 MWO |
566 | * In 4, it moves to the head of the inactive list so the folio is |
567 | * written out by flusher threads as this is much more efficient | |
278df9f4 | 568 | * than the single-page writeout from reclaim. |
31560180 | 569 | */ |
7a3dbfe8 | 570 | static void lru_deactivate_file_fn(struct lruvec *lruvec, struct folio *folio) |
31560180 | 571 | { |
7a3dbfe8 MWO |
572 | bool active = folio_test_active(folio); |
573 | long nr_pages = folio_nr_pages(folio); | |
31560180 | 574 | |
7a3dbfe8 | 575 | if (folio_test_unevictable(folio)) |
bad49d9c MK |
576 | return; |
577 | ||
7a3dbfe8 MWO |
578 | /* Some processes are using the folio */ |
579 | if (folio_mapped(folio)) | |
31560180 MK |
580 | return; |
581 | ||
7a3dbfe8 MWO |
582 | lruvec_del_folio(lruvec, folio); |
583 | folio_clear_active(folio); | |
584 | folio_clear_referenced(folio); | |
31560180 | 585 | |
7a3dbfe8 | 586 | if (folio_test_writeback(folio) || folio_test_dirty(folio)) { |
278df9f4 | 587 | /* |
7a3dbfe8 MWO |
588 | * Setting the reclaim flag could race with |
589 | * folio_end_writeback() and confuse readahead. But the | |
590 | * race window is _really_ small and it's not a critical | |
591 | * problem. | |
278df9f4 | 592 | */ |
7a3dbfe8 MWO |
593 | lruvec_add_folio(lruvec, folio); |
594 | folio_set_reclaim(folio); | |
278df9f4 MK |
595 | } else { |
596 | /* | |
7a3dbfe8 MWO |
597 | * The folio's writeback ended while it was in the batch. |
598 | * We move that folio to the tail of the inactive list. | |
278df9f4 | 599 | */ |
7a3dbfe8 | 600 | lruvec_add_folio_tail(lruvec, folio); |
5d91f31f | 601 | __count_vm_events(PGROTATED, nr_pages); |
278df9f4 MK |
602 | } |
603 | ||
21e330fc | 604 | if (active) { |
5d91f31f | 605 | __count_vm_events(PGDEACTIVATE, nr_pages); |
21e330fc SB |
606 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, |
607 | nr_pages); | |
608 | } | |
31560180 MK |
609 | } |
610 | ||
85cd7791 | 611 | static void lru_deactivate_fn(struct lruvec *lruvec, struct folio *folio) |
9c276cc6 | 612 | { |
ec1c86b2 | 613 | if (!folio_test_unevictable(folio) && (folio_test_active(folio) || lru_gen_enabled())) { |
85cd7791 | 614 | long nr_pages = folio_nr_pages(folio); |
9c276cc6 | 615 | |
85cd7791 MWO |
616 | lruvec_del_folio(lruvec, folio); |
617 | folio_clear_active(folio); | |
618 | folio_clear_referenced(folio); | |
619 | lruvec_add_folio(lruvec, folio); | |
9c276cc6 | 620 | |
21e330fc SB |
621 | __count_vm_events(PGDEACTIVATE, nr_pages); |
622 | __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, | |
623 | nr_pages); | |
9c276cc6 MK |
624 | } |
625 | } | |
10853a03 | 626 | |
cec394ba | 627 | static void lru_lazyfree_fn(struct lruvec *lruvec, struct folio *folio) |
10853a03 | 628 | { |
cec394ba MWO |
629 | if (folio_test_anon(folio) && folio_test_swapbacked(folio) && |
630 | !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) { | |
631 | long nr_pages = folio_nr_pages(folio); | |
10853a03 | 632 | |
cec394ba MWO |
633 | lruvec_del_folio(lruvec, folio); |
634 | folio_clear_active(folio); | |
635 | folio_clear_referenced(folio); | |
f7ad2a6c | 636 | /* |
cec394ba MWO |
637 | * Lazyfree folios are clean anonymous folios. They have |
638 | * the swapbacked flag cleared, to distinguish them from normal | |
639 | * anonymous folios | |
f7ad2a6c | 640 | */ |
cec394ba MWO |
641 | folio_clear_swapbacked(folio); |
642 | lruvec_add_folio(lruvec, folio); | |
10853a03 | 643 | |
21e330fc SB |
644 | __count_vm_events(PGLAZYFREE, nr_pages); |
645 | __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE, | |
646 | nr_pages); | |
10853a03 MK |
647 | } |
648 | } | |
649 | ||
902aaed0 | 650 | /* |
82ac64d8 | 651 | * Drain pages out of the cpu's folio_batch. |
902aaed0 HH |
652 | * Either "cpu" is the current CPU, and preemption has already been |
653 | * disabled; or "cpu" is being hot-unplugged, and is already dead. | |
654 | */ | |
f0cb3c76 | 655 | void lru_add_drain_cpu(int cpu) |
1da177e4 | 656 | { |
a2d33b5d MWO |
657 | struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu); |
658 | struct folio_batch *fbatch = &fbatches->lru_add; | |
1da177e4 | 659 | |
70dea534 MWO |
660 | if (folio_batch_count(fbatch)) |
661 | folio_batch_move_lru(fbatch, lru_add_fn); | |
902aaed0 | 662 | |
c2bc1681 | 663 | fbatch = &per_cpu(lru_rotate.fbatch, cpu); |
7e0cc01e | 664 | /* Disabling interrupts below acts as a compiler barrier. */ |
c2bc1681 | 665 | if (data_race(folio_batch_count(fbatch))) { |
902aaed0 HH |
666 | unsigned long flags; |
667 | ||
668 | /* No harm done if a racing interrupt already did this */ | |
b01b2141 | 669 | local_lock_irqsave(&lru_rotate.lock, flags); |
c2bc1681 | 670 | folio_batch_move_lru(fbatch, lru_move_tail_fn); |
b01b2141 | 671 | local_unlock_irqrestore(&lru_rotate.lock, flags); |
902aaed0 | 672 | } |
31560180 | 673 | |
a2d33b5d | 674 | fbatch = &fbatches->lru_deactivate_file; |
7a3dbfe8 MWO |
675 | if (folio_batch_count(fbatch)) |
676 | folio_batch_move_lru(fbatch, lru_deactivate_file_fn); | |
eb709b0d | 677 | |
a2d33b5d | 678 | fbatch = &fbatches->lru_deactivate; |
85cd7791 MWO |
679 | if (folio_batch_count(fbatch)) |
680 | folio_batch_move_lru(fbatch, lru_deactivate_fn); | |
9c276cc6 | 681 | |
a2d33b5d | 682 | fbatch = &fbatches->lru_lazyfree; |
cec394ba MWO |
683 | if (folio_batch_count(fbatch)) |
684 | folio_batch_move_lru(fbatch, lru_lazyfree_fn); | |
10853a03 | 685 | |
3a44610b | 686 | folio_activate_drain(cpu); |
31560180 MK |
687 | } |
688 | ||
689 | /** | |
7a3dbfe8 | 690 | * deactivate_file_folio() - Deactivate a file folio. |
261b6840 | 691 | * @folio: Folio to deactivate. |
31560180 | 692 | * |
261b6840 MWO |
693 | * This function hints to the VM that @folio is a good reclaim candidate, |
694 | * for example if its invalidation fails due to the folio being dirty | |
31560180 | 695 | * or under writeback. |
261b6840 | 696 | * |
7a3dbfe8 | 697 | * Context: Caller holds a reference on the folio. |
31560180 | 698 | */ |
261b6840 | 699 | void deactivate_file_folio(struct folio *folio) |
31560180 | 700 | { |
7a3dbfe8 | 701 | struct folio_batch *fbatch; |
261b6840 | 702 | |
7a3dbfe8 | 703 | /* Deactivating an unevictable folio will not accelerate reclaim */ |
261b6840 | 704 | if (folio_test_unevictable(folio)) |
821ed6bb MK |
705 | return; |
706 | ||
261b6840 | 707 | folio_get(folio); |
33dfe920 | 708 | if (!folio_test_clear_lru(folio)) { |
709 | folio_put(folio); | |
710 | return; | |
711 | } | |
712 | ||
82ac64d8 MWO |
713 | local_lock(&cpu_fbatches.lock); |
714 | fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file); | |
7a3dbfe8 | 715 | folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn); |
82ac64d8 | 716 | local_unlock(&cpu_fbatches.lock); |
80bfed90 AM |
717 | } |
718 | ||
9c276cc6 | 719 | /* |
5a9e3474 VMO |
720 | * folio_deactivate - deactivate a folio |
721 | * @folio: folio to deactivate | |
9c276cc6 | 722 | * |
5a9e3474 VMO |
723 | * folio_deactivate() moves @folio to the inactive list if @folio was on the |
724 | * active list and was not unevictable. This is done to accelerate the | |
725 | * reclaim of @folio. | |
9c276cc6 | 726 | */ |
5a9e3474 | 727 | void folio_deactivate(struct folio *folio) |
9c276cc6 | 728 | { |
33dfe920 | 729 | if (!folio_test_unevictable(folio) && (folio_test_active(folio) || |
730 | lru_gen_enabled())) { | |
85cd7791 MWO |
731 | struct folio_batch *fbatch; |
732 | ||
733 | folio_get(folio); | |
33dfe920 | 734 | if (!folio_test_clear_lru(folio)) { |
735 | folio_put(folio); | |
736 | return; | |
737 | } | |
738 | ||
82ac64d8 MWO |
739 | local_lock(&cpu_fbatches.lock); |
740 | fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate); | |
85cd7791 | 741 | folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn); |
82ac64d8 | 742 | local_unlock(&cpu_fbatches.lock); |
9c276cc6 MK |
743 | } |
744 | } | |
745 | ||
10853a03 | 746 | /** |
6a6fe9eb KW |
747 | * folio_mark_lazyfree - make an anon folio lazyfree |
748 | * @folio: folio to deactivate | |
10853a03 | 749 | * |
6a6fe9eb KW |
750 | * folio_mark_lazyfree() moves @folio to the inactive file list. |
751 | * This is done to accelerate the reclaim of @folio. | |
10853a03 | 752 | */ |
6a6fe9eb | 753 | void folio_mark_lazyfree(struct folio *folio) |
10853a03 | 754 | { |
33dfe920 | 755 | if (folio_test_anon(folio) && folio_test_swapbacked(folio) && |
756 | !folio_test_swapcache(folio) && !folio_test_unevictable(folio)) { | |
cec394ba MWO |
757 | struct folio_batch *fbatch; |
758 | ||
759 | folio_get(folio); | |
33dfe920 | 760 | if (!folio_test_clear_lru(folio)) { |
761 | folio_put(folio); | |
762 | return; | |
763 | } | |
764 | ||
82ac64d8 MWO |
765 | local_lock(&cpu_fbatches.lock); |
766 | fbatch = this_cpu_ptr(&cpu_fbatches.lru_lazyfree); | |
cec394ba | 767 | folio_batch_add_and_move(fbatch, folio, lru_lazyfree_fn); |
82ac64d8 | 768 | local_unlock(&cpu_fbatches.lock); |
10853a03 MK |
769 | } |
770 | } | |
771 | ||
80bfed90 AM |
772 | void lru_add_drain(void) |
773 | { | |
82ac64d8 | 774 | local_lock(&cpu_fbatches.lock); |
b01b2141 | 775 | lru_add_drain_cpu(smp_processor_id()); |
82ac64d8 | 776 | local_unlock(&cpu_fbatches.lock); |
96f97c43 | 777 | mlock_drain_local(); |
b01b2141 IM |
778 | } |
779 | ||
243418e3 MK |
780 | /* |
781 | * It's called from per-cpu workqueue context in SMP case so | |
782 | * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on | |
783 | * the same cpu. It shouldn't be a problem in !SMP case since | |
784 | * the core is only one and the locks will disable preemption. | |
785 | */ | |
786 | static void lru_add_and_bh_lrus_drain(void) | |
787 | { | |
82ac64d8 | 788 | local_lock(&cpu_fbatches.lock); |
243418e3 | 789 | lru_add_drain_cpu(smp_processor_id()); |
82ac64d8 | 790 | local_unlock(&cpu_fbatches.lock); |
243418e3 | 791 | invalidate_bh_lrus_cpu(); |
96f97c43 | 792 | mlock_drain_local(); |
243418e3 MK |
793 | } |
794 | ||
b01b2141 IM |
795 | void lru_add_drain_cpu_zone(struct zone *zone) |
796 | { | |
82ac64d8 | 797 | local_lock(&cpu_fbatches.lock); |
b01b2141 IM |
798 | lru_add_drain_cpu(smp_processor_id()); |
799 | drain_local_pages(zone); | |
82ac64d8 | 800 | local_unlock(&cpu_fbatches.lock); |
96f97c43 | 801 | mlock_drain_local(); |
1da177e4 LT |
802 | } |
803 | ||
6ea183d6 MH |
804 | #ifdef CONFIG_SMP |
805 | ||
806 | static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work); | |
807 | ||
c4028958 | 808 | static void lru_add_drain_per_cpu(struct work_struct *dummy) |
053837fc | 809 | { |
243418e3 | 810 | lru_add_and_bh_lrus_drain(); |
053837fc NP |
811 | } |
812 | ||
4864545a MWO |
813 | static bool cpu_needs_drain(unsigned int cpu) |
814 | { | |
815 | struct cpu_fbatches *fbatches = &per_cpu(cpu_fbatches, cpu); | |
816 | ||
817 | /* Check these in order of likelihood that they're not zero */ | |
818 | return folio_batch_count(&fbatches->lru_add) || | |
819 | data_race(folio_batch_count(&per_cpu(lru_rotate.fbatch, cpu))) || | |
820 | folio_batch_count(&fbatches->lru_deactivate_file) || | |
821 | folio_batch_count(&fbatches->lru_deactivate) || | |
822 | folio_batch_count(&fbatches->lru_lazyfree) || | |
823 | folio_batch_count(&fbatches->activate) || | |
96f97c43 | 824 | need_mlock_drain(cpu) || |
4864545a MWO |
825 | has_bh_in_lru(cpu, NULL); |
826 | } | |
827 | ||
9852a721 MH |
828 | /* |
829 | * Doesn't need any cpu hotplug locking because we do rely on per-cpu | |
830 | * kworkers being shut down before our page_alloc_cpu_dead callback is | |
831 | * executed on the offlined cpu. | |
832 | * Calling this function with cpu hotplug locks held can actually lead | |
833 | * to obscure indirect dependencies via WQ context. | |
834 | */ | |
3db3264d | 835 | static inline void __lru_add_drain_all(bool force_all_cpus) |
053837fc | 836 | { |
6446a513 AD |
837 | /* |
838 | * lru_drain_gen - Global pages generation number | |
839 | * | |
840 | * (A) Definition: global lru_drain_gen = x implies that all generations | |
841 | * 0 < n <= x are already *scheduled* for draining. | |
842 | * | |
843 | * This is an optimization for the highly-contended use case where a | |
844 | * user space workload keeps constantly generating a flow of pages for | |
845 | * each CPU. | |
846 | */ | |
847 | static unsigned int lru_drain_gen; | |
5fbc4616 | 848 | static struct cpumask has_work; |
6446a513 AD |
849 | static DEFINE_MUTEX(lock); |
850 | unsigned cpu, this_gen; | |
5fbc4616 | 851 | |
ce612879 MH |
852 | /* |
853 | * Make sure nobody triggers this path before mm_percpu_wq is fully | |
854 | * initialized. | |
855 | */ | |
856 | if (WARN_ON(!mm_percpu_wq)) | |
857 | return; | |
858 | ||
6446a513 | 859 | /* |
82ac64d8 MWO |
860 | * Guarantee folio_batch counter stores visible by this CPU |
861 | * are visible to other CPUs before loading the current drain | |
862 | * generation. | |
6446a513 AD |
863 | */ |
864 | smp_mb(); | |
865 | ||
866 | /* | |
867 | * (B) Locally cache global LRU draining generation number | |
868 | * | |
869 | * The read barrier ensures that the counter is loaded before the mutex | |
870 | * is taken. It pairs with smp_mb() inside the mutex critical section | |
871 | * at (D). | |
872 | */ | |
873 | this_gen = smp_load_acquire(&lru_drain_gen); | |
eef1a429 | 874 | |
5fbc4616 | 875 | mutex_lock(&lock); |
eef1a429 KK |
876 | |
877 | /* | |
6446a513 AD |
878 | * (C) Exit the draining operation if a newer generation, from another |
879 | * lru_add_drain_all(), was already scheduled for draining. Check (A). | |
eef1a429 | 880 | */ |
d479960e | 881 | if (unlikely(this_gen != lru_drain_gen && !force_all_cpus)) |
eef1a429 KK |
882 | goto done; |
883 | ||
6446a513 AD |
884 | /* |
885 | * (D) Increment global generation number | |
886 | * | |
887 | * Pairs with smp_load_acquire() at (B), outside of the critical | |
82ac64d8 MWO |
888 | * section. Use a full memory barrier to guarantee that the |
889 | * new global drain generation number is stored before loading | |
890 | * folio_batch counters. | |
6446a513 AD |
891 | * |
892 | * This pairing must be done here, before the for_each_online_cpu loop | |
893 | * below which drains the page vectors. | |
894 | * | |
895 | * Let x, y, and z represent some system CPU numbers, where x < y < z. | |
cb152a1a | 896 | * Assume CPU #z is in the middle of the for_each_online_cpu loop |
6446a513 AD |
897 | * below and has already reached CPU #y's per-cpu data. CPU #x comes |
898 | * along, adds some pages to its per-cpu vectors, then calls | |
899 | * lru_add_drain_all(). | |
900 | * | |
901 | * If the paired barrier is done at any later step, e.g. after the | |
902 | * loop, CPU #x will just exit at (C) and miss flushing out all of its | |
903 | * added pages. | |
904 | */ | |
905 | WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1); | |
906 | smp_mb(); | |
eef1a429 | 907 | |
5fbc4616 | 908 | cpumask_clear(&has_work); |
5fbc4616 CM |
909 | for_each_online_cpu(cpu) { |
910 | struct work_struct *work = &per_cpu(lru_add_drain_work, cpu); | |
911 | ||
4864545a | 912 | if (cpu_needs_drain(cpu)) { |
5fbc4616 | 913 | INIT_WORK(work, lru_add_drain_per_cpu); |
ce612879 | 914 | queue_work_on(cpu, mm_percpu_wq, work); |
6446a513 | 915 | __cpumask_set_cpu(cpu, &has_work); |
5fbc4616 CM |
916 | } |
917 | } | |
918 | ||
919 | for_each_cpu(cpu, &has_work) | |
920 | flush_work(&per_cpu(lru_add_drain_work, cpu)); | |
921 | ||
eef1a429 | 922 | done: |
5fbc4616 | 923 | mutex_unlock(&lock); |
053837fc | 924 | } |
d479960e MK |
925 | |
926 | void lru_add_drain_all(void) | |
927 | { | |
928 | __lru_add_drain_all(false); | |
929 | } | |
6ea183d6 MH |
930 | #else |
931 | void lru_add_drain_all(void) | |
932 | { | |
933 | lru_add_drain(); | |
934 | } | |
6446a513 | 935 | #endif /* CONFIG_SMP */ |
053837fc | 936 | |
d479960e MK |
937 | atomic_t lru_disable_count = ATOMIC_INIT(0); |
938 | ||
939 | /* | |
940 | * lru_cache_disable() needs to be called before we start compiling | |
941 | * a list of pages to be migrated using isolate_lru_page(). | |
942 | * It drains pages on LRU cache and then disable on all cpus until | |
943 | * lru_cache_enable is called. | |
944 | * | |
945 | * Must be paired with a call to lru_cache_enable(). | |
946 | */ | |
947 | void lru_cache_disable(void) | |
948 | { | |
949 | atomic_inc(&lru_disable_count); | |
d479960e | 950 | /* |
ff042f4a MT |
951 | * Readers of lru_disable_count are protected by either disabling |
952 | * preemption or rcu_read_lock: | |
953 | * | |
954 | * preempt_disable, local_irq_disable [bh_lru_lock()] | |
955 | * rcu_read_lock [rt_spin_lock CONFIG_PREEMPT_RT] | |
956 | * preempt_disable [local_lock !CONFIG_PREEMPT_RT] | |
957 | * | |
958 | * Since v5.1 kernel, synchronize_rcu() is guaranteed to wait on | |
959 | * preempt_disable() regions of code. So any CPU which sees | |
960 | * lru_disable_count = 0 will have exited the critical | |
961 | * section when synchronize_rcu() returns. | |
d479960e | 962 | */ |
31733463 | 963 | synchronize_rcu_expedited(); |
ff042f4a | 964 | #ifdef CONFIG_SMP |
d479960e MK |
965 | __lru_add_drain_all(true); |
966 | #else | |
243418e3 | 967 | lru_add_and_bh_lrus_drain(); |
d479960e MK |
968 | #endif |
969 | } | |
970 | ||
aabfb572 | 971 | /** |
99fbb6bf MWO |
972 | * folios_put_refs - Reduce the reference count on a batch of folios. |
973 | * @folios: The folios. | |
974 | * @refs: The number of refs to subtract from each folio. | |
1da177e4 | 975 | * |
99fbb6bf MWO |
976 | * Like folio_put(), but for a batch of folios. This is more efficient |
977 | * than writing the loop yourself as it will optimise the locks which need | |
978 | * to be taken if the folios are freed. The folios batch is returned | |
979 | * empty and ready to be reused for another batch; there is no need | |
980 | * to reinitialise it. If @refs is NULL, we subtract one from each | |
981 | * folio refcount. | |
449c7967 | 982 | * |
99fbb6bf MWO |
983 | * Context: May be called in process or interrupt context, but not in NMI |
984 | * context. May be called while holding a spinlock. | |
1da177e4 | 985 | */ |
99fbb6bf | 986 | void folios_put_refs(struct folio_batch *folios, unsigned int *refs) |
1da177e4 | 987 | { |
7c33b8c4 | 988 | int i, j; |
6168d0da | 989 | struct lruvec *lruvec = NULL; |
0de340cb | 990 | unsigned long flags = 0; |
1da177e4 | 991 | |
7c33b8c4 | 992 | for (i = 0, j = 0; i < folios->nr; i++) { |
99fbb6bf MWO |
993 | struct folio *folio = folios->folios[i]; |
994 | unsigned int nr_refs = refs ? refs[i] : 1; | |
aabfb572 | 995 | |
5beaee54 | 996 | if (is_huge_zero_folio(folio)) |
aa88b68c | 997 | continue; |
aa88b68c | 998 | |
ab5e653e | 999 | if (folio_is_zone_device(folio)) { |
6168d0da AS |
1000 | if (lruvec) { |
1001 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
1002 | lruvec = NULL; | |
df6ad698 | 1003 | } |
53e45c4f | 1004 | if (put_devmap_managed_folio_refs(folio, nr_refs)) |
c5d6c45e | 1005 | continue; |
d7f861b9 | 1006 | if (folio_ref_sub_and_test(folio, nr_refs)) |
9f100e3b | 1007 | free_zone_device_folio(folio); |
43fbdeb3 | 1008 | continue; |
df6ad698 JG |
1009 | } |
1010 | ||
d7f861b9 | 1011 | if (!folio_ref_sub_and_test(folio, nr_refs)) |
1da177e4 LT |
1012 | continue; |
1013 | ||
f77171d2 MWO |
1014 | /* hugetlb has its own memcg */ |
1015 | if (folio_test_hugetlb(folio)) { | |
6168d0da AS |
1016 | if (lruvec) { |
1017 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
1018 | lruvec = NULL; | |
ddc58f27 | 1019 | } |
f77171d2 | 1020 | free_huge_folio(folio); |
ddc58f27 KS |
1021 | continue; |
1022 | } | |
593a10da | 1023 | folio_undo_large_rmappable(folio); |
f1ee018b | 1024 | __page_cache_release(folio, &lruvec, &flags); |
b109b870 | 1025 | |
7c33b8c4 MWO |
1026 | if (j != i) |
1027 | folios->folios[j] = folio; | |
1028 | j++; | |
1da177e4 | 1029 | } |
6168d0da AS |
1030 | if (lruvec) |
1031 | unlock_page_lruvec_irqrestore(lruvec, flags); | |
7c33b8c4 MWO |
1032 | if (!j) { |
1033 | folio_batch_reinit(folios); | |
1034 | return; | |
1035 | } | |
1da177e4 | 1036 | |
7c33b8c4 MWO |
1037 | folios->nr = j; |
1038 | mem_cgroup_uncharge_folios(folios); | |
1039 | free_unref_folios(folios); | |
99fbb6bf MWO |
1040 | } |
1041 | EXPORT_SYMBOL(folios_put_refs); | |
1042 | ||
1043 | /** | |
1044 | * release_pages - batched put_page() | |
1045 | * @arg: array of pages to release | |
1046 | * @nr: number of pages | |
1047 | * | |
1048 | * Decrement the reference count on all the pages in @arg. If it | |
1049 | * fell to zero, remove the page from the LRU and free it. | |
1050 | * | |
1051 | * Note that the argument can be an array of pages, encoded pages, | |
1052 | * or folio pointers. We ignore any encoded bits, and turn any of | |
1053 | * them into just a folio that gets free'd. | |
1054 | */ | |
1055 | void release_pages(release_pages_arg arg, int nr) | |
1056 | { | |
1057 | struct folio_batch fbatch; | |
1058 | int refs[PAGEVEC_SIZE]; | |
1059 | struct encoded_page **encoded = arg.encoded_pages; | |
1060 | int i; | |
1061 | ||
1062 | folio_batch_init(&fbatch); | |
1063 | for (i = 0; i < nr; i++) { | |
1064 | /* Turn any of the argument types into a folio */ | |
1065 | struct folio *folio = page_folio(encoded_page_ptr(encoded[i])); | |
1066 | ||
1067 | /* Is our next entry actually "nr_pages" -> "nr_refs" ? */ | |
1068 | refs[fbatch.nr] = 1; | |
1069 | if (unlikely(encoded_page_flags(encoded[i]) & | |
1070 | ENCODED_PAGE_BIT_NR_PAGES_NEXT)) | |
1071 | refs[fbatch.nr] = encoded_nr_pages(encoded[++i]); | |
1072 | ||
1073 | if (folio_batch_add(&fbatch, folio) > 0) | |
1074 | continue; | |
1075 | folios_put_refs(&fbatch, refs); | |
1076 | } | |
1077 | ||
1078 | if (fbatch.nr) | |
1079 | folios_put_refs(&fbatch, refs); | |
1da177e4 | 1080 | } |
0be8557b | 1081 | EXPORT_SYMBOL(release_pages); |
1da177e4 LT |
1082 | |
1083 | /* | |
1e0877d5 | 1084 | * The folios which we're about to release may be in the deferred lru-addition |
1da177e4 | 1085 | * queues. That would prevent them from really being freed right now. That's |
1e0877d5 | 1086 | * OK from a correctness point of view but is inefficient - those folios may be |
1da177e4 LT |
1087 | * cache-warm and we want to give them back to the page allocator ASAP. |
1088 | * | |
1e0877d5 | 1089 | * So __folio_batch_release() will drain those queues here. |
70dea534 | 1090 | * folio_batch_move_lru() calls folios_put() directly to avoid |
1da177e4 LT |
1091 | * mutual recursion. |
1092 | */ | |
1e0877d5 | 1093 | void __folio_batch_release(struct folio_batch *fbatch) |
1da177e4 | 1094 | { |
1e0877d5 | 1095 | if (!fbatch->percpu_pvec_drained) { |
d9ed0d08 | 1096 | lru_add_drain(); |
1e0877d5 | 1097 | fbatch->percpu_pvec_drained = true; |
d9ed0d08 | 1098 | } |
6871cc57 | 1099 | folios_put(fbatch); |
1da177e4 | 1100 | } |
1e0877d5 | 1101 | EXPORT_SYMBOL(__folio_batch_release); |
7f285701 | 1102 | |
0cd6144a | 1103 | /** |
1613fac9 MWO |
1104 | * folio_batch_remove_exceptionals() - Prune non-folios from a batch. |
1105 | * @fbatch: The batch to prune | |
0cd6144a | 1106 | * |
1613fac9 MWO |
1107 | * find_get_entries() fills a batch with both folios and shadow/swap/DAX |
1108 | * entries. This function prunes all the non-folio entries from @fbatch | |
1109 | * without leaving holes, so that it can be passed on to folio-only batch | |
1110 | * operations. | |
0cd6144a | 1111 | */ |
1613fac9 | 1112 | void folio_batch_remove_exceptionals(struct folio_batch *fbatch) |
0cd6144a | 1113 | { |
1613fac9 | 1114 | unsigned int i, j; |
0cd6144a | 1115 | |
1613fac9 MWO |
1116 | for (i = 0, j = 0; i < folio_batch_count(fbatch); i++) { |
1117 | struct folio *folio = fbatch->folios[i]; | |
1118 | if (!xa_is_value(folio)) | |
1119 | fbatch->folios[j++] = folio; | |
0cd6144a | 1120 | } |
1613fac9 | 1121 | fbatch->nr = j; |
0cd6144a JW |
1122 | } |
1123 | ||
1da177e4 LT |
1124 | /* |
1125 | * Perform any setup for the swap system | |
1126 | */ | |
1127 | void __init swap_setup(void) | |
1128 | { | |
ca79b0c2 | 1129 | unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT); |
e0bf68dd | 1130 | |
1da177e4 LT |
1131 | /* Use a smaller cluster for small-memory machines */ |
1132 | if (megs < 16) | |
1133 | page_cluster = 2; | |
1134 | else | |
1135 | page_cluster = 3; | |
1136 | /* | |
1137 | * Right now other parts of the system means that we | |
1138 | * _really_ don't want to cluster much more | |
1139 | */ | |
1da177e4 | 1140 | } |