mmap.c 86 KB

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