Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/mm/msync.c |
| 4 | * |
| 5 | * Copyright (C) 1994-1999 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * The msync() system call. |
| 10 | */ |
Andrew Morton | 8f2e9f1 | 2006-03-24 03:18:15 -0800 | [diff] [blame] | 11 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/mman.h> |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 14 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/syscalls.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
| 19 | * MS_SYNC syncs the entire file - including mappings. |
| 20 | * |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 21 | * MS_ASYNC does not start I/O (it used to, up to 2.5.67). |
| 22 | * Nor does it marks the relevant pages dirty (it used to up to 2.6.17). |
| 23 | * Now it doesn't do anything, since dirty pages are properly tracked. |
| 24 | * |
| 25 | * The application may now run fsync() to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * write out the dirty pages and wait on the writeout and check the result. |
| 27 | * Or the application may run fadvise(FADV_DONTNEED) against the fd to start |
| 28 | * async writeout immediately. |
Amos Waterland | 16538c4 | 2006-03-24 18:30:53 +0100 | [diff] [blame] | 29 | * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * applications. |
| 31 | */ |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 32 | SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | unsigned long end; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 35 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct vm_area_struct *vma; |
Andrew Morton | 676758b | 2006-03-24 03:18:14 -0800 | [diff] [blame] | 37 | int unmapped_error = 0; |
| 38 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Andrey Konovalov | 057d3389 | 2019-09-25 16:48:30 -0700 | [diff] [blame] | 40 | start = untagged_addr(start); |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC)) |
| 43 | goto out; |
Alexander Kuleshov | b0d61c7 | 2015-11-05 18:46:32 -0800 | [diff] [blame] | 44 | if (offset_in_page(start)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | goto out; |
| 46 | if ((flags & MS_ASYNC) && (flags & MS_SYNC)) |
| 47 | goto out; |
| 48 | error = -ENOMEM; |
| 49 | len = (len + ~PAGE_MASK) & PAGE_MASK; |
| 50 | end = start + len; |
| 51 | if (end < start) |
| 52 | goto out; |
| 53 | error = 0; |
| 54 | if (end == start) |
| 55 | goto out; |
| 56 | /* |
| 57 | * If the interval [start,end) covers some unmapped address ranges, |
Nikita Ermakov | f6899bc | 2021-04-29 22:55:41 -0700 | [diff] [blame] | 58 | * just ignore them, but return -ENOMEM at the end. Besides, if the |
| 59 | * flag is MS_ASYNC (w/o MS_INVALIDATE) the result would be -ENOMEM |
| 60 | * anyway and there is nothing left to do, so return immediately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | */ |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 62 | mmap_read_lock(mm); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 63 | vma = find_vma(mm, start); |
| 64 | for (;;) { |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 65 | struct file *file; |
Matthew Wilcox | 7fc34a6 | 2014-06-04 16:10:44 -0700 | [diff] [blame] | 66 | loff_t fstart, fend; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 67 | |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 68 | /* Still start < end. */ |
| 69 | error = -ENOMEM; |
| 70 | if (!vma) |
| 71 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* Here start < vma->vm_end. */ |
| 73 | if (start < vma->vm_start) { |
Nikita Ermakov | f6899bc | 2021-04-29 22:55:41 -0700 | [diff] [blame] | 74 | if (flags == MS_ASYNC) |
| 75 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | start = vma->vm_start; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 77 | if (start >= end) |
| 78 | goto out_unlock; |
| 79 | unmapped_error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | /* Here vma->vm_start <= start < vma->vm_end. */ |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 82 | if ((flags & MS_INVALIDATE) && |
| 83 | (vma->vm_flags & VM_LOCKED)) { |
| 84 | error = -EBUSY; |
| 85 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 87 | file = vma->vm_file; |
Namjae Jeon | 496a8e6 | 2014-07-02 15:22:36 -0700 | [diff] [blame] | 88 | fstart = (start - vma->vm_start) + |
| 89 | ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
Matthew Wilcox | 7fc34a6 | 2014-06-04 16:10:44 -0700 | [diff] [blame] | 90 | fend = fstart + (min(end, vma->vm_end) - start) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | start = vma->vm_end; |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 92 | if ((flags & MS_SYNC) && file && |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 93 | (vma->vm_flags & VM_SHARED)) { |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 94 | get_file(file); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 95 | mmap_read_unlock(mm); |
Kirill A. Shutemov | 0661a33 | 2015-02-10 14:10:04 -0800 | [diff] [blame] | 96 | error = vfs_fsync_range(file, fstart, fend, 1); |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 97 | fput(file); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 98 | if (error || start >= end) |
| 99 | goto out; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 100 | mmap_read_lock(mm); |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 101 | vma = find_vma(mm, start); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 102 | } else { |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 103 | if (start >= end) { |
| 104 | error = 0; |
| 105 | goto out_unlock; |
| 106 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 107 | vma = vma->vm_next; |
| 108 | } |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 109 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 110 | out_unlock: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 111 | mmap_read_unlock(mm); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 112 | out: |
Peter Zijlstra | 204ec84 | 2006-09-25 23:31:01 -0700 | [diff] [blame] | 113 | return error ? : unmapped_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |