mmap.c 89 KB

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