mmap.c 96 KB

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