khugepaged.c 49 KB

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