hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/mm/swap.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/mm/swap.c
34 *
....@@ -7,7 +8,7 @@
78 /*
89 * This file contains the default values for the operation of the
910 * Linux VM subsystem. Fine-tuning documentation can be found in
10
- * Documentation/sysctl/vm.txt.
11
+ * Documentation/admin-guide/sysctl/vm.rst.
1112 * Started 18.12.91
1213 * Swap aging added 23.2.95, Stephen Tweedie.
1314 * Buffermem limits added 12.3.98, Rik van Riel.
....@@ -29,12 +30,13 @@
2930 #include <linux/cpu.h>
3031 #include <linux/notifier.h>
3132 #include <linux/backing-dev.h>
32
-#include <linux/memremap.h>
3333 #include <linux/memcontrol.h>
3434 #include <linux/gfp.h>
3535 #include <linux/uio.h>
3636 #include <linux/hugetlb.h>
3737 #include <linux/page_idle.h>
38
+#include <linux/local_lock.h>
39
+#include <linux/buffer_head.h>
3840
3941 #include "internal.h"
4042
....@@ -44,13 +46,33 @@
4446 /* How many pages do we try to swap or page in/out together? */
4547 int page_cluster;
4648
47
-static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
48
-static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
49
-static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
50
-static DEFINE_PER_CPU(struct pagevec, lru_lazyfree_pvecs);
49
+/* Protecting only lru_rotate.pvec which requires disabling interrupts */
50
+struct lru_rotate {
51
+ local_lock_t lock;
52
+ struct pagevec pvec;
53
+};
54
+static DEFINE_PER_CPU(struct lru_rotate, lru_rotate) = {
55
+ .lock = INIT_LOCAL_LOCK(lock),
56
+};
57
+
58
+/*
59
+ * The following struct pagevec are grouped together because they are protected
60
+ * by disabling preemption (and interrupts remain enabled).
61
+ */
62
+struct lru_pvecs {
63
+ local_lock_t lock;
64
+ struct pagevec lru_add;
65
+ struct pagevec lru_deactivate_file;
66
+ struct pagevec lru_deactivate;
67
+ struct pagevec lru_lazyfree;
68
+ struct pagevec lru_lazyfree_movetail;
5169 #ifdef CONFIG_SMP
52
-static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
70
+ struct pagevec activate_page;
5371 #endif
72
+};
73
+static DEFINE_PER_CPU(struct lru_pvecs, lru_pvecs) = {
74
+ .lock = INIT_LOCAL_LOCK(lock),
75
+};
5476
5577 /*
5678 * This path almost never happens for VM activity - pages are normally
....@@ -59,31 +81,29 @@
5981 static void __page_cache_release(struct page *page)
6082 {
6183 if (PageLRU(page)) {
62
- struct zone *zone = page_zone(page);
84
+ pg_data_t *pgdat = page_pgdat(page);
6385 struct lruvec *lruvec;
6486 unsigned long flags;
6587
66
- spin_lock_irqsave(zone_lru_lock(zone), flags);
67
- lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
88
+ spin_lock_irqsave(&pgdat->lru_lock, flags);
89
+ lruvec = mem_cgroup_page_lruvec(page, pgdat);
6890 VM_BUG_ON_PAGE(!PageLRU(page), page);
6991 __ClearPageLRU(page);
7092 del_page_from_lru_list(page, lruvec, page_off_lru(page));
71
- spin_unlock_irqrestore(zone_lru_lock(zone), flags);
93
+ spin_unlock_irqrestore(&pgdat->lru_lock, flags);
7294 }
7395 __ClearPageWaiters(page);
74
- mem_cgroup_uncharge(page);
7596 }
7697
7798 static void __put_single_page(struct page *page)
7899 {
79100 __page_cache_release(page);
101
+ mem_cgroup_uncharge(page);
80102 free_unref_page(page);
81103 }
82104
83105 static void __put_compound_page(struct page *page)
84106 {
85
- compound_page_dtor *dtor;
86
-
87107 /*
88108 * __page_cache_release() is supposed to be called for thp, not for
89109 * hugetlb. This is because hugetlb page does never have PageLRU set
....@@ -92,8 +112,7 @@
92112 */
93113 if (!PageHuge(page))
94114 __page_cache_release(page);
95
- dtor = get_compound_page_dtor(page);
96
- (*dtor)(page);
115
+ destroy_compound_page(page);
97116 }
98117
99118 void __put_page(struct page *page)
....@@ -127,7 +146,7 @@
127146 while (!list_empty(pages)) {
128147 struct page *victim;
129148
130
- victim = list_entry(pages->prev, struct page, lru);
149
+ victim = lru_to_page(pages);
131150 list_del(&victim->lru);
132151 put_page(victim);
133152 }
....@@ -224,7 +243,7 @@
224243 del_page_from_lru_list(page, lruvec, page_lru(page));
225244 ClearPageActive(page);
226245 add_page_to_lru_list_tail(page, lruvec, page_lru(page));
227
- (*pgmoved)++;
246
+ (*pgmoved) += thp_nr_pages(page);
228247 }
229248 }
230249
....@@ -240,6 +259,18 @@
240259 __count_vm_events(PGROTATED, pgmoved);
241260 }
242261
262
+/* return true if pagevec needs to drain */
263
+static bool pagevec_add_and_need_flush(struct pagevec *pvec, struct page *page)
264
+{
265
+ bool ret = false;
266
+
267
+ if (!pagevec_add(pvec, page) || PageCompound(page) ||
268
+ lru_cache_disabled())
269
+ ret = true;
270
+
271
+ return ret;
272
+}
273
+
243274 /*
244275 * Writeback is about to end against a page which has been marked for immediate
245276 * reclaim. If it still appears to be reclaimable, move it to the tail of the
....@@ -253,30 +284,57 @@
253284 unsigned long flags;
254285
255286 get_page(page);
256
- local_irq_save(flags);
257
- pvec = this_cpu_ptr(&lru_rotate_pvecs);
258
- if (!pagevec_add(pvec, page) || PageCompound(page))
287
+ local_lock_irqsave(&lru_rotate.lock, flags);
288
+ pvec = this_cpu_ptr(&lru_rotate.pvec);
289
+ if (pagevec_add_and_need_flush(pvec, page))
259290 pagevec_move_tail(pvec);
260
- local_irq_restore(flags);
291
+ local_unlock_irqrestore(&lru_rotate.lock, flags);
261292 }
262293 }
263294
264
-static void update_page_reclaim_stat(struct lruvec *lruvec,
265
- int file, int rotated)
295
+void lru_note_cost(struct lruvec *lruvec, bool file, unsigned int nr_pages)
266296 {
267
- struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
297
+ do {
298
+ unsigned long lrusize;
268299
269
- reclaim_stat->recent_scanned[file]++;
270
- if (rotated)
271
- reclaim_stat->recent_rotated[file]++;
300
+ /* Record cost event */
301
+ if (file)
302
+ lruvec->file_cost += nr_pages;
303
+ else
304
+ lruvec->anon_cost += nr_pages;
305
+
306
+ /*
307
+ * Decay previous events
308
+ *
309
+ * Because workloads change over time (and to avoid
310
+ * overflow) we keep these statistics as a floating
311
+ * average, which ends up weighing recent refaults
312
+ * more than old ones.
313
+ */
314
+ lrusize = lruvec_page_state(lruvec, NR_INACTIVE_ANON) +
315
+ lruvec_page_state(lruvec, NR_ACTIVE_ANON) +
316
+ lruvec_page_state(lruvec, NR_INACTIVE_FILE) +
317
+ lruvec_page_state(lruvec, NR_ACTIVE_FILE);
318
+
319
+ if (lruvec->file_cost + lruvec->anon_cost > lrusize / 4) {
320
+ lruvec->file_cost /= 2;
321
+ lruvec->anon_cost /= 2;
322
+ }
323
+ } while ((lruvec = parent_lruvec(lruvec)));
324
+}
325
+
326
+void lru_note_cost_page(struct page *page)
327
+{
328
+ lru_note_cost(mem_cgroup_page_lruvec(page, page_pgdat(page)),
329
+ page_is_file_lru(page), thp_nr_pages(page));
272330 }
273331
274332 static void __activate_page(struct page *page, struct lruvec *lruvec,
275333 void *arg)
276334 {
277335 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
278
- int file = page_is_file_cache(page);
279336 int lru = page_lru_base_type(page);
337
+ int nr_pages = thp_nr_pages(page);
280338
281339 del_page_from_lru_list(page, lruvec, lru);
282340 SetPageActive(page);
....@@ -284,15 +342,16 @@
284342 add_page_to_lru_list(page, lruvec, lru);
285343 trace_mm_lru_activate(page);
286344
287
- __count_vm_event(PGACTIVATE);
288
- update_page_reclaim_stat(lruvec, file, 1);
345
+ __count_vm_events(PGACTIVATE, nr_pages);
346
+ __count_memcg_events(lruvec_memcg(lruvec), PGACTIVATE,
347
+ nr_pages);
289348 }
290349 }
291350
292351 #ifdef CONFIG_SMP
293352 static void activate_page_drain(int cpu)
294353 {
295
- struct pagevec *pvec = &per_cpu(activate_page_pvecs, cpu);
354
+ struct pagevec *pvec = &per_cpu(lru_pvecs.activate_page, cpu);
296355
297356 if (pagevec_count(pvec))
298357 pagevec_lru_move_fn(pvec, __activate_page, NULL);
....@@ -300,19 +359,21 @@
300359
301360 static bool need_activate_page_drain(int cpu)
302361 {
303
- return pagevec_count(&per_cpu(activate_page_pvecs, cpu)) != 0;
362
+ return pagevec_count(&per_cpu(lru_pvecs.activate_page, cpu)) != 0;
304363 }
305364
306
-void activate_page(struct page *page)
365
+static void activate_page(struct page *page)
307366 {
308367 page = compound_head(page);
309368 if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
310
- struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
369
+ struct pagevec *pvec;
311370
371
+ local_lock(&lru_pvecs.lock);
372
+ pvec = this_cpu_ptr(&lru_pvecs.activate_page);
312373 get_page(page);
313
- if (!pagevec_add(pvec, page) || PageCompound(page))
374
+ if (pagevec_add_and_need_flush(pvec, page))
314375 pagevec_lru_move_fn(pvec, __activate_page, NULL);
315
- put_cpu_var(activate_page_pvecs);
376
+ local_unlock(&lru_pvecs.lock);
316377 }
317378 }
318379
....@@ -321,21 +382,24 @@
321382 {
322383 }
323384
324
-void activate_page(struct page *page)
385
+static void activate_page(struct page *page)
325386 {
326
- struct zone *zone = page_zone(page);
387
+ pg_data_t *pgdat = page_pgdat(page);
327388
328389 page = compound_head(page);
329
- spin_lock_irq(zone_lru_lock(zone));
330
- __activate_page(page, mem_cgroup_page_lruvec(page, zone->zone_pgdat), NULL);
331
- spin_unlock_irq(zone_lru_lock(zone));
390
+ spin_lock_irq(&pgdat->lru_lock);
391
+ __activate_page(page, mem_cgroup_page_lruvec(page, pgdat), NULL);
392
+ spin_unlock_irq(&pgdat->lru_lock);
332393 }
333394 #endif
334395
335396 static void __lru_cache_activate_page(struct page *page)
336397 {
337
- struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
398
+ struct pagevec *pvec;
338399 int i;
400
+
401
+ local_lock(&lru_pvecs.lock);
402
+ pvec = this_cpu_ptr(&lru_pvecs.lru_add);
339403
340404 /*
341405 * Search backwards on the optimistic assumption that the page being
....@@ -356,7 +420,7 @@
356420 }
357421 }
358422
359
- put_cpu_var(lru_add_pvec);
423
+ local_unlock(&lru_pvecs.lock);
360424 }
361425
362426 /*
....@@ -372,12 +436,20 @@
372436 void mark_page_accessed(struct page *page)
373437 {
374438 page = compound_head(page);
375
- if (!PageActive(page) && !PageUnevictable(page) &&
376
- PageReferenced(page)) {
377439
440
+ trace_android_vh_mark_page_accessed(page);
441
+ if (!PageReferenced(page)) {
442
+ SetPageReferenced(page);
443
+ } else if (PageUnevictable(page)) {
444
+ /*
445
+ * Unevictable pages are on the "LRU_UNEVICTABLE" list. But,
446
+ * this list is never rotated or maintained, so marking an
447
+ * evictable page accessed has no effect.
448
+ */
449
+ } else if (!PageActive(page)) {
378450 /*
379451 * If the page is on the LRU, queue it for activation via
380
- * activate_page_pvecs. Otherwise, assume the page is on a
452
+ * lru_pvecs.activate_page. Otherwise, assume the page is on a
381453 * pagevec, mark it active and it'll be moved to the active
382454 * LRU on the next drain.
383455 */
....@@ -386,44 +458,12 @@
386458 else
387459 __lru_cache_activate_page(page);
388460 ClearPageReferenced(page);
389
- if (page_is_file_cache(page))
390
- workingset_activation(page);
391
- } else if (!PageReferenced(page)) {
392
- SetPageReferenced(page);
461
+ workingset_activation(page);
393462 }
394463 if (page_is_idle(page))
395464 clear_page_idle(page);
396465 }
397466 EXPORT_SYMBOL(mark_page_accessed);
398
-
399
-static void __lru_cache_add(struct page *page)
400
-{
401
- struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
402
-
403
- get_page(page);
404
- if (!pagevec_add(pvec, page) || PageCompound(page))
405
- __pagevec_lru_add(pvec);
406
- put_cpu_var(lru_add_pvec);
407
-}
408
-
409
-/**
410
- * lru_cache_add_anon - add a page to the page lists
411
- * @page: the page to add
412
- */
413
-void lru_cache_add_anon(struct page *page)
414
-{
415
- if (PageActive(page))
416
- ClearPageActive(page);
417
- __lru_cache_add(page);
418
-}
419
-
420
-void lru_cache_add_file(struct page *page)
421
-{
422
- if (PageActive(page))
423
- ClearPageActive(page);
424
- __lru_cache_add(page);
425
-}
426
-EXPORT_SYMBOL(lru_cache_add_file);
427467
428468 /**
429469 * lru_cache_add - add a page to a page list
....@@ -436,37 +476,45 @@
436476 */
437477 void lru_cache_add(struct page *page)
438478 {
479
+ struct pagevec *pvec;
480
+
439481 VM_BUG_ON_PAGE(PageActive(page) && PageUnevictable(page), page);
440482 VM_BUG_ON_PAGE(PageLRU(page), page);
441
- __lru_cache_add(page);
483
+
484
+ get_page(page);
485
+ local_lock(&lru_pvecs.lock);
486
+ pvec = this_cpu_ptr(&lru_pvecs.lru_add);
487
+ if (pagevec_add_and_need_flush(pvec, page))
488
+ __pagevec_lru_add(pvec);
489
+ local_unlock(&lru_pvecs.lock);
442490 }
491
+EXPORT_SYMBOL(lru_cache_add);
443492
444493 /**
445
- * lru_cache_add_active_or_unevictable
494
+ * lru_cache_add_inactive_or_unevictable
446495 * @page: the page to be added to LRU
447496 * @vma: vma in which page is mapped for determining reclaimability
448497 *
449
- * Place @page on the active or unevictable LRU list, depending on its
450
- * evictability. Note that if the page is not evictable, it goes
451
- * directly back onto it's zone's unevictable list, it does NOT use a
452
- * per cpu pagevec.
498
+ * Place @page on the inactive or unevictable LRU list, depending on its
499
+ * evictability.
453500 */
454
-void lru_cache_add_active_or_unevictable(struct page *page,
455
- struct vm_area_struct *vma)
501
+void __lru_cache_add_inactive_or_unevictable(struct page *page,
502
+ unsigned long vma_flags)
456503 {
504
+ bool unevictable;
505
+
457506 VM_BUG_ON_PAGE(PageLRU(page), page);
458507
459
- if (likely((vma->vm_flags & (VM_LOCKED | VM_SPECIAL)) != VM_LOCKED))
460
- SetPageActive(page);
461
- else if (!TestSetPageMlocked(page)) {
508
+ unevictable = (vma_flags & (VM_LOCKED | VM_SPECIAL)) == VM_LOCKED;
509
+ if (unlikely(unevictable) && !TestSetPageMlocked(page)) {
510
+ int nr_pages = thp_nr_pages(page);
462511 /*
463512 * We use the irq-unsafe __mod_zone_page_stat because this
464513 * counter is not modified from interrupt context, and the pte
465514 * lock is held(spinlock), which implies preemption disabled.
466515 */
467
- __mod_zone_page_state(page_zone(page), NR_MLOCK,
468
- hpage_nr_pages(page));
469
- count_vm_event(UNEVICTABLE_PGMLOCKED);
516
+ __mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages);
517
+ count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
470518 }
471519 lru_cache_add(page);
472520 }
....@@ -495,8 +543,9 @@
495543 static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec,
496544 void *arg)
497545 {
498
- int lru, file;
546
+ int lru;
499547 bool active;
548
+ int nr_pages = thp_nr_pages(page);
500549
501550 if (!PageLRU(page))
502551 return;
....@@ -509,13 +558,11 @@
509558 return;
510559
511560 active = PageActive(page);
512
- file = page_is_file_cache(page);
513561 lru = page_lru_base_type(page);
514562
515563 del_page_from_lru_list(page, lruvec, lru + active);
516564 ClearPageActive(page);
517565 ClearPageReferenced(page);
518
- add_page_to_lru_list(page, lruvec, lru);
519566
520567 if (PageWriteback(page) || PageDirty(page)) {
521568 /*
....@@ -523,21 +570,41 @@
523570 * It can make readahead confusing. But race window
524571 * is _really_ small and it's non-critical problem.
525572 */
573
+ add_page_to_lru_list(page, lruvec, lru);
526574 SetPageReclaim(page);
527575 } else {
528576 /*
529577 * The page's writeback ends up during pagevec
530578 * We moves tha page into tail of inactive.
531579 */
532
- list_move_tail(&page->lru, &lruvec->lists[lru]);
533
- __count_vm_event(PGROTATED);
580
+ add_page_to_lru_list_tail(page, lruvec, lru);
581
+ __count_vm_events(PGROTATED, nr_pages);
534582 }
535583
536
- if (active)
537
- __count_vm_event(PGDEACTIVATE);
538
- update_page_reclaim_stat(lruvec, file, 0);
584
+ if (active) {
585
+ __count_vm_events(PGDEACTIVATE, nr_pages);
586
+ __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
587
+ nr_pages);
588
+ }
539589 }
540590
591
+static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec,
592
+ void *arg)
593
+{
594
+ if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
595
+ int lru = page_lru_base_type(page);
596
+ int nr_pages = thp_nr_pages(page);
597
+
598
+ del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE);
599
+ ClearPageActive(page);
600
+ ClearPageReferenced(page);
601
+ add_page_to_lru_list(page, lruvec, lru);
602
+
603
+ __count_vm_events(PGDEACTIVATE, nr_pages);
604
+ __count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE,
605
+ nr_pages);
606
+ }
607
+}
541608
542609 static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec,
543610 void *arg)
....@@ -545,22 +612,43 @@
545612 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
546613 !PageSwapCache(page) && !PageUnevictable(page)) {
547614 bool active = PageActive(page);
615
+ int nr_pages = thp_nr_pages(page);
548616
549617 del_page_from_lru_list(page, lruvec,
550618 LRU_INACTIVE_ANON + active);
551619 ClearPageActive(page);
552620 ClearPageReferenced(page);
553621 /*
554
- * lazyfree pages are clean anonymous pages. They have
555
- * SwapBacked flag cleared to distinguish normal anonymous
556
- * pages
622
+ * Lazyfree pages are clean anonymous pages. They have
623
+ * PG_swapbacked flag cleared, to distinguish them from normal
624
+ * anonymous pages
557625 */
558626 ClearPageSwapBacked(page);
559627 add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
560628
561
- __count_vm_events(PGLAZYFREE, hpage_nr_pages(page));
562
- count_memcg_page_event(page, PGLAZYFREE);
563
- update_page_reclaim_stat(lruvec, 1, 0);
629
+ __count_vm_events(PGLAZYFREE, nr_pages);
630
+ __count_memcg_events(lruvec_memcg(lruvec), PGLAZYFREE,
631
+ nr_pages);
632
+ }
633
+}
634
+
635
+static void lru_lazyfree_movetail_fn(struct page *page, struct lruvec *lruvec,
636
+ void *arg)
637
+{
638
+ bool *add_to_tail = (bool *)arg;
639
+
640
+ if (PageLRU(page) && !PageUnevictable(page) && PageSwapBacked(page) &&
641
+ !PageSwapCache(page)) {
642
+ bool active = PageActive(page);
643
+
644
+ del_page_from_lru_list(page, lruvec,
645
+ LRU_INACTIVE_ANON + active);
646
+ ClearPageActive(page);
647
+ ClearPageReferenced(page);
648
+ if (add_to_tail && *add_to_tail)
649
+ add_page_to_lru_list_tail(page, lruvec, LRU_INACTIVE_FILE);
650
+ else
651
+ add_page_to_lru_list(page, lruvec, LRU_INACTIVE_FILE);
564652 }
565653 }
566654
....@@ -571,28 +659,37 @@
571659 */
572660 void lru_add_drain_cpu(int cpu)
573661 {
574
- struct pagevec *pvec = &per_cpu(lru_add_pvec, cpu);
662
+ struct pagevec *pvec = &per_cpu(lru_pvecs.lru_add, cpu);
575663
576664 if (pagevec_count(pvec))
577665 __pagevec_lru_add(pvec);
578666
579
- pvec = &per_cpu(lru_rotate_pvecs, cpu);
580
- if (pagevec_count(pvec)) {
667
+ pvec = &per_cpu(lru_rotate.pvec, cpu);
668
+ /* Disabling interrupts below acts as a compiler barrier. */
669
+ if (data_race(pagevec_count(pvec))) {
581670 unsigned long flags;
582671
583672 /* No harm done if a racing interrupt already did this */
584
- local_irq_save(flags);
673
+ local_lock_irqsave(&lru_rotate.lock, flags);
585674 pagevec_move_tail(pvec);
586
- local_irq_restore(flags);
675
+ local_unlock_irqrestore(&lru_rotate.lock, flags);
587676 }
588677
589
- pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
678
+ pvec = &per_cpu(lru_pvecs.lru_deactivate_file, cpu);
590679 if (pagevec_count(pvec))
591680 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
592681
593
- pvec = &per_cpu(lru_lazyfree_pvecs, cpu);
682
+ pvec = &per_cpu(lru_pvecs.lru_deactivate, cpu);
683
+ if (pagevec_count(pvec))
684
+ pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
685
+
686
+ pvec = &per_cpu(lru_pvecs.lru_lazyfree, cpu);
594687 if (pagevec_count(pvec))
595688 pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
689
+
690
+ pvec = &per_cpu(lru_pvecs.lru_lazyfree_movetail, cpu);
691
+ if (pagevec_count(pvec))
692
+ pagevec_lru_move_fn(pvec, lru_lazyfree_movetail_fn, NULL);
596693
597694 activate_page_drain(cpu);
598695 }
....@@ -615,11 +712,36 @@
615712 return;
616713
617714 if (likely(get_page_unless_zero(page))) {
618
- struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
715
+ struct pagevec *pvec;
619716
620
- if (!pagevec_add(pvec, page) || PageCompound(page))
717
+ local_lock(&lru_pvecs.lock);
718
+ pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate_file);
719
+
720
+ if (pagevec_add_and_need_flush(pvec, page))
621721 pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
622
- put_cpu_var(lru_deactivate_file_pvecs);
722
+ local_unlock(&lru_pvecs.lock);
723
+ }
724
+}
725
+
726
+/*
727
+ * deactivate_page - deactivate a page
728
+ * @page: page to deactivate
729
+ *
730
+ * deactivate_page() moves @page to the inactive list if @page was on the active
731
+ * list and was not an unevictable page. This is done to accelerate the reclaim
732
+ * of @page.
733
+ */
734
+void deactivate_page(struct page *page)
735
+{
736
+ if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
737
+ struct pagevec *pvec;
738
+
739
+ local_lock(&lru_pvecs.lock);
740
+ pvec = this_cpu_ptr(&lru_pvecs.lru_deactivate);
741
+ get_page(page);
742
+ if (pagevec_add_and_need_flush(pvec, page))
743
+ pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
744
+ local_unlock(&lru_pvecs.lock);
623745 }
624746 }
625747
....@@ -634,19 +756,67 @@
634756 {
635757 if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) &&
636758 !PageSwapCache(page) && !PageUnevictable(page)) {
637
- struct pagevec *pvec = &get_cpu_var(lru_lazyfree_pvecs);
759
+ struct pagevec *pvec;
638760
761
+ local_lock(&lru_pvecs.lock);
762
+ pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree);
639763 get_page(page);
640
- if (!pagevec_add(pvec, page) || PageCompound(page))
764
+ if (pagevec_add_and_need_flush(pvec, page))
641765 pagevec_lru_move_fn(pvec, lru_lazyfree_fn, NULL);
642
- put_cpu_var(lru_lazyfree_pvecs);
766
+ local_unlock(&lru_pvecs.lock);
767
+ }
768
+}
769
+
770
+/**
771
+ * mark_page_lazyfree_movetail - make a swapbacked page lazyfree
772
+ * @page: page to deactivate
773
+ *
774
+ * mark_page_lazyfree_movetail() moves @page to the tail of inactive file list.
775
+ * This is done to accelerate the reclaim of @page.
776
+ */
777
+void mark_page_lazyfree_movetail(struct page *page, bool tail)
778
+{
779
+ if (PageLRU(page) && !PageUnevictable(page) && PageSwapBacked(page) &&
780
+ !PageSwapCache(page)) {
781
+ struct pagevec *pvec;
782
+
783
+ local_lock(&lru_pvecs.lock);
784
+ pvec = this_cpu_ptr(&lru_pvecs.lru_lazyfree_movetail);
785
+ get_page(page);
786
+ if (pagevec_add_and_need_flush(pvec, page))
787
+ pagevec_lru_move_fn(pvec,
788
+ lru_lazyfree_movetail_fn, &tail);
789
+ local_unlock(&lru_pvecs.lock);
643790 }
644791 }
645792
646793 void lru_add_drain(void)
647794 {
648
- lru_add_drain_cpu(get_cpu());
649
- put_cpu();
795
+ local_lock(&lru_pvecs.lock);
796
+ lru_add_drain_cpu(smp_processor_id());
797
+ local_unlock(&lru_pvecs.lock);
798
+}
799
+
800
+/*
801
+ * It's called from per-cpu workqueue context in SMP case so
802
+ * lru_add_drain_cpu and invalidate_bh_lrus_cpu should run on
803
+ * the same cpu. It shouldn't be a problem in !SMP case since
804
+ * the core is only one and the locks will disable preemption.
805
+ */
806
+static void lru_add_and_bh_lrus_drain(void)
807
+{
808
+ local_lock(&lru_pvecs.lock);
809
+ lru_add_drain_cpu(smp_processor_id());
810
+ local_unlock(&lru_pvecs.lock);
811
+ invalidate_bh_lrus_cpu();
812
+}
813
+
814
+void lru_add_drain_cpu_zone(struct zone *zone)
815
+{
816
+ local_lock(&lru_pvecs.lock);
817
+ lru_add_drain_cpu(smp_processor_id());
818
+ drain_local_pages(zone);
819
+ local_unlock(&lru_pvecs.lock);
650820 }
651821
652822 #ifdef CONFIG_SMP
....@@ -655,7 +825,7 @@
655825
656826 static void lru_add_drain_per_cpu(struct work_struct *dummy)
657827 {
658
- lru_add_drain();
828
+ lru_add_and_bh_lrus_drain();
659829 }
660830
661831 /*
....@@ -665,11 +835,22 @@
665835 * Calling this function with cpu hotplug locks held can actually lead
666836 * to obscure indirect dependencies via WQ context.
667837 */
668
-void lru_add_drain_all(void)
838
+inline void __lru_add_drain_all(bool force_all_cpus)
669839 {
670
- static DEFINE_MUTEX(lock);
840
+ /*
841
+ * lru_drain_gen - Global pages generation number
842
+ *
843
+ * (A) Definition: global lru_drain_gen = x implies that all generations
844
+ * 0 < n <= x are already *scheduled* for draining.
845
+ *
846
+ * This is an optimization for the highly-contended use case where a
847
+ * user space workload keeps constantly generating a flow of pages for
848
+ * each CPU.
849
+ */
850
+ static unsigned int lru_drain_gen;
671851 static struct cpumask has_work;
672
- int cpu;
852
+ static DEFINE_MUTEX(lock);
853
+ unsigned cpu, this_gen;
673854
674855 /*
675856 * Make sure nobody triggers this path before mm_percpu_wq is fully
....@@ -678,34 +859,135 @@
678859 if (WARN_ON(!mm_percpu_wq))
679860 return;
680861
681
- mutex_lock(&lock);
682
- cpumask_clear(&has_work);
862
+ /*
863
+ * Guarantee pagevec counter stores visible by this CPU are visible to
864
+ * other CPUs before loading the current drain generation.
865
+ */
866
+ smp_mb();
683867
868
+ /*
869
+ * (B) Locally cache global LRU draining generation number
870
+ *
871
+ * The read barrier ensures that the counter is loaded before the mutex
872
+ * is taken. It pairs with smp_mb() inside the mutex critical section
873
+ * at (D).
874
+ */
875
+ this_gen = smp_load_acquire(&lru_drain_gen);
876
+
877
+ mutex_lock(&lock);
878
+
879
+ /*
880
+ * (C) Exit the draining operation if a newer generation, from another
881
+ * lru_add_drain_all(), was already scheduled for draining. Check (A).
882
+ */
883
+ if (unlikely(this_gen != lru_drain_gen && !force_all_cpus))
884
+ goto done;
885
+
886
+ /*
887
+ * (D) Increment global generation number
888
+ *
889
+ * Pairs with smp_load_acquire() at (B), outside of the critical
890
+ * section. Use a full memory barrier to guarantee that the new global
891
+ * drain generation number is stored before loading pagevec counters.
892
+ *
893
+ * This pairing must be done here, before the for_each_online_cpu loop
894
+ * below which drains the page vectors.
895
+ *
896
+ * Let x, y, and z represent some system CPU numbers, where x < y < z.
897
+ * Assume CPU #z is is in the middle of the for_each_online_cpu loop
898
+ * below and has already reached CPU #y's per-cpu data. CPU #x comes
899
+ * along, adds some pages to its per-cpu vectors, then calls
900
+ * lru_add_drain_all().
901
+ *
902
+ * If the paired barrier is done at any later step, e.g. after the
903
+ * loop, CPU #x will just exit at (C) and miss flushing out all of its
904
+ * added pages.
905
+ */
906
+ WRITE_ONCE(lru_drain_gen, lru_drain_gen + 1);
907
+ smp_mb();
908
+
909
+ cpumask_clear(&has_work);
684910 for_each_online_cpu(cpu) {
685911 struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
686912
687
- if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
688
- pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
689
- pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
690
- pagevec_count(&per_cpu(lru_lazyfree_pvecs, cpu)) ||
691
- need_activate_page_drain(cpu)) {
913
+ if (force_all_cpus ||
914
+ pagevec_count(&per_cpu(lru_pvecs.lru_add, cpu)) ||
915
+ data_race(pagevec_count(&per_cpu(lru_rotate.pvec, cpu))) ||
916
+ pagevec_count(&per_cpu(lru_pvecs.lru_deactivate_file, cpu)) ||
917
+ pagevec_count(&per_cpu(lru_pvecs.lru_deactivate, cpu)) ||
918
+ pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree, cpu)) ||
919
+ pagevec_count(&per_cpu(lru_pvecs.lru_lazyfree_movetail, cpu)) ||
920
+ need_activate_page_drain(cpu) ||
921
+ has_bh_in_lru(cpu, NULL)) {
692922 INIT_WORK(work, lru_add_drain_per_cpu);
693923 queue_work_on(cpu, mm_percpu_wq, work);
694
- cpumask_set_cpu(cpu, &has_work);
924
+ __cpumask_set_cpu(cpu, &has_work);
695925 }
696926 }
697927
698928 for_each_cpu(cpu, &has_work)
699929 flush_work(&per_cpu(lru_add_drain_work, cpu));
700930
931
+done:
701932 mutex_unlock(&lock);
933
+}
934
+
935
+void lru_add_drain_all(void)
936
+{
937
+ __lru_add_drain_all(false);
702938 }
703939 #else
704940 void lru_add_drain_all(void)
705941 {
706942 lru_add_drain();
707943 }
944
+#endif /* CONFIG_SMP */
945
+
946
+static atomic_t lru_disable_count = ATOMIC_INIT(0);
947
+
948
+bool lru_cache_disabled(void)
949
+{
950
+ return atomic_read(&lru_disable_count) != 0;
951
+}
952
+
953
+void lru_cache_enable(void)
954
+{
955
+ atomic_dec(&lru_disable_count);
956
+}
957
+EXPORT_SYMBOL_GPL(lru_cache_enable);
958
+
959
+/*
960
+ * lru_cache_disable() needs to be called before we start compiling
961
+ * a list of pages to be migrated using isolate_lru_page().
962
+ * It drains pages on LRU cache and then disable on all cpus until
963
+ * lru_cache_enable is called.
964
+ *
965
+ * Must be paired with a call to lru_cache_enable().
966
+ */
967
+void lru_cache_disable(void)
968
+{
969
+ /*
970
+ * If someone is already disabled lru_cache, just return with
971
+ * increasing the lru_disable_count.
972
+ */
973
+ if (atomic_inc_not_zero(&lru_disable_count))
974
+ return;
975
+#ifdef CONFIG_SMP
976
+ /*
977
+ * lru_add_drain_all in the force mode will schedule draining on
978
+ * all online CPUs so any calls of lru_cache_disabled wrapped by
979
+ * local_lock or preemption disabled would be ordered by that.
980
+ * The atomic operation doesn't need to have stronger ordering
981
+ * requirements because that is enforeced by the scheduling
982
+ * guarantees.
983
+ */
984
+ __lru_add_drain_all(true);
985
+#else
986
+ lru_add_and_bh_lrus_drain();
708987 #endif
988
+ atomic_inc(&lru_disable_count);
989
+}
990
+EXPORT_SYMBOL_GPL(lru_cache_disable);
709991
710992 /**
711993 * release_pages - batched put_page()
....@@ -721,8 +1003,8 @@
7211003 LIST_HEAD(pages_to_free);
7221004 struct pglist_data *locked_pgdat = NULL;
7231005 struct lruvec *lruvec;
724
- unsigned long uninitialized_var(flags);
725
- unsigned int uninitialized_var(lock_batch);
1006
+ unsigned long flags;
1007
+ unsigned int lock_batch;
7261008
7271009 for (i = 0; i < nr; i++) {
7281010 struct page *page = pages[i];
....@@ -737,6 +1019,7 @@
7371019 locked_pgdat = NULL;
7381020 }
7391021
1022
+ page = compound_head(page);
7401023 if (is_huge_zero_page(page))
7411024 continue;
7421025
....@@ -748,15 +1031,16 @@
7481031 }
7491032 /*
7501033 * ZONE_DEVICE pages that return 'false' from
751
- * put_devmap_managed_page() do not require special
1034
+ * page_is_devmap_managed() do not require special
7521035 * processing, and instead, expect a call to
7531036 * put_page_testzero().
7541037 */
755
- if (put_devmap_managed_page(page))
1038
+ if (page_is_devmap_managed(page)) {
1039
+ put_devmap_managed_page(page);
7561040 continue;
1041
+ }
7571042 }
7581043
759
- page = compound_head(page);
7601044 if (!put_page_testzero(page))
7611045 continue;
7621046
....@@ -787,8 +1071,6 @@
7871071 del_page_from_lru_list(page, lruvec, page_off_lru(page));
7881072 }
7891073
790
- /* Clear Active bit in case of parallel mark_page_accessed */
791
- __ClearPageActive(page);
7921074 __ClearPageWaiters(page);
7931075
7941076 list_add(&page->lru, &pages_to_free);
....@@ -827,13 +1109,10 @@
8271109 void lru_add_page_tail(struct page *page, struct page *page_tail,
8281110 struct lruvec *lruvec, struct list_head *list)
8291111 {
830
- const int file = 0;
831
-
8321112 VM_BUG_ON_PAGE(!PageHead(page), page);
8331113 VM_BUG_ON_PAGE(PageCompound(page_tail), page);
8341114 VM_BUG_ON_PAGE(PageLRU(page_tail), page);
835
- VM_BUG_ON(NR_CPUS != 1 &&
836
- !spin_is_locked(&lruvec_pgdat(lruvec)->lru_lock));
1115
+ lockdep_assert_held(&lruvec_pgdat(lruvec)->lru_lock);
8371116
8381117 if (!list)
8391118 SetPageLRU(page_tail);
....@@ -845,21 +1124,16 @@
8451124 get_page(page_tail);
8461125 list_add_tail(&page_tail->lru, list);
8471126 } else {
848
- struct list_head *list_head;
8491127 /*
8501128 * Head page has not yet been counted, as an hpage,
8511129 * so we must account for each subpage individually.
8521130 *
853
- * Use the standard add function to put page_tail on the list,
854
- * but then correct its position so they all end up in order.
1131
+ * Put page_tail on the list at the correct position
1132
+ * so they all end up in order.
8551133 */
856
- add_page_to_lru_list(page_tail, lruvec, page_lru(page_tail));
857
- list_head = page_tail->lru.prev;
858
- list_move_tail(&page_tail->lru, list_head);
1134
+ add_page_to_lru_list_tail(page_tail, lruvec,
1135
+ page_lru(page_tail));
8591136 }
860
-
861
- if (!PageUnevictable(page))
862
- update_page_reclaim_stat(lruvec, file, PageActive(page_tail));
8631137 }
8641138 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
8651139
....@@ -868,13 +1142,13 @@
8681142 {
8691143 enum lru_list lru;
8701144 int was_unevictable = TestClearPageUnevictable(page);
1145
+ int nr_pages = thp_nr_pages(page);
8711146
8721147 VM_BUG_ON_PAGE(PageLRU(page), page);
8731148
874
- SetPageLRU(page);
8751149 /*
8761150 * Page becomes evictable in two ways:
877
- * 1) Within LRU lock [munlock_vma_pages() and __munlock_pagevec()].
1151
+ * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()].
8781152 * 2) Before acquiring LRU lock to put the page to correct LRU and then
8791153 * a) do PageLRU check with lock [check_move_unevictable_pages]
8801154 * b) do PageLRU check before lock [clear_page_mlock]
....@@ -898,20 +1172,19 @@
8981172 * looking at the same page) and the evictable page will be stranded
8991173 * in an unevictable LRU.
9001174 */
901
- smp_mb();
1175
+ SetPageLRU(page);
1176
+ smp_mb__after_atomic();
9021177
9031178 if (page_evictable(page)) {
9041179 lru = page_lru(page);
905
- update_page_reclaim_stat(lruvec, page_is_file_cache(page),
906
- PageActive(page));
9071180 if (was_unevictable)
908
- count_vm_event(UNEVICTABLE_PGRESCUED);
1181
+ __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
9091182 } else {
9101183 lru = LRU_UNEVICTABLE;
9111184 ClearPageActive(page);
9121185 SetPageUnevictable(page);
9131186 if (!was_unevictable)
914
- count_vm_event(UNEVICTABLE_PGCULLED);
1187
+ __count_vm_events(UNEVICTABLE_PGCULLED, nr_pages);
9151188 }
9161189
9171190 add_page_to_lru_list(page, lruvec, lru);
....@@ -926,7 +1199,6 @@
9261199 {
9271200 pagevec_lru_move_fn(pvec, __pagevec_lru_add_fn, NULL);
9281201 }
929
-EXPORT_SYMBOL(__pagevec_lru_add);
9301202
9311203 /**
9321204 * pagevec_lookup_entries - gang pagecache lookup
....@@ -944,6 +1216,10 @@
9441216 * The search returns a group of mapping-contiguous entries with
9451217 * ascending indexes. There may be holes in the indices due to
9461218 * not-present entries.
1219
+ *
1220
+ * Only one subpage of a Transparent Huge Page is returned in one call:
1221
+ * allowing truncate_inode_pages_range() to evict the whole THP without
1222
+ * cycling through a pagevec of extra references.
9471223 *
9481224 * pagevec_lookup_entries() returns the number of entries which were
9491225 * found.
....@@ -973,7 +1249,7 @@
9731249
9741250 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
9751251 struct page *page = pvec->pages[i];
976
- if (!radix_tree_exceptional_entry(page))
1252
+ if (!xa_is_value(page))
9771253 pvec->pages[j++] = page;
9781254 }
9791255 pvec->nr = j;
....@@ -1010,7 +1286,7 @@
10101286
10111287 unsigned pagevec_lookup_range_tag(struct pagevec *pvec,
10121288 struct address_space *mapping, pgoff_t *index, pgoff_t end,
1013
- int tag)
1289
+ xa_mark_t tag)
10141290 {
10151291 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
10161292 PAGEVEC_SIZE, pvec->pages);
....@@ -1020,7 +1296,7 @@
10201296
10211297 unsigned pagevec_lookup_range_nr_tag(struct pagevec *pvec,
10221298 struct address_space *mapping, pgoff_t *index, pgoff_t end,
1023
- int tag, unsigned max_pages)
1299
+ xa_mark_t tag, unsigned max_pages)
10241300 {
10251301 pvec->nr = find_get_pages_range_tag(mapping, index, end, tag,
10261302 min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
....@@ -1032,7 +1308,7 @@
10321308 */
10331309 void __init swap_setup(void)
10341310 {
1035
- unsigned long megs = totalram_pages >> (20 - PAGE_SHIFT);
1311
+ unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
10361312
10371313 /* Use a smaller cluster for small-memory machines */
10381314 if (megs < 16)
....@@ -1044,3 +1320,26 @@
10441320 * _really_ don't want to cluster much more
10451321 */
10461322 }
1323
+
1324
+#ifdef CONFIG_DEV_PAGEMAP_OPS
1325
+void put_devmap_managed_page(struct page *page)
1326
+{
1327
+ int count;
1328
+
1329
+ if (WARN_ON_ONCE(!page_is_devmap_managed(page)))
1330
+ return;
1331
+
1332
+ count = page_ref_dec_return(page);
1333
+
1334
+ /*
1335
+ * devmap page refcounts are 1-based, rather than 0-based: if
1336
+ * refcount is 1, then the page is free and the refcount is
1337
+ * stable because nobody holds a reference on the page.
1338
+ */
1339
+ if (count == 1)
1340
+ free_devmap_managed_page(page);
1341
+ else if (!count)
1342
+ __put_page(page);
1343
+}
1344
+EXPORT_SYMBOL(put_devmap_managed_page);
1345
+#endif