title: Mmap DMA Coherent Memory to User Space
body: <p>I am trying to map DMA coherent memory, which I allocated in my kernel driver, to user space. There I use <code>mmap()</code> and in kernel driver I use <code>dma_alloc_coherent()</code> and afterwards <code>remap_pfn_range()</code> to remap the pages. </p>
  
  <p>The purpose of mapping the DMA memory to User Space is to minimize the ioctl access to the kernel. The host must perform quite a high number of DMA coherent memory accesses and I want to access it directly in User Space instead of wasting time by using countless <code>ioctl()</code> operations.</p>
  
  <p><code>mmap()</code> returns <strong>EPERM</strong> (1) - <strong>Operation not permitted</strong>. </p>
  
  <p>I found this post: <a href="https://stackoverflow.com/questions/8213671/mmap-operation-not-permitted">mmap: Operation not permitted</a></p>
  
  <p>Answer:</p>
  
  <blockquote>
    <p>It sounds like the kernel has been compiled with CONFIG_STRICT_DEVMEM
    enabled. This is a security feature to prevent user space access to
    (possibly sensitive) physical memory above 1MB (IIRC). You might be
    able to disable this with sysctl dev.mem.restricted.</p>
  </blockquote>
  
  <p>That is the only useful info I've found. However, I see 2 issues:</p>
  
  <p>1) I've allocated for test purposes only 4k. According to the above statement, only physical memory > 1MB should be a problem. I still can't mmap (anyway, for the final driver I would need a lot more dma memory, but recompiling the kernel can't be the solution to my problem) Which leads me to 2)</p>
  
  <p>2) Furthermore, re-compiling the kernel is not an option as the driver should work without tweaking the kernel in a specific way.</p>
  
  <p>Any ideas on this one? I appreciate the help.</p>
  
  <p>I am using <strong>Ubuntu 16.04.1</strong>, Kernel: <strong>4.10.0-40-generic</strong></p>
  
  <p><strong>EDIT: SOLVED</strong></p>
  
  <p>I made a copy-paste mistake which resulted in a ret=-1. So the .mmap function in the kernel driver which calls remap_pfn_range, returned -1 instead of 0. This resulted in a failing mmap() in user space</p>