mmap.c 58 KB

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