Let’s say the buffer is allocated using a page based scheme. One way to implement mmap would be to use remap_pfn_range but LDD3 says this does not work for conventional memory. It appears we can work around this by marking the page(s) reserved using SetPageReserved so that it gets locked in memory. But isn’t all kernel memory already non-swappable i.e. already reserved? Why the need to set the reserved bit explicitly?
Does this have something to do with pages allocated from HIGH_MEM?
The simplest way to map a set of pages from the kernel in your mmap method is to use the fault handler to map the pages. Basically you end up with something like:
(where the other file operations are whatever your module needs). Also in
my_mmapyou do whatever range checking etc. is needed to validate the mmap parameters.Then the
vm_opslook like:where you just need to figure out for a given vma / vmf passed to your fault function which page to map into userspace. This depends on exactly how your module works. For example, if you did
then the page you use would be something like
But you could easily create an array and allocate a page for each entry, use kmalloc, whatever.
[just noticed that
my_faultis a slightly amusing name for a function]