blob: 137d1c104f3e94399d6706c5e342af0048615a4d [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,
Nikita Ermakovf6899bc2021-04-29 22:55:41 -070058 * 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 Torvalds1da177e2005-04-16 15:20:36 -070061 */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070062 mmap_read_lock(mm);
Peter Zijlstra204ec842006-09-25 23:31:01 -070063 vma = find_vma(mm, start);
64 for (;;) {
Andrew Morton9c50823e2006-03-24 03:18:12 -080065 struct file *file;
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070066 loff_t fstart, fend;
Andrew Morton9c50823e2006-03-24 03:18:12 -080067
Peter Zijlstra204ec842006-09-25 23:31:01 -070068 /* Still start < end. */
69 error = -ENOMEM;
70 if (!vma)
71 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* Here start < vma->vm_end. */
73 if (start < vma->vm_start) {
Nikita Ermakovf6899bc2021-04-29 22:55:41 -070074 if (flags == MS_ASYNC)
75 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 start = vma->vm_start;
Peter Zijlstra204ec842006-09-25 23:31:01 -070077 if (start >= end)
78 goto out_unlock;
79 unmapped_error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81 /* Here vma->vm_start <= start < vma->vm_end. */
Peter Zijlstra204ec842006-09-25 23:31:01 -070082 if ((flags & MS_INVALIDATE) &&
83 (vma->vm_flags & VM_LOCKED)) {
84 error = -EBUSY;
85 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Andrew Morton9c50823e2006-03-24 03:18:12 -080087 file = vma->vm_file;
Namjae Jeon496a8e62014-07-02 15:22:36 -070088 fstart = (start - vma->vm_start) +
89 ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Matthew Wilcox7fc34a62014-06-04 16:10:44 -070090 fend = fstart + (min(end, vma->vm_end) - start) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 start = vma->vm_end;
Peter Zijlstra204ec842006-09-25 23:31:01 -070092 if ((flags & MS_SYNC) && file &&
Andrew Morton707c21c2006-03-24 03:18:13 -080093 (vma->vm_flags & VM_SHARED)) {
Andrew Morton707c21c2006-03-24 03:18:13 -080094 get_file(file);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -070095 mmap_read_unlock(mm);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -080096 error = vfs_fsync_range(file, fstart, fend, 1);
Andrew Morton707c21c2006-03-24 03:18:13 -080097 fput(file);
Peter Zijlstra204ec842006-09-25 23:31:01 -070098 if (error || start >= end)
99 goto out;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700100 mmap_read_lock(mm);
Peter Zijlstra204ec842006-09-25 23:31:01 -0700101 vma = find_vma(mm, start);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800102 } else {
Peter Zijlstra204ec842006-09-25 23:31:01 -0700103 if (start >= end) {
104 error = 0;
105 goto out_unlock;
106 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800107 vma = vma->vm_next;
108 }
Peter Zijlstra204ec842006-09-25 23:31:01 -0700109 }
Andrew Morton9c50823e2006-03-24 03:18:12 -0800110out_unlock:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700111 mmap_read_unlock(mm);
Andrew Morton9c50823e2006-03-24 03:18:12 -0800112out:
Peter Zijlstra204ec842006-09-25 23:31:01 -0700113 return error ? : unmapped_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}