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