khugepaged.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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 = hugepage_vma_revalidate(mm, address, &vma);
  883. if (result)
  884. goto out;
  885. /* check if the pmd is still valid */
  886. if (mm_find_pmd(mm, address) != pmd)
  887. goto out;
  888. anon_vma_lock_write(vma->anon_vma);
  889. pte = pte_offset_map(pmd, address);
  890. pte_ptl = pte_lockptr(mm, pmd);
  891. mmun_start = address;
  892. mmun_end = address + HPAGE_PMD_SIZE;
  893. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  894. pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
  895. /*
  896. * After this gup_fast can't run anymore. This also removes
  897. * any huge TLB entry from the CPU so we won't allow
  898. * huge and small TLB entries for the same virtual address
  899. * to avoid the risk of CPU bugs in that area.
  900. */
  901. _pmd = pmdp_collapse_flush(vma, address, pmd);
  902. spin_unlock(pmd_ptl);
  903. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  904. spin_lock(pte_ptl);
  905. isolated = __collapse_huge_page_isolate(vma, address, pte);
  906. spin_unlock(pte_ptl);
  907. if (unlikely(!isolated)) {
  908. pte_unmap(pte);
  909. spin_lock(pmd_ptl);
  910. BUG_ON(!pmd_none(*pmd));
  911. /*
  912. * We can only use set_pmd_at when establishing
  913. * hugepmds and never for establishing regular pmds that
  914. * points to regular pagetables. Use pmd_populate for that
  915. */
  916. pmd_populate(mm, pmd, pmd_pgtable(_pmd));
  917. spin_unlock(pmd_ptl);
  918. anon_vma_unlock_write(vma->anon_vma);
  919. result = SCAN_FAIL;
  920. goto out;
  921. }
  922. /*
  923. * All pages are isolated and locked so anon_vma rmap
  924. * can't run anymore.
  925. */
  926. anon_vma_unlock_write(vma->anon_vma);
  927. __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
  928. pte_unmap(pte);
  929. __SetPageUptodate(new_page);
  930. pgtable = pmd_pgtable(_pmd);
  931. _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
  932. _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
  933. /*
  934. * spin_lock() below is not the equivalent of smp_wmb(), so
  935. * this is needed to avoid the copy_huge_page writes to become
  936. * visible after the set_pmd_at() write.
  937. */
  938. smp_wmb();
  939. spin_lock(pmd_ptl);
  940. BUG_ON(!pmd_none(*pmd));
  941. page_add_new_anon_rmap(new_page, vma, address, true);
  942. mem_cgroup_commit_charge(new_page, memcg, false, true);
  943. lru_cache_add_active_or_unevictable(new_page, vma);
  944. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  945. set_pmd_at(mm, address, pmd, _pmd);
  946. update_mmu_cache_pmd(vma, address, pmd);
  947. spin_unlock(pmd_ptl);
  948. *hpage = NULL;
  949. khugepaged_pages_collapsed++;
  950. result = SCAN_SUCCEED;
  951. out_up_write:
  952. up_write(&mm->mmap_sem);
  953. out_nolock:
  954. trace_mm_collapse_huge_page(mm, isolated, result);
  955. return;
  956. out:
  957. mem_cgroup_cancel_charge(new_page, memcg, true);
  958. goto out_up_write;
  959. }
  960. static int khugepaged_scan_pmd(struct mm_struct *mm,
  961. struct vm_area_struct *vma,
  962. unsigned long address,
  963. struct page **hpage)
  964. {
  965. pmd_t *pmd;
  966. pte_t *pte, *_pte;
  967. int ret = 0, none_or_zero = 0, result = 0, referenced = 0;
  968. struct page *page = NULL;
  969. unsigned long _address;
  970. spinlock_t *ptl;
  971. int node = NUMA_NO_NODE, unmapped = 0;
  972. bool writable = false;
  973. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  974. pmd = mm_find_pmd(mm, address);
  975. if (!pmd) {
  976. result = SCAN_PMD_NULL;
  977. goto out;
  978. }
  979. memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
  980. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  981. for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
  982. _pte++, _address += PAGE_SIZE) {
  983. pte_t pteval = *_pte;
  984. if (is_swap_pte(pteval)) {
  985. if (++unmapped <= khugepaged_max_ptes_swap) {
  986. continue;
  987. } else {
  988. result = SCAN_EXCEED_SWAP_PTE;
  989. goto out_unmap;
  990. }
  991. }
  992. if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
  993. if (!userfaultfd_armed(vma) &&
  994. ++none_or_zero <= khugepaged_max_ptes_none) {
  995. continue;
  996. } else {
  997. result = SCAN_EXCEED_NONE_PTE;
  998. goto out_unmap;
  999. }
  1000. }
  1001. if (!pte_present(pteval)) {
  1002. result = SCAN_PTE_NON_PRESENT;
  1003. goto out_unmap;
  1004. }
  1005. if (pte_write(pteval))
  1006. writable = true;
  1007. page = vm_normal_page(vma, _address, pteval);
  1008. if (unlikely(!page)) {
  1009. result = SCAN_PAGE_NULL;
  1010. goto out_unmap;
  1011. }
  1012. /* TODO: teach khugepaged to collapse THP mapped with pte */
  1013. if (PageCompound(page)) {
  1014. result = SCAN_PAGE_COMPOUND;
  1015. goto out_unmap;
  1016. }
  1017. /*
  1018. * Record which node the original page is from and save this
  1019. * information to khugepaged_node_load[].
  1020. * Khupaged will allocate hugepage from the node has the max
  1021. * hit record.
  1022. */
  1023. node = page_to_nid(page);
  1024. if (khugepaged_scan_abort(node)) {
  1025. result = SCAN_SCAN_ABORT;
  1026. goto out_unmap;
  1027. }
  1028. khugepaged_node_load[node]++;
  1029. if (!PageLRU(page)) {
  1030. result = SCAN_PAGE_LRU;
  1031. goto out_unmap;
  1032. }
  1033. if (PageLocked(page)) {
  1034. result = SCAN_PAGE_LOCK;
  1035. goto out_unmap;
  1036. }
  1037. if (!PageAnon(page)) {
  1038. result = SCAN_PAGE_ANON;
  1039. goto out_unmap;
  1040. }
  1041. /*
  1042. * cannot use mapcount: can't collapse if there's a gup pin.
  1043. * The page must only be referenced by the scanned process
  1044. * and page swap cache.
  1045. */
  1046. if (page_count(page) != 1 + !!PageSwapCache(page)) {
  1047. result = SCAN_PAGE_COUNT;
  1048. goto out_unmap;
  1049. }
  1050. if (pte_young(pteval) ||
  1051. page_is_young(page) || PageReferenced(page) ||
  1052. mmu_notifier_test_young(vma->vm_mm, address))
  1053. referenced++;
  1054. }
  1055. if (writable) {
  1056. if (referenced) {
  1057. result = SCAN_SUCCEED;
  1058. ret = 1;
  1059. } else {
  1060. result = SCAN_LACK_REFERENCED_PAGE;
  1061. }
  1062. } else {
  1063. result = SCAN_PAGE_RO;
  1064. }
  1065. out_unmap:
  1066. pte_unmap_unlock(pte, ptl);
  1067. if (ret) {
  1068. node = khugepaged_find_target_node();
  1069. /* collapse_huge_page will return with the mmap_sem released */
  1070. collapse_huge_page(mm, address, hpage, node, referenced);
  1071. }
  1072. out:
  1073. trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
  1074. none_or_zero, result, unmapped);
  1075. return ret;
  1076. }
  1077. static void collect_mm_slot(struct mm_slot *mm_slot)
  1078. {
  1079. struct mm_struct *mm = mm_slot->mm;
  1080. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1081. if (khugepaged_test_exit(mm)) {
  1082. /* free mm_slot */
  1083. hash_del(&mm_slot->hash);
  1084. list_del(&mm_slot->mm_node);
  1085. /*
  1086. * Not strictly needed because the mm exited already.
  1087. *
  1088. * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1089. */
  1090. /* khugepaged_mm_lock actually not necessary for the below */
  1091. free_mm_slot(mm_slot);
  1092. mmdrop(mm);
  1093. }
  1094. }
  1095. #if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
  1096. static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
  1097. {
  1098. struct vm_area_struct *vma;
  1099. unsigned long addr;
  1100. pmd_t *pmd, _pmd;
  1101. i_mmap_lock_write(mapping);
  1102. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  1103. /* probably overkill */
  1104. if (vma->anon_vma)
  1105. continue;
  1106. addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  1107. if (addr & ~HPAGE_PMD_MASK)
  1108. continue;
  1109. if (vma->vm_end < addr + HPAGE_PMD_SIZE)
  1110. continue;
  1111. pmd = mm_find_pmd(vma->vm_mm, addr);
  1112. if (!pmd)
  1113. continue;
  1114. /*
  1115. * We need exclusive mmap_sem to retract page table.
  1116. * If trylock fails we would end up with pte-mapped THP after
  1117. * re-fault. Not ideal, but it's more important to not disturb
  1118. * the system too much.
  1119. */
  1120. if (down_write_trylock(&vma->vm_mm->mmap_sem)) {
  1121. spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd);
  1122. /* assume page table is clear */
  1123. _pmd = pmdp_collapse_flush(vma, addr, pmd);
  1124. spin_unlock(ptl);
  1125. up_write(&vma->vm_mm->mmap_sem);
  1126. atomic_long_dec(&vma->vm_mm->nr_ptes);
  1127. pte_free(vma->vm_mm, pmd_pgtable(_pmd));
  1128. }
  1129. }
  1130. i_mmap_unlock_write(mapping);
  1131. }
  1132. /**
  1133. * collapse_shmem - collapse small tmpfs/shmem pages into huge one.
  1134. *
  1135. * Basic scheme is simple, details are more complex:
  1136. * - allocate and freeze a new huge page;
  1137. * - scan over radix tree replacing old pages the new one
  1138. * + swap in pages if necessary;
  1139. * + fill in gaps;
  1140. * + keep old pages around in case if rollback is required;
  1141. * - if replacing succeed:
  1142. * + copy data over;
  1143. * + free old pages;
  1144. * + unfreeze huge page;
  1145. * - if replacing failed;
  1146. * + put all pages back and unfreeze them;
  1147. * + restore gaps in the radix-tree;
  1148. * + free huge page;
  1149. */
  1150. static void collapse_shmem(struct mm_struct *mm,
  1151. struct address_space *mapping, pgoff_t start,
  1152. struct page **hpage, int node)
  1153. {
  1154. gfp_t gfp;
  1155. struct page *page, *new_page, *tmp;
  1156. struct mem_cgroup *memcg;
  1157. pgoff_t index, end = start + HPAGE_PMD_NR;
  1158. LIST_HEAD(pagelist);
  1159. struct radix_tree_iter iter;
  1160. void **slot;
  1161. int nr_none = 0, result = SCAN_SUCCEED;
  1162. VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
  1163. /* Only allocate from the target node */
  1164. gfp = alloc_hugepage_khugepaged_gfpmask() |
  1165. __GFP_OTHER_NODE | __GFP_THISNODE;
  1166. new_page = khugepaged_alloc_page(hpage, gfp, node);
  1167. if (!new_page) {
  1168. result = SCAN_ALLOC_HUGE_PAGE_FAIL;
  1169. goto out;
  1170. }
  1171. /* Do not oom kill for khugepaged charges */
  1172. if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp | __GFP_NORETRY,
  1173. &memcg, true))) {
  1174. result = SCAN_CGROUP_CHARGE_FAIL;
  1175. goto out;
  1176. }
  1177. new_page->index = start;
  1178. new_page->mapping = mapping;
  1179. __SetPageSwapBacked(new_page);
  1180. __SetPageLocked(new_page);
  1181. BUG_ON(!page_ref_freeze(new_page, 1));
  1182. /*
  1183. * At this point the new_page is 'frozen' (page_count() is zero), locked
  1184. * and not up-to-date. It's safe to insert it into radix tree, because
  1185. * nobody would be able to map it or use it in other way until we
  1186. * unfreeze it.
  1187. */
  1188. index = start;
  1189. spin_lock_irq(&mapping->tree_lock);
  1190. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1191. int n = min(iter.index, end) - index;
  1192. /*
  1193. * Handle holes in the radix tree: charge it from shmem and
  1194. * insert relevant subpage of new_page into the radix-tree.
  1195. */
  1196. if (n && !shmem_charge(mapping->host, n)) {
  1197. result = SCAN_FAIL;
  1198. break;
  1199. }
  1200. nr_none += n;
  1201. for (; index < min(iter.index, end); index++) {
  1202. radix_tree_insert(&mapping->page_tree, index,
  1203. new_page + (index % HPAGE_PMD_NR));
  1204. }
  1205. /* We are done. */
  1206. if (index >= end)
  1207. break;
  1208. page = radix_tree_deref_slot_protected(slot,
  1209. &mapping->tree_lock);
  1210. if (radix_tree_exceptional_entry(page) || !PageUptodate(page)) {
  1211. spin_unlock_irq(&mapping->tree_lock);
  1212. /* swap in or instantiate fallocated page */
  1213. if (shmem_getpage(mapping->host, index, &page,
  1214. SGP_NOHUGE)) {
  1215. result = SCAN_FAIL;
  1216. goto tree_unlocked;
  1217. }
  1218. spin_lock_irq(&mapping->tree_lock);
  1219. } else if (trylock_page(page)) {
  1220. get_page(page);
  1221. } else {
  1222. result = SCAN_PAGE_LOCK;
  1223. break;
  1224. }
  1225. /*
  1226. * The page must be locked, so we can drop the tree_lock
  1227. * without racing with truncate.
  1228. */
  1229. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1230. VM_BUG_ON_PAGE(!PageUptodate(page), page);
  1231. VM_BUG_ON_PAGE(PageTransCompound(page), page);
  1232. if (page_mapping(page) != mapping) {
  1233. result = SCAN_TRUNCATED;
  1234. goto out_unlock;
  1235. }
  1236. spin_unlock_irq(&mapping->tree_lock);
  1237. if (isolate_lru_page(page)) {
  1238. result = SCAN_DEL_PAGE_LRU;
  1239. goto out_isolate_failed;
  1240. }
  1241. if (page_mapped(page))
  1242. unmap_mapping_range(mapping, index << PAGE_SHIFT,
  1243. PAGE_SIZE, 0);
  1244. spin_lock_irq(&mapping->tree_lock);
  1245. slot = radix_tree_lookup_slot(&mapping->page_tree, index);
  1246. VM_BUG_ON_PAGE(page != radix_tree_deref_slot_protected(slot,
  1247. &mapping->tree_lock), page);
  1248. VM_BUG_ON_PAGE(page_mapped(page), page);
  1249. /*
  1250. * The page is expected to have page_count() == 3:
  1251. * - we hold a pin on it;
  1252. * - one reference from radix tree;
  1253. * - one from isolate_lru_page;
  1254. */
  1255. if (!page_ref_freeze(page, 3)) {
  1256. result = SCAN_PAGE_COUNT;
  1257. goto out_lru;
  1258. }
  1259. /*
  1260. * Add the page to the list to be able to undo the collapse if
  1261. * something go wrong.
  1262. */
  1263. list_add_tail(&page->lru, &pagelist);
  1264. /* Finally, replace with the new page. */
  1265. radix_tree_replace_slot(slot,
  1266. new_page + (index % HPAGE_PMD_NR));
  1267. slot = radix_tree_iter_next(&iter);
  1268. index++;
  1269. continue;
  1270. out_lru:
  1271. spin_unlock_irq(&mapping->tree_lock);
  1272. putback_lru_page(page);
  1273. out_isolate_failed:
  1274. unlock_page(page);
  1275. put_page(page);
  1276. goto tree_unlocked;
  1277. out_unlock:
  1278. unlock_page(page);
  1279. put_page(page);
  1280. break;
  1281. }
  1282. /*
  1283. * Handle hole in radix tree at the end of the range.
  1284. * This code only triggers if there's nothing in radix tree
  1285. * beyond 'end'.
  1286. */
  1287. if (result == SCAN_SUCCEED && index < end) {
  1288. int n = end - index;
  1289. if (!shmem_charge(mapping->host, n)) {
  1290. result = SCAN_FAIL;
  1291. goto tree_locked;
  1292. }
  1293. for (; index < end; index++) {
  1294. radix_tree_insert(&mapping->page_tree, index,
  1295. new_page + (index % HPAGE_PMD_NR));
  1296. }
  1297. nr_none += n;
  1298. }
  1299. tree_locked:
  1300. spin_unlock_irq(&mapping->tree_lock);
  1301. tree_unlocked:
  1302. if (result == SCAN_SUCCEED) {
  1303. unsigned long flags;
  1304. struct zone *zone = page_zone(new_page);
  1305. /*
  1306. * Replacing old pages with new one has succeed, now we need to
  1307. * copy the content and free old pages.
  1308. */
  1309. list_for_each_entry_safe(page, tmp, &pagelist, lru) {
  1310. copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
  1311. page);
  1312. list_del(&page->lru);
  1313. unlock_page(page);
  1314. page_ref_unfreeze(page, 1);
  1315. page->mapping = NULL;
  1316. ClearPageActive(page);
  1317. ClearPageUnevictable(page);
  1318. put_page(page);
  1319. }
  1320. local_irq_save(flags);
  1321. __inc_node_page_state(new_page, NR_SHMEM_THPS);
  1322. if (nr_none) {
  1323. __mod_node_page_state(zone->zone_pgdat, NR_FILE_PAGES, nr_none);
  1324. __mod_node_page_state(zone->zone_pgdat, NR_SHMEM, nr_none);
  1325. }
  1326. local_irq_restore(flags);
  1327. /*
  1328. * Remove pte page tables, so we can re-faulti
  1329. * the page as huge.
  1330. */
  1331. retract_page_tables(mapping, start);
  1332. /* Everything is ready, let's unfreeze the new_page */
  1333. set_page_dirty(new_page);
  1334. SetPageUptodate(new_page);
  1335. page_ref_unfreeze(new_page, HPAGE_PMD_NR);
  1336. mem_cgroup_commit_charge(new_page, memcg, false, true);
  1337. lru_cache_add_anon(new_page);
  1338. unlock_page(new_page);
  1339. *hpage = NULL;
  1340. } else {
  1341. /* Something went wrong: rollback changes to the radix-tree */
  1342. shmem_uncharge(mapping->host, nr_none);
  1343. spin_lock_irq(&mapping->tree_lock);
  1344. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter,
  1345. start) {
  1346. if (iter.index >= end)
  1347. break;
  1348. page = list_first_entry_or_null(&pagelist,
  1349. struct page, lru);
  1350. if (!page || iter.index < page->index) {
  1351. if (!nr_none)
  1352. break;
  1353. nr_none--;
  1354. /* Put holes back where they were */
  1355. radix_tree_delete(&mapping->page_tree,
  1356. iter.index);
  1357. slot = radix_tree_iter_next(&iter);
  1358. continue;
  1359. }
  1360. VM_BUG_ON_PAGE(page->index != iter.index, page);
  1361. /* Unfreeze the page. */
  1362. list_del(&page->lru);
  1363. page_ref_unfreeze(page, 2);
  1364. radix_tree_replace_slot(slot, page);
  1365. spin_unlock_irq(&mapping->tree_lock);
  1366. putback_lru_page(page);
  1367. unlock_page(page);
  1368. spin_lock_irq(&mapping->tree_lock);
  1369. slot = radix_tree_iter_next(&iter);
  1370. }
  1371. VM_BUG_ON(nr_none);
  1372. spin_unlock_irq(&mapping->tree_lock);
  1373. /* Unfreeze new_page, caller would take care about freeing it */
  1374. page_ref_unfreeze(new_page, 1);
  1375. mem_cgroup_cancel_charge(new_page, memcg, true);
  1376. unlock_page(new_page);
  1377. new_page->mapping = NULL;
  1378. }
  1379. out:
  1380. VM_BUG_ON(!list_empty(&pagelist));
  1381. /* TODO: tracepoints */
  1382. }
  1383. static void khugepaged_scan_shmem(struct mm_struct *mm,
  1384. struct address_space *mapping,
  1385. pgoff_t start, struct page **hpage)
  1386. {
  1387. struct page *page = NULL;
  1388. struct radix_tree_iter iter;
  1389. void **slot;
  1390. int present, swap;
  1391. int node = NUMA_NO_NODE;
  1392. int result = SCAN_SUCCEED;
  1393. present = 0;
  1394. swap = 0;
  1395. memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
  1396. rcu_read_lock();
  1397. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  1398. if (iter.index >= start + HPAGE_PMD_NR)
  1399. break;
  1400. page = radix_tree_deref_slot(slot);
  1401. if (radix_tree_deref_retry(page)) {
  1402. slot = radix_tree_iter_retry(&iter);
  1403. continue;
  1404. }
  1405. if (radix_tree_exception(page)) {
  1406. if (++swap > khugepaged_max_ptes_swap) {
  1407. result = SCAN_EXCEED_SWAP_PTE;
  1408. break;
  1409. }
  1410. continue;
  1411. }
  1412. if (PageTransCompound(page)) {
  1413. result = SCAN_PAGE_COMPOUND;
  1414. break;
  1415. }
  1416. node = page_to_nid(page);
  1417. if (khugepaged_scan_abort(node)) {
  1418. result = SCAN_SCAN_ABORT;
  1419. break;
  1420. }
  1421. khugepaged_node_load[node]++;
  1422. if (!PageLRU(page)) {
  1423. result = SCAN_PAGE_LRU;
  1424. break;
  1425. }
  1426. if (page_count(page) != 1 + page_mapcount(page)) {
  1427. result = SCAN_PAGE_COUNT;
  1428. break;
  1429. }
  1430. /*
  1431. * We probably should check if the page is referenced here, but
  1432. * nobody would transfer pte_young() to PageReferenced() for us.
  1433. * And rmap walk here is just too costly...
  1434. */
  1435. present++;
  1436. if (need_resched()) {
  1437. cond_resched_rcu();
  1438. slot = radix_tree_iter_next(&iter);
  1439. }
  1440. }
  1441. rcu_read_unlock();
  1442. if (result == SCAN_SUCCEED) {
  1443. if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
  1444. result = SCAN_EXCEED_NONE_PTE;
  1445. } else {
  1446. node = khugepaged_find_target_node();
  1447. collapse_shmem(mm, mapping, start, hpage, node);
  1448. }
  1449. }
  1450. /* TODO: tracepoints */
  1451. }
  1452. #else
  1453. static void khugepaged_scan_shmem(struct mm_struct *mm,
  1454. struct address_space *mapping,
  1455. pgoff_t start, struct page **hpage)
  1456. {
  1457. BUILD_BUG();
  1458. }
  1459. #endif
  1460. static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
  1461. struct page **hpage)
  1462. __releases(&khugepaged_mm_lock)
  1463. __acquires(&khugepaged_mm_lock)
  1464. {
  1465. struct mm_slot *mm_slot;
  1466. struct mm_struct *mm;
  1467. struct vm_area_struct *vma;
  1468. int progress = 0;
  1469. VM_BUG_ON(!pages);
  1470. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1471. if (khugepaged_scan.mm_slot)
  1472. mm_slot = khugepaged_scan.mm_slot;
  1473. else {
  1474. mm_slot = list_entry(khugepaged_scan.mm_head.next,
  1475. struct mm_slot, mm_node);
  1476. khugepaged_scan.address = 0;
  1477. khugepaged_scan.mm_slot = mm_slot;
  1478. }
  1479. spin_unlock(&khugepaged_mm_lock);
  1480. mm = mm_slot->mm;
  1481. /*
  1482. * Don't wait for semaphore (to avoid long wait times). Just move to
  1483. * the next mm on the list.
  1484. */
  1485. vma = NULL;
  1486. if (unlikely(!down_read_trylock(&mm->mmap_sem)))
  1487. goto breakouterloop_mmap_sem;
  1488. if (likely(!khugepaged_test_exit(mm)))
  1489. vma = find_vma(mm, khugepaged_scan.address);
  1490. progress++;
  1491. for (; vma; vma = vma->vm_next) {
  1492. unsigned long hstart, hend;
  1493. cond_resched();
  1494. if (unlikely(khugepaged_test_exit(mm))) {
  1495. progress++;
  1496. break;
  1497. }
  1498. if (!hugepage_vma_check(vma)) {
  1499. skip:
  1500. progress++;
  1501. continue;
  1502. }
  1503. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1504. hend = vma->vm_end & HPAGE_PMD_MASK;
  1505. if (hstart >= hend)
  1506. goto skip;
  1507. if (khugepaged_scan.address > hend)
  1508. goto skip;
  1509. if (khugepaged_scan.address < hstart)
  1510. khugepaged_scan.address = hstart;
  1511. VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
  1512. while (khugepaged_scan.address < hend) {
  1513. int ret;
  1514. cond_resched();
  1515. if (unlikely(khugepaged_test_exit(mm)))
  1516. goto breakouterloop;
  1517. VM_BUG_ON(khugepaged_scan.address < hstart ||
  1518. khugepaged_scan.address + HPAGE_PMD_SIZE >
  1519. hend);
  1520. if (shmem_file(vma->vm_file)) {
  1521. struct file *file;
  1522. pgoff_t pgoff = linear_page_index(vma,
  1523. khugepaged_scan.address);
  1524. if (!shmem_huge_enabled(vma))
  1525. goto skip;
  1526. file = get_file(vma->vm_file);
  1527. up_read(&mm->mmap_sem);
  1528. ret = 1;
  1529. khugepaged_scan_shmem(mm, file->f_mapping,
  1530. pgoff, hpage);
  1531. fput(file);
  1532. } else {
  1533. ret = khugepaged_scan_pmd(mm, vma,
  1534. khugepaged_scan.address,
  1535. hpage);
  1536. }
  1537. /* move to next address */
  1538. khugepaged_scan.address += HPAGE_PMD_SIZE;
  1539. progress += HPAGE_PMD_NR;
  1540. if (ret)
  1541. /* we released mmap_sem so break loop */
  1542. goto breakouterloop_mmap_sem;
  1543. if (progress >= pages)
  1544. goto breakouterloop;
  1545. }
  1546. }
  1547. breakouterloop:
  1548. up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
  1549. breakouterloop_mmap_sem:
  1550. spin_lock(&khugepaged_mm_lock);
  1551. VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
  1552. /*
  1553. * Release the current mm_slot if this mm is about to die, or
  1554. * if we scanned all vmas of this mm.
  1555. */
  1556. if (khugepaged_test_exit(mm) || !vma) {
  1557. /*
  1558. * Make sure that if mm_users is reaching zero while
  1559. * khugepaged runs here, khugepaged_exit will find
  1560. * mm_slot not pointing to the exiting mm.
  1561. */
  1562. if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
  1563. khugepaged_scan.mm_slot = list_entry(
  1564. mm_slot->mm_node.next,
  1565. struct mm_slot, mm_node);
  1566. khugepaged_scan.address = 0;
  1567. } else {
  1568. khugepaged_scan.mm_slot = NULL;
  1569. khugepaged_full_scans++;
  1570. }
  1571. collect_mm_slot(mm_slot);
  1572. }
  1573. return progress;
  1574. }
  1575. static int khugepaged_has_work(void)
  1576. {
  1577. return !list_empty(&khugepaged_scan.mm_head) &&
  1578. khugepaged_enabled();
  1579. }
  1580. static int khugepaged_wait_event(void)
  1581. {
  1582. return !list_empty(&khugepaged_scan.mm_head) ||
  1583. kthread_should_stop();
  1584. }
  1585. static void khugepaged_do_scan(void)
  1586. {
  1587. struct page *hpage = NULL;
  1588. unsigned int progress = 0, pass_through_head = 0;
  1589. unsigned int pages = khugepaged_pages_to_scan;
  1590. bool wait = true;
  1591. barrier(); /* write khugepaged_pages_to_scan to local stack */
  1592. while (progress < pages) {
  1593. if (!khugepaged_prealloc_page(&hpage, &wait))
  1594. break;
  1595. cond_resched();
  1596. if (unlikely(kthread_should_stop() || try_to_freeze()))
  1597. break;
  1598. spin_lock(&khugepaged_mm_lock);
  1599. if (!khugepaged_scan.mm_slot)
  1600. pass_through_head++;
  1601. if (khugepaged_has_work() &&
  1602. pass_through_head < 2)
  1603. progress += khugepaged_scan_mm_slot(pages - progress,
  1604. &hpage);
  1605. else
  1606. progress = pages;
  1607. spin_unlock(&khugepaged_mm_lock);
  1608. }
  1609. if (!IS_ERR_OR_NULL(hpage))
  1610. put_page(hpage);
  1611. }
  1612. static bool khugepaged_should_wakeup(void)
  1613. {
  1614. return kthread_should_stop() ||
  1615. time_after_eq(jiffies, khugepaged_sleep_expire);
  1616. }
  1617. static void khugepaged_wait_work(void)
  1618. {
  1619. if (khugepaged_has_work()) {
  1620. const unsigned long scan_sleep_jiffies =
  1621. msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
  1622. if (!scan_sleep_jiffies)
  1623. return;
  1624. khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
  1625. wait_event_freezable_timeout(khugepaged_wait,
  1626. khugepaged_should_wakeup(),
  1627. scan_sleep_jiffies);
  1628. return;
  1629. }
  1630. if (khugepaged_enabled())
  1631. wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
  1632. }
  1633. static int khugepaged(void *none)
  1634. {
  1635. struct mm_slot *mm_slot;
  1636. set_freezable();
  1637. set_user_nice(current, MAX_NICE);
  1638. while (!kthread_should_stop()) {
  1639. khugepaged_do_scan();
  1640. khugepaged_wait_work();
  1641. }
  1642. spin_lock(&khugepaged_mm_lock);
  1643. mm_slot = khugepaged_scan.mm_slot;
  1644. khugepaged_scan.mm_slot = NULL;
  1645. if (mm_slot)
  1646. collect_mm_slot(mm_slot);
  1647. spin_unlock(&khugepaged_mm_lock);
  1648. return 0;
  1649. }
  1650. static void set_recommended_min_free_kbytes(void)
  1651. {
  1652. struct zone *zone;
  1653. int nr_zones = 0;
  1654. unsigned long recommended_min;
  1655. for_each_populated_zone(zone)
  1656. nr_zones++;
  1657. /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
  1658. recommended_min = pageblock_nr_pages * nr_zones * 2;
  1659. /*
  1660. * Make sure that on average at least two pageblocks are almost free
  1661. * of another type, one for a migratetype to fall back to and a
  1662. * second to avoid subsequent fallbacks of other types There are 3
  1663. * MIGRATE_TYPES we care about.
  1664. */
  1665. recommended_min += pageblock_nr_pages * nr_zones *
  1666. MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
  1667. /* don't ever allow to reserve more than 5% of the lowmem */
  1668. recommended_min = min(recommended_min,
  1669. (unsigned long) nr_free_buffer_pages() / 20);
  1670. recommended_min <<= (PAGE_SHIFT-10);
  1671. if (recommended_min > min_free_kbytes) {
  1672. if (user_min_free_kbytes >= 0)
  1673. pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
  1674. min_free_kbytes, recommended_min);
  1675. min_free_kbytes = recommended_min;
  1676. }
  1677. setup_per_zone_wmarks();
  1678. }
  1679. int start_stop_khugepaged(void)
  1680. {
  1681. static struct task_struct *khugepaged_thread __read_mostly;
  1682. static DEFINE_MUTEX(khugepaged_mutex);
  1683. int err = 0;
  1684. mutex_lock(&khugepaged_mutex);
  1685. if (khugepaged_enabled()) {
  1686. if (!khugepaged_thread)
  1687. khugepaged_thread = kthread_run(khugepaged, NULL,
  1688. "khugepaged");
  1689. if (IS_ERR(khugepaged_thread)) {
  1690. pr_err("khugepaged: kthread_run(khugepaged) failed\n");
  1691. err = PTR_ERR(khugepaged_thread);
  1692. khugepaged_thread = NULL;
  1693. goto fail;
  1694. }
  1695. if (!list_empty(&khugepaged_scan.mm_head))
  1696. wake_up_interruptible(&khugepaged_wait);
  1697. set_recommended_min_free_kbytes();
  1698. } else if (khugepaged_thread) {
  1699. kthread_stop(khugepaged_thread);
  1700. khugepaged_thread = NULL;
  1701. }
  1702. fail:
  1703. mutex_unlock(&khugepaged_mutex);
  1704. return err;
  1705. }