blob: 69c6d202953187f911c14356ce67200a84208b37 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/msync.c
4 *
5 * Copyright (C) 1994-1999 Linus Torvalds
6 */
7
8/*
9 * The msync() system call.
10 */
Andrew Morton8f2e9f12006-03-24 03:18:15 -080011#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/mman.h>
Andrew Morton9c50823e2006-03-24 03:18:12 -080014#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/syscalls.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * MS_SYNC syncs the entire file - including mappings.
20 *
Peter Zijlstra204ec842006-09-25 23:31:01 -070021 * 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 Torvalds1da177e2005-04-16 15:20:36 -070026 * 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 Waterland16538c42006-03-24 18:30:53 +010029 * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * applications.
31 */
Heiko Carstens6a6160a2009-01-14 14:14:15 +010032SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 unsigned long end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070035 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct vm_area_struct *vma;
Andrew Morton676758b2006-03-24 03:18:14 -080037 int unmapped_error = 0;
38 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Andrey Konovalov057d33892019-09-25 16:48:30 -070040 start = untagged_addr(start);
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
43 goto out;
Alexander Kuleshovb0d61c72015-11-05 18:46:32 -080044 if (offset_in_page(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 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,
58 * just ignore them, but return -ENOMEM at the end.
59 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070060 mmap_read_lock(mm);
Peter Zijlstra204ec842006-09-25 23:31:01 -070061 vma = find_vma(mm, start);
62 for (;;) {
Andrew Morton9c50823e2006-03-24 03:18:12 -080063 struct file *file;
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070064 loff_t fstart, fend;
Andrew Morton9c50823e2006-03-24 03:18:12 -080065
Peter Zijlstra204ec842006-09-25 23:31:01 -070066 /* Still start < end. */
67 error = -ENOMEM;
68 if (!vma)
69 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* Here start < vma->vm_end. */
71 if (start < vma->vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 start = vma->vm_start;
Peter Zijlstra204ec842006-09-25 23:31:01 -070073 if (start >= end)
74 goto out_unlock;
75 unmapped_error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 /* Here vma->vm_start <= start < vma->vm_end. */
Peter Zijlstra204ec842006-09-25 23:31:01 -070078 if ((flags & MS_INVALIDATE) &&
79 (vma->vm_flags & VM_LOCKED)) {
80 error = -EBUSY;
81 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080083 file = vma->vm_file;
Namjae Jeon496a8e62014-07-02 15:22:36 -070084 fstart = (start - vma->vm_start) +
85 ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070086 fend = fstart + (min(end, vma->vm_end) - start) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070088 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080089 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080090 get_file(file);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070091 mmap_read_unlock(mm);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -080092 error = vfs_fsync_range(file, fstart, fend, 1);
Andrew Morton707c21c2006-03-24 03:18:13 -080093 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070094 if (error || start >= end)
95 goto out;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070096 mmap_read_lock(mm);
Peter Zijlstra204ec842006-09-25 23:31:01 -070097 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -080098 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -070099 if (start >= end) {
100 error = 0;
101 goto out_unlock;
102 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800103 vma = vma->vm_next;
104 }
Peter Zijlstra204ec842006-09-25 23:31:01 -0700105 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800106out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700107 mmap_read_unlock(mm);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800108out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700109 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}