memory.c 127 KB

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