mmap.c 71 KB

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