mmap.c 58 KB

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