mmap.c 81 KB

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