Lines Matching +full:trim +full:- +full:hs +full:- +full:current

1 // SPDX-License-Identifier: GPL-2.0-only
106 if (spool->count) in subpool_is_free()
108 if (spool->max_hpages != -1) in subpool_is_free()
109 return spool->used_hpages == 0; in subpool_is_free()
110 if (spool->min_hpages != -1) in subpool_is_free()
111 return spool->rsv_hpages == spool->min_hpages; in subpool_is_free()
119 spin_unlock_irqrestore(&spool->lock, irq_flags); in unlock_or_release_subpool()
125 if (spool->min_hpages != -1) in unlock_or_release_subpool()
126 hugetlb_acct_memory(spool->hstate, in unlock_or_release_subpool()
127 -spool->min_hpages); in unlock_or_release_subpool()
141 spin_lock_init(&spool->lock); in hugepage_new_subpool()
142 spool->count = 1; in hugepage_new_subpool()
143 spool->max_hpages = max_hpages; in hugepage_new_subpool()
144 spool->hstate = h; in hugepage_new_subpool()
145 spool->min_hpages = min_hpages; in hugepage_new_subpool()
147 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) { in hugepage_new_subpool()
151 spool->rsv_hpages = min_hpages; in hugepage_new_subpool()
160 spin_lock_irqsave(&spool->lock, flags); in hugepage_put_subpool()
161 BUG_ON(!spool->count); in hugepage_put_subpool()
162 spool->count--; in hugepage_put_subpool()
168 * Return -ENOMEM if there are not enough resources to satisfy the
182 spin_lock_irq(&spool->lock); in hugepage_subpool_get_pages()
184 if (spool->max_hpages != -1) { /* maximum size accounting */ in hugepage_subpool_get_pages()
185 if ((spool->used_hpages + delta) <= spool->max_hpages) in hugepage_subpool_get_pages()
186 spool->used_hpages += delta; in hugepage_subpool_get_pages()
188 ret = -ENOMEM; in hugepage_subpool_get_pages()
194 if (spool->min_hpages != -1 && spool->rsv_hpages) { in hugepage_subpool_get_pages()
195 if (delta > spool->rsv_hpages) { in hugepage_subpool_get_pages()
200 ret = delta - spool->rsv_hpages; in hugepage_subpool_get_pages()
201 spool->rsv_hpages = 0; in hugepage_subpool_get_pages()
204 spool->rsv_hpages -= delta; in hugepage_subpool_get_pages()
209 spin_unlock_irq(&spool->lock); in hugepage_subpool_get_pages()
228 spin_lock_irqsave(&spool->lock, flags); in hugepage_subpool_put_pages()
230 if (spool->max_hpages != -1) /* maximum size accounting */ in hugepage_subpool_put_pages()
231 spool->used_hpages -= delta; in hugepage_subpool_put_pages()
234 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) { in hugepage_subpool_put_pages()
235 if (spool->rsv_hpages + delta <= spool->min_hpages) in hugepage_subpool_put_pages()
238 ret = spool->rsv_hpages + delta - spool->min_hpages; in hugepage_subpool_put_pages()
240 spool->rsv_hpages += delta; in hugepage_subpool_put_pages()
241 if (spool->rsv_hpages > spool->min_hpages) in hugepage_subpool_put_pages()
242 spool->rsv_hpages = spool->min_hpages; in hugepage_subpool_put_pages()
256 return HUGETLBFS_SB(inode->i_sb)->spool; in subpool_inode()
261 return subpool_inode(file_inode(vma->vm_file)); in subpool_vma()
270 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_lock_read()
272 down_read(&vma_lock->rw_sema); in hugetlb_vma_lock_read()
276 down_read(&resv_map->rw_sema); in hugetlb_vma_lock_read()
283 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_unlock_read()
285 up_read(&vma_lock->rw_sema); in hugetlb_vma_unlock_read()
289 up_read(&resv_map->rw_sema); in hugetlb_vma_unlock_read()
296 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_lock_write()
298 down_write(&vma_lock->rw_sema); in hugetlb_vma_lock_write()
302 down_write(&resv_map->rw_sema); in hugetlb_vma_lock_write()
309 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_unlock_write()
311 up_write(&vma_lock->rw_sema); in hugetlb_vma_unlock_write()
315 up_write(&resv_map->rw_sema); in hugetlb_vma_unlock_write()
323 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_trylock_write()
325 return down_write_trylock(&vma_lock->rw_sema); in hugetlb_vma_trylock_write()
329 return down_write_trylock(&resv_map->rw_sema); in hugetlb_vma_trylock_write()
338 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_assert_locked()
340 lockdep_assert_held(&vma_lock->rw_sema); in hugetlb_vma_assert_locked()
344 lockdep_assert_held(&resv_map->rw_sema); in hugetlb_vma_assert_locked()
358 struct vm_area_struct *vma = vma_lock->vma; in __hugetlb_vma_unlock_write_put()
363 * Semaphore synchronizes access to vma_lock->vma field. in __hugetlb_vma_unlock_write_put()
365 vma_lock->vma = NULL; in __hugetlb_vma_unlock_write_put()
366 vma->vm_private_data = NULL; in __hugetlb_vma_unlock_write_put()
367 up_write(&vma_lock->rw_sema); in __hugetlb_vma_unlock_write_put()
368 kref_put(&vma_lock->refs, hugetlb_vma_lock_release); in __hugetlb_vma_unlock_write_put()
374 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in __hugetlb_vma_unlock_write_free()
381 up_write(&resv_map->rw_sema); in __hugetlb_vma_unlock_write_free()
393 if (vma->vm_private_data) { in hugetlb_vma_lock_free()
394 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vma_lock_free()
396 down_write(&vma_lock->rw_sema); in hugetlb_vma_lock_free()
406 if (!vma || !(vma->vm_flags & VM_MAYSHARE)) in hugetlb_vma_lock_alloc()
409 /* Should never get here with non-NULL vm_private_data */ in hugetlb_vma_lock_alloc()
410 if (vma->vm_private_data) in hugetlb_vma_lock_alloc()
429 kref_init(&vma_lock->refs); in hugetlb_vma_lock_alloc()
430 init_rwsem(&vma_lock->rw_sema); in hugetlb_vma_lock_alloc()
431 vma_lock->vma = vma; in hugetlb_vma_lock_alloc()
432 vma->vm_private_data = vma_lock; in hugetlb_vma_lock_alloc()
443 VM_BUG_ON(resv->region_cache_count <= 0); in get_file_region_entry_from_cache()
445 resv->region_cache_count--; in get_file_region_entry_from_cache()
446 nrg = list_first_entry(&resv->region_cache, struct file_region, link); in get_file_region_entry_from_cache()
447 list_del(&nrg->link); in get_file_region_entry_from_cache()
449 nrg->from = from; in get_file_region_entry_from_cache()
450 nrg->to = to; in get_file_region_entry_from_cache()
459 nrg->reservation_counter = rg->reservation_counter; in copy_hugetlb_cgroup_uncharge_info()
460 nrg->css = rg->css; in copy_hugetlb_cgroup_uncharge_info()
461 if (rg->css) in copy_hugetlb_cgroup_uncharge_info()
462 css_get(rg->css); in copy_hugetlb_cgroup_uncharge_info()
474 nrg->reservation_counter = in record_hugetlb_cgroup_uncharge_info()
475 &h_cg->rsvd_hugepage[hstate_index(h)]; in record_hugetlb_cgroup_uncharge_info()
476 nrg->css = &h_cg->css; in record_hugetlb_cgroup_uncharge_info()
478 * The caller will hold exactly one h_cg->css reference for the in record_hugetlb_cgroup_uncharge_info()
483 * exactly one h_cg->css reference, we should do css_get for in record_hugetlb_cgroup_uncharge_info()
487 css_get(&h_cg->css); in record_hugetlb_cgroup_uncharge_info()
488 if (!resv->pages_per_hpage) in record_hugetlb_cgroup_uncharge_info()
489 resv->pages_per_hpage = pages_per_huge_page(h); in record_hugetlb_cgroup_uncharge_info()
493 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h)); in record_hugetlb_cgroup_uncharge_info()
495 nrg->reservation_counter = NULL; in record_hugetlb_cgroup_uncharge_info()
496 nrg->css = NULL; in record_hugetlb_cgroup_uncharge_info()
504 if (rg->css) in put_uncharge_info()
505 css_put(rg->css); in put_uncharge_info()
513 return rg->reservation_counter == org->reservation_counter && in has_same_uncharge_info()
514 rg->css == org->css; in has_same_uncharge_info()
526 if (&prg->link != &resv->regions && prg->to == rg->from && in coalesce_file_region()
528 prg->to = rg->to; in coalesce_file_region()
530 list_del(&rg->link); in coalesce_file_region()
538 if (&nrg->link != &resv->regions && nrg->from == rg->to && in coalesce_file_region()
540 nrg->from = rg->from; in coalesce_file_region()
542 list_del(&rg->link); in coalesce_file_region()
558 list_add(&nrg->link, rg); in hugetlb_resv_map_add()
563 return to - from; in hugetlb_resv_map_add()
567 * Must be called with resv->lock held.
579 struct list_head *head = &resv->regions; in add_reservation_in_range()
588 * [last_accounted_offset, iter->from), at every iteration, with some in add_reservation_in_range()
593 if (iter->from < f) { in add_reservation_in_range()
597 if (iter->to > last_accounted_offset) in add_reservation_in_range()
598 last_accounted_offset = iter->to; in add_reservation_in_range()
605 if (iter->from >= t) { in add_reservation_in_range()
606 rg = iter->link.prev; in add_reservation_in_range()
610 /* Add an entry for last_accounted_offset -> iter->from, and in add_reservation_in_range()
613 if (iter->from > last_accounted_offset) in add_reservation_in_range()
614 add += hugetlb_resv_map_add(resv, iter->link.prev, in add_reservation_in_range()
616 iter->from, h, h_cg, in add_reservation_in_range()
619 last_accounted_offset = iter->to; in add_reservation_in_range()
626 rg = head->prev; in add_reservation_in_range()
634 /* Must be called with resv->lock acquired. Will drop lock to allocate entries.
638 __must_hold(&resv->lock) in allocate_file_region_entries()
655 while (resv->region_cache_count < in allocate_file_region_entries()
656 (resv->adds_in_progress + regions_needed)) { in allocate_file_region_entries()
657 to_allocate = resv->adds_in_progress + regions_needed - in allocate_file_region_entries()
658 resv->region_cache_count; in allocate_file_region_entries()
664 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress); in allocate_file_region_entries()
666 spin_unlock(&resv->lock); in allocate_file_region_entries()
671 list_add(&trg->link, &allocated_regions); in allocate_file_region_entries()
674 spin_lock(&resv->lock); in allocate_file_region_entries()
676 list_splice(&allocated_regions, &resv->region_cache); in allocate_file_region_entries()
677 resv->region_cache_count += to_allocate; in allocate_file_region_entries()
684 list_del(&rg->link); in allocate_file_region_entries()
687 return -ENOMEM; in allocate_file_region_entries()
702 * this operation and we were not able to allocate, it returns -ENOMEM.
713 spin_lock(&resv->lock); in region_add()
730 resv->region_cache_count < in region_add()
731 resv->adds_in_progress + in region_add()
732 (actual_regions_needed - in_regions_needed)) { in region_add()
736 VM_BUG_ON(t - f <= 1); in region_add()
739 resv, actual_regions_needed - in_regions_needed)) { in region_add()
740 return -ENOMEM; in region_add()
748 resv->adds_in_progress -= in_regions_needed; in region_add()
750 spin_unlock(&resv->lock); in region_add()
766 * resv->adds_in_progress. This value needs to be provided to a follow up call
771 * zero. -ENOMEM is returned if a new file_region structure or cache entry
779 spin_lock(&resv->lock); in region_chg()
789 return -ENOMEM; in region_chg()
791 resv->adds_in_progress += *out_regions_needed; in region_chg()
793 spin_unlock(&resv->lock); in region_chg()
813 spin_lock(&resv->lock); in region_abort()
814 VM_BUG_ON(!resv->region_cache_count); in region_abort()
815 resv->adds_in_progress -= regions_needed; in region_abort()
816 spin_unlock(&resv->lock); in region_abort()
823 * and either trim, delete or split the existing regions.
828 * be allocated. If the allocation fails, -ENOMEM will be returned.
830 * a region and possibly return -ENOMEM. Callers specifying
831 * t == LONG_MAX do not need to check for -ENOMEM error.
835 struct list_head *head = &resv->regions; in region_del()
841 spin_lock(&resv->lock); in region_del()
850 if (rg->to <= f && (rg->to != rg->from || rg->to != f)) in region_del()
853 if (rg->from >= t) in region_del()
856 if (f > rg->from && t < rg->to) { /* Must split region */ in region_del()
862 resv->region_cache_count > resv->adds_in_progress) { in region_del()
863 nrg = list_first_entry(&resv->region_cache, in region_del()
866 list_del(&nrg->link); in region_del()
867 resv->region_cache_count--; in region_del()
871 spin_unlock(&resv->lock); in region_del()
874 return -ENOMEM; in region_del()
878 del += t - f; in region_del()
880 resv, rg, t - f, false); in region_del()
883 nrg->from = t; in region_del()
884 nrg->to = rg->to; in region_del()
888 INIT_LIST_HEAD(&nrg->link); in region_del()
891 rg->to = f; in region_del()
893 list_add(&nrg->link, &rg->link); in region_del()
898 if (f <= rg->from && t >= rg->to) { /* Remove entire region */ in region_del()
899 del += rg->to - rg->from; in region_del()
901 rg->to - rg->from, true); in region_del()
902 list_del(&rg->link); in region_del()
907 if (f <= rg->from) { /* Trim beginning of region */ in region_del()
909 t - rg->from, false); in region_del()
911 del += t - rg->from; in region_del()
912 rg->from = t; in region_del()
913 } else { /* Trim end of region */ in region_del()
915 rg->to - f, false); in region_del()
917 del += rg->to - f; in region_del()
918 rg->to = f; in region_del()
922 spin_unlock(&resv->lock); in region_del()
962 struct list_head *head = &resv->regions; in region_count()
966 spin_lock(&resv->lock); in region_count()
972 if (rg->to <= f) in region_count()
974 if (rg->from >= t) in region_count()
977 seg_from = max(rg->from, f); in region_count()
978 seg_to = min(rg->to, t); in region_count()
980 chg += seg_to - seg_from; in region_count()
982 spin_unlock(&resv->lock); in region_count()
994 return ((address - vma->vm_start) >> huge_page_shift(h)) + in vma_hugecache_offset()
995 (vma->vm_pgoff >> huge_page_order(h)); in vma_hugecache_offset()
999 * vma_kernel_pagesize - Page size granularity for this VMA.
1009 if (vma->vm_ops && vma->vm_ops->pagesize) in vma_kernel_pagesize()
1010 return vma->vm_ops->pagesize(vma); in vma_kernel_pagesize()
1018 * architectures where it differs, an architecture-specific 'strong'
1056 return (unsigned long)vma->vm_private_data; in get_vma_private_data()
1062 vma->vm_private_data = (void *)value; in set_vma_private_data()
1072 resv_map->reservation_counter = NULL; in resv_map_set_hugetlb_cgroup_uncharge_info()
1073 resv_map->pages_per_hpage = 0; in resv_map_set_hugetlb_cgroup_uncharge_info()
1074 resv_map->css = NULL; in resv_map_set_hugetlb_cgroup_uncharge_info()
1076 resv_map->reservation_counter = in resv_map_set_hugetlb_cgroup_uncharge_info()
1077 &h_cg->rsvd_hugepage[hstate_index(h)]; in resv_map_set_hugetlb_cgroup_uncharge_info()
1078 resv_map->pages_per_hpage = pages_per_huge_page(h); in resv_map_set_hugetlb_cgroup_uncharge_info()
1079 resv_map->css = &h_cg->css; in resv_map_set_hugetlb_cgroup_uncharge_info()
1095 kref_init(&resv_map->refs); in resv_map_alloc()
1096 spin_lock_init(&resv_map->lock); in resv_map_alloc()
1097 INIT_LIST_HEAD(&resv_map->regions); in resv_map_alloc()
1098 init_rwsem(&resv_map->rw_sema); in resv_map_alloc()
1100 resv_map->adds_in_progress = 0; in resv_map_alloc()
1104 * re-initialized to the proper values, to indicate that hugetlb cgroup in resv_map_alloc()
1105 * reservations are to be un-charged from here. in resv_map_alloc()
1109 INIT_LIST_HEAD(&resv_map->region_cache); in resv_map_alloc()
1110 list_add(&rg->link, &resv_map->region_cache); in resv_map_alloc()
1111 resv_map->region_cache_count = 1; in resv_map_alloc()
1119 struct list_head *head = &resv_map->region_cache; in resv_map_release()
1127 list_del(&rg->link); in resv_map_release()
1131 VM_BUG_ON(resv_map->adds_in_progress); in resv_map_release()
1143 * The VERY common case is inode->mapping == &inode->i_data but, in inode_resv_map()
1146 return (struct resv_map *)(&inode->i_data)->i_private_data; in inode_resv_map()
1152 if (vma->vm_flags & VM_MAYSHARE) { in vma_resv_map()
1153 struct address_space *mapping = vma->vm_file->f_mapping; in vma_resv_map()
1154 struct inode *inode = mapping->host; in vma_resv_map()
1167 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); in set_vma_resv_map()
1175 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma); in set_vma_resv_flags()
1189 return !(vma->vm_flags & VM_MAYSHARE) && in __vma_private_lock()
1199 * - For shared mappings this is a per-vma semaphore that may be in hugetlb_dup_vma_private()
1205 * - For MAP_PRIVATE mappings, this is the reserve map which does in hugetlb_dup_vma_private()
1207 * not guaranteed to succeed, even if read-only. in hugetlb_dup_vma_private()
1209 if (vma->vm_flags & VM_MAYSHARE) { in hugetlb_dup_vma_private()
1210 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_dup_vma_private()
1212 if (vma_lock && vma_lock->vma != vma) in hugetlb_dup_vma_private()
1213 vma->vm_private_data = NULL; in hugetlb_dup_vma_private()
1215 vma->vm_private_data = NULL; in hugetlb_dup_vma_private()
1220 * Called with mm->mmap_lock writer semaphore held.
1243 kref_put(&reservations->refs, resv_map_release); in clear_vma_resv_huge_pages()
1252 if (vma->vm_flags & VM_NORESERVE) { in vma_has_reserves()
1260 * properly, so add work-around here. in vma_has_reserves()
1262 if (vma->vm_flags & VM_MAYSHARE && chg == 0) in vma_has_reserves()
1269 if (vma->vm_flags & VM_MAYSHARE) { in vma_has_reserves()
1293 * Very Subtle - The value of chg comes from a previous in vma_has_reserves()
1319 list_move(&folio->lru, &h->hugepage_freelists[nid]); in enqueue_hugetlb_folio()
1320 h->free_huge_pages++; in enqueue_hugetlb_folio()
1321 h->free_huge_pages_node[nid]++; in enqueue_hugetlb_folio()
1329 bool pin = !!(current->flags & PF_MEMALLOC_PIN); in dequeue_hugetlb_folio_node_exact()
1332 list_for_each_entry(folio, &h->hugepage_freelists[nid], lru) { in dequeue_hugetlb_folio_node_exact()
1339 list_move(&folio->lru, &h->hugepage_activelist); in dequeue_hugetlb_folio_node_exact()
1342 h->free_huge_pages--; in dequeue_hugetlb_folio_node_exact()
1343 h->free_huge_pages_node[nid]--; in dequeue_hugetlb_folio_node_exact()
1392 return h->free_huge_pages - h->resv_huge_pages; in available_huge_pages()
1435 h->resv_huge_pages--; in dequeue_hugetlb_folio_vma()
1448 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1487 * helper for remove_pool_hugetlb_folio() - return the previously saved
1498 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed); in hstate_next_node_to_free()
1499 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed); in hstate_next_node_to_free()
1508 nr_nodes--)
1510 #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \ argument
1513 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1514 nr_nodes--)
1602 list_del(&folio->lru); in remove_hugetlb_folio()
1606 h->free_huge_pages--; in remove_hugetlb_folio()
1607 h->free_huge_pages_node[nid]--; in remove_hugetlb_folio()
1610 h->surplus_huge_pages--; in remove_hugetlb_folio()
1611 h->surplus_huge_pages_node[nid]--; in remove_hugetlb_folio()
1622 h->nr_huge_pages--; in remove_hugetlb_folio()
1623 h->nr_huge_pages_node[nid]--; in remove_hugetlb_folio()
1635 INIT_LIST_HEAD(&folio->lru); in add_hugetlb_folio()
1636 h->nr_huge_pages++; in add_hugetlb_folio()
1637 h->nr_huge_pages_node[nid]++; in add_hugetlb_folio()
1640 h->surplus_huge_pages++; in add_hugetlb_folio()
1641 h->surplus_huge_pages_node[nid]++; in add_hugetlb_folio()
1707 INIT_LIST_HEAD(&folio->_deferred_list); in __update_and_free_hugetlb_folio()
1718 * freed and frees them one-by-one. As the page->mapping pointer is going
1736 node = node->next; in free_hpage_workfn()
1737 folio->mapping = NULL; in free_hpage_workfn()
1774 if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist)) in update_and_free_hugetlb_folio()
1793 list_del(&folio->lru); in bulk_vmemmap_restore_error()
1813 list_del(&folio->lru); in bulk_vmemmap_restore_error()
1818 list_del(&folio->lru); in bulk_vmemmap_restore_error()
1898 __ClearPageAnonExclusive(&folio->page); in free_huge_folio()
1899 folio->mapping = NULL; in free_huge_folio()
1930 h->resv_huge_pages++; in free_huge_folio()
1936 } else if (h->surplus_huge_pages_node[nid]) { in free_huge_folio()
1954 h->nr_huge_pages++; in __prep_account_new_huge_page()
1955 h->nr_huge_pages_node[nid]++; in __prep_account_new_huge_page()
1961 INIT_LIST_HEAD(&folio->lru); in init_new_hugetlb_folio()
2164 if ((!acct_surplus || h->surplus_huge_pages_node[node]) && in remove_pool_hugetlb_folio()
2165 !list_empty(&h->hugepage_freelists[node])) { in remove_pool_hugetlb_folio()
2166 folio = list_entry(h->hugepage_freelists[node].next, in remove_pool_hugetlb_folio()
2178 * does nothing for in-use hugetlb folios and non-hugetlb folios.
2181 * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
2185 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
2192 int rc = -EBUSY; in dissolve_free_hugetlb_folio()
2219 * Theoretically, we should return -EBUSY when we in dissolve_free_hugetlb_folio()
2230 h->max_huge_pages--; in dissolve_free_hugetlb_folio()
2243 * non-vmemmap optimized hugetlb folios. in dissolve_free_hugetlb_folio()
2250 h->max_huge_pages++; in dissolve_free_hugetlb_folio()
2309 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) in alloc_surplus_hugetlb_folio()
2325 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { in alloc_surplus_hugetlb_folio()
2332 h->surplus_huge_pages++; in alloc_surplus_hugetlb_folio()
2333 h->surplus_huge_pages_node[folio_nid(folio)]++; in alloc_surplus_hugetlb_folio()
2402 VM_BUG_ON(!h->resv_huge_pages); in alloc_hugetlb_folio_reserve()
2403 h->resv_huge_pages--; in alloc_hugetlb_folio_reserve()
2427 /* We cannot fallback to other nodes, as we could break the per-node pool. */ in alloc_hugetlb_folio_nodemask()
2437 struct mempolicy *mpol = get_task_policy(current); in policy_mbind_nodemask()
2443 if (mpol->mode == MPOL_BIND && in policy_mbind_nodemask()
2445 cpuset_nodemask_valid_mems_allowed(&mpol->nodes))) in policy_mbind_nodemask()
2446 return &mpol->nodes; in policy_mbind_nodemask()
2468 needed = (h->resv_huge_pages + delta) - h->free_huge_pages; in gather_surplus_pages()
2470 h->resv_huge_pages += delta; in gather_surplus_pages()
2476 ret = -ENOMEM; in gather_surplus_pages()
2493 list_add(&folio->lru, &surplus_list); in gather_surplus_pages()
2503 needed = (h->resv_huge_pages + delta) - in gather_surplus_pages()
2504 (h->free_huge_pages + allocated); in gather_surplus_pages()
2524 h->resv_huge_pages += delta; in gather_surplus_pages()
2529 if ((--needed) < 0) in gather_surplus_pages()
2564 h->resv_huge_pages -= unused_resv_pages; in return_unused_surplus_pages()
2571 * by pre-allocated pages. Only free surplus pages. in return_unused_surplus_pages()
2573 nr_pages = min(unused_resv_pages, h->surplus_huge_pages); in return_unused_surplus_pages()
2581 * on-line nodes with memory and will handle the hstate accounting. in return_unused_surplus_pages()
2583 while (nr_pages--) { in return_unused_surplus_pages()
2590 list_add(&folio->lru, &page_list); in return_unused_surplus_pages()
2669 if (vma->vm_flags & VM_MAYSHARE) { in __vma_reservation_common()
2679 if (vma->vm_flags & VM_MAYSHARE) { in __vma_reservation_common()
2692 if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV) in __vma_reservation_common()
2701 * Subtle - The reserve map for private mappings has the in __vma_reservation_common()
2821 if (!(vma->vm_flags & VM_MAYSHARE)) in restore_reserve_on_error()
2840 * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve
2867 * Fail with -EBUSY if not possible. in alloc_and_dissolve_hugetlb_folio()
2871 ret = isolated ? 0 : -EBUSY; in alloc_and_dissolve_hugetlb_folio()
2889 return -ENOMEM; in alloc_and_dissolve_hugetlb_folio()
2931 int ret = -EBUSY; in isolate_or_dissolve_huge_page()
2949 * alloc_contig_range and them. Return -ENOMEM as this has the effect in isolate_or_dissolve_huge_page()
2953 return -ENOMEM; in isolate_or_dissolve_huge_page()
2979 if (memcg_charge_ret == -ENOMEM) { in alloc_hugetlb_folio()
2981 return ERR_PTR(-ENOMEM); in alloc_hugetlb_folio()
2995 return ERR_PTR(-ENOMEM); in alloc_hugetlb_folio()
3051 h->resv_huge_pages--; in alloc_hugetlb_folio()
3053 list_add(&folio->lru, &h->hugepage_activelist); in alloc_hugetlb_folio()
3085 hugetlb_acct_memory(h, -rsv_adjust); in alloc_hugetlb_folio()
3114 return ERR_PTR(-ENOSPC); in alloc_hugetlb_folio()
3133 for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, &node_states[N_MEMORY]) { in __alloc_bootmem_huge_page()
3157 huge_page_size(h) - PAGE_SIZE); in __alloc_bootmem_huge_page()
3159 INIT_LIST_HEAD(&m->list); in __alloc_bootmem_huge_page()
3160 list_add(&m->list, &huge_boot_pages[node]); in __alloc_bootmem_huge_page()
3161 m->hstate = h; in __alloc_bootmem_huge_page()
3179 __ClearPageReserved(folio_page(folio, pfn - head_pfn)); in hugetlb_folio_init_tail_vmemmap()
3181 prep_compound_tail((struct page *)folio, pfn - head_pfn); in hugetlb_folio_init_tail_vmemmap()
3246 h = m->hstate; in gather_bootmem_prealloc_node()
3261 list_add(&folio->lru, &folio_list); in gather_bootmem_prealloc_node()
3266 * other side-effects, like CommitLimit going negative. in gather_bootmem_prealloc_node()
3305 for (i = 0; i < h->max_huge_pages_node[nid]; ++i) { in hugetlb_hstate_alloc_pages_onenode()
3321 if (i == h->max_huge_pages_node[nid]) in hugetlb_hstate_alloc_pages_onenode()
3326 h->max_huge_pages_node[nid], buf, nid, i); in hugetlb_hstate_alloc_pages_onenode()
3327 h->max_huge_pages -= (h->max_huge_pages_node[nid] - i); in hugetlb_hstate_alloc_pages_onenode()
3328 h->max_huge_pages_node[nid] = i; in hugetlb_hstate_alloc_pages_onenode()
3337 if (h->max_huge_pages_node[i] > 0) { in hugetlb_hstate_alloc_pages_specific_nodes()
3348 if (allocated < h->max_huge_pages) { in hugetlb_hstate_alloc_pages_errcheck()
3353 h->max_huge_pages, buf, allocated); in hugetlb_hstate_alloc_pages_errcheck()
3354 h->max_huge_pages = allocated; in hugetlb_hstate_alloc_pages_errcheck()
3361 int i, num = end - start; in hugetlb_pages_alloc_boot_node()
3366 /* Bit mask controlling how hard we retry per-node allocations.*/ in hugetlb_pages_alloc_boot_node()
3375 list_move(&folio->lru, &folio_list); in hugetlb_pages_alloc_boot_node()
3386 for (i = 0; i < h->max_huge_pages; ++i) { in hugetlb_gigantic_pages_alloc_boot()
3405 job.size = h->max_huge_pages; in hugetlb_pages_alloc_boot()
3418 * +------------+-------+-------+-------+-------+-------+ in hugetlb_pages_alloc_boot()
3420 * +------------+-------+-------+-------+-------+-------+ in hugetlb_pages_alloc_boot()
3424 * +------------+-------+-------+-------+-------+-------+ in hugetlb_pages_alloc_boot()
3427 job.min_chunk = h->max_huge_pages / num_node_state(N_MEMORY) / 2; in hugetlb_pages_alloc_boot()
3430 return h->nr_huge_pages; in hugetlb_pages_alloc_boot()
3435 * non-gigantic pages.
3436 * - For gigantic pages, this is called early in the boot process and
3440 * - For non-gigantic pages, this is called later in the boot process after
3488 * h->demote_order is initially 0. in hugetlb_init_hstates()
3489 * - We can not demote gigantic pages if runtime freeing in hugetlb_init_hstates()
3491 * - If CMA allocation is possible, we can not demote in hugetlb_init_hstates()
3496 if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER) in hugetlb_init_hstates()
3501 if (h2->order < h->order && in hugetlb_init_hstates()
3502 h2->order > h->demote_order) in hugetlb_init_hstates()
3503 h->demote_order = h2->order; in hugetlb_init_hstates()
3516 pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n", in report_hugepages()
3517 buf, h->free_huge_pages); in report_hugepages()
3539 struct list_head *freel = &h->hugepage_freelists[i]; in try_to_free_low()
3541 if (count >= h->nr_huge_pages) in try_to_free_low()
3546 list_add(&folio->lru, &page_list); in try_to_free_low()
3563 * Increment or decrement surplus_huge_pages. Keep node-specific counters
3564 * balanced by operating on them in a round-robin fashion.
3573 VM_BUG_ON(delta != -1 && delta != 1); in adjust_pool_surplus()
3576 for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, nodes_allowed) { in adjust_pool_surplus()
3577 if (h->surplus_huge_pages_node[node]) in adjust_pool_surplus()
3582 if (h->surplus_huge_pages_node[node] < in adjust_pool_surplus()
3583 h->nr_huge_pages_node[node]) in adjust_pool_surplus()
3590 h->surplus_huge_pages += delta; in adjust_pool_surplus()
3591 h->surplus_huge_pages_node[node] += delta; in adjust_pool_surplus()
3595 #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
3606 * Bit mask controlling how hard we retry per-node allocations. in set_max_huge_pages()
3613 return -ENOMEM; in set_max_huge_pages()
3619 mutex_lock(&h->resize_lock); in set_max_huge_pages()
3632 count += persistent_huge_pages(h) - in set_max_huge_pages()
3633 (h->nr_huge_pages_node[nid] - in set_max_huge_pages()
3634 h->surplus_huge_pages_node[nid]); in set_max_huge_pages()
3655 mutex_unlock(&h->resize_lock); in set_max_huge_pages()
3657 return -EINVAL; in set_max_huge_pages()
3673 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { in set_max_huge_pages()
3674 if (!adjust_pool_surplus(h, nodes_allowed, -1)) in set_max_huge_pages()
3692 &h->next_nid_to_alloc); in set_max_huge_pages()
3699 list_add(&folio->lru, &page_list); in set_max_huge_pages()
3702 /* Bail for signals. Probably ctrl-c from user */ in set_max_huge_pages()
3703 if (signal_pending(current)) { in set_max_huge_pages()
3734 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; in set_max_huge_pages()
3746 list_add(&folio->lru, &page_list); in set_max_huge_pages()
3759 h->max_huge_pages = persistent_huge_pages(h); in set_max_huge_pages()
3761 mutex_unlock(&h->resize_lock); in set_max_huge_pages()
3784 * Note that we already hold src->resize_lock. To prevent deadlock, in demote_free_hugetlb_folios()
3787 mutex_lock(&dst->resize_lock); in demote_free_hugetlb_folios()
3795 list_del(&folio->lru); in demote_free_hugetlb_folios()
3797 split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst)); in demote_free_hugetlb_folios()
3803 page->mapping = NULL; in demote_free_hugetlb_folios()
3805 prep_compound_page(page, dst->order); in demote_free_hugetlb_folios()
3808 list_add(&page->lru, &dst_list); in demote_free_hugetlb_folios()
3814 mutex_unlock(&dst->resize_lock); in demote_free_hugetlb_folios()
3831 if (!src->demote_order) { in demote_pool_huge_page()
3833 return -EINVAL; /* internal error */ in demote_pool_huge_page()
3835 dst = size_to_hstate(PAGE_SIZE << src->demote_order); in demote_pool_huge_page()
3841 list_for_each_entry_safe(folio, next, &src->hugepage_freelists[node], lru) { in demote_pool_huge_page()
3846 list_add(&folio->lru, &list); in demote_pool_huge_page()
3859 list_del(&folio->lru); in demote_pool_huge_page()
3862 nr_demoted--; in demote_pool_huge_page()
3873 src->max_huge_pages -= nr_demoted; in demote_pool_huge_page()
3874 dst->max_huge_pages += nr_demoted << (huge_page_order(src) - huge_page_order(dst)); in demote_pool_huge_page()
3883 * Return -EBUSY so that caller will not retry. in demote_pool_huge_page()
3885 return -EBUSY; in demote_pool_huge_page()
3925 nr_huge_pages = h->nr_huge_pages; in nr_hugepages_show_common()
3927 nr_huge_pages = h->nr_huge_pages_node[nid]; in nr_hugepages_show_common()
3940 return -EINVAL; in __nr_hugepages_store_common()
3998 * hstate attribute for optionally mempolicy-based constraint on persistent
4021 return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages); in nr_overcommit_hugepages_show()
4032 return -EINVAL; in nr_overcommit_hugepages_store()
4039 h->nr_overcommit_huge_pages = input; in nr_overcommit_hugepages_store()
4055 free_huge_pages = h->free_huge_pages; in free_hugepages_show()
4057 free_huge_pages = h->free_huge_pages_node[nid]; in free_hugepages_show()
4067 return sysfs_emit(buf, "%lu\n", h->resv_huge_pages); in resv_hugepages_show()
4080 surplus_huge_pages = h->surplus_huge_pages; in surplus_hugepages_show()
4082 surplus_huge_pages = h->surplus_huge_pages_node[nid]; in surplus_hugepages_show()
4111 mutex_lock(&h->resize_lock); in demote_store()
4122 nr_available = h->free_huge_pages_node[nid]; in demote_store()
4124 nr_available = h->free_huge_pages; in demote_store()
4125 nr_available -= h->resv_huge_pages; in demote_store()
4135 nr_demote -= rc; in demote_store()
4139 mutex_unlock(&h->resize_lock); in demote_store()
4151 unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K; in demote_size_show()
4168 return -EINVAL; in demote_size_store()
4169 demote_order = demote_hstate->order; in demote_size_store()
4171 return -EINVAL; in demote_size_store()
4175 if (demote_order >= h->order) in demote_size_store()
4176 return -EINVAL; in demote_size_store()
4179 mutex_lock(&h->resize_lock); in demote_size_store()
4180 h->demote_order = demote_order; in demote_size_store()
4181 mutex_unlock(&h->resize_lock); in demote_size_store()
4220 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent); in hugetlb_sysfs_add_hstate()
4222 return -ENOMEM; in hugetlb_sysfs_add_hstate()
4231 if (h->demote_order) { in hugetlb_sysfs_add_hstate()
4235 pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name); in hugetlb_sysfs_add_hstate()
4250 * node_hstate/s - associate per node hstate attributes, via their kobjects,
4277 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
4278 * Returns node id via non-NULL nidp.
4288 if (nhs->hstate_kobjs[i] == kobj) { in kobj_to_node_hstate()
4301 * No-op if no hstate attributes attached.
4306 struct node_hstate *nhs = &node_hstates[node->dev.id]; in hugetlb_unregister_node()
4308 if (!nhs->hugepages_kobj) in hugetlb_unregister_node()
4313 struct kobject *hstate_kobj = nhs->hstate_kobjs[idx]; in hugetlb_unregister_node()
4317 if (h->demote_order) in hugetlb_unregister_node()
4321 nhs->hstate_kobjs[idx] = NULL; in hugetlb_unregister_node()
4324 kobject_put(nhs->hugepages_kobj); in hugetlb_unregister_node()
4325 nhs->hugepages_kobj = NULL; in hugetlb_unregister_node()
4331 * No-op if attributes already registered.
4336 struct node_hstate *nhs = &node_hstates[node->dev.id]; in hugetlb_register_node()
4342 if (nhs->hugepages_kobj) in hugetlb_register_node()
4345 nhs->hugepages_kobj = kobject_create_and_add("hugepages", in hugetlb_register_node()
4346 &node->dev.kobj); in hugetlb_register_node()
4347 if (!nhs->hugepages_kobj) in hugetlb_register_node()
4351 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, in hugetlb_register_node()
4352 nhs->hstate_kobjs, in hugetlb_register_node()
4356 h->name, node->dev.id); in hugetlb_register_node()
4365 * devices of nodes that have memory. All on-line nodes should have
4381 *nidp = -1; in kobj_to_node_hstate()
4410 pr_err("HugeTLB: Unable to add hstate %s", h->name); in hugetlb_sysfs_init()
4434 pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n"); in hugetlb_init()
4515 __mutex_init(&h->resize_lock, "resize mutex", &h->resize_key); in hugetlb_add_hstate()
4516 h->order = order; in hugetlb_add_hstate()
4517 h->mask = ~(huge_page_size(h) - 1); in hugetlb_add_hstate()
4519 INIT_LIST_HEAD(&h->hugepage_freelists[i]); in hugetlb_add_hstate()
4520 INIT_LIST_HEAD(&h->hugepage_activelist); in hugetlb_add_hstate()
4521 h->next_nid_to_alloc = first_memory_node; in hugetlb_add_hstate()
4522 h->next_nid_to_free = first_memory_node; in hugetlb_add_hstate()
4523 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", in hugetlb_add_hstate()
4541 parsed_hstate->max_huge_pages = 0; in hugepages_clear_pages_in_node()
4542 memset(parsed_hstate->max_huge_pages_node, 0, in hugepages_clear_pages_in_node()
4543 sizeof(parsed_hstate->max_huge_pages_node)); in hugepages_clear_pages_in_node()
4578 mhp = &parsed_hstate->max_huge_pages; in hugepages_setup()
4605 parsed_hstate->max_huge_pages_node[node] = tmp; in hugepages_setup()
4684 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); in hugepagesz_setup()
4712 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT); in default_hugepagesz_setup()
4743 unsigned int *array = h->free_huge_pages_node; in allowed_mems_nr()
4777 unsigned long tmp = h->max_huge_pages; in hugetlb_sysctl_handler_common()
4781 return -EOPNOTSUPP; in hugetlb_sysctl_handler_common()
4820 return -EOPNOTSUPP; in hugetlb_overcommit_handler()
4822 tmp = h->nr_overcommit_huge_pages; in hugetlb_overcommit_handler()
4825 return -EINVAL; in hugetlb_overcommit_handler()
4834 h->nr_overcommit_huge_pages = tmp; in hugetlb_overcommit_handler()
4889 unsigned long count = h->nr_huge_pages; in hugetlb_report_meminfo()
4901 h->free_huge_pages, in hugetlb_report_meminfo()
4902 h->resv_huge_pages, in hugetlb_report_meminfo()
4903 h->surplus_huge_pages, in hugetlb_report_meminfo()
4921 nid, h->nr_huge_pages_node[nid], in hugetlb_report_node_meminfo()
4922 nid, h->free_huge_pages_node[nid], in hugetlb_report_node_meminfo()
4923 nid, h->surplus_huge_pages_node[nid]); in hugetlb_report_node_meminfo()
4936 h->nr_huge_pages_node[nid], in hugetlb_show_meminfo_node()
4937 h->free_huge_pages_node[nid], in hugetlb_show_meminfo_node()
4938 h->surplus_huge_pages_node[nid], in hugetlb_show_meminfo_node()
4945 K(atomic_long_read(&mm->hugetlb_usage))); in hugetlb_report_usage()
4955 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h); in hugetlb_total_pages()
4961 int ret = -ENOMEM; in hugetlb_acct_memory()
4972 * current cpuset. Application can still potentially OOM'ed by kernel in hugetlb_acct_memory()
4980 * we fall back to check against current free page availability as in hugetlb_acct_memory()
4987 * the memory policy of the current task. Similar to the description in hugetlb_acct_memory()
5002 return_unused_surplus_pages(h, (unsigned long) -delta); in hugetlb_acct_memory()
5024 kref_get(&resv->refs); in hugetlb_vm_op_open()
5033 if (vma->vm_flags & VM_MAYSHARE) { in hugetlb_vm_op_open()
5034 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; in hugetlb_vm_op_open()
5037 if (vma_lock->vma != vma) { in hugetlb_vm_op_open()
5038 vma->vm_private_data = NULL; in hugetlb_vm_op_open()
5061 start = vma_hugecache_offset(h, vma, vma->vm_start); in hugetlb_vm_op_close()
5062 end = vma_hugecache_offset(h, vma, vma->vm_end); in hugetlb_vm_op_close()
5064 reserve = (end - start) - region_count(resv, start, end); in hugetlb_vm_op_close()
5072 hugetlb_acct_memory(h, -gbl_reserve); in hugetlb_vm_op_close()
5075 kref_put(&resv->refs, resv_map_release); in hugetlb_vm_op_close()
5081 return -EINVAL; in hugetlb_vm_op_split()
5084 * PMD sharing is only possible for PUD_SIZE-aligned address ranges in hugetlb_vm_op_split()
5097 if (floor >= vma->vm_start && ceil <= vma->vm_end) in hugetlb_vm_op_split()
5111 * handle_mm_fault() to try to instantiate regular-sized pages in the
5144 vma->vm_page_prot))); in make_huge_pte()
5147 vma->vm_page_prot)); in make_huge_pte()
5150 entry = arch_make_huge_pte(entry, shift, vma->vm_flags); in make_huge_pte()
5160 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(vma->vm_mm, address, ptep))); in set_huge_ptep_writable()
5195 pte_t newpte = make_huge_pte(vma, &new_folio->page, 1); in hugetlb_install_folio()
5201 set_huge_pte_at(vma->vm_mm, addr, ptep, newpte, sz); in hugetlb_install_folio()
5202 hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm); in hugetlb_install_folio()
5213 bool cow = is_cow_mapping(src_vma->vm_flags); in copy_hugetlb_page_range()
5223 src_vma->vm_start, in copy_hugetlb_page_range()
5224 src_vma->vm_end); in copy_hugetlb_page_range()
5227 raw_write_seqcount_begin(&src->write_protect_seq); in copy_hugetlb_page_range()
5239 for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) { in copy_hugetlb_page_range()
5248 ret = -ENOMEM; in copy_hugetlb_page_range()
5268 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte); in copy_hugetlb_page_range()
5306 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte); in copy_hugetlb_page_range()
5316 * When pre-allocating the page or copying data, we in copy_hugetlb_page_range()
5347 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte); in copy_hugetlb_page_range()
5385 raw_write_seqcount_end(&src->write_protect_seq); in copy_hugetlb_page_range()
5399 struct mm_struct *mm = vma->vm_mm; in move_huge_pte()
5427 struct address_space *mapping = vma->vm_file->f_mapping; in move_hugetlb_page_tables()
5429 struct mm_struct *mm = vma->vm_mm; in move_hugetlb_page_tables()
5477 flush_hugetlb_tlb_range(vma, old_end - len, old_end); in move_hugetlb_page_tables()
5482 return len + old_addr - old_end; in move_hugetlb_page_tables()
5489 struct mm_struct *mm = vma->vm_mm; in __unmap_hugepage_range()
5542 * If the pte was wr-protected by uffd-wp in any of the in __unmap_hugepage_range()
5544 * drop the uffd-wp bit in this zap, then replace the in __unmap_hugepage_range()
5581 /* Leave a uffd-wp pte marker if needed */ in __unmap_hugepage_range()
5596 if (!h->surplus_huge_pages && __vma_private_lock(vma) && in __unmap_hugepage_range()
5608 * resv->adds_in_progress if it succeeds. If this is not done, in __unmap_hugepage_range()
5656 if (!vma->vm_file) /* hugetlbfs_file_mmap error */ in __hugetlb_zap_begin()
5661 if (vma->vm_file) in __hugetlb_zap_begin()
5662 i_mmap_lock_write(vma->vm_file->f_mapping); in __hugetlb_zap_begin()
5668 zap_flags_t zap_flags = details ? details->zap_flags : 0; in __hugetlb_zap_end()
5670 if (!vma->vm_file) /* hugetlbfs_file_mmap error */ in __hugetlb_zap_end()
5688 if (vma->vm_file) in __hugetlb_zap_end()
5689 i_mmap_unlock_write(vma->vm_file->f_mapping); in __hugetlb_zap_end()
5699 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, in unmap_hugepage_range()
5703 tlb_gather_mmu(&tlb, vma->vm_mm); in unmap_hugepage_range()
5730 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + in unmap_ref_private()
5731 vma->vm_pgoff; in unmap_ref_private()
5732 mapping = vma->vm_file->f_mapping; in unmap_ref_private()
5740 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { in unmap_ref_private()
5741 /* Do not unmap the current VMA */ in unmap_ref_private()
5750 if (iter_vma->vm_flags & VM_MAYSHARE) in unmap_ref_private()
5756 * areas. This is because a future no-page fault on this VMA in unmap_ref_private()
5776 struct vm_area_struct *vma = vmf->vma; in hugetlb_wp()
5777 struct mm_struct *mm = vma->vm_mm; in hugetlb_wp()
5778 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; in hugetlb_wp()
5779 pte_t pte = huge_ptep_get(mm, vmf->address, vmf->pte); in hugetlb_wp()
5788 * Never handle CoW for uffd-wp protected pages. It should be only in hugetlb_wp()
5789 * handled when the uffd-wp protection is removed. in hugetlb_wp()
5793 * uffd-wp bit first. in hugetlb_wp()
5799 * hugetlb does not support FOLL_FORCE-style write faults that keep the in hugetlb_wp()
5802 if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE))) in hugetlb_wp()
5806 if (vma->vm_flags & VM_MAYSHARE) { in hugetlb_wp()
5807 set_huge_ptep_writable(vma, vmf->address, vmf->pte); in hugetlb_wp()
5817 * If no-one else is actually using this page, we're the exclusive in hugetlb_wp()
5828 if (!PageAnonExclusive(&old_folio->page)) { in hugetlb_wp()
5830 SetPageAnonExclusive(&old_folio->page); in hugetlb_wp()
5833 set_huge_ptep_writable(vma, vmf->address, vmf->pte); in hugetlb_wp()
5839 PageAnonExclusive(&old_folio->page), &old_folio->page); in hugetlb_wp()
5860 spin_unlock(vmf->ptl); in hugetlb_wp()
5861 new_folio = alloc_hugetlb_folio(vma, vmf->address, outside_reserve); in hugetlb_wp()
5872 struct address_space *mapping = vma->vm_file->f_mapping; in hugetlb_wp()
5886 idx = vma_hugecache_offset(h, vma, vmf->address); in hugetlb_wp()
5891 unmap_ref_private(mm, vma, &old_folio->page, in hugetlb_wp()
5892 vmf->address); in hugetlb_wp()
5896 spin_lock(vmf->ptl); in hugetlb_wp()
5897 vmf->pte = hugetlb_walk(vma, vmf->address, in hugetlb_wp()
5899 if (likely(vmf->pte && in hugetlb_wp()
5900 pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte))) in hugetlb_wp()
5903 * race occurs while re-acquiring page table in hugetlb_wp()
5922 if (copy_user_large_folio(new_folio, old_folio, vmf->real_address, vma)) { in hugetlb_wp()
5928 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, vmf->address, in hugetlb_wp()
5929 vmf->address + huge_page_size(h)); in hugetlb_wp()
5936 spin_lock(vmf->ptl); in hugetlb_wp()
5937 vmf->pte = hugetlb_walk(vma, vmf->address, huge_page_size(h)); in hugetlb_wp()
5938 if (likely(vmf->pte && pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte))) { in hugetlb_wp()
5939 pte_t newpte = make_huge_pte(vma, &new_folio->page, !unshare); in hugetlb_wp()
5942 huge_ptep_clear_flush(vma, vmf->address, vmf->pte); in hugetlb_wp()
5944 hugetlb_add_new_anon_rmap(new_folio, vma, vmf->address); in hugetlb_wp()
5947 set_huge_pte_at(mm, vmf->address, vmf->pte, newpte, in hugetlb_wp()
5953 spin_unlock(vmf->ptl); in hugetlb_wp()
5961 restore_reserve_on_error(h, vma, vmf->address, new_folio); in hugetlb_wp()
5966 spin_lock(vmf->ptl); /* Caller expects lock to be held */ in hugetlb_wp()
5978 struct address_space *mapping = vma->vm_file->f_mapping; in hugetlbfs_pagecache_present()
5992 struct inode *inode = mapping->host; in hugetlb_add_to_page_cache()
6008 * by non-hugetlbfs specific code paths. in hugetlb_add_to_page_cache()
6012 spin_lock(&inode->i_lock); in hugetlb_add_to_page_cache()
6013 inode->i_blocks += blocks_per_huge_page(h); in hugetlb_add_to_page_cache()
6014 spin_unlock(&inode->i_lock); in hugetlb_add_to_page_cache()
6029 hugetlb_vma_unlock_read(vmf->vma); in hugetlb_handle_userfault()
6030 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff); in hugetlb_handle_userfault()
6055 struct vm_area_struct *vma = vmf->vma; in hugetlb_no_page()
6056 struct mm_struct *mm = vma->vm_mm; in hugetlb_no_page()
6064 u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff); in hugetlb_no_page()
6074 current->pid); in hugetlb_no_page()
6083 folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff); in hugetlb_no_page()
6085 size = i_size_read(mapping->host) >> huge_page_shift(h); in hugetlb_no_page()
6086 if (vmf->pgoff >= size) in hugetlb_no_page()
6092 * without pgtable lock, we need to re-test under in hugetlb_no_page()
6095 * either changed or during-changing ptes and retry in hugetlb_no_page()
6107 if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) { in hugetlb_no_page()
6116 if (!(vma->vm_flags & VM_MAYSHARE)) { in hugetlb_no_page()
6122 folio = alloc_hugetlb_folio(vma, vmf->address, 0); in hugetlb_no_page()
6136 if (hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) in hugetlb_no_page()
6142 folio_zero_user(folio, vmf->real_address); in hugetlb_no_page()
6146 if (vma->vm_flags & VM_MAYSHARE) { in hugetlb_no_page()
6148 vmf->pgoff); in hugetlb_no_page()
6151 * err can't be -EEXIST which implies someone in hugetlb_no_page()
6157 restore_reserve_on_error(h, vma, vmf->address, in hugetlb_no_page()
6185 if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) { in hugetlb_no_page()
6200 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { in hugetlb_no_page()
6201 if (vma_needs_reservation(h, vma, vmf->address) < 0) { in hugetlb_no_page()
6206 vma_end_reservation(h, vma, vmf->address); in hugetlb_no_page()
6209 vmf->ptl = huge_pte_lock(h, mm, vmf->pte); in hugetlb_no_page()
6212 if (!pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), vmf->orig_pte)) in hugetlb_no_page()
6216 hugetlb_add_new_anon_rmap(folio, vma, vmf->address); in hugetlb_no_page()
6219 new_pte = make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE) in hugetlb_no_page()
6220 && (vma->vm_flags & VM_SHARED))); in hugetlb_no_page()
6222 * If this pte was previously wr-protected, keep it wr-protected even in hugetlb_no_page()
6225 if (unlikely(pte_marker_uffd_wp(vmf->orig_pte))) in hugetlb_no_page()
6227 set_huge_pte_at(mm, vmf->address, vmf->pte, new_pte, huge_page_size(h)); in hugetlb_no_page()
6230 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) { in hugetlb_no_page()
6235 spin_unlock(vmf->ptl); in hugetlb_no_page()
6250 * We must check to release the per-VMA lock. __vmf_anon_prepare() is in hugetlb_no_page()
6260 spin_unlock(vmf->ptl); in hugetlb_no_page()
6263 restore_reserve_on_error(h, vma, vmf->address, folio); in hugetlb_no_page()
6281 return hash & (num_fault_mutexes - 1); in hugetlb_fault_mutex_hash()
6324 mapping = vma->vm_file->f_mapping; in hugetlb_fault()
6398 !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(vmf.orig_pte)) { in hugetlb_fault()
6418 /* Handle userfault-wp first, before trying to lock more pages */ in hugetlb_fault()
6479 * We must check to release the per-VMA lock. __vmf_anon_prepare() in in hugetlb_fault()
6489 * the page is not used after unlocked before returning from the current in hugetlb_fault()
6517 * that breaking the per-node hugetlb pool is not allowed in this case. in alloc_hugetlb_folio_vma()
6536 struct mm_struct *dst_mm = dst_vma->vm_mm; in hugetlb_mfill_atomic_pte()
6540 struct address_space *mapping = dst_vma->vm_file->f_mapping; in hugetlb_mfill_atomic_pte()
6543 int vm_shared = dst_vma->vm_flags & VM_SHARED; in hugetlb_mfill_atomic_pte()
6546 int ret = -ENOMEM; in hugetlb_mfill_atomic_pte()
6557 return -EEXIST; in hugetlb_mfill_atomic_pte()
6563 /* No need to invalidate - it was non-present before */ in hugetlb_mfill_atomic_pte()
6571 ret = -EFAULT; in hugetlb_mfill_atomic_pte()
6578 * a non-missing case. Return -EEXIST. in hugetlb_mfill_atomic_pte()
6582 ret = -EEXIST; in hugetlb_mfill_atomic_pte()
6588 ret = -ENOMEM; in hugetlb_mfill_atomic_pte()
6597 ret = -ENOENT; in hugetlb_mfill_atomic_pte()
6609 ret = -ENOMEM; in hugetlb_mfill_atomic_pte()
6623 ret = -EEXIST; in hugetlb_mfill_atomic_pte()
6631 ret = -ENOMEM; in hugetlb_mfill_atomic_pte()
6663 ret = -EFAULT; in hugetlb_mfill_atomic_pte()
6664 if (idx >= (i_size_read(mapping->host) >> huge_page_shift(h))) in hugetlb_mfill_atomic_pte()
6681 ret = -EIO; in hugetlb_mfill_atomic_pte()
6687 * registered, we firstly wr-protect a none pte which has no page cache in hugetlb_mfill_atomic_pte()
6690 ret = -EEXIST; in hugetlb_mfill_atomic_pte()
6700 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY in hugetlb_mfill_atomic_pte()
6706 writable = dst_vma->vm_flags & VM_WRITE; in hugetlb_mfill_atomic_pte()
6708 _dst_pte = make_huge_pte(dst_vma, &folio->page, writable); in hugetlb_mfill_atomic_pte()
6725 /* No need to invalidate - it was non-present before */ in hugetlb_mfill_atomic_pte()
6752 struct mm_struct *mm = vma->vm_mm; in hugetlb_change_protection()
6778 i_mmap_lock_write(vma->vm_file->f_mapping); in hugetlb_change_protection()
6789 * Userfaultfd wr-protect requires pgtable in hugetlb_change_protection()
6790 * pre-allocations to install pte markers. in hugetlb_change_protection()
6794 pages = -ENOMEM; in hugetlb_change_protection()
6801 * When uffd-wp is enabled on the vma, unshare in hugetlb_change_protection()
6845 /* Safe to modify directly (non-present->none). */ in hugetlb_change_protection()
6853 pte = arch_make_huge_pte(pte, shift, vma->vm_flags); in hugetlb_change_protection()
6863 /* Safe to modify directly (none->non-present). */ in hugetlb_change_protection()
6888 i_mmap_unlock_write(vma->vm_file->f_mapping); in hugetlb_change_protection()
6892 return pages > 0 ? (pages << h->order) : pages; in hugetlb_change_protection()
6901 long chg = -1, add = -1; in hugetlb_reserve_pages()
6931 * to reserve the full area even if read-only as mprotect() may be in hugetlb_reserve_pages()
6932 * called to make the mapping read-write. Assume !vma is a shm mapping in hugetlb_reserve_pages()
6934 if (!vma || vma->vm_flags & VM_MAYSHARE) { in hugetlb_reserve_pages()
6949 chg = to - from; in hugetlb_reserve_pages()
6962 if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) { in hugetlb_reserve_pages()
6990 * the reservation was consumed. Private mappings are per-VMA and in hugetlb_reserve_pages()
6996 if (!vma || vma->vm_flags & VM_MAYSHARE) { in hugetlb_reserve_pages()
7000 hugetlb_acct_memory(h, -gbl_reserve); in hugetlb_reserve_pages()
7014 * reference to h_cg->css. See comment below for detail. in hugetlb_reserve_pages()
7018 (chg - add) * pages_per_huge_page(h), h_cg); in hugetlb_reserve_pages()
7021 chg - add); in hugetlb_reserve_pages()
7022 hugetlb_acct_memory(h, -rsv_adjust); in hugetlb_reserve_pages()
7026 * h_cg->css. So we should release the reference held in hugetlb_reserve_pages()
7043 if (!vma || vma->vm_flags & VM_MAYSHARE) in hugetlb_reserve_pages()
7050 kref_put(&resv_map->refs, resv_map_release); in hugetlb_reserve_pages()
7080 spin_lock(&inode->i_lock); in hugetlb_unreserve_pages()
7081 inode->i_blocks -= (blocks_per_huge_page(h) * freed); in hugetlb_unreserve_pages()
7082 spin_unlock(&inode->i_lock); in hugetlb_unreserve_pages()
7088 * Note that !resv_map implies freed == 0. So (chg - freed) in hugetlb_unreserve_pages()
7091 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed)); in hugetlb_unreserve_pages()
7092 hugetlb_acct_memory(h, -gbl_reserve); in hugetlb_unreserve_pages()
7102 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) + in page_table_shareable()
7103 svma->vm_start; in page_table_shareable()
7108 unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED_MASK; in page_table_shareable()
7109 unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED_MASK; in page_table_shareable()
7120 !svma->vm_private_data) in page_table_shareable()
7138 if (!(vma->vm_flags & VM_MAYSHARE)) in want_pmd_share()
7140 if (!vma->vm_private_data) /* vma lock required for sharing */ in want_pmd_share()
7155 unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE), in adjust_range_if_pmd_sharing_possible()
7156 v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE); in adjust_range_if_pmd_sharing_possible()
7162 if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) || in adjust_range_if_pmd_sharing_possible()
7179 * pud has to be populated inside the same i_mmap_rwsem section - otherwise
7186 struct address_space *mapping = vma->vm_file->f_mapping; in huge_pmd_share()
7187 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + in huge_pmd_share()
7188 vma->vm_pgoff; in huge_pmd_share()
7195 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { in huge_pmd_share()
7213 spin_lock(&mm->page_table_lock); in huge_pmd_share()
7221 spin_unlock(&mm->page_table_lock); in huge_pmd_share()
7247 i_mmap_assert_write_locked(vma->vm_file->f_mapping); in huge_pmd_unshare()
7320 * huge_pte_offset() - Walk the page table to resolve the hugepage
7345 /* must be pud huge, non-present or none */ in huge_pte_offset()
7352 /* must be pmd huge, non-present or none */ in huge_pte_offset()
7358 * page in a page table page mapping size. Used to skip non-present
7368 return P4D_SIZE - PUD_SIZE; in hugetlb_mask_last_page()
7370 return PUD_SIZE - PMD_SIZE; in hugetlb_mask_last_page()
7382 return PUD_SIZE - PMD_SIZE; in hugetlb_mask_last_page()
7401 list_move_tail(&folio->lru, list); in isolate_hugetlb()
7420 ret = -EBUSY; in get_hwpoison_hugetlb_folio()
7441 list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist); in folio_putback_active_hugetlb()
7451 set_page_owner_migrate_reason(&new_folio->page, reason); in move_hugetlb_state()
7459 * Also note that we have to transfer the per-node surplus state in move_hugetlb_state()
7461 * the per-node's. in move_hugetlb_state()
7472 * There is no need to transfer the per-node surplus state in move_hugetlb_state()
7478 if (h->surplus_huge_pages_node[old_nid]) { in move_hugetlb_state()
7479 h->surplus_huge_pages_node[old_nid]--; in move_hugetlb_state()
7480 h->surplus_huge_pages_node[new_nid]++; in move_hugetlb_state()
7492 struct mm_struct *mm = vma->vm_mm; in hugetlb_unshare_pmds()
7498 if (!(vma->vm_flags & VM_MAYSHARE)) in hugetlb_unshare_pmds()
7513 i_mmap_lock_write(vma->vm_file->f_mapping); in hugetlb_unshare_pmds()
7523 i_mmap_unlock_write(vma->vm_file->f_mapping); in hugetlb_unshare_pmds()
7538 hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE), in hugetlb_unshare_all_pmds()
7539 ALIGN_DOWN(vma->vm_end, PUD_SIZE)); in hugetlb_unshare_all_pmds()
7608 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid]; in hugetlb_cma_reserve()
7616 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid]; in hugetlb_cma_reserve()
7655 size = min(per_node, hugetlb_cma_size - reserved); in hugetlb_cma_reserve()
7697 pr_warn("hugetlb_cma: the option isn't supported by current arch\n"); in hugetlb_cma_check()