mmap.c 89 KB

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