mmap.c 69 KB

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