mmap.c 99 KB

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