mmap.c 98 KB

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