• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	linux/mm/madvise.c
4  *
5  * Copyright (C) 1999  Linus Torvalds
6  * Copyright (C) 2002  Christoph Hellwig
7  */
8 
9 #include <linux/mman.h>
10 #include <linux/pagemap.h>
11 #include <linux/syscalls.h>
12 #include <linux/mempolicy.h>
13 #include <linux/page-isolation.h>
14 #include <linux/page_idle.h>
15 #include <linux/userfaultfd_k.h>
16 #include <linux/hugetlb.h>
17 #include <linux/falloc.h>
18 #include <linux/fadvise.h>
19 #include <linux/sched.h>
20 #include <linux/sched/mm.h>
21 #include <linux/mm_inline.h>
22 #include <linux/string.h>
23 #include <linux/uio.h>
24 #include <linux/ksm.h>
25 #include <linux/fs.h>
26 #include <linux/file.h>
27 #include <linux/blkdev.h>
28 #include <linux/backing-dev.h>
29 #include <linux/pagewalk.h>
30 #include <linux/swap.h>
31 #include <linux/swapops.h>
32 #include <linux/shmem_fs.h>
33 #include <linux/mmu_notifier.h>
34 
35 #include <asm/tlb.h>
36 
37 #include "internal.h"
38 
39 struct madvise_walk_private {
40 	struct mmu_gather *tlb;
41 	bool pageout;
42 };
43 
44 /*
45  * Any behaviour which results in changes to the vma->vm_flags needs to
46  * take mmap_lock for writing. Others, which simply traverse vmas, need
47  * to only take it for reading.
48  */
madvise_need_mmap_write(int behavior)49 static int madvise_need_mmap_write(int behavior)
50 {
51 	switch (behavior) {
52 	case MADV_REMOVE:
53 	case MADV_WILLNEED:
54 	case MADV_DONTNEED:
55 	case MADV_COLD:
56 	case MADV_PAGEOUT:
57 	case MADV_FREE:
58 		return 0;
59 	default:
60 		/* be safe, default to 1. list exceptions explicitly */
61 		return 1;
62 	}
63 }
64 
65 #ifdef CONFIG_ANON_VMA_NAME
anon_vma_name_alloc(const char * name)66 struct anon_vma_name *anon_vma_name_alloc(const char *name)
67 {
68 	struct anon_vma_name *anon_name;
69 	size_t count;
70 
71 	/* Add 1 for NUL terminator at the end of the anon_name->name */
72 	count = strlen(name) + 1;
73 	anon_name = kmalloc(struct_size(anon_name, name, count), GFP_KERNEL);
74 	if (anon_name) {
75 		kref_init(&anon_name->kref);
76 		memcpy(anon_name->name, name, count);
77 	}
78 
79 	return anon_name;
80 }
81 
anon_vma_name_free(struct kref * kref)82 void anon_vma_name_free(struct kref *kref)
83 {
84 	struct anon_vma_name *anon_name =
85 			container_of(kref, struct anon_vma_name, kref);
86 	kfree(anon_name);
87 }
88 
anon_vma_name(struct vm_area_struct * vma)89 struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma)
90 {
91 	mmap_assert_locked(vma->vm_mm);
92 
93 	if (vma->vm_file)
94 		return NULL;
95 
96 	return vma->anon_name;
97 }
98 
99 /* mmap_lock should be write-locked */
replace_anon_vma_name(struct vm_area_struct * vma,struct anon_vma_name * anon_name)100 static int replace_anon_vma_name(struct vm_area_struct *vma,
101 				 struct anon_vma_name *anon_name)
102 {
103 	struct anon_vma_name *orig_name = anon_vma_name(vma);
104 
105 	if (!anon_name) {
106 		vma->anon_name = NULL;
107 		anon_vma_name_put(orig_name);
108 		return 0;
109 	}
110 
111 	if (anon_vma_name_eq(orig_name, anon_name))
112 		return 0;
113 
114 	vma->anon_name = anon_vma_name_reuse(anon_name);
115 	anon_vma_name_put(orig_name);
116 
117 	return 0;
118 }
119 #else /* CONFIG_ANON_VMA_NAME */
replace_anon_vma_name(struct vm_area_struct * vma,struct anon_vma_name * anon_name)120 static int replace_anon_vma_name(struct vm_area_struct *vma,
121 				 struct anon_vma_name *anon_name)
122 {
123 	if (anon_name)
124 		return -EINVAL;
125 
126 	return 0;
127 }
128 #endif /* CONFIG_ANON_VMA_NAME */
129 /*
130  * Update the vm_flags on region of a vma, splitting it or merging it as
131  * necessary.  Must be called with mmap_sem held for writing;
132  * Caller should ensure anon_name stability by raising its refcount even when
133  * anon_name belongs to a valid vma because this function might free that vma.
134  */
madvise_update_vma(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end,unsigned long new_flags,struct anon_vma_name * anon_name)135 static int madvise_update_vma(struct vm_area_struct *vma,
136 			      struct vm_area_struct **prev, unsigned long start,
137 			      unsigned long end, unsigned long new_flags,
138 			      struct anon_vma_name *anon_name)
139 {
140 	struct mm_struct *mm = vma->vm_mm;
141 	int error;
142 	pgoff_t pgoff;
143 
144 	if (new_flags == vma->vm_flags && anon_vma_name_eq(anon_vma_name(vma), anon_name)) {
145 		*prev = vma;
146 		return 0;
147 	}
148 
149 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
150 	*prev = vma_merge(mm, *prev, start, end, new_flags, vma->anon_vma,
151 			  vma->vm_file, pgoff, vma_policy(vma),
152 			  vma->vm_userfaultfd_ctx, anon_name);
153 	if (*prev) {
154 		vma = *prev;
155 		goto success;
156 	}
157 
158 	*prev = vma;
159 
160 	if (start != vma->vm_start) {
161 		if (unlikely(mm->map_count >= sysctl_max_map_count))
162 			return -ENOMEM;
163 		error = __split_vma(mm, vma, start, 1);
164 		if (error)
165 			return error;
166 	}
167 
168 	if (end != vma->vm_end) {
169 		if (unlikely(mm->map_count >= sysctl_max_map_count))
170 			return -ENOMEM;
171 		error = __split_vma(mm, vma, end, 0);
172 		if (error)
173 			return error;
174 	}
175 
176 success:
177 	/*
178 	 * vm_flags is protected by the mmap_lock held in write mode.
179 	 */
180 	vma->vm_flags = new_flags;
181 	if (!vma->vm_file) {
182 		error = replace_anon_vma_name(vma, anon_name);
183 		if (error)
184 			return error;
185 	}
186 
187 	return 0;
188 }
189 
190 #ifdef CONFIG_SWAP
swapin_walk_pmd_entry(pmd_t * pmd,unsigned long start,unsigned long end,struct mm_walk * walk)191 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
192 	unsigned long end, struct mm_walk *walk)
193 {
194 	pte_t *orig_pte;
195 	struct vm_area_struct *vma = walk->private;
196 	unsigned long index;
197 
198 	if (pmd_none_or_trans_huge_or_clear_bad(pmd))
199 		return 0;
200 
201 	for (index = start; index != end; index += PAGE_SIZE) {
202 		pte_t pte;
203 		swp_entry_t entry;
204 		struct page *page;
205 		spinlock_t *ptl;
206 
207 		orig_pte = pte_offset_map_lock(vma->vm_mm, pmd, start, &ptl);
208 		pte = *(orig_pte + ((index - start) / PAGE_SIZE));
209 		pte_unmap_unlock(orig_pte, ptl);
210 
211 		if (pte_present(pte) || pte_none(pte))
212 			continue;
213 		entry = pte_to_swp_entry(pte);
214 		if (unlikely(non_swap_entry(entry)))
215 			continue;
216 
217 		page = read_swap_cache_async(entry, GFP_HIGHUSER_MOVABLE,
218 							vma, index, false);
219 		if (page)
220 			put_page(page);
221 	}
222 
223 	return 0;
224 }
225 
226 static const struct mm_walk_ops swapin_walk_ops = {
227 	.pmd_entry		= swapin_walk_pmd_entry,
228 };
229 
force_shm_swapin_readahead(struct vm_area_struct * vma,unsigned long start,unsigned long end,struct address_space * mapping)230 static void force_shm_swapin_readahead(struct vm_area_struct *vma,
231 		unsigned long start, unsigned long end,
232 		struct address_space *mapping)
233 {
234 	XA_STATE(xas, &mapping->i_pages, linear_page_index(vma, start));
235 	pgoff_t end_index = linear_page_index(vma, end + PAGE_SIZE - 1);
236 	struct page *page;
237 
238 	rcu_read_lock();
239 	xas_for_each(&xas, page, end_index) {
240 		swp_entry_t swap;
241 
242 		if (!xa_is_value(page))
243 			continue;
244 		xas_pause(&xas);
245 		rcu_read_unlock();
246 
247 		swap = radix_to_swp_entry(page);
248 		page = read_swap_cache_async(swap, GFP_HIGHUSER_MOVABLE,
249 							NULL, 0, false);
250 		if (page)
251 			put_page(page);
252 
253 		rcu_read_lock();
254 	}
255 	rcu_read_unlock();
256 
257 	lru_add_drain();	/* Push any new pages onto the LRU now */
258 }
259 #endif		/* CONFIG_SWAP */
260 
261 /*
262  * Schedule all required I/O operations.  Do not wait for completion.
263  */
madvise_willneed(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end)264 static long madvise_willneed(struct vm_area_struct *vma,
265 			     struct vm_area_struct **prev,
266 			     unsigned long start, unsigned long end)
267 {
268 	struct mm_struct *mm = vma->vm_mm;
269 	struct file *file = vma->vm_file;
270 	loff_t offset;
271 
272 	*prev = vma;
273 #ifdef CONFIG_SWAP
274 	if (!file) {
275 		walk_page_range(vma->vm_mm, start, end, &swapin_walk_ops, vma);
276 		lru_add_drain(); /* Push any new pages onto the LRU now */
277 		return 0;
278 	}
279 
280 	if (shmem_mapping(file->f_mapping)) {
281 		force_shm_swapin_readahead(vma, start, end,
282 					file->f_mapping);
283 		return 0;
284 	}
285 #else
286 	if (!file)
287 		return -EBADF;
288 #endif
289 
290 	if (IS_DAX(file_inode(file))) {
291 		/* no bad return value, but ignore advice */
292 		return 0;
293 	}
294 
295 	/*
296 	 * Filesystem's fadvise may need to take various locks.  We need to
297 	 * explicitly grab a reference because the vma (and hence the
298 	 * vma's reference to the file) can go away as soon as we drop
299 	 * mmap_lock.
300 	 */
301 	*prev = NULL;	/* tell sys_madvise we drop mmap_lock */
302 	get_file(file);
303 	offset = (loff_t)(start - vma->vm_start)
304 			+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
305 	mmap_read_unlock(mm);
306 	vfs_fadvise(file, offset, end - start, POSIX_FADV_WILLNEED);
307 	fput(file);
308 	mmap_read_lock(mm);
309 	return 0;
310 }
311 
madvise_cold_or_pageout_pte_range(pmd_t * pmd,unsigned long addr,unsigned long end,struct mm_walk * walk)312 static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
313 				unsigned long addr, unsigned long end,
314 				struct mm_walk *walk)
315 {
316 	struct madvise_walk_private *private = walk->private;
317 	struct mmu_gather *tlb = private->tlb;
318 	bool pageout = private->pageout;
319 	struct mm_struct *mm = tlb->mm;
320 	struct vm_area_struct *vma = walk->vma;
321 	pte_t *orig_pte, *pte, ptent;
322 	spinlock_t *ptl;
323 	struct page *page = NULL;
324 	LIST_HEAD(page_list);
325 
326 	if (fatal_signal_pending(current))
327 		return -EINTR;
328 
329 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
330 	if (pmd_trans_huge(*pmd)) {
331 		pmd_t orig_pmd;
332 		unsigned long next = pmd_addr_end(addr, end);
333 
334 		tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
335 		ptl = pmd_trans_huge_lock(pmd, vma);
336 		if (!ptl)
337 			return 0;
338 
339 		orig_pmd = *pmd;
340 		if (is_huge_zero_pmd(orig_pmd))
341 			goto huge_unlock;
342 
343 		if (unlikely(!pmd_present(orig_pmd))) {
344 			VM_BUG_ON(thp_migration_supported() &&
345 					!is_pmd_migration_entry(orig_pmd));
346 			goto huge_unlock;
347 		}
348 
349 		page = pmd_page(orig_pmd);
350 
351 		/* Do not interfere with other mappings of this page */
352 		if (page_mapcount(page) != 1)
353 			goto huge_unlock;
354 
355 		if (next - addr != HPAGE_PMD_SIZE) {
356 			int err;
357 
358 			get_page(page);
359 			spin_unlock(ptl);
360 			lock_page(page);
361 			err = split_huge_page(page);
362 			unlock_page(page);
363 			put_page(page);
364 			if (!err)
365 				goto regular_page;
366 			return 0;
367 		}
368 
369 		if (pmd_young(orig_pmd)) {
370 			pmdp_invalidate(vma, addr, pmd);
371 			orig_pmd = pmd_mkold(orig_pmd);
372 
373 			set_pmd_at(mm, addr, pmd, orig_pmd);
374 			tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
375 		}
376 
377 		ClearPageReferenced(page);
378 		test_and_clear_page_young(page);
379 		if (pageout) {
380 			if (!isolate_lru_page(page)) {
381 				if (PageUnevictable(page))
382 					putback_lru_page(page);
383 				else
384 					list_add(&page->lru, &page_list);
385 			}
386 		} else
387 			deactivate_page(page);
388 huge_unlock:
389 		spin_unlock(ptl);
390 		if (pageout)
391 			reclaim_pages(&page_list);
392 		return 0;
393 	}
394 
395 regular_page:
396 	if (pmd_trans_unstable(pmd))
397 		return 0;
398 #endif
399 	tlb_change_page_size(tlb, PAGE_SIZE);
400 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
401 	flush_tlb_batched_pending(mm);
402 	arch_enter_lazy_mmu_mode();
403 	for (; addr < end; pte++, addr += PAGE_SIZE) {
404 		ptent = *pte;
405 
406 		if (pte_none(ptent))
407 			continue;
408 
409 		if (!pte_present(ptent))
410 			continue;
411 
412 		page = vm_normal_page(vma, addr, ptent);
413 		if (!page)
414 			continue;
415 
416 		/*
417 		 * Creating a THP page is expensive so split it only if we
418 		 * are sure it's worth. Split it if we are only owner.
419 		 */
420 		if (PageTransCompound(page)) {
421 			if (page_mapcount(page) != 1)
422 				break;
423 			get_page(page);
424 			if (!trylock_page(page)) {
425 				put_page(page);
426 				break;
427 			}
428 			pte_unmap_unlock(orig_pte, ptl);
429 			if (split_huge_page(page)) {
430 				unlock_page(page);
431 				put_page(page);
432 				pte_offset_map_lock(mm, pmd, addr, &ptl);
433 				break;
434 			}
435 			unlock_page(page);
436 			put_page(page);
437 			pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
438 			pte--;
439 			addr -= PAGE_SIZE;
440 			continue;
441 		}
442 
443 		/* Do not interfere with other mappings of this page */
444 		if (page_mapcount(page) != 1)
445 			continue;
446 
447 		VM_BUG_ON_PAGE(PageTransCompound(page), page);
448 
449 		if (pte_young(ptent)) {
450 			ptent = ptep_get_and_clear_full(mm, addr, pte,
451 							tlb->fullmm);
452 			ptent = pte_mkold(ptent);
453 			set_pte_at(mm, addr, pte, ptent);
454 			tlb_remove_tlb_entry(tlb, pte, addr);
455 		}
456 
457 		/*
458 		 * We are deactivating a page for accelerating reclaiming.
459 		 * VM couldn't reclaim the page unless we clear PG_young.
460 		 * As a side effect, it makes confuse idle-page tracking
461 		 * because they will miss recent referenced history.
462 		 */
463 		ClearPageReferenced(page);
464 		test_and_clear_page_young(page);
465 		if (pageout) {
466 			if (!isolate_lru_page(page)) {
467 				if (PageUnevictable(page))
468 					putback_lru_page(page);
469 				else
470 					list_add(&page->lru, &page_list);
471 			}
472 		} else
473 			deactivate_page(page);
474 	}
475 
476 	arch_leave_lazy_mmu_mode();
477 	pte_unmap_unlock(orig_pte, ptl);
478 	if (pageout)
479 		reclaim_pages(&page_list);
480 	cond_resched();
481 
482 	return 0;
483 }
484 
485 static const struct mm_walk_ops cold_walk_ops = {
486 	.pmd_entry = madvise_cold_or_pageout_pte_range,
487 };
488 
madvise_cold_page_range(struct mmu_gather * tlb,struct vm_area_struct * vma,unsigned long addr,unsigned long end)489 static void madvise_cold_page_range(struct mmu_gather *tlb,
490 			     struct vm_area_struct *vma,
491 			     unsigned long addr, unsigned long end)
492 {
493 	struct madvise_walk_private walk_private = {
494 		.pageout = false,
495 		.tlb = tlb,
496 	};
497 
498 	tlb_start_vma(tlb, vma);
499 	walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
500 	tlb_end_vma(tlb, vma);
501 }
502 
madvise_cold(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start_addr,unsigned long end_addr)503 static long madvise_cold(struct vm_area_struct *vma,
504 			struct vm_area_struct **prev,
505 			unsigned long start_addr, unsigned long end_addr)
506 {
507 	struct mm_struct *mm = vma->vm_mm;
508 	struct mmu_gather tlb;
509 
510 	*prev = vma;
511 	if (!can_madv_lru_vma(vma))
512 		return -EINVAL;
513 
514 	lru_add_drain();
515 	tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
516 	madvise_cold_page_range(&tlb, vma, start_addr, end_addr);
517 	tlb_finish_mmu(&tlb, start_addr, end_addr);
518 
519 	return 0;
520 }
521 
madvise_pageout_page_range(struct mmu_gather * tlb,struct vm_area_struct * vma,unsigned long addr,unsigned long end)522 static void madvise_pageout_page_range(struct mmu_gather *tlb,
523 			     struct vm_area_struct *vma,
524 			     unsigned long addr, unsigned long end)
525 {
526 	struct madvise_walk_private walk_private = {
527 		.pageout = true,
528 		.tlb = tlb,
529 	};
530 
531 	tlb_start_vma(tlb, vma);
532 	walk_page_range(vma->vm_mm, addr, end, &cold_walk_ops, &walk_private);
533 	tlb_end_vma(tlb, vma);
534 }
535 
can_do_pageout(struct vm_area_struct * vma)536 static inline bool can_do_pageout(struct vm_area_struct *vma)
537 {
538 	if (vma_is_anonymous(vma))
539 		return true;
540 	if (!vma->vm_file)
541 		return false;
542 	/*
543 	 * paging out pagecache only for non-anonymous mappings that correspond
544 	 * to the files the calling process could (if tried) open for writing;
545 	 * otherwise we'd be including shared non-exclusive mappings, which
546 	 * opens a side channel.
547 	 */
548 	return inode_owner_or_capable(file_inode(vma->vm_file)) ||
549 		inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0;
550 }
551 
madvise_pageout(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start_addr,unsigned long end_addr)552 static long madvise_pageout(struct vm_area_struct *vma,
553 			struct vm_area_struct **prev,
554 			unsigned long start_addr, unsigned long end_addr)
555 {
556 	struct mm_struct *mm = vma->vm_mm;
557 	struct mmu_gather tlb;
558 
559 	*prev = vma;
560 	if (!can_madv_lru_vma(vma))
561 		return -EINVAL;
562 
563 	if (!can_do_pageout(vma))
564 		return 0;
565 
566 	lru_add_drain();
567 	tlb_gather_mmu(&tlb, mm, start_addr, end_addr);
568 	madvise_pageout_page_range(&tlb, vma, start_addr, end_addr);
569 	tlb_finish_mmu(&tlb, start_addr, end_addr);
570 
571 	return 0;
572 }
573 
madvise_free_pte_range(pmd_t * pmd,unsigned long addr,unsigned long end,struct mm_walk * walk)574 static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
575 				unsigned long end, struct mm_walk *walk)
576 
577 {
578 	struct mmu_gather *tlb = walk->private;
579 	struct mm_struct *mm = tlb->mm;
580 	struct vm_area_struct *vma = walk->vma;
581 	spinlock_t *ptl;
582 	pte_t *orig_pte, *pte, ptent;
583 	struct page *page;
584 	int nr_swap = 0;
585 	unsigned long next;
586 
587 	next = pmd_addr_end(addr, end);
588 	if (pmd_trans_huge(*pmd))
589 		if (madvise_free_huge_pmd(tlb, vma, pmd, addr, next))
590 			goto next;
591 
592 	if (pmd_trans_unstable(pmd))
593 		return 0;
594 
595 	tlb_change_page_size(tlb, PAGE_SIZE);
596 	orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
597 	flush_tlb_batched_pending(mm);
598 	arch_enter_lazy_mmu_mode();
599 	for (; addr != end; pte++, addr += PAGE_SIZE) {
600 		ptent = *pte;
601 
602 		if (pte_none(ptent))
603 			continue;
604 		/*
605 		 * If the pte has swp_entry, just clear page table to
606 		 * prevent swap-in which is more expensive rather than
607 		 * (page allocation + zeroing).
608 		 */
609 		if (!pte_present(ptent)) {
610 			swp_entry_t entry;
611 
612 			entry = pte_to_swp_entry(ptent);
613 			if (non_swap_entry(entry))
614 				continue;
615 			nr_swap--;
616 			free_swap_and_cache(entry);
617 			pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
618 			continue;
619 		}
620 
621 		page = vm_normal_page(vma, addr, ptent);
622 		if (!page)
623 			continue;
624 
625 		/*
626 		 * If pmd isn't transhuge but the page is THP and
627 		 * is owned by only this process, split it and
628 		 * deactivate all pages.
629 		 */
630 		if (PageTransCompound(page)) {
631 			if (page_mapcount(page) != 1)
632 				goto out;
633 			get_page(page);
634 			if (!trylock_page(page)) {
635 				put_page(page);
636 				goto out;
637 			}
638 			pte_unmap_unlock(orig_pte, ptl);
639 			if (split_huge_page(page)) {
640 				unlock_page(page);
641 				put_page(page);
642 				pte_offset_map_lock(mm, pmd, addr, &ptl);
643 				goto out;
644 			}
645 			unlock_page(page);
646 			put_page(page);
647 			pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
648 			pte--;
649 			addr -= PAGE_SIZE;
650 			continue;
651 		}
652 
653 		VM_BUG_ON_PAGE(PageTransCompound(page), page);
654 
655 		if (PageSwapCache(page) || PageDirty(page)) {
656 			if (!trylock_page(page))
657 				continue;
658 			/*
659 			 * If page is shared with others, we couldn't clear
660 			 * PG_dirty of the page.
661 			 */
662 			if (page_mapcount(page) != 1) {
663 				unlock_page(page);
664 				continue;
665 			}
666 
667 			if (PageSwapCache(page) && !try_to_free_swap(page)) {
668 				unlock_page(page);
669 				continue;
670 			}
671 
672 			ClearPageDirty(page);
673 			unlock_page(page);
674 		}
675 
676 		if (pte_young(ptent) || pte_dirty(ptent)) {
677 			/*
678 			 * Some of architecture(ex, PPC) don't update TLB
679 			 * with set_pte_at and tlb_remove_tlb_entry so for
680 			 * the portability, remap the pte with old|clean
681 			 * after pte clearing.
682 			 */
683 			ptent = ptep_get_and_clear_full(mm, addr, pte,
684 							tlb->fullmm);
685 
686 			ptent = pte_mkold(ptent);
687 			ptent = pte_mkclean(ptent);
688 			set_pte_at(mm, addr, pte, ptent);
689 			tlb_remove_tlb_entry(tlb, pte, addr);
690 		}
691 		mark_page_lazyfree(page);
692 	}
693 out:
694 	if (nr_swap) {
695 		if (current->mm == mm)
696 			sync_mm_rss(mm);
697 
698 		add_mm_counter(mm, MM_SWAPENTS, nr_swap);
699 	}
700 	arch_leave_lazy_mmu_mode();
701 	pte_unmap_unlock(orig_pte, ptl);
702 	cond_resched();
703 next:
704 	return 0;
705 }
706 
707 static const struct mm_walk_ops madvise_free_walk_ops = {
708 	.pmd_entry		= madvise_free_pte_range,
709 };
710 
madvise_free_single_vma(struct vm_area_struct * vma,unsigned long start_addr,unsigned long end_addr)711 static int madvise_free_single_vma(struct vm_area_struct *vma,
712 			unsigned long start_addr, unsigned long end_addr)
713 {
714 	struct mm_struct *mm = vma->vm_mm;
715 	struct mmu_notifier_range range;
716 	struct mmu_gather tlb;
717 
718 	/* MADV_FREE works for only anon vma at the moment */
719 	if (!vma_is_anonymous(vma))
720 		return -EINVAL;
721 
722 	range.start = max(vma->vm_start, start_addr);
723 	if (range.start >= vma->vm_end)
724 		return -EINVAL;
725 	range.end = min(vma->vm_end, end_addr);
726 	if (range.end <= vma->vm_start)
727 		return -EINVAL;
728 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
729 				range.start, range.end);
730 
731 	lru_add_drain();
732 	tlb_gather_mmu(&tlb, mm, range.start, range.end);
733 	update_hiwater_rss(mm);
734 
735 	mmu_notifier_invalidate_range_start(&range);
736 	tlb_start_vma(&tlb, vma);
737 	walk_page_range(vma->vm_mm, range.start, range.end,
738 			&madvise_free_walk_ops, &tlb);
739 	tlb_end_vma(&tlb, vma);
740 	mmu_notifier_invalidate_range_end(&range);
741 	tlb_finish_mmu(&tlb, range.start, range.end);
742 
743 	return 0;
744 }
745 
746 /*
747  * Application no longer needs these pages.  If the pages are dirty,
748  * it's OK to just throw them away.  The app will be more careful about
749  * data it wants to keep.  Be sure to free swap resources too.  The
750  * zap_page_range call sets things up for shrink_active_list to actually free
751  * these pages later if no one else has touched them in the meantime,
752  * although we could add these pages to a global reuse list for
753  * shrink_active_list to pick up before reclaiming other pages.
754  *
755  * NB: This interface discards data rather than pushes it out to swap,
756  * as some implementations do.  This has performance implications for
757  * applications like large transactional databases which want to discard
758  * pages in anonymous maps after committing to backing store the data
759  * that was kept in them.  There is no reason to write this data out to
760  * the swap area if the application is discarding it.
761  *
762  * An interface that causes the system to free clean pages and flush
763  * dirty pages is already available as msync(MS_INVALIDATE).
764  */
madvise_dontneed_single_vma(struct vm_area_struct * vma,unsigned long start,unsigned long end)765 static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
766 					unsigned long start, unsigned long end)
767 {
768 	zap_page_range(vma, start, end - start);
769 	return 0;
770 }
771 
madvise_dontneed_free(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end,int behavior)772 static long madvise_dontneed_free(struct vm_area_struct *vma,
773 				  struct vm_area_struct **prev,
774 				  unsigned long start, unsigned long end,
775 				  int behavior)
776 {
777 	struct mm_struct *mm = vma->vm_mm;
778 
779 	*prev = vma;
780 	if (!can_madv_lru_vma(vma))
781 		return -EINVAL;
782 
783 	if (!userfaultfd_remove(vma, start, end)) {
784 		*prev = NULL; /* mmap_lock has been dropped, prev is stale */
785 
786 		mmap_read_lock(mm);
787 		vma = find_vma(mm, start);
788 		if (!vma)
789 			return -ENOMEM;
790 		if (start < vma->vm_start) {
791 			/*
792 			 * This "vma" under revalidation is the one
793 			 * with the lowest vma->vm_start where start
794 			 * is also < vma->vm_end. If start <
795 			 * vma->vm_start it means an hole materialized
796 			 * in the user address space within the
797 			 * virtual range passed to MADV_DONTNEED
798 			 * or MADV_FREE.
799 			 */
800 			return -ENOMEM;
801 		}
802 		if (!can_madv_lru_vma(vma))
803 			return -EINVAL;
804 		if (end > vma->vm_end) {
805 			/*
806 			 * Don't fail if end > vma->vm_end. If the old
807 			 * vma was splitted while the mmap_lock was
808 			 * released the effect of the concurrent
809 			 * operation may not cause madvise() to
810 			 * have an undefined result. There may be an
811 			 * adjacent next vma that we'll walk
812 			 * next. userfaultfd_remove() will generate an
813 			 * UFFD_EVENT_REMOVE repetition on the
814 			 * end-vma->vm_end range, but the manager can
815 			 * handle a repetition fine.
816 			 */
817 			end = vma->vm_end;
818 		}
819 		VM_WARN_ON(start >= end);
820 	}
821 
822 	if (behavior == MADV_DONTNEED)
823 		return madvise_dontneed_single_vma(vma, start, end);
824 	else if (behavior == MADV_FREE)
825 		return madvise_free_single_vma(vma, start, end);
826 	else
827 		return -EINVAL;
828 }
829 
830 /*
831  * Application wants to free up the pages and associated backing store.
832  * This is effectively punching a hole into the middle of a file.
833  */
madvise_remove(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end)834 static long madvise_remove(struct vm_area_struct *vma,
835 				struct vm_area_struct **prev,
836 				unsigned long start, unsigned long end)
837 {
838 	loff_t offset;
839 	int error;
840 	struct file *f;
841 	struct mm_struct *mm = vma->vm_mm;
842 
843 	*prev = NULL;	/* tell sys_madvise we drop mmap_lock */
844 
845 	if (vma->vm_flags & VM_LOCKED)
846 		return -EINVAL;
847 
848 	f = vma->vm_file;
849 
850 	if (!f || !f->f_mapping || !f->f_mapping->host) {
851 			return -EINVAL;
852 	}
853 
854 	if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
855 		return -EACCES;
856 
857 	offset = (loff_t)(start - vma->vm_start)
858 			+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
859 
860 	/*
861 	 * Filesystem's fallocate may need to take i_mutex.  We need to
862 	 * explicitly grab a reference because the vma (and hence the
863 	 * vma's reference to the file) can go away as soon as we drop
864 	 * mmap_lock.
865 	 */
866 	get_file(f);
867 	if (userfaultfd_remove(vma, start, end)) {
868 		/* mmap_lock was not released by userfaultfd_remove() */
869 		mmap_read_unlock(mm);
870 	}
871 	error = vfs_fallocate(f,
872 				FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
873 				offset, end - start);
874 	fput(f);
875 	mmap_read_lock(mm);
876 	return error;
877 }
878 
879 /*
880  * Apply an madvise behavior to a region of a vma.  madvise_update_vma
881  * will handle splitting a vm area into separate areas, each area with its own
882  * behavior.
883  */
madvise_vma_behavior(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end,unsigned long behavior)884 static int madvise_vma_behavior(struct vm_area_struct *vma,
885 				struct vm_area_struct **prev,
886 				unsigned long start, unsigned long end,
887 				unsigned long behavior)
888 {
889 	int error;
890 	struct anon_vma_name *anon_name;
891 	unsigned long new_flags = vma->vm_flags;
892 
893 	switch (behavior) {
894 	case MADV_REMOVE:
895 		return madvise_remove(vma, prev, start, end);
896 	case MADV_WILLNEED:
897 		return madvise_willneed(vma, prev, start, end);
898 	case MADV_COLD:
899 		return madvise_cold(vma, prev, start, end);
900 	case MADV_PAGEOUT:
901 		return madvise_pageout(vma, prev, start, end);
902 	case MADV_FREE:
903 	case MADV_DONTNEED:
904 		return madvise_dontneed_free(vma, prev, start, end, behavior);
905 	case MADV_NORMAL:
906 		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
907 		break;
908 	case MADV_SEQUENTIAL:
909 		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
910 		break;
911 	case MADV_RANDOM:
912 		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
913 		break;
914 	case MADV_DONTFORK:
915 		new_flags |= VM_DONTCOPY;
916 		break;
917 	case MADV_DOFORK:
918 		if (vma->vm_flags & VM_IO)
919 			return -EINVAL;
920 		new_flags &= ~VM_DONTCOPY;
921 		break;
922 	case MADV_WIPEONFORK:
923 		/* MADV_WIPEONFORK is only supported on anonymous memory. */
924 		if (vma->vm_file || vma->vm_flags & VM_SHARED)
925 			return -EINVAL;
926 		new_flags |= VM_WIPEONFORK;
927 		break;
928 	case MADV_KEEPONFORK:
929 		new_flags &= ~VM_WIPEONFORK;
930 		break;
931 	case MADV_DONTDUMP:
932 		new_flags |= VM_DONTDUMP;
933 		break;
934 	case MADV_DODUMP:
935 		if (!is_vm_hugetlb_page(vma) && new_flags & VM_SPECIAL)
936 			return -EINVAL;
937 		new_flags &= ~VM_DONTDUMP;
938 		break;
939 	case MADV_MERGEABLE:
940 	case MADV_UNMERGEABLE:
941 		error = ksm_madvise(vma, start, end, behavior, &new_flags);
942 		if (error)
943 			goto out;
944 		break;
945 	case MADV_HUGEPAGE:
946 	case MADV_NOHUGEPAGE:
947 		error = hugepage_madvise(vma, &new_flags, behavior);
948 		if (error)
949 			goto out;
950 		break;
951 	}
952 
953 	anon_name = anon_vma_name(vma);
954 	anon_vma_name_get(anon_name);
955 	error = madvise_update_vma(vma, prev, start, end, new_flags,
956 				   anon_name);
957 	anon_vma_name_put(anon_name);
958 
959 out:
960 	/*
961 	 * madvise() returns EAGAIN if kernel resources, such as
962 	 * slab, are temporarily unavailable.
963 	 */
964 	if (error == -ENOMEM)
965 		error = -EAGAIN;
966 	return error;
967 }
968 
969 #ifdef CONFIG_MEMORY_FAILURE
970 /*
971  * Error injection support for memory error handling.
972  */
madvise_inject_error(int behavior,unsigned long start,unsigned long end)973 static int madvise_inject_error(int behavior,
974 		unsigned long start, unsigned long end)
975 {
976 	struct zone *zone;
977 	unsigned long size;
978 
979 	if (!capable(CAP_SYS_ADMIN))
980 		return -EPERM;
981 
982 
983 	for (; start < end; start += size) {
984 		unsigned long pfn;
985 		struct page *page;
986 		int ret;
987 
988 		ret = get_user_pages_fast(start, 1, 0, &page);
989 		if (ret != 1)
990 			return ret;
991 		pfn = page_to_pfn(page);
992 
993 		/*
994 		 * When soft offlining hugepages, after migrating the page
995 		 * we dissolve it, therefore in the second loop "page" will
996 		 * no longer be a compound page.
997 		 */
998 		size = page_size(compound_head(page));
999 
1000 		if (behavior == MADV_SOFT_OFFLINE) {
1001 			pr_info("Soft offlining pfn %#lx at process virtual address %#lx\n",
1002 				 pfn, start);
1003 			ret = soft_offline_page(pfn, MF_COUNT_INCREASED);
1004 		} else {
1005 			pr_info("Injecting memory failure for pfn %#lx at process virtual address %#lx\n",
1006 				 pfn, start);
1007 			ret = memory_failure(pfn, MF_COUNT_INCREASED);
1008 		}
1009 
1010 		if (ret)
1011 			return ret;
1012 	}
1013 
1014 	/* Ensure that all poisoned pages are removed from per-cpu lists */
1015 	for_each_populated_zone(zone)
1016 		drain_all_pages(zone);
1017 
1018 	return 0;
1019 }
1020 #endif
1021 
1022 static bool
madvise_behavior_valid(int behavior)1023 madvise_behavior_valid(int behavior)
1024 {
1025 	switch (behavior) {
1026 	case MADV_DOFORK:
1027 	case MADV_DONTFORK:
1028 	case MADV_NORMAL:
1029 	case MADV_SEQUENTIAL:
1030 	case MADV_RANDOM:
1031 	case MADV_REMOVE:
1032 	case MADV_WILLNEED:
1033 	case MADV_DONTNEED:
1034 	case MADV_FREE:
1035 	case MADV_COLD:
1036 	case MADV_PAGEOUT:
1037 #ifdef CONFIG_KSM
1038 	case MADV_MERGEABLE:
1039 	case MADV_UNMERGEABLE:
1040 #endif
1041 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1042 	case MADV_HUGEPAGE:
1043 	case MADV_NOHUGEPAGE:
1044 #endif
1045 	case MADV_DONTDUMP:
1046 	case MADV_DODUMP:
1047 	case MADV_WIPEONFORK:
1048 	case MADV_KEEPONFORK:
1049 #ifdef CONFIG_MEMORY_FAILURE
1050 	case MADV_SOFT_OFFLINE:
1051 	case MADV_HWPOISON:
1052 #endif
1053 		return true;
1054 
1055 	default:
1056 		return false;
1057 	}
1058 }
1059 
1060 static bool
process_madvise_behavior_valid(int behavior)1061 process_madvise_behavior_valid(int behavior)
1062 {
1063 	switch (behavior) {
1064 	case MADV_COLD:
1065 	case MADV_PAGEOUT:
1066 		return true;
1067 	default:
1068 		return false;
1069 	}
1070 }
1071 
1072 /*
1073  * Walk the vmas in range [start,end), and call the visit function on each one.
1074  * The visit function will get start and end parameters that cover the overlap
1075  * between the current vma and the original range.  Any unmapped regions in the
1076  * original range will result in this function returning -ENOMEM while still
1077  * calling the visit function on all of the existing vmas in the range.
1078  * Must be called with the mmap_lock held for reading or writing.
1079  */
1080 static
madvise_walk_vmas(struct mm_struct * mm,unsigned long start,unsigned long end,unsigned long arg,int (* visit)(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end,unsigned long arg))1081 int madvise_walk_vmas(struct mm_struct *mm, unsigned long start,
1082 		      unsigned long end, unsigned long arg,
1083 		      int (*visit)(struct vm_area_struct *vma,
1084 				   struct vm_area_struct **prev, unsigned long start,
1085 				   unsigned long end, unsigned long arg))
1086 {
1087 	struct vm_area_struct *vma;
1088 	struct vm_area_struct *prev;
1089 	unsigned long tmp;
1090 	int unmapped_error = 0;
1091 
1092 	/*
1093 	 * If the interval [start,end) covers some unmapped address
1094 	 * ranges, just ignore them, but return -ENOMEM at the end.
1095 	 * - different from the way of handling in mlock etc.
1096 	 */
1097 	vma = find_vma_prev(mm, start, &prev);
1098 	if (vma && start > vma->vm_start)
1099 		prev = vma;
1100 
1101 	for (;;) {
1102 		int error;
1103 
1104 		/* Still start < end. */
1105 		if (!vma)
1106 			return -ENOMEM;
1107 
1108 		/* Here start < (end|vma->vm_end). */
1109 		if (start < vma->vm_start) {
1110 			unmapped_error = -ENOMEM;
1111 			start = vma->vm_start;
1112 			if (start >= end)
1113 				break;
1114 		}
1115 
1116 		/* Here vma->vm_start <= start < (end|vma->vm_end) */
1117 		tmp = vma->vm_end;
1118 		if (end < tmp)
1119 			tmp = end;
1120 
1121 		/* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */
1122 		error = visit(vma, &prev, start, tmp, arg);
1123 		if (error)
1124 			return error;
1125 		start = tmp;
1126 		if (prev && start < prev->vm_end)
1127 			start = prev->vm_end;
1128 		if (start >= end)
1129 			break;
1130 		if (prev)
1131 			vma = prev->vm_next;
1132 		else	/* madvise_remove dropped mmap_lock */
1133 			vma = find_vma(mm, start);
1134 	}
1135 
1136 	return unmapped_error;
1137 }
1138 
1139 #ifdef CONFIG_ANON_VMA_NAME
madvise_vma_anon_name(struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end,unsigned long anon_name)1140 static int madvise_vma_anon_name(struct vm_area_struct *vma,
1141 				 struct vm_area_struct **prev,
1142 				 unsigned long start, unsigned long end,
1143 				 unsigned long anon_name)
1144 {
1145 	int error;
1146 
1147 	/* Only anonymous mappings can be named */
1148 	if (vma->vm_file)
1149 		return -EBADF;
1150 
1151 	error = madvise_update_vma(vma, prev, start, end, vma->vm_flags,
1152 				   (struct anon_vma_name *)anon_name);
1153 
1154 	/*
1155 	 * madvise() returns EAGAIN if kernel resources, such as
1156 	 * slab, are temporarily unavailable.
1157 	 */
1158 	if (error == -ENOMEM)
1159 		error = -EAGAIN;
1160 	return error;
1161 }
1162 
madvise_set_anon_name(struct mm_struct * mm,unsigned long start,unsigned long len_in,struct anon_vma_name * anon_name)1163 int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
1164 			  unsigned long len_in, struct anon_vma_name *anon_name)
1165 {
1166 	unsigned long end;
1167 	unsigned long len;
1168 
1169 	if (start & ~PAGE_MASK)
1170 		return -EINVAL;
1171 	len = (len_in + ~PAGE_MASK) & PAGE_MASK;
1172 
1173 	/* Check to see whether len was rounded up from small -ve to zero */
1174 	if (len_in && !len)
1175 		return -EINVAL;
1176 
1177 	end = start + len;
1178 	if (end < start)
1179 		return -EINVAL;
1180 
1181 	if (end == start)
1182 		return 0;
1183 
1184 	return madvise_walk_vmas(mm, start, end, (unsigned long)anon_name,
1185 				 madvise_vma_anon_name);
1186 }
1187 #endif /* CONFIG_ANON_VMA_NAME */
1188 /*
1189  * The madvise(2) system call.
1190  *
1191  * Applications can use madvise() to advise the kernel how it should
1192  * handle paging I/O in this VM area.  The idea is to help the kernel
1193  * use appropriate read-ahead and caching techniques.  The information
1194  * provided is advisory only, and can be safely disregarded by the
1195  * kernel without affecting the correct operation of the application.
1196  *
1197  * behavior values:
1198  *  MADV_NORMAL - the default behavior is to read clusters.  This
1199  *		results in some read-ahead and read-behind.
1200  *  MADV_RANDOM - the system should read the minimum amount of data
1201  *		on any access, since it is unlikely that the appli-
1202  *		cation will need more than what it asks for.
1203  *  MADV_SEQUENTIAL - pages in the given range will probably be accessed
1204  *		once, so they can be aggressively read ahead, and
1205  *		can be freed soon after they are accessed.
1206  *  MADV_WILLNEED - the application is notifying the system to read
1207  *		some pages ahead.
1208  *  MADV_DONTNEED - the application is finished with the given range,
1209  *		so the kernel can free resources associated with it.
1210  *  MADV_FREE - the application marks pages in the given range as lazy free,
1211  *		where actual purges are postponed until memory pressure happens.
1212  *  MADV_REMOVE - the application wants to free up the given range of
1213  *		pages and associated backing store.
1214  *  MADV_DONTFORK - omit this area from child's address space when forking:
1215  *		typically, to avoid COWing pages pinned by get_user_pages().
1216  *  MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking.
1217  *  MADV_WIPEONFORK - present the child process with zero-filled memory in this
1218  *              range after a fork.
1219  *  MADV_KEEPONFORK - undo the effect of MADV_WIPEONFORK
1220  *  MADV_HWPOISON - trigger memory error handler as if the given memory range
1221  *		were corrupted by unrecoverable hardware memory failure.
1222  *  MADV_SOFT_OFFLINE - try to soft-offline the given range of memory.
1223  *  MADV_MERGEABLE - the application recommends that KSM try to merge pages in
1224  *		this area with pages of identical content from other such areas.
1225  *  MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others.
1226  *  MADV_HUGEPAGE - the application wants to back the given range by transparent
1227  *		huge pages in the future. Existing pages might be coalesced and
1228  *		new pages might be allocated as THP.
1229  *  MADV_NOHUGEPAGE - mark the given range as not worth being backed by
1230  *		transparent huge pages so the existing pages will not be
1231  *		coalesced into THP and new pages will not be allocated as THP.
1232  *  MADV_DONTDUMP - the application wants to prevent pages in the given range
1233  *		from being included in its core dump.
1234  *  MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump.
1235  *  MADV_COLD - the application is not expected to use this memory soon,
1236  *		deactivate pages in this range so that they can be reclaimed
1237  *		easily if memory pressure hanppens.
1238  *  MADV_PAGEOUT - the application is not expected to use this memory soon,
1239  *		page out the pages in this range immediately.
1240  *
1241  * return values:
1242  *  zero    - success
1243  *  -EINVAL - start + len < 0, start is not page-aligned,
1244  *		"behavior" is not a valid value, or application
1245  *		is attempting to release locked or shared pages,
1246  *		or the specified address range includes file, Huge TLB,
1247  *		MAP_SHARED or VMPFNMAP range.
1248  *  -ENOMEM - addresses in the specified range are not currently
1249  *		mapped, or are outside the AS of the process.
1250  *  -EIO    - an I/O error occurred while paging in data.
1251  *  -EBADF  - map exists, but area maps something that isn't a file.
1252  *  -EAGAIN - a kernel resource was temporarily unavailable.
1253  */
do_madvise(struct mm_struct * mm,unsigned long start,size_t len_in,int behavior)1254 int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior)
1255 {
1256 	unsigned long end;
1257 	int error;
1258 	int write;
1259 	size_t len;
1260 	struct blk_plug plug;
1261 
1262 	start = untagged_addr(start);
1263 
1264 	if (!madvise_behavior_valid(behavior))
1265 		return -EINVAL;
1266 
1267 	if (!PAGE_ALIGNED(start))
1268 		return -EINVAL;
1269 	len = PAGE_ALIGN(len_in);
1270 
1271 	/* Check to see whether len was rounded up from small -ve to zero */
1272 	if (len_in && !len)
1273 		return -EINVAL;
1274 
1275 	end = start + len;
1276 	if (end < start)
1277 		return -EINVAL;
1278 
1279 	if (end == start)
1280 		return 0;
1281 
1282 #ifdef CONFIG_MEMORY_FAILURE
1283 	if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
1284 		return madvise_inject_error(behavior, start, start + len_in);
1285 #endif
1286 
1287 	write = madvise_need_mmap_write(behavior);
1288 	if (write) {
1289 		if (mmap_write_lock_killable(mm))
1290 			return -EINTR;
1291 	} else {
1292 		mmap_read_lock(mm);
1293 	}
1294 
1295 	blk_start_plug(&plug);
1296 	error = madvise_walk_vmas(mm, start, end, behavior,
1297 			madvise_vma_behavior);
1298 	blk_finish_plug(&plug);
1299 	if (write)
1300 		mmap_write_unlock(mm);
1301 	else
1302 		mmap_read_unlock(mm);
1303 
1304 	return error;
1305 }
1306 
SYSCALL_DEFINE3(madvise,unsigned long,start,size_t,len_in,int,behavior)1307 SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
1308 {
1309 	return do_madvise(current->mm, start, len_in, behavior);
1310 }
1311 
SYSCALL_DEFINE5(process_madvise,int,pidfd,const struct iovec __user *,vec,size_t,vlen,int,behavior,unsigned int,flags)1312 SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
1313 		size_t, vlen, int, behavior, unsigned int, flags)
1314 {
1315 	ssize_t ret;
1316 	struct iovec iovstack[UIO_FASTIOV], iovec;
1317 	struct iovec *iov = iovstack;
1318 	struct iov_iter iter;
1319 	struct pid *pid;
1320 	struct task_struct *task;
1321 	struct mm_struct *mm;
1322 	size_t total_len;
1323 	unsigned int f_flags;
1324 
1325 	if (flags != 0) {
1326 		ret = -EINVAL;
1327 		goto out;
1328 	}
1329 
1330 	ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
1331 	if (ret < 0)
1332 		goto out;
1333 
1334 	pid = pidfd_get_pid(pidfd, &f_flags);
1335 	if (IS_ERR(pid)) {
1336 		ret = PTR_ERR(pid);
1337 		goto free_iov;
1338 	}
1339 
1340 	task = get_pid_task(pid, PIDTYPE_PID);
1341 	if (!task) {
1342 		ret = -ESRCH;
1343 		goto put_pid;
1344 	}
1345 
1346 	if (!process_madvise_behavior_valid(behavior)) {
1347 		ret = -EINVAL;
1348 		goto release_task;
1349 	}
1350 
1351 	/* Require PTRACE_MODE_READ to avoid leaking ASLR metadata. */
1352 	mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1353 	if (IS_ERR_OR_NULL(mm)) {
1354 		ret = IS_ERR(mm) ? PTR_ERR(mm) : -ESRCH;
1355 		goto release_task;
1356 	}
1357 
1358 	/*
1359 	 * Require CAP_SYS_NICE for influencing process performance. Note that
1360 	 * only non-destructive hints are currently supported.
1361 	 */
1362 	if (!capable(CAP_SYS_NICE)) {
1363 		ret = -EPERM;
1364 		goto release_mm;
1365 	}
1366 
1367 	total_len = iov_iter_count(&iter);
1368 
1369 	while (iov_iter_count(&iter)) {
1370 		iovec = iov_iter_iovec(&iter);
1371 		ret = do_madvise(mm, (unsigned long)iovec.iov_base,
1372 					iovec.iov_len, behavior);
1373 		if (ret < 0)
1374 			break;
1375 		iov_iter_advance(&iter, iovec.iov_len);
1376 	}
1377 
1378 	if (ret == 0)
1379 		ret = total_len - iov_iter_count(&iter);
1380 
1381 release_mm:
1382 	mmput(mm);
1383 release_task:
1384 	put_task_struct(task);
1385 put_pid:
1386 	put_pid(pid);
1387 free_iov:
1388 	kfree(iov);
1389 out:
1390 	return ret;
1391 }
1392