huge_memory.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. /*
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/sched.h>
  9. #include <linux/highmem.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/mmu_notifier.h>
  12. #include <linux/rmap.h>
  13. #include <linux/swap.h>
  14. #include <linux/mm_inline.h>
  15. #include <linux/kthread.h>
  16. #include <linux/khugepaged.h>
  17. #include <linux/freezer.h>
  18. #include <linux/mman.h>
  19. #include <asm/tlb.h>
  20. #include <asm/pgalloc.h>
  21. #include "internal.h"
  22. /*
  23. * By default transparent hugepage support is enabled for all mappings
  24. * and khugepaged scans all mappings. Defrag is only invoked by
  25. * khugepaged hugepage allocations and by page faults inside
  26. * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
  27. * allocations.
  28. */
  29. unsigned long transparent_hugepage_flags __read_mostly =
  30. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
  31. (1<<TRANSPARENT_HUGEPAGE_FLAG)|
  32. #endif
  33. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
  34. (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
  35. #endif
  36. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
  37. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  38. /* default scan 8*512 pte (or vmas) every 30 second */
  39. static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
  40. static unsigned int khugepaged_pages_collapsed;
  41. static unsigned int khugepaged_full_scans;
  42. static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
  43. /* during fragmentation poll the hugepage allocator once every minute */
  44. static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
  45. static struct task_struct *khugepaged_thread __read_mostly;
  46. static DEFINE_MUTEX(khugepaged_mutex);
  47. static DEFINE_SPINLOCK(khugepaged_mm_lock);
  48. static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
  49. /*
  50. * default collapse hugepages if there is at least one pte mapped like
  51. * it would have happened if the vma was large enough during page
  52. * fault.
  53. */
  54. static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
  55. static int khugepaged(void *none);
  56. static int mm_slots_hash_init(void);
  57. static int khugepaged_slab_init(void);
  58. static void khugepaged_slab_free(void);
  59. #define MM_SLOTS_HASH_HEADS 1024
  60. static struct hlist_head *mm_slots_hash __read_mostly;
  61. static struct kmem_cache *mm_slot_cache __read_mostly;
  62. /**
  63. * struct mm_slot - hash lookup from mm to mm_slot
  64. * @hash: hash collision list
  65. * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
  66. * @mm: the mm that this information is valid for
  67. */
  68. struct mm_slot {
  69. struct hlist_node hash;
  70. struct list_head mm_node;
  71. struct mm_struct *mm;
  72. };
  73. /**
  74. * struct khugepaged_scan - cursor for scanning
  75. * @mm_head: the head of the mm list to scan
  76. * @mm_slot: the current mm_slot we are scanning
  77. * @address: the next address inside that to be scanned
  78. *
  79. * There is only the one khugepaged_scan instance of this cursor structure.
  80. */
  81. struct khugepaged_scan {
  82. struct list_head mm_head;
  83. struct mm_slot *mm_slot;
  84. unsigned long address;
  85. };
  86. static struct khugepaged_scan khugepaged_scan = {
  87. .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
  88. };
  89. static int set_recommended_min_free_kbytes(void)
  90. {
  91. struct zone *zone;
  92. int nr_zones = 0;
  93. unsigned long recommended_min;
  94. extern int min_free_kbytes;
  95. if (!test_bit(TRANSPARENT_HUGEPAGE_FLAG,
  96. &transparent_hugepage_flags) &&
  97. !test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  98. &transparent_hugepage_flags))
  99. return 0;
  100. for_each_populated_zone(zone)
  101. nr_zones++;
  102. /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
  103. recommended_min = pageblock_nr_pages * nr_zones * 2;
  104. /*
  105. * Make sure that on average at least two pageblocks are almost free
  106. * of another type, one for a migratetype to fall back to and a
  107. * second to avoid subsequent fallbacks of other types There are 3
  108. * MIGRATE_TYPES we care about.
  109. */
  110. recommended_min += pageblock_nr_pages * nr_zones *
  111. MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
  112. /* don't ever allow to reserve more than 5% of the lowmem */
  113. recommended_min = min(recommended_min,
  114. (unsigned long) nr_free_buffer_pages() / 20);
  115. recommended_min <<= (PAGE_SHIFT-10);
  116. if (recommended_min > min_free_kbytes)
  117. min_free_kbytes = recommended_min;
  118. setup_per_zone_wmarks();
  119. return 0;
  120. }
  121. late_initcall(set_recommended_min_free_kbytes);
  122. static int start_khugepaged(void)
  123. {
  124. int err = 0;
  125. if (khugepaged_enabled()) {
  126. int wakeup;
  127. if (unlikely(!mm_slot_cache || !mm_slots_hash)) {
  128. err = -ENOMEM;
  129. goto out;
  130. }
  131. mutex_lock(&khugepaged_mutex);
  132. if (!khugepaged_thread)
  133. khugepaged_thread = kthread_run(khugepaged, NULL,
  134. "khugepaged");
  135. if (unlikely(IS_ERR(khugepaged_thread))) {
  136. printk(KERN_ERR
  137. "khugepaged: kthread_run(khugepaged) failed\n");
  138. err = PTR_ERR(khugepaged_thread);
  139. khugepaged_thread = NULL;
  140. }
  141. wakeup = !list_empty(&khugepaged_scan.mm_head);
  142. mutex_unlock(&khugepaged_mutex);
  143. if (wakeup)
  144. wake_up_interruptible(&khugepaged_wait);
  145. set_recommended_min_free_kbytes();
  146. } else
  147. /* wakeup to exit */
  148. wake_up_interruptible(&khugepaged_wait);
  149. out:
  150. return err;
  151. }
  152. #ifdef CONFIG_SYSFS
  153. static ssize_t double_flag_show(struct kobject *kobj,
  154. struct kobj_attribute *attr, char *buf,
  155. enum transparent_hugepage_flag enabled,
  156. enum transparent_hugepage_flag req_madv)
  157. {
  158. if (test_bit(enabled, &transparent_hugepage_flags)) {
  159. VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
  160. return sprintf(buf, "[always] madvise never\n");
  161. } else if (test_bit(req_madv, &transparent_hugepage_flags))
  162. return sprintf(buf, "always [madvise] never\n");
  163. else
  164. return sprintf(buf, "always madvise [never]\n");
  165. }
  166. static ssize_t double_flag_store(struct kobject *kobj,
  167. struct kobj_attribute *attr,
  168. const char *buf, size_t count,
  169. enum transparent_hugepage_flag enabled,
  170. enum transparent_hugepage_flag req_madv)
  171. {
  172. if (!memcmp("always", buf,
  173. min(sizeof("always")-1, count))) {
  174. set_bit(enabled, &transparent_hugepage_flags);
  175. clear_bit(req_madv, &transparent_hugepage_flags);
  176. } else if (!memcmp("madvise", buf,
  177. min(sizeof("madvise")-1, count))) {
  178. clear_bit(enabled, &transparent_hugepage_flags);
  179. set_bit(req_madv, &transparent_hugepage_flags);
  180. } else if (!memcmp("never", buf,
  181. min(sizeof("never")-1, count))) {
  182. clear_bit(enabled, &transparent_hugepage_flags);
  183. clear_bit(req_madv, &transparent_hugepage_flags);
  184. } else
  185. return -EINVAL;
  186. return count;
  187. }
  188. static ssize_t enabled_show(struct kobject *kobj,
  189. struct kobj_attribute *attr, char *buf)
  190. {
  191. return double_flag_show(kobj, attr, buf,
  192. TRANSPARENT_HUGEPAGE_FLAG,
  193. TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
  194. }
  195. static ssize_t enabled_store(struct kobject *kobj,
  196. struct kobj_attribute *attr,
  197. const char *buf, size_t count)
  198. {
  199. ssize_t ret;
  200. ret = double_flag_store(kobj, attr, buf, count,
  201. TRANSPARENT_HUGEPAGE_FLAG,
  202. TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
  203. if (ret > 0) {
  204. int err = start_khugepaged();
  205. if (err)
  206. ret = err;
  207. }
  208. if (ret > 0 &&
  209. (test_bit(TRANSPARENT_HUGEPAGE_FLAG,
  210. &transparent_hugepage_flags) ||
  211. test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  212. &transparent_hugepage_flags)))
  213. set_recommended_min_free_kbytes();
  214. return ret;
  215. }
  216. static struct kobj_attribute enabled_attr =
  217. __ATTR(enabled, 0644, enabled_show, enabled_store);
  218. static ssize_t single_flag_show(struct kobject *kobj,
  219. struct kobj_attribute *attr, char *buf,
  220. enum transparent_hugepage_flag flag)
  221. {
  222. return sprintf(buf, "%d\n",
  223. !!test_bit(flag, &transparent_hugepage_flags));
  224. }
  225. static ssize_t single_flag_store(struct kobject *kobj,
  226. struct kobj_attribute *attr,
  227. const char *buf, size_t count,
  228. enum transparent_hugepage_flag flag)
  229. {
  230. unsigned long value;
  231. int ret;
  232. ret = kstrtoul(buf, 10, &value);
  233. if (ret < 0)
  234. return ret;
  235. if (value > 1)
  236. return -EINVAL;
  237. if (value)
  238. set_bit(flag, &transparent_hugepage_flags);
  239. else
  240. clear_bit(flag, &transparent_hugepage_flags);
  241. return count;
  242. }
  243. /*
  244. * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
  245. * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
  246. * memory just to allocate one more hugepage.
  247. */
  248. static ssize_t defrag_show(struct kobject *kobj,
  249. struct kobj_attribute *attr, char *buf)
  250. {
  251. return double_flag_show(kobj, attr, buf,
  252. TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
  253. TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
  254. }
  255. static ssize_t defrag_store(struct kobject *kobj,
  256. struct kobj_attribute *attr,
  257. const char *buf, size_t count)
  258. {
  259. return double_flag_store(kobj, attr, buf, count,
  260. TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
  261. TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
  262. }
  263. static struct kobj_attribute defrag_attr =
  264. __ATTR(defrag, 0644, defrag_show, defrag_store);
  265. #ifdef CONFIG_DEBUG_VM
  266. static ssize_t debug_cow_show(struct kobject *kobj,
  267. struct kobj_attribute *attr, char *buf)
  268. {
  269. return single_flag_show(kobj, attr, buf,
  270. TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
  271. }
  272. static ssize_t debug_cow_store(struct kobject *kobj,
  273. struct kobj_attribute *attr,
  274. const char *buf, size_t count)
  275. {
  276. return single_flag_store(kobj, attr, buf, count,
  277. TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
  278. }
  279. static struct kobj_attribute debug_cow_attr =
  280. __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
  281. #endif /* CONFIG_DEBUG_VM */
  282. static struct attribute *hugepage_attr[] = {
  283. &enabled_attr.attr,
  284. &defrag_attr.attr,
  285. #ifdef CONFIG_DEBUG_VM
  286. &debug_cow_attr.attr,
  287. #endif
  288. NULL,
  289. };
  290. static struct attribute_group hugepage_attr_group = {
  291. .attrs = hugepage_attr,
  292. };
  293. static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
  294. struct kobj_attribute *attr,
  295. char *buf)
  296. {
  297. return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
  298. }
  299. static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
  300. struct kobj_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. unsigned long msecs;
  304. int err;
  305. err = strict_strtoul(buf, 10, &msecs);
  306. if (err || msecs > UINT_MAX)
  307. return -EINVAL;
  308. khugepaged_scan_sleep_millisecs = msecs;
  309. wake_up_interruptible(&khugepaged_wait);
  310. return count;
  311. }
  312. static struct kobj_attribute scan_sleep_millisecs_attr =
  313. __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
  314. scan_sleep_millisecs_store);
  315. static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
  316. struct kobj_attribute *attr,
  317. char *buf)
  318. {
  319. return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
  320. }
  321. static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
  322. struct kobj_attribute *attr,
  323. const char *buf, size_t count)
  324. {
  325. unsigned long msecs;
  326. int err;
  327. err = strict_strtoul(buf, 10, &msecs);
  328. if (err || msecs > UINT_MAX)
  329. return -EINVAL;
  330. khugepaged_alloc_sleep_millisecs = msecs;
  331. wake_up_interruptible(&khugepaged_wait);
  332. return count;
  333. }
  334. static struct kobj_attribute alloc_sleep_millisecs_attr =
  335. __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
  336. alloc_sleep_millisecs_store);
  337. static ssize_t pages_to_scan_show(struct kobject *kobj,
  338. struct kobj_attribute *attr,
  339. char *buf)
  340. {
  341. return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
  342. }
  343. static ssize_t pages_to_scan_store(struct kobject *kobj,
  344. struct kobj_attribute *attr,
  345. const char *buf, size_t count)
  346. {
  347. int err;
  348. unsigned long pages;
  349. err = strict_strtoul(buf, 10, &pages);
  350. if (err || !pages || pages > UINT_MAX)
  351. return -EINVAL;
  352. khugepaged_pages_to_scan = pages;
  353. return count;
  354. }
  355. static struct kobj_attribute pages_to_scan_attr =
  356. __ATTR(pages_to_scan, 0644, pages_to_scan_show,
  357. pages_to_scan_store);
  358. static ssize_t pages_collapsed_show(struct kobject *kobj,
  359. struct kobj_attribute *attr,
  360. char *buf)
  361. {
  362. return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
  363. }
  364. static struct kobj_attribute pages_collapsed_attr =
  365. __ATTR_RO(pages_collapsed);
  366. static ssize_t full_scans_show(struct kobject *kobj,
  367. struct kobj_attribute *attr,
  368. char *buf)
  369. {
  370. return sprintf(buf, "%u\n", khugepaged_full_scans);
  371. }
  372. static struct kobj_attribute full_scans_attr =
  373. __ATTR_RO(full_scans);
  374. static ssize_t khugepaged_defrag_show(struct kobject *kobj,
  375. struct kobj_attribute *attr, char *buf)
  376. {
  377. return single_flag_show(kobj, attr, buf,
  378. TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  379. }
  380. static ssize_t khugepaged_defrag_store(struct kobject *kobj,
  381. struct kobj_attribute *attr,
  382. const char *buf, size_t count)
  383. {
  384. return single_flag_store(kobj, attr, buf, count,
  385. TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
  386. }
  387. static struct kobj_attribute khugepaged_defrag_attr =
  388. __ATTR(defrag, 0644, khugepaged_defrag_show,
  389. khugepaged_defrag_store);
  390. /*
  391. * max_ptes_none controls if khugepaged should collapse hugepages over
  392. * any unmapped ptes in turn potentially increasing the memory
  393. * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
  394. * reduce the available free memory in the system as it
  395. * runs. Increasing max_ptes_none will instead potentially reduce the
  396. * free memory in the system during the khugepaged scan.
  397. */
  398. static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
  399. struct kobj_attribute *attr,
  400. char *buf)
  401. {
  402. return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
  403. }
  404. static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
  405. struct kobj_attribute *attr,
  406. const char *buf, size_t count)
  407. {
  408. int err;
  409. unsigned long max_ptes_none;
  410. err = strict_strtoul(buf, 10, &max_ptes_none);
  411. if (err || max_ptes_none > HPAGE_PMD_NR-1)
  412. return -EINVAL;
  413. khugepaged_max_ptes_none = max_ptes_none;
  414. return count;
  415. }
  416. static struct kobj_attribute khugepaged_max_ptes_none_attr =
  417. __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
  418. khugepaged_max_ptes_none_store);
  419. static struct attribute *khugepaged_attr[] = {
  420. &khugepaged_defrag_attr.attr,
  421. &khugepaged_max_ptes_none_attr.attr,
  422. &pages_to_scan_attr.attr,
  423. &pages_collapsed_attr.attr,
  424. &full_scans_attr.attr,
  425. &scan_sleep_millisecs_attr.attr,
  426. &alloc_sleep_millisecs_attr.attr,
  427. NULL,
  428. };
  429. static struct attribute_group khugepaged_attr_group = {
  430. .attrs = khugepaged_attr,
  431. .name = "khugepaged",
  432. };
  433. static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
  434. {
  435. int err;
  436. *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
  437. if (unlikely(!*hugepage_kobj)) {
  438. printk(KERN_ERR "hugepage: failed kobject create\n");
  439. return -ENOMEM;
  440. }
  441. err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
  442. if (err) {
  443. printk(KERN_ERR "hugepage: failed register hugeage group\n");
  444. goto delete_obj;
  445. }
  446. err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
  447. if (err) {
  448. printk(KERN_ERR "hugepage: failed register hugeage group\n");
  449. goto remove_hp_group;
  450. }
  451. return 0;
  452. remove_hp_group:
  453. sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
  454. delete_obj:
  455. kobject_put(*hugepage_kobj);
  456. return err;
  457. }
  458. static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  459. {
  460. sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
  461. sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
  462. kobject_put(hugepage_kobj);
  463. }
  464. #else
  465. static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
  466. {
  467. return 0;
  468. }
  469. static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  470. {
  471. }
  472. #endif /* CONFIG_SYSFS */
  473. static int __init hugepage_init(void)
  474. {
  475. int err;
  476. struct kobject *hugepage_kobj;
  477. if (!has_transparent_hugepage()) {
  478. transparent_hugepage_flags = 0;
  479. return -EINVAL;
  480. }
  481. err = hugepage_init_sysfs(&hugepage_kobj);
  482. if (err)
  483. return err;
  484. err = khugepaged_slab_init();
  485. if (err)
  486. goto out;
  487. err = mm_slots_hash_init();
  488. if (err) {
  489. khugepaged_slab_free();
  490. goto out;
  491. }
  492. /*
  493. * By default disable transparent hugepages on smaller systems,
  494. * where the extra memory used could hurt more than TLB overhead
  495. * is likely to save. The admin can still enable it through /sys.
  496. */
  497. if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
  498. transparent_hugepage_flags = 0;
  499. start_khugepaged();
  500. set_recommended_min_free_kbytes();
  501. return 0;
  502. out:
  503. hugepage_exit_sysfs(hugepage_kobj);
  504. return err;
  505. }
  506. module_init(hugepage_init)
  507. static int __init setup_transparent_hugepage(char *str)
  508. {
  509. int ret = 0;
  510. if (!str)
  511. goto out;
  512. if (!strcmp(str, "always")) {
  513. set_bit(TRANSPARENT_HUGEPAGE_FLAG,
  514. &transparent_hugepage_flags);
  515. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  516. &transparent_hugepage_flags);
  517. ret = 1;
  518. } else if (!strcmp(str, "madvise")) {
  519. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  520. &transparent_hugepage_flags);
  521. set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  522. &transparent_hugepage_flags);
  523. ret = 1;
  524. } else if (!strcmp(str, "never")) {
  525. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  526. &transparent_hugepage_flags);
  527. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  528. &transparent_hugepage_flags);
  529. ret = 1;
  530. }
  531. out:
  532. if (!ret)
  533. printk(KERN_WARNING
  534. "transparent_hugepage= cannot parse, ignored\n");
  535. return ret;
  536. }
  537. __setup("transparent_hugepage=", setup_transparent_hugepage);
  538. static void prepare_pmd_huge_pte(pgtable_t pgtable,
  539. struct mm_struct *mm)
  540. {
  541. assert_spin_locked(&mm->page_table_lock);
  542. /* FIFO */
  543. if (!mm->pmd_huge_pte)
  544. INIT_LIST_HEAD(&pgtable->lru);
  545. else
  546. list_add(&pgtable->lru, &mm->pmd_huge_pte->lru);
  547. mm->pmd_huge_pte = pgtable;
  548. }
  549. static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
  550. {
  551. if (likely(vma->vm_flags & VM_WRITE))
  552. pmd = pmd_mkwrite(pmd);
  553. return pmd;
  554. }
  555. static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
  556. struct vm_area_struct *vma,
  557. unsigned long haddr, pmd_t *pmd,
  558. struct page *page)
  559. {
  560. int ret = 0;
  561. pgtable_t pgtable;
  562. VM_BUG_ON(!PageCompound(page));
  563. pgtable = pte_alloc_one(mm, haddr);
  564. if (unlikely(!pgtable)) {
  565. mem_cgroup_uncharge_page(page);
  566. put_page(page);
  567. return VM_FAULT_OOM;
  568. }
  569. clear_huge_page(page, haddr, HPAGE_PMD_NR);
  570. __SetPageUptodate(page);
  571. spin_lock(&mm->page_table_lock);
  572. if (unlikely(!pmd_none(*pmd))) {
  573. spin_unlock(&mm->page_table_lock);
  574. mem_cgroup_uncharge_page(page);
  575. put_page(page);
  576. pte_free(mm, pgtable);
  577. } else {
  578. pmd_t entry;
  579. entry = mk_pmd(page, vma->vm_page_prot);
  580. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  581. entry = pmd_mkhuge(entry);
  582. /*
  583. * The spinlocking to take the lru_lock inside
  584. * page_add_new_anon_rmap() acts as a full memory
  585. * barrier to be sure clear_huge_page writes become
  586. * visible after the set_pmd_at() write.
  587. */
  588. page_add_new_anon_rmap(page, vma, haddr);
  589. set_pmd_at(mm, haddr, pmd, entry);
  590. prepare_pmd_huge_pte(pgtable, mm);
  591. add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
  592. mm->nr_ptes++;
  593. spin_unlock(&mm->page_table_lock);
  594. }
  595. return ret;
  596. }
  597. static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
  598. {
  599. return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
  600. }
  601. static inline struct page *alloc_hugepage_vma(int defrag,
  602. struct vm_area_struct *vma,
  603. unsigned long haddr, int nd,
  604. gfp_t extra_gfp)
  605. {
  606. return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
  607. HPAGE_PMD_ORDER, vma, haddr, nd);
  608. }
  609. #ifndef CONFIG_NUMA
  610. static inline struct page *alloc_hugepage(int defrag)
  611. {
  612. return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
  613. HPAGE_PMD_ORDER);
  614. }
  615. #endif
  616. int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  617. unsigned long address, pmd_t *pmd,
  618. unsigned int flags)
  619. {
  620. struct page *page;
  621. unsigned long haddr = address & HPAGE_PMD_MASK;
  622. pte_t *pte;
  623. if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
  624. if (unlikely(anon_vma_prepare(vma)))
  625. return VM_FAULT_OOM;
  626. if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
  627. return VM_FAULT_OOM;
  628. page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
  629. vma, haddr, numa_node_id(), 0);
  630. if (unlikely(!page)) {
  631. count_vm_event(THP_FAULT_FALLBACK);
  632. goto out;
  633. }
  634. count_vm_event(THP_FAULT_ALLOC);
  635. if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
  636. put_page(page);
  637. goto out;
  638. }
  639. return __do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page);
  640. }
  641. out:
  642. /*
  643. * Use __pte_alloc instead of pte_alloc_map, because we can't
  644. * run pte_offset_map on the pmd, if an huge pmd could
  645. * materialize from under us from a different thread.
  646. */
  647. if (unlikely(__pte_alloc(mm, vma, pmd, address)))
  648. return VM_FAULT_OOM;
  649. /* if an huge pmd materialized from under us just retry later */
  650. if (unlikely(pmd_trans_huge(*pmd)))
  651. return 0;
  652. /*
  653. * A regular pmd is established and it can't morph into a huge pmd
  654. * from under us anymore at this point because we hold the mmap_sem
  655. * read mode and khugepaged takes it in write mode. So now it's
  656. * safe to run pte_offset_map().
  657. */
  658. pte = pte_offset_map(pmd, address);
  659. return handle_pte_fault(mm, vma, address, pte, pmd, flags);
  660. }
  661. int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  662. pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
  663. struct vm_area_struct *vma)
  664. {
  665. struct page *src_page;
  666. pmd_t pmd;
  667. pgtable_t pgtable;
  668. int ret;
  669. ret = -ENOMEM;
  670. pgtable = pte_alloc_one(dst_mm, addr);
  671. if (unlikely(!pgtable))
  672. goto out;
  673. spin_lock(&dst_mm->page_table_lock);
  674. spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
  675. ret = -EAGAIN;
  676. pmd = *src_pmd;
  677. if (unlikely(!pmd_trans_huge(pmd))) {
  678. pte_free(dst_mm, pgtable);
  679. goto out_unlock;
  680. }
  681. if (unlikely(pmd_trans_splitting(pmd))) {
  682. /* split huge page running from under us */
  683. spin_unlock(&src_mm->page_table_lock);
  684. spin_unlock(&dst_mm->page_table_lock);
  685. pte_free(dst_mm, pgtable);
  686. wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
  687. goto out;
  688. }
  689. src_page = pmd_page(pmd);
  690. VM_BUG_ON(!PageHead(src_page));
  691. get_page(src_page);
  692. page_dup_rmap(src_page);
  693. add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  694. pmdp_set_wrprotect(src_mm, addr, src_pmd);
  695. pmd = pmd_mkold(pmd_wrprotect(pmd));
  696. set_pmd_at(dst_mm, addr, dst_pmd, pmd);
  697. prepare_pmd_huge_pte(pgtable, dst_mm);
  698. dst_mm->nr_ptes++;
  699. ret = 0;
  700. out_unlock:
  701. spin_unlock(&src_mm->page_table_lock);
  702. spin_unlock(&dst_mm->page_table_lock);
  703. out:
  704. return ret;
  705. }
  706. /* no "address" argument so destroys page coloring of some arch */
  707. pgtable_t get_pmd_huge_pte(struct mm_struct *mm)
  708. {
  709. pgtable_t pgtable;
  710. assert_spin_locked(&mm->page_table_lock);
  711. /* FIFO */
  712. pgtable = mm->pmd_huge_pte;
  713. if (list_empty(&pgtable->lru))
  714. mm->pmd_huge_pte = NULL;
  715. else {
  716. mm->pmd_huge_pte = list_entry(pgtable->lru.next,
  717. struct page, lru);
  718. list_del(&pgtable->lru);
  719. }
  720. return pgtable;
  721. }
  722. static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
  723. struct vm_area_struct *vma,
  724. unsigned long address,
  725. pmd_t *pmd, pmd_t orig_pmd,
  726. struct page *page,
  727. unsigned long haddr)
  728. {
  729. pgtable_t pgtable;
  730. pmd_t _pmd;
  731. int ret = 0, i;
  732. struct page **pages;
  733. pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
  734. GFP_KERNEL);
  735. if (unlikely(!pages)) {
  736. ret |= VM_FAULT_OOM;
  737. goto out;
  738. }
  739. for (i = 0; i < HPAGE_PMD_NR; i++) {
  740. pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
  741. __GFP_OTHER_NODE,
  742. vma, address, page_to_nid(page));
  743. if (unlikely(!pages[i] ||
  744. mem_cgroup_newpage_charge(pages[i], mm,
  745. GFP_KERNEL))) {
  746. if (pages[i])
  747. put_page(pages[i]);
  748. mem_cgroup_uncharge_start();
  749. while (--i >= 0) {
  750. mem_cgroup_uncharge_page(pages[i]);
  751. put_page(pages[i]);
  752. }
  753. mem_cgroup_uncharge_end();
  754. kfree(pages);
  755. ret |= VM_FAULT_OOM;
  756. goto out;
  757. }
  758. }
  759. for (i = 0; i < HPAGE_PMD_NR; i++) {
  760. copy_user_highpage(pages[i], page + i,
  761. haddr + PAGE_SIZE * i, vma);
  762. __SetPageUptodate(pages[i]);
  763. cond_resched();
  764. }
  765. spin_lock(&mm->page_table_lock);
  766. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  767. goto out_free_pages;
  768. VM_BUG_ON(!PageHead(page));
  769. pmdp_clear_flush_notify(vma, haddr, pmd);
  770. /* leave pmd empty until pte is filled */
  771. pgtable = get_pmd_huge_pte(mm);
  772. pmd_populate(mm, &_pmd, pgtable);
  773. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  774. pte_t *pte, entry;
  775. entry = mk_pte(pages[i], vma->vm_page_prot);
  776. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  777. page_add_new_anon_rmap(pages[i], vma, haddr);
  778. pte = pte_offset_map(&_pmd, haddr);
  779. VM_BUG_ON(!pte_none(*pte));
  780. set_pte_at(mm, haddr, pte, entry);
  781. pte_unmap(pte);
  782. }
  783. kfree(pages);
  784. smp_wmb(); /* make pte visible before pmd */
  785. pmd_populate(mm, pmd, pgtable);
  786. page_remove_rmap(page);
  787. spin_unlock(&mm->page_table_lock);
  788. ret |= VM_FAULT_WRITE;
  789. put_page(page);
  790. out:
  791. return ret;
  792. out_free_pages:
  793. spin_unlock(&mm->page_table_lock);
  794. mem_cgroup_uncharge_start();
  795. for (i = 0; i < HPAGE_PMD_NR; i++) {
  796. mem_cgroup_uncharge_page(pages[i]);
  797. put_page(pages[i]);
  798. }
  799. mem_cgroup_uncharge_end();
  800. kfree(pages);
  801. goto out;
  802. }
  803. int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  804. unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
  805. {
  806. int ret = 0;
  807. struct page *page, *new_page;
  808. unsigned long haddr;
  809. VM_BUG_ON(!vma->anon_vma);
  810. spin_lock(&mm->page_table_lock);
  811. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  812. goto out_unlock;
  813. page = pmd_page(orig_pmd);
  814. VM_BUG_ON(!PageCompound(page) || !PageHead(page));
  815. haddr = address & HPAGE_PMD_MASK;
  816. if (page_mapcount(page) == 1) {
  817. pmd_t entry;
  818. entry = pmd_mkyoung(orig_pmd);
  819. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  820. if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
  821. update_mmu_cache(vma, address, entry);
  822. ret |= VM_FAULT_WRITE;
  823. goto out_unlock;
  824. }
  825. get_page(page);
  826. spin_unlock(&mm->page_table_lock);
  827. if (transparent_hugepage_enabled(vma) &&
  828. !transparent_hugepage_debug_cow())
  829. new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
  830. vma, haddr, numa_node_id(), 0);
  831. else
  832. new_page = NULL;
  833. if (unlikely(!new_page)) {
  834. count_vm_event(THP_FAULT_FALLBACK);
  835. ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
  836. pmd, orig_pmd, page, haddr);
  837. if (ret & VM_FAULT_OOM)
  838. split_huge_page(page);
  839. put_page(page);
  840. goto out;
  841. }
  842. count_vm_event(THP_FAULT_ALLOC);
  843. if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
  844. put_page(new_page);
  845. split_huge_page(page);
  846. put_page(page);
  847. ret |= VM_FAULT_OOM;
  848. goto out;
  849. }
  850. copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
  851. __SetPageUptodate(new_page);
  852. spin_lock(&mm->page_table_lock);
  853. put_page(page);
  854. if (unlikely(!pmd_same(*pmd, orig_pmd))) {
  855. mem_cgroup_uncharge_page(new_page);
  856. put_page(new_page);
  857. } else {
  858. pmd_t entry;
  859. VM_BUG_ON(!PageHead(page));
  860. entry = mk_pmd(new_page, vma->vm_page_prot);
  861. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  862. entry = pmd_mkhuge(entry);
  863. pmdp_clear_flush_notify(vma, haddr, pmd);
  864. page_add_new_anon_rmap(new_page, vma, haddr);
  865. set_pmd_at(mm, haddr, pmd, entry);
  866. update_mmu_cache(vma, address, entry);
  867. page_remove_rmap(page);
  868. put_page(page);
  869. ret |= VM_FAULT_WRITE;
  870. }
  871. out_unlock:
  872. spin_unlock(&mm->page_table_lock);
  873. out:
  874. return ret;
  875. }
  876. struct page *follow_trans_huge_pmd(struct mm_struct *mm,
  877. unsigned long addr,
  878. pmd_t *pmd,
  879. unsigned int flags)
  880. {
  881. struct page *page = NULL;
  882. assert_spin_locked(&mm->page_table_lock);
  883. if (flags & FOLL_WRITE && !pmd_write(*pmd))
  884. goto out;
  885. page = pmd_page(*pmd);
  886. VM_BUG_ON(!PageHead(page));
  887. if (flags & FOLL_TOUCH) {
  888. pmd_t _pmd;
  889. /*
  890. * We should set the dirty bit only for FOLL_WRITE but
  891. * for now the dirty bit in the pmd is meaningless.
  892. * And if the dirty bit will become meaningful and
  893. * we'll only set it with FOLL_WRITE, an atomic
  894. * set_bit will be required on the pmd to set the
  895. * young bit, instead of the current set_pmd_at.
  896. */
  897. _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
  898. set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
  899. }
  900. page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
  901. VM_BUG_ON(!PageCompound(page));
  902. if (flags & FOLL_GET)
  903. get_page_foll(page);
  904. out:
  905. return page;
  906. }
  907. int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  908. pmd_t *pmd, unsigned long addr)
  909. {
  910. int ret = 0;
  911. if (__pmd_trans_huge_lock(pmd, vma) == 1) {
  912. struct page *page;
  913. pgtable_t pgtable;
  914. pgtable = get_pmd_huge_pte(tlb->mm);
  915. page = pmd_page(*pmd);
  916. pmd_clear(pmd);
  917. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  918. page_remove_rmap(page);
  919. VM_BUG_ON(page_mapcount(page) < 0);
  920. add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
  921. VM_BUG_ON(!PageHead(page));
  922. tlb->mm->nr_ptes--;
  923. spin_unlock(&tlb->mm->page_table_lock);
  924. tlb_remove_page(tlb, page);
  925. pte_free(tlb->mm, pgtable);
  926. ret = 1;
  927. }
  928. return ret;
  929. }
  930. int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  931. unsigned long addr, unsigned long end,
  932. unsigned char *vec)
  933. {
  934. int ret = 0;
  935. if (__pmd_trans_huge_lock(pmd, vma) == 1) {
  936. /*
  937. * All logical pages in the range are present
  938. * if backed by a huge page.
  939. */
  940. spin_unlock(&vma->vm_mm->page_table_lock);
  941. memset(vec, 1, (end - addr) >> PAGE_SHIFT);
  942. ret = 1;
  943. }
  944. return ret;
  945. }
  946. int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
  947. unsigned long old_addr,
  948. unsigned long new_addr, unsigned long old_end,
  949. pmd_t *old_pmd, pmd_t *new_pmd)
  950. {
  951. int ret = 0;
  952. pmd_t pmd;
  953. struct mm_struct *mm = vma->vm_mm;
  954. if ((old_addr & ~HPAGE_PMD_MASK) ||
  955. (new_addr & ~HPAGE_PMD_MASK) ||
  956. old_end - old_addr < HPAGE_PMD_SIZE ||
  957. (new_vma->vm_flags & VM_NOHUGEPAGE))
  958. goto out;
  959. /*
  960. * The destination pmd shouldn't be established, free_pgtables()
  961. * should have release it.
  962. */
  963. if (WARN_ON(!pmd_none(*new_pmd))) {
  964. VM_BUG_ON(pmd_trans_huge(*new_pmd));
  965. goto out;
  966. }
  967. ret = __pmd_trans_huge_lock(old_pmd, vma);
  968. if (ret == 1) {
  969. pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
  970. VM_BUG_ON(!pmd_none(*new_pmd));
  971. set_pmd_at(mm, new_addr, new_pmd, pmd);
  972. spin_unlock(&mm->page_table_lock);
  973. }
  974. out:
  975. return ret;
  976. }
  977. int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  978. unsigned long addr, pgprot_t newprot)
  979. {
  980. struct mm_struct *mm = vma->vm_mm;
  981. int ret = 0;
  982. if (__pmd_trans_huge_lock(pmd, vma) == 1) {
  983. pmd_t entry;
  984. entry = pmdp_get_and_clear(mm, addr, pmd);
  985. entry = pmd_modify(entry, newprot);
  986. set_pmd_at(mm, addr, pmd, entry);
  987. spin_unlock(&vma->vm_mm->page_table_lock);
  988. ret = 1;
  989. }
  990. return ret;
  991. }
  992. /*
  993. * Returns 1 if a given pmd maps a stable (not under splitting) thp.
  994. * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
  995. *
  996. * Note that if it returns 1, this routine returns without unlocking page
  997. * table locks. So callers must unlock them.
  998. */
  999. int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
  1000. {
  1001. spin_lock(&vma->vm_mm->page_table_lock);
  1002. if (likely(pmd_trans_huge(*pmd))) {
  1003. if (unlikely(pmd_trans_splitting(*pmd))) {
  1004. spin_unlock(&vma->vm_mm->page_table_lock);
  1005. wait_split_huge_page(vma->anon_vma, pmd);
  1006. return -1;
  1007. } else {
  1008. /* Thp mapped by 'pmd' is stable, so we can
  1009. * handle it as it is. */
  1010. return 1;
  1011. }
  1012. }
  1013. spin_unlock(&vma->vm_mm->page_table_lock);
  1014. return 0;
  1015. }
  1016. pmd_t *page_check_address_pmd(struct page *page,
  1017. struct mm_struct *mm,
  1018. unsigned long address,
  1019. enum page_check_address_pmd_flag flag)
  1020. {
  1021. pgd_t *pgd;
  1022. pud_t *pud;
  1023. pmd_t *pmd, *ret = NULL;
  1024. if (address & ~HPAGE_PMD_MASK)
  1025. goto out;
  1026. pgd = pgd_offset(mm, address);
  1027. if (!pgd_present(*pgd))
  1028. goto out;
  1029. pud = pud_offset(pgd, address);
  1030. if (!pud_present(*pud))
  1031. goto out;
  1032. pmd = pmd_offset(pud, address);
  1033. if (pmd_none(*pmd))
  1034. goto out;
  1035. if (pmd_page(*pmd) != page)
  1036. goto out;
  1037. /*
  1038. * split_vma() may create temporary aliased mappings. There is
  1039. * no risk as long as all huge pmd are found and have their
  1040. * splitting bit set before __split_huge_page_refcount
  1041. * runs. Finding the same huge pmd more than once during the
  1042. * same rmap walk is not a problem.
  1043. */
  1044. if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
  1045. pmd_trans_splitting(*pmd))
  1046. goto out;
  1047. if (pmd_trans_huge(*pmd)) {
  1048. VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
  1049. !pmd_trans_splitting(*pmd));
  1050. ret = pmd;
  1051. }
  1052. out:
  1053. return ret;
  1054. }
  1055. static int __split_huge_page_splitting(struct page *page,
  1056. struct vm_area_struct *vma,
  1057. unsigned long address)
  1058. {
  1059. struct mm_struct *mm = vma->vm_mm;
  1060. pmd_t *pmd;
  1061. int ret = 0;
  1062. spin_lock(&mm->page_table_lock);
  1063. pmd = page_check_address_pmd(page, mm, address,
  1064. PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
  1065. if (pmd) {
  1066. /*
  1067. * We can't temporarily set the pmd to null in order
  1068. * to split it, the pmd must remain marked huge at all
  1069. * times or the VM won't take the pmd_trans_huge paths
  1070. * and it won't wait on the anon_vma->root->mutex to
  1071. * serialize against split_huge_page*.
  1072. */
  1073. pmdp_splitting_flush_notify(vma, address, pmd);
  1074. ret = 1;
  1075. }
  1076. spin_unlock(&mm->page_table_lock);
  1077. return ret;
  1078. }
  1079. static void __split_huge_page_refcount(struct page *page)
  1080. {
  1081. int i;
  1082. struct zone *zone = page_zone(page);
  1083. int tail_count = 0;
  1084. /* prevent PageLRU to go away from under us, and freeze lru stats */
  1085. spin_lock_irq(&zone->lru_lock);
  1086. compound_lock(page);
  1087. /* complete memcg works before add pages to LRU */
  1088. mem_cgroup_split_huge_fixup(page);
  1089. for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
  1090. struct page *page_tail = page + i;
  1091. /* tail_page->_mapcount cannot change */
  1092. BUG_ON(page_mapcount(page_tail) < 0);
  1093. tail_count += page_mapcount(page_tail);
  1094. /* check for overflow */
  1095. BUG_ON(tail_count < 0);
  1096. BUG_ON(atomic_read(&page_tail->_count) != 0);
  1097. /*
  1098. * tail_page->_count is zero and not changing from
  1099. * under us. But get_page_unless_zero() may be running
  1100. * from under us on the tail_page. If we used
  1101. * atomic_set() below instead of atomic_add(), we
  1102. * would then run atomic_set() concurrently with
  1103. * get_page_unless_zero(), and atomic_set() is
  1104. * implemented in C not using locked ops. spin_unlock
  1105. * on x86 sometime uses locked ops because of PPro
  1106. * errata 66, 92, so unless somebody can guarantee
  1107. * atomic_set() here would be safe on all archs (and
  1108. * not only on x86), it's safer to use atomic_add().
  1109. */
  1110. atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
  1111. &page_tail->_count);
  1112. /* after clearing PageTail the gup refcount can be released */
  1113. smp_mb();
  1114. /*
  1115. * retain hwpoison flag of the poisoned tail page:
  1116. * fix for the unsuitable process killed on Guest Machine(KVM)
  1117. * by the memory-failure.
  1118. */
  1119. page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
  1120. page_tail->flags |= (page->flags &
  1121. ((1L << PG_referenced) |
  1122. (1L << PG_swapbacked) |
  1123. (1L << PG_mlocked) |
  1124. (1L << PG_uptodate)));
  1125. page_tail->flags |= (1L << PG_dirty);
  1126. /* clear PageTail before overwriting first_page */
  1127. smp_wmb();
  1128. /*
  1129. * __split_huge_page_splitting() already set the
  1130. * splitting bit in all pmd that could map this
  1131. * hugepage, that will ensure no CPU can alter the
  1132. * mapcount on the head page. The mapcount is only
  1133. * accounted in the head page and it has to be
  1134. * transferred to all tail pages in the below code. So
  1135. * for this code to be safe, the split the mapcount
  1136. * can't change. But that doesn't mean userland can't
  1137. * keep changing and reading the page contents while
  1138. * we transfer the mapcount, so the pmd splitting
  1139. * status is achieved setting a reserved bit in the
  1140. * pmd, not by clearing the present bit.
  1141. */
  1142. page_tail->_mapcount = page->_mapcount;
  1143. BUG_ON(page_tail->mapping);
  1144. page_tail->mapping = page->mapping;
  1145. page_tail->index = page->index + i;
  1146. BUG_ON(!PageAnon(page_tail));
  1147. BUG_ON(!PageUptodate(page_tail));
  1148. BUG_ON(!PageDirty(page_tail));
  1149. BUG_ON(!PageSwapBacked(page_tail));
  1150. lru_add_page_tail(zone, page, page_tail);
  1151. }
  1152. atomic_sub(tail_count, &page->_count);
  1153. BUG_ON(atomic_read(&page->_count) <= 0);
  1154. __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
  1155. ClearPageCompound(page);
  1156. compound_unlock(page);
  1157. spin_unlock_irq(&zone->lru_lock);
  1158. for (i = 1; i < HPAGE_PMD_NR; i++) {
  1159. struct page *page_tail = page + i;
  1160. BUG_ON(page_count(page_tail) <= 0);
  1161. /*
  1162. * Tail pages may be freed if there wasn't any mapping
  1163. * like if add_to_swap() is running on a lru page that
  1164. * had its mapping zapped. And freeing these pages
  1165. * requires taking the lru_lock so we do the put_page
  1166. * of the tail pages after the split is complete.
  1167. */
  1168. put_page(page_tail);
  1169. }
  1170. /*
  1171. * Only the head page (now become a regular page) is required
  1172. * to be pinned by the caller.
  1173. */
  1174. BUG_ON(page_count(page) <= 0);
  1175. }
  1176. static int __split_huge_page_map(struct page *page,
  1177. struct vm_area_struct *vma,
  1178. unsigned long address)
  1179. {
  1180. struct mm_struct *mm = vma->vm_mm;
  1181. pmd_t *pmd, _pmd;
  1182. int ret = 0, i;
  1183. pgtable_t pgtable;
  1184. unsigned long haddr;
  1185. spin_lock(&mm->page_table_lock);
  1186. pmd = page_check_address_pmd(page, mm, address,
  1187. PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
  1188. if (pmd) {
  1189. pgtable = get_pmd_huge_pte(mm);
  1190. pmd_populate(mm, &_pmd, pgtable);
  1191. for (i = 0, haddr = address; i < HPAGE_PMD_NR;
  1192. i++, haddr += PAGE_SIZE) {
  1193. pte_t *pte, entry;
  1194. BUG_ON(PageCompound(page+i));
  1195. entry = mk_pte(page + i, vma->vm_page_prot);
  1196. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1197. if (!pmd_write(*pmd))
  1198. entry = pte_wrprotect(entry);
  1199. else
  1200. BUG_ON(page_mapcount(page) != 1);
  1201. if (!pmd_young(*pmd))
  1202. entry = pte_mkold(entry);
  1203. pte = pte_offset_map(&_pmd, haddr);
  1204. BUG_ON(!pte_none(*pte));
  1205. set_pte_at(mm, haddr, pte, entry);
  1206. pte_unmap(pte);
  1207. }
  1208. smp_wmb(); /* make pte visible before pmd */
  1209. /*
  1210. * Up to this point the pmd is present and huge and
  1211. * userland has the whole access to the hugepage
  1212. * during the split (which happens in place). If we
  1213. * overwrite the pmd with the not-huge version
  1214. * pointing to the pte here (which of course we could
  1215. * if all CPUs were bug free), userland could trigger
  1216. * a small page size TLB miss on the small sized TLB
  1217. * while the hugepage TLB entry is still established
  1218. * in the huge TLB. Some CPU doesn't like that. See
  1219. * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
  1220. * Erratum 383 on page 93. Intel should be safe but is
  1221. * also warns that it's only safe if the permission
  1222. * and cache attributes of the two entries loaded in
  1223. * the two TLB is identical (which should be the case
  1224. * here). But it is generally safer to never allow
  1225. * small and huge TLB entries for the same virtual
  1226. * address to be loaded simultaneously. So instead of
  1227. * doing "pmd_populate(); flush_tlb_range();" we first
  1228. * mark the current pmd notpresent (atomically because
  1229. * here the pmd_trans_huge and pmd_trans_splitting
  1230. * must remain set at all times on the pmd until the
  1231. * split is complete for this pmd), then we flush the
  1232. * SMP TLB and finally we write the non-huge version
  1233. * of the pmd entry with pmd_populate.
  1234. */
  1235. set_pmd_at(mm, address, pmd, pmd_mknotpresent(*pmd));
  1236. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  1237. pmd_populate(mm, pmd, pgtable);
  1238. ret = 1;
  1239. }
  1240. spin_unlock(&mm->page_table_lock);
  1241. return ret;
  1242. }
  1243. /* must be called with anon_vma->root->mutex hold */
  1244. static void __split_huge_page(struct page *page,
  1245. struct anon_vma *anon_vma)
  1246. {
  1247. int mapcount, mapcount2;
  1248. struct anon_vma_chain *avc;
  1249. BUG_ON(!PageHead(page));
  1250. BUG_ON(PageTail(page));
  1251. mapcount = 0;
  1252. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  1253. struct vm_area_struct *vma = avc->vma;
  1254. unsigned long addr = vma_address(page, vma);
  1255. BUG_ON(is_vma_temporary_stack(vma));
  1256. if (addr == -EFAULT)
  1257. continue;
  1258. mapcount += __split_huge_page_splitting(page, vma, addr);
  1259. }
  1260. /*
  1261. * It is critical that new vmas are added to the tail of the
  1262. * anon_vma list. This guarantes that if copy_huge_pmd() runs
  1263. * and establishes a child pmd before
  1264. * __split_huge_page_splitting() freezes the parent pmd (so if
  1265. * we fail to prevent copy_huge_pmd() from running until the
  1266. * whole __split_huge_page() is complete), we will still see
  1267. * the newly established pmd of the child later during the
  1268. * walk, to be able to set it as pmd_trans_splitting too.
  1269. */
  1270. if (mapcount != page_mapcount(page))
  1271. printk(KERN_ERR "mapcount %d page_mapcount %d\n",
  1272. mapcount, page_mapcount(page));
  1273. BUG_ON(mapcount != page_mapcount(page));
  1274. __split_huge_page_refcount(page);
  1275. mapcount2 = 0;
  1276. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  1277. struct vm_area_struct *vma = avc->vma;
  1278. unsigned long addr = vma_address(page, vma);
  1279. BUG_ON(is_vma_temporary_stack(vma));
  1280. if (addr == -EFAULT)
  1281. continue;
  1282. mapcount2 += __split_huge_page_map(page, vma, addr);
  1283. }
  1284. if (mapcount != mapcount2)
  1285. printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
  1286. mapcount, mapcount2, page_mapcount(page));
  1287. BUG_ON(mapcount != mapcount2);
  1288. }
  1289. int split_huge_page(struct page *page)
  1290. {
  1291. struct anon_vma *anon_vma;
  1292. int ret = 1;
  1293. BUG_ON(!PageAnon(page));
  1294. anon_vma = page_lock_anon_vma(page);
  1295. if (!anon_vma)
  1296. goto out;
  1297. ret = 0;
  1298. if (!PageCompound(page))
  1299. goto out_unlock;
  1300. BUG_ON(!PageSwapBacked(page));
  1301. __split_huge_page(page, anon_vma);
  1302. count_vm_event(THP_SPLIT);
  1303. BUG_ON(PageCompound(page));
  1304. out_unlock:
  1305. page_unlock_anon_vma(anon_vma);
  1306. out:
  1307. return ret;
  1308. }
  1309. #define VM_NO_THP (VM_SPECIAL|VM_INSERTPAGE|VM_MIXEDMAP|VM_SAO| \
  1310. VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
  1311. int hugepage_madvise(struct vm_area_struct *vma,
  1312. unsigned long *vm_flags, int advice)
  1313. {
  1314. switch (advice) {
  1315. case MADV_HUGEPAGE:
  1316. /*
  1317. * Be somewhat over-protective like KSM for now!
  1318. */
  1319. if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
  1320. return -EINVAL;
  1321. *vm_flags &= ~VM_NOHUGEPAGE;
  1322. *vm_flags |= VM_HUGEPAGE;
  1323. /*
  1324. * If the vma become good for khugepaged to scan,
  1325. * register it here without waiting a page fault that
  1326. * may not happen any time soon.
  1327. */
  1328. if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
  1329. return -ENOMEM;
  1330. break;
  1331. case MADV_NOHUGEPAGE:
  1332. /*
  1333. * Be somewhat over-protective like KSM for now!
  1334. */
  1335. if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
  1336. return -EINVAL;
  1337. *vm_flags &= ~VM_HUGEPAGE;
  1338. *vm_flags |= VM_NOHUGEPAGE;
  1339. /*
  1340. * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
  1341. * this vma even if we leave the mm registered in khugepaged if
  1342. * it got registered before VM_NOHUGEPAGE was set.
  1343. */
  1344. break;
  1345. }
  1346. return 0;
  1347. }
  1348. static int __init khugepaged_slab_init(void)
  1349. {
  1350. mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
  1351. sizeof(struct mm_slot),
  1352. __alignof__(struct mm_slot), 0, NULL);
  1353. if (!mm_slot_cache)
  1354. return -ENOMEM;
  1355. return 0;
  1356. }
  1357. static void __init khugepaged_slab_free(void)
  1358. {
  1359. kmem_cache_destroy(mm_slot_cache);
  1360. mm_slot_cache = NULL;
  1361. }
  1362. static inline struct mm_slot *alloc_mm_slot(void)
  1363. {
  1364. if (!mm_slot_cache) /* initialization failed */
  1365. return NULL;
  1366. return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
  1367. }
  1368. static inline void free_mm_slot(struct mm_slot *mm_slot)
  1369. {
  1370. kmem_cache_free(mm_slot_cache, mm_slot);
  1371. }
  1372. static int __init mm_slots_hash_init(void)
  1373. {
  1374. mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
  1375. GFP_KERNEL);
  1376. if (!mm_slots_hash)
  1377. return -ENOMEM;
  1378. return 0;
  1379. }
  1380. #if 0
  1381. static void __init mm_slots_hash_free(void)
  1382. {
  1383. kfree(mm_slots_hash);
  1384. mm_slots_hash = NULL;
  1385. }
  1386. #endif
  1387. static struct mm_slot *get_mm_slot(struct mm_struct *mm)
  1388. {
  1389. struct mm_slot *mm_slot;
  1390. struct hlist_head *bucket;
  1391. struct hlist_node *node;
  1392. bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
  1393. % MM_SLOTS_HASH_HEADS];
  1394. hlist_for_each_entry(mm_slot, node, bucket, hash) {
  1395. if (mm == mm_slot->mm)
  1396. return mm_slot;
  1397. }
  1398. return NULL;
  1399. }
  1400. static void insert_to_mm_slots_hash(struct mm_struct *mm,
  1401. struct mm_slot *mm_slot)
  1402. {
  1403. struct hlist_head *bucket;
  1404. bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
  1405. % MM_SLOTS_HASH_HEADS];
  1406. mm_slot->mm = mm;
  1407. hlist_add_head(&mm_slot->hash, bucket);
  1408. }
  1409. static inline int khugepaged_test_exit(struct mm_struct *mm)
  1410. {
  1411. return atomic_read(&mm->mm_users) == 0;
  1412. }
  1413. int __khugepaged_enter(struct mm_struct *mm)
  1414. {
  1415. struct mm_slot *mm_slot;
  1416. int wakeup;
  1417. mm_slot = alloc_mm_slot();
  1418. if (!mm_slot)
  1419. return -ENOMEM;
  1420. /* __khugepaged_exit() must not run from under us */
  1421. VM_BUG_ON(khugepaged_test_exit(mm));
  1422. if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
  1423. free_mm_slot(mm_slot);
  1424. return 0;
  1425. }
  1426. spin_lock(&khugepaged_mm_lock);
  1427. insert_to_mm_slots_hash(mm, mm_slot);
  1428. /*
  1429. * Insert just behind the scanning cursor, to let the area settle
  1430. * down a little.
  1431. */
  1432. wakeup = list_empty(&khugepaged_scan.mm_head);
  1433. list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
  1434. spin_unlock(&khugepaged_mm_lock);
  1435. atomic_inc(&mm->mm_count);
  1436. if (wakeup)
  1437. wake_up_interruptible(&khugepaged_wait);
  1438. return 0;
  1439. }
  1440. int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
  1441. unsigned long vm_flags)
  1442. {
  1443. unsigned long hstart, hend;
  1444. if (!vma->anon_vma)
  1445. /*
  1446. * Not yet faulted in so we will register later in the
  1447. * page fault if needed.
  1448. */
  1449. return 0;
  1450. if (vma->vm_ops)
  1451. /* khugepaged not yet working on file or special mappings */
  1452. return 0;
  1453. /*
  1454. * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
  1455. * true too, verify it here.
  1456. */
  1457. VM_BUG_ON(is_linear_pfn_mapping(vma) || vm_flags & VM_NO_THP);
  1458. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1459. hend = vma->vm_end & HPAGE_PMD_MASK;
  1460. if (hstart < hend)
  1461. return khugepaged_enter(vma, vm_flags);
  1462. return 0;
  1463. }
  1464. void __khugepaged_exit(struct mm_struct *mm)
  1465. {
  1466. struct mm_slot *mm_slot;
  1467. int free = 0;
  1468. spin_lock(&khugepaged_mm_lock);
  1469. mm_slot = get_mm_slot(mm);
  1470. if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
  1471. hlist_del(&mm_slot->hash);
  1472. list_del(&mm_slot->mm_node);
  1473. free = 1;
  1474. }
  1475. spin_unlock(&khugepaged_mm_lock);
  1476. if (free) {
  1477. clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1478. free_mm_slot(mm_slot);
  1479. mmdrop(mm);
  1480. } else if (mm_slot) {
  1481. /*
  1482. * This is required to serialize against
  1483. * khugepaged_test_exit() (which is guaranteed to run
  1484. * under mmap sem read mode). Stop here (after we
  1485. * return all pagetables will be destroyed) until
  1486. * khugepaged has finished working on the pagetables
  1487. * under the mmap_sem.
  1488. */
  1489. down_write(&mm->mmap_sem);
  1490. up_write(&mm->mmap_sem);
  1491. }
  1492. }
  1493. static void release_pte_page(struct page *page)
  1494. {
  1495. /* 0 stands for page_is_file_cache(page) == false */
  1496. dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
  1497. unlock_page(page);
  1498. putback_lru_page(page);
  1499. }
  1500. static void release_pte_pages(pte_t *pte, pte_t *_pte)
  1501. {
  1502. while (--_pte >= pte) {
  1503. pte_t pteval = *_pte;
  1504. if (!pte_none(pteval))
  1505. release_pte_page(pte_page(pteval));
  1506. }
  1507. }
  1508. static void release_all_pte_pages(pte_t *pte)
  1509. {
  1510. release_pte_pages(pte, pte + HPAGE_PMD_NR);
  1511. }
  1512. static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  1513. unsigned long address,
  1514. pte_t *pte)
  1515. {
  1516. struct page *page;
  1517. pte_t *_pte;
  1518. int referenced = 0, isolated = 0, none = 0;
  1519. for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
  1520. _pte++, address += PAGE_SIZE) {
  1521. pte_t pteval = *_pte;
  1522. if (pte_none(pteval)) {
  1523. if (++none <= khugepaged_max_ptes_none)
  1524. continue;
  1525. else {
  1526. release_pte_pages(pte, _pte);
  1527. goto out;
  1528. }
  1529. }
  1530. if (!pte_present(pteval) || !pte_write(pteval)) {
  1531. release_pte_pages(pte, _pte);
  1532. goto out;
  1533. }
  1534. page = vm_normal_page(vma, address, pteval);
  1535. if (unlikely(!page)) {
  1536. release_pte_pages(pte, _pte);
  1537. goto out;
  1538. }
  1539. VM_BUG_ON(PageCompound(page));
  1540. BUG_ON(!PageAnon(page));
  1541. VM_BUG_ON(!PageSwapBacked(page));
  1542. /* cannot use mapcount: can't collapse if there's a gup pin */
  1543. if (page_count(page) != 1) {
  1544. release_pte_pages(pte, _pte);
  1545. goto out;
  1546. }
  1547. /*
  1548. * We can do it before isolate_lru_page because the
  1549. * page can't be freed from under us. NOTE: PG_lock
  1550. * is needed to serialize against split_huge_page
  1551. * when invoked from the VM.
  1552. */
  1553. if (!trylock_page(page)) {
  1554. release_pte_pages(pte, _pte);
  1555. goto out;
  1556. }
  1557. /*
  1558. * Isolate the page to avoid collapsing an hugepage
  1559. * currently in use by the VM.
  1560. */
  1561. if (isolate_lru_page(page)) {
  1562. unlock_page(page);
  1563. release_pte_pages(pte, _pte);
  1564. goto out;
  1565. }
  1566. /* 0 stands for page_is_file_cache(page) == false */
  1567. inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
  1568. VM_BUG_ON(!PageLocked(page));
  1569. VM_BUG_ON(PageLRU(page));
  1570. /* If there is no mapped pte young don't collapse the page */
  1571. if (pte_young(pteval) || PageReferenced(page) ||
  1572. mmu_notifier_test_young(vma->vm_mm, address))
  1573. referenced = 1;
  1574. }
  1575. if (unlikely(!referenced))
  1576. release_all_pte_pages(pte);
  1577. else
  1578. isolated = 1;
  1579. out:
  1580. return isolated;
  1581. }
  1582. static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
  1583. struct vm_area_struct *vma,
  1584. unsigned long address,
  1585. spinlock_t *ptl)
  1586. {
  1587. pte_t *_pte;
  1588. for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
  1589. pte_t pteval = *_pte;
  1590. struct page *src_page;
  1591. if (pte_none(pteval)) {
  1592. clear_user_highpage(page, address);
  1593. add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
  1594. } else {
  1595. src_page = pte_page(pteval);
  1596. copy_user_highpage(page, src_page, address, vma);
  1597. VM_BUG_ON(page_mapcount(src_page) != 1);
  1598. VM_BUG_ON(page_count(src_page) != 2);
  1599. release_pte_page(src_page);
  1600. /*
  1601. * ptl mostly unnecessary, but preempt has to
  1602. * be disabled to update the per-cpu stats
  1603. * inside page_remove_rmap().
  1604. */
  1605. spin_lock(ptl);
  1606. /*
  1607. * paravirt calls inside pte_clear here are
  1608. * superfluous.
  1609. */
  1610. pte_clear(vma->vm_mm, address, _pte);
  1611. page_remove_rmap(src_page);
  1612. spin_unlock(ptl);
  1613. free_page_and_swap_cache(src_page);
  1614. }
  1615. address += PAGE_SIZE;
  1616. page++;
  1617. }
  1618. }
  1619. static void collapse_huge_page(struct mm_struct *mm,
  1620. unsigned long address,
  1621. struct page **hpage,
  1622. struct vm_area_struct *vma,
  1623. int node)
  1624. {
  1625. pgd_t *pgd;
  1626. pud_t *pud;
  1627. pmd_t *pmd, _pmd;
  1628. pte_t *pte;
  1629. pgtable_t pgtable;
  1630. struct page *new_page;
  1631. spinlock_t *ptl;
  1632. int isolated;
  1633. unsigned long hstart, hend;
  1634. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1635. #ifndef CONFIG_NUMA
  1636. up_read(&mm->mmap_sem);
  1637. VM_BUG_ON(!*hpage);
  1638. new_page = *hpage;
  1639. #else
  1640. VM_BUG_ON(*hpage);
  1641. /*
  1642. * Allocate the page while the vma is still valid and under
  1643. * the mmap_sem read mode so there is no memory allocation
  1644. * later when we take the mmap_sem in write mode. This is more
  1645. * friendly behavior (OTOH it may actually hide bugs) to
  1646. * filesystems in userland with daemons allocating memory in
  1647. * the userland I/O paths. Allocating memory with the
  1648. * mmap_sem in read mode is good idea also to allow greater
  1649. * scalability.
  1650. */
  1651. new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
  1652. node, __GFP_OTHER_NODE);
  1653. /*
  1654. * After allocating the hugepage, release the mmap_sem read lock in
  1655. * preparation for taking it in write mode.
  1656. */
  1657. up_read(&mm->mmap_sem);
  1658. if (unlikely(!new_page)) {
  1659. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  1660. *hpage = ERR_PTR(-ENOMEM);
  1661. return;
  1662. }
  1663. #endif
  1664. count_vm_event(THP_COLLAPSE_ALLOC);
  1665. if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
  1666. #ifdef CONFIG_NUMA
  1667. put_page(new_page);
  1668. #endif
  1669. return;
  1670. }
  1671. /*
  1672. * Prevent all access to pagetables with the exception of
  1673. * gup_fast later hanlded by the ptep_clear_flush and the VM
  1674. * handled by the anon_vma lock + PG_lock.
  1675. */
  1676. down_write(&mm->mmap_sem);
  1677. if (unlikely(khugepaged_test_exit(mm)))
  1678. goto out;
  1679. vma = find_vma(mm, address);
  1680. if (!vma)
  1681. goto out;
  1682. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1683. hend = vma->vm_end & HPAGE_PMD_MASK;
  1684. if (address < hstart || address + HPAGE_PMD_SIZE > hend)
  1685. goto out;
  1686. if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
  1687. (vma->vm_flags & VM_NOHUGEPAGE))
  1688. goto out;
  1689. if (!vma->anon_vma || vma->vm_ops)
  1690. goto out;
  1691. if (is_vma_temporary_stack(vma))
  1692. goto out;
  1693. /*
  1694. * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
  1695. * true too, verify it here.
  1696. */
  1697. VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
  1698. pgd = pgd_offset(mm, address);
  1699. if (!pgd_present(*pgd))
  1700. goto out;
  1701. pud = pud_offset(pgd, address);
  1702. if (!pud_present(*pud))
  1703. goto out;
  1704. pmd = pmd_offset(pud, address);
  1705. /* pmd can't go away or become huge under us */
  1706. if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
  1707. goto out;
  1708. anon_vma_lock(vma->anon_vma);
  1709. pte = pte_offset_map(pmd, address);
  1710. ptl = pte_lockptr(mm, pmd);
  1711. spin_lock(&mm->page_table_lock); /* probably unnecessary */
  1712. /*
  1713. * After this gup_fast can't run anymore. This also removes
  1714. * any huge TLB entry from the CPU so we won't allow
  1715. * huge and small TLB entries for the same virtual address
  1716. * to avoid the risk of CPU bugs in that area.
  1717. */
  1718. _pmd = pmdp_clear_flush_notify(vma, address, pmd);
  1719. spin_unlock(&mm->page_table_lock);
  1720. spin_lock(ptl);
  1721. isolated = __collapse_huge_page_isolate(vma, address, pte);
  1722. spin_unlock(ptl);
  1723. if (unlikely(!isolated)) {
  1724. pte_unmap(pte);
  1725. spin_lock(&mm->page_table_lock);
  1726. BUG_ON(!pmd_none(*pmd));
  1727. /*
  1728. * We can only use set_pmd_at when establishing
  1729. * hugepmds and never for establishing regular pmds that
  1730. * points to regular pagetables. Use pmd_populate for that
  1731. */
  1732. pmd_populate(mm, pmd, pmd_pgtable(_pmd));
  1733. spin_unlock(&mm->page_table_lock);
  1734. anon_vma_unlock(vma->anon_vma);
  1735. goto out;
  1736. }
  1737. /*
  1738. * All pages are isolated and locked so anon_vma rmap
  1739. * can't run anymore.
  1740. */
  1741. anon_vma_unlock(vma->anon_vma);
  1742. __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
  1743. pte_unmap(pte);
  1744. __SetPageUptodate(new_page);
  1745. pgtable = pmd_pgtable(_pmd);
  1746. VM_BUG_ON(page_count(pgtable) != 1);
  1747. VM_BUG_ON(page_mapcount(pgtable) != 0);
  1748. _pmd = mk_pmd(new_page, vma->vm_page_prot);
  1749. _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
  1750. _pmd = pmd_mkhuge(_pmd);
  1751. /*
  1752. * spin_lock() below is not the equivalent of smp_wmb(), so
  1753. * this is needed to avoid the copy_huge_page writes to become
  1754. * visible after the set_pmd_at() write.
  1755. */
  1756. smp_wmb();
  1757. spin_lock(&mm->page_table_lock);
  1758. BUG_ON(!pmd_none(*pmd));
  1759. page_add_new_anon_rmap(new_page, vma, address);
  1760. set_pmd_at(mm, address, pmd, _pmd);
  1761. update_mmu_cache(vma, address, _pmd);
  1762. prepare_pmd_huge_pte(pgtable, mm);
  1763. spin_unlock(&mm->page_table_lock);
  1764. #ifndef CONFIG_NUMA
  1765. *hpage = NULL;
  1766. #endif
  1767. khugepaged_pages_collapsed++;
  1768. out_up_write:
  1769. up_write(&mm->mmap_sem);
  1770. return;
  1771. out:
  1772. mem_cgroup_uncharge_page(new_page);
  1773. #ifdef CONFIG_NUMA
  1774. put_page(new_page);
  1775. #endif
  1776. goto out_up_write;
  1777. }
  1778. static int khugepaged_scan_pmd(struct mm_struct *mm,
  1779. struct vm_area_struct *vma,
  1780. unsigned long address,
  1781. struct page **hpage)
  1782. {
  1783. pgd_t *pgd;
  1784. pud_t *pud;
  1785. pmd_t *pmd;
  1786. pte_t *pte, *_pte;
  1787. int ret = 0, referenced = 0, none = 0;
  1788. struct page *page;
  1789. unsigned long _address;
  1790. spinlock_t *ptl;
  1791. int node = -1;
  1792. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1793. pgd = pgd_offset(mm, address);
  1794. if (!pgd_present(*pgd))
  1795. goto out;
  1796. pud = pud_offset(pgd, address);
  1797. if (!pud_present(*pud))
  1798. goto out;
  1799. pmd = pmd_offset(pud, address);
  1800. if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
  1801. goto out;
  1802. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  1803. for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
  1804. _pte++, _address += PAGE_SIZE) {
  1805. pte_t pteval = *_pte;
  1806. if (pte_none(pteval)) {
  1807. if (++none <= khugepaged_max_ptes_none)
  1808. continue;
  1809. else
  1810. goto out_unmap;
  1811. }
  1812. if (!pte_present(pteval) || !pte_write(pteval))
  1813. goto out_unmap;
  1814. page = vm_normal_page(vma, _address, pteval);
  1815. if (unlikely(!page))
  1816. goto out_unmap;
  1817. /*
  1818. * Chose the node of the first page. This could
  1819. * be more sophisticated and look at more pages,
  1820. * but isn't for now.
  1821. */
  1822. if (node == -1)
  1823. node = page_to_nid(page);
  1824. VM_BUG_ON(PageCompound(page));
  1825. if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
  1826. goto out_unmap;
  1827. /* cannot use mapcount: can't collapse if there's a gup pin */
  1828. if (page_count(page) != 1)
  1829. goto out_unmap;
  1830. if (pte_young(pteval) || PageReferenced(page) ||
  1831. mmu_notifier_test_young(vma->vm_mm, address))
  1832. referenced = 1;
  1833. }
  1834. if (referenced)
  1835. ret = 1;
  1836. out_unmap:
  1837. pte_unmap_unlock(pte, ptl);
  1838. if (ret)
  1839. /* collapse_huge_page will return with the mmap_sem released */
  1840. collapse_huge_page(mm, address, hpage, vma, node);
  1841. out:
  1842. return ret;
  1843. }
  1844. static void collect_mm_slot(struct mm_slot *mm_slot)
  1845. {
  1846. struct mm_struct *mm = mm_slot->mm;
  1847. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1848. if (khugepaged_test_exit(mm)) {
  1849. /* free mm_slot */
  1850. hlist_del(&mm_slot->hash);
  1851. list_del(&mm_slot->mm_node);
  1852. /*
  1853. * Not strictly needed because the mm exited already.
  1854. *
  1855. * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1856. */
  1857. /* khugepaged_mm_lock actually not necessary for the below */
  1858. free_mm_slot(mm_slot);
  1859. mmdrop(mm);
  1860. }
  1861. }
  1862. static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
  1863. struct page **hpage)
  1864. __releases(&khugepaged_mm_lock)
  1865. __acquires(&khugepaged_mm_lock)
  1866. {
  1867. struct mm_slot *mm_slot;
  1868. struct mm_struct *mm;
  1869. struct vm_area_struct *vma;
  1870. int progress = 0;
  1871. VM_BUG_ON(!pages);
  1872. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1873. if (khugepaged_scan.mm_slot)
  1874. mm_slot = khugepaged_scan.mm_slot;
  1875. else {
  1876. mm_slot = list_entry(khugepaged_scan.mm_head.next,
  1877. struct mm_slot, mm_node);
  1878. khugepaged_scan.address = 0;
  1879. khugepaged_scan.mm_slot = mm_slot;
  1880. }
  1881. spin_unlock(&khugepaged_mm_lock);
  1882. mm = mm_slot->mm;
  1883. down_read(&mm->mmap_sem);
  1884. if (unlikely(khugepaged_test_exit(mm)))
  1885. vma = NULL;
  1886. else
  1887. vma = find_vma(mm, khugepaged_scan.address);
  1888. progress++;
  1889. for (; vma; vma = vma->vm_next) {
  1890. unsigned long hstart, hend;
  1891. cond_resched();
  1892. if (unlikely(khugepaged_test_exit(mm))) {
  1893. progress++;
  1894. break;
  1895. }
  1896. if ((!(vma->vm_flags & VM_HUGEPAGE) &&
  1897. !khugepaged_always()) ||
  1898. (vma->vm_flags & VM_NOHUGEPAGE)) {
  1899. skip:
  1900. progress++;
  1901. continue;
  1902. }
  1903. if (!vma->anon_vma || vma->vm_ops)
  1904. goto skip;
  1905. if (is_vma_temporary_stack(vma))
  1906. goto skip;
  1907. /*
  1908. * If is_pfn_mapping() is true is_learn_pfn_mapping()
  1909. * must be true too, verify it here.
  1910. */
  1911. VM_BUG_ON(is_linear_pfn_mapping(vma) ||
  1912. vma->vm_flags & VM_NO_THP);
  1913. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1914. hend = vma->vm_end & HPAGE_PMD_MASK;
  1915. if (hstart >= hend)
  1916. goto skip;
  1917. if (khugepaged_scan.address > hend)
  1918. goto skip;
  1919. if (khugepaged_scan.address < hstart)
  1920. khugepaged_scan.address = hstart;
  1921. VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
  1922. while (khugepaged_scan.address < hend) {
  1923. int ret;
  1924. cond_resched();
  1925. if (unlikely(khugepaged_test_exit(mm)))
  1926. goto breakouterloop;
  1927. VM_BUG_ON(khugepaged_scan.address < hstart ||
  1928. khugepaged_scan.address + HPAGE_PMD_SIZE >
  1929. hend);
  1930. ret = khugepaged_scan_pmd(mm, vma,
  1931. khugepaged_scan.address,
  1932. hpage);
  1933. /* move to next address */
  1934. khugepaged_scan.address += HPAGE_PMD_SIZE;
  1935. progress += HPAGE_PMD_NR;
  1936. if (ret)
  1937. /* we released mmap_sem so break loop */
  1938. goto breakouterloop_mmap_sem;
  1939. if (progress >= pages)
  1940. goto breakouterloop;
  1941. }
  1942. }
  1943. breakouterloop:
  1944. up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
  1945. breakouterloop_mmap_sem:
  1946. spin_lock(&khugepaged_mm_lock);
  1947. VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
  1948. /*
  1949. * Release the current mm_slot if this mm is about to die, or
  1950. * if we scanned all vmas of this mm.
  1951. */
  1952. if (khugepaged_test_exit(mm) || !vma) {
  1953. /*
  1954. * Make sure that if mm_users is reaching zero while
  1955. * khugepaged runs here, khugepaged_exit will find
  1956. * mm_slot not pointing to the exiting mm.
  1957. */
  1958. if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
  1959. khugepaged_scan.mm_slot = list_entry(
  1960. mm_slot->mm_node.next,
  1961. struct mm_slot, mm_node);
  1962. khugepaged_scan.address = 0;
  1963. } else {
  1964. khugepaged_scan.mm_slot = NULL;
  1965. khugepaged_full_scans++;
  1966. }
  1967. collect_mm_slot(mm_slot);
  1968. }
  1969. return progress;
  1970. }
  1971. static int khugepaged_has_work(void)
  1972. {
  1973. return !list_empty(&khugepaged_scan.mm_head) &&
  1974. khugepaged_enabled();
  1975. }
  1976. static int khugepaged_wait_event(void)
  1977. {
  1978. return !list_empty(&khugepaged_scan.mm_head) ||
  1979. !khugepaged_enabled();
  1980. }
  1981. static void khugepaged_do_scan(struct page **hpage)
  1982. {
  1983. unsigned int progress = 0, pass_through_head = 0;
  1984. unsigned int pages = khugepaged_pages_to_scan;
  1985. barrier(); /* write khugepaged_pages_to_scan to local stack */
  1986. while (progress < pages) {
  1987. cond_resched();
  1988. #ifndef CONFIG_NUMA
  1989. if (!*hpage) {
  1990. *hpage = alloc_hugepage(khugepaged_defrag());
  1991. if (unlikely(!*hpage)) {
  1992. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  1993. break;
  1994. }
  1995. count_vm_event(THP_COLLAPSE_ALLOC);
  1996. }
  1997. #else
  1998. if (IS_ERR(*hpage))
  1999. break;
  2000. #endif
  2001. if (unlikely(kthread_should_stop() || freezing(current)))
  2002. break;
  2003. spin_lock(&khugepaged_mm_lock);
  2004. if (!khugepaged_scan.mm_slot)
  2005. pass_through_head++;
  2006. if (khugepaged_has_work() &&
  2007. pass_through_head < 2)
  2008. progress += khugepaged_scan_mm_slot(pages - progress,
  2009. hpage);
  2010. else
  2011. progress = pages;
  2012. spin_unlock(&khugepaged_mm_lock);
  2013. }
  2014. }
  2015. static void khugepaged_alloc_sleep(void)
  2016. {
  2017. wait_event_freezable_timeout(khugepaged_wait, false,
  2018. msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
  2019. }
  2020. #ifndef CONFIG_NUMA
  2021. static struct page *khugepaged_alloc_hugepage(void)
  2022. {
  2023. struct page *hpage;
  2024. do {
  2025. hpage = alloc_hugepage(khugepaged_defrag());
  2026. if (!hpage) {
  2027. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  2028. khugepaged_alloc_sleep();
  2029. } else
  2030. count_vm_event(THP_COLLAPSE_ALLOC);
  2031. } while (unlikely(!hpage) &&
  2032. likely(khugepaged_enabled()));
  2033. return hpage;
  2034. }
  2035. #endif
  2036. static void khugepaged_loop(void)
  2037. {
  2038. struct page *hpage;
  2039. #ifdef CONFIG_NUMA
  2040. hpage = NULL;
  2041. #endif
  2042. while (likely(khugepaged_enabled())) {
  2043. #ifndef CONFIG_NUMA
  2044. hpage = khugepaged_alloc_hugepage();
  2045. if (unlikely(!hpage))
  2046. break;
  2047. #else
  2048. if (IS_ERR(hpage)) {
  2049. khugepaged_alloc_sleep();
  2050. hpage = NULL;
  2051. }
  2052. #endif
  2053. khugepaged_do_scan(&hpage);
  2054. #ifndef CONFIG_NUMA
  2055. if (hpage)
  2056. put_page(hpage);
  2057. #endif
  2058. try_to_freeze();
  2059. if (unlikely(kthread_should_stop()))
  2060. break;
  2061. if (khugepaged_has_work()) {
  2062. if (!khugepaged_scan_sleep_millisecs)
  2063. continue;
  2064. wait_event_freezable_timeout(khugepaged_wait, false,
  2065. msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
  2066. } else if (khugepaged_enabled())
  2067. wait_event_freezable(khugepaged_wait,
  2068. khugepaged_wait_event());
  2069. }
  2070. }
  2071. static int khugepaged(void *none)
  2072. {
  2073. struct mm_slot *mm_slot;
  2074. set_freezable();
  2075. set_user_nice(current, 19);
  2076. /* serialize with start_khugepaged() */
  2077. mutex_lock(&khugepaged_mutex);
  2078. for (;;) {
  2079. mutex_unlock(&khugepaged_mutex);
  2080. VM_BUG_ON(khugepaged_thread != current);
  2081. khugepaged_loop();
  2082. VM_BUG_ON(khugepaged_thread != current);
  2083. mutex_lock(&khugepaged_mutex);
  2084. if (!khugepaged_enabled())
  2085. break;
  2086. if (unlikely(kthread_should_stop()))
  2087. break;
  2088. }
  2089. spin_lock(&khugepaged_mm_lock);
  2090. mm_slot = khugepaged_scan.mm_slot;
  2091. khugepaged_scan.mm_slot = NULL;
  2092. if (mm_slot)
  2093. collect_mm_slot(mm_slot);
  2094. spin_unlock(&khugepaged_mm_lock);
  2095. khugepaged_thread = NULL;
  2096. mutex_unlock(&khugepaged_mutex);
  2097. return 0;
  2098. }
  2099. void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
  2100. {
  2101. struct page *page;
  2102. spin_lock(&mm->page_table_lock);
  2103. if (unlikely(!pmd_trans_huge(*pmd))) {
  2104. spin_unlock(&mm->page_table_lock);
  2105. return;
  2106. }
  2107. page = pmd_page(*pmd);
  2108. VM_BUG_ON(!page_count(page));
  2109. get_page(page);
  2110. spin_unlock(&mm->page_table_lock);
  2111. split_huge_page(page);
  2112. put_page(page);
  2113. BUG_ON(pmd_trans_huge(*pmd));
  2114. }
  2115. static void split_huge_page_address(struct mm_struct *mm,
  2116. unsigned long address)
  2117. {
  2118. pgd_t *pgd;
  2119. pud_t *pud;
  2120. pmd_t *pmd;
  2121. VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
  2122. pgd = pgd_offset(mm, address);
  2123. if (!pgd_present(*pgd))
  2124. return;
  2125. pud = pud_offset(pgd, address);
  2126. if (!pud_present(*pud))
  2127. return;
  2128. pmd = pmd_offset(pud, address);
  2129. if (!pmd_present(*pmd))
  2130. return;
  2131. /*
  2132. * Caller holds the mmap_sem write mode, so a huge pmd cannot
  2133. * materialize from under us.
  2134. */
  2135. split_huge_page_pmd(mm, pmd);
  2136. }
  2137. void __vma_adjust_trans_huge(struct vm_area_struct *vma,
  2138. unsigned long start,
  2139. unsigned long end,
  2140. long adjust_next)
  2141. {
  2142. /*
  2143. * If the new start address isn't hpage aligned and it could
  2144. * previously contain an hugepage: check if we need to split
  2145. * an huge pmd.
  2146. */
  2147. if (start & ~HPAGE_PMD_MASK &&
  2148. (start & HPAGE_PMD_MASK) >= vma->vm_start &&
  2149. (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2150. split_huge_page_address(vma->vm_mm, start);
  2151. /*
  2152. * If the new end address isn't hpage aligned and it could
  2153. * previously contain an hugepage: check if we need to split
  2154. * an huge pmd.
  2155. */
  2156. if (end & ~HPAGE_PMD_MASK &&
  2157. (end & HPAGE_PMD_MASK) >= vma->vm_start &&
  2158. (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2159. split_huge_page_address(vma->vm_mm, end);
  2160. /*
  2161. * If we're also updating the vma->vm_next->vm_start, if the new
  2162. * vm_next->vm_start isn't page aligned and it could previously
  2163. * contain an hugepage: check if we need to split an huge pmd.
  2164. */
  2165. if (adjust_next > 0) {
  2166. struct vm_area_struct *next = vma->vm_next;
  2167. unsigned long nstart = next->vm_start;
  2168. nstart += adjust_next << PAGE_SHIFT;
  2169. if (nstart & ~HPAGE_PMD_MASK &&
  2170. (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
  2171. (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
  2172. split_huge_page_address(next->vm_mm, nstart);
  2173. }
  2174. }