mmap.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055
  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/export.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 <linux/audit.h>
  31. #include <linux/khugepaged.h>
  32. #include <linux/uprobes.h>
  33. #include <linux/rbtree_augmented.h>
  34. #include <linux/sched/sysctl.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/tlb.h>
  38. #include <asm/mmu_context.h>
  39. #include "internal.h"
  40. #ifndef arch_mmap_check
  41. #define arch_mmap_check(addr, len, flags) (0)
  42. #endif
  43. #ifndef arch_rebalance_pgtables
  44. #define arch_rebalance_pgtables(addr, len) (addr)
  45. #endif
  46. static void unmap_region(struct mm_struct *mm,
  47. struct vm_area_struct *vma, struct vm_area_struct *prev,
  48. unsigned long start, unsigned long end);
  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 __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
  76. int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
  77. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  78. /*
  79. * Make sure vm_committed_as in one cacheline and not cacheline shared with
  80. * other variables. It can be updated by several CPUs frequently.
  81. */
  82. struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
  83. /*
  84. * The global memory commitment made in the system can be a metric
  85. * that can be used to drive ballooning decisions when Linux is hosted
  86. * as a guest. On Hyper-V, the host implements a policy engine for dynamically
  87. * balancing memory across competing virtual machines that are hosted.
  88. * Several metrics drive this policy engine including the guest reported
  89. * memory commitment.
  90. */
  91. unsigned long vm_memory_committed(void)
  92. {
  93. return percpu_counter_read_positive(&vm_committed_as);
  94. }
  95. EXPORT_SYMBOL_GPL(vm_memory_committed);
  96. /*
  97. * Check that a process has enough memory to allocate a new virtual
  98. * mapping. 0 means there is enough memory for the allocation to
  99. * succeed and -ENOMEM implies there is not.
  100. *
  101. * We currently support three overcommit policies, which are set via the
  102. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  103. *
  104. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  105. * Additional code 2002 Jul 20 by Robert Love.
  106. *
  107. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  108. *
  109. * Note this is a helper function intended to be used by LSMs which
  110. * wish to use this logic.
  111. */
  112. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  113. {
  114. unsigned long free, allowed;
  115. vm_acct_memory(pages);
  116. /*
  117. * Sometimes we want to use more memory than we have
  118. */
  119. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  120. return 0;
  121. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  122. free = global_page_state(NR_FREE_PAGES);
  123. free += global_page_state(NR_FILE_PAGES);
  124. /*
  125. * shmem pages shouldn't be counted as free in this
  126. * case, they can't be purged, only swapped out, and
  127. * that won't affect the overall amount of available
  128. * memory in the system.
  129. */
  130. free -= global_page_state(NR_SHMEM);
  131. free += nr_swap_pages;
  132. /*
  133. * Any slabs which are created with the
  134. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  135. * which are reclaimable, under pressure. The dentry
  136. * cache and most inode caches should fall into this
  137. */
  138. free += global_page_state(NR_SLAB_RECLAIMABLE);
  139. /*
  140. * Leave reserved pages. The pages are not for anonymous pages.
  141. */
  142. if (free <= totalreserve_pages)
  143. goto error;
  144. else
  145. free -= totalreserve_pages;
  146. /*
  147. * Leave the last 3% for root
  148. */
  149. if (!cap_sys_admin)
  150. free -= free / 32;
  151. if (free > pages)
  152. return 0;
  153. goto error;
  154. }
  155. allowed = (totalram_pages - hugetlb_total_pages())
  156. * sysctl_overcommit_ratio / 100;
  157. /*
  158. * Leave the last 3% for root
  159. */
  160. if (!cap_sys_admin)
  161. allowed -= allowed / 32;
  162. allowed += total_swap_pages;
  163. /* Don't let a single process grow too big:
  164. leave 3% of the size of this process for other processes */
  165. if (mm)
  166. allowed -= mm->total_vm / 32;
  167. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  168. return 0;
  169. error:
  170. vm_unacct_memory(pages);
  171. return -ENOMEM;
  172. }
  173. /*
  174. * Requires inode->i_mapping->i_mmap_mutex
  175. */
  176. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  177. struct file *file, struct address_space *mapping)
  178. {
  179. if (vma->vm_flags & VM_DENYWRITE)
  180. atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
  181. if (vma->vm_flags & VM_SHARED)
  182. mapping->i_mmap_writable--;
  183. flush_dcache_mmap_lock(mapping);
  184. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  185. list_del_init(&vma->shared.nonlinear);
  186. else
  187. vma_interval_tree_remove(vma, &mapping->i_mmap);
  188. flush_dcache_mmap_unlock(mapping);
  189. }
  190. /*
  191. * Unlink a file-based vm structure from its interval tree, to hide
  192. * vma from rmap and vmtruncate before freeing its page tables.
  193. */
  194. void unlink_file_vma(struct vm_area_struct *vma)
  195. {
  196. struct file *file = vma->vm_file;
  197. if (file) {
  198. struct address_space *mapping = file->f_mapping;
  199. mutex_lock(&mapping->i_mmap_mutex);
  200. __remove_shared_vm_struct(vma, file, mapping);
  201. mutex_unlock(&mapping->i_mmap_mutex);
  202. }
  203. }
  204. /*
  205. * Close a vm structure and free it, returning the next.
  206. */
  207. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  208. {
  209. struct vm_area_struct *next = vma->vm_next;
  210. might_sleep();
  211. if (vma->vm_ops && vma->vm_ops->close)
  212. vma->vm_ops->close(vma);
  213. if (vma->vm_file)
  214. fput(vma->vm_file);
  215. mpol_put(vma_policy(vma));
  216. kmem_cache_free(vm_area_cachep, vma);
  217. return next;
  218. }
  219. static unsigned long do_brk(unsigned long addr, unsigned long len);
  220. SYSCALL_DEFINE1(brk, unsigned long, brk)
  221. {
  222. unsigned long rlim, retval;
  223. unsigned long newbrk, oldbrk;
  224. struct mm_struct *mm = current->mm;
  225. unsigned long min_brk;
  226. down_write(&mm->mmap_sem);
  227. #ifdef CONFIG_COMPAT_BRK
  228. /*
  229. * CONFIG_COMPAT_BRK can still be overridden by setting
  230. * randomize_va_space to 2, which will still cause mm->start_brk
  231. * to be arbitrarily shifted
  232. */
  233. if (current->brk_randomized)
  234. min_brk = mm->start_brk;
  235. else
  236. min_brk = mm->end_data;
  237. #else
  238. min_brk = mm->start_brk;
  239. #endif
  240. if (brk < min_brk)
  241. goto out;
  242. /*
  243. * Check against rlimit here. If this check is done later after the test
  244. * of oldbrk with newbrk then it can escape the test and let the data
  245. * segment grow beyond its set limit the in case where the limit is
  246. * not page aligned -Ram Gupta
  247. */
  248. rlim = rlimit(RLIMIT_DATA);
  249. if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
  250. (mm->end_data - mm->start_data) > rlim)
  251. goto out;
  252. newbrk = PAGE_ALIGN(brk);
  253. oldbrk = PAGE_ALIGN(mm->brk);
  254. if (oldbrk == newbrk)
  255. goto set_brk;
  256. /* Always allow shrinking brk. */
  257. if (brk <= mm->brk) {
  258. if (!do_munmap(mm, newbrk, oldbrk-newbrk))
  259. goto set_brk;
  260. goto out;
  261. }
  262. /* Check against existing mmap mappings. */
  263. if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
  264. goto out;
  265. /* Ok, looks good - let it rip. */
  266. if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
  267. goto out;
  268. set_brk:
  269. mm->brk = brk;
  270. out:
  271. retval = mm->brk;
  272. up_write(&mm->mmap_sem);
  273. return retval;
  274. }
  275. static long vma_compute_subtree_gap(struct vm_area_struct *vma)
  276. {
  277. unsigned long max, subtree_gap;
  278. max = vma->vm_start;
  279. if (vma->vm_prev)
  280. max -= vma->vm_prev->vm_end;
  281. if (vma->vm_rb.rb_left) {
  282. subtree_gap = rb_entry(vma->vm_rb.rb_left,
  283. struct vm_area_struct, vm_rb)->rb_subtree_gap;
  284. if (subtree_gap > max)
  285. max = subtree_gap;
  286. }
  287. if (vma->vm_rb.rb_right) {
  288. subtree_gap = rb_entry(vma->vm_rb.rb_right,
  289. struct vm_area_struct, vm_rb)->rb_subtree_gap;
  290. if (subtree_gap > max)
  291. max = subtree_gap;
  292. }
  293. return max;
  294. }
  295. #ifdef CONFIG_DEBUG_VM_RB
  296. static int browse_rb(struct rb_root *root)
  297. {
  298. int i = 0, j, bug = 0;
  299. struct rb_node *nd, *pn = NULL;
  300. unsigned long prev = 0, pend = 0;
  301. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  302. struct vm_area_struct *vma;
  303. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  304. if (vma->vm_start < prev) {
  305. printk("vm_start %lx prev %lx\n", vma->vm_start, prev);
  306. bug = 1;
  307. }
  308. if (vma->vm_start < pend) {
  309. printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
  310. bug = 1;
  311. }
  312. if (vma->vm_start > vma->vm_end) {
  313. printk("vm_end %lx < vm_start %lx\n",
  314. vma->vm_end, vma->vm_start);
  315. bug = 1;
  316. }
  317. if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
  318. printk("free gap %lx, correct %lx\n",
  319. vma->rb_subtree_gap,
  320. vma_compute_subtree_gap(vma));
  321. bug = 1;
  322. }
  323. i++;
  324. pn = nd;
  325. prev = vma->vm_start;
  326. pend = vma->vm_end;
  327. }
  328. j = 0;
  329. for (nd = pn; nd; nd = rb_prev(nd))
  330. j++;
  331. if (i != j) {
  332. printk("backwards %d, forwards %d\n", j, i);
  333. bug = 1;
  334. }
  335. return bug ? -1 : i;
  336. }
  337. static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
  338. {
  339. struct rb_node *nd;
  340. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  341. struct vm_area_struct *vma;
  342. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  343. BUG_ON(vma != ignore &&
  344. vma->rb_subtree_gap != vma_compute_subtree_gap(vma));
  345. }
  346. }
  347. void validate_mm(struct mm_struct *mm)
  348. {
  349. int bug = 0;
  350. int i = 0;
  351. unsigned long highest_address = 0;
  352. struct vm_area_struct *vma = mm->mmap;
  353. while (vma) {
  354. struct anon_vma_chain *avc;
  355. vma_lock_anon_vma(vma);
  356. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  357. anon_vma_interval_tree_verify(avc);
  358. vma_unlock_anon_vma(vma);
  359. highest_address = vma->vm_end;
  360. vma = vma->vm_next;
  361. i++;
  362. }
  363. if (i != mm->map_count) {
  364. printk("map_count %d vm_next %d\n", mm->map_count, i);
  365. bug = 1;
  366. }
  367. if (highest_address != mm->highest_vm_end) {
  368. printk("mm->highest_vm_end %lx, found %lx\n",
  369. mm->highest_vm_end, highest_address);
  370. bug = 1;
  371. }
  372. i = browse_rb(&mm->mm_rb);
  373. if (i != mm->map_count) {
  374. printk("map_count %d rb %d\n", mm->map_count, i);
  375. bug = 1;
  376. }
  377. BUG_ON(bug);
  378. }
  379. #else
  380. #define validate_mm_rb(root, ignore) do { } while (0)
  381. #define validate_mm(mm) do { } while (0)
  382. #endif
  383. RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
  384. unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
  385. /*
  386. * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
  387. * vma->vm_prev->vm_end values changed, without modifying the vma's position
  388. * in the rbtree.
  389. */
  390. static void vma_gap_update(struct vm_area_struct *vma)
  391. {
  392. /*
  393. * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
  394. * function that does exacltly what we want.
  395. */
  396. vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
  397. }
  398. static inline void vma_rb_insert(struct vm_area_struct *vma,
  399. struct rb_root *root)
  400. {
  401. /* All rb_subtree_gap values must be consistent prior to insertion */
  402. validate_mm_rb(root, NULL);
  403. rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
  404. }
  405. static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
  406. {
  407. /*
  408. * All rb_subtree_gap values must be consistent prior to erase,
  409. * with the possible exception of the vma being erased.
  410. */
  411. validate_mm_rb(root, vma);
  412. /*
  413. * Note rb_erase_augmented is a fairly large inline function,
  414. * so make sure we instantiate it only once with our desired
  415. * augmented rbtree callbacks.
  416. */
  417. rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
  418. }
  419. /*
  420. * vma has some anon_vma assigned, and is already inserted on that
  421. * anon_vma's interval trees.
  422. *
  423. * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
  424. * vma must be removed from the anon_vma's interval trees using
  425. * anon_vma_interval_tree_pre_update_vma().
  426. *
  427. * After the update, the vma will be reinserted using
  428. * anon_vma_interval_tree_post_update_vma().
  429. *
  430. * The entire update must be protected by exclusive mmap_sem and by
  431. * the root anon_vma's mutex.
  432. */
  433. static inline void
  434. anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
  435. {
  436. struct anon_vma_chain *avc;
  437. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  438. anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
  439. }
  440. static inline void
  441. anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
  442. {
  443. struct anon_vma_chain *avc;
  444. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  445. anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
  446. }
  447. static int find_vma_links(struct mm_struct *mm, unsigned long addr,
  448. unsigned long end, struct vm_area_struct **pprev,
  449. struct rb_node ***rb_link, struct rb_node **rb_parent)
  450. {
  451. struct rb_node **__rb_link, *__rb_parent, *rb_prev;
  452. __rb_link = &mm->mm_rb.rb_node;
  453. rb_prev = __rb_parent = NULL;
  454. while (*__rb_link) {
  455. struct vm_area_struct *vma_tmp;
  456. __rb_parent = *__rb_link;
  457. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  458. if (vma_tmp->vm_end > addr) {
  459. /* Fail if an existing vma overlaps the area */
  460. if (vma_tmp->vm_start < end)
  461. return -ENOMEM;
  462. __rb_link = &__rb_parent->rb_left;
  463. } else {
  464. rb_prev = __rb_parent;
  465. __rb_link = &__rb_parent->rb_right;
  466. }
  467. }
  468. *pprev = NULL;
  469. if (rb_prev)
  470. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  471. *rb_link = __rb_link;
  472. *rb_parent = __rb_parent;
  473. return 0;
  474. }
  475. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  476. struct rb_node **rb_link, struct rb_node *rb_parent)
  477. {
  478. /* Update tracking information for the gap following the new vma. */
  479. if (vma->vm_next)
  480. vma_gap_update(vma->vm_next);
  481. else
  482. mm->highest_vm_end = vma->vm_end;
  483. /*
  484. * vma->vm_prev wasn't known when we followed the rbtree to find the
  485. * correct insertion point for that vma. As a result, we could not
  486. * update the vma vm_rb parents rb_subtree_gap values on the way down.
  487. * So, we first insert the vma with a zero rb_subtree_gap value
  488. * (to be consistent with what we did on the way down), and then
  489. * immediately update the gap to the correct value. Finally we
  490. * rebalance the rbtree after all augmented values have been set.
  491. */
  492. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  493. vma->rb_subtree_gap = 0;
  494. vma_gap_update(vma);
  495. vma_rb_insert(vma, &mm->mm_rb);
  496. }
  497. static void __vma_link_file(struct vm_area_struct *vma)
  498. {
  499. struct file *file;
  500. file = vma->vm_file;
  501. if (file) {
  502. struct address_space *mapping = file->f_mapping;
  503. if (vma->vm_flags & VM_DENYWRITE)
  504. atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
  505. if (vma->vm_flags & VM_SHARED)
  506. mapping->i_mmap_writable++;
  507. flush_dcache_mmap_lock(mapping);
  508. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  509. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  510. else
  511. vma_interval_tree_insert(vma, &mapping->i_mmap);
  512. flush_dcache_mmap_unlock(mapping);
  513. }
  514. }
  515. static void
  516. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  517. struct vm_area_struct *prev, struct rb_node **rb_link,
  518. struct rb_node *rb_parent)
  519. {
  520. __vma_link_list(mm, vma, prev, rb_parent);
  521. __vma_link_rb(mm, vma, rb_link, rb_parent);
  522. }
  523. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  524. struct vm_area_struct *prev, struct rb_node **rb_link,
  525. struct rb_node *rb_parent)
  526. {
  527. struct address_space *mapping = NULL;
  528. if (vma->vm_file)
  529. mapping = vma->vm_file->f_mapping;
  530. if (mapping)
  531. mutex_lock(&mapping->i_mmap_mutex);
  532. __vma_link(mm, vma, prev, rb_link, rb_parent);
  533. __vma_link_file(vma);
  534. if (mapping)
  535. mutex_unlock(&mapping->i_mmap_mutex);
  536. mm->map_count++;
  537. validate_mm(mm);
  538. }
  539. /*
  540. * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
  541. * mm's list and rbtree. It has already been inserted into the interval tree.
  542. */
  543. static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  544. {
  545. struct vm_area_struct *prev;
  546. struct rb_node **rb_link, *rb_parent;
  547. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  548. &prev, &rb_link, &rb_parent))
  549. BUG();
  550. __vma_link(mm, vma, prev, rb_link, rb_parent);
  551. mm->map_count++;
  552. }
  553. static inline void
  554. __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
  555. struct vm_area_struct *prev)
  556. {
  557. struct vm_area_struct *next;
  558. vma_rb_erase(vma, &mm->mm_rb);
  559. prev->vm_next = next = vma->vm_next;
  560. if (next)
  561. next->vm_prev = prev;
  562. if (mm->mmap_cache == vma)
  563. mm->mmap_cache = prev;
  564. }
  565. /*
  566. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  567. * is already present in an i_mmap tree without adjusting the tree.
  568. * The following helper function should be used when such adjustments
  569. * are necessary. The "insert" vma (if any) is to be inserted
  570. * before we drop the necessary locks.
  571. */
  572. int vma_adjust(struct vm_area_struct *vma, unsigned long start,
  573. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  574. {
  575. struct mm_struct *mm = vma->vm_mm;
  576. struct vm_area_struct *next = vma->vm_next;
  577. struct vm_area_struct *importer = NULL;
  578. struct address_space *mapping = NULL;
  579. struct rb_root *root = NULL;
  580. struct anon_vma *anon_vma = NULL;
  581. struct file *file = vma->vm_file;
  582. bool start_changed = false, end_changed = false;
  583. long adjust_next = 0;
  584. int remove_next = 0;
  585. if (next && !insert) {
  586. struct vm_area_struct *exporter = NULL;
  587. if (end >= next->vm_end) {
  588. /*
  589. * vma expands, overlapping all the next, and
  590. * perhaps the one after too (mprotect case 6).
  591. */
  592. again: remove_next = 1 + (end > next->vm_end);
  593. end = next->vm_end;
  594. exporter = next;
  595. importer = vma;
  596. } else if (end > next->vm_start) {
  597. /*
  598. * vma expands, overlapping part of the next:
  599. * mprotect case 5 shifting the boundary up.
  600. */
  601. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  602. exporter = next;
  603. importer = vma;
  604. } else if (end < vma->vm_end) {
  605. /*
  606. * vma shrinks, and !insert tells it's not
  607. * split_vma inserting another: so it must be
  608. * mprotect case 4 shifting the boundary down.
  609. */
  610. adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
  611. exporter = vma;
  612. importer = next;
  613. }
  614. /*
  615. * Easily overlooked: when mprotect shifts the boundary,
  616. * make sure the expanding vma has anon_vma set if the
  617. * shrinking vma had, to cover any anon pages imported.
  618. */
  619. if (exporter && exporter->anon_vma && !importer->anon_vma) {
  620. if (anon_vma_clone(importer, exporter))
  621. return -ENOMEM;
  622. importer->anon_vma = exporter->anon_vma;
  623. }
  624. }
  625. if (file) {
  626. mapping = file->f_mapping;
  627. if (!(vma->vm_flags & VM_NONLINEAR)) {
  628. root = &mapping->i_mmap;
  629. uprobe_munmap(vma, vma->vm_start, vma->vm_end);
  630. if (adjust_next)
  631. uprobe_munmap(next, next->vm_start,
  632. next->vm_end);
  633. }
  634. mutex_lock(&mapping->i_mmap_mutex);
  635. if (insert) {
  636. /*
  637. * Put into interval tree now, so instantiated pages
  638. * are visible to arm/parisc __flush_dcache_page
  639. * throughout; but we cannot insert into address
  640. * space until vma start or end is updated.
  641. */
  642. __vma_link_file(insert);
  643. }
  644. }
  645. vma_adjust_trans_huge(vma, start, end, adjust_next);
  646. anon_vma = vma->anon_vma;
  647. if (!anon_vma && adjust_next)
  648. anon_vma = next->anon_vma;
  649. if (anon_vma) {
  650. VM_BUG_ON(adjust_next && next->anon_vma &&
  651. anon_vma != next->anon_vma);
  652. anon_vma_lock_write(anon_vma);
  653. anon_vma_interval_tree_pre_update_vma(vma);
  654. if (adjust_next)
  655. anon_vma_interval_tree_pre_update_vma(next);
  656. }
  657. if (root) {
  658. flush_dcache_mmap_lock(mapping);
  659. vma_interval_tree_remove(vma, root);
  660. if (adjust_next)
  661. vma_interval_tree_remove(next, root);
  662. }
  663. if (start != vma->vm_start) {
  664. vma->vm_start = start;
  665. start_changed = true;
  666. }
  667. if (end != vma->vm_end) {
  668. vma->vm_end = end;
  669. end_changed = true;
  670. }
  671. vma->vm_pgoff = pgoff;
  672. if (adjust_next) {
  673. next->vm_start += adjust_next << PAGE_SHIFT;
  674. next->vm_pgoff += adjust_next;
  675. }
  676. if (root) {
  677. if (adjust_next)
  678. vma_interval_tree_insert(next, root);
  679. vma_interval_tree_insert(vma, root);
  680. flush_dcache_mmap_unlock(mapping);
  681. }
  682. if (remove_next) {
  683. /*
  684. * vma_merge has merged next into vma, and needs
  685. * us to remove next before dropping the locks.
  686. */
  687. __vma_unlink(mm, next, vma);
  688. if (file)
  689. __remove_shared_vm_struct(next, file, mapping);
  690. } else if (insert) {
  691. /*
  692. * split_vma has split insert from vma, and needs
  693. * us to insert it before dropping the locks
  694. * (it may either follow vma or precede it).
  695. */
  696. __insert_vm_struct(mm, insert);
  697. } else {
  698. if (start_changed)
  699. vma_gap_update(vma);
  700. if (end_changed) {
  701. if (!next)
  702. mm->highest_vm_end = end;
  703. else if (!adjust_next)
  704. vma_gap_update(next);
  705. }
  706. }
  707. if (anon_vma) {
  708. anon_vma_interval_tree_post_update_vma(vma);
  709. if (adjust_next)
  710. anon_vma_interval_tree_post_update_vma(next);
  711. anon_vma_unlock(anon_vma);
  712. }
  713. if (mapping)
  714. mutex_unlock(&mapping->i_mmap_mutex);
  715. if (root) {
  716. uprobe_mmap(vma);
  717. if (adjust_next)
  718. uprobe_mmap(next);
  719. }
  720. if (remove_next) {
  721. if (file) {
  722. uprobe_munmap(next, next->vm_start, next->vm_end);
  723. fput(file);
  724. }
  725. if (next->anon_vma)
  726. anon_vma_merge(vma, next);
  727. mm->map_count--;
  728. mpol_put(vma_policy(next));
  729. kmem_cache_free(vm_area_cachep, next);
  730. /*
  731. * In mprotect's case 6 (see comments on vma_merge),
  732. * we must remove another next too. It would clutter
  733. * up the code too much to do both in one go.
  734. */
  735. next = vma->vm_next;
  736. if (remove_next == 2)
  737. goto again;
  738. else if (next)
  739. vma_gap_update(next);
  740. else
  741. mm->highest_vm_end = end;
  742. }
  743. if (insert && file)
  744. uprobe_mmap(insert);
  745. validate_mm(mm);
  746. return 0;
  747. }
  748. /*
  749. * If the vma has a ->close operation then the driver probably needs to release
  750. * per-vma resources, so we don't attempt to merge those.
  751. */
  752. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  753. struct file *file, unsigned long vm_flags)
  754. {
  755. if (vma->vm_flags ^ vm_flags)
  756. return 0;
  757. if (vma->vm_file != file)
  758. return 0;
  759. if (vma->vm_ops && vma->vm_ops->close)
  760. return 0;
  761. return 1;
  762. }
  763. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  764. struct anon_vma *anon_vma2,
  765. struct vm_area_struct *vma)
  766. {
  767. /*
  768. * The list_is_singular() test is to avoid merging VMA cloned from
  769. * parents. This can improve scalability caused by anon_vma lock.
  770. */
  771. if ((!anon_vma1 || !anon_vma2) && (!vma ||
  772. list_is_singular(&vma->anon_vma_chain)))
  773. return 1;
  774. return anon_vma1 == anon_vma2;
  775. }
  776. /*
  777. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  778. * in front of (at a lower virtual address and file offset than) the vma.
  779. *
  780. * We cannot merge two vmas if they have differently assigned (non-NULL)
  781. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  782. *
  783. * We don't check here for the merged mmap wrapping around the end of pagecache
  784. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  785. * wrap, nor mmaps which cover the final page at index -1UL.
  786. */
  787. static int
  788. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  789. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  790. {
  791. if (is_mergeable_vma(vma, file, vm_flags) &&
  792. is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
  793. if (vma->vm_pgoff == vm_pgoff)
  794. return 1;
  795. }
  796. return 0;
  797. }
  798. /*
  799. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  800. * beyond (at a higher virtual address and file offset than) the vma.
  801. *
  802. * We cannot merge two vmas if they have differently assigned (non-NULL)
  803. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  804. */
  805. static int
  806. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  807. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  808. {
  809. if (is_mergeable_vma(vma, file, vm_flags) &&
  810. is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
  811. pgoff_t vm_pglen;
  812. vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  813. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. /*
  819. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  820. * whether that can be merged with its predecessor or its successor.
  821. * Or both (it neatly fills a hole).
  822. *
  823. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  824. * certain not to be mapped by the time vma_merge is called; but when
  825. * called for mprotect, it is certain to be already mapped (either at
  826. * an offset within prev, or at the start of next), and the flags of
  827. * this area are about to be changed to vm_flags - and the no-change
  828. * case has already been eliminated.
  829. *
  830. * The following mprotect cases have to be considered, where AAAA is
  831. * the area passed down from mprotect_fixup, never extending beyond one
  832. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  833. *
  834. * AAAA AAAA AAAA AAAA
  835. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  836. * cannot merge might become might become might become
  837. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  838. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  839. * mremap move: PPPPNNNNNNNN 8
  840. * AAAA
  841. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  842. * might become case 1 below case 2 below case 3 below
  843. *
  844. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  845. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  846. */
  847. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  848. struct vm_area_struct *prev, unsigned long addr,
  849. unsigned long end, unsigned long vm_flags,
  850. struct anon_vma *anon_vma, struct file *file,
  851. pgoff_t pgoff, struct mempolicy *policy)
  852. {
  853. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  854. struct vm_area_struct *area, *next;
  855. int err;
  856. /*
  857. * We later require that vma->vm_flags == vm_flags,
  858. * so this tests vma->vm_flags & VM_SPECIAL, too.
  859. */
  860. if (vm_flags & VM_SPECIAL)
  861. return NULL;
  862. if (prev)
  863. next = prev->vm_next;
  864. else
  865. next = mm->mmap;
  866. area = next;
  867. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  868. next = next->vm_next;
  869. /*
  870. * Can it merge with the predecessor?
  871. */
  872. if (prev && prev->vm_end == addr &&
  873. mpol_equal(vma_policy(prev), policy) &&
  874. can_vma_merge_after(prev, vm_flags,
  875. anon_vma, file, pgoff)) {
  876. /*
  877. * OK, it can. Can we now merge in the successor as well?
  878. */
  879. if (next && end == next->vm_start &&
  880. mpol_equal(policy, vma_policy(next)) &&
  881. can_vma_merge_before(next, vm_flags,
  882. anon_vma, file, pgoff+pglen) &&
  883. is_mergeable_anon_vma(prev->anon_vma,
  884. next->anon_vma, NULL)) {
  885. /* cases 1, 6 */
  886. err = vma_adjust(prev, prev->vm_start,
  887. next->vm_end, prev->vm_pgoff, NULL);
  888. } else /* cases 2, 5, 7 */
  889. err = vma_adjust(prev, prev->vm_start,
  890. end, prev->vm_pgoff, NULL);
  891. if (err)
  892. return NULL;
  893. khugepaged_enter_vma_merge(prev);
  894. return prev;
  895. }
  896. /*
  897. * Can this new request be merged in front of next?
  898. */
  899. if (next && end == next->vm_start &&
  900. mpol_equal(policy, vma_policy(next)) &&
  901. can_vma_merge_before(next, vm_flags,
  902. anon_vma, file, pgoff+pglen)) {
  903. if (prev && addr < prev->vm_end) /* case 4 */
  904. err = vma_adjust(prev, prev->vm_start,
  905. addr, prev->vm_pgoff, NULL);
  906. else /* cases 3, 8 */
  907. err = vma_adjust(area, addr, next->vm_end,
  908. next->vm_pgoff - pglen, NULL);
  909. if (err)
  910. return NULL;
  911. khugepaged_enter_vma_merge(area);
  912. return area;
  913. }
  914. return NULL;
  915. }
  916. /*
  917. * Rough compatbility check to quickly see if it's even worth looking
  918. * at sharing an anon_vma.
  919. *
  920. * They need to have the same vm_file, and the flags can only differ
  921. * in things that mprotect may change.
  922. *
  923. * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
  924. * we can merge the two vma's. For example, we refuse to merge a vma if
  925. * there is a vm_ops->close() function, because that indicates that the
  926. * driver is doing some kind of reference counting. But that doesn't
  927. * really matter for the anon_vma sharing case.
  928. */
  929. static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
  930. {
  931. return a->vm_end == b->vm_start &&
  932. mpol_equal(vma_policy(a), vma_policy(b)) &&
  933. a->vm_file == b->vm_file &&
  934. !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
  935. b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
  936. }
  937. /*
  938. * Do some basic sanity checking to see if we can re-use the anon_vma
  939. * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
  940. * the same as 'old', the other will be the new one that is trying
  941. * to share the anon_vma.
  942. *
  943. * NOTE! This runs with mm_sem held for reading, so it is possible that
  944. * the anon_vma of 'old' is concurrently in the process of being set up
  945. * by another page fault trying to merge _that_. But that's ok: if it
  946. * is being set up, that automatically means that it will be a singleton
  947. * acceptable for merging, so we can do all of this optimistically. But
  948. * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
  949. *
  950. * IOW: that the "list_is_singular()" test on the anon_vma_chain only
  951. * matters for the 'stable anon_vma' case (ie the thing we want to avoid
  952. * is to return an anon_vma that is "complex" due to having gone through
  953. * a fork).
  954. *
  955. * We also make sure that the two vma's are compatible (adjacent,
  956. * and with the same memory policies). That's all stable, even with just
  957. * a read lock on the mm_sem.
  958. */
  959. static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
  960. {
  961. if (anon_vma_compatible(a, b)) {
  962. struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
  963. if (anon_vma && list_is_singular(&old->anon_vma_chain))
  964. return anon_vma;
  965. }
  966. return NULL;
  967. }
  968. /*
  969. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  970. * neighbouring vmas for a suitable anon_vma, before it goes off
  971. * to allocate a new anon_vma. It checks because a repetitive
  972. * sequence of mprotects and faults may otherwise lead to distinct
  973. * anon_vmas being allocated, preventing vma merge in subsequent
  974. * mprotect.
  975. */
  976. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  977. {
  978. struct anon_vma *anon_vma;
  979. struct vm_area_struct *near;
  980. near = vma->vm_next;
  981. if (!near)
  982. goto try_prev;
  983. anon_vma = reusable_anon_vma(near, vma, near);
  984. if (anon_vma)
  985. return anon_vma;
  986. try_prev:
  987. near = vma->vm_prev;
  988. if (!near)
  989. goto none;
  990. anon_vma = reusable_anon_vma(near, near, vma);
  991. if (anon_vma)
  992. return anon_vma;
  993. none:
  994. /*
  995. * There's no absolute need to look only at touching neighbours:
  996. * we could search further afield for "compatible" anon_vmas.
  997. * But it would probably just be a waste of time searching,
  998. * or lead to too many vmas hanging off the same anon_vma.
  999. * We're trying to allow mprotect remerging later on,
  1000. * not trying to minimize memory used for anon_vmas.
  1001. */
  1002. return NULL;
  1003. }
  1004. #ifdef CONFIG_PROC_FS
  1005. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  1006. struct file *file, long pages)
  1007. {
  1008. const unsigned long stack_flags
  1009. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  1010. mm->total_vm += pages;
  1011. if (file) {
  1012. mm->shared_vm += pages;
  1013. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  1014. mm->exec_vm += pages;
  1015. } else if (flags & stack_flags)
  1016. mm->stack_vm += pages;
  1017. }
  1018. #endif /* CONFIG_PROC_FS */
  1019. /*
  1020. * If a hint addr is less than mmap_min_addr change hint to be as
  1021. * low as possible but still greater than mmap_min_addr
  1022. */
  1023. static inline unsigned long round_hint_to_min(unsigned long hint)
  1024. {
  1025. hint &= PAGE_MASK;
  1026. if (((void *)hint != NULL) &&
  1027. (hint < mmap_min_addr))
  1028. return PAGE_ALIGN(mmap_min_addr);
  1029. return hint;
  1030. }
  1031. /*
  1032. * The caller must hold down_write(&current->mm->mmap_sem).
  1033. */
  1034. unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  1035. unsigned long len, unsigned long prot,
  1036. unsigned long flags, unsigned long pgoff)
  1037. {
  1038. struct mm_struct * mm = current->mm;
  1039. struct inode *inode;
  1040. vm_flags_t vm_flags;
  1041. /*
  1042. * Does the application expect PROT_READ to imply PROT_EXEC?
  1043. *
  1044. * (the exception is when the underlying filesystem is noexec
  1045. * mounted, in which case we dont add PROT_EXEC.)
  1046. */
  1047. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  1048. if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
  1049. prot |= PROT_EXEC;
  1050. if (!len)
  1051. return -EINVAL;
  1052. if (!(flags & MAP_FIXED))
  1053. addr = round_hint_to_min(addr);
  1054. /* Careful about overflows.. */
  1055. len = PAGE_ALIGN(len);
  1056. if (!len)
  1057. return -ENOMEM;
  1058. /* offset overflow? */
  1059. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  1060. return -EOVERFLOW;
  1061. /* Too many mappings? */
  1062. if (mm->map_count > sysctl_max_map_count)
  1063. return -ENOMEM;
  1064. /* Obtain the address to map to. we verify (or select) it and ensure
  1065. * that it represents a valid section of the address space.
  1066. */
  1067. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  1068. if (addr & ~PAGE_MASK)
  1069. return addr;
  1070. /* Do simple checking here so the lower-level routines won't have
  1071. * to. we assume access permissions have been handled by the open
  1072. * of the memory object, so we don't do any here.
  1073. */
  1074. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  1075. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  1076. if (flags & MAP_LOCKED)
  1077. if (!can_do_mlock())
  1078. return -EPERM;
  1079. /* mlock MCL_FUTURE? */
  1080. if (vm_flags & VM_LOCKED) {
  1081. unsigned long locked, lock_limit;
  1082. locked = len >> PAGE_SHIFT;
  1083. locked += mm->locked_vm;
  1084. lock_limit = rlimit(RLIMIT_MEMLOCK);
  1085. lock_limit >>= PAGE_SHIFT;
  1086. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1087. return -EAGAIN;
  1088. }
  1089. inode = file ? file->f_path.dentry->d_inode : NULL;
  1090. if (file) {
  1091. switch (flags & MAP_TYPE) {
  1092. case MAP_SHARED:
  1093. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  1094. return -EACCES;
  1095. /*
  1096. * Make sure we don't allow writing to an append-only
  1097. * file..
  1098. */
  1099. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  1100. return -EACCES;
  1101. /*
  1102. * Make sure there are no mandatory locks on the file.
  1103. */
  1104. if (locks_verify_locked(inode))
  1105. return -EAGAIN;
  1106. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1107. if (!(file->f_mode & FMODE_WRITE))
  1108. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  1109. /* fall through */
  1110. case MAP_PRIVATE:
  1111. if (!(file->f_mode & FMODE_READ))
  1112. return -EACCES;
  1113. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  1114. if (vm_flags & VM_EXEC)
  1115. return -EPERM;
  1116. vm_flags &= ~VM_MAYEXEC;
  1117. }
  1118. if (!file->f_op || !file->f_op->mmap)
  1119. return -ENODEV;
  1120. break;
  1121. default:
  1122. return -EINVAL;
  1123. }
  1124. } else {
  1125. switch (flags & MAP_TYPE) {
  1126. case MAP_SHARED:
  1127. /*
  1128. * Ignore pgoff.
  1129. */
  1130. pgoff = 0;
  1131. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1132. break;
  1133. case MAP_PRIVATE:
  1134. /*
  1135. * Set pgoff according to addr for anon_vma.
  1136. */
  1137. pgoff = addr >> PAGE_SHIFT;
  1138. break;
  1139. default:
  1140. return -EINVAL;
  1141. }
  1142. }
  1143. return mmap_region(file, addr, len, flags, vm_flags, pgoff);
  1144. }
  1145. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1146. unsigned long, prot, unsigned long, flags,
  1147. unsigned long, fd, unsigned long, pgoff)
  1148. {
  1149. struct file *file = NULL;
  1150. unsigned long retval = -EBADF;
  1151. if (!(flags & MAP_ANONYMOUS)) {
  1152. audit_mmap_fd(fd, flags);
  1153. if (unlikely(flags & MAP_HUGETLB))
  1154. return -EINVAL;
  1155. file = fget(fd);
  1156. if (!file)
  1157. goto out;
  1158. } else if (flags & MAP_HUGETLB) {
  1159. struct user_struct *user = NULL;
  1160. /*
  1161. * VM_NORESERVE is used because the reservations will be
  1162. * taken when vm_ops->mmap() is called
  1163. * A dummy user value is used because we are not locking
  1164. * memory so no accounting is necessary
  1165. */
  1166. file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
  1167. VM_NORESERVE,
  1168. &user, HUGETLB_ANONHUGE_INODE,
  1169. (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  1170. if (IS_ERR(file))
  1171. return PTR_ERR(file);
  1172. }
  1173. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1174. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1175. if (file)
  1176. fput(file);
  1177. out:
  1178. return retval;
  1179. }
  1180. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1181. struct mmap_arg_struct {
  1182. unsigned long addr;
  1183. unsigned long len;
  1184. unsigned long prot;
  1185. unsigned long flags;
  1186. unsigned long fd;
  1187. unsigned long offset;
  1188. };
  1189. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1190. {
  1191. struct mmap_arg_struct a;
  1192. if (copy_from_user(&a, arg, sizeof(a)))
  1193. return -EFAULT;
  1194. if (a.offset & ~PAGE_MASK)
  1195. return -EINVAL;
  1196. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1197. a.offset >> PAGE_SHIFT);
  1198. }
  1199. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1200. /*
  1201. * Some shared mappigns will want the pages marked read-only
  1202. * to track write events. If so, we'll downgrade vm_page_prot
  1203. * to the private version (using protection_map[] without the
  1204. * VM_SHARED bit).
  1205. */
  1206. int vma_wants_writenotify(struct vm_area_struct *vma)
  1207. {
  1208. vm_flags_t vm_flags = vma->vm_flags;
  1209. /* If it was private or non-writable, the write bit is already clear */
  1210. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  1211. return 0;
  1212. /* The backer wishes to know when pages are first written to? */
  1213. if (vma->vm_ops && vma->vm_ops->page_mkwrite)
  1214. return 1;
  1215. /* The open routine did something to the protections already? */
  1216. if (pgprot_val(vma->vm_page_prot) !=
  1217. pgprot_val(vm_get_page_prot(vm_flags)))
  1218. return 0;
  1219. /* Specialty mapping? */
  1220. if (vm_flags & VM_PFNMAP)
  1221. return 0;
  1222. /* Can the mapping track the dirty pages? */
  1223. return vma->vm_file && vma->vm_file->f_mapping &&
  1224. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  1225. }
  1226. /*
  1227. * We account for memory if it's a private writeable mapping,
  1228. * not hugepages and VM_NORESERVE wasn't set.
  1229. */
  1230. static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
  1231. {
  1232. /*
  1233. * hugetlb has its own accounting separate from the core VM
  1234. * VM_HUGETLB may not be set yet so we cannot check for that flag.
  1235. */
  1236. if (file && is_file_hugepages(file))
  1237. return 0;
  1238. return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
  1239. }
  1240. unsigned long mmap_region(struct file *file, unsigned long addr,
  1241. unsigned long len, unsigned long flags,
  1242. vm_flags_t vm_flags, unsigned long pgoff)
  1243. {
  1244. struct mm_struct *mm = current->mm;
  1245. struct vm_area_struct *vma, *prev;
  1246. int correct_wcount = 0;
  1247. int error;
  1248. struct rb_node **rb_link, *rb_parent;
  1249. unsigned long charged = 0;
  1250. struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
  1251. /* Clear old maps */
  1252. error = -ENOMEM;
  1253. munmap_back:
  1254. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  1255. if (do_munmap(mm, addr, len))
  1256. return -ENOMEM;
  1257. goto munmap_back;
  1258. }
  1259. /* Check against address space limit. */
  1260. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1261. return -ENOMEM;
  1262. /*
  1263. * Set 'VM_NORESERVE' if we should not account for the
  1264. * memory use of this mapping.
  1265. */
  1266. if ((flags & MAP_NORESERVE)) {
  1267. /* We honor MAP_NORESERVE if allowed to overcommit */
  1268. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  1269. vm_flags |= VM_NORESERVE;
  1270. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  1271. if (file && is_file_hugepages(file))
  1272. vm_flags |= VM_NORESERVE;
  1273. }
  1274. /*
  1275. * Private writable mapping: check memory availability
  1276. */
  1277. if (accountable_mapping(file, vm_flags)) {
  1278. charged = len >> PAGE_SHIFT;
  1279. if (security_vm_enough_memory_mm(mm, charged))
  1280. return -ENOMEM;
  1281. vm_flags |= VM_ACCOUNT;
  1282. }
  1283. /*
  1284. * Can we just expand an old mapping?
  1285. */
  1286. vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
  1287. if (vma)
  1288. goto out;
  1289. /*
  1290. * Determine the object being mapped and call the appropriate
  1291. * specific mapper. the address has already been validated, but
  1292. * not unmapped, but the maps are removed from the list.
  1293. */
  1294. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1295. if (!vma) {
  1296. error = -ENOMEM;
  1297. goto unacct_error;
  1298. }
  1299. vma->vm_mm = mm;
  1300. vma->vm_start = addr;
  1301. vma->vm_end = addr + len;
  1302. vma->vm_flags = vm_flags;
  1303. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1304. vma->vm_pgoff = pgoff;
  1305. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1306. error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
  1307. if (file) {
  1308. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1309. goto free_vma;
  1310. if (vm_flags & VM_DENYWRITE) {
  1311. error = deny_write_access(file);
  1312. if (error)
  1313. goto free_vma;
  1314. correct_wcount = 1;
  1315. }
  1316. vma->vm_file = get_file(file);
  1317. error = file->f_op->mmap(file, vma);
  1318. if (error)
  1319. goto unmap_and_free_vma;
  1320. /* Can addr have changed??
  1321. *
  1322. * Answer: Yes, several device drivers can do it in their
  1323. * f_op->mmap method. -DaveM
  1324. * Bug: If addr is changed, prev, rb_link, rb_parent should
  1325. * be updated for vma_link()
  1326. */
  1327. WARN_ON_ONCE(addr != vma->vm_start);
  1328. addr = vma->vm_start;
  1329. pgoff = vma->vm_pgoff;
  1330. vm_flags = vma->vm_flags;
  1331. } else if (vm_flags & VM_SHARED) {
  1332. if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
  1333. goto free_vma;
  1334. error = shmem_zero_setup(vma);
  1335. if (error)
  1336. goto free_vma;
  1337. }
  1338. if (vma_wants_writenotify(vma)) {
  1339. pgprot_t pprot = vma->vm_page_prot;
  1340. /* Can vma->vm_page_prot have changed??
  1341. *
  1342. * Answer: Yes, drivers may have changed it in their
  1343. * f_op->mmap method.
  1344. *
  1345. * Ensures that vmas marked as uncached stay that way.
  1346. */
  1347. vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
  1348. if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
  1349. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1350. }
  1351. vma_link(mm, vma, prev, rb_link, rb_parent);
  1352. file = vma->vm_file;
  1353. /* Once vma denies write, undo our temporary denial count */
  1354. if (correct_wcount)
  1355. atomic_inc(&inode->i_writecount);
  1356. out:
  1357. perf_event_mmap(vma);
  1358. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1359. if (vm_flags & VM_LOCKED) {
  1360. if (!mlock_vma_pages_range(vma, addr, addr + len))
  1361. mm->locked_vm += (len >> PAGE_SHIFT);
  1362. } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
  1363. make_pages_present(addr, addr + len);
  1364. if (file)
  1365. uprobe_mmap(vma);
  1366. return addr;
  1367. unmap_and_free_vma:
  1368. if (correct_wcount)
  1369. atomic_inc(&inode->i_writecount);
  1370. vma->vm_file = NULL;
  1371. fput(file);
  1372. /* Undo any partial mapping done by a device driver. */
  1373. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1374. charged = 0;
  1375. free_vma:
  1376. kmem_cache_free(vm_area_cachep, vma);
  1377. unacct_error:
  1378. if (charged)
  1379. vm_unacct_memory(charged);
  1380. return error;
  1381. }
  1382. unsigned long unmapped_area(struct vm_unmapped_area_info *info)
  1383. {
  1384. /*
  1385. * We implement the search by looking for an rbtree node that
  1386. * immediately follows a suitable gap. That is,
  1387. * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
  1388. * - gap_end = vma->vm_start >= info->low_limit + length;
  1389. * - gap_end - gap_start >= length
  1390. */
  1391. struct mm_struct *mm = current->mm;
  1392. struct vm_area_struct *vma;
  1393. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1394. /* Adjust search length to account for worst case alignment overhead */
  1395. length = info->length + info->align_mask;
  1396. if (length < info->length)
  1397. return -ENOMEM;
  1398. /* Adjust search limits by the desired length */
  1399. if (info->high_limit < length)
  1400. return -ENOMEM;
  1401. high_limit = info->high_limit - length;
  1402. if (info->low_limit > high_limit)
  1403. return -ENOMEM;
  1404. low_limit = info->low_limit + length;
  1405. /* Check if rbtree root looks promising */
  1406. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1407. goto check_highest;
  1408. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1409. if (vma->rb_subtree_gap < length)
  1410. goto check_highest;
  1411. while (true) {
  1412. /* Visit left subtree if it looks promising */
  1413. gap_end = vma->vm_start;
  1414. if (gap_end >= low_limit && vma->vm_rb.rb_left) {
  1415. struct vm_area_struct *left =
  1416. rb_entry(vma->vm_rb.rb_left,
  1417. struct vm_area_struct, vm_rb);
  1418. if (left->rb_subtree_gap >= length) {
  1419. vma = left;
  1420. continue;
  1421. }
  1422. }
  1423. gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
  1424. check_current:
  1425. /* Check if current node has a suitable gap */
  1426. if (gap_start > high_limit)
  1427. return -ENOMEM;
  1428. if (gap_end >= low_limit && gap_end - gap_start >= length)
  1429. goto found;
  1430. /* Visit right subtree if it looks promising */
  1431. if (vma->vm_rb.rb_right) {
  1432. struct vm_area_struct *right =
  1433. rb_entry(vma->vm_rb.rb_right,
  1434. struct vm_area_struct, vm_rb);
  1435. if (right->rb_subtree_gap >= length) {
  1436. vma = right;
  1437. continue;
  1438. }
  1439. }
  1440. /* Go back up the rbtree to find next candidate node */
  1441. while (true) {
  1442. struct rb_node *prev = &vma->vm_rb;
  1443. if (!rb_parent(prev))
  1444. goto check_highest;
  1445. vma = rb_entry(rb_parent(prev),
  1446. struct vm_area_struct, vm_rb);
  1447. if (prev == vma->vm_rb.rb_left) {
  1448. gap_start = vma->vm_prev->vm_end;
  1449. gap_end = vma->vm_start;
  1450. goto check_current;
  1451. }
  1452. }
  1453. }
  1454. check_highest:
  1455. /* Check highest gap, which does not precede any rbtree node */
  1456. gap_start = mm->highest_vm_end;
  1457. gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
  1458. if (gap_start > high_limit)
  1459. return -ENOMEM;
  1460. found:
  1461. /* We found a suitable gap. Clip it with the original low_limit. */
  1462. if (gap_start < info->low_limit)
  1463. gap_start = info->low_limit;
  1464. /* Adjust gap address to the desired alignment */
  1465. gap_start += (info->align_offset - gap_start) & info->align_mask;
  1466. VM_BUG_ON(gap_start + info->length > info->high_limit);
  1467. VM_BUG_ON(gap_start + info->length > gap_end);
  1468. return gap_start;
  1469. }
  1470. unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
  1471. {
  1472. struct mm_struct *mm = current->mm;
  1473. struct vm_area_struct *vma;
  1474. unsigned long length, low_limit, high_limit, gap_start, gap_end;
  1475. /* Adjust search length to account for worst case alignment overhead */
  1476. length = info->length + info->align_mask;
  1477. if (length < info->length)
  1478. return -ENOMEM;
  1479. /*
  1480. * Adjust search limits by the desired length.
  1481. * See implementation comment at top of unmapped_area().
  1482. */
  1483. gap_end = info->high_limit;
  1484. if (gap_end < length)
  1485. return -ENOMEM;
  1486. high_limit = gap_end - length;
  1487. if (info->low_limit > high_limit)
  1488. return -ENOMEM;
  1489. low_limit = info->low_limit + length;
  1490. /* Check highest gap, which does not precede any rbtree node */
  1491. gap_start = mm->highest_vm_end;
  1492. if (gap_start <= high_limit)
  1493. goto found_highest;
  1494. /* Check if rbtree root looks promising */
  1495. if (RB_EMPTY_ROOT(&mm->mm_rb))
  1496. return -ENOMEM;
  1497. vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
  1498. if (vma->rb_subtree_gap < length)
  1499. return -ENOMEM;
  1500. while (true) {
  1501. /* Visit right subtree if it looks promising */
  1502. gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
  1503. if (gap_start <= high_limit && vma->vm_rb.rb_right) {
  1504. struct vm_area_struct *right =
  1505. rb_entry(vma->vm_rb.rb_right,
  1506. struct vm_area_struct, vm_rb);
  1507. if (right->rb_subtree_gap >= length) {
  1508. vma = right;
  1509. continue;
  1510. }
  1511. }
  1512. check_current:
  1513. /* Check if current node has a suitable gap */
  1514. gap_end = vma->vm_start;
  1515. if (gap_end < low_limit)
  1516. return -ENOMEM;
  1517. if (gap_start <= high_limit && gap_end - gap_start >= length)
  1518. goto found;
  1519. /* Visit left subtree if it looks promising */
  1520. if (vma->vm_rb.rb_left) {
  1521. struct vm_area_struct *left =
  1522. rb_entry(vma->vm_rb.rb_left,
  1523. struct vm_area_struct, vm_rb);
  1524. if (left->rb_subtree_gap >= length) {
  1525. vma = left;
  1526. continue;
  1527. }
  1528. }
  1529. /* Go back up the rbtree to find next candidate node */
  1530. while (true) {
  1531. struct rb_node *prev = &vma->vm_rb;
  1532. if (!rb_parent(prev))
  1533. return -ENOMEM;
  1534. vma = rb_entry(rb_parent(prev),
  1535. struct vm_area_struct, vm_rb);
  1536. if (prev == vma->vm_rb.rb_right) {
  1537. gap_start = vma->vm_prev ?
  1538. vma->vm_prev->vm_end : 0;
  1539. goto check_current;
  1540. }
  1541. }
  1542. }
  1543. found:
  1544. /* We found a suitable gap. Clip it with the original high_limit. */
  1545. if (gap_end > info->high_limit)
  1546. gap_end = info->high_limit;
  1547. found_highest:
  1548. /* Compute highest gap address at the desired alignment */
  1549. gap_end -= info->length;
  1550. gap_end -= (gap_end - info->align_offset) & info->align_mask;
  1551. VM_BUG_ON(gap_end < info->low_limit);
  1552. VM_BUG_ON(gap_end < gap_start);
  1553. return gap_end;
  1554. }
  1555. /* Get an address range which is currently unmapped.
  1556. * For shmat() with addr=0.
  1557. *
  1558. * Ugly calling convention alert:
  1559. * Return value with the low bits set means error value,
  1560. * ie
  1561. * if (ret & ~PAGE_MASK)
  1562. * error = ret;
  1563. *
  1564. * This function "knows" that -ENOMEM has the bits set.
  1565. */
  1566. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1567. unsigned long
  1568. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1569. unsigned long len, unsigned long pgoff, unsigned long flags)
  1570. {
  1571. struct mm_struct *mm = current->mm;
  1572. struct vm_area_struct *vma;
  1573. struct vm_unmapped_area_info info;
  1574. if (len > TASK_SIZE)
  1575. return -ENOMEM;
  1576. if (flags & MAP_FIXED)
  1577. return addr;
  1578. if (addr) {
  1579. addr = PAGE_ALIGN(addr);
  1580. vma = find_vma(mm, addr);
  1581. if (TASK_SIZE - len >= addr &&
  1582. (!vma || addr + len <= vma->vm_start))
  1583. return addr;
  1584. }
  1585. info.flags = 0;
  1586. info.length = len;
  1587. info.low_limit = TASK_UNMAPPED_BASE;
  1588. info.high_limit = TASK_SIZE;
  1589. info.align_mask = 0;
  1590. return vm_unmapped_area(&info);
  1591. }
  1592. #endif
  1593. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1594. {
  1595. /*
  1596. * Is this a new hole at the lowest possible address?
  1597. */
  1598. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
  1599. mm->free_area_cache = addr;
  1600. }
  1601. /*
  1602. * This mmap-allocator allocates new areas top-down from below the
  1603. * stack's low limit (the base):
  1604. */
  1605. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1606. unsigned long
  1607. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1608. const unsigned long len, const unsigned long pgoff,
  1609. const unsigned long flags)
  1610. {
  1611. struct vm_area_struct *vma;
  1612. struct mm_struct *mm = current->mm;
  1613. unsigned long addr = addr0;
  1614. struct vm_unmapped_area_info info;
  1615. /* requested length too big for entire address space */
  1616. if (len > TASK_SIZE)
  1617. return -ENOMEM;
  1618. if (flags & MAP_FIXED)
  1619. return addr;
  1620. /* requesting a specific address */
  1621. if (addr) {
  1622. addr = PAGE_ALIGN(addr);
  1623. vma = find_vma(mm, addr);
  1624. if (TASK_SIZE - len >= addr &&
  1625. (!vma || addr + len <= vma->vm_start))
  1626. return addr;
  1627. }
  1628. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  1629. info.length = len;
  1630. info.low_limit = PAGE_SIZE;
  1631. info.high_limit = mm->mmap_base;
  1632. info.align_mask = 0;
  1633. addr = vm_unmapped_area(&info);
  1634. /*
  1635. * A failed mmap() very likely causes application failure,
  1636. * so fall back to the bottom-up function here. This scenario
  1637. * can happen with large stack limits and large mmap()
  1638. * allocations.
  1639. */
  1640. if (addr & ~PAGE_MASK) {
  1641. VM_BUG_ON(addr != -ENOMEM);
  1642. info.flags = 0;
  1643. info.low_limit = TASK_UNMAPPED_BASE;
  1644. info.high_limit = TASK_SIZE;
  1645. addr = vm_unmapped_area(&info);
  1646. }
  1647. return addr;
  1648. }
  1649. #endif
  1650. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1651. {
  1652. /*
  1653. * Is this a new hole at the highest possible address?
  1654. */
  1655. if (addr > mm->free_area_cache)
  1656. mm->free_area_cache = addr;
  1657. /* dont allow allocations above current base */
  1658. if (mm->free_area_cache > mm->mmap_base)
  1659. mm->free_area_cache = mm->mmap_base;
  1660. }
  1661. unsigned long
  1662. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1663. unsigned long pgoff, unsigned long flags)
  1664. {
  1665. unsigned long (*get_area)(struct file *, unsigned long,
  1666. unsigned long, unsigned long, unsigned long);
  1667. unsigned long error = arch_mmap_check(addr, len, flags);
  1668. if (error)
  1669. return error;
  1670. /* Careful about overflows.. */
  1671. if (len > TASK_SIZE)
  1672. return -ENOMEM;
  1673. get_area = current->mm->get_unmapped_area;
  1674. if (file && file->f_op && file->f_op->get_unmapped_area)
  1675. get_area = file->f_op->get_unmapped_area;
  1676. addr = get_area(file, addr, len, pgoff, flags);
  1677. if (IS_ERR_VALUE(addr))
  1678. return addr;
  1679. if (addr > TASK_SIZE - len)
  1680. return -ENOMEM;
  1681. if (addr & ~PAGE_MASK)
  1682. return -EINVAL;
  1683. addr = arch_rebalance_pgtables(addr, len);
  1684. error = security_mmap_addr(addr);
  1685. return error ? error : addr;
  1686. }
  1687. EXPORT_SYMBOL(get_unmapped_area);
  1688. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1689. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1690. {
  1691. struct vm_area_struct *vma = NULL;
  1692. if (WARN_ON_ONCE(!mm)) /* Remove this in linux-3.6 */
  1693. return NULL;
  1694. /* Check the cache first. */
  1695. /* (Cache hit rate is typically around 35%.) */
  1696. vma = mm->mmap_cache;
  1697. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1698. struct rb_node *rb_node;
  1699. rb_node = mm->mm_rb.rb_node;
  1700. vma = NULL;
  1701. while (rb_node) {
  1702. struct vm_area_struct *vma_tmp;
  1703. vma_tmp = rb_entry(rb_node,
  1704. struct vm_area_struct, vm_rb);
  1705. if (vma_tmp->vm_end > addr) {
  1706. vma = vma_tmp;
  1707. if (vma_tmp->vm_start <= addr)
  1708. break;
  1709. rb_node = rb_node->rb_left;
  1710. } else
  1711. rb_node = rb_node->rb_right;
  1712. }
  1713. if (vma)
  1714. mm->mmap_cache = vma;
  1715. }
  1716. return vma;
  1717. }
  1718. EXPORT_SYMBOL(find_vma);
  1719. /*
  1720. * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
  1721. */
  1722. struct vm_area_struct *
  1723. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1724. struct vm_area_struct **pprev)
  1725. {
  1726. struct vm_area_struct *vma;
  1727. vma = find_vma(mm, addr);
  1728. if (vma) {
  1729. *pprev = vma->vm_prev;
  1730. } else {
  1731. struct rb_node *rb_node = mm->mm_rb.rb_node;
  1732. *pprev = NULL;
  1733. while (rb_node) {
  1734. *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1735. rb_node = rb_node->rb_right;
  1736. }
  1737. }
  1738. return vma;
  1739. }
  1740. /*
  1741. * Verify that the stack growth is acceptable and
  1742. * update accounting. This is shared with both the
  1743. * grow-up and grow-down cases.
  1744. */
  1745. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1746. {
  1747. struct mm_struct *mm = vma->vm_mm;
  1748. struct rlimit *rlim = current->signal->rlim;
  1749. unsigned long new_start;
  1750. /* address space limit tests */
  1751. if (!may_expand_vm(mm, grow))
  1752. return -ENOMEM;
  1753. /* Stack limit test */
  1754. if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
  1755. return -ENOMEM;
  1756. /* mlock limit tests */
  1757. if (vma->vm_flags & VM_LOCKED) {
  1758. unsigned long locked;
  1759. unsigned long limit;
  1760. locked = mm->locked_vm + grow;
  1761. limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
  1762. limit >>= PAGE_SHIFT;
  1763. if (locked > limit && !capable(CAP_IPC_LOCK))
  1764. return -ENOMEM;
  1765. }
  1766. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1767. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1768. vma->vm_end - size;
  1769. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1770. return -EFAULT;
  1771. /*
  1772. * Overcommit.. This must be the final test, as it will
  1773. * update security statistics.
  1774. */
  1775. if (security_vm_enough_memory_mm(mm, grow))
  1776. return -ENOMEM;
  1777. /* Ok, everything looks good - let it rip */
  1778. if (vma->vm_flags & VM_LOCKED)
  1779. mm->locked_vm += grow;
  1780. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1781. return 0;
  1782. }
  1783. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1784. /*
  1785. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1786. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1787. */
  1788. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1789. {
  1790. int error;
  1791. if (!(vma->vm_flags & VM_GROWSUP))
  1792. return -EFAULT;
  1793. /*
  1794. * We must make sure the anon_vma is allocated
  1795. * so that the anon_vma locking is not a noop.
  1796. */
  1797. if (unlikely(anon_vma_prepare(vma)))
  1798. return -ENOMEM;
  1799. vma_lock_anon_vma(vma);
  1800. /*
  1801. * vma->vm_start/vm_end cannot change under us because the caller
  1802. * is required to hold the mmap_sem in read mode. We need the
  1803. * anon_vma lock to serialize against concurrent expand_stacks.
  1804. * Also guard against wrapping around to address 0.
  1805. */
  1806. if (address < PAGE_ALIGN(address+4))
  1807. address = PAGE_ALIGN(address+4);
  1808. else {
  1809. vma_unlock_anon_vma(vma);
  1810. return -ENOMEM;
  1811. }
  1812. error = 0;
  1813. /* Somebody else might have raced and expanded it already */
  1814. if (address > vma->vm_end) {
  1815. unsigned long size, grow;
  1816. size = address - vma->vm_start;
  1817. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1818. error = -ENOMEM;
  1819. if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
  1820. error = acct_stack_growth(vma, size, grow);
  1821. if (!error) {
  1822. /*
  1823. * vma_gap_update() doesn't support concurrent
  1824. * updates, but we only hold a shared mmap_sem
  1825. * lock here, so we need to protect against
  1826. * concurrent vma expansions.
  1827. * vma_lock_anon_vma() doesn't help here, as
  1828. * we don't guarantee that all growable vmas
  1829. * in a mm share the same root anon vma.
  1830. * So, we reuse mm->page_table_lock to guard
  1831. * against concurrent vma expansions.
  1832. */
  1833. spin_lock(&vma->vm_mm->page_table_lock);
  1834. anon_vma_interval_tree_pre_update_vma(vma);
  1835. vma->vm_end = address;
  1836. anon_vma_interval_tree_post_update_vma(vma);
  1837. if (vma->vm_next)
  1838. vma_gap_update(vma->vm_next);
  1839. else
  1840. vma->vm_mm->highest_vm_end = address;
  1841. spin_unlock(&vma->vm_mm->page_table_lock);
  1842. perf_event_mmap(vma);
  1843. }
  1844. }
  1845. }
  1846. vma_unlock_anon_vma(vma);
  1847. khugepaged_enter_vma_merge(vma);
  1848. validate_mm(vma->vm_mm);
  1849. return error;
  1850. }
  1851. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1852. /*
  1853. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1854. */
  1855. int expand_downwards(struct vm_area_struct *vma,
  1856. unsigned long address)
  1857. {
  1858. int error;
  1859. /*
  1860. * We must make sure the anon_vma is allocated
  1861. * so that the anon_vma locking is not a noop.
  1862. */
  1863. if (unlikely(anon_vma_prepare(vma)))
  1864. return -ENOMEM;
  1865. address &= PAGE_MASK;
  1866. error = security_mmap_addr(address);
  1867. if (error)
  1868. return error;
  1869. vma_lock_anon_vma(vma);
  1870. /*
  1871. * vma->vm_start/vm_end cannot change under us because the caller
  1872. * is required to hold the mmap_sem in read mode. We need the
  1873. * anon_vma lock to serialize against concurrent expand_stacks.
  1874. */
  1875. /* Somebody else might have raced and expanded it already */
  1876. if (address < vma->vm_start) {
  1877. unsigned long size, grow;
  1878. size = vma->vm_end - address;
  1879. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1880. error = -ENOMEM;
  1881. if (grow <= vma->vm_pgoff) {
  1882. error = acct_stack_growth(vma, size, grow);
  1883. if (!error) {
  1884. /*
  1885. * vma_gap_update() doesn't support concurrent
  1886. * updates, but we only hold a shared mmap_sem
  1887. * lock here, so we need to protect against
  1888. * concurrent vma expansions.
  1889. * vma_lock_anon_vma() doesn't help here, as
  1890. * we don't guarantee that all growable vmas
  1891. * in a mm share the same root anon vma.
  1892. * So, we reuse mm->page_table_lock to guard
  1893. * against concurrent vma expansions.
  1894. */
  1895. spin_lock(&vma->vm_mm->page_table_lock);
  1896. anon_vma_interval_tree_pre_update_vma(vma);
  1897. vma->vm_start = address;
  1898. vma->vm_pgoff -= grow;
  1899. anon_vma_interval_tree_post_update_vma(vma);
  1900. vma_gap_update(vma);
  1901. spin_unlock(&vma->vm_mm->page_table_lock);
  1902. perf_event_mmap(vma);
  1903. }
  1904. }
  1905. }
  1906. vma_unlock_anon_vma(vma);
  1907. khugepaged_enter_vma_merge(vma);
  1908. validate_mm(vma->vm_mm);
  1909. return error;
  1910. }
  1911. #ifdef CONFIG_STACK_GROWSUP
  1912. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1913. {
  1914. return expand_upwards(vma, address);
  1915. }
  1916. struct vm_area_struct *
  1917. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1918. {
  1919. struct vm_area_struct *vma, *prev;
  1920. addr &= PAGE_MASK;
  1921. vma = find_vma_prev(mm, addr, &prev);
  1922. if (vma && (vma->vm_start <= addr))
  1923. return vma;
  1924. if (!prev || expand_stack(prev, addr))
  1925. return NULL;
  1926. if (prev->vm_flags & VM_LOCKED) {
  1927. mlock_vma_pages_range(prev, addr, prev->vm_end);
  1928. }
  1929. return prev;
  1930. }
  1931. #else
  1932. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1933. {
  1934. return expand_downwards(vma, address);
  1935. }
  1936. struct vm_area_struct *
  1937. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1938. {
  1939. struct vm_area_struct * vma;
  1940. unsigned long start;
  1941. addr &= PAGE_MASK;
  1942. vma = find_vma(mm,addr);
  1943. if (!vma)
  1944. return NULL;
  1945. if (vma->vm_start <= addr)
  1946. return vma;
  1947. if (!(vma->vm_flags & VM_GROWSDOWN))
  1948. return NULL;
  1949. start = vma->vm_start;
  1950. if (expand_stack(vma, addr))
  1951. return NULL;
  1952. if (vma->vm_flags & VM_LOCKED) {
  1953. mlock_vma_pages_range(vma, addr, start);
  1954. }
  1955. return vma;
  1956. }
  1957. #endif
  1958. /*
  1959. * Ok - we have the memory areas we should free on the vma list,
  1960. * so release them, and do the vma updates.
  1961. *
  1962. * Called with the mm semaphore held.
  1963. */
  1964. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1965. {
  1966. unsigned long nr_accounted = 0;
  1967. /* Update high watermark before we lower total_vm */
  1968. update_hiwater_vm(mm);
  1969. do {
  1970. long nrpages = vma_pages(vma);
  1971. if (vma->vm_flags & VM_ACCOUNT)
  1972. nr_accounted += nrpages;
  1973. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1974. vma = remove_vma(vma);
  1975. } while (vma);
  1976. vm_unacct_memory(nr_accounted);
  1977. validate_mm(mm);
  1978. }
  1979. /*
  1980. * Get rid of page table information in the indicated region.
  1981. *
  1982. * Called with the mm semaphore held.
  1983. */
  1984. static void unmap_region(struct mm_struct *mm,
  1985. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1986. unsigned long start, unsigned long end)
  1987. {
  1988. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1989. struct mmu_gather tlb;
  1990. lru_add_drain();
  1991. tlb_gather_mmu(&tlb, mm, 0);
  1992. update_hiwater_rss(mm);
  1993. unmap_vmas(&tlb, vma, start, end);
  1994. free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
  1995. next ? next->vm_start : 0);
  1996. tlb_finish_mmu(&tlb, start, end);
  1997. }
  1998. /*
  1999. * Create a list of vma's touched by the unmap, removing them from the mm's
  2000. * vma list as we go..
  2001. */
  2002. static void
  2003. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  2004. struct vm_area_struct *prev, unsigned long end)
  2005. {
  2006. struct vm_area_struct **insertion_point;
  2007. struct vm_area_struct *tail_vma = NULL;
  2008. unsigned long addr;
  2009. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  2010. vma->vm_prev = NULL;
  2011. do {
  2012. vma_rb_erase(vma, &mm->mm_rb);
  2013. mm->map_count--;
  2014. tail_vma = vma;
  2015. vma = vma->vm_next;
  2016. } while (vma && vma->vm_start < end);
  2017. *insertion_point = vma;
  2018. if (vma) {
  2019. vma->vm_prev = prev;
  2020. vma_gap_update(vma);
  2021. } else
  2022. mm->highest_vm_end = prev ? prev->vm_end : 0;
  2023. tail_vma->vm_next = NULL;
  2024. if (mm->unmap_area == arch_unmap_area)
  2025. addr = prev ? prev->vm_end : mm->mmap_base;
  2026. else
  2027. addr = vma ? vma->vm_start : mm->mmap_base;
  2028. mm->unmap_area(mm, addr);
  2029. mm->mmap_cache = NULL; /* Kill the cache. */
  2030. }
  2031. /*
  2032. * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
  2033. * munmap path where it doesn't make sense to fail.
  2034. */
  2035. static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  2036. unsigned long addr, int new_below)
  2037. {
  2038. struct mempolicy *pol;
  2039. struct vm_area_struct *new;
  2040. int err = -ENOMEM;
  2041. if (is_vm_hugetlb_page(vma) && (addr &
  2042. ~(huge_page_mask(hstate_vma(vma)))))
  2043. return -EINVAL;
  2044. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2045. if (!new)
  2046. goto out_err;
  2047. /* most fields are the same, copy all, and then fixup */
  2048. *new = *vma;
  2049. INIT_LIST_HEAD(&new->anon_vma_chain);
  2050. if (new_below)
  2051. new->vm_end = addr;
  2052. else {
  2053. new->vm_start = addr;
  2054. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  2055. }
  2056. pol = mpol_dup(vma_policy(vma));
  2057. if (IS_ERR(pol)) {
  2058. err = PTR_ERR(pol);
  2059. goto out_free_vma;
  2060. }
  2061. vma_set_policy(new, pol);
  2062. if (anon_vma_clone(new, vma))
  2063. goto out_free_mpol;
  2064. if (new->vm_file)
  2065. get_file(new->vm_file);
  2066. if (new->vm_ops && new->vm_ops->open)
  2067. new->vm_ops->open(new);
  2068. if (new_below)
  2069. err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  2070. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  2071. else
  2072. err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  2073. /* Success. */
  2074. if (!err)
  2075. return 0;
  2076. /* Clean everything up if vma_adjust failed. */
  2077. if (new->vm_ops && new->vm_ops->close)
  2078. new->vm_ops->close(new);
  2079. if (new->vm_file)
  2080. fput(new->vm_file);
  2081. unlink_anon_vmas(new);
  2082. out_free_mpol:
  2083. mpol_put(pol);
  2084. out_free_vma:
  2085. kmem_cache_free(vm_area_cachep, new);
  2086. out_err:
  2087. return err;
  2088. }
  2089. /*
  2090. * Split a vma into two pieces at address 'addr', a new vma is allocated
  2091. * either for the first part or the tail.
  2092. */
  2093. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  2094. unsigned long addr, int new_below)
  2095. {
  2096. if (mm->map_count >= sysctl_max_map_count)
  2097. return -ENOMEM;
  2098. return __split_vma(mm, vma, addr, new_below);
  2099. }
  2100. /* Munmap is split into 2 main parts -- this part which finds
  2101. * what needs doing, and the areas themselves, which do the
  2102. * work. This now handles partial unmappings.
  2103. * Jeremy Fitzhardinge <jeremy@goop.org>
  2104. */
  2105. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  2106. {
  2107. unsigned long end;
  2108. struct vm_area_struct *vma, *prev, *last;
  2109. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  2110. return -EINVAL;
  2111. if ((len = PAGE_ALIGN(len)) == 0)
  2112. return -EINVAL;
  2113. /* Find the first overlapping VMA */
  2114. vma = find_vma(mm, start);
  2115. if (!vma)
  2116. return 0;
  2117. prev = vma->vm_prev;
  2118. /* we have start < vma->vm_end */
  2119. /* if it doesn't overlap, we have nothing.. */
  2120. end = start + len;
  2121. if (vma->vm_start >= end)
  2122. return 0;
  2123. /*
  2124. * If we need to split any vma, do it now to save pain later.
  2125. *
  2126. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  2127. * unmapped vm_area_struct will remain in use: so lower split_vma
  2128. * places tmp vma above, and higher split_vma places tmp vma below.
  2129. */
  2130. if (start > vma->vm_start) {
  2131. int error;
  2132. /*
  2133. * Make sure that map_count on return from munmap() will
  2134. * not exceed its limit; but let map_count go just above
  2135. * its limit temporarily, to help free resources as expected.
  2136. */
  2137. if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
  2138. return -ENOMEM;
  2139. error = __split_vma(mm, vma, start, 0);
  2140. if (error)
  2141. return error;
  2142. prev = vma;
  2143. }
  2144. /* Does it split the last one? */
  2145. last = find_vma(mm, end);
  2146. if (last && end > last->vm_start) {
  2147. int error = __split_vma(mm, last, end, 1);
  2148. if (error)
  2149. return error;
  2150. }
  2151. vma = prev? prev->vm_next: mm->mmap;
  2152. /*
  2153. * unlock any mlock()ed ranges before detaching vmas
  2154. */
  2155. if (mm->locked_vm) {
  2156. struct vm_area_struct *tmp = vma;
  2157. while (tmp && tmp->vm_start < end) {
  2158. if (tmp->vm_flags & VM_LOCKED) {
  2159. mm->locked_vm -= vma_pages(tmp);
  2160. munlock_vma_pages_all(tmp);
  2161. }
  2162. tmp = tmp->vm_next;
  2163. }
  2164. }
  2165. /*
  2166. * Remove the vma's, and unmap the actual pages
  2167. */
  2168. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  2169. unmap_region(mm, vma, prev, start, end);
  2170. /* Fix up all other VM information */
  2171. remove_vma_list(mm, vma);
  2172. return 0;
  2173. }
  2174. int vm_munmap(unsigned long start, size_t len)
  2175. {
  2176. int ret;
  2177. struct mm_struct *mm = current->mm;
  2178. down_write(&mm->mmap_sem);
  2179. ret = do_munmap(mm, start, len);
  2180. up_write(&mm->mmap_sem);
  2181. return ret;
  2182. }
  2183. EXPORT_SYMBOL(vm_munmap);
  2184. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  2185. {
  2186. profile_munmap(addr);
  2187. return vm_munmap(addr, len);
  2188. }
  2189. static inline void verify_mm_writelocked(struct mm_struct *mm)
  2190. {
  2191. #ifdef CONFIG_DEBUG_VM
  2192. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  2193. WARN_ON(1);
  2194. up_read(&mm->mmap_sem);
  2195. }
  2196. #endif
  2197. }
  2198. /*
  2199. * this is really a simplified "do_mmap". it only handles
  2200. * anonymous maps. eventually we may be able to do some
  2201. * brk-specific accounting here.
  2202. */
  2203. static unsigned long do_brk(unsigned long addr, unsigned long len)
  2204. {
  2205. struct mm_struct * mm = current->mm;
  2206. struct vm_area_struct * vma, * prev;
  2207. unsigned long flags;
  2208. struct rb_node ** rb_link, * rb_parent;
  2209. pgoff_t pgoff = addr >> PAGE_SHIFT;
  2210. int error;
  2211. len = PAGE_ALIGN(len);
  2212. if (!len)
  2213. return addr;
  2214. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  2215. error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  2216. if (error & ~PAGE_MASK)
  2217. return error;
  2218. /*
  2219. * mlock MCL_FUTURE?
  2220. */
  2221. if (mm->def_flags & VM_LOCKED) {
  2222. unsigned long locked, lock_limit;
  2223. locked = len >> PAGE_SHIFT;
  2224. locked += mm->locked_vm;
  2225. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2226. lock_limit >>= PAGE_SHIFT;
  2227. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  2228. return -EAGAIN;
  2229. }
  2230. /*
  2231. * mm->mmap_sem is required to protect against another thread
  2232. * changing the mappings in case we sleep.
  2233. */
  2234. verify_mm_writelocked(mm);
  2235. /*
  2236. * Clear old maps. this also does some error checking for us
  2237. */
  2238. munmap_back:
  2239. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  2240. if (do_munmap(mm, addr, len))
  2241. return -ENOMEM;
  2242. goto munmap_back;
  2243. }
  2244. /* Check against address space limits *after* clearing old maps... */
  2245. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  2246. return -ENOMEM;
  2247. if (mm->map_count > sysctl_max_map_count)
  2248. return -ENOMEM;
  2249. if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
  2250. return -ENOMEM;
  2251. /* Can we just expand an old private anonymous mapping? */
  2252. vma = vma_merge(mm, prev, addr, addr + len, flags,
  2253. NULL, NULL, pgoff, NULL);
  2254. if (vma)
  2255. goto out;
  2256. /*
  2257. * create a vma struct for an anonymous mapping
  2258. */
  2259. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2260. if (!vma) {
  2261. vm_unacct_memory(len >> PAGE_SHIFT);
  2262. return -ENOMEM;
  2263. }
  2264. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2265. vma->vm_mm = mm;
  2266. vma->vm_start = addr;
  2267. vma->vm_end = addr + len;
  2268. vma->vm_pgoff = pgoff;
  2269. vma->vm_flags = flags;
  2270. vma->vm_page_prot = vm_get_page_prot(flags);
  2271. vma_link(mm, vma, prev, rb_link, rb_parent);
  2272. out:
  2273. perf_event_mmap(vma);
  2274. mm->total_vm += len >> PAGE_SHIFT;
  2275. if (flags & VM_LOCKED) {
  2276. if (!mlock_vma_pages_range(vma, addr, addr + len))
  2277. mm->locked_vm += (len >> PAGE_SHIFT);
  2278. }
  2279. return addr;
  2280. }
  2281. unsigned long vm_brk(unsigned long addr, unsigned long len)
  2282. {
  2283. struct mm_struct *mm = current->mm;
  2284. unsigned long ret;
  2285. down_write(&mm->mmap_sem);
  2286. ret = do_brk(addr, len);
  2287. up_write(&mm->mmap_sem);
  2288. return ret;
  2289. }
  2290. EXPORT_SYMBOL(vm_brk);
  2291. /* Release all mmaps. */
  2292. void exit_mmap(struct mm_struct *mm)
  2293. {
  2294. struct mmu_gather tlb;
  2295. struct vm_area_struct *vma;
  2296. unsigned long nr_accounted = 0;
  2297. /* mm's last user has gone, and its about to be pulled down */
  2298. mmu_notifier_release(mm);
  2299. if (mm->locked_vm) {
  2300. vma = mm->mmap;
  2301. while (vma) {
  2302. if (vma->vm_flags & VM_LOCKED)
  2303. munlock_vma_pages_all(vma);
  2304. vma = vma->vm_next;
  2305. }
  2306. }
  2307. arch_exit_mmap(mm);
  2308. vma = mm->mmap;
  2309. if (!vma) /* Can happen if dup_mmap() received an OOM */
  2310. return;
  2311. lru_add_drain();
  2312. flush_cache_mm(mm);
  2313. tlb_gather_mmu(&tlb, mm, 1);
  2314. /* update_hiwater_rss(mm) here? but nobody should be looking */
  2315. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  2316. unmap_vmas(&tlb, vma, 0, -1);
  2317. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
  2318. tlb_finish_mmu(&tlb, 0, -1);
  2319. /*
  2320. * Walk the list again, actually closing and freeing it,
  2321. * with preemption enabled, without holding any MM locks.
  2322. */
  2323. while (vma) {
  2324. if (vma->vm_flags & VM_ACCOUNT)
  2325. nr_accounted += vma_pages(vma);
  2326. vma = remove_vma(vma);
  2327. }
  2328. vm_unacct_memory(nr_accounted);
  2329. WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  2330. }
  2331. /* Insert vm structure into process list sorted by address
  2332. * and into the inode's i_mmap tree. If vm_file is non-NULL
  2333. * then i_mmap_mutex is taken here.
  2334. */
  2335. int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  2336. {
  2337. struct vm_area_struct *prev;
  2338. struct rb_node **rb_link, *rb_parent;
  2339. /*
  2340. * The vm_pgoff of a purely anonymous vma should be irrelevant
  2341. * until its first write fault, when page's anon_vma and index
  2342. * are set. But now set the vm_pgoff it will almost certainly
  2343. * end up with (unless mremap moves it elsewhere before that
  2344. * first wfault), so /proc/pid/maps tells a consistent story.
  2345. *
  2346. * By setting it to reflect the virtual start address of the
  2347. * vma, merges and splits can happen in a seamless way, just
  2348. * using the existing file pgoff checks and manipulations.
  2349. * Similarly in do_mmap_pgoff and in do_brk.
  2350. */
  2351. if (!vma->vm_file) {
  2352. BUG_ON(vma->anon_vma);
  2353. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  2354. }
  2355. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  2356. &prev, &rb_link, &rb_parent))
  2357. return -ENOMEM;
  2358. if ((vma->vm_flags & VM_ACCOUNT) &&
  2359. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  2360. return -ENOMEM;
  2361. vma_link(mm, vma, prev, rb_link, rb_parent);
  2362. return 0;
  2363. }
  2364. /*
  2365. * Copy the vma structure to a new location in the same mm,
  2366. * prior to moving page table entries, to effect an mremap move.
  2367. */
  2368. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  2369. unsigned long addr, unsigned long len, pgoff_t pgoff,
  2370. bool *need_rmap_locks)
  2371. {
  2372. struct vm_area_struct *vma = *vmap;
  2373. unsigned long vma_start = vma->vm_start;
  2374. struct mm_struct *mm = vma->vm_mm;
  2375. struct vm_area_struct *new_vma, *prev;
  2376. struct rb_node **rb_link, *rb_parent;
  2377. struct mempolicy *pol;
  2378. bool faulted_in_anon_vma = true;
  2379. /*
  2380. * If anonymous vma has not yet been faulted, update new pgoff
  2381. * to match new location, to increase its chance of merging.
  2382. */
  2383. if (unlikely(!vma->vm_file && !vma->anon_vma)) {
  2384. pgoff = addr >> PAGE_SHIFT;
  2385. faulted_in_anon_vma = false;
  2386. }
  2387. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
  2388. return NULL; /* should never get here */
  2389. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  2390. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  2391. if (new_vma) {
  2392. /*
  2393. * Source vma may have been merged into new_vma
  2394. */
  2395. if (unlikely(vma_start >= new_vma->vm_start &&
  2396. vma_start < new_vma->vm_end)) {
  2397. /*
  2398. * The only way we can get a vma_merge with
  2399. * self during an mremap is if the vma hasn't
  2400. * been faulted in yet and we were allowed to
  2401. * reset the dst vma->vm_pgoff to the
  2402. * destination address of the mremap to allow
  2403. * the merge to happen. mremap must change the
  2404. * vm_pgoff linearity between src and dst vmas
  2405. * (in turn preventing a vma_merge) to be
  2406. * safe. It is only safe to keep the vm_pgoff
  2407. * linear if there are no pages mapped yet.
  2408. */
  2409. VM_BUG_ON(faulted_in_anon_vma);
  2410. *vmap = vma = new_vma;
  2411. }
  2412. *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
  2413. } else {
  2414. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2415. if (new_vma) {
  2416. *new_vma = *vma;
  2417. new_vma->vm_start = addr;
  2418. new_vma->vm_end = addr + len;
  2419. new_vma->vm_pgoff = pgoff;
  2420. pol = mpol_dup(vma_policy(vma));
  2421. if (IS_ERR(pol))
  2422. goto out_free_vma;
  2423. vma_set_policy(new_vma, pol);
  2424. INIT_LIST_HEAD(&new_vma->anon_vma_chain);
  2425. if (anon_vma_clone(new_vma, vma))
  2426. goto out_free_mempol;
  2427. if (new_vma->vm_file)
  2428. get_file(new_vma->vm_file);
  2429. if (new_vma->vm_ops && new_vma->vm_ops->open)
  2430. new_vma->vm_ops->open(new_vma);
  2431. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  2432. *need_rmap_locks = false;
  2433. }
  2434. }
  2435. return new_vma;
  2436. out_free_mempol:
  2437. mpol_put(pol);
  2438. out_free_vma:
  2439. kmem_cache_free(vm_area_cachep, new_vma);
  2440. return NULL;
  2441. }
  2442. /*
  2443. * Return true if the calling process may expand its vm space by the passed
  2444. * number of pages
  2445. */
  2446. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  2447. {
  2448. unsigned long cur = mm->total_vm; /* pages */
  2449. unsigned long lim;
  2450. lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
  2451. if (cur + npages > lim)
  2452. return 0;
  2453. return 1;
  2454. }
  2455. static int special_mapping_fault(struct vm_area_struct *vma,
  2456. struct vm_fault *vmf)
  2457. {
  2458. pgoff_t pgoff;
  2459. struct page **pages;
  2460. /*
  2461. * special mappings have no vm_file, and in that case, the mm
  2462. * uses vm_pgoff internally. So we have to subtract it from here.
  2463. * We are allowed to do this because we are the mm; do not copy
  2464. * this code into drivers!
  2465. */
  2466. pgoff = vmf->pgoff - vma->vm_pgoff;
  2467. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  2468. pgoff--;
  2469. if (*pages) {
  2470. struct page *page = *pages;
  2471. get_page(page);
  2472. vmf->page = page;
  2473. return 0;
  2474. }
  2475. return VM_FAULT_SIGBUS;
  2476. }
  2477. /*
  2478. * Having a close hook prevents vma merging regardless of flags.
  2479. */
  2480. static void special_mapping_close(struct vm_area_struct *vma)
  2481. {
  2482. }
  2483. static const struct vm_operations_struct special_mapping_vmops = {
  2484. .close = special_mapping_close,
  2485. .fault = special_mapping_fault,
  2486. };
  2487. /*
  2488. * Called with mm->mmap_sem held for writing.
  2489. * Insert a new vma covering the given region, with the given flags.
  2490. * Its pages are supplied by the given array of struct page *.
  2491. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  2492. * The region past the last page supplied will always produce SIGBUS.
  2493. * The array pointer and the pages it points to are assumed to stay alive
  2494. * for as long as this mapping might exist.
  2495. */
  2496. int install_special_mapping(struct mm_struct *mm,
  2497. unsigned long addr, unsigned long len,
  2498. unsigned long vm_flags, struct page **pages)
  2499. {
  2500. int ret;
  2501. struct vm_area_struct *vma;
  2502. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2503. if (unlikely(vma == NULL))
  2504. return -ENOMEM;
  2505. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2506. vma->vm_mm = mm;
  2507. vma->vm_start = addr;
  2508. vma->vm_end = addr + len;
  2509. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2510. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2511. vma->vm_ops = &special_mapping_vmops;
  2512. vma->vm_private_data = pages;
  2513. ret = insert_vm_struct(mm, vma);
  2514. if (ret)
  2515. goto out;
  2516. mm->total_vm += len >> PAGE_SHIFT;
  2517. perf_event_mmap(vma);
  2518. return 0;
  2519. out:
  2520. kmem_cache_free(vm_area_cachep, vma);
  2521. return ret;
  2522. }
  2523. static DEFINE_MUTEX(mm_all_locks_mutex);
  2524. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2525. {
  2526. if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2527. /*
  2528. * The LSB of head.next can't change from under us
  2529. * because we hold the mm_all_locks_mutex.
  2530. */
  2531. down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
  2532. /*
  2533. * We can safely modify head.next after taking the
  2534. * anon_vma->root->rwsem. If some other vma in this mm shares
  2535. * the same anon_vma we won't take it again.
  2536. *
  2537. * No need of atomic instructions here, head.next
  2538. * can't change from under us thanks to the
  2539. * anon_vma->root->rwsem.
  2540. */
  2541. if (__test_and_set_bit(0, (unsigned long *)
  2542. &anon_vma->root->rb_root.rb_node))
  2543. BUG();
  2544. }
  2545. }
  2546. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2547. {
  2548. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2549. /*
  2550. * AS_MM_ALL_LOCKS can't change from under us because
  2551. * we hold the mm_all_locks_mutex.
  2552. *
  2553. * Operations on ->flags have to be atomic because
  2554. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2555. * mm_all_locks_mutex, there may be other cpus
  2556. * changing other bitflags in parallel to us.
  2557. */
  2558. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2559. BUG();
  2560. mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
  2561. }
  2562. }
  2563. /*
  2564. * This operation locks against the VM for all pte/vma/mm related
  2565. * operations that could ever happen on a certain mm. This includes
  2566. * vmtruncate, try_to_unmap, and all page faults.
  2567. *
  2568. * The caller must take the mmap_sem in write mode before calling
  2569. * mm_take_all_locks(). The caller isn't allowed to release the
  2570. * mmap_sem until mm_drop_all_locks() returns.
  2571. *
  2572. * mmap_sem in write mode is required in order to block all operations
  2573. * that could modify pagetables and free pages without need of
  2574. * altering the vma layout (for example populate_range() with
  2575. * nonlinear vmas). It's also needed in write mode to avoid new
  2576. * anon_vmas to be associated with existing vmas.
  2577. *
  2578. * A single task can't take more than one mm_take_all_locks() in a row
  2579. * or it would deadlock.
  2580. *
  2581. * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
  2582. * mapping->flags avoid to take the same lock twice, if more than one
  2583. * vma in this mm is backed by the same anon_vma or address_space.
  2584. *
  2585. * We can take all the locks in random order because the VM code
  2586. * taking i_mmap_mutex or anon_vma->rwsem outside the mmap_sem never
  2587. * takes more than one of them in a row. Secondly we're protected
  2588. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2589. *
  2590. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2591. * that may have to take thousand of locks.
  2592. *
  2593. * mm_take_all_locks() can fail if it's interrupted by signals.
  2594. */
  2595. int mm_take_all_locks(struct mm_struct *mm)
  2596. {
  2597. struct vm_area_struct *vma;
  2598. struct anon_vma_chain *avc;
  2599. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2600. mutex_lock(&mm_all_locks_mutex);
  2601. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2602. if (signal_pending(current))
  2603. goto out_unlock;
  2604. if (vma->vm_file && vma->vm_file->f_mapping)
  2605. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2606. }
  2607. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2608. if (signal_pending(current))
  2609. goto out_unlock;
  2610. if (vma->anon_vma)
  2611. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2612. vm_lock_anon_vma(mm, avc->anon_vma);
  2613. }
  2614. return 0;
  2615. out_unlock:
  2616. mm_drop_all_locks(mm);
  2617. return -EINTR;
  2618. }
  2619. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2620. {
  2621. if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2622. /*
  2623. * The LSB of head.next can't change to 0 from under
  2624. * us because we hold the mm_all_locks_mutex.
  2625. *
  2626. * We must however clear the bitflag before unlocking
  2627. * the vma so the users using the anon_vma->rb_root will
  2628. * never see our bitflag.
  2629. *
  2630. * No need of atomic instructions here, head.next
  2631. * can't change from under us until we release the
  2632. * anon_vma->root->rwsem.
  2633. */
  2634. if (!__test_and_clear_bit(0, (unsigned long *)
  2635. &anon_vma->root->rb_root.rb_node))
  2636. BUG();
  2637. anon_vma_unlock(anon_vma);
  2638. }
  2639. }
  2640. static void vm_unlock_mapping(struct address_space *mapping)
  2641. {
  2642. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2643. /*
  2644. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2645. * because we hold the mm_all_locks_mutex.
  2646. */
  2647. mutex_unlock(&mapping->i_mmap_mutex);
  2648. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2649. &mapping->flags))
  2650. BUG();
  2651. }
  2652. }
  2653. /*
  2654. * The mmap_sem cannot be released by the caller until
  2655. * mm_drop_all_locks() returns.
  2656. */
  2657. void mm_drop_all_locks(struct mm_struct *mm)
  2658. {
  2659. struct vm_area_struct *vma;
  2660. struct anon_vma_chain *avc;
  2661. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2662. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2663. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2664. if (vma->anon_vma)
  2665. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2666. vm_unlock_anon_vma(avc->anon_vma);
  2667. if (vma->vm_file && vma->vm_file->f_mapping)
  2668. vm_unlock_mapping(vma->vm_file->f_mapping);
  2669. }
  2670. mutex_unlock(&mm_all_locks_mutex);
  2671. }
  2672. /*
  2673. * initialise the VMA slab
  2674. */
  2675. void __init mmap_init(void)
  2676. {
  2677. int ret;
  2678. ret = percpu_counter_init(&vm_committed_as, 0);
  2679. VM_BUG_ON(ret);
  2680. }