mmap.c 90 KB

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