mmap.c 88 KB

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