~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/mm/msync.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 33 {
 34         unsigned long end;
 35         struct mm_struct *mm = current->mm;
 36         struct vm_area_struct *vma;
 37         int unmapped_error = 0;
 38         int error = -EINVAL;
 39 
 40         start = untagged_addr(start);
 41 
 42         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
 43                 goto out;
 44         if (offset_in_page(start))
 45                 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. 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.
 61          */
 62         mmap_read_lock(mm);
 63         vma = find_vma(mm, start);
 64         for (;;) {
 65                 struct file *file;
 66                 loff_t fstart, fend;
 67 
 68                 /* Still start < end. */
 69                 error = -ENOMEM;
 70                 if (!vma)
 71                         goto out_unlock;
 72                 /* Here start < vma->vm_end. */
 73                 if (start < vma->vm_start) {
 74                         if (flags == MS_ASYNC)
 75                                 goto out_unlock;
 76                         start = vma->vm_start;
 77                         if (start >= end)
 78                                 goto out_unlock;
 79                         unmapped_error = -ENOMEM;
 80                 }
 81                 /* Here vma->vm_start <= start < vma->vm_end. */
 82                 if ((flags & MS_INVALIDATE) &&
 83                                 (vma->vm_flags & VM_LOCKED)) {
 84                         error = -EBUSY;
 85                         goto out_unlock;
 86                 }
 87                 file = vma->vm_file;
 88                 fstart = (start - vma->vm_start) +
 89                          ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 90                 fend = fstart + (min(end, vma->vm_end) - start) - 1;
 91                 start = vma->vm_end;
 92                 if ((flags & MS_SYNC) && file &&
 93                                 (vma->vm_flags & VM_SHARED)) {
 94                         get_file(file);
 95                         mmap_read_unlock(mm);
 96                         error = vfs_fsync_range(file, fstart, fend, 1);
 97                         fput(file);
 98                         if (error || start >= end)
 99                                 goto out;
100                         mmap_read_lock(mm);
101                         vma = find_vma(mm, start);
102                 } else {
103                         if (start >= end) {
104                                 error = 0;
105                                 goto out_unlock;
106                         }
107                         vma = find_vma(mm, vma->vm_end);
108                 }
109         }
110 out_unlock:
111         mmap_read_unlock(mm);
112 out:
113         return error ? : unmapped_error;
114 }
115 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php