mmap.c 88 KB

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