mmap.c 81 KB

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