mmap.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720
  1. /*
  2. * mm/mmap.c
  3. *
  4. * Written by obz.
  5. *
  6. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/backing-dev.h>
  12. #include <linux/mm.h>
  13. #include <linux/vmacache.h>
  14. #include <linux/shm.h>
  15. #include <linux/mman.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/swap.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/capability.h>
  20. #include <linux/init.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/personality.h>
  24. #include <linux/security.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/shmem_fs.h>
  27. #include <linux/profile.h>
  28. #include <linux/export.h>
  29. #include <linux/mount.h>
  30. #include <linux/mempolicy.h>
  31. #include <linux/rmap.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <linux/mmdebug.h>
  34. #include <linux/perf_event.h>
  35. #include <linux/audit.h>
  36. #include <linux/khugepaged.h>
  37. #include <linux/uprobes.h>
  38. #include <linux/rbtree_augmented.h>
  39. #include <linux/notifier.h>
  40. #include <linux/memory.h>
  41. #include <linux/printk.h>
  42. #include <linux/userfaultfd_k.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/pkeys.h>
  45. #include <linux/oom.h>
  46. #include <linux/uaccess.h>
  47. #include <asm/cacheflush.h>
  48. #include <asm/tlb.h>
  49. #include <asm/mmu_context.h>
  50. #include "internal.h"
  51. #ifndef arch_mmap_check
  52. #define arch_mmap_check(addr, len, flags) (0)
  53. #endif
  54. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
  55. const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
  56. const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
  57. int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
  58. #endif
  59. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
  60. const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
  61. const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
  62. int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
  63. #endif
  64. static bool ignore_rlimit_data;
  65. core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
  66. static void unmap_region(struct mm_struct *mm,
  67. struct vm_area_struct *vma, struct vm_area_struct *prev,
  68. unsigned long start, unsigned long end);
  69. /* description of effects of mapping type and prot in current implementation.
  70. * this is due to the limited x86 page protection hardware. The expected
  71. * behavior is in parens:
  72. *
  73. * map_type prot
  74. * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
  75. * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  76. * w: (no) no w: (no) no w: (yes) yes w: (no) no
  77. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  78. *
  79. * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  80. * w: (no) no w: (no) no w: (copy) copy w: (no) no
  81. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  82. *
  83. * On arm64, PROT_EXEC has the following behaviour for both MAP_SHARED and
  84. * MAP_PRIVATE:
  85. * r: (no) no
  86. * w: (no) no
  87. * x: (yes) yes
  88. */
  89. pgprot_t protection_map[16] __ro_after_init = {
  90. __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
  91. __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
  92. };
  93. #ifndef CONFIG_ARCH_HAS_FILTER_PGPROT
  94. static inline pgprot_t arch_filter_pgprot(pgprot_t prot)
  95. {
  96. return prot;
  97. }
  98. #endif
  99. pgprot_t vm_get_page_prot(unsigned long vm_flags)
  100. {
  101. pgprot_t ret = __pgprot(pgprot_val(protection_map[vm_flags &
  102. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
  103. pgprot_val(arch_vm_get_page_prot(vm_flags)));
  104. return arch_filter_pgprot(ret);
  105. }
  106. EXPORT_SYMBOL(vm_get_page_prot);
  107. static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
  108. {
  109. return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
  110. }
  111. /* Update vma->vm_page_prot to reflect vma->vm_flags. */
  112. void vma_set_page_prot(struct vm_area_struct *vma)
  113. {
  114. unsigned long vm_flags = vma->vm_flags;
  115. pgprot_t vm_page_prot;
  116. vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
  117. if (vma_wants_writenotify(vma, vm_page_prot)) {
  118. vm_flags &= ~VM_SHARED;
  119. vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags);
  120. }
  121. /* remove_protection_ptes reads vma->vm_page_prot without mmap_sem */
  122. WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
  123. }
  124. /*
  125. * Requires inode->i_mapping->i_mmap_rwsem
  126. */
  127. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  128. struct file *file, struct address_space *mapping)
  129. {
  130. if (vma->vm_flags & VM_DENYWRITE)
  131. atomic_inc(&file_inode(file)->i_writecount);
  132. if (vma->vm_flags & VM_SHARED)
  133. mapping_unmap_writable(mapping);
  134. flush_dcache_mmap_lock(mapping);
  135. vma_interval_tree_remove(vma, &mapping->i_mmap);
  136. flush_dcache_mmap_unlock(mapping);
  137. }
  138. /*
  139. * Unlink a file-based vm structure from its interval tree, to hide
  140. * vma from rmap and vmtruncate before freeing its page tables.
  141. */
  142. void unlink_file_vma(struct vm_area_struct *vma)
  143. {
  144. struct file *file = vma->vm_file;
  145. if (file) {
  146. struct address_space *mapping = file->f_mapping;
  147. i_mmap_lock_write(mapping);
  148. __remove_shared_vm_struct(vma, file, mapping);
  149. i_mmap_unlock_write(mapping);
  150. }
  151. }
  152. /*
  153. * Close a vm structure and free it, returning the next.
  154. */
  155. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  156. {
  157. struct vm_area_struct *next = vma->vm_next;
  158. might_sleep();
  159. if (vma->vm_ops && vma->vm_ops->close)
  160. vma->vm_ops->close(vma);
  161. if (vma->vm_file)
  162. fput(vma->vm_file);
  163. mpol_put(vma_policy(vma));
  164. kmem_cache_free(vm_area_cachep, vma);
  165. return next;
  166. }
  167. static int do_brk_flags(unsigned long addr, unsigned long request, unsigned long flags,
  168. struct list_head *uf);
  169. SYSCALL_DEFINE1(brk, unsigned long, brk)
  170. {
  171. unsigned long retval;
  172. unsigned long newbrk, oldbrk;
  173. struct mm_struct *mm = current->mm;
  174. struct vm_area_struct *next;
  175. unsigned long min_brk;
  176. bool populate;
  177. LIST_HEAD(uf);
  178. if (down_write_killable(&mm->mmap_sem))
  179. return -EINTR;
  180. #ifdef CONFIG_COMPAT_BRK
  181. /*
  182. * CONFIG_COMPAT_BRK can still be overridden by setting
  183. * randomize_va_space to 2, which will still cause mm->start_brk
  184. * to be arbitrarily shifted
  185. */
  186. if (current->brk_randomized)
  187. min_brk = mm->start_brk;
  188. else
  189. min_brk = mm->end_data;
  190. #else
  191. min_brk = mm->start_brk;
  192. #endif
  193. if (brk < min_brk)
  194. goto out;
  195. /*
  196. * Check against rlimit here. If this check is done later after the test
  197. * of oldbrk with newbrk then it can escape the test and let the data
  198. * segment grow beyond its set limit the in case where the limit is
  199. * not page aligned -Ram Gupta
  200. */
  201. if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
  202. mm->end_data, mm->start_data))
  203. goto out;
  204. newbrk = PAGE_ALIGN(brk);
  205. oldbrk = PAGE_ALIGN(mm->brk);
  206. if (oldbrk == newbrk)
  207. goto set_brk;
  208. /* Always allow shrinking brk. */
  209. if (brk <= mm->brk) {
  210. if (!do_munmap(mm, newbrk, oldbrk-newbrk, &uf))
  211. goto set_brk;
  212. goto out;
  213. }
  214. /* Check against existing mmap mappings. */
  215. next = find_vma(mm, oldbrk);
  216. if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
  217. goto out;
  218. /* Ok, looks good - let it rip. */
  219. if (do_brk_flags(oldbrk, newbrk-oldbrk, 0, &uf) < 0)
  220. goto out;
  221. set_brk:
  222. mm->brk = brk;
  223. populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
  224. up_write(&mm->mmap_sem);
  225. userfaultfd_unmap_complete(mm, &uf);
  226. if (populate)
  227. mm_populate(oldbrk, newbrk - oldbrk);
  228. return brk;
  229. out:
  230. retval = mm->brk;
  231. up_write(&mm->mmap_sem);
  232. return retval;
  233. }
  234. static long vma_compute_subtree_gap(struct vm_area_struct *vma)
  235. {
  236. unsigned long max, prev_end, subtree_gap;
  237. /*
  238. * Note: in the rare case of a VM_GROWSDOWN above a VM_GROWSUP, we
  239. * allow two stack_guard_gaps between them here, and when choosing
  240. * an unmapped area; whereas when expanding we only require one.
  241. * That's a little inconsistent, but keeps the code here simpler.
  242. */
  243. max = vm_start_gap(vma);
  244. if (vma->vm_prev) {
  245. prev_end = vm_end_gap(vma->vm_prev);
  246. if (max > prev_end)
  247. max -= prev_end;
  248. else
  249. max = 0;
  250. }
  251. if (vma->vm_rb.rb_left) {
  252. subtree_gap = rb_entry(vma->vm_rb.rb_left,
  253. struct vm_area_struct, vm_rb)->rb_subtree_gap;
  254. if (subtree_gap > max)
  255. max = subtree_gap;
  256. }
  257. if (vma->vm_rb.rb_right) {
  258. subtree_gap = rb_entry(vma->vm_rb.rb_right,
  259. struct vm_area_struct, vm_rb)->rb_subtree_gap;
  260. if (subtree_gap > max)
  261. max = subtree_gap;
  262. }
  263. return max;
  264. }
  265. #ifdef CONFIG_DEBUG_VM_RB
  266. static int browse_rb(struct mm_struct *mm)
  267. {
  268. struct rb_root *root = &mm->mm_rb;
  269. int i = 0, j, bug = 0;
  270. struct rb_node *nd, *pn = NULL;
  271. unsigned long prev = 0, pend = 0;
  272. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  273. struct vm_area_struct *vma;
  274. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  275. if (vma->vm_start < prev) {
  276. pr_emerg("vm_start %lx < prev %lx\n",
  277. vma->vm_start, prev);
  278. bug = 1;
  279. }
  280. if (vma->vm_start < pend) {
  281. pr_emerg("vm_start %lx < pend %lx\n",
  282. vma->vm_start, pend);
  283. bug = 1;
  284. }
  285. if (vma->vm_start > vma->vm_end) {
  286. pr_emerg("vm_start %lx > vm_end %lx\n",
  287. vma->vm_start, vma->vm_end);
  288. bug = 1;
  289. }
  290. spin_lock(&mm->page_table_lock);
  291. if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
  292. pr_emerg("free gap %lx, correct %lx\n",
  293. vma->rb_subtree_gap,
  294. vma_compute_subtree_gap(vma));
  295. bug = 1;
  296. }
  297. spin_unlock(&mm->page_table_lock);
  298. i++;
  299. pn = nd;
  300. prev = vma->vm_start;
  301. pend = vma->vm_end;
  302. }
  303. j = 0;
  304. for (nd = pn; nd; nd = rb_prev(nd))
  305. j++;
  306. if (i != j) {
  307. pr_emerg("backwards %d, forwards %d\n", j, i);
  308. bug = 1;
  309. }
  310. return bug ? -1 : i;
  311. }
  312. static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
  313. {
  314. struct rb_node *nd;
  315. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  316. struct vm_area_struct *vma;
  317. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  318. VM_BUG_ON_VMA(vma != ignore &&
  319. vma->rb_subtree_gap != vma_compute_subtree_gap(vma),
  320. vma);
  321. }
  322. }
  323. static void validate_mm(struct mm_struct *mm)
  324. {
  325. int bug = 0;
  326. int i = 0;
  327. unsigned long highest_address = 0;
  328. struct vm_area_struct *vma = mm->mmap;
  329. while (vma) {
  330. struct anon_vma *anon_vma = vma->anon_vma;
  331. struct anon_vma_chain *avc;
  332. if (anon_vma) {
  333. anon_vma_lock_read(anon_vma);
  334. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  335. anon_vma_interval_tree_verify(avc);
  336. anon_vma_unlock_read(anon_vma);
  337. }
  338. highest_address = vm_end_gap(vma);
  339. vma = vma->vm_next;
  340. i++;
  341. }
  342. if (i != mm->map_count) {
  343. pr_emerg("map_count %d vm_next %d\n", mm->map_count, i);
  344. bug = 1;
  345. }
  346. if (highest_address != mm->highest_vm_end) {
  347. pr_emerg("mm->highest_vm_end %lx, found %lx\n",
  348. mm->highest_vm_end, highest_address);
  349. bug = 1;
  350. }
  351. i = browse_rb(mm);
  352. if (i != mm->map_count) {
  353. if (i != -1)
  354. pr_emerg("map_count %d rb %d\n", mm->map_count, i);
  355. bug = 1;
  356. }
  357. VM_BUG_ON_MM(bug, mm);
  358. }
  359. #else
  360. #define validate_mm_rb(root, ignore) do { } while (0)
  361. #define validate_mm(mm) do { } while (0)
  362. #endif
  363. RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
  364. unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
  365. /*
  366. * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
  367. * vma->vm_prev->vm_end values changed, without modifying the vma's position
  368. * in the rbtree.
  369. */
  370. static void vma_gap_update(struct vm_area_struct *vma)
  371. {
  372. /*
  373. * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
  374. * function that does exacltly what we want.
  375. */
  376. vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
  377. }
  378. static inline void vma_rb_insert(struct vm_area_struct *vma,
  379. struct rb_root *root)
  380. {
  381. /* All rb_subtree_gap values must be consistent prior to insertion */
  382. validate_mm_rb(root, NULL);
  383. rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
  384. }
  385. static void __vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
  386. {
  387. /*
  388. * Note rb_erase_augmented is a fairly large inline function,
  389. * so make sure we instantiate it only once with our desired
  390. * augmented rbtree callbacks.
  391. */
  392. rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
  393. }
  394. static __always_inline void vma_rb_erase_ignore(struct vm_area_struct *vma,
  395. struct rb_root *root,
  396. struct vm_area_struct *ignore)
  397. {
  398. /*
  399. * All rb_subtree_gap values must be consistent prior to erase,
  400. * with the possible exception of the "next" vma being erased if
  401. * next->vm_start was reduced.
  402. */
  403. validate_mm_rb(root, ignore);
  404. __vma_rb_erase(vma, root);
  405. }
  406. static __always_inline void vma_rb_erase(struct vm_area_struct *vma,
  407. struct rb_root *root)
  408. {
  409. /*
  410. * All rb_subtree_gap values must be consistent prior to erase,
  411. * with the possible exception of the vma being erased.
  412. */
  413. validate_mm_rb(root, vma);
  414. __vma_rb_erase(vma, root);
  415. }
  416. /*
  417. * vma has some anon_vma assigned, and is already inserted on that
  418. * anon_vma's interval trees.
  419. *
  420. * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
  421. * vma must be removed from the anon_vma's interval trees using
  422. * anon_vma_interval_tree_pre_update_vma().
  423. *
  424. * After the update, the vma will be reinserted using
  425. * anon_vma_interval_tree_post_update_vma().
  426. *
  427. * The entire update must be protected by exclusive mmap_sem and by
  428. * the root anon_vma's mutex.
  429. */
  430. static inline void
  431. anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
  432. {
  433. struct anon_vma_chain *avc;
  434. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  435. anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
  436. }
  437. static inline void
  438. anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
  439. {
  440. struct anon_vma_chain *avc;
  441. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  442. anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
  443. }
  444. static int find_vma_links(struct mm_struct *mm, unsigned long addr,
  445. unsigned long end, struct vm_area_struct **pprev,
  446. struct rb_node ***rb_link, struct rb_node **rb_parent)
  447. {
  448. struct rb_node **__rb_link, *__rb_parent, *rb_prev;
  449. __rb_link = &mm->mm_rb.rb_node;
  450. rb_prev = __rb_parent = NULL;
  451. while (*__rb_link) {
  452. struct vm_area_struct *vma_tmp;
  453. __rb_parent = *__rb_link;
  454. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  455. if (vma_tmp->vm_end > addr) {
  456. /* Fail if an existing vma overlaps the area */
  457. if (vma_tmp->vm_start < end)
  458. return -ENOMEM;
  459. __rb_link = &__rb_parent->rb_left;
  460. } else {
  461. rb_prev = __rb_parent;
  462. __rb_link = &__rb_parent->rb_right;
  463. }
  464. }
  465. *pprev = NULL;
  466. if (rb_prev)
  467. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  468. *rb_link = __rb_link;
  469. *rb_parent = __rb_parent;
  470. return 0;
  471. }
  472. static unsigned long count_vma_pages_range(struct mm_struct *mm,
  473. unsigned long addr, unsigned long end)
  474. {
  475. unsigned long nr_pages = 0;
  476. struct vm_area_struct *vma;
  477. /* Find first overlaping mapping */
  478. vma = find_vma_intersection(mm, addr, end);
  479. if (!vma)
  480. return 0;
  481. nr_pages = (min(end, vma->vm_end) -
  482. max(addr, vma->vm_start)) >> PAGE_SHIFT;
  483. /* Iterate over the rest of the overlaps */
  484. for (vma = vma->vm_next; vma; vma = vma->vm_next) {
  485. unsigned long overlap_len;
  486. if (vma->vm_start > end)
  487. break;
  488. overlap_len = min(end, vma->vm_end) - vma->vm_start;
  489. nr_pages += overlap_len >> PAGE_SHIFT;
  490. }
  491. return nr_pages;
  492. }
  493. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  494. struct rb_node **rb_link, struct rb_node *rb_parent)
  495. {
  496. /* Update tracking information for the gap following the new vma. */
  497. if (vma->vm_next)
  498. vma_gap_update(vma->vm_next);
  499. else
  500. mm->highest_vm_end = vm_end_gap(vma);
  501. /*
  502. * vma->vm_prev wasn't known when we followed the rbtree to find the
  503. * correct insertion point for that vma. As a result, we could not
  504. * update the vma vm_rb parents rb_subtree_gap values on the way down.
  505. * So, we first insert the vma with a zero rb_subtree_gap value
  506. * (to be consistent with what we did on the way down), and then
  507. * immediately update the gap to the correct value. Finally we
  508. * rebalance the rbtree after all augmented values have been set.
  509. */
  510. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  511. vma->rb_subtree_gap = 0;
  512. vma_gap_update(vma);
  513. vma_rb_insert(vma, &mm->mm_rb);
  514. }
  515. static void __vma_link_file(struct vm_area_struct *vma)
  516. {
  517. struct file *file;
  518. file = vma->vm_file;
  519. if (file) {
  520. struct address_space *mapping = file->f_mapping;
  521. if (vma->vm_flags & VM_DENYWRITE)
  522. atomic_dec(&file_inode(file)->i_writecount);
  523. if (vma->vm_flags & VM_SHARED)
  524. atomic_inc(&mapping->i_mmap_writable);
  525. flush_dcache_mmap_lock(mapping);
  526. vma_interval_tree_insert(vma, &mapping->i_mmap);
  527. flush_dcache_mmap_unlock(mapping);
  528. }
  529. }
  530. static void
  531. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  532. struct vm_area_struct *prev, struct rb_node **rb_link,
  533. struct rb_node *rb_parent)
  534. {
  535. __vma_link_list(mm, vma, prev, rb_parent);
  536. __vma_link_rb(mm, vma, rb_link, rb_parent);
  537. }
  538. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  539. struct vm_area_struct *prev, struct rb_node **rb_link,
  540. struct rb_node *rb_parent)
  541. {
  542. struct address_space *mapping = NULL;
  543. if (vma->vm_file) {
  544. mapping = vma->vm_file->f_mapping;
  545. i_mmap_lock_write(mapping);
  546. }
  547. __vma_link(mm, vma, prev, rb_link, rb_parent);
  548. __vma_link_file(vma);
  549. if (mapping)
  550. i_mmap_unlock_write(mapping);
  551. mm->map_count++;
  552. validate_mm(mm);
  553. }
  554. /*
  555. * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
  556. * mm's list and rbtree. It has already been inserted into the interval tree.
  557. */
  558. static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  559. {
  560. struct vm_area_struct *prev;
  561. struct rb_node **rb_link, *rb_parent;
  562. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  563. &prev, &rb_link, &rb_parent))
  564. BUG();
  565. __vma_link(mm, vma, prev, rb_link, rb_parent);
  566. mm->map_count++;
  567. }
  568. static __always_inline void __vma_unlink_common(struct mm_struct *mm,
  569. struct vm_area_struct *vma,
  570. struct vm_area_struct *prev,
  571. bool has_prev,
  572. struct vm_area_struct *ignore)
  573. {
  574. struct vm_area_struct *next;
  575. vma_rb_erase_ignore(vma, &mm->mm_rb, ignore);
  576. next = vma->vm_next;
  577. if (has_prev)
  578. prev->vm_next = next;
  579. else {
  580. prev = vma->vm_prev;
  581. if (prev)
  582. prev->vm_next = next;
  583. else
  584. mm->mmap = next;
  585. }
  586. if (next)
  587. next->vm_prev = prev;
  588. /* Kill the cache */
  589. vmacache_invalidate(mm);
  590. }
  591. static inline void __vma_unlink_prev(struct mm_struct *mm,
  592. struct vm_area_struct *vma,
  593. struct vm_area_struct *prev)
  594. {
  595. __vma_unlink_common(mm, vma, prev, true, vma);
  596. }
  597. /*
  598. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  599. * is already present in an i_mmap tree without adjusting the tree.
  600. * The following helper function should be used when such adjustments
  601. * are necessary. The "insert" vma (if any) is to be inserted
  602. * before we drop the necessary locks.
  603. */
  604. int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
  605. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
  606. struct vm_area_struct *expand)
  607. {
  608. struct mm_struct *mm = vma->vm_mm;
  609. struct vm_area_struct *next = vma->vm_next, *orig_vma = vma;
  610. struct address_space *mapping = NULL;
  611. struct rb_root_cached *root = NULL;
  612. struct anon_vma *anon_vma = NULL;
  613. struct file *file = vma->vm_file;
  614. bool start_changed = false, end_changed = false;
  615. long adjust_next = 0;
  616. int remove_next = 0;
  617. if (next && !insert) {
  618. struct vm_area_struct *exporter = NULL, *importer = NULL;
  619. if (end >= next->vm_end) {
  620. /*
  621. * vma expands, overlapping all the next, and
  622. * perhaps the one after too (mprotect case 6).
  623. * The only other cases that gets here are
  624. * case 1, case 7 and case 8.
  625. */
  626. if (next == expand) {
  627. /*
  628. * The only case where we don't expand "vma"
  629. * and we expand "next" instead is case 8.
  630. */
  631. VM_WARN_ON(end != next->vm_end);
  632. /*
  633. * remove_next == 3 means we're
  634. * removing "vma" and that to do so we
  635. * swapped "vma" and "next".
  636. */
  637. remove_next = 3;
  638. VM_WARN_ON(file != next->vm_file);
  639. swap(vma, next);
  640. } else {
  641. VM_WARN_ON(expand != vma);
  642. /*
  643. * case 1, 6, 7, remove_next == 2 is case 6,
  644. * remove_next == 1 is case 1 or 7.
  645. */
  646. remove_next = 1 + (end > next->vm_end);
  647. VM_WARN_ON(remove_next == 2 &&
  648. end != next->vm_next->vm_end);
  649. VM_WARN_ON(remove_next == 1 &&
  650. end != next->vm_end);
  651. /* trim end to next, for case 6 first pass */
  652. end = next->vm_end;
  653. }
  654. exporter = next;
  655. importer = vma;
  656. /*
  657. * If next doesn't have anon_vma, import from vma after
  658. * next, if the vma overlaps with it.
  659. */
  660. if (remove_next == 2 && !next->anon_vma)
  661. exporter = next->vm_next;
  662. } else if (end > next->vm_start) {
  663. /*
  664. * vma expands, overlapping part of the next:
  665. * mprotect case 5 shifting the boundary up.
  666. */
  667. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  668. exporter = next;
  669. importer = vma;
  670. VM_WARN_ON(expand != importer);
  671. } else if (end < vma->vm_end) {
  672. /*
  673. * vma shrinks, and !insert tells it's not
  674. * split_vma inserting another: so it must be
  675. * mprotect case 4 shifting the boundary down.
  676. */
  677. adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT);
  678. exporter = vma;
  679. importer = next;
  680. VM_WARN_ON(expand != importer);
  681. }
  682. /*
  683. * Easily overlooked: when mprotect shifts the boundary,
  684. * make sure the expanding vma has anon_vma set if the
  685. * shrinking vma had, to cover any anon pages imported.
  686. */
  687. if (exporter && exporter->anon_vma && !importer->anon_vma) {
  688. int error;
  689. importer->anon_vma = exporter->anon_vma;
  690. error = anon_vma_clone(importer, exporter);
  691. if (error)
  692. return error;
  693. }
  694. }
  695. again:
  696. vma_adjust_trans_huge(orig_vma, start, end, adjust_next);
  697. if (file) {
  698. mapping = file->f_mapping;
  699. root = &mapping->i_mmap;
  700. uprobe_munmap(vma, vma->vm_start, vma->vm_end);
  701. if (adjust_next)
  702. uprobe_munmap(next, next->vm_start, next->vm_end);
  703. i_mmap_lock_write(mapping);
  704. if (insert) {
  705. /*
  706. * Put into interval tree now, so instantiated pages
  707. * are visible to arm/parisc __flush_dcache_page
  708. * throughout; but we cannot insert into address
  709. * space until vma start or end is updated.
  710. */
  711. __vma_link_file(insert);
  712. }
  713. }
  714. anon_vma = vma->anon_vma;
  715. if (!anon_vma && adjust_next)
  716. anon_vma = next->anon_vma;
  717. if (anon_vma) {
  718. VM_WARN_ON(adjust_next && next->anon_vma &&
  719. anon_vma != next->anon_vma);
  720. anon_vma_lock_write(anon_vma);
  721. anon_vma_interval_tree_pre_update_vma(vma);
  722. if (adjust_next)
  723. anon_vma_interval_tree_pre_update_vma(next);
  724. }
  725. if (root) {
  726. flush_dcache_mmap_lock(mapping);
  727. vma_interval_tree_remove(vma, root);
  728. if (adjust_next)
  729. vma_interval_tree_remove(next, root);
  730. }
  731. if (start != vma->vm_start) {
  732. vma->vm_start = start;
  733. start_changed = true;
  734. }
  735. if (end != vma->vm_end) {
  736. vma->vm_end = end;
  737. end_changed = true;
  738. }
  739. vma->vm_pgoff = pgoff;
  740. if (adjust_next) {
  741. next->vm_start += adjust_next << PAGE_SHIFT;
  742. next->vm_pgoff += adjust_next;
  743. }
  744. if (root) {
  745. if (adjust_next)
  746. vma_interval_tree_insert(next, root);
  747. vma_interval_tree_insert(vma, root);
  748. flush_dcache_mmap_unlock(mapping);
  749. }
  750. if (remove_next) {
  751. /*
  752. * vma_merge has merged next into vma, and needs
  753. * us to remove next before dropping the locks.
  754. */
  755. if (remove_next != 3)
  756. __vma_unlink_prev(mm, next, vma);
  757. else
  758. /*
  759. * vma is not before next if they've been
  760. * swapped.
  761. *
  762. * pre-swap() next->vm_start was reduced so
  763. * tell validate_mm_rb to ignore pre-swap()
  764. * "next" (which is stored in post-swap()
  765. * "vma").
  766. */
  767. __vma_unlink_common(mm, next, NULL, false, vma);
  768. if (file)
  769. __remove_shared_vm_struct(next, file, mapping);
  770. } else if (insert) {
  771. /*
  772. * split_vma has split insert from vma, and needs
  773. * us to insert it before dropping the locks
  774. * (it may either follow vma or precede it).
  775. */
  776. __insert_vm_struct(mm, insert);
  777. } else {
  778. if (start_changed)
  779. vma_gap_update(vma);
  780. if (end_changed) {
  781. if (!next)
  782. mm->highest_vm_end = vm_end_gap(vma);
  783. else if (!adjust_next)
  784. vma_gap_update(next);
  785. }
  786. }
  787. if (anon_vma) {
  788. anon_vma_interval_tree_post_update_vma(vma);
  789. if (adjust_next)
  790. anon_vma_interval_tree_post_update_vma(next);
  791. anon_vma_unlock_write(anon_vma);
  792. }
  793. if (mapping)
  794. i_mmap_unlock_write(mapping);
  795. if (root) {
  796. uprobe_mmap(vma);
  797. if (adjust_next)
  798. uprobe_mmap(next);
  799. }
  800. if (remove_next) {
  801. if (file) {
  802. uprobe_munmap(next, next->vm_start, next->vm_end);
  803. fput(file);
  804. }
  805. if (next->anon_vma)
  806. anon_vma_merge(vma, next);
  807. mm->map_count--;
  808. mpol_put(vma_policy(next));
  809. kmem_cache_free(vm_area_cachep, next);
  810. /*
  811. * In mprotect's case 6 (see comments on vma_merge),
  812. * we must remove another next too. It would clutter
  813. * up the code too much to do both in one go.
  814. */
  815. if (remove_next != 3) {
  816. /*
  817. * If "next" was removed and vma->vm_end was
  818. * expanded (up) over it, in turn
  819. * "next->vm_prev->vm_end" changed and the
  820. * "vma->vm_next" gap must be updated.
  821. */
  822. next = vma->vm_next;
  823. } else {
  824. /*
  825. * For the scope of the comment "next" and
  826. * "vma" considered pre-swap(): if "vma" was
  827. * removed, next->vm_start was expanded (down)
  828. * over it and the "next" gap must be updated.
  829. * Because of the swap() the post-swap() "vma"
  830. * actually points to pre-swap() "next"
  831. * (post-swap() "next" as opposed is now a
  832. * dangling pointer).
  833. */
  834. next = vma;
  835. }
  836. if (remove_next == 2) {
  837. remove_next = 1;
  838. end = next->vm_end;
  839. goto again;
  840. }
  841. else if (next)
  842. vma_gap_update(next);
  843. else {
  844. /*
  845. * If remove_next == 2 we obviously can't
  846. * reach this path.
  847. *
  848. * If remove_next == 3 we can't reach this
  849. * path because pre-swap() next is always not
  850. * NULL. pre-swap() "next" is not being
  851. * removed and its next->vm_end is not altered
  852. * (and furthermore "end" already matches
  853. * next->vm_end in remove_next == 3).
  854. *
  855. * We reach this only in the remove_next == 1
  856. * case if the "next" vma that was removed was
  857. * the highest vma of the mm. However in such
  858. * case next->vm_end == "end" and the extended
  859. * "vma" has vma->vm_end == next->vm_end so
  860. * mm->highest_vm_end doesn't need any update
  861. * in remove_next == 1 case.
  862. */
  863. VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma));
  864. }
  865. }
  866. if (insert && file)
  867. uprobe_mmap(insert);
  868. validate_mm(mm);
  869. return 0;
  870. }
  871. /*
  872. * If the vma has a ->close operation then the driver probably needs to release
  873. * per-vma resources, so we don't attempt to merge those.
  874. */
  875. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  876. struct file *file, unsigned long vm_flags,
  877. struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
  878. {
  879. /*
  880. * VM_SOFTDIRTY should not prevent from VMA merging, if we
  881. * match the flags but dirty bit -- the caller should mark
  882. * merged VMA as dirty. If dirty bit won't be excluded from
  883. * comparison, we increase pressue on the memory system forcing
  884. * the kernel to generate new VMAs when old one could be
  885. * extended instead.
  886. */
  887. if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
  888. return 0;
  889. if (vma->vm_file != file)
  890. return 0;
  891. if (vma->vm_ops && vma->vm_ops->close)
  892. return 0;
  893. if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
  894. return 0;
  895. return 1;
  896. }
  897. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  898. struct anon_vma *anon_vma2,
  899. struct vm_area_struct *vma)
  900. {
  901. /*
  902. * The list_is_singular() test is to avoid merging VMA cloned from
  903. * parents. This can improve scalability caused by anon_vma lock.
  904. */
  905. if ((!anon_vma1 || !anon_vma2) && (!vma ||
  906. list_is_singular(&vma->anon_vma_chain)))
  907. return 1;
  908. return anon_vma1 == anon_vma2;
  909. }
  910. /*
  911. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  912. * in front of (at a lower virtual address and file offset than) the vma.
  913. *
  914. * We cannot merge two vmas if they have differently assigned (non-NULL)
  915. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  916. *
  917. * We don't check here for the merged mmap wrapping around the end of pagecache
  918. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  919. * wrap, nor mmaps which cover the final page at index -1UL.
  920. */
  921. static int
  922. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  923. struct anon_vma *anon_vma, struct file *file,
  924. pgoff_t vm_pgoff,
  925. struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
  926. {
  927. if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
  928. is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
  929. if (vma->vm_pgoff == vm_pgoff)
  930. return 1;
  931. }
  932. return 0;
  933. }
  934. /*
  935. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  936. * beyond (at a higher virtual address and file offset than) the vma.
  937. *
  938. * We cannot merge two vmas if they have differently assigned (non-NULL)
  939. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  940. */
  941. static int
  942. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  943. struct anon_vma *anon_vma, struct file *file,
  944. pgoff_t vm_pgoff,
  945. struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
  946. {
  947. if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
  948. is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
  949. pgoff_t vm_pglen;
  950. vm_pglen = vma_pages(vma);
  951. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  952. return 1;
  953. }
  954. return 0;
  955. }
  956. /*
  957. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  958. * whether that can be merged with its predecessor or its successor.
  959. * Or both (it neatly fills a hole).
  960. *
  961. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  962. * certain not to be mapped by the time vma_merge is called; but when
  963. * called for mprotect, it is certain to be already mapped (either at
  964. * an offset within prev, or at the start of next), and the flags of
  965. * this area are about to be changed to vm_flags - and the no-change
  966. * case has already been eliminated.
  967. *
  968. * The following mprotect cases have to be considered, where AAAA is
  969. * the area passed down from mprotect_fixup, never extending beyond one
  970. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  971. *
  972. * AAAA AAAA AAAA AAAA
  973. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  974. * cannot merge might become might become might become
  975. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  976. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  977. * mremap move: PPPPXXXXXXXX 8
  978. * AAAA
  979. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  980. * might become case 1 below case 2 below case 3 below
  981. *
  982. * It is important for case 8 that the the vma NNNN overlapping the
  983. * region AAAA is never going to extended over XXXX. Instead XXXX must
  984. * be extended in region AAAA and NNNN must be removed. This way in
  985. * all cases where vma_merge succeeds, the moment vma_adjust drops the
  986. * rmap_locks, the properties of the merged vma will be already
  987. * correct for the whole merged range. Some of those properties like
  988. * vm_page_prot/vm_flags may be accessed by rmap_walks and they must
  989. * be correct for the whole merged range immediately after the
  990. * rmap_locks are released. Otherwise if XXXX would be removed and
  991. * NNNN would be extended over the XXXX range, remove_migration_ptes
  992. * or other rmap walkers (if working on addresses beyond the "end"
  993. * parameter) may establish ptes with the wrong permissions of NNNN
  994. * instead of the right permissions of XXXX.
  995. */
  996. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  997. struct vm_area_struct *prev, unsigned long addr,
  998. unsigned long end, unsigned long vm_flags,
  999. struct anon_vma *anon_vma, struct file *file,
  1000. pgoff_t pgoff, struct mempolicy *policy,
  1001. struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
  1002. {
  1003. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  1004. struct vm_area_struct *area, *next;
  1005. int err;
  1006. /*
  1007. * We later require that vma->vm_flags == vm_flags,
  1008. * so this tests vma->vm_flags & VM_SPECIAL, too.
  1009. */
  1010. if (vm_flags & VM_SPECIAL)
  1011. return NULL;
  1012. if (prev)
  1013. next = prev->vm_next;
  1014. else
  1015. next = mm->mmap;
  1016. area = next;
  1017. if (area && area->vm_end == end) /* cases 6, 7, 8 */
  1018. next = next->vm_next;
  1019. /* verify some invariant that must be enforced by the caller */
  1020. VM_WARN_ON(prev && addr <= prev->vm_start);
  1021. VM_WARN_ON(area && end > area->vm_end);
  1022. VM_WARN_ON(addr >= end);
  1023. /*
  1024. * Can it merge with the predecessor?
  1025. */
  1026. if (prev && prev->vm_end == addr &&
  1027. mpol_equal(vma_policy(prev), policy) &&
  1028. can_vma_merge_after(prev, vm_flags,
  1029. anon_vma, file, pgoff,
  1030. vm_userfaultfd_ctx)) {
  1031. /*
  1032. * OK, it can. Can we now merge in the successor as well?
  1033. */
  1034. if (next && end == next->vm_start &&
  1035. mpol_equal(policy, vma_policy(next)) &&
  1036. can_vma_merge_before(next, vm_flags,
  1037. anon_vma, file,
  1038. pgoff+pglen,
  1039. vm_userfaultfd_ctx) &&
  1040. is_mergeable_anon_vma(prev->anon_vma,
  1041. next->anon_vma, NULL)) {
  1042. /* cases 1, 6 */
  1043. err = __vma_adjust(prev, prev->vm_start,
  1044. next->vm_end, prev->vm_pgoff, NULL,
  1045. prev);
  1046. } else /* cases 2, 5, 7 */
  1047. err = __vma_adjust(prev, prev->vm_start,
  1048. end, prev->vm_pgoff, NULL, prev);
  1049. if (err)
  1050. return NULL;
  1051. khugepaged_enter_vma_merge(prev, vm_flags);
  1052. return prev;
  1053. }
  1054. /*
  1055. * Can this new request be merged in front of next?
  1056. */
  1057. if (next && end == next->vm_start &&
  1058. mpol_equal(policy, vma_policy(next)) &&
  1059. can_vma_merge_before(next, vm_flags,
  1060. anon_vma, file, pgoff+pglen,
  1061. vm_userfaultfd_ctx)) {
  1062. if (prev && addr < prev->vm_end) /* case 4 */
  1063. err = __vma_adjust(prev, prev->vm_start,
  1064. addr, prev->vm_pgoff, NULL, next);
  1065. else { /* cases 3, 8 */
  1066. err = __vma_adjust(area, addr, next->vm_end,
  1067. next->vm_pgoff - pglen, NULL, next);
  1068. /*
  1069. * In case 3 area is already equal to next and
  1070. * this is a noop, but in case 8 "area" has
  1071. * been removed and next was expanded over it.
  1072. */
  1073. area = next;
  1074. }
  1075. if (err)
  1076. return NULL;
  1077. khugepaged_enter_vma_merge(area, vm_flags);
  1078. return area;
  1079. }
  1080. return NULL;
  1081. }
  1082. /*
  1083. * Rough compatbility check to quickly see if it's even worth looking
  1084. * at sharing an anon_vma.
  1085. *
  1086. * They need to have the same vm_file, and the flags can only differ
  1087. * in things that mprotect may change.
  1088. *
  1089. * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
  1090. * we can merge the two vma's. For example, we refuse to merge a vma if
  1091. * there is a vm_ops->close() function, because that indicates that the
  1092. * driver is doing some kind of reference counting. But that doesn't
  1093. * really matter for the anon_vma sharing case.
  1094. */
  1095. static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
  1096. {
  1097. return a->vm_end == b->vm_start &&
  1098. mpol_equal(vma_policy(a), vma_policy(b)) &&
  1099. a->vm_file == b->vm_file &&
  1100. !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
  1101. b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
  1102. }
  1103. /*
  1104. * Do some basic sanity checking to see if we can re-use the anon_vma
  1105. * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
  1106. * the same as 'old', the other will be the new one that is trying
  1107. * to share the anon_vma.
  1108. *
  1109. * NOTE! This runs with mm_sem held for reading, so it is possible that
  1110. * the anon_vma of 'old' is concurrently in the process of being set up
  1111. * by another page fault trying to merge _that_. But that's ok: if it
  1112. * is being set up, that automatically means that it will be a singleton
  1113. * acceptable for merging, so we can do all of this optimistically. But
  1114. * we do that READ_ONCE() to make sure that we never re-load the pointer.
  1115. *
  1116. * IOW: that the "list_is_singular()" test on the anon_vma_chain only
  1117. * matters for the 'stable anon_vma' case (ie the thing we want to avoid
  1118. * is to return an anon_vma that is "complex" due to having gone through
  1119. * a fork).
  1120. *
  1121. * We also make sure that the two vma's are compatible (adjacent,
  1122. * and with the same memory policies). That's all stable, even with just
  1123. * a read lock on the mm_sem.
  1124. */
  1125. static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
  1126. {
  1127. if (anon_vma_compatible(a, b)) {
  1128. struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
  1129. if (anon_vma && list_is_singular(&old->anon_vma_chain))
  1130. return anon_vma;
  1131. }
  1132. return NULL;
  1133. }
  1134. /*
  1135. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  1136. * neighbouring vmas for a suitable anon_vma, before it goes off
  1137. * to allocate a new anon_vma. It checks because a repetitive
  1138. * sequence of mprotects and faults may otherwise lead to distinct
  1139. * anon_vmas being allocated, preventing vma merge in subsequent
  1140. * mprotect.
  1141. */
  1142. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  1143. {
  1144. struct anon_vma *anon_vma;
  1145. struct vm_area_struct *near;
  1146. near = vma->vm_next;
  1147. if (!near)
  1148. goto try_prev;
  1149. anon_vma = reusable_anon_vma(near, vma, near);
  1150. if (anon_vma)
  1151. return anon_vma;
  1152. try_prev:
  1153. near = vma->vm_prev;
  1154. if (!near)
  1155. goto none;
  1156. anon_vma = reusable_anon_vma(near, near, vma);
  1157. if (anon_vma)
  1158. return anon_vma;
  1159. none:
  1160. /*
  1161. * There's no absolute need to look only at touching neighbours:
  1162. * we could search further afield for "compatible" anon_vmas.
  1163. * But it would probably just be a waste of time searching,
  1164. * or lead to too many vmas hanging off the same anon_vma.
  1165. * We're trying to allow mprotect remerging later on,
  1166. * not trying to minimize memory used for anon_vmas.
  1167. */
  1168. return NULL;
  1169. }
  1170. /*
  1171. * If a hint addr is less than mmap_min_addr change hint to be as
  1172. * low as possible but still greater than mmap_min_addr
  1173. */
  1174. static inline unsigned long round_hint_to_min(unsigned long hint)
  1175. {
  1176. hint &= PAGE_MASK;
  1177. if (((void *)hint != NULL) &&
  1178. (hint < mmap_min_addr))
  1179. return PAGE_ALIGN(mmap_min_addr);
  1180. return hint;
  1181. }
  1182. static inline int mlock_future_check(struct mm_struct *mm,
  1183. unsigned long flags,
  1184. unsigned long len)
  1185. {
  1186. unsigned long locked, lock_limit;
  1187. /* mlock MCL_FUTURE? */
  1188. if (flags & VM_LOCKED) {
  1189. locked = len >> PAGE_SHIFT;
  1190. locked += mm->locked_vm;
  1191. lock_limit = rlimit(RLIMIT_MEMLOCK);
  1192. lock_limit >>= PAGE_SHIFT;
  1193. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1194. return -EAGAIN;
  1195. }
  1196. return 0;
  1197. }
  1198. static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
  1199. {
  1200. if (S_ISREG(inode->i_mode))
  1201. return MAX_LFS_FILESIZE;
  1202. if (S_ISBLK(inode->i_mode))
  1203. return MAX_LFS_FILESIZE;
  1204. /* Special "we do even unsigned file positions" case */
  1205. if (file->f_mode & FMODE_UNSIGNED_OFFSET)
  1206. return 0;
  1207. /* Yes, random drivers might want more. But I'm tired of buggy drivers */
  1208. return ULONG_MAX;
  1209. }
  1210. static inline bool file_mmap_ok(struct file *file, struct inode *inode,
  1211. unsigned long pgoff, unsigned long len)
  1212. {
  1213. u64 maxsize = file_mmap_size_max(file, inode);
  1214. if (maxsize && len > maxsize)
  1215. return false;
  1216. maxsize -= len;
  1217. if (pgoff > maxsize >> PAGE_SHIFT)
  1218. return false;
  1219. return true;
  1220. }
  1221. /*
  1222. * The caller must hold down_write(&current->mm->mmap_sem).
  1223. */
  1224. unsigned long do_mmap(struct file *file, unsigned long addr,
  1225. unsigned long len, unsigned long prot,
  1226. unsigned long flags, vm_flags_t vm_flags,
  1227. unsigned long pgoff, unsigned long *populate,
  1228. struct list_head *uf)
  1229. {
  1230. struct mm_struct *mm = current->mm;
  1231. int pkey = 0;
  1232. *populate = 0;
  1233. if (!len)
  1234. return -EINVAL;
  1235. /*
  1236. * Does the application expect PROT_READ to imply PROT_EXEC?
  1237. *
  1238. * (the exception is when the underlying filesystem is noexec
  1239. * mounted, in which case we dont add PROT_EXEC.)
  1240. */
  1241. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  1242. if (!(file && path_noexec(&file->f_path)))
  1243. prot |= PROT_EXEC;
  1244. /* force arch specific MAP_FIXED handling in get_unmapped_area */
  1245. if (flags & MAP_FIXED_NOREPLACE)
  1246. flags |= MAP_FIXED;
  1247. if (!(flags & MAP_FIXED))
  1248. addr = round_hint_to_min(addr);
  1249. /* Careful about overflows.. */
  1250. len = PAGE_ALIGN(len);
  1251. if (!len)
  1252. return -ENOMEM;
  1253. /* offset overflow? */
  1254. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  1255. return -EOVERFLOW;
  1256. /* Too many mappings? */
  1257. if (mm->map_count > sysctl_max_map_count)
  1258. return -ENOMEM;
  1259. /* Obtain the address to map to. we verify (or select) it and ensure
  1260. * that it represents a valid section of the address space.
  1261. */
  1262. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  1263. if (offset_in_page(addr))
  1264. return addr;
  1265. if (flags & MAP_FIXED_NOREPLACE) {
  1266. struct vm_area_struct *vma = find_vma(mm, addr);
  1267. if (vma && vma->vm_start <= addr)
  1268. return -EEXIST;
  1269. }
  1270. if (prot == PROT_EXEC) {
  1271. pkey = execute_only_pkey(mm);
  1272. if (pkey < 0)
  1273. pkey = 0;
  1274. }
  1275. /* Do simple checking here so the lower-level routines won't have
  1276. * to. we assume access permissions have been handled by the open
  1277. * of the memory object, so we don't do any here.
  1278. */
  1279. vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
  1280. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  1281. if (flags & MAP_LOCKED)
  1282. if (!can_do_mlock())
  1283. return -EPERM;
  1284. if (mlock_future_check(mm, vm_flags, len))
  1285. return -EAGAIN;
  1286. if (file) {
  1287. struct inode *inode = file_inode(file);
  1288. unsigned long flags_mask;
  1289. if (!file_mmap_ok(file, inode, pgoff, len))
  1290. return -EOVERFLOW;
  1291. flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags;
  1292. switch (flags & MAP_TYPE) {
  1293. case MAP_SHARED:
  1294. /*
  1295. * Force use of MAP_SHARED_VALIDATE with non-legacy
  1296. * flags. E.g. MAP_SYNC is dangerous to use with
  1297. * MAP_SHARED as you don't know which consistency model
  1298. * you will get. We silently ignore unsupported flags
  1299. * with MAP_SHARED to preserve backward compatibility.
  1300. */
  1301. flags &= LEGACY_MAP_MASK;
  1302. /* fall through */
  1303. case MAP_SHARED_VALIDATE:
  1304. if (flags & ~flags_mask)
  1305. return -EOPNOTSUPP;
  1306. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  1307. return -EACCES;
  1308. /*
  1309. * Make sure we don't allow writing to an append-only
  1310. * file..
  1311. */
  1312. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  1313. return -EACCES;
  1314. /*
  1315. * Make sure there are no mandatory locks on the file.
  1316. */
  1317. if (locks_verify_locked(file))
  1318. return -EAGAIN;
  1319. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1320. if (!(file->f_mode & FMODE_WRITE))
  1321. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  1322. /* fall through */
  1323. case MAP_PRIVATE:
  1324. if (!(file->f_mode & FMODE_READ))
  1325. return -EACCES;
  1326. if (path_noexec(&file->f_path)) {
  1327. if (vm_flags & VM_EXEC)
  1328. return -EPERM;
  1329. vm_flags &= ~VM_MAYEXEC;
  1330. }
  1331. if (!file->f_op->mmap)
  1332. return -ENODEV;
  1333. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1334. return -EINVAL;
  1335. break;
  1336. default:
  1337. return -EINVAL;
  1338. }
  1339. } else {
  1340. switch (flags & MAP_TYPE) {
  1341. case MAP_SHARED:
  1342. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1343. return -EINVAL;
  1344. /*
  1345. * Ignore pgoff.
  1346. */
  1347. pgoff = 0;
  1348. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1349. break;
  1350. case MAP_PRIVATE:
  1351. /*
  1352. * Set pgoff according to addr for anon_vma.
  1353. */
  1354. pgoff = addr >> PAGE_SHIFT;
  1355. break;
  1356. default:
  1357. return -EINVAL;
  1358. }
  1359. }
  1360. /*
  1361. * Set 'VM_NORESERVE' if we should not account for the
  1362. * memory use of this mapping.
  1363. */
  1364. if (flags & MAP_NORESERVE) {
  1365. /* We honor MAP_NORESERVE if allowed to overcommit */
  1366. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  1367. vm_flags |= VM_NORESERVE;
  1368. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  1369. if (file && is_file_hugepages(file))
  1370. vm_flags |= VM_NORESERVE;
  1371. }
  1372. addr = mmap_region(file, addr, len, vm_flags, pgoff, uf);
  1373. if (!IS_ERR_VALUE(addr) &&
  1374. ((vm_flags & VM_LOCKED) ||
  1375. (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
  1376. *populate = len;
  1377. return addr;
  1378. }
  1379. unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
  1380. unsigned long prot, unsigned long flags,
  1381. unsigned long fd, unsigned long pgoff)
  1382. {
  1383. struct file *file = NULL;
  1384. unsigned long retval;
  1385. if (!(flags & MAP_ANONYMOUS)) {
  1386. audit_mmap_fd(fd, flags);
  1387. file = fget(fd);
  1388. if (!file)
  1389. return -EBADF;
  1390. if (is_file_hugepages(file))
  1391. len = ALIGN(len, huge_page_size(hstate_file(file)));
  1392. retval = -EINVAL;
  1393. if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
  1394. goto out_fput;
  1395. } else if (flags & MAP_HUGETLB) {
  1396. struct user_struct *user = NULL;
  1397. struct hstate *hs;
  1398. hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  1399. if (!hs)
  1400. return -EINVAL;
  1401. len = ALIGN(len, huge_page_size(hs));
  1402. /*
  1403. * VM_NORESERVE is used because the reservations will be
  1404. * taken when vm_ops->mmap() is called
  1405. * A dummy user value is used because we are not locking
  1406. * memory so no accounting is necessary
  1407. */
  1408. file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
  1409. VM_NORESERVE,
  1410. &user, HUGETLB_ANONHUGE_INODE,
  1411. (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  1412. if (IS_ERR(file))
  1413. return PTR_ERR(file);
  1414. }
  1415. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1416. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1417. out_fput:
  1418. if (file)
  1419. fput(file);
  1420. return retval;
  1421. }
  1422. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1423. unsigned long, prot, unsigned long, flags,
  1424. unsigned long, fd, unsigned long, pgoff)
  1425. {
  1426. return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
  1427. }
  1428. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1429. struct mmap_arg_struct {
  1430. unsigned long addr;
  1431. unsigned long len;
  1432. unsigned long prot;
  1433. unsigned long flags;
  1434. unsigned long fd;
  1435. unsigned long offset;
  1436. };
  1437. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1438. {
  1439. struct mmap_arg_struct a;
  1440. if (copy_from_user(&a, arg, sizeof(a)))
  1441. return -EFAULT;
  1442. if (offset_in_page(a.offset))
  1443. return -EINVAL;
  1444. return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1445. a.offset >> PAGE_SHIFT);
  1446. }
  1447. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1448. /*
  1449. * Some shared mappigns will want the pages marked read-only
  1450. * to track write events. If so, we'll downgrade vm_page_prot
  1451. * to the private version (using protection_map[] without the
  1452. * VM_SHARED bit).
  1453. */
  1454. int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
  1455. {
  1456. vm_flags_t vm_flags = vma->vm_flags;
  1457. const struct vm_operations_struct *vm_ops = vma->vm_ops;
  1458. /* If it was private or non-writable, the write bit is already clear */
  1459. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  1460. return 0;
  1461. /* The backer wishes to know when pages are first written to? */
  1462. if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))
  1463. return 1;
  1464. /* The open routine did something to the protections that pgprot_modify
  1465. * won't preserve? */
  1466. if (pgprot_val(vm_page_prot) !=
  1467. pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags)))
  1468. return 0;
  1469. /* Do we need to track softdirty? */
  1470. if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
  1471. return 1;
  1472. /* Specialty mapping? */
  1473. if (vm_flags & VM_PFNMAP)
  1474. return 0;
  1475. /* Can the mapping track the dirty pages? */
  1476. return vma->vm_file && vma->vm_file->f_mapping &&
  1477. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  1478. }
  1479. /*
  1480. * We account for memory if it's a private writeable mapping,
  1481. * not hugepages and VM_NORESERVE wasn't set.
  1482. */
  1483. static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
  1484. {
  1485. /*
  1486. * hugetlb has its own accounting separate from the core VM
  1487. * VM_HUGETLB may not be set yet so we cannot check for that flag.
  1488. */
  1489. if (file && is_file_hugepages(file))
  1490. return 0;
  1491. return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
  1492. }
  1493. unsigned long mmap_region(struct file *file, unsigned long addr,
  1494. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
  1495. struct list_head *uf)
  1496. {
  1497. struct mm_struct *mm = current->mm;
  1498. struct vm_area_struct *vma, *prev;
  1499. int error;
  1500. struct rb_node **rb_link, *rb_parent;
  1501. unsigned long charged = 0;
  1502. /* Check against address space limit. */
  1503. if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
  1504. unsigned long nr_pages;
  1505. /*
  1506. * MAP_FIXED may remove pages of mappings that intersects with
  1507. * requested mapping. Account for the pages it would unmap.
  1508. */
  1509. nr_pages = count_vma_pages_range(mm, addr, addr + len);
  1510. if (!may_expand_vm(mm, vm_flags,
  1511. (len >> PAGE_SHIFT) - nr_pages))
  1512. return -ENOMEM;
  1513. }
  1514. /* Clear old maps */
  1515. while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
  1516. &rb_parent)) {
  1517. if (do_munmap(mm, addr, len, uf))
  1518. return -ENOMEM;
  1519. }
  1520. /*
  1521. * Private writable mapping: check memory availability
  1522. */
  1523. if (accountable_mapping(file, vm_flags)) {
  1524. charged = len >> PAGE_SHIFT;
  1525. if (security_vm_enough_memory_mm(mm, charged))
  1526. return -ENOMEM;
  1527. vm_flags |= VM_ACCOUNT;
  1528. }
  1529. /*
  1530. * Can we just expand an old mapping?
  1531. */
  1532. vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
  1533. NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX);
  1534. if (vma)
  1535. goto out;
  1536. /*
  1537. * Determine the object being mapped and call the appropriate
  1538. * specific mapper. the address has already been validated, but
  1539. * not unmapped, but the maps are removed from the list.
  1540. */
  1541. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1542. if (!vma) {
  1543. error = -ENOMEM;
  1544. goto unacct_error;
  1545. }
  1546. vma->vm_mm = mm;
  1547. vma->vm_start = addr;
  1548. vma->vm_end = addr + len;
  1549. vma->vm_flags = vm_flags;
  1550. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1551. vma->vm_pgoff = pgoff;
  1552. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1553. if (file) {
  1554. if (vm_flags & VM_DENYWRITE) {
  1555. error = deny_write_access(file);
  1556. if (error)
  1557. goto free_vma;
  1558. }
  1559. if (vm_flags & VM_SHARED) {
  1560. error = mapping_map_writable(file->f_mapping);
  1561. if (error)
  1562. goto allow_write_and_free_vma;
  1563. }
  1564. /* ->mmap() can change vma->vm_file, but must guarantee that
  1565. * vma_link() below can deny write-access if VM_DENYWRITE is set
  1566. * and map writably if VM_SHARED is set. This usually means the
  1567. * new file must not have been exposed to user-space, yet.
  1568. */
  1569. vma->vm_file = get_file(file);
  1570. error = call_mmap(file, vma);
  1571. if (error)
  1572. goto unmap_and_free_vma;
  1573. /* Can addr have changed??
  1574. *
  1575. * Answer: Yes, several device drivers can do it in their
  1576. * f_op->mmap method. -DaveM
  1577. * Bug: If addr is changed, prev, rb_link, rb_parent should
  1578. * be updated for vma_link()
  1579. */
  1580. WARN_ON_ONCE(addr != vma->vm_start);
  1581. addr = vma->vm_start;
  1582. vm_flags = vma->vm_flags;
  1583. } else if (vm_flags & VM_SHARED) {
  1584. error = shmem_zero_setup(vma);
  1585. if (error)
  1586. goto free_vma;
  1587. }
  1588. vma_link(mm, vma, prev, rb_link, rb_parent);
  1589. /* Once vma denies write, undo our temporary denial count */
  1590. if (file) {
  1591. if (vm_flags & VM_SHARED)
  1592. mapping_unmap_writable(file->f_mapping);
  1593. if (vm_flags & VM_DENYWRITE)
  1594. allow_write_access(file);
  1595. }
  1596. file = vma->vm_file;
  1597. out:
  1598. perf_event_mmap(vma);
  1599. vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
  1600. if (vm_flags & VM_LOCKED) {
  1601. if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
  1602. vma == get_gate_vma(current->mm)))
  1603. mm->locked_vm += (len >> PAGE_SHIFT);
  1604. else
  1605. vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
  1606. }
  1607. if (file)
  1608. uprobe_mmap(vma);
  1609. /*
  1610. * New (or expanded) vma always get soft dirty status.
  1611. * Otherwise user-space soft-dirty page tracker won't
  1612. * be able to distinguish situation when vma area unmapped,
  1613. * then new mapped in-place (which must be aimed as
  1614. * a completely new data area).
  1615. */
  1616. vma->vm_flags |= VM_SOFTDIRTY;
  1617. vma_set_page_prot(vma);
  1618. return addr;
  1619. unmap_and_free_vma:
  1620. vma->vm_file = NULL;
  1621. fput(file);
  1622. /* Undo any partial mapping done by a device driver. */
  1623. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1624. charged = 0;
  1625. if (vm_flags & VM_SHARED)
  1626. mapping_unmap_writable(file->f_mapping);
  1627. allow_write_and_free_vma:
  1628. if (vm_flags & VM_DENYWRITE)
  1629. allow_write_access(file);
  1630. free_vma:
  1631. kmem_cache_free(vm_area_cachep, vma);
  1632. unacct_error:
  1633. if (charged)
  1634. vm_unacct_memory(charged);
  1635. return error;
  1636. }
  1637. unsigned long unmapped_area(struct vm_unmapped_area_info *info)
  1638. {
  1639. /*
  1640. * We implement the search by looking for an rbtree node that
  1641. * immediately follows a suitable gap. That is,
  1642. * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
  1643. * - gap_end = vma->vm_start >= info->low_limit + length;
  1644. * - gap_end - gap_start >= length
  1645. */
  1646. struct mm_struct *mm = current->mm;
  1647. struct vm_area_struct *vma;
  1648. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1649. /* Adjust search length to account for worst case alignment overhead */
  1650. length = info->length + info->align_mask;
  1651. if (length < info->length)
  1652. return -ENOMEM;
  1653. /* Adjust search limits by the desired length */
  1654. if (info->high_limit < length)
  1655. return -ENOMEM;
  1656. high_limit = info->high_limit - length;
  1657. if (info->low_limit > high_limit)
  1658. return -ENOMEM;
  1659. low_limit = info->low_limit + length;
  1660. /* Check if rbtree root looks promising */
  1661. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1662. goto check_highest;
  1663. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1664. if (vma->rb_subtree_gap < length)
  1665. goto check_highest;
  1666. while (true) {
  1667. /* Visit left subtree if it looks promising */
  1668. gap_end = vm_start_gap(vma);
  1669. if (gap_end >= low_limit && vma->vm_rb.rb_left) {
  1670. struct vm_area_struct *left =
  1671. rb_entry(vma->vm_rb.rb_left,
  1672. struct vm_area_struct, vm_rb);
  1673. if (left->rb_subtree_gap >= length) {
  1674. vma = left;
  1675. continue;
  1676. }
  1677. }
  1678. gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
  1679. check_current:
  1680. /* Check if current node has a suitable gap */
  1681. if (gap_start > high_limit)
  1682. return -ENOMEM;
  1683. if (gap_end >= low_limit &&
  1684. gap_end > gap_start && gap_end - gap_start >= length)
  1685. goto found;
  1686. /* Visit right subtree if it looks promising */
  1687. if (vma->vm_rb.rb_right) {
  1688. struct vm_area_struct *right =
  1689. rb_entry(vma->vm_rb.rb_right,
  1690. struct vm_area_struct, vm_rb);
  1691. if (right->rb_subtree_gap >= length) {
  1692. vma = right;
  1693. continue;
  1694. }
  1695. }
  1696. /* Go back up the rbtree to find next candidate node */
  1697. while (true) {
  1698. struct rb_node *prev = &vma->vm_rb;
  1699. if (!rb_parent(prev))
  1700. goto check_highest;
  1701. vma = rb_entry(rb_parent(prev),
  1702. struct vm_area_struct, vm_rb);
  1703. if (prev == vma->vm_rb.rb_left) {
  1704. gap_start = vm_end_gap(vma->vm_prev);
  1705. gap_end = vm_start_gap(vma);
  1706. goto check_current;
  1707. }
  1708. }
  1709. }
  1710. check_highest:
  1711. /* Check highest gap, which does not precede any rbtree node */
  1712. gap_start = mm->highest_vm_end;
  1713. gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
  1714. if (gap_start > high_limit)
  1715. return -ENOMEM;
  1716. found:
  1717. /* We found a suitable gap. Clip it with the original low_limit. */
  1718. if (gap_start < info->low_limit)
  1719. gap_start = info->low_limit;
  1720. /* Adjust gap address to the desired alignment */
  1721. gap_start += (info->align_offset - gap_start) & info->align_mask;
  1722. VM_BUG_ON(gap_start + info->length > info->high_limit);
  1723. VM_BUG_ON(gap_start + info->length > gap_end);
  1724. return gap_start;
  1725. }
  1726. unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
  1727. {
  1728. struct mm_struct *mm = current->mm;
  1729. struct vm_area_struct *vma;
  1730. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1731. /* Adjust search length to account for worst case alignment overhead */
  1732. length = info->length + info->align_mask;
  1733. if (length < info->length)
  1734. return -ENOMEM;
  1735. /*
  1736. * Adjust search limits by the desired length.
  1737. * See implementation comment at top of unmapped_area().
  1738. */
  1739. gap_end = info->high_limit;
  1740. if (gap_end < length)
  1741. return -ENOMEM;
  1742. high_limit = gap_end - length;
  1743. if (info->low_limit > high_limit)
  1744. return -ENOMEM;
  1745. low_limit = info->low_limit + length;
  1746. /* Check highest gap, which does not precede any rbtree node */
  1747. gap_start = mm->highest_vm_end;
  1748. if (gap_start <= high_limit)
  1749. goto found_highest;
  1750. /* Check if rbtree root looks promising */
  1751. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1752. return -ENOMEM;
  1753. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1754. if (vma->rb_subtree_gap < length)
  1755. return -ENOMEM;
  1756. while (true) {
  1757. /* Visit right subtree if it looks promising */
  1758. gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
  1759. if (gap_start <= high_limit && vma->vm_rb.rb_right) {
  1760. struct vm_area_struct *right =
  1761. rb_entry(vma->vm_rb.rb_right,
  1762. struct vm_area_struct, vm_rb);
  1763. if (right->rb_subtree_gap >= length) {
  1764. vma = right;
  1765. continue;
  1766. }
  1767. }
  1768. check_current:
  1769. /* Check if current node has a suitable gap */
  1770. gap_end = vm_start_gap(vma);
  1771. if (gap_end < low_limit)
  1772. return -ENOMEM;
  1773. if (gap_start <= high_limit &&
  1774. gap_end > gap_start && gap_end - gap_start >= length)
  1775. goto found;
  1776. /* Visit left subtree if it looks promising */
  1777. if (vma->vm_rb.rb_left) {
  1778. struct vm_area_struct *left =
  1779. rb_entry(vma->vm_rb.rb_left,
  1780. struct vm_area_struct, vm_rb);
  1781. if (left->rb_subtree_gap >= length) {
  1782. vma = left;
  1783. continue;
  1784. }
  1785. }
  1786. /* Go back up the rbtree to find next candidate node */
  1787. while (true) {
  1788. struct rb_node *prev = &vma->vm_rb;
  1789. if (!rb_parent(prev))
  1790. return -ENOMEM;
  1791. vma = rb_entry(rb_parent(prev),
  1792. struct vm_area_struct, vm_rb);
  1793. if (prev == vma->vm_rb.rb_right) {
  1794. gap_start = vma->vm_prev ?
  1795. vm_end_gap(vma->vm_prev) : 0;
  1796. goto check_current;
  1797. }
  1798. }
  1799. }
  1800. found:
  1801. /* We found a suitable gap. Clip it with the original high_limit. */
  1802. if (gap_end > info->high_limit)
  1803. gap_end = info->high_limit;
  1804. found_highest:
  1805. /* Compute highest gap address at the desired alignment */
  1806. gap_end -= info->length;
  1807. gap_end -= (gap_end - info->align_offset) & info->align_mask;
  1808. VM_BUG_ON(gap_end < info->low_limit);
  1809. VM_BUG_ON(gap_end < gap_start);
  1810. return gap_end;
  1811. }
  1812. /* Get an address range which is currently unmapped.
  1813. * For shmat() with addr=0.
  1814. *
  1815. * Ugly calling convention alert:
  1816. * Return value with the low bits set means error value,
  1817. * ie
  1818. * if (ret & ~PAGE_MASK)
  1819. * error = ret;
  1820. *
  1821. * This function "knows" that -ENOMEM has the bits set.
  1822. */
  1823. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1824. unsigned long
  1825. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1826. unsigned long len, unsigned long pgoff, unsigned long flags)
  1827. {
  1828. struct mm_struct *mm = current->mm;
  1829. struct vm_area_struct *vma, *prev;
  1830. struct vm_unmapped_area_info info;
  1831. if (len > TASK_SIZE - mmap_min_addr)
  1832. return -ENOMEM;
  1833. if (flags & MAP_FIXED)
  1834. return addr;
  1835. if (addr) {
  1836. addr = PAGE_ALIGN(addr);
  1837. vma = find_vma_prev(mm, addr, &prev);
  1838. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  1839. (!vma || addr + len <= vm_start_gap(vma)) &&
  1840. (!prev || addr >= vm_end_gap(prev)))
  1841. return addr;
  1842. }
  1843. info.flags = 0;
  1844. info.length = len;
  1845. info.low_limit = mm->mmap_base;
  1846. info.high_limit = TASK_SIZE;
  1847. info.align_mask = 0;
  1848. return vm_unmapped_area(&info);
  1849. }
  1850. #endif
  1851. /*
  1852. * This mmap-allocator allocates new areas top-down from below the
  1853. * stack's low limit (the base):
  1854. */
  1855. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1856. unsigned long
  1857. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1858. const unsigned long len, const unsigned long pgoff,
  1859. const unsigned long flags)
  1860. {
  1861. struct vm_area_struct *vma, *prev;
  1862. struct mm_struct *mm = current->mm;
  1863. unsigned long addr = addr0;
  1864. struct vm_unmapped_area_info info;
  1865. /* requested length too big for entire address space */
  1866. if (len > TASK_SIZE - mmap_min_addr)
  1867. return -ENOMEM;
  1868. if (flags & MAP_FIXED)
  1869. return addr;
  1870. /* requesting a specific address */
  1871. if (addr) {
  1872. addr = PAGE_ALIGN(addr);
  1873. vma = find_vma_prev(mm, addr, &prev);
  1874. if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
  1875. (!vma || addr + len <= vm_start_gap(vma)) &&
  1876. (!prev || addr >= vm_end_gap(prev)))
  1877. return addr;
  1878. }
  1879. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  1880. info.length = len;
  1881. info.low_limit = max(PAGE_SIZE, mmap_min_addr);
  1882. info.high_limit = mm->mmap_base;
  1883. info.align_mask = 0;
  1884. addr = vm_unmapped_area(&info);
  1885. /*
  1886. * A failed mmap() very likely causes application failure,
  1887. * so fall back to the bottom-up function here. This scenario
  1888. * can happen with large stack limits and large mmap()
  1889. * allocations.
  1890. */
  1891. if (offset_in_page(addr)) {
  1892. VM_BUG_ON(addr != -ENOMEM);
  1893. info.flags = 0;
  1894. info.low_limit = TASK_UNMAPPED_BASE;
  1895. info.high_limit = TASK_SIZE;
  1896. addr = vm_unmapped_area(&info);
  1897. }
  1898. return addr;
  1899. }
  1900. #endif
  1901. unsigned long
  1902. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1903. unsigned long pgoff, unsigned long flags)
  1904. {
  1905. unsigned long (*get_area)(struct file *, unsigned long,
  1906. unsigned long, unsigned long, unsigned long);
  1907. unsigned long error = arch_mmap_check(addr, len, flags);
  1908. if (error)
  1909. return error;
  1910. /* Careful about overflows.. */
  1911. if (len > TASK_SIZE)
  1912. return -ENOMEM;
  1913. get_area = current->mm->get_unmapped_area;
  1914. if (file) {
  1915. if (file->f_op->get_unmapped_area)
  1916. get_area = file->f_op->get_unmapped_area;
  1917. } else if (flags & MAP_SHARED) {
  1918. /*
  1919. * mmap_region() will call shmem_zero_setup() to create a file,
  1920. * so use shmem's get_unmapped_area in case it can be huge.
  1921. * do_mmap_pgoff() will clear pgoff, so match alignment.
  1922. */
  1923. pgoff = 0;
  1924. get_area = shmem_get_unmapped_area;
  1925. }
  1926. addr = get_area(file, addr, len, pgoff, flags);
  1927. if (IS_ERR_VALUE(addr))
  1928. return addr;
  1929. if (addr > TASK_SIZE - len)
  1930. return -ENOMEM;
  1931. if (offset_in_page(addr))
  1932. return -EINVAL;
  1933. error = security_mmap_addr(addr);
  1934. return error ? error : addr;
  1935. }
  1936. EXPORT_SYMBOL(get_unmapped_area);
  1937. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1938. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1939. {
  1940. struct rb_node *rb_node;
  1941. struct vm_area_struct *vma;
  1942. /* Check the cache first. */
  1943. vma = vmacache_find(mm, addr);
  1944. if (likely(vma))
  1945. return vma;
  1946. rb_node = mm->mm_rb.rb_node;
  1947. while (rb_node) {
  1948. struct vm_area_struct *tmp;
  1949. tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1950. if (tmp->vm_end > addr) {
  1951. vma = tmp;
  1952. if (tmp->vm_start <= addr)
  1953. break;
  1954. rb_node = rb_node->rb_left;
  1955. } else
  1956. rb_node = rb_node->rb_right;
  1957. }
  1958. if (vma)
  1959. vmacache_update(addr, vma);
  1960. return vma;
  1961. }
  1962. EXPORT_SYMBOL(find_vma);
  1963. /*
  1964. * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
  1965. */
  1966. struct vm_area_struct *
  1967. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1968. struct vm_area_struct **pprev)
  1969. {
  1970. struct vm_area_struct *vma;
  1971. vma = find_vma(mm, addr);
  1972. if (vma) {
  1973. *pprev = vma->vm_prev;
  1974. } else {
  1975. struct rb_node *rb_node = mm->mm_rb.rb_node;
  1976. *pprev = NULL;
  1977. while (rb_node) {
  1978. *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1979. rb_node = rb_node->rb_right;
  1980. }
  1981. }
  1982. return vma;
  1983. }
  1984. /*
  1985. * Verify that the stack growth is acceptable and
  1986. * update accounting. This is shared with both the
  1987. * grow-up and grow-down cases.
  1988. */
  1989. static int acct_stack_growth(struct vm_area_struct *vma,
  1990. unsigned long size, unsigned long grow)
  1991. {
  1992. struct mm_struct *mm = vma->vm_mm;
  1993. unsigned long new_start;
  1994. /* address space limit tests */
  1995. if (!may_expand_vm(mm, vma->vm_flags, grow))
  1996. return -ENOMEM;
  1997. /* Stack limit test */
  1998. if (size > rlimit(RLIMIT_STACK))
  1999. return -ENOMEM;
  2000. /* mlock limit tests */
  2001. if (vma->vm_flags & VM_LOCKED) {
  2002. unsigned long locked;
  2003. unsigned long limit;
  2004. locked = mm->locked_vm + grow;
  2005. limit = rlimit(RLIMIT_MEMLOCK);
  2006. limit >>= PAGE_SHIFT;
  2007. if (locked > limit && !capable(CAP_IPC_LOCK))
  2008. return -ENOMEM;
  2009. }
  2010. /* Check to ensure the stack will not grow into a hugetlb-only region */
  2011. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  2012. vma->vm_end - size;
  2013. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  2014. return -EFAULT;
  2015. /*
  2016. * Overcommit.. This must be the final test, as it will
  2017. * update security statistics.
  2018. */
  2019. if (security_vm_enough_memory_mm(mm, grow))
  2020. return -ENOMEM;
  2021. return 0;
  2022. }
  2023. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  2024. /*
  2025. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  2026. * vma is the last one with address > vma->vm_end. Have to extend vma.
  2027. */
  2028. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  2029. {
  2030. struct mm_struct *mm = vma->vm_mm;
  2031. struct vm_area_struct *next;
  2032. unsigned long gap_addr;
  2033. int error = 0;
  2034. if (!(vma->vm_flags & VM_GROWSUP))
  2035. return -EFAULT;
  2036. /* Guard against exceeding limits of the address space. */
  2037. address &= PAGE_MASK;
  2038. if (address >= (TASK_SIZE & PAGE_MASK))
  2039. return -ENOMEM;
  2040. address += PAGE_SIZE;
  2041. /* Enforce stack_guard_gap */
  2042. gap_addr = address + stack_guard_gap;
  2043. /* Guard against overflow */
  2044. if (gap_addr < address || gap_addr > TASK_SIZE)
  2045. gap_addr = TASK_SIZE;
  2046. next = vma->vm_next;
  2047. if (next && next->vm_start < gap_addr &&
  2048. (next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
  2049. if (!(next->vm_flags & VM_GROWSUP))
  2050. return -ENOMEM;
  2051. /* Check that both stack segments have the same anon_vma? */
  2052. }
  2053. /* We must make sure the anon_vma is allocated. */
  2054. if (unlikely(anon_vma_prepare(vma)))
  2055. return -ENOMEM;
  2056. /*
  2057. * vma->vm_start/vm_end cannot change under us because the caller
  2058. * is required to hold the mmap_sem in read mode. We need the
  2059. * anon_vma lock to serialize against concurrent expand_stacks.
  2060. */
  2061. anon_vma_lock_write(vma->anon_vma);
  2062. /* Somebody else might have raced and expanded it already */
  2063. if (address > vma->vm_end) {
  2064. unsigned long size, grow;
  2065. size = address - vma->vm_start;
  2066. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  2067. error = -ENOMEM;
  2068. if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
  2069. error = acct_stack_growth(vma, size, grow);
  2070. if (!error) {
  2071. /*
  2072. * vma_gap_update() doesn't support concurrent
  2073. * updates, but we only hold a shared mmap_sem
  2074. * lock here, so we need to protect against
  2075. * concurrent vma expansions.
  2076. * anon_vma_lock_write() doesn't help here, as
  2077. * we don't guarantee that all growable vmas
  2078. * in a mm share the same root anon vma.
  2079. * So, we reuse mm->page_table_lock to guard
  2080. * against concurrent vma expansions.
  2081. */
  2082. spin_lock(&mm->page_table_lock);
  2083. if (vma->vm_flags & VM_LOCKED)
  2084. mm->locked_vm += grow;
  2085. vm_stat_account(mm, vma->vm_flags, grow);
  2086. anon_vma_interval_tree_pre_update_vma(vma);
  2087. vma->vm_end = address;
  2088. anon_vma_interval_tree_post_update_vma(vma);
  2089. if (vma->vm_next)
  2090. vma_gap_update(vma->vm_next);
  2091. else
  2092. mm->highest_vm_end = vm_end_gap(vma);
  2093. spin_unlock(&mm->page_table_lock);
  2094. perf_event_mmap(vma);
  2095. }
  2096. }
  2097. }
  2098. anon_vma_unlock_write(vma->anon_vma);
  2099. khugepaged_enter_vma_merge(vma, vma->vm_flags);
  2100. validate_mm(mm);
  2101. return error;
  2102. }
  2103. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  2104. /*
  2105. * vma is the first one with address < vma->vm_start. Have to extend vma.
  2106. */
  2107. int expand_downwards(struct vm_area_struct *vma,
  2108. unsigned long address)
  2109. {
  2110. struct mm_struct *mm = vma->vm_mm;
  2111. struct vm_area_struct *prev;
  2112. int error;
  2113. address &= PAGE_MASK;
  2114. error = security_mmap_addr(address);
  2115. if (error)
  2116. return error;
  2117. /* Enforce stack_guard_gap */
  2118. prev = vma->vm_prev;
  2119. /* Check that both stack segments have the same anon_vma? */
  2120. if (prev && !(prev->vm_flags & VM_GROWSDOWN) &&
  2121. (prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
  2122. if (address - prev->vm_end < stack_guard_gap)
  2123. return -ENOMEM;
  2124. }
  2125. /* We must make sure the anon_vma is allocated. */
  2126. if (unlikely(anon_vma_prepare(vma)))
  2127. return -ENOMEM;
  2128. /*
  2129. * vma->vm_start/vm_end cannot change under us because the caller
  2130. * is required to hold the mmap_sem in read mode. We need the
  2131. * anon_vma lock to serialize against concurrent expand_stacks.
  2132. */
  2133. anon_vma_lock_write(vma->anon_vma);
  2134. /* Somebody else might have raced and expanded it already */
  2135. if (address < vma->vm_start) {
  2136. unsigned long size, grow;
  2137. size = vma->vm_end - address;
  2138. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  2139. error = -ENOMEM;
  2140. if (grow <= vma->vm_pgoff) {
  2141. error = acct_stack_growth(vma, size, grow);
  2142. if (!error) {
  2143. /*
  2144. * vma_gap_update() doesn't support concurrent
  2145. * updates, but we only hold a shared mmap_sem
  2146. * lock here, so we need to protect against
  2147. * concurrent vma expansions.
  2148. * anon_vma_lock_write() doesn't help here, as
  2149. * we don't guarantee that all growable vmas
  2150. * in a mm share the same root anon vma.
  2151. * So, we reuse mm->page_table_lock to guard
  2152. * against concurrent vma expansions.
  2153. */
  2154. spin_lock(&mm->page_table_lock);
  2155. if (vma->vm_flags & VM_LOCKED)
  2156. mm->locked_vm += grow;
  2157. vm_stat_account(mm, vma->vm_flags, grow);
  2158. anon_vma_interval_tree_pre_update_vma(vma);
  2159. vma->vm_start = address;
  2160. vma->vm_pgoff -= grow;
  2161. anon_vma_interval_tree_post_update_vma(vma);
  2162. vma_gap_update(vma);
  2163. spin_unlock(&mm->page_table_lock);
  2164. perf_event_mmap(vma);
  2165. }
  2166. }
  2167. }
  2168. anon_vma_unlock_write(vma->anon_vma);
  2169. khugepaged_enter_vma_merge(vma, vma->vm_flags);
  2170. validate_mm(mm);
  2171. return error;
  2172. }
  2173. /* enforced gap between the expanding stack and other mappings. */
  2174. unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
  2175. static int __init cmdline_parse_stack_guard_gap(char *p)
  2176. {
  2177. unsigned long val;
  2178. char *endptr;
  2179. val = simple_strtoul(p, &endptr, 10);
  2180. if (!*endptr)
  2181. stack_guard_gap = val << PAGE_SHIFT;
  2182. return 0;
  2183. }
  2184. __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
  2185. #ifdef CONFIG_STACK_GROWSUP
  2186. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  2187. {
  2188. return expand_upwards(vma, address);
  2189. }
  2190. struct vm_area_struct *
  2191. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  2192. {
  2193. struct vm_area_struct *vma, *prev;
  2194. addr &= PAGE_MASK;
  2195. vma = find_vma_prev(mm, addr, &prev);
  2196. if (vma && (vma->vm_start <= addr))
  2197. return vma;
  2198. if (!prev || expand_stack(prev, addr))
  2199. return NULL;
  2200. if (prev->vm_flags & VM_LOCKED)
  2201. populate_vma_page_range(prev, addr, prev->vm_end, NULL);
  2202. return prev;
  2203. }
  2204. #else
  2205. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  2206. {
  2207. return expand_downwards(vma, address);
  2208. }
  2209. struct vm_area_struct *
  2210. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  2211. {
  2212. struct vm_area_struct *vma;
  2213. unsigned long start;
  2214. addr &= PAGE_MASK;
  2215. vma = find_vma(mm, addr);
  2216. if (!vma)
  2217. return NULL;
  2218. if (vma->vm_start <= addr)
  2219. return vma;
  2220. if (!(vma->vm_flags & VM_GROWSDOWN))
  2221. return NULL;
  2222. start = vma->vm_start;
  2223. if (expand_stack(vma, addr))
  2224. return NULL;
  2225. if (vma->vm_flags & VM_LOCKED)
  2226. populate_vma_page_range(vma, addr, start, NULL);
  2227. return vma;
  2228. }
  2229. #endif
  2230. EXPORT_SYMBOL_GPL(find_extend_vma);
  2231. /*
  2232. * Ok - we have the memory areas we should free on the vma list,
  2233. * so release them, and do the vma updates.
  2234. *
  2235. * Called with the mm semaphore held.
  2236. */
  2237. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  2238. {
  2239. unsigned long nr_accounted = 0;
  2240. /* Update high watermark before we lower total_vm */
  2241. update_hiwater_vm(mm);
  2242. do {
  2243. long nrpages = vma_pages(vma);
  2244. if (vma->vm_flags & VM_ACCOUNT)
  2245. nr_accounted += nrpages;
  2246. vm_stat_account(mm, vma->vm_flags, -nrpages);
  2247. vma = remove_vma(vma);
  2248. } while (vma);
  2249. vm_unacct_memory(nr_accounted);
  2250. validate_mm(mm);
  2251. }
  2252. /*
  2253. * Get rid of page table information in the indicated region.
  2254. *
  2255. * Called with the mm semaphore held.
  2256. */
  2257. static void unmap_region(struct mm_struct *mm,
  2258. struct vm_area_struct *vma, struct vm_area_struct *prev,
  2259. unsigned long start, unsigned long end)
  2260. {
  2261. struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
  2262. struct mmu_gather tlb;
  2263. lru_add_drain();
  2264. tlb_gather_mmu(&tlb, mm, start, end);
  2265. update_hiwater_rss(mm);
  2266. unmap_vmas(&tlb, vma, start, end);
  2267. free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
  2268. next ? next->vm_start : USER_PGTABLES_CEILING);
  2269. tlb_finish_mmu(&tlb, start, end);
  2270. }
  2271. /*
  2272. * Create a list of vma's touched by the unmap, removing them from the mm's
  2273. * vma list as we go..
  2274. */
  2275. static void
  2276. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  2277. struct vm_area_struct *prev, unsigned long end)
  2278. {
  2279. struct vm_area_struct **insertion_point;
  2280. struct vm_area_struct *tail_vma = NULL;
  2281. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  2282. vma->vm_prev = NULL;
  2283. do {
  2284. vma_rb_erase(vma, &mm->mm_rb);
  2285. mm->map_count--;
  2286. tail_vma = vma;
  2287. vma = vma->vm_next;
  2288. } while (vma && vma->vm_start < end);
  2289. *insertion_point = vma;
  2290. if (vma) {
  2291. vma->vm_prev = prev;
  2292. vma_gap_update(vma);
  2293. } else
  2294. mm->highest_vm_end = prev ? vm_end_gap(prev) : 0;
  2295. tail_vma->vm_next = NULL;
  2296. /* Kill the cache */
  2297. vmacache_invalidate(mm);
  2298. }
  2299. /*
  2300. * __split_vma() bypasses sysctl_max_map_count checking. We use this where it
  2301. * has already been checked or doesn't make sense to fail.
  2302. */
  2303. int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  2304. unsigned long addr, int new_below)
  2305. {
  2306. struct vm_area_struct *new;
  2307. int err;
  2308. if (vma->vm_ops && vma->vm_ops->split) {
  2309. err = vma->vm_ops->split(vma, addr);
  2310. if (err)
  2311. return err;
  2312. }
  2313. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2314. if (!new)
  2315. return -ENOMEM;
  2316. /* most fields are the same, copy all, and then fixup */
  2317. *new = *vma;
  2318. INIT_LIST_HEAD(&new->anon_vma_chain);
  2319. if (new_below)
  2320. new->vm_end = addr;
  2321. else {
  2322. new->vm_start = addr;
  2323. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  2324. }
  2325. err = vma_dup_policy(vma, new);
  2326. if (err)
  2327. goto out_free_vma;
  2328. err = anon_vma_clone(new, vma);
  2329. if (err)
  2330. goto out_free_mpol;
  2331. if (new->vm_file)
  2332. get_file(new->vm_file);
  2333. if (new->vm_ops && new->vm_ops->open)
  2334. new->vm_ops->open(new);
  2335. if (new_below)
  2336. err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  2337. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  2338. else
  2339. err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  2340. /* Success. */
  2341. if (!err)
  2342. return 0;
  2343. /* Clean everything up if vma_adjust failed. */
  2344. if (new->vm_ops && new->vm_ops->close)
  2345. new->vm_ops->close(new);
  2346. if (new->vm_file)
  2347. fput(new->vm_file);
  2348. unlink_anon_vmas(new);
  2349. out_free_mpol:
  2350. mpol_put(vma_policy(new));
  2351. out_free_vma:
  2352. kmem_cache_free(vm_area_cachep, new);
  2353. return err;
  2354. }
  2355. /*
  2356. * Split a vma into two pieces at address 'addr', a new vma is allocated
  2357. * either for the first part or the tail.
  2358. */
  2359. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  2360. unsigned long addr, int new_below)
  2361. {
  2362. if (mm->map_count >= sysctl_max_map_count)
  2363. return -ENOMEM;
  2364. return __split_vma(mm, vma, addr, new_below);
  2365. }
  2366. /* Munmap is split into 2 main parts -- this part which finds
  2367. * what needs doing, and the areas themselves, which do the
  2368. * work. This now handles partial unmappings.
  2369. * Jeremy Fitzhardinge <jeremy@goop.org>
  2370. */
  2371. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
  2372. struct list_head *uf)
  2373. {
  2374. unsigned long end;
  2375. struct vm_area_struct *vma, *prev, *last;
  2376. if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
  2377. return -EINVAL;
  2378. len = PAGE_ALIGN(len);
  2379. if (len == 0)
  2380. return -EINVAL;
  2381. /* Find the first overlapping VMA */
  2382. vma = find_vma(mm, start);
  2383. if (!vma)
  2384. return 0;
  2385. prev = vma->vm_prev;
  2386. /* we have start < vma->vm_end */
  2387. /* if it doesn't overlap, we have nothing.. */
  2388. end = start + len;
  2389. if (vma->vm_start >= end)
  2390. return 0;
  2391. /*
  2392. * If we need to split any vma, do it now to save pain later.
  2393. *
  2394. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  2395. * unmapped vm_area_struct will remain in use: so lower split_vma
  2396. * places tmp vma above, and higher split_vma places tmp vma below.
  2397. */
  2398. if (start > vma->vm_start) {
  2399. int error;
  2400. /*
  2401. * Make sure that map_count on return from munmap() will
  2402. * not exceed its limit; but let map_count go just above
  2403. * its limit temporarily, to help free resources as expected.
  2404. */
  2405. if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
  2406. return -ENOMEM;
  2407. error = __split_vma(mm, vma, start, 0);
  2408. if (error)
  2409. return error;
  2410. prev = vma;
  2411. }
  2412. /* Does it split the last one? */
  2413. last = find_vma(mm, end);
  2414. if (last && end > last->vm_start) {
  2415. int error = __split_vma(mm, last, end, 1);
  2416. if (error)
  2417. return error;
  2418. }
  2419. vma = prev ? prev->vm_next : mm->mmap;
  2420. if (unlikely(uf)) {
  2421. /*
  2422. * If userfaultfd_unmap_prep returns an error the vmas
  2423. * will remain splitted, but userland will get a
  2424. * highly unexpected error anyway. This is no
  2425. * different than the case where the first of the two
  2426. * __split_vma fails, but we don't undo the first
  2427. * split, despite we could. This is unlikely enough
  2428. * failure that it's not worth optimizing it for.
  2429. */
  2430. int error = userfaultfd_unmap_prep(vma, start, end, uf);
  2431. if (error)
  2432. return error;
  2433. }
  2434. /*
  2435. * unlock any mlock()ed ranges before detaching vmas
  2436. */
  2437. if (mm->locked_vm) {
  2438. struct vm_area_struct *tmp = vma;
  2439. while (tmp && tmp->vm_start < end) {
  2440. if (tmp->vm_flags & VM_LOCKED) {
  2441. mm->locked_vm -= vma_pages(tmp);
  2442. munlock_vma_pages_all(tmp);
  2443. }
  2444. tmp = tmp->vm_next;
  2445. }
  2446. }
  2447. /*
  2448. * Remove the vma's, and unmap the actual pages
  2449. */
  2450. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  2451. unmap_region(mm, vma, prev, start, end);
  2452. arch_unmap(mm, vma, start, end);
  2453. /* Fix up all other VM information */
  2454. remove_vma_list(mm, vma);
  2455. return 0;
  2456. }
  2457. int vm_munmap(unsigned long start, size_t len)
  2458. {
  2459. int ret;
  2460. struct mm_struct *mm = current->mm;
  2461. LIST_HEAD(uf);
  2462. if (down_write_killable(&mm->mmap_sem))
  2463. return -EINTR;
  2464. ret = do_munmap(mm, start, len, &uf);
  2465. up_write(&mm->mmap_sem);
  2466. userfaultfd_unmap_complete(mm, &uf);
  2467. return ret;
  2468. }
  2469. EXPORT_SYMBOL(vm_munmap);
  2470. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  2471. {
  2472. profile_munmap(addr);
  2473. return vm_munmap(addr, len);
  2474. }
  2475. /*
  2476. * Emulation of deprecated remap_file_pages() syscall.
  2477. */
  2478. SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  2479. unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
  2480. {
  2481. struct mm_struct *mm = current->mm;
  2482. struct vm_area_struct *vma;
  2483. unsigned long populate = 0;
  2484. unsigned long ret = -EINVAL;
  2485. struct file *file;
  2486. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
  2487. current->comm, current->pid);
  2488. if (prot)
  2489. return ret;
  2490. start = start & PAGE_MASK;
  2491. size = size & PAGE_MASK;
  2492. if (start + size <= start)
  2493. return ret;
  2494. /* Does pgoff wrap? */
  2495. if (pgoff + (size >> PAGE_SHIFT) < pgoff)
  2496. return ret;
  2497. if (down_write_killable(&mm->mmap_sem))
  2498. return -EINTR;
  2499. vma = find_vma(mm, start);
  2500. if (!vma || !(vma->vm_flags & VM_SHARED))
  2501. goto out;
  2502. if (start < vma->vm_start)
  2503. goto out;
  2504. if (start + size > vma->vm_end) {
  2505. struct vm_area_struct *next;
  2506. for (next = vma->vm_next; next; next = next->vm_next) {
  2507. /* hole between vmas ? */
  2508. if (next->vm_start != next->vm_prev->vm_end)
  2509. goto out;
  2510. if (next->vm_file != vma->vm_file)
  2511. goto out;
  2512. if (next->vm_flags != vma->vm_flags)
  2513. goto out;
  2514. if (start + size <= next->vm_end)
  2515. break;
  2516. }
  2517. if (!next)
  2518. goto out;
  2519. }
  2520. prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
  2521. prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
  2522. prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
  2523. flags &= MAP_NONBLOCK;
  2524. flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
  2525. if (vma->vm_flags & VM_LOCKED) {
  2526. struct vm_area_struct *tmp;
  2527. flags |= MAP_LOCKED;
  2528. /* drop PG_Mlocked flag for over-mapped range */
  2529. for (tmp = vma; tmp->vm_start >= start + size;
  2530. tmp = tmp->vm_next) {
  2531. /*
  2532. * Split pmd and munlock page on the border
  2533. * of the range.
  2534. */
  2535. vma_adjust_trans_huge(tmp, start, start + size, 0);
  2536. munlock_vma_pages_range(tmp,
  2537. max(tmp->vm_start, start),
  2538. min(tmp->vm_end, start + size));
  2539. }
  2540. }
  2541. file = get_file(vma->vm_file);
  2542. ret = do_mmap_pgoff(vma->vm_file, start, size,
  2543. prot, flags, pgoff, &populate, NULL);
  2544. fput(file);
  2545. out:
  2546. up_write(&mm->mmap_sem);
  2547. if (populate)
  2548. mm_populate(ret, populate);
  2549. if (!IS_ERR_VALUE(ret))
  2550. ret = 0;
  2551. return ret;
  2552. }
  2553. static inline void verify_mm_writelocked(struct mm_struct *mm)
  2554. {
  2555. #ifdef CONFIG_DEBUG_VM
  2556. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  2557. WARN_ON(1);
  2558. up_read(&mm->mmap_sem);
  2559. }
  2560. #endif
  2561. }
  2562. /*
  2563. * this is really a simplified "do_mmap". it only handles
  2564. * anonymous maps. eventually we may be able to do some
  2565. * brk-specific accounting here.
  2566. */
  2567. static int do_brk_flags(unsigned long addr, unsigned long len, unsigned long flags, struct list_head *uf)
  2568. {
  2569. struct mm_struct *mm = current->mm;
  2570. struct vm_area_struct *vma, *prev;
  2571. struct rb_node **rb_link, *rb_parent;
  2572. pgoff_t pgoff = addr >> PAGE_SHIFT;
  2573. int error;
  2574. /* Until we need other flags, refuse anything except VM_EXEC. */
  2575. if ((flags & (~VM_EXEC)) != 0)
  2576. return -EINVAL;
  2577. flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  2578. error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  2579. if (offset_in_page(error))
  2580. return error;
  2581. error = mlock_future_check(mm, mm->def_flags, len);
  2582. if (error)
  2583. return error;
  2584. /*
  2585. * mm->mmap_sem is required to protect against another thread
  2586. * changing the mappings in case we sleep.
  2587. */
  2588. verify_mm_writelocked(mm);
  2589. /*
  2590. * Clear old maps. this also does some error checking for us
  2591. */
  2592. while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
  2593. &rb_parent)) {
  2594. if (do_munmap(mm, addr, len, uf))
  2595. return -ENOMEM;
  2596. }
  2597. /* Check against address space limits *after* clearing old maps... */
  2598. if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
  2599. return -ENOMEM;
  2600. if (mm->map_count > sysctl_max_map_count)
  2601. return -ENOMEM;
  2602. if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
  2603. return -ENOMEM;
  2604. /* Can we just expand an old private anonymous mapping? */
  2605. vma = vma_merge(mm, prev, addr, addr + len, flags,
  2606. NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX);
  2607. if (vma)
  2608. goto out;
  2609. /*
  2610. * create a vma struct for an anonymous mapping
  2611. */
  2612. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2613. if (!vma) {
  2614. vm_unacct_memory(len >> PAGE_SHIFT);
  2615. return -ENOMEM;
  2616. }
  2617. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2618. vma->vm_mm = mm;
  2619. vma->vm_start = addr;
  2620. vma->vm_end = addr + len;
  2621. vma->vm_pgoff = pgoff;
  2622. vma->vm_flags = flags;
  2623. vma->vm_page_prot = vm_get_page_prot(flags);
  2624. vma_link(mm, vma, prev, rb_link, rb_parent);
  2625. out:
  2626. perf_event_mmap(vma);
  2627. mm->total_vm += len >> PAGE_SHIFT;
  2628. mm->data_vm += len >> PAGE_SHIFT;
  2629. if (flags & VM_LOCKED)
  2630. mm->locked_vm += (len >> PAGE_SHIFT);
  2631. vma->vm_flags |= VM_SOFTDIRTY;
  2632. return 0;
  2633. }
  2634. int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)
  2635. {
  2636. struct mm_struct *mm = current->mm;
  2637. unsigned long len;
  2638. int ret;
  2639. bool populate;
  2640. LIST_HEAD(uf);
  2641. len = PAGE_ALIGN(request);
  2642. if (len < request)
  2643. return -ENOMEM;
  2644. if (!len)
  2645. return 0;
  2646. if (down_write_killable(&mm->mmap_sem))
  2647. return -EINTR;
  2648. ret = do_brk_flags(addr, len, flags, &uf);
  2649. populate = ((mm->def_flags & VM_LOCKED) != 0);
  2650. up_write(&mm->mmap_sem);
  2651. userfaultfd_unmap_complete(mm, &uf);
  2652. if (populate && !ret)
  2653. mm_populate(addr, len);
  2654. return ret;
  2655. }
  2656. EXPORT_SYMBOL(vm_brk_flags);
  2657. int vm_brk(unsigned long addr, unsigned long len)
  2658. {
  2659. return vm_brk_flags(addr, len, 0);
  2660. }
  2661. EXPORT_SYMBOL(vm_brk);
  2662. /* Release all mmaps. */
  2663. void exit_mmap(struct mm_struct *mm)
  2664. {
  2665. struct mmu_gather tlb;
  2666. struct vm_area_struct *vma;
  2667. unsigned long nr_accounted = 0;
  2668. /* mm's last user has gone, and its about to be pulled down */
  2669. mmu_notifier_release(mm);
  2670. if (unlikely(mm_is_oom_victim(mm))) {
  2671. /*
  2672. * Manually reap the mm to free as much memory as possible.
  2673. * Then, as the oom reaper does, set MMF_OOM_SKIP to disregard
  2674. * this mm from further consideration. Taking mm->mmap_sem for
  2675. * write after setting MMF_OOM_SKIP will guarantee that the oom
  2676. * reaper will not run on this mm again after mmap_sem is
  2677. * dropped.
  2678. *
  2679. * Nothing can be holding mm->mmap_sem here and the above call
  2680. * to mmu_notifier_release(mm) ensures mmu notifier callbacks in
  2681. * __oom_reap_task_mm() will not block.
  2682. *
  2683. * This needs to be done before calling munlock_vma_pages_all(),
  2684. * which clears VM_LOCKED, otherwise the oom reaper cannot
  2685. * reliably test it.
  2686. */
  2687. mutex_lock(&oom_lock);
  2688. __oom_reap_task_mm(mm);
  2689. mutex_unlock(&oom_lock);
  2690. set_bit(MMF_OOM_SKIP, &mm->flags);
  2691. down_write(&mm->mmap_sem);
  2692. up_write(&mm->mmap_sem);
  2693. }
  2694. if (mm->locked_vm) {
  2695. vma = mm->mmap;
  2696. while (vma) {
  2697. if (vma->vm_flags & VM_LOCKED)
  2698. munlock_vma_pages_all(vma);
  2699. vma = vma->vm_next;
  2700. }
  2701. }
  2702. arch_exit_mmap(mm);
  2703. vma = mm->mmap;
  2704. if (!vma) /* Can happen if dup_mmap() received an OOM */
  2705. return;
  2706. lru_add_drain();
  2707. flush_cache_mm(mm);
  2708. tlb_gather_mmu(&tlb, mm, 0, -1);
  2709. /* update_hiwater_rss(mm) here? but nobody should be looking */
  2710. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  2711. unmap_vmas(&tlb, vma, 0, -1);
  2712. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
  2713. tlb_finish_mmu(&tlb, 0, -1);
  2714. /*
  2715. * Walk the list again, actually closing and freeing it,
  2716. * with preemption enabled, without holding any MM locks.
  2717. */
  2718. while (vma) {
  2719. if (vma->vm_flags & VM_ACCOUNT)
  2720. nr_accounted += vma_pages(vma);
  2721. vma = remove_vma(vma);
  2722. }
  2723. vm_unacct_memory(nr_accounted);
  2724. }
  2725. /* Insert vm structure into process list sorted by address
  2726. * and into the inode's i_mmap tree. If vm_file is non-NULL
  2727. * then i_mmap_rwsem is taken here.
  2728. */
  2729. int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  2730. {
  2731. struct vm_area_struct *prev;
  2732. struct rb_node **rb_link, *rb_parent;
  2733. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  2734. &prev, &rb_link, &rb_parent))
  2735. return -ENOMEM;
  2736. if ((vma->vm_flags & VM_ACCOUNT) &&
  2737. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  2738. return -ENOMEM;
  2739. /*
  2740. * The vm_pgoff of a purely anonymous vma should be irrelevant
  2741. * until its first write fault, when page's anon_vma and index
  2742. * are set. But now set the vm_pgoff it will almost certainly
  2743. * end up with (unless mremap moves it elsewhere before that
  2744. * first wfault), so /proc/pid/maps tells a consistent story.
  2745. *
  2746. * By setting it to reflect the virtual start address of the
  2747. * vma, merges and splits can happen in a seamless way, just
  2748. * using the existing file pgoff checks and manipulations.
  2749. * Similarly in do_mmap_pgoff and in do_brk.
  2750. */
  2751. if (vma_is_anonymous(vma)) {
  2752. BUG_ON(vma->anon_vma);
  2753. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  2754. }
  2755. vma_link(mm, vma, prev, rb_link, rb_parent);
  2756. return 0;
  2757. }
  2758. /*
  2759. * Copy the vma structure to a new location in the same mm,
  2760. * prior to moving page table entries, to effect an mremap move.
  2761. */
  2762. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  2763. unsigned long addr, unsigned long len, pgoff_t pgoff,
  2764. bool *need_rmap_locks)
  2765. {
  2766. struct vm_area_struct *vma = *vmap;
  2767. unsigned long vma_start = vma->vm_start;
  2768. struct mm_struct *mm = vma->vm_mm;
  2769. struct vm_area_struct *new_vma, *prev;
  2770. struct rb_node **rb_link, *rb_parent;
  2771. bool faulted_in_anon_vma = true;
  2772. /*
  2773. * If anonymous vma has not yet been faulted, update new pgoff
  2774. * to match new location, to increase its chance of merging.
  2775. */
  2776. if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
  2777. pgoff = addr >> PAGE_SHIFT;
  2778. faulted_in_anon_vma = false;
  2779. }
  2780. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
  2781. return NULL; /* should never get here */
  2782. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  2783. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  2784. vma->vm_userfaultfd_ctx);
  2785. if (new_vma) {
  2786. /*
  2787. * Source vma may have been merged into new_vma
  2788. */
  2789. if (unlikely(vma_start >= new_vma->vm_start &&
  2790. vma_start < new_vma->vm_end)) {
  2791. /*
  2792. * The only way we can get a vma_merge with
  2793. * self during an mremap is if the vma hasn't
  2794. * been faulted in yet and we were allowed to
  2795. * reset the dst vma->vm_pgoff to the
  2796. * destination address of the mremap to allow
  2797. * the merge to happen. mremap must change the
  2798. * vm_pgoff linearity between src and dst vmas
  2799. * (in turn preventing a vma_merge) to be
  2800. * safe. It is only safe to keep the vm_pgoff
  2801. * linear if there are no pages mapped yet.
  2802. */
  2803. VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
  2804. *vmap = vma = new_vma;
  2805. }
  2806. *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
  2807. } else {
  2808. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2809. if (!new_vma)
  2810. goto out;
  2811. *new_vma = *vma;
  2812. new_vma->vm_start = addr;
  2813. new_vma->vm_end = addr + len;
  2814. new_vma->vm_pgoff = pgoff;
  2815. if (vma_dup_policy(vma, new_vma))
  2816. goto out_free_vma;
  2817. INIT_LIST_HEAD(&new_vma->anon_vma_chain);
  2818. if (anon_vma_clone(new_vma, vma))
  2819. goto out_free_mempol;
  2820. if (new_vma->vm_file)
  2821. get_file(new_vma->vm_file);
  2822. if (new_vma->vm_ops && new_vma->vm_ops->open)
  2823. new_vma->vm_ops->open(new_vma);
  2824. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  2825. *need_rmap_locks = false;
  2826. }
  2827. return new_vma;
  2828. out_free_mempol:
  2829. mpol_put(vma_policy(new_vma));
  2830. out_free_vma:
  2831. kmem_cache_free(vm_area_cachep, new_vma);
  2832. out:
  2833. return NULL;
  2834. }
  2835. /*
  2836. * Return true if the calling process may expand its vm space by the passed
  2837. * number of pages
  2838. */
  2839. bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
  2840. {
  2841. if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
  2842. return false;
  2843. if (is_data_mapping(flags) &&
  2844. mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
  2845. /* Workaround for Valgrind */
  2846. if (rlimit(RLIMIT_DATA) == 0 &&
  2847. mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
  2848. return true;
  2849. pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n",
  2850. current->comm, current->pid,
  2851. (mm->data_vm + npages) << PAGE_SHIFT,
  2852. rlimit(RLIMIT_DATA),
  2853. ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data");
  2854. if (!ignore_rlimit_data)
  2855. return false;
  2856. }
  2857. return true;
  2858. }
  2859. void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
  2860. {
  2861. mm->total_vm += npages;
  2862. if (is_exec_mapping(flags))
  2863. mm->exec_vm += npages;
  2864. else if (is_stack_mapping(flags))
  2865. mm->stack_vm += npages;
  2866. else if (is_data_mapping(flags))
  2867. mm->data_vm += npages;
  2868. }
  2869. static vm_fault_t special_mapping_fault(struct vm_fault *vmf);
  2870. /*
  2871. * Having a close hook prevents vma merging regardless of flags.
  2872. */
  2873. static void special_mapping_close(struct vm_area_struct *vma)
  2874. {
  2875. }
  2876. static const char *special_mapping_name(struct vm_area_struct *vma)
  2877. {
  2878. return ((struct vm_special_mapping *)vma->vm_private_data)->name;
  2879. }
  2880. static int special_mapping_mremap(struct vm_area_struct *new_vma)
  2881. {
  2882. struct vm_special_mapping *sm = new_vma->vm_private_data;
  2883. if (WARN_ON_ONCE(current->mm != new_vma->vm_mm))
  2884. return -EFAULT;
  2885. if (sm->mremap)
  2886. return sm->mremap(sm, new_vma);
  2887. return 0;
  2888. }
  2889. static const struct vm_operations_struct special_mapping_vmops = {
  2890. .close = special_mapping_close,
  2891. .fault = special_mapping_fault,
  2892. .mremap = special_mapping_mremap,
  2893. .name = special_mapping_name,
  2894. };
  2895. static const struct vm_operations_struct legacy_special_mapping_vmops = {
  2896. .close = special_mapping_close,
  2897. .fault = special_mapping_fault,
  2898. };
  2899. static vm_fault_t special_mapping_fault(struct vm_fault *vmf)
  2900. {
  2901. struct vm_area_struct *vma = vmf->vma;
  2902. pgoff_t pgoff;
  2903. struct page **pages;
  2904. if (vma->vm_ops == &legacy_special_mapping_vmops) {
  2905. pages = vma->vm_private_data;
  2906. } else {
  2907. struct vm_special_mapping *sm = vma->vm_private_data;
  2908. if (sm->fault)
  2909. return sm->fault(sm, vmf->vma, vmf);
  2910. pages = sm->pages;
  2911. }
  2912. for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
  2913. pgoff--;
  2914. if (*pages) {
  2915. struct page *page = *pages;
  2916. get_page(page);
  2917. vmf->page = page;
  2918. return 0;
  2919. }
  2920. return VM_FAULT_SIGBUS;
  2921. }
  2922. static struct vm_area_struct *__install_special_mapping(
  2923. struct mm_struct *mm,
  2924. unsigned long addr, unsigned long len,
  2925. unsigned long vm_flags, void *priv,
  2926. const struct vm_operations_struct *ops)
  2927. {
  2928. int ret;
  2929. struct vm_area_struct *vma;
  2930. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2931. if (unlikely(vma == NULL))
  2932. return ERR_PTR(-ENOMEM);
  2933. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2934. vma->vm_mm = mm;
  2935. vma->vm_start = addr;
  2936. vma->vm_end = addr + len;
  2937. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
  2938. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2939. vma->vm_ops = ops;
  2940. vma->vm_private_data = priv;
  2941. ret = insert_vm_struct(mm, vma);
  2942. if (ret)
  2943. goto out;
  2944. vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
  2945. perf_event_mmap(vma);
  2946. return vma;
  2947. out:
  2948. kmem_cache_free(vm_area_cachep, vma);
  2949. return ERR_PTR(ret);
  2950. }
  2951. bool vma_is_special_mapping(const struct vm_area_struct *vma,
  2952. const struct vm_special_mapping *sm)
  2953. {
  2954. return vma->vm_private_data == sm &&
  2955. (vma->vm_ops == &special_mapping_vmops ||
  2956. vma->vm_ops == &legacy_special_mapping_vmops);
  2957. }
  2958. /*
  2959. * Called with mm->mmap_sem held for writing.
  2960. * Insert a new vma covering the given region, with the given flags.
  2961. * Its pages are supplied by the given array of struct page *.
  2962. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  2963. * The region past the last page supplied will always produce SIGBUS.
  2964. * The array pointer and the pages it points to are assumed to stay alive
  2965. * for as long as this mapping might exist.
  2966. */
  2967. struct vm_area_struct *_install_special_mapping(
  2968. struct mm_struct *mm,
  2969. unsigned long addr, unsigned long len,
  2970. unsigned long vm_flags, const struct vm_special_mapping *spec)
  2971. {
  2972. return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
  2973. &special_mapping_vmops);
  2974. }
  2975. int install_special_mapping(struct mm_struct *mm,
  2976. unsigned long addr, unsigned long len,
  2977. unsigned long vm_flags, struct page **pages)
  2978. {
  2979. struct vm_area_struct *vma = __install_special_mapping(
  2980. mm, addr, len, vm_flags, (void *)pages,
  2981. &legacy_special_mapping_vmops);
  2982. return PTR_ERR_OR_ZERO(vma);
  2983. }
  2984. static DEFINE_MUTEX(mm_all_locks_mutex);
  2985. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2986. {
  2987. if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
  2988. /*
  2989. * The LSB of head.next can't change from under us
  2990. * because we hold the mm_all_locks_mutex.
  2991. */
  2992. down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
  2993. /*
  2994. * We can safely modify head.next after taking the
  2995. * anon_vma->root->rwsem. If some other vma in this mm shares
  2996. * the same anon_vma we won't take it again.
  2997. *
  2998. * No need of atomic instructions here, head.next
  2999. * can't change from under us thanks to the
  3000. * anon_vma->root->rwsem.
  3001. */
  3002. if (__test_and_set_bit(0, (unsigned long *)
  3003. &anon_vma->root->rb_root.rb_root.rb_node))
  3004. BUG();
  3005. }
  3006. }
  3007. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  3008. {
  3009. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  3010. /*
  3011. * AS_MM_ALL_LOCKS can't change from under us because
  3012. * we hold the mm_all_locks_mutex.
  3013. *
  3014. * Operations on ->flags have to be atomic because
  3015. * even if AS_MM_ALL_LOCKS is stable thanks to the
  3016. * mm_all_locks_mutex, there may be other cpus
  3017. * changing other bitflags in parallel to us.
  3018. */
  3019. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  3020. BUG();
  3021. down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
  3022. }
  3023. }
  3024. /*
  3025. * This operation locks against the VM for all pte/vma/mm related
  3026. * operations that could ever happen on a certain mm. This includes
  3027. * vmtruncate, try_to_unmap, and all page faults.
  3028. *
  3029. * The caller must take the mmap_sem in write mode before calling
  3030. * mm_take_all_locks(). The caller isn't allowed to release the
  3031. * mmap_sem until mm_drop_all_locks() returns.
  3032. *
  3033. * mmap_sem in write mode is required in order to block all operations
  3034. * that could modify pagetables and free pages without need of
  3035. * altering the vma layout. It's also needed in write mode to avoid new
  3036. * anon_vmas to be associated with existing vmas.
  3037. *
  3038. * A single task can't take more than one mm_take_all_locks() in a row
  3039. * or it would deadlock.
  3040. *
  3041. * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
  3042. * mapping->flags avoid to take the same lock twice, if more than one
  3043. * vma in this mm is backed by the same anon_vma or address_space.
  3044. *
  3045. * We take locks in following order, accordingly to comment at beginning
  3046. * of mm/rmap.c:
  3047. * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
  3048. * hugetlb mapping);
  3049. * - all i_mmap_rwsem locks;
  3050. * - all anon_vma->rwseml
  3051. *
  3052. * We can take all locks within these types randomly because the VM code
  3053. * doesn't nest them and we protected from parallel mm_take_all_locks() by
  3054. * mm_all_locks_mutex.
  3055. *
  3056. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  3057. * that may have to take thousand of locks.
  3058. *
  3059. * mm_take_all_locks() can fail if it's interrupted by signals.
  3060. */
  3061. int mm_take_all_locks(struct mm_struct *mm)
  3062. {
  3063. struct vm_area_struct *vma;
  3064. struct anon_vma_chain *avc;
  3065. BUG_ON(down_read_trylock(&mm->mmap_sem));
  3066. mutex_lock(&mm_all_locks_mutex);
  3067. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  3068. if (signal_pending(current))
  3069. goto out_unlock;
  3070. if (vma->vm_file && vma->vm_file->f_mapping &&
  3071. is_vm_hugetlb_page(vma))
  3072. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  3073. }
  3074. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  3075. if (signal_pending(current))
  3076. goto out_unlock;
  3077. if (vma->vm_file && vma->vm_file->f_mapping &&
  3078. !is_vm_hugetlb_page(vma))
  3079. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  3080. }
  3081. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  3082. if (signal_pending(current))
  3083. goto out_unlock;
  3084. if (vma->anon_vma)
  3085. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  3086. vm_lock_anon_vma(mm, avc->anon_vma);
  3087. }
  3088. return 0;
  3089. out_unlock:
  3090. mm_drop_all_locks(mm);
  3091. return -EINTR;
  3092. }
  3093. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  3094. {
  3095. if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
  3096. /*
  3097. * The LSB of head.next can't change to 0 from under
  3098. * us because we hold the mm_all_locks_mutex.
  3099. *
  3100. * We must however clear the bitflag before unlocking
  3101. * the vma so the users using the anon_vma->rb_root will
  3102. * never see our bitflag.
  3103. *
  3104. * No need of atomic instructions here, head.next
  3105. * can't change from under us until we release the
  3106. * anon_vma->root->rwsem.
  3107. */
  3108. if (!__test_and_clear_bit(0, (unsigned long *)
  3109. &anon_vma->root->rb_root.rb_root.rb_node))
  3110. BUG();
  3111. anon_vma_unlock_write(anon_vma);
  3112. }
  3113. }
  3114. static void vm_unlock_mapping(struct address_space *mapping)
  3115. {
  3116. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  3117. /*
  3118. * AS_MM_ALL_LOCKS can't change to 0 from under us
  3119. * because we hold the mm_all_locks_mutex.
  3120. */
  3121. i_mmap_unlock_write(mapping);
  3122. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  3123. &mapping->flags))
  3124. BUG();
  3125. }
  3126. }
  3127. /*
  3128. * The mmap_sem cannot be released by the caller until
  3129. * mm_drop_all_locks() returns.
  3130. */
  3131. void mm_drop_all_locks(struct mm_struct *mm)
  3132. {
  3133. struct vm_area_struct *vma;
  3134. struct anon_vma_chain *avc;
  3135. BUG_ON(down_read_trylock(&mm->mmap_sem));
  3136. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  3137. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  3138. if (vma->anon_vma)
  3139. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  3140. vm_unlock_anon_vma(avc->anon_vma);
  3141. if (vma->vm_file && vma->vm_file->f_mapping)
  3142. vm_unlock_mapping(vma->vm_file->f_mapping);
  3143. }
  3144. mutex_unlock(&mm_all_locks_mutex);
  3145. }
  3146. /*
  3147. * initialise the percpu counter for VM
  3148. */
  3149. void __init mmap_init(void)
  3150. {
  3151. int ret;
  3152. ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
  3153. VM_BUG_ON(ret);
  3154. }
  3155. /*
  3156. * Initialise sysctl_user_reserve_kbytes.
  3157. *
  3158. * This is intended to prevent a user from starting a single memory hogging
  3159. * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
  3160. * mode.
  3161. *
  3162. * The default value is min(3% of free memory, 128MB)
  3163. * 128MB is enough to recover with sshd/login, bash, and top/kill.
  3164. */
  3165. static int init_user_reserve(void)
  3166. {
  3167. unsigned long free_kbytes;
  3168. free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  3169. sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
  3170. return 0;
  3171. }
  3172. subsys_initcall(init_user_reserve);
  3173. /*
  3174. * Initialise sysctl_admin_reserve_kbytes.
  3175. *
  3176. * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
  3177. * to log in and kill a memory hogging process.
  3178. *
  3179. * Systems with more than 256MB will reserve 8MB, enough to recover
  3180. * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
  3181. * only reserve 3% of free pages by default.
  3182. */
  3183. static int init_admin_reserve(void)
  3184. {
  3185. unsigned long free_kbytes;
  3186. free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  3187. sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
  3188. return 0;
  3189. }
  3190. subsys_initcall(init_admin_reserve);
  3191. /*
  3192. * Reinititalise user and admin reserves if memory is added or removed.
  3193. *
  3194. * The default user reserve max is 128MB, and the default max for the
  3195. * admin reserve is 8MB. These are usually, but not always, enough to
  3196. * enable recovery from a memory hogging process using login/sshd, a shell,
  3197. * and tools like top. It may make sense to increase or even disable the
  3198. * reserve depending on the existence of swap or variations in the recovery
  3199. * tools. So, the admin may have changed them.
  3200. *
  3201. * If memory is added and the reserves have been eliminated or increased above
  3202. * the default max, then we'll trust the admin.
  3203. *
  3204. * If memory is removed and there isn't enough free memory, then we
  3205. * need to reset the reserves.
  3206. *
  3207. * Otherwise keep the reserve set by the admin.
  3208. */
  3209. static int reserve_mem_notifier(struct notifier_block *nb,
  3210. unsigned long action, void *data)
  3211. {
  3212. unsigned long tmp, free_kbytes;
  3213. switch (action) {
  3214. case MEM_ONLINE:
  3215. /* Default max is 128MB. Leave alone if modified by operator. */
  3216. tmp = sysctl_user_reserve_kbytes;
  3217. if (0 < tmp && tmp < (1UL << 17))
  3218. init_user_reserve();
  3219. /* Default max is 8MB. Leave alone if modified by operator. */
  3220. tmp = sysctl_admin_reserve_kbytes;
  3221. if (0 < tmp && tmp < (1UL << 13))
  3222. init_admin_reserve();
  3223. break;
  3224. case MEM_OFFLINE:
  3225. free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  3226. if (sysctl_user_reserve_kbytes > free_kbytes) {
  3227. init_user_reserve();
  3228. pr_info("vm.user_reserve_kbytes reset to %lu\n",
  3229. sysctl_user_reserve_kbytes);
  3230. }
  3231. if (sysctl_admin_reserve_kbytes > free_kbytes) {
  3232. init_admin_reserve();
  3233. pr_info("vm.admin_reserve_kbytes reset to %lu\n",
  3234. sysctl_admin_reserve_kbytes);
  3235. }
  3236. break;
  3237. default:
  3238. break;
  3239. }
  3240. return NOTIFY_OK;
  3241. }
  3242. static struct notifier_block reserve_mem_nb = {
  3243. .notifier_call = reserve_mem_notifier,
  3244. };
  3245. static int __meminit init_reserve_notifier(void)
  3246. {
  3247. if (register_hotmemory_notifier(&reserve_mem_nb))
  3248. pr_err("Failed registering memory add/remove notifier for admin reserve\n");
  3249. return 0;
  3250. }
  3251. subsys_initcall(init_reserve_notifier);