mmap.c 59 KB

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