mmap.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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/ima.h>
  23. #include <linux/hugetlb.h>
  24. #include <linux/profile.h>
  25. #include <linux/module.h>
  26. #include <linux/mount.h>
  27. #include <linux/mempolicy.h>
  28. #include <linux/rmap.h>
  29. #include <linux/mmu_notifier.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/tlb.h>
  33. #include <asm/mmu_context.h>
  34. #include "internal.h"
  35. #ifndef arch_mmap_check
  36. #define arch_mmap_check(addr, len, flags) (0)
  37. #endif
  38. #ifndef arch_rebalance_pgtables
  39. #define arch_rebalance_pgtables(addr, len) (addr)
  40. #endif
  41. static void unmap_region(struct mm_struct *mm,
  42. struct vm_area_struct *vma, struct vm_area_struct *prev,
  43. unsigned long start, unsigned long end);
  44. /*
  45. * WARNING: the debugging will use recursive algorithms so never enable this
  46. * unless you know what you are doing.
  47. */
  48. #undef DEBUG_MM_RB
  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 = OVERCOMMIT_GUESS; /* heuristic overcommit */
  76. int sysctl_overcommit_ratio = 50; /* default is 50% */
  77. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  78. struct percpu_counter vm_committed_as;
  79. /* amount of vm to protect from userspace access */
  80. unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
  81. /*
  82. * Check that a process has enough memory to allocate a new virtual
  83. * mapping. 0 means there is enough memory for the allocation to
  84. * succeed and -ENOMEM implies there is not.
  85. *
  86. * We currently support three overcommit policies, which are set via the
  87. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  88. *
  89. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  90. * Additional code 2002 Jul 20 by Robert Love.
  91. *
  92. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  93. *
  94. * Note this is a helper function intended to be used by LSMs which
  95. * wish to use this logic.
  96. */
  97. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  98. {
  99. unsigned long free, allowed;
  100. vm_acct_memory(pages);
  101. /*
  102. * Sometimes we want to use more memory than we have
  103. */
  104. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  105. return 0;
  106. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  107. unsigned long n;
  108. free = global_page_state(NR_FILE_PAGES);
  109. free += nr_swap_pages;
  110. /*
  111. * Any slabs which are created with the
  112. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  113. * which are reclaimable, under pressure. The dentry
  114. * cache and most inode caches should fall into this
  115. */
  116. free += global_page_state(NR_SLAB_RECLAIMABLE);
  117. /*
  118. * Leave the last 3% for root
  119. */
  120. if (!cap_sys_admin)
  121. free -= free / 32;
  122. if (free > pages)
  123. return 0;
  124. /*
  125. * nr_free_pages() is very expensive on large systems,
  126. * only call if we're about to fail.
  127. */
  128. n = nr_free_pages();
  129. /*
  130. * Leave reserved pages. The pages are not for anonymous pages.
  131. */
  132. if (n <= totalreserve_pages)
  133. goto error;
  134. else
  135. n -= totalreserve_pages;
  136. /*
  137. * Leave the last 3% for root
  138. */
  139. if (!cap_sys_admin)
  140. n -= n / 32;
  141. free += n;
  142. if (free > pages)
  143. return 0;
  144. goto error;
  145. }
  146. allowed = (totalram_pages - hugetlb_total_pages())
  147. * sysctl_overcommit_ratio / 100;
  148. /*
  149. * Leave the last 3% for root
  150. */
  151. if (!cap_sys_admin)
  152. allowed -= allowed / 32;
  153. allowed += total_swap_pages;
  154. /* Don't let a single process grow too big:
  155. leave 3% of the size of this process for other processes */
  156. if (mm)
  157. allowed -= mm->total_vm / 32;
  158. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  159. return 0;
  160. error:
  161. vm_unacct_memory(pages);
  162. return -ENOMEM;
  163. }
  164. /*
  165. * Requires inode->i_mapping->i_mmap_lock
  166. */
  167. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  168. struct file *file, struct address_space *mapping)
  169. {
  170. if (vma->vm_flags & VM_DENYWRITE)
  171. atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
  172. if (vma->vm_flags & VM_SHARED)
  173. mapping->i_mmap_writable--;
  174. flush_dcache_mmap_lock(mapping);
  175. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  176. list_del_init(&vma->shared.vm_set.list);
  177. else
  178. vma_prio_tree_remove(vma, &mapping->i_mmap);
  179. flush_dcache_mmap_unlock(mapping);
  180. }
  181. /*
  182. * Unlink a file-based vm structure from its prio_tree, to hide
  183. * vma from rmap and vmtruncate before freeing its page tables.
  184. */
  185. void unlink_file_vma(struct vm_area_struct *vma)
  186. {
  187. struct file *file = vma->vm_file;
  188. if (file) {
  189. struct address_space *mapping = file->f_mapping;
  190. spin_lock(&mapping->i_mmap_lock);
  191. __remove_shared_vm_struct(vma, file, mapping);
  192. spin_unlock(&mapping->i_mmap_lock);
  193. }
  194. }
  195. /*
  196. * Close a vm structure and free it, returning the next.
  197. */
  198. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  199. {
  200. struct vm_area_struct *next = vma->vm_next;
  201. might_sleep();
  202. if (vma->vm_ops && vma->vm_ops->close)
  203. vma->vm_ops->close(vma);
  204. if (vma->vm_file) {
  205. fput(vma->vm_file);
  206. if (vma->vm_flags & VM_EXECUTABLE)
  207. removed_exe_file_vma(vma->vm_mm);
  208. }
  209. mpol_put(vma_policy(vma));
  210. kmem_cache_free(vm_area_cachep, vma);
  211. return next;
  212. }
  213. SYSCALL_DEFINE1(brk, unsigned long, brk)
  214. {
  215. unsigned long rlim, retval;
  216. unsigned long newbrk, oldbrk;
  217. struct mm_struct *mm = current->mm;
  218. unsigned long min_brk;
  219. down_write(&mm->mmap_sem);
  220. #ifdef CONFIG_COMPAT_BRK
  221. min_brk = mm->end_code;
  222. #else
  223. min_brk = mm->start_brk;
  224. #endif
  225. if (brk < min_brk)
  226. goto out;
  227. /*
  228. * Check against rlimit here. If this check is done later after the test
  229. * of oldbrk with newbrk then it can escape the test and let the data
  230. * segment grow beyond its set limit the in case where the limit is
  231. * not page aligned -Ram Gupta
  232. */
  233. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  234. if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
  235. (mm->end_data - mm->start_data) > rlim)
  236. goto out;
  237. newbrk = PAGE_ALIGN(brk);
  238. oldbrk = PAGE_ALIGN(mm->brk);
  239. if (oldbrk == newbrk)
  240. goto set_brk;
  241. /* Always allow shrinking brk. */
  242. if (brk <= mm->brk) {
  243. if (!do_munmap(mm, newbrk, oldbrk-newbrk))
  244. goto set_brk;
  245. goto out;
  246. }
  247. /* Check against existing mmap mappings. */
  248. if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
  249. goto out;
  250. /* Ok, looks good - let it rip. */
  251. if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
  252. goto out;
  253. set_brk:
  254. mm->brk = brk;
  255. out:
  256. retval = mm->brk;
  257. up_write(&mm->mmap_sem);
  258. return retval;
  259. }
  260. #ifdef DEBUG_MM_RB
  261. static int browse_rb(struct rb_root *root)
  262. {
  263. int i = 0, j;
  264. struct rb_node *nd, *pn = NULL;
  265. unsigned long prev = 0, pend = 0;
  266. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  267. struct vm_area_struct *vma;
  268. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  269. if (vma->vm_start < prev)
  270. printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
  271. if (vma->vm_start < pend)
  272. printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
  273. if (vma->vm_start > vma->vm_end)
  274. printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
  275. i++;
  276. pn = nd;
  277. prev = vma->vm_start;
  278. pend = vma->vm_end;
  279. }
  280. j = 0;
  281. for (nd = pn; nd; nd = rb_prev(nd)) {
  282. j++;
  283. }
  284. if (i != j)
  285. printk("backwards %d, forwards %d\n", j, i), i = 0;
  286. return i;
  287. }
  288. void validate_mm(struct mm_struct *mm)
  289. {
  290. int bug = 0;
  291. int i = 0;
  292. struct vm_area_struct *tmp = mm->mmap;
  293. while (tmp) {
  294. tmp = tmp->vm_next;
  295. i++;
  296. }
  297. if (i != mm->map_count)
  298. printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
  299. i = browse_rb(&mm->mm_rb);
  300. if (i != mm->map_count)
  301. printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
  302. BUG_ON(bug);
  303. }
  304. #else
  305. #define validate_mm(mm) do { } while (0)
  306. #endif
  307. static struct vm_area_struct *
  308. find_vma_prepare(struct mm_struct *mm, unsigned long addr,
  309. struct vm_area_struct **pprev, struct rb_node ***rb_link,
  310. struct rb_node ** rb_parent)
  311. {
  312. struct vm_area_struct * vma;
  313. struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
  314. __rb_link = &mm->mm_rb.rb_node;
  315. rb_prev = __rb_parent = NULL;
  316. vma = NULL;
  317. while (*__rb_link) {
  318. struct vm_area_struct *vma_tmp;
  319. __rb_parent = *__rb_link;
  320. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  321. if (vma_tmp->vm_end > addr) {
  322. vma = vma_tmp;
  323. if (vma_tmp->vm_start <= addr)
  324. break;
  325. __rb_link = &__rb_parent->rb_left;
  326. } else {
  327. rb_prev = __rb_parent;
  328. __rb_link = &__rb_parent->rb_right;
  329. }
  330. }
  331. *pprev = NULL;
  332. if (rb_prev)
  333. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  334. *rb_link = __rb_link;
  335. *rb_parent = __rb_parent;
  336. return vma;
  337. }
  338. static inline void
  339. __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  340. struct vm_area_struct *prev, struct rb_node *rb_parent)
  341. {
  342. if (prev) {
  343. vma->vm_next = prev->vm_next;
  344. prev->vm_next = vma;
  345. } else {
  346. mm->mmap = vma;
  347. if (rb_parent)
  348. vma->vm_next = rb_entry(rb_parent,
  349. struct vm_area_struct, vm_rb);
  350. else
  351. vma->vm_next = NULL;
  352. }
  353. }
  354. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  355. struct rb_node **rb_link, struct rb_node *rb_parent)
  356. {
  357. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  358. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  359. }
  360. static void __vma_link_file(struct vm_area_struct *vma)
  361. {
  362. struct file *file;
  363. file = vma->vm_file;
  364. if (file) {
  365. struct address_space *mapping = file->f_mapping;
  366. if (vma->vm_flags & VM_DENYWRITE)
  367. atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
  368. if (vma->vm_flags & VM_SHARED)
  369. mapping->i_mmap_writable++;
  370. flush_dcache_mmap_lock(mapping);
  371. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  372. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  373. else
  374. vma_prio_tree_insert(vma, &mapping->i_mmap);
  375. flush_dcache_mmap_unlock(mapping);
  376. }
  377. }
  378. static void
  379. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  380. struct vm_area_struct *prev, struct rb_node **rb_link,
  381. struct rb_node *rb_parent)
  382. {
  383. __vma_link_list(mm, vma, prev, rb_parent);
  384. __vma_link_rb(mm, vma, rb_link, rb_parent);
  385. __anon_vma_link(vma);
  386. }
  387. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  388. struct vm_area_struct *prev, struct rb_node **rb_link,
  389. struct rb_node *rb_parent)
  390. {
  391. struct address_space *mapping = NULL;
  392. if (vma->vm_file)
  393. mapping = vma->vm_file->f_mapping;
  394. if (mapping) {
  395. spin_lock(&mapping->i_mmap_lock);
  396. vma->vm_truncate_count = mapping->truncate_count;
  397. }
  398. anon_vma_lock(vma);
  399. __vma_link(mm, vma, prev, rb_link, rb_parent);
  400. __vma_link_file(vma);
  401. anon_vma_unlock(vma);
  402. if (mapping)
  403. spin_unlock(&mapping->i_mmap_lock);
  404. mm->map_count++;
  405. validate_mm(mm);
  406. }
  407. /*
  408. * Helper for vma_adjust in the split_vma insert case:
  409. * insert vm structure into list and rbtree and anon_vma,
  410. * but it has already been inserted into prio_tree earlier.
  411. */
  412. static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  413. {
  414. struct vm_area_struct *__vma, *prev;
  415. struct rb_node **rb_link, *rb_parent;
  416. __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
  417. BUG_ON(__vma && __vma->vm_start < vma->vm_end);
  418. __vma_link(mm, vma, prev, rb_link, rb_parent);
  419. mm->map_count++;
  420. }
  421. static inline void
  422. __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
  423. struct vm_area_struct *prev)
  424. {
  425. prev->vm_next = vma->vm_next;
  426. rb_erase(&vma->vm_rb, &mm->mm_rb);
  427. if (mm->mmap_cache == vma)
  428. mm->mmap_cache = prev;
  429. }
  430. /*
  431. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  432. * is already present in an i_mmap tree without adjusting the tree.
  433. * The following helper function should be used when such adjustments
  434. * are necessary. The "insert" vma (if any) is to be inserted
  435. * before we drop the necessary locks.
  436. */
  437. void vma_adjust(struct vm_area_struct *vma, unsigned long start,
  438. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  439. {
  440. struct mm_struct *mm = vma->vm_mm;
  441. struct vm_area_struct *next = vma->vm_next;
  442. struct vm_area_struct *importer = NULL;
  443. struct address_space *mapping = NULL;
  444. struct prio_tree_root *root = NULL;
  445. struct file *file = vma->vm_file;
  446. struct anon_vma *anon_vma = NULL;
  447. long adjust_next = 0;
  448. int remove_next = 0;
  449. if (next && !insert) {
  450. if (end >= next->vm_end) {
  451. /*
  452. * vma expands, overlapping all the next, and
  453. * perhaps the one after too (mprotect case 6).
  454. */
  455. again: remove_next = 1 + (end > next->vm_end);
  456. end = next->vm_end;
  457. anon_vma = next->anon_vma;
  458. importer = vma;
  459. } else if (end > next->vm_start) {
  460. /*
  461. * vma expands, overlapping part of the next:
  462. * mprotect case 5 shifting the boundary up.
  463. */
  464. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  465. anon_vma = next->anon_vma;
  466. importer = vma;
  467. } else if (end < vma->vm_end) {
  468. /*
  469. * vma shrinks, and !insert tells it's not
  470. * split_vma inserting another: so it must be
  471. * mprotect case 4 shifting the boundary down.
  472. */
  473. adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
  474. anon_vma = next->anon_vma;
  475. importer = next;
  476. }
  477. }
  478. if (file) {
  479. mapping = file->f_mapping;
  480. if (!(vma->vm_flags & VM_NONLINEAR))
  481. root = &mapping->i_mmap;
  482. spin_lock(&mapping->i_mmap_lock);
  483. if (importer &&
  484. vma->vm_truncate_count != next->vm_truncate_count) {
  485. /*
  486. * unmap_mapping_range might be in progress:
  487. * ensure that the expanding vma is rescanned.
  488. */
  489. importer->vm_truncate_count = 0;
  490. }
  491. if (insert) {
  492. insert->vm_truncate_count = vma->vm_truncate_count;
  493. /*
  494. * Put into prio_tree now, so instantiated pages
  495. * are visible to arm/parisc __flush_dcache_page
  496. * throughout; but we cannot insert into address
  497. * space until vma start or end is updated.
  498. */
  499. __vma_link_file(insert);
  500. }
  501. }
  502. /*
  503. * When changing only vma->vm_end, we don't really need
  504. * anon_vma lock: but is that case worth optimizing out?
  505. */
  506. if (vma->anon_vma)
  507. anon_vma = vma->anon_vma;
  508. if (anon_vma) {
  509. spin_lock(&anon_vma->lock);
  510. /*
  511. * Easily overlooked: when mprotect shifts the boundary,
  512. * make sure the expanding vma has anon_vma set if the
  513. * shrinking vma had, to cover any anon pages imported.
  514. */
  515. if (importer && !importer->anon_vma) {
  516. importer->anon_vma = anon_vma;
  517. __anon_vma_link(importer);
  518. }
  519. }
  520. if (root) {
  521. flush_dcache_mmap_lock(mapping);
  522. vma_prio_tree_remove(vma, root);
  523. if (adjust_next)
  524. vma_prio_tree_remove(next, root);
  525. }
  526. vma->vm_start = start;
  527. vma->vm_end = end;
  528. vma->vm_pgoff = pgoff;
  529. if (adjust_next) {
  530. next->vm_start += adjust_next << PAGE_SHIFT;
  531. next->vm_pgoff += adjust_next;
  532. }
  533. if (root) {
  534. if (adjust_next)
  535. vma_prio_tree_insert(next, root);
  536. vma_prio_tree_insert(vma, root);
  537. flush_dcache_mmap_unlock(mapping);
  538. }
  539. if (remove_next) {
  540. /*
  541. * vma_merge has merged next into vma, and needs
  542. * us to remove next before dropping the locks.
  543. */
  544. __vma_unlink(mm, next, vma);
  545. if (file)
  546. __remove_shared_vm_struct(next, file, mapping);
  547. if (next->anon_vma)
  548. __anon_vma_merge(vma, next);
  549. } else if (insert) {
  550. /*
  551. * split_vma has split insert from vma, and needs
  552. * us to insert it before dropping the locks
  553. * (it may either follow vma or precede it).
  554. */
  555. __insert_vm_struct(mm, insert);
  556. }
  557. if (anon_vma)
  558. spin_unlock(&anon_vma->lock);
  559. if (mapping)
  560. spin_unlock(&mapping->i_mmap_lock);
  561. if (remove_next) {
  562. if (file) {
  563. fput(file);
  564. if (next->vm_flags & VM_EXECUTABLE)
  565. removed_exe_file_vma(mm);
  566. }
  567. mm->map_count--;
  568. mpol_put(vma_policy(next));
  569. kmem_cache_free(vm_area_cachep, next);
  570. /*
  571. * In mprotect's case 6 (see comments on vma_merge),
  572. * we must remove another next too. It would clutter
  573. * up the code too much to do both in one go.
  574. */
  575. if (remove_next == 2) {
  576. next = vma->vm_next;
  577. goto again;
  578. }
  579. }
  580. validate_mm(mm);
  581. }
  582. /* Flags that can be inherited from an existing mapping when merging */
  583. #define VM_MERGEABLE_FLAGS (VM_CAN_NONLINEAR)
  584. /*
  585. * If the vma has a ->close operation then the driver probably needs to release
  586. * per-vma resources, so we don't attempt to merge those.
  587. */
  588. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  589. struct file *file, unsigned long vm_flags)
  590. {
  591. if ((vma->vm_flags ^ vm_flags) & ~VM_MERGEABLE_FLAGS)
  592. return 0;
  593. if (vma->vm_file != file)
  594. return 0;
  595. if (vma->vm_ops && vma->vm_ops->close)
  596. return 0;
  597. return 1;
  598. }
  599. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  600. struct anon_vma *anon_vma2)
  601. {
  602. return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
  603. }
  604. /*
  605. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  606. * in front of (at a lower virtual address and file offset than) the vma.
  607. *
  608. * We cannot merge two vmas if they have differently assigned (non-NULL)
  609. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  610. *
  611. * We don't check here for the merged mmap wrapping around the end of pagecache
  612. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  613. * wrap, nor mmaps which cover the final page at index -1UL.
  614. */
  615. static int
  616. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  617. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  618. {
  619. if (is_mergeable_vma(vma, file, vm_flags) &&
  620. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  621. if (vma->vm_pgoff == vm_pgoff)
  622. return 1;
  623. }
  624. return 0;
  625. }
  626. /*
  627. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  628. * beyond (at a higher virtual address and file offset than) the vma.
  629. *
  630. * We cannot merge two vmas if they have differently assigned (non-NULL)
  631. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  632. */
  633. static int
  634. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  635. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  636. {
  637. if (is_mergeable_vma(vma, file, vm_flags) &&
  638. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  639. pgoff_t vm_pglen;
  640. vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  641. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  642. return 1;
  643. }
  644. return 0;
  645. }
  646. /*
  647. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  648. * whether that can be merged with its predecessor or its successor.
  649. * Or both (it neatly fills a hole).
  650. *
  651. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  652. * certain not to be mapped by the time vma_merge is called; but when
  653. * called for mprotect, it is certain to be already mapped (either at
  654. * an offset within prev, or at the start of next), and the flags of
  655. * this area are about to be changed to vm_flags - and the no-change
  656. * case has already been eliminated.
  657. *
  658. * The following mprotect cases have to be considered, where AAAA is
  659. * the area passed down from mprotect_fixup, never extending beyond one
  660. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  661. *
  662. * AAAA AAAA AAAA AAAA
  663. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  664. * cannot merge might become might become might become
  665. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  666. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  667. * mremap move: PPPPNNNNNNNN 8
  668. * AAAA
  669. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  670. * might become case 1 below case 2 below case 3 below
  671. *
  672. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  673. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  674. */
  675. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  676. struct vm_area_struct *prev, unsigned long addr,
  677. unsigned long end, unsigned long vm_flags,
  678. struct anon_vma *anon_vma, struct file *file,
  679. pgoff_t pgoff, struct mempolicy *policy)
  680. {
  681. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  682. struct vm_area_struct *area, *next;
  683. /*
  684. * We later require that vma->vm_flags == vm_flags,
  685. * so this tests vma->vm_flags & VM_SPECIAL, too.
  686. */
  687. if (vm_flags & VM_SPECIAL)
  688. return NULL;
  689. if (prev)
  690. next = prev->vm_next;
  691. else
  692. next = mm->mmap;
  693. area = next;
  694. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  695. next = next->vm_next;
  696. /*
  697. * Can it merge with the predecessor?
  698. */
  699. if (prev && prev->vm_end == addr &&
  700. mpol_equal(vma_policy(prev), policy) &&
  701. can_vma_merge_after(prev, vm_flags,
  702. anon_vma, file, pgoff)) {
  703. /*
  704. * OK, it can. Can we now merge in the successor as well?
  705. */
  706. if (next && end == next->vm_start &&
  707. mpol_equal(policy, vma_policy(next)) &&
  708. can_vma_merge_before(next, vm_flags,
  709. anon_vma, file, pgoff+pglen) &&
  710. is_mergeable_anon_vma(prev->anon_vma,
  711. next->anon_vma)) {
  712. /* cases 1, 6 */
  713. vma_adjust(prev, prev->vm_start,
  714. next->vm_end, prev->vm_pgoff, NULL);
  715. } else /* cases 2, 5, 7 */
  716. vma_adjust(prev, prev->vm_start,
  717. end, prev->vm_pgoff, NULL);
  718. return prev;
  719. }
  720. /*
  721. * Can this new request be merged in front of next?
  722. */
  723. if (next && end == next->vm_start &&
  724. mpol_equal(policy, vma_policy(next)) &&
  725. can_vma_merge_before(next, vm_flags,
  726. anon_vma, file, pgoff+pglen)) {
  727. if (prev && addr < prev->vm_end) /* case 4 */
  728. vma_adjust(prev, prev->vm_start,
  729. addr, prev->vm_pgoff, NULL);
  730. else /* cases 3, 8 */
  731. vma_adjust(area, addr, next->vm_end,
  732. next->vm_pgoff - pglen, NULL);
  733. return area;
  734. }
  735. return NULL;
  736. }
  737. /*
  738. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  739. * neighbouring vmas for a suitable anon_vma, before it goes off
  740. * to allocate a new anon_vma. It checks because a repetitive
  741. * sequence of mprotects and faults may otherwise lead to distinct
  742. * anon_vmas being allocated, preventing vma merge in subsequent
  743. * mprotect.
  744. */
  745. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  746. {
  747. struct vm_area_struct *near;
  748. unsigned long vm_flags;
  749. near = vma->vm_next;
  750. if (!near)
  751. goto try_prev;
  752. /*
  753. * Since only mprotect tries to remerge vmas, match flags
  754. * which might be mprotected into each other later on.
  755. * Neither mlock nor madvise tries to remerge at present,
  756. * so leave their flags as obstructing a merge.
  757. */
  758. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  759. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  760. if (near->anon_vma && vma->vm_end == near->vm_start &&
  761. mpol_equal(vma_policy(vma), vma_policy(near)) &&
  762. can_vma_merge_before(near, vm_flags,
  763. NULL, vma->vm_file, vma->vm_pgoff +
  764. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
  765. return near->anon_vma;
  766. try_prev:
  767. /*
  768. * It is potentially slow to have to call find_vma_prev here.
  769. * But it's only on the first write fault on the vma, not
  770. * every time, and we could devise a way to avoid it later
  771. * (e.g. stash info in next's anon_vma_node when assigning
  772. * an anon_vma, or when trying vma_merge). Another time.
  773. */
  774. BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
  775. if (!near)
  776. goto none;
  777. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  778. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  779. if (near->anon_vma && near->vm_end == vma->vm_start &&
  780. mpol_equal(vma_policy(near), vma_policy(vma)) &&
  781. can_vma_merge_after(near, vm_flags,
  782. NULL, vma->vm_file, vma->vm_pgoff))
  783. return near->anon_vma;
  784. none:
  785. /*
  786. * There's no absolute need to look only at touching neighbours:
  787. * we could search further afield for "compatible" anon_vmas.
  788. * But it would probably just be a waste of time searching,
  789. * or lead to too many vmas hanging off the same anon_vma.
  790. * We're trying to allow mprotect remerging later on,
  791. * not trying to minimize memory used for anon_vmas.
  792. */
  793. return NULL;
  794. }
  795. #ifdef CONFIG_PROC_FS
  796. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  797. struct file *file, long pages)
  798. {
  799. const unsigned long stack_flags
  800. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  801. if (file) {
  802. mm->shared_vm += pages;
  803. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  804. mm->exec_vm += pages;
  805. } else if (flags & stack_flags)
  806. mm->stack_vm += pages;
  807. if (flags & (VM_RESERVED|VM_IO))
  808. mm->reserved_vm += pages;
  809. }
  810. #endif /* CONFIG_PROC_FS */
  811. /*
  812. * The caller must hold down_write(current->mm->mmap_sem).
  813. */
  814. unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  815. unsigned long len, unsigned long prot,
  816. unsigned long flags, unsigned long pgoff)
  817. {
  818. struct mm_struct * mm = current->mm;
  819. struct inode *inode;
  820. unsigned int vm_flags;
  821. int error;
  822. unsigned long reqprot = prot;
  823. /*
  824. * Does the application expect PROT_READ to imply PROT_EXEC?
  825. *
  826. * (the exception is when the underlying filesystem is noexec
  827. * mounted, in which case we dont add PROT_EXEC.)
  828. */
  829. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  830. if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
  831. prot |= PROT_EXEC;
  832. if (!len)
  833. return -EINVAL;
  834. if (!(flags & MAP_FIXED))
  835. addr = round_hint_to_min(addr);
  836. error = arch_mmap_check(addr, len, flags);
  837. if (error)
  838. return error;
  839. /* Careful about overflows.. */
  840. len = PAGE_ALIGN(len);
  841. if (!len || len > TASK_SIZE)
  842. return -ENOMEM;
  843. /* offset overflow? */
  844. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  845. return -EOVERFLOW;
  846. /* Too many mappings? */
  847. if (mm->map_count > sysctl_max_map_count)
  848. return -ENOMEM;
  849. /* Obtain the address to map to. we verify (or select) it and ensure
  850. * that it represents a valid section of the address space.
  851. */
  852. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  853. if (addr & ~PAGE_MASK)
  854. return addr;
  855. /* Do simple checking here so the lower-level routines won't have
  856. * to. we assume access permissions have been handled by the open
  857. * of the memory object, so we don't do any here.
  858. */
  859. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  860. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  861. if (flags & MAP_LOCKED) {
  862. if (!can_do_mlock())
  863. return -EPERM;
  864. vm_flags |= VM_LOCKED;
  865. }
  866. /* mlock MCL_FUTURE? */
  867. if (vm_flags & VM_LOCKED) {
  868. unsigned long locked, lock_limit;
  869. locked = len >> PAGE_SHIFT;
  870. locked += mm->locked_vm;
  871. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  872. lock_limit >>= PAGE_SHIFT;
  873. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  874. return -EAGAIN;
  875. }
  876. inode = file ? file->f_path.dentry->d_inode : NULL;
  877. if (file) {
  878. switch (flags & MAP_TYPE) {
  879. case MAP_SHARED:
  880. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  881. return -EACCES;
  882. /*
  883. * Make sure we don't allow writing to an append-only
  884. * file..
  885. */
  886. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  887. return -EACCES;
  888. /*
  889. * Make sure there are no mandatory locks on the file.
  890. */
  891. if (locks_verify_locked(inode))
  892. return -EAGAIN;
  893. vm_flags |= VM_SHARED | VM_MAYSHARE;
  894. if (!(file->f_mode & FMODE_WRITE))
  895. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  896. /* fall through */
  897. case MAP_PRIVATE:
  898. if (!(file->f_mode & FMODE_READ))
  899. return -EACCES;
  900. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  901. if (vm_flags & VM_EXEC)
  902. return -EPERM;
  903. vm_flags &= ~VM_MAYEXEC;
  904. }
  905. if (!file->f_op || !file->f_op->mmap)
  906. return -ENODEV;
  907. break;
  908. default:
  909. return -EINVAL;
  910. }
  911. } else {
  912. switch (flags & MAP_TYPE) {
  913. case MAP_SHARED:
  914. /*
  915. * Ignore pgoff.
  916. */
  917. pgoff = 0;
  918. vm_flags |= VM_SHARED | VM_MAYSHARE;
  919. break;
  920. case MAP_PRIVATE:
  921. /*
  922. * Set pgoff according to addr for anon_vma.
  923. */
  924. pgoff = addr >> PAGE_SHIFT;
  925. break;
  926. default:
  927. return -EINVAL;
  928. }
  929. }
  930. error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  931. if (error)
  932. return error;
  933. error = ima_file_mmap(file, prot);
  934. if (error)
  935. return error;
  936. return mmap_region(file, addr, len, flags, vm_flags, pgoff);
  937. }
  938. EXPORT_SYMBOL(do_mmap_pgoff);
  939. /*
  940. * Some shared mappigns will want the pages marked read-only
  941. * to track write events. If so, we'll downgrade vm_page_prot
  942. * to the private version (using protection_map[] without the
  943. * VM_SHARED bit).
  944. */
  945. int vma_wants_writenotify(struct vm_area_struct *vma)
  946. {
  947. unsigned int vm_flags = vma->vm_flags;
  948. /* If it was private or non-writable, the write bit is already clear */
  949. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  950. return 0;
  951. /* The backer wishes to know when pages are first written to? */
  952. if (vma->vm_ops && vma->vm_ops->page_mkwrite)
  953. return 1;
  954. /* The open routine did something to the protections already? */
  955. if (pgprot_val(vma->vm_page_prot) !=
  956. pgprot_val(vm_get_page_prot(vm_flags)))
  957. return 0;
  958. /* Specialty mapping? */
  959. if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
  960. return 0;
  961. /* Can the mapping track the dirty pages? */
  962. return vma->vm_file && vma->vm_file->f_mapping &&
  963. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  964. }
  965. /*
  966. * We account for memory if it's a private writeable mapping,
  967. * not hugepages and VM_NORESERVE wasn't set.
  968. */
  969. static inline int accountable_mapping(struct file *file, unsigned int vm_flags)
  970. {
  971. /*
  972. * hugetlb has its own accounting separate from the core VM
  973. * VM_HUGETLB may not be set yet so we cannot check for that flag.
  974. */
  975. if (file && is_file_hugepages(file))
  976. return 0;
  977. return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
  978. }
  979. unsigned long mmap_region(struct file *file, unsigned long addr,
  980. unsigned long len, unsigned long flags,
  981. unsigned int vm_flags, unsigned long pgoff)
  982. {
  983. struct mm_struct *mm = current->mm;
  984. struct vm_area_struct *vma, *prev;
  985. int correct_wcount = 0;
  986. int error;
  987. struct rb_node **rb_link, *rb_parent;
  988. unsigned long charged = 0;
  989. struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
  990. /* Clear old maps */
  991. error = -ENOMEM;
  992. munmap_back:
  993. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  994. if (vma && vma->vm_start < addr + len) {
  995. if (do_munmap(mm, addr, len))
  996. return -ENOMEM;
  997. goto munmap_back;
  998. }
  999. /* Check against address space limit. */
  1000. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1001. return -ENOMEM;
  1002. /*
  1003. * Set 'VM_NORESERVE' if we should not account for the
  1004. * memory use of this mapping.
  1005. */
  1006. if ((flags & MAP_NORESERVE)) {
  1007. /* We honor MAP_NORESERVE if allowed to overcommit */
  1008. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  1009. vm_flags |= VM_NORESERVE;
  1010. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  1011. if (file && is_file_hugepages(file))
  1012. vm_flags |= VM_NORESERVE;
  1013. }
  1014. /*
  1015. * Private writable mapping: check memory availability
  1016. */
  1017. if (accountable_mapping(file, vm_flags)) {
  1018. charged = len >> PAGE_SHIFT;
  1019. if (security_vm_enough_memory(charged))
  1020. return -ENOMEM;
  1021. vm_flags |= VM_ACCOUNT;
  1022. }
  1023. /*
  1024. * Can we just expand an old mapping?
  1025. */
  1026. vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
  1027. if (vma)
  1028. goto out;
  1029. /*
  1030. * Determine the object being mapped and call the appropriate
  1031. * specific mapper. the address has already been validated, but
  1032. * not unmapped, but the maps are removed from the list.
  1033. */
  1034. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1035. if (!vma) {
  1036. error = -ENOMEM;
  1037. goto unacct_error;
  1038. }
  1039. vma->vm_mm = mm;
  1040. vma->vm_start = addr;
  1041. vma->vm_end = addr + len;
  1042. vma->vm_flags = vm_flags;
  1043. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1044. vma->vm_pgoff = pgoff;
  1045. if (file) {
  1046. error = -EINVAL;
  1047. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1048. goto free_vma;
  1049. if (vm_flags & VM_DENYWRITE) {
  1050. error = deny_write_access(file);
  1051. if (error)
  1052. goto free_vma;
  1053. correct_wcount = 1;
  1054. }
  1055. vma->vm_file = file;
  1056. get_file(file);
  1057. error = file->f_op->mmap(file, vma);
  1058. if (error)
  1059. goto unmap_and_free_vma;
  1060. if (vm_flags & VM_EXECUTABLE)
  1061. added_exe_file_vma(mm);
  1062. } else if (vm_flags & VM_SHARED) {
  1063. error = shmem_zero_setup(vma);
  1064. if (error)
  1065. goto free_vma;
  1066. }
  1067. /* Can addr have changed??
  1068. *
  1069. * Answer: Yes, several device drivers can do it in their
  1070. * f_op->mmap method. -DaveM
  1071. */
  1072. addr = vma->vm_start;
  1073. pgoff = vma->vm_pgoff;
  1074. vm_flags = vma->vm_flags;
  1075. if (vma_wants_writenotify(vma))
  1076. vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
  1077. vma_link(mm, vma, prev, rb_link, rb_parent);
  1078. file = vma->vm_file;
  1079. /* Once vma denies write, undo our temporary denial count */
  1080. if (correct_wcount)
  1081. atomic_inc(&inode->i_writecount);
  1082. out:
  1083. mm->total_vm += len >> PAGE_SHIFT;
  1084. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1085. if (vm_flags & VM_LOCKED) {
  1086. /*
  1087. * makes pages present; downgrades, drops, reacquires mmap_sem
  1088. */
  1089. long nr_pages = mlock_vma_pages_range(vma, addr, addr + len);
  1090. if (nr_pages < 0)
  1091. return nr_pages; /* vma gone! */
  1092. mm->locked_vm += (len >> PAGE_SHIFT) - nr_pages;
  1093. } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
  1094. make_pages_present(addr, addr + len);
  1095. return addr;
  1096. unmap_and_free_vma:
  1097. if (correct_wcount)
  1098. atomic_inc(&inode->i_writecount);
  1099. vma->vm_file = NULL;
  1100. fput(file);
  1101. /* Undo any partial mapping done by a device driver. */
  1102. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1103. charged = 0;
  1104. free_vma:
  1105. kmem_cache_free(vm_area_cachep, vma);
  1106. unacct_error:
  1107. if (charged)
  1108. vm_unacct_memory(charged);
  1109. return error;
  1110. }
  1111. /* Get an address range which is currently unmapped.
  1112. * For shmat() with addr=0.
  1113. *
  1114. * Ugly calling convention alert:
  1115. * Return value with the low bits set means error value,
  1116. * ie
  1117. * if (ret & ~PAGE_MASK)
  1118. * error = ret;
  1119. *
  1120. * This function "knows" that -ENOMEM has the bits set.
  1121. */
  1122. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1123. unsigned long
  1124. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1125. unsigned long len, unsigned long pgoff, unsigned long flags)
  1126. {
  1127. struct mm_struct *mm = current->mm;
  1128. struct vm_area_struct *vma;
  1129. unsigned long start_addr;
  1130. if (len > TASK_SIZE)
  1131. return -ENOMEM;
  1132. if (flags & MAP_FIXED)
  1133. return addr;
  1134. if (addr) {
  1135. addr = PAGE_ALIGN(addr);
  1136. vma = find_vma(mm, addr);
  1137. if (TASK_SIZE - len >= addr &&
  1138. (!vma || addr + len <= vma->vm_start))
  1139. return addr;
  1140. }
  1141. if (len > mm->cached_hole_size) {
  1142. start_addr = addr = mm->free_area_cache;
  1143. } else {
  1144. start_addr = addr = TASK_UNMAPPED_BASE;
  1145. mm->cached_hole_size = 0;
  1146. }
  1147. full_search:
  1148. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  1149. /* At this point: (!vma || addr < vma->vm_end). */
  1150. if (TASK_SIZE - len < addr) {
  1151. /*
  1152. * Start a new search - just in case we missed
  1153. * some holes.
  1154. */
  1155. if (start_addr != TASK_UNMAPPED_BASE) {
  1156. addr = TASK_UNMAPPED_BASE;
  1157. start_addr = addr;
  1158. mm->cached_hole_size = 0;
  1159. goto full_search;
  1160. }
  1161. return -ENOMEM;
  1162. }
  1163. if (!vma || addr + len <= vma->vm_start) {
  1164. /*
  1165. * Remember the place where we stopped the search:
  1166. */
  1167. mm->free_area_cache = addr + len;
  1168. return addr;
  1169. }
  1170. if (addr + mm->cached_hole_size < vma->vm_start)
  1171. mm->cached_hole_size = vma->vm_start - addr;
  1172. addr = vma->vm_end;
  1173. }
  1174. }
  1175. #endif
  1176. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1177. {
  1178. /*
  1179. * Is this a new hole at the lowest possible address?
  1180. */
  1181. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
  1182. mm->free_area_cache = addr;
  1183. mm->cached_hole_size = ~0UL;
  1184. }
  1185. }
  1186. /*
  1187. * This mmap-allocator allocates new areas top-down from below the
  1188. * stack's low limit (the base):
  1189. */
  1190. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1191. unsigned long
  1192. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1193. const unsigned long len, const unsigned long pgoff,
  1194. const unsigned long flags)
  1195. {
  1196. struct vm_area_struct *vma;
  1197. struct mm_struct *mm = current->mm;
  1198. unsigned long addr = addr0;
  1199. /* requested length too big for entire address space */
  1200. if (len > TASK_SIZE)
  1201. return -ENOMEM;
  1202. if (flags & MAP_FIXED)
  1203. return addr;
  1204. /* requesting a specific address */
  1205. if (addr) {
  1206. addr = PAGE_ALIGN(addr);
  1207. vma = find_vma(mm, addr);
  1208. if (TASK_SIZE - len >= addr &&
  1209. (!vma || addr + len <= vma->vm_start))
  1210. return addr;
  1211. }
  1212. /* check if free_area_cache is useful for us */
  1213. if (len <= mm->cached_hole_size) {
  1214. mm->cached_hole_size = 0;
  1215. mm->free_area_cache = mm->mmap_base;
  1216. }
  1217. /* either no address requested or can't fit in requested address hole */
  1218. addr = mm->free_area_cache;
  1219. /* make sure it can fit in the remaining address space */
  1220. if (addr > len) {
  1221. vma = find_vma(mm, addr-len);
  1222. if (!vma || addr <= vma->vm_start)
  1223. /* remember the address as a hint for next time */
  1224. return (mm->free_area_cache = addr-len);
  1225. }
  1226. if (mm->mmap_base < len)
  1227. goto bottomup;
  1228. addr = mm->mmap_base-len;
  1229. do {
  1230. /*
  1231. * Lookup failure means no vma is above this address,
  1232. * else if new region fits below vma->vm_start,
  1233. * return with success:
  1234. */
  1235. vma = find_vma(mm, addr);
  1236. if (!vma || addr+len <= vma->vm_start)
  1237. /* remember the address as a hint for next time */
  1238. return (mm->free_area_cache = addr);
  1239. /* remember the largest hole we saw so far */
  1240. if (addr + mm->cached_hole_size < vma->vm_start)
  1241. mm->cached_hole_size = vma->vm_start - addr;
  1242. /* try just below the current vma->vm_start */
  1243. addr = vma->vm_start-len;
  1244. } while (len < vma->vm_start);
  1245. bottomup:
  1246. /*
  1247. * A failed mmap() very likely causes application failure,
  1248. * so fall back to the bottom-up function here. This scenario
  1249. * can happen with large stack limits and large mmap()
  1250. * allocations.
  1251. */
  1252. mm->cached_hole_size = ~0UL;
  1253. mm->free_area_cache = TASK_UNMAPPED_BASE;
  1254. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  1255. /*
  1256. * Restore the topdown base:
  1257. */
  1258. mm->free_area_cache = mm->mmap_base;
  1259. mm->cached_hole_size = ~0UL;
  1260. return addr;
  1261. }
  1262. #endif
  1263. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1264. {
  1265. /*
  1266. * Is this a new hole at the highest possible address?
  1267. */
  1268. if (addr > mm->free_area_cache)
  1269. mm->free_area_cache = addr;
  1270. /* dont allow allocations above current base */
  1271. if (mm->free_area_cache > mm->mmap_base)
  1272. mm->free_area_cache = mm->mmap_base;
  1273. }
  1274. unsigned long
  1275. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1276. unsigned long pgoff, unsigned long flags)
  1277. {
  1278. unsigned long (*get_area)(struct file *, unsigned long,
  1279. unsigned long, unsigned long, unsigned long);
  1280. get_area = current->mm->get_unmapped_area;
  1281. if (file && file->f_op && file->f_op->get_unmapped_area)
  1282. get_area = file->f_op->get_unmapped_area;
  1283. addr = get_area(file, addr, len, pgoff, flags);
  1284. if (IS_ERR_VALUE(addr))
  1285. return addr;
  1286. if (addr > TASK_SIZE - len)
  1287. return -ENOMEM;
  1288. if (addr & ~PAGE_MASK)
  1289. return -EINVAL;
  1290. return arch_rebalance_pgtables(addr, len);
  1291. }
  1292. EXPORT_SYMBOL(get_unmapped_area);
  1293. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1294. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1295. {
  1296. struct vm_area_struct *vma = NULL;
  1297. if (mm) {
  1298. /* Check the cache first. */
  1299. /* (Cache hit rate is typically around 35%.) */
  1300. vma = mm->mmap_cache;
  1301. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1302. struct rb_node * rb_node;
  1303. rb_node = mm->mm_rb.rb_node;
  1304. vma = NULL;
  1305. while (rb_node) {
  1306. struct vm_area_struct * vma_tmp;
  1307. vma_tmp = rb_entry(rb_node,
  1308. struct vm_area_struct, vm_rb);
  1309. if (vma_tmp->vm_end > addr) {
  1310. vma = vma_tmp;
  1311. if (vma_tmp->vm_start <= addr)
  1312. break;
  1313. rb_node = rb_node->rb_left;
  1314. } else
  1315. rb_node = rb_node->rb_right;
  1316. }
  1317. if (vma)
  1318. mm->mmap_cache = vma;
  1319. }
  1320. }
  1321. return vma;
  1322. }
  1323. EXPORT_SYMBOL(find_vma);
  1324. /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
  1325. struct vm_area_struct *
  1326. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1327. struct vm_area_struct **pprev)
  1328. {
  1329. struct vm_area_struct *vma = NULL, *prev = NULL;
  1330. struct rb_node *rb_node;
  1331. if (!mm)
  1332. goto out;
  1333. /* Guard against addr being lower than the first VMA */
  1334. vma = mm->mmap;
  1335. /* Go through the RB tree quickly. */
  1336. rb_node = mm->mm_rb.rb_node;
  1337. while (rb_node) {
  1338. struct vm_area_struct *vma_tmp;
  1339. vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1340. if (addr < vma_tmp->vm_end) {
  1341. rb_node = rb_node->rb_left;
  1342. } else {
  1343. prev = vma_tmp;
  1344. if (!prev->vm_next || (addr < prev->vm_next->vm_end))
  1345. break;
  1346. rb_node = rb_node->rb_right;
  1347. }
  1348. }
  1349. out:
  1350. *pprev = prev;
  1351. return prev ? prev->vm_next : vma;
  1352. }
  1353. /*
  1354. * Verify that the stack growth is acceptable and
  1355. * update accounting. This is shared with both the
  1356. * grow-up and grow-down cases.
  1357. */
  1358. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1359. {
  1360. struct mm_struct *mm = vma->vm_mm;
  1361. struct rlimit *rlim = current->signal->rlim;
  1362. unsigned long new_start;
  1363. /* address space limit tests */
  1364. if (!may_expand_vm(mm, grow))
  1365. return -ENOMEM;
  1366. /* Stack limit test */
  1367. if (size > rlim[RLIMIT_STACK].rlim_cur)
  1368. return -ENOMEM;
  1369. /* mlock limit tests */
  1370. if (vma->vm_flags & VM_LOCKED) {
  1371. unsigned long locked;
  1372. unsigned long limit;
  1373. locked = mm->locked_vm + grow;
  1374. limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  1375. if (locked > limit && !capable(CAP_IPC_LOCK))
  1376. return -ENOMEM;
  1377. }
  1378. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1379. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1380. vma->vm_end - size;
  1381. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1382. return -EFAULT;
  1383. /*
  1384. * Overcommit.. This must be the final test, as it will
  1385. * update security statistics.
  1386. */
  1387. if (security_vm_enough_memory_mm(mm, grow))
  1388. return -ENOMEM;
  1389. /* Ok, everything looks good - let it rip */
  1390. mm->total_vm += grow;
  1391. if (vma->vm_flags & VM_LOCKED)
  1392. mm->locked_vm += grow;
  1393. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1394. return 0;
  1395. }
  1396. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1397. /*
  1398. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1399. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1400. */
  1401. #ifndef CONFIG_IA64
  1402. static
  1403. #endif
  1404. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1405. {
  1406. int error;
  1407. if (!(vma->vm_flags & VM_GROWSUP))
  1408. return -EFAULT;
  1409. /*
  1410. * We must make sure the anon_vma is allocated
  1411. * so that the anon_vma locking is not a noop.
  1412. */
  1413. if (unlikely(anon_vma_prepare(vma)))
  1414. return -ENOMEM;
  1415. anon_vma_lock(vma);
  1416. /*
  1417. * vma->vm_start/vm_end cannot change under us because the caller
  1418. * is required to hold the mmap_sem in read mode. We need the
  1419. * anon_vma lock to serialize against concurrent expand_stacks.
  1420. * Also guard against wrapping around to address 0.
  1421. */
  1422. if (address < PAGE_ALIGN(address+4))
  1423. address = PAGE_ALIGN(address+4);
  1424. else {
  1425. anon_vma_unlock(vma);
  1426. return -ENOMEM;
  1427. }
  1428. error = 0;
  1429. /* Somebody else might have raced and expanded it already */
  1430. if (address > vma->vm_end) {
  1431. unsigned long size, grow;
  1432. size = address - vma->vm_start;
  1433. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1434. error = acct_stack_growth(vma, size, grow);
  1435. if (!error)
  1436. vma->vm_end = address;
  1437. }
  1438. anon_vma_unlock(vma);
  1439. return error;
  1440. }
  1441. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1442. /*
  1443. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1444. */
  1445. static int expand_downwards(struct vm_area_struct *vma,
  1446. unsigned long address)
  1447. {
  1448. int error;
  1449. /*
  1450. * We must make sure the anon_vma is allocated
  1451. * so that the anon_vma locking is not a noop.
  1452. */
  1453. if (unlikely(anon_vma_prepare(vma)))
  1454. return -ENOMEM;
  1455. address &= PAGE_MASK;
  1456. error = security_file_mmap(NULL, 0, 0, 0, address, 1);
  1457. if (error)
  1458. return error;
  1459. anon_vma_lock(vma);
  1460. /*
  1461. * vma->vm_start/vm_end cannot change under us because the caller
  1462. * is required to hold the mmap_sem in read mode. We need the
  1463. * anon_vma lock to serialize against concurrent expand_stacks.
  1464. */
  1465. /* Somebody else might have raced and expanded it already */
  1466. if (address < vma->vm_start) {
  1467. unsigned long size, grow;
  1468. size = vma->vm_end - address;
  1469. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1470. error = acct_stack_growth(vma, size, grow);
  1471. if (!error) {
  1472. vma->vm_start = address;
  1473. vma->vm_pgoff -= grow;
  1474. }
  1475. }
  1476. anon_vma_unlock(vma);
  1477. return error;
  1478. }
  1479. int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
  1480. {
  1481. return expand_downwards(vma, address);
  1482. }
  1483. #ifdef CONFIG_STACK_GROWSUP
  1484. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1485. {
  1486. return expand_upwards(vma, address);
  1487. }
  1488. struct vm_area_struct *
  1489. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1490. {
  1491. struct vm_area_struct *vma, *prev;
  1492. addr &= PAGE_MASK;
  1493. vma = find_vma_prev(mm, addr, &prev);
  1494. if (vma && (vma->vm_start <= addr))
  1495. return vma;
  1496. if (!prev || expand_stack(prev, addr))
  1497. return NULL;
  1498. if (prev->vm_flags & VM_LOCKED) {
  1499. if (mlock_vma_pages_range(prev, addr, prev->vm_end) < 0)
  1500. return NULL; /* vma gone! */
  1501. }
  1502. return prev;
  1503. }
  1504. #else
  1505. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1506. {
  1507. return expand_downwards(vma, address);
  1508. }
  1509. struct vm_area_struct *
  1510. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1511. {
  1512. struct vm_area_struct * vma;
  1513. unsigned long start;
  1514. addr &= PAGE_MASK;
  1515. vma = find_vma(mm,addr);
  1516. if (!vma)
  1517. return NULL;
  1518. if (vma->vm_start <= addr)
  1519. return vma;
  1520. if (!(vma->vm_flags & VM_GROWSDOWN))
  1521. return NULL;
  1522. start = vma->vm_start;
  1523. if (expand_stack(vma, addr))
  1524. return NULL;
  1525. if (vma->vm_flags & VM_LOCKED) {
  1526. if (mlock_vma_pages_range(vma, addr, start) < 0)
  1527. return NULL; /* vma gone! */
  1528. }
  1529. return vma;
  1530. }
  1531. #endif
  1532. /*
  1533. * Ok - we have the memory areas we should free on the vma list,
  1534. * so release them, and do the vma updates.
  1535. *
  1536. * Called with the mm semaphore held.
  1537. */
  1538. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1539. {
  1540. /* Update high watermark before we lower total_vm */
  1541. update_hiwater_vm(mm);
  1542. do {
  1543. long nrpages = vma_pages(vma);
  1544. mm->total_vm -= nrpages;
  1545. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1546. vma = remove_vma(vma);
  1547. } while (vma);
  1548. validate_mm(mm);
  1549. }
  1550. /*
  1551. * Get rid of page table information in the indicated region.
  1552. *
  1553. * Called with the mm semaphore held.
  1554. */
  1555. static void unmap_region(struct mm_struct *mm,
  1556. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1557. unsigned long start, unsigned long end)
  1558. {
  1559. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1560. struct mmu_gather *tlb;
  1561. unsigned long nr_accounted = 0;
  1562. lru_add_drain();
  1563. tlb = tlb_gather_mmu(mm, 0);
  1564. update_hiwater_rss(mm);
  1565. unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
  1566. vm_unacct_memory(nr_accounted);
  1567. free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
  1568. next? next->vm_start: 0);
  1569. tlb_finish_mmu(tlb, start, end);
  1570. }
  1571. /*
  1572. * Create a list of vma's touched by the unmap, removing them from the mm's
  1573. * vma list as we go..
  1574. */
  1575. static void
  1576. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  1577. struct vm_area_struct *prev, unsigned long end)
  1578. {
  1579. struct vm_area_struct **insertion_point;
  1580. struct vm_area_struct *tail_vma = NULL;
  1581. unsigned long addr;
  1582. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  1583. do {
  1584. rb_erase(&vma->vm_rb, &mm->mm_rb);
  1585. mm->map_count--;
  1586. tail_vma = vma;
  1587. vma = vma->vm_next;
  1588. } while (vma && vma->vm_start < end);
  1589. *insertion_point = vma;
  1590. tail_vma->vm_next = NULL;
  1591. if (mm->unmap_area == arch_unmap_area)
  1592. addr = prev ? prev->vm_end : mm->mmap_base;
  1593. else
  1594. addr = vma ? vma->vm_start : mm->mmap_base;
  1595. mm->unmap_area(mm, addr);
  1596. mm->mmap_cache = NULL; /* Kill the cache. */
  1597. }
  1598. /*
  1599. * Split a vma into two pieces at address 'addr', a new vma is allocated
  1600. * either for the first part or the tail.
  1601. */
  1602. int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  1603. unsigned long addr, int new_below)
  1604. {
  1605. struct mempolicy *pol;
  1606. struct vm_area_struct *new;
  1607. if (is_vm_hugetlb_page(vma) && (addr &
  1608. ~(huge_page_mask(hstate_vma(vma)))))
  1609. return -EINVAL;
  1610. if (mm->map_count >= sysctl_max_map_count)
  1611. return -ENOMEM;
  1612. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1613. if (!new)
  1614. return -ENOMEM;
  1615. /* most fields are the same, copy all, and then fixup */
  1616. *new = *vma;
  1617. if (new_below)
  1618. new->vm_end = addr;
  1619. else {
  1620. new->vm_start = addr;
  1621. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  1622. }
  1623. pol = mpol_dup(vma_policy(vma));
  1624. if (IS_ERR(pol)) {
  1625. kmem_cache_free(vm_area_cachep, new);
  1626. return PTR_ERR(pol);
  1627. }
  1628. vma_set_policy(new, pol);
  1629. if (new->vm_file) {
  1630. get_file(new->vm_file);
  1631. if (vma->vm_flags & VM_EXECUTABLE)
  1632. added_exe_file_vma(mm);
  1633. }
  1634. if (new->vm_ops && new->vm_ops->open)
  1635. new->vm_ops->open(new);
  1636. if (new_below)
  1637. vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  1638. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  1639. else
  1640. vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  1641. return 0;
  1642. }
  1643. /* Munmap is split into 2 main parts -- this part which finds
  1644. * what needs doing, and the areas themselves, which do the
  1645. * work. This now handles partial unmappings.
  1646. * Jeremy Fitzhardinge <jeremy@goop.org>
  1647. */
  1648. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1649. {
  1650. unsigned long end;
  1651. struct vm_area_struct *vma, *prev, *last;
  1652. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  1653. return -EINVAL;
  1654. if ((len = PAGE_ALIGN(len)) == 0)
  1655. return -EINVAL;
  1656. /* Find the first overlapping VMA */
  1657. vma = find_vma_prev(mm, start, &prev);
  1658. if (!vma)
  1659. return 0;
  1660. /* we have start < vma->vm_end */
  1661. /* if it doesn't overlap, we have nothing.. */
  1662. end = start + len;
  1663. if (vma->vm_start >= end)
  1664. return 0;
  1665. /*
  1666. * If we need to split any vma, do it now to save pain later.
  1667. *
  1668. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  1669. * unmapped vm_area_struct will remain in use: so lower split_vma
  1670. * places tmp vma above, and higher split_vma places tmp vma below.
  1671. */
  1672. if (start > vma->vm_start) {
  1673. int error = split_vma(mm, vma, start, 0);
  1674. if (error)
  1675. return error;
  1676. prev = vma;
  1677. }
  1678. /* Does it split the last one? */
  1679. last = find_vma(mm, end);
  1680. if (last && end > last->vm_start) {
  1681. int error = split_vma(mm, last, end, 1);
  1682. if (error)
  1683. return error;
  1684. }
  1685. vma = prev? prev->vm_next: mm->mmap;
  1686. /*
  1687. * unlock any mlock()ed ranges before detaching vmas
  1688. */
  1689. if (mm->locked_vm) {
  1690. struct vm_area_struct *tmp = vma;
  1691. while (tmp && tmp->vm_start < end) {
  1692. if (tmp->vm_flags & VM_LOCKED) {
  1693. mm->locked_vm -= vma_pages(tmp);
  1694. munlock_vma_pages_all(tmp);
  1695. }
  1696. tmp = tmp->vm_next;
  1697. }
  1698. }
  1699. /*
  1700. * Remove the vma's, and unmap the actual pages
  1701. */
  1702. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  1703. unmap_region(mm, vma, prev, start, end);
  1704. /* Fix up all other VM information */
  1705. remove_vma_list(mm, vma);
  1706. return 0;
  1707. }
  1708. EXPORT_SYMBOL(do_munmap);
  1709. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1710. {
  1711. int ret;
  1712. struct mm_struct *mm = current->mm;
  1713. profile_munmap(addr);
  1714. down_write(&mm->mmap_sem);
  1715. ret = do_munmap(mm, addr, len);
  1716. up_write(&mm->mmap_sem);
  1717. return ret;
  1718. }
  1719. static inline void verify_mm_writelocked(struct mm_struct *mm)
  1720. {
  1721. #ifdef CONFIG_DEBUG_VM
  1722. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  1723. WARN_ON(1);
  1724. up_read(&mm->mmap_sem);
  1725. }
  1726. #endif
  1727. }
  1728. /*
  1729. * this is really a simplified "do_mmap". it only handles
  1730. * anonymous maps. eventually we may be able to do some
  1731. * brk-specific accounting here.
  1732. */
  1733. unsigned long do_brk(unsigned long addr, unsigned long len)
  1734. {
  1735. struct mm_struct * mm = current->mm;
  1736. struct vm_area_struct * vma, * prev;
  1737. unsigned long flags;
  1738. struct rb_node ** rb_link, * rb_parent;
  1739. pgoff_t pgoff = addr >> PAGE_SHIFT;
  1740. int error;
  1741. len = PAGE_ALIGN(len);
  1742. if (!len)
  1743. return addr;
  1744. if ((addr + len) > TASK_SIZE || (addr + len) < addr)
  1745. return -EINVAL;
  1746. if (is_hugepage_only_range(mm, addr, len))
  1747. return -EINVAL;
  1748. error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
  1749. if (error)
  1750. return error;
  1751. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  1752. error = arch_mmap_check(addr, len, flags);
  1753. if (error)
  1754. return error;
  1755. /*
  1756. * mlock MCL_FUTURE?
  1757. */
  1758. if (mm->def_flags & VM_LOCKED) {
  1759. unsigned long locked, lock_limit;
  1760. locked = len >> PAGE_SHIFT;
  1761. locked += mm->locked_vm;
  1762. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1763. lock_limit >>= PAGE_SHIFT;
  1764. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1765. return -EAGAIN;
  1766. }
  1767. /*
  1768. * mm->mmap_sem is required to protect against another thread
  1769. * changing the mappings in case we sleep.
  1770. */
  1771. verify_mm_writelocked(mm);
  1772. /*
  1773. * Clear old maps. this also does some error checking for us
  1774. */
  1775. munmap_back:
  1776. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1777. if (vma && vma->vm_start < addr + len) {
  1778. if (do_munmap(mm, addr, len))
  1779. return -ENOMEM;
  1780. goto munmap_back;
  1781. }
  1782. /* Check against address space limits *after* clearing old maps... */
  1783. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1784. return -ENOMEM;
  1785. if (mm->map_count > sysctl_max_map_count)
  1786. return -ENOMEM;
  1787. if (security_vm_enough_memory(len >> PAGE_SHIFT))
  1788. return -ENOMEM;
  1789. /* Can we just expand an old private anonymous mapping? */
  1790. vma = vma_merge(mm, prev, addr, addr + len, flags,
  1791. NULL, NULL, pgoff, NULL);
  1792. if (vma)
  1793. goto out;
  1794. /*
  1795. * create a vma struct for an anonymous mapping
  1796. */
  1797. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1798. if (!vma) {
  1799. vm_unacct_memory(len >> PAGE_SHIFT);
  1800. return -ENOMEM;
  1801. }
  1802. vma->vm_mm = mm;
  1803. vma->vm_start = addr;
  1804. vma->vm_end = addr + len;
  1805. vma->vm_pgoff = pgoff;
  1806. vma->vm_flags = flags;
  1807. vma->vm_page_prot = vm_get_page_prot(flags);
  1808. vma_link(mm, vma, prev, rb_link, rb_parent);
  1809. out:
  1810. mm->total_vm += len >> PAGE_SHIFT;
  1811. if (flags & VM_LOCKED) {
  1812. if (!mlock_vma_pages_range(vma, addr, addr + len))
  1813. mm->locked_vm += (len >> PAGE_SHIFT);
  1814. }
  1815. return addr;
  1816. }
  1817. EXPORT_SYMBOL(do_brk);
  1818. /* Release all mmaps. */
  1819. void exit_mmap(struct mm_struct *mm)
  1820. {
  1821. struct mmu_gather *tlb;
  1822. struct vm_area_struct *vma;
  1823. unsigned long nr_accounted = 0;
  1824. unsigned long end;
  1825. /* mm's last user has gone, and its about to be pulled down */
  1826. mmu_notifier_release(mm);
  1827. if (mm->locked_vm) {
  1828. vma = mm->mmap;
  1829. while (vma) {
  1830. if (vma->vm_flags & VM_LOCKED)
  1831. munlock_vma_pages_all(vma);
  1832. vma = vma->vm_next;
  1833. }
  1834. }
  1835. arch_exit_mmap(mm);
  1836. vma = mm->mmap;
  1837. if (!vma) /* Can happen if dup_mmap() received an OOM */
  1838. return;
  1839. lru_add_drain();
  1840. flush_cache_mm(mm);
  1841. tlb = tlb_gather_mmu(mm, 1);
  1842. /* update_hiwater_rss(mm) here? but nobody should be looking */
  1843. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  1844. end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
  1845. vm_unacct_memory(nr_accounted);
  1846. free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
  1847. tlb_finish_mmu(tlb, 0, end);
  1848. /*
  1849. * Walk the list again, actually closing and freeing it,
  1850. * with preemption enabled, without holding any MM locks.
  1851. */
  1852. while (vma)
  1853. vma = remove_vma(vma);
  1854. BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  1855. }
  1856. /* Insert vm structure into process list sorted by address
  1857. * and into the inode's i_mmap tree. If vm_file is non-NULL
  1858. * then i_mmap_lock is taken here.
  1859. */
  1860. int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  1861. {
  1862. struct vm_area_struct * __vma, * prev;
  1863. struct rb_node ** rb_link, * rb_parent;
  1864. /*
  1865. * The vm_pgoff of a purely anonymous vma should be irrelevant
  1866. * until its first write fault, when page's anon_vma and index
  1867. * are set. But now set the vm_pgoff it will almost certainly
  1868. * end up with (unless mremap moves it elsewhere before that
  1869. * first wfault), so /proc/pid/maps tells a consistent story.
  1870. *
  1871. * By setting it to reflect the virtual start address of the
  1872. * vma, merges and splits can happen in a seamless way, just
  1873. * using the existing file pgoff checks and manipulations.
  1874. * Similarly in do_mmap_pgoff and in do_brk.
  1875. */
  1876. if (!vma->vm_file) {
  1877. BUG_ON(vma->anon_vma);
  1878. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1879. }
  1880. __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
  1881. if (__vma && __vma->vm_start < vma->vm_end)
  1882. return -ENOMEM;
  1883. if ((vma->vm_flags & VM_ACCOUNT) &&
  1884. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  1885. return -ENOMEM;
  1886. vma_link(mm, vma, prev, rb_link, rb_parent);
  1887. return 0;
  1888. }
  1889. /*
  1890. * Copy the vma structure to a new location in the same mm,
  1891. * prior to moving page table entries, to effect an mremap move.
  1892. */
  1893. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  1894. unsigned long addr, unsigned long len, pgoff_t pgoff)
  1895. {
  1896. struct vm_area_struct *vma = *vmap;
  1897. unsigned long vma_start = vma->vm_start;
  1898. struct mm_struct *mm = vma->vm_mm;
  1899. struct vm_area_struct *new_vma, *prev;
  1900. struct rb_node **rb_link, *rb_parent;
  1901. struct mempolicy *pol;
  1902. /*
  1903. * If anonymous vma has not yet been faulted, update new pgoff
  1904. * to match new location, to increase its chance of merging.
  1905. */
  1906. if (!vma->vm_file && !vma->anon_vma)
  1907. pgoff = addr >> PAGE_SHIFT;
  1908. find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1909. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  1910. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  1911. if (new_vma) {
  1912. /*
  1913. * Source vma may have been merged into new_vma
  1914. */
  1915. if (vma_start >= new_vma->vm_start &&
  1916. vma_start < new_vma->vm_end)
  1917. *vmap = new_vma;
  1918. } else {
  1919. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1920. if (new_vma) {
  1921. *new_vma = *vma;
  1922. pol = mpol_dup(vma_policy(vma));
  1923. if (IS_ERR(pol)) {
  1924. kmem_cache_free(vm_area_cachep, new_vma);
  1925. return NULL;
  1926. }
  1927. vma_set_policy(new_vma, pol);
  1928. new_vma->vm_start = addr;
  1929. new_vma->vm_end = addr + len;
  1930. new_vma->vm_pgoff = pgoff;
  1931. if (new_vma->vm_file) {
  1932. get_file(new_vma->vm_file);
  1933. if (vma->vm_flags & VM_EXECUTABLE)
  1934. added_exe_file_vma(mm);
  1935. }
  1936. if (new_vma->vm_ops && new_vma->vm_ops->open)
  1937. new_vma->vm_ops->open(new_vma);
  1938. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  1939. }
  1940. }
  1941. return new_vma;
  1942. }
  1943. /*
  1944. * Return true if the calling process may expand its vm space by the passed
  1945. * number of pages
  1946. */
  1947. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  1948. {
  1949. unsigned long cur = mm->total_vm; /* pages */
  1950. unsigned long lim;
  1951. lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  1952. if (cur + npages > lim)
  1953. return 0;
  1954. return 1;
  1955. }
  1956. static int special_mapping_fault(struct vm_area_struct *vma,
  1957. struct vm_fault *vmf)
  1958. {
  1959. pgoff_t pgoff;
  1960. struct page **pages;
  1961. /*
  1962. * special mappings have no vm_file, and in that case, the mm
  1963. * uses vm_pgoff internally. So we have to subtract it from here.
  1964. * We are allowed to do this because we are the mm; do not copy
  1965. * this code into drivers!
  1966. */
  1967. pgoff = vmf->pgoff - vma->vm_pgoff;
  1968. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  1969. pgoff--;
  1970. if (*pages) {
  1971. struct page *page = *pages;
  1972. get_page(page);
  1973. vmf->page = page;
  1974. return 0;
  1975. }
  1976. return VM_FAULT_SIGBUS;
  1977. }
  1978. /*
  1979. * Having a close hook prevents vma merging regardless of flags.
  1980. */
  1981. static void special_mapping_close(struct vm_area_struct *vma)
  1982. {
  1983. }
  1984. static struct vm_operations_struct special_mapping_vmops = {
  1985. .close = special_mapping_close,
  1986. .fault = special_mapping_fault,
  1987. };
  1988. /*
  1989. * Called with mm->mmap_sem held for writing.
  1990. * Insert a new vma covering the given region, with the given flags.
  1991. * Its pages are supplied by the given array of struct page *.
  1992. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  1993. * The region past the last page supplied will always produce SIGBUS.
  1994. * The array pointer and the pages it points to are assumed to stay alive
  1995. * for as long as this mapping might exist.
  1996. */
  1997. int install_special_mapping(struct mm_struct *mm,
  1998. unsigned long addr, unsigned long len,
  1999. unsigned long vm_flags, struct page **pages)
  2000. {
  2001. struct vm_area_struct *vma;
  2002. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2003. if (unlikely(vma == NULL))
  2004. return -ENOMEM;
  2005. vma->vm_mm = mm;
  2006. vma->vm_start = addr;
  2007. vma->vm_end = addr + len;
  2008. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2009. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2010. vma->vm_ops = &special_mapping_vmops;
  2011. vma->vm_private_data = pages;
  2012. if (unlikely(insert_vm_struct(mm, vma))) {
  2013. kmem_cache_free(vm_area_cachep, vma);
  2014. return -ENOMEM;
  2015. }
  2016. mm->total_vm += len >> PAGE_SHIFT;
  2017. return 0;
  2018. }
  2019. static DEFINE_MUTEX(mm_all_locks_mutex);
  2020. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2021. {
  2022. if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
  2023. /*
  2024. * The LSB of head.next can't change from under us
  2025. * because we hold the mm_all_locks_mutex.
  2026. */
  2027. spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
  2028. /*
  2029. * We can safely modify head.next after taking the
  2030. * anon_vma->lock. If some other vma in this mm shares
  2031. * the same anon_vma we won't take it again.
  2032. *
  2033. * No need of atomic instructions here, head.next
  2034. * can't change from under us thanks to the
  2035. * anon_vma->lock.
  2036. */
  2037. if (__test_and_set_bit(0, (unsigned long *)
  2038. &anon_vma->head.next))
  2039. BUG();
  2040. }
  2041. }
  2042. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2043. {
  2044. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2045. /*
  2046. * AS_MM_ALL_LOCKS can't change from under us because
  2047. * we hold the mm_all_locks_mutex.
  2048. *
  2049. * Operations on ->flags have to be atomic because
  2050. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2051. * mm_all_locks_mutex, there may be other cpus
  2052. * changing other bitflags in parallel to us.
  2053. */
  2054. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2055. BUG();
  2056. spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
  2057. }
  2058. }
  2059. /*
  2060. * This operation locks against the VM for all pte/vma/mm related
  2061. * operations that could ever happen on a certain mm. This includes
  2062. * vmtruncate, try_to_unmap, and all page faults.
  2063. *
  2064. * The caller must take the mmap_sem in write mode before calling
  2065. * mm_take_all_locks(). The caller isn't allowed to release the
  2066. * mmap_sem until mm_drop_all_locks() returns.
  2067. *
  2068. * mmap_sem in write mode is required in order to block all operations
  2069. * that could modify pagetables and free pages without need of
  2070. * altering the vma layout (for example populate_range() with
  2071. * nonlinear vmas). It's also needed in write mode to avoid new
  2072. * anon_vmas to be associated with existing vmas.
  2073. *
  2074. * A single task can't take more than one mm_take_all_locks() in a row
  2075. * or it would deadlock.
  2076. *
  2077. * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
  2078. * mapping->flags avoid to take the same lock twice, if more than one
  2079. * vma in this mm is backed by the same anon_vma or address_space.
  2080. *
  2081. * We can take all the locks in random order because the VM code
  2082. * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
  2083. * takes more than one of them in a row. Secondly we're protected
  2084. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2085. *
  2086. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2087. * that may have to take thousand of locks.
  2088. *
  2089. * mm_take_all_locks() can fail if it's interrupted by signals.
  2090. */
  2091. int mm_take_all_locks(struct mm_struct *mm)
  2092. {
  2093. struct vm_area_struct *vma;
  2094. int ret = -EINTR;
  2095. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2096. mutex_lock(&mm_all_locks_mutex);
  2097. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2098. if (signal_pending(current))
  2099. goto out_unlock;
  2100. if (vma->vm_file && vma->vm_file->f_mapping)
  2101. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2102. }
  2103. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2104. if (signal_pending(current))
  2105. goto out_unlock;
  2106. if (vma->anon_vma)
  2107. vm_lock_anon_vma(mm, vma->anon_vma);
  2108. }
  2109. ret = 0;
  2110. out_unlock:
  2111. if (ret)
  2112. mm_drop_all_locks(mm);
  2113. return ret;
  2114. }
  2115. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2116. {
  2117. if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
  2118. /*
  2119. * The LSB of head.next can't change to 0 from under
  2120. * us because we hold the mm_all_locks_mutex.
  2121. *
  2122. * We must however clear the bitflag before unlocking
  2123. * the vma so the users using the anon_vma->head will
  2124. * never see our bitflag.
  2125. *
  2126. * No need of atomic instructions here, head.next
  2127. * can't change from under us until we release the
  2128. * anon_vma->lock.
  2129. */
  2130. if (!__test_and_clear_bit(0, (unsigned long *)
  2131. &anon_vma->head.next))
  2132. BUG();
  2133. spin_unlock(&anon_vma->lock);
  2134. }
  2135. }
  2136. static void vm_unlock_mapping(struct address_space *mapping)
  2137. {
  2138. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2139. /*
  2140. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2141. * because we hold the mm_all_locks_mutex.
  2142. */
  2143. spin_unlock(&mapping->i_mmap_lock);
  2144. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2145. &mapping->flags))
  2146. BUG();
  2147. }
  2148. }
  2149. /*
  2150. * The mmap_sem cannot be released by the caller until
  2151. * mm_drop_all_locks() returns.
  2152. */
  2153. void mm_drop_all_locks(struct mm_struct *mm)
  2154. {
  2155. struct vm_area_struct *vma;
  2156. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2157. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2158. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2159. if (vma->anon_vma)
  2160. vm_unlock_anon_vma(vma->anon_vma);
  2161. if (vma->vm_file && vma->vm_file->f_mapping)
  2162. vm_unlock_mapping(vma->vm_file->f_mapping);
  2163. }
  2164. mutex_unlock(&mm_all_locks_mutex);
  2165. }
  2166. /*
  2167. * initialise the VMA slab
  2168. */
  2169. void __init mmap_init(void)
  2170. {
  2171. int ret;
  2172. ret = percpu_counter_init(&vm_committed_as, 0);
  2173. VM_BUG_ON(ret);
  2174. }