2

I know how to get normal pages in linux kernel and share that with user process using mmap based on this code: http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-8.html Now I want to extend it to use hugepages (2M).

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?

Progress until now: I used hugeadm to enable hugepages and transparent hugepages and added the corresponding grub configs.

This post on stackoverflow says to use the following parameters for alloc_pages

struct page *p = alloc_pages(GFP_TRANSHUGE, HPAGE_PMD_ORDER);

It didn't work as /proc/meminfo still says all huge pages are free.

Then I changed

vma->vm_flags |= VM_RESERVED;

to

vma->vm_flags |= VM_HUGETLB;

but the following error happened:

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: [<ffffffff8119522e>] 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 
Community
  • 1
  • 1
Masood_mj
  • 1,144
  • 12
  • 25
  • Not good StackOverflow etiquette man! I'm gonna ask what have you tried so far? :) – Zahaib Akhtar Oct 03 '14 at 07:41
  • 1
    Masood_mj, Moshref, I think it can be easier to allocate memory in userspace, even using hugetlbfs (libhugetlbfs) and then pass it (via ioctl?) to kernel to lock it (mlock in userspace or put_page in kernelspace). Your Oops sounds like kernel was unable to pagefault the hugepage from kernel context. Try newer versions of kernel or do this pagefault from userspace or prefault the page before accessing it. – osgx Mar 03 '15 at 17:37
  • Thanks osgx, at the end I created hugepages at the userspace and passed their address to the kernel. – Masood_mj Mar 04 '15 at 13:07
  • @Masood_mj: How did you pass the addresses to kernel? It would be helpful if you can elaborate an answer to your question! – Sachin Mokashi Jun 20 '17 at 06:22
  • I remember I put that in a file and let kernel read it! – Masood_mj Jun 20 '17 at 17:29
  • Hi Sachin I finally came back and updated [my post (linked in the OP)](https://stackoverflow.com/questions/19460544/how-do-i-allocate-a-dma-buffer-backed-by-1gb-hugepages-in-a-linux-kernel-module) with a solution that worked for me (to get the physical address for DMA). – muusbolla Jun 26 '17 at 09:47
  • To get the kernel virtual address(pointer) from a user address: get a struct page* with either follow_page for a normal user address, or follow_huge_addr for hugepage-backed address. Then pass the struct page* to kmap, this will give you a kernel virtual address. – muusbolla Jun 26 '17 at 10:11
  • get_user_pages() may also work to get an array of struct page* that corresponds to the user buffer (only for normal buffer, not for hugepage). My memory is a little hazy on this. – muusbolla Jun 26 '17 at 10:13

0 Answers0