mmap.c 77 KB

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