mmap.c 66 KB

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