mmap.c 70 KB

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