memory.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121
  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. #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
  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. arch_enter_lazy_mmu_mode();
  991. do {
  992. pte_t ptent = *pte;
  993. if (pte_none(ptent)) {
  994. continue;
  995. }
  996. if (pte_present(ptent)) {
  997. struct page *page;
  998. page = vm_normal_page(vma, addr, ptent);
  999. if (unlikely(details) && page) {
  1000. /*
  1001. * unmap_shared_mapping_pages() wants to
  1002. * invalidate cache without truncating:
  1003. * unmap shared but keep private pages.
  1004. */
  1005. if (details->check_mapping &&
  1006. details->check_mapping != page_rmapping(page))
  1007. continue;
  1008. }
  1009. ptent = ptep_get_and_clear_full(mm, addr, pte,
  1010. tlb->fullmm);
  1011. tlb_remove_tlb_entry(tlb, pte, addr);
  1012. if (unlikely(!page))
  1013. continue;
  1014. if (!PageAnon(page)) {
  1015. if (pte_dirty(ptent)) {
  1016. /*
  1017. * oom_reaper cannot tear down dirty
  1018. * pages
  1019. */
  1020. if (unlikely(details && details->ignore_dirty))
  1021. continue;
  1022. force_flush = 1;
  1023. set_page_dirty(page);
  1024. }
  1025. if (pte_young(ptent) &&
  1026. likely(!(vma->vm_flags & VM_SEQ_READ)))
  1027. mark_page_accessed(page);
  1028. }
  1029. rss[mm_counter(page)]--;
  1030. page_remove_rmap(page, false);
  1031. if (unlikely(page_mapcount(page) < 0))
  1032. print_bad_pte(vma, addr, ptent, page);
  1033. if (unlikely(__tlb_remove_page(tlb, page))) {
  1034. force_flush = 1;
  1035. pending_page = page;
  1036. addr += PAGE_SIZE;
  1037. break;
  1038. }
  1039. continue;
  1040. }
  1041. /* only check swap_entries if explicitly asked for in details */
  1042. if (unlikely(details && !details->check_swap_entries))
  1043. continue;
  1044. entry = pte_to_swp_entry(ptent);
  1045. if (!non_swap_entry(entry))
  1046. rss[MM_SWAPENTS]--;
  1047. else if (is_migration_entry(entry)) {
  1048. struct page *page;
  1049. page = migration_entry_to_page(entry);
  1050. rss[mm_counter(page)]--;
  1051. }
  1052. if (unlikely(!free_swap_and_cache(entry)))
  1053. print_bad_pte(vma, addr, ptent, NULL);
  1054. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  1055. } while (pte++, addr += PAGE_SIZE, addr != end);
  1056. add_mm_rss_vec(mm, rss);
  1057. arch_leave_lazy_mmu_mode();
  1058. /* Do the actual TLB flush before dropping ptl */
  1059. if (force_flush)
  1060. tlb_flush_mmu_tlbonly(tlb);
  1061. pte_unmap_unlock(start_pte, ptl);
  1062. /*
  1063. * If we forced a TLB flush (either due to running out of
  1064. * batch buffers or because we needed to flush dirty TLB
  1065. * entries before releasing the ptl), free the batched
  1066. * memory too. Restart if we didn't do everything.
  1067. */
  1068. if (force_flush) {
  1069. force_flush = 0;
  1070. tlb_flush_mmu_free(tlb);
  1071. if (pending_page) {
  1072. /* remove the page with new size */
  1073. __tlb_remove_pte_page(tlb, pending_page);
  1074. pending_page = NULL;
  1075. }
  1076. if (addr != end)
  1077. goto again;
  1078. }
  1079. return addr;
  1080. }
  1081. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  1082. struct vm_area_struct *vma, pud_t *pud,
  1083. unsigned long addr, unsigned long end,
  1084. struct zap_details *details)
  1085. {
  1086. pmd_t *pmd;
  1087. unsigned long next;
  1088. pmd = pmd_offset(pud, addr);
  1089. do {
  1090. next = pmd_addr_end(addr, end);
  1091. if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
  1092. if (next - addr != HPAGE_PMD_SIZE) {
  1093. VM_BUG_ON_VMA(vma_is_anonymous(vma) &&
  1094. !rwsem_is_locked(&tlb->mm->mmap_sem), vma);
  1095. split_huge_pmd(vma, pmd, addr);
  1096. } else if (zap_huge_pmd(tlb, vma, pmd, addr))
  1097. goto next;
  1098. /* fall through */
  1099. }
  1100. /*
  1101. * Here there can be other concurrent MADV_DONTNEED or
  1102. * trans huge page faults running, and if the pmd is
  1103. * none or trans huge it can change under us. This is
  1104. * because MADV_DONTNEED holds the mmap_sem in read
  1105. * mode.
  1106. */
  1107. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  1108. goto next;
  1109. next = zap_pte_range(tlb, vma, pmd, addr, next, details);
  1110. next:
  1111. cond_resched();
  1112. } while (pmd++, addr = next, addr != end);
  1113. return addr;
  1114. }
  1115. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  1116. struct vm_area_struct *vma, pgd_t *pgd,
  1117. unsigned long addr, unsigned long end,
  1118. struct zap_details *details)
  1119. {
  1120. pud_t *pud;
  1121. unsigned long next;
  1122. pud = pud_offset(pgd, addr);
  1123. do {
  1124. next = pud_addr_end(addr, end);
  1125. if (pud_none_or_clear_bad(pud))
  1126. continue;
  1127. next = zap_pmd_range(tlb, vma, pud, addr, next, details);
  1128. } while (pud++, addr = next, addr != end);
  1129. return addr;
  1130. }
  1131. void unmap_page_range(struct mmu_gather *tlb,
  1132. struct vm_area_struct *vma,
  1133. unsigned long addr, unsigned long end,
  1134. struct zap_details *details)
  1135. {
  1136. pgd_t *pgd;
  1137. unsigned long next;
  1138. BUG_ON(addr >= end);
  1139. tlb_start_vma(tlb, vma);
  1140. pgd = pgd_offset(vma->vm_mm, addr);
  1141. do {
  1142. next = pgd_addr_end(addr, end);
  1143. if (pgd_none_or_clear_bad(pgd))
  1144. continue;
  1145. next = zap_pud_range(tlb, vma, pgd, addr, next, details);
  1146. } while (pgd++, addr = next, addr != end);
  1147. tlb_end_vma(tlb, vma);
  1148. }
  1149. static void unmap_single_vma(struct mmu_gather *tlb,
  1150. struct vm_area_struct *vma, unsigned long start_addr,
  1151. unsigned long end_addr,
  1152. struct zap_details *details)
  1153. {
  1154. unsigned long start = max(vma->vm_start, start_addr);
  1155. unsigned long end;
  1156. if (start >= vma->vm_end)
  1157. return;
  1158. end = min(vma->vm_end, end_addr);
  1159. if (end <= vma->vm_start)
  1160. return;
  1161. if (vma->vm_file)
  1162. uprobe_munmap(vma, start, end);
  1163. if (unlikely(vma->vm_flags & VM_PFNMAP))
  1164. untrack_pfn(vma, 0, 0);
  1165. if (start != end) {
  1166. if (unlikely(is_vm_hugetlb_page(vma))) {
  1167. /*
  1168. * It is undesirable to test vma->vm_file as it
  1169. * should be non-null for valid hugetlb area.
  1170. * However, vm_file will be NULL in the error
  1171. * cleanup path of mmap_region. When
  1172. * hugetlbfs ->mmap method fails,
  1173. * mmap_region() nullifies vma->vm_file
  1174. * before calling this function to clean up.
  1175. * Since no pte has actually been setup, it is
  1176. * safe to do nothing in this case.
  1177. */
  1178. if (vma->vm_file) {
  1179. i_mmap_lock_write(vma->vm_file->f_mapping);
  1180. __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
  1181. i_mmap_unlock_write(vma->vm_file->f_mapping);
  1182. }
  1183. } else
  1184. unmap_page_range(tlb, vma, start, end, details);
  1185. }
  1186. }
  1187. /**
  1188. * unmap_vmas - unmap a range of memory covered by a list of vma's
  1189. * @tlb: address of the caller's struct mmu_gather
  1190. * @vma: the starting vma
  1191. * @start_addr: virtual address at which to start unmapping
  1192. * @end_addr: virtual address at which to end unmapping
  1193. *
  1194. * Unmap all pages in the vma list.
  1195. *
  1196. * Only addresses between `start' and `end' will be unmapped.
  1197. *
  1198. * The VMA list must be sorted in ascending virtual address order.
  1199. *
  1200. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  1201. * range after unmap_vmas() returns. So the only responsibility here is to
  1202. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  1203. * drops the lock and schedules.
  1204. */
  1205. void unmap_vmas(struct mmu_gather *tlb,
  1206. struct vm_area_struct *vma, unsigned long start_addr,
  1207. unsigned long end_addr)
  1208. {
  1209. struct mm_struct *mm = vma->vm_mm;
  1210. mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
  1211. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
  1212. unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
  1213. mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
  1214. }
  1215. /**
  1216. * zap_page_range - remove user pages in a given range
  1217. * @vma: vm_area_struct holding the applicable pages
  1218. * @start: starting address of pages to zap
  1219. * @size: number of bytes to zap
  1220. * @details: details of shared cache invalidation
  1221. *
  1222. * Caller must protect the VMA list
  1223. */
  1224. void zap_page_range(struct vm_area_struct *vma, unsigned long start,
  1225. unsigned long size, struct zap_details *details)
  1226. {
  1227. struct mm_struct *mm = vma->vm_mm;
  1228. struct mmu_gather tlb;
  1229. unsigned long end = start + size;
  1230. lru_add_drain();
  1231. tlb_gather_mmu(&tlb, mm, start, end);
  1232. update_hiwater_rss(mm);
  1233. mmu_notifier_invalidate_range_start(mm, start, end);
  1234. for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
  1235. unmap_single_vma(&tlb, vma, start, end, details);
  1236. mmu_notifier_invalidate_range_end(mm, start, end);
  1237. tlb_finish_mmu(&tlb, start, end);
  1238. }
  1239. /**
  1240. * zap_page_range_single - remove user pages in a given range
  1241. * @vma: vm_area_struct holding the applicable pages
  1242. * @address: starting address of pages to zap
  1243. * @size: number of bytes to zap
  1244. * @details: details of shared cache invalidation
  1245. *
  1246. * The range must fit into one VMA.
  1247. */
  1248. static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
  1249. unsigned long size, struct zap_details *details)
  1250. {
  1251. struct mm_struct *mm = vma->vm_mm;
  1252. struct mmu_gather tlb;
  1253. unsigned long end = address + size;
  1254. lru_add_drain();
  1255. tlb_gather_mmu(&tlb, mm, address, end);
  1256. update_hiwater_rss(mm);
  1257. mmu_notifier_invalidate_range_start(mm, address, end);
  1258. unmap_single_vma(&tlb, vma, address, end, details);
  1259. mmu_notifier_invalidate_range_end(mm, address, end);
  1260. tlb_finish_mmu(&tlb, address, end);
  1261. }
  1262. /**
  1263. * zap_vma_ptes - remove ptes mapping the vma
  1264. * @vma: vm_area_struct holding ptes to be zapped
  1265. * @address: starting address of pages to zap
  1266. * @size: number of bytes to zap
  1267. *
  1268. * This function only unmaps ptes assigned to VM_PFNMAP vmas.
  1269. *
  1270. * The entire address range must be fully contained within the vma.
  1271. *
  1272. * Returns 0 if successful.
  1273. */
  1274. int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  1275. unsigned long size)
  1276. {
  1277. if (address < vma->vm_start || address + size > vma->vm_end ||
  1278. !(vma->vm_flags & VM_PFNMAP))
  1279. return -1;
  1280. zap_page_range_single(vma, address, size, NULL);
  1281. return 0;
  1282. }
  1283. EXPORT_SYMBOL_GPL(zap_vma_ptes);
  1284. pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1285. spinlock_t **ptl)
  1286. {
  1287. pgd_t * pgd = pgd_offset(mm, addr);
  1288. pud_t * pud = pud_alloc(mm, pgd, addr);
  1289. if (pud) {
  1290. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1291. if (pmd) {
  1292. VM_BUG_ON(pmd_trans_huge(*pmd));
  1293. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1294. }
  1295. }
  1296. return NULL;
  1297. }
  1298. /*
  1299. * This is the old fallback for page remapping.
  1300. *
  1301. * For historical reasons, it only allows reserved pages. Only
  1302. * old drivers should use this, and they needed to mark their
  1303. * pages reserved for the old functions anyway.
  1304. */
  1305. static int insert_page(struct vm_area_struct *vma, unsigned long addr,
  1306. struct page *page, pgprot_t prot)
  1307. {
  1308. struct mm_struct *mm = vma->vm_mm;
  1309. int retval;
  1310. pte_t *pte;
  1311. spinlock_t *ptl;
  1312. retval = -EINVAL;
  1313. if (PageAnon(page))
  1314. goto out;
  1315. retval = -ENOMEM;
  1316. flush_dcache_page(page);
  1317. pte = get_locked_pte(mm, addr, &ptl);
  1318. if (!pte)
  1319. goto out;
  1320. retval = -EBUSY;
  1321. if (!pte_none(*pte))
  1322. goto out_unlock;
  1323. /* Ok, finally just insert the thing.. */
  1324. get_page(page);
  1325. inc_mm_counter_fast(mm, mm_counter_file(page));
  1326. page_add_file_rmap(page, false);
  1327. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1328. retval = 0;
  1329. pte_unmap_unlock(pte, ptl);
  1330. return retval;
  1331. out_unlock:
  1332. pte_unmap_unlock(pte, ptl);
  1333. out:
  1334. return retval;
  1335. }
  1336. /**
  1337. * vm_insert_page - insert single page into user vma
  1338. * @vma: user vma to map to
  1339. * @addr: target user address of this page
  1340. * @page: source kernel page
  1341. *
  1342. * This allows drivers to insert individual pages they've allocated
  1343. * into a user vma.
  1344. *
  1345. * The page has to be a nice clean _individual_ kernel allocation.
  1346. * If you allocate a compound page, you need to have marked it as
  1347. * such (__GFP_COMP), or manually just split the page up yourself
  1348. * (see split_page()).
  1349. *
  1350. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1351. * took an arbitrary page protection parameter. This doesn't allow
  1352. * that. Your vma protection will have to be set up correctly, which
  1353. * means that if you want a shared writable mapping, you'd better
  1354. * ask for a shared writable mapping!
  1355. *
  1356. * The page does not need to be reserved.
  1357. *
  1358. * Usually this function is called from f_op->mmap() handler
  1359. * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
  1360. * Caller must set VM_MIXEDMAP on vma if it wants to call this
  1361. * function from other places, for example from page-fault handler.
  1362. */
  1363. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  1364. struct page *page)
  1365. {
  1366. if (addr < vma->vm_start || addr >= vma->vm_end)
  1367. return -EFAULT;
  1368. if (!page_count(page))
  1369. return -EINVAL;
  1370. if (!(vma->vm_flags & VM_MIXEDMAP)) {
  1371. BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
  1372. BUG_ON(vma->vm_flags & VM_PFNMAP);
  1373. vma->vm_flags |= VM_MIXEDMAP;
  1374. }
  1375. return insert_page(vma, addr, page, vma->vm_page_prot);
  1376. }
  1377. EXPORT_SYMBOL(vm_insert_page);
  1378. static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1379. pfn_t pfn, pgprot_t prot)
  1380. {
  1381. struct mm_struct *mm = vma->vm_mm;
  1382. int retval;
  1383. pte_t *pte, entry;
  1384. spinlock_t *ptl;
  1385. retval = -ENOMEM;
  1386. pte = get_locked_pte(mm, addr, &ptl);
  1387. if (!pte)
  1388. goto out;
  1389. retval = -EBUSY;
  1390. if (!pte_none(*pte))
  1391. goto out_unlock;
  1392. /* Ok, finally just insert the thing.. */
  1393. if (pfn_t_devmap(pfn))
  1394. entry = pte_mkdevmap(pfn_t_pte(pfn, prot));
  1395. else
  1396. entry = pte_mkspecial(pfn_t_pte(pfn, prot));
  1397. set_pte_at(mm, addr, pte, entry);
  1398. update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
  1399. retval = 0;
  1400. out_unlock:
  1401. pte_unmap_unlock(pte, ptl);
  1402. out:
  1403. return retval;
  1404. }
  1405. /**
  1406. * vm_insert_pfn - insert single pfn into user vma
  1407. * @vma: user vma to map to
  1408. * @addr: target user address of this page
  1409. * @pfn: source kernel pfn
  1410. *
  1411. * Similar to vm_insert_page, this allows drivers to insert individual pages
  1412. * they've allocated into a user vma. Same comments apply.
  1413. *
  1414. * This function should only be called from a vm_ops->fault handler, and
  1415. * in that case the handler should return NULL.
  1416. *
  1417. * vma cannot be a COW mapping.
  1418. *
  1419. * As this is called only for pages that do not currently exist, we
  1420. * do not need to flush old virtual caches or the TLB.
  1421. */
  1422. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1423. unsigned long pfn)
  1424. {
  1425. return vm_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);
  1426. }
  1427. EXPORT_SYMBOL(vm_insert_pfn);
  1428. /**
  1429. * vm_insert_pfn_prot - insert single pfn into user vma with specified pgprot
  1430. * @vma: user vma to map to
  1431. * @addr: target user address of this page
  1432. * @pfn: source kernel pfn
  1433. * @pgprot: pgprot flags for the inserted page
  1434. *
  1435. * This is exactly like vm_insert_pfn, except that it allows drivers to
  1436. * to override pgprot on a per-page basis.
  1437. *
  1438. * This only makes sense for IO mappings, and it makes no sense for
  1439. * cow mappings. In general, using multiple vmas is preferable;
  1440. * vm_insert_pfn_prot should only be used if using multiple VMAs is
  1441. * impractical.
  1442. */
  1443. int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
  1444. unsigned long pfn, pgprot_t pgprot)
  1445. {
  1446. int ret;
  1447. /*
  1448. * Technically, architectures with pte_special can avoid all these
  1449. * restrictions (same for remap_pfn_range). However we would like
  1450. * consistency in testing and feature parity among all, so we should
  1451. * try to keep these invariants in place for everybody.
  1452. */
  1453. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  1454. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  1455. (VM_PFNMAP|VM_MIXEDMAP));
  1456. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  1457. BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
  1458. if (addr < vma->vm_start || addr >= vma->vm_end)
  1459. return -EFAULT;
  1460. if (track_pfn_insert(vma, &pgprot, __pfn_to_pfn_t(pfn, PFN_DEV)))
  1461. return -EINVAL;
  1462. ret = insert_pfn(vma, addr, __pfn_to_pfn_t(pfn, PFN_DEV), pgprot);
  1463. return ret;
  1464. }
  1465. EXPORT_SYMBOL(vm_insert_pfn_prot);
  1466. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  1467. pfn_t pfn)
  1468. {
  1469. pgprot_t pgprot = vma->vm_page_prot;
  1470. BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
  1471. if (addr < vma->vm_start || addr >= vma->vm_end)
  1472. return -EFAULT;
  1473. if (track_pfn_insert(vma, &pgprot, pfn))
  1474. return -EINVAL;
  1475. /*
  1476. * If we don't have pte special, then we have to use the pfn_valid()
  1477. * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
  1478. * refcount the page if pfn_valid is true (hence insert_page rather
  1479. * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
  1480. * without pte special, it would there be refcounted as a normal page.
  1481. */
  1482. if (!HAVE_PTE_SPECIAL && !pfn_t_devmap(pfn) && pfn_t_valid(pfn)) {
  1483. struct page *page;
  1484. /*
  1485. * At this point we are committed to insert_page()
  1486. * regardless of whether the caller specified flags that
  1487. * result in pfn_t_has_page() == false.
  1488. */
  1489. page = pfn_to_page(pfn_t_to_pfn(pfn));
  1490. return insert_page(vma, addr, page, pgprot);
  1491. }
  1492. return insert_pfn(vma, addr, pfn, pgprot);
  1493. }
  1494. EXPORT_SYMBOL(vm_insert_mixed);
  1495. /*
  1496. * maps a range of physical memory into the requested pages. the old
  1497. * mappings are removed. any references to nonexistent pages results
  1498. * in null mappings (currently treated as "copy-on-access")
  1499. */
  1500. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1501. unsigned long addr, unsigned long end,
  1502. unsigned long pfn, pgprot_t prot)
  1503. {
  1504. pte_t *pte;
  1505. spinlock_t *ptl;
  1506. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1507. if (!pte)
  1508. return -ENOMEM;
  1509. arch_enter_lazy_mmu_mode();
  1510. do {
  1511. BUG_ON(!pte_none(*pte));
  1512. set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
  1513. pfn++;
  1514. } while (pte++, addr += PAGE_SIZE, addr != end);
  1515. arch_leave_lazy_mmu_mode();
  1516. pte_unmap_unlock(pte - 1, ptl);
  1517. return 0;
  1518. }
  1519. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1520. unsigned long addr, unsigned long end,
  1521. unsigned long pfn, pgprot_t prot)
  1522. {
  1523. pmd_t *pmd;
  1524. unsigned long next;
  1525. pfn -= addr >> PAGE_SHIFT;
  1526. pmd = pmd_alloc(mm, pud, addr);
  1527. if (!pmd)
  1528. return -ENOMEM;
  1529. VM_BUG_ON(pmd_trans_huge(*pmd));
  1530. do {
  1531. next = pmd_addr_end(addr, end);
  1532. if (remap_pte_range(mm, pmd, addr, next,
  1533. pfn + (addr >> PAGE_SHIFT), prot))
  1534. return -ENOMEM;
  1535. } while (pmd++, addr = next, addr != end);
  1536. return 0;
  1537. }
  1538. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1539. unsigned long addr, unsigned long end,
  1540. unsigned long pfn, pgprot_t prot)
  1541. {
  1542. pud_t *pud;
  1543. unsigned long next;
  1544. pfn -= addr >> PAGE_SHIFT;
  1545. pud = pud_alloc(mm, pgd, addr);
  1546. if (!pud)
  1547. return -ENOMEM;
  1548. do {
  1549. next = pud_addr_end(addr, end);
  1550. if (remap_pmd_range(mm, pud, addr, next,
  1551. pfn + (addr >> PAGE_SHIFT), prot))
  1552. return -ENOMEM;
  1553. } while (pud++, addr = next, addr != end);
  1554. return 0;
  1555. }
  1556. /**
  1557. * remap_pfn_range - remap kernel memory to userspace
  1558. * @vma: user vma to map to
  1559. * @addr: target user address to start at
  1560. * @pfn: physical address of kernel memory
  1561. * @size: size of map area
  1562. * @prot: page protection flags for this mapping
  1563. *
  1564. * Note: this is only safe if the mm semaphore is held when called.
  1565. */
  1566. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1567. unsigned long pfn, unsigned long size, pgprot_t prot)
  1568. {
  1569. pgd_t *pgd;
  1570. unsigned long next;
  1571. unsigned long end = addr + PAGE_ALIGN(size);
  1572. struct mm_struct *mm = vma->vm_mm;
  1573. unsigned long remap_pfn = pfn;
  1574. int err;
  1575. /*
  1576. * Physically remapped pages are special. Tell the
  1577. * rest of the world about it:
  1578. * VM_IO tells people not to look at these pages
  1579. * (accesses can have side effects).
  1580. * VM_PFNMAP tells the core MM that the base pages are just
  1581. * raw PFN mappings, and do not have a "struct page" associated
  1582. * with them.
  1583. * VM_DONTEXPAND
  1584. * Disable vma merging and expanding with mremap().
  1585. * VM_DONTDUMP
  1586. * Omit vma from core dump, even when VM_IO turned off.
  1587. *
  1588. * There's a horrible special case to handle copy-on-write
  1589. * behaviour that some programs depend on. We mark the "original"
  1590. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1591. * See vm_normal_page() for details.
  1592. */
  1593. if (is_cow_mapping(vma->vm_flags)) {
  1594. if (addr != vma->vm_start || end != vma->vm_end)
  1595. return -EINVAL;
  1596. vma->vm_pgoff = pfn;
  1597. }
  1598. err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size));
  1599. if (err)
  1600. return -EINVAL;
  1601. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  1602. BUG_ON(addr >= end);
  1603. pfn -= addr >> PAGE_SHIFT;
  1604. pgd = pgd_offset(mm, addr);
  1605. flush_cache_range(vma, addr, end);
  1606. do {
  1607. next = pgd_addr_end(addr, end);
  1608. err = remap_pud_range(mm, pgd, addr, next,
  1609. pfn + (addr >> PAGE_SHIFT), prot);
  1610. if (err)
  1611. break;
  1612. } while (pgd++, addr = next, addr != end);
  1613. if (err)
  1614. untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size));
  1615. return err;
  1616. }
  1617. EXPORT_SYMBOL(remap_pfn_range);
  1618. /**
  1619. * vm_iomap_memory - remap memory to userspace
  1620. * @vma: user vma to map to
  1621. * @start: start of area
  1622. * @len: size of area
  1623. *
  1624. * This is a simplified io_remap_pfn_range() for common driver use. The
  1625. * driver just needs to give us the physical memory range to be mapped,
  1626. * we'll figure out the rest from the vma information.
  1627. *
  1628. * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
  1629. * whatever write-combining details or similar.
  1630. */
  1631. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
  1632. {
  1633. unsigned long vm_len, pfn, pages;
  1634. /* Check that the physical memory area passed in looks valid */
  1635. if (start + len < start)
  1636. return -EINVAL;
  1637. /*
  1638. * You *really* shouldn't map things that aren't page-aligned,
  1639. * but we've historically allowed it because IO memory might
  1640. * just have smaller alignment.
  1641. */
  1642. len += start & ~PAGE_MASK;
  1643. pfn = start >> PAGE_SHIFT;
  1644. pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
  1645. if (pfn + pages < pfn)
  1646. return -EINVAL;
  1647. /* We start the mapping 'vm_pgoff' pages into the area */
  1648. if (vma->vm_pgoff > pages)
  1649. return -EINVAL;
  1650. pfn += vma->vm_pgoff;
  1651. pages -= vma->vm_pgoff;
  1652. /* Can we fit all of the mapping? */
  1653. vm_len = vma->vm_end - vma->vm_start;
  1654. if (vm_len >> PAGE_SHIFT > pages)
  1655. return -EINVAL;
  1656. /* Ok, let it rip */
  1657. return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
  1658. }
  1659. EXPORT_SYMBOL(vm_iomap_memory);
  1660. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1661. unsigned long addr, unsigned long end,
  1662. pte_fn_t fn, void *data)
  1663. {
  1664. pte_t *pte;
  1665. int err;
  1666. pgtable_t token;
  1667. spinlock_t *uninitialized_var(ptl);
  1668. pte = (mm == &init_mm) ?
  1669. pte_alloc_kernel(pmd, addr) :
  1670. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1671. if (!pte)
  1672. return -ENOMEM;
  1673. BUG_ON(pmd_huge(*pmd));
  1674. arch_enter_lazy_mmu_mode();
  1675. token = pmd_pgtable(*pmd);
  1676. do {
  1677. err = fn(pte++, token, addr, data);
  1678. if (err)
  1679. break;
  1680. } while (addr += PAGE_SIZE, addr != end);
  1681. arch_leave_lazy_mmu_mode();
  1682. if (mm != &init_mm)
  1683. pte_unmap_unlock(pte-1, ptl);
  1684. return err;
  1685. }
  1686. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  1687. unsigned long addr, unsigned long end,
  1688. pte_fn_t fn, void *data)
  1689. {
  1690. pmd_t *pmd;
  1691. unsigned long next;
  1692. int err;
  1693. BUG_ON(pud_huge(*pud));
  1694. pmd = pmd_alloc(mm, pud, addr);
  1695. if (!pmd)
  1696. return -ENOMEM;
  1697. do {
  1698. next = pmd_addr_end(addr, end);
  1699. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  1700. if (err)
  1701. break;
  1702. } while (pmd++, addr = next, addr != end);
  1703. return err;
  1704. }
  1705. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1706. unsigned long addr, unsigned long end,
  1707. pte_fn_t fn, void *data)
  1708. {
  1709. pud_t *pud;
  1710. unsigned long next;
  1711. int err;
  1712. pud = pud_alloc(mm, pgd, addr);
  1713. if (!pud)
  1714. return -ENOMEM;
  1715. do {
  1716. next = pud_addr_end(addr, end);
  1717. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  1718. if (err)
  1719. break;
  1720. } while (pud++, addr = next, addr != end);
  1721. return err;
  1722. }
  1723. /*
  1724. * Scan a region of virtual memory, filling in page tables as necessary
  1725. * and calling a provided function on each leaf page table.
  1726. */
  1727. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  1728. unsigned long size, pte_fn_t fn, void *data)
  1729. {
  1730. pgd_t *pgd;
  1731. unsigned long next;
  1732. unsigned long end = addr + size;
  1733. int err;
  1734. if (WARN_ON(addr >= end))
  1735. return -EINVAL;
  1736. pgd = pgd_offset(mm, addr);
  1737. do {
  1738. next = pgd_addr_end(addr, end);
  1739. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  1740. if (err)
  1741. break;
  1742. } while (pgd++, addr = next, addr != end);
  1743. return err;
  1744. }
  1745. EXPORT_SYMBOL_GPL(apply_to_page_range);
  1746. /*
  1747. * handle_pte_fault chooses page fault handler according to an entry which was
  1748. * read non-atomically. Before making any commitment, on those architectures
  1749. * or configurations (e.g. i386 with PAE) which might give a mix of unmatched
  1750. * parts, do_swap_page must check under lock before unmapping the pte and
  1751. * proceeding (but do_wp_page is only called after already making such a check;
  1752. * and do_anonymous_page can safely check later on).
  1753. */
  1754. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1755. pte_t *page_table, pte_t orig_pte)
  1756. {
  1757. int same = 1;
  1758. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1759. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1760. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1761. spin_lock(ptl);
  1762. same = pte_same(*page_table, orig_pte);
  1763. spin_unlock(ptl);
  1764. }
  1765. #endif
  1766. pte_unmap(page_table);
  1767. return same;
  1768. }
  1769. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  1770. {
  1771. debug_dma_assert_idle(src);
  1772. /*
  1773. * If the source page was a PFN mapping, we don't have
  1774. * a "struct page" for it. We do a best-effort copy by
  1775. * just copying from the original user address. If that
  1776. * fails, we just zero-fill it. Live with it.
  1777. */
  1778. if (unlikely(!src)) {
  1779. void *kaddr = kmap_atomic(dst);
  1780. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1781. /*
  1782. * This really shouldn't fail, because the page is there
  1783. * in the page tables. But it might just be unreadable,
  1784. * in which case we just give up and fill the result with
  1785. * zeroes.
  1786. */
  1787. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1788. clear_page(kaddr);
  1789. kunmap_atomic(kaddr);
  1790. flush_dcache_page(dst);
  1791. } else
  1792. copy_user_highpage(dst, src, va, vma);
  1793. }
  1794. static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
  1795. {
  1796. struct file *vm_file = vma->vm_file;
  1797. if (vm_file)
  1798. return mapping_gfp_mask(vm_file->f_mapping) | __GFP_FS | __GFP_IO;
  1799. /*
  1800. * Special mappings (e.g. VDSO) do not have any file so fake
  1801. * a default GFP_KERNEL for them.
  1802. */
  1803. return GFP_KERNEL;
  1804. }
  1805. /*
  1806. * Notify the address space that the page is about to become writable so that
  1807. * it can prohibit this or wait for the page to get into an appropriate state.
  1808. *
  1809. * We do this without the lock held, so that it can sleep if it needs to.
  1810. */
  1811. static int do_page_mkwrite(struct vm_area_struct *vma, struct page *page,
  1812. unsigned long address)
  1813. {
  1814. struct vm_fault vmf;
  1815. int ret;
  1816. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  1817. vmf.pgoff = page->index;
  1818. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  1819. vmf.gfp_mask = __get_fault_gfp_mask(vma);
  1820. vmf.page = page;
  1821. vmf.cow_page = NULL;
  1822. ret = vma->vm_ops->page_mkwrite(vma, &vmf);
  1823. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
  1824. return ret;
  1825. if (unlikely(!(ret & VM_FAULT_LOCKED))) {
  1826. lock_page(page);
  1827. if (!page->mapping) {
  1828. unlock_page(page);
  1829. return 0; /* retry */
  1830. }
  1831. ret |= VM_FAULT_LOCKED;
  1832. } else
  1833. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1834. return ret;
  1835. }
  1836. /*
  1837. * Handle write page faults for pages that can be reused in the current vma
  1838. *
  1839. * This can happen either due to the mapping being with the VM_SHARED flag,
  1840. * or due to us being the last reference standing to the page. In either
  1841. * case, all we need to do here is to mark the page as writable and update
  1842. * any related book-keeping.
  1843. */
  1844. static inline int wp_page_reuse(struct fault_env *fe, pte_t orig_pte,
  1845. struct page *page, int page_mkwrite, int dirty_shared)
  1846. __releases(fe->ptl)
  1847. {
  1848. struct vm_area_struct *vma = fe->vma;
  1849. pte_t entry;
  1850. /*
  1851. * Clear the pages cpupid information as the existing
  1852. * information potentially belongs to a now completely
  1853. * unrelated process.
  1854. */
  1855. if (page)
  1856. page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1);
  1857. flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
  1858. entry = pte_mkyoung(orig_pte);
  1859. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1860. if (ptep_set_access_flags(vma, fe->address, fe->pte, entry, 1))
  1861. update_mmu_cache(vma, fe->address, fe->pte);
  1862. pte_unmap_unlock(fe->pte, fe->ptl);
  1863. if (dirty_shared) {
  1864. struct address_space *mapping;
  1865. int dirtied;
  1866. if (!page_mkwrite)
  1867. lock_page(page);
  1868. dirtied = set_page_dirty(page);
  1869. VM_BUG_ON_PAGE(PageAnon(page), page);
  1870. mapping = page->mapping;
  1871. unlock_page(page);
  1872. put_page(page);
  1873. if ((dirtied || page_mkwrite) && mapping) {
  1874. /*
  1875. * Some device drivers do not set page.mapping
  1876. * but still dirty their pages
  1877. */
  1878. balance_dirty_pages_ratelimited(mapping);
  1879. }
  1880. if (!page_mkwrite)
  1881. file_update_time(vma->vm_file);
  1882. }
  1883. return VM_FAULT_WRITE;
  1884. }
  1885. /*
  1886. * Handle the case of a page which we actually need to copy to a new page.
  1887. *
  1888. * Called with mmap_sem locked and the old page referenced, but
  1889. * without the ptl held.
  1890. *
  1891. * High level logic flow:
  1892. *
  1893. * - Allocate a page, copy the content of the old page to the new one.
  1894. * - Handle book keeping and accounting - cgroups, mmu-notifiers, etc.
  1895. * - Take the PTL. If the pte changed, bail out and release the allocated page
  1896. * - If the pte is still the way we remember it, update the page table and all
  1897. * relevant references. This includes dropping the reference the page-table
  1898. * held to the old page, as well as updating the rmap.
  1899. * - In any case, unlock the PTL and drop the reference we took to the old page.
  1900. */
  1901. static int wp_page_copy(struct fault_env *fe, pte_t orig_pte,
  1902. struct page *old_page)
  1903. {
  1904. struct vm_area_struct *vma = fe->vma;
  1905. struct mm_struct *mm = vma->vm_mm;
  1906. struct page *new_page = NULL;
  1907. pte_t entry;
  1908. int page_copied = 0;
  1909. const unsigned long mmun_start = fe->address & PAGE_MASK;
  1910. const unsigned long mmun_end = mmun_start + PAGE_SIZE;
  1911. struct mem_cgroup *memcg;
  1912. if (unlikely(anon_vma_prepare(vma)))
  1913. goto oom;
  1914. if (is_zero_pfn(pte_pfn(orig_pte))) {
  1915. new_page = alloc_zeroed_user_highpage_movable(vma, fe->address);
  1916. if (!new_page)
  1917. goto oom;
  1918. } else {
  1919. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma,
  1920. fe->address);
  1921. if (!new_page)
  1922. goto oom;
  1923. cow_user_page(new_page, old_page, fe->address, vma);
  1924. }
  1925. if (mem_cgroup_try_charge(new_page, mm, GFP_KERNEL, &memcg, false))
  1926. goto oom_free_new;
  1927. __SetPageUptodate(new_page);
  1928. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1929. /*
  1930. * Re-check the pte - we dropped the lock
  1931. */
  1932. fe->pte = pte_offset_map_lock(mm, fe->pmd, fe->address, &fe->ptl);
  1933. if (likely(pte_same(*fe->pte, orig_pte))) {
  1934. if (old_page) {
  1935. if (!PageAnon(old_page)) {
  1936. dec_mm_counter_fast(mm,
  1937. mm_counter_file(old_page));
  1938. inc_mm_counter_fast(mm, MM_ANONPAGES);
  1939. }
  1940. } else {
  1941. inc_mm_counter_fast(mm, MM_ANONPAGES);
  1942. }
  1943. flush_cache_page(vma, fe->address, pte_pfn(orig_pte));
  1944. entry = mk_pte(new_page, vma->vm_page_prot);
  1945. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1946. /*
  1947. * Clear the pte entry and flush it first, before updating the
  1948. * pte with the new entry. This will avoid a race condition
  1949. * seen in the presence of one thread doing SMC and another
  1950. * thread doing COW.
  1951. */
  1952. ptep_clear_flush_notify(vma, fe->address, fe->pte);
  1953. page_add_new_anon_rmap(new_page, vma, fe->address, false);
  1954. mem_cgroup_commit_charge(new_page, memcg, false, false);
  1955. lru_cache_add_active_or_unevictable(new_page, vma);
  1956. /*
  1957. * We call the notify macro here because, when using secondary
  1958. * mmu page tables (such as kvm shadow page tables), we want the
  1959. * new page to be mapped directly into the secondary page table.
  1960. */
  1961. set_pte_at_notify(mm, fe->address, fe->pte, entry);
  1962. update_mmu_cache(vma, fe->address, fe->pte);
  1963. if (old_page) {
  1964. /*
  1965. * Only after switching the pte to the new page may
  1966. * we remove the mapcount here. Otherwise another
  1967. * process may come and find the rmap count decremented
  1968. * before the pte is switched to the new page, and
  1969. * "reuse" the old page writing into it while our pte
  1970. * here still points into it and can be read by other
  1971. * threads.
  1972. *
  1973. * The critical issue is to order this
  1974. * page_remove_rmap with the ptp_clear_flush above.
  1975. * Those stores are ordered by (if nothing else,)
  1976. * the barrier present in the atomic_add_negative
  1977. * in page_remove_rmap.
  1978. *
  1979. * Then the TLB flush in ptep_clear_flush ensures that
  1980. * no process can access the old page before the
  1981. * decremented mapcount is visible. And the old page
  1982. * cannot be reused until after the decremented
  1983. * mapcount is visible. So transitively, TLBs to
  1984. * old page will be flushed before it can be reused.
  1985. */
  1986. page_remove_rmap(old_page, false);
  1987. }
  1988. /* Free the old page.. */
  1989. new_page = old_page;
  1990. page_copied = 1;
  1991. } else {
  1992. mem_cgroup_cancel_charge(new_page, memcg, false);
  1993. }
  1994. if (new_page)
  1995. put_page(new_page);
  1996. pte_unmap_unlock(fe->pte, fe->ptl);
  1997. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1998. if (old_page) {
  1999. /*
  2000. * Don't let another task, with possibly unlocked vma,
  2001. * keep the mlocked page.
  2002. */
  2003. if (page_copied && (vma->vm_flags & VM_LOCKED)) {
  2004. lock_page(old_page); /* LRU manipulation */
  2005. if (PageMlocked(old_page))
  2006. munlock_vma_page(old_page);
  2007. unlock_page(old_page);
  2008. }
  2009. put_page(old_page);
  2010. }
  2011. return page_copied ? VM_FAULT_WRITE : 0;
  2012. oom_free_new:
  2013. put_page(new_page);
  2014. oom:
  2015. if (old_page)
  2016. put_page(old_page);
  2017. return VM_FAULT_OOM;
  2018. }
  2019. /*
  2020. * Handle write page faults for VM_MIXEDMAP or VM_PFNMAP for a VM_SHARED
  2021. * mapping
  2022. */
  2023. static int wp_pfn_shared(struct fault_env *fe, pte_t orig_pte)
  2024. {
  2025. struct vm_area_struct *vma = fe->vma;
  2026. if (vma->vm_ops && vma->vm_ops->pfn_mkwrite) {
  2027. struct vm_fault vmf = {
  2028. .page = NULL,
  2029. .pgoff = linear_page_index(vma, fe->address),
  2030. .virtual_address =
  2031. (void __user *)(fe->address & PAGE_MASK),
  2032. .flags = FAULT_FLAG_WRITE | FAULT_FLAG_MKWRITE,
  2033. };
  2034. int ret;
  2035. pte_unmap_unlock(fe->pte, fe->ptl);
  2036. ret = vma->vm_ops->pfn_mkwrite(vma, &vmf);
  2037. if (ret & VM_FAULT_ERROR)
  2038. return ret;
  2039. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2040. &fe->ptl);
  2041. /*
  2042. * We might have raced with another page fault while we
  2043. * released the pte_offset_map_lock.
  2044. */
  2045. if (!pte_same(*fe->pte, orig_pte)) {
  2046. pte_unmap_unlock(fe->pte, fe->ptl);
  2047. return 0;
  2048. }
  2049. }
  2050. return wp_page_reuse(fe, orig_pte, NULL, 0, 0);
  2051. }
  2052. static int wp_page_shared(struct fault_env *fe, pte_t orig_pte,
  2053. struct page *old_page)
  2054. __releases(fe->ptl)
  2055. {
  2056. struct vm_area_struct *vma = fe->vma;
  2057. int page_mkwrite = 0;
  2058. get_page(old_page);
  2059. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  2060. int tmp;
  2061. pte_unmap_unlock(fe->pte, fe->ptl);
  2062. tmp = do_page_mkwrite(vma, old_page, fe->address);
  2063. if (unlikely(!tmp || (tmp &
  2064. (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
  2065. put_page(old_page);
  2066. return tmp;
  2067. }
  2068. /*
  2069. * Since we dropped the lock we need to revalidate
  2070. * the PTE as someone else may have changed it. If
  2071. * they did, we just return, as we can count on the
  2072. * MMU to tell us if they didn't also make it writable.
  2073. */
  2074. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2075. &fe->ptl);
  2076. if (!pte_same(*fe->pte, orig_pte)) {
  2077. unlock_page(old_page);
  2078. pte_unmap_unlock(fe->pte, fe->ptl);
  2079. put_page(old_page);
  2080. return 0;
  2081. }
  2082. page_mkwrite = 1;
  2083. }
  2084. return wp_page_reuse(fe, orig_pte, old_page, page_mkwrite, 1);
  2085. }
  2086. /*
  2087. * This routine handles present pages, when users try to write
  2088. * to a shared page. It is done by copying the page to a new address
  2089. * and decrementing the shared-page counter for the old page.
  2090. *
  2091. * Note that this routine assumes that the protection checks have been
  2092. * done by the caller (the low-level page fault routine in most cases).
  2093. * Thus we can safely just mark it writable once we've done any necessary
  2094. * COW.
  2095. *
  2096. * We also mark the page dirty at this point even though the page will
  2097. * change only once the write actually happens. This avoids a few races,
  2098. * and potentially makes it more efficient.
  2099. *
  2100. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2101. * but allow concurrent faults), with pte both mapped and locked.
  2102. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2103. */
  2104. static int do_wp_page(struct fault_env *fe, pte_t orig_pte)
  2105. __releases(fe->ptl)
  2106. {
  2107. struct vm_area_struct *vma = fe->vma;
  2108. struct page *old_page;
  2109. old_page = vm_normal_page(vma, fe->address, orig_pte);
  2110. if (!old_page) {
  2111. /*
  2112. * VM_MIXEDMAP !pfn_valid() case, or VM_SOFTDIRTY clear on a
  2113. * VM_PFNMAP VMA.
  2114. *
  2115. * We should not cow pages in a shared writeable mapping.
  2116. * Just mark the pages writable and/or call ops->pfn_mkwrite.
  2117. */
  2118. if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2119. (VM_WRITE|VM_SHARED))
  2120. return wp_pfn_shared(fe, orig_pte);
  2121. pte_unmap_unlock(fe->pte, fe->ptl);
  2122. return wp_page_copy(fe, orig_pte, old_page);
  2123. }
  2124. /*
  2125. * Take out anonymous pages first, anonymous shared vmas are
  2126. * not dirty accountable.
  2127. */
  2128. if (PageAnon(old_page) && !PageKsm(old_page)) {
  2129. int total_mapcount;
  2130. if (!trylock_page(old_page)) {
  2131. get_page(old_page);
  2132. pte_unmap_unlock(fe->pte, fe->ptl);
  2133. lock_page(old_page);
  2134. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
  2135. fe->address, &fe->ptl);
  2136. if (!pte_same(*fe->pte, orig_pte)) {
  2137. unlock_page(old_page);
  2138. pte_unmap_unlock(fe->pte, fe->ptl);
  2139. put_page(old_page);
  2140. return 0;
  2141. }
  2142. put_page(old_page);
  2143. }
  2144. if (reuse_swap_page(old_page, &total_mapcount)) {
  2145. if (total_mapcount == 1) {
  2146. /*
  2147. * The page is all ours. Move it to
  2148. * our anon_vma so the rmap code will
  2149. * not search our parent or siblings.
  2150. * Protected against the rmap code by
  2151. * the page lock.
  2152. */
  2153. page_move_anon_rmap(old_page, vma);
  2154. }
  2155. unlock_page(old_page);
  2156. return wp_page_reuse(fe, orig_pte, old_page, 0, 0);
  2157. }
  2158. unlock_page(old_page);
  2159. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2160. (VM_WRITE|VM_SHARED))) {
  2161. return wp_page_shared(fe, orig_pte, old_page);
  2162. }
  2163. /*
  2164. * Ok, we need to copy. Oh, well..
  2165. */
  2166. get_page(old_page);
  2167. pte_unmap_unlock(fe->pte, fe->ptl);
  2168. return wp_page_copy(fe, orig_pte, old_page);
  2169. }
  2170. static void unmap_mapping_range_vma(struct vm_area_struct *vma,
  2171. unsigned long start_addr, unsigned long end_addr,
  2172. struct zap_details *details)
  2173. {
  2174. zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
  2175. }
  2176. static inline void unmap_mapping_range_tree(struct rb_root *root,
  2177. struct zap_details *details)
  2178. {
  2179. struct vm_area_struct *vma;
  2180. pgoff_t vba, vea, zba, zea;
  2181. vma_interval_tree_foreach(vma, root,
  2182. details->first_index, details->last_index) {
  2183. vba = vma->vm_pgoff;
  2184. vea = vba + vma_pages(vma) - 1;
  2185. zba = details->first_index;
  2186. if (zba < vba)
  2187. zba = vba;
  2188. zea = details->last_index;
  2189. if (zea > vea)
  2190. zea = vea;
  2191. unmap_mapping_range_vma(vma,
  2192. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  2193. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  2194. details);
  2195. }
  2196. }
  2197. /**
  2198. * unmap_mapping_range - unmap the portion of all mmaps in the specified
  2199. * address_space corresponding to the specified page range in the underlying
  2200. * file.
  2201. *
  2202. * @mapping: the address space containing mmaps to be unmapped.
  2203. * @holebegin: byte in first page to unmap, relative to the start of
  2204. * the underlying file. This will be rounded down to a PAGE_SIZE
  2205. * boundary. Note that this is different from truncate_pagecache(), which
  2206. * must keep the partial page. In contrast, we must get rid of
  2207. * partial pages.
  2208. * @holelen: size of prospective hole in bytes. This will be rounded
  2209. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  2210. * end of the file.
  2211. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  2212. * but 0 when invalidating pagecache, don't throw away private data.
  2213. */
  2214. void unmap_mapping_range(struct address_space *mapping,
  2215. loff_t const holebegin, loff_t const holelen, int even_cows)
  2216. {
  2217. struct zap_details details = { };
  2218. pgoff_t hba = holebegin >> PAGE_SHIFT;
  2219. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2220. /* Check for overflow. */
  2221. if (sizeof(holelen) > sizeof(hlen)) {
  2222. long long holeend =
  2223. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2224. if (holeend & ~(long long)ULONG_MAX)
  2225. hlen = ULONG_MAX - hba + 1;
  2226. }
  2227. details.check_mapping = even_cows? NULL: mapping;
  2228. details.first_index = hba;
  2229. details.last_index = hba + hlen - 1;
  2230. if (details.last_index < details.first_index)
  2231. details.last_index = ULONG_MAX;
  2232. i_mmap_lock_write(mapping);
  2233. if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
  2234. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  2235. i_mmap_unlock_write(mapping);
  2236. }
  2237. EXPORT_SYMBOL(unmap_mapping_range);
  2238. /*
  2239. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2240. * but allow concurrent faults), and pte mapped but not yet locked.
  2241. * We return with pte unmapped and unlocked.
  2242. *
  2243. * We return with the mmap_sem locked or unlocked in the same cases
  2244. * as does filemap_fault().
  2245. */
  2246. int do_swap_page(struct fault_env *fe, pte_t orig_pte)
  2247. {
  2248. struct vm_area_struct *vma = fe->vma;
  2249. struct page *page, *swapcache;
  2250. struct mem_cgroup *memcg;
  2251. swp_entry_t entry;
  2252. pte_t pte;
  2253. int locked;
  2254. int exclusive = 0;
  2255. int ret = 0;
  2256. if (!pte_unmap_same(vma->vm_mm, fe->pmd, fe->pte, orig_pte))
  2257. goto out;
  2258. entry = pte_to_swp_entry(orig_pte);
  2259. if (unlikely(non_swap_entry(entry))) {
  2260. if (is_migration_entry(entry)) {
  2261. migration_entry_wait(vma->vm_mm, fe->pmd, fe->address);
  2262. } else if (is_hwpoison_entry(entry)) {
  2263. ret = VM_FAULT_HWPOISON;
  2264. } else {
  2265. print_bad_pte(vma, fe->address, orig_pte, NULL);
  2266. ret = VM_FAULT_SIGBUS;
  2267. }
  2268. goto out;
  2269. }
  2270. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  2271. page = lookup_swap_cache(entry);
  2272. if (!page) {
  2273. page = swapin_readahead(entry,
  2274. GFP_HIGHUSER_MOVABLE, vma, fe->address);
  2275. if (!page) {
  2276. /*
  2277. * Back out if somebody else faulted in this pte
  2278. * while we released the pte lock.
  2279. */
  2280. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd,
  2281. fe->address, &fe->ptl);
  2282. if (likely(pte_same(*fe->pte, orig_pte)))
  2283. ret = VM_FAULT_OOM;
  2284. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2285. goto unlock;
  2286. }
  2287. /* Had to read the page from swap area: Major fault */
  2288. ret = VM_FAULT_MAJOR;
  2289. count_vm_event(PGMAJFAULT);
  2290. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  2291. } else if (PageHWPoison(page)) {
  2292. /*
  2293. * hwpoisoned dirty swapcache pages are kept for killing
  2294. * owner processes (which may be unknown at hwpoison time)
  2295. */
  2296. ret = VM_FAULT_HWPOISON;
  2297. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2298. swapcache = page;
  2299. goto out_release;
  2300. }
  2301. swapcache = page;
  2302. locked = lock_page_or_retry(page, vma->vm_mm, fe->flags);
  2303. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2304. if (!locked) {
  2305. ret |= VM_FAULT_RETRY;
  2306. goto out_release;
  2307. }
  2308. /*
  2309. * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
  2310. * release the swapcache from under us. The page pin, and pte_same
  2311. * test below, are not enough to exclude that. Even if it is still
  2312. * swapcache, we need to check that the page's swap has not changed.
  2313. */
  2314. if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
  2315. goto out_page;
  2316. page = ksm_might_need_to_copy(page, vma, fe->address);
  2317. if (unlikely(!page)) {
  2318. ret = VM_FAULT_OOM;
  2319. page = swapcache;
  2320. goto out_page;
  2321. }
  2322. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
  2323. &memcg, false)) {
  2324. ret = VM_FAULT_OOM;
  2325. goto out_page;
  2326. }
  2327. /*
  2328. * Back out if somebody else already faulted in this pte.
  2329. */
  2330. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2331. &fe->ptl);
  2332. if (unlikely(!pte_same(*fe->pte, orig_pte)))
  2333. goto out_nomap;
  2334. if (unlikely(!PageUptodate(page))) {
  2335. ret = VM_FAULT_SIGBUS;
  2336. goto out_nomap;
  2337. }
  2338. /*
  2339. * The page isn't present yet, go ahead with the fault.
  2340. *
  2341. * Be careful about the sequence of operations here.
  2342. * To get its accounting right, reuse_swap_page() must be called
  2343. * while the page is counted on swap but not yet in mapcount i.e.
  2344. * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
  2345. * must be called after the swap_free(), or it will never succeed.
  2346. */
  2347. inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
  2348. dec_mm_counter_fast(vma->vm_mm, MM_SWAPENTS);
  2349. pte = mk_pte(page, vma->vm_page_prot);
  2350. if ((fe->flags & FAULT_FLAG_WRITE) && reuse_swap_page(page, NULL)) {
  2351. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  2352. fe->flags &= ~FAULT_FLAG_WRITE;
  2353. ret |= VM_FAULT_WRITE;
  2354. exclusive = RMAP_EXCLUSIVE;
  2355. }
  2356. flush_icache_page(vma, page);
  2357. if (pte_swp_soft_dirty(orig_pte))
  2358. pte = pte_mksoft_dirty(pte);
  2359. set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
  2360. if (page == swapcache) {
  2361. do_page_add_anon_rmap(page, vma, fe->address, exclusive);
  2362. mem_cgroup_commit_charge(page, memcg, true, false);
  2363. activate_page(page);
  2364. } else { /* ksm created a completely new copy */
  2365. page_add_new_anon_rmap(page, vma, fe->address, false);
  2366. mem_cgroup_commit_charge(page, memcg, false, false);
  2367. lru_cache_add_active_or_unevictable(page, vma);
  2368. }
  2369. swap_free(entry);
  2370. if (mem_cgroup_swap_full(page) ||
  2371. (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
  2372. try_to_free_swap(page);
  2373. unlock_page(page);
  2374. if (page != swapcache) {
  2375. /*
  2376. * Hold the lock to avoid the swap entry to be reused
  2377. * until we take the PT lock for the pte_same() check
  2378. * (to avoid false positives from pte_same). For
  2379. * further safety release the lock after the swap_free
  2380. * so that the swap count won't change under a
  2381. * parallel locked swapcache.
  2382. */
  2383. unlock_page(swapcache);
  2384. put_page(swapcache);
  2385. }
  2386. if (fe->flags & FAULT_FLAG_WRITE) {
  2387. ret |= do_wp_page(fe, pte);
  2388. if (ret & VM_FAULT_ERROR)
  2389. ret &= VM_FAULT_ERROR;
  2390. goto out;
  2391. }
  2392. /* No need to invalidate - it was non-present before */
  2393. update_mmu_cache(vma, fe->address, fe->pte);
  2394. unlock:
  2395. pte_unmap_unlock(fe->pte, fe->ptl);
  2396. out:
  2397. return ret;
  2398. out_nomap:
  2399. mem_cgroup_cancel_charge(page, memcg, false);
  2400. pte_unmap_unlock(fe->pte, fe->ptl);
  2401. out_page:
  2402. unlock_page(page);
  2403. out_release:
  2404. put_page(page);
  2405. if (page != swapcache) {
  2406. unlock_page(swapcache);
  2407. put_page(swapcache);
  2408. }
  2409. return ret;
  2410. }
  2411. /*
  2412. * This is like a special single-page "expand_{down|up}wards()",
  2413. * except we must first make sure that 'address{-|+}PAGE_SIZE'
  2414. * doesn't hit another vma.
  2415. */
  2416. static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
  2417. {
  2418. address &= PAGE_MASK;
  2419. if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
  2420. struct vm_area_struct *prev = vma->vm_prev;
  2421. /*
  2422. * Is there a mapping abutting this one below?
  2423. *
  2424. * That's only ok if it's the same stack mapping
  2425. * that has gotten split..
  2426. */
  2427. if (prev && prev->vm_end == address)
  2428. return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
  2429. return expand_downwards(vma, address - PAGE_SIZE);
  2430. }
  2431. if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
  2432. struct vm_area_struct *next = vma->vm_next;
  2433. /* As VM_GROWSDOWN but s/below/above/ */
  2434. if (next && next->vm_start == address + PAGE_SIZE)
  2435. return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
  2436. return expand_upwards(vma, address + PAGE_SIZE);
  2437. }
  2438. return 0;
  2439. }
  2440. /*
  2441. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2442. * but allow concurrent faults), and pte mapped but not yet locked.
  2443. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2444. */
  2445. static int do_anonymous_page(struct fault_env *fe)
  2446. {
  2447. struct vm_area_struct *vma = fe->vma;
  2448. struct mem_cgroup *memcg;
  2449. struct page *page;
  2450. pte_t entry;
  2451. /* File mapping without ->vm_ops ? */
  2452. if (vma->vm_flags & VM_SHARED)
  2453. return VM_FAULT_SIGBUS;
  2454. /* Check if we need to add a guard page to the stack */
  2455. if (check_stack_guard_page(vma, fe->address) < 0)
  2456. return VM_FAULT_SIGSEGV;
  2457. /*
  2458. * Use pte_alloc() instead of pte_alloc_map(). We can't run
  2459. * pte_offset_map() on pmds where a huge pmd might be created
  2460. * from a different thread.
  2461. *
  2462. * pte_alloc_map() is safe to use under down_write(mmap_sem) or when
  2463. * parallel threads are excluded by other means.
  2464. *
  2465. * Here we only have down_read(mmap_sem).
  2466. */
  2467. if (pte_alloc(vma->vm_mm, fe->pmd, fe->address))
  2468. return VM_FAULT_OOM;
  2469. /* See the comment in pte_alloc_one_map() */
  2470. if (unlikely(pmd_trans_unstable(fe->pmd)))
  2471. return 0;
  2472. /* Use the zero-page for reads */
  2473. if (!(fe->flags & FAULT_FLAG_WRITE) &&
  2474. !mm_forbids_zeropage(vma->vm_mm)) {
  2475. entry = pte_mkspecial(pfn_pte(my_zero_pfn(fe->address),
  2476. vma->vm_page_prot));
  2477. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2478. &fe->ptl);
  2479. if (!pte_none(*fe->pte))
  2480. goto unlock;
  2481. /* Deliver the page fault to userland, check inside PT lock */
  2482. if (userfaultfd_missing(vma)) {
  2483. pte_unmap_unlock(fe->pte, fe->ptl);
  2484. return handle_userfault(fe, VM_UFFD_MISSING);
  2485. }
  2486. goto setpte;
  2487. }
  2488. /* Allocate our own private page. */
  2489. if (unlikely(anon_vma_prepare(vma)))
  2490. goto oom;
  2491. page = alloc_zeroed_user_highpage_movable(vma, fe->address);
  2492. if (!page)
  2493. goto oom;
  2494. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg, false))
  2495. goto oom_free_page;
  2496. /*
  2497. * The memory barrier inside __SetPageUptodate makes sure that
  2498. * preceeding stores to the page contents become visible before
  2499. * the set_pte_at() write.
  2500. */
  2501. __SetPageUptodate(page);
  2502. entry = mk_pte(page, vma->vm_page_prot);
  2503. if (vma->vm_flags & VM_WRITE)
  2504. entry = pte_mkwrite(pte_mkdirty(entry));
  2505. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2506. &fe->ptl);
  2507. if (!pte_none(*fe->pte))
  2508. goto release;
  2509. /* Deliver the page fault to userland, check inside PT lock */
  2510. if (userfaultfd_missing(vma)) {
  2511. pte_unmap_unlock(fe->pte, fe->ptl);
  2512. mem_cgroup_cancel_charge(page, memcg, false);
  2513. put_page(page);
  2514. return handle_userfault(fe, VM_UFFD_MISSING);
  2515. }
  2516. inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
  2517. page_add_new_anon_rmap(page, vma, fe->address, false);
  2518. mem_cgroup_commit_charge(page, memcg, false, false);
  2519. lru_cache_add_active_or_unevictable(page, vma);
  2520. setpte:
  2521. set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
  2522. /* No need to invalidate - it was non-present before */
  2523. update_mmu_cache(vma, fe->address, fe->pte);
  2524. unlock:
  2525. pte_unmap_unlock(fe->pte, fe->ptl);
  2526. return 0;
  2527. release:
  2528. mem_cgroup_cancel_charge(page, memcg, false);
  2529. put_page(page);
  2530. goto unlock;
  2531. oom_free_page:
  2532. put_page(page);
  2533. oom:
  2534. return VM_FAULT_OOM;
  2535. }
  2536. /*
  2537. * The mmap_sem must have been held on entry, and may have been
  2538. * released depending on flags and vma->vm_ops->fault() return value.
  2539. * See filemap_fault() and __lock_page_retry().
  2540. */
  2541. static int __do_fault(struct fault_env *fe, pgoff_t pgoff,
  2542. struct page *cow_page, struct page **page, void **entry)
  2543. {
  2544. struct vm_area_struct *vma = fe->vma;
  2545. struct vm_fault vmf;
  2546. int ret;
  2547. vmf.virtual_address = (void __user *)(fe->address & PAGE_MASK);
  2548. vmf.pgoff = pgoff;
  2549. vmf.flags = fe->flags;
  2550. vmf.page = NULL;
  2551. vmf.gfp_mask = __get_fault_gfp_mask(vma);
  2552. vmf.cow_page = cow_page;
  2553. ret = vma->vm_ops->fault(vma, &vmf);
  2554. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2555. return ret;
  2556. if (ret & VM_FAULT_DAX_LOCKED) {
  2557. *entry = vmf.entry;
  2558. return ret;
  2559. }
  2560. if (unlikely(PageHWPoison(vmf.page))) {
  2561. if (ret & VM_FAULT_LOCKED)
  2562. unlock_page(vmf.page);
  2563. put_page(vmf.page);
  2564. return VM_FAULT_HWPOISON;
  2565. }
  2566. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  2567. lock_page(vmf.page);
  2568. else
  2569. VM_BUG_ON_PAGE(!PageLocked(vmf.page), vmf.page);
  2570. *page = vmf.page;
  2571. return ret;
  2572. }
  2573. static int pte_alloc_one_map(struct fault_env *fe)
  2574. {
  2575. struct vm_area_struct *vma = fe->vma;
  2576. if (!pmd_none(*fe->pmd))
  2577. goto map_pte;
  2578. if (fe->prealloc_pte) {
  2579. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  2580. if (unlikely(!pmd_none(*fe->pmd))) {
  2581. spin_unlock(fe->ptl);
  2582. goto map_pte;
  2583. }
  2584. atomic_long_inc(&vma->vm_mm->nr_ptes);
  2585. pmd_populate(vma->vm_mm, fe->pmd, fe->prealloc_pte);
  2586. spin_unlock(fe->ptl);
  2587. fe->prealloc_pte = 0;
  2588. } else if (unlikely(pte_alloc(vma->vm_mm, fe->pmd, fe->address))) {
  2589. return VM_FAULT_OOM;
  2590. }
  2591. map_pte:
  2592. /*
  2593. * If a huge pmd materialized under us just retry later. Use
  2594. * pmd_trans_unstable() instead of pmd_trans_huge() to ensure the pmd
  2595. * didn't become pmd_trans_huge under us and then back to pmd_none, as
  2596. * a result of MADV_DONTNEED running immediately after a huge pmd fault
  2597. * in a different thread of this mm, in turn leading to a misleading
  2598. * pmd_trans_huge() retval. All we have to ensure is that it is a
  2599. * regular pmd that we can walk with pte_offset_map() and we can do that
  2600. * through an atomic read in C, which is what pmd_trans_unstable()
  2601. * provides.
  2602. */
  2603. if (pmd_trans_unstable(fe->pmd) || pmd_devmap(*fe->pmd))
  2604. return VM_FAULT_NOPAGE;
  2605. fe->pte = pte_offset_map_lock(vma->vm_mm, fe->pmd, fe->address,
  2606. &fe->ptl);
  2607. return 0;
  2608. }
  2609. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  2610. #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
  2611. static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
  2612. unsigned long haddr)
  2613. {
  2614. if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
  2615. (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
  2616. return false;
  2617. if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
  2618. return false;
  2619. return true;
  2620. }
  2621. static int do_set_pmd(struct fault_env *fe, struct page *page)
  2622. {
  2623. struct vm_area_struct *vma = fe->vma;
  2624. bool write = fe->flags & FAULT_FLAG_WRITE;
  2625. unsigned long haddr = fe->address & HPAGE_PMD_MASK;
  2626. pmd_t entry;
  2627. int i, ret;
  2628. if (!transhuge_vma_suitable(vma, haddr))
  2629. return VM_FAULT_FALLBACK;
  2630. ret = VM_FAULT_FALLBACK;
  2631. page = compound_head(page);
  2632. fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
  2633. if (unlikely(!pmd_none(*fe->pmd)))
  2634. goto out;
  2635. for (i = 0; i < HPAGE_PMD_NR; i++)
  2636. flush_icache_page(vma, page + i);
  2637. entry = mk_huge_pmd(page, vma->vm_page_prot);
  2638. if (write)
  2639. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  2640. add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR);
  2641. page_add_file_rmap(page, true);
  2642. set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
  2643. update_mmu_cache_pmd(vma, haddr, fe->pmd);
  2644. /* fault is handled */
  2645. ret = 0;
  2646. count_vm_event(THP_FILE_MAPPED);
  2647. out:
  2648. spin_unlock(fe->ptl);
  2649. return ret;
  2650. }
  2651. #else
  2652. static int do_set_pmd(struct fault_env *fe, struct page *page)
  2653. {
  2654. BUILD_BUG();
  2655. return 0;
  2656. }
  2657. #endif
  2658. /**
  2659. * alloc_set_pte - setup new PTE entry for given page and add reverse page
  2660. * mapping. If needed, the fucntion allocates page table or use pre-allocated.
  2661. *
  2662. * @fe: fault environment
  2663. * @memcg: memcg to charge page (only for private mappings)
  2664. * @page: page to map
  2665. *
  2666. * Caller must take care of unlocking fe->ptl, if fe->pte is non-NULL on return.
  2667. *
  2668. * Target users are page handler itself and implementations of
  2669. * vm_ops->map_pages.
  2670. */
  2671. int alloc_set_pte(struct fault_env *fe, struct mem_cgroup *memcg,
  2672. struct page *page)
  2673. {
  2674. struct vm_area_struct *vma = fe->vma;
  2675. bool write = fe->flags & FAULT_FLAG_WRITE;
  2676. pte_t entry;
  2677. int ret;
  2678. if (pmd_none(*fe->pmd) && PageTransCompound(page) &&
  2679. IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
  2680. /* THP on COW? */
  2681. VM_BUG_ON_PAGE(memcg, page);
  2682. ret = do_set_pmd(fe, page);
  2683. if (ret != VM_FAULT_FALLBACK)
  2684. return ret;
  2685. }
  2686. if (!fe->pte) {
  2687. ret = pte_alloc_one_map(fe);
  2688. if (ret)
  2689. return ret;
  2690. }
  2691. /* Re-check under ptl */
  2692. if (unlikely(!pte_none(*fe->pte)))
  2693. return VM_FAULT_NOPAGE;
  2694. flush_icache_page(vma, page);
  2695. entry = mk_pte(page, vma->vm_page_prot);
  2696. if (write)
  2697. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2698. /* copy-on-write page */
  2699. if (write && !(vma->vm_flags & VM_SHARED)) {
  2700. inc_mm_counter_fast(vma->vm_mm, MM_ANONPAGES);
  2701. page_add_new_anon_rmap(page, vma, fe->address, false);
  2702. mem_cgroup_commit_charge(page, memcg, false, false);
  2703. lru_cache_add_active_or_unevictable(page, vma);
  2704. } else {
  2705. inc_mm_counter_fast(vma->vm_mm, mm_counter_file(page));
  2706. page_add_file_rmap(page, false);
  2707. }
  2708. set_pte_at(vma->vm_mm, fe->address, fe->pte, entry);
  2709. /* no need to invalidate: a not-present page won't be cached */
  2710. update_mmu_cache(vma, fe->address, fe->pte);
  2711. return 0;
  2712. }
  2713. static unsigned long fault_around_bytes __read_mostly =
  2714. rounddown_pow_of_two(65536);
  2715. #ifdef CONFIG_DEBUG_FS
  2716. static int fault_around_bytes_get(void *data, u64 *val)
  2717. {
  2718. *val = fault_around_bytes;
  2719. return 0;
  2720. }
  2721. /*
  2722. * fault_around_pages() and fault_around_mask() expects fault_around_bytes
  2723. * rounded down to nearest page order. It's what do_fault_around() expects to
  2724. * see.
  2725. */
  2726. static int fault_around_bytes_set(void *data, u64 val)
  2727. {
  2728. if (val / PAGE_SIZE > PTRS_PER_PTE)
  2729. return -EINVAL;
  2730. if (val > PAGE_SIZE)
  2731. fault_around_bytes = rounddown_pow_of_two(val);
  2732. else
  2733. fault_around_bytes = PAGE_SIZE; /* rounddown_pow_of_two(0) is undefined */
  2734. return 0;
  2735. }
  2736. DEFINE_SIMPLE_ATTRIBUTE(fault_around_bytes_fops,
  2737. fault_around_bytes_get, fault_around_bytes_set, "%llu\n");
  2738. static int __init fault_around_debugfs(void)
  2739. {
  2740. void *ret;
  2741. ret = debugfs_create_file("fault_around_bytes", 0644, NULL, NULL,
  2742. &fault_around_bytes_fops);
  2743. if (!ret)
  2744. pr_warn("Failed to create fault_around_bytes in debugfs");
  2745. return 0;
  2746. }
  2747. late_initcall(fault_around_debugfs);
  2748. #endif
  2749. /*
  2750. * do_fault_around() tries to map few pages around the fault address. The hope
  2751. * is that the pages will be needed soon and this will lower the number of
  2752. * faults to handle.
  2753. *
  2754. * It uses vm_ops->map_pages() to map the pages, which skips the page if it's
  2755. * not ready to be mapped: not up-to-date, locked, etc.
  2756. *
  2757. * This function is called with the page table lock taken. In the split ptlock
  2758. * case the page table lock only protects only those entries which belong to
  2759. * the page table corresponding to the fault address.
  2760. *
  2761. * This function doesn't cross the VMA boundaries, in order to call map_pages()
  2762. * only once.
  2763. *
  2764. * fault_around_pages() defines how many pages we'll try to map.
  2765. * do_fault_around() expects it to return a power of two less than or equal to
  2766. * PTRS_PER_PTE.
  2767. *
  2768. * The virtual address of the area that we map is naturally aligned to the
  2769. * fault_around_pages() value (and therefore to page order). This way it's
  2770. * easier to guarantee that we don't cross page table boundaries.
  2771. */
  2772. static int do_fault_around(struct fault_env *fe, pgoff_t start_pgoff)
  2773. {
  2774. unsigned long address = fe->address, nr_pages, mask;
  2775. pgoff_t end_pgoff;
  2776. int off, ret = 0;
  2777. nr_pages = READ_ONCE(fault_around_bytes) >> PAGE_SHIFT;
  2778. mask = ~(nr_pages * PAGE_SIZE - 1) & PAGE_MASK;
  2779. fe->address = max(address & mask, fe->vma->vm_start);
  2780. off = ((address - fe->address) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  2781. start_pgoff -= off;
  2782. /*
  2783. * end_pgoff is either end of page table or end of vma
  2784. * or fault_around_pages() from start_pgoff, depending what is nearest.
  2785. */
  2786. end_pgoff = start_pgoff -
  2787. ((fe->address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1)) +
  2788. PTRS_PER_PTE - 1;
  2789. end_pgoff = min3(end_pgoff, vma_pages(fe->vma) + fe->vma->vm_pgoff - 1,
  2790. start_pgoff + nr_pages - 1);
  2791. if (pmd_none(*fe->pmd)) {
  2792. fe->prealloc_pte = pte_alloc_one(fe->vma->vm_mm, fe->address);
  2793. if (!fe->prealloc_pte)
  2794. goto out;
  2795. smp_wmb(); /* See comment in __pte_alloc() */
  2796. }
  2797. fe->vma->vm_ops->map_pages(fe, start_pgoff, end_pgoff);
  2798. /* preallocated pagetable is unused: free it */
  2799. if (fe->prealloc_pte) {
  2800. pte_free(fe->vma->vm_mm, fe->prealloc_pte);
  2801. fe->prealloc_pte = 0;
  2802. }
  2803. /* Huge page is mapped? Page fault is solved */
  2804. if (pmd_trans_huge(*fe->pmd)) {
  2805. ret = VM_FAULT_NOPAGE;
  2806. goto out;
  2807. }
  2808. /* ->map_pages() haven't done anything useful. Cold page cache? */
  2809. if (!fe->pte)
  2810. goto out;
  2811. /* check if the page fault is solved */
  2812. fe->pte -= (fe->address >> PAGE_SHIFT) - (address >> PAGE_SHIFT);
  2813. if (!pte_none(*fe->pte))
  2814. ret = VM_FAULT_NOPAGE;
  2815. pte_unmap_unlock(fe->pte, fe->ptl);
  2816. out:
  2817. fe->address = address;
  2818. fe->pte = NULL;
  2819. return ret;
  2820. }
  2821. static int do_read_fault(struct fault_env *fe, pgoff_t pgoff)
  2822. {
  2823. struct vm_area_struct *vma = fe->vma;
  2824. struct page *fault_page;
  2825. int ret = 0;
  2826. /*
  2827. * Let's call ->map_pages() first and use ->fault() as fallback
  2828. * if page by the offset is not ready to be mapped (cold cache or
  2829. * something).
  2830. */
  2831. if (vma->vm_ops->map_pages && fault_around_bytes >> PAGE_SHIFT > 1) {
  2832. ret = do_fault_around(fe, pgoff);
  2833. if (ret)
  2834. return ret;
  2835. }
  2836. ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
  2837. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2838. return ret;
  2839. ret |= alloc_set_pte(fe, NULL, fault_page);
  2840. if (fe->pte)
  2841. pte_unmap_unlock(fe->pte, fe->ptl);
  2842. unlock_page(fault_page);
  2843. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2844. put_page(fault_page);
  2845. return ret;
  2846. }
  2847. static int do_cow_fault(struct fault_env *fe, pgoff_t pgoff)
  2848. {
  2849. struct vm_area_struct *vma = fe->vma;
  2850. struct page *fault_page, *new_page;
  2851. void *fault_entry;
  2852. struct mem_cgroup *memcg;
  2853. int ret;
  2854. if (unlikely(anon_vma_prepare(vma)))
  2855. return VM_FAULT_OOM;
  2856. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, fe->address);
  2857. if (!new_page)
  2858. return VM_FAULT_OOM;
  2859. if (mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL,
  2860. &memcg, false)) {
  2861. put_page(new_page);
  2862. return VM_FAULT_OOM;
  2863. }
  2864. ret = __do_fault(fe, pgoff, new_page, &fault_page, &fault_entry);
  2865. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2866. goto uncharge_out;
  2867. if (!(ret & VM_FAULT_DAX_LOCKED))
  2868. copy_user_highpage(new_page, fault_page, fe->address, vma);
  2869. __SetPageUptodate(new_page);
  2870. ret |= alloc_set_pte(fe, memcg, new_page);
  2871. if (fe->pte)
  2872. pte_unmap_unlock(fe->pte, fe->ptl);
  2873. if (!(ret & VM_FAULT_DAX_LOCKED)) {
  2874. unlock_page(fault_page);
  2875. put_page(fault_page);
  2876. } else {
  2877. dax_unlock_mapping_entry(vma->vm_file->f_mapping, pgoff);
  2878. }
  2879. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2880. goto uncharge_out;
  2881. return ret;
  2882. uncharge_out:
  2883. mem_cgroup_cancel_charge(new_page, memcg, false);
  2884. put_page(new_page);
  2885. return ret;
  2886. }
  2887. static int do_shared_fault(struct fault_env *fe, pgoff_t pgoff)
  2888. {
  2889. struct vm_area_struct *vma = fe->vma;
  2890. struct page *fault_page;
  2891. struct address_space *mapping;
  2892. int dirtied = 0;
  2893. int ret, tmp;
  2894. ret = __do_fault(fe, pgoff, NULL, &fault_page, NULL);
  2895. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
  2896. return ret;
  2897. /*
  2898. * Check if the backing address space wants to know that the page is
  2899. * about to become writable
  2900. */
  2901. if (vma->vm_ops->page_mkwrite) {
  2902. unlock_page(fault_page);
  2903. tmp = do_page_mkwrite(vma, fault_page, fe->address);
  2904. if (unlikely(!tmp ||
  2905. (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
  2906. put_page(fault_page);
  2907. return tmp;
  2908. }
  2909. }
  2910. ret |= alloc_set_pte(fe, NULL, fault_page);
  2911. if (fe->pte)
  2912. pte_unmap_unlock(fe->pte, fe->ptl);
  2913. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
  2914. VM_FAULT_RETRY))) {
  2915. unlock_page(fault_page);
  2916. put_page(fault_page);
  2917. return ret;
  2918. }
  2919. if (set_page_dirty(fault_page))
  2920. dirtied = 1;
  2921. /*
  2922. * Take a local copy of the address_space - page.mapping may be zeroed
  2923. * by truncate after unlock_page(). The address_space itself remains
  2924. * pinned by vma->vm_file's reference. We rely on unlock_page()'s
  2925. * release semantics to prevent the compiler from undoing this copying.
  2926. */
  2927. mapping = page_rmapping(fault_page);
  2928. unlock_page(fault_page);
  2929. if ((dirtied || vma->vm_ops->page_mkwrite) && mapping) {
  2930. /*
  2931. * Some device drivers do not set page.mapping but still
  2932. * dirty their pages
  2933. */
  2934. balance_dirty_pages_ratelimited(mapping);
  2935. }
  2936. if (!vma->vm_ops->page_mkwrite)
  2937. file_update_time(vma->vm_file);
  2938. return ret;
  2939. }
  2940. /*
  2941. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2942. * but allow concurrent faults).
  2943. * The mmap_sem may have been released depending on flags and our
  2944. * return value. See filemap_fault() and __lock_page_or_retry().
  2945. */
  2946. static int do_fault(struct fault_env *fe)
  2947. {
  2948. struct vm_area_struct *vma = fe->vma;
  2949. pgoff_t pgoff = linear_page_index(vma, fe->address);
  2950. /* The VMA was not fully populated on mmap() or missing VM_DONTEXPAND */
  2951. if (!vma->vm_ops->fault)
  2952. return VM_FAULT_SIGBUS;
  2953. if (!(fe->flags & FAULT_FLAG_WRITE))
  2954. return do_read_fault(fe, pgoff);
  2955. if (!(vma->vm_flags & VM_SHARED))
  2956. return do_cow_fault(fe, pgoff);
  2957. return do_shared_fault(fe, pgoff);
  2958. }
  2959. static int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
  2960. unsigned long addr, int page_nid,
  2961. int *flags)
  2962. {
  2963. get_page(page);
  2964. count_vm_numa_event(NUMA_HINT_FAULTS);
  2965. if (page_nid == numa_node_id()) {
  2966. count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
  2967. *flags |= TNF_FAULT_LOCAL;
  2968. }
  2969. return mpol_misplaced(page, vma, addr);
  2970. }
  2971. static int do_numa_page(struct fault_env *fe, pte_t pte)
  2972. {
  2973. struct vm_area_struct *vma = fe->vma;
  2974. struct page *page = NULL;
  2975. int page_nid = -1;
  2976. int last_cpupid;
  2977. int target_nid;
  2978. bool migrated = false;
  2979. bool was_writable = pte_write(pte);
  2980. int flags = 0;
  2981. /*
  2982. * The "pte" at this point cannot be used safely without
  2983. * validation through pte_unmap_same(). It's of NUMA type but
  2984. * the pfn may be screwed if the read is non atomic.
  2985. *
  2986. * We can safely just do a "set_pte_at()", because the old
  2987. * page table entry is not accessible, so there would be no
  2988. * concurrent hardware modifications to the PTE.
  2989. */
  2990. fe->ptl = pte_lockptr(vma->vm_mm, fe->pmd);
  2991. spin_lock(fe->ptl);
  2992. if (unlikely(!pte_same(*fe->pte, pte))) {
  2993. pte_unmap_unlock(fe->pte, fe->ptl);
  2994. goto out;
  2995. }
  2996. /* Make it present again */
  2997. pte = pte_modify(pte, vma->vm_page_prot);
  2998. pte = pte_mkyoung(pte);
  2999. if (was_writable)
  3000. pte = pte_mkwrite(pte);
  3001. set_pte_at(vma->vm_mm, fe->address, fe->pte, pte);
  3002. update_mmu_cache(vma, fe->address, fe->pte);
  3003. page = vm_normal_page(vma, fe->address, pte);
  3004. if (!page) {
  3005. pte_unmap_unlock(fe->pte, fe->ptl);
  3006. return 0;
  3007. }
  3008. /* TODO: handle PTE-mapped THP */
  3009. if (PageCompound(page)) {
  3010. pte_unmap_unlock(fe->pte, fe->ptl);
  3011. return 0;
  3012. }
  3013. /*
  3014. * Avoid grouping on RO pages in general. RO pages shouldn't hurt as
  3015. * much anyway since they can be in shared cache state. This misses
  3016. * the case where a mapping is writable but the process never writes
  3017. * to it but pte_write gets cleared during protection updates and
  3018. * pte_dirty has unpredictable behaviour between PTE scan updates,
  3019. * background writeback, dirty balancing and application behaviour.
  3020. */
  3021. if (!pte_write(pte))
  3022. flags |= TNF_NO_GROUP;
  3023. /*
  3024. * Flag if the page is shared between multiple address spaces. This
  3025. * is later used when determining whether to group tasks together
  3026. */
  3027. if (page_mapcount(page) > 1 && (vma->vm_flags & VM_SHARED))
  3028. flags |= TNF_SHARED;
  3029. last_cpupid = page_cpupid_last(page);
  3030. page_nid = page_to_nid(page);
  3031. target_nid = numa_migrate_prep(page, vma, fe->address, page_nid,
  3032. &flags);
  3033. pte_unmap_unlock(fe->pte, fe->ptl);
  3034. if (target_nid == -1) {
  3035. put_page(page);
  3036. goto out;
  3037. }
  3038. /* Migrate to the requested node */
  3039. migrated = migrate_misplaced_page(page, vma, target_nid);
  3040. if (migrated) {
  3041. page_nid = target_nid;
  3042. flags |= TNF_MIGRATED;
  3043. } else
  3044. flags |= TNF_MIGRATE_FAIL;
  3045. out:
  3046. if (page_nid != -1)
  3047. task_numa_fault(last_cpupid, page_nid, 1, flags);
  3048. return 0;
  3049. }
  3050. static int create_huge_pmd(struct fault_env *fe)
  3051. {
  3052. struct vm_area_struct *vma = fe->vma;
  3053. if (vma_is_anonymous(vma))
  3054. return do_huge_pmd_anonymous_page(fe);
  3055. if (vma->vm_ops->pmd_fault)
  3056. return vma->vm_ops->pmd_fault(vma, fe->address, fe->pmd,
  3057. fe->flags);
  3058. return VM_FAULT_FALLBACK;
  3059. }
  3060. static int wp_huge_pmd(struct fault_env *fe, pmd_t orig_pmd)
  3061. {
  3062. if (vma_is_anonymous(fe->vma))
  3063. return do_huge_pmd_wp_page(fe, orig_pmd);
  3064. if (fe->vma->vm_ops->pmd_fault)
  3065. return fe->vma->vm_ops->pmd_fault(fe->vma, fe->address, fe->pmd,
  3066. fe->flags);
  3067. /* COW handled on pte level: split pmd */
  3068. VM_BUG_ON_VMA(fe->vma->vm_flags & VM_SHARED, fe->vma);
  3069. split_huge_pmd(fe->vma, fe->pmd, fe->address);
  3070. return VM_FAULT_FALLBACK;
  3071. }
  3072. static inline bool vma_is_accessible(struct vm_area_struct *vma)
  3073. {
  3074. return vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE);
  3075. }
  3076. /*
  3077. * These routines also need to handle stuff like marking pages dirty
  3078. * and/or accessed for architectures that don't do it in hardware (most
  3079. * RISC architectures). The early dirtying is also good on the i386.
  3080. *
  3081. * There is also a hook called "update_mmu_cache()" that architectures
  3082. * with external mmu caches can use to update those (ie the Sparc or
  3083. * PowerPC hashed page tables that act as extended TLBs).
  3084. *
  3085. * We enter with non-exclusive mmap_sem (to exclude vma changes, but allow
  3086. * concurrent faults).
  3087. *
  3088. * The mmap_sem may have been released depending on flags and our return value.
  3089. * See filemap_fault() and __lock_page_or_retry().
  3090. */
  3091. static int handle_pte_fault(struct fault_env *fe)
  3092. {
  3093. pte_t entry;
  3094. if (unlikely(pmd_none(*fe->pmd))) {
  3095. /*
  3096. * Leave __pte_alloc() until later: because vm_ops->fault may
  3097. * want to allocate huge page, and if we expose page table
  3098. * for an instant, it will be difficult to retract from
  3099. * concurrent faults and from rmap lookups.
  3100. */
  3101. fe->pte = NULL;
  3102. } else {
  3103. /* See comment in pte_alloc_one_map() */
  3104. if (pmd_trans_unstable(fe->pmd) || pmd_devmap(*fe->pmd))
  3105. return 0;
  3106. /*
  3107. * A regular pmd is established and it can't morph into a huge
  3108. * pmd from under us anymore at this point because we hold the
  3109. * mmap_sem read mode and khugepaged takes it in write mode.
  3110. * So now it's safe to run pte_offset_map().
  3111. */
  3112. fe->pte = pte_offset_map(fe->pmd, fe->address);
  3113. entry = *fe->pte;
  3114. /*
  3115. * some architectures can have larger ptes than wordsize,
  3116. * e.g.ppc44x-defconfig has CONFIG_PTE_64BIT=y and
  3117. * CONFIG_32BIT=y, so READ_ONCE or ACCESS_ONCE cannot guarantee
  3118. * atomic accesses. The code below just needs a consistent
  3119. * view for the ifs and we later double check anyway with the
  3120. * ptl lock held. So here a barrier will do.
  3121. */
  3122. barrier();
  3123. if (pte_none(entry)) {
  3124. pte_unmap(fe->pte);
  3125. fe->pte = NULL;
  3126. }
  3127. }
  3128. if (!fe->pte) {
  3129. if (vma_is_anonymous(fe->vma))
  3130. return do_anonymous_page(fe);
  3131. else
  3132. return do_fault(fe);
  3133. }
  3134. if (!pte_present(entry))
  3135. return do_swap_page(fe, entry);
  3136. if (pte_protnone(entry) && vma_is_accessible(fe->vma))
  3137. return do_numa_page(fe, entry);
  3138. fe->ptl = pte_lockptr(fe->vma->vm_mm, fe->pmd);
  3139. spin_lock(fe->ptl);
  3140. if (unlikely(!pte_same(*fe->pte, entry)))
  3141. goto unlock;
  3142. if (fe->flags & FAULT_FLAG_WRITE) {
  3143. if (!pte_write(entry))
  3144. return do_wp_page(fe, entry);
  3145. entry = pte_mkdirty(entry);
  3146. }
  3147. entry = pte_mkyoung(entry);
  3148. if (ptep_set_access_flags(fe->vma, fe->address, fe->pte, entry,
  3149. fe->flags & FAULT_FLAG_WRITE)) {
  3150. update_mmu_cache(fe->vma, fe->address, fe->pte);
  3151. } else {
  3152. /*
  3153. * This is needed only for protection faults but the arch code
  3154. * is not yet telling us if this is a protection fault or not.
  3155. * This still avoids useless tlb flushes for .text page faults
  3156. * with threads.
  3157. */
  3158. if (fe->flags & FAULT_FLAG_WRITE)
  3159. flush_tlb_fix_spurious_fault(fe->vma, fe->address);
  3160. }
  3161. unlock:
  3162. pte_unmap_unlock(fe->pte, fe->ptl);
  3163. return 0;
  3164. }
  3165. /*
  3166. * By the time we get here, we already hold the mm semaphore
  3167. *
  3168. * The mmap_sem may have been released depending on flags and our
  3169. * return value. See filemap_fault() and __lock_page_or_retry().
  3170. */
  3171. static int __handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
  3172. unsigned int flags)
  3173. {
  3174. struct fault_env fe = {
  3175. .vma = vma,
  3176. .address = address,
  3177. .flags = flags,
  3178. };
  3179. struct mm_struct *mm = vma->vm_mm;
  3180. pgd_t *pgd;
  3181. pud_t *pud;
  3182. pgd = pgd_offset(mm, address);
  3183. pud = pud_alloc(mm, pgd, address);
  3184. if (!pud)
  3185. return VM_FAULT_OOM;
  3186. fe.pmd = pmd_alloc(mm, pud, address);
  3187. if (!fe.pmd)
  3188. return VM_FAULT_OOM;
  3189. if (pmd_none(*fe.pmd) && transparent_hugepage_enabled(vma)) {
  3190. int ret = create_huge_pmd(&fe);
  3191. if (!(ret & VM_FAULT_FALLBACK))
  3192. return ret;
  3193. } else {
  3194. pmd_t orig_pmd = *fe.pmd;
  3195. int ret;
  3196. barrier();
  3197. if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) {
  3198. if (pmd_protnone(orig_pmd) && vma_is_accessible(vma))
  3199. return do_huge_pmd_numa_page(&fe, orig_pmd);
  3200. if ((fe.flags & FAULT_FLAG_WRITE) &&
  3201. !pmd_write(orig_pmd)) {
  3202. ret = wp_huge_pmd(&fe, orig_pmd);
  3203. if (!(ret & VM_FAULT_FALLBACK))
  3204. return ret;
  3205. } else {
  3206. huge_pmd_set_accessed(&fe, orig_pmd);
  3207. return 0;
  3208. }
  3209. }
  3210. }
  3211. return handle_pte_fault(&fe);
  3212. }
  3213. /*
  3214. * By the time we get here, we already hold the mm semaphore
  3215. *
  3216. * The mmap_sem may have been released depending on flags and our
  3217. * return value. See filemap_fault() and __lock_page_or_retry().
  3218. */
  3219. int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
  3220. unsigned int flags)
  3221. {
  3222. int ret;
  3223. __set_current_state(TASK_RUNNING);
  3224. count_vm_event(PGFAULT);
  3225. mem_cgroup_count_vm_event(vma->vm_mm, PGFAULT);
  3226. /* do counter updates before entering really critical section. */
  3227. check_sync_rss_stat(current);
  3228. /*
  3229. * Enable the memcg OOM handling for faults triggered in user
  3230. * space. Kernel faults are handled more gracefully.
  3231. */
  3232. if (flags & FAULT_FLAG_USER)
  3233. mem_cgroup_oom_enable();
  3234. if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
  3235. flags & FAULT_FLAG_INSTRUCTION,
  3236. flags & FAULT_FLAG_REMOTE))
  3237. return VM_FAULT_SIGSEGV;
  3238. if (unlikely(is_vm_hugetlb_page(vma)))
  3239. ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
  3240. else
  3241. ret = __handle_mm_fault(vma, address, flags);
  3242. if (flags & FAULT_FLAG_USER) {
  3243. mem_cgroup_oom_disable();
  3244. /*
  3245. * The task may have entered a memcg OOM situation but
  3246. * if the allocation error was handled gracefully (no
  3247. * VM_FAULT_OOM), there is no need to kill anything.
  3248. * Just clean up the OOM state peacefully.
  3249. */
  3250. if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
  3251. mem_cgroup_oom_synchronize(false);
  3252. }
  3253. /*
  3254. * This mm has been already reaped by the oom reaper and so the
  3255. * refault cannot be trusted in general. Anonymous refaults would
  3256. * lose data and give a zero page instead e.g. This is especially
  3257. * problem for use_mm() because regular tasks will just die and
  3258. * the corrupted data will not be visible anywhere while kthread
  3259. * will outlive the oom victim and potentially propagate the date
  3260. * further.
  3261. */
  3262. if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
  3263. && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags)))
  3264. ret = VM_FAULT_SIGBUS;
  3265. return ret;
  3266. }
  3267. EXPORT_SYMBOL_GPL(handle_mm_fault);
  3268. #ifndef __PAGETABLE_PUD_FOLDED
  3269. /*
  3270. * Allocate page upper directory.
  3271. * We've already handled the fast-path in-line.
  3272. */
  3273. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  3274. {
  3275. pud_t *new = pud_alloc_one(mm, address);
  3276. if (!new)
  3277. return -ENOMEM;
  3278. smp_wmb(); /* See comment in __pte_alloc */
  3279. spin_lock(&mm->page_table_lock);
  3280. if (pgd_present(*pgd)) /* Another has populated it */
  3281. pud_free(mm, new);
  3282. else
  3283. pgd_populate(mm, pgd, new);
  3284. spin_unlock(&mm->page_table_lock);
  3285. return 0;
  3286. }
  3287. #endif /* __PAGETABLE_PUD_FOLDED */
  3288. #ifndef __PAGETABLE_PMD_FOLDED
  3289. /*
  3290. * Allocate page middle directory.
  3291. * We've already handled the fast-path in-line.
  3292. */
  3293. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  3294. {
  3295. pmd_t *new = pmd_alloc_one(mm, address);
  3296. if (!new)
  3297. return -ENOMEM;
  3298. smp_wmb(); /* See comment in __pte_alloc */
  3299. spin_lock(&mm->page_table_lock);
  3300. #ifndef __ARCH_HAS_4LEVEL_HACK
  3301. if (!pud_present(*pud)) {
  3302. mm_inc_nr_pmds(mm);
  3303. pud_populate(mm, pud, new);
  3304. } else /* Another has populated it */
  3305. pmd_free(mm, new);
  3306. #else
  3307. if (!pgd_present(*pud)) {
  3308. mm_inc_nr_pmds(mm);
  3309. pgd_populate(mm, pud, new);
  3310. } else /* Another has populated it */
  3311. pmd_free(mm, new);
  3312. #endif /* __ARCH_HAS_4LEVEL_HACK */
  3313. spin_unlock(&mm->page_table_lock);
  3314. return 0;
  3315. }
  3316. #endif /* __PAGETABLE_PMD_FOLDED */
  3317. static int __follow_pte(struct mm_struct *mm, unsigned long address,
  3318. pte_t **ptepp, spinlock_t **ptlp)
  3319. {
  3320. pgd_t *pgd;
  3321. pud_t *pud;
  3322. pmd_t *pmd;
  3323. pte_t *ptep;
  3324. pgd = pgd_offset(mm, address);
  3325. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  3326. goto out;
  3327. pud = pud_offset(pgd, address);
  3328. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  3329. goto out;
  3330. pmd = pmd_offset(pud, address);
  3331. VM_BUG_ON(pmd_trans_huge(*pmd));
  3332. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  3333. goto out;
  3334. /* We cannot handle huge page PFN maps. Luckily they don't exist. */
  3335. if (pmd_huge(*pmd))
  3336. goto out;
  3337. ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
  3338. if (!ptep)
  3339. goto out;
  3340. if (!pte_present(*ptep))
  3341. goto unlock;
  3342. *ptepp = ptep;
  3343. return 0;
  3344. unlock:
  3345. pte_unmap_unlock(ptep, *ptlp);
  3346. out:
  3347. return -EINVAL;
  3348. }
  3349. static inline int follow_pte(struct mm_struct *mm, unsigned long address,
  3350. pte_t **ptepp, spinlock_t **ptlp)
  3351. {
  3352. int res;
  3353. /* (void) is needed to make gcc happy */
  3354. (void) __cond_lock(*ptlp,
  3355. !(res = __follow_pte(mm, address, ptepp, ptlp)));
  3356. return res;
  3357. }
  3358. /**
  3359. * follow_pfn - look up PFN at a user virtual address
  3360. * @vma: memory mapping
  3361. * @address: user virtual address
  3362. * @pfn: location to store found PFN
  3363. *
  3364. * Only IO mappings and raw PFN mappings are allowed.
  3365. *
  3366. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  3367. */
  3368. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  3369. unsigned long *pfn)
  3370. {
  3371. int ret = -EINVAL;
  3372. spinlock_t *ptl;
  3373. pte_t *ptep;
  3374. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3375. return ret;
  3376. ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
  3377. if (ret)
  3378. return ret;
  3379. *pfn = pte_pfn(*ptep);
  3380. pte_unmap_unlock(ptep, ptl);
  3381. return 0;
  3382. }
  3383. EXPORT_SYMBOL(follow_pfn);
  3384. #ifdef CONFIG_HAVE_IOREMAP_PROT
  3385. int follow_phys(struct vm_area_struct *vma,
  3386. unsigned long address, unsigned int flags,
  3387. unsigned long *prot, resource_size_t *phys)
  3388. {
  3389. int ret = -EINVAL;
  3390. pte_t *ptep, pte;
  3391. spinlock_t *ptl;
  3392. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3393. goto out;
  3394. if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
  3395. goto out;
  3396. pte = *ptep;
  3397. if ((flags & FOLL_WRITE) && !pte_write(pte))
  3398. goto unlock;
  3399. *prot = pgprot_val(pte_pgprot(pte));
  3400. *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
  3401. ret = 0;
  3402. unlock:
  3403. pte_unmap_unlock(ptep, ptl);
  3404. out:
  3405. return ret;
  3406. }
  3407. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  3408. void *buf, int len, int write)
  3409. {
  3410. resource_size_t phys_addr;
  3411. unsigned long prot = 0;
  3412. void __iomem *maddr;
  3413. int offset = addr & (PAGE_SIZE-1);
  3414. if (follow_phys(vma, addr, write, &prot, &phys_addr))
  3415. return -EINVAL;
  3416. maddr = ioremap_prot(phys_addr, PAGE_ALIGN(len + offset), prot);
  3417. if (write)
  3418. memcpy_toio(maddr + offset, buf, len);
  3419. else
  3420. memcpy_fromio(buf, maddr + offset, len);
  3421. iounmap(maddr);
  3422. return len;
  3423. }
  3424. EXPORT_SYMBOL_GPL(generic_access_phys);
  3425. #endif
  3426. /*
  3427. * Access another process' address space as given in mm. If non-NULL, use the
  3428. * given task for page fault accounting.
  3429. */
  3430. static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  3431. unsigned long addr, void *buf, int len, int write)
  3432. {
  3433. struct vm_area_struct *vma;
  3434. void *old_buf = buf;
  3435. down_read(&mm->mmap_sem);
  3436. /* ignore errors, just check how much was successfully transferred */
  3437. while (len) {
  3438. int bytes, ret, offset;
  3439. void *maddr;
  3440. struct page *page = NULL;
  3441. ret = get_user_pages_remote(tsk, mm, addr, 1,
  3442. write, 1, &page, &vma);
  3443. if (ret <= 0) {
  3444. #ifndef CONFIG_HAVE_IOREMAP_PROT
  3445. break;
  3446. #else
  3447. /*
  3448. * Check if this is a VM_IO | VM_PFNMAP VMA, which
  3449. * we can access using slightly different code.
  3450. */
  3451. vma = find_vma(mm, addr);
  3452. if (!vma || vma->vm_start > addr)
  3453. break;
  3454. if (vma->vm_ops && vma->vm_ops->access)
  3455. ret = vma->vm_ops->access(vma, addr, buf,
  3456. len, write);
  3457. if (ret <= 0)
  3458. break;
  3459. bytes = ret;
  3460. #endif
  3461. } else {
  3462. bytes = len;
  3463. offset = addr & (PAGE_SIZE-1);
  3464. if (bytes > PAGE_SIZE-offset)
  3465. bytes = PAGE_SIZE-offset;
  3466. maddr = kmap(page);
  3467. if (write) {
  3468. copy_to_user_page(vma, page, addr,
  3469. maddr + offset, buf, bytes);
  3470. set_page_dirty_lock(page);
  3471. } else {
  3472. copy_from_user_page(vma, page, addr,
  3473. buf, maddr + offset, bytes);
  3474. }
  3475. kunmap(page);
  3476. put_page(page);
  3477. }
  3478. len -= bytes;
  3479. buf += bytes;
  3480. addr += bytes;
  3481. }
  3482. up_read(&mm->mmap_sem);
  3483. return buf - old_buf;
  3484. }
  3485. /**
  3486. * access_remote_vm - access another process' address space
  3487. * @mm: the mm_struct of the target address space
  3488. * @addr: start address to access
  3489. * @buf: source or destination buffer
  3490. * @len: number of bytes to transfer
  3491. * @write: whether the access is a write
  3492. *
  3493. * The caller must hold a reference on @mm.
  3494. */
  3495. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  3496. void *buf, int len, int write)
  3497. {
  3498. return __access_remote_vm(NULL, mm, addr, buf, len, write);
  3499. }
  3500. /*
  3501. * Access another process' address space.
  3502. * Source/target buffer must be kernel space,
  3503. * Do not walk the page table directly, use get_user_pages
  3504. */
  3505. int access_process_vm(struct task_struct *tsk, unsigned long addr,
  3506. void *buf, int len, int write)
  3507. {
  3508. struct mm_struct *mm;
  3509. int ret;
  3510. mm = get_task_mm(tsk);
  3511. if (!mm)
  3512. return 0;
  3513. ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
  3514. mmput(mm);
  3515. return ret;
  3516. }
  3517. /*
  3518. * Print the name of a VMA.
  3519. */
  3520. void print_vma_addr(char *prefix, unsigned long ip)
  3521. {
  3522. struct mm_struct *mm = current->mm;
  3523. struct vm_area_struct *vma;
  3524. /*
  3525. * Do not print if we are in atomic
  3526. * contexts (in exception stacks, etc.):
  3527. */
  3528. if (preempt_count())
  3529. return;
  3530. down_read(&mm->mmap_sem);
  3531. vma = find_vma(mm, ip);
  3532. if (vma && vma->vm_file) {
  3533. struct file *f = vma->vm_file;
  3534. char *buf = (char *)__get_free_page(GFP_KERNEL);
  3535. if (buf) {
  3536. char *p;
  3537. p = file_path(f, buf, PAGE_SIZE);
  3538. if (IS_ERR(p))
  3539. p = "?";
  3540. printk("%s%s[%lx+%lx]", prefix, kbasename(p),
  3541. vma->vm_start,
  3542. vma->vm_end - vma->vm_start);
  3543. free_page((unsigned long)buf);
  3544. }
  3545. }
  3546. up_read(&mm->mmap_sem);
  3547. }
  3548. #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  3549. void __might_fault(const char *file, int line)
  3550. {
  3551. /*
  3552. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  3553. * holding the mmap_sem, this is safe because kernel memory doesn't
  3554. * get paged out, therefore we'll never actually fault, and the
  3555. * below annotations will generate false positives.
  3556. */
  3557. if (segment_eq(get_fs(), KERNEL_DS))
  3558. return;
  3559. if (pagefault_disabled())
  3560. return;
  3561. __might_sleep(file, line, 0);
  3562. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  3563. if (current->mm)
  3564. might_lock_read(&current->mm->mmap_sem);
  3565. #endif
  3566. }
  3567. EXPORT_SYMBOL(__might_fault);
  3568. #endif
  3569. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  3570. static void clear_gigantic_page(struct page *page,
  3571. unsigned long addr,
  3572. unsigned int pages_per_huge_page)
  3573. {
  3574. int i;
  3575. struct page *p = page;
  3576. might_sleep();
  3577. for (i = 0; i < pages_per_huge_page;
  3578. i++, p = mem_map_next(p, page, i)) {
  3579. cond_resched();
  3580. clear_user_highpage(p, addr + i * PAGE_SIZE);
  3581. }
  3582. }
  3583. void clear_huge_page(struct page *page,
  3584. unsigned long addr, unsigned int pages_per_huge_page)
  3585. {
  3586. int i;
  3587. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3588. clear_gigantic_page(page, addr, pages_per_huge_page);
  3589. return;
  3590. }
  3591. might_sleep();
  3592. for (i = 0; i < pages_per_huge_page; i++) {
  3593. cond_resched();
  3594. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  3595. }
  3596. }
  3597. static void copy_user_gigantic_page(struct page *dst, struct page *src,
  3598. unsigned long addr,
  3599. struct vm_area_struct *vma,
  3600. unsigned int pages_per_huge_page)
  3601. {
  3602. int i;
  3603. struct page *dst_base = dst;
  3604. struct page *src_base = src;
  3605. for (i = 0; i < pages_per_huge_page; ) {
  3606. cond_resched();
  3607. copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
  3608. i++;
  3609. dst = mem_map_next(dst, dst_base, i);
  3610. src = mem_map_next(src, src_base, i);
  3611. }
  3612. }
  3613. void copy_user_huge_page(struct page *dst, struct page *src,
  3614. unsigned long addr, struct vm_area_struct *vma,
  3615. unsigned int pages_per_huge_page)
  3616. {
  3617. int i;
  3618. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3619. copy_user_gigantic_page(dst, src, addr, vma,
  3620. pages_per_huge_page);
  3621. return;
  3622. }
  3623. might_sleep();
  3624. for (i = 0; i < pages_per_huge_page; i++) {
  3625. cond_resched();
  3626. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  3627. }
  3628. }
  3629. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
  3630. #if USE_SPLIT_PTE_PTLOCKS && ALLOC_SPLIT_PTLOCKS
  3631. static struct kmem_cache *page_ptl_cachep;
  3632. void __init ptlock_cache_init(void)
  3633. {
  3634. page_ptl_cachep = kmem_cache_create("page->ptl", sizeof(spinlock_t), 0,
  3635. SLAB_PANIC, NULL);
  3636. }
  3637. bool ptlock_alloc(struct page *page)
  3638. {
  3639. spinlock_t *ptl;
  3640. ptl = kmem_cache_alloc(page_ptl_cachep, GFP_KERNEL);
  3641. if (!ptl)
  3642. return false;
  3643. page->ptl = ptl;
  3644. return true;
  3645. }
  3646. void ptlock_free(struct page *page)
  3647. {
  3648. kmem_cache_free(page_ptl_cachep, page->ptl);
  3649. }
  3650. #endif