mmap.c 54 KB

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