mmap.c 71 KB

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