mmap.c 87 KB

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