hugetlbpage.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * IA-32 Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/fs.h>
  8. #include <linux/mm.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/err.h>
  12. #include <linux/sysctl.h>
  13. #include <asm/mman.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/pgalloc.h>
  17. #if 0 /* This is just for testing */
  18. struct page *
  19. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  20. {
  21. unsigned long start = address;
  22. int length = 1;
  23. int nr;
  24. struct page *page;
  25. struct vm_area_struct *vma;
  26. vma = find_vma(mm, addr);
  27. if (!vma || !is_vm_hugetlb_page(vma))
  28. return ERR_PTR(-EINVAL);
  29. pte = huge_pte_offset(mm, address);
  30. /* hugetlb should be locked, and hence, prefaulted */
  31. WARN_ON(!pte || pte_none(*pte));
  32. page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
  33. WARN_ON(!PageHead(page));
  34. return page;
  35. }
  36. int pmd_huge(pmd_t pmd)
  37. {
  38. return 0;
  39. }
  40. int pud_huge(pud_t pud)
  41. {
  42. return 0;
  43. }
  44. struct page *
  45. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  46. pmd_t *pmd, int write)
  47. {
  48. return NULL;
  49. }
  50. #else
  51. struct page *
  52. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  53. {
  54. return ERR_PTR(-EINVAL);
  55. }
  56. int pmd_huge(pmd_t pmd)
  57. {
  58. return !!(pmd_val(pmd) & _PAGE_PSE);
  59. }
  60. int pud_huge(pud_t pud)
  61. {
  62. return !!(pud_val(pud) & _PAGE_PSE);
  63. }
  64. #endif
  65. #ifdef CONFIG_HUGETLB_PAGE
  66. static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
  67. unsigned long addr, unsigned long len,
  68. unsigned long pgoff, unsigned long flags)
  69. {
  70. struct hstate *h = hstate_file(file);
  71. struct vm_unmapped_area_info info;
  72. info.flags = 0;
  73. info.length = len;
  74. info.low_limit = current->mm->mmap_legacy_base;
  75. info.high_limit = TASK_SIZE;
  76. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  77. info.align_offset = 0;
  78. return vm_unmapped_area(&info);
  79. }
  80. static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
  81. unsigned long addr0, unsigned long len,
  82. unsigned long pgoff, unsigned long flags)
  83. {
  84. struct hstate *h = hstate_file(file);
  85. struct vm_unmapped_area_info info;
  86. unsigned long addr;
  87. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  88. info.length = len;
  89. info.low_limit = PAGE_SIZE;
  90. info.high_limit = current->mm->mmap_base;
  91. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  92. info.align_offset = 0;
  93. addr = vm_unmapped_area(&info);
  94. /*
  95. * A failed mmap() very likely causes application failure,
  96. * so fall back to the bottom-up function here. This scenario
  97. * can happen with large stack limits and large mmap()
  98. * allocations.
  99. */
  100. if (addr & ~PAGE_MASK) {
  101. VM_BUG_ON(addr != -ENOMEM);
  102. info.flags = 0;
  103. info.low_limit = TASK_UNMAPPED_BASE;
  104. info.high_limit = TASK_SIZE;
  105. addr = vm_unmapped_area(&info);
  106. }
  107. return addr;
  108. }
  109. unsigned long
  110. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  111. unsigned long len, unsigned long pgoff, unsigned long flags)
  112. {
  113. struct hstate *h = hstate_file(file);
  114. struct mm_struct *mm = current->mm;
  115. struct vm_area_struct *vma;
  116. if (len & ~huge_page_mask(h))
  117. return -EINVAL;
  118. if (len > TASK_SIZE)
  119. return -ENOMEM;
  120. if (flags & MAP_FIXED) {
  121. if (prepare_hugepage_range(file, addr, len))
  122. return -EINVAL;
  123. return addr;
  124. }
  125. if (addr) {
  126. addr = ALIGN(addr, huge_page_size(h));
  127. vma = find_vma(mm, addr);
  128. if (TASK_SIZE - len >= addr &&
  129. (!vma || addr + len <= vma->vm_start))
  130. return addr;
  131. }
  132. if (mm->get_unmapped_area == arch_get_unmapped_area)
  133. return hugetlb_get_unmapped_area_bottomup(file, addr, len,
  134. pgoff, flags);
  135. else
  136. return hugetlb_get_unmapped_area_topdown(file, addr, len,
  137. pgoff, flags);
  138. }
  139. #endif /* CONFIG_HUGETLB_PAGE */
  140. #ifdef CONFIG_X86_64
  141. static __init int setup_hugepagesz(char *opt)
  142. {
  143. unsigned long ps = memparse(opt, &opt);
  144. if (ps == PMD_SIZE) {
  145. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  146. } else if (ps == PUD_SIZE && cpu_has_gbpages) {
  147. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  148. } else {
  149. printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
  150. ps >> 20);
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. __setup("hugepagesz=", setup_hugepagesz);
  156. #endif