memory.c 107 KB

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