mmap.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  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/ima.h>
  23. #include <linux/hugetlb.h>
  24. #include <linux/profile.h>
  25. #include <linux/module.h>
  26. #include <linux/mount.h>
  27. #include <linux/mempolicy.h>
  28. #include <linux/rmap.h>
  29. #include <linux/mmu_notifier.h>
  30. #include <linux/perf_counter.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/tlb.h>
  34. #include <asm/mmu_context.h>
  35. #include "internal.h"
  36. #ifndef arch_mmap_check
  37. #define arch_mmap_check(addr, len, flags) (0)
  38. #endif
  39. #ifndef arch_rebalance_pgtables
  40. #define arch_rebalance_pgtables(addr, len) (addr)
  41. #endif
  42. static void unmap_region(struct mm_struct *mm,
  43. struct vm_area_struct *vma, struct vm_area_struct *prev,
  44. unsigned long start, unsigned long end);
  45. /*
  46. * WARNING: the debugging will use recursive algorithms so never enable this
  47. * unless you know what you are doing.
  48. */
  49. #undef DEBUG_MM_RB
  50. /* description of effects of mapping type and prot in current implementation.
  51. * this is due to the limited x86 page protection hardware. The expected
  52. * behavior is in parens:
  53. *
  54. * map_type prot
  55. * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
  56. * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  57. * w: (no) no w: (no) no w: (yes) yes w: (no) no
  58. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  59. *
  60. * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
  61. * w: (no) no w: (no) no w: (copy) copy w: (no) no
  62. * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
  63. *
  64. */
  65. pgprot_t protection_map[16] = {
  66. __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
  67. __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
  68. };
  69. pgprot_t vm_get_page_prot(unsigned long vm_flags)
  70. {
  71. return __pgprot(pgprot_val(protection_map[vm_flags &
  72. (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
  73. pgprot_val(arch_vm_get_page_prot(vm_flags)));
  74. }
  75. EXPORT_SYMBOL(vm_get_page_prot);
  76. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  77. int sysctl_overcommit_ratio = 50; /* default is 50% */
  78. int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
  79. struct percpu_counter vm_committed_as;
  80. /* amount of vm to protect from userspace access */
  81. unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
  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. unsigned long n;
  109. free = global_page_state(NR_FILE_PAGES);
  110. free += nr_swap_pages;
  111. /*
  112. * Any slabs which are created with the
  113. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  114. * which are reclaimable, under pressure. The dentry
  115. * cache and most inode caches should fall into this
  116. */
  117. free += global_page_state(NR_SLAB_RECLAIMABLE);
  118. /*
  119. * Leave the last 3% for root
  120. */
  121. if (!cap_sys_admin)
  122. free -= free / 32;
  123. if (free > pages)
  124. return 0;
  125. /*
  126. * nr_free_pages() is very expensive on large systems,
  127. * only call if we're about to fail.
  128. */
  129. n = nr_free_pages();
  130. /*
  131. * Leave reserved pages. The pages are not for anonymous pages.
  132. */
  133. if (n <= totalreserve_pages)
  134. goto error;
  135. else
  136. n -= totalreserve_pages;
  137. /*
  138. * Leave the last 3% for root
  139. */
  140. if (!cap_sys_admin)
  141. n -= n / 32;
  142. free += n;
  143. if (free > pages)
  144. return 0;
  145. goto error;
  146. }
  147. allowed = (totalram_pages - hugetlb_total_pages())
  148. * sysctl_overcommit_ratio / 100;
  149. /*
  150. * Leave the last 3% for root
  151. */
  152. if (!cap_sys_admin)
  153. allowed -= allowed / 32;
  154. allowed += total_swap_pages;
  155. /* Don't let a single process grow too big:
  156. leave 3% of the size of this process for other processes */
  157. if (mm)
  158. allowed -= mm->total_vm / 32;
  159. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  160. return 0;
  161. error:
  162. vm_unacct_memory(pages);
  163. return -ENOMEM;
  164. }
  165. /*
  166. * Requires inode->i_mapping->i_mmap_lock
  167. */
  168. static void __remove_shared_vm_struct(struct vm_area_struct *vma,
  169. struct file *file, struct address_space *mapping)
  170. {
  171. if (vma->vm_flags & VM_DENYWRITE)
  172. atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
  173. if (vma->vm_flags & VM_SHARED)
  174. mapping->i_mmap_writable--;
  175. flush_dcache_mmap_lock(mapping);
  176. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  177. list_del_init(&vma->shared.vm_set.list);
  178. else
  179. vma_prio_tree_remove(vma, &mapping->i_mmap);
  180. flush_dcache_mmap_unlock(mapping);
  181. }
  182. /*
  183. * Unlink a file-based vm structure from its prio_tree, to hide
  184. * vma from rmap and vmtruncate before freeing its page tables.
  185. */
  186. void unlink_file_vma(struct vm_area_struct *vma)
  187. {
  188. struct file *file = vma->vm_file;
  189. if (file) {
  190. struct address_space *mapping = file->f_mapping;
  191. spin_lock(&mapping->i_mmap_lock);
  192. __remove_shared_vm_struct(vma, file, mapping);
  193. spin_unlock(&mapping->i_mmap_lock);
  194. }
  195. }
  196. /*
  197. * Close a vm structure and free it, returning the next.
  198. */
  199. static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
  200. {
  201. struct vm_area_struct *next = vma->vm_next;
  202. might_sleep();
  203. if (vma->vm_ops && vma->vm_ops->close)
  204. vma->vm_ops->close(vma);
  205. if (vma->vm_file) {
  206. fput(vma->vm_file);
  207. if (vma->vm_flags & VM_EXECUTABLE)
  208. removed_exe_file_vma(vma->vm_mm);
  209. }
  210. mpol_put(vma_policy(vma));
  211. kmem_cache_free(vm_area_cachep, vma);
  212. return next;
  213. }
  214. SYSCALL_DEFINE1(brk, unsigned long, brk)
  215. {
  216. unsigned long rlim, retval;
  217. unsigned long newbrk, oldbrk;
  218. struct mm_struct *mm = current->mm;
  219. unsigned long min_brk;
  220. down_write(&mm->mmap_sem);
  221. #ifdef CONFIG_COMPAT_BRK
  222. min_brk = mm->end_code;
  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 = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  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. #ifdef DEBUG_MM_RB
  262. static int browse_rb(struct rb_root *root)
  263. {
  264. int i = 0, j;
  265. struct rb_node *nd, *pn = NULL;
  266. unsigned long prev = 0, pend = 0;
  267. for (nd = rb_first(root); nd; nd = rb_next(nd)) {
  268. struct vm_area_struct *vma;
  269. vma = rb_entry(nd, struct vm_area_struct, vm_rb);
  270. if (vma->vm_start < prev)
  271. printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
  272. if (vma->vm_start < pend)
  273. printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
  274. if (vma->vm_start > vma->vm_end)
  275. printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
  276. i++;
  277. pn = nd;
  278. prev = vma->vm_start;
  279. pend = vma->vm_end;
  280. }
  281. j = 0;
  282. for (nd = pn; nd; nd = rb_prev(nd)) {
  283. j++;
  284. }
  285. if (i != j)
  286. printk("backwards %d, forwards %d\n", j, i), i = 0;
  287. return i;
  288. }
  289. void validate_mm(struct mm_struct *mm)
  290. {
  291. int bug = 0;
  292. int i = 0;
  293. struct vm_area_struct *tmp = mm->mmap;
  294. while (tmp) {
  295. tmp = tmp->vm_next;
  296. i++;
  297. }
  298. if (i != mm->map_count)
  299. printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
  300. i = browse_rb(&mm->mm_rb);
  301. if (i != mm->map_count)
  302. printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
  303. BUG_ON(bug);
  304. }
  305. #else
  306. #define validate_mm(mm) do { } while (0)
  307. #endif
  308. static struct vm_area_struct *
  309. find_vma_prepare(struct mm_struct *mm, unsigned long addr,
  310. struct vm_area_struct **pprev, struct rb_node ***rb_link,
  311. struct rb_node ** rb_parent)
  312. {
  313. struct vm_area_struct * vma;
  314. struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
  315. __rb_link = &mm->mm_rb.rb_node;
  316. rb_prev = __rb_parent = NULL;
  317. vma = NULL;
  318. while (*__rb_link) {
  319. struct vm_area_struct *vma_tmp;
  320. __rb_parent = *__rb_link;
  321. vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
  322. if (vma_tmp->vm_end > addr) {
  323. vma = vma_tmp;
  324. if (vma_tmp->vm_start <= addr)
  325. break;
  326. __rb_link = &__rb_parent->rb_left;
  327. } else {
  328. rb_prev = __rb_parent;
  329. __rb_link = &__rb_parent->rb_right;
  330. }
  331. }
  332. *pprev = NULL;
  333. if (rb_prev)
  334. *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  335. *rb_link = __rb_link;
  336. *rb_parent = __rb_parent;
  337. return vma;
  338. }
  339. static inline void
  340. __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
  341. struct vm_area_struct *prev, struct rb_node *rb_parent)
  342. {
  343. if (prev) {
  344. vma->vm_next = prev->vm_next;
  345. prev->vm_next = vma;
  346. } else {
  347. mm->mmap = vma;
  348. if (rb_parent)
  349. vma->vm_next = rb_entry(rb_parent,
  350. struct vm_area_struct, vm_rb);
  351. else
  352. vma->vm_next = NULL;
  353. }
  354. }
  355. void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
  356. struct rb_node **rb_link, struct rb_node *rb_parent)
  357. {
  358. rb_link_node(&vma->vm_rb, rb_parent, rb_link);
  359. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  360. }
  361. static void __vma_link_file(struct vm_area_struct *vma)
  362. {
  363. struct file *file;
  364. file = vma->vm_file;
  365. if (file) {
  366. struct address_space *mapping = file->f_mapping;
  367. if (vma->vm_flags & VM_DENYWRITE)
  368. atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
  369. if (vma->vm_flags & VM_SHARED)
  370. mapping->i_mmap_writable++;
  371. flush_dcache_mmap_lock(mapping);
  372. if (unlikely(vma->vm_flags & VM_NONLINEAR))
  373. vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
  374. else
  375. vma_prio_tree_insert(vma, &mapping->i_mmap);
  376. flush_dcache_mmap_unlock(mapping);
  377. }
  378. }
  379. static void
  380. __vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  381. struct vm_area_struct *prev, struct rb_node **rb_link,
  382. struct rb_node *rb_parent)
  383. {
  384. __vma_link_list(mm, vma, prev, rb_parent);
  385. __vma_link_rb(mm, vma, rb_link, rb_parent);
  386. __anon_vma_link(vma);
  387. }
  388. static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
  389. struct vm_area_struct *prev, struct rb_node **rb_link,
  390. struct rb_node *rb_parent)
  391. {
  392. struct address_space *mapping = NULL;
  393. if (vma->vm_file)
  394. mapping = vma->vm_file->f_mapping;
  395. if (mapping) {
  396. spin_lock(&mapping->i_mmap_lock);
  397. vma->vm_truncate_count = mapping->truncate_count;
  398. }
  399. anon_vma_lock(vma);
  400. __vma_link(mm, vma, prev, rb_link, rb_parent);
  401. __vma_link_file(vma);
  402. anon_vma_unlock(vma);
  403. if (mapping)
  404. spin_unlock(&mapping->i_mmap_lock);
  405. mm->map_count++;
  406. validate_mm(mm);
  407. }
  408. /*
  409. * Helper for vma_adjust in the split_vma insert case:
  410. * insert vm structure into list and rbtree and anon_vma,
  411. * but it has already been inserted into prio_tree earlier.
  412. */
  413. static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
  414. {
  415. struct vm_area_struct *__vma, *prev;
  416. struct rb_node **rb_link, *rb_parent;
  417. __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
  418. BUG_ON(__vma && __vma->vm_start < vma->vm_end);
  419. __vma_link(mm, vma, prev, rb_link, rb_parent);
  420. mm->map_count++;
  421. }
  422. static inline void
  423. __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
  424. struct vm_area_struct *prev)
  425. {
  426. prev->vm_next = vma->vm_next;
  427. rb_erase(&vma->vm_rb, &mm->mm_rb);
  428. if (mm->mmap_cache == vma)
  429. mm->mmap_cache = prev;
  430. }
  431. /*
  432. * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  433. * is already present in an i_mmap tree without adjusting the tree.
  434. * The following helper function should be used when such adjustments
  435. * are necessary. The "insert" vma (if any) is to be inserted
  436. * before we drop the necessary locks.
  437. */
  438. void vma_adjust(struct vm_area_struct *vma, unsigned long start,
  439. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  440. {
  441. struct mm_struct *mm = vma->vm_mm;
  442. struct vm_area_struct *next = vma->vm_next;
  443. struct vm_area_struct *importer = NULL;
  444. struct address_space *mapping = NULL;
  445. struct prio_tree_root *root = NULL;
  446. struct file *file = vma->vm_file;
  447. struct anon_vma *anon_vma = NULL;
  448. long adjust_next = 0;
  449. int remove_next = 0;
  450. if (next && !insert) {
  451. if (end >= next->vm_end) {
  452. /*
  453. * vma expands, overlapping all the next, and
  454. * perhaps the one after too (mprotect case 6).
  455. */
  456. again: remove_next = 1 + (end > next->vm_end);
  457. end = next->vm_end;
  458. anon_vma = next->anon_vma;
  459. importer = vma;
  460. } else if (end > next->vm_start) {
  461. /*
  462. * vma expands, overlapping part of the next:
  463. * mprotect case 5 shifting the boundary up.
  464. */
  465. adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
  466. anon_vma = next->anon_vma;
  467. importer = vma;
  468. } else if (end < vma->vm_end) {
  469. /*
  470. * vma shrinks, and !insert tells it's not
  471. * split_vma inserting another: so it must be
  472. * mprotect case 4 shifting the boundary down.
  473. */
  474. adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
  475. anon_vma = next->anon_vma;
  476. importer = next;
  477. }
  478. }
  479. if (file) {
  480. mapping = file->f_mapping;
  481. if (!(vma->vm_flags & VM_NONLINEAR))
  482. root = &mapping->i_mmap;
  483. spin_lock(&mapping->i_mmap_lock);
  484. if (importer &&
  485. vma->vm_truncate_count != next->vm_truncate_count) {
  486. /*
  487. * unmap_mapping_range might be in progress:
  488. * ensure that the expanding vma is rescanned.
  489. */
  490. importer->vm_truncate_count = 0;
  491. }
  492. if (insert) {
  493. insert->vm_truncate_count = vma->vm_truncate_count;
  494. /*
  495. * Put into prio_tree now, so instantiated pages
  496. * are visible to arm/parisc __flush_dcache_page
  497. * throughout; but we cannot insert into address
  498. * space until vma start or end is updated.
  499. */
  500. __vma_link_file(insert);
  501. }
  502. }
  503. /*
  504. * When changing only vma->vm_end, we don't really need
  505. * anon_vma lock: but is that case worth optimizing out?
  506. */
  507. if (vma->anon_vma)
  508. anon_vma = vma->anon_vma;
  509. if (anon_vma) {
  510. spin_lock(&anon_vma->lock);
  511. /*
  512. * Easily overlooked: when mprotect shifts the boundary,
  513. * make sure the expanding vma has anon_vma set if the
  514. * shrinking vma had, to cover any anon pages imported.
  515. */
  516. if (importer && !importer->anon_vma) {
  517. importer->anon_vma = anon_vma;
  518. __anon_vma_link(importer);
  519. }
  520. }
  521. if (root) {
  522. flush_dcache_mmap_lock(mapping);
  523. vma_prio_tree_remove(vma, root);
  524. if (adjust_next)
  525. vma_prio_tree_remove(next, root);
  526. }
  527. vma->vm_start = start;
  528. vma->vm_end = end;
  529. vma->vm_pgoff = pgoff;
  530. if (adjust_next) {
  531. next->vm_start += adjust_next << PAGE_SHIFT;
  532. next->vm_pgoff += adjust_next;
  533. }
  534. if (root) {
  535. if (adjust_next)
  536. vma_prio_tree_insert(next, root);
  537. vma_prio_tree_insert(vma, root);
  538. flush_dcache_mmap_unlock(mapping);
  539. }
  540. if (remove_next) {
  541. /*
  542. * vma_merge has merged next into vma, and needs
  543. * us to remove next before dropping the locks.
  544. */
  545. __vma_unlink(mm, next, vma);
  546. if (file)
  547. __remove_shared_vm_struct(next, file, mapping);
  548. if (next->anon_vma)
  549. __anon_vma_merge(vma, next);
  550. } else if (insert) {
  551. /*
  552. * split_vma has split insert from vma, and needs
  553. * us to insert it before dropping the locks
  554. * (it may either follow vma or precede it).
  555. */
  556. __insert_vm_struct(mm, insert);
  557. }
  558. if (anon_vma)
  559. spin_unlock(&anon_vma->lock);
  560. if (mapping)
  561. spin_unlock(&mapping->i_mmap_lock);
  562. if (remove_next) {
  563. if (file) {
  564. fput(file);
  565. if (next->vm_flags & VM_EXECUTABLE)
  566. removed_exe_file_vma(mm);
  567. }
  568. mm->map_count--;
  569. mpol_put(vma_policy(next));
  570. kmem_cache_free(vm_area_cachep, next);
  571. /*
  572. * In mprotect's case 6 (see comments on vma_merge),
  573. * we must remove another next too. It would clutter
  574. * up the code too much to do both in one go.
  575. */
  576. if (remove_next == 2) {
  577. next = vma->vm_next;
  578. goto again;
  579. }
  580. }
  581. validate_mm(mm);
  582. }
  583. /* Flags that can be inherited from an existing mapping when merging */
  584. #define VM_MERGEABLE_FLAGS (VM_CAN_NONLINEAR)
  585. /*
  586. * If the vma has a ->close operation then the driver probably needs to release
  587. * per-vma resources, so we don't attempt to merge those.
  588. */
  589. static inline int is_mergeable_vma(struct vm_area_struct *vma,
  590. struct file *file, unsigned long vm_flags)
  591. {
  592. if ((vma->vm_flags ^ vm_flags) & ~VM_MERGEABLE_FLAGS)
  593. return 0;
  594. if (vma->vm_file != file)
  595. return 0;
  596. if (vma->vm_ops && vma->vm_ops->close)
  597. return 0;
  598. return 1;
  599. }
  600. static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
  601. struct anon_vma *anon_vma2)
  602. {
  603. return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
  604. }
  605. /*
  606. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  607. * in front of (at a lower virtual address and file offset than) the vma.
  608. *
  609. * We cannot merge two vmas if they have differently assigned (non-NULL)
  610. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  611. *
  612. * We don't check here for the merged mmap wrapping around the end of pagecache
  613. * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
  614. * wrap, nor mmaps which cover the final page at index -1UL.
  615. */
  616. static int
  617. can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
  618. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  619. {
  620. if (is_mergeable_vma(vma, file, vm_flags) &&
  621. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  622. if (vma->vm_pgoff == vm_pgoff)
  623. return 1;
  624. }
  625. return 0;
  626. }
  627. /*
  628. * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
  629. * beyond (at a higher virtual address and file offset than) the vma.
  630. *
  631. * We cannot merge two vmas if they have differently assigned (non-NULL)
  632. * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
  633. */
  634. static int
  635. can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
  636. struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
  637. {
  638. if (is_mergeable_vma(vma, file, vm_flags) &&
  639. is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
  640. pgoff_t vm_pglen;
  641. vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  642. if (vma->vm_pgoff + vm_pglen == vm_pgoff)
  643. return 1;
  644. }
  645. return 0;
  646. }
  647. /*
  648. * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
  649. * whether that can be merged with its predecessor or its successor.
  650. * Or both (it neatly fills a hole).
  651. *
  652. * In most cases - when called for mmap, brk or mremap - [addr,end) is
  653. * certain not to be mapped by the time vma_merge is called; but when
  654. * called for mprotect, it is certain to be already mapped (either at
  655. * an offset within prev, or at the start of next), and the flags of
  656. * this area are about to be changed to vm_flags - and the no-change
  657. * case has already been eliminated.
  658. *
  659. * The following mprotect cases have to be considered, where AAAA is
  660. * the area passed down from mprotect_fixup, never extending beyond one
  661. * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
  662. *
  663. * AAAA AAAA AAAA AAAA
  664. * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
  665. * cannot merge might become might become might become
  666. * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
  667. * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
  668. * mremap move: PPPPNNNNNNNN 8
  669. * AAAA
  670. * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
  671. * might become case 1 below case 2 below case 3 below
  672. *
  673. * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
  674. * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
  675. */
  676. struct vm_area_struct *vma_merge(struct mm_struct *mm,
  677. struct vm_area_struct *prev, unsigned long addr,
  678. unsigned long end, unsigned long vm_flags,
  679. struct anon_vma *anon_vma, struct file *file,
  680. pgoff_t pgoff, struct mempolicy *policy)
  681. {
  682. pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
  683. struct vm_area_struct *area, *next;
  684. /*
  685. * We later require that vma->vm_flags == vm_flags,
  686. * so this tests vma->vm_flags & VM_SPECIAL, too.
  687. */
  688. if (vm_flags & VM_SPECIAL)
  689. return NULL;
  690. if (prev)
  691. next = prev->vm_next;
  692. else
  693. next = mm->mmap;
  694. area = next;
  695. if (next && next->vm_end == end) /* cases 6, 7, 8 */
  696. next = next->vm_next;
  697. /*
  698. * Can it merge with the predecessor?
  699. */
  700. if (prev && prev->vm_end == addr &&
  701. mpol_equal(vma_policy(prev), policy) &&
  702. can_vma_merge_after(prev, vm_flags,
  703. anon_vma, file, pgoff)) {
  704. /*
  705. * OK, it can. Can we now merge in the successor as well?
  706. */
  707. if (next && end == next->vm_start &&
  708. mpol_equal(policy, vma_policy(next)) &&
  709. can_vma_merge_before(next, vm_flags,
  710. anon_vma, file, pgoff+pglen) &&
  711. is_mergeable_anon_vma(prev->anon_vma,
  712. next->anon_vma)) {
  713. /* cases 1, 6 */
  714. vma_adjust(prev, prev->vm_start,
  715. next->vm_end, prev->vm_pgoff, NULL);
  716. } else /* cases 2, 5, 7 */
  717. vma_adjust(prev, prev->vm_start,
  718. end, prev->vm_pgoff, NULL);
  719. return prev;
  720. }
  721. /*
  722. * Can this new request be merged in front of next?
  723. */
  724. if (next && end == next->vm_start &&
  725. mpol_equal(policy, vma_policy(next)) &&
  726. can_vma_merge_before(next, vm_flags,
  727. anon_vma, file, pgoff+pglen)) {
  728. if (prev && addr < prev->vm_end) /* case 4 */
  729. vma_adjust(prev, prev->vm_start,
  730. addr, prev->vm_pgoff, NULL);
  731. else /* cases 3, 8 */
  732. vma_adjust(area, addr, next->vm_end,
  733. next->vm_pgoff - pglen, NULL);
  734. return area;
  735. }
  736. return NULL;
  737. }
  738. /*
  739. * find_mergeable_anon_vma is used by anon_vma_prepare, to check
  740. * neighbouring vmas for a suitable anon_vma, before it goes off
  741. * to allocate a new anon_vma. It checks because a repetitive
  742. * sequence of mprotects and faults may otherwise lead to distinct
  743. * anon_vmas being allocated, preventing vma merge in subsequent
  744. * mprotect.
  745. */
  746. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
  747. {
  748. struct vm_area_struct *near;
  749. unsigned long vm_flags;
  750. near = vma->vm_next;
  751. if (!near)
  752. goto try_prev;
  753. /*
  754. * Since only mprotect tries to remerge vmas, match flags
  755. * which might be mprotected into each other later on.
  756. * Neither mlock nor madvise tries to remerge at present,
  757. * so leave their flags as obstructing a merge.
  758. */
  759. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  760. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  761. if (near->anon_vma && vma->vm_end == near->vm_start &&
  762. mpol_equal(vma_policy(vma), vma_policy(near)) &&
  763. can_vma_merge_before(near, vm_flags,
  764. NULL, vma->vm_file, vma->vm_pgoff +
  765. ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
  766. return near->anon_vma;
  767. try_prev:
  768. /*
  769. * It is potentially slow to have to call find_vma_prev here.
  770. * But it's only on the first write fault on the vma, not
  771. * every time, and we could devise a way to avoid it later
  772. * (e.g. stash info in next's anon_vma_node when assigning
  773. * an anon_vma, or when trying vma_merge). Another time.
  774. */
  775. BUG_ON(find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma);
  776. if (!near)
  777. goto none;
  778. vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
  779. vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);
  780. if (near->anon_vma && near->vm_end == vma->vm_start &&
  781. mpol_equal(vma_policy(near), vma_policy(vma)) &&
  782. can_vma_merge_after(near, vm_flags,
  783. NULL, vma->vm_file, vma->vm_pgoff))
  784. return near->anon_vma;
  785. none:
  786. /*
  787. * There's no absolute need to look only at touching neighbours:
  788. * we could search further afield for "compatible" anon_vmas.
  789. * But it would probably just be a waste of time searching,
  790. * or lead to too many vmas hanging off the same anon_vma.
  791. * We're trying to allow mprotect remerging later on,
  792. * not trying to minimize memory used for anon_vmas.
  793. */
  794. return NULL;
  795. }
  796. #ifdef CONFIG_PROC_FS
  797. void vm_stat_account(struct mm_struct *mm, unsigned long flags,
  798. struct file *file, long pages)
  799. {
  800. const unsigned long stack_flags
  801. = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
  802. if (file) {
  803. mm->shared_vm += pages;
  804. if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
  805. mm->exec_vm += pages;
  806. } else if (flags & stack_flags)
  807. mm->stack_vm += pages;
  808. if (flags & (VM_RESERVED|VM_IO))
  809. mm->reserved_vm += pages;
  810. }
  811. #endif /* CONFIG_PROC_FS */
  812. /*
  813. * The caller must hold down_write(current->mm->mmap_sem).
  814. */
  815. unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  816. unsigned long len, unsigned long prot,
  817. unsigned long flags, unsigned long pgoff)
  818. {
  819. struct mm_struct * mm = current->mm;
  820. struct inode *inode;
  821. unsigned int vm_flags;
  822. int error;
  823. unsigned long reqprot = prot;
  824. /*
  825. * Does the application expect PROT_READ to imply PROT_EXEC?
  826. *
  827. * (the exception is when the underlying filesystem is noexec
  828. * mounted, in which case we dont add PROT_EXEC.)
  829. */
  830. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  831. if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
  832. prot |= PROT_EXEC;
  833. if (!len)
  834. return -EINVAL;
  835. if (!(flags & MAP_FIXED))
  836. addr = round_hint_to_min(addr);
  837. error = arch_mmap_check(addr, len, flags);
  838. if (error)
  839. return error;
  840. /* Careful about overflows.. */
  841. len = PAGE_ALIGN(len);
  842. if (!len || len > TASK_SIZE)
  843. return -ENOMEM;
  844. /* offset overflow? */
  845. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  846. return -EOVERFLOW;
  847. /* Too many mappings? */
  848. if (mm->map_count > sysctl_max_map_count)
  849. return -ENOMEM;
  850. /* Obtain the address to map to. we verify (or select) it and ensure
  851. * that it represents a valid section of the address space.
  852. */
  853. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  854. if (addr & ~PAGE_MASK)
  855. return addr;
  856. /* Do simple checking here so the lower-level routines won't have
  857. * to. we assume access permissions have been handled by the open
  858. * of the memory object, so we don't do any here.
  859. */
  860. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  861. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  862. if (flags & MAP_LOCKED) {
  863. if (!can_do_mlock())
  864. return -EPERM;
  865. vm_flags |= VM_LOCKED;
  866. }
  867. /* mlock MCL_FUTURE? */
  868. if (vm_flags & VM_LOCKED) {
  869. unsigned long locked, lock_limit;
  870. locked = len >> PAGE_SHIFT;
  871. locked += mm->locked_vm;
  872. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  873. lock_limit >>= PAGE_SHIFT;
  874. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  875. return -EAGAIN;
  876. }
  877. inode = file ? file->f_path.dentry->d_inode : NULL;
  878. if (file) {
  879. switch (flags & MAP_TYPE) {
  880. case MAP_SHARED:
  881. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  882. return -EACCES;
  883. /*
  884. * Make sure we don't allow writing to an append-only
  885. * file..
  886. */
  887. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  888. return -EACCES;
  889. /*
  890. * Make sure there are no mandatory locks on the file.
  891. */
  892. if (locks_verify_locked(inode))
  893. return -EAGAIN;
  894. vm_flags |= VM_SHARED | VM_MAYSHARE;
  895. if (!(file->f_mode & FMODE_WRITE))
  896. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  897. /* fall through */
  898. case MAP_PRIVATE:
  899. if (!(file->f_mode & FMODE_READ))
  900. return -EACCES;
  901. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  902. if (vm_flags & VM_EXEC)
  903. return -EPERM;
  904. vm_flags &= ~VM_MAYEXEC;
  905. }
  906. if (!file->f_op || !file->f_op->mmap)
  907. return -ENODEV;
  908. break;
  909. default:
  910. return -EINVAL;
  911. }
  912. } else {
  913. switch (flags & MAP_TYPE) {
  914. case MAP_SHARED:
  915. /*
  916. * Ignore pgoff.
  917. */
  918. pgoff = 0;
  919. vm_flags |= VM_SHARED | VM_MAYSHARE;
  920. break;
  921. case MAP_PRIVATE:
  922. /*
  923. * Set pgoff according to addr for anon_vma.
  924. */
  925. pgoff = addr >> PAGE_SHIFT;
  926. break;
  927. default:
  928. return -EINVAL;
  929. }
  930. }
  931. error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  932. if (error)
  933. return error;
  934. error = ima_file_mmap(file, prot);
  935. if (error)
  936. return error;
  937. return mmap_region(file, addr, len, flags, vm_flags, pgoff);
  938. }
  939. EXPORT_SYMBOL(do_mmap_pgoff);
  940. /*
  941. * Some shared mappigns will want the pages marked read-only
  942. * to track write events. If so, we'll downgrade vm_page_prot
  943. * to the private version (using protection_map[] without the
  944. * VM_SHARED bit).
  945. */
  946. int vma_wants_writenotify(struct vm_area_struct *vma)
  947. {
  948. unsigned int vm_flags = vma->vm_flags;
  949. /* If it was private or non-writable, the write bit is already clear */
  950. if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
  951. return 0;
  952. /* The backer wishes to know when pages are first written to? */
  953. if (vma->vm_ops && vma->vm_ops->page_mkwrite)
  954. return 1;
  955. /* The open routine did something to the protections already? */
  956. if (pgprot_val(vma->vm_page_prot) !=
  957. pgprot_val(vm_get_page_prot(vm_flags)))
  958. return 0;
  959. /* Specialty mapping? */
  960. if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
  961. return 0;
  962. /* Can the mapping track the dirty pages? */
  963. return vma->vm_file && vma->vm_file->f_mapping &&
  964. mapping_cap_account_dirty(vma->vm_file->f_mapping);
  965. }
  966. /*
  967. * We account for memory if it's a private writeable mapping,
  968. * not hugepages and VM_NORESERVE wasn't set.
  969. */
  970. static inline int accountable_mapping(struct file *file, unsigned int vm_flags)
  971. {
  972. /*
  973. * hugetlb has its own accounting separate from the core VM
  974. * VM_HUGETLB may not be set yet so we cannot check for that flag.
  975. */
  976. if (file && is_file_hugepages(file))
  977. return 0;
  978. return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
  979. }
  980. unsigned long mmap_region(struct file *file, unsigned long addr,
  981. unsigned long len, unsigned long flags,
  982. unsigned int vm_flags, unsigned long pgoff)
  983. {
  984. struct mm_struct *mm = current->mm;
  985. struct vm_area_struct *vma, *prev;
  986. int correct_wcount = 0;
  987. int error;
  988. struct rb_node **rb_link, *rb_parent;
  989. unsigned long charged = 0;
  990. struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
  991. /* Clear old maps */
  992. error = -ENOMEM;
  993. munmap_back:
  994. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  995. if (vma && vma->vm_start < addr + len) {
  996. if (do_munmap(mm, addr, len))
  997. return -ENOMEM;
  998. goto munmap_back;
  999. }
  1000. /* Check against address space limit. */
  1001. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1002. return -ENOMEM;
  1003. /*
  1004. * Set 'VM_NORESERVE' if we should not account for the
  1005. * memory use of this mapping.
  1006. */
  1007. if ((flags & MAP_NORESERVE)) {
  1008. /* We honor MAP_NORESERVE if allowed to overcommit */
  1009. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  1010. vm_flags |= VM_NORESERVE;
  1011. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  1012. if (file && is_file_hugepages(file))
  1013. vm_flags |= VM_NORESERVE;
  1014. }
  1015. /*
  1016. * Private writable mapping: check memory availability
  1017. */
  1018. if (accountable_mapping(file, vm_flags)) {
  1019. charged = len >> PAGE_SHIFT;
  1020. if (security_vm_enough_memory(charged))
  1021. return -ENOMEM;
  1022. vm_flags |= VM_ACCOUNT;
  1023. }
  1024. /*
  1025. * Can we just expand an old mapping?
  1026. */
  1027. vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
  1028. if (vma)
  1029. goto out;
  1030. /*
  1031. * Determine the object being mapped and call the appropriate
  1032. * specific mapper. the address has already been validated, but
  1033. * not unmapped, but the maps are removed from the list.
  1034. */
  1035. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1036. if (!vma) {
  1037. error = -ENOMEM;
  1038. goto unacct_error;
  1039. }
  1040. vma->vm_mm = mm;
  1041. vma->vm_start = addr;
  1042. vma->vm_end = addr + len;
  1043. vma->vm_flags = vm_flags;
  1044. vma->vm_page_prot = vm_get_page_prot(vm_flags);
  1045. vma->vm_pgoff = pgoff;
  1046. if (file) {
  1047. error = -EINVAL;
  1048. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  1049. goto free_vma;
  1050. if (vm_flags & VM_DENYWRITE) {
  1051. error = deny_write_access(file);
  1052. if (error)
  1053. goto free_vma;
  1054. correct_wcount = 1;
  1055. }
  1056. vma->vm_file = file;
  1057. get_file(file);
  1058. error = file->f_op->mmap(file, vma);
  1059. if (error)
  1060. goto unmap_and_free_vma;
  1061. if (vm_flags & VM_EXECUTABLE)
  1062. added_exe_file_vma(mm);
  1063. } else if (vm_flags & VM_SHARED) {
  1064. error = shmem_zero_setup(vma);
  1065. if (error)
  1066. goto free_vma;
  1067. }
  1068. /* Can addr have changed??
  1069. *
  1070. * Answer: Yes, several device drivers can do it in their
  1071. * f_op->mmap method. -DaveM
  1072. */
  1073. addr = vma->vm_start;
  1074. pgoff = vma->vm_pgoff;
  1075. vm_flags = vma->vm_flags;
  1076. if (vma_wants_writenotify(vma))
  1077. vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
  1078. vma_link(mm, vma, prev, rb_link, rb_parent);
  1079. file = vma->vm_file;
  1080. /* Once vma denies write, undo our temporary denial count */
  1081. if (correct_wcount)
  1082. atomic_inc(&inode->i_writecount);
  1083. out:
  1084. perf_counter_mmap(vma);
  1085. mm->total_vm += len >> PAGE_SHIFT;
  1086. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1087. if (vm_flags & VM_LOCKED) {
  1088. /*
  1089. * makes pages present; downgrades, drops, reacquires mmap_sem
  1090. */
  1091. long nr_pages = mlock_vma_pages_range(vma, addr, addr + len);
  1092. if (nr_pages < 0)
  1093. return nr_pages; /* vma gone! */
  1094. mm->locked_vm += (len >> PAGE_SHIFT) - nr_pages;
  1095. } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
  1096. make_pages_present(addr, addr + len);
  1097. return addr;
  1098. unmap_and_free_vma:
  1099. if (correct_wcount)
  1100. atomic_inc(&inode->i_writecount);
  1101. vma->vm_file = NULL;
  1102. fput(file);
  1103. /* Undo any partial mapping done by a device driver. */
  1104. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1105. charged = 0;
  1106. free_vma:
  1107. kmem_cache_free(vm_area_cachep, vma);
  1108. unacct_error:
  1109. if (charged)
  1110. vm_unacct_memory(charged);
  1111. return error;
  1112. }
  1113. /* Get an address range which is currently unmapped.
  1114. * For shmat() with addr=0.
  1115. *
  1116. * Ugly calling convention alert:
  1117. * Return value with the low bits set means error value,
  1118. * ie
  1119. * if (ret & ~PAGE_MASK)
  1120. * error = ret;
  1121. *
  1122. * This function "knows" that -ENOMEM has the bits set.
  1123. */
  1124. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1125. unsigned long
  1126. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1127. unsigned long len, unsigned long pgoff, unsigned long flags)
  1128. {
  1129. struct mm_struct *mm = current->mm;
  1130. struct vm_area_struct *vma;
  1131. unsigned long start_addr;
  1132. if (len > TASK_SIZE)
  1133. return -ENOMEM;
  1134. if (flags & MAP_FIXED)
  1135. return addr;
  1136. if (addr) {
  1137. addr = PAGE_ALIGN(addr);
  1138. vma = find_vma(mm, addr);
  1139. if (TASK_SIZE - len >= addr &&
  1140. (!vma || addr + len <= vma->vm_start))
  1141. return addr;
  1142. }
  1143. if (len > mm->cached_hole_size) {
  1144. start_addr = addr = mm->free_area_cache;
  1145. } else {
  1146. start_addr = addr = TASK_UNMAPPED_BASE;
  1147. mm->cached_hole_size = 0;
  1148. }
  1149. full_search:
  1150. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  1151. /* At this point: (!vma || addr < vma->vm_end). */
  1152. if (TASK_SIZE - len < addr) {
  1153. /*
  1154. * Start a new search - just in case we missed
  1155. * some holes.
  1156. */
  1157. if (start_addr != TASK_UNMAPPED_BASE) {
  1158. addr = TASK_UNMAPPED_BASE;
  1159. start_addr = addr;
  1160. mm->cached_hole_size = 0;
  1161. goto full_search;
  1162. }
  1163. return -ENOMEM;
  1164. }
  1165. if (!vma || addr + len <= vma->vm_start) {
  1166. /*
  1167. * Remember the place where we stopped the search:
  1168. */
  1169. mm->free_area_cache = addr + len;
  1170. return addr;
  1171. }
  1172. if (addr + mm->cached_hole_size < vma->vm_start)
  1173. mm->cached_hole_size = vma->vm_start - addr;
  1174. addr = vma->vm_end;
  1175. }
  1176. }
  1177. #endif
  1178. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1179. {
  1180. /*
  1181. * Is this a new hole at the lowest possible address?
  1182. */
  1183. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) {
  1184. mm->free_area_cache = addr;
  1185. mm->cached_hole_size = ~0UL;
  1186. }
  1187. }
  1188. /*
  1189. * This mmap-allocator allocates new areas top-down from below the
  1190. * stack's low limit (the base):
  1191. */
  1192. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1193. unsigned long
  1194. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1195. const unsigned long len, const unsigned long pgoff,
  1196. const unsigned long flags)
  1197. {
  1198. struct vm_area_struct *vma;
  1199. struct mm_struct *mm = current->mm;
  1200. unsigned long addr = addr0;
  1201. /* requested length too big for entire address space */
  1202. if (len > TASK_SIZE)
  1203. return -ENOMEM;
  1204. if (flags & MAP_FIXED)
  1205. return addr;
  1206. /* requesting a specific address */
  1207. if (addr) {
  1208. addr = PAGE_ALIGN(addr);
  1209. vma = find_vma(mm, addr);
  1210. if (TASK_SIZE - len >= addr &&
  1211. (!vma || addr + len <= vma->vm_start))
  1212. return addr;
  1213. }
  1214. /* check if free_area_cache is useful for us */
  1215. if (len <= mm->cached_hole_size) {
  1216. mm->cached_hole_size = 0;
  1217. mm->free_area_cache = mm->mmap_base;
  1218. }
  1219. /* either no address requested or can't fit in requested address hole */
  1220. addr = mm->free_area_cache;
  1221. /* make sure it can fit in the remaining address space */
  1222. if (addr > len) {
  1223. vma = find_vma(mm, addr-len);
  1224. if (!vma || addr <= vma->vm_start)
  1225. /* remember the address as a hint for next time */
  1226. return (mm->free_area_cache = addr-len);
  1227. }
  1228. if (mm->mmap_base < len)
  1229. goto bottomup;
  1230. addr = mm->mmap_base-len;
  1231. do {
  1232. /*
  1233. * Lookup failure means no vma is above this address,
  1234. * else if new region fits below vma->vm_start,
  1235. * return with success:
  1236. */
  1237. vma = find_vma(mm, addr);
  1238. if (!vma || addr+len <= vma->vm_start)
  1239. /* remember the address as a hint for next time */
  1240. return (mm->free_area_cache = addr);
  1241. /* remember the largest hole we saw so far */
  1242. if (addr + mm->cached_hole_size < vma->vm_start)
  1243. mm->cached_hole_size = vma->vm_start - addr;
  1244. /* try just below the current vma->vm_start */
  1245. addr = vma->vm_start-len;
  1246. } while (len < vma->vm_start);
  1247. bottomup:
  1248. /*
  1249. * A failed mmap() very likely causes application failure,
  1250. * so fall back to the bottom-up function here. This scenario
  1251. * can happen with large stack limits and large mmap()
  1252. * allocations.
  1253. */
  1254. mm->cached_hole_size = ~0UL;
  1255. mm->free_area_cache = TASK_UNMAPPED_BASE;
  1256. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  1257. /*
  1258. * Restore the topdown base:
  1259. */
  1260. mm->free_area_cache = mm->mmap_base;
  1261. mm->cached_hole_size = ~0UL;
  1262. return addr;
  1263. }
  1264. #endif
  1265. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1266. {
  1267. /*
  1268. * Is this a new hole at the highest possible address?
  1269. */
  1270. if (addr > mm->free_area_cache)
  1271. mm->free_area_cache = addr;
  1272. /* dont allow allocations above current base */
  1273. if (mm->free_area_cache > mm->mmap_base)
  1274. mm->free_area_cache = mm->mmap_base;
  1275. }
  1276. unsigned long
  1277. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1278. unsigned long pgoff, unsigned long flags)
  1279. {
  1280. unsigned long (*get_area)(struct file *, unsigned long,
  1281. unsigned long, unsigned long, unsigned long);
  1282. get_area = current->mm->get_unmapped_area;
  1283. if (file && file->f_op && file->f_op->get_unmapped_area)
  1284. get_area = file->f_op->get_unmapped_area;
  1285. addr = get_area(file, addr, len, pgoff, flags);
  1286. if (IS_ERR_VALUE(addr))
  1287. return addr;
  1288. if (addr > TASK_SIZE - len)
  1289. return -ENOMEM;
  1290. if (addr & ~PAGE_MASK)
  1291. return -EINVAL;
  1292. return arch_rebalance_pgtables(addr, len);
  1293. }
  1294. EXPORT_SYMBOL(get_unmapped_area);
  1295. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1296. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1297. {
  1298. struct vm_area_struct *vma = NULL;
  1299. if (mm) {
  1300. /* Check the cache first. */
  1301. /* (Cache hit rate is typically around 35%.) */
  1302. vma = mm->mmap_cache;
  1303. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1304. struct rb_node * rb_node;
  1305. rb_node = mm->mm_rb.rb_node;
  1306. vma = NULL;
  1307. while (rb_node) {
  1308. struct vm_area_struct * vma_tmp;
  1309. vma_tmp = rb_entry(rb_node,
  1310. struct vm_area_struct, vm_rb);
  1311. if (vma_tmp->vm_end > addr) {
  1312. vma = vma_tmp;
  1313. if (vma_tmp->vm_start <= addr)
  1314. break;
  1315. rb_node = rb_node->rb_left;
  1316. } else
  1317. rb_node = rb_node->rb_right;
  1318. }
  1319. if (vma)
  1320. mm->mmap_cache = vma;
  1321. }
  1322. }
  1323. return vma;
  1324. }
  1325. EXPORT_SYMBOL(find_vma);
  1326. /* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
  1327. struct vm_area_struct *
  1328. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1329. struct vm_area_struct **pprev)
  1330. {
  1331. struct vm_area_struct *vma = NULL, *prev = NULL;
  1332. struct rb_node *rb_node;
  1333. if (!mm)
  1334. goto out;
  1335. /* Guard against addr being lower than the first VMA */
  1336. vma = mm->mmap;
  1337. /* Go through the RB tree quickly. */
  1338. rb_node = mm->mm_rb.rb_node;
  1339. while (rb_node) {
  1340. struct vm_area_struct *vma_tmp;
  1341. vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1342. if (addr < vma_tmp->vm_end) {
  1343. rb_node = rb_node->rb_left;
  1344. } else {
  1345. prev = vma_tmp;
  1346. if (!prev->vm_next || (addr < prev->vm_next->vm_end))
  1347. break;
  1348. rb_node = rb_node->rb_right;
  1349. }
  1350. }
  1351. out:
  1352. *pprev = prev;
  1353. return prev ? prev->vm_next : vma;
  1354. }
  1355. /*
  1356. * Verify that the stack growth is acceptable and
  1357. * update accounting. This is shared with both the
  1358. * grow-up and grow-down cases.
  1359. */
  1360. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1361. {
  1362. struct mm_struct *mm = vma->vm_mm;
  1363. struct rlimit *rlim = current->signal->rlim;
  1364. unsigned long new_start;
  1365. /* address space limit tests */
  1366. if (!may_expand_vm(mm, grow))
  1367. return -ENOMEM;
  1368. /* Stack limit test */
  1369. if (size > rlim[RLIMIT_STACK].rlim_cur)
  1370. return -ENOMEM;
  1371. /* mlock limit tests */
  1372. if (vma->vm_flags & VM_LOCKED) {
  1373. unsigned long locked;
  1374. unsigned long limit;
  1375. locked = mm->locked_vm + grow;
  1376. limit = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  1377. if (locked > limit && !capable(CAP_IPC_LOCK))
  1378. return -ENOMEM;
  1379. }
  1380. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1381. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1382. vma->vm_end - size;
  1383. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1384. return -EFAULT;
  1385. /*
  1386. * Overcommit.. This must be the final test, as it will
  1387. * update security statistics.
  1388. */
  1389. if (security_vm_enough_memory_mm(mm, grow))
  1390. return -ENOMEM;
  1391. /* Ok, everything looks good - let it rip */
  1392. mm->total_vm += grow;
  1393. if (vma->vm_flags & VM_LOCKED)
  1394. mm->locked_vm += grow;
  1395. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1396. return 0;
  1397. }
  1398. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1399. /*
  1400. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1401. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1402. */
  1403. #ifndef CONFIG_IA64
  1404. static
  1405. #endif
  1406. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1407. {
  1408. int error;
  1409. if (!(vma->vm_flags & VM_GROWSUP))
  1410. return -EFAULT;
  1411. /*
  1412. * We must make sure the anon_vma is allocated
  1413. * so that the anon_vma locking is not a noop.
  1414. */
  1415. if (unlikely(anon_vma_prepare(vma)))
  1416. return -ENOMEM;
  1417. anon_vma_lock(vma);
  1418. /*
  1419. * vma->vm_start/vm_end cannot change under us because the caller
  1420. * is required to hold the mmap_sem in read mode. We need the
  1421. * anon_vma lock to serialize against concurrent expand_stacks.
  1422. * Also guard against wrapping around to address 0.
  1423. */
  1424. if (address < PAGE_ALIGN(address+4))
  1425. address = PAGE_ALIGN(address+4);
  1426. else {
  1427. anon_vma_unlock(vma);
  1428. return -ENOMEM;
  1429. }
  1430. error = 0;
  1431. /* Somebody else might have raced and expanded it already */
  1432. if (address > vma->vm_end) {
  1433. unsigned long size, grow;
  1434. size = address - vma->vm_start;
  1435. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1436. error = acct_stack_growth(vma, size, grow);
  1437. if (!error)
  1438. vma->vm_end = address;
  1439. }
  1440. anon_vma_unlock(vma);
  1441. return error;
  1442. }
  1443. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1444. /*
  1445. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1446. */
  1447. static int expand_downwards(struct vm_area_struct *vma,
  1448. unsigned long address)
  1449. {
  1450. int error;
  1451. /*
  1452. * We must make sure the anon_vma is allocated
  1453. * so that the anon_vma locking is not a noop.
  1454. */
  1455. if (unlikely(anon_vma_prepare(vma)))
  1456. return -ENOMEM;
  1457. address &= PAGE_MASK;
  1458. error = security_file_mmap(NULL, 0, 0, 0, address, 1);
  1459. if (error)
  1460. return error;
  1461. anon_vma_lock(vma);
  1462. /*
  1463. * vma->vm_start/vm_end cannot change under us because the caller
  1464. * is required to hold the mmap_sem in read mode. We need the
  1465. * anon_vma lock to serialize against concurrent expand_stacks.
  1466. */
  1467. /* Somebody else might have raced and expanded it already */
  1468. if (address < vma->vm_start) {
  1469. unsigned long size, grow;
  1470. size = vma->vm_end - address;
  1471. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1472. error = acct_stack_growth(vma, size, grow);
  1473. if (!error) {
  1474. vma->vm_start = address;
  1475. vma->vm_pgoff -= grow;
  1476. }
  1477. }
  1478. anon_vma_unlock(vma);
  1479. return error;
  1480. }
  1481. int expand_stack_downwards(struct vm_area_struct *vma, unsigned long address)
  1482. {
  1483. return expand_downwards(vma, address);
  1484. }
  1485. #ifdef CONFIG_STACK_GROWSUP
  1486. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1487. {
  1488. return expand_upwards(vma, address);
  1489. }
  1490. struct vm_area_struct *
  1491. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1492. {
  1493. struct vm_area_struct *vma, *prev;
  1494. addr &= PAGE_MASK;
  1495. vma = find_vma_prev(mm, addr, &prev);
  1496. if (vma && (vma->vm_start <= addr))
  1497. return vma;
  1498. if (!prev || expand_stack(prev, addr))
  1499. return NULL;
  1500. if (prev->vm_flags & VM_LOCKED) {
  1501. if (mlock_vma_pages_range(prev, addr, prev->vm_end) < 0)
  1502. return NULL; /* vma gone! */
  1503. }
  1504. return prev;
  1505. }
  1506. #else
  1507. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1508. {
  1509. return expand_downwards(vma, address);
  1510. }
  1511. struct vm_area_struct *
  1512. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1513. {
  1514. struct vm_area_struct * vma;
  1515. unsigned long start;
  1516. addr &= PAGE_MASK;
  1517. vma = find_vma(mm,addr);
  1518. if (!vma)
  1519. return NULL;
  1520. if (vma->vm_start <= addr)
  1521. return vma;
  1522. if (!(vma->vm_flags & VM_GROWSDOWN))
  1523. return NULL;
  1524. start = vma->vm_start;
  1525. if (expand_stack(vma, addr))
  1526. return NULL;
  1527. if (vma->vm_flags & VM_LOCKED) {
  1528. if (mlock_vma_pages_range(vma, addr, start) < 0)
  1529. return NULL; /* vma gone! */
  1530. }
  1531. return vma;
  1532. }
  1533. #endif
  1534. /*
  1535. * Ok - we have the memory areas we should free on the vma list,
  1536. * so release them, and do the vma updates.
  1537. *
  1538. * Called with the mm semaphore held.
  1539. */
  1540. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1541. {
  1542. /* Update high watermark before we lower total_vm */
  1543. update_hiwater_vm(mm);
  1544. do {
  1545. long nrpages = vma_pages(vma);
  1546. mm->total_vm -= nrpages;
  1547. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1548. vma = remove_vma(vma);
  1549. } while (vma);
  1550. validate_mm(mm);
  1551. }
  1552. /*
  1553. * Get rid of page table information in the indicated region.
  1554. *
  1555. * Called with the mm semaphore held.
  1556. */
  1557. static void unmap_region(struct mm_struct *mm,
  1558. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1559. unsigned long start, unsigned long end)
  1560. {
  1561. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1562. struct mmu_gather *tlb;
  1563. unsigned long nr_accounted = 0;
  1564. lru_add_drain();
  1565. tlb = tlb_gather_mmu(mm, 0);
  1566. update_hiwater_rss(mm);
  1567. unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
  1568. vm_unacct_memory(nr_accounted);
  1569. free_pgtables(tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
  1570. next? next->vm_start: 0);
  1571. tlb_finish_mmu(tlb, start, end);
  1572. }
  1573. /*
  1574. * Create a list of vma's touched by the unmap, removing them from the mm's
  1575. * vma list as we go..
  1576. */
  1577. static void
  1578. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  1579. struct vm_area_struct *prev, unsigned long end)
  1580. {
  1581. struct vm_area_struct **insertion_point;
  1582. struct vm_area_struct *tail_vma = NULL;
  1583. unsigned long addr;
  1584. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  1585. do {
  1586. rb_erase(&vma->vm_rb, &mm->mm_rb);
  1587. mm->map_count--;
  1588. tail_vma = vma;
  1589. vma = vma->vm_next;
  1590. } while (vma && vma->vm_start < end);
  1591. *insertion_point = vma;
  1592. tail_vma->vm_next = NULL;
  1593. if (mm->unmap_area == arch_unmap_area)
  1594. addr = prev ? prev->vm_end : mm->mmap_base;
  1595. else
  1596. addr = vma ? vma->vm_start : mm->mmap_base;
  1597. mm->unmap_area(mm, addr);
  1598. mm->mmap_cache = NULL; /* Kill the cache. */
  1599. }
  1600. /*
  1601. * Split a vma into two pieces at address 'addr', a new vma is allocated
  1602. * either for the first part or the tail.
  1603. */
  1604. int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  1605. unsigned long addr, int new_below)
  1606. {
  1607. struct mempolicy *pol;
  1608. struct vm_area_struct *new;
  1609. if (is_vm_hugetlb_page(vma) && (addr &
  1610. ~(huge_page_mask(hstate_vma(vma)))))
  1611. return -EINVAL;
  1612. if (mm->map_count >= sysctl_max_map_count)
  1613. return -ENOMEM;
  1614. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1615. if (!new)
  1616. return -ENOMEM;
  1617. /* most fields are the same, copy all, and then fixup */
  1618. *new = *vma;
  1619. if (new_below)
  1620. new->vm_end = addr;
  1621. else {
  1622. new->vm_start = addr;
  1623. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  1624. }
  1625. pol = mpol_dup(vma_policy(vma));
  1626. if (IS_ERR(pol)) {
  1627. kmem_cache_free(vm_area_cachep, new);
  1628. return PTR_ERR(pol);
  1629. }
  1630. vma_set_policy(new, pol);
  1631. if (new->vm_file) {
  1632. get_file(new->vm_file);
  1633. if (vma->vm_flags & VM_EXECUTABLE)
  1634. added_exe_file_vma(mm);
  1635. }
  1636. if (new->vm_ops && new->vm_ops->open)
  1637. new->vm_ops->open(new);
  1638. if (new_below)
  1639. vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  1640. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  1641. else
  1642. vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  1643. return 0;
  1644. }
  1645. /* Munmap is split into 2 main parts -- this part which finds
  1646. * what needs doing, and the areas themselves, which do the
  1647. * work. This now handles partial unmappings.
  1648. * Jeremy Fitzhardinge <jeremy@goop.org>
  1649. */
  1650. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1651. {
  1652. unsigned long end;
  1653. struct vm_area_struct *vma, *prev, *last;
  1654. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  1655. return -EINVAL;
  1656. if ((len = PAGE_ALIGN(len)) == 0)
  1657. return -EINVAL;
  1658. /* Find the first overlapping VMA */
  1659. vma = find_vma_prev(mm, start, &prev);
  1660. if (!vma)
  1661. return 0;
  1662. /* we have start < vma->vm_end */
  1663. /* if it doesn't overlap, we have nothing.. */
  1664. end = start + len;
  1665. if (vma->vm_start >= end)
  1666. return 0;
  1667. /*
  1668. * If we need to split any vma, do it now to save pain later.
  1669. *
  1670. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  1671. * unmapped vm_area_struct will remain in use: so lower split_vma
  1672. * places tmp vma above, and higher split_vma places tmp vma below.
  1673. */
  1674. if (start > vma->vm_start) {
  1675. int error = split_vma(mm, vma, start, 0);
  1676. if (error)
  1677. return error;
  1678. prev = vma;
  1679. }
  1680. /* Does it split the last one? */
  1681. last = find_vma(mm, end);
  1682. if (last && end > last->vm_start) {
  1683. int error = split_vma(mm, last, end, 1);
  1684. if (error)
  1685. return error;
  1686. }
  1687. vma = prev? prev->vm_next: mm->mmap;
  1688. /*
  1689. * unlock any mlock()ed ranges before detaching vmas
  1690. */
  1691. if (mm->locked_vm) {
  1692. struct vm_area_struct *tmp = vma;
  1693. while (tmp && tmp->vm_start < end) {
  1694. if (tmp->vm_flags & VM_LOCKED) {
  1695. mm->locked_vm -= vma_pages(tmp);
  1696. munlock_vma_pages_all(tmp);
  1697. }
  1698. tmp = tmp->vm_next;
  1699. }
  1700. }
  1701. /*
  1702. * Remove the vma's, and unmap the actual pages
  1703. */
  1704. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  1705. unmap_region(mm, vma, prev, start, end);
  1706. /* Fix up all other VM information */
  1707. remove_vma_list(mm, vma);
  1708. return 0;
  1709. }
  1710. EXPORT_SYMBOL(do_munmap);
  1711. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1712. {
  1713. int ret;
  1714. struct mm_struct *mm = current->mm;
  1715. profile_munmap(addr);
  1716. down_write(&mm->mmap_sem);
  1717. ret = do_munmap(mm, addr, len);
  1718. up_write(&mm->mmap_sem);
  1719. return ret;
  1720. }
  1721. static inline void verify_mm_writelocked(struct mm_struct *mm)
  1722. {
  1723. #ifdef CONFIG_DEBUG_VM
  1724. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  1725. WARN_ON(1);
  1726. up_read(&mm->mmap_sem);
  1727. }
  1728. #endif
  1729. }
  1730. /*
  1731. * this is really a simplified "do_mmap". it only handles
  1732. * anonymous maps. eventually we may be able to do some
  1733. * brk-specific accounting here.
  1734. */
  1735. unsigned long do_brk(unsigned long addr, unsigned long len)
  1736. {
  1737. struct mm_struct * mm = current->mm;
  1738. struct vm_area_struct * vma, * prev;
  1739. unsigned long flags;
  1740. struct rb_node ** rb_link, * rb_parent;
  1741. pgoff_t pgoff = addr >> PAGE_SHIFT;
  1742. int error;
  1743. len = PAGE_ALIGN(len);
  1744. if (!len)
  1745. return addr;
  1746. if ((addr + len) > TASK_SIZE || (addr + len) < addr)
  1747. return -EINVAL;
  1748. if (is_hugepage_only_range(mm, addr, len))
  1749. return -EINVAL;
  1750. error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
  1751. if (error)
  1752. return error;
  1753. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  1754. error = arch_mmap_check(addr, len, flags);
  1755. if (error)
  1756. return error;
  1757. /*
  1758. * mlock MCL_FUTURE?
  1759. */
  1760. if (mm->def_flags & VM_LOCKED) {
  1761. unsigned long locked, lock_limit;
  1762. locked = len >> PAGE_SHIFT;
  1763. locked += mm->locked_vm;
  1764. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  1765. lock_limit >>= PAGE_SHIFT;
  1766. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  1767. return -EAGAIN;
  1768. }
  1769. /*
  1770. * mm->mmap_sem is required to protect against another thread
  1771. * changing the mappings in case we sleep.
  1772. */
  1773. verify_mm_writelocked(mm);
  1774. /*
  1775. * Clear old maps. this also does some error checking for us
  1776. */
  1777. munmap_back:
  1778. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1779. if (vma && vma->vm_start < addr + len) {
  1780. if (do_munmap(mm, addr, len))
  1781. return -ENOMEM;
  1782. goto munmap_back;
  1783. }
  1784. /* Check against address space limits *after* clearing old maps... */
  1785. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  1786. return -ENOMEM;
  1787. if (mm->map_count > sysctl_max_map_count)
  1788. return -ENOMEM;
  1789. if (security_vm_enough_memory(len >> PAGE_SHIFT))
  1790. return -ENOMEM;
  1791. /* Can we just expand an old private anonymous mapping? */
  1792. vma = vma_merge(mm, prev, addr, addr + len, flags,
  1793. NULL, NULL, pgoff, NULL);
  1794. if (vma)
  1795. goto out;
  1796. /*
  1797. * create a vma struct for an anonymous mapping
  1798. */
  1799. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1800. if (!vma) {
  1801. vm_unacct_memory(len >> PAGE_SHIFT);
  1802. return -ENOMEM;
  1803. }
  1804. vma->vm_mm = mm;
  1805. vma->vm_start = addr;
  1806. vma->vm_end = addr + len;
  1807. vma->vm_pgoff = pgoff;
  1808. vma->vm_flags = flags;
  1809. vma->vm_page_prot = vm_get_page_prot(flags);
  1810. vma_link(mm, vma, prev, rb_link, rb_parent);
  1811. out:
  1812. mm->total_vm += len >> PAGE_SHIFT;
  1813. if (flags & VM_LOCKED) {
  1814. if (!mlock_vma_pages_range(vma, addr, addr + len))
  1815. mm->locked_vm += (len >> PAGE_SHIFT);
  1816. }
  1817. return addr;
  1818. }
  1819. EXPORT_SYMBOL(do_brk);
  1820. /* Release all mmaps. */
  1821. void exit_mmap(struct mm_struct *mm)
  1822. {
  1823. struct mmu_gather *tlb;
  1824. struct vm_area_struct *vma;
  1825. unsigned long nr_accounted = 0;
  1826. unsigned long end;
  1827. /* mm's last user has gone, and its about to be pulled down */
  1828. mmu_notifier_release(mm);
  1829. if (mm->locked_vm) {
  1830. vma = mm->mmap;
  1831. while (vma) {
  1832. if (vma->vm_flags & VM_LOCKED)
  1833. munlock_vma_pages_all(vma);
  1834. vma = vma->vm_next;
  1835. }
  1836. }
  1837. arch_exit_mmap(mm);
  1838. vma = mm->mmap;
  1839. if (!vma) /* Can happen if dup_mmap() received an OOM */
  1840. return;
  1841. lru_add_drain();
  1842. flush_cache_mm(mm);
  1843. tlb = tlb_gather_mmu(mm, 1);
  1844. /* update_hiwater_rss(mm) here? but nobody should be looking */
  1845. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  1846. end = unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
  1847. vm_unacct_memory(nr_accounted);
  1848. free_pgtables(tlb, vma, FIRST_USER_ADDRESS, 0);
  1849. tlb_finish_mmu(tlb, 0, end);
  1850. /*
  1851. * Walk the list again, actually closing and freeing it,
  1852. * with preemption enabled, without holding any MM locks.
  1853. */
  1854. while (vma)
  1855. vma = remove_vma(vma);
  1856. BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  1857. }
  1858. /* Insert vm structure into process list sorted by address
  1859. * and into the inode's i_mmap tree. If vm_file is non-NULL
  1860. * then i_mmap_lock is taken here.
  1861. */
  1862. int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  1863. {
  1864. struct vm_area_struct * __vma, * prev;
  1865. struct rb_node ** rb_link, * rb_parent;
  1866. /*
  1867. * The vm_pgoff of a purely anonymous vma should be irrelevant
  1868. * until its first write fault, when page's anon_vma and index
  1869. * are set. But now set the vm_pgoff it will almost certainly
  1870. * end up with (unless mremap moves it elsewhere before that
  1871. * first wfault), so /proc/pid/maps tells a consistent story.
  1872. *
  1873. * By setting it to reflect the virtual start address of the
  1874. * vma, merges and splits can happen in a seamless way, just
  1875. * using the existing file pgoff checks and manipulations.
  1876. * Similarly in do_mmap_pgoff and in do_brk.
  1877. */
  1878. if (!vma->vm_file) {
  1879. BUG_ON(vma->anon_vma);
  1880. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1881. }
  1882. __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
  1883. if (__vma && __vma->vm_start < vma->vm_end)
  1884. return -ENOMEM;
  1885. if ((vma->vm_flags & VM_ACCOUNT) &&
  1886. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  1887. return -ENOMEM;
  1888. vma_link(mm, vma, prev, rb_link, rb_parent);
  1889. return 0;
  1890. }
  1891. /*
  1892. * Copy the vma structure to a new location in the same mm,
  1893. * prior to moving page table entries, to effect an mremap move.
  1894. */
  1895. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  1896. unsigned long addr, unsigned long len, pgoff_t pgoff)
  1897. {
  1898. struct vm_area_struct *vma = *vmap;
  1899. unsigned long vma_start = vma->vm_start;
  1900. struct mm_struct *mm = vma->vm_mm;
  1901. struct vm_area_struct *new_vma, *prev;
  1902. struct rb_node **rb_link, *rb_parent;
  1903. struct mempolicy *pol;
  1904. /*
  1905. * If anonymous vma has not yet been faulted, update new pgoff
  1906. * to match new location, to increase its chance of merging.
  1907. */
  1908. if (!vma->vm_file && !vma->anon_vma)
  1909. pgoff = addr >> PAGE_SHIFT;
  1910. find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  1911. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  1912. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  1913. if (new_vma) {
  1914. /*
  1915. * Source vma may have been merged into new_vma
  1916. */
  1917. if (vma_start >= new_vma->vm_start &&
  1918. vma_start < new_vma->vm_end)
  1919. *vmap = new_vma;
  1920. } else {
  1921. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1922. if (new_vma) {
  1923. *new_vma = *vma;
  1924. pol = mpol_dup(vma_policy(vma));
  1925. if (IS_ERR(pol)) {
  1926. kmem_cache_free(vm_area_cachep, new_vma);
  1927. return NULL;
  1928. }
  1929. vma_set_policy(new_vma, pol);
  1930. new_vma->vm_start = addr;
  1931. new_vma->vm_end = addr + len;
  1932. new_vma->vm_pgoff = pgoff;
  1933. if (new_vma->vm_file) {
  1934. get_file(new_vma->vm_file);
  1935. if (vma->vm_flags & VM_EXECUTABLE)
  1936. added_exe_file_vma(mm);
  1937. }
  1938. if (new_vma->vm_ops && new_vma->vm_ops->open)
  1939. new_vma->vm_ops->open(new_vma);
  1940. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  1941. }
  1942. }
  1943. return new_vma;
  1944. }
  1945. /*
  1946. * Return true if the calling process may expand its vm space by the passed
  1947. * number of pages
  1948. */
  1949. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  1950. {
  1951. unsigned long cur = mm->total_vm; /* pages */
  1952. unsigned long lim;
  1953. lim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  1954. if (cur + npages > lim)
  1955. return 0;
  1956. return 1;
  1957. }
  1958. static int special_mapping_fault(struct vm_area_struct *vma,
  1959. struct vm_fault *vmf)
  1960. {
  1961. pgoff_t pgoff;
  1962. struct page **pages;
  1963. /*
  1964. * special mappings have no vm_file, and in that case, the mm
  1965. * uses vm_pgoff internally. So we have to subtract it from here.
  1966. * We are allowed to do this because we are the mm; do not copy
  1967. * this code into drivers!
  1968. */
  1969. pgoff = vmf->pgoff - vma->vm_pgoff;
  1970. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  1971. pgoff--;
  1972. if (*pages) {
  1973. struct page *page = *pages;
  1974. get_page(page);
  1975. vmf->page = page;
  1976. return 0;
  1977. }
  1978. return VM_FAULT_SIGBUS;
  1979. }
  1980. /*
  1981. * Having a close hook prevents vma merging regardless of flags.
  1982. */
  1983. static void special_mapping_close(struct vm_area_struct *vma)
  1984. {
  1985. }
  1986. static struct vm_operations_struct special_mapping_vmops = {
  1987. .close = special_mapping_close,
  1988. .fault = special_mapping_fault,
  1989. };
  1990. /*
  1991. * Called with mm->mmap_sem held for writing.
  1992. * Insert a new vma covering the given region, with the given flags.
  1993. * Its pages are supplied by the given array of struct page *.
  1994. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  1995. * The region past the last page supplied will always produce SIGBUS.
  1996. * The array pointer and the pages it points to are assumed to stay alive
  1997. * for as long as this mapping might exist.
  1998. */
  1999. int install_special_mapping(struct mm_struct *mm,
  2000. unsigned long addr, unsigned long len,
  2001. unsigned long vm_flags, struct page **pages)
  2002. {
  2003. struct vm_area_struct *vma;
  2004. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2005. if (unlikely(vma == NULL))
  2006. return -ENOMEM;
  2007. vma->vm_mm = mm;
  2008. vma->vm_start = addr;
  2009. vma->vm_end = addr + len;
  2010. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2011. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2012. vma->vm_ops = &special_mapping_vmops;
  2013. vma->vm_private_data = pages;
  2014. if (unlikely(insert_vm_struct(mm, vma))) {
  2015. kmem_cache_free(vm_area_cachep, vma);
  2016. return -ENOMEM;
  2017. }
  2018. mm->total_vm += len >> PAGE_SHIFT;
  2019. perf_counter_mmap(vma);
  2020. return 0;
  2021. }
  2022. static DEFINE_MUTEX(mm_all_locks_mutex);
  2023. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2024. {
  2025. if (!test_bit(0, (unsigned long *) &anon_vma->head.next)) {
  2026. /*
  2027. * The LSB of head.next can't change from under us
  2028. * because we hold the mm_all_locks_mutex.
  2029. */
  2030. spin_lock_nest_lock(&anon_vma->lock, &mm->mmap_sem);
  2031. /*
  2032. * We can safely modify head.next after taking the
  2033. * anon_vma->lock. If some other vma in this mm shares
  2034. * the same anon_vma we won't take it again.
  2035. *
  2036. * No need of atomic instructions here, head.next
  2037. * can't change from under us thanks to the
  2038. * anon_vma->lock.
  2039. */
  2040. if (__test_and_set_bit(0, (unsigned long *)
  2041. &anon_vma->head.next))
  2042. BUG();
  2043. }
  2044. }
  2045. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2046. {
  2047. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2048. /*
  2049. * AS_MM_ALL_LOCKS can't change from under us because
  2050. * we hold the mm_all_locks_mutex.
  2051. *
  2052. * Operations on ->flags have to be atomic because
  2053. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2054. * mm_all_locks_mutex, there may be other cpus
  2055. * changing other bitflags in parallel to us.
  2056. */
  2057. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2058. BUG();
  2059. spin_lock_nest_lock(&mapping->i_mmap_lock, &mm->mmap_sem);
  2060. }
  2061. }
  2062. /*
  2063. * This operation locks against the VM for all pte/vma/mm related
  2064. * operations that could ever happen on a certain mm. This includes
  2065. * vmtruncate, try_to_unmap, and all page faults.
  2066. *
  2067. * The caller must take the mmap_sem in write mode before calling
  2068. * mm_take_all_locks(). The caller isn't allowed to release the
  2069. * mmap_sem until mm_drop_all_locks() returns.
  2070. *
  2071. * mmap_sem in write mode is required in order to block all operations
  2072. * that could modify pagetables and free pages without need of
  2073. * altering the vma layout (for example populate_range() with
  2074. * nonlinear vmas). It's also needed in write mode to avoid new
  2075. * anon_vmas to be associated with existing vmas.
  2076. *
  2077. * A single task can't take more than one mm_take_all_locks() in a row
  2078. * or it would deadlock.
  2079. *
  2080. * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
  2081. * mapping->flags avoid to take the same lock twice, if more than one
  2082. * vma in this mm is backed by the same anon_vma or address_space.
  2083. *
  2084. * We can take all the locks in random order because the VM code
  2085. * taking i_mmap_lock or anon_vma->lock outside the mmap_sem never
  2086. * takes more than one of them in a row. Secondly we're protected
  2087. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2088. *
  2089. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2090. * that may have to take thousand of locks.
  2091. *
  2092. * mm_take_all_locks() can fail if it's interrupted by signals.
  2093. */
  2094. int mm_take_all_locks(struct mm_struct *mm)
  2095. {
  2096. struct vm_area_struct *vma;
  2097. int ret = -EINTR;
  2098. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2099. mutex_lock(&mm_all_locks_mutex);
  2100. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2101. if (signal_pending(current))
  2102. goto out_unlock;
  2103. if (vma->vm_file && vma->vm_file->f_mapping)
  2104. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2105. }
  2106. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2107. if (signal_pending(current))
  2108. goto out_unlock;
  2109. if (vma->anon_vma)
  2110. vm_lock_anon_vma(mm, vma->anon_vma);
  2111. }
  2112. ret = 0;
  2113. out_unlock:
  2114. if (ret)
  2115. mm_drop_all_locks(mm);
  2116. return ret;
  2117. }
  2118. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2119. {
  2120. if (test_bit(0, (unsigned long *) &anon_vma->head.next)) {
  2121. /*
  2122. * The LSB of head.next can't change to 0 from under
  2123. * us because we hold the mm_all_locks_mutex.
  2124. *
  2125. * We must however clear the bitflag before unlocking
  2126. * the vma so the users using the anon_vma->head will
  2127. * never see our bitflag.
  2128. *
  2129. * No need of atomic instructions here, head.next
  2130. * can't change from under us until we release the
  2131. * anon_vma->lock.
  2132. */
  2133. if (!__test_and_clear_bit(0, (unsigned long *)
  2134. &anon_vma->head.next))
  2135. BUG();
  2136. spin_unlock(&anon_vma->lock);
  2137. }
  2138. }
  2139. static void vm_unlock_mapping(struct address_space *mapping)
  2140. {
  2141. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2142. /*
  2143. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2144. * because we hold the mm_all_locks_mutex.
  2145. */
  2146. spin_unlock(&mapping->i_mmap_lock);
  2147. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2148. &mapping->flags))
  2149. BUG();
  2150. }
  2151. }
  2152. /*
  2153. * The mmap_sem cannot be released by the caller until
  2154. * mm_drop_all_locks() returns.
  2155. */
  2156. void mm_drop_all_locks(struct mm_struct *mm)
  2157. {
  2158. struct vm_area_struct *vma;
  2159. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2160. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2161. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2162. if (vma->anon_vma)
  2163. vm_unlock_anon_vma(vma->anon_vma);
  2164. if (vma->vm_file && vma->vm_file->f_mapping)
  2165. vm_unlock_mapping(vma->vm_file->f_mapping);
  2166. }
  2167. mutex_unlock(&mm_all_locks_mutex);
  2168. }
  2169. /*
  2170. * initialise the VMA slab
  2171. */
  2172. void __init mmap_init(void)
  2173. {
  2174. int ret;
  2175. ret = percpu_counter_init(&vm_committed_as, 0);
  2176. VM_BUG_ON(ret);
  2177. }