I want to implement a Linux kernel device driver, which includes the mmap file operation. I want to write a user space program (in C++), which uses the mmap system call to read/write to the device's memory.
I read this (and other chapters of this book). The mmap file operation needs a filep. The user space program needs a file, which it can access. I know how to create a file in /dev/ by register_chrdev
, but I learned here, that this includes higher latency (which is what I try to avoid).
What is the correct way to create a filep for the file operation mmap, along with a file that is accessible by the mmap system call?
Basically, you need to expose to user space some object (file) which will be mmapable and you want to control the kernel side of things.
The easiest way to achieve this is to create a procfs
, sysfs
or debugfs
file (it will be available somewhere under "/proc", "/sys" or wherever the debugfs is mounted on your system). All those filesystems support easy (on the kernel side) creation of files immediately visible to the userspace.
Additional options include:
Registering a char or block device (all devices are mmapable, but the boilerplate may get extensive for a custom device; I don't think latency is of any special concern, device variety notwithstanding).
Using shmem_file_setup
and friends - the slight difficulty here is exposing the newly created shmem file to the user space.
If you just want to play a bit with mmaps, procfs
feels like the simplest approach overall (a single call to proc_create
will do).