mmap.c 70 KB

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