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