mmap.c 96 KB

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