mmap.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923
  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. /*
  907. * Does the application expect PROT_READ to imply PROT_EXEC?
  908. *
  909. * (the exception is when the underlying filesystem is noexec
  910. * mounted, in which case we dont add PROT_EXEC.)
  911. */
  912. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  913. if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
  914. prot |= PROT_EXEC;
  915. if (!len)
  916. return -EINVAL;
  917. if (!(flags & MAP_FIXED))
  918. addr = round_hint_to_min(addr);
  919. /* Careful about overflows.. */
  920. len = PAGE_ALIGN(len);
  921. if (!len)
  922. return -ENOMEM;
  923. /* offset overflow? */
  924. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  925. return -EOVERFLOW;
  926. /* Too many mappings? */
  927. if (mm->map_count > sysctl_max_map_count)
  928. return -ENOMEM;
  929. /* Obtain the address to map to. we verify (or select) it and ensure
  930. * that it represents a valid section of the address space.
  931. */
  932. addr = get_unmapped_area(file, addr, len, pgoff, flags);
  933. if (addr & ~PAGE_MASK)
  934. return addr;
  935. /* Do simple checking here so the lower-level routines won't have
  936. * to. we assume access permissions have been handled by the open
  937. * of the memory object, so we don't do any here.
  938. */
  939. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
  940. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  941. if (flags & MAP_LOCKED)
  942. if (!can_do_mlock())
  943. return -EPERM;
  944. /* mlock MCL_FUTURE? */
  945. if (vm_flags & VM_LOCKED) {
  946. unsigned long locked, lock_limit;
  947. locked = len >> PAGE_SHIFT;
  948. locked += mm->locked_vm;
  949. lock_limit = rlimit(RLIMIT_MEMLOCK);
  950. lock_limit >>= PAGE_SHIFT;
  951. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  952. return -EAGAIN;
  953. }
  954. inode = file ? file->f_path.dentry->d_inode : NULL;
  955. if (file) {
  956. switch (flags & MAP_TYPE) {
  957. case MAP_SHARED:
  958. if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
  959. return -EACCES;
  960. /*
  961. * Make sure we don't allow writing to an append-only
  962. * file..
  963. */
  964. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  965. return -EACCES;
  966. /*
  967. * Make sure there are no mandatory locks on the file.
  968. */
  969. if (locks_verify_locked(inode))
  970. return -EAGAIN;
  971. vm_flags |= VM_SHARED | VM_MAYSHARE;
  972. if (!(file->f_mode & FMODE_WRITE))
  973. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  974. /* fall through */
  975. case MAP_PRIVATE:
  976. if (!(file->f_mode & FMODE_READ))
  977. return -EACCES;
  978. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  979. if (vm_flags & VM_EXEC)
  980. return -EPERM;
  981. vm_flags &= ~VM_MAYEXEC;
  982. }
  983. if (!file->f_op || !file->f_op->mmap)
  984. return -ENODEV;
  985. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  986. return -EINVAL;
  987. break;
  988. default:
  989. return -EINVAL;
  990. }
  991. } else {
  992. switch (flags & MAP_TYPE) {
  993. case MAP_SHARED:
  994. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  995. return -EINVAL;
  996. /*
  997. * Ignore pgoff.
  998. */
  999. pgoff = 0;
  1000. vm_flags |= VM_SHARED | VM_MAYSHARE;
  1001. break;
  1002. case MAP_PRIVATE:
  1003. /*
  1004. * Set pgoff according to addr for anon_vma.
  1005. */
  1006. pgoff = addr >> PAGE_SHIFT;
  1007. break;
  1008. default:
  1009. return -EINVAL;
  1010. }
  1011. }
  1012. return mmap_region(file, addr, len, flags, vm_flags, pgoff);
  1013. }
  1014. unsigned long do_mmap(struct file *file, unsigned long addr,
  1015. unsigned long len, unsigned long prot,
  1016. unsigned long flag, unsigned long offset)
  1017. {
  1018. if (unlikely(offset + PAGE_ALIGN(len) < offset))
  1019. return -EINVAL;
  1020. if (unlikely(offset & ~PAGE_MASK))
  1021. return -EINVAL;
  1022. return do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
  1023. }
  1024. unsigned long vm_mmap(struct file *file, unsigned long addr,
  1025. unsigned long len, unsigned long prot,
  1026. unsigned long flag, unsigned long offset)
  1027. {
  1028. unsigned long ret;
  1029. struct mm_struct *mm = current->mm;
  1030. ret = security_mmap_file(file, prot, flag);
  1031. if (!ret) {
  1032. down_write(&mm->mmap_sem);
  1033. ret = do_mmap(file, addr, len, prot, flag, offset);
  1034. up_write(&mm->mmap_sem);
  1035. }
  1036. return ret;
  1037. }
  1038. EXPORT_SYMBOL(vm_mmap);
  1039. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1040. unsigned long, prot, unsigned long, flags,
  1041. unsigned long, fd, unsigned long, pgoff)
  1042. {
  1043. struct file *file = NULL;
  1044. unsigned long retval = -EBADF;
  1045. if (!(flags & MAP_ANONYMOUS)) {
  1046. audit_mmap_fd(fd, flags);
  1047. if (unlikely(flags & MAP_HUGETLB))
  1048. return -EINVAL;
  1049. file = fget(fd);
  1050. if (!file)
  1051. goto out;
  1052. if (is_file_hugepages(file))
  1053. len = ALIGN(len, huge_page_size(hstate_file(file)));
  1054. } else if (flags & MAP_HUGETLB) {
  1055. struct user_struct *user = NULL;
  1056. len = ALIGN(len, huge_page_size(&default_hstate));
  1057. /*
  1058. * VM_NORESERVE is used because the reservations will be
  1059. * taken when vm_ops->mmap() is called
  1060. * A dummy user value is used because we are not locking
  1061. * memory so no accounting is necessary
  1062. */
  1063. file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
  1064. VM_NORESERVE, &user,
  1065. HUGETLB_ANONHUGE_INODE);
  1066. if (IS_ERR(file))
  1067. return PTR_ERR(file);
  1068. }
  1069. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1070. retval = security_mmap_file(file, prot, flags);
  1071. if (!retval) {
  1072. down_write(&current->mm->mmap_sem);
  1073. retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1074. up_write(&current->mm->mmap_sem);
  1075. }
  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 = get_file(file);
  1234. error = file->f_op->mmap(file, vma);
  1235. if (error)
  1236. goto unmap_and_free_vma;
  1237. if (vm_flags & VM_EXECUTABLE)
  1238. added_exe_file_vma(mm);
  1239. /* Can addr have changed??
  1240. *
  1241. * Answer: Yes, several device drivers can do it in their
  1242. * f_op->mmap method. -DaveM
  1243. */
  1244. addr = vma->vm_start;
  1245. pgoff = vma->vm_pgoff;
  1246. vm_flags = vma->vm_flags;
  1247. } else if (vm_flags & VM_SHARED) {
  1248. error = shmem_zero_setup(vma);
  1249. if (error)
  1250. goto free_vma;
  1251. }
  1252. if (vma_wants_writenotify(vma)) {
  1253. pgprot_t pprot = vma->vm_page_prot;
  1254. /* Can vma->vm_page_prot have changed??
  1255. *
  1256. * Answer: Yes, drivers may have changed it in their
  1257. * f_op->mmap method.
  1258. *
  1259. * Ensures that vmas marked as uncached stay that way.
  1260. */
  1261. vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
  1262. if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
  1263. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1264. }
  1265. vma_link(mm, vma, prev, rb_link, rb_parent);
  1266. #ifdef CONFIG_TIMA_RKP
  1267. if(file && (strcmp(current->comm, "zygote") == 0)){
  1268. char *tmp;
  1269. char *pathname;
  1270. struct path path;
  1271. path = file->f_path;
  1272. path_get(&file->f_path);
  1273. tmp = (char *)__get_free_page(GFP_TEMPORARY);
  1274. if (!tmp) {
  1275. path_put(&path);
  1276. return -ENOMEM;
  1277. }
  1278. pathname = d_path(&path, tmp, PAGE_SIZE);
  1279. path_put(&path);
  1280. if (IS_ERR(pathname)) {
  1281. free_page((unsigned long)tmp);
  1282. return PTR_ERR(pathname);
  1283. }
  1284. if (strstr(pathname, "dalvik-heap") != NULL
  1285. || strstr(pathname, "dalvik-bitmap") != NULL
  1286. || strstr(pathname, "dalvik-LinearAlloc") != NULL
  1287. || strstr(pathname, "dalvik-mark-stack") != NULL
  1288. || strstr(pathname, "dalvik-card-table") != NULL) {
  1289. //printk("PROC %s\tFILE %s\tSTART %lx\tLEN %lx\n", current->comm, pathname, addr, len);
  1290. tima_send_cmd2(addr, len, 0x30);
  1291. }
  1292. /* do something here with pathname */
  1293. free_page((unsigned long)tmp);
  1294. }
  1295. #endif
  1296. /* Once vma denies write, undo our temporary denial count */
  1297. if (file) {
  1298. if (vm_flags & VM_SHARED)
  1299. mapping_unmap_writable(file->f_mapping);
  1300. if (vm_flags & VM_DENYWRITE)
  1301. allow_write_access(file);
  1302. }
  1303. file = vma->vm_file;
  1304. out:
  1305. perf_event_mmap(vma);
  1306. mm->total_vm += len >> PAGE_SHIFT;
  1307. vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
  1308. if (vm_flags & VM_LOCKED) {
  1309. if (!mlock_vma_pages_range(vma, addr, addr + len))
  1310. mm->locked_vm += (len >> PAGE_SHIFT);
  1311. } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
  1312. make_pages_present(addr, addr + len);
  1313. return addr;
  1314. unmap_and_free_vma:
  1315. vma->vm_file = NULL;
  1316. fput(file);
  1317. /* Undo any partial mapping done by a device driver. */
  1318. unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
  1319. charged = 0;
  1320. if (vm_flags & VM_SHARED)
  1321. mapping_unmap_writable(file->f_mapping);
  1322. allow_write_and_free_vma:
  1323. if (vm_flags & VM_DENYWRITE)
  1324. allow_write_access(file);
  1325. free_vma:
  1326. kmem_cache_free(vm_area_cachep, vma);
  1327. unacct_error:
  1328. if (charged)
  1329. vm_unacct_memory(charged);
  1330. return error;
  1331. }
  1332. /* Get an address range which is currently unmapped.
  1333. * For shmat() with addr=0.
  1334. *
  1335. * Ugly calling convention alert:
  1336. * Return value with the low bits set means error value,
  1337. * ie
  1338. * if (ret & ~PAGE_MASK)
  1339. * error = ret;
  1340. *
  1341. * This function "knows" that -ENOMEM has the bits set.
  1342. */
  1343. #ifndef HAVE_ARCH_UNMAPPED_AREA
  1344. unsigned long
  1345. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  1346. unsigned long len, unsigned long pgoff, unsigned long flags)
  1347. {
  1348. struct mm_struct *mm = current->mm;
  1349. struct vm_area_struct *vma, *prev;
  1350. unsigned long start_addr, vm_start, prev_end;
  1351. if (len > TASK_SIZE)
  1352. return -ENOMEM;
  1353. if (flags & MAP_FIXED)
  1354. return addr;
  1355. if (addr) {
  1356. addr = PAGE_ALIGN(addr);
  1357. vma = find_vma_prev(mm, addr, &prev);
  1358. if (TASK_SIZE - len >= addr &&
  1359. (!vma || addr + len <= vm_start_gap(vma)) &&
  1360. (!prev || addr >= vm_end_gap(prev)))
  1361. return addr;
  1362. }
  1363. if (len > mm->cached_hole_size) {
  1364. start_addr = addr = mm->free_area_cache;
  1365. } else {
  1366. start_addr = addr = TASK_UNMAPPED_BASE;
  1367. mm->cached_hole_size = 0;
  1368. }
  1369. full_search:
  1370. for (vma = find_vma_prev(mm, addr, &prev); ; prev = vma,
  1371. vma = vma->vm_next) {
  1372. if (prev) {
  1373. prev_end = vm_end_gap(prev);
  1374. if (addr < prev_end) {
  1375. addr = prev_end;
  1376. /* If vma already violates gap, forget it */
  1377. if (vma && addr > vma->vm_start)
  1378. addr = vma->vm_start;
  1379. }
  1380. }
  1381. /* At this point: (!vma || addr < vma->vm_end). */
  1382. if (TASK_SIZE - len < addr) {
  1383. /*
  1384. * Start a new search - just in case we missed
  1385. * some holes.
  1386. */
  1387. if (start_addr != TASK_UNMAPPED_BASE) {
  1388. addr = TASK_UNMAPPED_BASE;
  1389. start_addr = addr;
  1390. mm->cached_hole_size = 0;
  1391. goto full_search;
  1392. }
  1393. return -ENOMEM;
  1394. }
  1395. vm_start = vma ? vm_start_gap(vma) : TASK_SIZE;
  1396. if (addr + len <= vm_start) {
  1397. /*
  1398. * Remember the place where we stopped the search:
  1399. */
  1400. mm->free_area_cache = addr + len;
  1401. return addr;
  1402. }
  1403. if (addr + mm->cached_hole_size < vm_start)
  1404. mm->cached_hole_size = vm_start - addr;
  1405. }
  1406. }
  1407. #endif
  1408. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1409. {
  1410. /*
  1411. * Is this a new hole at the lowest possible address?
  1412. */
  1413. if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
  1414. mm->free_area_cache = addr;
  1415. }
  1416. /*
  1417. * This mmap-allocator allocates new areas top-down from below the
  1418. * stack's low limit (the base):
  1419. */
  1420. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  1421. unsigned long
  1422. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  1423. const unsigned long len, const unsigned long pgoff,
  1424. const unsigned long flags)
  1425. {
  1426. struct vm_area_struct *vma, *prev;
  1427. struct mm_struct *mm = current->mm;
  1428. unsigned long addr = addr0, start_addr;
  1429. unsigned long vm_start, prev_end;
  1430. /* requested length too big for entire address space */
  1431. if (len > TASK_SIZE)
  1432. return -ENOMEM;
  1433. if (flags & MAP_FIXED)
  1434. return addr;
  1435. /* requesting a specific address */
  1436. if (addr) {
  1437. addr = PAGE_ALIGN(addr);
  1438. vma = find_vma_prev(mm, addr, &prev);
  1439. if (TASK_SIZE - len >= addr &&
  1440. (!vma || addr + len <= vm_start_gap(vma)) &&
  1441. (!prev || addr >= vm_end_gap(prev)))
  1442. return addr;
  1443. }
  1444. /* check if free_area_cache is useful for us */
  1445. if (len <= mm->cached_hole_size) {
  1446. mm->cached_hole_size = 0;
  1447. mm->free_area_cache = mm->mmap_base;
  1448. }
  1449. try_again:
  1450. /* either no address requested or can't fit in requested address hole */
  1451. start_addr = addr = mm->free_area_cache;
  1452. if (addr < len)
  1453. goto fail;
  1454. addr -= len;
  1455. do {
  1456. /*
  1457. * Lookup failure means no vma is above this address,
  1458. * else if new region fits below vma->vm_start,
  1459. * return with success:
  1460. */
  1461. vma = find_vma_prev(mm, addr, &prev);
  1462. vm_start = vma ? vm_start_gap(vma) : mm->mmap_base;
  1463. prev_end = vm_end_gap(prev);
  1464. if (addr + len <= vm_start && addr >= prev_end)
  1465. /* remember the address as a hint for next time */
  1466. return (mm->free_area_cache = addr);
  1467. /* remember the largest hole we saw so far */
  1468. if (addr + mm->cached_hole_size < vm_start)
  1469. mm->cached_hole_size = vm_start - addr;
  1470. /* try just below the current vma->vm_start */
  1471. addr = vm_start - len;
  1472. } while (len < vm_start);
  1473. fail:
  1474. /*
  1475. * if hint left us with no space for the requested
  1476. * mapping then try again:
  1477. *
  1478. * Note: this is different with the case of bottomup
  1479. * which does the fully line-search, but we use find_vma
  1480. * here that causes some holes skipped.
  1481. */
  1482. if (start_addr != mm->mmap_base) {
  1483. mm->free_area_cache = mm->mmap_base;
  1484. mm->cached_hole_size = 0;
  1485. goto try_again;
  1486. }
  1487. /*
  1488. * A failed mmap() very likely causes application failure,
  1489. * so fall back to the bottom-up function here. This scenario
  1490. * can happen with large stack limits and large mmap()
  1491. * allocations.
  1492. */
  1493. mm->cached_hole_size = ~0UL;
  1494. mm->free_area_cache = TASK_UNMAPPED_BASE;
  1495. addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  1496. /*
  1497. * Restore the topdown base:
  1498. */
  1499. mm->free_area_cache = mm->mmap_base;
  1500. mm->cached_hole_size = ~0UL;
  1501. return addr;
  1502. }
  1503. #endif
  1504. void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
  1505. {
  1506. /*
  1507. * Is this a new hole at the highest possible address?
  1508. */
  1509. if (addr > mm->free_area_cache)
  1510. mm->free_area_cache = addr;
  1511. /* dont allow allocations above current base */
  1512. if (mm->free_area_cache > mm->mmap_base)
  1513. mm->free_area_cache = mm->mmap_base;
  1514. }
  1515. unsigned long
  1516. get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  1517. unsigned long pgoff, unsigned long flags)
  1518. {
  1519. unsigned long (*get_area)(struct file *, unsigned long,
  1520. unsigned long, unsigned long, unsigned long);
  1521. unsigned long error = arch_mmap_check(addr, len, flags);
  1522. if (error)
  1523. return error;
  1524. /* Careful about overflows.. */
  1525. if (len > TASK_SIZE)
  1526. return -ENOMEM;
  1527. get_area = current->mm->get_unmapped_area;
  1528. if (file && file->f_op && file->f_op->get_unmapped_area)
  1529. get_area = file->f_op->get_unmapped_area;
  1530. addr = get_area(file, addr, len, pgoff, flags);
  1531. if (IS_ERR_VALUE(addr))
  1532. return addr;
  1533. if (addr > TASK_SIZE - len)
  1534. return -ENOMEM;
  1535. if (addr & ~PAGE_MASK)
  1536. return -EINVAL;
  1537. addr = arch_rebalance_pgtables(addr, len);
  1538. error = security_mmap_addr(addr);
  1539. return error ? error : addr;
  1540. }
  1541. EXPORT_SYMBOL(get_unmapped_area);
  1542. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1543. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  1544. {
  1545. struct vm_area_struct *vma = NULL;
  1546. if (mm) {
  1547. /* Check the cache first. */
  1548. /* (Cache hit rate is typically around 35%.) */
  1549. vma = ACCESS_ONCE(mm->mmap_cache);
  1550. if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
  1551. struct rb_node * rb_node;
  1552. rb_node = mm->mm_rb.rb_node;
  1553. vma = NULL;
  1554. while (rb_node) {
  1555. struct vm_area_struct * vma_tmp;
  1556. vma_tmp = rb_entry(rb_node,
  1557. struct vm_area_struct, vm_rb);
  1558. if (vma_tmp->vm_end > addr) {
  1559. vma = vma_tmp;
  1560. if (vma_tmp->vm_start <= addr)
  1561. break;
  1562. rb_node = rb_node->rb_left;
  1563. } else
  1564. rb_node = rb_node->rb_right;
  1565. }
  1566. if (vma)
  1567. mm->mmap_cache = vma;
  1568. }
  1569. }
  1570. return vma;
  1571. }
  1572. EXPORT_SYMBOL(find_vma);
  1573. /*
  1574. * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
  1575. */
  1576. struct vm_area_struct *
  1577. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  1578. struct vm_area_struct **pprev)
  1579. {
  1580. struct vm_area_struct *vma;
  1581. vma = find_vma(mm, addr);
  1582. if (vma) {
  1583. *pprev = vma->vm_prev;
  1584. } else {
  1585. struct rb_node *rb_node = mm->mm_rb.rb_node;
  1586. *pprev = NULL;
  1587. while (rb_node) {
  1588. *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
  1589. rb_node = rb_node->rb_right;
  1590. }
  1591. }
  1592. return vma;
  1593. }
  1594. /*
  1595. * Verify that the stack growth is acceptable and
  1596. * update accounting. This is shared with both the
  1597. * grow-up and grow-down cases.
  1598. */
  1599. static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
  1600. {
  1601. struct mm_struct *mm = vma->vm_mm;
  1602. struct rlimit *rlim = current->signal->rlim;
  1603. unsigned long new_start, actual_size;
  1604. /* address space limit tests */
  1605. if (!may_expand_vm(mm, grow))
  1606. return -ENOMEM;
  1607. /* Stack limit test */
  1608. actual_size = size;
  1609. if (size && (vma->vm_flags & (VM_GROWSUP | VM_GROWSDOWN)))
  1610. actual_size -= PAGE_SIZE;
  1611. if (actual_size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
  1612. return -ENOMEM;
  1613. /* mlock limit tests */
  1614. if (vma->vm_flags & VM_LOCKED) {
  1615. unsigned long locked;
  1616. unsigned long limit;
  1617. locked = mm->locked_vm + grow;
  1618. limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
  1619. limit >>= PAGE_SHIFT;
  1620. if (locked > limit && !capable(CAP_IPC_LOCK))
  1621. return -ENOMEM;
  1622. }
  1623. /* Check to ensure the stack will not grow into a hugetlb-only region */
  1624. new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
  1625. vma->vm_end - size;
  1626. if (is_hugepage_only_range(vma->vm_mm, new_start, size))
  1627. return -EFAULT;
  1628. /*
  1629. * Overcommit.. This must be the final test, as it will
  1630. * update security statistics.
  1631. */
  1632. if (security_vm_enough_memory_mm(mm, grow))
  1633. return -ENOMEM;
  1634. /* Ok, everything looks good - let it rip */
  1635. mm->total_vm += grow;
  1636. if (vma->vm_flags & VM_LOCKED)
  1637. mm->locked_vm += grow;
  1638. vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
  1639. return 0;
  1640. }
  1641. #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
  1642. /*
  1643. * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
  1644. * vma is the last one with address > vma->vm_end. Have to extend vma.
  1645. */
  1646. int expand_upwards(struct vm_area_struct *vma, unsigned long address)
  1647. {
  1648. struct vm_area_struct *next;
  1649. unsigned long gap_addr;
  1650. int error = 0;
  1651. if (!(vma->vm_flags & VM_GROWSUP))
  1652. return -EFAULT;
  1653. /*
  1654. * We must make sure the anon_vma is allocated
  1655. * so that the anon_vma locking is not a noop.
  1656. */
  1657. /* Guard against exceeding limits of the address space. */
  1658. address &= PAGE_MASK;
  1659. if (address >= TASK_SIZE)
  1660. return -ENOMEM;
  1661. address += PAGE_SIZE;
  1662. /* Enforce stack_guard_gap */
  1663. gap_addr = address + stack_guard_gap;
  1664. /* Guard against overflow */
  1665. if (gap_addr < address || gap_addr > TASK_SIZE)
  1666. gap_addr = TASK_SIZE;
  1667. next = vma->vm_next;
  1668. if (next && next->vm_start < gap_addr) {
  1669. if (!(next->vm_flags & VM_GROWSUP))
  1670. return -ENOMEM;
  1671. /* Check that both stack segments have the same anon_vma? */
  1672. }
  1673. /* We must make sure the anon_vma is allocated. */
  1674. if (unlikely(anon_vma_prepare(vma)))
  1675. return -ENOMEM;
  1676. /*
  1677. * vma->vm_start/vm_end cannot change under us because the caller
  1678. * is required to hold the mmap_sem in read mode. We need the
  1679. * anon_vma lock to serialize against concurrent expand_stacks.
  1680. */
  1681. vma_lock_anon_vma(vma);
  1682. /* Somebody else might have raced and expanded it already */
  1683. if (address > vma->vm_end) {
  1684. unsigned long size, grow;
  1685. size = address - vma->vm_start;
  1686. grow = (address - vma->vm_end) >> PAGE_SHIFT;
  1687. error = -ENOMEM;
  1688. if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
  1689. error = acct_stack_growth(vma, size, grow);
  1690. if (!error) {
  1691. vma->vm_end = address;
  1692. perf_event_mmap(vma);
  1693. }
  1694. }
  1695. }
  1696. vma_unlock_anon_vma(vma);
  1697. khugepaged_enter_vma_merge(vma, vma->vm_flags);
  1698. return error;
  1699. }
  1700. #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
  1701. /*
  1702. * vma is the first one with address < vma->vm_start. Have to extend vma.
  1703. */
  1704. int expand_downwards(struct vm_area_struct *vma,
  1705. unsigned long address)
  1706. {
  1707. struct vm_area_struct *prev;
  1708. unsigned long gap_addr;
  1709. int error;
  1710. address &= PAGE_MASK;
  1711. error = security_mmap_addr(address);
  1712. if (error)
  1713. return error;
  1714. /* Enforce stack_guard_gap */
  1715. gap_addr = address - stack_guard_gap;
  1716. if (gap_addr > address)
  1717. return -ENOMEM;
  1718. prev = vma->vm_prev;
  1719. if (prev && prev->vm_end > gap_addr) {
  1720. if (!(prev->vm_flags & VM_GROWSDOWN))
  1721. return -ENOMEM;
  1722. /* Check that both stack segments have the same anon_vma? */
  1723. }
  1724. /* We must make sure the anon_vma is allocated. */
  1725. if (unlikely(anon_vma_prepare(vma)))
  1726. return -ENOMEM;
  1727. /*
  1728. * vma->vm_start/vm_end cannot change under us because the caller
  1729. * is required to hold the mmap_sem in read mode. We need the
  1730. * anon_vma lock to serialize against concurrent expand_stacks.
  1731. */
  1732. vma_lock_anon_vma(vma);
  1733. /* Somebody else might have raced and expanded it already */
  1734. if (address < vma->vm_start) {
  1735. unsigned long size, grow;
  1736. size = vma->vm_end - address;
  1737. grow = (vma->vm_start - address) >> PAGE_SHIFT;
  1738. error = -ENOMEM;
  1739. if (grow <= vma->vm_pgoff) {
  1740. error = acct_stack_growth(vma, size, grow);
  1741. if (!error) {
  1742. vma->vm_start = address;
  1743. vma->vm_pgoff -= grow;
  1744. perf_event_mmap(vma);
  1745. }
  1746. }
  1747. }
  1748. vma_unlock_anon_vma(vma);
  1749. khugepaged_enter_vma_merge(vma, vma->vm_flags);
  1750. return error;
  1751. }
  1752. /* enforced gap between the expanding stack and other mappings. */
  1753. unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
  1754. static int __init cmdline_parse_stack_guard_gap(char *p)
  1755. {
  1756. unsigned long val;
  1757. char *endptr;
  1758. val = simple_strtoul(p, &endptr, 10);
  1759. if (!*endptr)
  1760. stack_guard_gap = val << PAGE_SHIFT;
  1761. return 0;
  1762. }
  1763. __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
  1764. /*
  1765. * Note how expand_stack() refuses to expand the stack all the way to
  1766. * abut the next virtual mapping, *unless* that mapping itself is also
  1767. * a stack mapping. We want to leave room for a guard page, after all
  1768. * (the guard page itself is not added here, that is done by the
  1769. * actual page faulting logic)
  1770. *
  1771. * This matches the behavior of the guard page logic (see mm/memory.c:
  1772. * check_stack_guard_page()), which only allows the guard page to be
  1773. * removed under these circumstances.
  1774. */
  1775. #ifdef CONFIG_STACK_GROWSUP
  1776. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1777. {
  1778. struct vm_area_struct *next;
  1779. address &= PAGE_MASK;
  1780. next = vma->vm_next;
  1781. if (next && next->vm_start == address + PAGE_SIZE) {
  1782. if (!(next->vm_flags & VM_GROWSUP))
  1783. return -ENOMEM;
  1784. }
  1785. return expand_upwards(vma, address);
  1786. }
  1787. struct vm_area_struct *
  1788. find_extend_vma(struct mm_struct *mm, unsigned long addr)
  1789. {
  1790. struct vm_area_struct *vma, *prev;
  1791. addr &= PAGE_MASK;
  1792. vma = find_vma_prev(mm, addr, &prev);
  1793. if (vma && (vma->vm_start <= addr))
  1794. return vma;
  1795. /* don't alter vm_end if the coredump is running */
  1796. if (!prev || !mmget_still_valid(mm) || expand_stack(prev, addr))
  1797. return NULL;
  1798. if (prev->vm_flags & VM_LOCKED) {
  1799. mlock_vma_pages_range(prev, addr, prev->vm_end);
  1800. }
  1801. return prev;
  1802. }
  1803. #else
  1804. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  1805. {
  1806. struct vm_area_struct *prev;
  1807. address &= PAGE_MASK;
  1808. prev = vma->vm_prev;
  1809. if (prev && prev->vm_end == address) {
  1810. if (!(prev->vm_flags & VM_GROWSDOWN))
  1811. return -ENOMEM;
  1812. }
  1813. return expand_downwards(vma, address);
  1814. }
  1815. struct vm_area_struct *
  1816. find_extend_vma(struct mm_struct * mm, unsigned long addr)
  1817. {
  1818. struct vm_area_struct * vma;
  1819. unsigned long start;
  1820. addr &= PAGE_MASK;
  1821. vma = find_vma(mm,addr);
  1822. if (!vma)
  1823. return NULL;
  1824. if (vma->vm_start <= addr)
  1825. return vma;
  1826. if (!(vma->vm_flags & VM_GROWSDOWN))
  1827. return NULL;
  1828. /* don't alter vm_start if the coredump is running */
  1829. if (!mmget_still_valid(mm))
  1830. return NULL;
  1831. start = vma->vm_start;
  1832. if (expand_stack(vma, addr))
  1833. return NULL;
  1834. if (vma->vm_flags & VM_LOCKED) {
  1835. mlock_vma_pages_range(vma, addr, start);
  1836. }
  1837. return vma;
  1838. }
  1839. #endif
  1840. /*
  1841. * Ok - we have the memory areas we should free on the vma list,
  1842. * so release them, and do the vma updates.
  1843. *
  1844. * Called with the mm semaphore held.
  1845. */
  1846. static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
  1847. {
  1848. /* Update high watermark before we lower total_vm */
  1849. update_hiwater_vm(mm);
  1850. do {
  1851. long nrpages = vma_pages(vma);
  1852. mm->total_vm -= nrpages;
  1853. vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
  1854. vma = remove_vma(vma);
  1855. } while (vma);
  1856. validate_mm(mm);
  1857. }
  1858. /*
  1859. * Get rid of page table information in the indicated region.
  1860. *
  1861. * Called with the mm semaphore held.
  1862. */
  1863. static void unmap_region(struct mm_struct *mm,
  1864. struct vm_area_struct *vma, struct vm_area_struct *prev,
  1865. unsigned long start, unsigned long end)
  1866. {
  1867. struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
  1868. struct mmu_gather tlb;
  1869. unsigned long nr_accounted = 0;
  1870. lru_add_drain();
  1871. tlb_gather_mmu(&tlb, mm, 0);
  1872. update_hiwater_rss(mm);
  1873. unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
  1874. vm_unacct_memory(nr_accounted);
  1875. free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
  1876. next ? next->vm_start : USER_PGTABLES_CEILING);
  1877. tlb_finish_mmu(&tlb, start, end);
  1878. }
  1879. /*
  1880. * Create a list of vma's touched by the unmap, removing them from the mm's
  1881. * vma list as we go..
  1882. */
  1883. static void
  1884. detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
  1885. struct vm_area_struct *prev, unsigned long end)
  1886. {
  1887. struct vm_area_struct **insertion_point;
  1888. struct vm_area_struct *tail_vma = NULL;
  1889. unsigned long addr;
  1890. insertion_point = (prev ? &prev->vm_next : &mm->mmap);
  1891. vma->vm_prev = NULL;
  1892. do {
  1893. rb_erase(&vma->vm_rb, &mm->mm_rb);
  1894. mm->map_count--;
  1895. tail_vma = vma;
  1896. vma = vma->vm_next;
  1897. } while (vma && vma->vm_start < end);
  1898. *insertion_point = vma;
  1899. if (vma)
  1900. vma->vm_prev = prev;
  1901. tail_vma->vm_next = NULL;
  1902. if (mm->unmap_area == arch_unmap_area)
  1903. addr = prev ? prev->vm_end : mm->mmap_base;
  1904. else
  1905. addr = vma ? vma->vm_start : mm->mmap_base;
  1906. mm->unmap_area(mm, addr);
  1907. mm->mmap_cache = NULL; /* Kill the cache. */
  1908. }
  1909. /*
  1910. * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
  1911. * munmap path where it doesn't make sense to fail.
  1912. */
  1913. static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
  1914. unsigned long addr, int new_below)
  1915. {
  1916. struct vm_area_struct *new;
  1917. int err = -ENOMEM;
  1918. if (is_vm_hugetlb_page(vma) && (addr &
  1919. ~(huge_page_mask(hstate_vma(vma)))))
  1920. return -EINVAL;
  1921. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1922. if (!new)
  1923. goto out_err;
  1924. /* most fields are the same, copy all, and then fixup */
  1925. *new = *vma;
  1926. INIT_LIST_HEAD(&new->anon_vma_chain);
  1927. if (new_below)
  1928. new->vm_end = addr;
  1929. else {
  1930. new->vm_start = addr;
  1931. new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
  1932. }
  1933. err = vma_dup_policy(vma, new);
  1934. if (err)
  1935. goto out_free_vma;
  1936. err = anon_vma_clone(new, vma);
  1937. if (err)
  1938. goto out_free_mpol;
  1939. if (new->vm_file) {
  1940. get_file(new->vm_file);
  1941. if (vma->vm_flags & VM_EXECUTABLE)
  1942. added_exe_file_vma(mm);
  1943. }
  1944. if (new->vm_ops && new->vm_ops->open)
  1945. new->vm_ops->open(new);
  1946. if (new_below)
  1947. err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
  1948. ((addr - new->vm_start) >> PAGE_SHIFT), new);
  1949. else
  1950. err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
  1951. /* Success. */
  1952. if (!err)
  1953. return 0;
  1954. /* Clean everything up if vma_adjust failed. */
  1955. if (new->vm_ops && new->vm_ops->close)
  1956. new->vm_ops->close(new);
  1957. if (new->vm_file) {
  1958. if (vma->vm_flags & VM_EXECUTABLE)
  1959. removed_exe_file_vma(mm);
  1960. fput(new->vm_file);
  1961. }
  1962. unlink_anon_vmas(new);
  1963. out_free_mpol:
  1964. mpol_put(vma_policy(new));
  1965. out_free_vma:
  1966. kmem_cache_free(vm_area_cachep, new);
  1967. out_err:
  1968. return err;
  1969. }
  1970. /*
  1971. * Split a vma into two pieces at address 'addr', a new vma is allocated
  1972. * either for the first part or the tail.
  1973. */
  1974. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1975. unsigned long addr, int new_below)
  1976. {
  1977. if (mm->map_count >= sysctl_max_map_count)
  1978. return -ENOMEM;
  1979. return __split_vma(mm, vma, addr, new_below);
  1980. }
  1981. /* Munmap is split into 2 main parts -- this part which finds
  1982. * what needs doing, and the areas themselves, which do the
  1983. * work. This now handles partial unmappings.
  1984. * Jeremy Fitzhardinge <jeremy@goop.org>
  1985. */
  1986. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1987. {
  1988. unsigned long end;
  1989. struct vm_area_struct *vma, *prev, *last;
  1990. if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
  1991. return -EINVAL;
  1992. if ((len = PAGE_ALIGN(len)) == 0)
  1993. return -EINVAL;
  1994. /* Find the first overlapping VMA */
  1995. vma = find_vma(mm, start);
  1996. if (!vma)
  1997. return 0;
  1998. prev = vma->vm_prev;
  1999. /* we have start < vma->vm_end */
  2000. /* if it doesn't overlap, we have nothing.. */
  2001. end = start + len;
  2002. if (vma->vm_start >= end)
  2003. return 0;
  2004. /*
  2005. * If we need to split any vma, do it now to save pain later.
  2006. *
  2007. * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
  2008. * unmapped vm_area_struct will remain in use: so lower split_vma
  2009. * places tmp vma above, and higher split_vma places tmp vma below.
  2010. */
  2011. if (start > vma->vm_start) {
  2012. int error;
  2013. /*
  2014. * Make sure that map_count on return from munmap() will
  2015. * not exceed its limit; but let map_count go just above
  2016. * its limit temporarily, to help free resources as expected.
  2017. */
  2018. if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
  2019. return -ENOMEM;
  2020. error = __split_vma(mm, vma, start, 0);
  2021. if (error)
  2022. return error;
  2023. prev = vma;
  2024. }
  2025. /* Does it split the last one? */
  2026. last = find_vma(mm, end);
  2027. if (last && end > last->vm_start) {
  2028. int error = __split_vma(mm, last, end, 1);
  2029. if (error)
  2030. return error;
  2031. }
  2032. vma = prev? prev->vm_next: mm->mmap;
  2033. /*
  2034. * unlock any mlock()ed ranges before detaching vmas
  2035. */
  2036. if (mm->locked_vm) {
  2037. struct vm_area_struct *tmp = vma;
  2038. while (tmp && tmp->vm_start < end) {
  2039. if (tmp->vm_flags & VM_LOCKED) {
  2040. mm->locked_vm -= vma_pages(tmp);
  2041. munlock_vma_pages_all(tmp);
  2042. }
  2043. tmp = tmp->vm_next;
  2044. }
  2045. }
  2046. /*
  2047. * Remove the vma's, and unmap the actual pages
  2048. */
  2049. detach_vmas_to_be_unmapped(mm, vma, prev, end);
  2050. unmap_region(mm, vma, prev, start, end);
  2051. /* Fix up all other VM information */
  2052. remove_vma_list(mm, vma);
  2053. return 0;
  2054. }
  2055. EXPORT_SYMBOL(do_munmap);
  2056. int vm_munmap(unsigned long start, size_t len)
  2057. {
  2058. int ret;
  2059. struct mm_struct *mm = current->mm;
  2060. down_write(&mm->mmap_sem);
  2061. ret = do_munmap(mm, start, len);
  2062. up_write(&mm->mmap_sem);
  2063. return ret;
  2064. }
  2065. EXPORT_SYMBOL(vm_munmap);
  2066. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  2067. {
  2068. profile_munmap(addr);
  2069. return vm_munmap(addr, len);
  2070. }
  2071. static inline void verify_mm_writelocked(struct mm_struct *mm)
  2072. {
  2073. #ifdef CONFIG_DEBUG_VM
  2074. if (unlikely(down_read_trylock(&mm->mmap_sem))) {
  2075. WARN_ON(1);
  2076. up_read(&mm->mmap_sem);
  2077. }
  2078. #endif
  2079. }
  2080. /*
  2081. * this is really a simplified "do_mmap". it only handles
  2082. * anonymous maps. eventually we may be able to do some
  2083. * brk-specific accounting here.
  2084. */
  2085. static unsigned long do_brk(unsigned long addr, unsigned long len)
  2086. {
  2087. struct mm_struct * mm = current->mm;
  2088. struct vm_area_struct * vma, * prev;
  2089. unsigned long flags;
  2090. struct rb_node ** rb_link, * rb_parent;
  2091. pgoff_t pgoff = addr >> PAGE_SHIFT;
  2092. int error;
  2093. len = PAGE_ALIGN(len);
  2094. if (!len)
  2095. return addr;
  2096. flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
  2097. error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  2098. if (error & ~PAGE_MASK)
  2099. return error;
  2100. /*
  2101. * mlock MCL_FUTURE?
  2102. */
  2103. if (mm->def_flags & VM_LOCKED) {
  2104. unsigned long locked, lock_limit;
  2105. locked = len >> PAGE_SHIFT;
  2106. locked += mm->locked_vm;
  2107. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2108. lock_limit >>= PAGE_SHIFT;
  2109. if (locked > lock_limit && !capable(CAP_IPC_LOCK))
  2110. return -EAGAIN;
  2111. }
  2112. /*
  2113. * mm->mmap_sem is required to protect against another thread
  2114. * changing the mappings in case we sleep.
  2115. */
  2116. verify_mm_writelocked(mm);
  2117. /*
  2118. * Clear old maps. this also does some error checking for us
  2119. */
  2120. munmap_back:
  2121. vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  2122. if (vma && vma->vm_start < addr + len) {
  2123. if (do_munmap(mm, addr, len))
  2124. return -ENOMEM;
  2125. goto munmap_back;
  2126. }
  2127. /* Check against address space limits *after* clearing old maps... */
  2128. if (!may_expand_vm(mm, len >> PAGE_SHIFT))
  2129. return -ENOMEM;
  2130. if (mm->map_count > sysctl_max_map_count)
  2131. return -ENOMEM;
  2132. if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
  2133. return -ENOMEM;
  2134. /* Can we just expand an old private anonymous mapping? */
  2135. vma = vma_merge(mm, prev, addr, addr + len, flags,
  2136. NULL, NULL, pgoff, NULL, NULL);
  2137. if (vma)
  2138. goto out;
  2139. /*
  2140. * create a vma struct for an anonymous mapping
  2141. */
  2142. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2143. if (!vma) {
  2144. vm_unacct_memory(len >> PAGE_SHIFT);
  2145. return -ENOMEM;
  2146. }
  2147. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2148. vma->vm_mm = mm;
  2149. vma->vm_start = addr;
  2150. vma->vm_end = addr + len;
  2151. vma->vm_pgoff = pgoff;
  2152. vma->vm_flags = flags;
  2153. vma->vm_page_prot = vm_get_page_prot(flags);
  2154. vma_link(mm, vma, prev, rb_link, rb_parent);
  2155. out:
  2156. perf_event_mmap(vma);
  2157. mm->total_vm += len >> PAGE_SHIFT;
  2158. if (flags & VM_LOCKED) {
  2159. if (!mlock_vma_pages_range(vma, addr, addr + len))
  2160. mm->locked_vm += (len >> PAGE_SHIFT);
  2161. }
  2162. return addr;
  2163. }
  2164. unsigned long vm_brk(unsigned long addr, unsigned long len)
  2165. {
  2166. struct mm_struct *mm = current->mm;
  2167. unsigned long ret;
  2168. down_write(&mm->mmap_sem);
  2169. ret = do_brk(addr, len);
  2170. up_write(&mm->mmap_sem);
  2171. return ret;
  2172. }
  2173. EXPORT_SYMBOL(vm_brk);
  2174. /* Release all mmaps. */
  2175. void exit_mmap(struct mm_struct *mm)
  2176. {
  2177. struct mmu_gather tlb;
  2178. struct vm_area_struct *vma;
  2179. unsigned long nr_accounted = 0;
  2180. /* mm's last user has gone, and its about to be pulled down */
  2181. mmu_notifier_release(mm);
  2182. if (mm->locked_vm) {
  2183. vma = mm->mmap;
  2184. while (vma) {
  2185. if (vma->vm_flags & VM_LOCKED)
  2186. munlock_vma_pages_all(vma);
  2187. vma = vma->vm_next;
  2188. }
  2189. }
  2190. arch_exit_mmap(mm);
  2191. vma = mm->mmap;
  2192. if (!vma) /* Can happen if dup_mmap() received an OOM */
  2193. return;
  2194. lru_add_drain();
  2195. flush_cache_mm(mm);
  2196. tlb_gather_mmu(&tlb, mm, 1);
  2197. /* update_hiwater_rss(mm) here? but nobody should be looking */
  2198. /* Use -1 here to ensure all VMAs in the mm are unmapped */
  2199. unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
  2200. vm_unacct_memory(nr_accounted);
  2201. free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
  2202. tlb_finish_mmu(&tlb, 0, -1);
  2203. /*
  2204. * Walk the list again, actually closing and freeing it,
  2205. * with preemption enabled, without holding any MM locks.
  2206. */
  2207. while (vma)
  2208. vma = remove_vma(vma);
  2209. WARN_ON(atomic_long_read(&mm->nr_ptes) >
  2210. (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
  2211. }
  2212. /* Insert vm structure into process list sorted by address
  2213. * and into the inode's i_mmap tree. If vm_file is non-NULL
  2214. * then i_mmap_mutex is taken here.
  2215. */
  2216. int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
  2217. {
  2218. struct vm_area_struct * __vma, * prev;
  2219. struct rb_node ** rb_link, * rb_parent;
  2220. /*
  2221. * The vm_pgoff of a purely anonymous vma should be irrelevant
  2222. * until its first write fault, when page's anon_vma and index
  2223. * are set. But now set the vm_pgoff it will almost certainly
  2224. * end up with (unless mremap moves it elsewhere before that
  2225. * first wfault), so /proc/pid/maps tells a consistent story.
  2226. *
  2227. * By setting it to reflect the virtual start address of the
  2228. * vma, merges and splits can happen in a seamless way, just
  2229. * using the existing file pgoff checks and manipulations.
  2230. * Similarly in do_mmap_pgoff and in do_brk.
  2231. */
  2232. if (!vma->vm_file) {
  2233. BUG_ON(vma->anon_vma);
  2234. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  2235. }
  2236. __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
  2237. if (__vma && __vma->vm_start < vma->vm_end)
  2238. return -ENOMEM;
  2239. if ((vma->vm_flags & VM_ACCOUNT) &&
  2240. security_vm_enough_memory_mm(mm, vma_pages(vma)))
  2241. return -ENOMEM;
  2242. vma_link(mm, vma, prev, rb_link, rb_parent);
  2243. return 0;
  2244. }
  2245. /*
  2246. * Copy the vma structure to a new location in the same mm,
  2247. * prior to moving page table entries, to effect an mremap move.
  2248. */
  2249. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  2250. unsigned long addr, unsigned long len, pgoff_t pgoff)
  2251. {
  2252. struct vm_area_struct *vma = *vmap;
  2253. unsigned long vma_start = vma->vm_start;
  2254. struct mm_struct *mm = vma->vm_mm;
  2255. struct vm_area_struct *new_vma, *prev;
  2256. struct rb_node **rb_link, *rb_parent;
  2257. bool faulted_in_anon_vma = true;
  2258. /*
  2259. * If anonymous vma has not yet been faulted, update new pgoff
  2260. * to match new location, to increase its chance of merging.
  2261. */
  2262. if (unlikely(!vma->vm_file && !vma->anon_vma)) {
  2263. pgoff = addr >> PAGE_SHIFT;
  2264. faulted_in_anon_vma = false;
  2265. }
  2266. find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
  2267. new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
  2268. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  2269. vma_get_anon_name(vma));
  2270. if (new_vma) {
  2271. /*
  2272. * Source vma may have been merged into new_vma
  2273. */
  2274. if (unlikely(vma_start >= new_vma->vm_start &&
  2275. vma_start < new_vma->vm_end)) {
  2276. /*
  2277. * The only way we can get a vma_merge with
  2278. * self during an mremap is if the vma hasn't
  2279. * been faulted in yet and we were allowed to
  2280. * reset the dst vma->vm_pgoff to the
  2281. * destination address of the mremap to allow
  2282. * the merge to happen. mremap must change the
  2283. * vm_pgoff linearity between src and dst vmas
  2284. * (in turn preventing a vma_merge) to be
  2285. * safe. It is only safe to keep the vm_pgoff
  2286. * linear if there are no pages mapped yet.
  2287. */
  2288. VM_BUG_ON(faulted_in_anon_vma);
  2289. *vmap = new_vma;
  2290. } else
  2291. anon_vma_moveto_tail(new_vma);
  2292. } else {
  2293. new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  2294. if (new_vma) {
  2295. *new_vma = *vma;
  2296. if (vma_dup_policy(vma, new_vma))
  2297. goto out_free_vma;
  2298. INIT_LIST_HEAD(&new_vma->anon_vma_chain);
  2299. if (anon_vma_clone(new_vma, vma))
  2300. goto out_free_mempol;
  2301. new_vma->vm_start = addr;
  2302. new_vma->vm_end = addr + len;
  2303. new_vma->vm_pgoff = pgoff;
  2304. if (new_vma->vm_file) {
  2305. get_file(new_vma->vm_file);
  2306. if (vma->vm_flags & VM_EXECUTABLE)
  2307. added_exe_file_vma(mm);
  2308. }
  2309. if (new_vma->vm_ops && new_vma->vm_ops->open)
  2310. new_vma->vm_ops->open(new_vma);
  2311. vma_link(mm, new_vma, prev, rb_link, rb_parent);
  2312. }
  2313. }
  2314. return new_vma;
  2315. out_free_mempol:
  2316. mpol_put(vma_policy(new_vma));
  2317. out_free_vma:
  2318. kmem_cache_free(vm_area_cachep, new_vma);
  2319. return NULL;
  2320. }
  2321. /*
  2322. * Return true if the calling process may expand its vm space by the passed
  2323. * number of pages
  2324. */
  2325. int may_expand_vm(struct mm_struct *mm, unsigned long npages)
  2326. {
  2327. unsigned long cur = mm->total_vm; /* pages */
  2328. unsigned long lim;
  2329. lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
  2330. if (cur + npages > lim)
  2331. return 0;
  2332. return 1;
  2333. }
  2334. static int special_mapping_fault(struct vm_area_struct *vma,
  2335. struct vm_fault *vmf)
  2336. {
  2337. pgoff_t pgoff;
  2338. struct page **pages;
  2339. /*
  2340. * special mappings have no vm_file, and in that case, the mm
  2341. * uses vm_pgoff internally. So we have to subtract it from here.
  2342. * We are allowed to do this because we are the mm; do not copy
  2343. * this code into drivers!
  2344. */
  2345. pgoff = vmf->pgoff - vma->vm_pgoff;
  2346. for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
  2347. pgoff--;
  2348. if (*pages) {
  2349. struct page *page = *pages;
  2350. get_page(page);
  2351. vmf->page = page;
  2352. return 0;
  2353. }
  2354. return VM_FAULT_SIGBUS;
  2355. }
  2356. /*
  2357. * Having a close hook prevents vma merging regardless of flags.
  2358. */
  2359. static void special_mapping_close(struct vm_area_struct *vma)
  2360. {
  2361. }
  2362. static const struct vm_operations_struct special_mapping_vmops = {
  2363. .close = special_mapping_close,
  2364. .fault = special_mapping_fault,
  2365. };
  2366. /*
  2367. * Called with mm->mmap_sem held for writing.
  2368. * Insert a new vma covering the given region, with the given flags.
  2369. * Its pages are supplied by the given array of struct page *.
  2370. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  2371. * The region past the last page supplied will always produce SIGBUS.
  2372. * The array pointer and the pages it points to are assumed to stay alive
  2373. * for as long as this mapping might exist.
  2374. */
  2375. int install_special_mapping(struct mm_struct *mm,
  2376. unsigned long addr, unsigned long len,
  2377. unsigned long vm_flags, struct page **pages)
  2378. {
  2379. int ret;
  2380. struct vm_area_struct *vma;
  2381. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  2382. if (unlikely(vma == NULL))
  2383. return -ENOMEM;
  2384. INIT_LIST_HEAD(&vma->anon_vma_chain);
  2385. vma->vm_mm = mm;
  2386. vma->vm_start = addr;
  2387. vma->vm_end = addr + len;
  2388. vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
  2389. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2390. vma->vm_ops = &special_mapping_vmops;
  2391. vma->vm_private_data = pages;
  2392. ret = insert_vm_struct(mm, vma);
  2393. if (ret)
  2394. goto out;
  2395. mm->total_vm += len >> PAGE_SHIFT;
  2396. perf_event_mmap(vma);
  2397. return 0;
  2398. out:
  2399. kmem_cache_free(vm_area_cachep, vma);
  2400. return ret;
  2401. }
  2402. static DEFINE_MUTEX(mm_all_locks_mutex);
  2403. static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
  2404. {
  2405. if (!test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
  2406. /*
  2407. * The LSB of head.next can't change from under us
  2408. * because we hold the mm_all_locks_mutex.
  2409. */
  2410. mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
  2411. /*
  2412. * We can safely modify head.next after taking the
  2413. * anon_vma->root->mutex. If some other vma in this mm shares
  2414. * the same anon_vma we won't take it again.
  2415. *
  2416. * No need of atomic instructions here, head.next
  2417. * can't change from under us thanks to the
  2418. * anon_vma->root->mutex.
  2419. */
  2420. if (__test_and_set_bit(0, (unsigned long *)
  2421. &anon_vma->root->head.next))
  2422. BUG();
  2423. }
  2424. }
  2425. static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
  2426. {
  2427. if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2428. /*
  2429. * AS_MM_ALL_LOCKS can't change from under us because
  2430. * we hold the mm_all_locks_mutex.
  2431. *
  2432. * Operations on ->flags have to be atomic because
  2433. * even if AS_MM_ALL_LOCKS is stable thanks to the
  2434. * mm_all_locks_mutex, there may be other cpus
  2435. * changing other bitflags in parallel to us.
  2436. */
  2437. if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
  2438. BUG();
  2439. mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
  2440. }
  2441. }
  2442. /*
  2443. * This operation locks against the VM for all pte/vma/mm related
  2444. * operations that could ever happen on a certain mm. This includes
  2445. * vmtruncate, try_to_unmap, and all page faults.
  2446. *
  2447. * The caller must take the mmap_sem in write mode before calling
  2448. * mm_take_all_locks(). The caller isn't allowed to release the
  2449. * mmap_sem until mm_drop_all_locks() returns.
  2450. *
  2451. * mmap_sem in write mode is required in order to block all operations
  2452. * that could modify pagetables and free pages without need of
  2453. * altering the vma layout (for example populate_range() with
  2454. * nonlinear vmas). It's also needed in write mode to avoid new
  2455. * anon_vmas to be associated with existing vmas.
  2456. *
  2457. * A single task can't take more than one mm_take_all_locks() in a row
  2458. * or it would deadlock.
  2459. *
  2460. * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
  2461. * mapping->flags avoid to take the same lock twice, if more than one
  2462. * vma in this mm is backed by the same anon_vma or address_space.
  2463. *
  2464. * We can take all the locks in random order because the VM code
  2465. * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
  2466. * takes more than one of them in a row. Secondly we're protected
  2467. * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
  2468. *
  2469. * mm_take_all_locks() and mm_drop_all_locks are expensive operations
  2470. * that may have to take thousand of locks.
  2471. *
  2472. * mm_take_all_locks() can fail if it's interrupted by signals.
  2473. */
  2474. int mm_take_all_locks(struct mm_struct *mm)
  2475. {
  2476. struct vm_area_struct *vma;
  2477. struct anon_vma_chain *avc;
  2478. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2479. mutex_lock(&mm_all_locks_mutex);
  2480. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2481. if (signal_pending(current))
  2482. goto out_unlock;
  2483. if (vma->vm_file && vma->vm_file->f_mapping)
  2484. vm_lock_mapping(mm, vma->vm_file->f_mapping);
  2485. }
  2486. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2487. if (signal_pending(current))
  2488. goto out_unlock;
  2489. if (vma->anon_vma)
  2490. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2491. vm_lock_anon_vma(mm, avc->anon_vma);
  2492. }
  2493. return 0;
  2494. out_unlock:
  2495. mm_drop_all_locks(mm);
  2496. return -EINTR;
  2497. }
  2498. static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
  2499. {
  2500. if (test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
  2501. /*
  2502. * The LSB of head.next can't change to 0 from under
  2503. * us because we hold the mm_all_locks_mutex.
  2504. *
  2505. * We must however clear the bitflag before unlocking
  2506. * the vma so the users using the anon_vma->head will
  2507. * never see our bitflag.
  2508. *
  2509. * No need of atomic instructions here, head.next
  2510. * can't change from under us until we release the
  2511. * anon_vma->root->mutex.
  2512. */
  2513. if (!__test_and_clear_bit(0, (unsigned long *)
  2514. &anon_vma->root->head.next))
  2515. BUG();
  2516. anon_vma_unlock(anon_vma);
  2517. }
  2518. }
  2519. static void vm_unlock_mapping(struct address_space *mapping)
  2520. {
  2521. if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
  2522. /*
  2523. * AS_MM_ALL_LOCKS can't change to 0 from under us
  2524. * because we hold the mm_all_locks_mutex.
  2525. */
  2526. mutex_unlock(&mapping->i_mmap_mutex);
  2527. if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
  2528. &mapping->flags))
  2529. BUG();
  2530. }
  2531. }
  2532. /*
  2533. * The mmap_sem cannot be released by the caller until
  2534. * mm_drop_all_locks() returns.
  2535. */
  2536. void mm_drop_all_locks(struct mm_struct *mm)
  2537. {
  2538. struct vm_area_struct *vma;
  2539. struct anon_vma_chain *avc;
  2540. BUG_ON(down_read_trylock(&mm->mmap_sem));
  2541. BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
  2542. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  2543. if (vma->anon_vma)
  2544. list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
  2545. vm_unlock_anon_vma(avc->anon_vma);
  2546. if (vma->vm_file && vma->vm_file->f_mapping)
  2547. vm_unlock_mapping(vma->vm_file->f_mapping);
  2548. }
  2549. mutex_unlock(&mm_all_locks_mutex);
  2550. }
  2551. /*
  2552. * initialise the VMA slab
  2553. */
  2554. void __init mmap_init(void)
  2555. {
  2556. int ret;
  2557. ret = percpu_counter_init(&vm_committed_as, 0);
  2558. VM_BUG_ON(ret);
  2559. }