memory.c 106 KB

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