mmap.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089
  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. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  61. int sysctl_overcommit_ratio = 50; /* default is 50% */
  62. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  63. atomic_t vm_committed_space = ATOMIC_INIT(0);
  64. /*
  65. * Check that a process has enough memory to allocate a new virtual
  66. * mapping. 0 means there is enough memory for the allocation to
  67. * succeed and -ENOMEM implies there is not.
  68. *
  69. * We currently support three overcommit policies, which are set via the
  70. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  71. *
  72. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  73. * Additional code 2002 Jul 20 by Robert Love.
  74. *
  75. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  76. *
  77. * Note this is a helper function intended to be used by LSMs which
  78. * wish to use this logic.
  79. */
  80. int __vm_enough_memory(long pages, int cap_sys_admin)
  81. {
  82. unsigned long free, allowed;
  83. vm_acct_memory(pages);
  84. /*
  85. * Sometimes we want to use more memory than we have
  86. */
  87. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  88. return 0;
  89. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  90. unsigned long n;
  91. free = global_page_state(NR_FILE_PAGES);
  92. free += nr_swap_pages;
  93. /*
  94. * Any slabs which are created with the
  95. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  96. * which are reclaimable, under pressure. The dentry
  97. * cache and most inode caches should fall into this
  98. */
  99. free += atomic_read(&slab_reclaim_pages);
  100. /*
  101. * Leave the last 3% for root
  102. */
  103. if (!cap_sys_admin)
  104. free -= free / 32;
  105. if (free > pages)
  106. return 0;
  107. /*
  108. * nr_free_pages() is very expensive on large systems,
  109. * only call if we're about to fail.
  110. */
  111. n = nr_free_pages();
  112. /*
  113. * Leave reserved pages. The pages are not for anonymous pages.
  114. */
  115. if (n <= totalreserve_pages)
  116. goto error;
  117. else
  118. n -= totalreserve_pages;
  119. /*
  120. * Leave the last 3% for root
  121. */
  122. if (!cap_sys_admin)
  123. n -= n / 32;
  124. free += n;
  125. if (free > pages)
  126. return 0;
  127. goto error;
  128. }
  129. allowed = (totalram_pages - hugetlb_total_pages())
  130. * sysctl_overcommit_ratio / 100;
  131. /*
  132. * Leave the last 3% for root
  133. */
  134. if (!cap_sys_admin)
  135. allowed -= allowed / 32;
  136. allowed += total_swap_pages;
  137. /* Don't let a single process grow too big:
  138. leave 3% of the size of this process for other processes */
  139. allowed -= current->mm->total_vm / 32;
  140. /*
  141. * cast `allowed' as a signed long because vm_committed_space
  142. * sometimes has a negative value
  143. */
  144. if (atomic_read(&vm_committed_space) < (long)allowed)
  145. return 0;
  146. error:
  147. vm_unacct_memory(pages);
  148. return -ENOMEM;
  149. }
  150. EXPORT_SYMBOL(__vm_enough_memory);
  151. /*
  152. * Requires inode->i_mapping->i_mmap_lock
  153. */
  154. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  155. struct file *file, struct address_space *mapping)
  156. {
  157. if (vma->vm_flags & VM_DENYWRITE)
  158. atomic_inc(&file->f_dentry->d_inode->i_writecount);
  159. if (vma->vm_flags & VM_SHARED)
  160. mapping->i_mmap_writable--;
  161. flush_dcache_mmap_lock(mapping);
  162. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  163. list_del_init(&vma->shared.vm_set.list);
  164. else
  165. vma_prio_tree_remove(vma, &mapping->i_mmap);
  166. flush_dcache_mmap_unlock(mapping);
  167. }
  168. /*
  169. * Unlink a file-based vm structure from its prio_tree, to hide
  170. * vma from rmap and vmtruncate before freeing its page tables.
  171. */
  172. void unlink_file_vma(struct vm_area_struct *vma)
  173. {
  174. struct file *file = vma->vm_file;
  175. if (file) {
  176. struct address_space *mapping = file->f_mapping;
  177. spin_lock(&mapping->i_mmap_lock);
  178. __remove_shared_vm_struct(vma, file, mapping);
  179. spin_unlock(&mapping->i_mmap_lock);
  180. }
  181. }
  182. /*
  183. * Close a vm structure and free it, returning the next.
  184. */
  185. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  186. {
  187. struct vm_area_struct *next = vma->vm_next;
  188. might_sleep();
  189. if (vma->vm_ops && vma->vm_ops->close)
  190. vma->vm_ops->close(vma);
  191. if (vma->vm_file)
  192. fput(vma->vm_file);
  193. mpol_free(vma_policy(vma));
  194. kmem_cache_free(vm_area_cachep, vma);
  195. return next;
  196. }
  197. asmlinkage unsigned long sys_brk(unsigned long brk)
  198. {
  199. unsigned long rlim, retval;
  200. unsigned long newbrk, oldbrk;
  201. struct mm_struct *mm = current->mm;
  202. down_write(&mm->mmap_sem);
  203. if (brk < mm->end_code)
  204. goto out;
  205. /*
  206. * Check against rlimit here. If this check is done later after the test
  207. * of oldbrk with newbrk then it can escape the test and let the data
  208. * segment grow beyond its set limit the in case where the limit is
  209. * not page aligned -Ram Gupta
  210. */
  211. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  212. if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
  213. goto out;
  214. newbrk = PAGE_ALIGN(brk);
  215. oldbrk = PAGE_ALIGN(mm->brk);
  216. if (oldbrk == newbrk)
  217. goto set_brk;
  218. /* Always allow shrinking brk. */
  219. if (brk <= mm->brk) {
  220. if (!do_munmap(mm, newbrk, oldbrk-newbrk))
  221. goto set_brk;
  222. goto out;
  223. }
  224. /* Check against existing mmap mappings. */
  225. if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
  226. goto out;
  227. /* Ok, looks good - let it rip. */
  228. if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
  229. goto out;
  230. set_brk:
  231. mm->brk = brk;
  232. out:
  233. retval = mm->brk;
  234. up_write(&mm->mmap_sem);
  235. return retval;
  236. }
  237. #ifdef DEBUG_MM_RB
  238. static int browse_rb(struct rb_root *root)
  239. {
  240. int i = 0, j;
  241. struct rb_node *nd, *pn = NULL;
  242. unsigned long prev = 0, pend = 0;
  243. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  244. struct vm_area_struct *vma;
  245. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  246. if (vma->vm_start < prev)
  247. printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
  248. if (vma->vm_start < pend)
  249. printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
  250. if (vma->vm_start > vma->vm_end)
  251. printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
  252. i++;
  253. pn = nd;
  254. }
  255. j = 0;
  256. for (nd = pn; nd; nd = rb_prev(nd)) {
  257. j++;
  258. }
  259. if (i != j)
  260. printk("backwards %d, forwards %d\n", j, i), i = 0;
  261. return i;
  262. }
  263. void validate_mm(struct mm_struct *mm)
  264. {
  265. int bug = 0;
  266. int i = 0;
  267. struct vm_area_struct *tmp = mm->mmap;
  268. while (tmp) {
  269. tmp = tmp->vm_next;
  270. i++;
  271. }
  272. if (i != mm->map_count)
  273. printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
  274. i = browse_rb(&mm->mm_rb);
  275. if (i != mm->map_count)
  276. printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
  277. BUG_ON(bug);
  278. }
  279. #else
  280. #define validate_mm(mm) do { } while (0)
  281. #endif
  282. static struct vm_area_struct *
  283. find_vma_prepare(struct mm_struct *mm, unsigned long addr,
  284. struct vm_area_struct **pprev, struct rb_node ***rb_link,
  285. struct rb_node ** rb_parent)
  286. {
  287. struct vm_area_struct * vma;
  288. struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
  289. __rb_link = &mm->mm_rb.rb_node;
  290. rb_prev = __rb_parent = NULL;
  291. vma = NULL;
  292. while (*__rb_link) {
  293. struct vm_area_struct *vma_tmp;
  294. __rb_parent = *__rb_link;
  295. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  296. if (vma_tmp->vm_end > addr) {
  297. vma = vma_tmp;
  298. if (vma_tmp->vm_start <= addr)
  299. return vma;
  300. __rb_link = &__rb_parent->rb_left;
  301. } else {
  302. rb_prev = __rb_parent;
  303. __rb_link = &__rb_parent->rb_right;
  304. }
  305. }
  306. *pprev = NULL;
  307. if (rb_prev)
  308. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  309. *rb_link = __rb_link;
  310. *rb_parent = __rb_parent;
  311. return vma;
  312. }
  313. static inline void
  314. __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  315. struct vm_area_struct *prev, struct rb_node *rb_parent)
  316. {
  317. if (prev) {
  318. vma->vm_next = prev->vm_next;
  319. prev->vm_next = vma;
  320. } else {
  321. mm->mmap = vma;
  322. if (rb_parent)
  323. vma->vm_next = rb_entry(rb_parent,
  324. struct vm_area_struct, vm_rb);
  325. else
  326. vma->vm_next = NULL;
  327. }
  328. }
  329. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  330. struct rb_node **rb_link, struct rb_node *rb_parent)
  331. {
  332. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  333. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  334. }
  335. static inline void __vma_link_file(struct vm_area_struct *vma)
  336. {
  337. struct file * file;
  338. file = vma->vm_file;
  339. if (file) {
  340. struct address_space *mapping = file->f_mapping;
  341. if (vma->vm_flags & VM_DENYWRITE)
  342. atomic_dec(&file->f_dentry->d_inode->i_writecount);
  343. if (vma->vm_flags & VM_SHARED)
  344. mapping->i_mmap_writable++;
  345. flush_dcache_mmap_lock(mapping);
  346. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  347. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  348. else
  349. vma_prio_tree_insert(vma, &mapping->i_mmap);
  350. flush_dcache_mmap_unlock(mapping);
  351. }
  352. }
  353. static void
  354. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  355. struct vm_area_struct *prev, struct rb_node **rb_link,
  356. struct rb_node *rb_parent)
  357. {
  358. __vma_link_list(mm, vma, prev, rb_parent);
  359. __vma_link_rb(mm, vma, rb_link, rb_parent);
  360. __anon_vma_link(vma);
  361. }
  362. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  363. struct vm_area_struct *prev, struct rb_node **rb_link,
  364. struct rb_node *rb_parent)
  365. {
  366. struct address_space *mapping = NULL;
  367. if (vma->vm_file)
  368. mapping = vma->vm_file->f_mapping;
  369. if (mapping) {
  370. spin_lock(&mapping->i_mmap_lock);
  371. vma->vm_truncate_count = mapping->truncate_count;
  372. }
  373. anon_vma_lock(vma);
  374. __vma_link(mm, vma, prev, rb_link, rb_parent);
  375. __vma_link_file(vma);
  376. anon_vma_unlock(vma);
  377. if (mapping)
  378. spin_unlock(&mapping->i_mmap_lock);
  379. mm->map_count++;
  380. validate_mm(mm);
  381. }
  382. /*
  383. * Helper for vma_adjust in the split_vma insert case:
  384. * insert vm structure into list and rbtree and anon_vma,
  385. * but it has already been inserted into prio_tree earlier.
  386. */
  387. static void
  388. __insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  389. {
  390. struct vm_area_struct * __vma, * prev;
  391. struct rb_node ** rb_link, * rb_parent;
  392. __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
  393. BUG_ON(__vma && __vma->vm_start < vma->vm_end);
  394. __vma_link(mm, vma, prev, rb_link, rb_parent);
  395. mm->map_count++;
  396. }
  397. static inline void
  398. __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
  399. struct vm_area_struct *prev)
  400. {
  401. prev->vm_next = vma->vm_next;
  402. rb_erase(&vma->vm_rb, &mm->mm_rb);
  403. if (mm->mmap_cache == vma)
  404. mm->mmap_cache = prev;
  405. }
  406. /*
  407. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  408. * is already present in an i_mmap tree without adjusting the tree.
  409. * The following helper function should be used when such adjustments
  410. * are necessary. The "insert" vma (if any) is to be inserted
  411. * before we drop the necessary locks.
  412. */
  413. void vma_adjust(struct vm_area_struct *vma, unsigned long start,
  414. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  415. {
  416. struct mm_struct *mm = vma->vm_mm;
  417. struct vm_area_struct *next = vma->vm_next;
  418. struct vm_area_struct *importer = NULL;
  419. struct address_space *mapping = NULL;
  420. struct prio_tree_root *root = NULL;
  421. struct file *file = vma->vm_file;
  422. struct anon_vma *anon_vma = NULL;
  423. long adjust_next = 0;
  424. int remove_next = 0;
  425. if (next && !insert) {
  426. if (end >= next->vm_end) {
  427. /*
  428. * vma expands, overlapping all the next, and
  429. * perhaps the one after too (mprotect case 6).
  430. */
  431. again: remove_next = 1 + (end > next->vm_end);
  432. end = next->vm_end;
  433. anon_vma = next->anon_vma;
  434. importer = vma;
  435. } else if (end > next->vm_start) {
  436. /*
  437. * vma expands, overlapping part of the next:
  438. * mprotect case 5 shifting the boundary up.
  439. */
  440. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  441. anon_vma = next->anon_vma;
  442. importer = vma;
  443. } else if (end < vma->vm_end) {
  444. /*
  445. * vma shrinks, and !insert tells it's not
  446. * split_vma inserting another: so it must be
  447. * mprotect case 4 shifting the boundary down.
  448. */
  449. adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
  450. anon_vma = next->anon_vma;
  451. importer = next;
  452. }
  453. }
  454. if (file) {
  455. mapping = file->f_mapping;
  456. if (!(vma->vm_flags & VM_NONLINEAR))
  457. root = &mapping->i_mmap;
  458. spin_lock(&mapping->i_mmap_lock);
  459. if (importer &&
  460. vma->vm_truncate_count != next->vm_truncate_count) {
  461. /*
  462. * unmap_mapping_range might be in progress:
  463. * ensure that the expanding vma is rescanned.
  464. */
  465. importer->vm_truncate_count = 0;
  466. }
  467. if (insert) {
  468. insert->vm_truncate_count = vma->vm_truncate_count;
  469. /*
  470. * Put into prio_tree now, so instantiated pages
  471. * are visible to arm/parisc __flush_dcache_page
  472. * throughout; but we cannot insert into address
  473. * space until vma start or end is updated.
  474. */
  475. __vma_link_file(insert);
  476. }
  477. }
  478. /*
  479. * When changing only vma->vm_end, we don't really need
  480. * anon_vma lock: but is that case worth optimizing out?
  481. */
  482. if (vma->anon_vma)
  483. anon_vma = vma->anon_vma;
  484. if (anon_vma) {
  485. spin_lock(&anon_vma->lock);
  486. /*
  487. * Easily overlooked: when mprotect shifts the boundary,
  488. * make sure the expanding vma has anon_vma set if the
  489. * shrinking vma had, to cover any anon pages imported.
  490. */
  491. if (importer && !importer->anon_vma) {
  492. importer->anon_vma = anon_vma;
  493. __anon_vma_link(importer);
  494. }
  495. }
  496. if (root) {
  497. flush_dcache_mmap_lock(mapping);
  498. vma_prio_tree_remove(vma, root);
  499. if (adjust_next)
  500. vma_prio_tree_remove(next, root);
  501. }
  502. vma->vm_start = start;
  503. vma->vm_end = end;
  504. vma->vm_pgoff = pgoff;
  505. if (adjust_next) {
  506. next->vm_start += adjust_next << PAGE_SHIFT;
  507. next->vm_pgoff += adjust_next;
  508. }
  509. if (root) {
  510. if (adjust_next)
  511. vma_prio_tree_insert(next, root);
  512. vma_prio_tree_insert(vma, root);
  513. flush_dcache_mmap_unlock(mapping);
  514. }
  515. if (remove_next) {
  516. /*
  517. * vma_merge has merged next into vma, and needs
  518. * us to remove next before dropping the locks.
  519. */
  520. __vma_unlink(mm, next, vma);
  521. if (file)
  522. __remove_shared_vm_struct(next, file, mapping);
  523. if (next->anon_vma)
  524. __anon_vma_merge(vma, next);
  525. } else if (insert) {
  526. /*
  527. * split_vma has split insert from vma, and needs
  528. * us to insert it before dropping the locks
  529. * (it may either follow vma or precede it).
  530. */
  531. __insert_vm_struct(mm, insert);
  532. }
  533. if (anon_vma)
  534. spin_unlock(&anon_vma->lock);
  535. if (mapping)
  536. spin_unlock(&mapping->i_mmap_lock);
  537. if (remove_next) {
  538. if (file)
  539. fput(file);
  540. mm->map_count--;
  541. mpol_free(vma_policy(next));
  542. kmem_cache_free(vm_area_cachep, next);
  543. /*
  544. * In mprotect's case 6 (see comments on vma_merge),
  545. * we must remove another next too. It would clutter
  546. * up the code too much to do both in one go.
  547. */
  548. if (remove_next == 2) {
  549. next = vma->vm_next;
  550. goto again;
  551. }
  552. }
  553. validate_mm(mm);
  554. }
  555. /*
  556. * If the vma has a ->close operation then the driver probably needs to release
  557. * per-vma resources, so we don't attempt to merge those.
  558. */
  559. #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_RESERVED | VM_PFNMAP)
  560. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  561. struct file *file, unsigned long vm_flags)
  562. {
  563. if (vma->vm_flags != vm_flags)
  564. return 0;
  565. if (vma->vm_file != file)
  566. return 0;
  567. if (vma->vm_ops && vma->vm_ops->close)
  568. return 0;
  569. return 1;
  570. }
  571. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  572. struct anon_vma *anon_vma2)
  573. {
  574. return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
  575. }
  576. /*
  577. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  578. * in front of (at a lower virtual address and file offset than) the vma.
  579. *
  580. * We cannot merge two vmas if they have differently assigned (non-NULL)
  581. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  582. *
  583. * We don't check here for the merged mmap wrapping around the end of pagecache
  584. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  585. * wrap, nor mmaps which cover the final page at index -1UL.
  586. */
  587. static int
  588. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  589. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  590. {
  591. if (is_mergeable_vma(vma, file, vm_flags) &&
  592. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  593. if (vma->vm_pgoff == vm_pgoff)
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. /*
  599. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  600. * beyond (at a higher virtual address and file offset than) the vma.
  601. *
  602. * We cannot merge two vmas if they have differently assigned (non-NULL)
  603. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  604. */
  605. static int
  606. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  607. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  608. {
  609. if (is_mergeable_vma(vma, file, vm_flags) &&
  610. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  611. pgoff_t vm_pglen;
  612. vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  613. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  614. return 1;
  615. }
  616. return 0;
  617. }
  618. /*
  619. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  620. * whether that can be merged with its predecessor or its successor.
  621. * Or both (it neatly fills a hole).
  622. *
  623. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  624. * certain not to be mapped by the time vma_merge is called; but when
  625. * called for mprotect, it is certain to be already mapped (either at
  626. * an offset within prev, or at the start of next), and the flags of
  627. * this area are about to be changed to vm_flags - and the no-change
  628. * case has already been eliminated.
  629. *
  630. * The following mprotect cases have to be considered, where AAAA is
  631. * the area passed down from mprotect_fixup, never extending beyond one
  632. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  633. *
  634. * AAAA AAAA AAAA AAAA
  635. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  636. * cannot merge might become might become might become
  637. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  638. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  639. * mremap move: PPPPNNNNNNNN 8
  640. * AAAA
  641. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  642. * might become case 1 below case 2 below case 3 below
  643. *
  644. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  645. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  646. */
  647. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  648. struct vm_area_struct *prev, unsigned long addr,
  649. unsigned long end, unsigned long vm_flags,
  650. struct anon_vma *anon_vma, struct file *file,
  651. pgoff_t pgoff, struct mempolicy *policy)
  652. {
  653. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  654. struct vm_area_struct *area, *next;
  655. /*
  656. * We later require that vma->vm_flags == vm_flags,
  657. * so this tests vma->vm_flags & VM_SPECIAL, too.
  658. */
  659. if (vm_flags & VM_SPECIAL)
  660. return NULL;
  661. if (prev)
  662. next = prev->vm_next;
  663. else
  664. next = mm->mmap;
  665. area = next;
  666. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  667. next = next->vm_next;
  668. /*
  669. * Can it merge with the predecessor?
  670. */
  671. if (prev && prev->vm_end == addr &&
  672. mpol_equal(vma_policy(prev), policy) &&
  673. can_vma_merge_after(prev, vm_flags,
  674. anon_vma, file, pgoff)) {
  675. /*
  676. * OK, it can. Can we now merge in the successor as well?
  677. */
  678. if (next && end == next->vm_start &&
  679. mpol_equal(policy, vma_policy(next)) &&
  680. can_vma_merge_before(next, vm_flags,
  681. anon_vma, file, pgoff+pglen) &&
  682. is_mergeable_anon_vma(prev->anon_vma,
  683. next->anon_vma)) {
  684. /* cases 1, 6 */
  685. vma_adjust(prev, prev->vm_start,
  686. next->vm_end, prev->vm_pgoff, NULL);
  687. } else /* cases 2, 5, 7 */
  688. vma_adjust(prev, prev->vm_start,
  689. end, prev->vm_pgoff, NULL);
  690. return prev;
  691. }
  692. /*
  693. * Can this new request be merged in front of next?
  694. */
  695. if (next && end == next->vm_start &&
  696. mpol_equal(policy, vma_policy(next)) &&
  697. can_vma_merge_before(next, vm_flags,
  698. anon_vma, file, pgoff+pglen)) {
  699. if (prev && addr < prev->vm_end) /* case 4 */
  700. vma_adjust(prev, prev->vm_start,
  701. addr, prev->vm_pgoff, NULL);
  702. else /* cases 3, 8 */
  703. vma_adjust(area, addr, next->vm_end,
  704. next->vm_pgoff - pglen, NULL);
  705. return area;
  706. }
  707. return NULL;
  708. }
  709. /*
  710. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  711. * neighbouring vmas for a suitable anon_vma, before it goes off
  712. * to allocate a new anon_vma. It checks because a repetitive
  713. * sequence of mprotects and faults may otherwise lead to distinct
  714. * anon_vmas being allocated, preventing vma merge in subsequent
  715. * mprotect.
  716. */
  717. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  718. {
  719. struct vm_area_struct *near;
  720. unsigned long vm_flags;
  721. near = vma->vm_next;
  722. if (!near)
  723. goto try_prev;
  724. /*
  725. * Since only mprotect tries to remerge vmas, match flags
  726. * which might be mprotected into each other later on.
  727. * Neither mlock nor madvise tries to remerge at present,
  728. * so leave their flags as obstructing a merge.
  729. */
  730. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  731. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  732. if (near->anon_vma && vma->vm_end == near->vm_start &&
  733. mpol_equal(vma_policy(vma), vma_policy(near)) &&
  734. can_vma_merge_before(near, vm_flags,
  735. NULL, vma->vm_file, vma->vm_pgoff +
  736. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
  737. return near->anon_vma;
  738. try_prev:
  739. /*
  740. * It is potentially slow to have to call find_vma_prev here.
  741. * But it's only on the first write fault on the vma, not
  742. * every time, and we could devise a way to avoid it later
  743. * (e.g. stash info in next's anon_vma_node when assigning
  744. * an anon_vma, or when trying vma_merge). Another time.
  745. */
  746. BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
  747. if (!near)
  748. goto none;
  749. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  750. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  751. if (near->anon_vma && near->vm_end == vma->vm_start &&
  752. mpol_equal(vma_policy(near), vma_policy(vma)) &&
  753. can_vma_merge_after(near, vm_flags,
  754. NULL, vma->vm_file, vma->vm_pgoff))
  755. return near->anon_vma;
  756. none:
  757. /*
  758. * There's no absolute need to look only at touching neighbours:
  759. * we could search further afield for "compatible" anon_vmas.
  760. * But it would probably just be a waste of time searching,
  761. * or lead to too many vmas hanging off the same anon_vma.
  762. * We're trying to allow mprotect remerging later on,
  763. * not trying to minimize memory used for anon_vmas.
  764. */
  765. return NULL;
  766. }
  767. #ifdef CONFIG_PROC_FS
  768. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  769. struct file *file, long pages)
  770. {
  771. const unsigned long stack_flags
  772. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  773. if (file) {
  774. mm->shared_vm += pages;
  775. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  776. mm->exec_vm += pages;
  777. } else if (flags & stack_flags)
  778. mm->stack_vm += pages;
  779. if (flags & (VM_RESERVED|VM_IO))
  780. mm->reserved_vm += pages;
  781. }
  782. #endif /* CONFIG_PROC_FS */
  783. /*
  784. * The caller must hold down_write(current->mm->mmap_sem).
  785. */
  786. unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
  787. unsigned long len, unsigned long prot,
  788. unsigned long flags, unsigned long pgoff)
  789. {
  790. struct mm_struct * mm = current->mm;
  791. struct vm_area_struct * vma, * prev;
  792. struct inode *inode;
  793. unsigned int vm_flags;
  794. int correct_wcount = 0;
  795. int error;
  796. struct rb_node ** rb_link, * rb_parent;
  797. int accountable = 1;
  798. unsigned long charged = 0, reqprot = prot;
  799. if (file) {
  800. if (is_file_hugepages(file))
  801. accountable = 0;
  802. if (!file->f_op || !file->f_op->mmap)
  803. return -ENODEV;
  804. if ((prot & PROT_EXEC) &&
  805. (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
  806. return -EPERM;
  807. }
  808. /*
  809. * Does the application expect PROT_READ to imply PROT_EXEC?
  810. *
  811. * (the exception is when the underlying filesystem is noexec
  812. * mounted, in which case we dont add PROT_EXEC.)
  813. */
  814. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  815. if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
  816. prot |= PROT_EXEC;
  817. if (!len)
  818. return -EINVAL;
  819. error = arch_mmap_check(addr, len, flags);
  820. if (error)
  821. return error;
  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. int error;
  1648. len = PAGE_ALIGN(len);
  1649. if (!len)
  1650. return addr;
  1651. if ((addr + len) > TASK_SIZE || (addr + len) < addr)
  1652. return -EINVAL;
  1653. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  1654. error = arch_mmap_check(addr, len, flags);
  1655. if (error)
  1656. return error;
  1657. /*
  1658. * mlock MCL_FUTURE?
  1659. */
  1660. if (mm->def_flags & VM_LOCKED) {
  1661. unsigned long locked, lock_limit;
  1662. locked = len >> PAGE_SHIFT;
  1663. locked += mm->locked_vm;
  1664. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1665. lock_limit >>= PAGE_SHIFT;
  1666. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1667. return -EAGAIN;
  1668. }
  1669. /*
  1670. * mm->mmap_sem is required to protect against another thread
  1671. * changing the mappings in case we sleep.
  1672. */
  1673. verify_mm_writelocked(mm);
  1674. /*
  1675. * Clear old maps. this also does some error checking for us
  1676. */
  1677. munmap_back:
  1678. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1679. if (vma && vma->vm_start < addr + len) {
  1680. if (do_munmap(mm, addr, len))
  1681. return -ENOMEM;
  1682. goto munmap_back;
  1683. }
  1684. /* Check against address space limits *after* clearing old maps... */
  1685. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1686. return -ENOMEM;
  1687. if (mm->map_count > sysctl_max_map_count)
  1688. return -ENOMEM;
  1689. if (security_vm_enough_memory(len >> PAGE_SHIFT))
  1690. return -ENOMEM;
  1691. /* Can we just expand an old private anonymous mapping? */
  1692. if (vma_merge(mm, prev, addr, addr + len, flags,
  1693. NULL, NULL, pgoff, NULL))
  1694. goto out;
  1695. /*
  1696. * create a vma struct for an anonymous mapping
  1697. */
  1698. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1699. if (!vma) {
  1700. vm_unacct_memory(len >> PAGE_SHIFT);
  1701. return -ENOMEM;
  1702. }
  1703. vma->vm_mm = mm;
  1704. vma->vm_start = addr;
  1705. vma->vm_end = addr + len;
  1706. vma->vm_pgoff = pgoff;
  1707. vma->vm_flags = flags;
  1708. vma->vm_page_prot = protection_map[flags &
  1709. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)];
  1710. vma_link(mm, vma, prev, rb_link, rb_parent);
  1711. out:
  1712. mm->total_vm += len >> PAGE_SHIFT;
  1713. if (flags & VM_LOCKED) {
  1714. mm->locked_vm += len >> PAGE_SHIFT;
  1715. make_pages_present(addr, addr + len);
  1716. }
  1717. return addr;
  1718. }
  1719. EXPORT_SYMBOL(do_brk);
  1720. /* Release all mmaps. */
  1721. void exit_mmap(struct mm_struct *mm)
  1722. {
  1723. struct mmu_gather *tlb;
  1724. struct vm_area_struct *vma = mm->mmap;
  1725. unsigned long nr_accounted = 0;
  1726. unsigned long end;
  1727. lru_add_drain();
  1728. flush_cache_mm(mm);
  1729. tlb = tlb_gather_mmu(mm, 1);
  1730. /* Don't update_hiwater_rss(mm) here, do_exit already did */
  1731. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  1732. end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
  1733. vm_unacct_memory(nr_accounted);
  1734. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
  1735. tlb_finish_mmu(tlb, 0, end);
  1736. /*
  1737. * Walk the list again, actually closing and freeing it,
  1738. * with preemption enabled, without holding any MM locks.
  1739. */
  1740. while (vma)
  1741. vma = remove_vma(vma);
  1742. BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  1743. }
  1744. /* Insert vm structure into process list sorted by address
  1745. * and into the inode's i_mmap tree. If vm_file is non-NULL
  1746. * then i_mmap_lock is taken here.
  1747. */
  1748. int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  1749. {
  1750. struct vm_area_struct * __vma, * prev;
  1751. struct rb_node ** rb_link, * rb_parent;
  1752. /*
  1753. * The vm_pgoff of a purely anonymous vma should be irrelevant
  1754. * until its first write fault, when page's anon_vma and index
  1755. * are set. But now set the vm_pgoff it will almost certainly
  1756. * end up with (unless mremap moves it elsewhere before that
  1757. * first wfault), so /proc/pid/maps tells a consistent story.
  1758. *
  1759. * By setting it to reflect the virtual start address of the
  1760. * vma, merges and splits can happen in a seamless way, just
  1761. * using the existing file pgoff checks and manipulations.
  1762. * Similarly in do_mmap_pgoff and in do_brk.
  1763. */
  1764. if (!vma->vm_file) {
  1765. BUG_ON(vma->anon_vma);
  1766. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1767. }
  1768. __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
  1769. if (__vma && __vma->vm_start < vma->vm_end)
  1770. return -ENOMEM;
  1771. if ((vma->vm_flags & VM_ACCOUNT) &&
  1772. security_vm_enough_memory(vma_pages(vma)))
  1773. return -ENOMEM;
  1774. vma_link(mm, vma, prev, rb_link, rb_parent);
  1775. return 0;
  1776. }
  1777. /*
  1778. * Copy the vma structure to a new location in the same mm,
  1779. * prior to moving page table entries, to effect an mremap move.
  1780. */
  1781. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  1782. unsigned long addr, unsigned long len, pgoff_t pgoff)
  1783. {
  1784. struct vm_area_struct *vma = *vmap;
  1785. unsigned long vma_start = vma->vm_start;
  1786. struct mm_struct *mm = vma->vm_mm;
  1787. struct vm_area_struct *new_vma, *prev;
  1788. struct rb_node **rb_link, *rb_parent;
  1789. struct mempolicy *pol;
  1790. /*
  1791. * If anonymous vma has not yet been faulted, update new pgoff
  1792. * to match new location, to increase its chance of merging.
  1793. */
  1794. if (!vma->vm_file && !vma->anon_vma)
  1795. pgoff = addr >> PAGE_SHIFT;
  1796. find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1797. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  1798. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  1799. if (new_vma) {
  1800. /*
  1801. * Source vma may have been merged into new_vma
  1802. */
  1803. if (vma_start >= new_vma->vm_start &&
  1804. vma_start < new_vma->vm_end)
  1805. *vmap = new_vma;
  1806. } else {
  1807. new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  1808. if (new_vma) {
  1809. *new_vma = *vma;
  1810. pol = mpol_copy(vma_policy(vma));
  1811. if (IS_ERR(pol)) {
  1812. kmem_cache_free(vm_area_cachep, new_vma);
  1813. return NULL;
  1814. }
  1815. vma_set_policy(new_vma, pol);
  1816. new_vma->vm_start = addr;
  1817. new_vma->vm_end = addr + len;
  1818. new_vma->vm_pgoff = pgoff;
  1819. if (new_vma->vm_file)
  1820. get_file(new_vma->vm_file);
  1821. if (new_vma->vm_ops && new_vma->vm_ops->open)
  1822. new_vma->vm_ops->open(new_vma);
  1823. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  1824. }
  1825. }
  1826. return new_vma;
  1827. }
  1828. /*
  1829. * Return true if the calling process may expand its vm space by the passed
  1830. * number of pages
  1831. */
  1832. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  1833. {
  1834. unsigned long cur = mm->total_vm; /* pages */
  1835. unsigned long lim;
  1836. lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  1837. if (cur + npages > lim)
  1838. return 0;
  1839. return 1;
  1840. }