mmap.c 65 KB

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