1#ifndef _LINUX_HUGE_MM_H
2#define _LINUX_HUGE_MM_H
3
4extern int do_huge_pmd_anonymous_page(struct mm_struct *mm,
5				      struct vm_area_struct *vma,
6				      unsigned long address, pmd_t *pmd,
7				      unsigned int flags);
8extern int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
9			 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
10			 struct vm_area_struct *vma);
11extern void huge_pmd_set_accessed(struct mm_struct *mm,
12				  struct vm_area_struct *vma,
13				  unsigned long address, pmd_t *pmd,
14				  pmd_t orig_pmd, int dirty);
15extern int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
16			       unsigned long address, pmd_t *pmd,
17			       pmd_t orig_pmd);
18extern struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
19					  unsigned long addr,
20					  pmd_t *pmd,
21					  unsigned int flags);
22extern int zap_huge_pmd(struct mmu_gather *tlb,
23			struct vm_area_struct *vma,
24			pmd_t *pmd, unsigned long addr);
25extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
26			unsigned long addr, unsigned long end,
27			unsigned char *vec);
28extern int move_huge_pmd(struct vm_area_struct *vma,
29			 struct vm_area_struct *new_vma,
30			 unsigned long old_addr,
31			 unsigned long new_addr, unsigned long old_end,
32			 pmd_t *old_pmd, pmd_t *new_pmd);
33extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
34			unsigned long addr, pgprot_t newprot,
35			int prot_numa);
36int vmf_insert_pfn_pmd(struct vm_area_struct *, unsigned long addr, pmd_t *,
37			unsigned long pfn, bool write);
38
39enum transparent_hugepage_flag {
40	TRANSPARENT_HUGEPAGE_FLAG,
41	TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
42	TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
43	TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
44	TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
45	TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
46#ifdef CONFIG_DEBUG_VM
47	TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG,
48#endif
49};
50
51enum page_check_address_pmd_flag {
52	PAGE_CHECK_ADDRESS_PMD_FLAG,
53	PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG,
54	PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG,
55};
56extern pmd_t *page_check_address_pmd(struct page *page,
57				     struct mm_struct *mm,
58				     unsigned long address,
59				     enum page_check_address_pmd_flag flag,
60				     spinlock_t **ptl);
61
62#define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
63#define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
64
65#ifdef CONFIG_TRANSPARENT_HUGEPAGE
66#define HPAGE_PMD_SHIFT PMD_SHIFT
67#define HPAGE_PMD_SIZE	((1UL) << HPAGE_PMD_SHIFT)
68#define HPAGE_PMD_MASK	(~(HPAGE_PMD_SIZE - 1))
69
70extern bool is_vma_temporary_stack(struct vm_area_struct *vma);
71
72#define transparent_hugepage_enabled(__vma)				\
73	((transparent_hugepage_flags &					\
74	  (1<<TRANSPARENT_HUGEPAGE_FLAG) ||				\
75	  (transparent_hugepage_flags &					\
76	   (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG) &&			\
77	   ((__vma)->vm_flags & VM_HUGEPAGE))) &&			\
78	 !((__vma)->vm_flags & VM_NOHUGEPAGE) &&			\
79	 !is_vma_temporary_stack(__vma))
80#define transparent_hugepage_defrag(__vma)				\
81	((transparent_hugepage_flags &					\
82	  (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)) ||			\
83	 (transparent_hugepage_flags &					\
84	  (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG) &&		\
85	  (__vma)->vm_flags & VM_HUGEPAGE))
86#define transparent_hugepage_use_zero_page()				\
87	(transparent_hugepage_flags &					\
88	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
89#ifdef CONFIG_DEBUG_VM
90#define transparent_hugepage_debug_cow()				\
91	(transparent_hugepage_flags &					\
92	 (1<<TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG))
93#else /* CONFIG_DEBUG_VM */
94#define transparent_hugepage_debug_cow() 0
95#endif /* CONFIG_DEBUG_VM */
96
97extern unsigned long transparent_hugepage_flags;
98extern int split_huge_page_to_list(struct page *page, struct list_head *list);
99static inline int split_huge_page(struct page *page)
100{
101	return split_huge_page_to_list(page, NULL);
102}
103extern void __split_huge_page_pmd(struct vm_area_struct *vma,
104		unsigned long address, pmd_t *pmd);
105#define split_huge_page_pmd(__vma, __address, __pmd)			\
106	do {								\
107		pmd_t *____pmd = (__pmd);				\
108		if (unlikely(pmd_trans_huge(*____pmd)))			\
109			__split_huge_page_pmd(__vma, __address,		\
110					____pmd);			\
111	}  while (0)
112#define wait_split_huge_page(__anon_vma, __pmd)				\
113	do {								\
114		pmd_t *____pmd = (__pmd);				\
115		anon_vma_lock_write(__anon_vma);			\
116		anon_vma_unlock_write(__anon_vma);			\
117		BUG_ON(pmd_trans_splitting(*____pmd) ||			\
118		       pmd_trans_huge(*____pmd));			\
119	} while (0)
120extern void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
121		pmd_t *pmd);
122#if HPAGE_PMD_ORDER >= MAX_ORDER
123#error "hugepages can't be allocated by the buddy allocator"
124#endif
125extern int hugepage_madvise(struct vm_area_struct *vma,
126			    unsigned long *vm_flags, int advice);
127extern void vma_adjust_trans_huge(struct vm_area_struct *vma,
128				    unsigned long start,
129				    unsigned long end,
130				    long adjust_next);
131extern int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
132		spinlock_t **ptl);
133/* mmap_sem must be held on entry */
134static inline int pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
135		spinlock_t **ptl)
136{
137	VM_BUG_ON_VMA(!rwsem_is_locked(&vma->vm_mm->mmap_sem), vma);
138	if (pmd_trans_huge(*pmd))
139		return __pmd_trans_huge_lock(pmd, vma, ptl);
140	else
141		return 0;
142}
143static inline int hpage_nr_pages(struct page *page)
144{
145	if (unlikely(PageTransHuge(page)))
146		return HPAGE_PMD_NR;
147	return 1;
148}
149
150extern int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
151				unsigned long addr, pmd_t pmd, pmd_t *pmdp);
152
153extern struct page *huge_zero_page;
154
155static inline bool is_huge_zero_page(struct page *page)
156{
157	return ACCESS_ONCE(huge_zero_page) == page;
158}
159
160static inline bool is_huge_zero_pmd(pmd_t pmd)
161{
162	return is_huge_zero_page(pmd_page(pmd));
163}
164
165struct page *get_huge_zero_page(void);
166
167#else /* CONFIG_TRANSPARENT_HUGEPAGE */
168#define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
169#define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
170#define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
171
172#define hpage_nr_pages(x) 1
173
174#define transparent_hugepage_enabled(__vma) 0
175
176#define transparent_hugepage_flags 0UL
177static inline int
178split_huge_page_to_list(struct page *page, struct list_head *list)
179{
180	return 0;
181}
182static inline int split_huge_page(struct page *page)
183{
184	return 0;
185}
186#define split_huge_page_pmd(__vma, __address, __pmd)	\
187	do { } while (0)
188#define wait_split_huge_page(__anon_vma, __pmd)	\
189	do { } while (0)
190#define split_huge_page_pmd_mm(__mm, __address, __pmd)	\
191	do { } while (0)
192static inline int hugepage_madvise(struct vm_area_struct *vma,
193				   unsigned long *vm_flags, int advice)
194{
195	BUG();
196	return 0;
197}
198static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
199					 unsigned long start,
200					 unsigned long end,
201					 long adjust_next)
202{
203}
204static inline int pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
205		spinlock_t **ptl)
206{
207	return 0;
208}
209
210static inline int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
211					unsigned long addr, pmd_t pmd, pmd_t *pmdp)
212{
213	return 0;
214}
215
216static inline bool is_huge_zero_page(struct page *page)
217{
218	return false;
219}
220
221#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
222
223#endif /* _LINUX_HUGE_MM_H */
224