mmap.c 90 KB

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