memory.c 130 KB

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