mmap.c 65 KB

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