mmap.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  1. /*
  2. * mm/mmap.c
  3. *
  4. * Written by obz.
  5. *
  6. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  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 <linux/mmu_notifier.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/tlb.h>
  32. #include <asm/mmu_context.h>
  33. #include "internal.h"
  34. #ifndef arch_mmap_check
  35. #define arch_mmap_check(addr, len, flags) (0)
  36. #endif
  37. #ifndef arch_rebalance_pgtables
  38. #define arch_rebalance_pgtables(addr, len) (addr)
  39. #endif
  40. static void unmap_region(struct mm_struct *mm,
  41. struct vm_area_struct *vma, struct vm_area_struct *prev,
  42. unsigned long start, unsigned long end);
  43. /*
  44. * WARNING: the debugging will use recursive algorithms so never enable this
  45. * unless you know what you are doing.
  46. */
  47. #undef DEBUG_MM_RB
  48. /* description of effects of mapping type and prot in current implementation.
  49. * this is due to the limited x86 page protection hardware. The expected
  50. * behavior is in parens:
  51. *
  52. * map_type prot
  53. * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
  54. * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  55. * w: (no) no w: (no) no w: (yes) yes w: (no) no
  56. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  57. *
  58. * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  59. * w: (no) no w: (no) no w: (copy) copy w: (no) no
  60. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  61. *
  62. */
  63. pgprot_t protection_map[16] = {
  64. __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
  65. __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
  66. };
  67. pgprot_t vm_get_page_prot(unsigned long vm_flags)
  68. {
  69. return __pgprot(pgprot_val(protection_map[vm_flags &
  70. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
  71. pgprot_val(arch_vm_get_page_prot(vm_flags)));
  72. }
  73. EXPORT_SYMBOL(vm_get_page_prot);
  74. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  75. int sysctl_overcommit_ratio = 50; /* default is 50% */
  76. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  77. atomic_long_t vm_committed_space = ATOMIC_LONG_INIT(0);
  78. /*
  79. * Check that a process has enough memory to allocate a new virtual
  80. * mapping. 0 means there is enough memory for the allocation to
  81. * succeed and -ENOMEM implies there is not.
  82. *
  83. * We currently support three overcommit policies, which are set via the
  84. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  85. *
  86. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  87. * Additional code 2002 Jul 20 by Robert Love.
  88. *
  89. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  90. *
  91. * Note this is a helper function intended to be used by LSMs which
  92. * wish to use this logic.
  93. */
  94. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  95. {
  96. unsigned long free, allowed;
  97. vm_acct_memory(pages);
  98. /*
  99. * Sometimes we want to use more memory than we have
  100. */
  101. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  102. return 0;
  103. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  104. unsigned long n;
  105. free = global_page_state(NR_FILE_PAGES);
  106. free += nr_swap_pages;
  107. /*
  108. * Any slabs which are created with the
  109. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  110. * which are reclaimable, under pressure. The dentry
  111. * cache and most inode caches should fall into this
  112. */
  113. free += global_page_state(NR_SLAB_RECLAIMABLE);
  114. /*
  115. * Leave the last 3% for root
  116. */
  117. if (!cap_sys_admin)
  118. free -= free / 32;
  119. if (free > pages)
  120. return 0;
  121. /*
  122. * nr_free_pages() is very expensive on large systems,
  123. * only call if we're about to fail.
  124. */
  125. n = nr_free_pages();
  126. /*
  127. * Leave reserved pages. The pages are not for anonymous pages.
  128. */
  129. if (n <= totalreserve_pages)
  130. goto error;
  131. else
  132. n -= totalreserve_pages;
  133. /*
  134. * Leave the last 3% for root
  135. */
  136. if (!cap_sys_admin)
  137. n -= n / 32;
  138. free += n;
  139. if (free > pages)
  140. return 0;
  141. goto error;
  142. }
  143. allowed = (totalram_pages - hugetlb_total_pages())
  144. * sysctl_overcommit_ratio / 100;
  145. /*
  146. * Leave the last 3% for root
  147. */
  148. if (!cap_sys_admin)
  149. allowed -= allowed / 32;
  150. allowed += total_swap_pages;
  151. /* Don't let a single process grow too big:
  152. leave 3% of the size of this process for other processes */
  153. if (mm)
  154. allowed -= mm->total_vm / 32;
  155. /*
  156. * cast `allowed' as a signed long because vm_committed_space
  157. * sometimes has a negative value
  158. */
  159. if (atomic_long_read(&vm_committed_space) < (long)allowed)
  160. return 0;
  161. error:
  162. vm_unacct_memory(pages);
  163. return -ENOMEM;
  164. }
  165. /*
  166. * Requires inode->i_mapping->i_mmap_lock
  167. */
  168. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  169. struct file *file, struct address_space *mapping)
  170. {
  171. if (vma->vm_flags & VM_DENYWRITE)
  172. atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
  173. if (vma->vm_flags & VM_SHARED)
  174. mapping->i_mmap_writable--;
  175. flush_dcache_mmap_lock(mapping);
  176. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  177. list_del_init(&vma->shared.vm_set.list);
  178. else
  179. vma_prio_tree_remove(vma, &mapping->i_mmap);
  180. flush_dcache_mmap_unlock(mapping);
  181. }
  182. /*
  183. * Unlink a file-based vm structure from its prio_tree, to hide
  184. * vma from rmap and vmtruncate before freeing its page tables.
  185. */
  186. void unlink_file_vma(struct vm_area_struct *vma)
  187. {
  188. struct file *file = vma->vm_file;
  189. if (file) {
  190. struct address_space *mapping = file->f_mapping;
  191. spin_lock(&mapping->i_mmap_lock);
  192. __remove_shared_vm_struct(vma, file, mapping);
  193. spin_unlock(&mapping->i_mmap_lock);
  194. }
  195. }
  196. /*
  197. * Close a vm structure and free it, returning the next.
  198. */
  199. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  200. {
  201. struct vm_area_struct *next = vma->vm_next;
  202. might_sleep();
  203. if (vma->vm_ops && vma->vm_ops->close)
  204. vma->vm_ops->close(vma);
  205. if (vma->vm_file) {
  206. fput(vma->vm_file);
  207. if (vma->vm_flags & VM_EXECUTABLE)
  208. removed_exe_file_vma(vma->vm_mm);
  209. }
  210. mpol_put(vma_policy(vma));
  211. kmem_cache_free(vm_area_cachep, vma);
  212. return next;
  213. }
  214. SYSCALL_DEFINE1(brk, unsigned long, brk)
  215. {
  216. unsigned long rlim, retval;
  217. unsigned long newbrk, oldbrk;
  218. struct mm_struct *mm = current->mm;
  219. unsigned long min_brk;
  220. down_write(&mm->mmap_sem);
  221. #ifdef CONFIG_COMPAT_BRK
  222. min_brk = mm->end_code;
  223. #else
  224. min_brk = mm->start_brk;
  225. #endif
  226. if (brk < min_brk)
  227. goto out;
  228. /*
  229. * Check against rlimit here. If this check is done later after the test
  230. * of oldbrk with newbrk then it can escape the test and let the data
  231. * segment grow beyond its set limit the in case where the limit is
  232. * not page aligned -Ram Gupta
  233. */
  234. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  235. if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
  236. (mm->end_data - mm->start_data) > rlim)
  237. goto out;
  238. newbrk = PAGE_ALIGN(brk);
  239. oldbrk = PAGE_ALIGN(mm->brk);
  240. if (oldbrk == newbrk)
  241. goto set_brk;
  242. /* Always allow shrinking brk. */
  243. if (brk <= mm->brk) {
  244. if (!do_munmap(mm, newbrk, oldbrk-newbrk))
  245. goto set_brk;
  246. goto out;
  247. }
  248. /* Check against existing mmap mappings. */
  249. if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
  250. goto out;
  251. /* Ok, looks good - let it rip. */
  252. if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
  253. goto out;
  254. set_brk:
  255. mm->brk = brk;
  256. out:
  257. retval = mm->brk;
  258. up_write(&mm->mmap_sem);
  259. return retval;
  260. }
  261. #ifdef DEBUG_MM_RB
  262. static int browse_rb(struct rb_root *root)
  263. {
  264. int i = 0, j;
  265. struct rb_node *nd, *pn = NULL;
  266. unsigned long prev = 0, pend = 0;
  267. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  268. struct vm_area_struct *vma;
  269. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  270. if (vma->vm_start < prev)
  271. printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
  272. if (vma->vm_start < pend)
  273. printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
  274. if (vma->vm_start > vma->vm_end)
  275. printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
  276. i++;
  277. pn = nd;
  278. prev = vma->vm_start;
  279. pend = vma->vm_end;
  280. }
  281. j = 0;
  282. for (nd = pn; nd; nd = rb_prev(nd)) {
  283. j++;
  284. }
  285. if (i != j)
  286. printk("backwards %d, forwards %d\n", j, i), i = 0;
  287. return i;
  288. }
  289. void validate_mm(struct mm_struct *mm)
  290. {
  291. int bug = 0;
  292. int i = 0;
  293. struct vm_area_struct *tmp = mm->mmap;
  294. while (tmp) {
  295. tmp = tmp->vm_next;
  296. i++;
  297. }
  298. if (i != mm->map_count)
  299. printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
  300. i = browse_rb(&mm->mm_rb);
  301. if (i != mm->map_count)
  302. printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
  303. BUG_ON(bug);
  304. }
  305. #else
  306. #define validate_mm(mm) do { } while (0)
  307. #endif
  308. static struct vm_area_struct *
  309. find_vma_prepare(struct mm_struct *mm, unsigned long addr,
  310. struct vm_area_struct **pprev, struct rb_node ***rb_link,
  311. struct rb_node ** rb_parent)
  312. {
  313. struct vm_area_struct * vma;
  314. struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
  315. __rb_link = &mm->mm_rb.rb_node;
  316. rb_prev = __rb_parent = NULL;
  317. vma = NULL;
  318. while (*__rb_link) {
  319. struct vm_area_struct *vma_tmp;
  320. __rb_parent = *__rb_link;
  321. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  322. if (vma_tmp->vm_end > addr) {
  323. vma = vma_tmp;
  324. if (vma_tmp->vm_start <= addr)
  325. break;
  326. __rb_link = &__rb_parent->rb_left;
  327. } else {
  328. rb_prev = __rb_parent;
  329. __rb_link = &__rb_parent->rb_right;
  330. }
  331. }
  332. *pprev = NULL;
  333. if (rb_prev)
  334. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  335. *rb_link = __rb_link;
  336. *rb_parent = __rb_parent;
  337. return vma;
  338. }
  339. static inline void
  340. __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  341. struct vm_area_struct *prev, struct rb_node *rb_parent)
  342. {
  343. if (prev) {
  344. vma->vm_next = prev->vm_next;
  345. prev->vm_next = vma;
  346. } else {
  347. mm->mmap = vma;
  348. if (rb_parent)
  349. vma->vm_next = rb_entry(rb_parent,
  350. struct vm_area_struct, vm_rb);
  351. else
  352. vma->vm_next = NULL;
  353. }
  354. }
  355. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  356. struct rb_node **rb_link, struct rb_node *rb_parent)
  357. {
  358. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  359. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  360. }
  361. static void __vma_link_file(struct vm_area_struct *vma)
  362. {
  363. struct file *file;
  364. file = vma->vm_file;
  365. if (file) {
  366. struct address_space *mapping = file->f_mapping;
  367. if (vma->vm_flags & VM_DENYWRITE)
  368. atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
  369. if (vma->vm_flags & VM_SHARED)
  370. mapping->i_mmap_writable++;
  371. flush_dcache_mmap_lock(mapping);
  372. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  373. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  374. else
  375. vma_prio_tree_insert(vma, &mapping->i_mmap);
  376. flush_dcache_mmap_unlock(mapping);
  377. }
  378. }
  379. static void
  380. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  381. struct vm_area_struct *prev, struct rb_node **rb_link,
  382. struct rb_node *rb_parent)
  383. {
  384. __vma_link_list(mm, vma, prev, rb_parent);
  385. __vma_link_rb(mm, vma, rb_link, rb_parent);
  386. __anon_vma_link(vma);
  387. }
  388. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  389. struct vm_area_struct *prev, struct rb_node **rb_link,
  390. struct rb_node *rb_parent)
  391. {
  392. struct address_space *mapping = NULL;
  393. if (vma->vm_file)
  394. mapping = vma->vm_file->f_mapping;
  395. if (mapping) {
  396. spin_lock(&mapping->i_mmap_lock);
  397. vma->vm_truncate_count = mapping->truncate_count;
  398. }
  399. anon_vma_lock(vma);
  400. __vma_link(mm, vma, prev, rb_link, rb_parent);
  401. __vma_link_file(vma);
  402. anon_vma_unlock(vma);
  403. if (mapping)
  404. spin_unlock(&mapping->i_mmap_lock);
  405. mm->map_count++;
  406. validate_mm(mm);
  407. }
  408. /*
  409. * Helper for vma_adjust in the split_vma insert case:
  410. * insert vm structure into list and rbtree and anon_vma,
  411. * but it has already been inserted into prio_tree earlier.
  412. */
  413. static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  414. {
  415. struct vm_area_struct *__vma, *prev;
  416. struct rb_node **rb_link, *rb_parent;
  417. __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
  418. BUG_ON(__vma && __vma->vm_start < vma->vm_end);
  419. __vma_link(mm, vma, prev, rb_link, rb_parent);
  420. mm->map_count++;
  421. }
  422. static inline void
  423. __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
  424. struct vm_area_struct *prev)
  425. {
  426. prev->vm_next = vma->vm_next;
  427. rb_erase(&vma->vm_rb, &mm->mm_rb);
  428. if (mm->mmap_cache == vma)
  429. mm->mmap_cache = prev;
  430. }
  431. /*
  432. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  433. * is already present in an i_mmap tree without adjusting the tree.
  434. * The following helper function should be used when such adjustments
  435. * are necessary. The "insert" vma (if any) is to be inserted
  436. * before we drop the necessary locks.
  437. */
  438. void vma_adjust(struct vm_area_struct *vma, unsigned long start,
  439. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  440. {
  441. struct mm_struct *mm = vma->vm_mm;
  442. struct vm_area_struct *next = vma->vm_next;
  443. struct vm_area_struct *importer = NULL;
  444. struct address_space *mapping = NULL;
  445. struct prio_tree_root *root = NULL;
  446. struct file *file = vma->vm_file;
  447. struct anon_vma *anon_vma = NULL;
  448. long adjust_next = 0;
  449. int remove_next = 0;
  450. if (next && !insert) {
  451. if (end >= next->vm_end) {
  452. /*
  453. * vma expands, overlapping all the next, and
  454. * perhaps the one after too (mprotect case 6).
  455. */
  456. again: remove_next = 1 + (end > next->vm_end);
  457. end = next->vm_end;
  458. anon_vma = next->anon_vma;
  459. importer = vma;
  460. } else if (end > next->vm_start) {
  461. /*
  462. * vma expands, overlapping part of the next:
  463. * mprotect case 5 shifting the boundary up.
  464. */
  465. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  466. anon_vma = next->anon_vma;
  467. importer = vma;
  468. } else if (end < vma->vm_end) {
  469. /*
  470. * vma shrinks, and !insert tells it's not
  471. * split_vma inserting another: so it must be
  472. * mprotect case 4 shifting the boundary down.
  473. */
  474. adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
  475. anon_vma = next->anon_vma;
  476. importer = next;
  477. }
  478. }
  479. if (file) {
  480. mapping = file->f_mapping;
  481. if (!(vma->vm_flags & VM_NONLINEAR))
  482. root = &mapping->i_mmap;
  483. spin_lock(&mapping->i_mmap_lock);
  484. if (importer &&
  485. vma->vm_truncate_count != next->vm_truncate_count) {
  486. /*
  487. * unmap_mapping_range might be in progress:
  488. * ensure that the expanding vma is rescanned.
  489. */
  490. importer->vm_truncate_count = 0;
  491. }
  492. if (insert) {
  493. insert->vm_truncate_count = vma->vm_truncate_count;
  494. /*
  495. * Put into prio_tree now, so instantiated pages
  496. * are visible to arm/parisc __flush_dcache_page
  497. * throughout; but we cannot insert into address
  498. * space until vma start or end is updated.
  499. */
  500. __vma_link_file(insert);
  501. }
  502. }
  503. /*
  504. * When changing only vma->vm_end, we don't really need
  505. * anon_vma lock: but is that case worth optimizing out?
  506. */
  507. if (vma->anon_vma)
  508. anon_vma = vma->anon_vma;
  509. if (anon_vma) {
  510. spin_lock(&anon_vma->lock);
  511. /*
  512. * Easily overlooked: when mprotect shifts the boundary,
  513. * make sure the expanding vma has anon_vma set if the
  514. * shrinking vma had, to cover any anon pages imported.
  515. */
  516. if (importer && !importer->anon_vma) {
  517. importer->anon_vma = anon_vma;
  518. __anon_vma_link(importer);
  519. }
  520. }
  521. if (root) {
  522. flush_dcache_mmap_lock(mapping);
  523. vma_prio_tree_remove(vma, root);
  524. if (adjust_next)
  525. vma_prio_tree_remove(next, root);
  526. }
  527. vma->vm_start = start;
  528. vma->vm_end = end;
  529. vma->vm_pgoff = pgoff;
  530. if (adjust_next) {
  531. next->vm_start += adjust_next << PAGE_SHIFT;
  532. next->vm_pgoff += adjust_next;
  533. }
  534. if (root) {
  535. if (adjust_next)
  536. vma_prio_tree_insert(next, root);
  537. vma_prio_tree_insert(vma, root);
  538. flush_dcache_mmap_unlock(mapping);
  539. }
  540. if (remove_next) {
  541. /*
  542. * vma_merge has merged next into vma, and needs
  543. * us to remove next before dropping the locks.
  544. */
  545. __vma_unlink(mm, next, vma);
  546. if (file)
  547. __remove_shared_vm_struct(next, file, mapping);
  548. if (next->anon_vma)
  549. __anon_vma_merge(vma, next);
  550. } else if (insert) {
  551. /*
  552. * split_vma has split insert from vma, and needs
  553. * us to insert it before dropping the locks
  554. * (it may either follow vma or precede it).
  555. */
  556. __insert_vm_struct(mm, insert);
  557. }
  558. if (anon_vma)
  559. spin_unlock(&anon_vma->lock);
  560. if (mapping)
  561. spin_unlock(&mapping->i_mmap_lock);
  562. if (remove_next) {
  563. if (file) {
  564. fput(file);
  565. if (next->vm_flags & VM_EXECUTABLE)
  566. removed_exe_file_vma(mm);
  567. }
  568. mm->map_count--;
  569. mpol_put(vma_policy(next));
  570. kmem_cache_free(vm_area_cachep, next);
  571. /*
  572. * In mprotect's case 6 (see comments on vma_merge),
  573. * we must remove another next too. It would clutter
  574. * up the code too much to do both in one go.
  575. */
  576. if (remove_next == 2) {
  577. next = vma->vm_next;
  578. goto again;
  579. }
  580. }
  581. validate_mm(mm);
  582. }
  583. /* Flags that can be inherited from an existing mapping when merging */
  584. #define VM_MERGEABLE_FLAGS (VM_CAN_NONLINEAR)
  585. /*
  586. * If the vma has a ->close operation then the driver probably needs to release
  587. * per-vma resources, so we don't attempt to merge those.
  588. */
  589. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  590. struct file *file, unsigned long vm_flags)
  591. {
  592. if ((vma->vm_flags ^ vm_flags) & ~VM_MERGEABLE_FLAGS)
  593. return 0;
  594. if (vma->vm_file != file)
  595. return 0;
  596. if (vma->vm_ops && vma->vm_ops->close)
  597. return 0;
  598. return 1;
  599. }
  600. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  601. struct anon_vma *anon_vma2)
  602. {
  603. return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
  604. }
  605. /*
  606. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  607. * in front of (at a lower virtual address and file offset than) the vma.
  608. *
  609. * We cannot merge two vmas if they have differently assigned (non-NULL)
  610. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  611. *
  612. * We don't check here for the merged mmap wrapping around the end of pagecache
  613. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  614. * wrap, nor mmaps which cover the final page at index -1UL.
  615. */
  616. static int
  617. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  618. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  619. {
  620. if (is_mergeable_vma(vma, file, vm_flags) &&
  621. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  622. if (vma->vm_pgoff == vm_pgoff)
  623. return 1;
  624. }
  625. return 0;
  626. }
  627. /*
  628. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  629. * beyond (at a higher virtual address and file offset than) the vma.
  630. *
  631. * We cannot merge two vmas if they have differently assigned (non-NULL)
  632. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  633. */
  634. static int
  635. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  636. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  637. {
  638. if (is_mergeable_vma(vma, file, vm_flags) &&
  639. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  640. pgoff_t vm_pglen;
  641. vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  642. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  643. return 1;
  644. }
  645. return 0;
  646. }
  647. /*
  648. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  649. * whether that can be merged with its predecessor or its successor.
  650. * Or both (it neatly fills a hole).
  651. *
  652. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  653. * certain not to be mapped by the time vma_merge is called; but when
  654. * called for mprotect, it is certain to be already mapped (either at
  655. * an offset within prev, or at the start of next), and the flags of
  656. * this area are about to be changed to vm_flags - and the no-change
  657. * case has already been eliminated.
  658. *
  659. * The following mprotect cases have to be considered, where AAAA is
  660. * the area passed down from mprotect_fixup, never extending beyond one
  661. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  662. *
  663. * AAAA AAAA AAAA AAAA
  664. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  665. * cannot merge might become might become might become
  666. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  667. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  668. * mremap move: PPPPNNNNNNNN 8
  669. * AAAA
  670. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  671. * might become case 1 below case 2 below case 3 below
  672. *
  673. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  674. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  675. */
  676. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  677. struct vm_area_struct *prev, unsigned long addr,
  678. unsigned long end, unsigned long vm_flags,
  679. struct anon_vma *anon_vma, struct file *file,
  680. pgoff_t pgoff, struct mempolicy *policy)
  681. {
  682. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  683. struct vm_area_struct *area, *next;
  684. /*
  685. * We later require that vma->vm_flags == vm_flags,
  686. * so this tests vma->vm_flags & VM_SPECIAL, too.
  687. */
  688. if (vm_flags & VM_SPECIAL)
  689. return NULL;
  690. if (prev)
  691. next = prev->vm_next;
  692. else
  693. next = mm->mmap;
  694. area = next;
  695. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  696. next = next->vm_next;
  697. /*
  698. * Can it merge with the predecessor?
  699. */
  700. if (prev && prev->vm_end == addr &&
  701. mpol_equal(vma_policy(prev), policy) &&
  702. can_vma_merge_after(prev, vm_flags,
  703. anon_vma, file, pgoff)) {
  704. /*
  705. * OK, it can. Can we now merge in the successor as well?
  706. */
  707. if (next && end == next->vm_start &&
  708. mpol_equal(policy, vma_policy(next)) &&
  709. can_vma_merge_before(next, vm_flags,
  710. anon_vma, file, pgoff+pglen) &&
  711. is_mergeable_anon_vma(prev->anon_vma,
  712. next->anon_vma)) {
  713. /* cases 1, 6 */
  714. vma_adjust(prev, prev->vm_start,
  715. next->vm_end, prev->vm_pgoff, NULL);
  716. } else /* cases 2, 5, 7 */
  717. vma_adjust(prev, prev->vm_start,
  718. end, prev->vm_pgoff, NULL);
  719. return prev;
  720. }
  721. /*
  722. * Can this new request be merged in front of next?
  723. */
  724. if (next && end == next->vm_start &&
  725. mpol_equal(policy, vma_policy(next)) &&
  726. can_vma_merge_before(next, vm_flags,
  727. anon_vma, file, pgoff+pglen)) {
  728. if (prev && addr < prev->vm_end) /* case 4 */
  729. vma_adjust(prev, prev->vm_start,
  730. addr, prev->vm_pgoff, NULL);
  731. else /* cases 3, 8 */
  732. vma_adjust(area, addr, next->vm_end,
  733. next->vm_pgoff - pglen, NULL);
  734. return area;
  735. }
  736. return NULL;
  737. }
  738. /*
  739. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  740. * neighbouring vmas for a suitable anon_vma, before it goes off
  741. * to allocate a new anon_vma. It checks because a repetitive
  742. * sequence of mprotects and faults may otherwise lead to distinct
  743. * anon_vmas being allocated, preventing vma merge in subsequent
  744. * mprotect.
  745. */
  746. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  747. {
  748. struct vm_area_struct *near;
  749. unsigned long vm_flags;
  750. near = vma->vm_next;
  751. if (!near)
  752. goto try_prev;
  753. /*
  754. * Since only mprotect tries to remerge vmas, match flags
  755. * which might be mprotected into each other later on.
  756. * Neither mlock nor madvise tries to remerge at present,
  757. * so leave their flags as obstructing a merge.
  758. */
  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 && vma->vm_end == near->vm_start &&
  762. mpol_equal(vma_policy(vma), vma_policy(near)) &&
  763. can_vma_merge_before(near, vm_flags,
  764. NULL, vma->vm_file, vma->vm_pgoff +
  765. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
  766. return near->anon_vma;
  767. try_prev:
  768. /*
  769. * It is potentially slow to have to call find_vma_prev here.
  770. * But it's only on the first write fault on the vma, not
  771. * every time, and we could devise a way to avoid it later
  772. * (e.g. stash info in next's anon_vma_node when assigning
  773. * an anon_vma, or when trying vma_merge). Another time.
  774. */
  775. BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
  776. if (!near)
  777. goto none;
  778. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  779. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  780. if (near->anon_vma && near->vm_end == vma->vm_start &&
  781. mpol_equal(vma_policy(near), vma_policy(vma)) &&
  782. can_vma_merge_after(near, vm_flags,
  783. NULL, vma->vm_file, vma->vm_pgoff))
  784. return near->anon_vma;
  785. none:
  786. /*
  787. * There's no absolute need to look only at touching neighbours:
  788. * we could search further afield for "compatible" anon_vmas.
  789. * But it would probably just be a waste of time searching,
  790. * or lead to too many vmas hanging off the same anon_vma.
  791. * We're trying to allow mprotect remerging later on,
  792. * not trying to minimize memory used for anon_vmas.
  793. */
  794. return NULL;
  795. }
  796. #ifdef CONFIG_PROC_FS
  797. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  798. struct file *file, long pages)
  799. {
  800. const unsigned long stack_flags
  801. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  802. if (file) {
  803. mm->shared_vm += pages;
  804. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  805. mm->exec_vm += pages;
  806. } else if (flags & stack_flags)
  807. mm->stack_vm += pages;
  808. if (flags & (VM_RESERVED|VM_IO))
  809. mm->reserved_vm += pages;
  810. }
  811. #endif /* CONFIG_PROC_FS */
  812. /*
  813. * The caller must hold down_write(current->mm->mmap_sem).
  814. */
  815. unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  816. unsigned long len, unsigned long prot,
  817. unsigned long flags, unsigned long pgoff)
  818. {
  819. struct mm_struct * mm = current->mm;
  820. struct inode *inode;
  821. unsigned int vm_flags;
  822. int error;
  823. int accountable = 1;
  824. unsigned long reqprot = prot;
  825. /*
  826. * Does the application expect PROT_READ to imply PROT_EXEC?
  827. *
  828. * (the exception is when the underlying filesystem is noexec
  829. * mounted, in which case we dont add PROT_EXEC.)
  830. */
  831. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  832. if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
  833. prot |= PROT_EXEC;
  834. if (!len)
  835. return -EINVAL;
  836. if (!(flags & MAP_FIXED))
  837. addr = round_hint_to_min(addr);
  838. error = arch_mmap_check(addr, len, flags);
  839. if (error)
  840. return error;
  841. /* Careful about overflows.. */
  842. len = PAGE_ALIGN(len);
  843. if (!len || len > TASK_SIZE)
  844. return -ENOMEM;
  845. /* offset overflow? */
  846. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  847. return -EOVERFLOW;
  848. /* Too many mappings? */
  849. if (mm->map_count > sysctl_max_map_count)
  850. return -ENOMEM;
  851. /* Obtain the address to map to. we verify (or select) it and ensure
  852. * that it represents a valid section of the address space.
  853. */
  854. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  855. if (addr & ~PAGE_MASK)
  856. return addr;
  857. /* Do simple checking here so the lower-level routines won't have
  858. * to. we assume access permissions have been handled by the open
  859. * of the memory object, so we don't do any here.
  860. */
  861. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  862. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  863. if (flags & MAP_LOCKED) {
  864. if (!can_do_mlock())
  865. return -EPERM;
  866. vm_flags |= VM_LOCKED;
  867. }
  868. /* mlock MCL_FUTURE? */
  869. if (vm_flags & VM_LOCKED) {
  870. unsigned long locked, lock_limit;
  871. locked = len >> PAGE_SHIFT;
  872. locked += mm->locked_vm;
  873. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  874. lock_limit >>= PAGE_SHIFT;
  875. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  876. return -EAGAIN;
  877. }
  878. inode = file ? file->f_path.dentry->d_inode : NULL;
  879. if (file) {
  880. switch (flags & MAP_TYPE) {
  881. case MAP_SHARED:
  882. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  883. return -EACCES;
  884. /*
  885. * Make sure we don't allow writing to an append-only
  886. * file..
  887. */
  888. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  889. return -EACCES;
  890. /*
  891. * Make sure there are no mandatory locks on the file.
  892. */
  893. if (locks_verify_locked(inode))
  894. return -EAGAIN;
  895. vm_flags |= VM_SHARED | VM_MAYSHARE;
  896. if (!(file->f_mode & FMODE_WRITE))
  897. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  898. /* fall through */
  899. case MAP_PRIVATE:
  900. if (!(file->f_mode & FMODE_READ))
  901. return -EACCES;
  902. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  903. if (vm_flags & VM_EXEC)
  904. return -EPERM;
  905. vm_flags &= ~VM_MAYEXEC;
  906. }
  907. if (is_file_hugepages(file))
  908. accountable = 0;
  909. if (!file->f_op || !file->f_op->mmap)
  910. return -ENODEV;
  911. break;
  912. default:
  913. return -EINVAL;
  914. }
  915. } else {
  916. switch (flags & MAP_TYPE) {
  917. case MAP_SHARED:
  918. /*
  919. * Ignore pgoff.
  920. */
  921. pgoff = 0;
  922. vm_flags |= VM_SHARED | VM_MAYSHARE;
  923. break;
  924. case MAP_PRIVATE:
  925. /*
  926. * Set pgoff according to addr for anon_vma.
  927. */
  928. pgoff = addr >> PAGE_SHIFT;
  929. break;
  930. default:
  931. return -EINVAL;
  932. }
  933. }
  934. error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  935. if (error)
  936. return error;
  937. return mmap_region(file, addr, len, flags, vm_flags, pgoff,
  938. accountable);
  939. }
  940. EXPORT_SYMBOL(do_mmap_pgoff);
  941. /*
  942. * Some shared mappigns will want the pages marked read-only
  943. * to track write events. If so, we'll downgrade vm_page_prot
  944. * to the private version (using protection_map[] without the
  945. * VM_SHARED bit).
  946. */
  947. int vma_wants_writenotify(struct vm_area_struct *vma)
  948. {
  949. unsigned int vm_flags = vma->vm_flags;
  950. /* If it was private or non-writable, the write bit is already clear */
  951. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  952. return 0;
  953. /* The backer wishes to know when pages are first written to? */
  954. if (vma->vm_ops && vma->vm_ops->page_mkwrite)
  955. return 1;
  956. /* The open routine did something to the protections already? */
  957. if (pgprot_val(vma->vm_page_prot) !=
  958. pgprot_val(vm_get_page_prot(vm_flags)))
  959. return 0;
  960. /* Specialty mapping? */
  961. if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
  962. return 0;
  963. /* Can the mapping track the dirty pages? */
  964. return vma->vm_file && vma->vm_file->f_mapping &&
  965. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  966. }
  967. unsigned long mmap_region(struct file *file, unsigned long addr,
  968. unsigned long len, unsigned long flags,
  969. unsigned int vm_flags, unsigned long pgoff,
  970. int accountable)
  971. {
  972. struct mm_struct *mm = current->mm;
  973. struct vm_area_struct *vma, *prev;
  974. int correct_wcount = 0;
  975. int error;
  976. struct rb_node **rb_link, *rb_parent;
  977. unsigned long charged = 0;
  978. struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
  979. /* Clear old maps */
  980. error = -ENOMEM;
  981. munmap_back:
  982. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  983. if (vma && vma->vm_start < addr + len) {
  984. if (do_munmap(mm, addr, len))
  985. return -ENOMEM;
  986. goto munmap_back;
  987. }
  988. /* Check against address space limit. */
  989. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  990. return -ENOMEM;
  991. if (flags & MAP_NORESERVE)
  992. vm_flags |= VM_NORESERVE;
  993. if (accountable && (!(flags & MAP_NORESERVE) ||
  994. sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
  995. if (vm_flags & VM_SHARED) {
  996. /* Check memory availability in shmem_file_setup? */
  997. vm_flags |= VM_ACCOUNT;
  998. } else if (vm_flags & VM_WRITE) {
  999. /*
  1000. * Private writable mapping: check memory availability
  1001. */
  1002. charged = len >> PAGE_SHIFT;
  1003. if (security_vm_enough_memory(charged))
  1004. return -ENOMEM;
  1005. vm_flags |= VM_ACCOUNT;
  1006. }
  1007. }
  1008. /*
  1009. * Can we just expand an old mapping?
  1010. */
  1011. vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
  1012. if (vma)
  1013. goto out;
  1014. /*
  1015. * Determine the object being mapped and call the appropriate
  1016. * specific mapper. the address has already been validated, but
  1017. * not unmapped, but the maps are removed from the list.
  1018. */
  1019. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1020. if (!vma) {
  1021. error = -ENOMEM;
  1022. goto unacct_error;
  1023. }
  1024. vma->vm_mm = mm;
  1025. vma->vm_start = addr;
  1026. vma->vm_end = addr + len;
  1027. vma->vm_flags = vm_flags;
  1028. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1029. vma->vm_pgoff = pgoff;
  1030. if (file) {
  1031. error = -EINVAL;
  1032. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1033. goto free_vma;
  1034. if (vm_flags & VM_DENYWRITE) {
  1035. error = deny_write_access(file);
  1036. if (error)
  1037. goto free_vma;
  1038. correct_wcount = 1;
  1039. }
  1040. vma->vm_file = file;
  1041. get_file(file);
  1042. error = file->f_op->mmap(file, vma);
  1043. if (error)
  1044. goto unmap_and_free_vma;
  1045. if (vm_flags & VM_EXECUTABLE)
  1046. added_exe_file_vma(mm);
  1047. } else if (vm_flags & VM_SHARED) {
  1048. error = shmem_zero_setup(vma);
  1049. if (error)
  1050. goto free_vma;
  1051. }
  1052. /* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
  1053. * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
  1054. * that memory reservation must be checked; but that reservation
  1055. * belongs to shared memory object, not to vma: so now clear it.
  1056. */
  1057. if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
  1058. vma->vm_flags &= ~VM_ACCOUNT;
  1059. /* Can addr have changed??
  1060. *
  1061. * Answer: Yes, several device drivers can do it in their
  1062. * f_op->mmap method. -DaveM
  1063. */
  1064. addr = vma->vm_start;
  1065. pgoff = vma->vm_pgoff;
  1066. vm_flags = vma->vm_flags;
  1067. if (vma_wants_writenotify(vma))
  1068. vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
  1069. vma_link(mm, vma, prev, rb_link, rb_parent);
  1070. file = vma->vm_file;
  1071. /* Once vma denies write, undo our temporary denial count */
  1072. if (correct_wcount)
  1073. atomic_inc(&inode->i_writecount);
  1074. out:
  1075. mm->total_vm += len >> PAGE_SHIFT;
  1076. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1077. if (vm_flags & VM_LOCKED) {
  1078. /*
  1079. * makes pages present; downgrades, drops, reacquires mmap_sem
  1080. */
  1081. long nr_pages = mlock_vma_pages_range(vma, addr, addr + len);
  1082. if (nr_pages < 0)
  1083. return nr_pages; /* vma gone! */
  1084. mm->locked_vm += (len >> PAGE_SHIFT) - nr_pages;
  1085. } else 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
  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 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. if (mlock_vma_pages_range(prev, addr, prev->vm_end) < 0)
  1492. return NULL; /* vma gone! */
  1493. }
  1494. return prev;
  1495. }
  1496. #else
  1497. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1498. {
  1499. return expand_downwards(vma, address);
  1500. }
  1501. struct vm_area_struct *
  1502. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1503. {
  1504. struct vm_area_struct * vma;
  1505. unsigned long start;
  1506. addr &= PAGE_MASK;
  1507. vma = find_vma(mm,addr);
  1508. if (!vma)
  1509. return NULL;
  1510. if (vma->vm_start <= addr)
  1511. return vma;
  1512. if (!(vma->vm_flags & VM_GROWSDOWN))
  1513. return NULL;
  1514. start = vma->vm_start;
  1515. if (expand_stack(vma, addr))
  1516. return NULL;
  1517. if (vma->vm_flags & VM_LOCKED) {
  1518. if (mlock_vma_pages_range(vma, addr, start) < 0)
  1519. return NULL; /* vma gone! */
  1520. }
  1521. return vma;
  1522. }
  1523. #endif
  1524. /*
  1525. * Ok - we have the memory areas we should free on the vma list,
  1526. * so release them, and do the vma updates.
  1527. *
  1528. * Called with the mm semaphore held.
  1529. */
  1530. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1531. {
  1532. /* Update high watermark before we lower total_vm */
  1533. update_hiwater_vm(mm);
  1534. do {
  1535. long nrpages = vma_pages(vma);
  1536. mm->total_vm -= nrpages;
  1537. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1538. vma = remove_vma(vma);
  1539. } while (vma);
  1540. validate_mm(mm);
  1541. }
  1542. /*
  1543. * Get rid of page table information in the indicated region.
  1544. *
  1545. * Called with the mm semaphore held.
  1546. */
  1547. static void unmap_region(struct mm_struct *mm,
  1548. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1549. unsigned long start, unsigned long end)
  1550. {
  1551. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1552. struct mmu_gather *tlb;
  1553. unsigned long nr_accounted = 0;
  1554. lru_add_drain();
  1555. tlb = tlb_gather_mmu(mm, 0);
  1556. update_hiwater_rss(mm);
  1557. unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
  1558. vm_unacct_memory(nr_accounted);
  1559. free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
  1560. next? next->vm_start: 0);
  1561. tlb_finish_mmu(tlb, start, end);
  1562. }
  1563. /*
  1564. * Create a list of vma's touched by the unmap, removing them from the mm's
  1565. * vma list as we go..
  1566. */
  1567. static void
  1568. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  1569. struct vm_area_struct *prev, unsigned long end)
  1570. {
  1571. struct vm_area_struct **insertion_point;
  1572. struct vm_area_struct *tail_vma = NULL;
  1573. unsigned long addr;
  1574. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  1575. do {
  1576. rb_erase(&vma->vm_rb, &mm->mm_rb);
  1577. mm->map_count--;
  1578. tail_vma = vma;
  1579. vma = vma->vm_next;
  1580. } while (vma && vma->vm_start < end);
  1581. *insertion_point = vma;
  1582. tail_vma->vm_next = NULL;
  1583. if (mm->unmap_area == arch_unmap_area)
  1584. addr = prev ? prev->vm_end : mm->mmap_base;
  1585. else
  1586. addr = vma ? vma->vm_start : mm->mmap_base;
  1587. mm->unmap_area(mm, addr);
  1588. mm->mmap_cache = NULL; /* Kill the cache. */
  1589. }
  1590. /*
  1591. * Split a vma into two pieces at address 'addr', a new vma is allocated
  1592. * either for the first part or the tail.
  1593. */
  1594. int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  1595. unsigned long addr, int new_below)
  1596. {
  1597. struct mempolicy *pol;
  1598. struct vm_area_struct *new;
  1599. if (is_vm_hugetlb_page(vma) && (addr &
  1600. ~(huge_page_mask(hstate_vma(vma)))))
  1601. return -EINVAL;
  1602. if (mm->map_count >= sysctl_max_map_count)
  1603. return -ENOMEM;
  1604. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1605. if (!new)
  1606. return -ENOMEM;
  1607. /* most fields are the same, copy all, and then fixup */
  1608. *new = *vma;
  1609. if (new_below)
  1610. new->vm_end = addr;
  1611. else {
  1612. new->vm_start = addr;
  1613. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  1614. }
  1615. pol = mpol_dup(vma_policy(vma));
  1616. if (IS_ERR(pol)) {
  1617. kmem_cache_free(vm_area_cachep, new);
  1618. return PTR_ERR(pol);
  1619. }
  1620. vma_set_policy(new, pol);
  1621. if (new->vm_file) {
  1622. get_file(new->vm_file);
  1623. if (vma->vm_flags & VM_EXECUTABLE)
  1624. added_exe_file_vma(mm);
  1625. }
  1626. if (new->vm_ops && new->vm_ops->open)
  1627. new->vm_ops->open(new);
  1628. if (new_below)
  1629. vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  1630. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  1631. else
  1632. vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  1633. return 0;
  1634. }
  1635. /* Munmap is split into 2 main parts -- this part which finds
  1636. * what needs doing, and the areas themselves, which do the
  1637. * work. This now handles partial unmappings.
  1638. * Jeremy Fitzhardinge <jeremy@goop.org>
  1639. */
  1640. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1641. {
  1642. unsigned long end;
  1643. struct vm_area_struct *vma, *prev, *last;
  1644. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  1645. return -EINVAL;
  1646. if ((len = PAGE_ALIGN(len)) == 0)
  1647. return -EINVAL;
  1648. /* Find the first overlapping VMA */
  1649. vma = find_vma_prev(mm, start, &prev);
  1650. if (!vma)
  1651. return 0;
  1652. /* we have start < vma->vm_end */
  1653. /* if it doesn't overlap, we have nothing.. */
  1654. end = start + len;
  1655. if (vma->vm_start >= end)
  1656. return 0;
  1657. /*
  1658. * If we need to split any vma, do it now to save pain later.
  1659. *
  1660. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  1661. * unmapped vm_area_struct will remain in use: so lower split_vma
  1662. * places tmp vma above, and higher split_vma places tmp vma below.
  1663. */
  1664. if (start > vma->vm_start) {
  1665. int error = split_vma(mm, vma, start, 0);
  1666. if (error)
  1667. return error;
  1668. prev = vma;
  1669. }
  1670. /* Does it split the last one? */
  1671. last = find_vma(mm, end);
  1672. if (last && end > last->vm_start) {
  1673. int error = split_vma(mm, last, end, 1);
  1674. if (error)
  1675. return error;
  1676. }
  1677. vma = prev? prev->vm_next: mm->mmap;
  1678. /*
  1679. * unlock any mlock()ed ranges before detaching vmas
  1680. */
  1681. if (mm->locked_vm) {
  1682. struct vm_area_struct *tmp = vma;
  1683. while (tmp && tmp->vm_start < end) {
  1684. if (tmp->vm_flags & VM_LOCKED) {
  1685. mm->locked_vm -= vma_pages(tmp);
  1686. munlock_vma_pages_all(tmp);
  1687. }
  1688. tmp = tmp->vm_next;
  1689. }
  1690. }
  1691. /*
  1692. * Remove the vma's, and unmap the actual pages
  1693. */
  1694. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  1695. unmap_region(mm, vma, prev, start, end);
  1696. /* Fix up all other VM information */
  1697. remove_vma_list(mm, vma);
  1698. return 0;
  1699. }
  1700. EXPORT_SYMBOL(do_munmap);
  1701. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1702. {
  1703. int ret;
  1704. struct mm_struct *mm = current->mm;
  1705. profile_munmap(addr);
  1706. down_write(&mm->mmap_sem);
  1707. ret = do_munmap(mm, addr, len);
  1708. up_write(&mm->mmap_sem);
  1709. return ret;
  1710. }
  1711. static inline void verify_mm_writelocked(struct mm_struct *mm)
  1712. {
  1713. #ifdef CONFIG_DEBUG_VM
  1714. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  1715. WARN_ON(1);
  1716. up_read(&mm->mmap_sem);
  1717. }
  1718. #endif
  1719. }
  1720. /*
  1721. * this is really a simplified "do_mmap". it only handles
  1722. * anonymous maps. eventually we may be able to do some
  1723. * brk-specific accounting here.
  1724. */
  1725. unsigned long do_brk(unsigned long addr, unsigned long len)
  1726. {
  1727. struct mm_struct * mm = current->mm;
  1728. struct vm_area_struct * vma, * prev;
  1729. unsigned long flags;
  1730. struct rb_node ** rb_link, * rb_parent;
  1731. pgoff_t pgoff = addr >> PAGE_SHIFT;
  1732. int error;
  1733. len = PAGE_ALIGN(len);
  1734. if (!len)
  1735. return addr;
  1736. if ((addr + len) > TASK_SIZE || (addr + len) < addr)
  1737. return -EINVAL;
  1738. if (is_hugepage_only_range(mm, addr, len))
  1739. return -EINVAL;
  1740. error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
  1741. if (error)
  1742. return error;
  1743. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  1744. error = arch_mmap_check(addr, len, flags);
  1745. if (error)
  1746. return error;
  1747. /*
  1748. * mlock MCL_FUTURE?
  1749. */
  1750. if (mm->def_flags & VM_LOCKED) {
  1751. unsigned long locked, lock_limit;
  1752. locked = len >> PAGE_SHIFT;
  1753. locked += mm->locked_vm;
  1754. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1755. lock_limit >>= PAGE_SHIFT;
  1756. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1757. return -EAGAIN;
  1758. }
  1759. /*
  1760. * mm->mmap_sem is required to protect against another thread
  1761. * changing the mappings in case we sleep.
  1762. */
  1763. verify_mm_writelocked(mm);
  1764. /*
  1765. * Clear old maps. this also does some error checking for us
  1766. */
  1767. munmap_back:
  1768. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1769. if (vma && vma->vm_start < addr + len) {
  1770. if (do_munmap(mm, addr, len))
  1771. return -ENOMEM;
  1772. goto munmap_back;
  1773. }
  1774. /* Check against address space limits *after* clearing old maps... */
  1775. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1776. return -ENOMEM;
  1777. if (mm->map_count > sysctl_max_map_count)
  1778. return -ENOMEM;
  1779. if (security_vm_enough_memory(len >> PAGE_SHIFT))
  1780. return -ENOMEM;
  1781. /* Can we just expand an old private anonymous mapping? */
  1782. vma = vma_merge(mm, prev, addr, addr + len, flags,
  1783. NULL, NULL, pgoff, NULL);
  1784. if (vma)
  1785. goto out;
  1786. /*
  1787. * create a vma struct for an anonymous mapping
  1788. */
  1789. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1790. if (!vma) {
  1791. vm_unacct_memory(len >> PAGE_SHIFT);
  1792. return -ENOMEM;
  1793. }
  1794. vma->vm_mm = mm;
  1795. vma->vm_start = addr;
  1796. vma->vm_end = addr + len;
  1797. vma->vm_pgoff = pgoff;
  1798. vma->vm_flags = flags;
  1799. vma->vm_page_prot = vm_get_page_prot(flags);
  1800. vma_link(mm, vma, prev, rb_link, rb_parent);
  1801. out:
  1802. mm->total_vm += len >> PAGE_SHIFT;
  1803. if (flags & VM_LOCKED) {
  1804. if (!mlock_vma_pages_range(vma, addr, addr + len))
  1805. mm->locked_vm += (len >> PAGE_SHIFT);
  1806. }
  1807. return addr;
  1808. }
  1809. EXPORT_SYMBOL(do_brk);
  1810. /* Release all mmaps. */
  1811. void exit_mmap(struct mm_struct *mm)
  1812. {
  1813. struct mmu_gather *tlb;
  1814. struct vm_area_struct *vma;
  1815. unsigned long nr_accounted = 0;
  1816. unsigned long end;
  1817. /* mm's last user has gone, and its about to be pulled down */
  1818. arch_exit_mmap(mm);
  1819. mmu_notifier_release(mm);
  1820. if (!mm->mmap) /* Can happen if dup_mmap() received an OOM */
  1821. return;
  1822. if (mm->locked_vm) {
  1823. vma = mm->mmap;
  1824. while (vma) {
  1825. if (vma->vm_flags & VM_LOCKED)
  1826. munlock_vma_pages_all(vma);
  1827. vma = vma->vm_next;
  1828. }
  1829. }
  1830. vma = mm->mmap;
  1831. lru_add_drain();
  1832. flush_cache_mm(mm);
  1833. tlb = tlb_gather_mmu(mm, 1);
  1834. /* update_hiwater_rss(mm) here? but nobody should be looking */
  1835. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  1836. end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
  1837. vm_unacct_memory(nr_accounted);
  1838. free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
  1839. tlb_finish_mmu(tlb, 0, end);
  1840. /*
  1841. * Walk the list again, actually closing and freeing it,
  1842. * with preemption enabled, without holding any MM locks.
  1843. */
  1844. while (vma)
  1845. vma = remove_vma(vma);
  1846. BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  1847. }
  1848. /* Insert vm structure into process list sorted by address
  1849. * and into the inode's i_mmap tree. If vm_file is non-NULL
  1850. * then i_mmap_lock is taken here.
  1851. */
  1852. int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  1853. {
  1854. struct vm_area_struct * __vma, * prev;
  1855. struct rb_node ** rb_link, * rb_parent;
  1856. /*
  1857. * The vm_pgoff of a purely anonymous vma should be irrelevant
  1858. * until its first write fault, when page's anon_vma and index
  1859. * are set. But now set the vm_pgoff it will almost certainly
  1860. * end up with (unless mremap moves it elsewhere before that
  1861. * first wfault), so /proc/pid/maps tells a consistent story.
  1862. *
  1863. * By setting it to reflect the virtual start address of the
  1864. * vma, merges and splits can happen in a seamless way, just
  1865. * using the existing file pgoff checks and manipulations.
  1866. * Similarly in do_mmap_pgoff and in do_brk.
  1867. */
  1868. if (!vma->vm_file) {
  1869. BUG_ON(vma->anon_vma);
  1870. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1871. }
  1872. __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
  1873. if (__vma && __vma->vm_start < vma->vm_end)
  1874. return -ENOMEM;
  1875. if ((vma->vm_flags & VM_ACCOUNT) &&
  1876. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  1877. return -ENOMEM;
  1878. vma_link(mm, vma, prev, rb_link, rb_parent);
  1879. return 0;
  1880. }
  1881. /*
  1882. * Copy the vma structure to a new location in the same mm,
  1883. * prior to moving page table entries, to effect an mremap move.
  1884. */
  1885. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  1886. unsigned long addr, unsigned long len, pgoff_t pgoff)
  1887. {
  1888. struct vm_area_struct *vma = *vmap;
  1889. unsigned long vma_start = vma->vm_start;
  1890. struct mm_struct *mm = vma->vm_mm;
  1891. struct vm_area_struct *new_vma, *prev;
  1892. struct rb_node **rb_link, *rb_parent;
  1893. struct mempolicy *pol;
  1894. /*
  1895. * If anonymous vma has not yet been faulted, update new pgoff
  1896. * to match new location, to increase its chance of merging.
  1897. */
  1898. if (!vma->vm_file && !vma->anon_vma)
  1899. pgoff = addr >> PAGE_SHIFT;
  1900. find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1901. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  1902. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  1903. if (new_vma) {
  1904. /*
  1905. * Source vma may have been merged into new_vma
  1906. */
  1907. if (vma_start >= new_vma->vm_start &&
  1908. vma_start < new_vma->vm_end)
  1909. *vmap = new_vma;
  1910. } else {
  1911. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1912. if (new_vma) {
  1913. *new_vma = *vma;
  1914. pol = mpol_dup(vma_policy(vma));
  1915. if (IS_ERR(pol)) {
  1916. kmem_cache_free(vm_area_cachep, new_vma);
  1917. return NULL;
  1918. }
  1919. vma_set_policy(new_vma, pol);
  1920. new_vma->vm_start = addr;
  1921. new_vma->vm_end = addr + len;
  1922. new_vma->vm_pgoff = pgoff;
  1923. if (new_vma->vm_file) {
  1924. get_file(new_vma->vm_file);
  1925. if (vma->vm_flags & VM_EXECUTABLE)
  1926. added_exe_file_vma(mm);
  1927. }
  1928. if (new_vma->vm_ops && new_vma->vm_ops->open)
  1929. new_vma->vm_ops->open(new_vma);
  1930. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  1931. }
  1932. }
  1933. return new_vma;
  1934. }
  1935. /*
  1936. * Return true if the calling process may expand its vm space by the passed
  1937. * number of pages
  1938. */
  1939. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  1940. {
  1941. unsigned long cur = mm->total_vm; /* pages */
  1942. unsigned long lim;
  1943. lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  1944. if (cur + npages > lim)
  1945. return 0;
  1946. return 1;
  1947. }
  1948. static int special_mapping_fault(struct vm_area_struct *vma,
  1949. struct vm_fault *vmf)
  1950. {
  1951. pgoff_t pgoff;
  1952. struct page **pages;
  1953. /*
  1954. * special mappings have no vm_file, and in that case, the mm
  1955. * uses vm_pgoff internally. So we have to subtract it from here.
  1956. * We are allowed to do this because we are the mm; do not copy
  1957. * this code into drivers!
  1958. */
  1959. pgoff = vmf->pgoff - vma->vm_pgoff;
  1960. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  1961. pgoff--;
  1962. if (*pages) {
  1963. struct page *page = *pages;
  1964. get_page(page);
  1965. vmf->page = page;
  1966. return 0;
  1967. }
  1968. return VM_FAULT_SIGBUS;
  1969. }
  1970. /*
  1971. * Having a close hook prevents vma merging regardless of flags.
  1972. */
  1973. static void special_mapping_close(struct vm_area_struct *vma)
  1974. {
  1975. }
  1976. static struct vm_operations_struct special_mapping_vmops = {
  1977. .close = special_mapping_close,
  1978. .fault = special_mapping_fault,
  1979. };
  1980. /*
  1981. * Called with mm->mmap_sem held for writing.
  1982. * Insert a new vma covering the given region, with the given flags.
  1983. * Its pages are supplied by the given array of struct page *.
  1984. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  1985. * The region past the last page supplied will always produce SIGBUS.
  1986. * The array pointer and the pages it points to are assumed to stay alive
  1987. * for as long as this mapping might exist.
  1988. */
  1989. int install_special_mapping(struct mm_struct *mm,
  1990. unsigned long addr, unsigned long len,
  1991. unsigned long vm_flags, struct page **pages)
  1992. {
  1993. struct vm_area_struct *vma;
  1994. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1995. if (unlikely(vma == NULL))
  1996. return -ENOMEM;
  1997. vma->vm_mm = mm;
  1998. vma->vm_start = addr;
  1999. vma->vm_end = addr + len;
  2000. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2001. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2002. vma->vm_ops = &special_mapping_vmops;
  2003. vma->vm_private_data = pages;
  2004. if (unlikely(insert_vm_struct(mm, vma))) {
  2005. kmem_cache_free(vm_area_cachep, vma);
  2006. return -ENOMEM;
  2007. }
  2008. mm->total_vm += len >> PAGE_SHIFT;
  2009. return 0;
  2010. }
  2011. static DEFINE_MUTEX(mm_all_locks_mutex);
  2012. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2013. {
  2014. if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
  2015. /*
  2016. * The LSB of head.next can't change from under us
  2017. * because we hold the mm_all_locks_mutex.
  2018. */
  2019. spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
  2020. /*
  2021. * We can safely modify head.next after taking the
  2022. * anon_vma->lock. If some other vma in this mm shares
  2023. * the same anon_vma we won't take it again.
  2024. *
  2025. * No need of atomic instructions here, head.next
  2026. * can't change from under us thanks to the
  2027. * anon_vma->lock.
  2028. */
  2029. if (__test_and_set_bit(0, (unsigned long *)
  2030. &anon_vma->head.next))
  2031. BUG();
  2032. }
  2033. }
  2034. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2035. {
  2036. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2037. /*
  2038. * AS_MM_ALL_LOCKS can't change from under us because
  2039. * we hold the mm_all_locks_mutex.
  2040. *
  2041. * Operations on ->flags have to be atomic because
  2042. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2043. * mm_all_locks_mutex, there may be other cpus
  2044. * changing other bitflags in parallel to us.
  2045. */
  2046. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2047. BUG();
  2048. spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
  2049. }
  2050. }
  2051. /*
  2052. * This operation locks against the VM for all pte/vma/mm related
  2053. * operations that could ever happen on a certain mm. This includes
  2054. * vmtruncate, try_to_unmap, and all page faults.
  2055. *
  2056. * The caller must take the mmap_sem in write mode before calling
  2057. * mm_take_all_locks(). The caller isn't allowed to release the
  2058. * mmap_sem until mm_drop_all_locks() returns.
  2059. *
  2060. * mmap_sem in write mode is required in order to block all operations
  2061. * that could modify pagetables and free pages without need of
  2062. * altering the vma layout (for example populate_range() with
  2063. * nonlinear vmas). It's also needed in write mode to avoid new
  2064. * anon_vmas to be associated with existing vmas.
  2065. *
  2066. * A single task can't take more than one mm_take_all_locks() in a row
  2067. * or it would deadlock.
  2068. *
  2069. * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
  2070. * mapping->flags avoid to take the same lock twice, if more than one
  2071. * vma in this mm is backed by the same anon_vma or address_space.
  2072. *
  2073. * We can take all the locks in random order because the VM code
  2074. * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
  2075. * takes more than one of them in a row. Secondly we're protected
  2076. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2077. *
  2078. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2079. * that may have to take thousand of locks.
  2080. *
  2081. * mm_take_all_locks() can fail if it's interrupted by signals.
  2082. */
  2083. int mm_take_all_locks(struct mm_struct *mm)
  2084. {
  2085. struct vm_area_struct *vma;
  2086. int ret = -EINTR;
  2087. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2088. mutex_lock(&mm_all_locks_mutex);
  2089. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2090. if (signal_pending(current))
  2091. goto out_unlock;
  2092. if (vma->vm_file && vma->vm_file->f_mapping)
  2093. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2094. }
  2095. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2096. if (signal_pending(current))
  2097. goto out_unlock;
  2098. if (vma->anon_vma)
  2099. vm_lock_anon_vma(mm, vma->anon_vma);
  2100. }
  2101. ret = 0;
  2102. out_unlock:
  2103. if (ret)
  2104. mm_drop_all_locks(mm);
  2105. return ret;
  2106. }
  2107. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2108. {
  2109. if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
  2110. /*
  2111. * The LSB of head.next can't change to 0 from under
  2112. * us because we hold the mm_all_locks_mutex.
  2113. *
  2114. * We must however clear the bitflag before unlocking
  2115. * the vma so the users using the anon_vma->head will
  2116. * never see our bitflag.
  2117. *
  2118. * No need of atomic instructions here, head.next
  2119. * can't change from under us until we release the
  2120. * anon_vma->lock.
  2121. */
  2122. if (!__test_and_clear_bit(0, (unsigned long *)
  2123. &anon_vma->head.next))
  2124. BUG();
  2125. spin_unlock(&anon_vma->lock);
  2126. }
  2127. }
  2128. static void vm_unlock_mapping(struct address_space *mapping)
  2129. {
  2130. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2131. /*
  2132. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2133. * because we hold the mm_all_locks_mutex.
  2134. */
  2135. spin_unlock(&mapping->i_mmap_lock);
  2136. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2137. &mapping->flags))
  2138. BUG();
  2139. }
  2140. }
  2141. /*
  2142. * The mmap_sem cannot be released by the caller until
  2143. * mm_drop_all_locks() returns.
  2144. */
  2145. void mm_drop_all_locks(struct mm_struct *mm)
  2146. {
  2147. struct vm_area_struct *vma;
  2148. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2149. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2150. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2151. if (vma->anon_vma)
  2152. vm_unlock_anon_vma(vma->anon_vma);
  2153. if (vma->vm_file && vma->vm_file->f_mapping)
  2154. vm_unlock_mapping(vma->vm_file->f_mapping);
  2155. }
  2156. mutex_unlock(&mm_all_locks_mutex);
  2157. }
  2158. /*
  2159. * initialise the VMA slab
  2160. */
  2161. void __init mmap_init(void)
  2162. {
  2163. vm_area_cachep = kmem_cache_create("vm_area_struct",
  2164. sizeof(struct vm_area_struct), 0,
  2165. SLAB_PANIC, NULL);
  2166. }