Line data Source code
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 : 18 : /* 19 : * MS_SYNC syncs the entire file - including mappings. 20 : * 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 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. 29 : * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to 30 : * applications. 31 : */ 32 166 : SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags) 33 : { 34 83 : unsigned long end; 35 83 : struct mm_struct *mm = current->mm; 36 83 : struct vm_area_struct *vma; 37 83 : int unmapped_error = 0; 38 83 : int error = -EINVAL; 39 : 40 83 : start = untagged_addr(start); 41 : 42 83 : if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC)) 43 0 : goto out; 44 83 : if (offset_in_page(start)) 45 0 : goto out; 46 83 : if ((flags & MS_ASYNC) && (flags & MS_SYNC)) 47 0 : goto out; 48 83 : error = -ENOMEM; 49 83 : len = (len + ~PAGE_MASK) & PAGE_MASK; 50 83 : end = start + len; 51 83 : if (end < start) 52 0 : goto out; 53 83 : error = 0; 54 83 : if (end == start) 55 0 : 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 : */ 60 83 : mmap_read_lock(mm); 61 83 : vma = find_vma(mm, start); 62 83 : for (;;) { 63 83 : struct file *file; 64 83 : loff_t fstart, fend; 65 : 66 : /* Still start < end. */ 67 83 : error = -ENOMEM; 68 83 : if (!vma) 69 0 : goto out_unlock; 70 : /* Here start < vma->vm_end. */ 71 83 : if (start < vma->vm_start) { 72 0 : start = vma->vm_start; 73 0 : if (start >= end) 74 0 : goto out_unlock; 75 : unmapped_error = -ENOMEM; 76 : } 77 : /* Here vma->vm_start <= start < vma->vm_end. */ 78 83 : if ((flags & MS_INVALIDATE) && 79 81 : (vma->vm_flags & VM_LOCKED)) { 80 0 : error = -EBUSY; 81 0 : goto out_unlock; 82 : } 83 83 : file = vma->vm_file; 84 83 : fstart = (start - vma->vm_start) + 85 83 : ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 86 83 : fend = fstart + (min(end, vma->vm_end) - start) - 1; 87 83 : start = vma->vm_end; 88 83 : if ((flags & MS_SYNC) && file && 89 83 : (vma->vm_flags & VM_SHARED)) { 90 81 : get_file(file); 91 81 : mmap_read_unlock(mm); 92 81 : error = vfs_fsync_range(file, fstart, fend, 1); 93 81 : fput(file); 94 81 : if (error || start >= end) 95 81 : goto out; 96 0 : mmap_read_lock(mm); 97 0 : vma = find_vma(mm, start); 98 : } else { 99 2 : if (start >= end) { 100 2 : error = 0; 101 2 : goto out_unlock; 102 : } 103 0 : vma = vma->vm_next; 104 : } 105 : } 106 2 : out_unlock: 107 2 : mmap_read_unlock(mm); 108 83 : out: 109 83 : return error ? : unmapped_error; 110 : }