mmap.c 54 KB

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