mmap.c 99 KB

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