mmap.c 89 KB

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