mmap.c 79 KB

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