memory.c 111 KB

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