huge_memory.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2009 Red Hat, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/mm.h>
  7. #include <linux/sched.h>
  8. #include <linux/sched/coredump.h>
  9. #include <linux/sched/numa_balancing.h>
  10. #include <linux/highmem.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/rmap.h>
  14. #include <linux/swap.h>
  15. #include <linux/shrinker.h>
  16. #include <linux/mm_inline.h>
  17. #include <linux/swapops.h>
  18. #include <linux/dax.h>
  19. #include <linux/khugepaged.h>
  20. #include <linux/freezer.h>
  21. #include <linux/pfn_t.h>
  22. #include <linux/mman.h>
  23. #include <linux/memremap.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/migrate.h>
  27. #include <linux/hashtable.h>
  28. #include <linux/userfaultfd_k.h>
  29. #include <linux/page_idle.h>
  30. #include <linux/shmem_fs.h>
  31. #include <linux/oom.h>
  32. #include <linux/numa.h>
  33. #include <linux/page_owner.h>
  34. #include <asm/tlb.h>
  35. #include <asm/pgalloc.h>
  36. #include "internal.h"
  37. /*
  38. * By default, transparent hugepage support is disabled in order to avoid
  39. * risking an increased memory footprint for applications that are not
  40. * guaranteed to benefit from it. When transparent hugepage support is
  41. * enabled, it is for all mappings, and khugepaged scans all mappings.
  42. * Defrag is invoked by khugepaged hugepage allocations and by page faults
  43. * for all hugepage allocations.
  44. */
  45. unsigned long transparent_hugepage_flags __read_mostly =
  46. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
  47. (1<<TRANSPARENT_HUGEPAGE_FLAG)|
  48. #endif
  49. #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
  50. (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
  51. #endif
  52. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
  53. (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
  54. (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  55. static struct shrinker deferred_split_shrinker;
  56. static atomic_t huge_zero_refcount;
  57. struct page *huge_zero_page __read_mostly;
  58. unsigned long huge_zero_pfn __read_mostly = ~0UL;
  59. static inline bool file_thp_enabled(struct vm_area_struct *vma)
  60. {
  61. return transhuge_vma_enabled(vma, vma->vm_flags) && vma->vm_file &&
  62. !inode_is_open_for_write(vma->vm_file->f_inode) &&
  63. (vma->vm_flags & VM_EXEC);
  64. }
  65. bool transparent_hugepage_active(struct vm_area_struct *vma)
  66. {
  67. /* The addr is used to check if the vma size fits */
  68. unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE;
  69. if (!transhuge_vma_suitable(vma, addr))
  70. return false;
  71. if (vma_is_anonymous(vma))
  72. return __transparent_hugepage_enabled(vma);
  73. if (vma_is_shmem(vma))
  74. return shmem_huge_enabled(vma);
  75. if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS))
  76. return file_thp_enabled(vma);
  77. return false;
  78. }
  79. static struct page *get_huge_zero_page(void)
  80. {
  81. struct page *zero_page;
  82. retry:
  83. if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
  84. return READ_ONCE(huge_zero_page);
  85. zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
  86. HPAGE_PMD_ORDER);
  87. if (!zero_page) {
  88. count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
  89. return NULL;
  90. }
  91. count_vm_event(THP_ZERO_PAGE_ALLOC);
  92. preempt_disable();
  93. if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
  94. preempt_enable();
  95. __free_pages(zero_page, compound_order(zero_page));
  96. goto retry;
  97. }
  98. WRITE_ONCE(huge_zero_pfn, page_to_pfn(zero_page));
  99. /* We take additional reference here. It will be put back by shrinker */
  100. atomic_set(&huge_zero_refcount, 2);
  101. preempt_enable();
  102. return READ_ONCE(huge_zero_page);
  103. }
  104. static void put_huge_zero_page(void)
  105. {
  106. /*
  107. * Counter should never go to zero here. Only shrinker can put
  108. * last reference.
  109. */
  110. BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
  111. }
  112. struct page *mm_get_huge_zero_page(struct mm_struct *mm)
  113. {
  114. if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
  115. return READ_ONCE(huge_zero_page);
  116. if (!get_huge_zero_page())
  117. return NULL;
  118. if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
  119. put_huge_zero_page();
  120. return READ_ONCE(huge_zero_page);
  121. }
  122. void mm_put_huge_zero_page(struct mm_struct *mm)
  123. {
  124. if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
  125. put_huge_zero_page();
  126. }
  127. static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
  128. struct shrink_control *sc)
  129. {
  130. /* we can free zero page only if last reference remains */
  131. return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
  132. }
  133. static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
  134. struct shrink_control *sc)
  135. {
  136. if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
  137. struct page *zero_page = xchg(&huge_zero_page, NULL);
  138. BUG_ON(zero_page == NULL);
  139. WRITE_ONCE(huge_zero_pfn, ~0UL);
  140. __free_pages(zero_page, compound_order(zero_page));
  141. return HPAGE_PMD_NR;
  142. }
  143. return 0;
  144. }
  145. static struct shrinker huge_zero_page_shrinker = {
  146. .count_objects = shrink_huge_zero_page_count,
  147. .scan_objects = shrink_huge_zero_page_scan,
  148. .seeks = DEFAULT_SEEKS,
  149. };
  150. #ifdef CONFIG_SYSFS
  151. static ssize_t enabled_show(struct kobject *kobj,
  152. struct kobj_attribute *attr, char *buf)
  153. {
  154. if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
  155. return sprintf(buf, "[always] madvise never\n");
  156. else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
  157. return sprintf(buf, "always [madvise] never\n");
  158. else
  159. return sprintf(buf, "always madvise [never]\n");
  160. }
  161. static ssize_t enabled_store(struct kobject *kobj,
  162. struct kobj_attribute *attr,
  163. const char *buf, size_t count)
  164. {
  165. ssize_t ret = count;
  166. if (sysfs_streq(buf, "always")) {
  167. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
  168. set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
  169. } else if (sysfs_streq(buf, "madvise")) {
  170. clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
  171. set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
  172. } else if (sysfs_streq(buf, "never")) {
  173. clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
  174. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
  175. } else
  176. ret = -EINVAL;
  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. static ssize_t defrag_show(struct kobject *kobj,
  212. struct kobj_attribute *attr, char *buf)
  213. {
  214. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
  215. return sprintf(buf, "[always] defer defer+madvise madvise never\n");
  216. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
  217. return sprintf(buf, "always [defer] defer+madvise madvise never\n");
  218. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
  219. return sprintf(buf, "always defer [defer+madvise] madvise never\n");
  220. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
  221. return sprintf(buf, "always defer defer+madvise [madvise] never\n");
  222. return sprintf(buf, "always defer defer+madvise madvise [never]\n");
  223. }
  224. static ssize_t defrag_store(struct kobject *kobj,
  225. struct kobj_attribute *attr,
  226. const char *buf, size_t count)
  227. {
  228. if (sysfs_streq(buf, "always")) {
  229. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
  230. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
  231. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
  232. set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
  233. } else if (sysfs_streq(buf, "defer+madvise")) {
  234. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
  235. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
  236. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
  237. set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
  238. } else if (sysfs_streq(buf, "defer")) {
  239. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
  240. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
  241. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
  242. set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
  243. } else if (sysfs_streq(buf, "madvise")) {
  244. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
  245. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
  246. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
  247. set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
  248. } else if (sysfs_streq(buf, "never")) {
  249. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
  250. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
  251. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
  252. clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
  253. } else
  254. return -EINVAL;
  255. return count;
  256. }
  257. static struct kobj_attribute defrag_attr =
  258. __ATTR(defrag, 0644, defrag_show, defrag_store);
  259. static ssize_t use_zero_page_show(struct kobject *kobj,
  260. struct kobj_attribute *attr, char *buf)
  261. {
  262. return single_hugepage_flag_show(kobj, attr, buf,
  263. TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  264. }
  265. static ssize_t use_zero_page_store(struct kobject *kobj,
  266. struct kobj_attribute *attr, const char *buf, size_t count)
  267. {
  268. return single_hugepage_flag_store(kobj, attr, buf, count,
  269. TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
  270. }
  271. static struct kobj_attribute use_zero_page_attr =
  272. __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
  273. static ssize_t hpage_pmd_size_show(struct kobject *kobj,
  274. struct kobj_attribute *attr, char *buf)
  275. {
  276. return sprintf(buf, "%lu\n", HPAGE_PMD_SIZE);
  277. }
  278. static struct kobj_attribute hpage_pmd_size_attr =
  279. __ATTR_RO(hpage_pmd_size);
  280. static struct attribute *hugepage_attr[] = {
  281. &enabled_attr.attr,
  282. &defrag_attr.attr,
  283. &use_zero_page_attr.attr,
  284. &hpage_pmd_size_attr.attr,
  285. #ifdef CONFIG_SHMEM
  286. &shmem_enabled_attr.attr,
  287. #endif
  288. NULL,
  289. };
  290. static const struct attribute_group hugepage_attr_group = {
  291. .attrs = hugepage_attr,
  292. };
  293. static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
  294. {
  295. int err;
  296. *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
  297. if (unlikely(!*hugepage_kobj)) {
  298. pr_err("failed to create transparent hugepage kobject\n");
  299. return -ENOMEM;
  300. }
  301. err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
  302. if (err) {
  303. pr_err("failed to register transparent hugepage group\n");
  304. goto delete_obj;
  305. }
  306. err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
  307. if (err) {
  308. pr_err("failed to register transparent hugepage group\n");
  309. goto remove_hp_group;
  310. }
  311. return 0;
  312. remove_hp_group:
  313. sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
  314. delete_obj:
  315. kobject_put(*hugepage_kobj);
  316. return err;
  317. }
  318. static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  319. {
  320. sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
  321. sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
  322. kobject_put(hugepage_kobj);
  323. }
  324. #else
  325. static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
  326. {
  327. return 0;
  328. }
  329. static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
  330. {
  331. }
  332. #endif /* CONFIG_SYSFS */
  333. static int __init hugepage_init(void)
  334. {
  335. int err;
  336. struct kobject *hugepage_kobj;
  337. if (!has_transparent_hugepage()) {
  338. /*
  339. * Hardware doesn't support hugepages, hence disable
  340. * DAX PMD support.
  341. */
  342. transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_NEVER_DAX;
  343. return -EINVAL;
  344. }
  345. /*
  346. * hugepages can't be allocated by the buddy allocator
  347. */
  348. MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
  349. /*
  350. * we use page->mapping and page->index in second tail page
  351. * as list_head: assuming THP order >= 2
  352. */
  353. MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
  354. err = hugepage_init_sysfs(&hugepage_kobj);
  355. if (err)
  356. goto err_sysfs;
  357. err = khugepaged_init();
  358. if (err)
  359. goto err_slab;
  360. err = register_shrinker(&huge_zero_page_shrinker);
  361. if (err)
  362. goto err_hzp_shrinker;
  363. err = register_shrinker(&deferred_split_shrinker);
  364. if (err)
  365. goto err_split_shrinker;
  366. /*
  367. * By default disable transparent hugepages on smaller systems,
  368. * where the extra memory used could hurt more than TLB overhead
  369. * is likely to save. The admin can still enable it through /sys.
  370. */
  371. if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
  372. transparent_hugepage_flags = 0;
  373. return 0;
  374. }
  375. err = start_stop_khugepaged();
  376. if (err)
  377. goto err_khugepaged;
  378. return 0;
  379. err_khugepaged:
  380. unregister_shrinker(&deferred_split_shrinker);
  381. err_split_shrinker:
  382. unregister_shrinker(&huge_zero_page_shrinker);
  383. err_hzp_shrinker:
  384. khugepaged_destroy();
  385. err_slab:
  386. hugepage_exit_sysfs(hugepage_kobj);
  387. err_sysfs:
  388. return err;
  389. }
  390. subsys_initcall(hugepage_init);
  391. static int __init setup_transparent_hugepage(char *str)
  392. {
  393. int ret = 0;
  394. if (!str)
  395. goto out;
  396. if (!strcmp(str, "always")) {
  397. set_bit(TRANSPARENT_HUGEPAGE_FLAG,
  398. &transparent_hugepage_flags);
  399. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  400. &transparent_hugepage_flags);
  401. ret = 1;
  402. } else if (!strcmp(str, "madvise")) {
  403. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  404. &transparent_hugepage_flags);
  405. set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  406. &transparent_hugepage_flags);
  407. ret = 1;
  408. } else if (!strcmp(str, "never")) {
  409. clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
  410. &transparent_hugepage_flags);
  411. clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
  412. &transparent_hugepage_flags);
  413. ret = 1;
  414. }
  415. out:
  416. if (!ret)
  417. pr_warn("transparent_hugepage= cannot parse, ignored\n");
  418. return ret;
  419. }
  420. __setup("transparent_hugepage=", setup_transparent_hugepage);
  421. pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
  422. {
  423. if (likely(vma->vm_flags & VM_WRITE))
  424. pmd = pmd_mkwrite(pmd);
  425. return pmd;
  426. }
  427. #ifdef CONFIG_MEMCG
  428. static inline struct deferred_split *get_deferred_split_queue(struct page *page)
  429. {
  430. struct mem_cgroup *memcg = compound_head(page)->mem_cgroup;
  431. struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
  432. if (memcg)
  433. return &memcg->deferred_split_queue;
  434. else
  435. return &pgdat->deferred_split_queue;
  436. }
  437. #else
  438. static inline struct deferred_split *get_deferred_split_queue(struct page *page)
  439. {
  440. struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
  441. return &pgdat->deferred_split_queue;
  442. }
  443. #endif
  444. void prep_transhuge_page(struct page *page)
  445. {
  446. /*
  447. * we use page->mapping and page->indexlru in second tail page
  448. * as list_head: assuming THP order >= 2
  449. */
  450. INIT_LIST_HEAD(page_deferred_list(page));
  451. set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
  452. }
  453. bool is_transparent_hugepage(struct page *page)
  454. {
  455. if (!PageCompound(page))
  456. return false;
  457. page = compound_head(page);
  458. return is_huge_zero_page(page) ||
  459. page[1].compound_dtor == TRANSHUGE_PAGE_DTOR;
  460. }
  461. EXPORT_SYMBOL_GPL(is_transparent_hugepage);
  462. static unsigned long __thp_get_unmapped_area(struct file *filp,
  463. unsigned long addr, unsigned long len,
  464. loff_t off, unsigned long flags, unsigned long size)
  465. {
  466. loff_t off_end = off + len;
  467. loff_t off_align = round_up(off, size);
  468. unsigned long len_pad, ret;
  469. if (off_end <= off_align || (off_end - off_align) < size)
  470. return 0;
  471. len_pad = len + size;
  472. if (len_pad < len || (off + len_pad) < off)
  473. return 0;
  474. ret = current->mm->get_unmapped_area(filp, addr, len_pad,
  475. off >> PAGE_SHIFT, flags);
  476. /*
  477. * The failure might be due to length padding. The caller will retry
  478. * without the padding.
  479. */
  480. if (IS_ERR_VALUE(ret))
  481. return 0;
  482. /*
  483. * Do not try to align to THP boundary if allocation at the address
  484. * hint succeeds.
  485. */
  486. if (ret == addr)
  487. return addr;
  488. ret += (off - ret) & (size - 1);
  489. return ret;
  490. }
  491. unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
  492. unsigned long len, unsigned long pgoff, unsigned long flags)
  493. {
  494. unsigned long ret;
  495. loff_t off = (loff_t)pgoff << PAGE_SHIFT;
  496. if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
  497. goto out;
  498. ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE);
  499. if (ret)
  500. return ret;
  501. out:
  502. return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
  503. }
  504. EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
  505. static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
  506. struct page *page, gfp_t gfp)
  507. {
  508. struct vm_area_struct *vma = vmf->vma;
  509. pgtable_t pgtable;
  510. unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
  511. vm_fault_t ret = 0;
  512. VM_BUG_ON_PAGE(!PageCompound(page), page);
  513. if (mem_cgroup_charge(page, vma->vm_mm, gfp)) {
  514. put_page(page);
  515. count_vm_event(THP_FAULT_FALLBACK);
  516. count_vm_event(THP_FAULT_FALLBACK_CHARGE);
  517. return VM_FAULT_FALLBACK;
  518. }
  519. cgroup_throttle_swaprate(page, gfp);
  520. pgtable = pte_alloc_one(vma->vm_mm);
  521. if (unlikely(!pgtable)) {
  522. ret = VM_FAULT_OOM;
  523. goto release;
  524. }
  525. clear_huge_page(page, vmf->address, HPAGE_PMD_NR);
  526. /*
  527. * The memory barrier inside __SetPageUptodate makes sure that
  528. * clear_huge_page writes become visible before the set_pmd_at()
  529. * write.
  530. */
  531. __SetPageUptodate(page);
  532. vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
  533. if (unlikely(!pmd_none(*vmf->pmd))) {
  534. goto unlock_release;
  535. } else {
  536. pmd_t entry;
  537. ret = check_stable_address_space(vma->vm_mm);
  538. if (ret)
  539. goto unlock_release;
  540. /* Deliver the page fault to userland */
  541. if (userfaultfd_missing(vma)) {
  542. vm_fault_t ret2;
  543. spin_unlock(vmf->ptl);
  544. put_page(page);
  545. pte_free(vma->vm_mm, pgtable);
  546. ret2 = handle_userfault(vmf, VM_UFFD_MISSING);
  547. VM_BUG_ON(ret2 & VM_FAULT_FALLBACK);
  548. return ret2;
  549. }
  550. entry = mk_huge_pmd(page, vma->vm_page_prot);
  551. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  552. page_add_new_anon_rmap(page, vma, haddr, true);
  553. lru_cache_add_inactive_or_unevictable(page, vma);
  554. pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
  555. set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
  556. add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  557. mm_inc_nr_ptes(vma->vm_mm);
  558. spin_unlock(vmf->ptl);
  559. count_vm_event(THP_FAULT_ALLOC);
  560. count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
  561. }
  562. return 0;
  563. unlock_release:
  564. spin_unlock(vmf->ptl);
  565. release:
  566. if (pgtable)
  567. pte_free(vma->vm_mm, pgtable);
  568. put_page(page);
  569. return ret;
  570. }
  571. /*
  572. * always: directly stall for all thp allocations
  573. * defer: wake kswapd and fail if not immediately available
  574. * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise
  575. * fail if not immediately available
  576. * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately
  577. * available
  578. * never: never stall for any thp allocation
  579. */
  580. static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
  581. {
  582. const bool vma_madvised = !!(vma->vm_flags & VM_HUGEPAGE);
  583. /* Always do synchronous compaction */
  584. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
  585. return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
  586. /* Kick kcompactd and fail quickly */
  587. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
  588. return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
  589. /* Synchronous compaction if madvised, otherwise kick kcompactd */
  590. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
  591. return GFP_TRANSHUGE_LIGHT |
  592. (vma_madvised ? __GFP_DIRECT_RECLAIM :
  593. __GFP_KSWAPD_RECLAIM);
  594. /* Only do synchronous compaction if madvised */
  595. if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
  596. return GFP_TRANSHUGE_LIGHT |
  597. (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
  598. return GFP_TRANSHUGE_LIGHT;
  599. }
  600. /* Caller must hold page table lock. */
  601. static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
  602. struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
  603. struct page *zero_page)
  604. {
  605. pmd_t entry;
  606. if (!pmd_none(*pmd))
  607. return false;
  608. entry = mk_pmd(zero_page, vma->vm_page_prot);
  609. entry = pmd_mkhuge(entry);
  610. if (pgtable)
  611. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  612. set_pmd_at(mm, haddr, pmd, entry);
  613. mm_inc_nr_ptes(mm);
  614. return true;
  615. }
  616. vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
  617. {
  618. struct vm_area_struct *vma = vmf->vma;
  619. gfp_t gfp;
  620. struct page *page;
  621. unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
  622. if (!transhuge_vma_suitable(vma, haddr))
  623. return VM_FAULT_FALLBACK;
  624. if (unlikely(anon_vma_prepare(vma)))
  625. return VM_FAULT_OOM;
  626. if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
  627. return VM_FAULT_OOM;
  628. if (!(vmf->flags & FAULT_FLAG_WRITE) &&
  629. !mm_forbids_zeropage(vma->vm_mm) &&
  630. transparent_hugepage_use_zero_page()) {
  631. pgtable_t pgtable;
  632. struct page *zero_page;
  633. vm_fault_t ret;
  634. pgtable = pte_alloc_one(vma->vm_mm);
  635. if (unlikely(!pgtable))
  636. return VM_FAULT_OOM;
  637. zero_page = mm_get_huge_zero_page(vma->vm_mm);
  638. if (unlikely(!zero_page)) {
  639. pte_free(vma->vm_mm, pgtable);
  640. count_vm_event(THP_FAULT_FALLBACK);
  641. return VM_FAULT_FALLBACK;
  642. }
  643. vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
  644. ret = 0;
  645. if (pmd_none(*vmf->pmd)) {
  646. ret = check_stable_address_space(vma->vm_mm);
  647. if (ret) {
  648. spin_unlock(vmf->ptl);
  649. pte_free(vma->vm_mm, pgtable);
  650. } else if (userfaultfd_missing(vma)) {
  651. spin_unlock(vmf->ptl);
  652. pte_free(vma->vm_mm, pgtable);
  653. ret = handle_userfault(vmf, VM_UFFD_MISSING);
  654. VM_BUG_ON(ret & VM_FAULT_FALLBACK);
  655. } else {
  656. set_huge_zero_page(pgtable, vma->vm_mm, vma,
  657. haddr, vmf->pmd, zero_page);
  658. spin_unlock(vmf->ptl);
  659. }
  660. } else {
  661. spin_unlock(vmf->ptl);
  662. pte_free(vma->vm_mm, pgtable);
  663. }
  664. return ret;
  665. }
  666. gfp = alloc_hugepage_direct_gfpmask(vma);
  667. page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
  668. if (unlikely(!page)) {
  669. count_vm_event(THP_FAULT_FALLBACK);
  670. return VM_FAULT_FALLBACK;
  671. }
  672. prep_transhuge_page(page);
  673. return __do_huge_pmd_anonymous_page(vmf, page, gfp);
  674. }
  675. static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
  676. pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write,
  677. pgtable_t pgtable)
  678. {
  679. struct mm_struct *mm = vma->vm_mm;
  680. pmd_t entry;
  681. spinlock_t *ptl;
  682. ptl = pmd_lock(mm, pmd);
  683. if (!pmd_none(*pmd)) {
  684. if (write) {
  685. if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) {
  686. WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
  687. goto out_unlock;
  688. }
  689. entry = pmd_mkyoung(*pmd);
  690. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  691. if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
  692. update_mmu_cache_pmd(vma, addr, pmd);
  693. }
  694. goto out_unlock;
  695. }
  696. entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
  697. if (pfn_t_devmap(pfn))
  698. entry = pmd_mkdevmap(entry);
  699. if (write) {
  700. entry = pmd_mkyoung(pmd_mkdirty(entry));
  701. entry = maybe_pmd_mkwrite(entry, vma);
  702. }
  703. if (pgtable) {
  704. pgtable_trans_huge_deposit(mm, pmd, pgtable);
  705. mm_inc_nr_ptes(mm);
  706. pgtable = NULL;
  707. }
  708. set_pmd_at(mm, addr, pmd, entry);
  709. update_mmu_cache_pmd(vma, addr, pmd);
  710. out_unlock:
  711. spin_unlock(ptl);
  712. if (pgtable)
  713. pte_free(mm, pgtable);
  714. }
  715. /**
  716. * vmf_insert_pfn_pmd_prot - insert a pmd size pfn
  717. * @vmf: Structure describing the fault
  718. * @pfn: pfn to insert
  719. * @pgprot: page protection to use
  720. * @write: whether it's a write fault
  721. *
  722. * Insert a pmd size pfn. See vmf_insert_pfn() for additional info and
  723. * also consult the vmf_insert_mixed_prot() documentation when
  724. * @pgprot != @vmf->vma->vm_page_prot.
  725. *
  726. * Return: vm_fault_t value.
  727. */
  728. vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
  729. pgprot_t pgprot, bool write)
  730. {
  731. unsigned long addr = vmf->address & PMD_MASK;
  732. struct vm_area_struct *vma = vmf->vma;
  733. pgtable_t pgtable = NULL;
  734. /*
  735. * If we had pmd_special, we could avoid all these restrictions,
  736. * but we need to be consistent with PTEs and architectures that
  737. * can't support a 'special' bit.
  738. */
  739. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
  740. !pfn_t_devmap(pfn));
  741. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  742. (VM_PFNMAP|VM_MIXEDMAP));
  743. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  744. if (addr < vma->vm_start || addr >= vma->vm_end)
  745. return VM_FAULT_SIGBUS;
  746. if (arch_needs_pgtable_deposit()) {
  747. pgtable = pte_alloc_one(vma->vm_mm);
  748. if (!pgtable)
  749. return VM_FAULT_OOM;
  750. }
  751. track_pfn_insert(vma, &pgprot, pfn);
  752. insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
  753. return VM_FAULT_NOPAGE;
  754. }
  755. EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd_prot);
  756. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  757. static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
  758. {
  759. if (likely(vma->vm_flags & VM_WRITE))
  760. pud = pud_mkwrite(pud);
  761. return pud;
  762. }
  763. static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr,
  764. pud_t *pud, pfn_t pfn, pgprot_t prot, bool write)
  765. {
  766. struct mm_struct *mm = vma->vm_mm;
  767. pud_t entry;
  768. spinlock_t *ptl;
  769. ptl = pud_lock(mm, pud);
  770. if (!pud_none(*pud)) {
  771. if (write) {
  772. if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) {
  773. WARN_ON_ONCE(!is_huge_zero_pud(*pud));
  774. goto out_unlock;
  775. }
  776. entry = pud_mkyoung(*pud);
  777. entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
  778. if (pudp_set_access_flags(vma, addr, pud, entry, 1))
  779. update_mmu_cache_pud(vma, addr, pud);
  780. }
  781. goto out_unlock;
  782. }
  783. entry = pud_mkhuge(pfn_t_pud(pfn, prot));
  784. if (pfn_t_devmap(pfn))
  785. entry = pud_mkdevmap(entry);
  786. if (write) {
  787. entry = pud_mkyoung(pud_mkdirty(entry));
  788. entry = maybe_pud_mkwrite(entry, vma);
  789. }
  790. set_pud_at(mm, addr, pud, entry);
  791. update_mmu_cache_pud(vma, addr, pud);
  792. out_unlock:
  793. spin_unlock(ptl);
  794. }
  795. /**
  796. * vmf_insert_pfn_pud_prot - insert a pud size pfn
  797. * @vmf: Structure describing the fault
  798. * @pfn: pfn to insert
  799. * @pgprot: page protection to use
  800. * @write: whether it's a write fault
  801. *
  802. * Insert a pud size pfn. See vmf_insert_pfn() for additional info and
  803. * also consult the vmf_insert_mixed_prot() documentation when
  804. * @pgprot != @vmf->vma->vm_page_prot.
  805. *
  806. * Return: vm_fault_t value.
  807. */
  808. vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
  809. pgprot_t pgprot, bool write)
  810. {
  811. unsigned long addr = vmf->address & PUD_MASK;
  812. struct vm_area_struct *vma = vmf->vma;
  813. /*
  814. * If we had pud_special, we could avoid all these restrictions,
  815. * but we need to be consistent with PTEs and architectures that
  816. * can't support a 'special' bit.
  817. */
  818. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
  819. !pfn_t_devmap(pfn));
  820. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  821. (VM_PFNMAP|VM_MIXEDMAP));
  822. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  823. if (addr < vma->vm_start || addr >= vma->vm_end)
  824. return VM_FAULT_SIGBUS;
  825. track_pfn_insert(vma, &pgprot, pfn);
  826. insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write);
  827. return VM_FAULT_NOPAGE;
  828. }
  829. EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud_prot);
  830. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  831. static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
  832. pmd_t *pmd, int flags)
  833. {
  834. pmd_t _pmd;
  835. _pmd = pmd_mkyoung(*pmd);
  836. if (flags & FOLL_WRITE)
  837. _pmd = pmd_mkdirty(_pmd);
  838. if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
  839. pmd, _pmd, flags & FOLL_WRITE))
  840. update_mmu_cache_pmd(vma, addr, pmd);
  841. }
  842. struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
  843. pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
  844. {
  845. unsigned long pfn = pmd_pfn(*pmd);
  846. struct mm_struct *mm = vma->vm_mm;
  847. struct page *page;
  848. assert_spin_locked(pmd_lockptr(mm, pmd));
  849. /*
  850. * When we COW a devmap PMD entry, we split it into PTEs, so we should
  851. * not be in this function with `flags & FOLL_COW` set.
  852. */
  853. WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set");
  854. /* FOLL_GET and FOLL_PIN are mutually exclusive. */
  855. if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
  856. (FOLL_PIN | FOLL_GET)))
  857. return NULL;
  858. if (flags & FOLL_WRITE && !pmd_write(*pmd))
  859. return NULL;
  860. if (pmd_present(*pmd) && pmd_devmap(*pmd))
  861. /* pass */;
  862. else
  863. return NULL;
  864. if (flags & FOLL_TOUCH)
  865. touch_pmd(vma, addr, pmd, flags);
  866. /*
  867. * device mapped pages can only be returned if the
  868. * caller will manage the page reference count.
  869. */
  870. if (!(flags & (FOLL_GET | FOLL_PIN)))
  871. return ERR_PTR(-EEXIST);
  872. pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
  873. *pgmap = get_dev_pagemap(pfn, *pgmap);
  874. if (!*pgmap)
  875. return ERR_PTR(-EFAULT);
  876. page = pfn_to_page(pfn);
  877. if (!try_grab_page(page, flags))
  878. page = ERR_PTR(-ENOMEM);
  879. return page;
  880. }
  881. int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  882. pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
  883. struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
  884. {
  885. spinlock_t *dst_ptl, *src_ptl;
  886. struct page *src_page;
  887. pmd_t pmd;
  888. pgtable_t pgtable = NULL;
  889. int ret = -ENOMEM;
  890. /* Skip if can be re-fill on fault */
  891. if (!vma_is_anonymous(dst_vma))
  892. return 0;
  893. pgtable = pte_alloc_one(dst_mm);
  894. if (unlikely(!pgtable))
  895. goto out;
  896. dst_ptl = pmd_lock(dst_mm, dst_pmd);
  897. src_ptl = pmd_lockptr(src_mm, src_pmd);
  898. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  899. ret = -EAGAIN;
  900. pmd = *src_pmd;
  901. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  902. if (unlikely(is_swap_pmd(pmd))) {
  903. swp_entry_t entry = pmd_to_swp_entry(pmd);
  904. VM_BUG_ON(!is_pmd_migration_entry(pmd));
  905. if (is_write_migration_entry(entry)) {
  906. make_migration_entry_read(&entry);
  907. pmd = swp_entry_to_pmd(entry);
  908. if (pmd_swp_soft_dirty(*src_pmd))
  909. pmd = pmd_swp_mksoft_dirty(pmd);
  910. if (pmd_swp_uffd_wp(*src_pmd))
  911. pmd = pmd_swp_mkuffd_wp(pmd);
  912. set_pmd_at(src_mm, addr, src_pmd, pmd);
  913. }
  914. add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  915. mm_inc_nr_ptes(dst_mm);
  916. pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
  917. if (!userfaultfd_wp(dst_vma))
  918. pmd = pmd_swp_clear_uffd_wp(pmd);
  919. set_pmd_at(dst_mm, addr, dst_pmd, pmd);
  920. ret = 0;
  921. goto out_unlock;
  922. }
  923. #endif
  924. if (unlikely(!pmd_trans_huge(pmd))) {
  925. pte_free(dst_mm, pgtable);
  926. goto out_unlock;
  927. }
  928. /*
  929. * When page table lock is held, the huge zero pmd should not be
  930. * under splitting since we don't split the page itself, only pmd to
  931. * a page table.
  932. */
  933. if (is_huge_zero_pmd(pmd)) {
  934. /*
  935. * get_huge_zero_page() will never allocate a new page here,
  936. * since we already have a zero page to copy. It just takes a
  937. * reference.
  938. */
  939. mm_get_huge_zero_page(dst_mm);
  940. goto out_zero_page;
  941. }
  942. src_page = pmd_page(pmd);
  943. VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
  944. /*
  945. * If this page is a potentially pinned page, split and retry the fault
  946. * with smaller page size. Normally this should not happen because the
  947. * userspace should use MADV_DONTFORK upon pinned regions. This is a
  948. * best effort that the pinned pages won't be replaced by another
  949. * random page during the coming copy-on-write.
  950. */
  951. if (unlikely(is_cow_mapping(src_vma->vm_flags) &&
  952. atomic_read(&src_mm->has_pinned) &&
  953. page_maybe_dma_pinned(src_page))) {
  954. pte_free(dst_mm, pgtable);
  955. spin_unlock(src_ptl);
  956. spin_unlock(dst_ptl);
  957. __split_huge_pmd(src_vma, src_pmd, addr, false, NULL);
  958. return -EAGAIN;
  959. }
  960. get_page(src_page);
  961. page_dup_rmap(src_page, true);
  962. add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
  963. out_zero_page:
  964. mm_inc_nr_ptes(dst_mm);
  965. pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
  966. pmdp_set_wrprotect(src_mm, addr, src_pmd);
  967. if (!userfaultfd_wp(dst_vma))
  968. pmd = pmd_clear_uffd_wp(pmd);
  969. pmd = pmd_mkold(pmd_wrprotect(pmd));
  970. set_pmd_at(dst_mm, addr, dst_pmd, pmd);
  971. ret = 0;
  972. out_unlock:
  973. spin_unlock(src_ptl);
  974. spin_unlock(dst_ptl);
  975. out:
  976. return ret;
  977. }
  978. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  979. static void touch_pud(struct vm_area_struct *vma, unsigned long addr,
  980. pud_t *pud, int flags)
  981. {
  982. pud_t _pud;
  983. _pud = pud_mkyoung(*pud);
  984. if (flags & FOLL_WRITE)
  985. _pud = pud_mkdirty(_pud);
  986. if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
  987. pud, _pud, flags & FOLL_WRITE))
  988. update_mmu_cache_pud(vma, addr, pud);
  989. }
  990. struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
  991. pud_t *pud, int flags, struct dev_pagemap **pgmap)
  992. {
  993. unsigned long pfn = pud_pfn(*pud);
  994. struct mm_struct *mm = vma->vm_mm;
  995. struct page *page;
  996. assert_spin_locked(pud_lockptr(mm, pud));
  997. if (flags & FOLL_WRITE && !pud_write(*pud))
  998. return NULL;
  999. /* FOLL_GET and FOLL_PIN are mutually exclusive. */
  1000. if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
  1001. (FOLL_PIN | FOLL_GET)))
  1002. return NULL;
  1003. if (pud_present(*pud) && pud_devmap(*pud))
  1004. /* pass */;
  1005. else
  1006. return NULL;
  1007. if (flags & FOLL_TOUCH)
  1008. touch_pud(vma, addr, pud, flags);
  1009. /*
  1010. * device mapped pages can only be returned if the
  1011. * caller will manage the page reference count.
  1012. *
  1013. * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here:
  1014. */
  1015. if (!(flags & (FOLL_GET | FOLL_PIN)))
  1016. return ERR_PTR(-EEXIST);
  1017. pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
  1018. *pgmap = get_dev_pagemap(pfn, *pgmap);
  1019. if (!*pgmap)
  1020. return ERR_PTR(-EFAULT);
  1021. page = pfn_to_page(pfn);
  1022. if (!try_grab_page(page, flags))
  1023. page = ERR_PTR(-ENOMEM);
  1024. return page;
  1025. }
  1026. int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  1027. pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
  1028. struct vm_area_struct *vma)
  1029. {
  1030. spinlock_t *dst_ptl, *src_ptl;
  1031. pud_t pud;
  1032. int ret;
  1033. dst_ptl = pud_lock(dst_mm, dst_pud);
  1034. src_ptl = pud_lockptr(src_mm, src_pud);
  1035. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  1036. ret = -EAGAIN;
  1037. pud = *src_pud;
  1038. if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud)))
  1039. goto out_unlock;
  1040. /*
  1041. * When page table lock is held, the huge zero pud should not be
  1042. * under splitting since we don't split the page itself, only pud to
  1043. * a page table.
  1044. */
  1045. if (is_huge_zero_pud(pud)) {
  1046. /* No huge zero pud yet */
  1047. }
  1048. /* Please refer to comments in copy_huge_pmd() */
  1049. if (unlikely(is_cow_mapping(vma->vm_flags) &&
  1050. atomic_read(&src_mm->has_pinned) &&
  1051. page_maybe_dma_pinned(pud_page(pud)))) {
  1052. spin_unlock(src_ptl);
  1053. spin_unlock(dst_ptl);
  1054. __split_huge_pud(vma, src_pud, addr);
  1055. return -EAGAIN;
  1056. }
  1057. pudp_set_wrprotect(src_mm, addr, src_pud);
  1058. pud = pud_mkold(pud_wrprotect(pud));
  1059. set_pud_at(dst_mm, addr, dst_pud, pud);
  1060. ret = 0;
  1061. out_unlock:
  1062. spin_unlock(src_ptl);
  1063. spin_unlock(dst_ptl);
  1064. return ret;
  1065. }
  1066. void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
  1067. {
  1068. pud_t entry;
  1069. unsigned long haddr;
  1070. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1071. vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
  1072. if (unlikely(!pud_same(*vmf->pud, orig_pud)))
  1073. goto unlock;
  1074. entry = pud_mkyoung(orig_pud);
  1075. if (write)
  1076. entry = pud_mkdirty(entry);
  1077. haddr = vmf->address & HPAGE_PUD_MASK;
  1078. if (pudp_set_access_flags(vmf->vma, haddr, vmf->pud, entry, write))
  1079. update_mmu_cache_pud(vmf->vma, vmf->address, vmf->pud);
  1080. unlock:
  1081. spin_unlock(vmf->ptl);
  1082. }
  1083. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  1084. void huge_pmd_set_accessed(struct vm_fault *vmf, pmd_t orig_pmd)
  1085. {
  1086. pmd_t entry;
  1087. unsigned long haddr;
  1088. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1089. vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
  1090. if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
  1091. goto unlock;
  1092. entry = pmd_mkyoung(orig_pmd);
  1093. if (write)
  1094. entry = pmd_mkdirty(entry);
  1095. haddr = vmf->address & HPAGE_PMD_MASK;
  1096. if (pmdp_set_access_flags(vmf->vma, haddr, vmf->pmd, entry, write))
  1097. update_mmu_cache_pmd(vmf->vma, vmf->address, vmf->pmd);
  1098. unlock:
  1099. spin_unlock(vmf->ptl);
  1100. }
  1101. vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)
  1102. {
  1103. struct vm_area_struct *vma = vmf->vma;
  1104. struct page *page;
  1105. unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
  1106. vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
  1107. VM_BUG_ON_VMA(!vma->anon_vma, vma);
  1108. if (is_huge_zero_pmd(orig_pmd))
  1109. goto fallback;
  1110. spin_lock(vmf->ptl);
  1111. if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
  1112. spin_unlock(vmf->ptl);
  1113. return 0;
  1114. }
  1115. page = pmd_page(orig_pmd);
  1116. VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
  1117. /* Lock page for reuse_swap_page() */
  1118. if (!trylock_page(page)) {
  1119. get_page(page);
  1120. spin_unlock(vmf->ptl);
  1121. lock_page(page);
  1122. spin_lock(vmf->ptl);
  1123. if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
  1124. spin_unlock(vmf->ptl);
  1125. unlock_page(page);
  1126. put_page(page);
  1127. return 0;
  1128. }
  1129. put_page(page);
  1130. }
  1131. /*
  1132. * We can only reuse the page if nobody else maps the huge page or it's
  1133. * part.
  1134. */
  1135. if (reuse_swap_page(page, NULL)) {
  1136. pmd_t entry;
  1137. entry = pmd_mkyoung(orig_pmd);
  1138. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1139. if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
  1140. update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
  1141. unlock_page(page);
  1142. spin_unlock(vmf->ptl);
  1143. return VM_FAULT_WRITE;
  1144. }
  1145. unlock_page(page);
  1146. spin_unlock(vmf->ptl);
  1147. fallback:
  1148. __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
  1149. return VM_FAULT_FALLBACK;
  1150. }
  1151. /*
  1152. * FOLL_FORCE can write to even unwritable pmd's, but only
  1153. * after we've gone through a COW cycle and they are dirty.
  1154. */
  1155. static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
  1156. {
  1157. return pmd_write(pmd) ||
  1158. ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
  1159. }
  1160. struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
  1161. unsigned long addr,
  1162. pmd_t *pmd,
  1163. unsigned int flags)
  1164. {
  1165. struct mm_struct *mm = vma->vm_mm;
  1166. struct page *page = NULL;
  1167. assert_spin_locked(pmd_lockptr(mm, pmd));
  1168. if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
  1169. goto out;
  1170. /* Avoid dumping huge zero page */
  1171. if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
  1172. return ERR_PTR(-EFAULT);
  1173. /* Full NUMA hinting faults to serialise migration in fault paths */
  1174. if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
  1175. goto out;
  1176. page = pmd_page(*pmd);
  1177. VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
  1178. if (!try_grab_page(page, flags))
  1179. return ERR_PTR(-ENOMEM);
  1180. if (flags & FOLL_TOUCH)
  1181. touch_pmd(vma, addr, pmd, flags);
  1182. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  1183. /*
  1184. * We don't mlock() pte-mapped THPs. This way we can avoid
  1185. * leaking mlocked pages into non-VM_LOCKED VMAs.
  1186. *
  1187. * For anon THP:
  1188. *
  1189. * In most cases the pmd is the only mapping of the page as we
  1190. * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
  1191. * writable private mappings in populate_vma_page_range().
  1192. *
  1193. * The only scenario when we have the page shared here is if we
  1194. * mlocking read-only mapping shared over fork(). We skip
  1195. * mlocking such pages.
  1196. *
  1197. * For file THP:
  1198. *
  1199. * We can expect PageDoubleMap() to be stable under page lock:
  1200. * for file pages we set it in page_add_file_rmap(), which
  1201. * requires page to be locked.
  1202. */
  1203. if (PageAnon(page) && compound_mapcount(page) != 1)
  1204. goto skip_mlock;
  1205. if (PageDoubleMap(page) || !page->mapping)
  1206. goto skip_mlock;
  1207. if (!trylock_page(page))
  1208. goto skip_mlock;
  1209. if (page->mapping && !PageDoubleMap(page))
  1210. mlock_vma_page(page);
  1211. unlock_page(page);
  1212. }
  1213. skip_mlock:
  1214. page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
  1215. VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
  1216. out:
  1217. return page;
  1218. }
  1219. /* NUMA hinting page fault entry point for trans huge pmds */
  1220. vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)
  1221. {
  1222. struct vm_area_struct *vma = vmf->vma;
  1223. struct anon_vma *anon_vma = NULL;
  1224. struct page *page;
  1225. unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
  1226. int page_nid = NUMA_NO_NODE, this_nid = numa_node_id();
  1227. int target_nid, last_cpupid = -1;
  1228. bool page_locked;
  1229. bool migrated = false;
  1230. bool was_writable;
  1231. int flags = 0;
  1232. vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
  1233. if (unlikely(!pmd_same(pmd, *vmf->pmd)))
  1234. goto out_unlock;
  1235. /*
  1236. * If there are potential migrations, wait for completion and retry
  1237. * without disrupting NUMA hinting information. Do not relock and
  1238. * check_same as the page may no longer be mapped.
  1239. */
  1240. if (unlikely(pmd_trans_migrating(*vmf->pmd))) {
  1241. page = pmd_page(*vmf->pmd);
  1242. if (!get_page_unless_zero(page))
  1243. goto out_unlock;
  1244. spin_unlock(vmf->ptl);
  1245. put_and_wait_on_page_locked(page);
  1246. goto out;
  1247. }
  1248. page = pmd_page(pmd);
  1249. BUG_ON(is_huge_zero_page(page));
  1250. page_nid = page_to_nid(page);
  1251. last_cpupid = page_cpupid_last(page);
  1252. count_vm_numa_event(NUMA_HINT_FAULTS);
  1253. if (page_nid == this_nid) {
  1254. count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
  1255. flags |= TNF_FAULT_LOCAL;
  1256. }
  1257. /* See similar comment in do_numa_page for explanation */
  1258. if (!pmd_savedwrite(pmd))
  1259. flags |= TNF_NO_GROUP;
  1260. /*
  1261. * Acquire the page lock to serialise THP migrations but avoid dropping
  1262. * page_table_lock if at all possible
  1263. */
  1264. page_locked = trylock_page(page);
  1265. target_nid = mpol_misplaced(page, vma, haddr);
  1266. if (target_nid == NUMA_NO_NODE) {
  1267. /* If the page was locked, there are no parallel migrations */
  1268. if (page_locked)
  1269. goto clear_pmdnuma;
  1270. }
  1271. /* Migration could have started since the pmd_trans_migrating check */
  1272. if (!page_locked) {
  1273. page_nid = NUMA_NO_NODE;
  1274. if (!get_page_unless_zero(page))
  1275. goto out_unlock;
  1276. spin_unlock(vmf->ptl);
  1277. put_and_wait_on_page_locked(page);
  1278. goto out;
  1279. }
  1280. /*
  1281. * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
  1282. * to serialises splits
  1283. */
  1284. get_page(page);
  1285. spin_unlock(vmf->ptl);
  1286. anon_vma = page_lock_anon_vma_read(page);
  1287. /* Confirm the PMD did not change while page_table_lock was released */
  1288. spin_lock(vmf->ptl);
  1289. if (unlikely(!pmd_same(pmd, *vmf->pmd))) {
  1290. unlock_page(page);
  1291. put_page(page);
  1292. page_nid = NUMA_NO_NODE;
  1293. goto out_unlock;
  1294. }
  1295. /* Bail if we fail to protect against THP splits for any reason */
  1296. if (unlikely(!anon_vma)) {
  1297. put_page(page);
  1298. page_nid = NUMA_NO_NODE;
  1299. goto clear_pmdnuma;
  1300. }
  1301. /*
  1302. * Since we took the NUMA fault, we must have observed the !accessible
  1303. * bit. Make sure all other CPUs agree with that, to avoid them
  1304. * modifying the page we're about to migrate.
  1305. *
  1306. * Must be done under PTL such that we'll observe the relevant
  1307. * inc_tlb_flush_pending().
  1308. *
  1309. * We are not sure a pending tlb flush here is for a huge page
  1310. * mapping or not. Hence use the tlb range variant
  1311. */
  1312. if (mm_tlb_flush_pending(vma->vm_mm)) {
  1313. flush_tlb_range(vma, haddr, haddr + HPAGE_PMD_SIZE);
  1314. /*
  1315. * change_huge_pmd() released the pmd lock before
  1316. * invalidating the secondary MMUs sharing the primary
  1317. * MMU pagetables (with ->invalidate_range()). The
  1318. * mmu_notifier_invalidate_range_end() (which
  1319. * internally calls ->invalidate_range()) in
  1320. * change_pmd_range() will run after us, so we can't
  1321. * rely on it here and we need an explicit invalidate.
  1322. */
  1323. mmu_notifier_invalidate_range(vma->vm_mm, haddr,
  1324. haddr + HPAGE_PMD_SIZE);
  1325. }
  1326. /*
  1327. * Migrate the THP to the requested node, returns with page unlocked
  1328. * and access rights restored.
  1329. */
  1330. spin_unlock(vmf->ptl);
  1331. migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
  1332. vmf->pmd, pmd, vmf->address, page, target_nid);
  1333. if (migrated) {
  1334. flags |= TNF_MIGRATED;
  1335. page_nid = target_nid;
  1336. } else
  1337. flags |= TNF_MIGRATE_FAIL;
  1338. goto out;
  1339. clear_pmdnuma:
  1340. BUG_ON(!PageLocked(page));
  1341. was_writable = pmd_savedwrite(pmd);
  1342. pmd = pmd_modify(pmd, vma->vm_page_prot);
  1343. pmd = pmd_mkyoung(pmd);
  1344. if (was_writable)
  1345. pmd = pmd_mkwrite(pmd);
  1346. set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
  1347. update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
  1348. unlock_page(page);
  1349. out_unlock:
  1350. spin_unlock(vmf->ptl);
  1351. out:
  1352. if (anon_vma)
  1353. page_unlock_anon_vma_read(anon_vma);
  1354. if (page_nid != NUMA_NO_NODE)
  1355. task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR,
  1356. flags);
  1357. return 0;
  1358. }
  1359. /*
  1360. * Return true if we do MADV_FREE successfully on entire pmd page.
  1361. * Otherwise, return false.
  1362. */
  1363. bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  1364. pmd_t *pmd, unsigned long addr, unsigned long next)
  1365. {
  1366. spinlock_t *ptl;
  1367. pmd_t orig_pmd;
  1368. struct page *page;
  1369. struct mm_struct *mm = tlb->mm;
  1370. bool ret = false;
  1371. tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
  1372. ptl = pmd_trans_huge_lock(pmd, vma);
  1373. if (!ptl)
  1374. goto out_unlocked;
  1375. orig_pmd = *pmd;
  1376. if (is_huge_zero_pmd(orig_pmd))
  1377. goto out;
  1378. if (unlikely(!pmd_present(orig_pmd))) {
  1379. VM_BUG_ON(thp_migration_supported() &&
  1380. !is_pmd_migration_entry(orig_pmd));
  1381. goto out;
  1382. }
  1383. page = pmd_page(orig_pmd);
  1384. /*
  1385. * If other processes are mapping this page, we couldn't discard
  1386. * the page unless they all do MADV_FREE so let's skip the page.
  1387. */
  1388. if (total_mapcount(page) != 1)
  1389. goto out;
  1390. if (!trylock_page(page))
  1391. goto out;
  1392. /*
  1393. * If user want to discard part-pages of THP, split it so MADV_FREE
  1394. * will deactivate only them.
  1395. */
  1396. if (next - addr != HPAGE_PMD_SIZE) {
  1397. get_page(page);
  1398. spin_unlock(ptl);
  1399. split_huge_page(page);
  1400. unlock_page(page);
  1401. put_page(page);
  1402. goto out_unlocked;
  1403. }
  1404. if (PageDirty(page))
  1405. ClearPageDirty(page);
  1406. unlock_page(page);
  1407. if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
  1408. pmdp_invalidate(vma, addr, pmd);
  1409. orig_pmd = pmd_mkold(orig_pmd);
  1410. orig_pmd = pmd_mkclean(orig_pmd);
  1411. set_pmd_at(mm, addr, pmd, orig_pmd);
  1412. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  1413. }
  1414. mark_page_lazyfree(page);
  1415. ret = true;
  1416. out:
  1417. spin_unlock(ptl);
  1418. out_unlocked:
  1419. return ret;
  1420. }
  1421. static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
  1422. {
  1423. pgtable_t pgtable;
  1424. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1425. pte_free(mm, pgtable);
  1426. mm_dec_nr_ptes(mm);
  1427. }
  1428. int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
  1429. pmd_t *pmd, unsigned long addr)
  1430. {
  1431. pmd_t orig_pmd;
  1432. spinlock_t *ptl;
  1433. tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
  1434. ptl = __pmd_trans_huge_lock(pmd, vma);
  1435. if (!ptl)
  1436. return 0;
  1437. /*
  1438. * For architectures like ppc64 we look at deposited pgtable
  1439. * when calling pmdp_huge_get_and_clear. So do the
  1440. * pgtable_trans_huge_withdraw after finishing pmdp related
  1441. * operations.
  1442. */
  1443. orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
  1444. tlb->fullmm);
  1445. tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
  1446. if (vma_is_special_huge(vma)) {
  1447. if (arch_needs_pgtable_deposit())
  1448. zap_deposited_table(tlb->mm, pmd);
  1449. spin_unlock(ptl);
  1450. if (is_huge_zero_pmd(orig_pmd))
  1451. tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
  1452. } else if (is_huge_zero_pmd(orig_pmd)) {
  1453. zap_deposited_table(tlb->mm, pmd);
  1454. spin_unlock(ptl);
  1455. tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
  1456. } else {
  1457. struct page *page = NULL;
  1458. int flush_needed = 1;
  1459. if (pmd_present(orig_pmd)) {
  1460. page = pmd_page(orig_pmd);
  1461. page_remove_rmap(page, true);
  1462. VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
  1463. VM_BUG_ON_PAGE(!PageHead(page), page);
  1464. } else if (thp_migration_supported()) {
  1465. swp_entry_t entry;
  1466. VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
  1467. entry = pmd_to_swp_entry(orig_pmd);
  1468. page = pfn_to_page(swp_offset(entry));
  1469. flush_needed = 0;
  1470. } else
  1471. WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
  1472. if (PageAnon(page)) {
  1473. zap_deposited_table(tlb->mm, pmd);
  1474. add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
  1475. } else {
  1476. if (arch_needs_pgtable_deposit())
  1477. zap_deposited_table(tlb->mm, pmd);
  1478. add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
  1479. }
  1480. spin_unlock(ptl);
  1481. if (flush_needed)
  1482. tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
  1483. }
  1484. return 1;
  1485. }
  1486. #ifndef pmd_move_must_withdraw
  1487. static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
  1488. spinlock_t *old_pmd_ptl,
  1489. struct vm_area_struct *vma)
  1490. {
  1491. /*
  1492. * With split pmd lock we also need to move preallocated
  1493. * PTE page table if new_pmd is on different PMD page table.
  1494. *
  1495. * We also don't deposit and withdraw tables for file pages.
  1496. */
  1497. return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
  1498. }
  1499. #endif
  1500. static pmd_t move_soft_dirty_pmd(pmd_t pmd)
  1501. {
  1502. #ifdef CONFIG_MEM_SOFT_DIRTY
  1503. if (unlikely(is_pmd_migration_entry(pmd)))
  1504. pmd = pmd_swp_mksoft_dirty(pmd);
  1505. else if (pmd_present(pmd))
  1506. pmd = pmd_mksoft_dirty(pmd);
  1507. #endif
  1508. return pmd;
  1509. }
  1510. bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
  1511. unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
  1512. {
  1513. spinlock_t *old_ptl, *new_ptl;
  1514. pmd_t pmd;
  1515. struct mm_struct *mm = vma->vm_mm;
  1516. bool force_flush = false;
  1517. /*
  1518. * The destination pmd shouldn't be established, free_pgtables()
  1519. * should have release it.
  1520. */
  1521. if (WARN_ON(!pmd_none(*new_pmd))) {
  1522. VM_BUG_ON(pmd_trans_huge(*new_pmd));
  1523. return false;
  1524. }
  1525. /*
  1526. * We don't have to worry about the ordering of src and dst
  1527. * ptlocks because exclusive mmap_lock prevents deadlock.
  1528. */
  1529. old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
  1530. if (old_ptl) {
  1531. new_ptl = pmd_lockptr(mm, new_pmd);
  1532. if (new_ptl != old_ptl)
  1533. spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
  1534. pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
  1535. if (pmd_present(pmd))
  1536. force_flush = true;
  1537. VM_BUG_ON(!pmd_none(*new_pmd));
  1538. if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
  1539. pgtable_t pgtable;
  1540. pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
  1541. pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
  1542. }
  1543. pmd = move_soft_dirty_pmd(pmd);
  1544. set_pmd_at(mm, new_addr, new_pmd, pmd);
  1545. if (force_flush)
  1546. flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
  1547. if (new_ptl != old_ptl)
  1548. spin_unlock(new_ptl);
  1549. spin_unlock(old_ptl);
  1550. return true;
  1551. }
  1552. return false;
  1553. }
  1554. /*
  1555. * Returns
  1556. * - 0 if PMD could not be locked
  1557. * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
  1558. * - HPAGE_PMD_NR is protections changed and TLB flush necessary
  1559. */
  1560. int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1561. unsigned long addr, pgprot_t newprot, unsigned long cp_flags)
  1562. {
  1563. struct mm_struct *mm = vma->vm_mm;
  1564. spinlock_t *ptl;
  1565. pmd_t entry;
  1566. bool preserve_write;
  1567. int ret;
  1568. bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
  1569. bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
  1570. bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
  1571. ptl = __pmd_trans_huge_lock(pmd, vma);
  1572. if (!ptl)
  1573. return 0;
  1574. preserve_write = prot_numa && pmd_write(*pmd);
  1575. ret = 1;
  1576. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  1577. if (is_swap_pmd(*pmd)) {
  1578. swp_entry_t entry = pmd_to_swp_entry(*pmd);
  1579. VM_BUG_ON(!is_pmd_migration_entry(*pmd));
  1580. if (is_write_migration_entry(entry)) {
  1581. pmd_t newpmd;
  1582. /*
  1583. * A protection check is difficult so
  1584. * just be safe and disable write
  1585. */
  1586. make_migration_entry_read(&entry);
  1587. newpmd = swp_entry_to_pmd(entry);
  1588. if (pmd_swp_soft_dirty(*pmd))
  1589. newpmd = pmd_swp_mksoft_dirty(newpmd);
  1590. if (pmd_swp_uffd_wp(*pmd))
  1591. newpmd = pmd_swp_mkuffd_wp(newpmd);
  1592. set_pmd_at(mm, addr, pmd, newpmd);
  1593. }
  1594. goto unlock;
  1595. }
  1596. #endif
  1597. /*
  1598. * Avoid trapping faults against the zero page. The read-only
  1599. * data is likely to be read-cached on the local CPU and
  1600. * local/remote hits to the zero page are not interesting.
  1601. */
  1602. if (prot_numa && is_huge_zero_pmd(*pmd))
  1603. goto unlock;
  1604. if (prot_numa && pmd_protnone(*pmd))
  1605. goto unlock;
  1606. /*
  1607. * In case prot_numa, we are under mmap_read_lock(mm). It's critical
  1608. * to not clear pmd intermittently to avoid race with MADV_DONTNEED
  1609. * which is also under mmap_read_lock(mm):
  1610. *
  1611. * CPU0: CPU1:
  1612. * change_huge_pmd(prot_numa=1)
  1613. * pmdp_huge_get_and_clear_notify()
  1614. * madvise_dontneed()
  1615. * zap_pmd_range()
  1616. * pmd_trans_huge(*pmd) == 0 (without ptl)
  1617. * // skip the pmd
  1618. * set_pmd_at();
  1619. * // pmd is re-established
  1620. *
  1621. * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
  1622. * which may break userspace.
  1623. *
  1624. * pmdp_invalidate() is required to make sure we don't miss
  1625. * dirty/young flags set by hardware.
  1626. */
  1627. entry = pmdp_invalidate(vma, addr, pmd);
  1628. entry = pmd_modify(entry, newprot);
  1629. if (preserve_write)
  1630. entry = pmd_mk_savedwrite(entry);
  1631. if (uffd_wp) {
  1632. entry = pmd_wrprotect(entry);
  1633. entry = pmd_mkuffd_wp(entry);
  1634. } else if (uffd_wp_resolve) {
  1635. /*
  1636. * Leave the write bit to be handled by PF interrupt
  1637. * handler, then things like COW could be properly
  1638. * handled.
  1639. */
  1640. entry = pmd_clear_uffd_wp(entry);
  1641. }
  1642. ret = HPAGE_PMD_NR;
  1643. set_pmd_at(mm, addr, pmd, entry);
  1644. BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
  1645. unlock:
  1646. spin_unlock(ptl);
  1647. return ret;
  1648. }
  1649. /*
  1650. * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
  1651. *
  1652. * Note that if it returns page table lock pointer, this routine returns without
  1653. * unlocking page table lock. So callers must unlock it.
  1654. */
  1655. spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
  1656. {
  1657. spinlock_t *ptl;
  1658. ptl = pmd_lock(vma->vm_mm, pmd);
  1659. if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
  1660. pmd_devmap(*pmd)))
  1661. return ptl;
  1662. spin_unlock(ptl);
  1663. return NULL;
  1664. }
  1665. /*
  1666. * Returns true if a given pud maps a thp, false otherwise.
  1667. *
  1668. * Note that if it returns true, this routine returns without unlocking page
  1669. * table lock. So callers must unlock it.
  1670. */
  1671. spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
  1672. {
  1673. spinlock_t *ptl;
  1674. ptl = pud_lock(vma->vm_mm, pud);
  1675. if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
  1676. return ptl;
  1677. spin_unlock(ptl);
  1678. return NULL;
  1679. }
  1680. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  1681. int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
  1682. pud_t *pud, unsigned long addr)
  1683. {
  1684. spinlock_t *ptl;
  1685. ptl = __pud_trans_huge_lock(pud, vma);
  1686. if (!ptl)
  1687. return 0;
  1688. /*
  1689. * For architectures like ppc64 we look at deposited pgtable
  1690. * when calling pudp_huge_get_and_clear. So do the
  1691. * pgtable_trans_huge_withdraw after finishing pudp related
  1692. * operations.
  1693. */
  1694. pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm);
  1695. tlb_remove_pud_tlb_entry(tlb, pud, addr);
  1696. if (vma_is_special_huge(vma)) {
  1697. spin_unlock(ptl);
  1698. /* No zero page support yet */
  1699. } else {
  1700. /* No support for anonymous PUD pages yet */
  1701. BUG();
  1702. }
  1703. return 1;
  1704. }
  1705. static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
  1706. unsigned long haddr)
  1707. {
  1708. VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
  1709. VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
  1710. VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
  1711. VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
  1712. count_vm_event(THP_SPLIT_PUD);
  1713. pudp_huge_clear_flush_notify(vma, haddr, pud);
  1714. }
  1715. void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
  1716. unsigned long address)
  1717. {
  1718. spinlock_t *ptl;
  1719. struct mmu_notifier_range range;
  1720. mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
  1721. address & HPAGE_PUD_MASK,
  1722. (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
  1723. mmu_notifier_invalidate_range_start(&range);
  1724. ptl = pud_lock(vma->vm_mm, pud);
  1725. if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
  1726. goto out;
  1727. __split_huge_pud_locked(vma, pud, range.start);
  1728. out:
  1729. spin_unlock(ptl);
  1730. /*
  1731. * No need to double call mmu_notifier->invalidate_range() callback as
  1732. * the above pudp_huge_clear_flush_notify() did already call it.
  1733. */
  1734. mmu_notifier_invalidate_range_only_end(&range);
  1735. }
  1736. #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  1737. static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
  1738. unsigned long haddr, pmd_t *pmd)
  1739. {
  1740. struct mm_struct *mm = vma->vm_mm;
  1741. pgtable_t pgtable;
  1742. pmd_t _pmd;
  1743. int i;
  1744. /*
  1745. * Leave pmd empty until pte is filled note that it is fine to delay
  1746. * notification until mmu_notifier_invalidate_range_end() as we are
  1747. * replacing a zero pmd write protected page with a zero pte write
  1748. * protected page.
  1749. *
  1750. * See Documentation/vm/mmu_notifier.rst
  1751. */
  1752. pmdp_huge_clear_flush(vma, haddr, pmd);
  1753. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1754. pmd_populate(mm, &_pmd, pgtable);
  1755. for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
  1756. pte_t *pte, entry;
  1757. entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
  1758. entry = pte_mkspecial(entry);
  1759. pte = pte_offset_map(&_pmd, haddr);
  1760. VM_BUG_ON(!pte_none(*pte));
  1761. set_pte_at(mm, haddr, pte, entry);
  1762. pte_unmap(pte);
  1763. }
  1764. smp_wmb(); /* make pte visible before pmd */
  1765. pmd_populate(mm, pmd, pgtable);
  1766. }
  1767. static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
  1768. unsigned long haddr, bool freeze)
  1769. {
  1770. struct mm_struct *mm = vma->vm_mm;
  1771. struct page *page;
  1772. pgtable_t pgtable;
  1773. pmd_t old_pmd, _pmd;
  1774. bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false;
  1775. unsigned long addr;
  1776. int i;
  1777. VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
  1778. VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
  1779. VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
  1780. VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
  1781. && !pmd_devmap(*pmd));
  1782. count_vm_event(THP_SPLIT_PMD);
  1783. if (!vma_is_anonymous(vma)) {
  1784. old_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
  1785. /*
  1786. * We are going to unmap this huge page. So
  1787. * just go ahead and zap it
  1788. */
  1789. if (arch_needs_pgtable_deposit())
  1790. zap_deposited_table(mm, pmd);
  1791. if (vma_is_special_huge(vma))
  1792. return;
  1793. if (unlikely(is_pmd_migration_entry(old_pmd))) {
  1794. swp_entry_t entry;
  1795. entry = pmd_to_swp_entry(old_pmd);
  1796. page = migration_entry_to_page(entry);
  1797. } else {
  1798. page = pmd_page(old_pmd);
  1799. if (!PageDirty(page) && pmd_dirty(old_pmd))
  1800. set_page_dirty(page);
  1801. if (!PageReferenced(page) && pmd_young(old_pmd))
  1802. SetPageReferenced(page);
  1803. page_remove_rmap(page, true);
  1804. put_page(page);
  1805. }
  1806. add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
  1807. return;
  1808. }
  1809. if (is_huge_zero_pmd(*pmd)) {
  1810. /*
  1811. * FIXME: Do we want to invalidate secondary mmu by calling
  1812. * mmu_notifier_invalidate_range() see comments below inside
  1813. * __split_huge_pmd() ?
  1814. *
  1815. * We are going from a zero huge page write protected to zero
  1816. * small page also write protected so it does not seems useful
  1817. * to invalidate secondary mmu at this time.
  1818. */
  1819. return __split_huge_zero_page_pmd(vma, haddr, pmd);
  1820. }
  1821. /*
  1822. * Up to this point the pmd is present and huge and userland has the
  1823. * whole access to the hugepage during the split (which happens in
  1824. * place). If we overwrite the pmd with the not-huge version pointing
  1825. * to the pte here (which of course we could if all CPUs were bug
  1826. * free), userland could trigger a small page size TLB miss on the
  1827. * small sized TLB while the hugepage TLB entry is still established in
  1828. * the huge TLB. Some CPU doesn't like that.
  1829. * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
  1830. * 383 on page 105. Intel should be safe but is also warns that it's
  1831. * only safe if the permission and cache attributes of the two entries
  1832. * loaded in the two TLB is identical (which should be the case here).
  1833. * But it is generally safer to never allow small and huge TLB entries
  1834. * for the same virtual address to be loaded simultaneously. So instead
  1835. * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
  1836. * current pmd notpresent (atomically because here the pmd_trans_huge
  1837. * must remain set at all times on the pmd until the split is complete
  1838. * for this pmd), then we flush the SMP TLB and finally we write the
  1839. * non-huge version of the pmd entry with pmd_populate.
  1840. */
  1841. old_pmd = pmdp_invalidate(vma, haddr, pmd);
  1842. pmd_migration = is_pmd_migration_entry(old_pmd);
  1843. if (unlikely(pmd_migration)) {
  1844. swp_entry_t entry;
  1845. entry = pmd_to_swp_entry(old_pmd);
  1846. page = pfn_to_page(swp_offset(entry));
  1847. write = is_write_migration_entry(entry);
  1848. young = false;
  1849. soft_dirty = pmd_swp_soft_dirty(old_pmd);
  1850. uffd_wp = pmd_swp_uffd_wp(old_pmd);
  1851. } else {
  1852. page = pmd_page(old_pmd);
  1853. if (pmd_dirty(old_pmd))
  1854. SetPageDirty(page);
  1855. write = pmd_write(old_pmd);
  1856. young = pmd_young(old_pmd);
  1857. soft_dirty = pmd_soft_dirty(old_pmd);
  1858. uffd_wp = pmd_uffd_wp(old_pmd);
  1859. }
  1860. VM_BUG_ON_PAGE(!page_count(page), page);
  1861. page_ref_add(page, HPAGE_PMD_NR - 1);
  1862. /*
  1863. * Withdraw the table only after we mark the pmd entry invalid.
  1864. * This's critical for some architectures (Power).
  1865. */
  1866. pgtable = pgtable_trans_huge_withdraw(mm, pmd);
  1867. pmd_populate(mm, &_pmd, pgtable);
  1868. for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
  1869. pte_t entry, *pte;
  1870. /*
  1871. * Note that NUMA hinting access restrictions are not
  1872. * transferred to avoid any possibility of altering
  1873. * permissions across VMAs.
  1874. */
  1875. if (freeze || pmd_migration) {
  1876. swp_entry_t swp_entry;
  1877. swp_entry = make_migration_entry(page + i, write);
  1878. entry = swp_entry_to_pte(swp_entry);
  1879. if (soft_dirty)
  1880. entry = pte_swp_mksoft_dirty(entry);
  1881. if (uffd_wp)
  1882. entry = pte_swp_mkuffd_wp(entry);
  1883. } else {
  1884. entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
  1885. entry = maybe_mkwrite(entry, vma->vm_flags);
  1886. if (!write)
  1887. entry = pte_wrprotect(entry);
  1888. if (!young)
  1889. entry = pte_mkold(entry);
  1890. if (soft_dirty)
  1891. entry = pte_mksoft_dirty(entry);
  1892. if (uffd_wp)
  1893. entry = pte_mkuffd_wp(entry);
  1894. }
  1895. pte = pte_offset_map(&_pmd, addr);
  1896. BUG_ON(!pte_none(*pte));
  1897. set_pte_at(mm, addr, pte, entry);
  1898. if (!pmd_migration)
  1899. atomic_inc(&page[i]._mapcount);
  1900. pte_unmap(pte);
  1901. }
  1902. if (!pmd_migration) {
  1903. /*
  1904. * Set PG_double_map before dropping compound_mapcount to avoid
  1905. * false-negative page_mapped().
  1906. */
  1907. if (compound_mapcount(page) > 1 &&
  1908. !TestSetPageDoubleMap(page)) {
  1909. for (i = 0; i < HPAGE_PMD_NR; i++)
  1910. atomic_inc(&page[i]._mapcount);
  1911. }
  1912. lock_page_memcg(page);
  1913. if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
  1914. /* Last compound_mapcount is gone. */
  1915. __dec_lruvec_page_state(page, NR_ANON_THPS);
  1916. if (TestClearPageDoubleMap(page)) {
  1917. /* No need in mapcount reference anymore */
  1918. for (i = 0; i < HPAGE_PMD_NR; i++)
  1919. atomic_dec(&page[i]._mapcount);
  1920. }
  1921. }
  1922. unlock_page_memcg(page);
  1923. }
  1924. smp_wmb(); /* make pte visible before pmd */
  1925. pmd_populate(mm, pmd, pgtable);
  1926. if (freeze) {
  1927. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1928. page_remove_rmap(page + i, false);
  1929. put_page(page + i);
  1930. }
  1931. }
  1932. }
  1933. void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
  1934. unsigned long address, bool freeze, struct page *page)
  1935. {
  1936. spinlock_t *ptl;
  1937. struct mmu_notifier_range range;
  1938. bool do_unlock_page = false;
  1939. pmd_t _pmd;
  1940. mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
  1941. address & HPAGE_PMD_MASK,
  1942. (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
  1943. mmu_notifier_invalidate_range_start(&range);
  1944. ptl = pmd_lock(vma->vm_mm, pmd);
  1945. /*
  1946. * If caller asks to setup a migration entries, we need a page to check
  1947. * pmd against. Otherwise we can end up replacing wrong page.
  1948. */
  1949. VM_BUG_ON(freeze && !page);
  1950. if (page) {
  1951. VM_WARN_ON_ONCE(!PageLocked(page));
  1952. if (page != pmd_page(*pmd))
  1953. goto out;
  1954. }
  1955. repeat:
  1956. if (pmd_trans_huge(*pmd)) {
  1957. if (!page) {
  1958. page = pmd_page(*pmd);
  1959. /*
  1960. * An anonymous page must be locked, to ensure that a
  1961. * concurrent reuse_swap_page() sees stable mapcount;
  1962. * but reuse_swap_page() is not used on shmem or file,
  1963. * and page lock must not be taken when zap_pmd_range()
  1964. * calls __split_huge_pmd() while i_mmap_lock is held.
  1965. */
  1966. if (PageAnon(page)) {
  1967. if (unlikely(!trylock_page(page))) {
  1968. get_page(page);
  1969. _pmd = *pmd;
  1970. spin_unlock(ptl);
  1971. lock_page(page);
  1972. spin_lock(ptl);
  1973. if (unlikely(!pmd_same(*pmd, _pmd))) {
  1974. unlock_page(page);
  1975. put_page(page);
  1976. page = NULL;
  1977. goto repeat;
  1978. }
  1979. put_page(page);
  1980. }
  1981. do_unlock_page = true;
  1982. }
  1983. }
  1984. if (PageMlocked(page))
  1985. clear_page_mlock(page);
  1986. } else if (!(pmd_devmap(*pmd) || is_pmd_migration_entry(*pmd)))
  1987. goto out;
  1988. __split_huge_pmd_locked(vma, pmd, range.start, freeze);
  1989. out:
  1990. spin_unlock(ptl);
  1991. if (do_unlock_page)
  1992. unlock_page(page);
  1993. /*
  1994. * No need to double call mmu_notifier->invalidate_range() callback.
  1995. * They are 3 cases to consider inside __split_huge_pmd_locked():
  1996. * 1) pmdp_huge_clear_flush_notify() call invalidate_range() obvious
  1997. * 2) __split_huge_zero_page_pmd() read only zero page and any write
  1998. * fault will trigger a flush_notify before pointing to a new page
  1999. * (it is fine if the secondary mmu keeps pointing to the old zero
  2000. * page in the meantime)
  2001. * 3) Split a huge pmd into pte pointing to the same page. No need
  2002. * to invalidate secondary tlb entry they are all still valid.
  2003. * any further changes to individual pte will notify. So no need
  2004. * to call mmu_notifier->invalidate_range()
  2005. */
  2006. mmu_notifier_invalidate_range_only_end(&range);
  2007. }
  2008. void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
  2009. bool freeze, struct page *page)
  2010. {
  2011. pgd_t *pgd;
  2012. p4d_t *p4d;
  2013. pud_t *pud;
  2014. pmd_t *pmd;
  2015. pgd = pgd_offset(vma->vm_mm, address);
  2016. if (!pgd_present(*pgd))
  2017. return;
  2018. p4d = p4d_offset(pgd, address);
  2019. if (!p4d_present(*p4d))
  2020. return;
  2021. pud = pud_offset(p4d, address);
  2022. if (!pud_present(*pud))
  2023. return;
  2024. pmd = pmd_offset(pud, address);
  2025. __split_huge_pmd(vma, pmd, address, freeze, page);
  2026. }
  2027. void vma_adjust_trans_huge(struct vm_area_struct *vma,
  2028. unsigned long start,
  2029. unsigned long end,
  2030. long adjust_next)
  2031. {
  2032. /*
  2033. * If the new start address isn't hpage aligned and it could
  2034. * previously contain an hugepage: check if we need to split
  2035. * an huge pmd.
  2036. */
  2037. if (start & ~HPAGE_PMD_MASK &&
  2038. (start & HPAGE_PMD_MASK) >= vma->vm_start &&
  2039. (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2040. split_huge_pmd_address(vma, start, false, NULL);
  2041. /*
  2042. * If the new end address isn't hpage aligned and it could
  2043. * previously contain an hugepage: check if we need to split
  2044. * an huge pmd.
  2045. */
  2046. if (end & ~HPAGE_PMD_MASK &&
  2047. (end & HPAGE_PMD_MASK) >= vma->vm_start &&
  2048. (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
  2049. split_huge_pmd_address(vma, end, false, NULL);
  2050. /*
  2051. * If we're also updating the vma->vm_next->vm_start, if the new
  2052. * vm_next->vm_start isn't hpage aligned and it could previously
  2053. * contain an hugepage: check if we need to split an huge pmd.
  2054. */
  2055. if (adjust_next > 0) {
  2056. struct vm_area_struct *next = vma->vm_next;
  2057. unsigned long nstart = next->vm_start;
  2058. nstart += adjust_next;
  2059. if (nstart & ~HPAGE_PMD_MASK &&
  2060. (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
  2061. (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
  2062. split_huge_pmd_address(next, nstart, false, NULL);
  2063. }
  2064. }
  2065. static void unmap_page(struct page *page)
  2066. {
  2067. enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_SYNC |
  2068. TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD;
  2069. VM_BUG_ON_PAGE(!PageHead(page), page);
  2070. if (PageAnon(page))
  2071. ttu_flags |= TTU_SPLIT_FREEZE;
  2072. try_to_unmap(page, ttu_flags);
  2073. VM_WARN_ON_ONCE_PAGE(page_mapped(page), page);
  2074. }
  2075. static void remap_page(struct page *page, unsigned int nr)
  2076. {
  2077. int i;
  2078. if (PageTransHuge(page)) {
  2079. remove_migration_ptes(page, page, true);
  2080. } else {
  2081. for (i = 0; i < nr; i++)
  2082. remove_migration_ptes(page + i, page + i, true);
  2083. }
  2084. }
  2085. static void __split_huge_page_tail(struct page *head, int tail,
  2086. struct lruvec *lruvec, struct list_head *list)
  2087. {
  2088. struct page *page_tail = head + tail;
  2089. VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
  2090. /*
  2091. * Clone page flags before unfreezing refcount.
  2092. *
  2093. * After successful get_page_unless_zero() might follow flags change,
  2094. * for exmaple lock_page() which set PG_waiters.
  2095. */
  2096. page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  2097. page_tail->flags |= (head->flags &
  2098. ((1L << PG_referenced) |
  2099. (1L << PG_swapbacked) |
  2100. (1L << PG_swapcache) |
  2101. (1L << PG_mlocked) |
  2102. (1L << PG_uptodate) |
  2103. (1L << PG_active) |
  2104. (1L << PG_workingset) |
  2105. (1L << PG_locked) |
  2106. (1L << PG_unevictable) |
  2107. #ifdef CONFIG_64BIT
  2108. (1L << PG_arch_2) |
  2109. #endif
  2110. (1L << PG_dirty)));
  2111. /* ->mapping in first tail page is compound_mapcount */
  2112. VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
  2113. page_tail);
  2114. page_tail->mapping = head->mapping;
  2115. page_tail->index = head->index + tail;
  2116. /* Page flags must be visible before we make the page non-compound. */
  2117. smp_wmb();
  2118. /*
  2119. * Clear PageTail before unfreezing page refcount.
  2120. *
  2121. * After successful get_page_unless_zero() might follow put_page()
  2122. * which needs correct compound_head().
  2123. */
  2124. clear_compound_head(page_tail);
  2125. /* Finally unfreeze refcount. Additional reference from page cache. */
  2126. page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
  2127. PageSwapCache(head)));
  2128. if (page_is_young(head))
  2129. set_page_young(page_tail);
  2130. if (page_is_idle(head))
  2131. set_page_idle(page_tail);
  2132. page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
  2133. /*
  2134. * always add to the tail because some iterators expect new
  2135. * pages to show after the currently processed elements - e.g.
  2136. * migrate_pages
  2137. */
  2138. lru_add_page_tail(head, page_tail, lruvec, list);
  2139. }
  2140. static void __split_huge_page(struct page *page, struct list_head *list,
  2141. pgoff_t end, unsigned long flags)
  2142. {
  2143. struct page *head = compound_head(page);
  2144. pg_data_t *pgdat = page_pgdat(head);
  2145. struct lruvec *lruvec;
  2146. struct address_space *swap_cache = NULL;
  2147. unsigned long offset = 0;
  2148. unsigned int nr = thp_nr_pages(head);
  2149. int i;
  2150. lruvec = mem_cgroup_page_lruvec(head, pgdat);
  2151. /* complete memcg works before add pages to LRU */
  2152. split_page_memcg(head, nr);
  2153. if (PageAnon(head) && PageSwapCache(head)) {
  2154. swp_entry_t entry = { .val = page_private(head) };
  2155. offset = swp_offset(entry);
  2156. swap_cache = swap_address_space(entry);
  2157. xa_lock(&swap_cache->i_pages);
  2158. }
  2159. for (i = nr - 1; i >= 1; i--) {
  2160. __split_huge_page_tail(head, i, lruvec, list);
  2161. /* Some pages can be beyond i_size: drop them from page cache */
  2162. if (head[i].index >= end) {
  2163. ClearPageDirty(head + i);
  2164. __delete_from_page_cache(head + i, NULL);
  2165. if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
  2166. shmem_uncharge(head->mapping->host, 1);
  2167. put_page(head + i);
  2168. } else if (!PageAnon(page)) {
  2169. __xa_store(&head->mapping->i_pages, head[i].index,
  2170. head + i, 0);
  2171. } else if (swap_cache) {
  2172. __xa_store(&swap_cache->i_pages, offset + i,
  2173. head + i, 0);
  2174. }
  2175. }
  2176. ClearPageCompound(head);
  2177. split_page_owner(head, nr);
  2178. /* See comment in __split_huge_page_tail() */
  2179. if (PageAnon(head)) {
  2180. /* Additional pin to swap cache */
  2181. if (PageSwapCache(head)) {
  2182. page_ref_add(head, 2);
  2183. xa_unlock(&swap_cache->i_pages);
  2184. } else {
  2185. page_ref_inc(head);
  2186. }
  2187. } else {
  2188. /* Additional pin to page cache */
  2189. page_ref_add(head, 2);
  2190. xa_unlock(&head->mapping->i_pages);
  2191. }
  2192. spin_unlock_irqrestore(&pgdat->lru_lock, flags);
  2193. remap_page(head, nr);
  2194. if (PageSwapCache(head)) {
  2195. swp_entry_t entry = { .val = page_private(head) };
  2196. split_swap_cluster(entry);
  2197. }
  2198. for (i = 0; i < nr; i++) {
  2199. struct page *subpage = head + i;
  2200. if (subpage == page)
  2201. continue;
  2202. unlock_page(subpage);
  2203. /*
  2204. * Subpages may be freed if there wasn't any mapping
  2205. * like if add_to_swap() is running on a lru page that
  2206. * had its mapping zapped. And freeing these pages
  2207. * requires taking the lru_lock so we do the put_page
  2208. * of the tail pages after the split is complete.
  2209. */
  2210. put_page(subpage);
  2211. }
  2212. }
  2213. int total_mapcount(struct page *page)
  2214. {
  2215. int i, compound, nr, ret;
  2216. VM_BUG_ON_PAGE(PageTail(page), page);
  2217. if (likely(!PageCompound(page)))
  2218. return atomic_read(&page->_mapcount) + 1;
  2219. compound = compound_mapcount(page);
  2220. nr = compound_nr(page);
  2221. if (PageHuge(page))
  2222. return compound;
  2223. ret = compound;
  2224. for (i = 0; i < nr; i++)
  2225. ret += atomic_read(&page[i]._mapcount) + 1;
  2226. /* File pages has compound_mapcount included in _mapcount */
  2227. if (!PageAnon(page))
  2228. return ret - compound * nr;
  2229. if (PageDoubleMap(page))
  2230. ret -= nr;
  2231. return ret;
  2232. }
  2233. /*
  2234. * This calculates accurately how many mappings a transparent hugepage
  2235. * has (unlike page_mapcount() which isn't fully accurate). This full
  2236. * accuracy is primarily needed to know if copy-on-write faults can
  2237. * reuse the page and change the mapping to read-write instead of
  2238. * copying them. At the same time this returns the total_mapcount too.
  2239. *
  2240. * The function returns the highest mapcount any one of the subpages
  2241. * has. If the return value is one, even if different processes are
  2242. * mapping different subpages of the transparent hugepage, they can
  2243. * all reuse it, because each process is reusing a different subpage.
  2244. *
  2245. * The total_mapcount is instead counting all virtual mappings of the
  2246. * subpages. If the total_mapcount is equal to "one", it tells the
  2247. * caller all mappings belong to the same "mm" and in turn the
  2248. * anon_vma of the transparent hugepage can become the vma->anon_vma
  2249. * local one as no other process may be mapping any of the subpages.
  2250. *
  2251. * It would be more accurate to replace page_mapcount() with
  2252. * page_trans_huge_mapcount(), however we only use
  2253. * page_trans_huge_mapcount() in the copy-on-write faults where we
  2254. * need full accuracy to avoid breaking page pinning, because
  2255. * page_trans_huge_mapcount() is slower than page_mapcount().
  2256. */
  2257. int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
  2258. {
  2259. int i, ret, _total_mapcount, mapcount;
  2260. /* hugetlbfs shouldn't call it */
  2261. VM_BUG_ON_PAGE(PageHuge(page), page);
  2262. if (likely(!PageTransCompound(page))) {
  2263. mapcount = atomic_read(&page->_mapcount) + 1;
  2264. if (total_mapcount)
  2265. *total_mapcount = mapcount;
  2266. return mapcount;
  2267. }
  2268. page = compound_head(page);
  2269. _total_mapcount = ret = 0;
  2270. for (i = 0; i < thp_nr_pages(page); i++) {
  2271. mapcount = atomic_read(&page[i]._mapcount) + 1;
  2272. ret = max(ret, mapcount);
  2273. _total_mapcount += mapcount;
  2274. }
  2275. if (PageDoubleMap(page)) {
  2276. ret -= 1;
  2277. _total_mapcount -= thp_nr_pages(page);
  2278. }
  2279. mapcount = compound_mapcount(page);
  2280. ret += mapcount;
  2281. _total_mapcount += mapcount;
  2282. if (total_mapcount)
  2283. *total_mapcount = _total_mapcount;
  2284. return ret;
  2285. }
  2286. /* Racy check whether the huge page can be split */
  2287. bool can_split_huge_page(struct page *page, int *pextra_pins)
  2288. {
  2289. int extra_pins;
  2290. /* Additional pins from page cache */
  2291. if (PageAnon(page))
  2292. extra_pins = PageSwapCache(page) ? thp_nr_pages(page) : 0;
  2293. else
  2294. extra_pins = thp_nr_pages(page);
  2295. if (pextra_pins)
  2296. *pextra_pins = extra_pins;
  2297. return total_mapcount(page) == page_count(page) - extra_pins - 1;
  2298. }
  2299. /*
  2300. * This function splits huge page into normal pages. @page can point to any
  2301. * subpage of huge page to split. Split doesn't change the position of @page.
  2302. *
  2303. * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
  2304. * The huge page must be locked.
  2305. *
  2306. * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
  2307. *
  2308. * Both head page and tail pages will inherit mapping, flags, and so on from
  2309. * the hugepage.
  2310. *
  2311. * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
  2312. * they are not mapped.
  2313. *
  2314. * Returns 0 if the hugepage is split successfully.
  2315. * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
  2316. * us.
  2317. */
  2318. int split_huge_page_to_list(struct page *page, struct list_head *list)
  2319. {
  2320. struct page *head = compound_head(page);
  2321. struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
  2322. struct deferred_split *ds_queue = get_deferred_split_queue(head);
  2323. struct anon_vma *anon_vma = NULL;
  2324. struct address_space *mapping = NULL;
  2325. int extra_pins, ret;
  2326. unsigned long flags;
  2327. pgoff_t end;
  2328. VM_BUG_ON_PAGE(is_huge_zero_page(head), head);
  2329. VM_BUG_ON_PAGE(!PageLocked(head), head);
  2330. VM_BUG_ON_PAGE(!PageCompound(head), head);
  2331. if (PageWriteback(head))
  2332. return -EBUSY;
  2333. if (PageAnon(head)) {
  2334. /*
  2335. * The caller does not necessarily hold an mmap_lock that would
  2336. * prevent the anon_vma disappearing so we first we take a
  2337. * reference to it and then lock the anon_vma for write. This
  2338. * is similar to page_lock_anon_vma_read except the write lock
  2339. * is taken to serialise against parallel split or collapse
  2340. * operations.
  2341. */
  2342. anon_vma = page_get_anon_vma(head);
  2343. if (!anon_vma) {
  2344. ret = -EBUSY;
  2345. goto out;
  2346. }
  2347. end = -1;
  2348. mapping = NULL;
  2349. anon_vma_lock_write(anon_vma);
  2350. } else {
  2351. mapping = head->mapping;
  2352. /* Truncated ? */
  2353. if (!mapping) {
  2354. ret = -EBUSY;
  2355. goto out;
  2356. }
  2357. anon_vma = NULL;
  2358. i_mmap_lock_read(mapping);
  2359. /*
  2360. *__split_huge_page() may need to trim off pages beyond EOF:
  2361. * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
  2362. * which cannot be nested inside the page tree lock. So note
  2363. * end now: i_size itself may be changed at any moment, but
  2364. * head page lock is good enough to serialize the trimming.
  2365. */
  2366. end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
  2367. }
  2368. /*
  2369. * Racy check if we can split the page, before unmap_page() will
  2370. * split PMDs
  2371. */
  2372. if (!can_split_huge_page(head, &extra_pins)) {
  2373. ret = -EBUSY;
  2374. goto out_unlock;
  2375. }
  2376. unmap_page(head);
  2377. /* prevent PageLRU to go away from under us, and freeze lru stats */
  2378. spin_lock_irqsave(&pgdata->lru_lock, flags);
  2379. if (mapping) {
  2380. XA_STATE(xas, &mapping->i_pages, page_index(head));
  2381. /*
  2382. * Check if the head page is present in page cache.
  2383. * We assume all tail are present too, if head is there.
  2384. */
  2385. xa_lock(&mapping->i_pages);
  2386. if (xas_load(&xas) != head)
  2387. goto fail;
  2388. }
  2389. /* Prevent deferred_split_scan() touching ->_refcount */
  2390. spin_lock(&ds_queue->split_queue_lock);
  2391. if (page_ref_freeze(head, 1 + extra_pins)) {
  2392. if (!list_empty(page_deferred_list(head))) {
  2393. ds_queue->split_queue_len--;
  2394. list_del(page_deferred_list(head));
  2395. }
  2396. spin_unlock(&ds_queue->split_queue_lock);
  2397. if (mapping) {
  2398. if (PageSwapBacked(head))
  2399. __dec_node_page_state(head, NR_SHMEM_THPS);
  2400. else
  2401. __dec_node_page_state(head, NR_FILE_THPS);
  2402. }
  2403. __split_huge_page(page, list, end, flags);
  2404. ret = 0;
  2405. } else {
  2406. spin_unlock(&ds_queue->split_queue_lock);
  2407. fail:
  2408. if (mapping)
  2409. xa_unlock(&mapping->i_pages);
  2410. spin_unlock_irqrestore(&pgdata->lru_lock, flags);
  2411. remap_page(head, thp_nr_pages(head));
  2412. ret = -EBUSY;
  2413. }
  2414. out_unlock:
  2415. if (anon_vma) {
  2416. anon_vma_unlock_write(anon_vma);
  2417. put_anon_vma(anon_vma);
  2418. }
  2419. if (mapping)
  2420. i_mmap_unlock_read(mapping);
  2421. out:
  2422. count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
  2423. return ret;
  2424. }
  2425. void free_transhuge_page(struct page *page)
  2426. {
  2427. struct deferred_split *ds_queue = get_deferred_split_queue(page);
  2428. unsigned long flags;
  2429. spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
  2430. if (!list_empty(page_deferred_list(page))) {
  2431. ds_queue->split_queue_len--;
  2432. list_del(page_deferred_list(page));
  2433. }
  2434. spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
  2435. free_compound_page(page);
  2436. }
  2437. void deferred_split_huge_page(struct page *page)
  2438. {
  2439. struct deferred_split *ds_queue = get_deferred_split_queue(page);
  2440. #ifdef CONFIG_MEMCG
  2441. struct mem_cgroup *memcg = compound_head(page)->mem_cgroup;
  2442. #endif
  2443. unsigned long flags;
  2444. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  2445. /*
  2446. * The try_to_unmap() in page reclaim path might reach here too,
  2447. * this may cause a race condition to corrupt deferred split queue.
  2448. * And, if page reclaim is already handling the same page, it is
  2449. * unnecessary to handle it again in shrinker.
  2450. *
  2451. * Check PageSwapCache to determine if the page is being
  2452. * handled by page reclaim since THP swap would add the page into
  2453. * swap cache before calling try_to_unmap().
  2454. */
  2455. if (PageSwapCache(page))
  2456. return;
  2457. spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
  2458. if (list_empty(page_deferred_list(page))) {
  2459. count_vm_event(THP_DEFERRED_SPLIT_PAGE);
  2460. list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
  2461. ds_queue->split_queue_len++;
  2462. #ifdef CONFIG_MEMCG
  2463. if (memcg)
  2464. memcg_set_shrinker_bit(memcg, page_to_nid(page),
  2465. deferred_split_shrinker.id);
  2466. #endif
  2467. }
  2468. spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
  2469. }
  2470. static unsigned long deferred_split_count(struct shrinker *shrink,
  2471. struct shrink_control *sc)
  2472. {
  2473. struct pglist_data *pgdata = NODE_DATA(sc->nid);
  2474. struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
  2475. #ifdef CONFIG_MEMCG
  2476. if (sc->memcg)
  2477. ds_queue = &sc->memcg->deferred_split_queue;
  2478. #endif
  2479. return READ_ONCE(ds_queue->split_queue_len);
  2480. }
  2481. static unsigned long deferred_split_scan(struct shrinker *shrink,
  2482. struct shrink_control *sc)
  2483. {
  2484. struct pglist_data *pgdata = NODE_DATA(sc->nid);
  2485. struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
  2486. unsigned long flags;
  2487. LIST_HEAD(list), *pos, *next;
  2488. struct page *page;
  2489. int split = 0;
  2490. #ifdef CONFIG_MEMCG
  2491. if (sc->memcg)
  2492. ds_queue = &sc->memcg->deferred_split_queue;
  2493. #endif
  2494. spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
  2495. /* Take pin on all head pages to avoid freeing them under us */
  2496. list_for_each_safe(pos, next, &ds_queue->split_queue) {
  2497. page = list_entry((void *)pos, struct page, mapping);
  2498. page = compound_head(page);
  2499. if (get_page_unless_zero(page)) {
  2500. list_move(page_deferred_list(page), &list);
  2501. } else {
  2502. /* We lost race with put_compound_page() */
  2503. list_del_init(page_deferred_list(page));
  2504. ds_queue->split_queue_len--;
  2505. }
  2506. if (!--sc->nr_to_scan)
  2507. break;
  2508. }
  2509. spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
  2510. list_for_each_safe(pos, next, &list) {
  2511. page = list_entry((void *)pos, struct page, mapping);
  2512. if (!trylock_page(page))
  2513. goto next;
  2514. /* split_huge_page() removes page from list on success */
  2515. if (!split_huge_page(page))
  2516. split++;
  2517. unlock_page(page);
  2518. next:
  2519. put_page(page);
  2520. }
  2521. spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
  2522. list_splice_tail(&list, &ds_queue->split_queue);
  2523. spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
  2524. /*
  2525. * Stop shrinker if we didn't split any page, but the queue is empty.
  2526. * This can happen if pages were freed under us.
  2527. */
  2528. if (!split && list_empty(&ds_queue->split_queue))
  2529. return SHRINK_STOP;
  2530. return split;
  2531. }
  2532. static struct shrinker deferred_split_shrinker = {
  2533. .count_objects = deferred_split_count,
  2534. .scan_objects = deferred_split_scan,
  2535. .seeks = DEFAULT_SEEKS,
  2536. .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE |
  2537. SHRINKER_NONSLAB,
  2538. };
  2539. #ifdef CONFIG_DEBUG_FS
  2540. static int split_huge_pages_set(void *data, u64 val)
  2541. {
  2542. struct zone *zone;
  2543. struct page *page;
  2544. unsigned long pfn, max_zone_pfn;
  2545. unsigned long total = 0, split = 0;
  2546. if (val != 1)
  2547. return -EINVAL;
  2548. for_each_populated_zone(zone) {
  2549. max_zone_pfn = zone_end_pfn(zone);
  2550. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
  2551. if (!pfn_valid(pfn))
  2552. continue;
  2553. page = pfn_to_page(pfn);
  2554. if (!get_page_unless_zero(page))
  2555. continue;
  2556. if (zone != page_zone(page))
  2557. goto next;
  2558. if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
  2559. goto next;
  2560. total++;
  2561. lock_page(page);
  2562. if (!split_huge_page(page))
  2563. split++;
  2564. unlock_page(page);
  2565. next:
  2566. put_page(page);
  2567. }
  2568. }
  2569. pr_info("%lu of %lu THP split\n", split, total);
  2570. return 0;
  2571. }
  2572. DEFINE_DEBUGFS_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
  2573. "%llu\n");
  2574. static int __init split_huge_pages_debugfs(void)
  2575. {
  2576. debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
  2577. &split_huge_pages_fops);
  2578. return 0;
  2579. }
  2580. late_initcall(split_huge_pages_debugfs);
  2581. #endif
  2582. #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
  2583. void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
  2584. struct page *page)
  2585. {
  2586. struct vm_area_struct *vma = pvmw->vma;
  2587. struct mm_struct *mm = vma->vm_mm;
  2588. unsigned long address = pvmw->address;
  2589. pmd_t pmdval;
  2590. swp_entry_t entry;
  2591. pmd_t pmdswp;
  2592. if (!(pvmw->pmd && !pvmw->pte))
  2593. return;
  2594. flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
  2595. pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
  2596. if (pmd_dirty(pmdval))
  2597. set_page_dirty(page);
  2598. entry = make_migration_entry(page, pmd_write(pmdval));
  2599. pmdswp = swp_entry_to_pmd(entry);
  2600. if (pmd_soft_dirty(pmdval))
  2601. pmdswp = pmd_swp_mksoft_dirty(pmdswp);
  2602. set_pmd_at(mm, address, pvmw->pmd, pmdswp);
  2603. page_remove_rmap(page, true);
  2604. put_page(page);
  2605. }
  2606. void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
  2607. {
  2608. struct vm_area_struct *vma = pvmw->vma;
  2609. struct mm_struct *mm = vma->vm_mm;
  2610. unsigned long address = pvmw->address;
  2611. unsigned long mmun_start = address & HPAGE_PMD_MASK;
  2612. pmd_t pmde;
  2613. swp_entry_t entry;
  2614. if (!(pvmw->pmd && !pvmw->pte))
  2615. return;
  2616. entry = pmd_to_swp_entry(*pvmw->pmd);
  2617. get_page(new);
  2618. pmde = pmd_mkold(mk_huge_pmd(new, vma->vm_page_prot));
  2619. if (pmd_swp_soft_dirty(*pvmw->pmd))
  2620. pmde = pmd_mksoft_dirty(pmde);
  2621. if (is_write_migration_entry(entry))
  2622. pmde = maybe_pmd_mkwrite(pmde, vma);
  2623. if (pmd_swp_uffd_wp(*pvmw->pmd))
  2624. pmde = pmd_wrprotect(pmd_mkuffd_wp(pmde));
  2625. flush_cache_range(vma, mmun_start, mmun_start + HPAGE_PMD_SIZE);
  2626. if (PageAnon(new))
  2627. page_add_anon_rmap(new, vma, mmun_start, true);
  2628. else
  2629. page_add_file_rmap(new, true);
  2630. set_pmd_at(mm, mmun_start, pvmw->pmd, pmde);
  2631. if ((vma->vm_flags & VM_LOCKED) && !PageDoubleMap(new))
  2632. mlock_vma_page(new);
  2633. update_mmu_cache_pmd(vma, address, pvmw->pmd);
  2634. }
  2635. #endif