huge_memory.c 63 KB

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