Specifications
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
432
|
Chapter 15: Memory Mapping and DMA
simply does not know how to properly manage reference counts for pages that
are part of higher-order allocations. (Return to the section “A scull Using Whole
Pages: scullp” in Chapter 8 if you need a refresher on scullp and the memory allo-
cation order value.)
The zero-order limitation is mostly intended to keep the code simple. It is possible to
correctly implement mmap for multipage allocations by playing with the usage count
of the pages, but it would only add to the complexity of the example without intro-
ducing any interesting information.
Code that is intended to map RAM according to the rules just outlined needs to
implement the open, close, and nopage VMA methods; it also needs to access the
memory map to adjust the page usage counts.
This implementation of scullp_mmap is very short, because it relies on the nopage
function to do all the interesting work:
int scullp_mmap(struct file *filp, struct vm_area_struct *vma)
{
struct inode *inode = filp->f_dentry->d_inode;
/* refuse to map if order is not 0 */
if (scullp_devices[iminor(inode)].order)
return -ENODEV;
/* don't do anything here: "nopage" will fill the holes */
vma->vm_ops = &scullp_vm_ops;
vma->vm_flags |= VM_RESERVED;
vma->vm_private_data = filp->private_data;
scullp_vma_open(vma);
return 0;
}
The purpose of the if statement is to avoid mapping devices whose allocation order
is not
0. scullp’s operations are stored in the vm_ops field, and a pointer to the device
structure is stashed in the
vm_private_data field. At the end, vm_ops->open is called to
update the count of active mappings for the device.
open and close simply keep track of the mapping count and are defined as follows:
void scullp_vma_open(struct vm_area_struct *vma)
{
struct scullp_dev *dev = vma->vm_private_data;
dev->vmas++;
}
void scullp_vma_close(struct vm_area_struct *vma)
{
struct scullp_dev *dev = vma->vm_private_data;
dev->vmas--;
}
,ch15.13676 Page 432 Friday, January 21, 2005 11:04 AM