Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 17: Data Synchronization


The implementation of the system call is remarkably simple. As documented in the manual page
msync(2), the system call essentially distinguishes two modes. IfMS_SYNCis set in the flags, then dirty
pages are written to disk synchronously, whileMS_ASYNCis supposed to schedule dirty data for later
writeback.

The good news is that forMS_ASYNC, no work at all is required! Since the kernel tracks the state of dirty
pages, they will be synchronized at some point by the mechanisms described in this chapter anyway.

WithMS_SYNCset, a little more work is necessary, and the code flow diagram in Figure 17-14 considers
this case solely.

Iterate
over all
vm_areas

sys_msync

find_vma

do_fsync

Figure 17-14: Code flow diagram
forsys_msyncwith the flag
MS_SYNCset.

find_vmafinds the firstvm_areainstance in the selected area.vm_area->vm_fileholds a pointer to the
fileinstance from which the mapped data originate (this is discussed in Chapter 4). Therefore,do_fsync
can be used to synchronize the file as described above.

The method is repeated for all intervals in the desired area. This is possible because the intervals are
linked by means ofvm_area->next, as discussed in Chapter 4.

17.16 Summary


Data are persistently stored on block devices but modified in RAM. This makes it necessary to synchro-
nize the contents of both from time to time, and this chapter has introduced you to the corresponding
methods. There are several system calls that allow forexplicitly requesting that some portions of memory
are written back to disk. Additionally, the kernel uses threads that perform the same job periodically to
ensure that not too much modified data exist in RAM. While it is a good thing to keep disks busy, the ker-
nel needs to ensure that not more information than the disk can handle is scheduled to be written back,
and this chapter has discussed the techniques used toavoid congestion of the block layer. Additionally,
some corrections are necessary for systems where power is scarce, and the policy changes in laptop mode
have also been examined.

You have been introduced to all technical aspects ofhow data are shuffled back and forth between block
devices and RAM by now. What is still lacking is the decision ofwhichpages are supposed to be syn-
chronized or discarded from RAM once the kernel gets short on memory, which is the subject of the next
chapter.
Free download pdf