mmap.c 83 KB

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