mmap.c 98 KB

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