mmap.c 54 KB

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