mmap.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876
  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. /* Get an address range which is currently unmapped.
  1366. * For shmat() with addr=0.
  1367. *
  1368. * Ugly calling convention alert:
  1369. * Return value with the low bits set means error value,
  1370. * ie
  1371. * if (ret & ~PAGE_MASK)
  1372. * error = ret;
  1373. *
  1374. * This function "knows" that -ENOMEM has the bits set.
  1375. */
  1376. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1377. unsigned long
  1378. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1379. unsigned long len, unsigned long pgoff, unsigned long flags)
  1380. {
  1381. struct mm_struct *mm = current->mm;
  1382. struct vm_area_struct *vma;
  1383. unsigned long start_addr;
  1384. if (len > TASK_SIZE)
  1385. return -ENOMEM;
  1386. if (flags & MAP_FIXED)
  1387. return addr;
  1388. if (addr) {
  1389. addr = PAGE_ALIGN(addr);
  1390. vma = find_vma(mm, addr);
  1391. if (TASK_SIZE - len >= addr &&
  1392. (!vma || addr + len <= vma->vm_start))
  1393. return addr;
  1394. }
  1395. if (len > mm->cached_hole_size) {
  1396. start_addr = addr = mm->free_area_cache;
  1397. } else {
  1398. start_addr = addr = TASK_UNMAPPED_BASE;
  1399. mm->cached_hole_size = 0;
  1400. }
  1401. full_search:
  1402. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  1403. /* At this point: (!vma || addr < vma->vm_end). */
  1404. if (TASK_SIZE - len < addr) {
  1405. /*
  1406. * Start a new search - just in case we missed
  1407. * some holes.
  1408. */
  1409. if (start_addr != TASK_UNMAPPED_BASE) {
  1410. addr = TASK_UNMAPPED_BASE;
  1411. start_addr = addr;
  1412. mm->cached_hole_size = 0;
  1413. goto full_search;
  1414. }
  1415. return -ENOMEM;
  1416. }
  1417. if (!vma || addr + len <= vma->vm_start) {
  1418. /*
  1419. * Remember the place where we stopped the search:
  1420. */
  1421. mm->free_area_cache = addr + len;
  1422. return addr;
  1423. }
  1424. if (addr + mm->cached_hole_size < vma->vm_start)
  1425. mm->cached_hole_size = vma->vm_start - addr;
  1426. addr = vma->vm_end;
  1427. }
  1428. }
  1429. #endif
  1430. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1431. {
  1432. /*
  1433. * Is this a new hole at the lowest possible address?
  1434. */
  1435. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
  1436. mm->free_area_cache = addr;
  1437. }
  1438. /*
  1439. * This mmap-allocator allocates new areas top-down from below the
  1440. * stack's low limit (the base):
  1441. */
  1442. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1443. unsigned long
  1444. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1445. const unsigned long len, const unsigned long pgoff,
  1446. const unsigned long flags)
  1447. {
  1448. struct vm_area_struct *vma;
  1449. struct mm_struct *mm = current->mm;
  1450. unsigned long addr = addr0, start_addr;
  1451. /* requested length too big for entire address space */
  1452. if (len > TASK_SIZE)
  1453. return -ENOMEM;
  1454. if (flags & MAP_FIXED)
  1455. return addr;
  1456. /* requesting a specific address */
  1457. if (addr) {
  1458. addr = PAGE_ALIGN(addr);
  1459. vma = find_vma(mm, addr);
  1460. if (TASK_SIZE - len >= addr &&
  1461. (!vma || addr + len <= vma->vm_start))
  1462. return addr;
  1463. }
  1464. /* check if free_area_cache is useful for us */
  1465. if (len <= mm->cached_hole_size) {
  1466. mm->cached_hole_size = 0;
  1467. mm->free_area_cache = mm->mmap_base;
  1468. }
  1469. try_again:
  1470. /* either no address requested or can't fit in requested address hole */
  1471. start_addr = addr = mm->free_area_cache;
  1472. if (addr < len)
  1473. goto fail;
  1474. addr -= len;
  1475. do {
  1476. /*
  1477. * Lookup failure means no vma is above this address,
  1478. * else if new region fits below vma->vm_start,
  1479. * return with success:
  1480. */
  1481. vma = find_vma(mm, addr);
  1482. if (!vma || addr+len <= vma->vm_start)
  1483. /* remember the address as a hint for next time */
  1484. return (mm->free_area_cache = addr);
  1485. /* remember the largest hole we saw so far */
  1486. if (addr + mm->cached_hole_size < vma->vm_start)
  1487. mm->cached_hole_size = vma->vm_start - addr;
  1488. /* try just below the current vma->vm_start */
  1489. addr = vma->vm_start-len;
  1490. } while (len < vma->vm_start);
  1491. fail:
  1492. /*
  1493. * if hint left us with no space for the requested
  1494. * mapping then try again:
  1495. *
  1496. * Note: this is different with the case of bottomup
  1497. * which does the fully line-search, but we use find_vma
  1498. * here that causes some holes skipped.
  1499. */
  1500. if (start_addr != mm->mmap_base) {
  1501. mm->free_area_cache = mm->mmap_base;
  1502. mm->cached_hole_size = 0;
  1503. goto try_again;
  1504. }
  1505. /*
  1506. * A failed mmap() very likely causes application failure,
  1507. * so fall back to the bottom-up function here. This scenario
  1508. * can happen with large stack limits and large mmap()
  1509. * allocations.
  1510. */
  1511. mm->cached_hole_size = ~0UL;
  1512. mm->free_area_cache = TASK_UNMAPPED_BASE;
  1513. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  1514. /*
  1515. * Restore the topdown base:
  1516. */
  1517. mm->free_area_cache = mm->mmap_base;
  1518. mm->cached_hole_size = ~0UL;
  1519. return addr;
  1520. }
  1521. #endif
  1522. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1523. {
  1524. /*
  1525. * Is this a new hole at the highest possible address?
  1526. */
  1527. if (addr > mm->free_area_cache)
  1528. mm->free_area_cache = addr;
  1529. /* dont allow allocations above current base */
  1530. if (mm->free_area_cache > mm->mmap_base)
  1531. mm->free_area_cache = mm->mmap_base;
  1532. }
  1533. unsigned long
  1534. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1535. unsigned long pgoff, unsigned long flags)
  1536. {
  1537. unsigned long (*get_area)(struct file *, unsigned long,
  1538. unsigned long, unsigned long, unsigned long);
  1539. unsigned long error = arch_mmap_check(addr, len, flags);
  1540. if (error)
  1541. return error;
  1542. /* Careful about overflows.. */
  1543. if (len > TASK_SIZE)
  1544. return -ENOMEM;
  1545. get_area = current->mm->get_unmapped_area;
  1546. if (file && file->f_op && file->f_op->get_unmapped_area)
  1547. get_area = file->f_op->get_unmapped_area;
  1548. addr = get_area(file, addr, len, pgoff, flags);
  1549. if (IS_ERR_VALUE(addr))
  1550. return addr;
  1551. if (addr > TASK_SIZE - len)
  1552. return -ENOMEM;
  1553. if (addr & ~PAGE_MASK)
  1554. return -EINVAL;
  1555. addr = arch_rebalance_pgtables(addr, len);
  1556. error = security_mmap_addr(addr);
  1557. return error ? error : addr;
  1558. }
  1559. EXPORT_SYMBOL(get_unmapped_area);
  1560. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1561. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1562. {
  1563. struct vm_area_struct *vma = NULL;
  1564. if (WARN_ON_ONCE(!mm)) /* Remove this in linux-3.6 */
  1565. return NULL;
  1566. /* Check the cache first. */
  1567. /* (Cache hit rate is typically around 35%.) */
  1568. vma = mm->mmap_cache;
  1569. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1570. struct rb_node *rb_node;
  1571. rb_node = mm->mm_rb.rb_node;
  1572. vma = NULL;
  1573. while (rb_node) {
  1574. struct vm_area_struct *vma_tmp;
  1575. vma_tmp = rb_entry(rb_node,
  1576. struct vm_area_struct, vm_rb);
  1577. if (vma_tmp->vm_end > addr) {
  1578. vma = vma_tmp;
  1579. if (vma_tmp->vm_start <= addr)
  1580. break;
  1581. rb_node = rb_node->rb_left;
  1582. } else
  1583. rb_node = rb_node->rb_right;
  1584. }
  1585. if (vma)
  1586. mm->mmap_cache = vma;
  1587. }
  1588. return vma;
  1589. }
  1590. EXPORT_SYMBOL(find_vma);
  1591. /*
  1592. * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
  1593. */
  1594. struct vm_area_struct *
  1595. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1596. struct vm_area_struct **pprev)
  1597. {
  1598. struct vm_area_struct *vma;
  1599. vma = find_vma(mm, addr);
  1600. if (vma) {
  1601. *pprev = vma->vm_prev;
  1602. } else {
  1603. struct rb_node *rb_node = mm->mm_rb.rb_node;
  1604. *pprev = NULL;
  1605. while (rb_node) {
  1606. *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1607. rb_node = rb_node->rb_right;
  1608. }
  1609. }
  1610. return vma;
  1611. }
  1612. /*
  1613. * Verify that the stack growth is acceptable and
  1614. * update accounting. This is shared with both the
  1615. * grow-up and grow-down cases.
  1616. */
  1617. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1618. {
  1619. struct mm_struct *mm = vma->vm_mm;
  1620. struct rlimit *rlim = current->signal->rlim;
  1621. unsigned long new_start;
  1622. /* address space limit tests */
  1623. if (!may_expand_vm(mm, grow))
  1624. return -ENOMEM;
  1625. /* Stack limit test */
  1626. if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
  1627. return -ENOMEM;
  1628. /* mlock limit tests */
  1629. if (vma->vm_flags & VM_LOCKED) {
  1630. unsigned long locked;
  1631. unsigned long limit;
  1632. locked = mm->locked_vm + grow;
  1633. limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
  1634. limit >>= PAGE_SHIFT;
  1635. if (locked > limit && !capable(CAP_IPC_LOCK))
  1636. return -ENOMEM;
  1637. }
  1638. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1639. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1640. vma->vm_end - size;
  1641. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1642. return -EFAULT;
  1643. /*
  1644. * Overcommit.. This must be the final test, as it will
  1645. * update security statistics.
  1646. */
  1647. if (security_vm_enough_memory_mm(mm, grow))
  1648. return -ENOMEM;
  1649. /* Ok, everything looks good - let it rip */
  1650. if (vma->vm_flags & VM_LOCKED)
  1651. mm->locked_vm += grow;
  1652. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1653. return 0;
  1654. }
  1655. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1656. /*
  1657. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1658. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1659. */
  1660. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1661. {
  1662. int error;
  1663. if (!(vma->vm_flags & VM_GROWSUP))
  1664. return -EFAULT;
  1665. /*
  1666. * We must make sure the anon_vma is allocated
  1667. * so that the anon_vma locking is not a noop.
  1668. */
  1669. if (unlikely(anon_vma_prepare(vma)))
  1670. return -ENOMEM;
  1671. vma_lock_anon_vma(vma);
  1672. /*
  1673. * vma->vm_start/vm_end cannot change under us because the caller
  1674. * is required to hold the mmap_sem in read mode. We need the
  1675. * anon_vma lock to serialize against concurrent expand_stacks.
  1676. * Also guard against wrapping around to address 0.
  1677. */
  1678. if (address < PAGE_ALIGN(address+4))
  1679. address = PAGE_ALIGN(address+4);
  1680. else {
  1681. vma_unlock_anon_vma(vma);
  1682. return -ENOMEM;
  1683. }
  1684. error = 0;
  1685. /* Somebody else might have raced and expanded it already */
  1686. if (address > vma->vm_end) {
  1687. unsigned long size, grow;
  1688. size = address - vma->vm_start;
  1689. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1690. error = -ENOMEM;
  1691. if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
  1692. error = acct_stack_growth(vma, size, grow);
  1693. if (!error) {
  1694. anon_vma_interval_tree_pre_update_vma(vma);
  1695. vma->vm_end = address;
  1696. anon_vma_interval_tree_post_update_vma(vma);
  1697. if (vma->vm_next)
  1698. vma_gap_update(vma->vm_next);
  1699. else
  1700. vma->vm_mm->highest_vm_end = address;
  1701. perf_event_mmap(vma);
  1702. }
  1703. }
  1704. }
  1705. vma_unlock_anon_vma(vma);
  1706. khugepaged_enter_vma_merge(vma);
  1707. validate_mm(vma->vm_mm);
  1708. return error;
  1709. }
  1710. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1711. /*
  1712. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1713. */
  1714. int expand_downwards(struct vm_area_struct *vma,
  1715. unsigned long address)
  1716. {
  1717. int error;
  1718. /*
  1719. * We must make sure the anon_vma is allocated
  1720. * so that the anon_vma locking is not a noop.
  1721. */
  1722. if (unlikely(anon_vma_prepare(vma)))
  1723. return -ENOMEM;
  1724. address &= PAGE_MASK;
  1725. error = security_mmap_addr(address);
  1726. if (error)
  1727. return error;
  1728. vma_lock_anon_vma(vma);
  1729. /*
  1730. * vma->vm_start/vm_end cannot change under us because the caller
  1731. * is required to hold the mmap_sem in read mode. We need the
  1732. * anon_vma lock to serialize against concurrent expand_stacks.
  1733. */
  1734. /* Somebody else might have raced and expanded it already */
  1735. if (address < vma->vm_start) {
  1736. unsigned long size, grow;
  1737. size = vma->vm_end - address;
  1738. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1739. error = -ENOMEM;
  1740. if (grow <= vma->vm_pgoff) {
  1741. error = acct_stack_growth(vma, size, grow);
  1742. if (!error) {
  1743. anon_vma_interval_tree_pre_update_vma(vma);
  1744. vma->vm_start = address;
  1745. vma->vm_pgoff -= grow;
  1746. anon_vma_interval_tree_post_update_vma(vma);
  1747. vma_gap_update(vma);
  1748. perf_event_mmap(vma);
  1749. }
  1750. }
  1751. }
  1752. vma_unlock_anon_vma(vma);
  1753. khugepaged_enter_vma_merge(vma);
  1754. validate_mm(vma->vm_mm);
  1755. return error;
  1756. }
  1757. #ifdef CONFIG_STACK_GROWSUP
  1758. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1759. {
  1760. return expand_upwards(vma, address);
  1761. }
  1762. struct vm_area_struct *
  1763. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1764. {
  1765. struct vm_area_struct *vma, *prev;
  1766. addr &= PAGE_MASK;
  1767. vma = find_vma_prev(mm, addr, &prev);
  1768. if (vma && (vma->vm_start <= addr))
  1769. return vma;
  1770. if (!prev || expand_stack(prev, addr))
  1771. return NULL;
  1772. if (prev->vm_flags & VM_LOCKED) {
  1773. mlock_vma_pages_range(prev, addr, prev->vm_end);
  1774. }
  1775. return prev;
  1776. }
  1777. #else
  1778. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1779. {
  1780. return expand_downwards(vma, address);
  1781. }
  1782. struct vm_area_struct *
  1783. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1784. {
  1785. struct vm_area_struct * vma;
  1786. unsigned long start;
  1787. addr &= PAGE_MASK;
  1788. vma = find_vma(mm,addr);
  1789. if (!vma)
  1790. return NULL;
  1791. if (vma->vm_start <= addr)
  1792. return vma;
  1793. if (!(vma->vm_flags & VM_GROWSDOWN))
  1794. return NULL;
  1795. start = vma->vm_start;
  1796. if (expand_stack(vma, addr))
  1797. return NULL;
  1798. if (vma->vm_flags & VM_LOCKED) {
  1799. mlock_vma_pages_range(vma, addr, start);
  1800. }
  1801. return vma;
  1802. }
  1803. #endif
  1804. /*
  1805. * Ok - we have the memory areas we should free on the vma list,
  1806. * so release them, and do the vma updates.
  1807. *
  1808. * Called with the mm semaphore held.
  1809. */
  1810. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1811. {
  1812. unsigned long nr_accounted = 0;
  1813. /* Update high watermark before we lower total_vm */
  1814. update_hiwater_vm(mm);
  1815. do {
  1816. long nrpages = vma_pages(vma);
  1817. if (vma->vm_flags & VM_ACCOUNT)
  1818. nr_accounted += nrpages;
  1819. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1820. vma = remove_vma(vma);
  1821. } while (vma);
  1822. vm_unacct_memory(nr_accounted);
  1823. validate_mm(mm);
  1824. }
  1825. /*
  1826. * Get rid of page table information in the indicated region.
  1827. *
  1828. * Called with the mm semaphore held.
  1829. */
  1830. static void unmap_region(struct mm_struct *mm,
  1831. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1832. unsigned long start, unsigned long end)
  1833. {
  1834. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1835. struct mmu_gather tlb;
  1836. lru_add_drain();
  1837. tlb_gather_mmu(&tlb, mm, 0);
  1838. update_hiwater_rss(mm);
  1839. unmap_vmas(&tlb, vma, start, end);
  1840. free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
  1841. next ? next->vm_start : 0);
  1842. tlb_finish_mmu(&tlb, start, end);
  1843. }
  1844. /*
  1845. * Create a list of vma's touched by the unmap, removing them from the mm's
  1846. * vma list as we go..
  1847. */
  1848. static void
  1849. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  1850. struct vm_area_struct *prev, unsigned long end)
  1851. {
  1852. struct vm_area_struct **insertion_point;
  1853. struct vm_area_struct *tail_vma = NULL;
  1854. unsigned long addr;
  1855. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  1856. vma->vm_prev = NULL;
  1857. do {
  1858. vma_rb_erase(vma, &mm->mm_rb);
  1859. mm->map_count--;
  1860. tail_vma = vma;
  1861. vma = vma->vm_next;
  1862. } while (vma && vma->vm_start < end);
  1863. *insertion_point = vma;
  1864. if (vma) {
  1865. vma->vm_prev = prev;
  1866. vma_gap_update(vma);
  1867. } else
  1868. mm->highest_vm_end = prev ? prev->vm_end : 0;
  1869. tail_vma->vm_next = NULL;
  1870. if (mm->unmap_area == arch_unmap_area)
  1871. addr = prev ? prev->vm_end : mm->mmap_base;
  1872. else
  1873. addr = vma ? vma->vm_start : mm->mmap_base;
  1874. mm->unmap_area(mm, addr);
  1875. mm->mmap_cache = NULL; /* Kill the cache. */
  1876. }
  1877. /*
  1878. * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
  1879. * munmap path where it doesn't make sense to fail.
  1880. */
  1881. static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  1882. unsigned long addr, int new_below)
  1883. {
  1884. struct mempolicy *pol;
  1885. struct vm_area_struct *new;
  1886. int err = -ENOMEM;
  1887. if (is_vm_hugetlb_page(vma) && (addr &
  1888. ~(huge_page_mask(hstate_vma(vma)))))
  1889. return -EINVAL;
  1890. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1891. if (!new)
  1892. goto out_err;
  1893. /* most fields are the same, copy all, and then fixup */
  1894. *new = *vma;
  1895. INIT_LIST_HEAD(&new->anon_vma_chain);
  1896. if (new_below)
  1897. new->vm_end = addr;
  1898. else {
  1899. new->vm_start = addr;
  1900. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  1901. }
  1902. pol = mpol_dup(vma_policy(vma));
  1903. if (IS_ERR(pol)) {
  1904. err = PTR_ERR(pol);
  1905. goto out_free_vma;
  1906. }
  1907. vma_set_policy(new, pol);
  1908. if (anon_vma_clone(new, vma))
  1909. goto out_free_mpol;
  1910. if (new->vm_file)
  1911. get_file(new->vm_file);
  1912. if (new->vm_ops && new->vm_ops->open)
  1913. new->vm_ops->open(new);
  1914. if (new_below)
  1915. err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  1916. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  1917. else
  1918. err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  1919. /* Success. */
  1920. if (!err)
  1921. return 0;
  1922. /* Clean everything up if vma_adjust failed. */
  1923. if (new->vm_ops && new->vm_ops->close)
  1924. new->vm_ops->close(new);
  1925. if (new->vm_file)
  1926. fput(new->vm_file);
  1927. unlink_anon_vmas(new);
  1928. out_free_mpol:
  1929. mpol_put(pol);
  1930. out_free_vma:
  1931. kmem_cache_free(vm_area_cachep, new);
  1932. out_err:
  1933. return err;
  1934. }
  1935. /*
  1936. * Split a vma into two pieces at address 'addr', a new vma is allocated
  1937. * either for the first part or the tail.
  1938. */
  1939. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1940. unsigned long addr, int new_below)
  1941. {
  1942. if (mm->map_count >= sysctl_max_map_count)
  1943. return -ENOMEM;
  1944. return __split_vma(mm, vma, addr, new_below);
  1945. }
  1946. /* Munmap is split into 2 main parts -- this part which finds
  1947. * what needs doing, and the areas themselves, which do the
  1948. * work. This now handles partial unmappings.
  1949. * Jeremy Fitzhardinge <jeremy@goop.org>
  1950. */
  1951. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1952. {
  1953. unsigned long end;
  1954. struct vm_area_struct *vma, *prev, *last;
  1955. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  1956. return -EINVAL;
  1957. if ((len = PAGE_ALIGN(len)) == 0)
  1958. return -EINVAL;
  1959. /* Find the first overlapping VMA */
  1960. vma = find_vma(mm, start);
  1961. if (!vma)
  1962. return 0;
  1963. prev = vma->vm_prev;
  1964. /* we have start < vma->vm_end */
  1965. /* if it doesn't overlap, we have nothing.. */
  1966. end = start + len;
  1967. if (vma->vm_start >= end)
  1968. return 0;
  1969. /*
  1970. * If we need to split any vma, do it now to save pain later.
  1971. *
  1972. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  1973. * unmapped vm_area_struct will remain in use: so lower split_vma
  1974. * places tmp vma above, and higher split_vma places tmp vma below.
  1975. */
  1976. if (start > vma->vm_start) {
  1977. int error;
  1978. /*
  1979. * Make sure that map_count on return from munmap() will
  1980. * not exceed its limit; but let map_count go just above
  1981. * its limit temporarily, to help free resources as expected.
  1982. */
  1983. if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
  1984. return -ENOMEM;
  1985. error = __split_vma(mm, vma, start, 0);
  1986. if (error)
  1987. return error;
  1988. prev = vma;
  1989. }
  1990. /* Does it split the last one? */
  1991. last = find_vma(mm, end);
  1992. if (last && end > last->vm_start) {
  1993. int error = __split_vma(mm, last, end, 1);
  1994. if (error)
  1995. return error;
  1996. }
  1997. vma = prev? prev->vm_next: mm->mmap;
  1998. /*
  1999. * unlock any mlock()ed ranges before detaching vmas
  2000. */
  2001. if (mm->locked_vm) {
  2002. struct vm_area_struct *tmp = vma;
  2003. while (tmp && tmp->vm_start < end) {
  2004. if (tmp->vm_flags & VM_LOCKED) {
  2005. mm->locked_vm -= vma_pages(tmp);
  2006. munlock_vma_pages_all(tmp);
  2007. }
  2008. tmp = tmp->vm_next;
  2009. }
  2010. }
  2011. /*
  2012. * Remove the vma's, and unmap the actual pages
  2013. */
  2014. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  2015. unmap_region(mm, vma, prev, start, end);
  2016. /* Fix up all other VM information */
  2017. remove_vma_list(mm, vma);
  2018. return 0;
  2019. }
  2020. int vm_munmap(unsigned long start, size_t len)
  2021. {
  2022. int ret;
  2023. struct mm_struct *mm = current->mm;
  2024. down_write(&mm->mmap_sem);
  2025. ret = do_munmap(mm, start, len);
  2026. up_write(&mm->mmap_sem);
  2027. return ret;
  2028. }
  2029. EXPORT_SYMBOL(vm_munmap);
  2030. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  2031. {
  2032. profile_munmap(addr);
  2033. return vm_munmap(addr, len);
  2034. }
  2035. static inline void verify_mm_writelocked(struct mm_struct *mm)
  2036. {
  2037. #ifdef CONFIG_DEBUG_VM
  2038. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  2039. WARN_ON(1);
  2040. up_read(&mm->mmap_sem);
  2041. }
  2042. #endif
  2043. }
  2044. /*
  2045. * this is really a simplified "do_mmap". it only handles
  2046. * anonymous maps. eventually we may be able to do some
  2047. * brk-specific accounting here.
  2048. */
  2049. static unsigned long do_brk(unsigned long addr, unsigned long len)
  2050. {
  2051. struct mm_struct * mm = current->mm;
  2052. struct vm_area_struct * vma, * prev;
  2053. unsigned long flags;
  2054. struct rb_node ** rb_link, * rb_parent;
  2055. pgoff_t pgoff = addr >> PAGE_SHIFT;
  2056. int error;
  2057. len = PAGE_ALIGN(len);
  2058. if (!len)
  2059. return addr;
  2060. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  2061. error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  2062. if (error & ~PAGE_MASK)
  2063. return error;
  2064. /*
  2065. * mlock MCL_FUTURE?
  2066. */
  2067. if (mm->def_flags & VM_LOCKED) {
  2068. unsigned long locked, lock_limit;
  2069. locked = len >> PAGE_SHIFT;
  2070. locked += mm->locked_vm;
  2071. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2072. lock_limit >>= PAGE_SHIFT;
  2073. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  2074. return -EAGAIN;
  2075. }
  2076. /*
  2077. * mm->mmap_sem is required to protect against another thread
  2078. * changing the mappings in case we sleep.
  2079. */
  2080. verify_mm_writelocked(mm);
  2081. /*
  2082. * Clear old maps. this also does some error checking for us
  2083. */
  2084. munmap_back:
  2085. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
  2086. if (do_munmap(mm, addr, len))
  2087. return -ENOMEM;
  2088. goto munmap_back;
  2089. }
  2090. /* Check against address space limits *after* clearing old maps... */
  2091. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  2092. return -ENOMEM;
  2093. if (mm->map_count > sysctl_max_map_count)
  2094. return -ENOMEM;
  2095. if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
  2096. return -ENOMEM;
  2097. /* Can we just expand an old private anonymous mapping? */
  2098. vma = vma_merge(mm, prev, addr, addr + len, flags,
  2099. NULL, NULL, pgoff, NULL);
  2100. if (vma)
  2101. goto out;
  2102. /*
  2103. * create a vma struct for an anonymous mapping
  2104. */
  2105. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2106. if (!vma) {
  2107. vm_unacct_memory(len >> PAGE_SHIFT);
  2108. return -ENOMEM;
  2109. }
  2110. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2111. vma->vm_mm = mm;
  2112. vma->vm_start = addr;
  2113. vma->vm_end = addr + len;
  2114. vma->vm_pgoff = pgoff;
  2115. vma->vm_flags = flags;
  2116. vma->vm_page_prot = vm_get_page_prot(flags);
  2117. vma_link(mm, vma, prev, rb_link, rb_parent);
  2118. out:
  2119. perf_event_mmap(vma);
  2120. mm->total_vm += len >> PAGE_SHIFT;
  2121. if (flags & VM_LOCKED) {
  2122. if (!mlock_vma_pages_range(vma, addr, addr + len))
  2123. mm->locked_vm += (len >> PAGE_SHIFT);
  2124. }
  2125. return addr;
  2126. }
  2127. unsigned long vm_brk(unsigned long addr, unsigned long len)
  2128. {
  2129. struct mm_struct *mm = current->mm;
  2130. unsigned long ret;
  2131. down_write(&mm->mmap_sem);
  2132. ret = do_brk(addr, len);
  2133. up_write(&mm->mmap_sem);
  2134. return ret;
  2135. }
  2136. EXPORT_SYMBOL(vm_brk);
  2137. /* Release all mmaps. */
  2138. void exit_mmap(struct mm_struct *mm)
  2139. {
  2140. struct mmu_gather tlb;
  2141. struct vm_area_struct *vma;
  2142. unsigned long nr_accounted = 0;
  2143. /* mm's last user has gone, and its about to be pulled down */
  2144. mmu_notifier_release(mm);
  2145. if (mm->locked_vm) {
  2146. vma = mm->mmap;
  2147. while (vma) {
  2148. if (vma->vm_flags & VM_LOCKED)
  2149. munlock_vma_pages_all(vma);
  2150. vma = vma->vm_next;
  2151. }
  2152. }
  2153. arch_exit_mmap(mm);
  2154. vma = mm->mmap;
  2155. if (!vma) /* Can happen if dup_mmap() received an OOM */
  2156. return;
  2157. lru_add_drain();
  2158. flush_cache_mm(mm);
  2159. tlb_gather_mmu(&tlb, mm, 1);
  2160. /* update_hiwater_rss(mm) here? but nobody should be looking */
  2161. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  2162. unmap_vmas(&tlb, vma, 0, -1);
  2163. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
  2164. tlb_finish_mmu(&tlb, 0, -1);
  2165. /*
  2166. * Walk the list again, actually closing and freeing it,
  2167. * with preemption enabled, without holding any MM locks.
  2168. */
  2169. while (vma) {
  2170. if (vma->vm_flags & VM_ACCOUNT)
  2171. nr_accounted += vma_pages(vma);
  2172. vma = remove_vma(vma);
  2173. }
  2174. vm_unacct_memory(nr_accounted);
  2175. WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  2176. }
  2177. /* Insert vm structure into process list sorted by address
  2178. * and into the inode's i_mmap tree. If vm_file is non-NULL
  2179. * then i_mmap_mutex is taken here.
  2180. */
  2181. int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  2182. {
  2183. struct vm_area_struct *prev;
  2184. struct rb_node **rb_link, *rb_parent;
  2185. /*
  2186. * The vm_pgoff of a purely anonymous vma should be irrelevant
  2187. * until its first write fault, when page's anon_vma and index
  2188. * are set. But now set the vm_pgoff it will almost certainly
  2189. * end up with (unless mremap moves it elsewhere before that
  2190. * first wfault), so /proc/pid/maps tells a consistent story.
  2191. *
  2192. * By setting it to reflect the virtual start address of the
  2193. * vma, merges and splits can happen in a seamless way, just
  2194. * using the existing file pgoff checks and manipulations.
  2195. * Similarly in do_mmap_pgoff and in do_brk.
  2196. */
  2197. if (!vma->vm_file) {
  2198. BUG_ON(vma->anon_vma);
  2199. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  2200. }
  2201. if (find_vma_links(mm, vma->vm_start, vma->vm_end,
  2202. &prev, &rb_link, &rb_parent))
  2203. return -ENOMEM;
  2204. if ((vma->vm_flags & VM_ACCOUNT) &&
  2205. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  2206. return -ENOMEM;
  2207. vma_link(mm, vma, prev, rb_link, rb_parent);
  2208. return 0;
  2209. }
  2210. /*
  2211. * Copy the vma structure to a new location in the same mm,
  2212. * prior to moving page table entries, to effect an mremap move.
  2213. */
  2214. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  2215. unsigned long addr, unsigned long len, pgoff_t pgoff,
  2216. bool *need_rmap_locks)
  2217. {
  2218. struct vm_area_struct *vma = *vmap;
  2219. unsigned long vma_start = vma->vm_start;
  2220. struct mm_struct *mm = vma->vm_mm;
  2221. struct vm_area_struct *new_vma, *prev;
  2222. struct rb_node **rb_link, *rb_parent;
  2223. struct mempolicy *pol;
  2224. bool faulted_in_anon_vma = true;
  2225. /*
  2226. * If anonymous vma has not yet been faulted, update new pgoff
  2227. * to match new location, to increase its chance of merging.
  2228. */
  2229. if (unlikely(!vma->vm_file && !vma->anon_vma)) {
  2230. pgoff = addr >> PAGE_SHIFT;
  2231. faulted_in_anon_vma = false;
  2232. }
  2233. if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
  2234. return NULL; /* should never get here */
  2235. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  2236. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  2237. if (new_vma) {
  2238. /*
  2239. * Source vma may have been merged into new_vma
  2240. */
  2241. if (unlikely(vma_start >= new_vma->vm_start &&
  2242. vma_start < new_vma->vm_end)) {
  2243. /*
  2244. * The only way we can get a vma_merge with
  2245. * self during an mremap is if the vma hasn't
  2246. * been faulted in yet and we were allowed to
  2247. * reset the dst vma->vm_pgoff to the
  2248. * destination address of the mremap to allow
  2249. * the merge to happen. mremap must change the
  2250. * vm_pgoff linearity between src and dst vmas
  2251. * (in turn preventing a vma_merge) to be
  2252. * safe. It is only safe to keep the vm_pgoff
  2253. * linear if there are no pages mapped yet.
  2254. */
  2255. VM_BUG_ON(faulted_in_anon_vma);
  2256. *vmap = vma = new_vma;
  2257. }
  2258. *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
  2259. } else {
  2260. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2261. if (new_vma) {
  2262. *new_vma = *vma;
  2263. new_vma->vm_start = addr;
  2264. new_vma->vm_end = addr + len;
  2265. new_vma->vm_pgoff = pgoff;
  2266. pol = mpol_dup(vma_policy(vma));
  2267. if (IS_ERR(pol))
  2268. goto out_free_vma;
  2269. vma_set_policy(new_vma, pol);
  2270. INIT_LIST_HEAD(&new_vma->anon_vma_chain);
  2271. if (anon_vma_clone(new_vma, vma))
  2272. goto out_free_mempol;
  2273. if (new_vma->vm_file)
  2274. get_file(new_vma->vm_file);
  2275. if (new_vma->vm_ops && new_vma->vm_ops->open)
  2276. new_vma->vm_ops->open(new_vma);
  2277. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  2278. *need_rmap_locks = false;
  2279. }
  2280. }
  2281. return new_vma;
  2282. out_free_mempol:
  2283. mpol_put(pol);
  2284. out_free_vma:
  2285. kmem_cache_free(vm_area_cachep, new_vma);
  2286. return NULL;
  2287. }
  2288. /*
  2289. * Return true if the calling process may expand its vm space by the passed
  2290. * number of pages
  2291. */
  2292. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  2293. {
  2294. unsigned long cur = mm->total_vm; /* pages */
  2295. unsigned long lim;
  2296. lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
  2297. if (cur + npages > lim)
  2298. return 0;
  2299. return 1;
  2300. }
  2301. static int special_mapping_fault(struct vm_area_struct *vma,
  2302. struct vm_fault *vmf)
  2303. {
  2304. pgoff_t pgoff;
  2305. struct page **pages;
  2306. /*
  2307. * special mappings have no vm_file, and in that case, the mm
  2308. * uses vm_pgoff internally. So we have to subtract it from here.
  2309. * We are allowed to do this because we are the mm; do not copy
  2310. * this code into drivers!
  2311. */
  2312. pgoff = vmf->pgoff - vma->vm_pgoff;
  2313. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  2314. pgoff--;
  2315. if (*pages) {
  2316. struct page *page = *pages;
  2317. get_page(page);
  2318. vmf->page = page;
  2319. return 0;
  2320. }
  2321. return VM_FAULT_SIGBUS;
  2322. }
  2323. /*
  2324. * Having a close hook prevents vma merging regardless of flags.
  2325. */
  2326. static void special_mapping_close(struct vm_area_struct *vma)
  2327. {
  2328. }
  2329. static const struct vm_operations_struct special_mapping_vmops = {
  2330. .close = special_mapping_close,
  2331. .fault = special_mapping_fault,
  2332. };
  2333. /*
  2334. * Called with mm->mmap_sem held for writing.
  2335. * Insert a new vma covering the given region, with the given flags.
  2336. * Its pages are supplied by the given array of struct page *.
  2337. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  2338. * The region past the last page supplied will always produce SIGBUS.
  2339. * The array pointer and the pages it points to are assumed to stay alive
  2340. * for as long as this mapping might exist.
  2341. */
  2342. int install_special_mapping(struct mm_struct *mm,
  2343. unsigned long addr, unsigned long len,
  2344. unsigned long vm_flags, struct page **pages)
  2345. {
  2346. int ret;
  2347. struct vm_area_struct *vma;
  2348. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2349. if (unlikely(vma == NULL))
  2350. return -ENOMEM;
  2351. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2352. vma->vm_mm = mm;
  2353. vma->vm_start = addr;
  2354. vma->vm_end = addr + len;
  2355. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2356. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2357. vma->vm_ops = &special_mapping_vmops;
  2358. vma->vm_private_data = pages;
  2359. ret = insert_vm_struct(mm, vma);
  2360. if (ret)
  2361. goto out;
  2362. mm->total_vm += len >> PAGE_SHIFT;
  2363. perf_event_mmap(vma);
  2364. return 0;
  2365. out:
  2366. kmem_cache_free(vm_area_cachep, vma);
  2367. return ret;
  2368. }
  2369. static DEFINE_MUTEX(mm_all_locks_mutex);
  2370. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2371. {
  2372. if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2373. /*
  2374. * The LSB of head.next can't change from under us
  2375. * because we hold the mm_all_locks_mutex.
  2376. */
  2377. mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
  2378. /*
  2379. * We can safely modify head.next after taking the
  2380. * anon_vma->root->mutex. If some other vma in this mm shares
  2381. * the same anon_vma we won't take it again.
  2382. *
  2383. * No need of atomic instructions here, head.next
  2384. * can't change from under us thanks to the
  2385. * anon_vma->root->mutex.
  2386. */
  2387. if (__test_and_set_bit(0, (unsigned long *)
  2388. &anon_vma->root->rb_root.rb_node))
  2389. BUG();
  2390. }
  2391. }
  2392. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2393. {
  2394. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2395. /*
  2396. * AS_MM_ALL_LOCKS can't change from under us because
  2397. * we hold the mm_all_locks_mutex.
  2398. *
  2399. * Operations on ->flags have to be atomic because
  2400. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2401. * mm_all_locks_mutex, there may be other cpus
  2402. * changing other bitflags in parallel to us.
  2403. */
  2404. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2405. BUG();
  2406. mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
  2407. }
  2408. }
  2409. /*
  2410. * This operation locks against the VM for all pte/vma/mm related
  2411. * operations that could ever happen on a certain mm. This includes
  2412. * vmtruncate, try_to_unmap, and all page faults.
  2413. *
  2414. * The caller must take the mmap_sem in write mode before calling
  2415. * mm_take_all_locks(). The caller isn't allowed to release the
  2416. * mmap_sem until mm_drop_all_locks() returns.
  2417. *
  2418. * mmap_sem in write mode is required in order to block all operations
  2419. * that could modify pagetables and free pages without need of
  2420. * altering the vma layout (for example populate_range() with
  2421. * nonlinear vmas). It's also needed in write mode to avoid new
  2422. * anon_vmas to be associated with existing vmas.
  2423. *
  2424. * A single task can't take more than one mm_take_all_locks() in a row
  2425. * or it would deadlock.
  2426. *
  2427. * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
  2428. * mapping->flags avoid to take the same lock twice, if more than one
  2429. * vma in this mm is backed by the same anon_vma or address_space.
  2430. *
  2431. * We can take all the locks in random order because the VM code
  2432. * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
  2433. * takes more than one of them in a row. Secondly we're protected
  2434. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2435. *
  2436. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2437. * that may have to take thousand of locks.
  2438. *
  2439. * mm_take_all_locks() can fail if it's interrupted by signals.
  2440. */
  2441. int mm_take_all_locks(struct mm_struct *mm)
  2442. {
  2443. struct vm_area_struct *vma;
  2444. struct anon_vma_chain *avc;
  2445. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2446. mutex_lock(&mm_all_locks_mutex);
  2447. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2448. if (signal_pending(current))
  2449. goto out_unlock;
  2450. if (vma->vm_file && vma->vm_file->f_mapping)
  2451. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2452. }
  2453. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2454. if (signal_pending(current))
  2455. goto out_unlock;
  2456. if (vma->anon_vma)
  2457. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2458. vm_lock_anon_vma(mm, avc->anon_vma);
  2459. }
  2460. return 0;
  2461. out_unlock:
  2462. mm_drop_all_locks(mm);
  2463. return -EINTR;
  2464. }
  2465. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2466. {
  2467. if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
  2468. /*
  2469. * The LSB of head.next can't change to 0 from under
  2470. * us because we hold the mm_all_locks_mutex.
  2471. *
  2472. * We must however clear the bitflag before unlocking
  2473. * the vma so the users using the anon_vma->rb_root will
  2474. * never see our bitflag.
  2475. *
  2476. * No need of atomic instructions here, head.next
  2477. * can't change from under us until we release the
  2478. * anon_vma->root->mutex.
  2479. */
  2480. if (!__test_and_clear_bit(0, (unsigned long *)
  2481. &anon_vma->root->rb_root.rb_node))
  2482. BUG();
  2483. anon_vma_unlock(anon_vma);
  2484. }
  2485. }
  2486. static void vm_unlock_mapping(struct address_space *mapping)
  2487. {
  2488. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2489. /*
  2490. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2491. * because we hold the mm_all_locks_mutex.
  2492. */
  2493. mutex_unlock(&mapping->i_mmap_mutex);
  2494. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2495. &mapping->flags))
  2496. BUG();
  2497. }
  2498. }
  2499. /*
  2500. * The mmap_sem cannot be released by the caller until
  2501. * mm_drop_all_locks() returns.
  2502. */
  2503. void mm_drop_all_locks(struct mm_struct *mm)
  2504. {
  2505. struct vm_area_struct *vma;
  2506. struct anon_vma_chain *avc;
  2507. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2508. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2509. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2510. if (vma->anon_vma)
  2511. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2512. vm_unlock_anon_vma(avc->anon_vma);
  2513. if (vma->vm_file && vma->vm_file->f_mapping)
  2514. vm_unlock_mapping(vma->vm_file->f_mapping);
  2515. }
  2516. mutex_unlock(&mm_all_locks_mutex);
  2517. }
  2518. /*
  2519. * initialise the VMA slab
  2520. */
  2521. void __init mmap_init(void)
  2522. {
  2523. int ret;
  2524. ret = percpu_counter_init(&vm_committed_as, 0);
  2525. VM_BUG_ON(ret);
  2526. }