# Linux Kernel Module Cheat

#### 17.8.5. mmap

The `mmap` system call allows us to share memory between user and kernel space without copying:

    ./mmap.sh
    echo $?

Outcome: the test passes:

    0

Sources:

- kernel_modules/mmap.c

- userland/kernel_modules/mmap.c

- rootfs_overlay/lkmc/mmap.sh

In this example, we make a tiny 4 byte kernel buffer available to user-space, and we then modify it on userspace, and check that the kernel can see the modification.

`mmap`, like most more complex File operations, does not work with debugfs as of 4.9, so we use a procfs file for it.

Example adapted from: https://coherentmusings.wordpress.com/2014/06/10/implementing-mmap-for-transferring-data-from-user-space-to-kernel-space/

Bibliography:

- https://stackoverflow.com/questions/10760479/mmap-kernel-buffer-to-user-space/10770582#10770582

- https://stackoverflow.com/questions/1623008/allocating-memory-for-user-space-from-kernel-thread

- https://stackoverflow.com/questions/6967933/mmap-mapping-in-user-space-a-kernel-buffer-allocated-with-kmalloc

- https://github.com/jeremytrimble/ezdma

- https://github.com/simonjhall/dma

- https://github.com/ikwzm/udmabuf