title: How to share hugepage between kernel and user space
body: <p>I know how to get normal pages in linux kernel and share that with user process using mmap based on this code: <a href="http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-8.html" rel="nofollow noreferrer">http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-8.html</a>
  Now I want to extend it to use <strong>hugepages</strong> (2M).</p>
  
  <p>Is it enough to put the file that backs up the shared memory in the hugetlbfs filesystem?
  Can you also point me to a tutorial on using hugepages?</p>
  
  <p><strong>Progress until now:</strong>
   I used hugeadm to enable hugepages and transparent hugepages and added the corresponding grub configs.</p>
  
  <p><a href="https://stackoverflow.com/questions/19460544/how-do-i-allocate-a-dma-buffer-backed-by-1gb-hugepages-in-a-linux-kernel-module">This post</a> on stackoverflow says to use the following parameters for alloc_pages</p>
  
  <pre><code>struct page *p = alloc_pages(GFP_TRANSHUGE, HPAGE_PMD_ORDER);
  </code></pre>
  
  <p>It didn't work as /proc/meminfo still says all huge pages are free.</p>
  
  <p>Then I changed</p>
  
  <pre><code>vma-&gt;vm_flags |= VM_RESERVED;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>vma-&gt;vm_flags |= VM_HUGETLB;
  </code></pre>
  
  <p>but the following error happened:</p>
  
  <pre><code>Oct  4 15:43:50 masoud-VirtualBox kernel: [ 2756.091381] BUG: unable to handle kernel paging request at 00000000006f0082
  Oct  4 15:43:50 masoud-VirtualBox kernel: [ 2756.091387] IP: [&lt;ffffffff8119522e&gt;] hugetlb_fault+0x3e/0x760
  Oct  4 15:43:50 masoud-VirtualBox kernel: [ 2756.091394] PGD bde87067 PUD c2ad7067 PMD b79ca067 PTE 0
  Oct  4 15:43:50 masoud-VirtualBox kernel: [ 2756.091399] Oops: 0000 [#1] SMP 
  </code></pre>