mmap.c 87 KB

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