mmap.c 81 KB

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