mmap.c 86 KB

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