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