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