mmap.c 59 KB

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