mmap.c 54 KB

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