hugetlb.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #ifndef _LINUX_HUGETLB_H
  2. #define _LINUX_HUGETLB_H
  3. #include <linux/mm_types.h>
  4. #include <linux/mmdebug.h>
  5. #include <linux/fs.h>
  6. #include <linux/hugetlb_inline.h>
  7. #include <linux/cgroup.h>
  8. #include <linux/list.h>
  9. #include <linux/kref.h>
  10. struct ctl_table;
  11. struct user_struct;
  12. struct mmu_gather;
  13. #ifdef CONFIG_HUGETLB_PAGE
  14. #include <linux/mempolicy.h>
  15. #include <linux/shm.h>
  16. #include <asm/tlbflush.h>
  17. struct hugepage_subpool {
  18. spinlock_t lock;
  19. long count;
  20. long max_hpages; /* Maximum huge pages or -1 if no maximum. */
  21. long used_hpages; /* Used count against maximum, includes */
  22. /* both alloced and reserved pages. */
  23. struct hstate *hstate;
  24. long min_hpages; /* Minimum huge pages or -1 if no minimum. */
  25. long rsv_hpages; /* Pages reserved against global pool to */
  26. /* sasitfy minimum size. */
  27. };
  28. struct resv_map {
  29. struct kref refs;
  30. spinlock_t lock;
  31. struct list_head regions;
  32. long adds_in_progress;
  33. struct list_head region_cache;
  34. long region_cache_count;
  35. };
  36. extern struct resv_map *resv_map_alloc(void);
  37. void resv_map_release(struct kref *ref);
  38. extern spinlock_t hugetlb_lock;
  39. extern int hugetlb_max_hstate __read_mostly;
  40. #define for_each_hstate(h) \
  41. for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++)
  42. struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
  43. long min_hpages);
  44. void hugepage_put_subpool(struct hugepage_subpool *spool);
  45. void reset_vma_resv_huge_pages(struct vm_area_struct *vma);
  46. int hugetlb_sysctl_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);
  47. int hugetlb_overcommit_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);
  48. int hugetlb_treat_movable_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);
  49. #ifdef CONFIG_NUMA
  50. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int,
  51. void __user *, size_t *, loff_t *);
  52. #endif
  53. int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *, struct vm_area_struct *);
  54. long follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *,
  55. struct page **, struct vm_area_struct **,
  56. unsigned long *, unsigned long *, long, unsigned int);
  57. void unmap_hugepage_range(struct vm_area_struct *,
  58. unsigned long, unsigned long, struct page *);
  59. void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  60. struct vm_area_struct *vma,
  61. unsigned long start, unsigned long end,
  62. struct page *ref_page);
  63. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  64. unsigned long start, unsigned long end,
  65. struct page *ref_page);
  66. void hugetlb_report_meminfo(struct seq_file *);
  67. int hugetlb_report_node_meminfo(int, char *);
  68. void hugetlb_show_meminfo(void);
  69. unsigned long hugetlb_total_pages(void);
  70. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  71. unsigned long address, unsigned int flags);
  72. int hugetlb_reserve_pages(struct inode *inode, long from, long to,
  73. struct vm_area_struct *vma,
  74. vm_flags_t vm_flags);
  75. long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
  76. long freed);
  77. int dequeue_hwpoisoned_huge_page(struct page *page);
  78. bool isolate_huge_page(struct page *page, struct list_head *list);
  79. void putback_active_hugepage(struct page *page);
  80. void free_huge_page(struct page *page);
  81. void hugetlb_fix_reserve_counts(struct inode *inode, bool restore_reserve);
  82. extern struct mutex *hugetlb_fault_mutex_table;
  83. u32 hugetlb_fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
  84. struct vm_area_struct *vma,
  85. struct address_space *mapping,
  86. pgoff_t idx, unsigned long address);
  87. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud);
  88. extern int hugepages_treat_as_movable;
  89. extern int sysctl_hugetlb_shm_group;
  90. extern struct list_head huge_boot_pages;
  91. /* arch callbacks */
  92. pte_t *huge_pte_alloc(struct mm_struct *mm,
  93. unsigned long addr, unsigned long sz);
  94. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr);
  95. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep);
  96. struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address,
  97. int write);
  98. struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  99. pmd_t *pmd, int flags);
  100. struct page *follow_huge_pud(struct mm_struct *mm, unsigned long address,
  101. pud_t *pud, int flags);
  102. int pmd_huge(pmd_t pmd);
  103. int pud_huge(pud_t pmd);
  104. unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  105. unsigned long address, unsigned long end, pgprot_t newprot);
  106. #else /* !CONFIG_HUGETLB_PAGE */
  107. static inline void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  108. {
  109. }
  110. static inline unsigned long hugetlb_total_pages(void)
  111. {
  112. return 0;
  113. }
  114. #define follow_hugetlb_page(m,v,p,vs,a,b,i,w) ({ BUG(); 0; })
  115. #define follow_huge_addr(mm, addr, write) ERR_PTR(-EINVAL)
  116. #define copy_hugetlb_page_range(src, dst, vma) ({ BUG(); 0; })
  117. static inline void hugetlb_report_meminfo(struct seq_file *m)
  118. {
  119. }
  120. #define hugetlb_report_node_meminfo(n, buf) 0
  121. static inline void hugetlb_show_meminfo(void)
  122. {
  123. }
  124. #define follow_huge_pmd(mm, addr, pmd, flags) NULL
  125. #define follow_huge_pud(mm, addr, pud, flags) NULL
  126. #define prepare_hugepage_range(file, addr, len) (-EINVAL)
  127. #define pmd_huge(x) 0
  128. #define pud_huge(x) 0
  129. #define is_hugepage_only_range(mm, addr, len) 0
  130. #define hugetlb_free_pgd_range(tlb, addr, end, floor, ceiling) ({BUG(); 0; })
  131. #define hugetlb_fault(mm, vma, addr, flags) ({ BUG(); 0; })
  132. #define huge_pte_offset(mm, address) 0
  133. static inline int dequeue_hwpoisoned_huge_page(struct page *page)
  134. {
  135. return 0;
  136. }
  137. static inline bool isolate_huge_page(struct page *page, struct list_head *list)
  138. {
  139. return false;
  140. }
  141. #define putback_active_hugepage(p) do {} while (0)
  142. static inline unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  143. unsigned long address, unsigned long end, pgprot_t newprot)
  144. {
  145. return 0;
  146. }
  147. static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  148. struct vm_area_struct *vma, unsigned long start,
  149. unsigned long end, struct page *ref_page)
  150. {
  151. BUG();
  152. }
  153. static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
  154. struct vm_area_struct *vma, unsigned long start,
  155. unsigned long end, struct page *ref_page)
  156. {
  157. BUG();
  158. }
  159. #endif /* !CONFIG_HUGETLB_PAGE */
  160. /*
  161. * hugepages at page global directory. If arch support
  162. * hugepages at pgd level, they need to define this.
  163. */
  164. #ifndef pgd_huge
  165. #define pgd_huge(x) 0
  166. #endif
  167. #ifndef pgd_write
  168. static inline int pgd_write(pgd_t pgd)
  169. {
  170. BUG();
  171. return 0;
  172. }
  173. #endif
  174. #ifndef pud_write
  175. static inline int pud_write(pud_t pud)
  176. {
  177. BUG();
  178. return 0;
  179. }
  180. #endif
  181. #ifndef is_hugepd
  182. /*
  183. * Some architectures requires a hugepage directory format that is
  184. * required to support multiple hugepage sizes. For example
  185. * a4fe3ce76 "powerpc/mm: Allow more flexible layouts for hugepage pagetables"
  186. * introduced the same on powerpc. This allows for a more flexible hugepage
  187. * pagetable layout.
  188. */
  189. typedef struct { unsigned long pd; } hugepd_t;
  190. #define is_hugepd(hugepd) (0)
  191. #define __hugepd(x) ((hugepd_t) { (x) })
  192. static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
  193. unsigned pdshift, unsigned long end,
  194. int write, struct page **pages, int *nr)
  195. {
  196. return 0;
  197. }
  198. #else
  199. extern int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
  200. unsigned pdshift, unsigned long end,
  201. int write, struct page **pages, int *nr);
  202. #endif
  203. #define HUGETLB_ANON_FILE "anon_hugepage"
  204. enum {
  205. /*
  206. * The file will be used as an shm file so shmfs accounting rules
  207. * apply
  208. */
  209. HUGETLB_SHMFS_INODE = 1,
  210. /*
  211. * The file is being created on the internal vfs mount and shmfs
  212. * accounting rules do not apply
  213. */
  214. HUGETLB_ANONHUGE_INODE = 2,
  215. };
  216. #ifdef CONFIG_HUGETLBFS
  217. struct hugetlbfs_sb_info {
  218. long max_inodes; /* inodes allowed */
  219. long free_inodes; /* inodes free */
  220. spinlock_t stat_lock;
  221. struct hstate *hstate;
  222. struct hugepage_subpool *spool;
  223. };
  224. static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
  225. {
  226. return sb->s_fs_info;
  227. }
  228. extern const struct file_operations hugetlbfs_file_operations;
  229. extern const struct vm_operations_struct hugetlb_vm_ops;
  230. struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
  231. struct user_struct **user, int creat_flags,
  232. int page_size_log);
  233. static inline int is_file_hugepages(struct file *file)
  234. {
  235. if (file->f_op == &hugetlbfs_file_operations)
  236. return 1;
  237. if (is_file_shm_hugepages(file))
  238. return 1;
  239. return 0;
  240. }
  241. #else /* !CONFIG_HUGETLBFS */
  242. #define is_file_hugepages(file) 0
  243. static inline struct file *
  244. hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
  245. struct user_struct **user, int creat_flags,
  246. int page_size_log)
  247. {
  248. return ERR_PTR(-ENOSYS);
  249. }
  250. #endif /* !CONFIG_HUGETLBFS */
  251. #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  252. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  253. unsigned long len, unsigned long pgoff,
  254. unsigned long flags);
  255. #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
  256. #ifdef CONFIG_HUGETLB_PAGE
  257. #define HSTATE_NAME_LEN 32
  258. /* Defines one hugetlb page size */
  259. struct hstate {
  260. int next_nid_to_alloc;
  261. int next_nid_to_free;
  262. unsigned int order;
  263. unsigned long mask;
  264. unsigned long max_huge_pages;
  265. unsigned long nr_huge_pages;
  266. unsigned long free_huge_pages;
  267. unsigned long resv_huge_pages;
  268. unsigned long surplus_huge_pages;
  269. unsigned long nr_overcommit_huge_pages;
  270. struct list_head hugepage_activelist;
  271. struct list_head hugepage_freelists[MAX_NUMNODES];
  272. unsigned int nr_huge_pages_node[MAX_NUMNODES];
  273. unsigned int free_huge_pages_node[MAX_NUMNODES];
  274. unsigned int surplus_huge_pages_node[MAX_NUMNODES];
  275. #ifdef CONFIG_CGROUP_HUGETLB
  276. /* cgroup control files */
  277. struct cftype cgroup_files[5];
  278. #endif
  279. char name[HSTATE_NAME_LEN];
  280. };
  281. struct huge_bootmem_page {
  282. struct list_head list;
  283. struct hstate *hstate;
  284. #ifdef CONFIG_HIGHMEM
  285. phys_addr_t phys;
  286. #endif
  287. };
  288. struct page *alloc_huge_page(struct vm_area_struct *vma,
  289. unsigned long addr, int avoid_reserve);
  290. struct page *alloc_huge_page_node(struct hstate *h, int nid);
  291. struct page *alloc_huge_page_noerr(struct vm_area_struct *vma,
  292. unsigned long addr, int avoid_reserve);
  293. int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
  294. pgoff_t idx);
  295. /* arch callback */
  296. int __init alloc_bootmem_huge_page(struct hstate *h);
  297. void __init hugetlb_add_hstate(unsigned order);
  298. struct hstate *size_to_hstate(unsigned long size);
  299. #ifndef HUGE_MAX_HSTATE
  300. #define HUGE_MAX_HSTATE 1
  301. #endif
  302. extern struct hstate hstates[HUGE_MAX_HSTATE];
  303. extern unsigned int default_hstate_idx;
  304. #define default_hstate (hstates[default_hstate_idx])
  305. static inline struct hstate *hstate_inode(struct inode *i)
  306. {
  307. struct hugetlbfs_sb_info *hsb;
  308. hsb = HUGETLBFS_SB(i->i_sb);
  309. return hsb->hstate;
  310. }
  311. static inline struct hstate *hstate_file(struct file *f)
  312. {
  313. return hstate_inode(file_inode(f));
  314. }
  315. static inline struct hstate *hstate_sizelog(int page_size_log)
  316. {
  317. if (!page_size_log)
  318. return &default_hstate;
  319. return size_to_hstate(1UL << page_size_log);
  320. }
  321. static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
  322. {
  323. return hstate_file(vma->vm_file);
  324. }
  325. static inline unsigned long huge_page_size(struct hstate *h)
  326. {
  327. return (unsigned long)PAGE_SIZE << h->order;
  328. }
  329. extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
  330. extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
  331. static inline unsigned long huge_page_mask(struct hstate *h)
  332. {
  333. return h->mask;
  334. }
  335. static inline unsigned int huge_page_order(struct hstate *h)
  336. {
  337. return h->order;
  338. }
  339. static inline unsigned huge_page_shift(struct hstate *h)
  340. {
  341. return h->order + PAGE_SHIFT;
  342. }
  343. static inline bool hstate_is_gigantic(struct hstate *h)
  344. {
  345. return huge_page_order(h) >= MAX_ORDER;
  346. }
  347. static inline unsigned int pages_per_huge_page(struct hstate *h)
  348. {
  349. return 1 << h->order;
  350. }
  351. static inline unsigned int blocks_per_huge_page(struct hstate *h)
  352. {
  353. return huge_page_size(h) / 512;
  354. }
  355. #include <asm/hugetlb.h>
  356. #ifndef arch_make_huge_pte
  357. static inline pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
  358. struct page *page, int writable)
  359. {
  360. return entry;
  361. }
  362. #endif
  363. static inline struct hstate *page_hstate(struct page *page)
  364. {
  365. VM_BUG_ON_PAGE(!PageHuge(page), page);
  366. return size_to_hstate(PAGE_SIZE << compound_order(page));
  367. }
  368. static inline unsigned hstate_index_to_shift(unsigned index)
  369. {
  370. return hstates[index].order + PAGE_SHIFT;
  371. }
  372. static inline int hstate_index(struct hstate *h)
  373. {
  374. return h - hstates;
  375. }
  376. pgoff_t __basepage_index(struct page *page);
  377. /* Return page->index in PAGE_SIZE units */
  378. static inline pgoff_t basepage_index(struct page *page)
  379. {
  380. if (!PageCompound(page))
  381. return page->index;
  382. return __basepage_index(page);
  383. }
  384. extern void dissolve_free_huge_pages(unsigned long start_pfn,
  385. unsigned long end_pfn);
  386. static inline int hugepage_migration_supported(struct hstate *h)
  387. {
  388. #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
  389. return huge_page_shift(h) == PMD_SHIFT;
  390. #else
  391. return 0;
  392. #endif
  393. }
  394. static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
  395. struct mm_struct *mm, pte_t *pte)
  396. {
  397. if (huge_page_size(h) == PMD_SIZE)
  398. return pmd_lockptr(mm, (pmd_t *) pte);
  399. VM_BUG_ON(huge_page_size(h) == PAGE_SIZE);
  400. return &mm->page_table_lock;
  401. }
  402. #ifndef hugepages_supported
  403. /*
  404. * Some platform decide whether they support huge pages at boot
  405. * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0
  406. * when there is no such support
  407. */
  408. #define hugepages_supported() (HPAGE_SHIFT != 0)
  409. #endif
  410. void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
  411. static inline void hugetlb_count_add(long l, struct mm_struct *mm)
  412. {
  413. atomic_long_add(l, &mm->hugetlb_usage);
  414. }
  415. static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
  416. {
  417. atomic_long_sub(l, &mm->hugetlb_usage);
  418. }
  419. #else /* CONFIG_HUGETLB_PAGE */
  420. struct hstate {};
  421. #define alloc_huge_page(v, a, r) NULL
  422. #define alloc_huge_page_node(h, nid) NULL
  423. #define alloc_huge_page_noerr(v, a, r) NULL
  424. #define alloc_bootmem_huge_page(h) NULL
  425. #define hstate_file(f) NULL
  426. #define hstate_sizelog(s) NULL
  427. #define hstate_vma(v) NULL
  428. #define hstate_inode(i) NULL
  429. #define page_hstate(page) NULL
  430. #define huge_page_size(h) PAGE_SIZE
  431. #define huge_page_mask(h) PAGE_MASK
  432. #define vma_kernel_pagesize(v) PAGE_SIZE
  433. #define vma_mmu_pagesize(v) PAGE_SIZE
  434. #define huge_page_order(h) 0
  435. #define huge_page_shift(h) PAGE_SHIFT
  436. static inline unsigned int pages_per_huge_page(struct hstate *h)
  437. {
  438. return 1;
  439. }
  440. #define hstate_index_to_shift(index) 0
  441. #define hstate_index(h) 0
  442. static inline pgoff_t basepage_index(struct page *page)
  443. {
  444. return page->index;
  445. }
  446. #define dissolve_free_huge_pages(s, e) do {} while (0)
  447. #define hugepage_migration_supported(h) 0
  448. static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
  449. struct mm_struct *mm, pte_t *pte)
  450. {
  451. return &mm->page_table_lock;
  452. }
  453. static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
  454. {
  455. }
  456. static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
  457. {
  458. }
  459. #endif /* CONFIG_HUGETLB_PAGE */
  460. static inline spinlock_t *huge_pte_lock(struct hstate *h,
  461. struct mm_struct *mm, pte_t *pte)
  462. {
  463. spinlock_t *ptl;
  464. ptl = huge_pte_lockptr(h, mm, pte);
  465. spin_lock(ptl);
  466. return ptl;
  467. }
  468. #endif /* _LINUX_HUGETLB_H */