mmap.c 75 KB

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