huge_memory.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. } 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. #endif /* CONFIG_SYSFS */
  433. static int __init hugepage_init(void)
  434. {
  435. int err;
  436. #ifdef CONFIG_SYSFS
  437. static struct kobject *hugepage_kobj;
  438. #endif
  439. err = -EINVAL;
  440. if (!has_transparent_hugepage()) {
  441. transparent_hugepage_flags = 0;
  442. goto out;
  443. }
  444. #ifdef CONFIG_SYSFS
  445. err = -ENOMEM;
  446. hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
  447. if (unlikely(!hugepage_kobj)) {
  448. printk(KERN_ERR "hugepage: failed kobject create\n");
  449. goto out;
  450. }
  451. err = sysfs_create_group(hugepage_kobj, &hugepage_attr_group);
  452. if (err) {
  453. printk(KERN_ERR "hugepage: failed register hugeage group\n");
  454. goto out;
  455. }
  456. err = sysfs_create_group(hugepage_kobj, &khugepaged_attr_group);
  457. if (err) {
  458. printk(KERN_ERR "hugepage: failed register hugeage group\n");
  459. goto out;
  460. }
  461. #endif
  462. err = khugepaged_slab_init();
  463. if (err)
  464. goto out;
  465. err = mm_slots_hash_init();
  466. if (err) {
  467. khugepaged_slab_free();
  468. goto out;
  469. }
  470. /*
  471. * By default disable transparent hugepages on smaller systems,
  472. * where the extra memory used could hurt more than TLB overhead
  473. * is likely to save. The admin can still enable it through /sys.
  474. */
  475. if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
  476. transparent_hugepage_flags = 0;
  477. start_khugepaged();
  478. set_recommended_min_free_kbytes();
  479. out:
  480. return err;
  481. }
  482. module_init(hugepage_init)
  483. static int __init setup_transparent_hugepage(char *str)
  484. {
  485. int ret = 0;
  486. if (!str)
  487. goto out;
  488. if (!strcmp(str, "always")) {
  489. set_bit(TRANSPARENT_HUGEPAGE_FLAG,
  490. &transparent_hugepage_flags);
  491. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  492. &transparent_hugepage_flags);
  493. ret = 1;
  494. } else if (!strcmp(str, "madvise")) {
  495. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  496. &transparent_hugepage_flags);
  497. set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  498. &transparent_hugepage_flags);
  499. ret = 1;
  500. } else if (!strcmp(str, "never")) {
  501. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  502. &transparent_hugepage_flags);
  503. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  504. &transparent_hugepage_flags);
  505. ret = 1;
  506. }
  507. out:
  508. if (!ret)
  509. printk(KERN_WARNING
  510. "transparent_hugepage= cannot parse, ignored\n");
  511. return ret;
  512. }
  513. __setup("transparent_hugepage=", setup_transparent_hugepage);
  514. static void prepare_pmd_huge_pte(pgtable_t pgtable,
  515. struct mm_struct *mm)
  516. {
  517. assert_spin_locked(&mm->page_table_lock);
  518. /* FIFO */
  519. if (!mm->pmd_huge_pte)
  520. INIT_LIST_HEAD(&pgtable->lru);
  521. else
  522. list_add(&pgtable->lru, &mm->pmd_huge_pte->lru);
  523. mm->pmd_huge_pte = pgtable;
  524. }
  525. static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
  526. {
  527. if (likely(vma->vm_flags & VM_WRITE))
  528. pmd = pmd_mkwrite(pmd);
  529. return pmd;
  530. }
  531. static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
  532. struct vm_area_struct *vma,
  533. unsigned long haddr, pmd_t *pmd,
  534. struct page *page)
  535. {
  536. int ret = 0;
  537. pgtable_t pgtable;
  538. VM_BUG_ON(!PageCompound(page));
  539. pgtable = pte_alloc_one(mm, haddr);
  540. if (unlikely(!pgtable)) {
  541. mem_cgroup_uncharge_page(page);
  542. put_page(page);
  543. return VM_FAULT_OOM;
  544. }
  545. clear_huge_page(page, haddr, HPAGE_PMD_NR);
  546. __SetPageUptodate(page);
  547. spin_lock(&mm->page_table_lock);
  548. if (unlikely(!pmd_none(*pmd))) {
  549. spin_unlock(&mm->page_table_lock);
  550. mem_cgroup_uncharge_page(page);
  551. put_page(page);
  552. pte_free(mm, pgtable);
  553. } else {
  554. pmd_t entry;
  555. entry = mk_pmd(page, vma->vm_page_prot);
  556. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  557. entry = pmd_mkhuge(entry);
  558. /*
  559. * The spinlocking to take the lru_lock inside
  560. * page_add_new_anon_rmap() acts as a full memory
  561. * barrier to be sure clear_huge_page writes become
  562. * visible after the set_pmd_at() write.
  563. */
  564. page_add_new_anon_rmap(page, vma, haddr);
  565. set_pmd_at(mm, haddr, pmd, entry);
  566. prepare_pmd_huge_pte(pgtable, mm);
  567. add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
  568. mm->nr_ptes++;
  569. spin_unlock(&mm->page_table_lock);
  570. }
  571. return ret;
  572. }
  573. static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
  574. {
  575. return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
  576. }
  577. static inline struct page *alloc_hugepage_vma(int defrag,
  578. struct vm_area_struct *vma,
  579. unsigned long haddr, int nd,
  580. gfp_t extra_gfp)
  581. {
  582. return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
  583. HPAGE_PMD_ORDER, vma, haddr, nd);
  584. }
  585. #ifndef CONFIG_NUMA
  586. static inline struct page *alloc_hugepage(int defrag)
  587. {
  588. return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
  589. HPAGE_PMD_ORDER);
  590. }
  591. #endif
  592. int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  593. unsigned long address, pmd_t *pmd,
  594. unsigned int flags)
  595. {
  596. struct page *page;
  597. unsigned long haddr = address & HPAGE_PMD_MASK;
  598. pte_t *pte;
  599. if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
  600. if (unlikely(anon_vma_prepare(vma)))
  601. return VM_FAULT_OOM;
  602. if (unlikely(khugepaged_enter(vma)))
  603. return VM_FAULT_OOM;
  604. page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
  605. vma, haddr, numa_node_id(), 0);
  606. if (unlikely(!page)) {
  607. count_vm_event(THP_FAULT_FALLBACK);
  608. goto out;
  609. }
  610. count_vm_event(THP_FAULT_ALLOC);
  611. if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
  612. put_page(page);
  613. goto out;
  614. }
  615. return __do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page);
  616. }
  617. out:
  618. /*
  619. * Use __pte_alloc instead of pte_alloc_map, because we can't
  620. * run pte_offset_map on the pmd, if an huge pmd could
  621. * materialize from under us from a different thread.
  622. */
  623. if (unlikely(__pte_alloc(mm, vma, pmd, address)))
  624. return VM_FAULT_OOM;
  625. /* if an huge pmd materialized from under us just retry later */
  626. if (unlikely(pmd_trans_huge(*pmd)))
  627. return 0;
  628. /*
  629. * A regular pmd is established and it can't morph into a huge pmd
  630. * from under us anymore at this point because we hold the mmap_sem
  631. * read mode and khugepaged takes it in write mode. So now it's
  632. * safe to run pte_offset_map().
  633. */
  634. pte = pte_offset_map(pmd, address);
  635. return handle_pte_fault(mm, vma, address, pte, pmd, flags);
  636. }
  637. int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  638. pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
  639. struct vm_area_struct *vma)
  640. {
  641. struct page *src_page;
  642. pmd_t pmd;
  643. pgtable_t pgtable;
  644. int ret;
  645. ret = -ENOMEM;
  646. pgtable = pte_alloc_one(dst_mm, addr);
  647. if (unlikely(!pgtable))
  648. goto out;
  649. spin_lock(&dst_mm->page_table_lock);
  650. spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
  651. ret = -EAGAIN;
  652. pmd = *src_pmd;
  653. if (unlikely(!pmd_trans_huge(pmd))) {
  654. pte_free(dst_mm, pgtable);
  655. goto out_unlock;
  656. }
  657. if (unlikely(pmd_trans_splitting(pmd))) {
  658. /* split huge page running from under us */
  659. spin_unlock(&src_mm->page_table_lock);
  660. spin_unlock(&dst_mm->page_table_lock);
  661. pte_free(dst_mm, pgtable);
  662. wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
  663. goto out;
  664. }
  665. src_page = pmd_page(pmd);
  666. VM_BUG_ON(!PageHead(src_page));
  667. get_page(src_page);
  668. page_dup_rmap(src_page);
  669. add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  670. pmdp_set_wrprotect(src_mm, addr, src_pmd);
  671. pmd = pmd_mkold(pmd_wrprotect(pmd));
  672. set_pmd_at(dst_mm, addr, dst_pmd, pmd);
  673. prepare_pmd_huge_pte(pgtable, dst_mm);
  674. dst_mm->nr_ptes++;
  675. ret = 0;
  676. out_unlock:
  677. spin_unlock(&src_mm->page_table_lock);
  678. spin_unlock(&dst_mm->page_table_lock);
  679. out:
  680. return ret;
  681. }
  682. /* no "address" argument so destroys page coloring of some arch */
  683. pgtable_t get_pmd_huge_pte(struct mm_struct *mm)
  684. {
  685. pgtable_t pgtable;
  686. assert_spin_locked(&mm->page_table_lock);
  687. /* FIFO */
  688. pgtable = mm->pmd_huge_pte;
  689. if (list_empty(&pgtable->lru))
  690. mm->pmd_huge_pte = NULL;
  691. else {
  692. mm->pmd_huge_pte = list_entry(pgtable->lru.next,
  693. struct page, lru);
  694. list_del(&pgtable->lru);
  695. }
  696. return pgtable;
  697. }
  698. static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
  699. struct vm_area_struct *vma,
  700. unsigned long address,
  701. pmd_t *pmd, pmd_t orig_pmd,
  702. struct page *page,
  703. unsigned long haddr)
  704. {
  705. pgtable_t pgtable;
  706. pmd_t _pmd;
  707. int ret = 0, i;
  708. struct page **pages;
  709. pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
  710. GFP_KERNEL);
  711. if (unlikely(!pages)) {
  712. ret |= VM_FAULT_OOM;
  713. goto out;
  714. }
  715. for (i = 0; i < HPAGE_PMD_NR; i++) {
  716. pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
  717. __GFP_OTHER_NODE,
  718. vma, address, page_to_nid(page));
  719. if (unlikely(!pages[i] ||
  720. mem_cgroup_newpage_charge(pages[i], mm,
  721. GFP_KERNEL))) {
  722. if (pages[i])
  723. put_page(pages[i]);
  724. mem_cgroup_uncharge_start();
  725. while (--i >= 0) {
  726. mem_cgroup_uncharge_page(pages[i]);
  727. put_page(pages[i]);
  728. }
  729. mem_cgroup_uncharge_end();
  730. kfree(pages);
  731. ret |= VM_FAULT_OOM;
  732. goto out;
  733. }
  734. }
  735. for (i = 0; i < HPAGE_PMD_NR; i++) {
  736. copy_user_highpage(pages[i], page + i,
  737. haddr + PAGE_SHIFT*i, vma);
  738. __SetPageUptodate(pages[i]);
  739. cond_resched();
  740. }
  741. spin_lock(&mm->page_table_lock);
  742. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  743. goto out_free_pages;
  744. VM_BUG_ON(!PageHead(page));
  745. pmdp_clear_flush_notify(vma, haddr, pmd);
  746. /* leave pmd empty until pte is filled */
  747. pgtable = get_pmd_huge_pte(mm);
  748. pmd_populate(mm, &_pmd, pgtable);
  749. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  750. pte_t *pte, entry;
  751. entry = mk_pte(pages[i], vma->vm_page_prot);
  752. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  753. page_add_new_anon_rmap(pages[i], vma, haddr);
  754. pte = pte_offset_map(&_pmd, haddr);
  755. VM_BUG_ON(!pte_none(*pte));
  756. set_pte_at(mm, haddr, pte, entry);
  757. pte_unmap(pte);
  758. }
  759. kfree(pages);
  760. smp_wmb(); /* make pte visible before pmd */
  761. pmd_populate(mm, pmd, pgtable);
  762. page_remove_rmap(page);
  763. spin_unlock(&mm->page_table_lock);
  764. ret |= VM_FAULT_WRITE;
  765. put_page(page);
  766. out:
  767. return ret;
  768. out_free_pages:
  769. spin_unlock(&mm->page_table_lock);
  770. mem_cgroup_uncharge_start();
  771. for (i = 0; i < HPAGE_PMD_NR; i++) {
  772. mem_cgroup_uncharge_page(pages[i]);
  773. put_page(pages[i]);
  774. }
  775. mem_cgroup_uncharge_end();
  776. kfree(pages);
  777. goto out;
  778. }
  779. int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  780. unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
  781. {
  782. int ret = 0;
  783. struct page *page, *new_page;
  784. unsigned long haddr;
  785. VM_BUG_ON(!vma->anon_vma);
  786. spin_lock(&mm->page_table_lock);
  787. if (unlikely(!pmd_same(*pmd, orig_pmd)))
  788. goto out_unlock;
  789. page = pmd_page(orig_pmd);
  790. VM_BUG_ON(!PageCompound(page) || !PageHead(page));
  791. haddr = address & HPAGE_PMD_MASK;
  792. if (page_mapcount(page) == 1) {
  793. pmd_t entry;
  794. entry = pmd_mkyoung(orig_pmd);
  795. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  796. if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
  797. update_mmu_cache(vma, address, entry);
  798. ret |= VM_FAULT_WRITE;
  799. goto out_unlock;
  800. }
  801. get_page(page);
  802. spin_unlock(&mm->page_table_lock);
  803. if (transparent_hugepage_enabled(vma) &&
  804. !transparent_hugepage_debug_cow())
  805. new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
  806. vma, haddr, numa_node_id(), 0);
  807. else
  808. new_page = NULL;
  809. if (unlikely(!new_page)) {
  810. count_vm_event(THP_FAULT_FALLBACK);
  811. ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
  812. pmd, orig_pmd, page, haddr);
  813. put_page(page);
  814. goto out;
  815. }
  816. count_vm_event(THP_FAULT_ALLOC);
  817. if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
  818. put_page(new_page);
  819. put_page(page);
  820. ret |= VM_FAULT_OOM;
  821. goto out;
  822. }
  823. copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
  824. __SetPageUptodate(new_page);
  825. spin_lock(&mm->page_table_lock);
  826. put_page(page);
  827. if (unlikely(!pmd_same(*pmd, orig_pmd))) {
  828. mem_cgroup_uncharge_page(new_page);
  829. put_page(new_page);
  830. } else {
  831. pmd_t entry;
  832. VM_BUG_ON(!PageHead(page));
  833. entry = mk_pmd(new_page, vma->vm_page_prot);
  834. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  835. entry = pmd_mkhuge(entry);
  836. pmdp_clear_flush_notify(vma, haddr, pmd);
  837. page_add_new_anon_rmap(new_page, vma, haddr);
  838. set_pmd_at(mm, haddr, pmd, entry);
  839. update_mmu_cache(vma, address, entry);
  840. page_remove_rmap(page);
  841. put_page(page);
  842. ret |= VM_FAULT_WRITE;
  843. }
  844. out_unlock:
  845. spin_unlock(&mm->page_table_lock);
  846. out:
  847. return ret;
  848. }
  849. struct page *follow_trans_huge_pmd(struct mm_struct *mm,
  850. unsigned long addr,
  851. pmd_t *pmd,
  852. unsigned int flags)
  853. {
  854. struct page *page = NULL;
  855. assert_spin_locked(&mm->page_table_lock);
  856. if (flags & FOLL_WRITE && !pmd_write(*pmd))
  857. goto out;
  858. page = pmd_page(*pmd);
  859. VM_BUG_ON(!PageHead(page));
  860. if (flags & FOLL_TOUCH) {
  861. pmd_t _pmd;
  862. /*
  863. * We should set the dirty bit only for FOLL_WRITE but
  864. * for now the dirty bit in the pmd is meaningless.
  865. * And if the dirty bit will become meaningful and
  866. * we'll only set it with FOLL_WRITE, an atomic
  867. * set_bit will be required on the pmd to set the
  868. * young bit, instead of the current set_pmd_at.
  869. */
  870. _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
  871. set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
  872. }
  873. page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
  874. VM_BUG_ON(!PageCompound(page));
  875. if (flags & FOLL_GET)
  876. get_page_foll(page);
  877. out:
  878. return page;
  879. }
  880. int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  881. pmd_t *pmd)
  882. {
  883. int ret = 0;
  884. spin_lock(&tlb->mm->page_table_lock);
  885. if (likely(pmd_trans_huge(*pmd))) {
  886. if (unlikely(pmd_trans_splitting(*pmd))) {
  887. spin_unlock(&tlb->mm->page_table_lock);
  888. wait_split_huge_page(vma->anon_vma,
  889. pmd);
  890. } else {
  891. struct page *page;
  892. pgtable_t pgtable;
  893. pgtable = get_pmd_huge_pte(tlb->mm);
  894. page = pmd_page(*pmd);
  895. pmd_clear(pmd);
  896. page_remove_rmap(page);
  897. VM_BUG_ON(page_mapcount(page) < 0);
  898. add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
  899. VM_BUG_ON(!PageHead(page));
  900. tlb->mm->nr_ptes--;
  901. spin_unlock(&tlb->mm->page_table_lock);
  902. tlb_remove_page(tlb, page);
  903. pte_free(tlb->mm, pgtable);
  904. ret = 1;
  905. }
  906. } else
  907. spin_unlock(&tlb->mm->page_table_lock);
  908. return ret;
  909. }
  910. int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  911. unsigned long addr, unsigned long end,
  912. unsigned char *vec)
  913. {
  914. int ret = 0;
  915. spin_lock(&vma->vm_mm->page_table_lock);
  916. if (likely(pmd_trans_huge(*pmd))) {
  917. ret = !pmd_trans_splitting(*pmd);
  918. spin_unlock(&vma->vm_mm->page_table_lock);
  919. if (unlikely(!ret))
  920. wait_split_huge_page(vma->anon_vma, pmd);
  921. else {
  922. /*
  923. * All logical pages in the range are present
  924. * if backed by a huge page.
  925. */
  926. memset(vec, 1, (end - addr) >> PAGE_SHIFT);
  927. }
  928. } else
  929. spin_unlock(&vma->vm_mm->page_table_lock);
  930. return ret;
  931. }
  932. int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  933. unsigned long addr, pgprot_t newprot)
  934. {
  935. struct mm_struct *mm = vma->vm_mm;
  936. int ret = 0;
  937. spin_lock(&mm->page_table_lock);
  938. if (likely(pmd_trans_huge(*pmd))) {
  939. if (unlikely(pmd_trans_splitting(*pmd))) {
  940. spin_unlock(&mm->page_table_lock);
  941. wait_split_huge_page(vma->anon_vma, pmd);
  942. } else {
  943. pmd_t entry;
  944. entry = pmdp_get_and_clear(mm, addr, pmd);
  945. entry = pmd_modify(entry, newprot);
  946. set_pmd_at(mm, addr, pmd, entry);
  947. spin_unlock(&vma->vm_mm->page_table_lock);
  948. flush_tlb_range(vma, addr, addr + HPAGE_PMD_SIZE);
  949. ret = 1;
  950. }
  951. } else
  952. spin_unlock(&vma->vm_mm->page_table_lock);
  953. return ret;
  954. }
  955. pmd_t *page_check_address_pmd(struct page *page,
  956. struct mm_struct *mm,
  957. unsigned long address,
  958. enum page_check_address_pmd_flag flag)
  959. {
  960. pgd_t *pgd;
  961. pud_t *pud;
  962. pmd_t *pmd, *ret = NULL;
  963. if (address & ~HPAGE_PMD_MASK)
  964. goto out;
  965. pgd = pgd_offset(mm, address);
  966. if (!pgd_present(*pgd))
  967. goto out;
  968. pud = pud_offset(pgd, address);
  969. if (!pud_present(*pud))
  970. goto out;
  971. pmd = pmd_offset(pud, address);
  972. if (pmd_none(*pmd))
  973. goto out;
  974. if (pmd_page(*pmd) != page)
  975. goto out;
  976. /*
  977. * split_vma() may create temporary aliased mappings. There is
  978. * no risk as long as all huge pmd are found and have their
  979. * splitting bit set before __split_huge_page_refcount
  980. * runs. Finding the same huge pmd more than once during the
  981. * same rmap walk is not a problem.
  982. */
  983. if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
  984. pmd_trans_splitting(*pmd))
  985. goto out;
  986. if (pmd_trans_huge(*pmd)) {
  987. VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
  988. !pmd_trans_splitting(*pmd));
  989. ret = pmd;
  990. }
  991. out:
  992. return ret;
  993. }
  994. static int __split_huge_page_splitting(struct page *page,
  995. struct vm_area_struct *vma,
  996. unsigned long address)
  997. {
  998. struct mm_struct *mm = vma->vm_mm;
  999. pmd_t *pmd;
  1000. int ret = 0;
  1001. spin_lock(&mm->page_table_lock);
  1002. pmd = page_check_address_pmd(page, mm, address,
  1003. PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
  1004. if (pmd) {
  1005. /*
  1006. * We can't temporarily set the pmd to null in order
  1007. * to split it, the pmd must remain marked huge at all
  1008. * times or the VM won't take the pmd_trans_huge paths
  1009. * and it won't wait on the anon_vma->root->mutex to
  1010. * serialize against split_huge_page*.
  1011. */
  1012. pmdp_splitting_flush_notify(vma, address, pmd);
  1013. ret = 1;
  1014. }
  1015. spin_unlock(&mm->page_table_lock);
  1016. return ret;
  1017. }
  1018. static void __split_huge_page_refcount(struct page *page)
  1019. {
  1020. int i;
  1021. unsigned long head_index = page->index;
  1022. struct zone *zone = page_zone(page);
  1023. int zonestat;
  1024. int tail_count = 0;
  1025. /* prevent PageLRU to go away from under us, and freeze lru stats */
  1026. spin_lock_irq(&zone->lru_lock);
  1027. compound_lock(page);
  1028. for (i = 1; i < HPAGE_PMD_NR; i++) {
  1029. struct page *page_tail = page + i;
  1030. /* tail_page->_mapcount cannot change */
  1031. BUG_ON(page_mapcount(page_tail) < 0);
  1032. tail_count += page_mapcount(page_tail);
  1033. /* check for overflow */
  1034. BUG_ON(tail_count < 0);
  1035. BUG_ON(atomic_read(&page_tail->_count) != 0);
  1036. /*
  1037. * tail_page->_count is zero and not changing from
  1038. * under us. But get_page_unless_zero() may be running
  1039. * from under us on the tail_page. If we used
  1040. * atomic_set() below instead of atomic_add(), we
  1041. * would then run atomic_set() concurrently with
  1042. * get_page_unless_zero(), and atomic_set() is
  1043. * implemented in C not using locked ops. spin_unlock
  1044. * on x86 sometime uses locked ops because of PPro
  1045. * errata 66, 92, so unless somebody can guarantee
  1046. * atomic_set() here would be safe on all archs (and
  1047. * not only on x86), it's safer to use atomic_add().
  1048. */
  1049. atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
  1050. &page_tail->_count);
  1051. /* after clearing PageTail the gup refcount can be released */
  1052. smp_mb();
  1053. /*
  1054. * retain hwpoison flag of the poisoned tail page:
  1055. * fix for the unsuitable process killed on Guest Machine(KVM)
  1056. * by the memory-failure.
  1057. */
  1058. page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
  1059. page_tail->flags |= (page->flags &
  1060. ((1L << PG_referenced) |
  1061. (1L << PG_swapbacked) |
  1062. (1L << PG_mlocked) |
  1063. (1L << PG_uptodate)));
  1064. page_tail->flags |= (1L << PG_dirty);
  1065. /* clear PageTail before overwriting first_page */
  1066. smp_wmb();
  1067. /*
  1068. * __split_huge_page_splitting() already set the
  1069. * splitting bit in all pmd that could map this
  1070. * hugepage, that will ensure no CPU can alter the
  1071. * mapcount on the head page. The mapcount is only
  1072. * accounted in the head page and it has to be
  1073. * transferred to all tail pages in the below code. So
  1074. * for this code to be safe, the split the mapcount
  1075. * can't change. But that doesn't mean userland can't
  1076. * keep changing and reading the page contents while
  1077. * we transfer the mapcount, so the pmd splitting
  1078. * status is achieved setting a reserved bit in the
  1079. * pmd, not by clearing the present bit.
  1080. */
  1081. page_tail->_mapcount = page->_mapcount;
  1082. BUG_ON(page_tail->mapping);
  1083. page_tail->mapping = page->mapping;
  1084. page_tail->index = ++head_index;
  1085. BUG_ON(!PageAnon(page_tail));
  1086. BUG_ON(!PageUptodate(page_tail));
  1087. BUG_ON(!PageDirty(page_tail));
  1088. BUG_ON(!PageSwapBacked(page_tail));
  1089. mem_cgroup_split_huge_fixup(page, page_tail);
  1090. lru_add_page_tail(zone, page, page_tail);
  1091. }
  1092. atomic_sub(tail_count, &page->_count);
  1093. BUG_ON(atomic_read(&page->_count) <= 0);
  1094. __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
  1095. __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
  1096. /*
  1097. * A hugepage counts for HPAGE_PMD_NR pages on the LRU statistics,
  1098. * so adjust those appropriately if this page is on the LRU.
  1099. */
  1100. if (PageLRU(page)) {
  1101. zonestat = NR_LRU_BASE + page_lru(page);
  1102. __mod_zone_page_state(zone, zonestat, -(HPAGE_PMD_NR-1));
  1103. }
  1104. ClearPageCompound(page);
  1105. compound_unlock(page);
  1106. spin_unlock_irq(&zone->lru_lock);
  1107. for (i = 1; i < HPAGE_PMD_NR; i++) {
  1108. struct page *page_tail = page + i;
  1109. BUG_ON(page_count(page_tail) <= 0);
  1110. /*
  1111. * Tail pages may be freed if there wasn't any mapping
  1112. * like if add_to_swap() is running on a lru page that
  1113. * had its mapping zapped. And freeing these pages
  1114. * requires taking the lru_lock so we do the put_page
  1115. * of the tail pages after the split is complete.
  1116. */
  1117. put_page(page_tail);
  1118. }
  1119. /*
  1120. * Only the head page (now become a regular page) is required
  1121. * to be pinned by the caller.
  1122. */
  1123. BUG_ON(page_count(page) <= 0);
  1124. }
  1125. static int __split_huge_page_map(struct page *page,
  1126. struct vm_area_struct *vma,
  1127. unsigned long address)
  1128. {
  1129. struct mm_struct *mm = vma->vm_mm;
  1130. pmd_t *pmd, _pmd;
  1131. int ret = 0, i;
  1132. pgtable_t pgtable;
  1133. unsigned long haddr;
  1134. spin_lock(&mm->page_table_lock);
  1135. pmd = page_check_address_pmd(page, mm, address,
  1136. PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
  1137. if (pmd) {
  1138. pgtable = get_pmd_huge_pte(mm);
  1139. pmd_populate(mm, &_pmd, pgtable);
  1140. for (i = 0, haddr = address; i < HPAGE_PMD_NR;
  1141. i++, haddr += PAGE_SIZE) {
  1142. pte_t *pte, entry;
  1143. BUG_ON(PageCompound(page+i));
  1144. entry = mk_pte(page + i, vma->vm_page_prot);
  1145. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1146. if (!pmd_write(*pmd))
  1147. entry = pte_wrprotect(entry);
  1148. else
  1149. BUG_ON(page_mapcount(page) != 1);
  1150. if (!pmd_young(*pmd))
  1151. entry = pte_mkold(entry);
  1152. pte = pte_offset_map(&_pmd, haddr);
  1153. BUG_ON(!pte_none(*pte));
  1154. set_pte_at(mm, haddr, pte, entry);
  1155. pte_unmap(pte);
  1156. }
  1157. smp_wmb(); /* make pte visible before pmd */
  1158. /*
  1159. * Up to this point the pmd is present and huge and
  1160. * userland has the whole access to the hugepage
  1161. * during the split (which happens in place). If we
  1162. * overwrite the pmd with the not-huge version
  1163. * pointing to the pte here (which of course we could
  1164. * if all CPUs were bug free), userland could trigger
  1165. * a small page size TLB miss on the small sized TLB
  1166. * while the hugepage TLB entry is still established
  1167. * in the huge TLB. Some CPU doesn't like that. See
  1168. * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
  1169. * Erratum 383 on page 93. Intel should be safe but is
  1170. * also warns that it's only safe if the permission
  1171. * and cache attributes of the two entries loaded in
  1172. * the two TLB is identical (which should be the case
  1173. * here). But it is generally safer to never allow
  1174. * small and huge TLB entries for the same virtual
  1175. * address to be loaded simultaneously. So instead of
  1176. * doing "pmd_populate(); flush_tlb_range();" we first
  1177. * mark the current pmd notpresent (atomically because
  1178. * here the pmd_trans_huge and pmd_trans_splitting
  1179. * must remain set at all times on the pmd until the
  1180. * split is complete for this pmd), then we flush the
  1181. * SMP TLB and finally we write the non-huge version
  1182. * of the pmd entry with pmd_populate.
  1183. */
  1184. set_pmd_at(mm, address, pmd, pmd_mknotpresent(*pmd));
  1185. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  1186. pmd_populate(mm, pmd, pgtable);
  1187. ret = 1;
  1188. }
  1189. spin_unlock(&mm->page_table_lock);
  1190. return ret;
  1191. }
  1192. /* must be called with anon_vma->root->mutex hold */
  1193. static void __split_huge_page(struct page *page,
  1194. struct anon_vma *anon_vma)
  1195. {
  1196. int mapcount, mapcount2;
  1197. struct anon_vma_chain *avc;
  1198. BUG_ON(!PageHead(page));
  1199. BUG_ON(PageTail(page));
  1200. mapcount = 0;
  1201. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  1202. struct vm_area_struct *vma = avc->vma;
  1203. unsigned long addr = vma_address(page, vma);
  1204. BUG_ON(is_vma_temporary_stack(vma));
  1205. if (addr == -EFAULT)
  1206. continue;
  1207. mapcount += __split_huge_page_splitting(page, vma, addr);
  1208. }
  1209. /*
  1210. * It is critical that new vmas are added to the tail of the
  1211. * anon_vma list. This guarantes that if copy_huge_pmd() runs
  1212. * and establishes a child pmd before
  1213. * __split_huge_page_splitting() freezes the parent pmd (so if
  1214. * we fail to prevent copy_huge_pmd() from running until the
  1215. * whole __split_huge_page() is complete), we will still see
  1216. * the newly established pmd of the child later during the
  1217. * walk, to be able to set it as pmd_trans_splitting too.
  1218. */
  1219. if (mapcount != page_mapcount(page))
  1220. printk(KERN_ERR "mapcount %d page_mapcount %d\n",
  1221. mapcount, page_mapcount(page));
  1222. BUG_ON(mapcount != page_mapcount(page));
  1223. __split_huge_page_refcount(page);
  1224. mapcount2 = 0;
  1225. list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
  1226. struct vm_area_struct *vma = avc->vma;
  1227. unsigned long addr = vma_address(page, vma);
  1228. BUG_ON(is_vma_temporary_stack(vma));
  1229. if (addr == -EFAULT)
  1230. continue;
  1231. mapcount2 += __split_huge_page_map(page, vma, addr);
  1232. }
  1233. if (mapcount != mapcount2)
  1234. printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
  1235. mapcount, mapcount2, page_mapcount(page));
  1236. BUG_ON(mapcount != mapcount2);
  1237. }
  1238. int split_huge_page(struct page *page)
  1239. {
  1240. struct anon_vma *anon_vma;
  1241. int ret = 1;
  1242. BUG_ON(!PageAnon(page));
  1243. anon_vma = page_lock_anon_vma(page);
  1244. if (!anon_vma)
  1245. goto out;
  1246. ret = 0;
  1247. if (!PageCompound(page))
  1248. goto out_unlock;
  1249. BUG_ON(!PageSwapBacked(page));
  1250. __split_huge_page(page, anon_vma);
  1251. count_vm_event(THP_SPLIT);
  1252. BUG_ON(PageCompound(page));
  1253. out_unlock:
  1254. page_unlock_anon_vma(anon_vma);
  1255. out:
  1256. return ret;
  1257. }
  1258. #define VM_NO_THP (VM_SPECIAL|VM_INSERTPAGE|VM_MIXEDMAP|VM_SAO| \
  1259. VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
  1260. int hugepage_madvise(struct vm_area_struct *vma,
  1261. unsigned long *vm_flags, int advice)
  1262. {
  1263. switch (advice) {
  1264. case MADV_HUGEPAGE:
  1265. /*
  1266. * Be somewhat over-protective like KSM for now!
  1267. */
  1268. if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
  1269. return -EINVAL;
  1270. *vm_flags &= ~VM_NOHUGEPAGE;
  1271. *vm_flags |= VM_HUGEPAGE;
  1272. /*
  1273. * If the vma become good for khugepaged to scan,
  1274. * register it here without waiting a page fault that
  1275. * may not happen any time soon.
  1276. */
  1277. if (unlikely(khugepaged_enter_vma_merge(vma)))
  1278. return -ENOMEM;
  1279. break;
  1280. case MADV_NOHUGEPAGE:
  1281. /*
  1282. * Be somewhat over-protective like KSM for now!
  1283. */
  1284. if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
  1285. return -EINVAL;
  1286. *vm_flags &= ~VM_HUGEPAGE;
  1287. *vm_flags |= VM_NOHUGEPAGE;
  1288. /*
  1289. * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
  1290. * this vma even if we leave the mm registered in khugepaged if
  1291. * it got registered before VM_NOHUGEPAGE was set.
  1292. */
  1293. break;
  1294. }
  1295. return 0;
  1296. }
  1297. static int __init khugepaged_slab_init(void)
  1298. {
  1299. mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
  1300. sizeof(struct mm_slot),
  1301. __alignof__(struct mm_slot), 0, NULL);
  1302. if (!mm_slot_cache)
  1303. return -ENOMEM;
  1304. return 0;
  1305. }
  1306. static void __init khugepaged_slab_free(void)
  1307. {
  1308. kmem_cache_destroy(mm_slot_cache);
  1309. mm_slot_cache = NULL;
  1310. }
  1311. static inline struct mm_slot *alloc_mm_slot(void)
  1312. {
  1313. if (!mm_slot_cache) /* initialization failed */
  1314. return NULL;
  1315. return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
  1316. }
  1317. static inline void free_mm_slot(struct mm_slot *mm_slot)
  1318. {
  1319. kmem_cache_free(mm_slot_cache, mm_slot);
  1320. }
  1321. static int __init mm_slots_hash_init(void)
  1322. {
  1323. mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
  1324. GFP_KERNEL);
  1325. if (!mm_slots_hash)
  1326. return -ENOMEM;
  1327. return 0;
  1328. }
  1329. #if 0
  1330. static void __init mm_slots_hash_free(void)
  1331. {
  1332. kfree(mm_slots_hash);
  1333. mm_slots_hash = NULL;
  1334. }
  1335. #endif
  1336. static struct mm_slot *get_mm_slot(struct mm_struct *mm)
  1337. {
  1338. struct mm_slot *mm_slot;
  1339. struct hlist_head *bucket;
  1340. struct hlist_node *node;
  1341. bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
  1342. % MM_SLOTS_HASH_HEADS];
  1343. hlist_for_each_entry(mm_slot, node, bucket, hash) {
  1344. if (mm == mm_slot->mm)
  1345. return mm_slot;
  1346. }
  1347. return NULL;
  1348. }
  1349. static void insert_to_mm_slots_hash(struct mm_struct *mm,
  1350. struct mm_slot *mm_slot)
  1351. {
  1352. struct hlist_head *bucket;
  1353. bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
  1354. % MM_SLOTS_HASH_HEADS];
  1355. mm_slot->mm = mm;
  1356. hlist_add_head(&mm_slot->hash, bucket);
  1357. }
  1358. static inline int khugepaged_test_exit(struct mm_struct *mm)
  1359. {
  1360. return atomic_read(&mm->mm_users) == 0;
  1361. }
  1362. int __khugepaged_enter(struct mm_struct *mm)
  1363. {
  1364. struct mm_slot *mm_slot;
  1365. int wakeup;
  1366. mm_slot = alloc_mm_slot();
  1367. if (!mm_slot)
  1368. return -ENOMEM;
  1369. /* __khugepaged_exit() must not run from under us */
  1370. VM_BUG_ON(khugepaged_test_exit(mm));
  1371. if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
  1372. free_mm_slot(mm_slot);
  1373. return 0;
  1374. }
  1375. spin_lock(&khugepaged_mm_lock);
  1376. insert_to_mm_slots_hash(mm, mm_slot);
  1377. /*
  1378. * Insert just behind the scanning cursor, to let the area settle
  1379. * down a little.
  1380. */
  1381. wakeup = list_empty(&khugepaged_scan.mm_head);
  1382. list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
  1383. spin_unlock(&khugepaged_mm_lock);
  1384. atomic_inc(&mm->mm_count);
  1385. if (wakeup)
  1386. wake_up_interruptible(&khugepaged_wait);
  1387. return 0;
  1388. }
  1389. int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
  1390. {
  1391. unsigned long hstart, hend;
  1392. if (!vma->anon_vma)
  1393. /*
  1394. * Not yet faulted in so we will register later in the
  1395. * page fault if needed.
  1396. */
  1397. return 0;
  1398. if (vma->vm_ops)
  1399. /* khugepaged not yet working on file or special mappings */
  1400. return 0;
  1401. /*
  1402. * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
  1403. * true too, verify it here.
  1404. */
  1405. VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
  1406. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1407. hend = vma->vm_end & HPAGE_PMD_MASK;
  1408. if (hstart < hend)
  1409. return khugepaged_enter(vma);
  1410. return 0;
  1411. }
  1412. void __khugepaged_exit(struct mm_struct *mm)
  1413. {
  1414. struct mm_slot *mm_slot;
  1415. int free = 0;
  1416. spin_lock(&khugepaged_mm_lock);
  1417. mm_slot = get_mm_slot(mm);
  1418. if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
  1419. hlist_del(&mm_slot->hash);
  1420. list_del(&mm_slot->mm_node);
  1421. free = 1;
  1422. }
  1423. if (free) {
  1424. spin_unlock(&khugepaged_mm_lock);
  1425. clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1426. free_mm_slot(mm_slot);
  1427. mmdrop(mm);
  1428. } else if (mm_slot) {
  1429. spin_unlock(&khugepaged_mm_lock);
  1430. /*
  1431. * This is required to serialize against
  1432. * khugepaged_test_exit() (which is guaranteed to run
  1433. * under mmap sem read mode). Stop here (after we
  1434. * return all pagetables will be destroyed) until
  1435. * khugepaged has finished working on the pagetables
  1436. * under the mmap_sem.
  1437. */
  1438. down_write(&mm->mmap_sem);
  1439. up_write(&mm->mmap_sem);
  1440. } else
  1441. spin_unlock(&khugepaged_mm_lock);
  1442. }
  1443. static void release_pte_page(struct page *page)
  1444. {
  1445. /* 0 stands for page_is_file_cache(page) == false */
  1446. dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
  1447. unlock_page(page);
  1448. putback_lru_page(page);
  1449. }
  1450. static void release_pte_pages(pte_t *pte, pte_t *_pte)
  1451. {
  1452. while (--_pte >= pte) {
  1453. pte_t pteval = *_pte;
  1454. if (!pte_none(pteval))
  1455. release_pte_page(pte_page(pteval));
  1456. }
  1457. }
  1458. static void release_all_pte_pages(pte_t *pte)
  1459. {
  1460. release_pte_pages(pte, pte + HPAGE_PMD_NR);
  1461. }
  1462. static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
  1463. unsigned long address,
  1464. pte_t *pte)
  1465. {
  1466. struct page *page;
  1467. pte_t *_pte;
  1468. int referenced = 0, isolated = 0, none = 0;
  1469. for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
  1470. _pte++, address += PAGE_SIZE) {
  1471. pte_t pteval = *_pte;
  1472. if (pte_none(pteval)) {
  1473. if (++none <= khugepaged_max_ptes_none)
  1474. continue;
  1475. else {
  1476. release_pte_pages(pte, _pte);
  1477. goto out;
  1478. }
  1479. }
  1480. if (!pte_present(pteval) || !pte_write(pteval)) {
  1481. release_pte_pages(pte, _pte);
  1482. goto out;
  1483. }
  1484. page = vm_normal_page(vma, address, pteval);
  1485. if (unlikely(!page)) {
  1486. release_pte_pages(pte, _pte);
  1487. goto out;
  1488. }
  1489. VM_BUG_ON(PageCompound(page));
  1490. BUG_ON(!PageAnon(page));
  1491. VM_BUG_ON(!PageSwapBacked(page));
  1492. /* cannot use mapcount: can't collapse if there's a gup pin */
  1493. if (page_count(page) != 1) {
  1494. release_pte_pages(pte, _pte);
  1495. goto out;
  1496. }
  1497. /*
  1498. * We can do it before isolate_lru_page because the
  1499. * page can't be freed from under us. NOTE: PG_lock
  1500. * is needed to serialize against split_huge_page
  1501. * when invoked from the VM.
  1502. */
  1503. if (!trylock_page(page)) {
  1504. release_pte_pages(pte, _pte);
  1505. goto out;
  1506. }
  1507. /*
  1508. * Isolate the page to avoid collapsing an hugepage
  1509. * currently in use by the VM.
  1510. */
  1511. if (isolate_lru_page(page)) {
  1512. unlock_page(page);
  1513. release_pte_pages(pte, _pte);
  1514. goto out;
  1515. }
  1516. /* 0 stands for page_is_file_cache(page) == false */
  1517. inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
  1518. VM_BUG_ON(!PageLocked(page));
  1519. VM_BUG_ON(PageLRU(page));
  1520. /* If there is no mapped pte young don't collapse the page */
  1521. if (pte_young(pteval) || PageReferenced(page) ||
  1522. mmu_notifier_test_young(vma->vm_mm, address))
  1523. referenced = 1;
  1524. }
  1525. if (unlikely(!referenced))
  1526. release_all_pte_pages(pte);
  1527. else
  1528. isolated = 1;
  1529. out:
  1530. return isolated;
  1531. }
  1532. static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
  1533. struct vm_area_struct *vma,
  1534. unsigned long address,
  1535. spinlock_t *ptl)
  1536. {
  1537. pte_t *_pte;
  1538. for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
  1539. pte_t pteval = *_pte;
  1540. struct page *src_page;
  1541. if (pte_none(pteval)) {
  1542. clear_user_highpage(page, address);
  1543. add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
  1544. } else {
  1545. src_page = pte_page(pteval);
  1546. copy_user_highpage(page, src_page, address, vma);
  1547. VM_BUG_ON(page_mapcount(src_page) != 1);
  1548. VM_BUG_ON(page_count(src_page) != 2);
  1549. release_pte_page(src_page);
  1550. /*
  1551. * ptl mostly unnecessary, but preempt has to
  1552. * be disabled to update the per-cpu stats
  1553. * inside page_remove_rmap().
  1554. */
  1555. spin_lock(ptl);
  1556. /*
  1557. * paravirt calls inside pte_clear here are
  1558. * superfluous.
  1559. */
  1560. pte_clear(vma->vm_mm, address, _pte);
  1561. page_remove_rmap(src_page);
  1562. spin_unlock(ptl);
  1563. free_page_and_swap_cache(src_page);
  1564. }
  1565. address += PAGE_SIZE;
  1566. page++;
  1567. }
  1568. }
  1569. static void collapse_huge_page(struct mm_struct *mm,
  1570. unsigned long address,
  1571. struct page **hpage,
  1572. struct vm_area_struct *vma,
  1573. int node)
  1574. {
  1575. pgd_t *pgd;
  1576. pud_t *pud;
  1577. pmd_t *pmd, _pmd;
  1578. pte_t *pte;
  1579. pgtable_t pgtable;
  1580. struct page *new_page;
  1581. spinlock_t *ptl;
  1582. int isolated;
  1583. unsigned long hstart, hend;
  1584. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1585. #ifndef CONFIG_NUMA
  1586. up_read(&mm->mmap_sem);
  1587. VM_BUG_ON(!*hpage);
  1588. new_page = *hpage;
  1589. #else
  1590. VM_BUG_ON(*hpage);
  1591. /*
  1592. * Allocate the page while the vma is still valid and under
  1593. * the mmap_sem read mode so there is no memory allocation
  1594. * later when we take the mmap_sem in write mode. This is more
  1595. * friendly behavior (OTOH it may actually hide bugs) to
  1596. * filesystems in userland with daemons allocating memory in
  1597. * the userland I/O paths. Allocating memory with the
  1598. * mmap_sem in read mode is good idea also to allow greater
  1599. * scalability.
  1600. */
  1601. new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
  1602. node, __GFP_OTHER_NODE);
  1603. /*
  1604. * After allocating the hugepage, release the mmap_sem read lock in
  1605. * preparation for taking it in write mode.
  1606. */
  1607. up_read(&mm->mmap_sem);
  1608. if (unlikely(!new_page)) {
  1609. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  1610. *hpage = ERR_PTR(-ENOMEM);
  1611. return;
  1612. }
  1613. #endif
  1614. count_vm_event(THP_COLLAPSE_ALLOC);
  1615. if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
  1616. #ifdef CONFIG_NUMA
  1617. put_page(new_page);
  1618. #endif
  1619. return;
  1620. }
  1621. /*
  1622. * Prevent all access to pagetables with the exception of
  1623. * gup_fast later hanlded by the ptep_clear_flush and the VM
  1624. * handled by the anon_vma lock + PG_lock.
  1625. */
  1626. down_write(&mm->mmap_sem);
  1627. if (unlikely(khugepaged_test_exit(mm)))
  1628. goto out;
  1629. vma = find_vma(mm, address);
  1630. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1631. hend = vma->vm_end & HPAGE_PMD_MASK;
  1632. if (address < hstart || address + HPAGE_PMD_SIZE > hend)
  1633. goto out;
  1634. if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
  1635. (vma->vm_flags & VM_NOHUGEPAGE))
  1636. goto out;
  1637. if (!vma->anon_vma || vma->vm_ops)
  1638. goto out;
  1639. if (is_vma_temporary_stack(vma))
  1640. goto out;
  1641. /*
  1642. * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
  1643. * true too, verify it here.
  1644. */
  1645. VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
  1646. pgd = pgd_offset(mm, address);
  1647. if (!pgd_present(*pgd))
  1648. goto out;
  1649. pud = pud_offset(pgd, address);
  1650. if (!pud_present(*pud))
  1651. goto out;
  1652. pmd = pmd_offset(pud, address);
  1653. /* pmd can't go away or become huge under us */
  1654. if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
  1655. goto out;
  1656. anon_vma_lock(vma->anon_vma);
  1657. pte = pte_offset_map(pmd, address);
  1658. ptl = pte_lockptr(mm, pmd);
  1659. spin_lock(&mm->page_table_lock); /* probably unnecessary */
  1660. /*
  1661. * After this gup_fast can't run anymore. This also removes
  1662. * any huge TLB entry from the CPU so we won't allow
  1663. * huge and small TLB entries for the same virtual address
  1664. * to avoid the risk of CPU bugs in that area.
  1665. */
  1666. _pmd = pmdp_clear_flush_notify(vma, address, pmd);
  1667. spin_unlock(&mm->page_table_lock);
  1668. spin_lock(ptl);
  1669. isolated = __collapse_huge_page_isolate(vma, address, pte);
  1670. spin_unlock(ptl);
  1671. if (unlikely(!isolated)) {
  1672. pte_unmap(pte);
  1673. spin_lock(&mm->page_table_lock);
  1674. BUG_ON(!pmd_none(*pmd));
  1675. set_pmd_at(mm, address, pmd, _pmd);
  1676. spin_unlock(&mm->page_table_lock);
  1677. anon_vma_unlock(vma->anon_vma);
  1678. goto out;
  1679. }
  1680. /*
  1681. * All pages are isolated and locked so anon_vma rmap
  1682. * can't run anymore.
  1683. */
  1684. anon_vma_unlock(vma->anon_vma);
  1685. __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
  1686. pte_unmap(pte);
  1687. __SetPageUptodate(new_page);
  1688. pgtable = pmd_pgtable(_pmd);
  1689. VM_BUG_ON(page_count(pgtable) != 1);
  1690. VM_BUG_ON(page_mapcount(pgtable) != 0);
  1691. _pmd = mk_pmd(new_page, vma->vm_page_prot);
  1692. _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
  1693. _pmd = pmd_mkhuge(_pmd);
  1694. /*
  1695. * spin_lock() below is not the equivalent of smp_wmb(), so
  1696. * this is needed to avoid the copy_huge_page writes to become
  1697. * visible after the set_pmd_at() write.
  1698. */
  1699. smp_wmb();
  1700. spin_lock(&mm->page_table_lock);
  1701. BUG_ON(!pmd_none(*pmd));
  1702. page_add_new_anon_rmap(new_page, vma, address);
  1703. set_pmd_at(mm, address, pmd, _pmd);
  1704. update_mmu_cache(vma, address, entry);
  1705. prepare_pmd_huge_pte(pgtable, mm);
  1706. spin_unlock(&mm->page_table_lock);
  1707. #ifndef CONFIG_NUMA
  1708. *hpage = NULL;
  1709. #endif
  1710. khugepaged_pages_collapsed++;
  1711. out_up_write:
  1712. up_write(&mm->mmap_sem);
  1713. return;
  1714. out:
  1715. mem_cgroup_uncharge_page(new_page);
  1716. #ifdef CONFIG_NUMA
  1717. put_page(new_page);
  1718. #endif
  1719. goto out_up_write;
  1720. }
  1721. static int khugepaged_scan_pmd(struct mm_struct *mm,
  1722. struct vm_area_struct *vma,
  1723. unsigned long address,
  1724. struct page **hpage)
  1725. {
  1726. pgd_t *pgd;
  1727. pud_t *pud;
  1728. pmd_t *pmd;
  1729. pte_t *pte, *_pte;
  1730. int ret = 0, referenced = 0, none = 0;
  1731. struct page *page;
  1732. unsigned long _address;
  1733. spinlock_t *ptl;
  1734. int node = -1;
  1735. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  1736. pgd = pgd_offset(mm, address);
  1737. if (!pgd_present(*pgd))
  1738. goto out;
  1739. pud = pud_offset(pgd, address);
  1740. if (!pud_present(*pud))
  1741. goto out;
  1742. pmd = pmd_offset(pud, address);
  1743. if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
  1744. goto out;
  1745. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  1746. for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
  1747. _pte++, _address += PAGE_SIZE) {
  1748. pte_t pteval = *_pte;
  1749. if (pte_none(pteval)) {
  1750. if (++none <= khugepaged_max_ptes_none)
  1751. continue;
  1752. else
  1753. goto out_unmap;
  1754. }
  1755. if (!pte_present(pteval) || !pte_write(pteval))
  1756. goto out_unmap;
  1757. page = vm_normal_page(vma, _address, pteval);
  1758. if (unlikely(!page))
  1759. goto out_unmap;
  1760. /*
  1761. * Chose the node of the first page. This could
  1762. * be more sophisticated and look at more pages,
  1763. * but isn't for now.
  1764. */
  1765. if (node == -1)
  1766. node = page_to_nid(page);
  1767. VM_BUG_ON(PageCompound(page));
  1768. if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
  1769. goto out_unmap;
  1770. /* cannot use mapcount: can't collapse if there's a gup pin */
  1771. if (page_count(page) != 1)
  1772. goto out_unmap;
  1773. if (pte_young(pteval) || PageReferenced(page) ||
  1774. mmu_notifier_test_young(vma->vm_mm, address))
  1775. referenced = 1;
  1776. }
  1777. if (referenced)
  1778. ret = 1;
  1779. out_unmap:
  1780. pte_unmap_unlock(pte, ptl);
  1781. if (ret)
  1782. /* collapse_huge_page will return with the mmap_sem released */
  1783. collapse_huge_page(mm, address, hpage, vma, node);
  1784. out:
  1785. return ret;
  1786. }
  1787. static void collect_mm_slot(struct mm_slot *mm_slot)
  1788. {
  1789. struct mm_struct *mm = mm_slot->mm;
  1790. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1791. if (khugepaged_test_exit(mm)) {
  1792. /* free mm_slot */
  1793. hlist_del(&mm_slot->hash);
  1794. list_del(&mm_slot->mm_node);
  1795. /*
  1796. * Not strictly needed because the mm exited already.
  1797. *
  1798. * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
  1799. */
  1800. /* khugepaged_mm_lock actually not necessary for the below */
  1801. free_mm_slot(mm_slot);
  1802. mmdrop(mm);
  1803. }
  1804. }
  1805. static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
  1806. struct page **hpage)
  1807. {
  1808. struct mm_slot *mm_slot;
  1809. struct mm_struct *mm;
  1810. struct vm_area_struct *vma;
  1811. int progress = 0;
  1812. VM_BUG_ON(!pages);
  1813. VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
  1814. if (khugepaged_scan.mm_slot)
  1815. mm_slot = khugepaged_scan.mm_slot;
  1816. else {
  1817. mm_slot = list_entry(khugepaged_scan.mm_head.next,
  1818. struct mm_slot, mm_node);
  1819. khugepaged_scan.address = 0;
  1820. khugepaged_scan.mm_slot = mm_slot;
  1821. }
  1822. spin_unlock(&khugepaged_mm_lock);
  1823. mm = mm_slot->mm;
  1824. down_read(&mm->mmap_sem);
  1825. if (unlikely(khugepaged_test_exit(mm)))
  1826. vma = NULL;
  1827. else
  1828. vma = find_vma(mm, khugepaged_scan.address);
  1829. progress++;
  1830. for (; vma; vma = vma->vm_next) {
  1831. unsigned long hstart, hend;
  1832. cond_resched();
  1833. if (unlikely(khugepaged_test_exit(mm))) {
  1834. progress++;
  1835. break;
  1836. }
  1837. if ((!(vma->vm_flags & VM_HUGEPAGE) &&
  1838. !khugepaged_always()) ||
  1839. (vma->vm_flags & VM_NOHUGEPAGE)) {
  1840. skip:
  1841. progress++;
  1842. continue;
  1843. }
  1844. if (!vma->anon_vma || vma->vm_ops)
  1845. goto skip;
  1846. if (is_vma_temporary_stack(vma))
  1847. goto skip;
  1848. /*
  1849. * If is_pfn_mapping() is true is_learn_pfn_mapping()
  1850. * must be true too, verify it here.
  1851. */
  1852. VM_BUG_ON(is_linear_pfn_mapping(vma) ||
  1853. vma->vm_flags & VM_NO_THP);
  1854. hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
  1855. hend = vma->vm_end & HPAGE_PMD_MASK;
  1856. if (hstart >= hend)
  1857. goto skip;
  1858. if (khugepaged_scan.address > hend)
  1859. goto skip;
  1860. if (khugepaged_scan.address < hstart)
  1861. khugepaged_scan.address = hstart;
  1862. VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
  1863. while (khugepaged_scan.address < hend) {
  1864. int ret;
  1865. cond_resched();
  1866. if (unlikely(khugepaged_test_exit(mm)))
  1867. goto breakouterloop;
  1868. VM_BUG_ON(khugepaged_scan.address < hstart ||
  1869. khugepaged_scan.address + HPAGE_PMD_SIZE >
  1870. hend);
  1871. ret = khugepaged_scan_pmd(mm, vma,
  1872. khugepaged_scan.address,
  1873. hpage);
  1874. /* move to next address */
  1875. khugepaged_scan.address += HPAGE_PMD_SIZE;
  1876. progress += HPAGE_PMD_NR;
  1877. if (ret)
  1878. /* we released mmap_sem so break loop */
  1879. goto breakouterloop_mmap_sem;
  1880. if (progress >= pages)
  1881. goto breakouterloop;
  1882. }
  1883. }
  1884. breakouterloop:
  1885. up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
  1886. breakouterloop_mmap_sem:
  1887. spin_lock(&khugepaged_mm_lock);
  1888. VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
  1889. /*
  1890. * Release the current mm_slot if this mm is about to die, or
  1891. * if we scanned all vmas of this mm.
  1892. */
  1893. if (khugepaged_test_exit(mm) || !vma) {
  1894. /*
  1895. * Make sure that if mm_users is reaching zero while
  1896. * khugepaged runs here, khugepaged_exit will find
  1897. * mm_slot not pointing to the exiting mm.
  1898. */
  1899. if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
  1900. khugepaged_scan.mm_slot = list_entry(
  1901. mm_slot->mm_node.next,
  1902. struct mm_slot, mm_node);
  1903. khugepaged_scan.address = 0;
  1904. } else {
  1905. khugepaged_scan.mm_slot = NULL;
  1906. khugepaged_full_scans++;
  1907. }
  1908. collect_mm_slot(mm_slot);
  1909. }
  1910. return progress;
  1911. }
  1912. static int khugepaged_has_work(void)
  1913. {
  1914. return !list_empty(&khugepaged_scan.mm_head) &&
  1915. khugepaged_enabled();
  1916. }
  1917. static int khugepaged_wait_event(void)
  1918. {
  1919. return !list_empty(&khugepaged_scan.mm_head) ||
  1920. !khugepaged_enabled();
  1921. }
  1922. static void khugepaged_do_scan(struct page **hpage)
  1923. {
  1924. unsigned int progress = 0, pass_through_head = 0;
  1925. unsigned int pages = khugepaged_pages_to_scan;
  1926. barrier(); /* write khugepaged_pages_to_scan to local stack */
  1927. while (progress < pages) {
  1928. cond_resched();
  1929. #ifndef CONFIG_NUMA
  1930. if (!*hpage) {
  1931. *hpage = alloc_hugepage(khugepaged_defrag());
  1932. if (unlikely(!*hpage)) {
  1933. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  1934. break;
  1935. }
  1936. count_vm_event(THP_COLLAPSE_ALLOC);
  1937. }
  1938. #else
  1939. if (IS_ERR(*hpage))
  1940. break;
  1941. #endif
  1942. if (unlikely(kthread_should_stop() || freezing(current)))
  1943. break;
  1944. spin_lock(&khugepaged_mm_lock);
  1945. if (!khugepaged_scan.mm_slot)
  1946. pass_through_head++;
  1947. if (khugepaged_has_work() &&
  1948. pass_through_head < 2)
  1949. progress += khugepaged_scan_mm_slot(pages - progress,
  1950. hpage);
  1951. else
  1952. progress = pages;
  1953. spin_unlock(&khugepaged_mm_lock);
  1954. }
  1955. }
  1956. static void khugepaged_alloc_sleep(void)
  1957. {
  1958. DEFINE_WAIT(wait);
  1959. add_wait_queue(&khugepaged_wait, &wait);
  1960. schedule_timeout_interruptible(
  1961. msecs_to_jiffies(
  1962. khugepaged_alloc_sleep_millisecs));
  1963. remove_wait_queue(&khugepaged_wait, &wait);
  1964. }
  1965. #ifndef CONFIG_NUMA
  1966. static struct page *khugepaged_alloc_hugepage(void)
  1967. {
  1968. struct page *hpage;
  1969. do {
  1970. hpage = alloc_hugepage(khugepaged_defrag());
  1971. if (!hpage) {
  1972. count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
  1973. khugepaged_alloc_sleep();
  1974. } else
  1975. count_vm_event(THP_COLLAPSE_ALLOC);
  1976. } while (unlikely(!hpage) &&
  1977. likely(khugepaged_enabled()));
  1978. return hpage;
  1979. }
  1980. #endif
  1981. static void khugepaged_loop(void)
  1982. {
  1983. struct page *hpage;
  1984. #ifdef CONFIG_NUMA
  1985. hpage = NULL;
  1986. #endif
  1987. while (likely(khugepaged_enabled())) {
  1988. #ifndef CONFIG_NUMA
  1989. hpage = khugepaged_alloc_hugepage();
  1990. if (unlikely(!hpage))
  1991. break;
  1992. #else
  1993. if (IS_ERR(hpage)) {
  1994. khugepaged_alloc_sleep();
  1995. hpage = NULL;
  1996. }
  1997. #endif
  1998. khugepaged_do_scan(&hpage);
  1999. #ifndef CONFIG_NUMA
  2000. if (hpage)
  2001. put_page(hpage);
  2002. #endif
  2003. try_to_freeze();
  2004. if (unlikely(kthread_should_stop()))
  2005. break;
  2006. if (khugepaged_has_work()) {
  2007. DEFINE_WAIT(wait);
  2008. if (!khugepaged_scan_sleep_millisecs)
  2009. continue;
  2010. add_wait_queue(&khugepaged_wait, &wait);
  2011. schedule_timeout_interruptible(
  2012. msecs_to_jiffies(
  2013. khugepaged_scan_sleep_millisecs));
  2014. remove_wait_queue(&khugepaged_wait, &wait);
  2015. } else if (khugepaged_enabled())
  2016. wait_event_freezable(khugepaged_wait,
  2017. khugepaged_wait_event());
  2018. }
  2019. }
  2020. static int khugepaged(void *none)
  2021. {
  2022. struct mm_slot *mm_slot;
  2023. set_freezable();
  2024. set_user_nice(current, 19);
  2025. /* serialize with start_khugepaged() */
  2026. mutex_lock(&khugepaged_mutex);
  2027. for (;;) {
  2028. mutex_unlock(&khugepaged_mutex);
  2029. VM_BUG_ON(khugepaged_thread != current);
  2030. khugepaged_loop();
  2031. VM_BUG_ON(khugepaged_thread != current);
  2032. mutex_lock(&khugepaged_mutex);
  2033. if (!khugepaged_enabled())
  2034. break;
  2035. if (unlikely(kthread_should_stop()))
  2036. break;
  2037. }
  2038. spin_lock(&khugepaged_mm_lock);
  2039. mm_slot = khugepaged_scan.mm_slot;
  2040. khugepaged_scan.mm_slot = NULL;
  2041. if (mm_slot)
  2042. collect_mm_slot(mm_slot);
  2043. spin_unlock(&khugepaged_mm_lock);
  2044. khugepaged_thread = NULL;
  2045. mutex_unlock(&khugepaged_mutex);
  2046. return 0;
  2047. }
  2048. void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
  2049. {
  2050. struct page *page;
  2051. spin_lock(&mm->page_table_lock);
  2052. if (unlikely(!pmd_trans_huge(*pmd))) {
  2053. spin_unlock(&mm->page_table_lock);
  2054. return;
  2055. }
  2056. page = pmd_page(*pmd);
  2057. VM_BUG_ON(!page_count(page));
  2058. get_page(page);
  2059. spin_unlock(&mm->page_table_lock);
  2060. split_huge_page(page);
  2061. put_page(page);
  2062. BUG_ON(pmd_trans_huge(*pmd));
  2063. }
  2064. static void split_huge_page_address(struct mm_struct *mm,
  2065. unsigned long address)
  2066. {
  2067. pgd_t *pgd;
  2068. pud_t *pud;
  2069. pmd_t *pmd;
  2070. VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
  2071. pgd = pgd_offset(mm, address);
  2072. if (!pgd_present(*pgd))
  2073. return;
  2074. pud = pud_offset(pgd, address);
  2075. if (!pud_present(*pud))
  2076. return;
  2077. pmd = pmd_offset(pud, address);
  2078. if (!pmd_present(*pmd))
  2079. return;
  2080. /*
  2081. * Caller holds the mmap_sem write mode, so a huge pmd cannot
  2082. * materialize from under us.
  2083. */
  2084. split_huge_page_pmd(mm, pmd);
  2085. }
  2086. void __vma_adjust_trans_huge(struct vm_area_struct *vma,
  2087. unsigned long start,
  2088. unsigned long end,
  2089. long adjust_next)
  2090. {
  2091. /*
  2092. * If the new start address isn't hpage aligned and it could
  2093. * previously contain an hugepage: check if we need to split
  2094. * an huge pmd.
  2095. */
  2096. if (start & ~HPAGE_PMD_MASK &&
  2097. (start & HPAGE_PMD_MASK) >= vma->vm_start &&
  2098. (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2099. split_huge_page_address(vma->vm_mm, start);
  2100. /*
  2101. * If the new end address isn't hpage aligned and it could
  2102. * previously contain an hugepage: check if we need to split
  2103. * an huge pmd.
  2104. */
  2105. if (end & ~HPAGE_PMD_MASK &&
  2106. (end & HPAGE_PMD_MASK) >= vma->vm_start &&
  2107. (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2108. split_huge_page_address(vma->vm_mm, end);
  2109. /*
  2110. * If we're also updating the vma->vm_next->vm_start, if the new
  2111. * vm_next->vm_start isn't page aligned and it could previously
  2112. * contain an hugepage: check if we need to split an huge pmd.
  2113. */
  2114. if (adjust_next > 0) {
  2115. struct vm_area_struct *next = vma->vm_next;
  2116. unsigned long nstart = next->vm_start;
  2117. nstart += adjust_next << PAGE_SHIFT;
  2118. if (nstart & ~HPAGE_PMD_MASK &&
  2119. (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
  2120. (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
  2121. split_huge_page_address(next->vm_mm, nstart);
  2122. }
  2123. }