mmap.c 69 KB

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