memory.c 117 KB

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