Search code examples
clinuxoperating-systemfilesystemsmmap

What's the exact meaning of the flag MS_INVALIDATE in msync?


Having read the manual of msync, I think the exact meaning of MS_INVALIDATE is as follows:

Provided that there are three processes p1, p2, and p3.

p1 and p2 both use mmap with MAP_SHARED to simutaneously read and write the file /tmp/data.txt.

p3 uses fread to read the same file.

Assume that p1 have modified the file, p2 will immediately see the modification. However, p3 using fread is not sure to see the modification.

If p1 call msync with MS_INVALIDATE|MS_SYNC after the modification, then p3 using fread is SURE to see the modification. That's all the meanings of the flag MS_INVALIDATE.

Is my understanding correct?


Solution

  • AFAIK, on linux kernel, MS_INVALIDATE actually doesn't do much, this is from msync.c

    The only use is this check.

            /* Here vma->vm_start <= start < vma->vm_end. */
            if ((flags & MS_INVALIDATE) &&
                    (vma->vm_flags & VM_LOCKED)) {
                error = -EBUSY;
                goto out_unlock;
            }
    

    Earlier linux systems had separate stores for I/O coming in via write/read syscalls ( aka buffer cache) and and the other one (page cache) which the mmap maps onto. However, now Most unix based systems have a global unified cache, i.e no matter where the I/O comes from it will land up onto same in-core buffer of memory. Of course the implementation is way tricker than it seems, but MS_INVALIDATE seems to me a no-op on most UNIX based systems.

    Some links: link1 link2