mmap.c 89 KB

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