mmap.c 64 KB

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