memory.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117
  1. /*
  2. * linux/mm/memory.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * demand-loading started 01.12.91 - seems it is high on the list of
  8. * things wanted, and it should be easy to implement. - Linus
  9. */
  10. /*
  11. * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  12. * pages started 02.12.91, seems to work. - Linus.
  13. *
  14. * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  15. * would have taken more than the 6M I have free, but it worked well as
  16. * far as I could see.
  17. *
  18. * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  19. */
  20. /*
  21. * Real VM (paging to/from disk) started 18.12.91. Much more work and
  22. * thought has to go into this. Oh, well..
  23. * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
  24. * Found it. Everything seems to work now.
  25. * 20.12.91 - Ok, making the swap-device changeable like the root.
  26. */
  27. /*
  28. * 05.04.94 - Multi-page memory management added for v1.1.
  29. * Idea by Alex Bligh (alex@cconcepts.co.uk)
  30. *
  31. * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
  32. * (Gerhard.Wichert@pdb.siemens.de)
  33. *
  34. * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
  35. */
  36. #include <linux/kernel_stat.h>
  37. #include <linux/mm.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/mman.h>
  40. #include <linux/swap.h>
  41. #include <linux/highmem.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/ksm.h>
  44. #include <linux/rmap.h>
  45. #include <linux/export.h>
  46. #include <linux/delayacct.h>
  47. #include <linux/init.h>
  48. #include <linux/pfn_t.h>
  49. #include <linux/writeback.h>
  50. #include <linux/memcontrol.h>
  51. #include <linux/mmu_notifier.h>
  52. #include <linux/kallsyms.h>
  53. #include <linux/swapops.h>
  54. #include <linux/elf.h>
  55. #include <linux/gfp.h>
  56. #include <linux/migrate.h>
  57. #include <linux/string.h>
  58. #include <linux/dma-debug.h>
  59. #include <linux/debugfs.h>
  60. #include <linux/userfaultfd_k.h>
  61. #include <linux/dax.h>
  62. #include <asm/io.h>
  63. #include <asm/mmu_context.h>
  64. #include <asm/pgalloc.h>
  65. #include <asm/uaccess.h>
  66. #include <asm/tlb.h>
  67. #include <asm/tlbflush.h>
  68. #include <asm/pgtable.h>
  69. #include "internal.h"
  70. #if defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS) && !defined(CONFIG_COMPILE_TEST)
  71. #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
  72. #endif
  73. #ifndef CONFIG_NEED_MULTIPLE_NODES
  74. /* use the per-pgdat data instead for discontigmem - mbligh */
  75. unsigned long max_mapnr;
  76. struct page *mem_map;
  77. EXPORT_SYMBOL(max_mapnr);
  78. EXPORT_SYMBOL(mem_map);
  79. #endif
  80. /*
  81. * A number of key systems in x86 including ioremap() rely on the assumption
  82. * that high_memory defines the upper bound on direct map memory, then end
  83. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  84. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  85. * and ZONE_HIGHMEM.
  86. */
  87. void * high_memory;
  88. EXPORT_SYMBOL(high_memory);
  89. /*
  90. * Randomize the address space (stacks, mmaps, brk, etc.).
  91. *
  92. * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
  93. * as ancient (libc5 based) binaries can segfault. )
  94. */
  95. int randomize_va_space __read_mostly =
  96. #ifdef CONFIG_COMPAT_BRK
  97. 1;
  98. #else
  99. 2;
  100. #endif
  101. static int __init disable_randmaps(char *s)
  102. {
  103. randomize_va_space = 0;
  104. return 1;
  105. }
  106. __setup("norandmaps", disable_randmaps);
  107. unsigned long zero_pfn __read_mostly;
  108. unsigned long highest_memmap_pfn __read_mostly;
  109. EXPORT_SYMBOL(zero_pfn);
  110. /*
  111. * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
  112. */
  113. static int __init init_zero_pfn(void)
  114. {
  115. zero_pfn = page_to_pfn(ZERO_PAGE(0));
  116. return 0;
  117. }
  118. core_initcall(init_zero_pfn);
  119. #if defined(SPLIT_RSS_COUNTING)
  120. void sync_mm_rss(struct mm_struct *mm)
  121. {
  122. int i;
  123. for (i = 0; i < NR_MM_COUNTERS; i++) {
  124. if (current->rss_stat.count[i]) {
  125. add_mm_counter(mm, i, current->rss_stat.count[i]);
  126. current->rss_stat.count[i] = 0;
  127. }
  128. }
  129. current->rss_stat.events = 0;
  130. }
  131. static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
  132. {
  133. struct task_struct *task = current;
  134. if (likely(task->mm == mm))
  135. task->rss_stat.count[member] += val;
  136. else
  137. add_mm_counter(mm, member, val);
  138. }
  139. #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
  140. #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
  141. /* sync counter once per 64 page faults */
  142. #define TASK_RSS_EVENTS_THRESH (64)
  143. static void check_sync_rss_stat(struct task_struct *task)
  144. {
  145. if (unlikely(task != current))
  146. return;
  147. if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
  148. sync_mm_rss(task->mm);
  149. }
  150. #else /* SPLIT_RSS_COUNTING */
  151. #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
  152. #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
  153. static void check_sync_rss_stat(struct task_struct *task)
  154. {
  155. }
  156. #endif /* SPLIT_RSS_COUNTING */
  157. #ifdef HAVE_GENERIC_MMU_GATHER
  158. static bool tlb_next_batch(struct mmu_gather *tlb)
  159. {
  160. struct mmu_gather_batch *batch;
  161. batch = tlb->active;
  162. if (batch->next) {
  163. tlb->active = batch->next;
  164. return true;
  165. }
  166. if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
  167. return false;
  168. batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  169. if (!batch)
  170. return false;
  171. tlb->batch_count++;
  172. batch->next = NULL;
  173. batch->nr = 0;
  174. batch->max = MAX_GATHER_BATCH;
  175. tlb->active->next = batch;
  176. tlb->active = batch;
  177. return true;
  178. }
  179. /* tlb_gather_mmu
  180. * Called to initialize an (on-stack) mmu_gather structure for page-table
  181. * tear-down from @mm. The @fullmm argument is used when @mm is without
  182. * users and we're going to destroy the full address space (exit/execve).
  183. */
  184. void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
  185. {
  186. tlb->mm = mm;
  187. /* Is it from 0 to ~0? */
  188. tlb->fullmm = !(start | (end+1));
  189. tlb->need_flush_all = 0;
  190. tlb->local.next = NULL;
  191. tlb->local.nr = 0;
  192. tlb->local.max = ARRAY_SIZE(tlb->__pages);
  193. tlb->active = &tlb->local;
  194. tlb->batch_count = 0;
  195. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  196. tlb->batch = NULL;
  197. #endif
  198. tlb->page_size = 0;
  199. __tlb_reset_range(tlb);
  200. }
  201. static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
  202. {
  203. if (!tlb->end)
  204. return;
  205. tlb_flush(tlb);
  206. mmu_notifier_invalidate_range(tlb->mm, tlb->start, tlb->end);
  207. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  208. tlb_table_flush(tlb);
  209. #endif
  210. __tlb_reset_range(tlb);
  211. }
  212. static void tlb_flush_mmu_free(struct mmu_gather *tlb)
  213. {
  214. struct mmu_gather_batch *batch;
  215. for (batch = &tlb->local; batch && batch->nr; batch = batch->next) {
  216. free_pages_and_swap_cache(batch->pages, batch->nr);
  217. batch->nr = 0;
  218. }
  219. tlb->active = &tlb->local;
  220. }
  221. void tlb_flush_mmu(struct mmu_gather *tlb)
  222. {
  223. tlb_flush_mmu_tlbonly(tlb);
  224. tlb_flush_mmu_free(tlb);
  225. }
  226. /* tlb_finish_mmu
  227. * Called at the end of the shootdown operation to free up any resources
  228. * that were required.
  229. */
  230. void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  231. {
  232. struct mmu_gather_batch *batch, *next;
  233. tlb_flush_mmu(tlb);
  234. /* keep the page table cache within bounds */
  235. check_pgt_cache();
  236. for (batch = tlb->local.next; batch; batch = next) {
  237. next = batch->next;
  238. free_pages((unsigned long)batch, 0);
  239. }
  240. tlb->local.next = NULL;
  241. }
  242. /* __tlb_remove_page
  243. * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
  244. * handling the additional races in SMP caused by other CPUs caching valid
  245. * mappings in their TLBs. Returns the number of free page slots left.
  246. * When out of page slots we must call tlb_flush_mmu().
  247. *returns true if the caller should flush.
  248. */
  249. bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_size)
  250. {
  251. struct mmu_gather_batch *batch;
  252. VM_BUG_ON(!tlb->end);
  253. if (!tlb->page_size)
  254. tlb->page_size = page_size;
  255. else {
  256. if (page_size != tlb->page_size)
  257. return true;
  258. }
  259. batch = tlb->active;
  260. if (batch->nr == batch->max) {
  261. if (!tlb_next_batch(tlb))
  262. return true;
  263. batch = tlb->active;
  264. }
  265. VM_BUG_ON_PAGE(batch->nr > batch->max, page);
  266. batch->pages[batch->nr++] = page;
  267. return false;
  268. }
  269. #endif /* HAVE_GENERIC_MMU_GATHER */
  270. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  271. /*
  272. * See the comment near struct mmu_table_batch.
  273. */
  274. static void tlb_remove_table_smp_sync(void *arg)
  275. {
  276. /* Simply deliver the interrupt */
  277. }
  278. static void tlb_remove_table_one(void *table)
  279. {
  280. /*
  281. * This isn't an RCU grace period and hence the page-tables cannot be
  282. * assumed to be actually RCU-freed.
  283. *
  284. * It is however sufficient for software page-table walkers that rely on
  285. * IRQ disabling. See the comment near struct mmu_table_batch.
  286. */
  287. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  288. __tlb_remove_table(table);
  289. }
  290. static void tlb_remove_table_rcu(struct rcu_head *head)
  291. {
  292. struct mmu_table_batch *batch;
  293. int i;
  294. batch = container_of(head, struct mmu_table_batch, rcu);
  295. for (i = 0; i < batch->nr; i++)
  296. __tlb_remove_table(batch->tables[i]);
  297. free_page((unsigned long)batch);
  298. }
  299. void tlb_table_flush(struct mmu_gather *tlb)
  300. {
  301. struct mmu_table_batch **batch = &tlb->batch;
  302. if (*batch) {
  303. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  304. *batch = NULL;
  305. }
  306. }
  307. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  308. {
  309. struct mmu_table_batch **batch = &tlb->batch;
  310. /*
  311. * When there's less then two users of this mm there cannot be a
  312. * concurrent page-table walk.
  313. */
  314. if (atomic_read(&tlb->mm->mm_users) < 2) {
  315. __tlb_remove_table(table);
  316. return;
  317. }
  318. if (*batch == NULL) {
  319. *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  320. if (*batch == NULL) {
  321. tlb_remove_table_one(table);
  322. return;
  323. }
  324. (*batch)->nr = 0;
  325. }
  326. (*batch)->tables[(*batch)->nr++] = table;
  327. if ((*batch)->nr == MAX_TABLE_BATCH)
  328. tlb_table_flush(tlb);
  329. }
  330. #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
  331. /*
  332. * Note: this doesn't free the actual pages themselves. That
  333. * has been handled earlier when unmapping all the memory regions.
  334. */
  335. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
  336. unsigned long addr)
  337. {
  338. pgtable_t token = pmd_pgtable(*pmd);
  339. pmd_clear(pmd);
  340. pte_free_tlb(tlb, token, addr);
  341. atomic_long_dec(&tlb->mm->nr_ptes);
  342. }
  343. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  344. unsigned long addr, unsigned long end,
  345. unsigned long floor, unsigned long ceiling)
  346. {
  347. pmd_t *pmd;
  348. unsigned long next;
  349. unsigned long start;
  350. start = addr;
  351. pmd = pmd_offset(pud, addr);
  352. do {
  353. next = pmd_addr_end(addr, end);
  354. if (pmd_none_or_clear_bad(pmd))
  355. continue;
  356. free_pte_range(tlb, pmd, addr);
  357. } while (pmd++, addr = next, addr != end);
  358. start &= PUD_MASK;
  359. if (start < floor)
  360. return;
  361. if (ceiling) {
  362. ceiling &= PUD_MASK;
  363. if (!ceiling)
  364. return;
  365. }
  366. if (end - 1 > ceiling - 1)
  367. return;
  368. pmd = pmd_offset(pud, start);
  369. pud_clear(pud);
  370. pmd_free_tlb(tlb, pmd, start);
  371. mm_dec_nr_pmds(tlb->mm);
  372. }
  373. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  374. unsigned long addr, unsigned long end,
  375. unsigned long floor, unsigned long ceiling)
  376. {
  377. pud_t *pud;
  378. unsigned long next;
  379. unsigned long start;
  380. start = addr;
  381. pud = pud_offset(pgd, addr);
  382. do {
  383. next = pud_addr_end(addr, end);
  384. if (pud_none_or_clear_bad(pud))
  385. continue;
  386. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  387. } while (pud++, addr = next, addr != end);
  388. start &= PGDIR_MASK;
  389. if (start < floor)
  390. return;
  391. if (ceiling) {
  392. ceiling &= PGDIR_MASK;
  393. if (!ceiling)
  394. return;
  395. }
  396. if (end - 1 > ceiling - 1)
  397. return;
  398. pud = pud_offset(pgd, start);
  399. pgd_clear(pgd);
  400. pud_free_tlb(tlb, pud, start);
  401. }
  402. /*
  403. * This function frees user-level page tables of a process.
  404. */
  405. void free_pgd_range(struct mmu_gather *tlb,
  406. unsigned long addr, unsigned long end,
  407. unsigned long floor, unsigned long ceiling)
  408. {
  409. pgd_t *pgd;
  410. unsigned long next;
  411. /*
  412. * The next few lines have given us lots of grief...
  413. *
  414. * Why are we testing PMD* at this top level? Because often
  415. * there will be no work to do at all, and we'd prefer not to
  416. * go all the way down to the bottom just to discover that.
  417. *
  418. * Why all these "- 1"s? Because 0 represents both the bottom
  419. * of the address space and the top of it (using -1 for the
  420. * top wouldn't help much: the masks would do the wrong thing).
  421. * The rule is that addr 0 and floor 0 refer to the bottom of
  422. * the address space, but end 0 and ceiling 0 refer to the top
  423. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  424. * that end 0 case should be mythical).
  425. *
  426. * Wherever addr is brought up or ceiling brought down, we must
  427. * be careful to reject "the opposite 0" before it confuses the
  428. * subsequent tests. But what about where end is brought down
  429. * by PMD_SIZE below? no, end can't go down to 0 there.
  430. *
  431. * Whereas we round start (addr) and ceiling down, by different
  432. * masks at different levels, in order to test whether a table
  433. * now has no other vmas using it, so can be freed, we don't
  434. * bother to round floor or end up - the tests don't need that.
  435. */
  436. addr &= PMD_MASK;
  437. if (addr < floor) {
  438. addr += PMD_SIZE;
  439. if (!addr)
  440. return;
  441. }
  442. if (ceiling) {
  443. ceiling &= PMD_MASK;
  444. if (!ceiling)
  445. return;
  446. }
  447. if (end - 1 > ceiling - 1)
  448. end -= PMD_SIZE;
  449. if (addr > end - 1)
  450. return;
  451. pgd = pgd_offset(tlb->mm, addr);
  452. do {
  453. next = pgd_addr_end(addr, end);
  454. if (pgd_none_or_clear_bad(pgd))
  455. continue;
  456. free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  457. } while (pgd++, addr = next, addr != end);
  458. }
  459. void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
  460. unsigned long floor, unsigned long ceiling)
  461. {
  462. while (vma) {
  463. struct vm_area_struct *next = vma->vm_next;
  464. unsigned long addr = vma->vm_start;
  465. /*
  466. * Hide vma from rmap and truncate_pagecache before freeing
  467. * pgtables
  468. */
  469. unlink_anon_vmas(vma);
  470. unlink_file_vma(vma);
  471. if (is_vm_hugetlb_page(vma)) {
  472. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  473. floor, next? next->vm_start: ceiling);
  474. } else {
  475. /*
  476. * Optimization: gather nearby vmas into one call down
  477. */
  478. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  479. && !is_vm_hugetlb_page(next)) {
  480. vma = next;
  481. next = vma->vm_next;
  482. unlink_anon_vmas(vma);
  483. unlink_file_vma(vma);
  484. }
  485. free_pgd_range(tlb, addr, vma->vm_end,
  486. floor, next? next->vm_start: ceiling);
  487. }
  488. vma = next;
  489. }
  490. }
  491. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
  492. {
  493. spinlock_t *ptl;
  494. pgtable_t new = pte_alloc_one(mm, address);
  495. if (!new)
  496. return -ENOMEM;
  497. /*
  498. * Ensure all pte setup (eg. pte page lock and page clearing) are
  499. * visible before the pte is made visible to other CPUs by being
  500. * put into page tables.
  501. *
  502. * The other side of the story is the pointer chasing in the page
  503. * table walking code (when walking the page table without locking;
  504. * ie. most of the time). Fortunately, these data accesses consist
  505. * of a chain of data-dependent loads, meaning most CPUs (alpha
  506. * being the notable exception) will already guarantee loads are
  507. * seen in-order. See the alpha page table accessors for the
  508. * smp_read_barrier_depends() barriers in page table walking code.
  509. */
  510. smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
  511. ptl = pmd_lock(mm, pmd);
  512. if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
  513. atomic_long_inc(&mm->nr_ptes);
  514. pmd_populate(mm, pmd, new);
  515. new = NULL;
  516. }
  517. spin_unlock(ptl);
  518. if (new)
  519. pte_free(mm, new);
  520. return 0;
  521. }
  522. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  523. {
  524. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  525. if (!new)
  526. return -ENOMEM;
  527. smp_wmb(); /* See comment in __pte_alloc */
  528. spin_lock(&init_mm.page_table_lock);
  529. if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
  530. pmd_populate_kernel(&init_mm, pmd, new);
  531. new = NULL;
  532. }
  533. spin_unlock(&init_mm.page_table_lock);
  534. if (new)
  535. pte_free_kernel(&init_mm, new);
  536. return 0;
  537. }
  538. static inline void init_rss_vec(int *rss)
  539. {
  540. memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
  541. }
  542. static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
  543. {
  544. int i;
  545. if (current->mm == mm)
  546. sync_mm_rss(mm);
  547. for (i = 0; i < NR_MM_COUNTERS; i++)
  548. if (rss[i])
  549. add_mm_counter(mm, i, rss[i]);
  550. }
  551. /*
  552. * This function is called to print an error when a bad pte
  553. * is found. For example, we might have a PFN-mapped pte in
  554. * a region that doesn't allow it.
  555. *
  556. * The calling function must still handle the error.
  557. */
  558. static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
  559. pte_t pte, struct page *page)
  560. {
  561. pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
  562. pud_t *pud = pud_offset(pgd, addr);
  563. pmd_t *pmd = pmd_offset(pud, addr);
  564. struct address_space *mapping;
  565. pgoff_t index;
  566. static unsigned long resume;
  567. static unsigned long nr_shown;
  568. static unsigned long nr_unshown;
  569. /*
  570. * Allow a burst of 60 reports, then keep quiet for that minute;
  571. * or allow a steady drip of one report per second.
  572. */
  573. if (nr_shown == 60) {
  574. if (time_before(jiffies, resume)) {
  575. nr_unshown++;
  576. return;
  577. }
  578. if (nr_unshown) {
  579. pr_alert("BUG: Bad page map: %lu messages suppressed\n",
  580. nr_unshown);
  581. nr_unshown = 0;
  582. }
  583. nr_shown = 0;
  584. }
  585. if (nr_shown++ == 0)
  586. resume = jiffies + 60 * HZ;
  587. mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
  588. index = linear_page_index(vma, addr);
  589. pr_alert("BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
  590. current->comm,
  591. (long long)pte_val(pte), (long long)pmd_val(*pmd));
  592. if (page)
  593. dump_page(page, "bad pte");
  594. pr_alert("addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
  595. (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
  596. /*
  597. * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
  598. */
  599. pr_alert("file:%pD fault:%pf mmap:%pf readpage:%pf\n",
  600. vma->vm_file,
  601. vma->vm_ops ? vma->vm_ops->fault : NULL,
  602. vma->vm_file ? vma->vm_file->f_op->mmap : NULL,
  603. mapping ? mapping->a_ops->readpage : NULL);
  604. dump_stack();
  605. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  606. }
  607. /*
  608. * vm_normal_page -- This function gets the "struct page" associated with a pte.
  609. *
  610. * "Special" mappings do not wish to be associated with a "struct page" (either
  611. * it doesn't exist, or it exists but they don't want to touch it). In this
  612. * case, NULL is returned here. "Normal" mappings do have a struct page.
  613. *
  614. * There are 2 broad cases. Firstly, an architecture may define a pte_special()
  615. * pte bit, in which case this function is trivial. Secondly, an architecture
  616. * may not have a spare pte bit, which requires a more complicated scheme,
  617. * described below.
  618. *
  619. * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
  620. * special mapping (even if there are underlying and valid "struct pages").
  621. * COWed pages of a VM_PFNMAP are always normal.
  622. *
  623. * The way we recognize COWed pages within VM_PFNMAP mappings is through the
  624. * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
  625. * set, and the vm_pgoff will point to the first PFN mapped: thus every special
  626. * mapping will always honor the rule
  627. *
  628. * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
  629. *
  630. * And for normal mappings this is false.
  631. *
  632. * This restricts such mappings to be a linear translation from virtual address
  633. * to pfn. To get around this restriction, we allow arbitrary mappings so long
  634. * as the vma is not a COW mapping; in that case, we know that all ptes are
  635. * special (because none can have been COWed).
  636. *
  637. *
  638. * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
  639. *
  640. * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
  641. * page" backing, however the difference is that _all_ pages with a struct
  642. * page (that is, those where pfn_valid is true) are refcounted and considered
  643. * normal pages by the VM. The disadvantage is that pages are refcounted
  644. * (which can be slower and simply not an option for some PFNMAP users). The
  645. * advantage is that we don't have to follow the strict linearity rule of
  646. * PFNMAP mappings in order to support COWable mappings.
  647. *
  648. */
  649. #ifdef __HAVE_ARCH_PTE_SPECIAL
  650. # define HAVE_PTE_SPECIAL 1
  651. #else
  652. # define HAVE_PTE_SPECIAL 0
  653. #endif
  654. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
  655. pte_t pte)
  656. {
  657. unsigned long pfn = pte_pfn(pte);
  658. if (HAVE_PTE_SPECIAL) {
  659. if (likely(!pte_special(pte)))
  660. goto check_pfn;
  661. if (vma->vm_ops && vma->vm_ops->find_special_page)
  662. return vma->vm_ops->find_special_page(vma, addr);
  663. if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
  664. return NULL;
  665. if (!is_zero_pfn(pfn))
  666. print_bad_pte(vma, addr, pte, NULL);
  667. return NULL;
  668. }
  669. /* !HAVE_PTE_SPECIAL case follows: */
  670. if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
  671. if (vma->vm_flags & VM_MIXEDMAP) {
  672. if (!pfn_valid(pfn))
  673. return NULL;
  674. goto out;
  675. } else {
  676. unsigned long off;
  677. off = (addr - vma->vm_start) >> PAGE_SHIFT;
  678. if (pfn == vma->vm_pgoff + off)
  679. return NULL;
  680. if (!is_cow_mapping(vma->vm_flags))
  681. return NULL;
  682. }
  683. }
  684. if (is_zero_pfn(pfn))
  685. return NULL;
  686. check_pfn:
  687. if (unlikely(pfn > highest_memmap_pfn)) {
  688. print_bad_pte(vma, addr, pte, NULL);
  689. return NULL;
  690. }
  691. /*
  692. * NOTE! We still have PageReserved() pages in the page tables.
  693. * eg. VDSO mappings can cause them to exist.
  694. */
  695. out:
  696. return pfn_to_page(pfn);
  697. }
  698. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  699. struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
  700. pmd_t pmd)
  701. {
  702. unsigned long pfn = pmd_pfn(pmd);
  703. /*
  704. * There is no pmd_special() but there may be special pmds, e.g.
  705. * in a direct-access (dax) mapping, so let's just replicate the
  706. * !HAVE_PTE_SPECIAL case from vm_normal_page() here.
  707. */
  708. if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
  709. if (vma->vm_flags & VM_MIXEDMAP) {
  710. if (!pfn_valid(pfn))
  711. return NULL;
  712. goto out;
  713. } else {
  714. unsigned long off;
  715. off = (addr - vma->vm_start) >> PAGE_SHIFT;
  716. if (pfn == vma->vm_pgoff + off)
  717. return NULL;
  718. if (!is_cow_mapping(vma->vm_flags))
  719. return NULL;
  720. }
  721. }
  722. if (is_zero_pfn(pfn))
  723. return NULL;
  724. if (unlikely(pfn > highest_memmap_pfn))
  725. return NULL;
  726. /*
  727. * NOTE! We still have PageReserved() pages in the page tables.
  728. * eg. VDSO mappings can cause them to exist.
  729. */
  730. out:
  731. return pfn_to_page(pfn);
  732. }
  733. #endif
  734. /*
  735. * copy one vm_area from one task to the other. Assumes the page tables
  736. * already present in the new task to be cleared in the whole range
  737. * covered by this vma.
  738. */
  739. static inline unsigned long
  740. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  741. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  742. unsigned long addr, int *rss)
  743. {
  744. unsigned long vm_flags = vma->vm_flags;
  745. pte_t pte = *src_pte;
  746. struct page *page;
  747. /* pte contains position in swap or file, so copy. */
  748. if (unlikely(!pte_present(pte))) {
  749. swp_entry_t entry = pte_to_swp_entry(pte);
  750. if (likely(!non_swap_entry(entry))) {
  751. if (swap_duplicate(entry) < 0)
  752. return entry.val;
  753. /* make sure dst_mm is on swapoff's mmlist. */
  754. if (unlikely(list_empty(&dst_mm->mmlist))) {
  755. spin_lock(&mmlist_lock);
  756. if (list_empty(&dst_mm->mmlist))
  757. list_add(&dst_mm->mmlist,
  758. &src_mm->mmlist);
  759. spin_unlock(&mmlist_lock);
  760. }
  761. rss[MM_SWAPENTS]++;
  762. } else if (is_migration_entry(entry)) {
  763. page = migration_entry_to_page(entry);
  764. rss[mm_counter(page)]++;
  765. if (is_write_migration_entry(entry) &&
  766. is_cow_mapping(vm_flags)) {
  767. /*
  768. * COW mappings require pages in both
  769. * parent and child to be set to read.
  770. */
  771. make_migration_entry_read(&entry);
  772. pte = swp_entry_to_pte(entry);
  773. if (pte_swp_soft_dirty(*src_pte))
  774. pte = pte_swp_mksoft_dirty(pte);
  775. set_pte_at(src_mm, addr, src_pte, pte);
  776. }
  777. }
  778. goto out_set_pte;
  779. }
  780. /*
  781. * If it's a COW mapping, write protect it both
  782. * in the parent and the child
  783. */
  784. if (is_cow_mapping(vm_flags)) {
  785. ptep_set_wrprotect(src_mm, addr, src_pte);
  786. pte = pte_wrprotect(pte);
  787. }
  788. /*
  789. * If it's a shared mapping, mark it clean in
  790. * the child
  791. */
  792. if (vm_flags & VM_SHARED)
  793. pte = pte_mkclean(pte);
  794. pte = pte_mkold(pte);
  795. page = vm_normal_page(vma, addr, pte);
  796. if (page) {
  797. get_page(page);
  798. page_dup_rmap(page, false);
  799. rss[mm_counter(page)]++;
  800. }
  801. out_set_pte:
  802. set_pte_at(dst_mm, addr, dst_pte, pte);
  803. return 0;
  804. }
  805. static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  806. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  807. unsigned long addr, unsigned long end)
  808. {
  809. pte_t *orig_src_pte, *orig_dst_pte;
  810. pte_t *src_pte, *dst_pte;
  811. spinlock_t *src_ptl, *dst_ptl;
  812. int progress = 0;
  813. int rss[NR_MM_COUNTERS];
  814. swp_entry_t entry = (swp_entry_t){0};
  815. again:
  816. init_rss_vec(rss);
  817. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  818. if (!dst_pte)
  819. return -ENOMEM;
  820. src_pte = pte_offset_map(src_pmd, addr);
  821. src_ptl = pte_lockptr(src_mm, src_pmd);
  822. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  823. orig_src_pte = src_pte;
  824. orig_dst_pte = dst_pte;
  825. arch_enter_lazy_mmu_mode();
  826. do {
  827. /*
  828. * We are holding two locks at this point - either of them
  829. * could generate latencies in another task on another CPU.
  830. */
  831. if (progress >= 32) {
  832. progress = 0;
  833. if (need_resched() ||
  834. spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
  835. break;
  836. }
  837. if (pte_none(*src_pte)) {
  838. progress++;
  839. continue;
  840. }
  841. entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
  842. vma, addr, rss);
  843. if (entry.val)
  844. break;
  845. progress += 8;
  846. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  847. arch_leave_lazy_mmu_mode();
  848. spin_unlock(src_ptl);
  849. pte_unmap(orig_src_pte);
  850. add_mm_rss_vec(dst_mm, rss);
  851. pte_unmap_unlock(orig_dst_pte, dst_ptl);
  852. cond_resched();
  853. if (entry.val) {
  854. if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
  855. return -ENOMEM;
  856. progress = 0;
  857. }
  858. if (addr != end)
  859. goto again;
  860. return 0;
  861. }
  862. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  863. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  864. unsigned long addr, unsigned long end)
  865. {
  866. pmd_t *src_pmd, *dst_pmd;
  867. unsigned long next;
  868. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  869. if (!dst_pmd)
  870. return -ENOMEM;
  871. src_pmd = pmd_offset(src_pud, addr);
  872. do {
  873. next = pmd_addr_end(addr, end);
  874. if (pmd_trans_huge(*src_pmd) || pmd_devmap(*src_pmd)) {
  875. int err;
  876. VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
  877. err = copy_huge_pmd(dst_mm, src_mm,
  878. dst_pmd, src_pmd, addr, vma);
  879. if (err == -ENOMEM)
  880. return -ENOMEM;
  881. if (!err)
  882. continue;
  883. /* fall through */
  884. }
  885. if (pmd_none_or_clear_bad(src_pmd))
  886. continue;
  887. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  888. vma, addr, next))
  889. return -ENOMEM;
  890. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  891. return 0;
  892. }
  893. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  894. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  895. unsigned long addr, unsigned long end)
  896. {
  897. pud_t *src_pud, *dst_pud;
  898. unsigned long next;
  899. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  900. if (!dst_pud)
  901. return -ENOMEM;
  902. src_pud = pud_offset(src_pgd, addr);
  903. do {
  904. next = pud_addr_end(addr, end);
  905. if (pud_none_or_clear_bad(src_pud))
  906. continue;
  907. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  908. vma, addr, next))
  909. return -ENOMEM;
  910. } while (dst_pud++, src_pud++, addr = next, addr != end);
  911. return 0;
  912. }
  913. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  914. struct vm_area_struct *vma)
  915. {
  916. pgd_t *src_pgd, *dst_pgd;
  917. unsigned long next;
  918. unsigned long addr = vma->vm_start;
  919. unsigned long end = vma->vm_end;
  920. unsigned long mmun_start; /* For mmu_notifiers */
  921. unsigned long mmun_end; /* For mmu_notifiers */
  922. bool is_cow;
  923. int ret;
  924. /*
  925. * Don't copy ptes where a page fault will fill them correctly.
  926. * Fork becomes much lighter when there are big shared or private
  927. * readonly mappings. The tradeoff is that copy_page_range is more
  928. * efficient than faulting.
  929. */
  930. if (!(vma->vm_flags & (VM_HUGETLB | VM_PFNMAP | VM_MIXEDMAP)) &&
  931. !vma->anon_vma)
  932. return 0;
  933. if (is_vm_hugetlb_page(vma))
  934. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  935. if (unlikely(vma->vm_flags & VM_PFNMAP)) {
  936. /*
  937. * We do not free on error cases below as remove_vma
  938. * gets called on error from higher level routine
  939. */
  940. ret = track_pfn_copy(vma);
  941. if (ret)
  942. return ret;
  943. }
  944. /*
  945. * We need to invalidate the secondary MMU mappings only when
  946. * there could be a permission downgrade on the ptes of the
  947. * parent mm. And a permission downgrade will only happen if
  948. * is_cow_mapping() returns true.
  949. */
  950. is_cow = is_cow_mapping(vma->vm_flags);
  951. mmun_start = addr;
  952. mmun_end = end;
  953. if (is_cow)
  954. mmu_notifier_invalidate_range_start(src_mm, mmun_start,
  955. mmun_end);
  956. ret = 0;
  957. dst_pgd = pgd_offset(dst_mm, addr);
  958. src_pgd = pgd_offset(src_mm, addr);
  959. do {
  960. next = pgd_addr_end(addr, end);
  961. if (pgd_none_or_clear_bad(src_pgd))
  962. continue;
  963. if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  964. vma, addr, next))) {
  965. ret = -ENOMEM;
  966. break;
  967. }
  968. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  969. if (is_cow)
  970. mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
  971. return ret;
  972. }
  973. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  974. struct vm_area_struct *vma, pmd_t *pmd,
  975. unsigned long addr, unsigned long end,
  976. struct zap_details *details)
  977. {
  978. struct mm_struct *mm = tlb->mm;
  979. int force_flush = 0;
  980. int rss[NR_MM_COUNTERS];
  981. spinlock_t *ptl;
  982. pte_t *start_pte;
  983. pte_t *pte;
  984. swp_entry_t entry;
  985. struct page *pending_page = NULL;
  986. again:
  987. init_rss_vec(rss);
  988. start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  989. pte = start_pte;
  990. flush_tlb_batched_pending(mm);
  991. arch_enter_lazy_mmu_mode();
  992. do {
  993. pte_t ptent = *pte;
  994. if (pte_none(ptent)) {
  995. continue;
  996. }
  997. if (pte_present(ptent)) {
  998. struct page *page;
  999. page = vm_normal_page(vma, addr, ptent);
  1000. if (unlikely(details) && page) {
  1001. /*
  1002. * unmap_shared_mapping_pages() wants to
  1003. * invalidate cache without truncating:
  1004. * unmap shared but keep private pages.
  1005. */
  1006. if (details->check_mapping &&
  1007. details->check_mapping != page_rmapping(page))
  1008. continue;
  1009. }
  1010. ptent = ptep_get_and_clear_full(mm, addr, pte,
  1011. tlb->fullmm);
  1012. tlb_remove_tlb_entry(tlb, pte, addr);
  1013. if (unlikely(!page))
  1014. continue;
  1015. if (!PageAnon(page)) {
  1016. if (pte_dirty(ptent)) {
  1017. /*
  1018. * oom_reaper cannot tear down dirty
  1019. * pages
  1020. */
  1021. if (unlikely(details && details->ignore_dirty))
  1022. continue;
  1023. force_flush = 1;
  1024. set_page_dirty(page);
  1025. }
  1026. if (pte_young(ptent) &&
  1027. likely(!(vma->vm_flags & VM_SEQ_READ)))
  1028. mark_page_accessed(page);
  1029. }
  1030. rss[mm_counter(page)]--;
  1031. page_remove_rmap(page, false);
  1032. if (unlikely(page_mapcount(page) < 0))
  1033. print_bad_pte(vma, addr, ptent, page);
  1034. if (unlikely(__tlb_remove_page(tlb, page))) {
  1035. force_flush = 1;
  1036. pending_page = page;
  1037. addr += PAGE_SIZE;
  1038. break;
  1039. }
  1040. continue;
  1041. }
  1042. /* only check swap_entries if explicitly asked for in details */
  1043. if (unlikely(details && !details->check_swap_entries))
  1044. continue;
  1045. entry = pte_to_swp_entry(ptent);
  1046. if (!non_swap_entry(entry))
  1047. rss[MM_SWAPENTS]--;
  1048. else if (is_migration_entry(entry)) {
  1049. struct page *page;
  1050. page = migration_entry_to_page(entry);
  1051. rss[mm_counter(page)]--;
  1052. }
  1053. if (unlikely(!free_swap_and_cache(entry)))
  1054. print_bad_pte(vma, addr, ptent, NULL);
  1055. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  1056. } while (pte++, addr += PAGE_SIZE, addr != end);
  1057. add_mm_rss_vec(mm, rss);
  1058. arch_leave_lazy_mmu_mode();
  1059. /* Do the actual TLB flush before dropping ptl */
  1060. if (force_flush)
  1061. tlb_flush_mmu_tlbonly(tlb);
  1062. pte_unmap_unlock(start_pte, ptl);
  1063. /*
  1064. * If we forced a TLB flush (either due to running out of
  1065. * batch buffers or because we needed to flush dirty TLB
  1066. * entries before releasing the ptl), free the batched
  1067. * memory too. Restart if we didn't do everything.
  1068. */
  1069. if (force_flush) {
  1070. force_flush = 0;
  1071. tlb_flush_mmu_free(tlb);
  1072. if (pending_page) {
  1073. /* remove the page with new size */
  1074. __tlb_remove_pte_page(tlb, pending_page);
  1075. pending_page = NULL;
  1076. }
  1077. if (addr != end)
  1078. goto again;
  1079. }
  1080. return addr;
  1081. }
  1082. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  1083. struct vm_area_struct *vma, pud_t *pud,
  1084. unsigned long addr, unsigned long end,
  1085. struct zap_details *details)
  1086. {
  1087. pmd_t *pmd;
  1088. unsigned long next;
  1089. pmd = pmd_offset(pud, addr);
  1090. do {
  1091. next = pmd_addr_end(addr, end);
  1092. if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
  1093. if (next - addr != HPAGE_PMD_SIZE) {
  1094. VM_BUG_ON_VMA(vma_is_anonymous(vma) &&
  1095. !rwsem_is_locked(&tlb->mm->mmap_sem), vma);
  1096. split_huge_pmd(vma, pmd, addr);
  1097. } else if (zap_huge_pmd(tlb, vma, pmd, addr))
  1098. goto next;
  1099. /* fall through */
  1100. }
  1101. /*
  1102. * Here there can be other concurrent MADV_DONTNEED or
  1103. * trans huge page faults running, and if the pmd is
  1104. * none or trans huge it can change under us. This is
  1105. * because MADV_DONTNEED holds the mmap_sem in read
  1106. * mode.
  1107. */
  1108. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  1109. goto next;
  1110. next = zap_pte_range(tlb, vma, pmd, addr, next, details);
  1111. next:
  1112. cond_resched();
  1113. } while (pmd++, addr = next, addr != end);
  1114. return addr;
  1115. }
  1116. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  1117. struct vm_area_struct *vma, pgd_t *pgd,
  1118. unsigned long addr, unsigned long end,
  1119. struct zap_details *details)
  1120. {
  1121. pud_t *pud;
  1122. unsigned long next;
  1123. pud = pud_offset(pgd, addr);
  1124. do {
  1125. next = pud_addr_end(addr, end);
  1126. if (pud_none_or_clear_bad(pud))
  1127. continue;
  1128. next = zap_pmd_range(tlb, vma, pud, addr, next, details);
  1129. } while (pud++, addr = next, addr != end);
  1130. return addr;
  1131. }
  1132. void unmap_page_range(struct mmu_gather *tlb,
  1133. struct vm_area_struct *vma,
  1134. unsigned long addr, unsigned long end,
  1135. struct zap_details *details)
  1136. {
  1137. pgd_t *pgd;
  1138. unsigned long next;
  1139. BUG_ON(addr >= end);
  1140. tlb_start_vma(tlb, vma);
  1141. pgd = pgd_offset(vma->vm_mm, addr);
  1142. do {
  1143. next = pgd_addr_end(addr, end);
  1144. if (pgd_none_or_clear_bad(pgd))
  1145. continue;
  1146. next = zap_pud_range(tlb, vma, pgd, addr, next, details);
  1147. } while (pgd++, addr = next, addr != end);
  1148. tlb_end_vma(tlb, vma);
  1149. }
  1150. static void unmap_single_vma(struct mmu_gather *tlb,
  1151. struct vm_area_struct *vma, unsigned long start_addr,
  1152. unsigned long end_addr,
  1153. struct zap_details *details)
  1154. {
  1155. unsigned long start = max(vma->vm_start, start_addr);
  1156. unsigned long end;
  1157. if (start >= vma->vm_end)
  1158. return;
  1159. end = min(vma->vm_end, end_addr);
  1160. if (end <= vma->vm_start)
  1161. return;
  1162. if (vma->vm_file)
  1163. uprobe_munmap(vma, start, end);
  1164. if (unlikely(vma->vm_flags & VM_PFNMAP))
  1165. untrack_pfn(vma, 0, 0);
  1166. if (start != end) {
  1167. if (unlikely(is_vm_hugetlb_page(vma))) {
  1168. /*
  1169. * It is undesirable to test vma->vm_file as it
  1170. * should be non-null for valid hugetlb area.
  1171. * However, vm_file will be NULL in the error
  1172. * cleanup path of mmap_region. When
  1173. * hugetlbfs ->mmap method fails,
  1174. * mmap_region() nullifies vma->vm_file
  1175. * before calling this function to clean up.
  1176. * Since no pte has actually been setup, it is
  1177. * safe to do nothing in this case.
  1178. */
  1179. if (vma->vm_file) {
  1180. i_mmap_lock_write(vma->vm_file->f_mapping);
  1181. __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
  1182. i_mmap_unlock_write(vma->vm_file->f_mapping);
  1183. }
  1184. } else
  1185. unmap_page_range(tlb, vma, start, end, details);
  1186. }
  1187. }
  1188. /**
  1189. * unmap_vmas - unmap a range of memory covered by a list of vma's
  1190. * @tlb: address of the caller's struct mmu_gather
  1191. * @vma: the starting vma
  1192. * @start_addr: virtual address at which to start unmapping
  1193. * @end_addr: virtual address at which to end unmapping
  1194. *
  1195. * Unmap all pages in the vma list.
  1196. *
  1197. * Only addresses between `start' and `end' will be unmapped.
  1198. *
  1199. * The VMA list must be sorted in ascending virtual address order.
  1200. *
  1201. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  1202. * range after unmap_vmas() returns. So the only responsibility here is to
  1203. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  1204. * drops the lock and schedules.
  1205. */
  1206. void unmap_vmas(struct mmu_gather *tlb,
  1207. struct vm_area_struct *vma, unsigned long start_addr,
  1208. unsigned long end_addr)
  1209. {
  1210. struct mm_struct *mm = vma->vm_mm;
  1211. mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
  1212. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
  1213. unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
  1214. mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
  1215. }
  1216. /**
  1217. * zap_page_range - remove user pages in a given range
  1218. * @vma: vm_area_struct holding the applicable pages
  1219. * @start: starting address of pages to zap
  1220. * @size: number of bytes to zap
  1221. * @details: details of shared cache invalidation
  1222. *
  1223. * Caller must protect the VMA list
  1224. */
  1225. void zap_page_range(struct vm_area_struct *vma, unsigned long start,
  1226. unsigned long size, struct zap_details *details)
  1227. {
  1228. struct mm_struct *mm = vma->vm_mm;
  1229. struct mmu_gather tlb;
  1230. unsigned long end = start + size;
  1231. lru_add_drain();
  1232. tlb_gather_mmu(&tlb, mm, start, end);
  1233. update_hiwater_rss(mm);
  1234. mmu_notifier_invalidate_range_start(mm, start, end);
  1235. for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
  1236. unmap_single_vma(&tlb, vma, start, end, details);
  1237. mmu_notifier_invalidate_range_end(mm, start, end);
  1238. tlb_finish_mmu(&tlb, start, end);
  1239. }
  1240. /**
  1241. * zap_page_range_single - remove user pages in a given range
  1242. * @vma: vm_area_struct holding the applicable pages
  1243. * @address: starting address of pages to zap
  1244. * @size: number of bytes to zap
  1245. * @details: details of shared cache invalidation
  1246. *
  1247. * The range must fit into one VMA.
  1248. */
  1249. static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
  1250. unsigned long size, struct zap_details *details)
  1251. {
  1252. struct mm_struct *mm = vma->vm_mm;
  1253. struct mmu_gather tlb;
  1254. unsigned long end = address + size;
  1255. lru_add_drain();
  1256. tlb_gather_mmu(&tlb, mm, address, end);
  1257. update_hiwater_rss(mm);
  1258. mmu_notifier_invalidate_range_start(mm, address, end);
  1259. unmap_single_vma(&tlb, vma, address, end, details);
  1260. mmu_notifier_invalidate_range_end(mm, address, end);
  1261. tlb_finish_mmu(&tlb, address, end);
  1262. }
  1263. /**
  1264. * zap_vma_ptes - remove ptes mapping the vma
  1265. * @vma: vm_area_struct holding ptes to be zapped
  1266. * @address: starting address of pages to zap
  1267. * @size: number of bytes to zap
  1268. *
  1269. * This function only unmaps ptes assigned to VM_PFNMAP vmas.
  1270. *
  1271. * The entire address range must be fully contained within the vma.
  1272. *
  1273. * Returns 0 if successful.
  1274. */
  1275. int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  1276. unsigned long size)
  1277. {
  1278. if (address < vma->vm_start || address + size > vma->vm_end ||
  1279. !(vma->vm_flags & VM_PFNMAP))
  1280. return -1;
  1281. zap_page_range_single(vma, address, size, NULL);
  1282. return 0;
  1283. }
  1284. EXPORT_SYMBOL_GPL(zap_vma_ptes);
  1285. pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1286. spinlock_t **ptl)
  1287. {
  1288. pgd_t * pgd = pgd_offset(mm, addr);
  1289. pud_t * pud = pud_alloc(mm, pgd, addr);
  1290. if (pud) {
  1291. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1292. if (pmd) {
  1293. VM_BUG_ON(pmd_trans_huge(*pmd));
  1294. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1295. }
  1296. }
  1297. return NULL;
  1298. }
  1299. /*
  1300. * This is the old fallback for page remapping.
  1301. *
  1302. * For historical reasons, it only allows reserved pages. Only
  1303. * old drivers should use this, and they needed to mark their
  1304. * pages reserved for the old functions anyway.
  1305. */
  1306. static int insert_page(struct vm_area_struct *vma, unsigned long addr,
  1307. struct page *page, pgprot_t prot)
  1308. {
  1309. struct mm_struct *mm = vma->vm_mm;
  1310. int retval;
  1311. pte_t *pte;
  1312. spinlock_t *ptl;
  1313. retval = -EINVAL;
  1314. if (PageAnon(page))
  1315. goto out;
  1316. retval = -ENOMEM;
  1317. flush_dcache_page(page);
  1318. pte = get_locked_pte(mm, addr, &ptl);
  1319. if (!pte)
  1320. goto out;
  1321. retval = -EBUSY;
  1322. if (!pte_none(*pte))
  1323. goto out_unlock;
  1324. /* Ok, finally just insert the thing.. */
  1325. get_page(page);
  1326. inc_mm_counter_fast(mm, mm_counter_file(page));
  1327. page_add_file_rmap(page, false);
  1328. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1329. retval = 0;
  1330. pte_unmap_unlock(pte, ptl);
  1331. return retval;
  1332. out_unlock:
  1333. pte_unmap_unlock(pte, ptl);
  1334. out:
  1335. return retval;
  1336. }
  1337. /**
  1338. * vm_insert_page - insert single page into user vma
  1339. * @vma: user vma to map to
  1340. * @addr: target user address of this page
  1341. * @page: source kernel page
  1342. *
  1343. * This allows drivers to insert individual pages they've allocated
  1344. * into a user vma.
  1345. *
  1346. * The page has to be a nice clean _individual_ kernel allocation.
  1347. * If you allocate a compound page, you need to have marked it as
  1348. * such (__GFP_COMP), or manually just split the page up yourself
  1349. * (see split_page()).
  1350. *
  1351. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1352. * took an arbitrary page protection parameter. This doesn't allow
  1353. * that. Your vma protection will have to be set up correctly, which
  1354. * means that if you want a shared writable mapping, you'd better
  1355. * ask for a shared writable mapping!
  1356. *
  1357. * The page does not need to be reserved.
  1358. *
  1359. * Usually this function is called from f_op->mmap() handler
  1360. * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
  1361. * Caller must set VM_MIXEDMAP on vma if it wants to call this
  1362. * function from other places, for example from page-fault handler.
  1363. */
  1364. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  1365. struct page *page)
  1366. {
  1367. if (addr < vma->vm_start || addr >= vma->vm_end)
  1368. return -EFAULT;
  1369. if (!page_count(page))
  1370. return -EINVAL;
  1371. if (!(vma->vm_flags & VM_MIXEDMAP)) {
  1372. BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
  1373. BUG_ON(vma->vm_flags & VM_PFNMAP);
  1374. vma->vm_flags |= VM_MIXEDMAP;
  1375. }
  1376. return insert_page(vma, addr, page, vma->vm_page_prot);
  1377. }
  1378. EXPORT_SYMBOL(vm_insert_page);
  1379. static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1380. pfn_t pfn, pgprot_t prot)
  1381. {
  1382. struct mm_struct *mm = vma->vm_mm;
  1383. int retval;
  1384. pte_t *pte, entry;
  1385. spinlock_t *ptl;
  1386. retval = -ENOMEM;
  1387. pte = get_locked_pte(mm, addr, &ptl);
  1388. if (!pte)
  1389. goto out;
  1390. retval = -EBUSY;
  1391. if (!pte_none(*pte))
  1392. goto out_unlock;
  1393. /* Ok, finally just insert the thing.. */
  1394. if (pfn_t_devmap(pfn))
  1395. entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
  1396. else
  1397. entry = pte_mkspecial(pfn_t_pte(pfn, prot));
  1398. set_pte_at(mm, addr, pte, entry);
  1399. update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
  1400. retval = 0;
  1401. out_unlock:
  1402. pte_unmap_unlock(pte, ptl);
  1403. out:
  1404. return retval;
  1405. }
  1406. /**
  1407. * vm_insert_pfn - insert single pfn into user vma
  1408. * @vma: user vma to map to
  1409. * @addr: target user address of this page
  1410. * @pfn: source kernel pfn
  1411. *
  1412. * Similar to vm_insert_page, this allows drivers to insert individual pages
  1413. * they've allocated into a user vma. Same comments apply.
  1414. *
  1415. * This function should only be called from a vm_ops->fault handler, and
  1416. * in that case the handler should return NULL.
  1417. *
  1418. * vma cannot be a COW mapping.
  1419. *
  1420. * As this is called only for pages that do not currently exist, we
  1421. * do not need to flush old virtual caches or the TLB.
  1422. */
  1423. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1424. unsigned long pfn)
  1425. {
  1426. return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
  1427. }
  1428. EXPORT_SYMBOL(vm_insert_pfn);
  1429. /**
  1430. * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
  1431. * @vma: user vma to map to
  1432. * @addr: target user address of this page
  1433. * @pfn: source kernel pfn
  1434. * @pgprot: pgprot flags for the inserted page
  1435. *
  1436. * This is exactly like vm_insert_pfn, except that it allows drivers to
  1437. * to override pgprot on a per-page basis.
  1438. *
  1439. * This only makes sense for IO mappings, and it makes no sense for
  1440. * cow mappings. In general, using multiple vmas is preferable;
  1441. * vm_insert_pfn_prot should only be used if using multiple VMAs is
  1442. * impractical.
  1443. */
  1444. int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
  1445. unsigned long pfn, pgprot_t pgprot)
  1446. {
  1447. int ret;
  1448. /*
  1449. * Technically, architectures with pte_special can avoid all these
  1450. * restrictions (same for remap_pfn_range). However we would like
  1451. * consistency in testing and feature parity among all, so we should
  1452. * try to keep these invariants in place for everybody.
  1453. */
  1454. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  1455. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  1456. (VM_PFNMAP|VM_MIXEDMAP));
  1457. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  1458. BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
  1459. if (addr < vma->vm_start || addr >= vma->vm_end)
  1460. return -EFAULT;
  1461. if (track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)))
  1462. return -EINVAL;
  1463. ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot);
  1464. return ret;
  1465. }
  1466. EXPORT_SYMBOL(vm_insert_pfn_prot);
  1467. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  1468. pfn_t pfn)
  1469. {
  1470. pgprot_t pgprot = vma->vm_page_prot;
  1471. BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
  1472. if (addr < vma->vm_start || addr >= vma->vm_end)
  1473. return -EFAULT;
  1474. if (track_pfn_insert(vma, &pgprot, pfn))
  1475. return -EINVAL;
  1476. /*
  1477. * If we don't have pte special, then we have to use the pfn_valid()
  1478. * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
  1479. * refcount the page if pfn_valid is true (hence insert_page rather
  1480. * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
  1481. * without pte special, it would there be refcounted as a normal page.
  1482. */
  1483. if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
  1484. struct page *page;
  1485. /*
  1486. * At this point we are committed to insert_page()
  1487. * regardless of whether the caller specified flags that
  1488. * result in pfn_t_has_page() == false.
  1489. */
  1490. page = pfn_to_page(pfn_t_to_pfn(pfn));
  1491. return insert_page(vma, addr, page, pgprot);
  1492. }
  1493. return insert_pfn(vma, addr, pfn, pgprot);
  1494. }
  1495. EXPORT_SYMBOL(vm_insert_mixed);
  1496. /*
  1497. * maps a range of physical memory into the requested pages. the old
  1498. * mappings are removed. any references to nonexistent pages results
  1499. * in null mappings (currently treated as "copy-on-access")
  1500. */
  1501. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1502. unsigned long addr, unsigned long end,
  1503. unsigned long pfn, pgprot_t prot)
  1504. {
  1505. pte_t *pte;
  1506. spinlock_t *ptl;
  1507. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1508. if (!pte)
  1509. return -ENOMEM;
  1510. arch_enter_lazy_mmu_mode();
  1511. do {
  1512. BUG_ON(!pte_none(*pte));
  1513. set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
  1514. pfn++;
  1515. } while (pte++, addr += PAGE_SIZE, addr != end);
  1516. arch_leave_lazy_mmu_mode();
  1517. pte_unmap_unlock(pte - 1, ptl);
  1518. return 0;
  1519. }
  1520. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1521. unsigned long addr, unsigned long end,
  1522. unsigned long pfn, pgprot_t prot)
  1523. {
  1524. pmd_t *pmd;
  1525. unsigned long next;
  1526. pfn -= addr >> PAGE_SHIFT;
  1527. pmd = pmd_alloc(mm, pud, addr);
  1528. if (!pmd)
  1529. return -ENOMEM;
  1530. VM_BUG_ON(pmd_trans_huge(*pmd));
  1531. do {
  1532. next = pmd_addr_end(addr, end);
  1533. if (remap_pte_range(mm, pmd, addr, next,
  1534. pfn + (addr >> PAGE_SHIFT), prot))
  1535. return -ENOMEM;
  1536. } while (pmd++, addr = next, addr != end);
  1537. return 0;
  1538. }
  1539. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1540. unsigned long addr, unsigned long end,
  1541. unsigned long pfn, pgprot_t prot)
  1542. {
  1543. pud_t *pud;
  1544. unsigned long next;
  1545. pfn -= addr >> PAGE_SHIFT;
  1546. pud = pud_alloc(mm, pgd, addr);
  1547. if (!pud)
  1548. return -ENOMEM;
  1549. do {
  1550. next = pud_addr_end(addr, end);
  1551. if (remap_pmd_range(mm, pud, addr, next,
  1552. pfn + (addr >> PAGE_SHIFT), prot))
  1553. return -ENOMEM;
  1554. } while (pud++, addr = next, addr != end);
  1555. return 0;
  1556. }
  1557. /**
  1558. * remap_pfn_range - remap kernel memory to userspace
  1559. * @vma: user vma to map to
  1560. * @addr: target user address to start at
  1561. * @pfn: physical address of kernel memory
  1562. * @size: size of map area
  1563. * @prot: page protection flags for this mapping
  1564. *
  1565. * Note: this is only safe if the mm semaphore is held when called.
  1566. */
  1567. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1568. unsigned long pfn, unsigned long size, pgprot_t prot)
  1569. {
  1570. pgd_t *pgd;
  1571. unsigned long next;
  1572. unsigned long end = addr + PAGE_ALIGN(size);
  1573. struct mm_struct *mm = vma->vm_mm;
  1574. unsigned long remap_pfn = pfn;
  1575. int err;
  1576. /*
  1577. * Physically remapped pages are special. Tell the
  1578. * rest of the world about it:
  1579. * VM_IO tells people not to look at these pages
  1580. * (accesses can have side effects).
  1581. * VM_PFNMAP tells the core MM that the base pages are just
  1582. * raw PFN mappings, and do not have a "struct page" associated
  1583. * with them.
  1584. * VM_DONTEXPAND
  1585. * Disable vma merging and expanding with mremap().
  1586. * VM_DONTDUMP
  1587. * Omit vma from core dump, even when VM_IO turned off.
  1588. *
  1589. * There's a horrible special case to handle copy-on-write
  1590. * behaviour that some programs depend on. We mark the "original"
  1591. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1592. * See vm_normal_page() for details.
  1593. */
  1594. if (is_cow_mapping(vma->vm_flags)) {
  1595. if (addr != vma->vm_start || end != vma->vm_end)
  1596. return -EINVAL;
  1597. vma->vm_pgoff = pfn;
  1598. }
  1599. err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
  1600. if (err)
  1601. return -EINVAL;
  1602. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  1603. BUG_ON(addr >= end);
  1604. pfn -= addr >> PAGE_SHIFT;
  1605. pgd = pgd_offset(mm, addr);
  1606. flush_cache_range(vma, addr, end);
  1607. do {
  1608. next = pgd_addr_end(addr, end);
  1609. err = remap_pud_range(mm, pgd, addr, next,
  1610. pfn + (addr >> PAGE_SHIFT), prot);
  1611. if (err)
  1612. break;
  1613. } while (pgd++, addr = next, addr != end);
  1614. if (err)
  1615. untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
  1616. return err;
  1617. }
  1618. EXPORT_SYMBOL(remap_pfn_range);
  1619. /**
  1620. * vm_iomap_memory - remap memory to userspace
  1621. * @vma: user vma to map to
  1622. * @start: start of area
  1623. * @len: size of area
  1624. *
  1625. * This is a simplified io_remap_pfn_range() for common driver use. The
  1626. * driver just needs to give us the physical memory range to be mapped,
  1627. * we'll figure out the rest from the vma information.
  1628. *
  1629. * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
  1630. * whatever write-combining details or similar.
  1631. */
  1632. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
  1633. {
  1634. unsigned long vm_len, pfn, pages;
  1635. /* Check that the physical memory area passed in looks valid */
  1636. if (start + len < start)
  1637. return -EINVAL;
  1638. /*
  1639. * You *really* shouldn't map things that aren't page-aligned,
  1640. * but we've historically allowed it because IO memory might
  1641. * just have smaller alignment.
  1642. */
  1643. len += start & ~PAGE_MASK;
  1644. pfn = start >> PAGE_SHIFT;
  1645. pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
  1646. if (pfn + pages < pfn)
  1647. return -EINVAL;
  1648. /* We start the mapping 'vm_pgoff' pages into the area */
  1649. if (vma->vm_pgoff > pages)
  1650. return -EINVAL;
  1651. pfn += vma->vm_pgoff;
  1652. pages -= vma->vm_pgoff;
  1653. /* Can we fit all of the mapping? */
  1654. vm_len = vma->vm_end - vma->vm_start;
  1655. if (vm_len >> PAGE_SHIFT > pages)
  1656. return -EINVAL;
  1657. /* Ok, let it rip */
  1658. return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
  1659. }
  1660. EXPORT_SYMBOL(vm_iomap_memory);
  1661. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1662. unsigned long addr, unsigned long end,
  1663. pte_fn_t fn, void *data)
  1664. {
  1665. pte_t *pte;
  1666. int err;
  1667. pgtable_t token;
  1668. spinlock_t *uninitialized_var(ptl);
  1669. pte = (mm == &init_mm) ?
  1670. pte_alloc_kernel(pmd, addr) :
  1671. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1672. if (!pte)
  1673. return -ENOMEM;
  1674. BUG_ON(pmd_huge(*pmd));
  1675. arch_enter_lazy_mmu_mode();
  1676. token = pmd_pgtable(*pmd);
  1677. do {
  1678. err = fn(pte++, token, addr, data);
  1679. if (err)
  1680. break;
  1681. } while (addr += PAGE_SIZE, addr != end);
  1682. arch_leave_lazy_mmu_mode();
  1683. if (mm != &init_mm)
  1684. pte_unmap_unlock(pte-1, ptl);
  1685. return err;
  1686. }
  1687. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  1688. unsigned long addr, unsigned long end,
  1689. pte_fn_t fn, void *data)
  1690. {
  1691. pmd_t *pmd;
  1692. unsigned long next;
  1693. int err;
  1694. BUG_ON(pud_huge(*pud));
  1695. pmd = pmd_alloc(mm, pud, addr);
  1696. if (!pmd)
  1697. return -ENOMEM;
  1698. do {
  1699. next = pmd_addr_end(addr, end);
  1700. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  1701. if (err)
  1702. break;
  1703. } while (pmd++, addr = next, addr != end);
  1704. return err;
  1705. }
  1706. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1707. unsigned long addr, unsigned long end,
  1708. pte_fn_t fn, void *data)
  1709. {
  1710. pud_t *pud;
  1711. unsigned long next;
  1712. int err;
  1713. pud = pud_alloc(mm, pgd, addr);
  1714. if (!pud)
  1715. return -ENOMEM;
  1716. do {
  1717. next = pud_addr_end(addr, end);
  1718. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  1719. if (err)
  1720. break;
  1721. } while (pud++, addr = next, addr != end);
  1722. return err;
  1723. }
  1724. /*
  1725. * Scan a region of virtual memory, filling in page tables as necessary
  1726. * and calling a provided function on each leaf page table.
  1727. */
  1728. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  1729. unsigned long size, pte_fn_t fn, void *data)
  1730. {
  1731. pgd_t *pgd;
  1732. unsigned long next;
  1733. unsigned long end = addr + size;
  1734. int err;
  1735. if (WARN_ON(addr >= end))
  1736. return -EINVAL;
  1737. pgd = pgd_offset(mm, addr);
  1738. do {
  1739. next = pgd_addr_end(addr, end);
  1740. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  1741. if (err)
  1742. break;
  1743. } while (pgd++, addr = next, addr != end);
  1744. return err;
  1745. }
  1746. EXPORT_SYMBOL_GPL(apply_to_page_range);
  1747. /*
  1748. * handle_pte_fault chooses page fault handler according to an entry which was
  1749. * read non-atomically. Before making any commitment, on those architectures
  1750. * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
  1751. * parts, do_swap_page must check under lock before unmapping the pte and
  1752. * proceeding (but do_wp_page is only called after already making such a check;
  1753. * and do_anonymous_page can safely check later on).
  1754. */
  1755. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1756. pte_t *page_table, pte_t orig_pte)
  1757. {
  1758. int same = 1;
  1759. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1760. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1761. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1762. spin_lock(ptl);
  1763. same = pte_same(*page_table, orig_pte);
  1764. spin_unlock(ptl);
  1765. }
  1766. #endif
  1767. pte_unmap(page_table);
  1768. return same;
  1769. }
  1770. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  1771. {
  1772. debug_dma_assert_idle(src);
  1773. /*
  1774. * If the source page was a PFN mapping, we don't have
  1775. * a "struct page" for it. We do a best-effort copy by
  1776. * just copying from the original user address. If that
  1777. * fails, we just zero-fill it. Live with it.
  1778. */
  1779. if (unlikely(!src)) {
  1780. void *kaddr = kmap_atomic(dst);
  1781. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1782. /*
  1783. * This really shouldn't fail, because the page is there
  1784. * in the page tables. But it might just be unreadable,
  1785. * in which case we just give up and fill the result with
  1786. * zeroes.
  1787. */
  1788. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1789. clear_page(kaddr);
  1790. kunmap_atomic(kaddr);
  1791. flush_dcache_page(dst);
  1792. } else
  1793. copy_user_highpage(dst, src, va, vma);
  1794. }
  1795. static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
  1796. {
  1797. struct file *vm_file = vma->vm_file;
  1798. if (vm_file)
  1799. return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
  1800. /*
  1801. * Special mappings (e.g. VDSO) do not have any file so fake
  1802. * a default GFP_KERNEL for them.
  1803. */
  1804. return GFP_KERNEL;
  1805. }
  1806. /*
  1807. * Notify the address space that the page is about to become writable so that
  1808. * it can prohibit this or wait for the page to get into an appropriate state.
  1809. *
  1810. * We do this without the lock held, so that it can sleep if it needs to.
  1811. */
  1812. static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
  1813. unsigned long address)
  1814. {
  1815. struct vm_fault vmf;
  1816. int ret;
  1817. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  1818. vmf.pgoff = page->index;
  1819. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  1820. vmf.gfp_mask = __get_fault_gfp_mask(vma);
  1821. vmf.page = page;
  1822. vmf.cow_page = NULL;
  1823. ret = vma->vm_ops->page_mkwrite(vma, &vmf);
  1824. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
  1825. return ret;
  1826. if (unlikely(!(ret & VM_FAULT_LOCKED))) {
  1827. lock_page(page);
  1828. if (!page->mapping) {
  1829. unlock_page(page);
  1830. return 0; /* retry */
  1831. }
  1832. ret |= VM_FAULT_LOCKED;
  1833. } else
  1834. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1835. return ret;
  1836. }
  1837. /*
  1838. * Handle write page faults for pages that can be reused in the current vma
  1839. *
  1840. * This can happen either due to the mapping being with the VM_SHARED flag,
  1841. * or due to us being the last reference standing to the page. In either
  1842. * case, all we need to do here is to mark the page as writable and update
  1843. * any related book-keeping.
  1844. */
  1845. static inline int wp_page_reuse(struct fault_env *fe, pte_t orig_pte,
  1846. struct page *page, int page_mkwrite, int dirty_shared)
  1847. __releases(fe->ptl)
  1848. {
  1849. struct vm_area_struct *vma = fe->vma;
  1850. pte_t entry;
  1851. /*
  1852. * Clear the pages cpupid information as the existing
  1853. * information potentially belongs to a now completely
  1854. * unrelated process.
  1855. */
  1856. if (page)
  1857. page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
  1858. flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
  1859. entry = pte_mkyoung(orig_pte);
  1860. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1861. if (ptep_set_access_flags(vma, fe->address, fe->pte, entry, 1))
  1862. update_mmu_cache(vma, fe->address, fe->pte);
  1863. pte_unmap_unlock(fe->pte, fe->ptl);
  1864. if (dirty_shared) {
  1865. struct address_space *mapping;
  1866. int dirtied;
  1867. if (!page_mkwrite)
  1868. lock_page(page);
  1869. dirtied = set_page_dirty(page);
  1870. VM_BUG_ON_PAGE(PageAnon(page), page);
  1871. mapping = page->mapping;
  1872. unlock_page(page);
  1873. put_page(page);
  1874. if ((dirtied || page_mkwrite) && mapping) {
  1875. /*
  1876. * Some device drivers do not set page.mapping
  1877. * but still dirty their pages
  1878. */
  1879. balance_dirty_pages_ratelimited(mapping);
  1880. }
  1881. if (!page_mkwrite)
  1882. file_update_time(vma->vm_file);
  1883. }
  1884. return VM_FAULT_WRITE;
  1885. }
  1886. /*
  1887. * Handle the case of a page which we actually need to copy to a new page.
  1888. *
  1889. * Called with mmap_sem locked and the old page referenced, but
  1890. * without the ptl held.
  1891. *
  1892. * High level logic flow:
  1893. *
  1894. * - Allocate a page, copy the content of the old page to the new one.
  1895. * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
  1896. * - Take the PTL. If the pte changed, bail out and release the allocated page
  1897. * - If the pte is still the way we remember it, update the page table and all
  1898. * relevant references. This includes dropping the reference the page-table
  1899. * held to the old page, as well as updating the rmap.
  1900. * - In any case, unlock the PTL and drop the reference we took to the old page.
  1901. */
  1902. static int wp_page_copy(struct fault_env *fe, pte_t orig_pte,
  1903. struct page *old_page)
  1904. {
  1905. struct vm_area_struct *vma = fe->vma;
  1906. struct mm_struct *mm = vma->vm_mm;
  1907. struct page *new_page = NULL;
  1908. pte_t entry;
  1909. int page_copied = 0;
  1910. const unsigned long mmun_start = fe->address & PAGE_MASK;
  1911. const unsigned long mmun_end = mmun_start + PAGE_SIZE;
  1912. struct mem_cgroup *memcg;
  1913. if (unlikely(anon_vma_prepare(vma)))
  1914. goto oom;
  1915. if (is_zero_pfn(pte_pfn(orig_pte))) {
  1916. new_page = alloc_zeroed_user_highpage_movable(vma, fe->address);
  1917. if (!new_page)
  1918. goto oom;
  1919. } else {
  1920. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
  1921. fe->address);
  1922. if (!new_page)
  1923. goto oom;
  1924. cow_user_page(new_page, old_page, fe->address, vma);
  1925. }
  1926. if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
  1927. goto oom_free_new;
  1928. __SetPageUptodate(new_page);
  1929. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1930. /*
  1931. * Re-check the pte - we dropped the lock
  1932. */
  1933. fe->pte = pte_offset_map_lock(mm, fe->pmd, fe->address, &fe->ptl);
  1934. if (likely(pte_same(*fe->pte, orig_pte))) {
  1935. if (old_page) {
  1936. if (!PageAnon(old_page)) {
  1937. dec_mm_counter_fast(mm,
  1938. mm_counter_file(old_page));
  1939. inc_mm_counter_fast(mm, MM_ANONPAGES);
  1940. }
  1941. } else {
  1942. inc_mm_counter_fast(mm, MM_ANONPAGES);
  1943. }
  1944. flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
  1945. entry = mk_pte(new_page, vma->vm_page_prot);
  1946. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1947. /*
  1948. * Clear the pte entry and flush it first, before updating the
  1949. * pte with the new entry. This will avoid a race condition
  1950. * seen in the presence of one thread doing SMC and another
  1951. * thread doing COW.
  1952. */
  1953. ptep_clear_flush_notify(vma, fe->address, fe->pte);
  1954. page_add_new_anon_rmap(new_page, vma, fe->address, false);
  1955. mem_cgroup_commit_charge(new_page, memcg, false, false);
  1956. lru_cache_add_active_or_unevictable(new_page, vma);
  1957. /*
  1958. * We call the notify macro here because, when using secondary
  1959. * mmu page tables (such as kvm shadow page tables), we want the
  1960. * new page to be mapped directly into the secondary page table.
  1961. */
  1962. set_pte_at_notify(mm, fe->address, fe->pte, entry);
  1963. update_mmu_cache(vma, fe->address, fe->pte);
  1964. if (old_page) {
  1965. /*
  1966. * Only after switching the pte to the new page may
  1967. * we remove the mapcount here. Otherwise another
  1968. * process may come and find the rmap count decremented
  1969. * before the pte is switched to the new page, and
  1970. * "reuse" the old page writing into it while our pte
  1971. * here still points into it and can be read by other
  1972. * threads.
  1973. *
  1974. * The critical issue is to order this
  1975. * page_remove_rmap with the ptp_clear_flush above.
  1976. * Those stores are ordered by (if nothing else,)
  1977. * the barrier present in the atomic_add_negative
  1978. * in page_remove_rmap.
  1979. *
  1980. * Then the TLB flush in ptep_clear_flush ensures that
  1981. * no process can access the old page before the
  1982. * decremented mapcount is visible. And the old page
  1983. * cannot be reused until after the decremented
  1984. * mapcount is visible. So transitively, TLBs to
  1985. * old page will be flushed before it can be reused.
  1986. */
  1987. page_remove_rmap(old_page, false);
  1988. }
  1989. /* Free the old page.. */
  1990. new_page = old_page;
  1991. page_copied = 1;
  1992. } else {
  1993. mem_cgroup_cancel_charge(new_page, memcg, false);
  1994. }
  1995. if (new_page)
  1996. put_page(new_page);
  1997. pte_unmap_unlock(fe->pte, fe->ptl);
  1998. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1999. if (old_page) {
  2000. /*
  2001. * Don't let another task, with possibly unlocked vma,
  2002. * keep the mlocked page.
  2003. */
  2004. if (page_copied && (vma->vm_flags & VM_LOCKED)) {
  2005. lock_page(old_page); /* LRU manipulation */
  2006. if (PageMlocked(old_page))
  2007. munlock_vma_page(old_page);
  2008. unlock_page(old_page);
  2009. }
  2010. put_page(old_page);
  2011. }
  2012. return page_copied ? VM_FAULT_WRITE : 0;
  2013. oom_free_new:
  2014. put_page(new_page);
  2015. oom:
  2016. if (old_page)
  2017. put_page(old_page);
  2018. return VM_FAULT_OOM;
  2019. }
  2020. /*
  2021. * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
  2022. * mapping
  2023. */
  2024. static int wp_pfn_shared(struct fault_env *fe, pte_t orig_pte)
  2025. {
  2026. struct vm_area_struct *vma = fe->vma;
  2027. if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
  2028. struct vm_fault vmf = {
  2029. .page = NULL,
  2030. .pgoff = linear_page_index(vma, fe->address),
  2031. .virtual_address =
  2032. (void __user *)(fe->address & PAGE_MASK),
  2033. .flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
  2034. };
  2035. int ret;
  2036. pte_unmap_unlock(fe->pte, fe->ptl);
  2037. ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
  2038. if (ret & VM_FAULT_ERROR)
  2039. return ret;
  2040. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2041. &fe->ptl);
  2042. /*
  2043. * We might have raced with another page fault while we
  2044. * released the pte_offset_map_lock.
  2045. */
  2046. if (!pte_same(*fe->pte, orig_pte)) {
  2047. pte_unmap_unlock(fe->pte, fe->ptl);
  2048. return 0;
  2049. }
  2050. }
  2051. return wp_page_reuse(fe, orig_pte, NULL, 0, 0);
  2052. }
  2053. static int wp_page_shared(struct fault_env *fe, pte_t orig_pte,
  2054. struct page *old_page)
  2055. __releases(fe->ptl)
  2056. {
  2057. struct vm_area_struct *vma = fe->vma;
  2058. int page_mkwrite = 0;
  2059. get_page(old_page);
  2060. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  2061. int tmp;
  2062. pte_unmap_unlock(fe->pte, fe->ptl);
  2063. tmp = do_page_mkwrite(vma, old_page, fe->address);
  2064. if (unlikely(!tmp || (tmp &
  2065. (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
  2066. put_page(old_page);
  2067. return tmp;
  2068. }
  2069. /*
  2070. * Since we dropped the lock we need to revalidate
  2071. * the PTE as someone else may have changed it. If
  2072. * they did, we just return, as we can count on the
  2073. * MMU to tell us if they didn't also make it writable.
  2074. */
  2075. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2076. &fe->ptl);
  2077. if (!pte_same(*fe->pte, orig_pte)) {
  2078. unlock_page(old_page);
  2079. pte_unmap_unlock(fe->pte, fe->ptl);
  2080. put_page(old_page);
  2081. return 0;
  2082. }
  2083. page_mkwrite = 1;
  2084. }
  2085. return wp_page_reuse(fe, orig_pte, old_page, page_mkwrite, 1);
  2086. }
  2087. /*
  2088. * This routine handles present pages, when users try to write
  2089. * to a shared page. It is done by copying the page to a new address
  2090. * and decrementing the shared-page counter for the old page.
  2091. *
  2092. * Note that this routine assumes that the protection checks have been
  2093. * done by the caller (the low-level page fault routine in most cases).
  2094. * Thus we can safely just mark it writable once we've done any necessary
  2095. * COW.
  2096. *
  2097. * We also mark the page dirty at this point even though the page will
  2098. * change only once the write actually happens. This avoids a few races,
  2099. * and potentially makes it more efficient.
  2100. *
  2101. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2102. * but allow concurrent faults), with pte both mapped and locked.
  2103. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2104. */
  2105. static int do_wp_page(struct fault_env *fe, pte_t orig_pte)
  2106. __releases(fe->ptl)
  2107. {
  2108. struct vm_area_struct *vma = fe->vma;
  2109. struct page *old_page;
  2110. old_page = vm_normal_page(vma, fe->address, orig_pte);
  2111. if (!old_page) {
  2112. /*
  2113. * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
  2114. * VM_PFNMAP VMA.
  2115. *
  2116. * We should not cow pages in a shared writeable mapping.
  2117. * Just mark the pages writable and/or call ops->pfn_mkwrite.
  2118. */
  2119. if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2120. (VM_WRITE|VM_SHARED))
  2121. return wp_pfn_shared(fe, orig_pte);
  2122. pte_unmap_unlock(fe->pte, fe->ptl);
  2123. return wp_page_copy(fe, orig_pte, old_page);
  2124. }
  2125. /*
  2126. * Take out anonymous pages first, anonymous shared vmas are
  2127. * not dirty accountable.
  2128. */
  2129. if (PageAnon(old_page) && !PageKsm(old_page)) {
  2130. int total_mapcount;
  2131. if (!trylock_page(old_page)) {
  2132. get_page(old_page);
  2133. pte_unmap_unlock(fe->pte, fe->ptl);
  2134. lock_page(old_page);
  2135. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
  2136. fe->address, &fe->ptl);
  2137. if (!pte_same(*fe->pte, orig_pte)) {
  2138. unlock_page(old_page);
  2139. pte_unmap_unlock(fe->pte, fe->ptl);
  2140. put_page(old_page);
  2141. return 0;
  2142. }
  2143. put_page(old_page);
  2144. }
  2145. if (reuse_swap_page(old_page, &total_mapcount)) {
  2146. if (total_mapcount == 1) {
  2147. /*
  2148. * The page is all ours. Move it to
  2149. * our anon_vma so the rmap code will
  2150. * not search our parent or siblings.
  2151. * Protected against the rmap code by
  2152. * the page lock.
  2153. */
  2154. page_move_anon_rmap(old_page, vma);
  2155. }
  2156. unlock_page(old_page);
  2157. return wp_page_reuse(fe, orig_pte, old_page, 0, 0);
  2158. }
  2159. unlock_page(old_page);
  2160. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2161. (VM_WRITE|VM_SHARED))) {
  2162. return wp_page_shared(fe, orig_pte, old_page);
  2163. }
  2164. /*
  2165. * Ok, we need to copy. Oh, well..
  2166. */
  2167. get_page(old_page);
  2168. pte_unmap_unlock(fe->pte, fe->ptl);
  2169. return wp_page_copy(fe, orig_pte, old_page);
  2170. }
  2171. static void unmap_mapping_range_vma(struct vm_area_struct *vma,
  2172. unsigned long start_addr, unsigned long end_addr,
  2173. struct zap_details *details)
  2174. {
  2175. zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
  2176. }
  2177. static inline void unmap_mapping_range_tree(struct rb_root *root,
  2178. struct zap_details *details)
  2179. {
  2180. struct vm_area_struct *vma;
  2181. pgoff_t vba, vea, zba, zea;
  2182. vma_interval_tree_foreach(vma, root,
  2183. details->first_index, details->last_index) {
  2184. vba = vma->vm_pgoff;
  2185. vea = vba + vma_pages(vma) - 1;
  2186. zba = details->first_index;
  2187. if (zba < vba)
  2188. zba = vba;
  2189. zea = details->last_index;
  2190. if (zea > vea)
  2191. zea = vea;
  2192. unmap_mapping_range_vma(vma,
  2193. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  2194. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  2195. details);
  2196. }
  2197. }
  2198. /**
  2199. * unmap_mapping_range - unmap the portion of all mmaps in the specified
  2200. * address_space corresponding to the specified page range in the underlying
  2201. * file.
  2202. *
  2203. * @mapping: the address space containing mmaps to be unmapped.
  2204. * @holebegin: byte in first page to unmap, relative to the start of
  2205. * the underlying file. This will be rounded down to a PAGE_SIZE
  2206. * boundary. Note that this is different from truncate_pagecache(), which
  2207. * must keep the partial page. In contrast, we must get rid of
  2208. * partial pages.
  2209. * @holelen: size of prospective hole in bytes. This will be rounded
  2210. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  2211. * end of the file.
  2212. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  2213. * but 0 when invalidating pagecache, don't throw away private data.
  2214. */
  2215. void unmap_mapping_range(struct address_space *mapping,
  2216. loff_t const holebegin, loff_t const holelen, int even_cows)
  2217. {
  2218. struct zap_details details = { };
  2219. pgoff_t hba = holebegin >> PAGE_SHIFT;
  2220. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2221. /* Check for overflow. */
  2222. if (sizeof(holelen) > sizeof(hlen)) {
  2223. long long holeend =
  2224. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2225. if (holeend & ~(long long)ULONG_MAX)
  2226. hlen = ULONG_MAX - hba + 1;
  2227. }
  2228. details.check_mapping = even_cows? NULL: mapping;
  2229. details.first_index = hba;
  2230. details.last_index = hba + hlen - 1;
  2231. if (details.last_index < details.first_index)
  2232. details.last_index = ULONG_MAX;
  2233. i_mmap_lock_write(mapping);
  2234. if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
  2235. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  2236. i_mmap_unlock_write(mapping);
  2237. }
  2238. EXPORT_SYMBOL(unmap_mapping_range);
  2239. /*
  2240. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2241. * but allow concurrent faults), and pte mapped but not yet locked.
  2242. * We return with pte unmapped and unlocked.
  2243. *
  2244. * We return with the mmap_sem locked or unlocked in the same cases
  2245. * as does filemap_fault().
  2246. */
  2247. int do_swap_page(struct fault_env *fe, pte_t orig_pte)
  2248. {
  2249. struct vm_area_struct *vma = fe->vma;
  2250. struct page *page, *swapcache;
  2251. struct mem_cgroup *memcg;
  2252. swp_entry_t entry;
  2253. pte_t pte;
  2254. int locked;
  2255. int exclusive = 0;
  2256. int ret = 0;
  2257. if (!pte_unmap_same(vma->vm_mm, fe->pmd, fe->pte, orig_pte))
  2258. goto out;
  2259. entry = pte_to_swp_entry(orig_pte);
  2260. if (unlikely(non_swap_entry(entry))) {
  2261. if (is_migration_entry(entry)) {
  2262. migration_entry_wait(vma->vm_mm, fe->pmd, fe->address);
  2263. } else if (is_hwpoison_entry(entry)) {
  2264. ret = VM_FAULT_HWPOISON;
  2265. } else {
  2266. print_bad_pte(vma, fe->address, orig_pte, NULL);
  2267. ret = VM_FAULT_SIGBUS;
  2268. }
  2269. goto out;
  2270. }
  2271. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  2272. page = lookup_swap_cache(entry);
  2273. if (!page) {
  2274. page = swapin_readahead(entry,
  2275. GFP_HIGHUSER_MOVABLE, vma, fe->address);
  2276. if (!page) {
  2277. /*
  2278. * Back out if somebody else faulted in this pte
  2279. * while we released the pte lock.
  2280. */
  2281. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
  2282. fe->address, &fe->ptl);
  2283. if (likely(pte_same(*fe->pte, orig_pte)))
  2284. ret = VM_FAULT_OOM;
  2285. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2286. goto unlock;
  2287. }
  2288. /* Had to read the page from swap area: Major fault */
  2289. ret = VM_FAULT_MAJOR;
  2290. count_vm_event(PGMAJFAULT);
  2291. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  2292. } else if (PageHWPoison(page)) {
  2293. /*
  2294. * hwpoisoned dirty swapcache pages are kept for killing
  2295. * owner processes (which may be unknown at hwpoison time)
  2296. */
  2297. ret = VM_FAULT_HWPOISON;
  2298. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2299. swapcache = page;
  2300. goto out_release;
  2301. }
  2302. swapcache = page;
  2303. locked = lock_page_or_retry(page, vma->vm_mm, fe->flags);
  2304. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2305. if (!locked) {
  2306. ret |= VM_FAULT_RETRY;
  2307. goto out_release;
  2308. }
  2309. /*
  2310. * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
  2311. * release the swapcache from under us. The page pin, and pte_same
  2312. * test below, are not enough to exclude that. Even if it is still
  2313. * swapcache, we need to check that the page's swap has not changed.
  2314. */
  2315. if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
  2316. goto out_page;
  2317. page = ksm_might_need_to_copy(page, vma, fe->address);
  2318. if (unlikely(!page)) {
  2319. ret = VM_FAULT_OOM;
  2320. page = swapcache;
  2321. goto out_page;
  2322. }
  2323. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
  2324. &memcg, false)) {
  2325. ret = VM_FAULT_OOM;
  2326. goto out_page;
  2327. }
  2328. /*
  2329. * Back out if somebody else already faulted in this pte.
  2330. */
  2331. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2332. &fe->ptl);
  2333. if (unlikely(!pte_same(*fe->pte, orig_pte)))
  2334. goto out_nomap;
  2335. if (unlikely(!PageUptodate(page))) {
  2336. ret = VM_FAULT_SIGBUS;
  2337. goto out_nomap;
  2338. }
  2339. /*
  2340. * The page isn't present yet, go ahead with the fault.
  2341. *
  2342. * Be careful about the sequence of operations here.
  2343. * To get its accounting right, reuse_swap_page() must be called
  2344. * while the page is counted on swap but not yet in mapcount i.e.
  2345. * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
  2346. * must be called after the swap_free(), or it will never succeed.
  2347. */
  2348. inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
  2349. dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
  2350. pte = mk_pte(page, vma->vm_page_prot);
  2351. if ((fe->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
  2352. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  2353. fe->flags &= ~FAULT_FLAG_WRITE;
  2354. ret |= VM_FAULT_WRITE;
  2355. exclusive = RMAP_EXCLUSIVE;
  2356. }
  2357. flush_icache_page(vma, page);
  2358. if (pte_swp_soft_dirty(orig_pte))
  2359. pte = pte_mksoft_dirty(pte);
  2360. set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
  2361. if (page == swapcache) {
  2362. do_page_add_anon_rmap(page, vma, fe->address, exclusive);
  2363. mem_cgroup_commit_charge(page, memcg, true, false);
  2364. activate_page(page);
  2365. } else { /* ksm created a completely new copy */
  2366. page_add_new_anon_rmap(page, vma, fe->address, false);
  2367. mem_cgroup_commit_charge(page, memcg, false, false);
  2368. lru_cache_add_active_or_unevictable(page, vma);
  2369. }
  2370. swap_free(entry);
  2371. if (mem_cgroup_swap_full(page) ||
  2372. (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
  2373. try_to_free_swap(page);
  2374. unlock_page(page);
  2375. if (page != swapcache) {
  2376. /*
  2377. * Hold the lock to avoid the swap entry to be reused
  2378. * until we take the PT lock for the pte_same() check
  2379. * (to avoid false positives from pte_same). For
  2380. * further safety release the lock after the swap_free
  2381. * so that the swap count won't change under a
  2382. * parallel locked swapcache.
  2383. */
  2384. unlock_page(swapcache);
  2385. put_page(swapcache);
  2386. }
  2387. if (fe->flags & FAULT_FLAG_WRITE) {
  2388. ret |= do_wp_page(fe, pte);
  2389. if (ret & VM_FAULT_ERROR)
  2390. ret &= VM_FAULT_ERROR;
  2391. goto out;
  2392. }
  2393. /* No need to invalidate - it was non-present before */
  2394. update_mmu_cache(vma, fe->address, fe->pte);
  2395. unlock:
  2396. pte_unmap_unlock(fe->pte, fe->ptl);
  2397. out:
  2398. return ret;
  2399. out_nomap:
  2400. mem_cgroup_cancel_charge(page, memcg, false);
  2401. pte_unmap_unlock(fe->pte, fe->ptl);
  2402. out_page:
  2403. unlock_page(page);
  2404. out_release:
  2405. put_page(page);
  2406. if (page != swapcache) {
  2407. unlock_page(swapcache);
  2408. put_page(swapcache);
  2409. }
  2410. return ret;
  2411. }
  2412. /*
  2413. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2414. * but allow concurrent faults), and pte mapped but not yet locked.
  2415. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2416. */
  2417. static int do_anonymous_page(struct fault_env *fe)
  2418. {
  2419. struct vm_area_struct *vma = fe->vma;
  2420. struct mem_cgroup *memcg;
  2421. struct page *page;
  2422. pte_t entry;
  2423. /* File mapping without ->vm_ops ? */
  2424. if (vma->vm_flags & VM_SHARED)
  2425. return VM_FAULT_SIGBUS;
  2426. /*
  2427. * Use pte_alloc() instead of pte_alloc_map(). We can't run
  2428. * pte_offset_map() on pmds where a huge pmd might be created
  2429. * from a different thread.
  2430. *
  2431. * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
  2432. * parallel threads are excluded by other means.
  2433. *
  2434. * Here we only have down_read(mmap_sem).
  2435. */
  2436. if (pte_alloc(vma->vm_mm, fe->pmd, fe->address))
  2437. return VM_FAULT_OOM;
  2438. /* See the comment in pte_alloc_one_map() */
  2439. if (unlikely(pmd_trans_unstable(fe->pmd)))
  2440. return 0;
  2441. /* Use the zero-page for reads */
  2442. if (!(fe->flags & FAULT_FLAG_WRITE) &&
  2443. !mm_forbids_zeropage(vma->vm_mm)) {
  2444. entry = pte_mkspecial(pfn_pte(my_zero_pfn(fe->address),
  2445. vma->vm_page_prot));
  2446. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2447. &fe->ptl);
  2448. if (!pte_none(*fe->pte))
  2449. goto unlock;
  2450. /* Deliver the page fault to userland, check inside PT lock */
  2451. if (userfaultfd_missing(vma)) {
  2452. pte_unmap_unlock(fe->pte, fe->ptl);
  2453. return handle_userfault(fe, VM_UFFD_MISSING);
  2454. }
  2455. goto setpte;
  2456. }
  2457. /* Allocate our own private page. */
  2458. if (unlikely(anon_vma_prepare(vma)))
  2459. goto oom;
  2460. page = alloc_zeroed_user_highpage_movable(vma, fe->address);
  2461. if (!page)
  2462. goto oom;
  2463. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
  2464. goto oom_free_page;
  2465. /*
  2466. * The memory barrier inside __SetPageUptodate makes sure that
  2467. * preceeding stores to the page contents become visible before
  2468. * the set_pte_at() write.
  2469. */
  2470. __SetPageUptodate(page);
  2471. entry = mk_pte(page, vma->vm_page_prot);
  2472. if (vma->vm_flags & VM_WRITE)
  2473. entry = pte_mkwrite(pte_mkdirty(entry));
  2474. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2475. &fe->ptl);
  2476. if (!pte_none(*fe->pte))
  2477. goto release;
  2478. /* Deliver the page fault to userland, check inside PT lock */
  2479. if (userfaultfd_missing(vma)) {
  2480. pte_unmap_unlock(fe->pte, fe->ptl);
  2481. mem_cgroup_cancel_charge(page, memcg, false);
  2482. put_page(page);
  2483. return handle_userfault(fe, VM_UFFD_MISSING);
  2484. }
  2485. inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
  2486. page_add_new_anon_rmap(page, vma, fe->address, false);
  2487. mem_cgroup_commit_charge(page, memcg, false, false);
  2488. lru_cache_add_active_or_unevictable(page, vma);
  2489. setpte:
  2490. set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
  2491. /* No need to invalidate - it was non-present before */
  2492. update_mmu_cache(vma, fe->address, fe->pte);
  2493. unlock:
  2494. pte_unmap_unlock(fe->pte, fe->ptl);
  2495. return 0;
  2496. release:
  2497. mem_cgroup_cancel_charge(page, memcg, false);
  2498. put_page(page);
  2499. goto unlock;
  2500. oom_free_page:
  2501. put_page(page);
  2502. oom:
  2503. return VM_FAULT_OOM;
  2504. }
  2505. /*
  2506. * The mmap_sem must have been held on entry, and may have been
  2507. * released depending on flags and vma->vm_ops->fault() return value.
  2508. * See filemap_fault() and __lock_page_retry().
  2509. */
  2510. static int __do_fault(struct fault_env *fe, pgoff_t pgoff,
  2511. struct page *cow_page, struct page **page, void **entry)
  2512. {
  2513. struct vm_area_struct *vma = fe->vma;
  2514. struct vm_fault vmf;
  2515. int ret;
  2516. vmf.virtual_address = (void __user *)(fe->address & PAGE_MASK);
  2517. vmf.pgoff = pgoff;
  2518. vmf.flags = fe->flags;
  2519. vmf.page = NULL;
  2520. vmf.gfp_mask = __get_fault_gfp_mask(vma);
  2521. vmf.cow_page = cow_page;
  2522. ret = vma->vm_ops->fault(vma, &vmf);
  2523. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2524. return ret;
  2525. if (ret & VM_FAULT_DAX_LOCKED) {
  2526. *entry = vmf.entry;
  2527. return ret;
  2528. }
  2529. if (unlikely(PageHWPoison(vmf.page))) {
  2530. if (ret & VM_FAULT_LOCKED)
  2531. unlock_page(vmf.page);
  2532. put_page(vmf.page);
  2533. return VM_FAULT_HWPOISON;
  2534. }
  2535. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  2536. lock_page(vmf.page);
  2537. else
  2538. VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
  2539. *page = vmf.page;
  2540. return ret;
  2541. }
  2542. /*
  2543. * The ordering of these checks is important for pmds with _PAGE_DEVMAP set.
  2544. * If we check pmd_trans_unstable() first we will trip the bad_pmd() check
  2545. * inside of pmd_none_or_trans_huge_or_clear_bad(). This will end up correctly
  2546. * returning 1 but not before it spams dmesg with the pmd_clear_bad() output.
  2547. */
  2548. static int pmd_devmap_trans_unstable(pmd_t *pmd)
  2549. {
  2550. return pmd_devmap(*pmd) || pmd_trans_unstable(pmd);
  2551. }
  2552. static int pte_alloc_one_map(struct fault_env *fe)
  2553. {
  2554. struct vm_area_struct *vma = fe->vma;
  2555. if (!pmd_none(*fe->pmd))
  2556. goto map_pte;
  2557. if (fe->prealloc_pte) {
  2558. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  2559. if (unlikely(!pmd_none(*fe->pmd))) {
  2560. spin_unlock(fe->ptl);
  2561. goto map_pte;
  2562. }
  2563. atomic_long_inc(&vma->vm_mm->nr_ptes);
  2564. pmd_populate(vma->vm_mm, fe->pmd, fe->prealloc_pte);
  2565. spin_unlock(fe->ptl);
  2566. fe->prealloc_pte = 0;
  2567. } else if (unlikely(pte_alloc(vma->vm_mm, fe->pmd, fe->address))) {
  2568. return VM_FAULT_OOM;
  2569. }
  2570. map_pte:
  2571. /*
  2572. * If a huge pmd materialized under us just retry later. Use
  2573. * pmd_trans_unstable() via pmd_devmap_trans_unstable() instead of
  2574. * pmd_trans_huge() to ensure the pmd didn't become pmd_trans_huge
  2575. * under us and then back to pmd_none, as a result of MADV_DONTNEED
  2576. * running immediately after a huge pmd fault in a different thread of
  2577. * this mm, in turn leading to a misleading pmd_trans_huge() retval.
  2578. * All we have to ensure is that it is a regular pmd that we can walk
  2579. * with pte_offset_map() and we can do that through an atomic read in
  2580. * C, which is what pmd_trans_unstable() provides.
  2581. */
  2582. if (pmd_devmap_trans_unstable(fe->pmd))
  2583. return VM_FAULT_NOPAGE;
  2584. /*
  2585. * At this point we know that our vmf->pmd points to a page of ptes
  2586. * and it cannot become pmd_none(), pmd_devmap() or pmd_trans_huge()
  2587. * for the duration of the fault. If a racing MADV_DONTNEED runs and
  2588. * we zap the ptes pointed to by our vmf->pmd, the vmf->ptl will still
  2589. * be valid and we will re-check to make sure the vmf->pte isn't
  2590. * pte_none() under vmf->ptl protection when we return to
  2591. * alloc_set_pte().
  2592. */
  2593. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2594. &fe->ptl);
  2595. return 0;
  2596. }
  2597. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  2598. #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
  2599. static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
  2600. unsigned long haddr)
  2601. {
  2602. if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
  2603. (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
  2604. return false;
  2605. if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
  2606. return false;
  2607. return true;
  2608. }
  2609. static int do_set_pmd(struct fault_env *fe, struct page *page)
  2610. {
  2611. struct vm_area_struct *vma = fe->vma;
  2612. bool write = fe->flags & FAULT_FLAG_WRITE;
  2613. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  2614. pmd_t entry;
  2615. int i, ret;
  2616. if (!transhuge_vma_suitable(vma, haddr))
  2617. return VM_FAULT_FALLBACK;
  2618. ret = VM_FAULT_FALLBACK;
  2619. page = compound_head(page);
  2620. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  2621. if (unlikely(!pmd_none(*fe->pmd)))
  2622. goto out;
  2623. for (i = 0; i < HPAGE_PMD_NR; i++)
  2624. flush_icache_page(vma, page + i);
  2625. entry = mk_huge_pmd(page, vma->vm_page_prot);
  2626. if (write)
  2627. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  2628. add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);
  2629. page_add_file_rmap(page, true);
  2630. set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
  2631. update_mmu_cache_pmd(vma, haddr, fe->pmd);
  2632. /* fault is handled */
  2633. ret = 0;
  2634. count_vm_event(THP_FILE_MAPPED);
  2635. out:
  2636. spin_unlock(fe->ptl);
  2637. return ret;
  2638. }
  2639. #else
  2640. static int do_set_pmd(struct fault_env *fe, struct page *page)
  2641. {
  2642. BUILD_BUG();
  2643. return 0;
  2644. }
  2645. #endif
  2646. /**
  2647. * alloc_set_pte - setup new PTE entry for given page and add reverse page
  2648. * mapping. If needed, the fucntion allocates page table or use pre-allocated.
  2649. *
  2650. * @fe: fault environment
  2651. * @memcg: memcg to charge page (only for private mappings)
  2652. * @page: page to map
  2653. *
  2654. * Caller must take care of unlocking fe->ptl, if fe->pte is non-NULL on return.
  2655. *
  2656. * Target users are page handler itself and implementations of
  2657. * vm_ops->map_pages.
  2658. */
  2659. int alloc_set_pte(struct fault_env *fe, struct mem_cgroup *memcg,
  2660. struct page *page)
  2661. {
  2662. struct vm_area_struct *vma = fe->vma;
  2663. bool write = fe->flags & FAULT_FLAG_WRITE;
  2664. pte_t entry;
  2665. int ret;
  2666. if (pmd_none(*fe->pmd) && PageTransCompound(page) &&
  2667. IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
  2668. /* THP on COW? */
  2669. VM_BUG_ON_PAGE(memcg, page);
  2670. ret = do_set_pmd(fe, page);
  2671. if (ret != VM_FAULT_FALLBACK)
  2672. return ret;
  2673. }
  2674. if (!fe->pte) {
  2675. ret = pte_alloc_one_map(fe);
  2676. if (ret)
  2677. return ret;
  2678. }
  2679. /* Re-check under ptl */
  2680. if (unlikely(!pte_none(*fe->pte)))
  2681. return VM_FAULT_NOPAGE;
  2682. flush_icache_page(vma, page);
  2683. entry = mk_pte(page, vma->vm_page_prot);
  2684. if (write)
  2685. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2686. /* copy-on-write page */
  2687. if (write && !(vma->vm_flags & VM_SHARED)) {
  2688. inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
  2689. page_add_new_anon_rmap(page, vma, fe->address, false);
  2690. mem_cgroup_commit_charge(page, memcg, false, false);
  2691. lru_cache_add_active_or_unevictable(page, vma);
  2692. } else {
  2693. inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
  2694. page_add_file_rmap(page, false);
  2695. }
  2696. set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
  2697. /* no need to invalidate: a not-present page won't be cached */
  2698. update_mmu_cache(vma, fe->address, fe->pte);
  2699. return 0;
  2700. }
  2701. static unsigned long fault_around_bytes __read_mostly =
  2702. rounddown_pow_of_two(65536);
  2703. #ifdef CONFIG_DEBUG_FS
  2704. static int fault_around_bytes_get(void *data, u64 *val)
  2705. {
  2706. *val = fault_around_bytes;
  2707. return 0;
  2708. }
  2709. /*
  2710. * fault_around_pages() and fault_around_mask() expects fault_around_bytes
  2711. * rounded down to nearest page order. It's what do_fault_around() expects to
  2712. * see.
  2713. */
  2714. static int fault_around_bytes_set(void *data, u64 val)
  2715. {
  2716. if (val / PAGE_SIZE > PTRS_PER_PTE)
  2717. return -EINVAL;
  2718. if (val > PAGE_SIZE)
  2719. fault_around_bytes = rounddown_pow_of_two(val);
  2720. else
  2721. fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
  2722. return 0;
  2723. }
  2724. DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
  2725. fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
  2726. static int __init fault_around_debugfs(void)
  2727. {
  2728. void *ret;
  2729. ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
  2730. &fault_around_bytes_fops);
  2731. if (!ret)
  2732. pr_warn("Failed to create fault_around_bytes in debugfs");
  2733. return 0;
  2734. }
  2735. late_initcall(fault_around_debugfs);
  2736. #endif
  2737. /*
  2738. * do_fault_around() tries to map few pages around the fault address. The hope
  2739. * is that the pages will be needed soon and this will lower the number of
  2740. * faults to handle.
  2741. *
  2742. * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
  2743. * not ready to be mapped: not up-to-date, locked, etc.
  2744. *
  2745. * This function is called with the page table lock taken. In the split ptlock
  2746. * case the page table lock only protects only those entries which belong to
  2747. * the page table corresponding to the fault address.
  2748. *
  2749. * This function doesn't cross the VMA boundaries, in order to call map_pages()
  2750. * only once.
  2751. *
  2752. * fault_around_pages() defines how many pages we'll try to map.
  2753. * do_fault_around() expects it to return a power of two less than or equal to
  2754. * PTRS_PER_PTE.
  2755. *
  2756. * The virtual address of the area that we map is naturally aligned to the
  2757. * fault_around_pages() value (and therefore to page order). This way it's
  2758. * easier to guarantee that we don't cross page table boundaries.
  2759. */
  2760. static int do_fault_around(struct fault_env *fe, pgoff_t start_pgoff)
  2761. {
  2762. unsigned long address = fe->address, nr_pages, mask;
  2763. pgoff_t end_pgoff;
  2764. int off, ret = 0;
  2765. nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
  2766. mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
  2767. fe->address = max(address & mask, fe->vma->vm_start);
  2768. off = ((address - fe->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  2769. start_pgoff -= off;
  2770. /*
  2771. * end_pgoff is either end of page table or end of vma
  2772. * or fault_around_pages() from start_pgoff, depending what is nearest.
  2773. */
  2774. end_pgoff = start_pgoff -
  2775. ((fe->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
  2776. PTRS_PER_PTE - 1;
  2777. end_pgoff = min3(end_pgoff, vma_pages(fe->vma) + fe->vma->vm_pgoff - 1,
  2778. start_pgoff + nr_pages - 1);
  2779. if (pmd_none(*fe->pmd)) {
  2780. fe->prealloc_pte = pte_alloc_one(fe->vma->vm_mm, fe->address);
  2781. if (!fe->prealloc_pte)
  2782. goto out;
  2783. smp_wmb(); /* See comment in __pte_alloc() */
  2784. }
  2785. fe->vma->vm_ops->map_pages(fe, start_pgoff, end_pgoff);
  2786. /* preallocated pagetable is unused: free it */
  2787. if (fe->prealloc_pte) {
  2788. pte_free(fe->vma->vm_mm, fe->prealloc_pte);
  2789. fe->prealloc_pte = 0;
  2790. }
  2791. /* Huge page is mapped? Page fault is solved */
  2792. if (pmd_trans_huge(*fe->pmd)) {
  2793. ret = VM_FAULT_NOPAGE;
  2794. goto out;
  2795. }
  2796. /* ->map_pages() haven't done anything useful. Cold page cache? */
  2797. if (!fe->pte)
  2798. goto out;
  2799. /* check if the page fault is solved */
  2800. fe->pte -= (fe->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
  2801. if (!pte_none(*fe->pte))
  2802. ret = VM_FAULT_NOPAGE;
  2803. pte_unmap_unlock(fe->pte, fe->ptl);
  2804. out:
  2805. fe->address = address;
  2806. fe->pte = NULL;
  2807. return ret;
  2808. }
  2809. static int do_read_fault(struct fault_env *fe, pgoff_t pgoff)
  2810. {
  2811. struct vm_area_struct *vma = fe->vma;
  2812. struct page *fault_page;
  2813. int ret = 0;
  2814. /*
  2815. * Let's call ->map_pages() first and use ->fault() as fallback
  2816. * if page by the offset is not ready to be mapped (cold cache or
  2817. * something).
  2818. */
  2819. if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
  2820. ret = do_fault_around(fe, pgoff);
  2821. if (ret)
  2822. return ret;
  2823. }
  2824. ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
  2825. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2826. return ret;
  2827. ret |= alloc_set_pte(fe, NULL, fault_page);
  2828. if (fe->pte)
  2829. pte_unmap_unlock(fe->pte, fe->ptl);
  2830. unlock_page(fault_page);
  2831. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2832. put_page(fault_page);
  2833. return ret;
  2834. }
  2835. static int do_cow_fault(struct fault_env *fe, pgoff_t pgoff)
  2836. {
  2837. struct vm_area_struct *vma = fe->vma;
  2838. struct page *fault_page, *new_page;
  2839. void *fault_entry;
  2840. struct mem_cgroup *memcg;
  2841. int ret;
  2842. if (unlikely(anon_vma_prepare(vma)))
  2843. return VM_FAULT_OOM;
  2844. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, fe->address);
  2845. if (!new_page)
  2846. return VM_FAULT_OOM;
  2847. if (mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL,
  2848. &memcg, false)) {
  2849. put_page(new_page);
  2850. return VM_FAULT_OOM;
  2851. }
  2852. ret = __do_fault(fe, pgoff, new_page, &fault_page, &fault_entry);
  2853. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2854. goto uncharge_out;
  2855. if (!(ret & VM_FAULT_DAX_LOCKED))
  2856. copy_user_highpage(new_page, fault_page, fe->address, vma);
  2857. __SetPageUptodate(new_page);
  2858. ret |= alloc_set_pte(fe, memcg, new_page);
  2859. if (fe->pte)
  2860. pte_unmap_unlock(fe->pte, fe->ptl);
  2861. if (!(ret & VM_FAULT_DAX_LOCKED)) {
  2862. unlock_page(fault_page);
  2863. put_page(fault_page);
  2864. } else {
  2865. dax_unlock_mapping_entry(vma->vm_file->f_mapping, pgoff);
  2866. }
  2867. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2868. goto uncharge_out;
  2869. return ret;
  2870. uncharge_out:
  2871. mem_cgroup_cancel_charge(new_page, memcg, false);
  2872. put_page(new_page);
  2873. return ret;
  2874. }
  2875. static int do_shared_fault(struct fault_env *fe, pgoff_t pgoff)
  2876. {
  2877. struct vm_area_struct *vma = fe->vma;
  2878. struct page *fault_page;
  2879. struct address_space *mapping;
  2880. int dirtied = 0;
  2881. int ret, tmp;
  2882. ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
  2883. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2884. return ret;
  2885. /*
  2886. * Check if the backing address space wants to know that the page is
  2887. * about to become writable
  2888. */
  2889. if (vma->vm_ops->page_mkwrite) {
  2890. unlock_page(fault_page);
  2891. tmp = do_page_mkwrite(vma, fault_page, fe->address);
  2892. if (unlikely(!tmp ||
  2893. (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
  2894. put_page(fault_page);
  2895. return tmp;
  2896. }
  2897. }
  2898. ret |= alloc_set_pte(fe, NULL, fault_page);
  2899. if (fe->pte)
  2900. pte_unmap_unlock(fe->pte, fe->ptl);
  2901. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
  2902. VM_FAULT_RETRY))) {
  2903. unlock_page(fault_page);
  2904. put_page(fault_page);
  2905. return ret;
  2906. }
  2907. if (set_page_dirty(fault_page))
  2908. dirtied = 1;
  2909. /*
  2910. * Take a local copy of the address_space - page.mapping may be zeroed
  2911. * by truncate after unlock_page(). The address_space itself remains
  2912. * pinned by vma->vm_file's reference. We rely on unlock_page()'s
  2913. * release semantics to prevent the compiler from undoing this copying.
  2914. */
  2915. mapping = page_rmapping(fault_page);
  2916. unlock_page(fault_page);
  2917. if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
  2918. /*
  2919. * Some device drivers do not set page.mapping but still
  2920. * dirty their pages
  2921. */
  2922. balance_dirty_pages_ratelimited(mapping);
  2923. }
  2924. if (!vma->vm_ops->page_mkwrite)
  2925. file_update_time(vma->vm_file);
  2926. return ret;
  2927. }
  2928. /*
  2929. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2930. * but allow concurrent faults).
  2931. * The mmap_sem may have been released depending on flags and our
  2932. * return value. See filemap_fault() and __lock_page_or_retry().
  2933. */
  2934. static int do_fault(struct fault_env *fe)
  2935. {
  2936. struct vm_area_struct *vma = fe->vma;
  2937. pgoff_t pgoff = linear_page_index(vma, fe->address);
  2938. /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
  2939. if (!vma->vm_ops->fault)
  2940. return VM_FAULT_SIGBUS;
  2941. if (!(fe->flags & FAULT_FLAG_WRITE))
  2942. return do_read_fault(fe, pgoff);
  2943. if (!(vma->vm_flags & VM_SHARED))
  2944. return do_cow_fault(fe, pgoff);
  2945. return do_shared_fault(fe, pgoff);
  2946. }
  2947. static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
  2948. unsigned long addr, int page_nid,
  2949. int *flags)
  2950. {
  2951. get_page(page);
  2952. count_vm_numa_event(NUMA_HINT_FAULTS);
  2953. if (page_nid == numa_node_id()) {
  2954. count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
  2955. *flags |= TNF_FAULT_LOCAL;
  2956. }
  2957. return mpol_misplaced(page, vma, addr);
  2958. }
  2959. static int do_numa_page(struct fault_env *fe, pte_t pte)
  2960. {
  2961. struct vm_area_struct *vma = fe->vma;
  2962. struct page *page = NULL;
  2963. int page_nid = -1;
  2964. int last_cpupid;
  2965. int target_nid;
  2966. bool migrated = false;
  2967. bool was_writable = pte_write(pte);
  2968. int flags = 0;
  2969. /*
  2970. * The "pte" at this point cannot be used safely without
  2971. * validation through pte_unmap_same(). It's of NUMA type but
  2972. * the pfn may be screwed if the read is non atomic.
  2973. *
  2974. * We can safely just do a "set_pte_at()", because the old
  2975. * page table entry is not accessible, so there would be no
  2976. * concurrent hardware modifications to the PTE.
  2977. */
  2978. fe->ptl = pte_lockptr(vma->vm_mm, fe->pmd);
  2979. spin_lock(fe->ptl);
  2980. if (unlikely(!pte_same(*fe->pte, pte))) {
  2981. pte_unmap_unlock(fe->pte, fe->ptl);
  2982. goto out;
  2983. }
  2984. /* Make it present again */
  2985. pte = pte_modify(pte, vma->vm_page_prot);
  2986. pte = pte_mkyoung(pte);
  2987. if (was_writable)
  2988. pte = pte_mkwrite(pte);
  2989. set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
  2990. update_mmu_cache(vma, fe->address, fe->pte);
  2991. page = vm_normal_page(vma, fe->address, pte);
  2992. if (!page) {
  2993. pte_unmap_unlock(fe->pte, fe->ptl);
  2994. return 0;
  2995. }
  2996. /* TODO: handle PTE-mapped THP */
  2997. if (PageCompound(page)) {
  2998. pte_unmap_unlock(fe->pte, fe->ptl);
  2999. return 0;
  3000. }
  3001. /*
  3002. * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
  3003. * much anyway since they can be in shared cache state. This misses
  3004. * the case where a mapping is writable but the process never writes
  3005. * to it but pte_write gets cleared during protection updates and
  3006. * pte_dirty has unpredictable behaviour between PTE scan updates,
  3007. * background writeback, dirty balancing and application behaviour.
  3008. */
  3009. if (!pte_write(pte))
  3010. flags |= TNF_NO_GROUP;
  3011. /*
  3012. * Flag if the page is shared between multiple address spaces. This
  3013. * is later used when determining whether to group tasks together
  3014. */
  3015. if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
  3016. flags |= TNF_SHARED;
  3017. last_cpupid = page_cpupid_last(page);
  3018. page_nid = page_to_nid(page);
  3019. target_nid = numa_migrate_prep(page, vma, fe->address, page_nid,
  3020. &flags);
  3021. pte_unmap_unlock(fe->pte, fe->ptl);
  3022. if (target_nid == -1) {
  3023. put_page(page);
  3024. goto out;
  3025. }
  3026. /* Migrate to the requested node */
  3027. migrated = migrate_misplaced_page(page, vma, target_nid);
  3028. if (migrated) {
  3029. page_nid = target_nid;
  3030. flags |= TNF_MIGRATED;
  3031. } else
  3032. flags |= TNF_MIGRATE_FAIL;
  3033. out:
  3034. if (page_nid != -1)
  3035. task_numa_fault(last_cpupid, page_nid, 1, flags);
  3036. return 0;
  3037. }
  3038. static int create_huge_pmd(struct fault_env *fe)
  3039. {
  3040. struct vm_area_struct *vma = fe->vma;
  3041. if (vma_is_anonymous(vma))
  3042. return do_huge_pmd_anonymous_page(fe);
  3043. if (vma->vm_ops->pmd_fault)
  3044. return vma->vm_ops->pmd_fault(vma, fe->address, fe->pmd,
  3045. fe->flags);
  3046. return VM_FAULT_FALLBACK;
  3047. }
  3048. static int wp_huge_pmd(struct fault_env *fe, pmd_t orig_pmd)
  3049. {
  3050. if (vma_is_anonymous(fe->vma))
  3051. return do_huge_pmd_wp_page(fe, orig_pmd);
  3052. if (fe->vma->vm_ops->pmd_fault)
  3053. return fe->vma->vm_ops->pmd_fault(fe->vma, fe->address, fe->pmd,
  3054. fe->flags);
  3055. /* COW handled on pte level: split pmd */
  3056. VM_BUG_ON_VMA(fe->vma->vm_flags & VM_SHARED, fe->vma);
  3057. split_huge_pmd(fe->vma, fe->pmd, fe->address);
  3058. return VM_FAULT_FALLBACK;
  3059. }
  3060. static inline bool vma_is_accessible(struct vm_area_struct *vma)
  3061. {
  3062. return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
  3063. }
  3064. /*
  3065. * These routines also need to handle stuff like marking pages dirty
  3066. * and/or accessed for architectures that don't do it in hardware (most
  3067. * RISC architectures). The early dirtying is also good on the i386.
  3068. *
  3069. * There is also a hook called "update_mmu_cache()" that architectures
  3070. * with external mmu caches can use to update those (ie the Sparc or
  3071. * PowerPC hashed page tables that act as extended TLBs).
  3072. *
  3073. * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
  3074. * concurrent faults).
  3075. *
  3076. * The mmap_sem may have been released depending on flags and our return value.
  3077. * See filemap_fault() and __lock_page_or_retry().
  3078. */
  3079. static int handle_pte_fault(struct fault_env *fe)
  3080. {
  3081. pte_t entry;
  3082. if (unlikely(pmd_none(*fe->pmd))) {
  3083. /*
  3084. * Leave __pte_alloc() until later: because vm_ops->fault may
  3085. * want to allocate huge page, and if we expose page table
  3086. * for an instant, it will be difficult to retract from
  3087. * concurrent faults and from rmap lookups.
  3088. */
  3089. fe->pte = NULL;
  3090. } else {
  3091. /* See comment in pte_alloc_one_map() */
  3092. if (pmd_devmap_trans_unstable(fe->pmd))
  3093. return 0;
  3094. /*
  3095. * A regular pmd is established and it can't morph into a huge
  3096. * pmd from under us anymore at this point because we hold the
  3097. * mmap_sem read mode and khugepaged takes it in write mode.
  3098. * So now it's safe to run pte_offset_map().
  3099. */
  3100. fe->pte = pte_offset_map(fe->pmd, fe->address);
  3101. entry = *fe->pte;
  3102. /*
  3103. * some architectures can have larger ptes than wordsize,
  3104. * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
  3105. * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee
  3106. * atomic accesses. The code below just needs a consistent
  3107. * view for the ifs and we later double check anyway with the
  3108. * ptl lock held. So here a barrier will do.
  3109. */
  3110. barrier();
  3111. if (pte_none(entry)) {
  3112. pte_unmap(fe->pte);
  3113. fe->pte = NULL;
  3114. }
  3115. }
  3116. if (!fe->pte) {
  3117. if (vma_is_anonymous(fe->vma))
  3118. return do_anonymous_page(fe);
  3119. else
  3120. return do_fault(fe);
  3121. }
  3122. if (!pte_present(entry))
  3123. return do_swap_page(fe, entry);
  3124. if (pte_protnone(entry) && vma_is_accessible(fe->vma))
  3125. return do_numa_page(fe, entry);
  3126. fe->ptl = pte_lockptr(fe->vma->vm_mm, fe->pmd);
  3127. spin_lock(fe->ptl);
  3128. if (unlikely(!pte_same(*fe->pte, entry)))
  3129. goto unlock;
  3130. if (fe->flags & FAULT_FLAG_WRITE) {
  3131. if (!pte_write(entry))
  3132. return do_wp_page(fe, entry);
  3133. entry = pte_mkdirty(entry);
  3134. }
  3135. entry = pte_mkyoung(entry);
  3136. if (ptep_set_access_flags(fe->vma, fe->address, fe->pte, entry,
  3137. fe->flags & FAULT_FLAG_WRITE)) {
  3138. update_mmu_cache(fe->vma, fe->address, fe->pte);
  3139. } else {
  3140. /*
  3141. * This is needed only for protection faults but the arch code
  3142. * is not yet telling us if this is a protection fault or not.
  3143. * This still avoids useless tlb flushes for .text page faults
  3144. * with threads.
  3145. */
  3146. if (fe->flags & FAULT_FLAG_WRITE)
  3147. flush_tlb_fix_spurious_fault(fe->vma, fe->address);
  3148. }
  3149. unlock:
  3150. pte_unmap_unlock(fe->pte, fe->ptl);
  3151. return 0;
  3152. }
  3153. /*
  3154. * By the time we get here, we already hold the mm semaphore
  3155. *
  3156. * The mmap_sem may have been released depending on flags and our
  3157. * return value. See filemap_fault() and __lock_page_or_retry().
  3158. */
  3159. static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
  3160. unsigned int flags)
  3161. {
  3162. struct fault_env fe = {
  3163. .vma = vma,
  3164. .address = address,
  3165. .flags = flags,
  3166. };
  3167. struct mm_struct *mm = vma->vm_mm;
  3168. pgd_t *pgd;
  3169. pud_t *pud;
  3170. pgd = pgd_offset(mm, address);
  3171. pud = pud_alloc(mm, pgd, address);
  3172. if (!pud)
  3173. return VM_FAULT_OOM;
  3174. fe.pmd = pmd_alloc(mm, pud, address);
  3175. if (!fe.pmd)
  3176. return VM_FAULT_OOM;
  3177. if (pmd_none(*fe.pmd) && transparent_hugepage_enabled(vma)) {
  3178. int ret = create_huge_pmd(&fe);
  3179. if (!(ret & VM_FAULT_FALLBACK))
  3180. return ret;
  3181. } else {
  3182. pmd_t orig_pmd = *fe.pmd;
  3183. int ret;
  3184. barrier();
  3185. if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
  3186. if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
  3187. return do_huge_pmd_numa_page(&fe, orig_pmd);
  3188. if ((fe.flags & FAULT_FLAG_WRITE) &&
  3189. !pmd_write(orig_pmd)) {
  3190. ret = wp_huge_pmd(&fe, orig_pmd);
  3191. if (!(ret & VM_FAULT_FALLBACK))
  3192. return ret;
  3193. } else {
  3194. huge_pmd_set_accessed(&fe, orig_pmd);
  3195. return 0;
  3196. }
  3197. }
  3198. }
  3199. return handle_pte_fault(&fe);
  3200. }
  3201. /*
  3202. * By the time we get here, we already hold the mm semaphore
  3203. *
  3204. * The mmap_sem may have been released depending on flags and our
  3205. * return value. See filemap_fault() and __lock_page_or_retry().
  3206. */
  3207. int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
  3208. unsigned int flags)
  3209. {
  3210. int ret;
  3211. __set_current_state(TASK_RUNNING);
  3212. count_vm_event(PGFAULT);
  3213. mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT);
  3214. /* do counter updates before entering really critical section. */
  3215. check_sync_rss_stat(current);
  3216. if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
  3217. flags & FAULT_FLAG_INSTRUCTION,
  3218. flags & FAULT_FLAG_REMOTE))
  3219. return VM_FAULT_SIGSEGV;
  3220. /*
  3221. * Enable the memcg OOM handling for faults triggered in user
  3222. * space. Kernel faults are handled more gracefully.
  3223. */
  3224. if (flags & FAULT_FLAG_USER)
  3225. mem_cgroup_oom_enable();
  3226. if (unlikely(is_vm_hugetlb_page(vma)))
  3227. ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
  3228. else
  3229. ret = __handle_mm_fault(vma, address, flags);
  3230. if (flags & FAULT_FLAG_USER) {
  3231. mem_cgroup_oom_disable();
  3232. /*
  3233. * The task may have entered a memcg OOM situation but
  3234. * if the allocation error was handled gracefully (no
  3235. * VM_FAULT_OOM), there is no need to kill anything.
  3236. * Just clean up the OOM state peacefully.
  3237. */
  3238. if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
  3239. mem_cgroup_oom_synchronize(false);
  3240. }
  3241. /*
  3242. * This mm has been already reaped by the oom reaper and so the
  3243. * refault cannot be trusted in general. Anonymous refaults would
  3244. * lose data and give a zero page instead e.g. This is especially
  3245. * problem for use_mm() because regular tasks will just die and
  3246. * the corrupted data will not be visible anywhere while kthread
  3247. * will outlive the oom victim and potentially propagate the date
  3248. * further.
  3249. */
  3250. if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
  3251. && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags))) {
  3252. /*
  3253. * We are going to enforce SIGBUS but the PF path might have
  3254. * dropped the mmap_sem already so take it again so that
  3255. * we do not break expectations of all arch specific PF paths
  3256. * and g-u-p
  3257. */
  3258. if (ret & VM_FAULT_RETRY)
  3259. down_read(&vma->vm_mm->mmap_sem);
  3260. ret = VM_FAULT_SIGBUS;
  3261. }
  3262. return ret;
  3263. }
  3264. EXPORT_SYMBOL_GPL(handle_mm_fault);
  3265. #ifndef __PAGETABLE_PUD_FOLDED
  3266. /*
  3267. * Allocate page upper directory.
  3268. * We've already handled the fast-path in-line.
  3269. */
  3270. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  3271. {
  3272. pud_t *new = pud_alloc_one(mm, address);
  3273. if (!new)
  3274. return -ENOMEM;
  3275. smp_wmb(); /* See comment in __pte_alloc */
  3276. spin_lock(&mm->page_table_lock);
  3277. if (pgd_present(*pgd)) /* Another has populated it */
  3278. pud_free(mm, new);
  3279. else
  3280. pgd_populate(mm, pgd, new);
  3281. spin_unlock(&mm->page_table_lock);
  3282. return 0;
  3283. }
  3284. #endif /* __PAGETABLE_PUD_FOLDED */
  3285. #ifndef __PAGETABLE_PMD_FOLDED
  3286. /*
  3287. * Allocate page middle directory.
  3288. * We've already handled the fast-path in-line.
  3289. */
  3290. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  3291. {
  3292. pmd_t *new = pmd_alloc_one(mm, address);
  3293. if (!new)
  3294. return -ENOMEM;
  3295. smp_wmb(); /* See comment in __pte_alloc */
  3296. spin_lock(&mm->page_table_lock);
  3297. #ifndef __ARCH_HAS_4LEVEL_HACK
  3298. if (!pud_present(*pud)) {
  3299. mm_inc_nr_pmds(mm);
  3300. pud_populate(mm, pud, new);
  3301. } else /* Another has populated it */
  3302. pmd_free(mm, new);
  3303. #else
  3304. if (!pgd_present(*pud)) {
  3305. mm_inc_nr_pmds(mm);
  3306. pgd_populate(mm, pud, new);
  3307. } else /* Another has populated it */
  3308. pmd_free(mm, new);
  3309. #endif /* __ARCH_HAS_4LEVEL_HACK */
  3310. spin_unlock(&mm->page_table_lock);
  3311. return 0;
  3312. }
  3313. #endif /* __PAGETABLE_PMD_FOLDED */
  3314. static int __follow_pte(struct mm_struct *mm, unsigned long address,
  3315. pte_t **ptepp, spinlock_t **ptlp)
  3316. {
  3317. pgd_t *pgd;
  3318. pud_t *pud;
  3319. pmd_t *pmd;
  3320. pte_t *ptep;
  3321. pgd = pgd_offset(mm, address);
  3322. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  3323. goto out;
  3324. pud = pud_offset(pgd, address);
  3325. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  3326. goto out;
  3327. pmd = pmd_offset(pud, address);
  3328. VM_BUG_ON(pmd_trans_huge(*pmd));
  3329. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  3330. goto out;
  3331. /* We cannot handle huge page PFN maps. Luckily they don't exist. */
  3332. if (pmd_huge(*pmd))
  3333. goto out;
  3334. ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
  3335. if (!ptep)
  3336. goto out;
  3337. if (!pte_present(*ptep))
  3338. goto unlock;
  3339. *ptepp = ptep;
  3340. return 0;
  3341. unlock:
  3342. pte_unmap_unlock(ptep, *ptlp);
  3343. out:
  3344. return -EINVAL;
  3345. }
  3346. static inline int follow_pte(struct mm_struct *mm, unsigned long address,
  3347. pte_t **ptepp, spinlock_t **ptlp)
  3348. {
  3349. int res;
  3350. /* (void) is needed to make gcc happy */
  3351. (void) __cond_lock(*ptlp,
  3352. !(res = __follow_pte(mm, address, ptepp, ptlp)));
  3353. return res;
  3354. }
  3355. /**
  3356. * follow_pfn - look up PFN at a user virtual address
  3357. * @vma: memory mapping
  3358. * @address: user virtual address
  3359. * @pfn: location to store found PFN
  3360. *
  3361. * Only IO mappings and raw PFN mappings are allowed.
  3362. *
  3363. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  3364. */
  3365. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  3366. unsigned long *pfn)
  3367. {
  3368. int ret = -EINVAL;
  3369. spinlock_t *ptl;
  3370. pte_t *ptep;
  3371. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3372. return ret;
  3373. ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
  3374. if (ret)
  3375. return ret;
  3376. *pfn = pte_pfn(*ptep);
  3377. pte_unmap_unlock(ptep, ptl);
  3378. return 0;
  3379. }
  3380. EXPORT_SYMBOL(follow_pfn);
  3381. #ifdef CONFIG_HAVE_IOREMAP_PROT
  3382. int follow_phys(struct vm_area_struct *vma,
  3383. unsigned long address, unsigned int flags,
  3384. unsigned long *prot, resource_size_t *phys)
  3385. {
  3386. int ret = -EINVAL;
  3387. pte_t *ptep, pte;
  3388. spinlock_t *ptl;
  3389. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3390. goto out;
  3391. if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
  3392. goto out;
  3393. pte = *ptep;
  3394. if ((flags & FOLL_WRITE) && !pte_write(pte))
  3395. goto unlock;
  3396. *prot = pgprot_val(pte_pgprot(pte));
  3397. *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
  3398. ret = 0;
  3399. unlock:
  3400. pte_unmap_unlock(ptep, ptl);
  3401. out:
  3402. return ret;
  3403. }
  3404. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  3405. void *buf, int len, int write)
  3406. {
  3407. resource_size_t phys_addr;
  3408. unsigned long prot = 0;
  3409. void __iomem *maddr;
  3410. int offset = addr & (PAGE_SIZE-1);
  3411. if (follow_phys(vma, addr, write, &prot, &phys_addr))
  3412. return -EINVAL;
  3413. maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
  3414. if (write)
  3415. memcpy_toio(maddr + offset, buf, len);
  3416. else
  3417. memcpy_fromio(buf, maddr + offset, len);
  3418. iounmap(maddr);
  3419. return len;
  3420. }
  3421. EXPORT_SYMBOL_GPL(generic_access_phys);
  3422. #endif
  3423. /*
  3424. * Access another process' address space as given in mm. If non-NULL, use the
  3425. * given task for page fault accounting.
  3426. */
  3427. int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  3428. unsigned long addr, void *buf, int len, unsigned int gup_flags)
  3429. {
  3430. struct vm_area_struct *vma;
  3431. void *old_buf = buf;
  3432. int write = gup_flags & FOLL_WRITE;
  3433. down_read(&mm->mmap_sem);
  3434. /* ignore errors, just check how much was successfully transferred */
  3435. while (len) {
  3436. int bytes, ret, offset;
  3437. void *maddr;
  3438. struct page *page = NULL;
  3439. ret = get_user_pages_remote(tsk, mm, addr, 1,
  3440. gup_flags, &page, &vma);
  3441. if (ret <= 0) {
  3442. #ifndef CONFIG_HAVE_IOREMAP_PROT
  3443. break;
  3444. #else
  3445. /*
  3446. * Check if this is a VM_IO | VM_PFNMAP VMA, which
  3447. * we can access using slightly different code.
  3448. */
  3449. vma = find_vma(mm, addr);
  3450. if (!vma || vma->vm_start > addr)
  3451. break;
  3452. if (vma->vm_ops && vma->vm_ops->access)
  3453. ret = vma->vm_ops->access(vma, addr, buf,
  3454. len, write);
  3455. if (ret <= 0)
  3456. break;
  3457. bytes = ret;
  3458. #endif
  3459. } else {
  3460. bytes = len;
  3461. offset = addr & (PAGE_SIZE-1);
  3462. if (bytes > PAGE_SIZE-offset)
  3463. bytes = PAGE_SIZE-offset;
  3464. maddr = kmap(page);
  3465. if (write) {
  3466. copy_to_user_page(vma, page, addr,
  3467. maddr + offset, buf, bytes);
  3468. set_page_dirty_lock(page);
  3469. } else {
  3470. copy_from_user_page(vma, page, addr,
  3471. buf, maddr + offset, bytes);
  3472. }
  3473. kunmap(page);
  3474. put_page(page);
  3475. }
  3476. len -= bytes;
  3477. buf += bytes;
  3478. addr += bytes;
  3479. }
  3480. up_read(&mm->mmap_sem);
  3481. return buf - old_buf;
  3482. }
  3483. /**
  3484. * access_remote_vm - access another process' address space
  3485. * @mm: the mm_struct of the target address space
  3486. * @addr: start address to access
  3487. * @buf: source or destination buffer
  3488. * @len: number of bytes to transfer
  3489. * @gup_flags: flags modifying lookup behaviour
  3490. *
  3491. * The caller must hold a reference on @mm.
  3492. */
  3493. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  3494. void *buf, int len, unsigned int gup_flags)
  3495. {
  3496. return __access_remote_vm(NULL, mm, addr, buf, len, gup_flags);
  3497. }
  3498. /*
  3499. * Access another process' address space.
  3500. * Source/target buffer must be kernel space,
  3501. * Do not walk the page table directly, use get_user_pages
  3502. */
  3503. int access_process_vm(struct task_struct *tsk, unsigned long addr,
  3504. void *buf, int len, unsigned int gup_flags)
  3505. {
  3506. struct mm_struct *mm;
  3507. int ret;
  3508. mm = get_task_mm(tsk);
  3509. if (!mm)
  3510. return 0;
  3511. ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
  3512. mmput(mm);
  3513. return ret;
  3514. }
  3515. /*
  3516. * Print the name of a VMA.
  3517. */
  3518. void print_vma_addr(char *prefix, unsigned long ip)
  3519. {
  3520. struct mm_struct *mm = current->mm;
  3521. struct vm_area_struct *vma;
  3522. /*
  3523. * Do not print if we are in atomic
  3524. * contexts (in exception stacks, etc.):
  3525. */
  3526. if (preempt_count())
  3527. return;
  3528. down_read(&mm->mmap_sem);
  3529. vma = find_vma(mm, ip);
  3530. if (vma && vma->vm_file) {
  3531. struct file *f = vma->vm_file;
  3532. char *buf = (char *)__get_free_page(GFP_KERNEL);
  3533. if (buf) {
  3534. char *p;
  3535. p = file_path(f, buf, PAGE_SIZE);
  3536. if (IS_ERR(p))
  3537. p = "?";
  3538. printk("%s%s[%lx+%lx]", prefix, kbasename(p),
  3539. vma->vm_start,
  3540. vma->vm_end - vma->vm_start);
  3541. free_page((unsigned long)buf);
  3542. }
  3543. }
  3544. up_read(&mm->mmap_sem);
  3545. }
  3546. #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  3547. void __might_fault(const char *file, int line)
  3548. {
  3549. /*
  3550. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  3551. * holding the mmap_sem, this is safe because kernel memory doesn't
  3552. * get paged out, therefore we'll never actually fault, and the
  3553. * below annotations will generate false positives.
  3554. */
  3555. if (segment_eq(get_fs(), KERNEL_DS))
  3556. return;
  3557. if (pagefault_disabled())
  3558. return;
  3559. __might_sleep(file, line, 0);
  3560. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  3561. if (current->mm)
  3562. might_lock_read(&current->mm->mmap_sem);
  3563. #endif
  3564. }
  3565. EXPORT_SYMBOL(__might_fault);
  3566. #endif
  3567. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  3568. static void clear_gigantic_page(struct page *page,
  3569. unsigned long addr,
  3570. unsigned int pages_per_huge_page)
  3571. {
  3572. int i;
  3573. struct page *p = page;
  3574. might_sleep();
  3575. for (i = 0; i < pages_per_huge_page;
  3576. i++, p = mem_map_next(p, page, i)) {
  3577. cond_resched();
  3578. clear_user_highpage(p, addr + i * PAGE_SIZE);
  3579. }
  3580. }
  3581. void clear_huge_page(struct page *page,
  3582. unsigned long addr, unsigned int pages_per_huge_page)
  3583. {
  3584. int i;
  3585. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3586. clear_gigantic_page(page, addr, pages_per_huge_page);
  3587. return;
  3588. }
  3589. might_sleep();
  3590. for (i = 0; i < pages_per_huge_page; i++) {
  3591. cond_resched();
  3592. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  3593. }
  3594. }
  3595. static void copy_user_gigantic_page(struct page *dst, struct page *src,
  3596. unsigned long addr,
  3597. struct vm_area_struct *vma,
  3598. unsigned int pages_per_huge_page)
  3599. {
  3600. int i;
  3601. struct page *dst_base = dst;
  3602. struct page *src_base = src;
  3603. for (i = 0; i < pages_per_huge_page; ) {
  3604. cond_resched();
  3605. copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
  3606. i++;
  3607. dst = mem_map_next(dst, dst_base, i);
  3608. src = mem_map_next(src, src_base, i);
  3609. }
  3610. }
  3611. void copy_user_huge_page(struct page *dst, struct page *src,
  3612. unsigned long addr, struct vm_area_struct *vma,
  3613. unsigned int pages_per_huge_page)
  3614. {
  3615. int i;
  3616. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3617. copy_user_gigantic_page(dst, src, addr, vma,
  3618. pages_per_huge_page);
  3619. return;
  3620. }
  3621. might_sleep();
  3622. for (i = 0; i < pages_per_huge_page; i++) {
  3623. cond_resched();
  3624. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  3625. }
  3626. }
  3627. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
  3628. #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
  3629. static struct kmem_cache *page_ptl_cachep;
  3630. void __init ptlock_cache_init(void)
  3631. {
  3632. page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
  3633. SLAB_PANIC, NULL);
  3634. }
  3635. bool ptlock_alloc(struct page *page)
  3636. {
  3637. spinlock_t *ptl;
  3638. ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
  3639. if (!ptl)
  3640. return false;
  3641. page->ptl = ptl;
  3642. return true;
  3643. }
  3644. void ptlock_free(struct page *page)
  3645. {
  3646. kmem_cache_free(page_ptl_cachep, page->ptl);
  3647. }
  3648. #endif