]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _LINUX_HUGETLB_H |
3 | #define _LINUX_HUGETLB_H | |
4 | ||
be93d8cf | 5 | #include <linux/mm_types.h> |
309381fe | 6 | #include <linux/mmdebug.h> |
4e950f6f | 7 | #include <linux/fs.h> |
8edf344c | 8 | #include <linux/hugetlb_inline.h> |
abb8206c | 9 | #include <linux/cgroup.h> |
9119a41e JK |
10 | #include <linux/list.h> |
11 | #include <linux/kref.h> | |
ca5999fd | 12 | #include <linux/pgtable.h> |
d92bbc27 | 13 | #include <linux/gfp.h> |
4e950f6f | 14 | |
e9ea0e2d AM |
15 | struct ctl_table; |
16 | struct user_struct; | |
24669e58 | 17 | struct mmu_gather; |
e9ea0e2d | 18 | |
e2299292 | 19 | #ifndef is_hugepd |
e2299292 AK |
20 | typedef struct { unsigned long pd; } hugepd_t; |
21 | #define is_hugepd(hugepd) (0) | |
22 | #define __hugepd(x) ((hugepd_t) { (x) }) | |
e2299292 AK |
23 | #endif |
24 | ||
1da177e4 LT |
25 | #ifdef CONFIG_HUGETLB_PAGE |
26 | ||
27 | #include <linux/mempolicy.h> | |
516dffdc | 28 | #include <linux/shm.h> |
63551ae0 | 29 | #include <asm/tlbflush.h> |
1da177e4 | 30 | |
90481622 DG |
31 | struct hugepage_subpool { |
32 | spinlock_t lock; | |
33 | long count; | |
c6a91820 MK |
34 | long max_hpages; /* Maximum huge pages or -1 if no maximum. */ |
35 | long used_hpages; /* Used count against maximum, includes */ | |
36 | /* both alloced and reserved pages. */ | |
37 | struct hstate *hstate; | |
38 | long min_hpages; /* Minimum huge pages or -1 if no minimum. */ | |
39 | long rsv_hpages; /* Pages reserved against global pool to */ | |
6c26d310 | 40 | /* satisfy minimum size. */ |
90481622 DG |
41 | }; |
42 | ||
9119a41e JK |
43 | struct resv_map { |
44 | struct kref refs; | |
7b24d861 | 45 | spinlock_t lock; |
9119a41e | 46 | struct list_head regions; |
5e911373 MK |
47 | long adds_in_progress; |
48 | struct list_head region_cache; | |
49 | long region_cache_count; | |
e9fe92ae MA |
50 | #ifdef CONFIG_CGROUP_HUGETLB |
51 | /* | |
52 | * On private mappings, the counter to uncharge reservations is stored | |
53 | * here. If these fields are 0, then either the mapping is shared, or | |
54 | * cgroup accounting is disabled for this resv_map. | |
55 | */ | |
56 | struct page_counter *reservation_counter; | |
57 | unsigned long pages_per_hpage; | |
58 | struct cgroup_subsys_state *css; | |
59 | #endif | |
9119a41e | 60 | }; |
075a61d0 MA |
61 | |
62 | /* | |
63 | * Region tracking -- allows tracking of reservations and instantiated pages | |
64 | * across the pages in a mapping. | |
65 | * | |
66 | * The region data structures are embedded into a resv_map and protected | |
67 | * by a resv_map's lock. The set of regions within the resv_map represent | |
68 | * reservations for huge pages, or huge pages that have already been | |
69 | * instantiated within the map. The from and to elements are huge page | |
70 | * indicies into the associated mapping. from indicates the starting index | |
71 | * of the region. to represents the first index past the end of the region. | |
72 | * | |
73 | * For example, a file region structure with from == 0 and to == 4 represents | |
74 | * four huge pages in a mapping. It is important to note that the to element | |
75 | * represents the first element past the end of the region. This is used in | |
76 | * arithmetic as 4(to) - 0(from) = 4 huge pages in the region. | |
77 | * | |
78 | * Interval notation of the form [from, to) will be used to indicate that | |
79 | * the endpoint from is inclusive and to is exclusive. | |
80 | */ | |
81 | struct file_region { | |
82 | struct list_head link; | |
83 | long from; | |
84 | long to; | |
85 | #ifdef CONFIG_CGROUP_HUGETLB | |
86 | /* | |
87 | * On shared mappings, each reserved region appears as a struct | |
88 | * file_region in resv_map. These fields hold the info needed to | |
89 | * uncharge each reservation. | |
90 | */ | |
91 | struct page_counter *reservation_counter; | |
92 | struct cgroup_subsys_state *css; | |
93 | #endif | |
94 | }; | |
95 | ||
9119a41e JK |
96 | extern struct resv_map *resv_map_alloc(void); |
97 | void resv_map_release(struct kref *ref); | |
98 | ||
c3f38a38 AK |
99 | extern spinlock_t hugetlb_lock; |
100 | extern int hugetlb_max_hstate __read_mostly; | |
101 | #define for_each_hstate(h) \ | |
102 | for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++) | |
103 | ||
7ca02d0a MK |
104 | struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages, |
105 | long min_hpages); | |
90481622 DG |
106 | void hugepage_put_subpool(struct hugepage_subpool *spool); |
107 | ||
a1e78772 | 108 | void reset_vma_resv_huge_pages(struct vm_area_struct *vma); |
32927393 CH |
109 | int hugetlb_sysctl_handler(struct ctl_table *, int, void *, size_t *, loff_t *); |
110 | int hugetlb_overcommit_handler(struct ctl_table *, int, void *, size_t *, | |
111 | loff_t *); | |
112 | int hugetlb_treat_movable_handler(struct ctl_table *, int, void *, size_t *, | |
113 | loff_t *); | |
114 | int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int, void *, size_t *, | |
115 | loff_t *); | |
06808b08 | 116 | |
1da177e4 | 117 | int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *, struct vm_area_struct *); |
28a35716 ML |
118 | long follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *, |
119 | struct page **, struct vm_area_struct **, | |
87ffc118 AA |
120 | unsigned long *, unsigned long *, long, unsigned int, |
121 | int *); | |
04f2cbe3 | 122 | void unmap_hugepage_range(struct vm_area_struct *, |
24669e58 | 123 | unsigned long, unsigned long, struct page *); |
d833352a MG |
124 | void __unmap_hugepage_range_final(struct mmu_gather *tlb, |
125 | struct vm_area_struct *vma, | |
126 | unsigned long start, unsigned long end, | |
127 | struct page *ref_page); | |
24669e58 AK |
128 | void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, |
129 | unsigned long start, unsigned long end, | |
130 | struct page *ref_page); | |
e1759c21 | 131 | void hugetlb_report_meminfo(struct seq_file *); |
7981593b | 132 | int hugetlb_report_node_meminfo(char *buf, int len, int nid); |
949f7ec5 | 133 | void hugetlb_show_meminfo(void); |
1da177e4 | 134 | unsigned long hugetlb_total_pages(void); |
2b740303 | 135 | vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
788c7df4 | 136 | unsigned long address, unsigned int flags); |
8fb5debc MK |
137 | int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte, |
138 | struct vm_area_struct *dst_vma, | |
139 | unsigned long dst_addr, | |
140 | unsigned long src_addr, | |
141 | struct page **pagep); | |
33b8f84a | 142 | bool hugetlb_reserve_pages(struct inode *inode, long from, long to, |
5a6fe125 | 143 | struct vm_area_struct *vma, |
ca16d140 | 144 | vm_flags_t vm_flags); |
b5cec28d MK |
145 | long hugetlb_unreserve_pages(struct inode *inode, long start, long end, |
146 | long freed); | |
31caf665 NH |
147 | bool isolate_huge_page(struct page *page, struct list_head *list); |
148 | void putback_active_hugepage(struct page *page); | |
ab5ac90a | 149 | void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason); |
8f1d26d0 | 150 | void free_huge_page(struct page *page); |
72e2936c | 151 | void hugetlb_fix_reserve_counts(struct inode *inode); |
c672c7f2 | 152 | extern struct mutex *hugetlb_fault_mutex_table; |
188b04a7 | 153 | u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx); |
1da177e4 | 154 | |
aec44e0f PX |
155 | pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, |
156 | unsigned long addr, pud_t *pud); | |
3212b535 | 157 | |
c0d0381a MK |
158 | struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage); |
159 | ||
1da177e4 | 160 | extern int sysctl_hugetlb_shm_group; |
53ba51d2 | 161 | extern struct list_head huge_boot_pages; |
1da177e4 | 162 | |
63551ae0 DG |
163 | /* arch callbacks */ |
164 | ||
aec44e0f | 165 | pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, |
a5516438 | 166 | unsigned long addr, unsigned long sz); |
7868a208 PA |
167 | pte_t *huge_pte_offset(struct mm_struct *mm, |
168 | unsigned long addr, unsigned long sz); | |
34ae204f MK |
169 | int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, |
170 | unsigned long *addr, pte_t *ptep); | |
017b1660 MK |
171 | void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, |
172 | unsigned long *start, unsigned long *end); | |
63551ae0 DG |
173 | struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address, |
174 | int write); | |
4dc71451 AK |
175 | struct page *follow_huge_pd(struct vm_area_struct *vma, |
176 | unsigned long address, hugepd_t hpd, | |
177 | int flags, int pdshift); | |
63551ae0 | 178 | struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
e66f17ff | 179 | pmd_t *pmd, int flags); |
ceb86879 | 180 | struct page *follow_huge_pud(struct mm_struct *mm, unsigned long address, |
e66f17ff | 181 | pud_t *pud, int flags); |
faaa5b62 AK |
182 | struct page *follow_huge_pgd(struct mm_struct *mm, unsigned long address, |
183 | pgd_t *pgd, int flags); | |
184 | ||
63551ae0 | 185 | int pmd_huge(pmd_t pmd); |
c2febafc | 186 | int pud_huge(pud_t pud); |
7da4d641 | 187 | unsigned long hugetlb_change_protection(struct vm_area_struct *vma, |
8f860591 | 188 | unsigned long address, unsigned long end, pgprot_t newprot); |
63551ae0 | 189 | |
d5ed7444 | 190 | bool is_hugetlb_entry_migration(pte_t pte); |
ab5ac90a | 191 | |
1da177e4 LT |
192 | #else /* !CONFIG_HUGETLB_PAGE */ |
193 | ||
a1e78772 MG |
194 | static inline void reset_vma_resv_huge_pages(struct vm_area_struct *vma) |
195 | { | |
196 | } | |
197 | ||
1da177e4 LT |
198 | static inline unsigned long hugetlb_total_pages(void) |
199 | { | |
200 | return 0; | |
201 | } | |
202 | ||
c0d0381a MK |
203 | static inline struct address_space *hugetlb_page_mapping_lock_write( |
204 | struct page *hpage) | |
205 | { | |
206 | return NULL; | |
207 | } | |
208 | ||
34ae204f MK |
209 | static inline int huge_pmd_unshare(struct mm_struct *mm, |
210 | struct vm_area_struct *vma, | |
211 | unsigned long *addr, pte_t *ptep) | |
017b1660 MK |
212 | { |
213 | return 0; | |
214 | } | |
215 | ||
216 | static inline void adjust_range_if_pmd_sharing_possible( | |
217 | struct vm_area_struct *vma, | |
218 | unsigned long *start, unsigned long *end) | |
219 | { | |
220 | } | |
221 | ||
1f9dccb2 MK |
222 | static inline long follow_hugetlb_page(struct mm_struct *mm, |
223 | struct vm_area_struct *vma, struct page **pages, | |
224 | struct vm_area_struct **vmas, unsigned long *position, | |
225 | unsigned long *nr_pages, long i, unsigned int flags, | |
226 | int *nonblocking) | |
227 | { | |
228 | BUG(); | |
229 | return 0; | |
230 | } | |
231 | ||
232 | static inline struct page *follow_huge_addr(struct mm_struct *mm, | |
233 | unsigned long address, int write) | |
234 | { | |
235 | return ERR_PTR(-EINVAL); | |
236 | } | |
237 | ||
238 | static inline int copy_hugetlb_page_range(struct mm_struct *dst, | |
239 | struct mm_struct *src, struct vm_area_struct *vma) | |
240 | { | |
241 | BUG(); | |
242 | return 0; | |
243 | } | |
244 | ||
e1759c21 AD |
245 | static inline void hugetlb_report_meminfo(struct seq_file *m) |
246 | { | |
247 | } | |
1f9dccb2 | 248 | |
7981593b | 249 | static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid) |
1f9dccb2 MK |
250 | { |
251 | return 0; | |
252 | } | |
253 | ||
949f7ec5 DR |
254 | static inline void hugetlb_show_meminfo(void) |
255 | { | |
256 | } | |
1f9dccb2 MK |
257 | |
258 | static inline struct page *follow_huge_pd(struct vm_area_struct *vma, | |
259 | unsigned long address, hugepd_t hpd, int flags, | |
260 | int pdshift) | |
261 | { | |
262 | return NULL; | |
263 | } | |
264 | ||
265 | static inline struct page *follow_huge_pmd(struct mm_struct *mm, | |
266 | unsigned long address, pmd_t *pmd, int flags) | |
267 | { | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | static inline struct page *follow_huge_pud(struct mm_struct *mm, | |
272 | unsigned long address, pud_t *pud, int flags) | |
273 | { | |
274 | return NULL; | |
275 | } | |
276 | ||
277 | static inline struct page *follow_huge_pgd(struct mm_struct *mm, | |
278 | unsigned long address, pgd_t *pgd, int flags) | |
279 | { | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | static inline int prepare_hugepage_range(struct file *file, | |
284 | unsigned long addr, unsigned long len) | |
285 | { | |
286 | return -EINVAL; | |
287 | } | |
288 | ||
289 | static inline int pmd_huge(pmd_t pmd) | |
290 | { | |
291 | return 0; | |
292 | } | |
293 | ||
294 | static inline int pud_huge(pud_t pud) | |
295 | { | |
296 | return 0; | |
297 | } | |
298 | ||
299 | static inline int is_hugepage_only_range(struct mm_struct *mm, | |
300 | unsigned long addr, unsigned long len) | |
301 | { | |
302 | return 0; | |
303 | } | |
304 | ||
305 | static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb, | |
306 | unsigned long addr, unsigned long end, | |
307 | unsigned long floor, unsigned long ceiling) | |
308 | { | |
309 | BUG(); | |
310 | } | |
311 | ||
312 | static inline int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, | |
313 | pte_t *dst_pte, | |
314 | struct vm_area_struct *dst_vma, | |
315 | unsigned long dst_addr, | |
316 | unsigned long src_addr, | |
317 | struct page **pagep) | |
318 | { | |
319 | BUG(); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, | |
324 | unsigned long sz) | |
325 | { | |
326 | return NULL; | |
327 | } | |
24669e58 | 328 | |
f40386a4 NH |
329 | static inline bool isolate_huge_page(struct page *page, struct list_head *list) |
330 | { | |
331 | return false; | |
332 | } | |
1da177e4 | 333 | |
1f9dccb2 MK |
334 | static inline void putback_active_hugepage(struct page *page) |
335 | { | |
336 | } | |
337 | ||
338 | static inline void move_hugetlb_state(struct page *oldpage, | |
339 | struct page *newpage, int reason) | |
340 | { | |
341 | } | |
342 | ||
343 | static inline unsigned long hugetlb_change_protection( | |
344 | struct vm_area_struct *vma, unsigned long address, | |
345 | unsigned long end, pgprot_t newprot) | |
7da4d641 PZ |
346 | { |
347 | return 0; | |
348 | } | |
8f860591 | 349 | |
d833352a MG |
350 | static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb, |
351 | struct vm_area_struct *vma, unsigned long start, | |
352 | unsigned long end, struct page *ref_page) | |
353 | { | |
354 | BUG(); | |
355 | } | |
356 | ||
24669e58 AK |
357 | static inline void __unmap_hugepage_range(struct mmu_gather *tlb, |
358 | struct vm_area_struct *vma, unsigned long start, | |
359 | unsigned long end, struct page *ref_page) | |
360 | { | |
361 | BUG(); | |
362 | } | |
1f9dccb2 | 363 | |
a953e772 | 364 | static inline vm_fault_t hugetlb_fault(struct mm_struct *mm, |
1f9dccb2 MK |
365 | struct vm_area_struct *vma, unsigned long address, |
366 | unsigned int flags) | |
a953e772 SJ |
367 | { |
368 | BUG(); | |
369 | return 0; | |
370 | } | |
24669e58 | 371 | |
1da177e4 | 372 | #endif /* !CONFIG_HUGETLB_PAGE */ |
f30c59e9 AK |
373 | /* |
374 | * hugepages at page global directory. If arch support | |
375 | * hugepages at pgd level, they need to define this. | |
376 | */ | |
377 | #ifndef pgd_huge | |
378 | #define pgd_huge(x) 0 | |
379 | #endif | |
c2febafc KS |
380 | #ifndef p4d_huge |
381 | #define p4d_huge(x) 0 | |
382 | #endif | |
f30c59e9 AK |
383 | |
384 | #ifndef pgd_write | |
385 | static inline int pgd_write(pgd_t pgd) | |
386 | { | |
387 | BUG(); | |
388 | return 0; | |
389 | } | |
390 | #endif | |
391 | ||
4e52780d EM |
392 | #define HUGETLB_ANON_FILE "anon_hugepage" |
393 | ||
6bfde05b EM |
394 | enum { |
395 | /* | |
396 | * The file will be used as an shm file so shmfs accounting rules | |
397 | * apply | |
398 | */ | |
399 | HUGETLB_SHMFS_INODE = 1, | |
4e52780d EM |
400 | /* |
401 | * The file is being created on the internal vfs mount and shmfs | |
402 | * accounting rules do not apply | |
403 | */ | |
404 | HUGETLB_ANONHUGE_INODE = 2, | |
6bfde05b EM |
405 | }; |
406 | ||
1da177e4 | 407 | #ifdef CONFIG_HUGETLBFS |
1da177e4 | 408 | struct hugetlbfs_sb_info { |
1da177e4 LT |
409 | long max_inodes; /* inodes allowed */ |
410 | long free_inodes; /* inodes free */ | |
411 | spinlock_t stat_lock; | |
a137e1cc | 412 | struct hstate *hstate; |
90481622 | 413 | struct hugepage_subpool *spool; |
4a25220d DH |
414 | kuid_t uid; |
415 | kgid_t gid; | |
416 | umode_t mode; | |
1da177e4 LT |
417 | }; |
418 | ||
1da177e4 LT |
419 | static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb) |
420 | { | |
421 | return sb->s_fs_info; | |
422 | } | |
423 | ||
da14c1e5 MAL |
424 | struct hugetlbfs_inode_info { |
425 | struct shared_policy policy; | |
426 | struct inode vfs_inode; | |
ff62a342 | 427 | unsigned int seals; |
da14c1e5 MAL |
428 | }; |
429 | ||
430 | static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode) | |
431 | { | |
432 | return container_of(inode, struct hugetlbfs_inode_info, vfs_inode); | |
433 | } | |
434 | ||
4b6f5d20 | 435 | extern const struct file_operations hugetlbfs_file_operations; |
f0f37e2f | 436 | extern const struct vm_operations_struct hugetlb_vm_ops; |
af73e4d9 | 437 | struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct, |
42d7395f AK |
438 | struct user_struct **user, int creat_flags, |
439 | int page_size_log); | |
1da177e4 | 440 | |
719ff321 | 441 | static inline bool is_file_hugepages(struct file *file) |
1da177e4 | 442 | { |
516dffdc | 443 | if (file->f_op == &hugetlbfs_file_operations) |
719ff321 | 444 | return true; |
516dffdc | 445 | |
719ff321 | 446 | return is_file_shm_hugepages(file); |
1da177e4 LT |
447 | } |
448 | ||
bb297bb2 CL |
449 | static inline struct hstate *hstate_inode(struct inode *i) |
450 | { | |
451 | return HUGETLBFS_SB(i->i_sb)->hstate; | |
452 | } | |
1da177e4 LT |
453 | #else /* !CONFIG_HUGETLBFS */ |
454 | ||
719ff321 | 455 | #define is_file_hugepages(file) false |
40716e29 | 456 | static inline struct file * |
af73e4d9 NH |
457 | hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag, |
458 | struct user_struct **user, int creat_flags, | |
42d7395f | 459 | int page_size_log) |
e9ea0e2d AM |
460 | { |
461 | return ERR_PTR(-ENOSYS); | |
462 | } | |
1da177e4 | 463 | |
bb297bb2 CL |
464 | static inline struct hstate *hstate_inode(struct inode *i) |
465 | { | |
466 | return NULL; | |
467 | } | |
1da177e4 LT |
468 | #endif /* !CONFIG_HUGETLBFS */ |
469 | ||
d2ba27e8 AB |
470 | #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA |
471 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, | |
472 | unsigned long len, unsigned long pgoff, | |
473 | unsigned long flags); | |
474 | #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */ | |
475 | ||
d6995da3 MK |
476 | /* |
477 | * huegtlb page specific state flags. These flags are located in page.private | |
478 | * of the hugetlb head page. Functions created via the below macros should be | |
479 | * used to manipulate these flags. | |
480 | * | |
481 | * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at | |
482 | * allocation time. Cleared when page is fully instantiated. Free | |
483 | * routine checks flag to restore a reservation on error paths. | |
d95c0337 MK |
484 | * Synchronization: Examined or modified by code that knows it has |
485 | * the only reference to page. i.e. After allocation but before use | |
486 | * or when the page is being freed. | |
8f251a3d MK |
487 | * HPG_migratable - Set after a newly allocated page is added to the page |
488 | * cache and/or page tables. Indicates the page is a candidate for | |
489 | * migration. | |
d95c0337 MK |
490 | * Synchronization: Initially set after new page allocation with no |
491 | * locking. When examined and modified during migration processing | |
492 | * (isolate, migrate, putback) the hugetlb_lock is held. | |
9157c311 MK |
493 | * HPG_temporary - - Set on a page that is temporarily allocated from the buddy |
494 | * allocator. Typically used for migration target pages when no pages | |
495 | * are available in the pool. The hugetlb free page path will | |
496 | * immediately free pages with this flag set to the buddy allocator. | |
d95c0337 MK |
497 | * Synchronization: Can be set after huge page allocation from buddy when |
498 | * code knows it has only reference. All other examinations and | |
499 | * modifications require hugetlb_lock. | |
6c037149 | 500 | * HPG_freed - Set when page is on the free lists. |
d95c0337 | 501 | * Synchronization: hugetlb_lock held for examination and modification. |
d6995da3 MK |
502 | */ |
503 | enum hugetlb_page_flags { | |
504 | HPG_restore_reserve = 0, | |
8f251a3d | 505 | HPG_migratable, |
9157c311 | 506 | HPG_temporary, |
6c037149 | 507 | HPG_freed, |
d6995da3 MK |
508 | __NR_HPAGEFLAGS, |
509 | }; | |
510 | ||
511 | /* | |
512 | * Macros to create test, set and clear function definitions for | |
513 | * hugetlb specific page flags. | |
514 | */ | |
515 | #ifdef CONFIG_HUGETLB_PAGE | |
516 | #define TESTHPAGEFLAG(uname, flname) \ | |
517 | static inline int HPage##uname(struct page *page) \ | |
518 | { return test_bit(HPG_##flname, &(page->private)); } | |
519 | ||
520 | #define SETHPAGEFLAG(uname, flname) \ | |
521 | static inline void SetHPage##uname(struct page *page) \ | |
522 | { set_bit(HPG_##flname, &(page->private)); } | |
523 | ||
524 | #define CLEARHPAGEFLAG(uname, flname) \ | |
525 | static inline void ClearHPage##uname(struct page *page) \ | |
526 | { clear_bit(HPG_##flname, &(page->private)); } | |
527 | #else | |
528 | #define TESTHPAGEFLAG(uname, flname) \ | |
529 | static inline int HPage##uname(struct page *page) \ | |
530 | { return 0; } | |
531 | ||
532 | #define SETHPAGEFLAG(uname, flname) \ | |
533 | static inline void SetHPage##uname(struct page *page) \ | |
534 | { } | |
535 | ||
536 | #define CLEARHPAGEFLAG(uname, flname) \ | |
537 | static inline void ClearHPage##uname(struct page *page) \ | |
538 | { } | |
539 | #endif | |
540 | ||
541 | #define HPAGEFLAG(uname, flname) \ | |
542 | TESTHPAGEFLAG(uname, flname) \ | |
543 | SETHPAGEFLAG(uname, flname) \ | |
544 | CLEARHPAGEFLAG(uname, flname) \ | |
545 | ||
546 | /* | |
547 | * Create functions associated with hugetlb page flags | |
548 | */ | |
549 | HPAGEFLAG(RestoreReserve, restore_reserve) | |
8f251a3d | 550 | HPAGEFLAG(Migratable, migratable) |
9157c311 | 551 | HPAGEFLAG(Temporary, temporary) |
6c037149 | 552 | HPAGEFLAG(Freed, freed) |
d6995da3 | 553 | |
a5516438 AK |
554 | #ifdef CONFIG_HUGETLB_PAGE |
555 | ||
a3437870 | 556 | #define HSTATE_NAME_LEN 32 |
a5516438 AK |
557 | /* Defines one hugetlb page size */ |
558 | struct hstate { | |
e8c5c824 LS |
559 | int next_nid_to_alloc; |
560 | int next_nid_to_free; | |
a5516438 AK |
561 | unsigned int order; |
562 | unsigned long mask; | |
563 | unsigned long max_huge_pages; | |
564 | unsigned long nr_huge_pages; | |
565 | unsigned long free_huge_pages; | |
566 | unsigned long resv_huge_pages; | |
567 | unsigned long surplus_huge_pages; | |
568 | unsigned long nr_overcommit_huge_pages; | |
0edaecfa | 569 | struct list_head hugepage_activelist; |
a5516438 AK |
570 | struct list_head hugepage_freelists[MAX_NUMNODES]; |
571 | unsigned int nr_huge_pages_node[MAX_NUMNODES]; | |
572 | unsigned int free_huge_pages_node[MAX_NUMNODES]; | |
573 | unsigned int surplus_huge_pages_node[MAX_NUMNODES]; | |
abb8206c AK |
574 | #ifdef CONFIG_CGROUP_HUGETLB |
575 | /* cgroup control files */ | |
cdc2fcfe MA |
576 | struct cftype cgroup_files_dfl[7]; |
577 | struct cftype cgroup_files_legacy[9]; | |
abb8206c | 578 | #endif |
a3437870 | 579 | char name[HSTATE_NAME_LEN]; |
a5516438 AK |
580 | }; |
581 | ||
53ba51d2 JT |
582 | struct huge_bootmem_page { |
583 | struct list_head list; | |
584 | struct hstate *hstate; | |
585 | }; | |
586 | ||
70c3547e MK |
587 | struct page *alloc_huge_page(struct vm_area_struct *vma, |
588 | unsigned long addr, int avoid_reserve); | |
3e59fcb0 | 589 | struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid, |
d92bbc27 | 590 | nodemask_t *nmask, gfp_t gfp_mask); |
389c8178 MH |
591 | struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma, |
592 | unsigned long address); | |
ab76ad54 MK |
593 | int huge_add_to_page_cache(struct page *page, struct address_space *mapping, |
594 | pgoff_t idx); | |
bf50bab2 | 595 | |
53ba51d2 | 596 | /* arch callback */ |
e24a1307 | 597 | int __init __alloc_bootmem_huge_page(struct hstate *h); |
53ba51d2 JT |
598 | int __init alloc_bootmem_huge_page(struct hstate *h); |
599 | ||
e5ff2159 | 600 | void __init hugetlb_add_hstate(unsigned order); |
ae94da89 | 601 | bool __init arch_hugetlb_valid_size(unsigned long size); |
e5ff2159 AK |
602 | struct hstate *size_to_hstate(unsigned long size); |
603 | ||
604 | #ifndef HUGE_MAX_HSTATE | |
605 | #define HUGE_MAX_HSTATE 1 | |
606 | #endif | |
607 | ||
608 | extern struct hstate hstates[HUGE_MAX_HSTATE]; | |
609 | extern unsigned int default_hstate_idx; | |
610 | ||
611 | #define default_hstate (hstates[default_hstate_idx]) | |
a5516438 | 612 | |
d6995da3 MK |
613 | /* |
614 | * hugetlb page subpool pointer located in hpage[1].private | |
615 | */ | |
616 | static inline struct hugepage_subpool *hugetlb_page_subpool(struct page *hpage) | |
617 | { | |
618 | return (struct hugepage_subpool *)(hpage+1)->private; | |
619 | } | |
620 | ||
621 | static inline void hugetlb_set_page_subpool(struct page *hpage, | |
622 | struct hugepage_subpool *subpool) | |
623 | { | |
624 | set_page_private(hpage+1, (unsigned long)subpool); | |
625 | } | |
626 | ||
a5516438 AK |
627 | static inline struct hstate *hstate_file(struct file *f) |
628 | { | |
496ad9aa | 629 | return hstate_inode(file_inode(f)); |
a5516438 AK |
630 | } |
631 | ||
af73e4d9 NH |
632 | static inline struct hstate *hstate_sizelog(int page_size_log) |
633 | { | |
634 | if (!page_size_log) | |
635 | return &default_hstate; | |
97ad2be1 SL |
636 | |
637 | return size_to_hstate(1UL << page_size_log); | |
af73e4d9 NH |
638 | } |
639 | ||
a137e1cc | 640 | static inline struct hstate *hstate_vma(struct vm_area_struct *vma) |
a5516438 | 641 | { |
a137e1cc | 642 | return hstate_file(vma->vm_file); |
a5516438 AK |
643 | } |
644 | ||
645 | static inline unsigned long huge_page_size(struct hstate *h) | |
646 | { | |
647 | return (unsigned long)PAGE_SIZE << h->order; | |
648 | } | |
649 | ||
08fba699 MG |
650 | extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma); |
651 | ||
3340289d MG |
652 | extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma); |
653 | ||
a5516438 AK |
654 | static inline unsigned long huge_page_mask(struct hstate *h) |
655 | { | |
656 | return h->mask; | |
657 | } | |
658 | ||
659 | static inline unsigned int huge_page_order(struct hstate *h) | |
660 | { | |
661 | return h->order; | |
662 | } | |
663 | ||
664 | static inline unsigned huge_page_shift(struct hstate *h) | |
665 | { | |
666 | return h->order + PAGE_SHIFT; | |
667 | } | |
668 | ||
bae7f4ae LC |
669 | static inline bool hstate_is_gigantic(struct hstate *h) |
670 | { | |
671 | return huge_page_order(h) >= MAX_ORDER; | |
672 | } | |
673 | ||
a5516438 AK |
674 | static inline unsigned int pages_per_huge_page(struct hstate *h) |
675 | { | |
676 | return 1 << h->order; | |
677 | } | |
678 | ||
679 | static inline unsigned int blocks_per_huge_page(struct hstate *h) | |
680 | { | |
681 | return huge_page_size(h) / 512; | |
682 | } | |
683 | ||
684 | #include <asm/hugetlb.h> | |
685 | ||
b0eae98c AK |
686 | #ifndef is_hugepage_only_range |
687 | static inline int is_hugepage_only_range(struct mm_struct *mm, | |
688 | unsigned long addr, unsigned long len) | |
689 | { | |
690 | return 0; | |
691 | } | |
692 | #define is_hugepage_only_range is_hugepage_only_range | |
693 | #endif | |
694 | ||
5be99343 AK |
695 | #ifndef arch_clear_hugepage_flags |
696 | static inline void arch_clear_hugepage_flags(struct page *page) { } | |
697 | #define arch_clear_hugepage_flags arch_clear_hugepage_flags | |
698 | #endif | |
699 | ||
d9ed9faa CM |
700 | #ifndef arch_make_huge_pte |
701 | static inline pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma, | |
702 | struct page *page, int writable) | |
703 | { | |
704 | return entry; | |
705 | } | |
706 | #endif | |
707 | ||
e5ff2159 AK |
708 | static inline struct hstate *page_hstate(struct page *page) |
709 | { | |
309381fe | 710 | VM_BUG_ON_PAGE(!PageHuge(page), page); |
a50b854e | 711 | return size_to_hstate(page_size(page)); |
e5ff2159 AK |
712 | } |
713 | ||
aa50d3a7 AK |
714 | static inline unsigned hstate_index_to_shift(unsigned index) |
715 | { | |
716 | return hstates[index].order + PAGE_SHIFT; | |
717 | } | |
718 | ||
972dc4de AK |
719 | static inline int hstate_index(struct hstate *h) |
720 | { | |
721 | return h - hstates; | |
722 | } | |
723 | ||
13d60f4b ZY |
724 | pgoff_t __basepage_index(struct page *page); |
725 | ||
726 | /* Return page->index in PAGE_SIZE units */ | |
727 | static inline pgoff_t basepage_index(struct page *page) | |
728 | { | |
729 | if (!PageCompound(page)) | |
730 | return page->index; | |
731 | ||
732 | return __basepage_index(page); | |
733 | } | |
734 | ||
c3114a84 | 735 | extern int dissolve_free_huge_page(struct page *page); |
082d5b6b GS |
736 | extern int dissolve_free_huge_pages(unsigned long start_pfn, |
737 | unsigned long end_pfn); | |
e693de18 | 738 | |
c177c81e | 739 | #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION |
e693de18 AK |
740 | #ifndef arch_hugetlb_migration_supported |
741 | static inline bool arch_hugetlb_migration_supported(struct hstate *h) | |
742 | { | |
94310cbc | 743 | if ((huge_page_shift(h) == PMD_SHIFT) || |
9b553bf5 AK |
744 | (huge_page_shift(h) == PUD_SHIFT) || |
745 | (huge_page_shift(h) == PGDIR_SHIFT)) | |
94310cbc AK |
746 | return true; |
747 | else | |
748 | return false; | |
e693de18 AK |
749 | } |
750 | #endif | |
c177c81e | 751 | #else |
e693de18 AK |
752 | static inline bool arch_hugetlb_migration_supported(struct hstate *h) |
753 | { | |
d70c17d4 | 754 | return false; |
e693de18 | 755 | } |
c177c81e | 756 | #endif |
e693de18 AK |
757 | |
758 | static inline bool hugepage_migration_supported(struct hstate *h) | |
759 | { | |
760 | return arch_hugetlb_migration_supported(h); | |
83467efb | 761 | } |
c8721bbb | 762 | |
7ed2c31d AK |
763 | /* |
764 | * Movability check is different as compared to migration check. | |
765 | * It determines whether or not a huge page should be placed on | |
766 | * movable zone or not. Movability of any huge page should be | |
767 | * required only if huge page size is supported for migration. | |
768 | * There wont be any reason for the huge page to be movable if | |
769 | * it is not migratable to start with. Also the size of the huge | |
770 | * page should be large enough to be placed under a movable zone | |
771 | * and still feasible enough to be migratable. Just the presence | |
772 | * in movable zone does not make the migration feasible. | |
773 | * | |
774 | * So even though large huge page sizes like the gigantic ones | |
775 | * are migratable they should not be movable because its not | |
776 | * feasible to migrate them from movable zone. | |
777 | */ | |
778 | static inline bool hugepage_movable_supported(struct hstate *h) | |
779 | { | |
780 | if (!hugepage_migration_supported(h)) | |
781 | return false; | |
782 | ||
783 | if (hstate_is_gigantic(h)) | |
784 | return false; | |
785 | return true; | |
786 | } | |
787 | ||
d92bbc27 JK |
788 | /* Movability of hugepages depends on migration support. */ |
789 | static inline gfp_t htlb_alloc_mask(struct hstate *h) | |
790 | { | |
791 | if (hugepage_movable_supported(h)) | |
792 | return GFP_HIGHUSER_MOVABLE; | |
793 | else | |
794 | return GFP_HIGHUSER; | |
795 | } | |
796 | ||
19fc7bed JK |
797 | static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) |
798 | { | |
799 | gfp_t modified_mask = htlb_alloc_mask(h); | |
800 | ||
801 | /* Some callers might want to enforce node */ | |
802 | modified_mask |= (gfp_mask & __GFP_THISNODE); | |
803 | ||
41b4dc14 JK |
804 | modified_mask |= (gfp_mask & __GFP_NOWARN); |
805 | ||
19fc7bed JK |
806 | return modified_mask; |
807 | } | |
808 | ||
cb900f41 KS |
809 | static inline spinlock_t *huge_pte_lockptr(struct hstate *h, |
810 | struct mm_struct *mm, pte_t *pte) | |
811 | { | |
812 | if (huge_page_size(h) == PMD_SIZE) | |
813 | return pmd_lockptr(mm, (pmd_t *) pte); | |
814 | VM_BUG_ON(huge_page_size(h) == PAGE_SIZE); | |
815 | return &mm->page_table_lock; | |
816 | } | |
817 | ||
2531c8cf DD |
818 | #ifndef hugepages_supported |
819 | /* | |
820 | * Some platform decide whether they support huge pages at boot | |
821 | * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0 | |
822 | * when there is no such support | |
823 | */ | |
824 | #define hugepages_supported() (HPAGE_SHIFT != 0) | |
825 | #endif | |
457c1b27 | 826 | |
5d317b2b NH |
827 | void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm); |
828 | ||
829 | static inline void hugetlb_count_add(long l, struct mm_struct *mm) | |
830 | { | |
831 | atomic_long_add(l, &mm->hugetlb_usage); | |
832 | } | |
833 | ||
834 | static inline void hugetlb_count_sub(long l, struct mm_struct *mm) | |
835 | { | |
836 | atomic_long_sub(l, &mm->hugetlb_usage); | |
837 | } | |
e5251fd4 PA |
838 | |
839 | #ifndef set_huge_swap_pte_at | |
840 | static inline void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr, | |
841 | pte_t *ptep, pte_t pte, unsigned long sz) | |
842 | { | |
843 | set_huge_pte_at(mm, addr, ptep, pte); | |
844 | } | |
845 | #endif | |
023bdd00 AK |
846 | |
847 | #ifndef huge_ptep_modify_prot_start | |
848 | #define huge_ptep_modify_prot_start huge_ptep_modify_prot_start | |
849 | static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, | |
850 | unsigned long addr, pte_t *ptep) | |
851 | { | |
852 | return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep); | |
853 | } | |
854 | #endif | |
855 | ||
856 | #ifndef huge_ptep_modify_prot_commit | |
857 | #define huge_ptep_modify_prot_commit huge_ptep_modify_prot_commit | |
858 | static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, | |
859 | unsigned long addr, pte_t *ptep, | |
860 | pte_t old_pte, pte_t pte) | |
861 | { | |
862 | set_huge_pte_at(vma->vm_mm, addr, ptep, pte); | |
863 | } | |
864 | #endif | |
865 | ||
af73e4d9 | 866 | #else /* CONFIG_HUGETLB_PAGE */ |
a5516438 | 867 | struct hstate {}; |
442a5a9a JG |
868 | |
869 | static inline struct page *alloc_huge_page(struct vm_area_struct *vma, | |
870 | unsigned long addr, | |
871 | int avoid_reserve) | |
872 | { | |
873 | return NULL; | |
874 | } | |
875 | ||
442a5a9a | 876 | static inline struct page * |
d92bbc27 JK |
877 | alloc_huge_page_nodemask(struct hstate *h, int preferred_nid, |
878 | nodemask_t *nmask, gfp_t gfp_mask) | |
442a5a9a JG |
879 | { |
880 | return NULL; | |
881 | } | |
882 | ||
883 | static inline struct page *alloc_huge_page_vma(struct hstate *h, | |
884 | struct vm_area_struct *vma, | |
885 | unsigned long address) | |
886 | { | |
887 | return NULL; | |
888 | } | |
889 | ||
890 | static inline int __alloc_bootmem_huge_page(struct hstate *h) | |
891 | { | |
892 | return 0; | |
893 | } | |
894 | ||
895 | static inline struct hstate *hstate_file(struct file *f) | |
896 | { | |
897 | return NULL; | |
898 | } | |
899 | ||
900 | static inline struct hstate *hstate_sizelog(int page_size_log) | |
901 | { | |
902 | return NULL; | |
903 | } | |
904 | ||
905 | static inline struct hstate *hstate_vma(struct vm_area_struct *vma) | |
906 | { | |
907 | return NULL; | |
442a5a9a JG |
908 | } |
909 | ||
910 | static inline struct hstate *page_hstate(struct page *page) | |
911 | { | |
912 | return NULL; | |
913 | } | |
914 | ||
915 | static inline unsigned long huge_page_size(struct hstate *h) | |
916 | { | |
917 | return PAGE_SIZE; | |
918 | } | |
919 | ||
920 | static inline unsigned long huge_page_mask(struct hstate *h) | |
921 | { | |
922 | return PAGE_MASK; | |
923 | } | |
924 | ||
925 | static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma) | |
926 | { | |
927 | return PAGE_SIZE; | |
928 | } | |
929 | ||
930 | static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) | |
931 | { | |
932 | return PAGE_SIZE; | |
933 | } | |
934 | ||
935 | static inline unsigned int huge_page_order(struct hstate *h) | |
936 | { | |
937 | return 0; | |
938 | } | |
939 | ||
940 | static inline unsigned int huge_page_shift(struct hstate *h) | |
941 | { | |
942 | return PAGE_SHIFT; | |
943 | } | |
944 | ||
94310cbc AK |
945 | static inline bool hstate_is_gigantic(struct hstate *h) |
946 | { | |
947 | return false; | |
948 | } | |
949 | ||
510a35d4 AR |
950 | static inline unsigned int pages_per_huge_page(struct hstate *h) |
951 | { | |
952 | return 1; | |
953 | } | |
c3114a84 AK |
954 | |
955 | static inline unsigned hstate_index_to_shift(unsigned index) | |
956 | { | |
957 | return 0; | |
958 | } | |
959 | ||
960 | static inline int hstate_index(struct hstate *h) | |
961 | { | |
962 | return 0; | |
963 | } | |
13d60f4b ZY |
964 | |
965 | static inline pgoff_t basepage_index(struct page *page) | |
966 | { | |
967 | return page->index; | |
968 | } | |
c3114a84 AK |
969 | |
970 | static inline int dissolve_free_huge_page(struct page *page) | |
971 | { | |
972 | return 0; | |
973 | } | |
974 | ||
975 | static inline int dissolve_free_huge_pages(unsigned long start_pfn, | |
976 | unsigned long end_pfn) | |
977 | { | |
978 | return 0; | |
979 | } | |
980 | ||
981 | static inline bool hugepage_migration_supported(struct hstate *h) | |
982 | { | |
983 | return false; | |
984 | } | |
cb900f41 | 985 | |
7ed2c31d AK |
986 | static inline bool hugepage_movable_supported(struct hstate *h) |
987 | { | |
988 | return false; | |
989 | } | |
990 | ||
d92bbc27 JK |
991 | static inline gfp_t htlb_alloc_mask(struct hstate *h) |
992 | { | |
993 | return 0; | |
994 | } | |
995 | ||
19fc7bed JK |
996 | static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask) |
997 | { | |
998 | return 0; | |
999 | } | |
1000 | ||
cb900f41 KS |
1001 | static inline spinlock_t *huge_pte_lockptr(struct hstate *h, |
1002 | struct mm_struct *mm, pte_t *pte) | |
1003 | { | |
1004 | return &mm->page_table_lock; | |
1005 | } | |
5d317b2b NH |
1006 | |
1007 | static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m) | |
1008 | { | |
1009 | } | |
1010 | ||
1011 | static inline void hugetlb_count_sub(long l, struct mm_struct *mm) | |
1012 | { | |
1013 | } | |
e5251fd4 PA |
1014 | |
1015 | static inline void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr, | |
1016 | pte_t *ptep, pte_t pte, unsigned long sz) | |
1017 | { | |
1018 | } | |
af73e4d9 | 1019 | #endif /* CONFIG_HUGETLB_PAGE */ |
a5516438 | 1020 | |
cb900f41 KS |
1021 | static inline spinlock_t *huge_pte_lock(struct hstate *h, |
1022 | struct mm_struct *mm, pte_t *pte) | |
1023 | { | |
1024 | spinlock_t *ptl; | |
1025 | ||
1026 | ptl = huge_pte_lockptr(h, mm, pte); | |
1027 | spin_lock(ptl); | |
1028 | return ptl; | |
1029 | } | |
1030 | ||
cf11e85f RG |
1031 | #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA) |
1032 | extern void __init hugetlb_cma_reserve(int order); | |
1033 | extern void __init hugetlb_cma_check(void); | |
1034 | #else | |
1035 | static inline __init void hugetlb_cma_reserve(int order) | |
1036 | { | |
1037 | } | |
1038 | static inline __init void hugetlb_cma_check(void) | |
1039 | { | |
1040 | } | |
1041 | #endif | |
1042 | ||
c1991e07 PX |
1043 | bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr); |
1044 | ||
1da177e4 | 1045 | #endif /* _LINUX_HUGETLB_H */ |