mmap.c 53 KB

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