mmap.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099
  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 += get_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_inode(file)->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_inode(file)->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_write(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. unsigned long *populate)
  1044. {
  1045. struct mm_struct * mm = current->mm;
  1046. struct inode *inode;
  1047. vm_flags_t vm_flags;
  1048. *populate = 0;
  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_inode(file) : 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. /*
  1152. * Set 'VM_NORESERVE' if we should not account for the
  1153. * memory use of this mapping.
  1154. */
  1155. if (flags & MAP_NORESERVE) {
  1156. /* We honor MAP_NORESERVE if allowed to overcommit */
  1157. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  1158. vm_flags |= VM_NORESERVE;
  1159. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  1160. if (file && is_file_hugepages(file))
  1161. vm_flags |= VM_NORESERVE;
  1162. }
  1163. addr = mmap_region(file, addr, len, vm_flags, pgoff);
  1164. if (!IS_ERR_VALUE(addr) &&
  1165. ((vm_flags & VM_LOCKED) ||
  1166. (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
  1167. *populate = len;
  1168. return addr;
  1169. }
  1170. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1171. unsigned long, prot, unsigned long, flags,
  1172. unsigned long, fd, unsigned long, pgoff)
  1173. {
  1174. struct file *file = NULL;
  1175. unsigned long retval = -EBADF;
  1176. if (!(flags & MAP_ANONYMOUS)) {
  1177. audit_mmap_fd(fd, flags);
  1178. if (unlikely(flags & MAP_HUGETLB))
  1179. return -EINVAL;
  1180. file = fget(fd);
  1181. if (!file)
  1182. goto out;
  1183. } else if (flags & MAP_HUGETLB) {
  1184. struct user_struct *user = NULL;
  1185. /*
  1186. * VM_NORESERVE is used because the reservations will be
  1187. * taken when vm_ops->mmap() is called
  1188. * A dummy user value is used because we are not locking
  1189. * memory so no accounting is necessary
  1190. */
  1191. file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
  1192. VM_NORESERVE,
  1193. &user, HUGETLB_ANONHUGE_INODE,
  1194. (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  1195. if (IS_ERR(file))
  1196. return PTR_ERR(file);
  1197. }
  1198. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1199. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1200. if (file)
  1201. fput(file);
  1202. out:
  1203. return retval;
  1204. }
  1205. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1206. struct mmap_arg_struct {
  1207. unsigned long addr;
  1208. unsigned long len;
  1209. unsigned long prot;
  1210. unsigned long flags;
  1211. unsigned long fd;
  1212. unsigned long offset;
  1213. };
  1214. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1215. {
  1216. struct mmap_arg_struct a;
  1217. if (copy_from_user(&a, arg, sizeof(a)))
  1218. return -EFAULT;
  1219. if (a.offset & ~PAGE_MASK)
  1220. return -EINVAL;
  1221. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1222. a.offset >> PAGE_SHIFT);
  1223. }
  1224. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1225. /*
  1226. * Some shared mappigns will want the pages marked read-only
  1227. * to track write events. If so, we'll downgrade vm_page_prot
  1228. * to the private version (using protection_map[] without the
  1229. * VM_SHARED bit).
  1230. */
  1231. int vma_wants_writenotify(struct vm_area_struct *vma)
  1232. {
  1233. vm_flags_t vm_flags = vma->vm_flags;
  1234. /* If it was private or non-writable, the write bit is already clear */
  1235. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  1236. return 0;
  1237. /* The backer wishes to know when pages are first written to? */
  1238. if (vma->vm_ops && vma->vm_ops->page_mkwrite)
  1239. return 1;
  1240. /* The open routine did something to the protections already? */
  1241. if (pgprot_val(vma->vm_page_prot) !=
  1242. pgprot_val(vm_get_page_prot(vm_flags)))
  1243. return 0;
  1244. /* Specialty mapping? */
  1245. if (vm_flags & VM_PFNMAP)
  1246. return 0;
  1247. /* Can the mapping track the dirty pages? */
  1248. return vma->vm_file && vma->vm_file->f_mapping &&
  1249. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  1250. }
  1251. /*
  1252. * We account for memory if it's a private writeable mapping,
  1253. * not hugepages and VM_NORESERVE wasn't set.
  1254. */
  1255. static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
  1256. {
  1257. /*
  1258. * hugetlb has its own accounting separate from the core VM
  1259. * VM_HUGETLB may not be set yet so we cannot check for that flag.
  1260. */
  1261. if (file && is_file_hugepages(file))
  1262. return 0;
  1263. return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
  1264. }
  1265. unsigned long mmap_region(struct file *file, unsigned long addr,
  1266. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
  1267. {
  1268. struct mm_struct *mm = current->mm;
  1269. struct vm_area_struct *vma, *prev;
  1270. int correct_wcount = 0;
  1271. int error;
  1272. struct rb_node **rb_link, *rb_parent;
  1273. unsigned long charged = 0;
  1274. struct inode *inode = file ? file_inode(file) : NULL;
  1275. /* Clear old maps */
  1276. error = -ENOMEM;
  1277. munmap_back:
  1278. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  1279. if (do_munmap(mm, addr, len))
  1280. return -ENOMEM;
  1281. goto munmap_back;
  1282. }
  1283. /* Check against address space limit. */
  1284. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1285. return -ENOMEM;
  1286. /*
  1287. * Private writable mapping: check memory availability
  1288. */
  1289. if (accountable_mapping(file, vm_flags)) {
  1290. charged = len >> PAGE_SHIFT;
  1291. if (security_vm_enough_memory_mm(mm, charged))
  1292. return -ENOMEM;
  1293. vm_flags |= VM_ACCOUNT;
  1294. }
  1295. /*
  1296. * Can we just expand an old mapping?
  1297. */
  1298. vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
  1299. if (vma)
  1300. goto out;
  1301. /*
  1302. * Determine the object being mapped and call the appropriate
  1303. * specific mapper. the address has already been validated, but
  1304. * not unmapped, but the maps are removed from the list.
  1305. */
  1306. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1307. if (!vma) {
  1308. error = -ENOMEM;
  1309. goto unacct_error;
  1310. }
  1311. vma->vm_mm = mm;
  1312. vma->vm_start = addr;
  1313. vma->vm_end = addr + len;
  1314. vma->vm_flags = vm_flags;
  1315. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1316. vma->vm_pgoff = pgoff;
  1317. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1318. error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
  1319. if (file) {
  1320. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1321. goto free_vma;
  1322. if (vm_flags & VM_DENYWRITE) {
  1323. error = deny_write_access(file);
  1324. if (error)
  1325. goto free_vma;
  1326. correct_wcount = 1;
  1327. }
  1328. vma->vm_file = get_file(file);
  1329. error = file->f_op->mmap(file, vma);
  1330. if (error)
  1331. goto unmap_and_free_vma;
  1332. /* Can addr have changed??
  1333. *
  1334. * Answer: Yes, several device drivers can do it in their
  1335. * f_op->mmap method. -DaveM
  1336. * Bug: If addr is changed, prev, rb_link, rb_parent should
  1337. * be updated for vma_link()
  1338. */
  1339. WARN_ON_ONCE(addr != vma->vm_start);
  1340. addr = vma->vm_start;
  1341. pgoff = vma->vm_pgoff;
  1342. vm_flags = vma->vm_flags;
  1343. } else if (vm_flags & VM_SHARED) {
  1344. if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
  1345. goto free_vma;
  1346. error = shmem_zero_setup(vma);
  1347. if (error)
  1348. goto free_vma;
  1349. }
  1350. if (vma_wants_writenotify(vma)) {
  1351. pgprot_t pprot = vma->vm_page_prot;
  1352. /* Can vma->vm_page_prot have changed??
  1353. *
  1354. * Answer: Yes, drivers may have changed it in their
  1355. * f_op->mmap method.
  1356. *
  1357. * Ensures that vmas marked as uncached stay that way.
  1358. */
  1359. vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
  1360. if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
  1361. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1362. }
  1363. vma_link(mm, vma, prev, rb_link, rb_parent);
  1364. file = vma->vm_file;
  1365. /* Once vma denies write, undo our temporary denial count */
  1366. if (correct_wcount)
  1367. atomic_inc(&inode->i_writecount);
  1368. out:
  1369. perf_event_mmap(vma);
  1370. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1371. if (vm_flags & VM_LOCKED) {
  1372. if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
  1373. vma == get_gate_vma(current->mm)))
  1374. mm->locked_vm += (len >> PAGE_SHIFT);
  1375. else
  1376. vma->vm_flags &= ~VM_LOCKED;
  1377. }
  1378. if (file)
  1379. uprobe_mmap(vma);
  1380. return addr;
  1381. unmap_and_free_vma:
  1382. if (correct_wcount)
  1383. atomic_inc(&inode->i_writecount);
  1384. vma->vm_file = NULL;
  1385. fput(file);
  1386. /* Undo any partial mapping done by a device driver. */
  1387. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1388. charged = 0;
  1389. free_vma:
  1390. kmem_cache_free(vm_area_cachep, vma);
  1391. unacct_error:
  1392. if (charged)
  1393. vm_unacct_memory(charged);
  1394. return error;
  1395. }
  1396. unsigned long unmapped_area(struct vm_unmapped_area_info *info)
  1397. {
  1398. /*
  1399. * We implement the search by looking for an rbtree node that
  1400. * immediately follows a suitable gap. That is,
  1401. * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
  1402. * - gap_end = vma->vm_start >= info->low_limit + length;
  1403. * - gap_end - gap_start >= length
  1404. */
  1405. struct mm_struct *mm = current->mm;
  1406. struct vm_area_struct *vma;
  1407. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1408. /* Adjust search length to account for worst case alignment overhead */
  1409. length = info->length + info->align_mask;
  1410. if (length < info->length)
  1411. return -ENOMEM;
  1412. /* Adjust search limits by the desired length */
  1413. if (info->high_limit < length)
  1414. return -ENOMEM;
  1415. high_limit = info->high_limit - length;
  1416. if (info->low_limit > high_limit)
  1417. return -ENOMEM;
  1418. low_limit = info->low_limit + length;
  1419. /* Check if rbtree root looks promising */
  1420. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1421. goto check_highest;
  1422. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1423. if (vma->rb_subtree_gap < length)
  1424. goto check_highest;
  1425. while (true) {
  1426. /* Visit left subtree if it looks promising */
  1427. gap_end = vma->vm_start;
  1428. if (gap_end >= low_limit && vma->vm_rb.rb_left) {
  1429. struct vm_area_struct *left =
  1430. rb_entry(vma->vm_rb.rb_left,
  1431. struct vm_area_struct, vm_rb);
  1432. if (left->rb_subtree_gap >= length) {
  1433. vma = left;
  1434. continue;
  1435. }
  1436. }
  1437. gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
  1438. check_current:
  1439. /* Check if current node has a suitable gap */
  1440. if (gap_start > high_limit)
  1441. return -ENOMEM;
  1442. if (gap_end >= low_limit && gap_end - gap_start >= length)
  1443. goto found;
  1444. /* Visit right subtree if it looks promising */
  1445. if (vma->vm_rb.rb_right) {
  1446. struct vm_area_struct *right =
  1447. rb_entry(vma->vm_rb.rb_right,
  1448. struct vm_area_struct, vm_rb);
  1449. if (right->rb_subtree_gap >= length) {
  1450. vma = right;
  1451. continue;
  1452. }
  1453. }
  1454. /* Go back up the rbtree to find next candidate node */
  1455. while (true) {
  1456. struct rb_node *prev = &vma->vm_rb;
  1457. if (!rb_parent(prev))
  1458. goto check_highest;
  1459. vma = rb_entry(rb_parent(prev),
  1460. struct vm_area_struct, vm_rb);
  1461. if (prev == vma->vm_rb.rb_left) {
  1462. gap_start = vma->vm_prev->vm_end;
  1463. gap_end = vma->vm_start;
  1464. goto check_current;
  1465. }
  1466. }
  1467. }
  1468. check_highest:
  1469. /* Check highest gap, which does not precede any rbtree node */
  1470. gap_start = mm->highest_vm_end;
  1471. gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
  1472. if (gap_start > high_limit)
  1473. return -ENOMEM;
  1474. found:
  1475. /* We found a suitable gap. Clip it with the original low_limit. */
  1476. if (gap_start < info->low_limit)
  1477. gap_start = info->low_limit;
  1478. /* Adjust gap address to the desired alignment */
  1479. gap_start += (info->align_offset - gap_start) & info->align_mask;
  1480. VM_BUG_ON(gap_start + info->length > info->high_limit);
  1481. VM_BUG_ON(gap_start + info->length > gap_end);
  1482. return gap_start;
  1483. }
  1484. unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
  1485. {
  1486. struct mm_struct *mm = current->mm;
  1487. struct vm_area_struct *vma;
  1488. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1489. /* Adjust search length to account for worst case alignment overhead */
  1490. length = info->length + info->align_mask;
  1491. if (length < info->length)
  1492. return -ENOMEM;
  1493. /*
  1494. * Adjust search limits by the desired length.
  1495. * See implementation comment at top of unmapped_area().
  1496. */
  1497. gap_end = info->high_limit;
  1498. if (gap_end < length)
  1499. return -ENOMEM;
  1500. high_limit = gap_end - length;
  1501. if (info->low_limit > high_limit)
  1502. return -ENOMEM;
  1503. low_limit = info->low_limit + length;
  1504. /* Check highest gap, which does not precede any rbtree node */
  1505. gap_start = mm->highest_vm_end;
  1506. if (gap_start <= high_limit)
  1507. goto found_highest;
  1508. /* Check if rbtree root looks promising */
  1509. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1510. return -ENOMEM;
  1511. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1512. if (vma->rb_subtree_gap < length)
  1513. return -ENOMEM;
  1514. while (true) {
  1515. /* Visit right subtree if it looks promising */
  1516. gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
  1517. if (gap_start <= high_limit && vma->vm_rb.rb_right) {
  1518. struct vm_area_struct *right =
  1519. rb_entry(vma->vm_rb.rb_right,
  1520. struct vm_area_struct, vm_rb);
  1521. if (right->rb_subtree_gap >= length) {
  1522. vma = right;
  1523. continue;
  1524. }
  1525. }
  1526. check_current:
  1527. /* Check if current node has a suitable gap */
  1528. gap_end = vma->vm_start;
  1529. if (gap_end < low_limit)
  1530. return -ENOMEM;
  1531. if (gap_start <= high_limit && gap_end - gap_start >= length)
  1532. goto found;
  1533. /* Visit left subtree if it looks promising */
  1534. if (vma->vm_rb.rb_left) {
  1535. struct vm_area_struct *left =
  1536. rb_entry(vma->vm_rb.rb_left,
  1537. struct vm_area_struct, vm_rb);
  1538. if (left->rb_subtree_gap >= length) {
  1539. vma = left;
  1540. continue;
  1541. }
  1542. }
  1543. /* Go back up the rbtree to find next candidate node */
  1544. while (true) {
  1545. struct rb_node *prev = &vma->vm_rb;
  1546. if (!rb_parent(prev))
  1547. return -ENOMEM;
  1548. vma = rb_entry(rb_parent(prev),
  1549. struct vm_area_struct, vm_rb);
  1550. if (prev == vma->vm_rb.rb_right) {
  1551. gap_start = vma->vm_prev ?
  1552. vma->vm_prev->vm_end : 0;
  1553. goto check_current;
  1554. }
  1555. }
  1556. }
  1557. found:
  1558. /* We found a suitable gap. Clip it with the original high_limit. */
  1559. if (gap_end > info->high_limit)
  1560. gap_end = info->high_limit;
  1561. found_highest:
  1562. /* Compute highest gap address at the desired alignment */
  1563. gap_end -= info->length;
  1564. gap_end -= (gap_end - info->align_offset) & info->align_mask;
  1565. VM_BUG_ON(gap_end < info->low_limit);
  1566. VM_BUG_ON(gap_end < gap_start);
  1567. return gap_end;
  1568. }
  1569. /* Get an address range which is currently unmapped.
  1570. * For shmat() with addr=0.
  1571. *
  1572. * Ugly calling convention alert:
  1573. * Return value with the low bits set means error value,
  1574. * ie
  1575. * if (ret & ~PAGE_MASK)
  1576. * error = ret;
  1577. *
  1578. * This function "knows" that -ENOMEM has the bits set.
  1579. */
  1580. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1581. unsigned long
  1582. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1583. unsigned long len, unsigned long pgoff, unsigned long flags)
  1584. {
  1585. struct mm_struct *mm = current->mm;
  1586. struct vm_area_struct *vma;
  1587. struct vm_unmapped_area_info info;
  1588. if (len > TASK_SIZE)
  1589. return -ENOMEM;
  1590. if (flags & MAP_FIXED)
  1591. return addr;
  1592. if (addr) {
  1593. addr = PAGE_ALIGN(addr);
  1594. vma = find_vma(mm, addr);
  1595. if (TASK_SIZE - len >= addr &&
  1596. (!vma || addr + len <= vma->vm_start))
  1597. return addr;
  1598. }
  1599. info.flags = 0;
  1600. info.length = len;
  1601. info.low_limit = TASK_UNMAPPED_BASE;
  1602. info.high_limit = TASK_SIZE;
  1603. info.align_mask = 0;
  1604. return vm_unmapped_area(&info);
  1605. }
  1606. #endif
  1607. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1608. {
  1609. /*
  1610. * Is this a new hole at the lowest possible address?
  1611. */
  1612. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
  1613. mm->free_area_cache = addr;
  1614. }
  1615. /*
  1616. * This mmap-allocator allocates new areas top-down from below the
  1617. * stack's low limit (the base):
  1618. */
  1619. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1620. unsigned long
  1621. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1622. const unsigned long len, const unsigned long pgoff,
  1623. const unsigned long flags)
  1624. {
  1625. struct vm_area_struct *vma;
  1626. struct mm_struct *mm = current->mm;
  1627. unsigned long addr = addr0;
  1628. struct vm_unmapped_area_info info;
  1629. /* requested length too big for entire address space */
  1630. if (len > TASK_SIZE)
  1631. return -ENOMEM;
  1632. if (flags & MAP_FIXED)
  1633. return addr;
  1634. /* requesting a specific address */
  1635. if (addr) {
  1636. addr = PAGE_ALIGN(addr);
  1637. vma = find_vma(mm, addr);
  1638. if (TASK_SIZE - len >= addr &&
  1639. (!vma || addr + len <= vma->vm_start))
  1640. return addr;
  1641. }
  1642. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  1643. info.length = len;
  1644. info.low_limit = PAGE_SIZE;
  1645. info.high_limit = mm->mmap_base;
  1646. info.align_mask = 0;
  1647. addr = vm_unmapped_area(&info);
  1648. /*
  1649. * A failed mmap() very likely causes application failure,
  1650. * so fall back to the bottom-up function here. This scenario
  1651. * can happen with large stack limits and large mmap()
  1652. * allocations.
  1653. */
  1654. if (addr & ~PAGE_MASK) {
  1655. VM_BUG_ON(addr != -ENOMEM);
  1656. info.flags = 0;
  1657. info.low_limit = TASK_UNMAPPED_BASE;
  1658. info.high_limit = TASK_SIZE;
  1659. addr = vm_unmapped_area(&info);
  1660. }
  1661. return addr;
  1662. }
  1663. #endif
  1664. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1665. {
  1666. /*
  1667. * Is this a new hole at the highest possible address?
  1668. */
  1669. if (addr > mm->free_area_cache)
  1670. mm->free_area_cache = addr;
  1671. /* dont allow allocations above current base */
  1672. if (mm->free_area_cache > mm->mmap_base)
  1673. mm->free_area_cache = mm->mmap_base;
  1674. }
  1675. unsigned long
  1676. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1677. unsigned long pgoff, unsigned long flags)
  1678. {
  1679. unsigned long (*get_area)(struct file *, unsigned long,
  1680. unsigned long, unsigned long, unsigned long);
  1681. unsigned long error = arch_mmap_check(addr, len, flags);
  1682. if (error)
  1683. return error;
  1684. /* Careful about overflows.. */
  1685. if (len > TASK_SIZE)
  1686. return -ENOMEM;
  1687. get_area = current->mm->get_unmapped_area;
  1688. if (file && file->f_op && file->f_op->get_unmapped_area)
  1689. get_area = file->f_op->get_unmapped_area;
  1690. addr = get_area(file, addr, len, pgoff, flags);
  1691. if (IS_ERR_VALUE(addr))
  1692. return addr;
  1693. if (addr > TASK_SIZE - len)
  1694. return -ENOMEM;
  1695. if (addr & ~PAGE_MASK)
  1696. return -EINVAL;
  1697. addr = arch_rebalance_pgtables(addr, len);
  1698. error = security_mmap_addr(addr);
  1699. return error ? error : addr;
  1700. }
  1701. EXPORT_SYMBOL(get_unmapped_area);
  1702. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1703. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1704. {
  1705. struct vm_area_struct *vma = NULL;
  1706. if (WARN_ON_ONCE(!mm)) /* Remove this in linux-3.6 */
  1707. return NULL;
  1708. /* Check the cache first. */
  1709. /* (Cache hit rate is typically around 35%.) */
  1710. vma = mm->mmap_cache;
  1711. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1712. struct rb_node *rb_node;
  1713. rb_node = mm->mm_rb.rb_node;
  1714. vma = NULL;
  1715. while (rb_node) {
  1716. struct vm_area_struct *vma_tmp;
  1717. vma_tmp = rb_entry(rb_node,
  1718. struct vm_area_struct, vm_rb);
  1719. if (vma_tmp->vm_end > addr) {
  1720. vma = vma_tmp;
  1721. if (vma_tmp->vm_start <= addr)
  1722. break;
  1723. rb_node = rb_node->rb_left;
  1724. } else
  1725. rb_node = rb_node->rb_right;
  1726. }
  1727. if (vma)
  1728. mm->mmap_cache = vma;
  1729. }
  1730. return vma;
  1731. }
  1732. EXPORT_SYMBOL(find_vma);
  1733. /*
  1734. * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
  1735. */
  1736. struct vm_area_struct *
  1737. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1738. struct vm_area_struct **pprev)
  1739. {
  1740. struct vm_area_struct *vma;
  1741. vma = find_vma(mm, addr);
  1742. if (vma) {
  1743. *pprev = vma->vm_prev;
  1744. } else {
  1745. struct rb_node *rb_node = mm->mm_rb.rb_node;
  1746. *pprev = NULL;
  1747. while (rb_node) {
  1748. *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1749. rb_node = rb_node->rb_right;
  1750. }
  1751. }
  1752. return vma;
  1753. }
  1754. /*
  1755. * Verify that the stack growth is acceptable and
  1756. * update accounting. This is shared with both the
  1757. * grow-up and grow-down cases.
  1758. */
  1759. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1760. {
  1761. struct mm_struct *mm = vma->vm_mm;
  1762. struct rlimit *rlim = current->signal->rlim;
  1763. unsigned long new_start;
  1764. /* address space limit tests */
  1765. if (!may_expand_vm(mm, grow))
  1766. return -ENOMEM;
  1767. /* Stack limit test */
  1768. if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
  1769. return -ENOMEM;
  1770. /* mlock limit tests */
  1771. if (vma->vm_flags & VM_LOCKED) {
  1772. unsigned long locked;
  1773. unsigned long limit;
  1774. locked = mm->locked_vm + grow;
  1775. limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
  1776. limit >>= PAGE_SHIFT;
  1777. if (locked > limit && !capable(CAP_IPC_LOCK))
  1778. return -ENOMEM;
  1779. }
  1780. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1781. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1782. vma->vm_end - size;
  1783. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1784. return -EFAULT;
  1785. /*
  1786. * Overcommit.. This must be the final test, as it will
  1787. * update security statistics.
  1788. */
  1789. if (security_vm_enough_memory_mm(mm, grow))
  1790. return -ENOMEM;
  1791. /* Ok, everything looks good - let it rip */
  1792. if (vma->vm_flags & VM_LOCKED)
  1793. mm->locked_vm += grow;
  1794. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1795. return 0;
  1796. }
  1797. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1798. /*
  1799. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1800. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1801. */
  1802. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1803. {
  1804. int error;
  1805. if (!(vma->vm_flags & VM_GROWSUP))
  1806. return -EFAULT;
  1807. /*
  1808. * We must make sure the anon_vma is allocated
  1809. * so that the anon_vma locking is not a noop.
  1810. */
  1811. if (unlikely(anon_vma_prepare(vma)))
  1812. return -ENOMEM;
  1813. vma_lock_anon_vma(vma);
  1814. /*
  1815. * vma->vm_start/vm_end cannot change under us because the caller
  1816. * is required to hold the mmap_sem in read mode. We need the
  1817. * anon_vma lock to serialize against concurrent expand_stacks.
  1818. * Also guard against wrapping around to address 0.
  1819. */
  1820. if (address < PAGE_ALIGN(address+4))
  1821. address = PAGE_ALIGN(address+4);
  1822. else {
  1823. vma_unlock_anon_vma(vma);
  1824. return -ENOMEM;
  1825. }
  1826. error = 0;
  1827. /* Somebody else might have raced and expanded it already */
  1828. if (address > vma->vm_end) {
  1829. unsigned long size, grow;
  1830. size = address - vma->vm_start;
  1831. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1832. error = -ENOMEM;
  1833. if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
  1834. error = acct_stack_growth(vma, size, grow);
  1835. if (!error) {
  1836. /*
  1837. * vma_gap_update() doesn't support concurrent
  1838. * updates, but we only hold a shared mmap_sem
  1839. * lock here, so we need to protect against
  1840. * concurrent vma expansions.
  1841. * vma_lock_anon_vma() doesn't help here, as
  1842. * we don't guarantee that all growable vmas
  1843. * in a mm share the same root anon vma.
  1844. * So, we reuse mm->page_table_lock to guard
  1845. * against concurrent vma expansions.
  1846. */
  1847. spin_lock(&vma->vm_mm->page_table_lock);
  1848. anon_vma_interval_tree_pre_update_vma(vma);
  1849. vma->vm_end = address;
  1850. anon_vma_interval_tree_post_update_vma(vma);
  1851. if (vma->vm_next)
  1852. vma_gap_update(vma->vm_next);
  1853. else
  1854. vma->vm_mm->highest_vm_end = address;
  1855. spin_unlock(&vma->vm_mm->page_table_lock);
  1856. perf_event_mmap(vma);
  1857. }
  1858. }
  1859. }
  1860. vma_unlock_anon_vma(vma);
  1861. khugepaged_enter_vma_merge(vma);
  1862. validate_mm(vma->vm_mm);
  1863. return error;
  1864. }
  1865. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1866. /*
  1867. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1868. */
  1869. int expand_downwards(struct vm_area_struct *vma,
  1870. unsigned long address)
  1871. {
  1872. int error;
  1873. /*
  1874. * We must make sure the anon_vma is allocated
  1875. * so that the anon_vma locking is not a noop.
  1876. */
  1877. if (unlikely(anon_vma_prepare(vma)))
  1878. return -ENOMEM;
  1879. address &= PAGE_MASK;
  1880. error = security_mmap_addr(address);
  1881. if (error)
  1882. return error;
  1883. vma_lock_anon_vma(vma);
  1884. /*
  1885. * vma->vm_start/vm_end cannot change under us because the caller
  1886. * is required to hold the mmap_sem in read mode. We need the
  1887. * anon_vma lock to serialize against concurrent expand_stacks.
  1888. */
  1889. /* Somebody else might have raced and expanded it already */
  1890. if (address < vma->vm_start) {
  1891. unsigned long size, grow;
  1892. size = vma->vm_end - address;
  1893. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1894. error = -ENOMEM;
  1895. if (grow <= vma->vm_pgoff) {
  1896. error = acct_stack_growth(vma, size, grow);
  1897. if (!error) {
  1898. /*
  1899. * vma_gap_update() doesn't support concurrent
  1900. * updates, but we only hold a shared mmap_sem
  1901. * lock here, so we need to protect against
  1902. * concurrent vma expansions.
  1903. * vma_lock_anon_vma() doesn't help here, as
  1904. * we don't guarantee that all growable vmas
  1905. * in a mm share the same root anon vma.
  1906. * So, we reuse mm->page_table_lock to guard
  1907. * against concurrent vma expansions.
  1908. */
  1909. spin_lock(&vma->vm_mm->page_table_lock);
  1910. anon_vma_interval_tree_pre_update_vma(vma);
  1911. vma->vm_start = address;
  1912. vma->vm_pgoff -= grow;
  1913. anon_vma_interval_tree_post_update_vma(vma);
  1914. vma_gap_update(vma);
  1915. spin_unlock(&vma->vm_mm->page_table_lock);
  1916. perf_event_mmap(vma);
  1917. }
  1918. }
  1919. }
  1920. vma_unlock_anon_vma(vma);
  1921. khugepaged_enter_vma_merge(vma);
  1922. validate_mm(vma->vm_mm);
  1923. return error;
  1924. }
  1925. /*
  1926. * Note how expand_stack() refuses to expand the stack all the way to
  1927. * abut the next virtual mapping, *unless* that mapping itself is also
  1928. * a stack mapping. We want to leave room for a guard page, after all
  1929. * (the guard page itself is not added here, that is done by the
  1930. * actual page faulting logic)
  1931. *
  1932. * This matches the behavior of the guard page logic (see mm/memory.c:
  1933. * check_stack_guard_page()), which only allows the guard page to be
  1934. * removed under these circumstances.
  1935. */
  1936. #ifdef CONFIG_STACK_GROWSUP
  1937. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1938. {
  1939. struct vm_area_struct *next;
  1940. address &= PAGE_MASK;
  1941. next = vma->vm_next;
  1942. if (next && next->vm_start == address + PAGE_SIZE) {
  1943. if (!(next->vm_flags & VM_GROWSUP))
  1944. return -ENOMEM;
  1945. }
  1946. return expand_upwards(vma, address);
  1947. }
  1948. struct vm_area_struct *
  1949. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1950. {
  1951. struct vm_area_struct *vma, *prev;
  1952. addr &= PAGE_MASK;
  1953. vma = find_vma_prev(mm, addr, &prev);
  1954. if (vma && (vma->vm_start <= addr))
  1955. return vma;
  1956. if (!prev || expand_stack(prev, addr))
  1957. return NULL;
  1958. if (prev->vm_flags & VM_LOCKED)
  1959. __mlock_vma_pages_range(prev, addr, prev->vm_end, NULL);
  1960. return prev;
  1961. }
  1962. #else
  1963. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1964. {
  1965. struct vm_area_struct *prev;
  1966. address &= PAGE_MASK;
  1967. prev = vma->vm_prev;
  1968. if (prev && prev->vm_end == address) {
  1969. if (!(prev->vm_flags & VM_GROWSDOWN))
  1970. return -ENOMEM;
  1971. }
  1972. return expand_downwards(vma, address);
  1973. }
  1974. struct vm_area_struct *
  1975. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1976. {
  1977. struct vm_area_struct * vma;
  1978. unsigned long start;
  1979. addr &= PAGE_MASK;
  1980. vma = find_vma(mm,addr);
  1981. if (!vma)
  1982. return NULL;
  1983. if (vma->vm_start <= addr)
  1984. return vma;
  1985. if (!(vma->vm_flags & VM_GROWSDOWN))
  1986. return NULL;
  1987. start = vma->vm_start;
  1988. if (expand_stack(vma, addr))
  1989. return NULL;
  1990. if (vma->vm_flags & VM_LOCKED)
  1991. __mlock_vma_pages_range(vma, addr, start, NULL);
  1992. return vma;
  1993. }
  1994. #endif
  1995. /*
  1996. * Ok - we have the memory areas we should free on the vma list,
  1997. * so release them, and do the vma updates.
  1998. *
  1999. * Called with the mm semaphore held.
  2000. */
  2001. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  2002. {
  2003. unsigned long nr_accounted = 0;
  2004. /* Update high watermark before we lower total_vm */
  2005. update_hiwater_vm(mm);
  2006. do {
  2007. long nrpages = vma_pages(vma);
  2008. if (vma->vm_flags & VM_ACCOUNT)
  2009. nr_accounted += nrpages;
  2010. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  2011. vma = remove_vma(vma);
  2012. } while (vma);
  2013. vm_unacct_memory(nr_accounted);
  2014. validate_mm(mm);
  2015. }
  2016. /*
  2017. * Get rid of page table information in the indicated region.
  2018. *
  2019. * Called with the mm semaphore held.
  2020. */
  2021. static void unmap_region(struct mm_struct *mm,
  2022. struct vm_area_struct *vma, struct vm_area_struct *prev,
  2023. unsigned long start, unsigned long end)
  2024. {
  2025. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  2026. struct mmu_gather tlb;
  2027. lru_add_drain();
  2028. tlb_gather_mmu(&tlb, mm, 0);
  2029. update_hiwater_rss(mm);
  2030. unmap_vmas(&tlb, vma, start, end);
  2031. free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
  2032. next ? next->vm_start : 0);
  2033. tlb_finish_mmu(&tlb, start, end);
  2034. }
  2035. /*
  2036. * Create a list of vma's touched by the unmap, removing them from the mm's
  2037. * vma list as we go..
  2038. */
  2039. static void
  2040. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  2041. struct vm_area_struct *prev, unsigned long end)
  2042. {
  2043. struct vm_area_struct **insertion_point;
  2044. struct vm_area_struct *tail_vma = NULL;
  2045. unsigned long addr;
  2046. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  2047. vma->vm_prev = NULL;
  2048. do {
  2049. vma_rb_erase(vma, &mm->mm_rb);
  2050. mm->map_count--;
  2051. tail_vma = vma;
  2052. vma = vma->vm_next;
  2053. } while (vma && vma->vm_start < end);
  2054. *insertion_point = vma;
  2055. if (vma) {
  2056. vma->vm_prev = prev;
  2057. vma_gap_update(vma);
  2058. } else
  2059. mm->highest_vm_end = prev ? prev->vm_end : 0;
  2060. tail_vma->vm_next = NULL;
  2061. if (mm->unmap_area == arch_unmap_area)
  2062. addr = prev ? prev->vm_end : mm->mmap_base;
  2063. else
  2064. addr = vma ? vma->vm_start : mm->mmap_base;
  2065. mm->unmap_area(mm, addr);
  2066. mm->mmap_cache = NULL; /* Kill the cache. */
  2067. }
  2068. /*
  2069. * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
  2070. * munmap path where it doesn't make sense to fail.
  2071. */
  2072. static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  2073. unsigned long addr, int new_below)
  2074. {
  2075. struct mempolicy *pol;
  2076. struct vm_area_struct *new;
  2077. int err = -ENOMEM;
  2078. if (is_vm_hugetlb_page(vma) && (addr &
  2079. ~(huge_page_mask(hstate_vma(vma)))))
  2080. return -EINVAL;
  2081. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2082. if (!new)
  2083. goto out_err;
  2084. /* most fields are the same, copy all, and then fixup */
  2085. *new = *vma;
  2086. INIT_LIST_HEAD(&new->anon_vma_chain);
  2087. if (new_below)
  2088. new->vm_end = addr;
  2089. else {
  2090. new->vm_start = addr;
  2091. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  2092. }
  2093. pol = mpol_dup(vma_policy(vma));
  2094. if (IS_ERR(pol)) {
  2095. err = PTR_ERR(pol);
  2096. goto out_free_vma;
  2097. }
  2098. vma_set_policy(new, pol);
  2099. if (anon_vma_clone(new, vma))
  2100. goto out_free_mpol;
  2101. if (new->vm_file)
  2102. get_file(new->vm_file);
  2103. if (new->vm_ops && new->vm_ops->open)
  2104. new->vm_ops->open(new);
  2105. if (new_below)
  2106. err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  2107. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  2108. else
  2109. err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  2110. /* Success. */
  2111. if (!err)
  2112. return 0;
  2113. /* Clean everything up if vma_adjust failed. */
  2114. if (new->vm_ops && new->vm_ops->close)
  2115. new->vm_ops->close(new);
  2116. if (new->vm_file)
  2117. fput(new->vm_file);
  2118. unlink_anon_vmas(new);
  2119. out_free_mpol:
  2120. mpol_put(pol);
  2121. out_free_vma:
  2122. kmem_cache_free(vm_area_cachep, new);
  2123. out_err:
  2124. return err;
  2125. }
  2126. /*
  2127. * Split a vma into two pieces at address 'addr', a new vma is allocated
  2128. * either for the first part or the tail.
  2129. */
  2130. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  2131. unsigned long addr, int new_below)
  2132. {
  2133. if (mm->map_count >= sysctl_max_map_count)
  2134. return -ENOMEM;
  2135. return __split_vma(mm, vma, addr, new_below);
  2136. }
  2137. /* Munmap is split into 2 main parts -- this part which finds
  2138. * what needs doing, and the areas themselves, which do the
  2139. * work. This now handles partial unmappings.
  2140. * Jeremy Fitzhardinge <jeremy@goop.org>
  2141. */
  2142. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  2143. {
  2144. unsigned long end;
  2145. struct vm_area_struct *vma, *prev, *last;
  2146. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  2147. return -EINVAL;
  2148. if ((len = PAGE_ALIGN(len)) == 0)
  2149. return -EINVAL;
  2150. /* Find the first overlapping VMA */
  2151. vma = find_vma(mm, start);
  2152. if (!vma)
  2153. return 0;
  2154. prev = vma->vm_prev;
  2155. /* we have start < vma->vm_end */
  2156. /* if it doesn't overlap, we have nothing.. */
  2157. end = start + len;
  2158. if (vma->vm_start >= end)
  2159. return 0;
  2160. /*
  2161. * If we need to split any vma, do it now to save pain later.
  2162. *
  2163. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  2164. * unmapped vm_area_struct will remain in use: so lower split_vma
  2165. * places tmp vma above, and higher split_vma places tmp vma below.
  2166. */
  2167. if (start > vma->vm_start) {
  2168. int error;
  2169. /*
  2170. * Make sure that map_count on return from munmap() will
  2171. * not exceed its limit; but let map_count go just above
  2172. * its limit temporarily, to help free resources as expected.
  2173. */
  2174. if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
  2175. return -ENOMEM;
  2176. error = __split_vma(mm, vma, start, 0);
  2177. if (error)
  2178. return error;
  2179. prev = vma;
  2180. }
  2181. /* Does it split the last one? */
  2182. last = find_vma(mm, end);
  2183. if (last && end > last->vm_start) {
  2184. int error = __split_vma(mm, last, end, 1);
  2185. if (error)
  2186. return error;
  2187. }
  2188. vma = prev? prev->vm_next: mm->mmap;
  2189. /*
  2190. * unlock any mlock()ed ranges before detaching vmas
  2191. */
  2192. if (mm->locked_vm) {
  2193. struct vm_area_struct *tmp = vma;
  2194. while (tmp && tmp->vm_start < end) {
  2195. if (tmp->vm_flags & VM_LOCKED) {
  2196. mm->locked_vm -= vma_pages(tmp);
  2197. munlock_vma_pages_all(tmp);
  2198. }
  2199. tmp = tmp->vm_next;
  2200. }
  2201. }
  2202. /*
  2203. * Remove the vma's, and unmap the actual pages
  2204. */
  2205. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  2206. unmap_region(mm, vma, prev, start, end);
  2207. /* Fix up all other VM information */
  2208. remove_vma_list(mm, vma);
  2209. return 0;
  2210. }
  2211. int vm_munmap(unsigned long start, size_t len)
  2212. {
  2213. int ret;
  2214. struct mm_struct *mm = current->mm;
  2215. down_write(&mm->mmap_sem);
  2216. ret = do_munmap(mm, start, len);
  2217. up_write(&mm->mmap_sem);
  2218. return ret;
  2219. }
  2220. EXPORT_SYMBOL(vm_munmap);
  2221. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  2222. {
  2223. profile_munmap(addr);
  2224. return vm_munmap(addr, len);
  2225. }
  2226. static inline void verify_mm_writelocked(struct mm_struct *mm)
  2227. {
  2228. #ifdef CONFIG_DEBUG_VM
  2229. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  2230. WARN_ON(1);
  2231. up_read(&mm->mmap_sem);
  2232. }
  2233. #endif
  2234. }
  2235. /*
  2236. * this is really a simplified "do_mmap". it only handles
  2237. * anonymous maps. eventually we may be able to do some
  2238. * brk-specific accounting here.
  2239. */
  2240. static unsigned long do_brk(unsigned long addr, unsigned long len)
  2241. {
  2242. struct mm_struct * mm = current->mm;
  2243. struct vm_area_struct * vma, * prev;
  2244. unsigned long flags;
  2245. struct rb_node ** rb_link, * rb_parent;
  2246. pgoff_t pgoff = addr >> PAGE_SHIFT;
  2247. int error;
  2248. len = PAGE_ALIGN(len);
  2249. if (!len)
  2250. return addr;
  2251. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  2252. error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  2253. if (error & ~PAGE_MASK)
  2254. return error;
  2255. /*
  2256. * mlock MCL_FUTURE?
  2257. */
  2258. if (mm->def_flags & VM_LOCKED) {
  2259. unsigned long locked, lock_limit;
  2260. locked = len >> PAGE_SHIFT;
  2261. locked += mm->locked_vm;
  2262. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2263. lock_limit >>= PAGE_SHIFT;
  2264. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  2265. return -EAGAIN;
  2266. }
  2267. /*
  2268. * mm->mmap_sem is required to protect against another thread
  2269. * changing the mappings in case we sleep.
  2270. */
  2271. verify_mm_writelocked(mm);
  2272. /*
  2273. * Clear old maps. this also does some error checking for us
  2274. */
  2275. munmap_back:
  2276. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  2277. if (do_munmap(mm, addr, len))
  2278. return -ENOMEM;
  2279. goto munmap_back;
  2280. }
  2281. /* Check against address space limits *after* clearing old maps... */
  2282. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  2283. return -ENOMEM;
  2284. if (mm->map_count > sysctl_max_map_count)
  2285. return -ENOMEM;
  2286. if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
  2287. return -ENOMEM;
  2288. /* Can we just expand an old private anonymous mapping? */
  2289. vma = vma_merge(mm, prev, addr, addr + len, flags,
  2290. NULL, NULL, pgoff, NULL);
  2291. if (vma)
  2292. goto out;
  2293. /*
  2294. * create a vma struct for an anonymous mapping
  2295. */
  2296. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2297. if (!vma) {
  2298. vm_unacct_memory(len >> PAGE_SHIFT);
  2299. return -ENOMEM;
  2300. }
  2301. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2302. vma->vm_mm = mm;
  2303. vma->vm_start = addr;
  2304. vma->vm_end = addr + len;
  2305. vma->vm_pgoff = pgoff;
  2306. vma->vm_flags = flags;
  2307. vma->vm_page_prot = vm_get_page_prot(flags);
  2308. vma_link(mm, vma, prev, rb_link, rb_parent);
  2309. out:
  2310. perf_event_mmap(vma);
  2311. mm->total_vm += len >> PAGE_SHIFT;
  2312. if (flags & VM_LOCKED)
  2313. mm->locked_vm += (len >> PAGE_SHIFT);
  2314. return addr;
  2315. }
  2316. unsigned long vm_brk(unsigned long addr, unsigned long len)
  2317. {
  2318. struct mm_struct *mm = current->mm;
  2319. unsigned long ret;
  2320. bool populate;
  2321. down_write(&mm->mmap_sem);
  2322. ret = do_brk(addr, len);
  2323. populate = ((mm->def_flags & VM_LOCKED) != 0);
  2324. up_write(&mm->mmap_sem);
  2325. if (populate)
  2326. mm_populate(addr, len);
  2327. return ret;
  2328. }
  2329. EXPORT_SYMBOL(vm_brk);
  2330. /* Release all mmaps. */
  2331. void exit_mmap(struct mm_struct *mm)
  2332. {
  2333. struct mmu_gather tlb;
  2334. struct vm_area_struct *vma;
  2335. unsigned long nr_accounted = 0;
  2336. /* mm's last user has gone, and its about to be pulled down */
  2337. mmu_notifier_release(mm);
  2338. if (mm->locked_vm) {
  2339. vma = mm->mmap;
  2340. while (vma) {
  2341. if (vma->vm_flags & VM_LOCKED)
  2342. munlock_vma_pages_all(vma);
  2343. vma = vma->vm_next;
  2344. }
  2345. }
  2346. arch_exit_mmap(mm);
  2347. vma = mm->mmap;
  2348. if (!vma) /* Can happen if dup_mmap() received an OOM */
  2349. return;
  2350. lru_add_drain();
  2351. flush_cache_mm(mm);
  2352. tlb_gather_mmu(&tlb, mm, 1);
  2353. /* update_hiwater_rss(mm) here? but nobody should be looking */
  2354. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  2355. unmap_vmas(&tlb, vma, 0, -1);
  2356. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
  2357. tlb_finish_mmu(&tlb, 0, -1);
  2358. /*
  2359. * Walk the list again, actually closing and freeing it,
  2360. * with preemption enabled, without holding any MM locks.
  2361. */
  2362. while (vma) {
  2363. if (vma->vm_flags & VM_ACCOUNT)
  2364. nr_accounted += vma_pages(vma);
  2365. vma = remove_vma(vma);
  2366. }
  2367. vm_unacct_memory(nr_accounted);
  2368. WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  2369. }
  2370. /* Insert vm structure into process list sorted by address
  2371. * and into the inode's i_mmap tree. If vm_file is non-NULL
  2372. * then i_mmap_mutex is taken here.
  2373. */
  2374. int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  2375. {
  2376. struct vm_area_struct *prev;
  2377. struct rb_node **rb_link, *rb_parent;
  2378. /*
  2379. * The vm_pgoff of a purely anonymous vma should be irrelevant
  2380. * until its first write fault, when page's anon_vma and index
  2381. * are set. But now set the vm_pgoff it will almost certainly
  2382. * end up with (unless mremap moves it elsewhere before that
  2383. * first wfault), so /proc/pid/maps tells a consistent story.
  2384. *
  2385. * By setting it to reflect the virtual start address of the
  2386. * vma, merges and splits can happen in a seamless way, just
  2387. * using the existing file pgoff checks and manipulations.
  2388. * Similarly in do_mmap_pgoff and in do_brk.
  2389. */
  2390. if (!vma->vm_file) {
  2391. BUG_ON(vma->anon_vma);
  2392. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  2393. }
  2394. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  2395. &prev, &rb_link, &rb_parent))
  2396. return -ENOMEM;
  2397. if ((vma->vm_flags & VM_ACCOUNT) &&
  2398. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  2399. return -ENOMEM;
  2400. vma_link(mm, vma, prev, rb_link, rb_parent);
  2401. return 0;
  2402. }
  2403. /*
  2404. * Copy the vma structure to a new location in the same mm,
  2405. * prior to moving page table entries, to effect an mremap move.
  2406. */
  2407. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  2408. unsigned long addr, unsigned long len, pgoff_t pgoff,
  2409. bool *need_rmap_locks)
  2410. {
  2411. struct vm_area_struct *vma = *vmap;
  2412. unsigned long vma_start = vma->vm_start;
  2413. struct mm_struct *mm = vma->vm_mm;
  2414. struct vm_area_struct *new_vma, *prev;
  2415. struct rb_node **rb_link, *rb_parent;
  2416. struct mempolicy *pol;
  2417. bool faulted_in_anon_vma = true;
  2418. /*
  2419. * If anonymous vma has not yet been faulted, update new pgoff
  2420. * to match new location, to increase its chance of merging.
  2421. */
  2422. if (unlikely(!vma->vm_file && !vma->anon_vma)) {
  2423. pgoff = addr >> PAGE_SHIFT;
  2424. faulted_in_anon_vma = false;
  2425. }
  2426. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
  2427. return NULL; /* should never get here */
  2428. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  2429. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  2430. if (new_vma) {
  2431. /*
  2432. * Source vma may have been merged into new_vma
  2433. */
  2434. if (unlikely(vma_start >= new_vma->vm_start &&
  2435. vma_start < new_vma->vm_end)) {
  2436. /*
  2437. * The only way we can get a vma_merge with
  2438. * self during an mremap is if the vma hasn't
  2439. * been faulted in yet and we were allowed to
  2440. * reset the dst vma->vm_pgoff to the
  2441. * destination address of the mremap to allow
  2442. * the merge to happen. mremap must change the
  2443. * vm_pgoff linearity between src and dst vmas
  2444. * (in turn preventing a vma_merge) to be
  2445. * safe. It is only safe to keep the vm_pgoff
  2446. * linear if there are no pages mapped yet.
  2447. */
  2448. VM_BUG_ON(faulted_in_anon_vma);
  2449. *vmap = vma = new_vma;
  2450. }
  2451. *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
  2452. } else {
  2453. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2454. if (new_vma) {
  2455. *new_vma = *vma;
  2456. new_vma->vm_start = addr;
  2457. new_vma->vm_end = addr + len;
  2458. new_vma->vm_pgoff = pgoff;
  2459. pol = mpol_dup(vma_policy(vma));
  2460. if (IS_ERR(pol))
  2461. goto out_free_vma;
  2462. vma_set_policy(new_vma, pol);
  2463. INIT_LIST_HEAD(&new_vma->anon_vma_chain);
  2464. if (anon_vma_clone(new_vma, vma))
  2465. goto out_free_mempol;
  2466. if (new_vma->vm_file)
  2467. get_file(new_vma->vm_file);
  2468. if (new_vma->vm_ops && new_vma->vm_ops->open)
  2469. new_vma->vm_ops->open(new_vma);
  2470. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  2471. *need_rmap_locks = false;
  2472. }
  2473. }
  2474. return new_vma;
  2475. out_free_mempol:
  2476. mpol_put(pol);
  2477. out_free_vma:
  2478. kmem_cache_free(vm_area_cachep, new_vma);
  2479. return NULL;
  2480. }
  2481. /*
  2482. * Return true if the calling process may expand its vm space by the passed
  2483. * number of pages
  2484. */
  2485. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  2486. {
  2487. unsigned long cur = mm->total_vm; /* pages */
  2488. unsigned long lim;
  2489. lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
  2490. if (cur + npages > lim)
  2491. return 0;
  2492. return 1;
  2493. }
  2494. static int special_mapping_fault(struct vm_area_struct *vma,
  2495. struct vm_fault *vmf)
  2496. {
  2497. pgoff_t pgoff;
  2498. struct page **pages;
  2499. /*
  2500. * special mappings have no vm_file, and in that case, the mm
  2501. * uses vm_pgoff internally. So we have to subtract it from here.
  2502. * We are allowed to do this because we are the mm; do not copy
  2503. * this code into drivers!
  2504. */
  2505. pgoff = vmf->pgoff - vma->vm_pgoff;
  2506. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  2507. pgoff--;
  2508. if (*pages) {
  2509. struct page *page = *pages;
  2510. get_page(page);
  2511. vmf->page = page;
  2512. return 0;
  2513. }
  2514. return VM_FAULT_SIGBUS;
  2515. }
  2516. /*
  2517. * Having a close hook prevents vma merging regardless of flags.
  2518. */
  2519. static void special_mapping_close(struct vm_area_struct *vma)
  2520. {
  2521. }
  2522. static const struct vm_operations_struct special_mapping_vmops = {
  2523. .close = special_mapping_close,
  2524. .fault = special_mapping_fault,
  2525. };
  2526. /*
  2527. * Called with mm->mmap_sem held for writing.
  2528. * Insert a new vma covering the given region, with the given flags.
  2529. * Its pages are supplied by the given array of struct page *.
  2530. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  2531. * The region past the last page supplied will always produce SIGBUS.
  2532. * The array pointer and the pages it points to are assumed to stay alive
  2533. * for as long as this mapping might exist.
  2534. */
  2535. int install_special_mapping(struct mm_struct *mm,
  2536. unsigned long addr, unsigned long len,
  2537. unsigned long vm_flags, struct page **pages)
  2538. {
  2539. int ret;
  2540. struct vm_area_struct *vma;
  2541. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2542. if (unlikely(vma == NULL))
  2543. return -ENOMEM;
  2544. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2545. vma->vm_mm = mm;
  2546. vma->vm_start = addr;
  2547. vma->vm_end = addr + len;
  2548. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2549. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2550. vma->vm_ops = &special_mapping_vmops;
  2551. vma->vm_private_data = pages;
  2552. ret = insert_vm_struct(mm, vma);
  2553. if (ret)
  2554. goto out;
  2555. mm->total_vm += len >> PAGE_SHIFT;
  2556. perf_event_mmap(vma);
  2557. return 0;
  2558. out:
  2559. kmem_cache_free(vm_area_cachep, vma);
  2560. return ret;
  2561. }
  2562. static DEFINE_MUTEX(mm_all_locks_mutex);
  2563. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2564. {
  2565. if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2566. /*
  2567. * The LSB of head.next can't change from under us
  2568. * because we hold the mm_all_locks_mutex.
  2569. */
  2570. down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
  2571. /*
  2572. * We can safely modify head.next after taking the
  2573. * anon_vma->root->rwsem. If some other vma in this mm shares
  2574. * the same anon_vma we won't take it again.
  2575. *
  2576. * No need of atomic instructions here, head.next
  2577. * can't change from under us thanks to the
  2578. * anon_vma->root->rwsem.
  2579. */
  2580. if (__test_and_set_bit(0, (unsigned long *)
  2581. &anon_vma->root->rb_root.rb_node))
  2582. BUG();
  2583. }
  2584. }
  2585. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2586. {
  2587. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2588. /*
  2589. * AS_MM_ALL_LOCKS can't change from under us because
  2590. * we hold the mm_all_locks_mutex.
  2591. *
  2592. * Operations on ->flags have to be atomic because
  2593. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2594. * mm_all_locks_mutex, there may be other cpus
  2595. * changing other bitflags in parallel to us.
  2596. */
  2597. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2598. BUG();
  2599. mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
  2600. }
  2601. }
  2602. /*
  2603. * This operation locks against the VM for all pte/vma/mm related
  2604. * operations that could ever happen on a certain mm. This includes
  2605. * vmtruncate, try_to_unmap, and all page faults.
  2606. *
  2607. * The caller must take the mmap_sem in write mode before calling
  2608. * mm_take_all_locks(). The caller isn't allowed to release the
  2609. * mmap_sem until mm_drop_all_locks() returns.
  2610. *
  2611. * mmap_sem in write mode is required in order to block all operations
  2612. * that could modify pagetables and free pages without need of
  2613. * altering the vma layout (for example populate_range() with
  2614. * nonlinear vmas). It's also needed in write mode to avoid new
  2615. * anon_vmas to be associated with existing vmas.
  2616. *
  2617. * A single task can't take more than one mm_take_all_locks() in a row
  2618. * or it would deadlock.
  2619. *
  2620. * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
  2621. * mapping->flags avoid to take the same lock twice, if more than one
  2622. * vma in this mm is backed by the same anon_vma or address_space.
  2623. *
  2624. * We can take all the locks in random order because the VM code
  2625. * taking i_mmap_mutex or anon_vma->rwsem outside the mmap_sem never
  2626. * takes more than one of them in a row. Secondly we're protected
  2627. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2628. *
  2629. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2630. * that may have to take thousand of locks.
  2631. *
  2632. * mm_take_all_locks() can fail if it's interrupted by signals.
  2633. */
  2634. int mm_take_all_locks(struct mm_struct *mm)
  2635. {
  2636. struct vm_area_struct *vma;
  2637. struct anon_vma_chain *avc;
  2638. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2639. mutex_lock(&mm_all_locks_mutex);
  2640. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2641. if (signal_pending(current))
  2642. goto out_unlock;
  2643. if (vma->vm_file && vma->vm_file->f_mapping)
  2644. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2645. }
  2646. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2647. if (signal_pending(current))
  2648. goto out_unlock;
  2649. if (vma->anon_vma)
  2650. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2651. vm_lock_anon_vma(mm, avc->anon_vma);
  2652. }
  2653. return 0;
  2654. out_unlock:
  2655. mm_drop_all_locks(mm);
  2656. return -EINTR;
  2657. }
  2658. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2659. {
  2660. if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2661. /*
  2662. * The LSB of head.next can't change to 0 from under
  2663. * us because we hold the mm_all_locks_mutex.
  2664. *
  2665. * We must however clear the bitflag before unlocking
  2666. * the vma so the users using the anon_vma->rb_root will
  2667. * never see our bitflag.
  2668. *
  2669. * No need of atomic instructions here, head.next
  2670. * can't change from under us until we release the
  2671. * anon_vma->root->rwsem.
  2672. */
  2673. if (!__test_and_clear_bit(0, (unsigned long *)
  2674. &anon_vma->root->rb_root.rb_node))
  2675. BUG();
  2676. anon_vma_unlock_write(anon_vma);
  2677. }
  2678. }
  2679. static void vm_unlock_mapping(struct address_space *mapping)
  2680. {
  2681. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2682. /*
  2683. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2684. * because we hold the mm_all_locks_mutex.
  2685. */
  2686. mutex_unlock(&mapping->i_mmap_mutex);
  2687. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2688. &mapping->flags))
  2689. BUG();
  2690. }
  2691. }
  2692. /*
  2693. * The mmap_sem cannot be released by the caller until
  2694. * mm_drop_all_locks() returns.
  2695. */
  2696. void mm_drop_all_locks(struct mm_struct *mm)
  2697. {
  2698. struct vm_area_struct *vma;
  2699. struct anon_vma_chain *avc;
  2700. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2701. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2702. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2703. if (vma->anon_vma)
  2704. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2705. vm_unlock_anon_vma(avc->anon_vma);
  2706. if (vma->vm_file && vma->vm_file->f_mapping)
  2707. vm_unlock_mapping(vma->vm_file->f_mapping);
  2708. }
  2709. mutex_unlock(&mm_all_locks_mutex);
  2710. }
  2711. /*
  2712. * initialise the VMA slab
  2713. */
  2714. void __init mmap_init(void)
  2715. {
  2716. int ret;
  2717. ret = percpu_counter_init(&vm_committed_as, 0);
  2718. VM_BUG_ON(ret);
  2719. }