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