huge_memory.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/mm.h>
  9. #include <linux/sched.h>
  10. #include <linux/highmem.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/rmap.h>
  14. #include <linux/swap.h>
  15. #include <linux/shrinker.h>
  16. #include <linux/mm_inline.h>
  17. #include <linux/swapops.h>
  18. #include <linux/dax.h>
  19. #include <linux/khugepaged.h>
  20. #include <linux/freezer.h>
  21. #include <linux/pfn_t.h>
  22. #include <linux/mman.h>
  23. #include <linux/memremap.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/migrate.h>
  27. #include <linux/hashtable.h>
  28. #include <linux/userfaultfd_k.h>
  29. #include <linux/page_idle.h>
  30. #include <linux/shmem_fs.h>
  31. #include <linux/page_owner.h>
  32. #include <asm/tlb.h>
  33. #include <asm/pgalloc.h>
  34. #include "internal.h"
  35. /*
  36. * By default transparent hugepage support is disabled in order that avoid
  37. * to risk increase the memory footprint of applications without a guaranteed
  38. * benefit. When transparent hugepage support is enabled, is for all mappings,
  39. * and khugepaged scans all mappings.
  40. * Defrag is invoked by khugepaged hugepage allocations and by page faults
  41. * for all hugepage allocations.
  42. */
  43. unsigned long transparent_hugepage_flags __read_mostly =
  44. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
  45. (1<<TRANSPARENT_HUGEPAGE_FLAG)|
  46. #endif
  47. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
  48. (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
  49. #endif
  50. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
  51. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
  52. (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  53. static struct shrinker deferred_split_shrinker;
  54. static atomic_t huge_zero_refcount;
  55. struct page *huge_zero_page __read_mostly;
  56. static struct page *get_huge_zero_page(void)
  57. {
  58. struct page *zero_page;
  59. retry:
  60. if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
  61. return READ_ONCE(huge_zero_page);
  62. zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
  63. HPAGE_PMD_ORDER);
  64. if (!zero_page) {
  65. count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
  66. return NULL;
  67. }
  68. count_vm_event(THP_ZERO_PAGE_ALLOC);
  69. preempt_disable();
  70. if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
  71. preempt_enable();
  72. __free_pages(zero_page, compound_order(zero_page));
  73. goto retry;
  74. }
  75. /* We take additional reference here. It will be put back by shrinker */
  76. atomic_set(&huge_zero_refcount, 2);
  77. preempt_enable();
  78. return READ_ONCE(huge_zero_page);
  79. }
  80. static void put_huge_zero_page(void)
  81. {
  82. /*
  83. * Counter should never go to zero here. Only shrinker can put
  84. * last reference.
  85. */
  86. BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
  87. }
  88. struct page *mm_get_huge_zero_page(struct mm_struct *mm)
  89. {
  90. if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
  91. return READ_ONCE(huge_zero_page);
  92. if (!get_huge_zero_page())
  93. return NULL;
  94. if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
  95. put_huge_zero_page();
  96. return READ_ONCE(huge_zero_page);
  97. }
  98. void mm_put_huge_zero_page(struct mm_struct *mm)
  99. {
  100. if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
  101. put_huge_zero_page();
  102. }
  103. static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
  104. struct shrink_control *sc)
  105. {
  106. /* we can free zero page only if last reference remains */
  107. return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
  108. }
  109. static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
  110. struct shrink_control *sc)
  111. {
  112. if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
  113. struct page *zero_page = xchg(&huge_zero_page, NULL);
  114. BUG_ON(zero_page == NULL);
  115. __free_pages(zero_page, compound_order(zero_page));
  116. return HPAGE_PMD_NR;
  117. }
  118. return 0;
  119. }
  120. static struct shrinker huge_zero_page_shrinker = {
  121. .count_objects = shrink_huge_zero_page_count,
  122. .scan_objects = shrink_huge_zero_page_scan,
  123. .seeks = DEFAULT_SEEKS,
  124. };
  125. #ifdef CONFIG_SYSFS
  126. static ssize_t triple_flag_store(struct kobject *kobj,
  127. struct kobj_attribute *attr,
  128. const char *buf, size_t count,
  129. enum transparent_hugepage_flag enabled,
  130. enum transparent_hugepage_flag deferred,
  131. enum transparent_hugepage_flag req_madv)
  132. {
  133. if (!memcmp("defer", buf,
  134. min(sizeof("defer")-1, count))) {
  135. if (enabled == deferred)
  136. return -EINVAL;
  137. clear_bit(enabled, &transparent_hugepage_flags);
  138. clear_bit(req_madv, &transparent_hugepage_flags);
  139. set_bit(deferred, &transparent_hugepage_flags);
  140. } else if (!memcmp("always", buf,
  141. min(sizeof("always")-1, count))) {
  142. clear_bit(deferred, &transparent_hugepage_flags);
  143. clear_bit(req_madv, &transparent_hugepage_flags);
  144. set_bit(enabled, &transparent_hugepage_flags);
  145. } else if (!memcmp("madvise", buf,
  146. min(sizeof("madvise")-1, count))) {
  147. clear_bit(enabled, &transparent_hugepage_flags);
  148. clear_bit(deferred, &transparent_hugepage_flags);
  149. set_bit(req_madv, &transparent_hugepage_flags);
  150. } else if (!memcmp("never", buf,
  151. min(sizeof("never")-1, count))) {
  152. clear_bit(enabled, &transparent_hugepage_flags);
  153. clear_bit(req_madv, &transparent_hugepage_flags);
  154. clear_bit(deferred, &transparent_hugepage_flags);
  155. } else
  156. return -EINVAL;
  157. return count;
  158. }
  159. static ssize_t enabled_show(struct kobject *kobj,
  160. struct kobj_attribute *attr, char *buf)
  161. {
  162. if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
  163. return sprintf(buf, "[always] madvise never\n");
  164. else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
  165. return sprintf(buf, "always [madvise] never\n");
  166. else
  167. return sprintf(buf, "always madvise [never]\n");
  168. }
  169. static ssize_t enabled_store(struct kobject *kobj,
  170. struct kobj_attribute *attr,
  171. const char *buf, size_t count)
  172. {
  173. ssize_t ret;
  174. ret = triple_flag_store(kobj, attr, buf, count,
  175. TRANSPARENT_HUGEPAGE_FLAG,
  176. TRANSPARENT_HUGEPAGE_FLAG,
  177. TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
  178. if (ret > 0) {
  179. int err = start_stop_khugepaged();
  180. if (err)
  181. ret = err;
  182. }
  183. return ret;
  184. }
  185. static struct kobj_attribute enabled_attr =
  186. __ATTR(enabled, 0644, enabled_show, enabled_store);
  187. ssize_t single_hugepage_flag_show(struct kobject *kobj,
  188. struct kobj_attribute *attr, char *buf,
  189. enum transparent_hugepage_flag flag)
  190. {
  191. return sprintf(buf, "%d\n",
  192. !!test_bit(flag, &transparent_hugepage_flags));
  193. }
  194. ssize_t single_hugepage_flag_store(struct kobject *kobj,
  195. struct kobj_attribute *attr,
  196. const char *buf, size_t count,
  197. enum transparent_hugepage_flag flag)
  198. {
  199. unsigned long value;
  200. int ret;
  201. ret = kstrtoul(buf, 10, &value);
  202. if (ret < 0)
  203. return ret;
  204. if (value > 1)
  205. return -EINVAL;
  206. if (value)
  207. set_bit(flag, &transparent_hugepage_flags);
  208. else
  209. clear_bit(flag, &transparent_hugepage_flags);
  210. return count;
  211. }
  212. /*
  213. * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
  214. * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
  215. * memory just to allocate one more hugepage.
  216. */
  217. static ssize_t defrag_show(struct kobject *kobj,
  218. struct kobj_attribute *attr, char *buf)
  219. {
  220. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
  221. return sprintf(buf, "[always] defer madvise never\n");
  222. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
  223. return sprintf(buf, "always [defer] madvise never\n");
  224. else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
  225. return sprintf(buf, "always defer [madvise] never\n");
  226. else
  227. return sprintf(buf, "always defer madvise [never]\n");
  228. }
  229. static ssize_t defrag_store(struct kobject *kobj,
  230. struct kobj_attribute *attr,
  231. const char *buf, size_t count)
  232. {
  233. return triple_flag_store(kobj, attr, buf, count,
  234. TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
  235. TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
  236. TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
  237. }
  238. static struct kobj_attribute defrag_attr =
  239. __ATTR(defrag, 0644, defrag_show, defrag_store);
  240. static ssize_t use_zero_page_show(struct kobject *kobj,
  241. struct kobj_attribute *attr, char *buf)
  242. {
  243. return single_hugepage_flag_show(kobj, attr, buf,
  244. TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  245. }
  246. static ssize_t use_zero_page_store(struct kobject *kobj,
  247. struct kobj_attribute *attr, const char *buf, size_t count)
  248. {
  249. return single_hugepage_flag_store(kobj, attr, buf, count,
  250. TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  251. }
  252. static struct kobj_attribute use_zero_page_attr =
  253. __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
  254. #ifdef CONFIG_DEBUG_VM
  255. static ssize_t debug_cow_show(struct kobject *kobj,
  256. struct kobj_attribute *attr, char *buf)
  257. {
  258. return single_hugepage_flag_show(kobj, attr, buf,
  259. TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
  260. }
  261. static ssize_t debug_cow_store(struct kobject *kobj,
  262. struct kobj_attribute *attr,
  263. const char *buf, size_t count)
  264. {
  265. return single_hugepage_flag_store(kobj, attr, buf, count,
  266. TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
  267. }
  268. static struct kobj_attribute debug_cow_attr =
  269. __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
  270. #endif /* CONFIG_DEBUG_VM */
  271. static struct attribute *hugepage_attr[] = {
  272. &enabled_attr.attr,
  273. &defrag_attr.attr,
  274. &use_zero_page_attr.attr,
  275. #if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
  276. &shmem_enabled_attr.attr,
  277. #endif
  278. #ifdef CONFIG_DEBUG_VM
  279. &debug_cow_attr.attr,
  280. #endif
  281. NULL,
  282. };
  283. static struct attribute_group hugepage_attr_group = {
  284. .attrs = hugepage_attr,
  285. };
  286. static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
  287. {
  288. int err;
  289. *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
  290. if (unlikely(!*hugepage_kobj)) {
  291. pr_err("failed to create transparent hugepage kobject\n");
  292. return -ENOMEM;
  293. }
  294. err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
  295. if (err) {
  296. pr_err("failed to register transparent hugepage group\n");
  297. goto delete_obj;
  298. }
  299. err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
  300. if (err) {
  301. pr_err("failed to register transparent hugepage group\n");
  302. goto remove_hp_group;
  303. }
  304. return 0;
  305. remove_hp_group:
  306. sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
  307. delete_obj:
  308. kobject_put(*hugepage_kobj);
  309. return err;
  310. }
  311. static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  312. {
  313. sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
  314. sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
  315. kobject_put(hugepage_kobj);
  316. }
  317. #else
  318. static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
  319. {
  320. return 0;
  321. }
  322. static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  323. {
  324. }
  325. #endif /* CONFIG_SYSFS */
  326. static int __init hugepage_init(void)
  327. {
  328. int err;
  329. struct kobject *hugepage_kobj;
  330. if (!has_transparent_hugepage()) {
  331. transparent_hugepage_flags = 0;
  332. return -EINVAL;
  333. }
  334. /*
  335. * hugepages can't be allocated by the buddy allocator
  336. */
  337. MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
  338. /*
  339. * we use page->mapping and page->index in second tail page
  340. * as list_head: assuming THP order >= 2
  341. */
  342. MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
  343. err = hugepage_init_sysfs(&hugepage_kobj);
  344. if (err)
  345. goto err_sysfs;
  346. err = khugepaged_init();
  347. if (err)
  348. goto err_slab;
  349. err = register_shrinker(&huge_zero_page_shrinker);
  350. if (err)
  351. goto err_hzp_shrinker;
  352. err = register_shrinker(&deferred_split_shrinker);
  353. if (err)
  354. goto err_split_shrinker;
  355. /*
  356. * By default disable transparent hugepages on smaller systems,
  357. * where the extra memory used could hurt more than TLB overhead
  358. * is likely to save. The admin can still enable it through /sys.
  359. */
  360. if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
  361. transparent_hugepage_flags = 0;
  362. return 0;
  363. }
  364. err = start_stop_khugepaged();
  365. if (err)
  366. goto err_khugepaged;
  367. return 0;
  368. err_khugepaged:
  369. unregister_shrinker(&deferred_split_shrinker);
  370. err_split_shrinker:
  371. unregister_shrinker(&huge_zero_page_shrinker);
  372. err_hzp_shrinker:
  373. khugepaged_destroy();
  374. err_slab:
  375. hugepage_exit_sysfs(hugepage_kobj);
  376. err_sysfs:
  377. return err;
  378. }
  379. subsys_initcall(hugepage_init);
  380. static int __init setup_transparent_hugepage(char *str)
  381. {
  382. int ret = 0;
  383. if (!str)
  384. goto out;
  385. if (!strcmp(str, "always")) {
  386. set_bit(TRANSPARENT_HUGEPAGE_FLAG,
  387. &transparent_hugepage_flags);
  388. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  389. &transparent_hugepage_flags);
  390. ret = 1;
  391. } else if (!strcmp(str, "madvise")) {
  392. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  393. &transparent_hugepage_flags);
  394. set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  395. &transparent_hugepage_flags);
  396. ret = 1;
  397. } else if (!strcmp(str, "never")) {
  398. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  399. &transparent_hugepage_flags);
  400. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  401. &transparent_hugepage_flags);
  402. ret = 1;
  403. }
  404. out:
  405. if (!ret)
  406. pr_warn("transparent_hugepage= cannot parse, ignored\n");
  407. return ret;
  408. }
  409. __setup("transparent_hugepage=", setup_transparent_hugepage);
  410. pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
  411. {
  412. if (likely(vma->vm_flags & VM_WRITE))
  413. pmd = pmd_mkwrite(pmd);
  414. return pmd;
  415. }
  416. static inline struct list_head *page_deferred_list(struct page *page)
  417. {
  418. /*
  419. * ->lru in the tail pages is occupied by compound_head.
  420. * Let's use ->mapping + ->index in the second tail page as list_head.
  421. */
  422. return (struct list_head *)&page[2].mapping;
  423. }
  424. void prep_transhuge_page(struct page *page)
  425. {
  426. /*
  427. * we use page->mapping and page->indexlru in second tail page
  428. * as list_head: assuming THP order >= 2
  429. */
  430. INIT_LIST_HEAD(page_deferred_list(page));
  431. set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
  432. }
  433. unsigned long __thp_get_unmapped_area(struct file *filp, unsigned long len,
  434. loff_t off, unsigned long flags, unsigned long size)
  435. {
  436. unsigned long addr;
  437. loff_t off_end = off + len;
  438. loff_t off_align = round_up(off, size);
  439. unsigned long len_pad;
  440. if (off_end <= off_align || (off_end - off_align) < size)
  441. return 0;
  442. len_pad = len + size;
  443. if (len_pad < len || (off + len_pad) < off)
  444. return 0;
  445. addr = current->mm->get_unmapped_area(filp, 0, len_pad,
  446. off >> PAGE_SHIFT, flags);
  447. if (IS_ERR_VALUE(addr))
  448. return 0;
  449. addr += (off - addr) & (size - 1);
  450. return addr;
  451. }
  452. unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
  453. unsigned long len, unsigned long pgoff, unsigned long flags)
  454. {
  455. loff_t off = (loff_t)pgoff << PAGE_SHIFT;
  456. if (addr)
  457. goto out;
  458. if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
  459. goto out;
  460. addr = __thp_get_unmapped_area(filp, len, off, flags, PMD_SIZE);
  461. if (addr)
  462. return addr;
  463. out:
  464. return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
  465. }
  466. EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
  467. static int __do_huge_pmd_anonymous_page(struct fault_env *fe, struct page *page,
  468. gfp_t gfp)
  469. {
  470. struct vm_area_struct *vma = fe->vma;
  471. struct mem_cgroup *memcg;
  472. pgtable_t pgtable;
  473. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  474. VM_BUG_ON_PAGE(!PageCompound(page), page);
  475. if (mem_cgroup_try_charge(page, vma->vm_mm, gfp | __GFP_NORETRY, &memcg,
  476. true)) {
  477. put_page(page);
  478. count_vm_event(THP_FAULT_FALLBACK);
  479. return VM_FAULT_FALLBACK;
  480. }
  481. pgtable = pte_alloc_one(vma->vm_mm, haddr);
  482. if (unlikely(!pgtable)) {
  483. mem_cgroup_cancel_charge(page, memcg, true);
  484. put_page(page);
  485. return VM_FAULT_OOM;
  486. }
  487. clear_huge_page(page, haddr, HPAGE_PMD_NR);
  488. /*
  489. * The memory barrier inside __SetPageUptodate makes sure that
  490. * clear_huge_page writes become visible before the set_pmd_at()
  491. * write.
  492. */
  493. __SetPageUptodate(page);
  494. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  495. if (unlikely(!pmd_none(*fe->pmd))) {
  496. spin_unlock(fe->ptl);
  497. mem_cgroup_cancel_charge(page, memcg, true);
  498. put_page(page);
  499. pte_free(vma->vm_mm, pgtable);
  500. } else {
  501. pmd_t entry;
  502. /* Deliver the page fault to userland */
  503. if (userfaultfd_missing(vma)) {
  504. int ret;
  505. spin_unlock(fe->ptl);
  506. mem_cgroup_cancel_charge(page, memcg, true);
  507. put_page(page);
  508. pte_free(vma->vm_mm, pgtable);
  509. ret = handle_userfault(fe, VM_UFFD_MISSING);
  510. VM_BUG_ON(ret & VM_FAULT_FALLBACK);
  511. return ret;
  512. }
  513. entry = mk_huge_pmd(page, vma->vm_page_prot);
  514. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  515. page_add_new_anon_rmap(page, vma, haddr, true);
  516. mem_cgroup_commit_charge(page, memcg, false, true);
  517. lru_cache_add_active_or_unevictable(page, vma);
  518. pgtable_trans_huge_deposit(vma->vm_mm, fe->pmd, pgtable);
  519. set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
  520. add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  521. atomic_long_inc(&vma->vm_mm->nr_ptes);
  522. spin_unlock(fe->ptl);
  523. count_vm_event(THP_FAULT_ALLOC);
  524. }
  525. return 0;
  526. }
  527. /*
  528. * If THP defrag is set to always then directly reclaim/compact as necessary
  529. * If set to defer then do only background reclaim/compact and defer to khugepaged
  530. * If set to madvise and the VMA is flagged then directly reclaim/compact
  531. * When direct reclaim/compact is allowed, don't retry except for flagged VMA's
  532. */
  533. static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
  534. {
  535. bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
  536. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
  537. &transparent_hugepage_flags) && vma_madvised)
  538. return GFP_TRANSHUGE;
  539. else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
  540. &transparent_hugepage_flags))
  541. return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
  542. else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
  543. &transparent_hugepage_flags))
  544. return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
  545. return GFP_TRANSHUGE_LIGHT;
  546. }
  547. /* Caller must hold page table lock. */
  548. static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
  549. struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
  550. struct page *zero_page)
  551. {
  552. pmd_t entry;
  553. if (!pmd_none(*pmd))
  554. return false;
  555. entry = mk_pmd(zero_page, vma->vm_page_prot);
  556. entry = pmd_mkhuge(entry);
  557. if (pgtable)
  558. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  559. set_pmd_at(mm, haddr, pmd, entry);
  560. atomic_long_inc(&mm->nr_ptes);
  561. return true;
  562. }
  563. int do_huge_pmd_anonymous_page(struct fault_env *fe)
  564. {
  565. struct vm_area_struct *vma = fe->vma;
  566. gfp_t gfp;
  567. struct page *page;
  568. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  569. if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
  570. return VM_FAULT_FALLBACK;
  571. if (unlikely(anon_vma_prepare(vma)))
  572. return VM_FAULT_OOM;
  573. if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
  574. return VM_FAULT_OOM;
  575. if (!(fe->flags & FAULT_FLAG_WRITE) &&
  576. !mm_forbids_zeropage(vma->vm_mm) &&
  577. transparent_hugepage_use_zero_page()) {
  578. pgtable_t pgtable;
  579. struct page *zero_page;
  580. bool set;
  581. int ret;
  582. pgtable = pte_alloc_one(vma->vm_mm, haddr);
  583. if (unlikely(!pgtable))
  584. return VM_FAULT_OOM;
  585. zero_page = mm_get_huge_zero_page(vma->vm_mm);
  586. if (unlikely(!zero_page)) {
  587. pte_free(vma->vm_mm, pgtable);
  588. count_vm_event(THP_FAULT_FALLBACK);
  589. return VM_FAULT_FALLBACK;
  590. }
  591. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  592. ret = 0;
  593. set = false;
  594. if (pmd_none(*fe->pmd)) {
  595. if (userfaultfd_missing(vma)) {
  596. spin_unlock(fe->ptl);
  597. ret = handle_userfault(fe, VM_UFFD_MISSING);
  598. VM_BUG_ON(ret & VM_FAULT_FALLBACK);
  599. } else {
  600. set_huge_zero_page(pgtable, vma->vm_mm, vma,
  601. haddr, fe->pmd, zero_page);
  602. spin_unlock(fe->ptl);
  603. set = true;
  604. }
  605. } else
  606. spin_unlock(fe->ptl);
  607. if (!set)
  608. pte_free(vma->vm_mm, pgtable);
  609. return ret;
  610. }
  611. gfp = alloc_hugepage_direct_gfpmask(vma);
  612. page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
  613. if (unlikely(!page)) {
  614. count_vm_event(THP_FAULT_FALLBACK);
  615. return VM_FAULT_FALLBACK;
  616. }
  617. prep_transhuge_page(page);
  618. return __do_huge_pmd_anonymous_page(fe, page, gfp);
  619. }
  620. static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
  621. pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
  622. {
  623. struct mm_struct *mm = vma->vm_mm;
  624. pmd_t entry;
  625. spinlock_t *ptl;
  626. ptl = pmd_lock(mm, pmd);
  627. entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
  628. if (pfn_t_devmap(pfn))
  629. entry = pmd_mkdevmap(entry);
  630. if (write) {
  631. entry = pmd_mkyoung(pmd_mkdirty(entry));
  632. entry = maybe_pmd_mkwrite(entry, vma);
  633. }
  634. set_pmd_at(mm, addr, pmd, entry);
  635. update_mmu_cache_pmd(vma, addr, pmd);
  636. spin_unlock(ptl);
  637. }
  638. int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
  639. pmd_t *pmd, pfn_t pfn, bool write)
  640. {
  641. pgprot_t pgprot = vma->vm_page_prot;
  642. /*
  643. * If we had pmd_special, we could avoid all these restrictions,
  644. * but we need to be consistent with PTEs and architectures that
  645. * can't support a 'special' bit.
  646. */
  647. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  648. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  649. (VM_PFNMAP|VM_MIXEDMAP));
  650. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  651. BUG_ON(!pfn_t_devmap(pfn));
  652. if (addr < vma->vm_start || addr >= vma->vm_end)
  653. return VM_FAULT_SIGBUS;
  654. if (track_pfn_insert(vma, &pgprot, pfn))
  655. return VM_FAULT_SIGBUS;
  656. insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
  657. return VM_FAULT_NOPAGE;
  658. }
  659. EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
  660. static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
  661. pmd_t *pmd, int flags)
  662. {
  663. pmd_t _pmd;
  664. _pmd = pmd_mkyoung(*pmd);
  665. if (flags & FOLL_WRITE)
  666. _pmd = pmd_mkdirty(_pmd);
  667. if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
  668. pmd, _pmd, flags & FOLL_WRITE))
  669. update_mmu_cache_pmd(vma, addr, pmd);
  670. }
  671. struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
  672. pmd_t *pmd, int flags)
  673. {
  674. unsigned long pfn = pmd_pfn(*pmd);
  675. struct mm_struct *mm = vma->vm_mm;
  676. struct dev_pagemap *pgmap;
  677. struct page *page;
  678. assert_spin_locked(pmd_lockptr(mm, pmd));
  679. /*
  680. * When we COW a devmap PMD entry, we split it into PTEs, so we should
  681. * not be in this function with `flags & FOLL_COW` set.
  682. */
  683. WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set");
  684. if (flags & FOLL_WRITE && !pmd_write(*pmd))
  685. return NULL;
  686. if (pmd_present(*pmd) && pmd_devmap(*pmd))
  687. /* pass */;
  688. else
  689. return NULL;
  690. if (flags & FOLL_TOUCH)
  691. touch_pmd(vma, addr, pmd, flags);
  692. /*
  693. * device mapped pages can only be returned if the
  694. * caller will manage the page reference count.
  695. */
  696. if (!(flags & FOLL_GET))
  697. return ERR_PTR(-EEXIST);
  698. pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
  699. pgmap = get_dev_pagemap(pfn, NULL);
  700. if (!pgmap)
  701. return ERR_PTR(-EFAULT);
  702. page = pfn_to_page(pfn);
  703. get_page(page);
  704. put_dev_pagemap(pgmap);
  705. return page;
  706. }
  707. int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  708. pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
  709. struct vm_area_struct *vma)
  710. {
  711. spinlock_t *dst_ptl, *src_ptl;
  712. struct page *src_page;
  713. pmd_t pmd;
  714. pgtable_t pgtable = NULL;
  715. int ret = -ENOMEM;
  716. /* Skip if can be re-fill on fault */
  717. if (!vma_is_anonymous(vma))
  718. return 0;
  719. pgtable = pte_alloc_one(dst_mm, addr);
  720. if (unlikely(!pgtable))
  721. goto out;
  722. dst_ptl = pmd_lock(dst_mm, dst_pmd);
  723. src_ptl = pmd_lockptr(src_mm, src_pmd);
  724. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  725. ret = -EAGAIN;
  726. pmd = *src_pmd;
  727. if (unlikely(!pmd_trans_huge(pmd))) {
  728. pte_free(dst_mm, pgtable);
  729. goto out_unlock;
  730. }
  731. /*
  732. * When page table lock is held, the huge zero pmd should not be
  733. * under splitting since we don't split the page itself, only pmd to
  734. * a page table.
  735. */
  736. if (is_huge_zero_pmd(pmd)) {
  737. struct page *zero_page;
  738. /*
  739. * get_huge_zero_page() will never allocate a new page here,
  740. * since we already have a zero page to copy. It just takes a
  741. * reference.
  742. */
  743. zero_page = mm_get_huge_zero_page(dst_mm);
  744. set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
  745. zero_page);
  746. ret = 0;
  747. goto out_unlock;
  748. }
  749. src_page = pmd_page(pmd);
  750. VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
  751. get_page(src_page);
  752. page_dup_rmap(src_page, true);
  753. add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  754. atomic_long_inc(&dst_mm->nr_ptes);
  755. pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
  756. pmdp_set_wrprotect(src_mm, addr, src_pmd);
  757. pmd = pmd_mkold(pmd_wrprotect(pmd));
  758. set_pmd_at(dst_mm, addr, dst_pmd, pmd);
  759. ret = 0;
  760. out_unlock:
  761. spin_unlock(src_ptl);
  762. spin_unlock(dst_ptl);
  763. out:
  764. return ret;
  765. }
  766. void huge_pmd_set_accessed(struct fault_env *fe, pmd_t orig_pmd)
  767. {
  768. pmd_t entry;
  769. unsigned long haddr;
  770. bool write = fe->flags & FAULT_FLAG_WRITE;
  771. fe->ptl = pmd_lock(fe->vma->vm_mm, fe->pmd);
  772. if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
  773. goto unlock;
  774. entry = pmd_mkyoung(orig_pmd);
  775. if (write)
  776. entry = pmd_mkdirty(entry);
  777. haddr = fe->address & HPAGE_PMD_MASK;
  778. if (pmdp_set_access_flags(fe->vma, haddr, fe->pmd, entry, write))
  779. update_mmu_cache_pmd(fe->vma, fe->address, fe->pmd);
  780. unlock:
  781. spin_unlock(fe->ptl);
  782. }
  783. static int do_huge_pmd_wp_page_fallback(struct fault_env *fe, pmd_t orig_pmd,
  784. struct page *page)
  785. {
  786. struct vm_area_struct *vma = fe->vma;
  787. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  788. struct mem_cgroup *memcg;
  789. pgtable_t pgtable;
  790. pmd_t _pmd;
  791. int ret = 0, i;
  792. struct page **pages;
  793. unsigned long mmun_start; /* For mmu_notifiers */
  794. unsigned long mmun_end; /* For mmu_notifiers */
  795. pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
  796. GFP_KERNEL);
  797. if (unlikely(!pages)) {
  798. ret |= VM_FAULT_OOM;
  799. goto out;
  800. }
  801. for (i = 0; i < HPAGE_PMD_NR; i++) {
  802. pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
  803. __GFP_OTHER_NODE, vma,
  804. fe->address, page_to_nid(page));
  805. if (unlikely(!pages[i] ||
  806. mem_cgroup_try_charge(pages[i], vma->vm_mm,
  807. GFP_KERNEL, &memcg, false))) {
  808. if (pages[i])
  809. put_page(pages[i]);
  810. while (--i >= 0) {
  811. memcg = (void *)page_private(pages[i]);
  812. set_page_private(pages[i], 0);
  813. mem_cgroup_cancel_charge(pages[i], memcg,
  814. false);
  815. put_page(pages[i]);
  816. }
  817. kfree(pages);
  818. ret |= VM_FAULT_OOM;
  819. goto out;
  820. }
  821. set_page_private(pages[i], (unsigned long)memcg);
  822. }
  823. for (i = 0; i < HPAGE_PMD_NR; i++) {
  824. copy_user_highpage(pages[i], page + i,
  825. haddr + PAGE_SIZE * i, vma);
  826. __SetPageUptodate(pages[i]);
  827. cond_resched();
  828. }
  829. mmun_start = haddr;
  830. mmun_end = haddr + HPAGE_PMD_SIZE;
  831. mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
  832. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  833. if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
  834. goto out_free_pages;
  835. VM_BUG_ON_PAGE(!PageHead(page), page);
  836. pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
  837. /* leave pmd empty until pte is filled */
  838. pgtable = pgtable_trans_huge_withdraw(vma->vm_mm, fe->pmd);
  839. pmd_populate(vma->vm_mm, &_pmd, pgtable);
  840. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  841. pte_t entry;
  842. entry = mk_pte(pages[i], vma->vm_page_prot);
  843. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  844. memcg = (void *)page_private(pages[i]);
  845. set_page_private(pages[i], 0);
  846. page_add_new_anon_rmap(pages[i], fe->vma, haddr, false);
  847. mem_cgroup_commit_charge(pages[i], memcg, false, false);
  848. lru_cache_add_active_or_unevictable(pages[i], vma);
  849. fe->pte = pte_offset_map(&_pmd, haddr);
  850. VM_BUG_ON(!pte_none(*fe->pte));
  851. set_pte_at(vma->vm_mm, haddr, fe->pte, entry);
  852. pte_unmap(fe->pte);
  853. }
  854. kfree(pages);
  855. smp_wmb(); /* make pte visible before pmd */
  856. pmd_populate(vma->vm_mm, fe->pmd, pgtable);
  857. page_remove_rmap(page, true);
  858. spin_unlock(fe->ptl);
  859. mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
  860. ret |= VM_FAULT_WRITE;
  861. put_page(page);
  862. out:
  863. return ret;
  864. out_free_pages:
  865. spin_unlock(fe->ptl);
  866. mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
  867. for (i = 0; i < HPAGE_PMD_NR; i++) {
  868. memcg = (void *)page_private(pages[i]);
  869. set_page_private(pages[i], 0);
  870. mem_cgroup_cancel_charge(pages[i], memcg, false);
  871. put_page(pages[i]);
  872. }
  873. kfree(pages);
  874. goto out;
  875. }
  876. int do_huge_pmd_wp_page(struct fault_env *fe, pmd_t orig_pmd)
  877. {
  878. struct vm_area_struct *vma = fe->vma;
  879. struct page *page = NULL, *new_page;
  880. struct mem_cgroup *memcg;
  881. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  882. unsigned long mmun_start; /* For mmu_notifiers */
  883. unsigned long mmun_end; /* For mmu_notifiers */
  884. gfp_t huge_gfp; /* for allocation and charge */
  885. int ret = 0;
  886. fe->ptl = pmd_lockptr(vma->vm_mm, fe->pmd);
  887. VM_BUG_ON_VMA(!vma->anon_vma, vma);
  888. if (is_huge_zero_pmd(orig_pmd))
  889. goto alloc;
  890. spin_lock(fe->ptl);
  891. if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
  892. goto out_unlock;
  893. page = pmd_page(orig_pmd);
  894. VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
  895. /*
  896. * We can only reuse the page if nobody else maps the huge page or it's
  897. * part.
  898. */
  899. if (page_trans_huge_mapcount(page, NULL) == 1) {
  900. pmd_t entry;
  901. entry = pmd_mkyoung(orig_pmd);
  902. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  903. if (pmdp_set_access_flags(vma, haddr, fe->pmd, entry, 1))
  904. update_mmu_cache_pmd(vma, fe->address, fe->pmd);
  905. ret |= VM_FAULT_WRITE;
  906. goto out_unlock;
  907. }
  908. get_page(page);
  909. spin_unlock(fe->ptl);
  910. alloc:
  911. if (transparent_hugepage_enabled(vma) &&
  912. !transparent_hugepage_debug_cow()) {
  913. huge_gfp = alloc_hugepage_direct_gfpmask(vma);
  914. new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
  915. } else
  916. new_page = NULL;
  917. if (likely(new_page)) {
  918. prep_transhuge_page(new_page);
  919. } else {
  920. if (!page) {
  921. split_huge_pmd(vma, fe->pmd, fe->address);
  922. ret |= VM_FAULT_FALLBACK;
  923. } else {
  924. ret = do_huge_pmd_wp_page_fallback(fe, orig_pmd, page);
  925. if (ret & VM_FAULT_OOM) {
  926. split_huge_pmd(vma, fe->pmd, fe->address);
  927. ret |= VM_FAULT_FALLBACK;
  928. }
  929. put_page(page);
  930. }
  931. count_vm_event(THP_FAULT_FALLBACK);
  932. goto out;
  933. }
  934. if (unlikely(mem_cgroup_try_charge(new_page, vma->vm_mm,
  935. huge_gfp | __GFP_NORETRY, &memcg, true))) {
  936. put_page(new_page);
  937. split_huge_pmd(vma, fe->pmd, fe->address);
  938. if (page)
  939. put_page(page);
  940. ret |= VM_FAULT_FALLBACK;
  941. count_vm_event(THP_FAULT_FALLBACK);
  942. goto out;
  943. }
  944. count_vm_event(THP_FAULT_ALLOC);
  945. if (!page)
  946. clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
  947. else
  948. copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
  949. __SetPageUptodate(new_page);
  950. mmun_start = haddr;
  951. mmun_end = haddr + HPAGE_PMD_SIZE;
  952. mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
  953. spin_lock(fe->ptl);
  954. if (page)
  955. put_page(page);
  956. if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) {
  957. spin_unlock(fe->ptl);
  958. mem_cgroup_cancel_charge(new_page, memcg, true);
  959. put_page(new_page);
  960. goto out_mn;
  961. } else {
  962. pmd_t entry;
  963. entry = mk_huge_pmd(new_page, vma->vm_page_prot);
  964. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  965. pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
  966. page_add_new_anon_rmap(new_page, vma, haddr, true);
  967. mem_cgroup_commit_charge(new_page, memcg, false, true);
  968. lru_cache_add_active_or_unevictable(new_page, vma);
  969. set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
  970. update_mmu_cache_pmd(vma, fe->address, fe->pmd);
  971. if (!page) {
  972. add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  973. } else {
  974. VM_BUG_ON_PAGE(!PageHead(page), page);
  975. page_remove_rmap(page, true);
  976. put_page(page);
  977. }
  978. ret |= VM_FAULT_WRITE;
  979. }
  980. spin_unlock(fe->ptl);
  981. out_mn:
  982. mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
  983. out:
  984. return ret;
  985. out_unlock:
  986. spin_unlock(fe->ptl);
  987. return ret;
  988. }
  989. /*
  990. * FOLL_FORCE can write to even unwritable pmd's, but only
  991. * after we've gone through a COW cycle and they are dirty.
  992. */
  993. static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
  994. {
  995. return pmd_write(pmd) ||
  996. ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
  997. }
  998. struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
  999. unsigned long addr,
  1000. pmd_t *pmd,
  1001. unsigned int flags)
  1002. {
  1003. struct mm_struct *mm = vma->vm_mm;
  1004. struct page *page = NULL;
  1005. assert_spin_locked(pmd_lockptr(mm, pmd));
  1006. if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
  1007. goto out;
  1008. /* Avoid dumping huge zero page */
  1009. if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
  1010. return ERR_PTR(-EFAULT);
  1011. /* Full NUMA hinting faults to serialise migration in fault paths */
  1012. if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
  1013. goto out;
  1014. page = pmd_page(*pmd);
  1015. VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
  1016. if (flags & FOLL_TOUCH)
  1017. touch_pmd(vma, addr, pmd, flags);
  1018. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  1019. /*
  1020. * We don't mlock() pte-mapped THPs. This way we can avoid
  1021. * leaking mlocked pages into non-VM_LOCKED VMAs.
  1022. *
  1023. * For anon THP:
  1024. *
  1025. * In most cases the pmd is the only mapping of the page as we
  1026. * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
  1027. * writable private mappings in populate_vma_page_range().
  1028. *
  1029. * The only scenario when we have the page shared here is if we
  1030. * mlocking read-only mapping shared over fork(). We skip
  1031. * mlocking such pages.
  1032. *
  1033. * For file THP:
  1034. *
  1035. * We can expect PageDoubleMap() to be stable under page lock:
  1036. * for file pages we set it in page_add_file_rmap(), which
  1037. * requires page to be locked.
  1038. */
  1039. if (PageAnon(page) && compound_mapcount(page) != 1)
  1040. goto skip_mlock;
  1041. if (PageDoubleMap(page) || !page->mapping)
  1042. goto skip_mlock;
  1043. if (!trylock_page(page))
  1044. goto skip_mlock;
  1045. lru_add_drain();
  1046. if (page->mapping && !PageDoubleMap(page))
  1047. mlock_vma_page(page);
  1048. unlock_page(page);
  1049. }
  1050. skip_mlock:
  1051. page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
  1052. VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
  1053. if (flags & FOLL_GET)
  1054. get_page(page);
  1055. out:
  1056. return page;
  1057. }
  1058. /* NUMA hinting page fault entry point for trans huge pmds */
  1059. int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t pmd)
  1060. {
  1061. struct vm_area_struct *vma = fe->vma;
  1062. struct anon_vma *anon_vma = NULL;
  1063. struct page *page;
  1064. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  1065. int page_nid = -1, this_nid = numa_node_id();
  1066. int target_nid, last_cpupid = -1;
  1067. bool page_locked;
  1068. bool migrated = false;
  1069. bool was_writable;
  1070. int flags = 0;
  1071. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  1072. if (unlikely(!pmd_same(pmd, *fe->pmd)))
  1073. goto out_unlock;
  1074. /*
  1075. * If there are potential migrations, wait for completion and retry
  1076. * without disrupting NUMA hinting information. Do not relock and
  1077. * check_same as the page may no longer be mapped.
  1078. */
  1079. if (unlikely(pmd_trans_migrating(*fe->pmd))) {
  1080. page = pmd_page(*fe->pmd);
  1081. if (!get_page_unless_zero(page))
  1082. goto out_unlock;
  1083. spin_unlock(fe->ptl);
  1084. wait_on_page_locked(page);
  1085. put_page(page);
  1086. goto out;
  1087. }
  1088. page = pmd_page(pmd);
  1089. BUG_ON(is_huge_zero_page(page));
  1090. page_nid = page_to_nid(page);
  1091. last_cpupid = page_cpupid_last(page);
  1092. count_vm_numa_event(NUMA_HINT_FAULTS);
  1093. if (page_nid == this_nid) {
  1094. count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
  1095. flags |= TNF_FAULT_LOCAL;
  1096. }
  1097. /* See similar comment in do_numa_page for explanation */
  1098. if (!pmd_write(pmd))
  1099. flags |= TNF_NO_GROUP;
  1100. /*
  1101. * Acquire the page lock to serialise THP migrations but avoid dropping
  1102. * page_table_lock if at all possible
  1103. */
  1104. page_locked = trylock_page(page);
  1105. target_nid = mpol_misplaced(page, vma, haddr);
  1106. if (target_nid == -1) {
  1107. /* If the page was locked, there are no parallel migrations */
  1108. if (page_locked)
  1109. goto clear_pmdnuma;
  1110. }
  1111. /* Migration could have started since the pmd_trans_migrating check */
  1112. if (!page_locked) {
  1113. page_nid = -1;
  1114. if (!get_page_unless_zero(page))
  1115. goto out_unlock;
  1116. spin_unlock(fe->ptl);
  1117. wait_on_page_locked(page);
  1118. put_page(page);
  1119. goto out;
  1120. }
  1121. /*
  1122. * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
  1123. * to serialises splits
  1124. */
  1125. get_page(page);
  1126. spin_unlock(fe->ptl);
  1127. anon_vma = page_lock_anon_vma_read(page);
  1128. /* Confirm the PMD did not change while page_table_lock was released */
  1129. spin_lock(fe->ptl);
  1130. if (unlikely(!pmd_same(pmd, *fe->pmd))) {
  1131. unlock_page(page);
  1132. put_page(page);
  1133. page_nid = -1;
  1134. goto out_unlock;
  1135. }
  1136. /* Bail if we fail to protect against THP splits for any reason */
  1137. if (unlikely(!anon_vma)) {
  1138. put_page(page);
  1139. page_nid = -1;
  1140. goto clear_pmdnuma;
  1141. }
  1142. /*
  1143. * Migrate the THP to the requested node, returns with page unlocked
  1144. * and access rights restored.
  1145. */
  1146. spin_unlock(fe->ptl);
  1147. migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
  1148. fe->pmd, pmd, fe->address, page, target_nid);
  1149. if (migrated) {
  1150. flags |= TNF_MIGRATED;
  1151. page_nid = target_nid;
  1152. } else
  1153. flags |= TNF_MIGRATE_FAIL;
  1154. goto out;
  1155. clear_pmdnuma:
  1156. BUG_ON(!PageLocked(page));
  1157. was_writable = pmd_write(pmd);
  1158. pmd = pmd_modify(pmd, vma->vm_page_prot);
  1159. pmd = pmd_mkyoung(pmd);
  1160. if (was_writable)
  1161. pmd = pmd_mkwrite(pmd);
  1162. set_pmd_at(vma->vm_mm, haddr, fe->pmd, pmd);
  1163. update_mmu_cache_pmd(vma, fe->address, fe->pmd);
  1164. unlock_page(page);
  1165. out_unlock:
  1166. spin_unlock(fe->ptl);
  1167. out:
  1168. if (anon_vma)
  1169. page_unlock_anon_vma_read(anon_vma);
  1170. if (page_nid != -1)
  1171. task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, fe->flags);
  1172. return 0;
  1173. }
  1174. /*
  1175. * Return true if we do MADV_FREE successfully on entire pmd page.
  1176. * Otherwise, return false.
  1177. */
  1178. bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  1179. pmd_t *pmd, unsigned long addr, unsigned long next)
  1180. {
  1181. spinlock_t *ptl;
  1182. pmd_t orig_pmd;
  1183. struct page *page;
  1184. struct mm_struct *mm = tlb->mm;
  1185. bool ret = false;
  1186. ptl = pmd_trans_huge_lock(pmd, vma);
  1187. if (!ptl)
  1188. goto out_unlocked;
  1189. orig_pmd = *pmd;
  1190. if (is_huge_zero_pmd(orig_pmd))
  1191. goto out;
  1192. page = pmd_page(orig_pmd);
  1193. /*
  1194. * If other processes are mapping this page, we couldn't discard
  1195. * the page unless they all do MADV_FREE so let's skip the page.
  1196. */
  1197. if (page_mapcount(page) != 1)
  1198. goto out;
  1199. if (!trylock_page(page))
  1200. goto out;
  1201. /*
  1202. * If user want to discard part-pages of THP, split it so MADV_FREE
  1203. * will deactivate only them.
  1204. */
  1205. if (next - addr != HPAGE_PMD_SIZE) {
  1206. get_page(page);
  1207. spin_unlock(ptl);
  1208. split_huge_page(page);
  1209. unlock_page(page);
  1210. put_page(page);
  1211. goto out_unlocked;
  1212. }
  1213. if (PageDirty(page))
  1214. ClearPageDirty(page);
  1215. unlock_page(page);
  1216. if (PageActive(page))
  1217. deactivate_page(page);
  1218. if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
  1219. pmdp_invalidate(vma, addr, pmd);
  1220. orig_pmd = pmd_mkold(orig_pmd);
  1221. orig_pmd = pmd_mkclean(orig_pmd);
  1222. set_pmd_at(mm, addr, pmd, orig_pmd);
  1223. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  1224. }
  1225. ret = true;
  1226. out:
  1227. spin_unlock(ptl);
  1228. out_unlocked:
  1229. return ret;
  1230. }
  1231. int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  1232. pmd_t *pmd, unsigned long addr)
  1233. {
  1234. pmd_t orig_pmd;
  1235. spinlock_t *ptl;
  1236. ptl = __pmd_trans_huge_lock(pmd, vma);
  1237. if (!ptl)
  1238. return 0;
  1239. /*
  1240. * For architectures like ppc64 we look at deposited pgtable
  1241. * when calling pmdp_huge_get_and_clear. So do the
  1242. * pgtable_trans_huge_withdraw after finishing pmdp related
  1243. * operations.
  1244. */
  1245. orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
  1246. tlb->fullmm);
  1247. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  1248. if (vma_is_dax(vma)) {
  1249. spin_unlock(ptl);
  1250. if (is_huge_zero_pmd(orig_pmd))
  1251. tlb_remove_page(tlb, pmd_page(orig_pmd));
  1252. } else if (is_huge_zero_pmd(orig_pmd)) {
  1253. pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
  1254. atomic_long_dec(&tlb->mm->nr_ptes);
  1255. spin_unlock(ptl);
  1256. tlb_remove_page(tlb, pmd_page(orig_pmd));
  1257. } else {
  1258. struct page *page = pmd_page(orig_pmd);
  1259. page_remove_rmap(page, true);
  1260. VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
  1261. VM_BUG_ON_PAGE(!PageHead(page), page);
  1262. if (PageAnon(page)) {
  1263. pgtable_t pgtable;
  1264. pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
  1265. pte_free(tlb->mm, pgtable);
  1266. atomic_long_dec(&tlb->mm->nr_ptes);
  1267. add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
  1268. } else {
  1269. add_mm_counter(tlb->mm, MM_FILEPAGES, -HPAGE_PMD_NR);
  1270. }
  1271. spin_unlock(ptl);
  1272. tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
  1273. }
  1274. return 1;
  1275. }
  1276. bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
  1277. unsigned long new_addr, unsigned long old_end,
  1278. pmd_t *old_pmd, pmd_t *new_pmd)
  1279. {
  1280. spinlock_t *old_ptl, *new_ptl;
  1281. pmd_t pmd;
  1282. struct mm_struct *mm = vma->vm_mm;
  1283. bool force_flush = false;
  1284. if ((old_addr & ~HPAGE_PMD_MASK) ||
  1285. (new_addr & ~HPAGE_PMD_MASK) ||
  1286. old_end - old_addr < HPAGE_PMD_SIZE)
  1287. return false;
  1288. /*
  1289. * The destination pmd shouldn't be established, free_pgtables()
  1290. * should have release it.
  1291. */
  1292. if (WARN_ON(!pmd_none(*new_pmd))) {
  1293. VM_BUG_ON(pmd_trans_huge(*new_pmd));
  1294. return false;
  1295. }
  1296. /*
  1297. * We don't have to worry about the ordering of src and dst
  1298. * ptlocks because exclusive mmap_sem prevents deadlock.
  1299. */
  1300. old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
  1301. if (old_ptl) {
  1302. new_ptl = pmd_lockptr(mm, new_pmd);
  1303. if (new_ptl != old_ptl)
  1304. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  1305. pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
  1306. if (pmd_present(pmd))
  1307. force_flush = true;
  1308. VM_BUG_ON(!pmd_none(*new_pmd));
  1309. if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
  1310. vma_is_anonymous(vma)) {
  1311. pgtable_t pgtable;
  1312. pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
  1313. pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
  1314. }
  1315. set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
  1316. if (force_flush)
  1317. flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
  1318. if (new_ptl != old_ptl)
  1319. spin_unlock(new_ptl);
  1320. spin_unlock(old_ptl);
  1321. return true;
  1322. }
  1323. return false;
  1324. }
  1325. /*
  1326. * Returns
  1327. * - 0 if PMD could not be locked
  1328. * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
  1329. * - HPAGE_PMD_NR is protections changed and TLB flush necessary
  1330. */
  1331. int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1332. unsigned long addr, pgprot_t newprot, int prot_numa)
  1333. {
  1334. struct mm_struct *mm = vma->vm_mm;
  1335. spinlock_t *ptl;
  1336. pmd_t entry;
  1337. bool preserve_write;
  1338. int ret;
  1339. ptl = __pmd_trans_huge_lock(pmd, vma);
  1340. if (!ptl)
  1341. return 0;
  1342. preserve_write = prot_numa && pmd_write(*pmd);
  1343. ret = 1;
  1344. /*
  1345. * Avoid trapping faults against the zero page. The read-only
  1346. * data is likely to be read-cached on the local CPU and
  1347. * local/remote hits to the zero page are not interesting.
  1348. */
  1349. if (prot_numa && is_huge_zero_pmd(*pmd))
  1350. goto unlock;
  1351. if (prot_numa && pmd_protnone(*pmd))
  1352. goto unlock;
  1353. /*
  1354. * In case prot_numa, we are under down_read(mmap_sem). It's critical
  1355. * to not clear pmd intermittently to avoid race with MADV_DONTNEED
  1356. * which is also under down_read(mmap_sem):
  1357. *
  1358. * CPU0: CPU1:
  1359. * change_huge_pmd(prot_numa=1)
  1360. * pmdp_huge_get_and_clear_notify()
  1361. * madvise_dontneed()
  1362. * zap_pmd_range()
  1363. * pmd_trans_huge(*pmd) == 0 (without ptl)
  1364. * // skip the pmd
  1365. * set_pmd_at();
  1366. * // pmd is re-established
  1367. *
  1368. * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
  1369. * which may break userspace.
  1370. *
  1371. * pmdp_invalidate() is required to make sure we don't miss
  1372. * dirty/young flags set by hardware.
  1373. */
  1374. entry = *pmd;
  1375. pmdp_invalidate(vma, addr, pmd);
  1376. /*
  1377. * Recover dirty/young flags. It relies on pmdp_invalidate to not
  1378. * corrupt them.
  1379. */
  1380. if (pmd_dirty(*pmd))
  1381. entry = pmd_mkdirty(entry);
  1382. if (pmd_young(*pmd))
  1383. entry = pmd_mkyoung(entry);
  1384. entry = pmd_modify(entry, newprot);
  1385. if (preserve_write)
  1386. entry = pmd_mkwrite(entry);
  1387. ret = HPAGE_PMD_NR;
  1388. set_pmd_at(mm, addr, pmd, entry);
  1389. BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
  1390. unlock:
  1391. spin_unlock(ptl);
  1392. return ret;
  1393. }
  1394. /*
  1395. * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
  1396. *
  1397. * Note that if it returns page table lock pointer, this routine returns without
  1398. * unlocking page table lock. So callers must unlock it.
  1399. */
  1400. spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
  1401. {
  1402. spinlock_t *ptl;
  1403. ptl = pmd_lock(vma->vm_mm, pmd);
  1404. if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
  1405. return ptl;
  1406. spin_unlock(ptl);
  1407. return NULL;
  1408. }
  1409. static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
  1410. unsigned long haddr, pmd_t *pmd)
  1411. {
  1412. struct mm_struct *mm = vma->vm_mm;
  1413. pgtable_t pgtable;
  1414. pmd_t _pmd;
  1415. int i;
  1416. /* leave pmd empty until pte is filled */
  1417. pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  1418. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1419. pmd_populate(mm, &_pmd, pgtable);
  1420. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  1421. pte_t *pte, entry;
  1422. entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
  1423. entry = pte_mkspecial(entry);
  1424. pte = pte_offset_map(&_pmd, haddr);
  1425. VM_BUG_ON(!pte_none(*pte));
  1426. set_pte_at(mm, haddr, pte, entry);
  1427. pte_unmap(pte);
  1428. }
  1429. smp_wmb(); /* make pte visible before pmd */
  1430. pmd_populate(mm, pmd, pgtable);
  1431. }
  1432. static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
  1433. unsigned long haddr, bool freeze)
  1434. {
  1435. struct mm_struct *mm = vma->vm_mm;
  1436. struct page *page;
  1437. pgtable_t pgtable;
  1438. pmd_t _pmd;
  1439. bool young, write, dirty, soft_dirty;
  1440. unsigned long addr;
  1441. int i;
  1442. VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
  1443. VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
  1444. VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
  1445. VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
  1446. count_vm_event(THP_SPLIT_PMD);
  1447. if (!vma_is_anonymous(vma)) {
  1448. _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  1449. if (vma_is_dax(vma))
  1450. return;
  1451. page = pmd_page(_pmd);
  1452. if (!PageDirty(page) && pmd_dirty(_pmd))
  1453. set_page_dirty(page);
  1454. if (!PageReferenced(page) && pmd_young(_pmd))
  1455. SetPageReferenced(page);
  1456. page_remove_rmap(page, true);
  1457. put_page(page);
  1458. add_mm_counter(mm, MM_FILEPAGES, -HPAGE_PMD_NR);
  1459. return;
  1460. } else if (is_huge_zero_pmd(*pmd)) {
  1461. return __split_huge_zero_page_pmd(vma, haddr, pmd);
  1462. }
  1463. page = pmd_page(*pmd);
  1464. VM_BUG_ON_PAGE(!page_count(page), page);
  1465. page_ref_add(page, HPAGE_PMD_NR - 1);
  1466. write = pmd_write(*pmd);
  1467. young = pmd_young(*pmd);
  1468. dirty = pmd_dirty(*pmd);
  1469. soft_dirty = pmd_soft_dirty(*pmd);
  1470. pmdp_huge_split_prepare(vma, haddr, pmd);
  1471. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1472. pmd_populate(mm, &_pmd, pgtable);
  1473. for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
  1474. pte_t entry, *pte;
  1475. /*
  1476. * Note that NUMA hinting access restrictions are not
  1477. * transferred to avoid any possibility of altering
  1478. * permissions across VMAs.
  1479. */
  1480. if (freeze) {
  1481. swp_entry_t swp_entry;
  1482. swp_entry = make_migration_entry(page + i, write);
  1483. entry = swp_entry_to_pte(swp_entry);
  1484. if (soft_dirty)
  1485. entry = pte_swp_mksoft_dirty(entry);
  1486. } else {
  1487. entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
  1488. entry = maybe_mkwrite(entry, vma);
  1489. if (!write)
  1490. entry = pte_wrprotect(entry);
  1491. if (!young)
  1492. entry = pte_mkold(entry);
  1493. if (soft_dirty)
  1494. entry = pte_mksoft_dirty(entry);
  1495. }
  1496. if (dirty)
  1497. SetPageDirty(page + i);
  1498. pte = pte_offset_map(&_pmd, addr);
  1499. BUG_ON(!pte_none(*pte));
  1500. set_pte_at(mm, addr, pte, entry);
  1501. atomic_inc(&page[i]._mapcount);
  1502. pte_unmap(pte);
  1503. }
  1504. /*
  1505. * Set PG_double_map before dropping compound_mapcount to avoid
  1506. * false-negative page_mapped().
  1507. */
  1508. if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
  1509. for (i = 0; i < HPAGE_PMD_NR; i++)
  1510. atomic_inc(&page[i]._mapcount);
  1511. }
  1512. if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
  1513. /* Last compound_mapcount is gone. */
  1514. __dec_node_page_state(page, NR_ANON_THPS);
  1515. if (TestClearPageDoubleMap(page)) {
  1516. /* No need in mapcount reference anymore */
  1517. for (i = 0; i < HPAGE_PMD_NR; i++)
  1518. atomic_dec(&page[i]._mapcount);
  1519. }
  1520. }
  1521. smp_wmb(); /* make pte visible before pmd */
  1522. /*
  1523. * Up to this point the pmd is present and huge and userland has the
  1524. * whole access to the hugepage during the split (which happens in
  1525. * place). If we overwrite the pmd with the not-huge version pointing
  1526. * to the pte here (which of course we could if all CPUs were bug
  1527. * free), userland could trigger a small page size TLB miss on the
  1528. * small sized TLB while the hugepage TLB entry is still established in
  1529. * the huge TLB. Some CPU doesn't like that.
  1530. * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
  1531. * 383 on page 93. Intel should be safe but is also warns that it's
  1532. * only safe if the permission and cache attributes of the two entries
  1533. * loaded in the two TLB is identical (which should be the case here).
  1534. * But it is generally safer to never allow small and huge TLB entries
  1535. * for the same virtual address to be loaded simultaneously. So instead
  1536. * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
  1537. * current pmd notpresent (atomically because here the pmd_trans_huge
  1538. * and pmd_trans_splitting must remain set at all times on the pmd
  1539. * until the split is complete for this pmd), then we flush the SMP TLB
  1540. * and finally we write the non-huge version of the pmd entry with
  1541. * pmd_populate.
  1542. */
  1543. pmdp_invalidate(vma, haddr, pmd);
  1544. pmd_populate(mm, pmd, pgtable);
  1545. if (freeze) {
  1546. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1547. page_remove_rmap(page + i, false);
  1548. put_page(page + i);
  1549. }
  1550. }
  1551. }
  1552. void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1553. unsigned long address, bool freeze, struct page *page)
  1554. {
  1555. spinlock_t *ptl;
  1556. struct mm_struct *mm = vma->vm_mm;
  1557. unsigned long haddr = address & HPAGE_PMD_MASK;
  1558. mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
  1559. ptl = pmd_lock(mm, pmd);
  1560. /*
  1561. * If caller asks to setup a migration entries, we need a page to check
  1562. * pmd against. Otherwise we can end up replacing wrong page.
  1563. */
  1564. VM_BUG_ON(freeze && !page);
  1565. if (page && page != pmd_page(*pmd))
  1566. goto out;
  1567. if (pmd_trans_huge(*pmd)) {
  1568. page = pmd_page(*pmd);
  1569. if (PageMlocked(page))
  1570. clear_page_mlock(page);
  1571. } else if (!pmd_devmap(*pmd))
  1572. goto out;
  1573. __split_huge_pmd_locked(vma, pmd, haddr, freeze);
  1574. out:
  1575. spin_unlock(ptl);
  1576. mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
  1577. }
  1578. void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
  1579. bool freeze, struct page *page)
  1580. {
  1581. pgd_t *pgd;
  1582. pud_t *pud;
  1583. pmd_t *pmd;
  1584. pgd = pgd_offset(vma->vm_mm, address);
  1585. if (!pgd_present(*pgd))
  1586. return;
  1587. pud = pud_offset(pgd, address);
  1588. if (!pud_present(*pud))
  1589. return;
  1590. pmd = pmd_offset(pud, address);
  1591. __split_huge_pmd(vma, pmd, address, freeze, page);
  1592. }
  1593. void vma_adjust_trans_huge(struct vm_area_struct *vma,
  1594. unsigned long start,
  1595. unsigned long end,
  1596. long adjust_next)
  1597. {
  1598. /*
  1599. * If the new start address isn't hpage aligned and it could
  1600. * previously contain an hugepage: check if we need to split
  1601. * an huge pmd.
  1602. */
  1603. if (start & ~HPAGE_PMD_MASK &&
  1604. (start & HPAGE_PMD_MASK) >= vma->vm_start &&
  1605. (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  1606. split_huge_pmd_address(vma, start, false, NULL);
  1607. /*
  1608. * If the new end address isn't hpage aligned and it could
  1609. * previously contain an hugepage: check if we need to split
  1610. * an huge pmd.
  1611. */
  1612. if (end & ~HPAGE_PMD_MASK &&
  1613. (end & HPAGE_PMD_MASK) >= vma->vm_start &&
  1614. (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  1615. split_huge_pmd_address(vma, end, false, NULL);
  1616. /*
  1617. * If we're also updating the vma->vm_next->vm_start, if the new
  1618. * vm_next->vm_start isn't page aligned and it could previously
  1619. * contain an hugepage: check if we need to split an huge pmd.
  1620. */
  1621. if (adjust_next > 0) {
  1622. struct vm_area_struct *next = vma->vm_next;
  1623. unsigned long nstart = next->vm_start;
  1624. nstart += adjust_next << PAGE_SHIFT;
  1625. if (nstart & ~HPAGE_PMD_MASK &&
  1626. (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
  1627. (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
  1628. split_huge_pmd_address(next, nstart, false, NULL);
  1629. }
  1630. }
  1631. static void unmap_page(struct page *page)
  1632. {
  1633. enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS |
  1634. TTU_RMAP_LOCKED;
  1635. int i, ret;
  1636. VM_BUG_ON_PAGE(!PageHead(page), page);
  1637. if (PageAnon(page))
  1638. ttu_flags |= TTU_MIGRATION;
  1639. /* We only need TTU_SPLIT_HUGE_PMD once */
  1640. ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
  1641. for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
  1642. /* Cut short if the page is unmapped */
  1643. if (page_count(page) == 1)
  1644. return;
  1645. ret = try_to_unmap(page + i, ttu_flags);
  1646. }
  1647. VM_BUG_ON_PAGE(ret, page + i - 1);
  1648. }
  1649. static void remap_page(struct page *page)
  1650. {
  1651. int i;
  1652. for (i = 0; i < HPAGE_PMD_NR; i++)
  1653. remove_migration_ptes(page + i, page + i, true);
  1654. }
  1655. static void __split_huge_page_tail(struct page *head, int tail,
  1656. struct lruvec *lruvec, struct list_head *list)
  1657. {
  1658. struct page *page_tail = head + tail;
  1659. VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
  1660. /*
  1661. * Clone page flags before unfreezing refcount.
  1662. *
  1663. * After successful get_page_unless_zero() might follow flags change,
  1664. * for exmaple lock_page() which set PG_waiters.
  1665. */
  1666. page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  1667. page_tail->flags |= (head->flags &
  1668. ((1L << PG_referenced) |
  1669. (1L << PG_swapbacked) |
  1670. (1L << PG_mlocked) |
  1671. (1L << PG_uptodate) |
  1672. (1L << PG_active) |
  1673. (1L << PG_workingset) |
  1674. (1L << PG_locked) |
  1675. (1L << PG_unevictable) |
  1676. (1L << PG_dirty)));
  1677. /* ->mapping in first tail page is compound_mapcount */
  1678. VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
  1679. page_tail);
  1680. page_tail->mapping = head->mapping;
  1681. page_tail->index = head->index + tail;
  1682. /* Page flags must be visible before we make the page non-compound. */
  1683. smp_wmb();
  1684. /*
  1685. * Clear PageTail before unfreezing page refcount.
  1686. *
  1687. * After successful get_page_unless_zero() might follow put_page()
  1688. * which needs correct compound_head().
  1689. */
  1690. clear_compound_head(page_tail);
  1691. /* Finally unfreeze refcount. Additional reference from page cache. */
  1692. page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
  1693. PageSwapCache(head)));
  1694. if (page_is_young(head))
  1695. set_page_young(page_tail);
  1696. if (page_is_idle(head))
  1697. set_page_idle(page_tail);
  1698. page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
  1699. lru_add_page_tail(head, page_tail, lruvec, list);
  1700. }
  1701. static void __split_huge_page(struct page *page, struct list_head *list,
  1702. pgoff_t end, unsigned long flags)
  1703. {
  1704. struct page *head = compound_head(page);
  1705. struct zone *zone = page_zone(head);
  1706. struct lruvec *lruvec;
  1707. int i;
  1708. lruvec = mem_cgroup_page_lruvec(head, zone->zone_pgdat);
  1709. /* complete memcg works before add pages to LRU */
  1710. mem_cgroup_split_huge_fixup(head);
  1711. for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
  1712. __split_huge_page_tail(head, i, lruvec, list);
  1713. /* Some pages can be beyond i_size: drop them from page cache */
  1714. if (head[i].index >= end) {
  1715. __ClearPageDirty(head + i);
  1716. __delete_from_page_cache(head + i, NULL);
  1717. if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
  1718. shmem_uncharge(head->mapping->host, 1);
  1719. put_page(head + i);
  1720. }
  1721. }
  1722. ClearPageCompound(head);
  1723. split_page_owner(head, HPAGE_PMD_ORDER);
  1724. /* See comment in __split_huge_page_tail() */
  1725. if (PageAnon(head)) {
  1726. page_ref_inc(head);
  1727. } else {
  1728. /* Additional pin to radix tree */
  1729. page_ref_add(head, 2);
  1730. spin_unlock(&head->mapping->tree_lock);
  1731. }
  1732. spin_unlock_irqrestore(zone_lru_lock(page_zone(head)), flags);
  1733. remap_page(head);
  1734. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1735. struct page *subpage = head + i;
  1736. if (subpage == page)
  1737. continue;
  1738. unlock_page(subpage);
  1739. /*
  1740. * Subpages may be freed if there wasn't any mapping
  1741. * like if add_to_swap() is running on a lru page that
  1742. * had its mapping zapped. And freeing these pages
  1743. * requires taking the lru_lock so we do the put_page
  1744. * of the tail pages after the split is complete.
  1745. */
  1746. put_page(subpage);
  1747. }
  1748. }
  1749. int total_mapcount(struct page *page)
  1750. {
  1751. int i, compound, ret;
  1752. VM_BUG_ON_PAGE(PageTail(page), page);
  1753. if (likely(!PageCompound(page)))
  1754. return atomic_read(&page->_mapcount) + 1;
  1755. compound = compound_mapcount(page);
  1756. if (PageHuge(page))
  1757. return compound;
  1758. ret = compound;
  1759. for (i = 0; i < HPAGE_PMD_NR; i++)
  1760. ret += atomic_read(&page[i]._mapcount) + 1;
  1761. /* File pages has compound_mapcount included in _mapcount */
  1762. if (!PageAnon(page))
  1763. return ret - compound * HPAGE_PMD_NR;
  1764. if (PageDoubleMap(page))
  1765. ret -= HPAGE_PMD_NR;
  1766. return ret;
  1767. }
  1768. /*
  1769. * This calculates accurately how many mappings a transparent hugepage
  1770. * has (unlike page_mapcount() which isn't fully accurate). This full
  1771. * accuracy is primarily needed to know if copy-on-write faults can
  1772. * reuse the page and change the mapping to read-write instead of
  1773. * copying them. At the same time this returns the total_mapcount too.
  1774. *
  1775. * The function returns the highest mapcount any one of the subpages
  1776. * has. If the return value is one, even if different processes are
  1777. * mapping different subpages of the transparent hugepage, they can
  1778. * all reuse it, because each process is reusing a different subpage.
  1779. *
  1780. * The total_mapcount is instead counting all virtual mappings of the
  1781. * subpages. If the total_mapcount is equal to "one", it tells the
  1782. * caller all mappings belong to the same "mm" and in turn the
  1783. * anon_vma of the transparent hugepage can become the vma->anon_vma
  1784. * local one as no other process may be mapping any of the subpages.
  1785. *
  1786. * It would be more accurate to replace page_mapcount() with
  1787. * page_trans_huge_mapcount(), however we only use
  1788. * page_trans_huge_mapcount() in the copy-on-write faults where we
  1789. * need full accuracy to avoid breaking page pinning, because
  1790. * page_trans_huge_mapcount() is slower than page_mapcount().
  1791. */
  1792. int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
  1793. {
  1794. int i, ret, _total_mapcount, mapcount;
  1795. /* hugetlbfs shouldn't call it */
  1796. VM_BUG_ON_PAGE(PageHuge(page), page);
  1797. if (likely(!PageTransCompound(page))) {
  1798. mapcount = atomic_read(&page->_mapcount) + 1;
  1799. if (total_mapcount)
  1800. *total_mapcount = mapcount;
  1801. return mapcount;
  1802. }
  1803. page = compound_head(page);
  1804. _total_mapcount = ret = 0;
  1805. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1806. mapcount = atomic_read(&page[i]._mapcount) + 1;
  1807. ret = max(ret, mapcount);
  1808. _total_mapcount += mapcount;
  1809. }
  1810. if (PageDoubleMap(page)) {
  1811. ret -= 1;
  1812. _total_mapcount -= HPAGE_PMD_NR;
  1813. }
  1814. mapcount = compound_mapcount(page);
  1815. ret += mapcount;
  1816. _total_mapcount += mapcount;
  1817. if (total_mapcount)
  1818. *total_mapcount = _total_mapcount;
  1819. return ret;
  1820. }
  1821. /*
  1822. * This function splits huge page into normal pages. @page can point to any
  1823. * subpage of huge page to split. Split doesn't change the position of @page.
  1824. *
  1825. * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
  1826. * The huge page must be locked.
  1827. *
  1828. * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
  1829. *
  1830. * Both head page and tail pages will inherit mapping, flags, and so on from
  1831. * the hugepage.
  1832. *
  1833. * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
  1834. * they are not mapped.
  1835. *
  1836. * Returns 0 if the hugepage is split successfully.
  1837. * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
  1838. * us.
  1839. */
  1840. int split_huge_page_to_list(struct page *page, struct list_head *list)
  1841. {
  1842. struct page *head = compound_head(page);
  1843. struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
  1844. struct anon_vma *anon_vma = NULL;
  1845. struct address_space *mapping = NULL;
  1846. int count, mapcount, extra_pins, ret;
  1847. bool mlocked;
  1848. unsigned long flags;
  1849. pgoff_t end;
  1850. VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
  1851. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1852. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  1853. VM_BUG_ON_PAGE(!PageCompound(page), page);
  1854. if (PageAnon(head)) {
  1855. /*
  1856. * The caller does not necessarily hold an mmap_sem that would
  1857. * prevent the anon_vma disappearing so we first we take a
  1858. * reference to it and then lock the anon_vma for write. This
  1859. * is similar to page_lock_anon_vma_read except the write lock
  1860. * is taken to serialise against parallel split or collapse
  1861. * operations.
  1862. */
  1863. anon_vma = page_get_anon_vma(head);
  1864. if (!anon_vma) {
  1865. ret = -EBUSY;
  1866. goto out;
  1867. }
  1868. extra_pins = 0;
  1869. end = -1;
  1870. mapping = NULL;
  1871. anon_vma_lock_write(anon_vma);
  1872. } else {
  1873. mapping = head->mapping;
  1874. /* Truncated ? */
  1875. if (!mapping) {
  1876. ret = -EBUSY;
  1877. goto out;
  1878. }
  1879. /* Addidional pins from radix tree */
  1880. extra_pins = HPAGE_PMD_NR;
  1881. anon_vma = NULL;
  1882. i_mmap_lock_read(mapping);
  1883. /*
  1884. *__split_huge_page() may need to trim off pages beyond EOF:
  1885. * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
  1886. * which cannot be nested inside the page tree lock. So note
  1887. * end now: i_size itself may be changed at any moment, but
  1888. * head page lock is good enough to serialize the trimming.
  1889. */
  1890. end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
  1891. }
  1892. /*
  1893. * Racy check if we can split the page, before unmap_page() will
  1894. * split PMDs
  1895. */
  1896. if (total_mapcount(head) != page_count(head) - extra_pins - 1) {
  1897. ret = -EBUSY;
  1898. goto out_unlock;
  1899. }
  1900. mlocked = PageMlocked(page);
  1901. unmap_page(head);
  1902. VM_BUG_ON_PAGE(compound_mapcount(head), head);
  1903. /* Make sure the page is not on per-CPU pagevec as it takes pin */
  1904. if (mlocked)
  1905. lru_add_drain();
  1906. /* prevent PageLRU to go away from under us, and freeze lru stats */
  1907. spin_lock_irqsave(zone_lru_lock(page_zone(head)), flags);
  1908. if (mapping) {
  1909. void **pslot;
  1910. spin_lock(&mapping->tree_lock);
  1911. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  1912. page_index(head));
  1913. /*
  1914. * Check if the head page is present in radix tree.
  1915. * We assume all tail are present too, if head is there.
  1916. */
  1917. if (radix_tree_deref_slot_protected(pslot,
  1918. &mapping->tree_lock) != head)
  1919. goto fail;
  1920. }
  1921. /* Prevent deferred_split_scan() touching ->_refcount */
  1922. spin_lock(&pgdata->split_queue_lock);
  1923. count = page_count(head);
  1924. mapcount = total_mapcount(head);
  1925. if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
  1926. if (!list_empty(page_deferred_list(head))) {
  1927. pgdata->split_queue_len--;
  1928. list_del(page_deferred_list(head));
  1929. }
  1930. if (mapping)
  1931. __dec_node_page_state(page, NR_SHMEM_THPS);
  1932. spin_unlock(&pgdata->split_queue_lock);
  1933. __split_huge_page(page, list, end, flags);
  1934. ret = 0;
  1935. } else {
  1936. if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
  1937. pr_alert("total_mapcount: %u, page_count(): %u\n",
  1938. mapcount, count);
  1939. if (PageTail(page))
  1940. dump_page(head, NULL);
  1941. dump_page(page, "total_mapcount(head) > 0");
  1942. BUG();
  1943. }
  1944. spin_unlock(&pgdata->split_queue_lock);
  1945. fail: if (mapping)
  1946. spin_unlock(&mapping->tree_lock);
  1947. spin_unlock_irqrestore(zone_lru_lock(page_zone(head)), flags);
  1948. remap_page(head);
  1949. ret = -EBUSY;
  1950. }
  1951. out_unlock:
  1952. if (anon_vma) {
  1953. anon_vma_unlock_write(anon_vma);
  1954. put_anon_vma(anon_vma);
  1955. }
  1956. if (mapping)
  1957. i_mmap_unlock_read(mapping);
  1958. out:
  1959. count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
  1960. return ret;
  1961. }
  1962. void free_transhuge_page(struct page *page)
  1963. {
  1964. struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
  1965. unsigned long flags;
  1966. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  1967. if (!list_empty(page_deferred_list(page))) {
  1968. pgdata->split_queue_len--;
  1969. list_del(page_deferred_list(page));
  1970. }
  1971. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  1972. free_compound_page(page);
  1973. }
  1974. void deferred_split_huge_page(struct page *page)
  1975. {
  1976. struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
  1977. unsigned long flags;
  1978. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  1979. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  1980. if (list_empty(page_deferred_list(page))) {
  1981. count_vm_event(THP_DEFERRED_SPLIT_PAGE);
  1982. list_add_tail(page_deferred_list(page), &pgdata->split_queue);
  1983. pgdata->split_queue_len++;
  1984. }
  1985. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  1986. }
  1987. static unsigned long deferred_split_count(struct shrinker *shrink,
  1988. struct shrink_control *sc)
  1989. {
  1990. struct pglist_data *pgdata = NODE_DATA(sc->nid);
  1991. return ACCESS_ONCE(pgdata->split_queue_len);
  1992. }
  1993. static unsigned long deferred_split_scan(struct shrinker *shrink,
  1994. struct shrink_control *sc)
  1995. {
  1996. struct pglist_data *pgdata = NODE_DATA(sc->nid);
  1997. unsigned long flags;
  1998. LIST_HEAD(list), *pos, *next;
  1999. struct page *page;
  2000. int split = 0;
  2001. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  2002. /* Take pin on all head pages to avoid freeing them under us */
  2003. list_for_each_safe(pos, next, &pgdata->split_queue) {
  2004. page = list_entry((void *)pos, struct page, mapping);
  2005. page = compound_head(page);
  2006. if (get_page_unless_zero(page)) {
  2007. list_move(page_deferred_list(page), &list);
  2008. } else {
  2009. /* We lost race with put_compound_page() */
  2010. list_del_init(page_deferred_list(page));
  2011. pgdata->split_queue_len--;
  2012. }
  2013. if (!--sc->nr_to_scan)
  2014. break;
  2015. }
  2016. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  2017. list_for_each_safe(pos, next, &list) {
  2018. page = list_entry((void *)pos, struct page, mapping);
  2019. if (!trylock_page(page))
  2020. goto next;
  2021. /* split_huge_page() removes page from list on success */
  2022. if (!split_huge_page(page))
  2023. split++;
  2024. unlock_page(page);
  2025. next:
  2026. put_page(page);
  2027. }
  2028. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  2029. list_splice_tail(&list, &pgdata->split_queue);
  2030. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  2031. /*
  2032. * Stop shrinker if we didn't split any page, but the queue is empty.
  2033. * This can happen if pages were freed under us.
  2034. */
  2035. if (!split && list_empty(&pgdata->split_queue))
  2036. return SHRINK_STOP;
  2037. return split;
  2038. }
  2039. static struct shrinker deferred_split_shrinker = {
  2040. .count_objects = deferred_split_count,
  2041. .scan_objects = deferred_split_scan,
  2042. .seeks = DEFAULT_SEEKS,
  2043. .flags = SHRINKER_NUMA_AWARE,
  2044. };
  2045. #ifdef CONFIG_DEBUG_FS
  2046. static int split_huge_pages_set(void *data, u64 val)
  2047. {
  2048. struct zone *zone;
  2049. struct page *page;
  2050. unsigned long pfn, max_zone_pfn;
  2051. unsigned long total = 0, split = 0;
  2052. if (val != 1)
  2053. return -EINVAL;
  2054. for_each_populated_zone(zone) {
  2055. max_zone_pfn = zone_end_pfn(zone);
  2056. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
  2057. if (!pfn_valid(pfn))
  2058. continue;
  2059. page = pfn_to_page(pfn);
  2060. if (!get_page_unless_zero(page))
  2061. continue;
  2062. if (zone != page_zone(page))
  2063. goto next;
  2064. if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
  2065. goto next;
  2066. total++;
  2067. lock_page(page);
  2068. if (!split_huge_page(page))
  2069. split++;
  2070. unlock_page(page);
  2071. next:
  2072. put_page(page);
  2073. }
  2074. }
  2075. pr_info("%lu of %lu THP split\n", split, total);
  2076. return 0;
  2077. }
  2078. DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
  2079. "%llu\n");
  2080. static int __init split_huge_pages_debugfs(void)
  2081. {
  2082. void *ret;
  2083. ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
  2084. &split_huge_pages_fops);
  2085. if (!ret)
  2086. pr_warn("Failed to create split_huge_pages in debugfs");
  2087. return 0;
  2088. }
  2089. late_initcall(split_huge_pages_debugfs);
  2090. #endif