mmap.c 96 KB

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