"lazy" mmap for a device driver ?

John Baldwin jhb at freebsd.org
Wed Jun 1 17:00:58 UTC 2011

On Tuesday, May 31, 2011 4:21:42 pm Luigi Rizzo wrote:
> hi,
> i have a kernel module implementing a memory mapped special device
> which exports a large block of memory to the process.
> I see that when the process calls mmap(), my routine foo_mmap()
> is called immediately once per page, even though the process is
> not actually touching the pages. I believe this happens
> through dev_pager_alloc().

Yes, this is to verify that all the pages in the mmap() request are
permitted for access so that an error can be returned to userland if it maps 
an invalid region of the device's virtual address space.

> Right now i can live with that because all the memory is allocated
> at module load time, but i might want to have a sparse memory
> region which is populated dynamically, so i was wondering if
> there is a way to achieve this. I see there are two other
> device routines, d_mmap2 and d_mmap_single, any pointer to
> documentation or comments on how they differ ?

d_mmap2 is an ABI hack in 7/8, it is identical to d_mmap in 9.

d_mmap_single() allows a driver to "claim" an entire mmap() request and return 
an arbitrary VM object.  If all you need is a random assortment of pages of a 
fixed size object that is shared between userland and the kernel, you can 
create an OBJT_DEFAULT VM object and let it fault in pages on demand perhaps.  
You would need to be careful to wire the pages if you are going to access them 
from the kernel.

John Baldwin

More information about the freebsd-current mailing list