mmap.c 53 KB

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