mmap.c 100 KB

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