mmap.c 70 KB

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