mmap.c 71 KB

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