memory.c 112 KB

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