mmap.c 54 KB

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