huge_memory.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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. page_nid = -1;
  1113. if (!get_page_unless_zero(page))
  1114. goto out_unlock;
  1115. spin_unlock(fe->ptl);
  1116. wait_on_page_locked(page);
  1117. put_page(page);
  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)
  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))
  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 (force_flush)
  1316. flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
  1317. if (new_ptl != old_ptl)
  1318. spin_unlock(new_ptl);
  1319. spin_unlock(old_ptl);
  1320. return true;
  1321. }
  1322. return false;
  1323. }
  1324. /*
  1325. * Returns
  1326. * - 0 if PMD could not be locked
  1327. * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
  1328. * - HPAGE_PMD_NR is protections changed and TLB flush necessary
  1329. */
  1330. int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1331. unsigned long addr, pgprot_t newprot, int prot_numa)
  1332. {
  1333. struct mm_struct *mm = vma->vm_mm;
  1334. spinlock_t *ptl;
  1335. pmd_t entry;
  1336. bool preserve_write;
  1337. int ret;
  1338. ptl = __pmd_trans_huge_lock(pmd, vma);
  1339. if (!ptl)
  1340. return 0;
  1341. preserve_write = prot_numa && pmd_write(*pmd);
  1342. ret = 1;
  1343. /*
  1344. * Avoid trapping faults against the zero page. The read-only
  1345. * data is likely to be read-cached on the local CPU and
  1346. * local/remote hits to the zero page are not interesting.
  1347. */
  1348. if (prot_numa && is_huge_zero_pmd(*pmd))
  1349. goto unlock;
  1350. if (prot_numa && pmd_protnone(*pmd))
  1351. goto unlock;
  1352. /*
  1353. * In case prot_numa, we are under down_read(mmap_sem). It's critical
  1354. * to not clear pmd intermittently to avoid race with MADV_DONTNEED
  1355. * which is also under down_read(mmap_sem):
  1356. *
  1357. * CPU0: CPU1:
  1358. * change_huge_pmd(prot_numa=1)
  1359. * pmdp_huge_get_and_clear_notify()
  1360. * madvise_dontneed()
  1361. * zap_pmd_range()
  1362. * pmd_trans_huge(*pmd) == 0 (without ptl)
  1363. * // skip the pmd
  1364. * set_pmd_at();
  1365. * // pmd is re-established
  1366. *
  1367. * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
  1368. * which may break userspace.
  1369. *
  1370. * pmdp_invalidate() is required to make sure we don't miss
  1371. * dirty/young flags set by hardware.
  1372. */
  1373. entry = *pmd;
  1374. pmdp_invalidate(vma, addr, pmd);
  1375. /*
  1376. * Recover dirty/young flags. It relies on pmdp_invalidate to not
  1377. * corrupt them.
  1378. */
  1379. if (pmd_dirty(*pmd))
  1380. entry = pmd_mkdirty(entry);
  1381. if (pmd_young(*pmd))
  1382. entry = pmd_mkyoung(entry);
  1383. entry = pmd_modify(entry, newprot);
  1384. if (preserve_write)
  1385. entry = pmd_mkwrite(entry);
  1386. ret = HPAGE_PMD_NR;
  1387. set_pmd_at(mm, addr, pmd, entry);
  1388. BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
  1389. unlock:
  1390. spin_unlock(ptl);
  1391. return ret;
  1392. }
  1393. /*
  1394. * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
  1395. *
  1396. * Note that if it returns page table lock pointer, this routine returns without
  1397. * unlocking page table lock. So callers must unlock it.
  1398. */
  1399. spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
  1400. {
  1401. spinlock_t *ptl;
  1402. ptl = pmd_lock(vma->vm_mm, pmd);
  1403. if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
  1404. return ptl;
  1405. spin_unlock(ptl);
  1406. return NULL;
  1407. }
  1408. static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
  1409. unsigned long haddr, pmd_t *pmd)
  1410. {
  1411. struct mm_struct *mm = vma->vm_mm;
  1412. pgtable_t pgtable;
  1413. pmd_t _pmd;
  1414. int i;
  1415. /* leave pmd empty until pte is filled */
  1416. pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  1417. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1418. pmd_populate(mm, &_pmd, pgtable);
  1419. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  1420. pte_t *pte, entry;
  1421. entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
  1422. entry = pte_mkspecial(entry);
  1423. pte = pte_offset_map(&_pmd, haddr);
  1424. VM_BUG_ON(!pte_none(*pte));
  1425. set_pte_at(mm, haddr, pte, entry);
  1426. pte_unmap(pte);
  1427. }
  1428. smp_wmb(); /* make pte visible before pmd */
  1429. pmd_populate(mm, pmd, pgtable);
  1430. }
  1431. static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
  1432. unsigned long haddr, bool freeze)
  1433. {
  1434. struct mm_struct *mm = vma->vm_mm;
  1435. struct page *page;
  1436. pgtable_t pgtable;
  1437. pmd_t _pmd;
  1438. bool young, write, dirty, soft_dirty;
  1439. unsigned long addr;
  1440. int i;
  1441. VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
  1442. VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
  1443. VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
  1444. VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
  1445. count_vm_event(THP_SPLIT_PMD);
  1446. if (!vma_is_anonymous(vma)) {
  1447. _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  1448. if (vma_is_dax(vma))
  1449. return;
  1450. page = pmd_page(_pmd);
  1451. if (!PageDirty(page) && pmd_dirty(_pmd))
  1452. set_page_dirty(page);
  1453. if (!PageReferenced(page) && pmd_young(_pmd))
  1454. SetPageReferenced(page);
  1455. page_remove_rmap(page, true);
  1456. put_page(page);
  1457. add_mm_counter(mm, MM_FILEPAGES, -HPAGE_PMD_NR);
  1458. return;
  1459. } else if (is_huge_zero_pmd(*pmd)) {
  1460. return __split_huge_zero_page_pmd(vma, haddr, pmd);
  1461. }
  1462. page = pmd_page(*pmd);
  1463. VM_BUG_ON_PAGE(!page_count(page), page);
  1464. page_ref_add(page, HPAGE_PMD_NR - 1);
  1465. write = pmd_write(*pmd);
  1466. young = pmd_young(*pmd);
  1467. dirty = pmd_dirty(*pmd);
  1468. soft_dirty = pmd_soft_dirty(*pmd);
  1469. pmdp_huge_split_prepare(vma, haddr, pmd);
  1470. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1471. pmd_populate(mm, &_pmd, pgtable);
  1472. for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
  1473. pte_t entry, *pte;
  1474. /*
  1475. * Note that NUMA hinting access restrictions are not
  1476. * transferred to avoid any possibility of altering
  1477. * permissions across VMAs.
  1478. */
  1479. if (freeze) {
  1480. swp_entry_t swp_entry;
  1481. swp_entry = make_migration_entry(page + i, write);
  1482. entry = swp_entry_to_pte(swp_entry);
  1483. if (soft_dirty)
  1484. entry = pte_swp_mksoft_dirty(entry);
  1485. } else {
  1486. entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
  1487. entry = maybe_mkwrite(entry, vma);
  1488. if (!write)
  1489. entry = pte_wrprotect(entry);
  1490. if (!young)
  1491. entry = pte_mkold(entry);
  1492. if (soft_dirty)
  1493. entry = pte_mksoft_dirty(entry);
  1494. }
  1495. if (dirty)
  1496. SetPageDirty(page + i);
  1497. pte = pte_offset_map(&_pmd, addr);
  1498. BUG_ON(!pte_none(*pte));
  1499. set_pte_at(mm, addr, pte, entry);
  1500. atomic_inc(&page[i]._mapcount);
  1501. pte_unmap(pte);
  1502. }
  1503. /*
  1504. * Set PG_double_map before dropping compound_mapcount to avoid
  1505. * false-negative page_mapped().
  1506. */
  1507. if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
  1508. for (i = 0; i < HPAGE_PMD_NR; i++)
  1509. atomic_inc(&page[i]._mapcount);
  1510. }
  1511. if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
  1512. /* Last compound_mapcount is gone. */
  1513. __dec_node_page_state(page, NR_ANON_THPS);
  1514. if (TestClearPageDoubleMap(page)) {
  1515. /* No need in mapcount reference anymore */
  1516. for (i = 0; i < HPAGE_PMD_NR; i++)
  1517. atomic_dec(&page[i]._mapcount);
  1518. }
  1519. }
  1520. smp_wmb(); /* make pte visible before pmd */
  1521. /*
  1522. * Up to this point the pmd is present and huge and userland has the
  1523. * whole access to the hugepage during the split (which happens in
  1524. * place). If we overwrite the pmd with the not-huge version pointing
  1525. * to the pte here (which of course we could if all CPUs were bug
  1526. * free), userland could trigger a small page size TLB miss on the
  1527. * small sized TLB while the hugepage TLB entry is still established in
  1528. * the huge TLB. Some CPU doesn't like that.
  1529. * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
  1530. * 383 on page 93. Intel should be safe but is also warns that it's
  1531. * only safe if the permission and cache attributes of the two entries
  1532. * loaded in the two TLB is identical (which should be the case here).
  1533. * But it is generally safer to never allow small and huge TLB entries
  1534. * for the same virtual address to be loaded simultaneously. So instead
  1535. * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
  1536. * current pmd notpresent (atomically because here the pmd_trans_huge
  1537. * and pmd_trans_splitting must remain set at all times on the pmd
  1538. * until the split is complete for this pmd), then we flush the SMP TLB
  1539. * and finally we write the non-huge version of the pmd entry with
  1540. * pmd_populate.
  1541. */
  1542. pmdp_invalidate(vma, haddr, pmd);
  1543. pmd_populate(mm, pmd, pgtable);
  1544. if (freeze) {
  1545. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1546. page_remove_rmap(page + i, false);
  1547. put_page(page + i);
  1548. }
  1549. }
  1550. }
  1551. void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1552. unsigned long address, bool freeze, struct page *page)
  1553. {
  1554. spinlock_t *ptl;
  1555. struct mm_struct *mm = vma->vm_mm;
  1556. unsigned long haddr = address & HPAGE_PMD_MASK;
  1557. mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
  1558. ptl = pmd_lock(mm, pmd);
  1559. /*
  1560. * If caller asks to setup a migration entries, we need a page to check
  1561. * pmd against. Otherwise we can end up replacing wrong page.
  1562. */
  1563. VM_BUG_ON(freeze && !page);
  1564. if (page && page != pmd_page(*pmd))
  1565. goto out;
  1566. if (pmd_trans_huge(*pmd)) {
  1567. page = pmd_page(*pmd);
  1568. if (PageMlocked(page))
  1569. clear_page_mlock(page);
  1570. } else if (!pmd_devmap(*pmd))
  1571. goto out;
  1572. __split_huge_pmd_locked(vma, pmd, haddr, freeze);
  1573. out:
  1574. spin_unlock(ptl);
  1575. mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
  1576. }
  1577. void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
  1578. bool freeze, struct page *page)
  1579. {
  1580. pgd_t *pgd;
  1581. pud_t *pud;
  1582. pmd_t *pmd;
  1583. pgd = pgd_offset(vma->vm_mm, address);
  1584. if (!pgd_present(*pgd))
  1585. return;
  1586. pud = pud_offset(pgd, address);
  1587. if (!pud_present(*pud))
  1588. return;
  1589. pmd = pmd_offset(pud, address);
  1590. __split_huge_pmd(vma, pmd, address, freeze, page);
  1591. }
  1592. void vma_adjust_trans_huge(struct vm_area_struct *vma,
  1593. unsigned long start,
  1594. unsigned long end,
  1595. long adjust_next)
  1596. {
  1597. /*
  1598. * If the new start address isn't hpage aligned and it could
  1599. * previously contain an hugepage: check if we need to split
  1600. * an huge pmd.
  1601. */
  1602. if (start & ~HPAGE_PMD_MASK &&
  1603. (start & HPAGE_PMD_MASK) >= vma->vm_start &&
  1604. (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  1605. split_huge_pmd_address(vma, start, false, NULL);
  1606. /*
  1607. * If the new end address isn't hpage aligned and it could
  1608. * previously contain an hugepage: check if we need to split
  1609. * an huge pmd.
  1610. */
  1611. if (end & ~HPAGE_PMD_MASK &&
  1612. (end & HPAGE_PMD_MASK) >= vma->vm_start &&
  1613. (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  1614. split_huge_pmd_address(vma, end, false, NULL);
  1615. /*
  1616. * If we're also updating the vma->vm_next->vm_start, if the new
  1617. * vm_next->vm_start isn't page aligned and it could previously
  1618. * contain an hugepage: check if we need to split an huge pmd.
  1619. */
  1620. if (adjust_next > 0) {
  1621. struct vm_area_struct *next = vma->vm_next;
  1622. unsigned long nstart = next->vm_start;
  1623. nstart += adjust_next << PAGE_SHIFT;
  1624. if (nstart & ~HPAGE_PMD_MASK &&
  1625. (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
  1626. (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
  1627. split_huge_pmd_address(next, nstart, false, NULL);
  1628. }
  1629. }
  1630. static void unmap_page(struct page *page)
  1631. {
  1632. enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS |
  1633. TTU_RMAP_LOCKED;
  1634. int i, ret;
  1635. VM_BUG_ON_PAGE(!PageHead(page), page);
  1636. if (PageAnon(page))
  1637. ttu_flags |= TTU_MIGRATION;
  1638. /* We only need TTU_SPLIT_HUGE_PMD once */
  1639. ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
  1640. for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
  1641. /* Cut short if the page is unmapped */
  1642. if (page_count(page) == 1)
  1643. return;
  1644. ret = try_to_unmap(page + i, ttu_flags);
  1645. }
  1646. VM_BUG_ON_PAGE(ret, page + i - 1);
  1647. }
  1648. static void remap_page(struct page *page)
  1649. {
  1650. int i;
  1651. for (i = 0; i < HPAGE_PMD_NR; i++)
  1652. remove_migration_ptes(page + i, page + i, true);
  1653. }
  1654. static void __split_huge_page_tail(struct page *head, int tail,
  1655. struct lruvec *lruvec, struct list_head *list)
  1656. {
  1657. struct page *page_tail = head + tail;
  1658. VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
  1659. /*
  1660. * Clone page flags before unfreezing refcount.
  1661. *
  1662. * After successful get_page_unless_zero() might follow flags change,
  1663. * for exmaple lock_page() which set PG_waiters.
  1664. */
  1665. page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  1666. page_tail->flags |= (head->flags &
  1667. ((1L << PG_referenced) |
  1668. (1L << PG_swapbacked) |
  1669. (1L << PG_mlocked) |
  1670. (1L << PG_uptodate) |
  1671. (1L << PG_active) |
  1672. (1L << PG_locked) |
  1673. (1L << PG_unevictable) |
  1674. (1L << PG_dirty)));
  1675. /* ->mapping in first tail page is compound_mapcount */
  1676. VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
  1677. page_tail);
  1678. page_tail->mapping = head->mapping;
  1679. page_tail->index = head->index + tail;
  1680. /* Page flags must be visible before we make the page non-compound. */
  1681. smp_wmb();
  1682. /*
  1683. * Clear PageTail before unfreezing page refcount.
  1684. *
  1685. * After successful get_page_unless_zero() might follow put_page()
  1686. * which needs correct compound_head().
  1687. */
  1688. clear_compound_head(page_tail);
  1689. /* Finally unfreeze refcount. Additional reference from page cache. */
  1690. page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
  1691. PageSwapCache(head)));
  1692. if (page_is_young(head))
  1693. set_page_young(page_tail);
  1694. if (page_is_idle(head))
  1695. set_page_idle(page_tail);
  1696. page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
  1697. lru_add_page_tail(head, page_tail, lruvec, list);
  1698. }
  1699. static void __split_huge_page(struct page *page, struct list_head *list,
  1700. pgoff_t end, unsigned long flags)
  1701. {
  1702. struct page *head = compound_head(page);
  1703. struct zone *zone = page_zone(head);
  1704. struct lruvec *lruvec;
  1705. int i;
  1706. lruvec = mem_cgroup_page_lruvec(head, zone->zone_pgdat);
  1707. /* complete memcg works before add pages to LRU */
  1708. mem_cgroup_split_huge_fixup(head);
  1709. for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
  1710. __split_huge_page_tail(head, i, lruvec, list);
  1711. /* Some pages can be beyond i_size: drop them from page cache */
  1712. if (head[i].index >= end) {
  1713. __ClearPageDirty(head + i);
  1714. __delete_from_page_cache(head + i, NULL);
  1715. if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
  1716. shmem_uncharge(head->mapping->host, 1);
  1717. put_page(head + i);
  1718. }
  1719. }
  1720. ClearPageCompound(head);
  1721. /* See comment in __split_huge_page_tail() */
  1722. if (PageAnon(head)) {
  1723. page_ref_inc(head);
  1724. } else {
  1725. /* Additional pin to radix tree */
  1726. page_ref_add(head, 2);
  1727. spin_unlock(&head->mapping->tree_lock);
  1728. }
  1729. spin_unlock_irqrestore(zone_lru_lock(page_zone(head)), flags);
  1730. remap_page(head);
  1731. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1732. struct page *subpage = head + i;
  1733. if (subpage == page)
  1734. continue;
  1735. unlock_page(subpage);
  1736. /*
  1737. * Subpages may be freed if there wasn't any mapping
  1738. * like if add_to_swap() is running on a lru page that
  1739. * had its mapping zapped. And freeing these pages
  1740. * requires taking the lru_lock so we do the put_page
  1741. * of the tail pages after the split is complete.
  1742. */
  1743. put_page(subpage);
  1744. }
  1745. }
  1746. int total_mapcount(struct page *page)
  1747. {
  1748. int i, compound, ret;
  1749. VM_BUG_ON_PAGE(PageTail(page), page);
  1750. if (likely(!PageCompound(page)))
  1751. return atomic_read(&page->_mapcount) + 1;
  1752. compound = compound_mapcount(page);
  1753. if (PageHuge(page))
  1754. return compound;
  1755. ret = compound;
  1756. for (i = 0; i < HPAGE_PMD_NR; i++)
  1757. ret += atomic_read(&page[i]._mapcount) + 1;
  1758. /* File pages has compound_mapcount included in _mapcount */
  1759. if (!PageAnon(page))
  1760. return ret - compound * HPAGE_PMD_NR;
  1761. if (PageDoubleMap(page))
  1762. ret -= HPAGE_PMD_NR;
  1763. return ret;
  1764. }
  1765. /*
  1766. * This calculates accurately how many mappings a transparent hugepage
  1767. * has (unlike page_mapcount() which isn't fully accurate). This full
  1768. * accuracy is primarily needed to know if copy-on-write faults can
  1769. * reuse the page and change the mapping to read-write instead of
  1770. * copying them. At the same time this returns the total_mapcount too.
  1771. *
  1772. * The function returns the highest mapcount any one of the subpages
  1773. * has. If the return value is one, even if different processes are
  1774. * mapping different subpages of the transparent hugepage, they can
  1775. * all reuse it, because each process is reusing a different subpage.
  1776. *
  1777. * The total_mapcount is instead counting all virtual mappings of the
  1778. * subpages. If the total_mapcount is equal to "one", it tells the
  1779. * caller all mappings belong to the same "mm" and in turn the
  1780. * anon_vma of the transparent hugepage can become the vma->anon_vma
  1781. * local one as no other process may be mapping any of the subpages.
  1782. *
  1783. * It would be more accurate to replace page_mapcount() with
  1784. * page_trans_huge_mapcount(), however we only use
  1785. * page_trans_huge_mapcount() in the copy-on-write faults where we
  1786. * need full accuracy to avoid breaking page pinning, because
  1787. * page_trans_huge_mapcount() is slower than page_mapcount().
  1788. */
  1789. int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
  1790. {
  1791. int i, ret, _total_mapcount, mapcount;
  1792. /* hugetlbfs shouldn't call it */
  1793. VM_BUG_ON_PAGE(PageHuge(page), page);
  1794. if (likely(!PageTransCompound(page))) {
  1795. mapcount = atomic_read(&page->_mapcount) + 1;
  1796. if (total_mapcount)
  1797. *total_mapcount = mapcount;
  1798. return mapcount;
  1799. }
  1800. page = compound_head(page);
  1801. _total_mapcount = ret = 0;
  1802. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1803. mapcount = atomic_read(&page[i]._mapcount) + 1;
  1804. ret = max(ret, mapcount);
  1805. _total_mapcount += mapcount;
  1806. }
  1807. if (PageDoubleMap(page)) {
  1808. ret -= 1;
  1809. _total_mapcount -= HPAGE_PMD_NR;
  1810. }
  1811. mapcount = compound_mapcount(page);
  1812. ret += mapcount;
  1813. _total_mapcount += mapcount;
  1814. if (total_mapcount)
  1815. *total_mapcount = _total_mapcount;
  1816. return ret;
  1817. }
  1818. /*
  1819. * This function splits huge page into normal pages. @page can point to any
  1820. * subpage of huge page to split. Split doesn't change the position of @page.
  1821. *
  1822. * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
  1823. * The huge page must be locked.
  1824. *
  1825. * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
  1826. *
  1827. * Both head page and tail pages will inherit mapping, flags, and so on from
  1828. * the hugepage.
  1829. *
  1830. * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
  1831. * they are not mapped.
  1832. *
  1833. * Returns 0 if the hugepage is split successfully.
  1834. * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
  1835. * us.
  1836. */
  1837. int split_huge_page_to_list(struct page *page, struct list_head *list)
  1838. {
  1839. struct page *head = compound_head(page);
  1840. struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
  1841. struct anon_vma *anon_vma = NULL;
  1842. struct address_space *mapping = NULL;
  1843. int count, mapcount, extra_pins, ret;
  1844. bool mlocked;
  1845. unsigned long flags;
  1846. pgoff_t end;
  1847. VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
  1848. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1849. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  1850. VM_BUG_ON_PAGE(!PageCompound(page), page);
  1851. if (PageAnon(head)) {
  1852. /*
  1853. * The caller does not necessarily hold an mmap_sem that would
  1854. * prevent the anon_vma disappearing so we first we take a
  1855. * reference to it and then lock the anon_vma for write. This
  1856. * is similar to page_lock_anon_vma_read except the write lock
  1857. * is taken to serialise against parallel split or collapse
  1858. * operations.
  1859. */
  1860. anon_vma = page_get_anon_vma(head);
  1861. if (!anon_vma) {
  1862. ret = -EBUSY;
  1863. goto out;
  1864. }
  1865. extra_pins = 0;
  1866. end = -1;
  1867. mapping = NULL;
  1868. anon_vma_lock_write(anon_vma);
  1869. } else {
  1870. mapping = head->mapping;
  1871. /* Truncated ? */
  1872. if (!mapping) {
  1873. ret = -EBUSY;
  1874. goto out;
  1875. }
  1876. /* Addidional pins from radix tree */
  1877. extra_pins = HPAGE_PMD_NR;
  1878. anon_vma = NULL;
  1879. i_mmap_lock_read(mapping);
  1880. /*
  1881. *__split_huge_page() may need to trim off pages beyond EOF:
  1882. * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
  1883. * which cannot be nested inside the page tree lock. So note
  1884. * end now: i_size itself may be changed at any moment, but
  1885. * head page lock is good enough to serialize the trimming.
  1886. */
  1887. end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
  1888. }
  1889. /*
  1890. * Racy check if we can split the page, before unmap_page() will
  1891. * split PMDs
  1892. */
  1893. if (total_mapcount(head) != page_count(head) - extra_pins - 1) {
  1894. ret = -EBUSY;
  1895. goto out_unlock;
  1896. }
  1897. mlocked = PageMlocked(page);
  1898. unmap_page(head);
  1899. VM_BUG_ON_PAGE(compound_mapcount(head), head);
  1900. /* Make sure the page is not on per-CPU pagevec as it takes pin */
  1901. if (mlocked)
  1902. lru_add_drain();
  1903. /* prevent PageLRU to go away from under us, and freeze lru stats */
  1904. spin_lock_irqsave(zone_lru_lock(page_zone(head)), flags);
  1905. if (mapping) {
  1906. void **pslot;
  1907. spin_lock(&mapping->tree_lock);
  1908. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  1909. page_index(head));
  1910. /*
  1911. * Check if the head page is present in radix tree.
  1912. * We assume all tail are present too, if head is there.
  1913. */
  1914. if (radix_tree_deref_slot_protected(pslot,
  1915. &mapping->tree_lock) != head)
  1916. goto fail;
  1917. }
  1918. /* Prevent deferred_split_scan() touching ->_refcount */
  1919. spin_lock(&pgdata->split_queue_lock);
  1920. count = page_count(head);
  1921. mapcount = total_mapcount(head);
  1922. if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
  1923. if (!list_empty(page_deferred_list(head))) {
  1924. pgdata->split_queue_len--;
  1925. list_del(page_deferred_list(head));
  1926. }
  1927. if (mapping)
  1928. __dec_node_page_state(page, NR_SHMEM_THPS);
  1929. spin_unlock(&pgdata->split_queue_lock);
  1930. __split_huge_page(page, list, end, flags);
  1931. ret = 0;
  1932. } else {
  1933. if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
  1934. pr_alert("total_mapcount: %u, page_count(): %u\n",
  1935. mapcount, count);
  1936. if (PageTail(page))
  1937. dump_page(head, NULL);
  1938. dump_page(page, "total_mapcount(head) > 0");
  1939. BUG();
  1940. }
  1941. spin_unlock(&pgdata->split_queue_lock);
  1942. fail: if (mapping)
  1943. spin_unlock(&mapping->tree_lock);
  1944. spin_unlock_irqrestore(zone_lru_lock(page_zone(head)), flags);
  1945. remap_page(head);
  1946. ret = -EBUSY;
  1947. }
  1948. out_unlock:
  1949. if (anon_vma) {
  1950. anon_vma_unlock_write(anon_vma);
  1951. put_anon_vma(anon_vma);
  1952. }
  1953. if (mapping)
  1954. i_mmap_unlock_read(mapping);
  1955. out:
  1956. count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
  1957. return ret;
  1958. }
  1959. void free_transhuge_page(struct page *page)
  1960. {
  1961. struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
  1962. unsigned long flags;
  1963. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  1964. if (!list_empty(page_deferred_list(page))) {
  1965. pgdata->split_queue_len--;
  1966. list_del(page_deferred_list(page));
  1967. }
  1968. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  1969. free_compound_page(page);
  1970. }
  1971. void deferred_split_huge_page(struct page *page)
  1972. {
  1973. struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
  1974. unsigned long flags;
  1975. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  1976. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  1977. if (list_empty(page_deferred_list(page))) {
  1978. count_vm_event(THP_DEFERRED_SPLIT_PAGE);
  1979. list_add_tail(page_deferred_list(page), &pgdata->split_queue);
  1980. pgdata->split_queue_len++;
  1981. }
  1982. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  1983. }
  1984. static unsigned long deferred_split_count(struct shrinker *shrink,
  1985. struct shrink_control *sc)
  1986. {
  1987. struct pglist_data *pgdata = NODE_DATA(sc->nid);
  1988. return ACCESS_ONCE(pgdata->split_queue_len);
  1989. }
  1990. static unsigned long deferred_split_scan(struct shrinker *shrink,
  1991. struct shrink_control *sc)
  1992. {
  1993. struct pglist_data *pgdata = NODE_DATA(sc->nid);
  1994. unsigned long flags;
  1995. LIST_HEAD(list), *pos, *next;
  1996. struct page *page;
  1997. int split = 0;
  1998. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  1999. /* Take pin on all head pages to avoid freeing them under us */
  2000. list_for_each_safe(pos, next, &pgdata->split_queue) {
  2001. page = list_entry((void *)pos, struct page, mapping);
  2002. page = compound_head(page);
  2003. if (get_page_unless_zero(page)) {
  2004. list_move(page_deferred_list(page), &list);
  2005. } else {
  2006. /* We lost race with put_compound_page() */
  2007. list_del_init(page_deferred_list(page));
  2008. pgdata->split_queue_len--;
  2009. }
  2010. if (!--sc->nr_to_scan)
  2011. break;
  2012. }
  2013. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  2014. list_for_each_safe(pos, next, &list) {
  2015. page = list_entry((void *)pos, struct page, mapping);
  2016. if (!trylock_page(page))
  2017. goto next;
  2018. /* split_huge_page() removes page from list on success */
  2019. if (!split_huge_page(page))
  2020. split++;
  2021. unlock_page(page);
  2022. next:
  2023. put_page(page);
  2024. }
  2025. spin_lock_irqsave(&pgdata->split_queue_lock, flags);
  2026. list_splice_tail(&list, &pgdata->split_queue);
  2027. spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
  2028. /*
  2029. * Stop shrinker if we didn't split any page, but the queue is empty.
  2030. * This can happen if pages were freed under us.
  2031. */
  2032. if (!split && list_empty(&pgdata->split_queue))
  2033. return SHRINK_STOP;
  2034. return split;
  2035. }
  2036. static struct shrinker deferred_split_shrinker = {
  2037. .count_objects = deferred_split_count,
  2038. .scan_objects = deferred_split_scan,
  2039. .seeks = DEFAULT_SEEKS,
  2040. .flags = SHRINKER_NUMA_AWARE,
  2041. };
  2042. #ifdef CONFIG_DEBUG_FS
  2043. static int split_huge_pages_set(void *data, u64 val)
  2044. {
  2045. struct zone *zone;
  2046. struct page *page;
  2047. unsigned long pfn, max_zone_pfn;
  2048. unsigned long total = 0, split = 0;
  2049. if (val != 1)
  2050. return -EINVAL;
  2051. for_each_populated_zone(zone) {
  2052. max_zone_pfn = zone_end_pfn(zone);
  2053. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
  2054. if (!pfn_valid(pfn))
  2055. continue;
  2056. page = pfn_to_page(pfn);
  2057. if (!get_page_unless_zero(page))
  2058. continue;
  2059. if (zone != page_zone(page))
  2060. goto next;
  2061. if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
  2062. goto next;
  2063. total++;
  2064. lock_page(page);
  2065. if (!split_huge_page(page))
  2066. split++;
  2067. unlock_page(page);
  2068. next:
  2069. put_page(page);
  2070. }
  2071. }
  2072. pr_info("%lu of %lu THP split\n", split, total);
  2073. return 0;
  2074. }
  2075. DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
  2076. "%llu\n");
  2077. static int __init split_huge_pages_debugfs(void)
  2078. {
  2079. void *ret;
  2080. ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
  2081. &split_huge_pages_fops);
  2082. if (!ret)
  2083. pr_warn("Failed to create split_huge_pages in debugfs");
  2084. return 0;
  2085. }
  2086. late_initcall(split_huge_pages_debugfs);
  2087. #endif