khugepaged.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/mm.h>
  3. #include <linux/sched.h>
  4. #include <linux/mmu_notifier.h>
  5. #include <linux/rmap.h>
  6. #include <linux/swap.h>
  7. #include <linux/mm_inline.h>
  8. #include <linux/kthread.h>
  9. #include <linux/khugepaged.h>
  10. #include <linux/freezer.h>
  11. #include <linux/mman.h>
  12. #include <linux/hashtable.h>
  13. #include <linux/userfaultfd_k.h>
  14. #include <linux/page_idle.h>
  15. #include <linux/swapops.h>
  16. #include <linux/shmem_fs.h>
  17. #include <asm/tlb.h>
  18. #include <asm/pgalloc.h>
  19. #include "internal.h"
  20. enum scan_result {
  21. SCAN_FAIL,
  22. SCAN_SUCCEED,
  23. SCAN_PMD_NULL,
  24. SCAN_EXCEED_NONE_PTE,
  25. SCAN_PTE_NON_PRESENT,
  26. SCAN_PAGE_RO,
  27. SCAN_LACK_REFERENCED_PAGE,
  28. SCAN_PAGE_NULL,
  29. SCAN_SCAN_ABORT,
  30. SCAN_PAGE_COUNT,
  31. SCAN_PAGE_LRU,
  32. SCAN_PAGE_LOCK,
  33. SCAN_PAGE_ANON,
  34. SCAN_PAGE_COMPOUND,
  35. SCAN_ANY_PROCESS,
  36. SCAN_VMA_NULL,
  37. SCAN_VMA_CHECK,
  38. SCAN_ADDRESS_RANGE,
  39. SCAN_SWAP_CACHE_PAGE,
  40. SCAN_DEL_PAGE_LRU,
  41. SCAN_ALLOC_HUGE_PAGE_FAIL,
  42. SCAN_CGROUP_CHARGE_FAIL,
  43. SCAN_EXCEED_SWAP_PTE,
  44. SCAN_TRUNCATED,
  45. };
  46. #define CREATE_TRACE_POINTS
  47. #include <trace/events/huge_memory.h>
  48. /* default scan 8*512 pte (or vmas) every 30 second */
  49. static unsigned int khugepaged_pages_to_scan __read_mostly;
  50. static unsigned int khugepaged_pages_collapsed;
  51. static unsigned int khugepaged_full_scans;
  52. static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
  53. /* during fragmentation poll the hugepage allocator once every minute */
  54. static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
  55. static unsigned long khugepaged_sleep_expire;
  56. static DEFINE_SPINLOCK(khugepaged_mm_lock);
  57. static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
  58. /*
  59. * default collapse hugepages if there is at least one pte mapped like
  60. * it would have happened if the vma was large enough during page
  61. * fault.
  62. */
  63. static unsigned int khugepaged_max_ptes_none __read_mostly;
  64. static unsigned int khugepaged_max_ptes_swap __read_mostly;
  65. #define MM_SLOTS_HASH_BITS 10
  66. static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
  67. static struct kmem_cache *mm_slot_cache __read_mostly;
  68. /**
  69. * struct mm_slot - hash lookup from mm to mm_slot
  70. * @hash: hash collision list
  71. * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
  72. * @mm: the mm that this information is valid for
  73. */
  74. struct mm_slot {
  75. struct hlist_node hash;
  76. struct list_head mm_node;
  77. struct mm_struct *mm;
  78. };
  79. /**
  80. * struct khugepaged_scan - cursor for scanning
  81. * @mm_head: the head of the mm list to scan
  82. * @mm_slot: the current mm_slot we are scanning
  83. * @address: the next address inside that to be scanned
  84. *
  85. * There is only the one khugepaged_scan instance of this cursor structure.
  86. */
  87. struct khugepaged_scan {
  88. struct list_head mm_head;
  89. struct mm_slot *mm_slot;
  90. unsigned long address;
  91. };
  92. static struct khugepaged_scan khugepaged_scan = {
  93. .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
  94. };
  95. #ifdef CONFIG_SYSFS
  96. static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
  97. struct kobj_attribute *attr,
  98. char *buf)
  99. {
  100. return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
  101. }
  102. static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
  103. struct kobj_attribute *attr,
  104. const char *buf, size_t count)
  105. {
  106. unsigned long msecs;
  107. int err;
  108. err = kstrtoul(buf, 10, &msecs);
  109. if (err || msecs > UINT_MAX)
  110. return -EINVAL;
  111. khugepaged_scan_sleep_millisecs = msecs;
  112. khugepaged_sleep_expire = 0;
  113. wake_up_interruptible(&khugepaged_wait);
  114. return count;
  115. }
  116. static struct kobj_attribute scan_sleep_millisecs_attr =
  117. __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
  118. scan_sleep_millisecs_store);
  119. static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
  120. struct kobj_attribute *attr,
  121. char *buf)
  122. {
  123. return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
  124. }
  125. static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
  126. struct kobj_attribute *attr,
  127. const char *buf, size_t count)
  128. {
  129. unsigned long msecs;
  130. int err;
  131. err = kstrtoul(buf, 10, &msecs);
  132. if (err || msecs > UINT_MAX)
  133. return -EINVAL;
  134. khugepaged_alloc_sleep_millisecs = msecs;
  135. khugepaged_sleep_expire = 0;
  136. wake_up_interruptible(&khugepaged_wait);
  137. return count;
  138. }
  139. static struct kobj_attribute alloc_sleep_millisecs_attr =
  140. __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
  141. alloc_sleep_millisecs_store);
  142. static ssize_t pages_to_scan_show(struct kobject *kobj,
  143. struct kobj_attribute *attr,
  144. char *buf)
  145. {
  146. return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
  147. }
  148. static ssize_t pages_to_scan_store(struct kobject *kobj,
  149. struct kobj_attribute *attr,
  150. const char *buf, size_t count)
  151. {
  152. int err;
  153. unsigned long pages;
  154. err = kstrtoul(buf, 10, &pages);
  155. if (err || !pages || pages > UINT_MAX)
  156. return -EINVAL;
  157. khugepaged_pages_to_scan = pages;
  158. return count;
  159. }
  160. static struct kobj_attribute pages_to_scan_attr =
  161. __ATTR(pages_to_scan, 0644, pages_to_scan_show,
  162. pages_to_scan_store);
  163. static ssize_t pages_collapsed_show(struct kobject *kobj,
  164. struct kobj_attribute *attr,
  165. char *buf)
  166. {
  167. return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
  168. }
  169. static struct kobj_attribute pages_collapsed_attr =
  170. __ATTR_RO(pages_collapsed);
  171. static ssize_t full_scans_show(struct kobject *kobj,
  172. struct kobj_attribute *attr,
  173. char *buf)
  174. {
  175. return sprintf(buf, "%u\n", khugepaged_full_scans);
  176. }
  177. static struct kobj_attribute full_scans_attr =
  178. __ATTR_RO(full_scans);
  179. static ssize_t khugepaged_defrag_show(struct kobject *kobj,
  180. struct kobj_attribute *attr, char *buf)
  181. {
  182. return single_hugepage_flag_show(kobj, attr, buf,
  183. TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  184. }
  185. static ssize_t khugepaged_defrag_store(struct kobject *kobj,
  186. struct kobj_attribute *attr,
  187. const char *buf, size_t count)
  188. {
  189. return single_hugepage_flag_store(kobj, attr, buf, count,
  190. TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  191. }
  192. static struct kobj_attribute khugepaged_defrag_attr =
  193. __ATTR(defrag, 0644, khugepaged_defrag_show,
  194. khugepaged_defrag_store);
  195. /*
  196. * max_ptes_none controls if khugepaged should collapse hugepages over
  197. * any unmapped ptes in turn potentially increasing the memory
  198. * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
  199. * reduce the available free memory in the system as it
  200. * runs. Increasing max_ptes_none will instead potentially reduce the
  201. * free memory in the system during the khugepaged scan.
  202. */
  203. static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
  204. struct kobj_attribute *attr,
  205. char *buf)
  206. {
  207. return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
  208. }
  209. static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
  210. struct kobj_attribute *attr,
  211. const char *buf, size_t count)
  212. {
  213. int err;
  214. unsigned long max_ptes_none;
  215. err = kstrtoul(buf, 10, &max_ptes_none);
  216. if (err || max_ptes_none > HPAGE_PMD_NR-1)
  217. return -EINVAL;
  218. khugepaged_max_ptes_none = max_ptes_none;
  219. return count;
  220. }
  221. static struct kobj_attribute khugepaged_max_ptes_none_attr =
  222. __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
  223. khugepaged_max_ptes_none_store);
  224. static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
  225. struct kobj_attribute *attr,
  226. char *buf)
  227. {
  228. return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
  229. }
  230. static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
  231. struct kobj_attribute *attr,
  232. const char *buf, size_t count)
  233. {
  234. int err;
  235. unsigned long max_ptes_swap;
  236. err = kstrtoul(buf, 10, &max_ptes_swap);
  237. if (err || max_ptes_swap > HPAGE_PMD_NR-1)
  238. return -EINVAL;
  239. khugepaged_max_ptes_swap = max_ptes_swap;
  240. return count;
  241. }
  242. static struct kobj_attribute khugepaged_max_ptes_swap_attr =
  243. __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
  244. khugepaged_max_ptes_swap_store);
  245. static struct attribute *khugepaged_attr[] = {
  246. &khugepaged_defrag_attr.attr,
  247. &khugepaged_max_ptes_none_attr.attr,
  248. &pages_to_scan_attr.attr,
  249. &pages_collapsed_attr.attr,
  250. &full_scans_attr.attr,
  251. &scan_sleep_millisecs_attr.attr,
  252. &alloc_sleep_millisecs_attr.attr,
  253. &khugepaged_max_ptes_swap_attr.attr,
  254. NULL,
  255. };
  256. struct attribute_group khugepaged_attr_group = {
  257. .attrs = khugepaged_attr,
  258. .name = "khugepaged",
  259. };
  260. #endif /* CONFIG_SYSFS */
  261. #define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
  262. int hugepage_madvise(struct vm_area_struct *vma,
  263. unsigned long *vm_flags, int advice)
  264. {
  265. switch (advice) {
  266. case MADV_HUGEPAGE:
  267. #ifdef CONFIG_S390
  268. /*
  269. * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
  270. * can't handle this properly after s390_enable_sie, so we simply
  271. * ignore the madvise to prevent qemu from causing a SIGSEGV.
  272. */
  273. if (mm_has_pgste(vma->vm_mm))
  274. return 0;
  275. #endif
  276. *vm_flags &= ~VM_NOHUGEPAGE;
  277. *vm_flags |= VM_HUGEPAGE;
  278. /*
  279. * If the vma become good for khugepaged to scan,
  280. * register it here without waiting a page fault that
  281. * may not happen any time soon.
  282. */
  283. if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
  284. khugepaged_enter_vma_merge(vma, *vm_flags))
  285. return -ENOMEM;
  286. break;
  287. case MADV_NOHUGEPAGE:
  288. *vm_flags &= ~VM_HUGEPAGE;
  289. *vm_flags |= VM_NOHUGEPAGE;
  290. /*
  291. * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
  292. * this vma even if we leave the mm registered in khugepaged if
  293. * it got registered before VM_NOHUGEPAGE was set.
  294. */
  295. break;
  296. }
  297. return 0;
  298. }
  299. int __init khugepaged_init(void)
  300. {
  301. mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
  302. sizeof(struct mm_slot),
  303. __alignof__(struct mm_slot), 0, NULL);
  304. if (!mm_slot_cache)
  305. return -ENOMEM;
  306. khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
  307. khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
  308. khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
  309. return 0;
  310. }
  311. void __init khugepaged_destroy(void)
  312. {
  313. kmem_cache_destroy(mm_slot_cache);
  314. }
  315. static inline struct mm_slot *alloc_mm_slot(void)
  316. {
  317. if (!mm_slot_cache) /* initialization failed */
  318. return NULL;
  319. return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
  320. }
  321. static inline void free_mm_slot(struct mm_slot *mm_slot)
  322. {
  323. kmem_cache_free(mm_slot_cache, mm_slot);
  324. }
  325. static struct mm_slot *get_mm_slot(struct mm_struct *mm)
  326. {
  327. struct mm_slot *mm_slot;
  328. hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
  329. if (mm == mm_slot->mm)
  330. return mm_slot;
  331. return NULL;
  332. }
  333. static void insert_to_mm_slots_hash(struct mm_struct *mm,
  334. struct mm_slot *mm_slot)
  335. {
  336. mm_slot->mm = mm;
  337. hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
  338. }
  339. static inline int khugepaged_test_exit(struct mm_struct *mm)
  340. {
  341. return atomic_read(&mm->mm_users) == 0;
  342. }
  343. int __khugepaged_enter(struct mm_struct *mm)
  344. {
  345. struct mm_slot *mm_slot;
  346. int wakeup;
  347. mm_slot = alloc_mm_slot();
  348. if (!mm_slot)
  349. return -ENOMEM;
  350. /* __khugepaged_exit() must not run from under us */
  351. VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
  352. if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
  353. free_mm_slot(mm_slot);
  354. return 0;
  355. }
  356. spin_lock(&khugepaged_mm_lock);
  357. insert_to_mm_slots_hash(mm, mm_slot);
  358. /*
  359. * Insert just behind the scanning cursor, to let the area settle
  360. * down a little.
  361. */
  362. wakeup = list_empty(&khugepaged_scan.mm_head);
  363. list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
  364. spin_unlock(&khugepaged_mm_lock);
  365. atomic_inc(&mm->mm_count);
  366. if (wakeup)
  367. wake_up_interruptible(&khugepaged_wait);
  368. return 0;
  369. }
  370. int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
  371. unsigned long vm_flags)
  372. {
  373. unsigned long hstart, hend;
  374. if (!vma->anon_vma)
  375. /*
  376. * Not yet faulted in so we will register later in the
  377. * page fault if needed.
  378. */
  379. return 0;
  380. if (vma->vm_ops || (vm_flags & VM_NO_KHUGEPAGED))
  381. /* khugepaged not yet working on file or special mappings */
  382. return 0;
  383. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  384. hend = vma->vm_end & HPAGE_PMD_MASK;
  385. if (hstart < hend)
  386. return khugepaged_enter(vma, vm_flags);
  387. return 0;
  388. }
  389. void __khugepaged_exit(struct mm_struct *mm)
  390. {
  391. struct mm_slot *mm_slot;
  392. int free = 0;
  393. spin_lock(&khugepaged_mm_lock);
  394. mm_slot = get_mm_slot(mm);
  395. if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
  396. hash_del(&mm_slot->hash);
  397. list_del(&mm_slot->mm_node);
  398. free = 1;
  399. }
  400. spin_unlock(&khugepaged_mm_lock);
  401. if (free) {
  402. clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  403. free_mm_slot(mm_slot);
  404. mmdrop(mm);
  405. } else if (mm_slot) {
  406. /*
  407. * This is required to serialize against
  408. * khugepaged_test_exit() (which is guaranteed to run
  409. * under mmap sem read mode). Stop here (after we
  410. * return all pagetables will be destroyed) until
  411. * khugepaged has finished working on the pagetables
  412. * under the mmap_sem.
  413. */
  414. down_write(&mm->mmap_sem);
  415. up_write(&mm->mmap_sem);
  416. }
  417. }
  418. static void release_pte_page(struct page *page)
  419. {
  420. /* 0 stands for page_is_file_cache(page) == false */
  421. dec_node_page_state(page, NR_ISOLATED_ANON + 0);
  422. unlock_page(page);
  423. putback_lru_page(page);
  424. }
  425. static void release_pte_pages(pte_t *pte, pte_t *_pte)
  426. {
  427. while (--_pte >= pte) {
  428. pte_t pteval = *_pte;
  429. if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
  430. release_pte_page(pte_page(pteval));
  431. }
  432. }
  433. static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  434. unsigned long address,
  435. pte_t *pte)
  436. {
  437. struct page *page = NULL;
  438. pte_t *_pte;
  439. int none_or_zero = 0, result = 0, referenced = 0;
  440. bool writable = false;
  441. for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
  442. _pte++, address += PAGE_SIZE) {
  443. pte_t pteval = *_pte;
  444. if (pte_none(pteval) || (pte_present(pteval) &&
  445. is_zero_pfn(pte_pfn(pteval)))) {
  446. if (!userfaultfd_armed(vma) &&
  447. ++none_or_zero <= khugepaged_max_ptes_none) {
  448. continue;
  449. } else {
  450. result = SCAN_EXCEED_NONE_PTE;
  451. goto out;
  452. }
  453. }
  454. if (!pte_present(pteval)) {
  455. result = SCAN_PTE_NON_PRESENT;
  456. goto out;
  457. }
  458. page = vm_normal_page(vma, address, pteval);
  459. if (unlikely(!page)) {
  460. result = SCAN_PAGE_NULL;
  461. goto out;
  462. }
  463. /* TODO: teach khugepaged to collapse THP mapped with pte */
  464. if (PageCompound(page)) {
  465. result = SCAN_PAGE_COMPOUND;
  466. goto out;
  467. }
  468. VM_BUG_ON_PAGE(!PageAnon(page), page);
  469. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  470. /*
  471. * We can do it before isolate_lru_page because the
  472. * page can't be freed from under us. NOTE: PG_lock
  473. * is needed to serialize against split_huge_page
  474. * when invoked from the VM.
  475. */
  476. if (!trylock_page(page)) {
  477. result = SCAN_PAGE_LOCK;
  478. goto out;
  479. }
  480. /*
  481. * cannot use mapcount: can't collapse if there's a gup pin.
  482. * The page must only be referenced by the scanned process
  483. * and page swap cache.
  484. */
  485. if (page_count(page) != 1 + !!PageSwapCache(page)) {
  486. unlock_page(page);
  487. result = SCAN_PAGE_COUNT;
  488. goto out;
  489. }
  490. if (pte_write(pteval)) {
  491. writable = true;
  492. } else {
  493. if (PageSwapCache(page) &&
  494. !reuse_swap_page(page, NULL)) {
  495. unlock_page(page);
  496. result = SCAN_SWAP_CACHE_PAGE;
  497. goto out;
  498. }
  499. /*
  500. * Page is not in the swap cache. It can be collapsed
  501. * into a THP.
  502. */
  503. }
  504. /*
  505. * Isolate the page to avoid collapsing an hugepage
  506. * currently in use by the VM.
  507. */
  508. if (isolate_lru_page(page)) {
  509. unlock_page(page);
  510. result = SCAN_DEL_PAGE_LRU;
  511. goto out;
  512. }
  513. /* 0 stands for page_is_file_cache(page) == false */
  514. inc_node_page_state(page, NR_ISOLATED_ANON + 0);
  515. VM_BUG_ON_PAGE(!PageLocked(page), page);
  516. VM_BUG_ON_PAGE(PageLRU(page), page);
  517. /* There should be enough young pte to collapse the page */
  518. if (pte_young(pteval) ||
  519. page_is_young(page) || PageReferenced(page) ||
  520. mmu_notifier_test_young(vma->vm_mm, address))
  521. referenced++;
  522. }
  523. if (likely(writable)) {
  524. if (likely(referenced)) {
  525. result = SCAN_SUCCEED;
  526. trace_mm_collapse_huge_page_isolate(page, none_or_zero,
  527. referenced, writable, result);
  528. return 1;
  529. }
  530. } else {
  531. result = SCAN_PAGE_RO;
  532. }
  533. out:
  534. release_pte_pages(pte, _pte);
  535. trace_mm_collapse_huge_page_isolate(page, none_or_zero,
  536. referenced, writable, result);
  537. return 0;
  538. }
  539. static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
  540. struct vm_area_struct *vma,
  541. unsigned long address,
  542. spinlock_t *ptl)
  543. {
  544. pte_t *_pte;
  545. for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
  546. pte_t pteval = *_pte;
  547. struct page *src_page;
  548. if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  549. clear_user_highpage(page, address);
  550. add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
  551. if (is_zero_pfn(pte_pfn(pteval))) {
  552. /*
  553. * ptl mostly unnecessary.
  554. */
  555. spin_lock(ptl);
  556. /*
  557. * paravirt calls inside pte_clear here are
  558. * superfluous.
  559. */
  560. pte_clear(vma->vm_mm, address, _pte);
  561. spin_unlock(ptl);
  562. }
  563. } else {
  564. src_page = pte_page(pteval);
  565. copy_user_highpage(page, src_page, address, vma);
  566. VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
  567. release_pte_page(src_page);
  568. /*
  569. * ptl mostly unnecessary, but preempt has to
  570. * be disabled to update the per-cpu stats
  571. * inside page_remove_rmap().
  572. */
  573. spin_lock(ptl);
  574. /*
  575. * paravirt calls inside pte_clear here are
  576. * superfluous.
  577. */
  578. pte_clear(vma->vm_mm, address, _pte);
  579. page_remove_rmap(src_page, false);
  580. spin_unlock(ptl);
  581. free_page_and_swap_cache(src_page);
  582. }
  583. address += PAGE_SIZE;
  584. page++;
  585. }
  586. }
  587. static void khugepaged_alloc_sleep(void)
  588. {
  589. DEFINE_WAIT(wait);
  590. add_wait_queue(&khugepaged_wait, &wait);
  591. freezable_schedule_timeout_interruptible(
  592. msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
  593. remove_wait_queue(&khugepaged_wait, &wait);
  594. }
  595. static int khugepaged_node_load[MAX_NUMNODES];
  596. static bool khugepaged_scan_abort(int nid)
  597. {
  598. int i;
  599. /*
  600. * If node_reclaim_mode is disabled, then no extra effort is made to
  601. * allocate memory locally.
  602. */
  603. if (!node_reclaim_mode)
  604. return false;
  605. /* If there is a count for this node already, it must be acceptable */
  606. if (khugepaged_node_load[nid])
  607. return false;
  608. for (i = 0; i < MAX_NUMNODES; i++) {
  609. if (!khugepaged_node_load[i])
  610. continue;
  611. if (node_distance(nid, i) > RECLAIM_DISTANCE)
  612. return true;
  613. }
  614. return false;
  615. }
  616. /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
  617. static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
  618. {
  619. return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
  620. }
  621. #ifdef CONFIG_NUMA
  622. static int khugepaged_find_target_node(void)
  623. {
  624. static int last_khugepaged_target_node = NUMA_NO_NODE;
  625. int nid, target_node = 0, max_value = 0;
  626. /* find first node with max normal pages hit */
  627. for (nid = 0; nid < MAX_NUMNODES; nid++)
  628. if (khugepaged_node_load[nid] > max_value) {
  629. max_value = khugepaged_node_load[nid];
  630. target_node = nid;
  631. }
  632. /* do some balance if several nodes have the same hit record */
  633. if (target_node <= last_khugepaged_target_node)
  634. for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
  635. nid++)
  636. if (max_value == khugepaged_node_load[nid]) {
  637. target_node = nid;
  638. break;
  639. }
  640. last_khugepaged_target_node = target_node;
  641. return target_node;
  642. }
  643. static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
  644. {
  645. if (IS_ERR(*hpage)) {
  646. if (!*wait)
  647. return false;
  648. *wait = false;
  649. *hpage = NULL;
  650. khugepaged_alloc_sleep();
  651. } else if (*hpage) {
  652. put_page(*hpage);
  653. *hpage = NULL;
  654. }
  655. return true;
  656. }
  657. static struct page *
  658. khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
  659. {
  660. VM_BUG_ON_PAGE(*hpage, *hpage);
  661. *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
  662. if (unlikely(!*hpage)) {
  663. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  664. *hpage = ERR_PTR(-ENOMEM);
  665. return NULL;
  666. }
  667. prep_transhuge_page(*hpage);
  668. count_vm_event(THP_COLLAPSE_ALLOC);
  669. return *hpage;
  670. }
  671. #else
  672. static int khugepaged_find_target_node(void)
  673. {
  674. return 0;
  675. }
  676. static inline struct page *alloc_khugepaged_hugepage(void)
  677. {
  678. struct page *page;
  679. page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
  680. HPAGE_PMD_ORDER);
  681. if (page)
  682. prep_transhuge_page(page);
  683. return page;
  684. }
  685. static struct page *khugepaged_alloc_hugepage(bool *wait)
  686. {
  687. struct page *hpage;
  688. do {
  689. hpage = alloc_khugepaged_hugepage();
  690. if (!hpage) {
  691. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  692. if (!*wait)
  693. return NULL;
  694. *wait = false;
  695. khugepaged_alloc_sleep();
  696. } else
  697. count_vm_event(THP_COLLAPSE_ALLOC);
  698. } while (unlikely(!hpage) && likely(khugepaged_enabled()));
  699. return hpage;
  700. }
  701. static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
  702. {
  703. if (!*hpage)
  704. *hpage = khugepaged_alloc_hugepage(wait);
  705. if (unlikely(!*hpage))
  706. return false;
  707. return true;
  708. }
  709. static struct page *
  710. khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
  711. {
  712. VM_BUG_ON(!*hpage);
  713. return *hpage;
  714. }
  715. #endif
  716. static bool hugepage_vma_check(struct vm_area_struct *vma)
  717. {
  718. if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
  719. (vma->vm_flags & VM_NOHUGEPAGE))
  720. return false;
  721. if (shmem_file(vma->vm_file)) {
  722. if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
  723. return false;
  724. return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
  725. HPAGE_PMD_NR);
  726. }
  727. if (!vma->anon_vma || vma->vm_ops)
  728. return false;
  729. if (is_vma_temporary_stack(vma))
  730. return false;
  731. return !(vma->vm_flags & VM_NO_KHUGEPAGED);
  732. }
  733. /*
  734. * If mmap_sem temporarily dropped, revalidate vma
  735. * before taking mmap_sem.
  736. * Return 0 if succeeds, otherwise return none-zero
  737. * value (scan code).
  738. */
  739. static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
  740. struct vm_area_struct **vmap)
  741. {
  742. struct vm_area_struct *vma;
  743. unsigned long hstart, hend;
  744. if (unlikely(khugepaged_test_exit(mm)))
  745. return SCAN_ANY_PROCESS;
  746. *vmap = vma = find_vma(mm, address);
  747. if (!vma)
  748. return SCAN_VMA_NULL;
  749. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  750. hend = vma->vm_end & HPAGE_PMD_MASK;
  751. if (address < hstart || address + HPAGE_PMD_SIZE > hend)
  752. return SCAN_ADDRESS_RANGE;
  753. if (!hugepage_vma_check(vma))
  754. return SCAN_VMA_CHECK;
  755. return 0;
  756. }
  757. /*
  758. * Bring missing pages in from swap, to complete THP collapse.
  759. * Only done if khugepaged_scan_pmd believes it is worthwhile.
  760. *
  761. * Called and returns without pte mapped or spinlocks held,
  762. * but with mmap_sem held to protect against vma changes.
  763. */
  764. static bool __collapse_huge_page_swapin(struct mm_struct *mm,
  765. struct vm_area_struct *vma,
  766. unsigned long address, pmd_t *pmd,
  767. int referenced)
  768. {
  769. pte_t pteval;
  770. int swapped_in = 0, ret = 0;
  771. struct fault_env fe = {
  772. .vma = vma,
  773. .address = address,
  774. .flags = FAULT_FLAG_ALLOW_RETRY,
  775. .pmd = pmd,
  776. };
  777. /* we only decide to swapin, if there is enough young ptes */
  778. if (referenced < HPAGE_PMD_NR/2) {
  779. trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
  780. return false;
  781. }
  782. fe.pte = pte_offset_map(pmd, address);
  783. for (; fe.address < address + HPAGE_PMD_NR*PAGE_SIZE;
  784. fe.pte++, fe.address += PAGE_SIZE) {
  785. pteval = *fe.pte;
  786. if (!is_swap_pte(pteval))
  787. continue;
  788. swapped_in++;
  789. ret = do_swap_page(&fe, pteval);
  790. /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
  791. if (ret & VM_FAULT_RETRY) {
  792. down_read(&mm->mmap_sem);
  793. if (hugepage_vma_revalidate(mm, address, &fe.vma)) {
  794. /* vma is no longer available, don't continue to swapin */
  795. trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
  796. return false;
  797. }
  798. /* check if the pmd is still valid */
  799. if (mm_find_pmd(mm, address) != pmd)
  800. return false;
  801. }
  802. if (ret & VM_FAULT_ERROR) {
  803. trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
  804. return false;
  805. }
  806. /* pte is unmapped now, we need to map it */
  807. fe.pte = pte_offset_map(pmd, fe.address);
  808. }
  809. fe.pte--;
  810. pte_unmap(fe.pte);
  811. trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
  812. return true;
  813. }
  814. static void collapse_huge_page(struct mm_struct *mm,
  815. unsigned long address,
  816. struct page **hpage,
  817. int node, int referenced)
  818. {
  819. pmd_t *pmd, _pmd;
  820. pte_t *pte;
  821. pgtable_t pgtable;
  822. struct page *new_page;
  823. spinlock_t *pmd_ptl, *pte_ptl;
  824. int isolated = 0, result = 0;
  825. struct mem_cgroup *memcg;
  826. struct vm_area_struct *vma;
  827. unsigned long mmun_start; /* For mmu_notifiers */
  828. unsigned long mmun_end; /* For mmu_notifiers */
  829. gfp_t gfp;
  830. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  831. /* Only allocate from the target node */
  832. gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
  833. /*
  834. * Before allocating the hugepage, release the mmap_sem read lock.
  835. * The allocation can take potentially a long time if it involves
  836. * sync compaction, and we do not need to hold the mmap_sem during
  837. * that. We will recheck the vma after taking it again in write mode.
  838. */
  839. up_read(&mm->mmap_sem);
  840. new_page = khugepaged_alloc_page(hpage, gfp, node);
  841. if (!new_page) {
  842. result = SCAN_ALLOC_HUGE_PAGE_FAIL;
  843. goto out_nolock;
  844. }
  845. /* Do not oom kill for khugepaged charges */
  846. if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp | __GFP_NORETRY,
  847. &memcg, true))) {
  848. result = SCAN_CGROUP_CHARGE_FAIL;
  849. goto out_nolock;
  850. }
  851. down_read(&mm->mmap_sem);
  852. result = hugepage_vma_revalidate(mm, address, &vma);
  853. if (result) {
  854. mem_cgroup_cancel_charge(new_page, memcg, true);
  855. up_read(&mm->mmap_sem);
  856. goto out_nolock;
  857. }
  858. pmd = mm_find_pmd(mm, address);
  859. if (!pmd) {
  860. result = SCAN_PMD_NULL;
  861. mem_cgroup_cancel_charge(new_page, memcg, true);
  862. up_read(&mm->mmap_sem);
  863. goto out_nolock;
  864. }
  865. /*
  866. * __collapse_huge_page_swapin always returns with mmap_sem locked.
  867. * If it fails, we release mmap_sem and jump out_nolock.
  868. * Continuing to collapse causes inconsistency.
  869. */
  870. if (!__collapse_huge_page_swapin(mm, vma, address, pmd, referenced)) {
  871. mem_cgroup_cancel_charge(new_page, memcg, true);
  872. up_read(&mm->mmap_sem);
  873. goto out_nolock;
  874. }
  875. up_read(&mm->mmap_sem);
  876. /*
  877. * Prevent all access to pagetables with the exception of
  878. * gup_fast later handled by the ptep_clear_flush and the VM
  879. * handled by the anon_vma lock + PG_lock.
  880. */
  881. down_write(&mm->mmap_sem);
  882. result = SCAN_ANY_PROCESS;
  883. if (!mmget_still_valid(mm))
  884. goto out;
  885. result = hugepage_vma_revalidate(mm, address, &vma);
  886. if (result)
  887. goto out;
  888. /* check if the pmd is still valid */
  889. if (mm_find_pmd(mm, address) != pmd)
  890. goto out;
  891. anon_vma_lock_write(vma->anon_vma);
  892. pte = pte_offset_map(pmd, address);
  893. pte_ptl = pte_lockptr(mm, pmd);
  894. mmun_start = address;
  895. mmun_end = address + HPAGE_PMD_SIZE;
  896. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  897. pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
  898. /*
  899. * After this gup_fast can't run anymore. This also removes
  900. * any huge TLB entry from the CPU so we won't allow
  901. * huge and small TLB entries for the same virtual address
  902. * to avoid the risk of CPU bugs in that area.
  903. */
  904. _pmd = pmdp_collapse_flush(vma, address, pmd);
  905. spin_unlock(pmd_ptl);
  906. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  907. spin_lock(pte_ptl);
  908. isolated = __collapse_huge_page_isolate(vma, address, pte);
  909. spin_unlock(pte_ptl);
  910. if (unlikely(!isolated)) {
  911. pte_unmap(pte);
  912. spin_lock(pmd_ptl);
  913. BUG_ON(!pmd_none(*pmd));
  914. /*
  915. * We can only use set_pmd_at when establishing
  916. * hugepmds and never for establishing regular pmds that
  917. * points to regular pagetables. Use pmd_populate for that
  918. */
  919. pmd_populate(mm, pmd, pmd_pgtable(_pmd));
  920. spin_unlock(pmd_ptl);
  921. anon_vma_unlock_write(vma->anon_vma);
  922. result = SCAN_FAIL;
  923. goto out;
  924. }
  925. /*
  926. * All pages are isolated and locked so anon_vma rmap
  927. * can't run anymore.
  928. */
  929. anon_vma_unlock_write(vma->anon_vma);
  930. __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
  931. pte_unmap(pte);
  932. __SetPageUptodate(new_page);
  933. pgtable = pmd_pgtable(_pmd);
  934. _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
  935. _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
  936. /*
  937. * spin_lock() below is not the equivalent of smp_wmb(), so
  938. * this is needed to avoid the copy_huge_page writes to become
  939. * visible after the set_pmd_at() write.
  940. */
  941. smp_wmb();
  942. spin_lock(pmd_ptl);
  943. BUG_ON(!pmd_none(*pmd));
  944. page_add_new_anon_rmap(new_page, vma, address, true);
  945. mem_cgroup_commit_charge(new_page, memcg, false, true);
  946. lru_cache_add_active_or_unevictable(new_page, vma);
  947. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  948. set_pmd_at(mm, address, pmd, _pmd);
  949. update_mmu_cache_pmd(vma, address, pmd);
  950. spin_unlock(pmd_ptl);
  951. *hpage = NULL;
  952. khugepaged_pages_collapsed++;
  953. result = SCAN_SUCCEED;
  954. out_up_write:
  955. up_write(&mm->mmap_sem);
  956. out_nolock:
  957. trace_mm_collapse_huge_page(mm, isolated, result);
  958. return;
  959. out:
  960. mem_cgroup_cancel_charge(new_page, memcg, true);
  961. goto out_up_write;
  962. }
  963. static int khugepaged_scan_pmd(struct mm_struct *mm,
  964. struct vm_area_struct *vma,
  965. unsigned long address,
  966. struct page **hpage)
  967. {
  968. pmd_t *pmd;
  969. pte_t *pte, *_pte;
  970. int ret = 0, none_or_zero = 0, result = 0, referenced = 0;
  971. struct page *page = NULL;
  972. unsigned long _address;
  973. spinlock_t *ptl;
  974. int node = NUMA_NO_NODE, unmapped = 0;
  975. bool writable = false;
  976. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  977. pmd = mm_find_pmd(mm, address);
  978. if (!pmd) {
  979. result = SCAN_PMD_NULL;
  980. goto out;
  981. }
  982. memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
  983. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  984. for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
  985. _pte++, _address += PAGE_SIZE) {
  986. pte_t pteval = *_pte;
  987. if (is_swap_pte(pteval)) {
  988. if (++unmapped <= khugepaged_max_ptes_swap) {
  989. continue;
  990. } else {
  991. result = SCAN_EXCEED_SWAP_PTE;
  992. goto out_unmap;
  993. }
  994. }
  995. if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  996. if (!userfaultfd_armed(vma) &&
  997. ++none_or_zero <= khugepaged_max_ptes_none) {
  998. continue;
  999. } else {
  1000. result = SCAN_EXCEED_NONE_PTE;
  1001. goto out_unmap;
  1002. }
  1003. }
  1004. if (!pte_present(pteval)) {
  1005. result = SCAN_PTE_NON_PRESENT;
  1006. goto out_unmap;
  1007. }
  1008. if (pte_write(pteval))
  1009. writable = true;
  1010. page = vm_normal_page(vma, _address, pteval);
  1011. if (unlikely(!page)) {
  1012. result = SCAN_PAGE_NULL;
  1013. goto out_unmap;
  1014. }
  1015. /* TODO: teach khugepaged to collapse THP mapped with pte */
  1016. if (PageCompound(page)) {
  1017. result = SCAN_PAGE_COMPOUND;
  1018. goto out_unmap;
  1019. }
  1020. /*
  1021. * Record which node the original page is from and save this
  1022. * information to khugepaged_node_load[].
  1023. * Khupaged will allocate hugepage from the node has the max
  1024. * hit record.
  1025. */
  1026. node = page_to_nid(page);
  1027. if (khugepaged_scan_abort(node)) {
  1028. result = SCAN_SCAN_ABORT;
  1029. goto out_unmap;
  1030. }
  1031. khugepaged_node_load[node]++;
  1032. if (!PageLRU(page)) {
  1033. result = SCAN_PAGE_LRU;
  1034. goto out_unmap;
  1035. }
  1036. if (PageLocked(page)) {
  1037. result = SCAN_PAGE_LOCK;
  1038. goto out_unmap;
  1039. }
  1040. if (!PageAnon(page)) {
  1041. result = SCAN_PAGE_ANON;
  1042. goto out_unmap;
  1043. }
  1044. /*
  1045. * cannot use mapcount: can't collapse if there's a gup pin.
  1046. * The page must only be referenced by the scanned process
  1047. * and page swap cache.
  1048. */
  1049. if (page_count(page) != 1 + !!PageSwapCache(page)) {
  1050. result = SCAN_PAGE_COUNT;
  1051. goto out_unmap;
  1052. }
  1053. if (pte_young(pteval) ||
  1054. page_is_young(page) || PageReferenced(page) ||
  1055. mmu_notifier_test_young(vma->vm_mm, address))
  1056. referenced++;
  1057. }
  1058. if (writable) {
  1059. if (referenced) {
  1060. result = SCAN_SUCCEED;
  1061. ret = 1;
  1062. } else {
  1063. result = SCAN_LACK_REFERENCED_PAGE;
  1064. }
  1065. } else {
  1066. result = SCAN_PAGE_RO;
  1067. }
  1068. out_unmap:
  1069. pte_unmap_unlock(pte, ptl);
  1070. if (ret) {
  1071. node = khugepaged_find_target_node();
  1072. /* collapse_huge_page will return with the mmap_sem released */
  1073. collapse_huge_page(mm, address, hpage, node, referenced);
  1074. }
  1075. out:
  1076. trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
  1077. none_or_zero, result, unmapped);
  1078. return ret;
  1079. }
  1080. static void collect_mm_slot(struct mm_slot *mm_slot)
  1081. {
  1082. struct mm_struct *mm = mm_slot->mm;
  1083. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1084. if (khugepaged_test_exit(mm)) {
  1085. /* free mm_slot */
  1086. hash_del(&mm_slot->hash);
  1087. list_del(&mm_slot->mm_node);
  1088. /*
  1089. * Not strictly needed because the mm exited already.
  1090. *
  1091. * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1092. */
  1093. /* khugepaged_mm_lock actually not necessary for the below */
  1094. free_mm_slot(mm_slot);
  1095. mmdrop(mm);
  1096. }
  1097. }
  1098. #if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
  1099. static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
  1100. {
  1101. struct vm_area_struct *vma;
  1102. unsigned long addr;
  1103. pmd_t *pmd, _pmd;
  1104. i_mmap_lock_write(mapping);
  1105. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  1106. /* probably overkill */
  1107. if (vma->anon_vma)
  1108. continue;
  1109. addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  1110. if (addr & ~HPAGE_PMD_MASK)
  1111. continue;
  1112. if (vma->vm_end < addr + HPAGE_PMD_SIZE)
  1113. continue;
  1114. pmd = mm_find_pmd(vma->vm_mm, addr);
  1115. if (!pmd)
  1116. continue;
  1117. /*
  1118. * We need exclusive mmap_sem to retract page table.
  1119. * If trylock fails we would end up with pte-mapped THP after
  1120. * re-fault. Not ideal, but it's more important to not disturb
  1121. * the system too much.
  1122. */
  1123. if (down_write_trylock(&vma->vm_mm->mmap_sem)) {
  1124. spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd);
  1125. /* assume page table is clear */
  1126. _pmd = pmdp_collapse_flush(vma, addr, pmd);
  1127. spin_unlock(ptl);
  1128. up_write(&vma->vm_mm->mmap_sem);
  1129. atomic_long_dec(&vma->vm_mm->nr_ptes);
  1130. pte_free(vma->vm_mm, pmd_pgtable(_pmd));
  1131. }
  1132. }
  1133. i_mmap_unlock_write(mapping);
  1134. }
  1135. /**
  1136. * collapse_shmem - collapse small tmpfs/shmem pages into huge one.
  1137. *
  1138. * Basic scheme is simple, details are more complex:
  1139. * - allocate and lock a new huge page;
  1140. * - scan over radix tree replacing old pages the new one
  1141. * + swap in pages if necessary;
  1142. * + fill in gaps;
  1143. * + keep old pages around in case if rollback is required;
  1144. * - if replacing succeed:
  1145. * + copy data over;
  1146. * + free old pages;
  1147. * + unlock huge page;
  1148. * - if replacing failed;
  1149. * + put all pages back and unfreeze them;
  1150. * + restore gaps in the radix-tree;
  1151. * + unlock and free huge page;
  1152. */
  1153. static void collapse_shmem(struct mm_struct *mm,
  1154. struct address_space *mapping, pgoff_t start,
  1155. struct page **hpage, int node)
  1156. {
  1157. gfp_t gfp;
  1158. struct page *page, *new_page, *tmp;
  1159. struct mem_cgroup *memcg;
  1160. pgoff_t index, end = start + HPAGE_PMD_NR;
  1161. LIST_HEAD(pagelist);
  1162. struct radix_tree_iter iter;
  1163. void **slot;
  1164. int nr_none = 0, result = SCAN_SUCCEED;
  1165. VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
  1166. /* Only allocate from the target node */
  1167. gfp = alloc_hugepage_khugepaged_gfpmask() |
  1168. __GFP_OTHER_NODE | __GFP_THISNODE;
  1169. new_page = khugepaged_alloc_page(hpage, gfp, node);
  1170. if (!new_page) {
  1171. result = SCAN_ALLOC_HUGE_PAGE_FAIL;
  1172. goto out;
  1173. }
  1174. /* Do not oom kill for khugepaged charges */
  1175. if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp | __GFP_NORETRY,
  1176. &memcg, true))) {
  1177. result = SCAN_CGROUP_CHARGE_FAIL;
  1178. goto out;
  1179. }
  1180. __SetPageLocked(new_page);
  1181. __SetPageSwapBacked(new_page);
  1182. new_page->index = start;
  1183. new_page->mapping = mapping;
  1184. /*
  1185. * At this point the new_page is locked and not up-to-date.
  1186. * It's safe to insert it into the page cache, because nobody would
  1187. * be able to map it or use it in another way until we unlock it.
  1188. */
  1189. index = start;
  1190. spin_lock_irq(&mapping->tree_lock);
  1191. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1192. int n = min(iter.index, end) - index;
  1193. /*
  1194. * Stop if extent has been hole-punched, and is now completely
  1195. * empty (the more obvious i_size_read() check would take an
  1196. * irq-unsafe seqlock on 32-bit).
  1197. */
  1198. if (n >= HPAGE_PMD_NR) {
  1199. result = SCAN_TRUNCATED;
  1200. goto tree_locked;
  1201. }
  1202. /*
  1203. * Handle holes in the radix tree: charge it from shmem and
  1204. * insert relevant subpage of new_page into the radix-tree.
  1205. */
  1206. if (n && !shmem_charge(mapping->host, n)) {
  1207. result = SCAN_FAIL;
  1208. goto tree_locked;
  1209. }
  1210. for (; index < min(iter.index, end); index++) {
  1211. radix_tree_insert(&mapping->page_tree, index,
  1212. new_page + (index % HPAGE_PMD_NR));
  1213. }
  1214. nr_none += n;
  1215. /* We are done. */
  1216. if (index >= end)
  1217. break;
  1218. page = radix_tree_deref_slot_protected(slot,
  1219. &mapping->tree_lock);
  1220. if (radix_tree_exceptional_entry(page) || !PageUptodate(page)) {
  1221. spin_unlock_irq(&mapping->tree_lock);
  1222. /* swap in or instantiate fallocated page */
  1223. if (shmem_getpage(mapping->host, index, &page,
  1224. SGP_NOHUGE)) {
  1225. result = SCAN_FAIL;
  1226. goto tree_unlocked;
  1227. }
  1228. } else if (trylock_page(page)) {
  1229. get_page(page);
  1230. spin_unlock_irq(&mapping->tree_lock);
  1231. } else {
  1232. result = SCAN_PAGE_LOCK;
  1233. goto tree_locked;
  1234. }
  1235. /*
  1236. * The page must be locked, so we can drop the tree_lock
  1237. * without racing with truncate.
  1238. */
  1239. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1240. VM_BUG_ON_PAGE(!PageUptodate(page), page);
  1241. /*
  1242. * If file was truncated then extended, or hole-punched, before
  1243. * we locked the first page, then a THP might be there already.
  1244. */
  1245. if (PageTransCompound(page)) {
  1246. result = SCAN_PAGE_COMPOUND;
  1247. goto out_unlock;
  1248. }
  1249. if (page_mapping(page) != mapping) {
  1250. result = SCAN_TRUNCATED;
  1251. goto out_unlock;
  1252. }
  1253. if (isolate_lru_page(page)) {
  1254. result = SCAN_DEL_PAGE_LRU;
  1255. goto out_unlock;
  1256. }
  1257. if (page_mapped(page))
  1258. unmap_mapping_range(mapping, index << PAGE_SHIFT,
  1259. PAGE_SIZE, 0);
  1260. spin_lock_irq(&mapping->tree_lock);
  1261. slot = radix_tree_lookup_slot(&mapping->page_tree, index);
  1262. VM_BUG_ON_PAGE(page != radix_tree_deref_slot_protected(slot,
  1263. &mapping->tree_lock), page);
  1264. VM_BUG_ON_PAGE(page_mapped(page), page);
  1265. /*
  1266. * The page is expected to have page_count() == 3:
  1267. * - we hold a pin on it;
  1268. * - one reference from radix tree;
  1269. * - one from isolate_lru_page;
  1270. */
  1271. if (!page_ref_freeze(page, 3)) {
  1272. result = SCAN_PAGE_COUNT;
  1273. spin_unlock_irq(&mapping->tree_lock);
  1274. putback_lru_page(page);
  1275. goto out_unlock;
  1276. }
  1277. /*
  1278. * Add the page to the list to be able to undo the collapse if
  1279. * something go wrong.
  1280. */
  1281. list_add_tail(&page->lru, &pagelist);
  1282. /* Finally, replace with the new page. */
  1283. radix_tree_replace_slot(slot,
  1284. new_page + (index % HPAGE_PMD_NR));
  1285. slot = radix_tree_iter_next(&iter);
  1286. index++;
  1287. continue;
  1288. out_unlock:
  1289. unlock_page(page);
  1290. put_page(page);
  1291. goto tree_unlocked;
  1292. }
  1293. /*
  1294. * Handle hole in radix tree at the end of the range.
  1295. * This code only triggers if there's nothing in radix tree
  1296. * beyond 'end'.
  1297. */
  1298. if (index < end) {
  1299. int n = end - index;
  1300. /* Stop if extent has been truncated, and is now empty */
  1301. if (n >= HPAGE_PMD_NR) {
  1302. result = SCAN_TRUNCATED;
  1303. goto tree_locked;
  1304. }
  1305. if (!shmem_charge(mapping->host, n)) {
  1306. result = SCAN_FAIL;
  1307. goto tree_locked;
  1308. }
  1309. for (; index < end; index++) {
  1310. radix_tree_insert(&mapping->page_tree, index,
  1311. new_page + (index % HPAGE_PMD_NR));
  1312. }
  1313. nr_none += n;
  1314. }
  1315. __inc_node_page_state(new_page, NR_SHMEM_THPS);
  1316. if (nr_none) {
  1317. struct zone *zone = page_zone(new_page);
  1318. __mod_node_page_state(zone->zone_pgdat, NR_FILE_PAGES, nr_none);
  1319. __mod_node_page_state(zone->zone_pgdat, NR_SHMEM, nr_none);
  1320. }
  1321. tree_locked:
  1322. spin_unlock_irq(&mapping->tree_lock);
  1323. tree_unlocked:
  1324. if (result == SCAN_SUCCEED) {
  1325. /*
  1326. * Replacing old pages with new one has succeed, now we need to
  1327. * copy the content and free old pages.
  1328. */
  1329. index = start;
  1330. list_for_each_entry_safe(page, tmp, &pagelist, lru) {
  1331. while (index < page->index) {
  1332. clear_highpage(new_page + (index % HPAGE_PMD_NR));
  1333. index++;
  1334. }
  1335. copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
  1336. page);
  1337. list_del(&page->lru);
  1338. page->mapping = NULL;
  1339. page_ref_unfreeze(page, 1);
  1340. ClearPageActive(page);
  1341. ClearPageUnevictable(page);
  1342. unlock_page(page);
  1343. put_page(page);
  1344. index++;
  1345. }
  1346. while (index < end) {
  1347. clear_highpage(new_page + (index % HPAGE_PMD_NR));
  1348. index++;
  1349. }
  1350. SetPageUptodate(new_page);
  1351. page_ref_add(new_page, HPAGE_PMD_NR - 1);
  1352. set_page_dirty(new_page);
  1353. mem_cgroup_commit_charge(new_page, memcg, false, true);
  1354. lru_cache_add_anon(new_page);
  1355. /*
  1356. * Remove pte page tables, so we can re-fault the page as huge.
  1357. */
  1358. retract_page_tables(mapping, start);
  1359. *hpage = NULL;
  1360. } else {
  1361. /* Something went wrong: rollback changes to the radix-tree */
  1362. spin_lock_irq(&mapping->tree_lock);
  1363. mapping->nrpages -= nr_none;
  1364. shmem_uncharge(mapping->host, nr_none);
  1365. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
  1366. start) {
  1367. if (iter.index >= end)
  1368. break;
  1369. page = list_first_entry_or_null(&pagelist,
  1370. struct page, lru);
  1371. if (!page || iter.index < page->index) {
  1372. if (!nr_none)
  1373. break;
  1374. nr_none--;
  1375. /* Put holes back where they were */
  1376. radix_tree_delete(&mapping->page_tree,
  1377. iter.index);
  1378. slot = radix_tree_iter_next(&iter);
  1379. continue;
  1380. }
  1381. VM_BUG_ON_PAGE(page->index != iter.index, page);
  1382. /* Unfreeze the page. */
  1383. list_del(&page->lru);
  1384. page_ref_unfreeze(page, 2);
  1385. radix_tree_replace_slot(slot, page);
  1386. spin_unlock_irq(&mapping->tree_lock);
  1387. unlock_page(page);
  1388. putback_lru_page(page);
  1389. spin_lock_irq(&mapping->tree_lock);
  1390. slot = radix_tree_iter_next(&iter);
  1391. }
  1392. VM_BUG_ON(nr_none);
  1393. spin_unlock_irq(&mapping->tree_lock);
  1394. mem_cgroup_cancel_charge(new_page, memcg, true);
  1395. new_page->mapping = NULL;
  1396. }
  1397. unlock_page(new_page);
  1398. out:
  1399. VM_BUG_ON(!list_empty(&pagelist));
  1400. /* TODO: tracepoints */
  1401. }
  1402. static void khugepaged_scan_shmem(struct mm_struct *mm,
  1403. struct address_space *mapping,
  1404. pgoff_t start, struct page **hpage)
  1405. {
  1406. struct page *page = NULL;
  1407. struct radix_tree_iter iter;
  1408. void **slot;
  1409. int present, swap;
  1410. int node = NUMA_NO_NODE;
  1411. int result = SCAN_SUCCEED;
  1412. present = 0;
  1413. swap = 0;
  1414. memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
  1415. rcu_read_lock();
  1416. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1417. if (iter.index >= start + HPAGE_PMD_NR)
  1418. break;
  1419. page = radix_tree_deref_slot(slot);
  1420. if (radix_tree_deref_retry(page)) {
  1421. slot = radix_tree_iter_retry(&iter);
  1422. continue;
  1423. }
  1424. if (radix_tree_exception(page)) {
  1425. if (++swap > khugepaged_max_ptes_swap) {
  1426. result = SCAN_EXCEED_SWAP_PTE;
  1427. break;
  1428. }
  1429. continue;
  1430. }
  1431. if (PageTransCompound(page)) {
  1432. result = SCAN_PAGE_COMPOUND;
  1433. break;
  1434. }
  1435. node = page_to_nid(page);
  1436. if (khugepaged_scan_abort(node)) {
  1437. result = SCAN_SCAN_ABORT;
  1438. break;
  1439. }
  1440. khugepaged_node_load[node]++;
  1441. if (!PageLRU(page)) {
  1442. result = SCAN_PAGE_LRU;
  1443. break;
  1444. }
  1445. if (page_count(page) != 1 + page_mapcount(page)) {
  1446. result = SCAN_PAGE_COUNT;
  1447. break;
  1448. }
  1449. /*
  1450. * We probably should check if the page is referenced here, but
  1451. * nobody would transfer pte_young() to PageReferenced() for us.
  1452. * And rmap walk here is just too costly...
  1453. */
  1454. present++;
  1455. if (need_resched()) {
  1456. cond_resched_rcu();
  1457. slot = radix_tree_iter_next(&iter);
  1458. }
  1459. }
  1460. rcu_read_unlock();
  1461. if (result == SCAN_SUCCEED) {
  1462. if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
  1463. result = SCAN_EXCEED_NONE_PTE;
  1464. } else {
  1465. node = khugepaged_find_target_node();
  1466. collapse_shmem(mm, mapping, start, hpage, node);
  1467. }
  1468. }
  1469. /* TODO: tracepoints */
  1470. }
  1471. #else
  1472. static void khugepaged_scan_shmem(struct mm_struct *mm,
  1473. struct address_space *mapping,
  1474. pgoff_t start, struct page **hpage)
  1475. {
  1476. BUILD_BUG();
  1477. }
  1478. #endif
  1479. static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
  1480. struct page **hpage)
  1481. __releases(&khugepaged_mm_lock)
  1482. __acquires(&khugepaged_mm_lock)
  1483. {
  1484. struct mm_slot *mm_slot;
  1485. struct mm_struct *mm;
  1486. struct vm_area_struct *vma;
  1487. int progress = 0;
  1488. VM_BUG_ON(!pages);
  1489. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1490. if (khugepaged_scan.mm_slot)
  1491. mm_slot = khugepaged_scan.mm_slot;
  1492. else {
  1493. mm_slot = list_entry(khugepaged_scan.mm_head.next,
  1494. struct mm_slot, mm_node);
  1495. khugepaged_scan.address = 0;
  1496. khugepaged_scan.mm_slot = mm_slot;
  1497. }
  1498. spin_unlock(&khugepaged_mm_lock);
  1499. mm = mm_slot->mm;
  1500. /*
  1501. * Don't wait for semaphore (to avoid long wait times). Just move to
  1502. * the next mm on the list.
  1503. */
  1504. vma = NULL;
  1505. if (unlikely(!down_read_trylock(&mm->mmap_sem)))
  1506. goto breakouterloop_mmap_sem;
  1507. if (likely(!khugepaged_test_exit(mm)))
  1508. vma = find_vma(mm, khugepaged_scan.address);
  1509. progress++;
  1510. for (; vma; vma = vma->vm_next) {
  1511. unsigned long hstart, hend;
  1512. cond_resched();
  1513. if (unlikely(khugepaged_test_exit(mm))) {
  1514. progress++;
  1515. break;
  1516. }
  1517. if (!hugepage_vma_check(vma)) {
  1518. skip:
  1519. progress++;
  1520. continue;
  1521. }
  1522. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1523. hend = vma->vm_end & HPAGE_PMD_MASK;
  1524. if (hstart >= hend)
  1525. goto skip;
  1526. if (khugepaged_scan.address > hend)
  1527. goto skip;
  1528. if (khugepaged_scan.address < hstart)
  1529. khugepaged_scan.address = hstart;
  1530. VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
  1531. while (khugepaged_scan.address < hend) {
  1532. int ret;
  1533. cond_resched();
  1534. if (unlikely(khugepaged_test_exit(mm)))
  1535. goto breakouterloop;
  1536. VM_BUG_ON(khugepaged_scan.address < hstart ||
  1537. khugepaged_scan.address + HPAGE_PMD_SIZE >
  1538. hend);
  1539. if (shmem_file(vma->vm_file)) {
  1540. struct file *file;
  1541. pgoff_t pgoff = linear_page_index(vma,
  1542. khugepaged_scan.address);
  1543. if (!shmem_huge_enabled(vma))
  1544. goto skip;
  1545. file = get_file(vma->vm_file);
  1546. up_read(&mm->mmap_sem);
  1547. ret = 1;
  1548. khugepaged_scan_shmem(mm, file->f_mapping,
  1549. pgoff, hpage);
  1550. fput(file);
  1551. } else {
  1552. ret = khugepaged_scan_pmd(mm, vma,
  1553. khugepaged_scan.address,
  1554. hpage);
  1555. }
  1556. /* move to next address */
  1557. khugepaged_scan.address += HPAGE_PMD_SIZE;
  1558. progress += HPAGE_PMD_NR;
  1559. if (ret)
  1560. /* we released mmap_sem so break loop */
  1561. goto breakouterloop_mmap_sem;
  1562. if (progress >= pages)
  1563. goto breakouterloop;
  1564. }
  1565. }
  1566. breakouterloop:
  1567. up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
  1568. breakouterloop_mmap_sem:
  1569. spin_lock(&khugepaged_mm_lock);
  1570. VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
  1571. /*
  1572. * Release the current mm_slot if this mm is about to die, or
  1573. * if we scanned all vmas of this mm.
  1574. */
  1575. if (khugepaged_test_exit(mm) || !vma) {
  1576. /*
  1577. * Make sure that if mm_users is reaching zero while
  1578. * khugepaged runs here, khugepaged_exit will find
  1579. * mm_slot not pointing to the exiting mm.
  1580. */
  1581. if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
  1582. khugepaged_scan.mm_slot = list_entry(
  1583. mm_slot->mm_node.next,
  1584. struct mm_slot, mm_node);
  1585. khugepaged_scan.address = 0;
  1586. } else {
  1587. khugepaged_scan.mm_slot = NULL;
  1588. khugepaged_full_scans++;
  1589. }
  1590. collect_mm_slot(mm_slot);
  1591. }
  1592. return progress;
  1593. }
  1594. static int khugepaged_has_work(void)
  1595. {
  1596. return !list_empty(&khugepaged_scan.mm_head) &&
  1597. khugepaged_enabled();
  1598. }
  1599. static int khugepaged_wait_event(void)
  1600. {
  1601. return !list_empty(&khugepaged_scan.mm_head) ||
  1602. kthread_should_stop();
  1603. }
  1604. static void khugepaged_do_scan(void)
  1605. {
  1606. struct page *hpage = NULL;
  1607. unsigned int progress = 0, pass_through_head = 0;
  1608. unsigned int pages = khugepaged_pages_to_scan;
  1609. bool wait = true;
  1610. barrier(); /* write khugepaged_pages_to_scan to local stack */
  1611. while (progress < pages) {
  1612. if (!khugepaged_prealloc_page(&hpage, &wait))
  1613. break;
  1614. cond_resched();
  1615. if (unlikely(kthread_should_stop() || try_to_freeze()))
  1616. break;
  1617. spin_lock(&khugepaged_mm_lock);
  1618. if (!khugepaged_scan.mm_slot)
  1619. pass_through_head++;
  1620. if (khugepaged_has_work() &&
  1621. pass_through_head < 2)
  1622. progress += khugepaged_scan_mm_slot(pages - progress,
  1623. &hpage);
  1624. else
  1625. progress = pages;
  1626. spin_unlock(&khugepaged_mm_lock);
  1627. }
  1628. if (!IS_ERR_OR_NULL(hpage))
  1629. put_page(hpage);
  1630. }
  1631. static bool khugepaged_should_wakeup(void)
  1632. {
  1633. return kthread_should_stop() ||
  1634. time_after_eq(jiffies, khugepaged_sleep_expire);
  1635. }
  1636. static void khugepaged_wait_work(void)
  1637. {
  1638. if (khugepaged_has_work()) {
  1639. const unsigned long scan_sleep_jiffies =
  1640. msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
  1641. if (!scan_sleep_jiffies)
  1642. return;
  1643. khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
  1644. wait_event_freezable_timeout(khugepaged_wait,
  1645. khugepaged_should_wakeup(),
  1646. scan_sleep_jiffies);
  1647. return;
  1648. }
  1649. if (khugepaged_enabled())
  1650. wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
  1651. }
  1652. static int khugepaged(void *none)
  1653. {
  1654. struct mm_slot *mm_slot;
  1655. set_freezable();
  1656. set_user_nice(current, MAX_NICE);
  1657. while (!kthread_should_stop()) {
  1658. khugepaged_do_scan();
  1659. khugepaged_wait_work();
  1660. }
  1661. spin_lock(&khugepaged_mm_lock);
  1662. mm_slot = khugepaged_scan.mm_slot;
  1663. khugepaged_scan.mm_slot = NULL;
  1664. if (mm_slot)
  1665. collect_mm_slot(mm_slot);
  1666. spin_unlock(&khugepaged_mm_lock);
  1667. return 0;
  1668. }
  1669. static void set_recommended_min_free_kbytes(void)
  1670. {
  1671. struct zone *zone;
  1672. int nr_zones = 0;
  1673. unsigned long recommended_min;
  1674. for_each_populated_zone(zone)
  1675. nr_zones++;
  1676. /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
  1677. recommended_min = pageblock_nr_pages * nr_zones * 2;
  1678. /*
  1679. * Make sure that on average at least two pageblocks are almost free
  1680. * of another type, one for a migratetype to fall back to and a
  1681. * second to avoid subsequent fallbacks of other types There are 3
  1682. * MIGRATE_TYPES we care about.
  1683. */
  1684. recommended_min += pageblock_nr_pages * nr_zones *
  1685. MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
  1686. /* don't ever allow to reserve more than 5% of the lowmem */
  1687. recommended_min = min(recommended_min,
  1688. (unsigned long) nr_free_buffer_pages() / 20);
  1689. recommended_min <<= (PAGE_SHIFT-10);
  1690. if (recommended_min > min_free_kbytes) {
  1691. if (user_min_free_kbytes >= 0)
  1692. pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
  1693. min_free_kbytes, recommended_min);
  1694. min_free_kbytes = recommended_min;
  1695. }
  1696. setup_per_zone_wmarks();
  1697. }
  1698. int start_stop_khugepaged(void)
  1699. {
  1700. static struct task_struct *khugepaged_thread __read_mostly;
  1701. static DEFINE_MUTEX(khugepaged_mutex);
  1702. int err = 0;
  1703. mutex_lock(&khugepaged_mutex);
  1704. if (khugepaged_enabled()) {
  1705. if (!khugepaged_thread)
  1706. khugepaged_thread = kthread_run(khugepaged, NULL,
  1707. "khugepaged");
  1708. if (IS_ERR(khugepaged_thread)) {
  1709. pr_err("khugepaged: kthread_run(khugepaged) failed\n");
  1710. err = PTR_ERR(khugepaged_thread);
  1711. khugepaged_thread = NULL;
  1712. goto fail;
  1713. }
  1714. if (!list_empty(&khugepaged_scan.mm_head))
  1715. wake_up_interruptible(&khugepaged_wait);
  1716. set_recommended_min_free_kbytes();
  1717. } else if (khugepaged_thread) {
  1718. kthread_stop(khugepaged_thread);
  1719. khugepaged_thread = NULL;
  1720. }
  1721. fail:
  1722. mutex_unlock(&khugepaged_mutex);
  1723. return err;
  1724. }