Memory Mapping Data Structures

A memory mapping is represented by a combination of the following data structures:

• The inode object associated with the mapped file

• The address_space object of the mapped file

• A file object for each different mapping performed on the file by different processes

• A vm_area_struct descriptor for each different mapping on the file

• A page descriptor for each page frame assigned to a memory region that maps the file

Figure 15-4 illustrates how the data structures are linked. In the upper-left corner, we show the inode, which identifies the file. The i_mapping field of each inode object points to the address space object of the file. In turn, the i mmap or i mmap shared fields of each address_space object point to the first element of a doubly linked list that includes all memory regions that currently map the file; if both fields are null, the file is not mapped by any memory region. The list contains vm_area_struct descriptors that represent memory regions, and is implemented by means of the vm_next_share and vm pprev share fields.

Figure 15-4. Data structures for file memory mapping

Figure 15-4. Data structures for file memory mapping

Linux Memory Mapping Files

The vm_file field of each memory region descriptor contains the address of a file object for the mapped file; if that field is null, the memory region is not used in a memory mapping. The file object contains fields that allow the kernel to identify both the process that owns the memory mapping and the file being mapped.

The position of the first mapped location is stored into the vm_pgoff field of the memory region descriptor; it represents the file offset as a number of page-size units. The length of the mapped file portion is simply the length of the memory region, which can be computed from the vm start and vm end fields.

Pages of shared memory mappings are always included in the page cache; pages of private memory mappings are included in the page cache as long as they are unmodified. When a process tries to modify a page of a private memory mapping, the kernel duplicates the page frame and replaces the original page frame with the duplicate in the process Page Table; this is one of the applications of the Copy On Write mechanism that we discussed in Chapter 8. The original page frame still remains in the page cache, although it no longer belongs to the memory mapping since it is replaced by the duplicate. In turn, the duplicate is not inserted into the page cache since it no longer contains valid data representing the file on disk.

Figure 15-4 also shows a few page descriptors of pages included in the page cache that refer to the memory-mapped file. Notice that the first memory region in the figure is three pages long, but only two page frames are allocated for it; presumably, the process owning the memory region has never accessed the third page. Although not shown in the figure, the page descriptors are inserted into the clean_pages, dirty_pages, and locked_pages doubly linked lists described in Section 14.1.2.

The kernel offers several hooks to customize the memory mapping mechanism for every different filesystem. The core of memory mapping implementation is delegated to a file object's method named mmap. For most disk-based filesystems and for block device files, this method is implemented by a general function called generic_file_mmap( ), which is described in the next section.

File memory mapping depends on the demand paging mechanism described in Section 8.4.3. In fact, a newly established memory mapping is a memory region that doesn't include any page; as the process references an address inside the region, a Page Fault occurs and the Page Fault handler checks whether the nopage method of the memory region is defined. If nopage is not defined, the memory region doesn't map a file on disk; otherwise, it does, and the method takes care of reading the page by accessing the block device. Almost all disk-based filesystems and block device files implement the nopage method by means of the filemap nopage( ) function.

Continue reading here: Creating a Memory Mapping

Was this article helpful?

+2 0


  • henrik
    Where is memory map defined in hal files?
    2 months ago
  • wolfgang
    How to share a kernal data structure using "mmap"?
    2 years ago