huge_memory.c 65 KB

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