mmap.c 86 KB

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