1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/mm/msync.c
4 *
5 * Copyright (C) 1994-1999 Linus Torvalds
6 */
7
8 /*
9 * The msync() system call.
10 */
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/file.h>
15 #include <linux/syscalls.h>
16 #include <linux/sched.h>
17 #include <linux/page_size_compat.h>
18
19 /*
20 * MS_SYNC syncs the entire file - including mappings.
21 *
22 * MS_ASYNC does not start I/O (it used to, up to 2.5.67).
23 * Nor does it marks the relevant pages dirty (it used to up to 2.6.17).
24 * Now it doesn't do anything, since dirty pages are properly tracked.
25 *
26 * The application may now run fsync() to
27 * write out the dirty pages and wait on the writeout and check the result.
28 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
29 * async writeout immediately.
30 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
31 * applications.
32 */
SYSCALL_DEFINE3(msync,unsigned long,start,size_t,len,int,flags)33 SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
34 {
35 unsigned long end;
36 struct mm_struct *mm = current->mm;
37 struct vm_area_struct *vma;
38 int unmapped_error = 0;
39 int error = -EINVAL;
40
41 start = untagged_addr(start);
42
43 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
44 goto out;
45 if (__offset_in_page_log(start))
46 goto out;
47 if ((flags & MS_ASYNC) && (flags & MS_SYNC))
48 goto out;
49 error = -ENOMEM;
50 len = (len + ~__PAGE_MASK) & __PAGE_MASK;
51 end = start + len;
52 if (end < start)
53 goto out;
54 error = 0;
55 if (end == start)
56 goto out;
57 /*
58 * If the interval [start,end) covers some unmapped address ranges,
59 * just ignore them, but return -ENOMEM at the end. Besides, if the
60 * flag is MS_ASYNC (w/o MS_INVALIDATE) the result would be -ENOMEM
61 * anyway and there is nothing left to do, so return immediately.
62 */
63 mmap_read_lock(mm);
64 vma = find_vma(mm, start);
65 for (;;) {
66 struct file *file;
67 loff_t fstart, fend;
68
69 /* Still start < end. */
70 error = -ENOMEM;
71 if (!vma)
72 goto out_unlock;
73 /* Here start < vma->vm_end. */
74 if (start < vma->vm_start) {
75 if (flags == MS_ASYNC)
76 goto out_unlock;
77 start = vma->vm_start;
78 if (start >= end)
79 goto out_unlock;
80 unmapped_error = -ENOMEM;
81 }
82 /* Here vma->vm_start <= start < vma->vm_end. */
83 if ((flags & MS_INVALIDATE) &&
84 (vma->vm_flags & VM_LOCKED)) {
85 error = -EBUSY;
86 goto out_unlock;
87 }
88 file = vma->vm_file;
89 fstart = (start - vma->vm_start) +
90 ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
91 fend = fstart + (min(end, vma->vm_end) - start) - 1;
92 start = vma->vm_end;
93 if ((flags & MS_SYNC) && file &&
94 (vma->vm_flags & VM_SHARED)) {
95 get_file(file);
96 mmap_read_unlock(mm);
97 error = vfs_fsync_range(file, fstart, fend, 1);
98 fput(file);
99 if (error || start >= end)
100 goto out;
101 mmap_read_lock(mm);
102 vma = find_vma(mm, start);
103 } else {
104 if (start >= end) {
105 error = 0;
106 goto out_unlock;
107 }
108 vma = find_vma(mm, vma->vm_end);
109 }
110 }
111 out_unlock:
112 mmap_read_unlock(mm);
113 out:
114 return error ? : unmapped_error;
115 }
116