memory.c 132 KB

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