mmap.c 82 KB

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