mmap.c 65 KB

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