hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/mm/madvise.c
....@@ -11,27 +11,39 @@
1111 #include <linux/syscalls.h>
1212 #include <linux/mempolicy.h>
1313 #include <linux/page-isolation.h>
14
+#include <linux/page_idle.h>
1415 #include <linux/userfaultfd_k.h>
1516 #include <linux/hugetlb.h>
1617 #include <linux/falloc.h>
18
+#include <linux/fadvise.h>
1719 #include <linux/sched.h>
20
+#include <linux/sched/mm.h>
21
+#include <linux/uio.h>
1822 #include <linux/ksm.h>
1923 #include <linux/fs.h>
2024 #include <linux/file.h>
2125 #include <linux/blkdev.h>
2226 #include <linux/backing-dev.h>
27
+#include <linux/pagewalk.h>
2328 #include <linux/swap.h>
2429 #include <linux/swapops.h>
2530 #include <linux/shmem_fs.h>
2631 #include <linux/mmu_notifier.h>
32
+#include <trace/hooks/mm.h>
2733
2834 #include <asm/tlb.h>
2935
3036 #include "internal.h"
3137
38
+struct madvise_walk_private {
39
+ struct mmu_gather *tlb;
40
+ bool pageout;
41
+ bool can_pageout_file;
42
+};
43
+
3244 /*
3345 * Any behaviour which results in changes to the vma->vm_flags needs to
34
- * take mmap_sem for writing. Others, which simply traverse vmas, need
46
+ * take mmap_lock for writing. Others, which simply traverse vmas, need
3547 * to only take it for reading.
3648 */
3749 static int madvise_need_mmap_write(int behavior)
....@@ -40,6 +52,8 @@
4052 case MADV_REMOVE:
4153 case MADV_WILLNEED:
4254 case MADV_DONTNEED:
55
+ case MADV_COLD:
56
+ case MADV_PAGEOUT:
4357 case MADV_FREE:
4458 return 0;
4559 default:
....@@ -105,28 +119,14 @@
105119 case MADV_MERGEABLE:
106120 case MADV_UNMERGEABLE:
107121 error = ksm_madvise(vma, start, end, behavior, &new_flags);
108
- if (error) {
109
- /*
110
- * madvise() returns EAGAIN if kernel resources, such as
111
- * slab, are temporarily unavailable.
112
- */
113
- if (error == -ENOMEM)
114
- error = -EAGAIN;
115
- goto out;
116
- }
122
+ if (error)
123
+ goto out_convert_errno;
117124 break;
118125 case MADV_HUGEPAGE:
119126 case MADV_NOHUGEPAGE:
120127 error = hugepage_madvise(vma, &new_flags, behavior);
121
- if (error) {
122
- /*
123
- * madvise() returns EAGAIN if kernel resources, such as
124
- * slab, are temporarily unavailable.
125
- */
126
- if (error == -ENOMEM)
127
- error = -EAGAIN;
128
- goto out;
129
- }
128
+ if (error)
129
+ goto out_convert_errno;
130130 break;
131131 }
132132
....@@ -152,15 +152,8 @@
152152 goto out;
153153 }
154154 error = __split_vma(mm, vma, start, 1);
155
- if (error) {
156
- /*
157
- * madvise() returns EAGAIN if kernel resources, such as
158
- * slab, are temporarily unavailable.
159
- */
160
- if (error == -ENOMEM)
161
- error = -EAGAIN;
162
- goto out;
163
- }
155
+ if (error)
156
+ goto out_convert_errno;
164157 }
165158
166159 if (end != vma->vm_end) {
....@@ -169,22 +162,25 @@
169162 goto out;
170163 }
171164 error = __split_vma(mm, vma, end, 0);
172
- if (error) {
173
- /*
174
- * madvise() returns EAGAIN if kernel resources, such as
175
- * slab, are temporarily unavailable.
176
- */
177
- if (error == -ENOMEM)
178
- error = -EAGAIN;
179
- goto out;
180
- }
165
+ if (error)
166
+ goto out_convert_errno;
181167 }
182168
183169 success:
184170 /*
185
- * vm_flags is protected by the mmap_sem held in write mode.
171
+ * vm_flags is protected by the mmap_lock held in write mode.
186172 */
187
- vma->vm_flags = new_flags;
173
+ vm_write_begin(vma);
174
+ WRITE_ONCE(vma->vm_flags, new_flags);
175
+ vm_write_end(vma);
176
+
177
+out_convert_errno:
178
+ /*
179
+ * madvise() returns EAGAIN if kernel resources, such as
180
+ * slab, are temporarily unavailable.
181
+ */
182
+ if (error == -ENOMEM)
183
+ error = -EAGAIN;
188184 out:
189185 return error;
190186 }
....@@ -225,43 +221,36 @@
225221 return 0;
226222 }
227223
228
-static void force_swapin_readahead(struct vm_area_struct *vma,
229
- unsigned long start, unsigned long end)
230
-{
231
- struct mm_walk walk = {
232
- .mm = vma->vm_mm,
233
- .pmd_entry = swapin_walk_pmd_entry,
234
- .private = vma,
235
- };
236
-
237
- walk_page_range(start, end, &walk);
238
-
239
- lru_add_drain(); /* Push any new pages onto the LRU now */
240
-}
224
+static const struct mm_walk_ops swapin_walk_ops = {
225
+ .pmd_entry = swapin_walk_pmd_entry,
226
+};
241227
242228 static void force_shm_swapin_readahead(struct vm_area_struct *vma,
243229 unsigned long start, unsigned long end,
244230 struct address_space *mapping)
245231 {
246
- pgoff_t index;
232
+ XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
233
+ pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
247234 struct page *page;
248
- swp_entry_t swap;
249235
250
- for (; start < end; start += PAGE_SIZE) {
251
- index = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
236
+ rcu_read_lock();
237
+ xas_for_each(&xas, page, end_index) {
238
+ swp_entry_t swap;
252239
253
- page = find_get_entry(mapping, index);
254
- if (!radix_tree_exceptional_entry(page)) {
255
- if (page)
256
- put_page(page);
240
+ if (!xa_is_value(page))
257241 continue;
258
- }
242
+ xas_pause(&xas);
243
+ rcu_read_unlock();
244
+
259245 swap = radix_to_swp_entry(page);
260246 page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
261247 NULL, 0, false);
262248 if (page)
263249 put_page(page);
250
+
251
+ rcu_read_lock();
264252 }
253
+ rcu_read_unlock();
265254
266255 lru_add_drain(); /* Push any new pages onto the LRU now */
267256 }
....@@ -274,12 +263,15 @@
274263 struct vm_area_struct **prev,
275264 unsigned long start, unsigned long end)
276265 {
266
+ struct mm_struct *mm = vma->vm_mm;
277267 struct file *file = vma->vm_file;
268
+ loff_t offset;
278269
279270 *prev = vma;
280271 #ifdef CONFIG_SWAP
281272 if (!file) {
282
- force_swapin_readahead(vma, start, end);
273
+ walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
274
+ lru_add_drain(); /* Push any new pages onto the LRU now */
283275 return 0;
284276 }
285277
....@@ -298,12 +290,304 @@
298290 return 0;
299291 }
300292
301
- start = ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
302
- if (end > vma->vm_end)
303
- end = vma->vm_end;
304
- end = ((end - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
293
+ /*
294
+ * Filesystem's fadvise may need to take various locks. We need to
295
+ * explicitly grab a reference because the vma (and hence the
296
+ * vma's reference to the file) can go away as soon as we drop
297
+ * mmap_lock.
298
+ */
299
+ *prev = NULL; /* tell sys_madvise we drop mmap_lock */
300
+ get_file(file);
301
+ offset = (loff_t)(start - vma->vm_start)
302
+ + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
303
+ mmap_read_unlock(mm);
304
+ vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
305
+ fput(file);
306
+ mmap_read_lock(mm);
307
+ return 0;
308
+}
305309
306
- force_page_cache_readahead(file->f_mapping, file, start, end - start);
310
+static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
311
+ unsigned long addr, unsigned long end,
312
+ struct mm_walk *walk)
313
+{
314
+ struct madvise_walk_private *private = walk->private;
315
+ struct mmu_gather *tlb = private->tlb;
316
+ bool pageout = private->pageout;
317
+ bool pageout_anon_only = pageout && !private->can_pageout_file;
318
+ struct mm_struct *mm = tlb->mm;
319
+ struct vm_area_struct *vma = walk->vma;
320
+ pte_t *orig_pte, *pte, ptent;
321
+ spinlock_t *ptl;
322
+ struct page *page = NULL;
323
+ LIST_HEAD(page_list);
324
+ bool allow_shared = false;
325
+
326
+ if (fatal_signal_pending(current))
327
+ return -EINTR;
328
+
329
+ trace_android_vh_madvise_cold_or_pageout(vma, &allow_shared);
330
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
331
+ if (pmd_trans_huge(*pmd)) {
332
+ pmd_t orig_pmd;
333
+ unsigned long next = pmd_addr_end(addr, end);
334
+
335
+ tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
336
+ ptl = pmd_trans_huge_lock(pmd, vma);
337
+ if (!ptl)
338
+ return 0;
339
+
340
+ orig_pmd = *pmd;
341
+ if (is_huge_zero_pmd(orig_pmd))
342
+ goto huge_unlock;
343
+
344
+ if (unlikely(!pmd_present(orig_pmd))) {
345
+ VM_BUG_ON(thp_migration_supported() &&
346
+ !is_pmd_migration_entry(orig_pmd));
347
+ goto huge_unlock;
348
+ }
349
+
350
+ page = pmd_page(orig_pmd);
351
+
352
+ /* Do not interfere with other mappings of this page */
353
+ if (page_mapcount(page) != 1)
354
+ goto huge_unlock;
355
+
356
+ if (pageout_anon_only && !PageAnon(page))
357
+ goto huge_unlock;
358
+
359
+ if (next - addr != HPAGE_PMD_SIZE) {
360
+ int err;
361
+
362
+ get_page(page);
363
+ spin_unlock(ptl);
364
+ lock_page(page);
365
+ err = split_huge_page(page);
366
+ unlock_page(page);
367
+ put_page(page);
368
+ if (!err)
369
+ goto regular_page;
370
+ return 0;
371
+ }
372
+
373
+ if (pmd_young(orig_pmd)) {
374
+ pmdp_invalidate(vma, addr, pmd);
375
+ orig_pmd = pmd_mkold(orig_pmd);
376
+
377
+ set_pmd_at(mm, addr, pmd, orig_pmd);
378
+ tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
379
+ }
380
+
381
+ ClearPageReferenced(page);
382
+ test_and_clear_page_young(page);
383
+ if (pageout) {
384
+ if (!isolate_lru_page(page)) {
385
+ if (PageUnevictable(page))
386
+ putback_lru_page(page);
387
+ else
388
+ list_add(&page->lru, &page_list);
389
+ }
390
+ } else
391
+ deactivate_page(page);
392
+huge_unlock:
393
+ spin_unlock(ptl);
394
+ if (pageout)
395
+ reclaim_pages(&page_list);
396
+ return 0;
397
+ }
398
+
399
+regular_page:
400
+ if (pmd_trans_unstable(pmd))
401
+ return 0;
402
+#endif
403
+ tlb_change_page_size(tlb, PAGE_SIZE);
404
+ orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
405
+ flush_tlb_batched_pending(mm);
406
+ arch_enter_lazy_mmu_mode();
407
+ for (; addr < end; pte++, addr += PAGE_SIZE) {
408
+ ptent = *pte;
409
+
410
+ if (pte_none(ptent))
411
+ continue;
412
+
413
+ if (!pte_present(ptent))
414
+ continue;
415
+
416
+ page = vm_normal_page(vma, addr, ptent);
417
+ if (!page)
418
+ continue;
419
+
420
+ /*
421
+ * Creating a THP page is expensive so split it only if we
422
+ * are sure it's worth. Split it if we are only owner.
423
+ */
424
+ if (PageTransCompound(page)) {
425
+ if (page_mapcount(page) != 1)
426
+ break;
427
+ if (pageout_anon_only && !PageAnon(page))
428
+ break;
429
+ get_page(page);
430
+ if (!trylock_page(page)) {
431
+ put_page(page);
432
+ break;
433
+ }
434
+ pte_unmap_unlock(orig_pte, ptl);
435
+ if (split_huge_page(page)) {
436
+ unlock_page(page);
437
+ put_page(page);
438
+ pte_offset_map_lock(mm, pmd, addr, &ptl);
439
+ break;
440
+ }
441
+ unlock_page(page);
442
+ put_page(page);
443
+ pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
444
+ pte--;
445
+ addr -= PAGE_SIZE;
446
+ continue;
447
+ }
448
+
449
+ /*
450
+ * Do not interfere with other mappings of this page and
451
+ * non-LRU page.
452
+ */
453
+ if (!allow_shared && (!PageLRU(page) || page_mapcount(page) != 1))
454
+ continue;
455
+
456
+ if (pageout_anon_only && !PageAnon(page))
457
+ continue;
458
+
459
+ VM_BUG_ON_PAGE(PageTransCompound(page), page);
460
+
461
+ if (pte_young(ptent)) {
462
+ ptent = ptep_get_and_clear_full(mm, addr, pte,
463
+ tlb->fullmm);
464
+ ptent = pte_mkold(ptent);
465
+ set_pte_at(mm, addr, pte, ptent);
466
+ tlb_remove_tlb_entry(tlb, pte, addr);
467
+ }
468
+
469
+ /*
470
+ * We are deactivating a page for accelerating reclaiming.
471
+ * VM couldn't reclaim the page unless we clear PG_young.
472
+ * As a side effect, it makes confuse idle-page tracking
473
+ * because they will miss recent referenced history.
474
+ */
475
+ ClearPageReferenced(page);
476
+ test_and_clear_page_young(page);
477
+ if (pageout) {
478
+ if (!isolate_lru_page(page)) {
479
+ if (PageUnevictable(page))
480
+ putback_lru_page(page);
481
+ else {
482
+ list_add(&page->lru, &page_list);
483
+ trace_android_vh_page_isolated_for_reclaim(mm, page);
484
+ }
485
+ }
486
+ } else
487
+ deactivate_page(page);
488
+ }
489
+
490
+ arch_leave_lazy_mmu_mode();
491
+ pte_unmap_unlock(orig_pte, ptl);
492
+ if (pageout)
493
+ reclaim_pages(&page_list);
494
+ cond_resched();
495
+
496
+ return 0;
497
+}
498
+
499
+static const struct mm_walk_ops cold_walk_ops = {
500
+ .pmd_entry = madvise_cold_or_pageout_pte_range,
501
+};
502
+
503
+static void madvise_cold_page_range(struct mmu_gather *tlb,
504
+ struct vm_area_struct *vma,
505
+ unsigned long addr, unsigned long end)
506
+{
507
+ struct madvise_walk_private walk_private = {
508
+ .pageout = false,
509
+ .tlb = tlb,
510
+ };
511
+
512
+ tlb_start_vma(tlb, vma);
513
+ walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
514
+ tlb_end_vma(tlb, vma);
515
+}
516
+
517
+static long madvise_cold(struct vm_area_struct *vma,
518
+ struct vm_area_struct **prev,
519
+ unsigned long start_addr, unsigned long end_addr)
520
+{
521
+ struct mm_struct *mm = vma->vm_mm;
522
+ struct mmu_gather tlb;
523
+
524
+ *prev = vma;
525
+ if (!can_madv_lru_vma(vma))
526
+ return -EINVAL;
527
+
528
+ lru_add_drain();
529
+ tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
530
+ madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
531
+ tlb_finish_mmu(&tlb, start_addr, end_addr);
532
+
533
+ return 0;
534
+}
535
+
536
+static void madvise_pageout_page_range(struct mmu_gather *tlb,
537
+ struct vm_area_struct *vma,
538
+ unsigned long addr, unsigned long end,
539
+ bool can_pageout_file)
540
+{
541
+ struct madvise_walk_private walk_private = {
542
+ .pageout = true,
543
+ .tlb = tlb,
544
+ .can_pageout_file = can_pageout_file,
545
+ };
546
+
547
+ tlb_start_vma(tlb, vma);
548
+ walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
549
+ tlb_end_vma(tlb, vma);
550
+}
551
+
552
+static inline bool can_do_file_pageout(struct vm_area_struct *vma)
553
+{
554
+ if (!vma->vm_file)
555
+ return false;
556
+ /*
557
+ * paging out pagecache only for non-anonymous mappings that correspond
558
+ * to the files the calling process could (if tried) open for writing;
559
+ * otherwise we'd be including shared non-exclusive mappings, which
560
+ * opens a side channel.
561
+ */
562
+ return inode_owner_or_capable(file_inode(vma->vm_file)) ||
563
+ inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
564
+}
565
+
566
+static long madvise_pageout(struct vm_area_struct *vma,
567
+ struct vm_area_struct **prev,
568
+ unsigned long start_addr, unsigned long end_addr)
569
+{
570
+ struct mm_struct *mm = vma->vm_mm;
571
+ struct mmu_gather tlb;
572
+ bool can_pageout_file;
573
+
574
+ *prev = vma;
575
+ if (!can_madv_lru_vma(vma))
576
+ return -EINVAL;
577
+
578
+ /*
579
+ * If the VMA belongs to a private file mapping, there can be private
580
+ * dirty pages which can be paged out if even this process is neither
581
+ * owner nor write capable of the file. Cache the file access check
582
+ * here and use it later during page walk.
583
+ */
584
+ can_pageout_file = can_do_file_pageout(vma);
585
+
586
+ lru_add_drain();
587
+ tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
588
+ madvise_pageout_page_range(&tlb, vma, start_addr, end_addr, can_pageout_file);
589
+ tlb_finish_mmu(&tlb, start_addr, end_addr);
590
+
307591 return 0;
308592 }
309593
....@@ -328,7 +612,7 @@
328612 if (pmd_trans_unstable(pmd))
329613 return 0;
330614
331
- tlb_remove_check_page_size_change(tlb, PAGE_SIZE);
615
+ tlb_change_page_size(tlb, PAGE_SIZE);
332616 orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
333617 flush_tlb_batched_pending(mm);
334618 arch_enter_lazy_mmu_mode();
....@@ -354,7 +638,7 @@
354638 continue;
355639 }
356640
357
- page = _vm_normal_page(vma, addr, ptent, true);
641
+ page = vm_normal_page(vma, addr, ptent);
358642 if (!page)
359643 continue;
360644
....@@ -440,47 +724,41 @@
440724 return 0;
441725 }
442726
443
-static void madvise_free_page_range(struct mmu_gather *tlb,
444
- struct vm_area_struct *vma,
445
- unsigned long addr, unsigned long end)
446
-{
447
- struct mm_walk free_walk = {
448
- .pmd_entry = madvise_free_pte_range,
449
- .mm = vma->vm_mm,
450
- .private = tlb,
451
- };
452
-
453
- tlb_start_vma(tlb, vma);
454
- walk_page_range(addr, end, &free_walk);
455
- tlb_end_vma(tlb, vma);
456
-}
727
+static const struct mm_walk_ops madvise_free_walk_ops = {
728
+ .pmd_entry = madvise_free_pte_range,
729
+};
457730
458731 static int madvise_free_single_vma(struct vm_area_struct *vma,
459732 unsigned long start_addr, unsigned long end_addr)
460733 {
461
- unsigned long start, end;
462734 struct mm_struct *mm = vma->vm_mm;
735
+ struct mmu_notifier_range range;
463736 struct mmu_gather tlb;
464737
465738 /* MADV_FREE works for only anon vma at the moment */
466739 if (!vma_is_anonymous(vma))
467740 return -EINVAL;
468741
469
- start = max(vma->vm_start, start_addr);
470
- if (start >= vma->vm_end)
742
+ range.start = max(vma->vm_start, start_addr);
743
+ if (range.start >= vma->vm_end)
471744 return -EINVAL;
472
- end = min(vma->vm_end, end_addr);
473
- if (end <= vma->vm_start)
745
+ range.end = min(vma->vm_end, end_addr);
746
+ if (range.end <= vma->vm_start)
474747 return -EINVAL;
748
+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
749
+ range.start, range.end);
475750
476751 lru_add_drain();
477
- tlb_gather_mmu(&tlb, mm, start, end);
752
+ tlb_gather_mmu(&tlb, mm, range.start, range.end);
478753 update_hiwater_rss(mm);
479754
480
- mmu_notifier_invalidate_range_start(mm, start, end);
481
- madvise_free_page_range(&tlb, vma, start, end);
482
- mmu_notifier_invalidate_range_end(mm, start, end);
483
- tlb_finish_mmu(&tlb, start, end);
755
+ mmu_notifier_invalidate_range_start(&range);
756
+ tlb_start_vma(&tlb, vma);
757
+ walk_page_range(vma->vm_mm, range.start, range.end,
758
+ &madvise_free_walk_ops, &tlb);
759
+ tlb_end_vma(&tlb, vma);
760
+ mmu_notifier_invalidate_range_end(&range);
761
+ tlb_finish_mmu(&tlb, range.start, range.end);
484762
485763 return 0;
486764 }
....@@ -516,15 +794,17 @@
516794 unsigned long start, unsigned long end,
517795 int behavior)
518796 {
797
+ struct mm_struct *mm = vma->vm_mm;
798
+
519799 *prev = vma;
520
- if (!can_madv_dontneed_vma(vma))
800
+ if (!can_madv_lru_vma(vma))
521801 return -EINVAL;
522802
523803 if (!userfaultfd_remove(vma, start, end)) {
524
- *prev = NULL; /* mmap_sem has been dropped, prev is stale */
804
+ *prev = NULL; /* mmap_lock has been dropped, prev is stale */
525805
526
- down_read(&current->mm->mmap_sem);
527
- vma = find_vma(current->mm, start);
806
+ mmap_read_lock(mm);
807
+ vma = find_vma(mm, start);
528808 if (!vma)
529809 return -ENOMEM;
530810 if (start < vma->vm_start) {
....@@ -539,12 +819,12 @@
539819 */
540820 return -ENOMEM;
541821 }
542
- if (!can_madv_dontneed_vma(vma))
822
+ if (!can_madv_lru_vma(vma))
543823 return -EINVAL;
544824 if (end > vma->vm_end) {
545825 /*
546826 * Don't fail if end > vma->vm_end. If the old
547
- * vma was splitted while the mmap_sem was
827
+ * vma was splitted while the mmap_lock was
548828 * released the effect of the concurrent
549829 * operation may not cause madvise() to
550830 * have an undefined result. There may be an
....@@ -578,8 +858,9 @@
578858 loff_t offset;
579859 int error;
580860 struct file *f;
861
+ struct mm_struct *mm = vma->vm_mm;
581862
582
- *prev = NULL; /* tell sys_madvise we drop mmap_sem */
863
+ *prev = NULL; /* tell sys_madvise we drop mmap_lock */
583864
584865 if (vma->vm_flags & VM_LOCKED)
585866 return -EINVAL;
....@@ -600,18 +881,18 @@
600881 * Filesystem's fallocate may need to take i_mutex. We need to
601882 * explicitly grab a reference because the vma (and hence the
602883 * vma's reference to the file) can go away as soon as we drop
603
- * mmap_sem.
884
+ * mmap_lock.
604885 */
605886 get_file(f);
606887 if (userfaultfd_remove(vma, start, end)) {
607
- /* mmap_sem was not released by userfaultfd_remove() */
608
- up_read(&current->mm->mmap_sem);
888
+ /* mmap_lock was not released by userfaultfd_remove() */
889
+ mmap_read_unlock(mm);
609890 }
610891 error = vfs_fallocate(f,
611892 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
612893 offset, end - start);
613894 fput(f);
614
- down_read(&current->mm->mmap_sem);
895
+ mmap_read_lock(mm);
615896 return error;
616897 }
617898
....@@ -622,16 +903,16 @@
622903 static int madvise_inject_error(int behavior,
623904 unsigned long start, unsigned long end)
624905 {
625
- struct page *page;
626906 struct zone *zone;
627
- unsigned int order;
907
+ unsigned long size;
628908
629909 if (!capable(CAP_SYS_ADMIN))
630910 return -EPERM;
631911
632912
633
- for (; start < end; start += PAGE_SIZE << order) {
913
+ for (; start < end; start += size) {
634914 unsigned long pfn;
915
+ struct page *page;
635916 int ret;
636917
637918 ret = get_user_pages_fast(start, 1, 0, &page);
....@@ -642,36 +923,20 @@
642923 /*
643924 * When soft offlining hugepages, after migrating the page
644925 * we dissolve it, therefore in the second loop "page" will
645
- * no longer be a compound page, and order will be 0.
926
+ * no longer be a compound page.
646927 */
647
- order = compound_order(compound_head(page));
648
-
649
- if (PageHWPoison(page)) {
650
- put_page(page);
651
- continue;
652
- }
928
+ size = page_size(compound_head(page));
653929
654930 if (behavior == MADV_SOFT_OFFLINE) {
655931 pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
656
- pfn, start);
657
-
658
- ret = soft_offline_page(page, MF_COUNT_INCREASED);
659
- if (ret)
660
- return ret;
661
- continue;
932
+ pfn, start);
933
+ ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
934
+ } else {
935
+ pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
936
+ pfn, start);
937
+ ret = memory_failure(pfn, MF_COUNT_INCREASED);
662938 }
663939
664
- pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
665
- pfn, start);
666
-
667
- /*
668
- * Drop the page reference taken by get_user_pages_fast(). In
669
- * the absence of MF_COUNT_INCREASED the memory_failure()
670
- * routine is responsible for pinning the page to prevent it
671
- * from being released back to the page allocator.
672
- */
673
- put_page(page);
674
- ret = memory_failure(pfn, 0);
675940 if (ret)
676941 return ret;
677942 }
....@@ -693,6 +958,10 @@
693958 return madvise_remove(vma, prev, start, end);
694959 case MADV_WILLNEED:
695960 return madvise_willneed(vma, prev, start, end);
961
+ case MADV_COLD:
962
+ return madvise_cold(vma, prev, start, end);
963
+ case MADV_PAGEOUT:
964
+ return madvise_pageout(vma, prev, start, end);
696965 case MADV_FREE:
697966 case MADV_DONTNEED:
698967 return madvise_dontneed_free(vma, prev, start, end, behavior);
....@@ -714,6 +983,8 @@
714983 case MADV_WILLNEED:
715984 case MADV_DONTNEED:
716985 case MADV_FREE:
986
+ case MADV_COLD:
987
+ case MADV_PAGEOUT:
717988 #ifdef CONFIG_KSM
718989 case MADV_MERGEABLE:
719990 case MADV_UNMERGEABLE:
....@@ -732,6 +1003,19 @@
7321003 #endif
7331004 return true;
7341005
1006
+ default:
1007
+ return false;
1008
+ }
1009
+}
1010
+
1011
+static bool
1012
+process_madvise_behavior_valid(int behavior)
1013
+{
1014
+ switch (behavior) {
1015
+ case MADV_COLD:
1016
+ case MADV_PAGEOUT:
1017
+ case MADV_WILLNEED:
1018
+ return true;
7351019 default:
7361020 return false;
7371021 }
....@@ -784,6 +1068,11 @@
7841068 * MADV_DONTDUMP - the application wants to prevent pages in the given range
7851069 * from being included in its core dump.
7861070 * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1071
+ * MADV_COLD - the application is not expected to use this memory soon,
1072
+ * deactivate pages in this range so that they can be reclaimed
1073
+ * easily if memory pressure hanppens.
1074
+ * MADV_PAGEOUT - the application is not expected to use this memory soon,
1075
+ * page out the pages in this range immediately.
7871076 *
7881077 * return values:
7891078 * zero - success
....@@ -798,7 +1087,7 @@
7981087 * -EBADF - map exists, but area maps something that isn't a file.
7991088 * -EAGAIN - a kernel resource was temporarily unavailable.
8001089 */
801
-SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1090
+int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
8021091 {
8031092 unsigned long end, tmp;
8041093 struct vm_area_struct *vma, *prev;
....@@ -813,9 +1102,9 @@
8131102 if (!madvise_behavior_valid(behavior))
8141103 return error;
8151104
816
- if (start & ~PAGE_MASK)
1105
+ if (!PAGE_ALIGNED(start))
8171106 return error;
818
- len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1107
+ len = PAGE_ALIGN(len_in);
8191108
8201109 /* Check to see whether len was rounded up from small -ve to zero */
8211110 if (len_in && !len)
....@@ -836,10 +1125,10 @@
8361125
8371126 write = madvise_need_mmap_write(behavior);
8381127 if (write) {
839
- if (down_write_killable(&current->mm->mmap_sem))
1128
+ if (mmap_write_lock_killable(mm))
8401129 return -EINTR;
8411130 } else {
842
- down_read(&current->mm->mmap_sem);
1131
+ mmap_read_lock(mm);
8431132 }
8441133
8451134 /*
....@@ -847,7 +1136,7 @@
8471136 * ranges, just ignore them, but return -ENOMEM at the end.
8481137 * - different from the way of handling in mlock etc.
8491138 */
850
- vma = find_vma_prev(current->mm, start, &prev);
1139
+ vma = find_vma_prev(mm, start, &prev);
8511140 if (vma && start > vma->vm_start)
8521141 prev = vma;
8531142
....@@ -883,15 +1172,100 @@
8831172 goto out;
8841173 if (prev)
8851174 vma = prev->vm_next;
886
- else /* madvise_remove dropped mmap_sem */
887
- vma = find_vma(current->mm, start);
1175
+ else /* madvise_remove dropped mmap_lock */
1176
+ vma = find_vma(mm, start);
8881177 }
8891178 out:
8901179 blk_finish_plug(&plug);
8911180 if (write)
892
- up_write(&current->mm->mmap_sem);
1181
+ mmap_write_unlock(mm);
8931182 else
894
- up_read(&current->mm->mmap_sem);
1183
+ mmap_read_unlock(mm);
8951184
8961185 return error;
8971186 }
1187
+
1188
+SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1189
+{
1190
+ return do_madvise(current->mm, start, len_in, behavior);
1191
+}
1192
+
1193
+SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1194
+ size_t, vlen, int, behavior, unsigned int, flags)
1195
+{
1196
+ ssize_t ret;
1197
+ struct iovec iovstack[UIO_FASTIOV], iovec;
1198
+ struct iovec *iov = iovstack;
1199
+ struct iov_iter iter;
1200
+ struct pid *pid;
1201
+ struct task_struct *task;
1202
+ struct mm_struct *mm;
1203
+ size_t total_len;
1204
+ unsigned int f_flags;
1205
+
1206
+ if (flags != 0) {
1207
+ ret = -EINVAL;
1208
+ goto out;
1209
+ }
1210
+
1211
+ ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1212
+ if (ret < 0)
1213
+ goto out;
1214
+
1215
+ pid = pidfd_get_pid(pidfd, &f_flags);
1216
+ if (IS_ERR(pid)) {
1217
+ ret = PTR_ERR(pid);
1218
+ goto free_iov;
1219
+ }
1220
+
1221
+ task = get_pid_task(pid, PIDTYPE_PID);
1222
+ if (!task) {
1223
+ ret = -ESRCH;
1224
+ goto put_pid;
1225
+ }
1226
+
1227
+ if (!process_madvise_behavior_valid(behavior)) {
1228
+ ret = -EINVAL;
1229
+ goto release_task;
1230
+ }
1231
+
1232
+ /* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1233
+ mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1234
+ if (IS_ERR_OR_NULL(mm)) {
1235
+ ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1236
+ goto release_task;
1237
+ }
1238
+
1239
+ /*
1240
+ * Require CAP_SYS_NICE for influencing process performance. Note that
1241
+ * only non-destructive hints are currently supported.
1242
+ */
1243
+ if (!capable(CAP_SYS_NICE)) {
1244
+ ret = -EPERM;
1245
+ goto release_mm;
1246
+ }
1247
+
1248
+ total_len = iov_iter_count(&iter);
1249
+
1250
+ while (iov_iter_count(&iter)) {
1251
+ iovec = iov_iter_iovec(&iter);
1252
+ ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1253
+ iovec.iov_len, behavior);
1254
+ if (ret < 0)
1255
+ break;
1256
+ iov_iter_advance(&iter, iovec.iov_len);
1257
+ }
1258
+
1259
+ ret = (total_len - iov_iter_count(&iter)) ? : ret;
1260
+
1261
+release_mm:
1262
+ mmput(mm);
1263
+release_task:
1264
+ put_task_struct(task);
1265
+put_pid:
1266
+ put_pid(pid);
1267
+free_iov:
1268
+ kfree(iov);
1269
+out:
1270
+ return ret;
1271
+}