9 #include <linux/sched.h>
19 #include <linux/mman.h>
22 #include <asm/pgalloc.h>
33 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
36 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
44 static unsigned int khugepaged_pages_collapsed;
45 static unsigned int khugepaged_full_scans;
46 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
48 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
58 static unsigned int khugepaged_max_ptes_none __read_mostly =
HPAGE_PMD_NR-1;
60 static int khugepaged(
void *
none);
61 static int mm_slots_hash_init(
void);
62 static int khugepaged_slab_init(
void);
63 static void khugepaged_slab_free(
void);
65 #define MM_SLOTS_HASH_HEADS 1024
99 static int set_recommended_min_free_kbytes(
void)
103 unsigned long recommended_min;
106 if (!khugepaged_enabled())
125 recommended_min =
min(recommended_min,
129 if (recommended_min > min_free_kbytes)
130 min_free_kbytes = recommended_min;
136 static
int start_khugepaged(
void)
139 if (khugepaged_enabled()) {
140 if (!khugepaged_thread)
143 if (
unlikely(IS_ERR(khugepaged_thread))) {
145 "khugepaged: kthread_run(khugepaged) failed\n");
146 err = PTR_ERR(khugepaged_thread);
147 khugepaged_thread =
NULL;
150 if (!list_empty(&khugepaged_scan.
mm_head))
153 set_recommended_min_free_kbytes();
154 }
else if (khugepaged_thread) {
156 khugepaged_thread =
NULL;
171 return sprintf(buf,
"[always] madvise never\n");
173 return sprintf(buf,
"always [madvise] never\n");
175 return sprintf(buf,
"always madvise [never]\n");
179 const char *buf,
size_t count,
183 if (!
memcmp(
"always", buf,
184 min(
sizeof(
"always")-1, count))) {
187 }
else if (!
memcmp(
"madvise", buf,
188 min(
sizeof(
"madvise")-1, count))) {
191 }
else if (!
memcmp(
"never", buf,
192 min(
sizeof(
"never")-1, count))) {
204 return double_flag_show(kobj, attr, buf,
210 const char *buf,
size_t count)
214 ret = double_flag_store(kobj, attr, buf, count,
222 err = start_khugepaged();
232 __ATTR(enabled, 0644, enabled_show, enabled_store);
244 const char *buf,
size_t count,
250 ret = kstrtoul(buf, 10, &value);
272 return double_flag_show(kobj, attr, buf,
278 const char *buf,
size_t count)
280 return double_flag_store(kobj, attr, buf, count,
285 __ATTR(defrag, 0644, defrag_show, defrag_store);
287 #ifdef CONFIG_DEBUG_VM
291 return single_flag_show(kobj, attr, buf,
292 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
296 const char *buf,
size_t count)
298 return single_flag_store(kobj, attr, buf, count,
299 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
302 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
305 static struct attribute *hugepage_attr[] = {
308 #ifdef CONFIG_DEBUG_VM
309 &debug_cow_attr.
attr,
315 .
attrs = hugepage_attr,
322 return sprintf(buf,
"%u\n", khugepaged_scan_sleep_millisecs);
327 const char *buf,
size_t count)
336 khugepaged_scan_sleep_millisecs = msecs;
342 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
343 scan_sleep_millisecs_store);
349 return sprintf(buf,
"%u\n", khugepaged_alloc_sleep_millisecs);
354 const char *buf,
size_t count)
363 khugepaged_alloc_sleep_millisecs = msecs;
369 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
370 alloc_sleep_millisecs_store);
376 return sprintf(buf,
"%u\n", khugepaged_pages_to_scan);
380 const char *buf,
size_t count)
386 if (err || !pages || pages >
UINT_MAX)
389 khugepaged_pages_to_scan =
pages;
394 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
395 pages_to_scan_store);
401 return sprintf(buf,
"%u\n", khugepaged_pages_collapsed);
410 return sprintf(buf,
"%u\n", khugepaged_full_scans);
418 return single_flag_show(kobj, attr, buf,
423 const char *buf,
size_t count)
425 return single_flag_store(kobj, attr, buf, count,
429 __ATTR(defrag, 0644, khugepaged_defrag_show,
430 khugepaged_defrag_store);
440 static ssize_t khugepaged_max_ptes_none_show(
struct kobject *kobj,
444 return sprintf(buf,
"%u\n", khugepaged_max_ptes_none);
446 static ssize_t khugepaged_max_ptes_none_store(
struct kobject *kobj,
448 const char *buf,
size_t count)
451 unsigned long max_ptes_none;
457 khugepaged_max_ptes_none = max_ptes_none;
462 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
463 khugepaged_max_ptes_none_store);
465 static struct attribute *khugepaged_attr[] = {
466 &khugepaged_defrag_attr.
attr,
467 &khugepaged_max_ptes_none_attr.
attr,
468 &pages_to_scan_attr.
attr,
469 &pages_collapsed_attr.
attr,
470 &full_scans_attr.
attr,
471 &scan_sleep_millisecs_attr.
attr,
472 &alloc_sleep_millisecs_attr.
attr,
477 .
attrs = khugepaged_attr,
478 .
name =
"khugepaged",
481 static int __init hugepage_init_sysfs(
struct kobject **hugepage_kobj)
500 goto remove_hp_group;
512 static void __init hugepage_exit_sysfs(
struct kobject *hugepage_kobj)
519 static inline int hugepage_init_sysfs(
struct kobject **hugepage_kobj)
524 static inline void hugepage_exit_sysfs(
struct kobject *hugepage_kobj)
529 static int __init hugepage_init(
void)
534 if (!has_transparent_hugepage()) {
539 err = hugepage_init_sysfs(&hugepage_kobj);
543 err = khugepaged_slab_init();
547 err = mm_slots_hash_init();
549 khugepaged_slab_free();
558 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
565 hugepage_exit_sysfs(hugepage_kobj);
570 static
int __init setup_transparent_hugepage(
char *
str)
575 if (!
strcmp(str,
"always")) {
581 }
else if (!
strcmp(str,
"madvise")) {
587 }
else if (!
strcmp(str,
"never")) {
597 "transparent_hugepage= cannot parse, ignored\n");
600 __setup(
"transparent_hugepage=", setup_transparent_hugepage);
609 static int __do_huge_pmd_anonymous_page(
struct mm_struct *mm,
611 unsigned long haddr,
pmd_t *pmd,
622 __SetPageUptodate(page);
633 entry = maybe_pmd_mkwrite(
pmd_mkdirty(entry), vma);
643 pgtable_trans_huge_deposit(mm, pgtable);
652 static inline gfp_t alloc_hugepage_gfpmask(
int defrag,
gfp_t extra_gfp)
657 static inline struct page *alloc_hugepage_vma(
int defrag,
659 unsigned long haddr,
int nd,
667 static inline struct page *alloc_hugepage(
int defrag)
669 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
682 if (haddr >= vma->
vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
685 if (
unlikely(khugepaged_enter(vma)))
687 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
690 count_vm_event(THP_FAULT_FALLBACK);
693 count_vm_event(THP_FAULT_ALLOC);
698 if (
unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
732 struct page *src_page;
747 if (
unlikely(!pmd_trans_huge(pmd))) {
751 if (
unlikely(pmd_trans_splitting(pmd))) {
763 page_dup_rmap(src_page);
766 pmdp_set_wrprotect(src_mm, addr, src_pmd);
769 pgtable_trans_huge_deposit(dst_mm, pgtable);
780 static int do_huge_pmd_wp_page_fallback(
struct mm_struct *mm,
791 unsigned long mmun_start;
792 unsigned long mmun_end;
804 vma, address, page_to_nid(page));
825 __SetPageUptodate(pages[i]);
831 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
834 if (
unlikely(!pmd_same(*pmd, orig_pmd)))
838 pmdp_clear_flush(vma, haddr, pmd);
841 pgtable = pgtable_trans_huge_withdraw(mm);
861 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
863 ret |= VM_FAULT_WRITE;
871 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
883 unsigned long address,
pmd_t *pmd,
pmd_t orig_pmd)
886 struct page *
page, *new_page;
888 unsigned long mmun_start;
889 unsigned long mmun_end;
893 if (
unlikely(!pmd_same(*pmd, orig_pmd)))
897 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
899 if (page_mapcount(page) == 1) {
902 entry = maybe_pmd_mkwrite(
pmd_mkdirty(entry), vma);
905 ret |= VM_FAULT_WRITE;
912 !transparent_hugepage_debug_cow())
913 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
919 count_vm_event(THP_FAULT_FALLBACK);
920 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
921 pmd, orig_pmd, page, haddr);
922 if (ret & VM_FAULT_OOM)
927 count_vm_event(THP_FAULT_ALLOC);
937 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
938 __SetPageUptodate(new_page);
942 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
946 if (
unlikely(!pmd_same(*pmd, orig_pmd))) {
955 entry = maybe_pmd_mkwrite(
pmd_mkdirty(entry), vma);
957 pmdp_clear_flush(vma, haddr, pmd);
963 ret |= VM_FAULT_WRITE;
967 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
981 struct page *page =
NULL;
985 if (flags & FOLL_WRITE && !
pmd_write(*pmd))
990 if (flags & FOLL_TOUCH) {
1003 if ((flags & FOLL_MLOCK) && (vma->
vm_flags & VM_LOCKED)) {
1004 if (page->
mapping && trylock_page(page)) {
1013 if (flags & FOLL_GET)
1014 get_page_foll(page);
1029 pgtable = pgtable_trans_huge_withdraw(tlb->
mm);
1030 orig_pmd = pmdp_get_and_clear(tlb->
mm, addr, pmd);
1038 spin_unlock(&tlb->
mm->page_table_lock);
1039 tlb_remove_page(tlb, page);
1047 unsigned long addr,
unsigned long end,
1057 spin_unlock(&vma->
vm_mm->page_table_lock);
1058 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1066 unsigned long old_addr,
1067 unsigned long new_addr,
unsigned long old_end,
1077 old_end - old_addr < HPAGE_PMD_SIZE ||
1078 (new_vma->
vm_flags & VM_NOHUGEPAGE))
1092 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1109 entry = pmdp_get_and_clear(mm, addr, pmd);
1110 entry = pmd_modify(entry, newprot);
1112 spin_unlock(&vma->
vm_mm->page_table_lock);
1128 spin_lock(&vma->
vm_mm->page_table_lock);
1129 if (
likely(pmd_trans_huge(*pmd))) {
1130 if (
unlikely(pmd_trans_splitting(*pmd))) {
1131 spin_unlock(&vma->
vm_mm->page_table_lock);
1140 spin_unlock(&vma->
vm_mm->page_table_lock);
1146 unsigned long address,
1177 pmd_trans_splitting(*pmd))
1179 if (pmd_trans_huge(*pmd)) {
1181 !pmd_trans_splitting(*pmd));
1188 static int __split_huge_page_splitting(
struct page *page,
1190 unsigned long address)
1196 const unsigned long mmun_start =
address;
1199 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1215 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1220 static void __split_huge_page_refcount(
struct page *page)
1223 struct zone *zone = page_zone(page);
1231 compound_lock(page);
1233 mem_cgroup_split_huge_fixup(page);
1235 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1236 struct page *page_tail = page +
i;
1239 BUG_ON(page_mapcount(page_tail) < 0);
1240 tail_count += page_mapcount(page_tail);
1257 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1272 (1
L << PG_mlocked) |
1300 BUG_ON(!PageAnon(page_tail));
1301 BUG_ON(!PageUptodate(page_tail));
1302 BUG_ON(!PageDirty(page_tail));
1303 BUG_ON(!PageSwapBacked(page_tail));
1305 lru_add_page_tail(page, page_tail, lruvec);
1313 ClearPageCompound(page);
1314 compound_unlock(page);
1318 struct page *page_tail = page +
i;
1319 BUG_ON(page_count(page_tail) <= 0);
1334 BUG_ON(page_count(page) <= 0);
1337 static int __split_huge_page_map(
struct page *page,
1339 unsigned long address)
1345 unsigned long haddr;
1351 pgtable = pgtable_trans_huge_withdraw(mm);
1357 BUG_ON(PageCompound(page+i));
1363 BUG_ON(page_mapcount(page) != 1);
1399 pmdp_invalidate(vma, address, pmd);
1409 static void __split_huge_page(
struct page *page,
1412 int mapcount, mapcount2;
1420 anon_vma_interval_tree_foreach(avc, &anon_vma->
rb_root, pgoff, pgoff) {
1424 mapcount += __split_huge_page_splitting(page, vma, addr);
1436 if (mapcount != page_mapcount(page))
1438 mapcount, page_mapcount(page));
1439 BUG_ON(mapcount != page_mapcount(page));
1441 __split_huge_page_refcount(page);
1444 anon_vma_interval_tree_foreach(avc, &anon_vma->
rb_root, pgoff, pgoff) {
1448 mapcount2 += __split_huge_page_map(page, vma, addr);
1450 if (mapcount != mapcount2)
1452 mapcount, mapcount2, page_mapcount(page));
1453 BUG_ON(mapcount != mapcount2);
1458 struct anon_vma *anon_vma;
1466 if (!PageCompound(page))
1469 BUG_ON(!PageSwapBacked(page));
1470 __split_huge_page(page, anon_vma);
1471 count_vm_event(THP_SPLIT);
1473 BUG_ON(PageCompound(page));
1480 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1483 unsigned long *
vm_flags,
int advice)
1492 if (*vm_flags & (VM_HUGEPAGE |
VM_NO_THP))
1496 *vm_flags &= ~VM_NOHUGEPAGE;
1497 *vm_flags |= VM_HUGEPAGE;
1510 if (*vm_flags & (VM_NOHUGEPAGE |
VM_NO_THP))
1512 *vm_flags &= ~VM_HUGEPAGE;
1513 *vm_flags |= VM_NOHUGEPAGE;
1525 static int __init khugepaged_slab_init(
void)
1536 static void __init khugepaged_slab_free(
void)
1539 mm_slot_cache =
NULL;
1542 static inline struct mm_slot *alloc_mm_slot(
void)
1546 return kmem_cache_zalloc(mm_slot_cache,
GFP_KERNEL);
1554 static int __init mm_slots_hash_init(
void)
1564 static void __init mm_slots_hash_free(
void)
1566 kfree(mm_slots_hash);
1567 mm_slots_hash =
NULL;
1577 bucket = &mm_slots_hash[((
unsigned long)mm /
sizeof(
struct mm_struct))
1580 if (mm == mm_slot->
mm)
1586 static void insert_to_mm_slots_hash(
struct mm_struct *mm,
1587 struct mm_slot *mm_slot)
1591 bucket = &mm_slots_hash[((
unsigned long)mm /
sizeof(
struct mm_struct))
1594 hlist_add_head(&mm_slot->
hash, bucket);
1597 static inline int khugepaged_test_exit(
struct mm_struct *mm)
1604 struct mm_slot *mm_slot;
1607 mm_slot = alloc_mm_slot();
1614 free_mm_slot(mm_slot);
1618 spin_lock(&khugepaged_mm_lock);
1619 insert_to_mm_slots_hash(mm, mm_slot);
1624 wakeup = list_empty(&khugepaged_scan.
mm_head);
1626 spin_unlock(&khugepaged_mm_lock);
1637 unsigned long hstart, hend;
1651 return khugepaged_enter(vma);
1657 struct mm_slot *mm_slot;
1660 spin_lock(&khugepaged_mm_lock);
1661 mm_slot = get_mm_slot(mm);
1662 if (mm_slot && khugepaged_scan.
mm_slot != mm_slot) {
1663 hlist_del(&mm_slot->
hash);
1667 spin_unlock(&khugepaged_mm_lock);
1671 free_mm_slot(mm_slot);
1673 }
else if (mm_slot) {
1687 static void release_pte_page(
struct page *page)
1695 static void release_pte_pages(
pte_t *pte,
pte_t *_pte)
1697 while (--_pte >= pte) {
1698 pte_t pteval = *_pte;
1700 release_pte_page(
pte_page(pteval));
1704 static void release_all_pte_pages(
pte_t *pte)
1706 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1709 static int __collapse_huge_page_isolate(
struct vm_area_struct *vma,
1710 unsigned long address,
1715 int referenced = 0, isolated = 0,
none = 0;
1718 pte_t pteval = *_pte;
1720 if (++
none <= khugepaged_max_ptes_none)
1723 release_pte_pages(pte, _pte);
1728 release_pte_pages(pte, _pte);
1733 release_pte_pages(pte, _pte);
1741 if (page_count(page) != 1) {
1742 release_pte_pages(pte, _pte);
1751 if (!trylock_page(page)) {
1752 release_pte_pages(pte, _pte);
1761 release_pte_pages(pte, _pte);
1770 if (
pte_young(pteval) || PageReferenced(page) ||
1771 mmu_notifier_test_young(vma->
vm_mm, address))
1775 release_all_pte_pages(pte);
1782 static void __collapse_huge_page_copy(
pte_t *pte,
struct page *page,
1784 unsigned long address,
1789 pte_t pteval = *_pte;
1790 struct page *src_page;
1798 VM_BUG_ON(page_mapcount(src_page) != 1);
1799 release_pte_page(src_page);
1821 static void khugepaged_alloc_sleep(
void)
1828 static bool khugepaged_prealloc_page(
struct page **hpage,
bool *
wait)
1830 if (IS_ERR(*hpage)) {
1836 khugepaged_alloc_sleep();
1837 }
else if (*hpage) {
1846 *khugepaged_alloc_page(
struct page **hpage,
struct mm_struct *mm,
1861 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
1870 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1871 *hpage = ERR_PTR(-
ENOMEM);
1875 count_vm_event(THP_COLLAPSE_ALLOC);
1879 static struct page *khugepaged_alloc_hugepage(
bool *wait)
1884 hpage = alloc_hugepage(khugepaged_defrag());
1886 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
1891 khugepaged_alloc_sleep();
1893 count_vm_event(THP_COLLAPSE_ALLOC);
1899 static bool khugepaged_prealloc_page(
struct page **hpage,
bool *wait)
1902 *hpage = khugepaged_alloc_hugepage(wait);
1911 *khugepaged_alloc_page(
struct page **hpage,
struct mm_struct *mm,
1921 static void collapse_huge_page(
struct mm_struct *mm,
1922 unsigned long address,
1923 struct page **hpage,
1932 struct page *new_page;
1935 unsigned long hstart, hend;
1936 unsigned long mmun_start;
1937 unsigned long mmun_end;
1942 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
1955 if (
unlikely(khugepaged_test_exit(mm)))
1964 if ((!(vma->
vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1990 ptl = pte_lockptr(mm, pmd);
1994 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2002 _pmd = pmdp_clear_flush(vma, address, pmd);
2004 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2007 isolated = __collapse_huge_page_isolate(vma, address, pte);
2026 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2028 __SetPageUptodate(new_page);
2047 pgtable_trans_huge_deposit(mm, pgtable);
2052 khugepaged_pages_collapsed++;
2062 static int khugepaged_scan_pmd(
struct mm_struct *mm,
2064 unsigned long address,
2065 struct page **hpage)
2071 int ret = 0, referenced = 0,
none = 0;
2073 unsigned long _address;
2091 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2092 for (_address = address, _pte = pte; _pte < pte+
HPAGE_PMD_NR;
2094 pte_t pteval = *_pte;
2096 if (++
none <= khugepaged_max_ptes_none)
2112 node = page_to_nid(page);
2114 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2117 if (page_count(page) != 1)
2119 if (
pte_young(pteval) || PageReferenced(page) ||
2120 mmu_notifier_test_young(vma->
vm_mm, address))
2126 pte_unmap_unlock(pte, ptl);
2129 collapse_huge_page(mm, address, hpage, vma, node);
2134 static void collect_mm_slot(
struct mm_slot *mm_slot)
2140 if (khugepaged_test_exit(mm)) {
2142 hlist_del(&mm_slot->
hash);
2152 free_mm_slot(mm_slot);
2157 static unsigned int khugepaged_scan_mm_slot(
unsigned int pages,
2158 struct page **hpage)
2162 struct mm_slot *mm_slot;
2171 mm_slot = khugepaged_scan.
mm_slot;
2174 struct mm_slot, mm_node);
2176 khugepaged_scan.
mm_slot = mm_slot;
2178 spin_unlock(&khugepaged_mm_lock);
2182 if (
unlikely(khugepaged_test_exit(mm)))
2188 for (; vma; vma = vma->
vm_next) {
2189 unsigned long hstart, hend;
2192 if (
unlikely(khugepaged_test_exit(mm))) {
2197 if ((!(vma->
vm_flags & VM_HUGEPAGE) &&
2198 !khugepaged_always()) ||
2214 if (khugepaged_scan.
address > hend)
2216 if (khugepaged_scan.
address < hstart)
2217 khugepaged_scan.
address = hstart;
2220 while (khugepaged_scan.
address < hend) {
2223 if (
unlikely(khugepaged_test_exit(mm)))
2224 goto breakouterloop;
2227 khugepaged_scan.
address + HPAGE_PMD_SIZE >
2229 ret = khugepaged_scan_pmd(mm, vma,
2237 goto breakouterloop_mmap_sem;
2238 if (progress >= pages)
2239 goto breakouterloop;
2244 breakouterloop_mmap_sem:
2246 spin_lock(&khugepaged_mm_lock);
2252 if (khugepaged_test_exit(mm) || !vma) {
2261 struct mm_slot, mm_node);
2265 khugepaged_full_scans++;
2268 collect_mm_slot(mm_slot);
2274 static int khugepaged_has_work(
void)
2276 return !list_empty(&khugepaged_scan.
mm_head) &&
2277 khugepaged_enabled();
2280 static int khugepaged_wait_event(
void)
2282 return !list_empty(&khugepaged_scan.
mm_head) ||
2286 static void khugepaged_do_scan(
void)
2288 struct page *hpage =
NULL;
2289 unsigned int progress = 0, pass_through_head = 0;
2290 unsigned int pages = khugepaged_pages_to_scan;
2295 while (progress < pages) {
2296 if (!khugepaged_prealloc_page(&hpage, &wait))
2304 spin_lock(&khugepaged_mm_lock);
2306 pass_through_head++;
2307 if (khugepaged_has_work() &&
2308 pass_through_head < 2)
2309 progress += khugepaged_scan_mm_slot(pages - progress,
2313 spin_unlock(&khugepaged_mm_lock);
2316 if (!IS_ERR_OR_NULL(hpage))
2320 static void khugepaged_wait_work(
void)
2324 if (khugepaged_has_work()) {
2325 if (!khugepaged_scan_sleep_millisecs)
2334 if (khugepaged_enabled())
2338 static int khugepaged(
void *
none)
2340 struct mm_slot *mm_slot;
2346 khugepaged_do_scan();
2347 khugepaged_wait_work();
2350 spin_lock(&khugepaged_mm_lock);
2351 mm_slot = khugepaged_scan.
mm_slot;
2354 collect_mm_slot(mm_slot);
2355 spin_unlock(&khugepaged_mm_lock);
2364 if (
unlikely(!pmd_trans_huge(*pmd))) {
2376 BUG_ON(pmd_trans_huge(*pmd));
2379 static void split_huge_page_address(
struct mm_struct *mm,
2380 unsigned long address)
2407 unsigned long start,
2418 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2419 split_huge_page_address(vma->
vm_mm, start);
2426 if (end & ~HPAGE_PMD_MASK &&
2427 (end & HPAGE_PMD_MASK) >= vma->
vm_start &&
2428 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2429 split_huge_page_address(vma->
vm_mm, end);
2436 if (adjust_next > 0) {
2438 unsigned long nstart = next->
vm_start;
2440 if (nstart & ~HPAGE_PMD_MASK &&
2441 (nstart & HPAGE_PMD_MASK) >= next->
vm_start &&
2442 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2443 split_huge_page_address(next->
vm_mm, nstart);