nommu.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /*
  2. * linux/mm/nommu.c
  3. *
  4. * Replacement code for mm functions to support CPU's that don't
  5. * have any form of memory management unit (thus no virtual memory).
  6. *
  7. * See Documentation/nommu-mmap.txt
  8. *
  9. * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
  10. * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
  11. * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
  12. * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
  13. * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
  14. */
  15. #include <linux/export.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/swap.h>
  19. #include <linux/file.h>
  20. #include <linux/highmem.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mount.h>
  27. #include <linux/personality.h>
  28. #include <linux/security.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/audit.h>
  31. #include <linux/sched/sysctl.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/tlb.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/mmu_context.h>
  36. #include "internal.h"
  37. #if 0
  38. #define kenter(FMT, ...) \
  39. printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  40. #define kleave(FMT, ...) \
  41. printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  42. #define kdebug(FMT, ...) \
  43. printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
  44. #else
  45. #define kenter(FMT, ...) \
  46. no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  47. #define kleave(FMT, ...) \
  48. no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  49. #define kdebug(FMT, ...) \
  50. no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
  51. #endif
  52. void *high_memory;
  53. struct page *mem_map;
  54. unsigned long max_mapnr;
  55. unsigned long num_physpages;
  56. unsigned long highest_memmap_pfn;
  57. struct percpu_counter vm_committed_as;
  58. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  59. int sysctl_overcommit_ratio = 50; /* default is 50% */
  60. int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
  61. int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
  62. unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
  63. unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
  64. int heap_stack_gap = 0;
  65. atomic_long_t mmap_pages_allocated;
  66. /*
  67. * The global memory commitment made in the system can be a metric
  68. * that can be used to drive ballooning decisions when Linux is hosted
  69. * as a guest. On Hyper-V, the host implements a policy engine for dynamically
  70. * balancing memory across competing virtual machines that are hosted.
  71. * Several metrics drive this policy engine including the guest reported
  72. * memory commitment.
  73. */
  74. unsigned long vm_memory_committed(void)
  75. {
  76. return percpu_counter_read_positive(&vm_committed_as);
  77. }
  78. EXPORT_SYMBOL_GPL(vm_memory_committed);
  79. EXPORT_SYMBOL(mem_map);
  80. EXPORT_SYMBOL(num_physpages);
  81. /* list of mapped, potentially shareable regions */
  82. static struct kmem_cache *vm_region_jar;
  83. struct rb_root nommu_region_tree = RB_ROOT;
  84. DECLARE_RWSEM(nommu_region_sem);
  85. const struct vm_operations_struct generic_file_vm_ops = {
  86. };
  87. /*
  88. * Return the total memory allocated for this pointer, not
  89. * just what the caller asked for.
  90. *
  91. * Doesn't have to be accurate, i.e. may have races.
  92. */
  93. unsigned int kobjsize(const void *objp)
  94. {
  95. struct page *page;
  96. /*
  97. * If the object we have should not have ksize performed on it,
  98. * return size of 0
  99. */
  100. if (!objp || !virt_addr_valid(objp))
  101. return 0;
  102. page = virt_to_head_page(objp);
  103. /*
  104. * If the allocator sets PageSlab, we know the pointer came from
  105. * kmalloc().
  106. */
  107. if (PageSlab(page))
  108. return ksize(objp);
  109. /*
  110. * If it's not a compound page, see if we have a matching VMA
  111. * region. This test is intentionally done in reverse order,
  112. * so if there's no VMA, we still fall through and hand back
  113. * PAGE_SIZE for 0-order pages.
  114. */
  115. if (!PageCompound(page)) {
  116. struct vm_area_struct *vma;
  117. vma = find_vma(current->mm, (unsigned long)objp);
  118. if (vma)
  119. return vma->vm_end - vma->vm_start;
  120. }
  121. /*
  122. * The ksize() function is only guaranteed to work for pointers
  123. * returned by kmalloc(). So handle arbitrary pointers here.
  124. */
  125. return PAGE_SIZE << compound_order(page);
  126. }
  127. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  128. unsigned long start, unsigned long nr_pages,
  129. unsigned int foll_flags, struct page **pages,
  130. struct vm_area_struct **vmas, int *nonblocking)
  131. {
  132. struct vm_area_struct *vma;
  133. unsigned long vm_flags;
  134. int i;
  135. /* calculate required read or write permissions.
  136. * If FOLL_FORCE is set, we only require the "MAY" flags.
  137. */
  138. vm_flags = (foll_flags & FOLL_WRITE) ?
  139. (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  140. vm_flags &= (foll_flags & FOLL_FORCE) ?
  141. (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  142. for (i = 0; i < nr_pages; i++) {
  143. vma = find_vma(mm, start);
  144. if (!vma)
  145. goto finish_or_fault;
  146. /* protect what we can, including chardevs */
  147. if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
  148. !(vm_flags & vma->vm_flags))
  149. goto finish_or_fault;
  150. if (pages) {
  151. pages[i] = virt_to_page(start);
  152. if (pages[i])
  153. page_cache_get(pages[i]);
  154. }
  155. if (vmas)
  156. vmas[i] = vma;
  157. start = (start + PAGE_SIZE) & PAGE_MASK;
  158. }
  159. return i;
  160. finish_or_fault:
  161. return i ? : -EFAULT;
  162. }
  163. /*
  164. * get a list of pages in an address range belonging to the specified process
  165. * and indicate the VMA that covers each page
  166. * - this is potentially dodgy as we may end incrementing the page count of a
  167. * slab page or a secondary page from a compound page
  168. * - don't permit access to VMAs that don't support it, such as I/O mappings
  169. */
  170. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  171. unsigned long start, unsigned long nr_pages,
  172. int write, int force, struct page **pages,
  173. struct vm_area_struct **vmas)
  174. {
  175. int flags = 0;
  176. if (write)
  177. flags |= FOLL_WRITE;
  178. if (force)
  179. flags |= FOLL_FORCE;
  180. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  181. NULL);
  182. }
  183. EXPORT_SYMBOL(get_user_pages);
  184. /**
  185. * follow_pfn - look up PFN at a user virtual address
  186. * @vma: memory mapping
  187. * @address: user virtual address
  188. * @pfn: location to store found PFN
  189. *
  190. * Only IO mappings and raw PFN mappings are allowed.
  191. *
  192. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  193. */
  194. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  195. unsigned long *pfn)
  196. {
  197. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  198. return -EINVAL;
  199. *pfn = address >> PAGE_SHIFT;
  200. return 0;
  201. }
  202. EXPORT_SYMBOL(follow_pfn);
  203. LIST_HEAD(vmap_area_list);
  204. void vfree(const void *addr)
  205. {
  206. kfree(addr);
  207. }
  208. EXPORT_SYMBOL(vfree);
  209. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  210. {
  211. /*
  212. * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
  213. * returns only a logical address.
  214. */
  215. return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
  216. }
  217. EXPORT_SYMBOL(__vmalloc);
  218. void *vmalloc_user(unsigned long size)
  219. {
  220. void *ret;
  221. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  222. PAGE_KERNEL);
  223. if (ret) {
  224. struct vm_area_struct *vma;
  225. down_write(&current->mm->mmap_sem);
  226. vma = find_vma(current->mm, (unsigned long)ret);
  227. if (vma)
  228. vma->vm_flags |= VM_USERMAP;
  229. up_write(&current->mm->mmap_sem);
  230. }
  231. return ret;
  232. }
  233. EXPORT_SYMBOL(vmalloc_user);
  234. struct page *vmalloc_to_page(const void *addr)
  235. {
  236. return virt_to_page(addr);
  237. }
  238. EXPORT_SYMBOL(vmalloc_to_page);
  239. unsigned long vmalloc_to_pfn(const void *addr)
  240. {
  241. return page_to_pfn(virt_to_page(addr));
  242. }
  243. EXPORT_SYMBOL(vmalloc_to_pfn);
  244. long vread(char *buf, char *addr, unsigned long count)
  245. {
  246. /* Don't allow overflow */
  247. if ((unsigned long) buf + count < count)
  248. count = -(unsigned long) buf;
  249. memcpy(buf, addr, count);
  250. return count;
  251. }
  252. long vwrite(char *buf, char *addr, unsigned long count)
  253. {
  254. /* Don't allow overflow */
  255. if ((unsigned long) addr + count < count)
  256. count = -(unsigned long) addr;
  257. memcpy(addr, buf, count);
  258. return(count);
  259. }
  260. /*
  261. * vmalloc - allocate virtually continguos memory
  262. *
  263. * @size: allocation size
  264. *
  265. * Allocate enough pages to cover @size from the page level
  266. * allocator and map them into continguos kernel virtual space.
  267. *
  268. * For tight control over page level allocator and protection flags
  269. * use __vmalloc() instead.
  270. */
  271. void *vmalloc(unsigned long size)
  272. {
  273. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  274. }
  275. EXPORT_SYMBOL(vmalloc);
  276. /*
  277. * vzalloc - allocate virtually continguos memory with zero fill
  278. *
  279. * @size: allocation size
  280. *
  281. * Allocate enough pages to cover @size from the page level
  282. * allocator and map them into continguos kernel virtual space.
  283. * The memory allocated is set to zero.
  284. *
  285. * For tight control over page level allocator and protection flags
  286. * use __vmalloc() instead.
  287. */
  288. void *vzalloc(unsigned long size)
  289. {
  290. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  291. PAGE_KERNEL);
  292. }
  293. EXPORT_SYMBOL(vzalloc);
  294. /**
  295. * vmalloc_node - allocate memory on a specific node
  296. * @size: allocation size
  297. * @node: numa node
  298. *
  299. * Allocate enough pages to cover @size from the page level
  300. * allocator and map them into contiguous kernel virtual space.
  301. *
  302. * For tight control over page level allocator and protection flags
  303. * use __vmalloc() instead.
  304. */
  305. void *vmalloc_node(unsigned long size, int node)
  306. {
  307. return vmalloc(size);
  308. }
  309. EXPORT_SYMBOL(vmalloc_node);
  310. /**
  311. * vzalloc_node - allocate memory on a specific node with zero fill
  312. * @size: allocation size
  313. * @node: numa node
  314. *
  315. * Allocate enough pages to cover @size from the page level
  316. * allocator and map them into contiguous kernel virtual space.
  317. * The memory allocated is set to zero.
  318. *
  319. * For tight control over page level allocator and protection flags
  320. * use __vmalloc() instead.
  321. */
  322. void *vzalloc_node(unsigned long size, int node)
  323. {
  324. return vzalloc(size);
  325. }
  326. EXPORT_SYMBOL(vzalloc_node);
  327. #ifndef PAGE_KERNEL_EXEC
  328. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  329. #endif
  330. /**
  331. * vmalloc_exec - allocate virtually contiguous, executable memory
  332. * @size: allocation size
  333. *
  334. * Kernel-internal function to allocate enough pages to cover @size
  335. * the page level allocator and map them into contiguous and
  336. * executable kernel virtual space.
  337. *
  338. * For tight control over page level allocator and protection flags
  339. * use __vmalloc() instead.
  340. */
  341. void *vmalloc_exec(unsigned long size)
  342. {
  343. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  344. }
  345. /**
  346. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  347. * @size: allocation size
  348. *
  349. * Allocate enough 32bit PA addressable pages to cover @size from the
  350. * page level allocator and map them into continguos kernel virtual space.
  351. */
  352. void *vmalloc_32(unsigned long size)
  353. {
  354. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  355. }
  356. EXPORT_SYMBOL(vmalloc_32);
  357. /**
  358. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  359. * @size: allocation size
  360. *
  361. * The resulting memory area is 32bit addressable and zeroed so it can be
  362. * mapped to userspace without leaking data.
  363. *
  364. * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
  365. * remap_vmalloc_range() are permissible.
  366. */
  367. void *vmalloc_32_user(unsigned long size)
  368. {
  369. /*
  370. * We'll have to sort out the ZONE_DMA bits for 64-bit,
  371. * but for now this can simply use vmalloc_user() directly.
  372. */
  373. return vmalloc_user(size);
  374. }
  375. EXPORT_SYMBOL(vmalloc_32_user);
  376. void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
  377. {
  378. BUG();
  379. return NULL;
  380. }
  381. EXPORT_SYMBOL(vmap);
  382. void vunmap(const void *addr)
  383. {
  384. BUG();
  385. }
  386. EXPORT_SYMBOL(vunmap);
  387. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  388. {
  389. BUG();
  390. return NULL;
  391. }
  392. EXPORT_SYMBOL(vm_map_ram);
  393. void vm_unmap_ram(const void *mem, unsigned int count)
  394. {
  395. BUG();
  396. }
  397. EXPORT_SYMBOL(vm_unmap_ram);
  398. void vm_unmap_aliases(void)
  399. {
  400. }
  401. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  402. /*
  403. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  404. * have one.
  405. */
  406. void __attribute__((weak)) vmalloc_sync_all(void)
  407. {
  408. }
  409. /**
  410. * alloc_vm_area - allocate a range of kernel address space
  411. * @size: size of the area
  412. *
  413. * Returns: NULL on failure, vm_struct on success
  414. *
  415. * This function reserves a range of kernel address space, and
  416. * allocates pagetables to map that range. No actual mappings
  417. * are created. If the kernel address space is not shared
  418. * between processes, it syncs the pagetable across all
  419. * processes.
  420. */
  421. struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
  422. {
  423. BUG();
  424. return NULL;
  425. }
  426. EXPORT_SYMBOL_GPL(alloc_vm_area);
  427. void free_vm_area(struct vm_struct *area)
  428. {
  429. BUG();
  430. }
  431. EXPORT_SYMBOL_GPL(free_vm_area);
  432. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  433. struct page *page)
  434. {
  435. return -EINVAL;
  436. }
  437. EXPORT_SYMBOL(vm_insert_page);
  438. /*
  439. * sys_brk() for the most part doesn't need the global kernel
  440. * lock, except when an application is doing something nasty
  441. * like trying to un-brk an area that has already been mapped
  442. * to a regular file. in this case, the unmapping will need
  443. * to invoke file system routines that need the global lock.
  444. */
  445. SYSCALL_DEFINE1(brk, unsigned long, brk)
  446. {
  447. struct mm_struct *mm = current->mm;
  448. if (brk < mm->start_brk || brk > mm->context.end_brk)
  449. return mm->brk;
  450. if (mm->brk == brk)
  451. return mm->brk;
  452. /*
  453. * Always allow shrinking brk
  454. */
  455. if (brk <= mm->brk) {
  456. mm->brk = brk;
  457. return brk;
  458. }
  459. /*
  460. * Ok, looks good - let it rip.
  461. */
  462. flush_icache_range(mm->brk, brk);
  463. return mm->brk = brk;
  464. }
  465. /*
  466. * initialise the VMA and region record slabs
  467. */
  468. void __init mmap_init(void)
  469. {
  470. int ret;
  471. ret = percpu_counter_init(&vm_committed_as, 0);
  472. VM_BUG_ON(ret);
  473. vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
  474. }
  475. /*
  476. * validate the region tree
  477. * - the caller must hold the region lock
  478. */
  479. #ifdef CONFIG_DEBUG_NOMMU_REGIONS
  480. static noinline void validate_nommu_regions(void)
  481. {
  482. struct vm_region *region, *last;
  483. struct rb_node *p, *lastp;
  484. lastp = rb_first(&nommu_region_tree);
  485. if (!lastp)
  486. return;
  487. last = rb_entry(lastp, struct vm_region, vm_rb);
  488. BUG_ON(unlikely(last->vm_end <= last->vm_start));
  489. BUG_ON(unlikely(last->vm_top < last->vm_end));
  490. while ((p = rb_next(lastp))) {
  491. region = rb_entry(p, struct vm_region, vm_rb);
  492. last = rb_entry(lastp, struct vm_region, vm_rb);
  493. BUG_ON(unlikely(region->vm_end <= region->vm_start));
  494. BUG_ON(unlikely(region->vm_top < region->vm_end));
  495. BUG_ON(unlikely(region->vm_start < last->vm_top));
  496. lastp = p;
  497. }
  498. }
  499. #else
  500. static void validate_nommu_regions(void)
  501. {
  502. }
  503. #endif
  504. /*
  505. * add a region into the global tree
  506. */
  507. static void add_nommu_region(struct vm_region *region)
  508. {
  509. struct vm_region *pregion;
  510. struct rb_node **p, *parent;
  511. validate_nommu_regions();
  512. parent = NULL;
  513. p = &nommu_region_tree.rb_node;
  514. while (*p) {
  515. parent = *p;
  516. pregion = rb_entry(parent, struct vm_region, vm_rb);
  517. if (region->vm_start < pregion->vm_start)
  518. p = &(*p)->rb_left;
  519. else if (region->vm_start > pregion->vm_start)
  520. p = &(*p)->rb_right;
  521. else if (pregion == region)
  522. return;
  523. else
  524. BUG();
  525. }
  526. rb_link_node(&region->vm_rb, parent, p);
  527. rb_insert_color(&region->vm_rb, &nommu_region_tree);
  528. validate_nommu_regions();
  529. }
  530. /*
  531. * delete a region from the global tree
  532. */
  533. static void delete_nommu_region(struct vm_region *region)
  534. {
  535. BUG_ON(!nommu_region_tree.rb_node);
  536. validate_nommu_regions();
  537. rb_erase(&region->vm_rb, &nommu_region_tree);
  538. validate_nommu_regions();
  539. }
  540. /*
  541. * free a contiguous series of pages
  542. */
  543. static void free_page_series(unsigned long from, unsigned long to)
  544. {
  545. for (; from < to; from += PAGE_SIZE) {
  546. struct page *page = virt_to_page(from);
  547. kdebug("- free %lx", from);
  548. atomic_long_dec(&mmap_pages_allocated);
  549. if (page_count(page) != 1)
  550. kdebug("free page %p: refcount not one: %d",
  551. page, page_count(page));
  552. put_page(page);
  553. }
  554. }
  555. /*
  556. * release a reference to a region
  557. * - the caller must hold the region semaphore for writing, which this releases
  558. * - the region may not have been added to the tree yet, in which case vm_top
  559. * will equal vm_start
  560. */
  561. static void __put_nommu_region(struct vm_region *region)
  562. __releases(nommu_region_sem)
  563. {
  564. kenter("%p{%d}", region, region->vm_usage);
  565. BUG_ON(!nommu_region_tree.rb_node);
  566. if (--region->vm_usage == 0) {
  567. if (region->vm_top > region->vm_start)
  568. delete_nommu_region(region);
  569. up_write(&nommu_region_sem);
  570. if (region->vm_file)
  571. fput(region->vm_file);
  572. /* IO memory and memory shared directly out of the pagecache
  573. * from ramfs/tmpfs mustn't be released here */
  574. if (region->vm_flags & VM_MAPPED_COPY) {
  575. kdebug("free series");
  576. free_page_series(region->vm_start, region->vm_top);
  577. }
  578. kmem_cache_free(vm_region_jar, region);
  579. } else {
  580. up_write(&nommu_region_sem);
  581. }
  582. }
  583. /*
  584. * release a reference to a region
  585. */
  586. static void put_nommu_region(struct vm_region *region)
  587. {
  588. down_write(&nommu_region_sem);
  589. __put_nommu_region(region);
  590. }
  591. /*
  592. * update protection on a vma
  593. */
  594. static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
  595. {
  596. #ifdef CONFIG_MPU
  597. struct mm_struct *mm = vma->vm_mm;
  598. long start = vma->vm_start & PAGE_MASK;
  599. while (start < vma->vm_end) {
  600. protect_page(mm, start, flags);
  601. start += PAGE_SIZE;
  602. }
  603. update_protections(mm);
  604. #endif
  605. }
  606. /*
  607. * add a VMA into a process's mm_struct in the appropriate place in the list
  608. * and tree and add to the address space's page tree also if not an anonymous
  609. * page
  610. * - should be called with mm->mmap_sem held writelocked
  611. */
  612. static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
  613. {
  614. struct vm_area_struct *pvma, *prev;
  615. struct address_space *mapping;
  616. struct rb_node **p, *parent, *rb_prev;
  617. kenter(",%p", vma);
  618. BUG_ON(!vma->vm_region);
  619. mm->map_count++;
  620. vma->vm_mm = mm;
  621. protect_vma(vma, vma->vm_flags);
  622. /* add the VMA to the mapping */
  623. if (vma->vm_file) {
  624. mapping = vma->vm_file->f_mapping;
  625. mutex_lock(&mapping->i_mmap_mutex);
  626. flush_dcache_mmap_lock(mapping);
  627. vma_interval_tree_insert(vma, &mapping->i_mmap);
  628. flush_dcache_mmap_unlock(mapping);
  629. mutex_unlock(&mapping->i_mmap_mutex);
  630. }
  631. /* add the VMA to the tree */
  632. parent = rb_prev = NULL;
  633. p = &mm->mm_rb.rb_node;
  634. while (*p) {
  635. parent = *p;
  636. pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
  637. /* sort by: start addr, end addr, VMA struct addr in that order
  638. * (the latter is necessary as we may get identical VMAs) */
  639. if (vma->vm_start < pvma->vm_start)
  640. p = &(*p)->rb_left;
  641. else if (vma->vm_start > pvma->vm_start) {
  642. rb_prev = parent;
  643. p = &(*p)->rb_right;
  644. } else if (vma->vm_end < pvma->vm_end)
  645. p = &(*p)->rb_left;
  646. else if (vma->vm_end > pvma->vm_end) {
  647. rb_prev = parent;
  648. p = &(*p)->rb_right;
  649. } else if (vma < pvma)
  650. p = &(*p)->rb_left;
  651. else if (vma > pvma) {
  652. rb_prev = parent;
  653. p = &(*p)->rb_right;
  654. } else
  655. BUG();
  656. }
  657. rb_link_node(&vma->vm_rb, parent, p);
  658. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  659. /* add VMA to the VMA list also */
  660. prev = NULL;
  661. if (rb_prev)
  662. prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
  663. __vma_link_list(mm, vma, prev, parent);
  664. }
  665. /*
  666. * delete a VMA from its owning mm_struct and address space
  667. */
  668. static void delete_vma_from_mm(struct vm_area_struct *vma)
  669. {
  670. struct address_space *mapping;
  671. struct mm_struct *mm = vma->vm_mm;
  672. kenter("%p", vma);
  673. protect_vma(vma, 0);
  674. mm->map_count--;
  675. if (mm->mmap_cache == vma)
  676. mm->mmap_cache = NULL;
  677. /* remove the VMA from the mapping */
  678. if (vma->vm_file) {
  679. mapping = vma->vm_file->f_mapping;
  680. mutex_lock(&mapping->i_mmap_mutex);
  681. flush_dcache_mmap_lock(mapping);
  682. vma_interval_tree_remove(vma, &mapping->i_mmap);
  683. flush_dcache_mmap_unlock(mapping);
  684. mutex_unlock(&mapping->i_mmap_mutex);
  685. }
  686. /* remove from the MM's tree and list */
  687. rb_erase(&vma->vm_rb, &mm->mm_rb);
  688. if (vma->vm_prev)
  689. vma->vm_prev->vm_next = vma->vm_next;
  690. else
  691. mm->mmap = vma->vm_next;
  692. if (vma->vm_next)
  693. vma->vm_next->vm_prev = vma->vm_prev;
  694. }
  695. /*
  696. * destroy a VMA record
  697. */
  698. static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  699. {
  700. kenter("%p", vma);
  701. if (vma->vm_ops && vma->vm_ops->close)
  702. vma->vm_ops->close(vma);
  703. if (vma->vm_file)
  704. fput(vma->vm_file);
  705. put_nommu_region(vma->vm_region);
  706. kmem_cache_free(vm_area_cachep, vma);
  707. }
  708. /*
  709. * look up the first VMA in which addr resides, NULL if none
  710. * - should be called with mm->mmap_sem at least held readlocked
  711. */
  712. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  713. {
  714. struct vm_area_struct *vma;
  715. /* check the cache first */
  716. vma = ACCESS_ONCE(mm->mmap_cache);
  717. if (vma && vma->vm_start <= addr && vma->vm_end > addr)
  718. return vma;
  719. /* trawl the list (there may be multiple mappings in which addr
  720. * resides) */
  721. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  722. if (vma->vm_start > addr)
  723. return NULL;
  724. if (vma->vm_end > addr) {
  725. mm->mmap_cache = vma;
  726. return vma;
  727. }
  728. }
  729. return NULL;
  730. }
  731. EXPORT_SYMBOL(find_vma);
  732. /*
  733. * find a VMA
  734. * - we don't extend stack VMAs under NOMMU conditions
  735. */
  736. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  737. {
  738. return find_vma(mm, addr);
  739. }
  740. /*
  741. * expand a stack to a given address
  742. * - not supported under NOMMU conditions
  743. */
  744. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  745. {
  746. return -ENOMEM;
  747. }
  748. /*
  749. * look up the first VMA exactly that exactly matches addr
  750. * - should be called with mm->mmap_sem at least held readlocked
  751. */
  752. static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  753. unsigned long addr,
  754. unsigned long len)
  755. {
  756. struct vm_area_struct *vma;
  757. unsigned long end = addr + len;
  758. /* check the cache first */
  759. vma = mm->mmap_cache;
  760. if (vma && vma->vm_start == addr && vma->vm_end == end)
  761. return vma;
  762. /* trawl the list (there may be multiple mappings in which addr
  763. * resides) */
  764. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  765. if (vma->vm_start < addr)
  766. continue;
  767. if (vma->vm_start > addr)
  768. return NULL;
  769. if (vma->vm_end == end) {
  770. mm->mmap_cache = vma;
  771. return vma;
  772. }
  773. }
  774. return NULL;
  775. }
  776. /*
  777. * determine whether a mapping should be permitted and, if so, what sort of
  778. * mapping we're capable of supporting
  779. */
  780. static int validate_mmap_request(struct file *file,
  781. unsigned long addr,
  782. unsigned long len,
  783. unsigned long prot,
  784. unsigned long flags,
  785. unsigned long pgoff,
  786. unsigned long *_capabilities)
  787. {
  788. unsigned long capabilities, rlen;
  789. int ret;
  790. /* do the simple checks first */
  791. if (flags & MAP_FIXED) {
  792. printk(KERN_DEBUG
  793. "%d: Can't do fixed-address/overlay mmap of RAM\n",
  794. current->pid);
  795. return -EINVAL;
  796. }
  797. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  798. (flags & MAP_TYPE) != MAP_SHARED)
  799. return -EINVAL;
  800. if (!len)
  801. return -EINVAL;
  802. /* Careful about overflows.. */
  803. rlen = PAGE_ALIGN(len);
  804. if (!rlen || rlen > TASK_SIZE)
  805. return -ENOMEM;
  806. /* offset overflow? */
  807. if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
  808. return -EOVERFLOW;
  809. if (file) {
  810. /* validate file mapping requests */
  811. struct address_space *mapping;
  812. /* files must support mmap */
  813. if (!file->f_op || !file->f_op->mmap)
  814. return -ENODEV;
  815. /* work out if what we've got could possibly be shared
  816. * - we support chardevs that provide their own "memory"
  817. * - we support files/blockdevs that are memory backed
  818. */
  819. mapping = file->f_mapping;
  820. if (!mapping)
  821. mapping = file_inode(file)->i_mapping;
  822. capabilities = 0;
  823. if (mapping && mapping->backing_dev_info)
  824. capabilities = mapping->backing_dev_info->capabilities;
  825. if (!capabilities) {
  826. /* no explicit capabilities set, so assume some
  827. * defaults */
  828. switch (file_inode(file)->i_mode & S_IFMT) {
  829. case S_IFREG:
  830. case S_IFBLK:
  831. capabilities = BDI_CAP_MAP_COPY;
  832. break;
  833. case S_IFCHR:
  834. capabilities =
  835. BDI_CAP_MAP_DIRECT |
  836. BDI_CAP_READ_MAP |
  837. BDI_CAP_WRITE_MAP;
  838. break;
  839. default:
  840. return -EINVAL;
  841. }
  842. }
  843. /* eliminate any capabilities that we can't support on this
  844. * device */
  845. if (!file->f_op->get_unmapped_area)
  846. capabilities &= ~BDI_CAP_MAP_DIRECT;
  847. if (!file->f_op->read)
  848. capabilities &= ~BDI_CAP_MAP_COPY;
  849. /* The file shall have been opened with read permission. */
  850. if (!(file->f_mode & FMODE_READ))
  851. return -EACCES;
  852. if (flags & MAP_SHARED) {
  853. /* do checks for writing, appending and locking */
  854. if ((prot & PROT_WRITE) &&
  855. !(file->f_mode & FMODE_WRITE))
  856. return -EACCES;
  857. if (IS_APPEND(file_inode(file)) &&
  858. (file->f_mode & FMODE_WRITE))
  859. return -EACCES;
  860. if (locks_verify_locked(file_inode(file)))
  861. return -EAGAIN;
  862. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  863. return -ENODEV;
  864. /* we mustn't privatise shared mappings */
  865. capabilities &= ~BDI_CAP_MAP_COPY;
  866. }
  867. else {
  868. /* we're going to read the file into private memory we
  869. * allocate */
  870. if (!(capabilities & BDI_CAP_MAP_COPY))
  871. return -ENODEV;
  872. /* we don't permit a private writable mapping to be
  873. * shared with the backing device */
  874. if (prot & PROT_WRITE)
  875. capabilities &= ~BDI_CAP_MAP_DIRECT;
  876. }
  877. if (capabilities & BDI_CAP_MAP_DIRECT) {
  878. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  879. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  880. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  881. ) {
  882. capabilities &= ~BDI_CAP_MAP_DIRECT;
  883. if (flags & MAP_SHARED) {
  884. printk(KERN_WARNING
  885. "MAP_SHARED not completely supported on !MMU\n");
  886. return -EINVAL;
  887. }
  888. }
  889. }
  890. /* handle executable mappings and implied executable
  891. * mappings */
  892. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  893. if (prot & PROT_EXEC)
  894. return -EPERM;
  895. }
  896. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  897. /* handle implication of PROT_EXEC by PROT_READ */
  898. if (current->personality & READ_IMPLIES_EXEC) {
  899. if (capabilities & BDI_CAP_EXEC_MAP)
  900. prot |= PROT_EXEC;
  901. }
  902. }
  903. else if ((prot & PROT_READ) &&
  904. (prot & PROT_EXEC) &&
  905. !(capabilities & BDI_CAP_EXEC_MAP)
  906. ) {
  907. /* backing file is not executable, try to copy */
  908. capabilities &= ~BDI_CAP_MAP_DIRECT;
  909. }
  910. }
  911. else {
  912. /* anonymous mappings are always memory backed and can be
  913. * privately mapped
  914. */
  915. capabilities = BDI_CAP_MAP_COPY;
  916. /* handle PROT_EXEC implication by PROT_READ */
  917. if ((prot & PROT_READ) &&
  918. (current->personality & READ_IMPLIES_EXEC))
  919. prot |= PROT_EXEC;
  920. }
  921. /* allow the security API to have its say */
  922. ret = security_mmap_addr(addr);
  923. if (ret < 0)
  924. return ret;
  925. /* looks okay */
  926. *_capabilities = capabilities;
  927. return 0;
  928. }
  929. /*
  930. * we've determined that we can make the mapping, now translate what we
  931. * now know into VMA flags
  932. */
  933. static unsigned long determine_vm_flags(struct file *file,
  934. unsigned long prot,
  935. unsigned long flags,
  936. unsigned long capabilities)
  937. {
  938. unsigned long vm_flags;
  939. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  940. /* vm_flags |= mm->def_flags; */
  941. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  942. /* attempt to share read-only copies of mapped file chunks */
  943. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  944. if (file && !(prot & PROT_WRITE))
  945. vm_flags |= VM_MAYSHARE;
  946. } else {
  947. /* overlay a shareable mapping on the backing device or inode
  948. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  949. * romfs/cramfs */
  950. vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
  951. if (flags & MAP_SHARED)
  952. vm_flags |= VM_SHARED;
  953. }
  954. /* refuse to let anyone share private mappings with this process if
  955. * it's being traced - otherwise breakpoints set in it may interfere
  956. * with another untraced process
  957. */
  958. if ((flags & MAP_PRIVATE) && current->ptrace)
  959. vm_flags &= ~VM_MAYSHARE;
  960. return vm_flags;
  961. }
  962. /*
  963. * set up a shared mapping on a file (the driver or filesystem provides and
  964. * pins the storage)
  965. */
  966. static int do_mmap_shared_file(struct vm_area_struct *vma)
  967. {
  968. int ret;
  969. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  970. if (ret == 0) {
  971. vma->vm_region->vm_top = vma->vm_region->vm_end;
  972. return 0;
  973. }
  974. if (ret != -ENOSYS)
  975. return ret;
  976. /* getting -ENOSYS indicates that direct mmap isn't possible (as
  977. * opposed to tried but failed) so we can only give a suitable error as
  978. * it's not possible to make a private copy if MAP_SHARED was given */
  979. return -ENODEV;
  980. }
  981. /*
  982. * set up a private mapping or an anonymous shared mapping
  983. */
  984. static int do_mmap_private(struct vm_area_struct *vma,
  985. struct vm_region *region,
  986. unsigned long len,
  987. unsigned long capabilities)
  988. {
  989. struct page *pages;
  990. unsigned long total, point, n;
  991. void *base;
  992. int ret, order;
  993. /* invoke the file's mapping function so that it can keep track of
  994. * shared mappings on devices or memory
  995. * - VM_MAYSHARE will be set if it may attempt to share
  996. */
  997. if (capabilities & BDI_CAP_MAP_DIRECT) {
  998. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  999. if (ret == 0) {
  1000. /* shouldn't return success if we're not sharing */
  1001. BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
  1002. vma->vm_region->vm_top = vma->vm_region->vm_end;
  1003. return 0;
  1004. }
  1005. if (ret != -ENOSYS)
  1006. return ret;
  1007. /* getting an ENOSYS error indicates that direct mmap isn't
  1008. * possible (as opposed to tried but failed) so we'll try to
  1009. * make a private copy of the data and map that instead */
  1010. }
  1011. /* allocate some memory to hold the mapping
  1012. * - note that this may not return a page-aligned address if the object
  1013. * we're allocating is smaller than a page
  1014. */
  1015. order = get_order(len);
  1016. kdebug("alloc order %d for %lx", order, len);
  1017. pages = alloc_pages(GFP_KERNEL, order);
  1018. if (!pages)
  1019. goto enomem;
  1020. total = 1 << order;
  1021. atomic_long_add(total, &mmap_pages_allocated);
  1022. point = len >> PAGE_SHIFT;
  1023. /* we allocated a power-of-2 sized page set, so we may want to trim off
  1024. * the excess */
  1025. if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
  1026. while (total > point) {
  1027. order = ilog2(total - point);
  1028. n = 1 << order;
  1029. kdebug("shave %lu/%lu @%lu", n, total - point, total);
  1030. atomic_long_sub(n, &mmap_pages_allocated);
  1031. total -= n;
  1032. set_page_refcounted(pages + total);
  1033. __free_pages(pages + total, order);
  1034. }
  1035. }
  1036. for (point = 1; point < total; point++)
  1037. set_page_refcounted(&pages[point]);
  1038. base = page_address(pages);
  1039. region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
  1040. region->vm_start = (unsigned long) base;
  1041. region->vm_end = region->vm_start + len;
  1042. region->vm_top = region->vm_start + (total << PAGE_SHIFT);
  1043. vma->vm_start = region->vm_start;
  1044. vma->vm_end = region->vm_start + len;
  1045. if (vma->vm_file) {
  1046. /* read the contents of a file into the copy */
  1047. mm_segment_t old_fs;
  1048. loff_t fpos;
  1049. fpos = vma->vm_pgoff;
  1050. fpos <<= PAGE_SHIFT;
  1051. old_fs = get_fs();
  1052. set_fs(KERNEL_DS);
  1053. ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
  1054. set_fs(old_fs);
  1055. if (ret < 0)
  1056. goto error_free;
  1057. /* clear the last little bit */
  1058. if (ret < len)
  1059. memset(base + ret, 0, len - ret);
  1060. }
  1061. return 0;
  1062. error_free:
  1063. free_page_series(region->vm_start, region->vm_top);
  1064. region->vm_start = vma->vm_start = 0;
  1065. region->vm_end = vma->vm_end = 0;
  1066. region->vm_top = 0;
  1067. return ret;
  1068. enomem:
  1069. printk("Allocation of length %lu from process %d (%s) failed\n",
  1070. len, current->pid, current->comm);
  1071. show_free_areas(0);
  1072. return -ENOMEM;
  1073. }
  1074. /*
  1075. * handle mapping creation for uClinux
  1076. */
  1077. unsigned long do_mmap_pgoff(struct file *file,
  1078. unsigned long addr,
  1079. unsigned long len,
  1080. unsigned long prot,
  1081. unsigned long flags,
  1082. unsigned long pgoff,
  1083. unsigned long *populate)
  1084. {
  1085. struct vm_area_struct *vma;
  1086. struct vm_region *region;
  1087. struct rb_node *rb;
  1088. unsigned long capabilities, vm_flags, result;
  1089. int ret;
  1090. kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
  1091. *populate = 0;
  1092. /* decide whether we should attempt the mapping, and if so what sort of
  1093. * mapping */
  1094. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  1095. &capabilities);
  1096. if (ret < 0) {
  1097. kleave(" = %d [val]", ret);
  1098. return ret;
  1099. }
  1100. /* we ignore the address hint */
  1101. addr = 0;
  1102. len = PAGE_ALIGN(len);
  1103. /* we've determined that we can make the mapping, now translate what we
  1104. * now know into VMA flags */
  1105. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  1106. /* we're going to need to record the mapping */
  1107. region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
  1108. if (!region)
  1109. goto error_getting_region;
  1110. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1111. if (!vma)
  1112. goto error_getting_vma;
  1113. region->vm_usage = 1;
  1114. region->vm_flags = vm_flags;
  1115. region->vm_pgoff = pgoff;
  1116. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1117. vma->vm_flags = vm_flags;
  1118. vma->vm_pgoff = pgoff;
  1119. if (file) {
  1120. region->vm_file = get_file(file);
  1121. vma->vm_file = get_file(file);
  1122. }
  1123. down_write(&nommu_region_sem);
  1124. /* if we want to share, we need to check for regions created by other
  1125. * mmap() calls that overlap with our proposed mapping
  1126. * - we can only share with a superset match on most regular files
  1127. * - shared mappings on character devices and memory backed files are
  1128. * permitted to overlap inexactly as far as we are concerned for in
  1129. * these cases, sharing is handled in the driver or filesystem rather
  1130. * than here
  1131. */
  1132. if (vm_flags & VM_MAYSHARE) {
  1133. struct vm_region *pregion;
  1134. unsigned long pglen, rpglen, pgend, rpgend, start;
  1135. pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1136. pgend = pgoff + pglen;
  1137. for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
  1138. pregion = rb_entry(rb, struct vm_region, vm_rb);
  1139. if (!(pregion->vm_flags & VM_MAYSHARE))
  1140. continue;
  1141. /* search for overlapping mappings on the same file */
  1142. if (file_inode(pregion->vm_file) !=
  1143. file_inode(file))
  1144. continue;
  1145. if (pregion->vm_pgoff >= pgend)
  1146. continue;
  1147. rpglen = pregion->vm_end - pregion->vm_start;
  1148. rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1149. rpgend = pregion->vm_pgoff + rpglen;
  1150. if (pgoff >= rpgend)
  1151. continue;
  1152. /* handle inexactly overlapping matches between
  1153. * mappings */
  1154. if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
  1155. !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
  1156. /* new mapping is not a subset of the region */
  1157. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  1158. goto sharing_violation;
  1159. continue;
  1160. }
  1161. /* we've found a region we can share */
  1162. pregion->vm_usage++;
  1163. vma->vm_region = pregion;
  1164. start = pregion->vm_start;
  1165. start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
  1166. vma->vm_start = start;
  1167. vma->vm_end = start + len;
  1168. if (pregion->vm_flags & VM_MAPPED_COPY) {
  1169. kdebug("share copy");
  1170. vma->vm_flags |= VM_MAPPED_COPY;
  1171. } else {
  1172. kdebug("share mmap");
  1173. ret = do_mmap_shared_file(vma);
  1174. if (ret < 0) {
  1175. vma->vm_region = NULL;
  1176. vma->vm_start = 0;
  1177. vma->vm_end = 0;
  1178. pregion->vm_usage--;
  1179. pregion = NULL;
  1180. goto error_just_free;
  1181. }
  1182. }
  1183. fput(region->vm_file);
  1184. kmem_cache_free(vm_region_jar, region);
  1185. region = pregion;
  1186. result = start;
  1187. goto share;
  1188. }
  1189. /* obtain the address at which to make a shared mapping
  1190. * - this is the hook for quasi-memory character devices to
  1191. * tell us the location of a shared mapping
  1192. */
  1193. if (capabilities & BDI_CAP_MAP_DIRECT) {
  1194. addr = file->f_op->get_unmapped_area(file, addr, len,
  1195. pgoff, flags);
  1196. if (IS_ERR_VALUE(addr)) {
  1197. ret = addr;
  1198. if (ret != -ENOSYS)
  1199. goto error_just_free;
  1200. /* the driver refused to tell us where to site
  1201. * the mapping so we'll have to attempt to copy
  1202. * it */
  1203. ret = -ENODEV;
  1204. if (!(capabilities & BDI_CAP_MAP_COPY))
  1205. goto error_just_free;
  1206. capabilities &= ~BDI_CAP_MAP_DIRECT;
  1207. } else {
  1208. vma->vm_start = region->vm_start = addr;
  1209. vma->vm_end = region->vm_end = addr + len;
  1210. }
  1211. }
  1212. }
  1213. vma->vm_region = region;
  1214. /* set up the mapping
  1215. * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
  1216. */
  1217. if (file && vma->vm_flags & VM_SHARED)
  1218. ret = do_mmap_shared_file(vma);
  1219. else
  1220. ret = do_mmap_private(vma, region, len, capabilities);
  1221. if (ret < 0)
  1222. goto error_just_free;
  1223. add_nommu_region(region);
  1224. /* clear anonymous mappings that don't ask for uninitialized data */
  1225. if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
  1226. memset((void *)region->vm_start, 0,
  1227. region->vm_end - region->vm_start);
  1228. /* okay... we have a mapping; now we have to register it */
  1229. result = vma->vm_start;
  1230. current->mm->total_vm += len >> PAGE_SHIFT;
  1231. share:
  1232. add_vma_to_mm(current->mm, vma);
  1233. /* we flush the region from the icache only when the first executable
  1234. * mapping of it is made */
  1235. if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
  1236. flush_icache_range(region->vm_start, region->vm_end);
  1237. region->vm_icache_flushed = true;
  1238. }
  1239. up_write(&nommu_region_sem);
  1240. kleave(" = %lx", result);
  1241. return result;
  1242. error_just_free:
  1243. up_write(&nommu_region_sem);
  1244. error:
  1245. if (region->vm_file)
  1246. fput(region->vm_file);
  1247. kmem_cache_free(vm_region_jar, region);
  1248. if (vma->vm_file)
  1249. fput(vma->vm_file);
  1250. kmem_cache_free(vm_area_cachep, vma);
  1251. kleave(" = %d", ret);
  1252. return ret;
  1253. sharing_violation:
  1254. up_write(&nommu_region_sem);
  1255. printk(KERN_WARNING "Attempt to share mismatched mappings\n");
  1256. ret = -EINVAL;
  1257. goto error;
  1258. error_getting_vma:
  1259. kmem_cache_free(vm_region_jar, region);
  1260. printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
  1261. " from process %d failed\n",
  1262. len, current->pid);
  1263. show_free_areas(0);
  1264. return -ENOMEM;
  1265. error_getting_region:
  1266. printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
  1267. " from process %d failed\n",
  1268. len, current->pid);
  1269. show_free_areas(0);
  1270. return -ENOMEM;
  1271. }
  1272. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1273. unsigned long, prot, unsigned long, flags,
  1274. unsigned long, fd, unsigned long, pgoff)
  1275. {
  1276. struct file *file = NULL;
  1277. unsigned long retval = -EBADF;
  1278. audit_mmap_fd(fd, flags);
  1279. if (!(flags & MAP_ANONYMOUS)) {
  1280. file = fget(fd);
  1281. if (!file)
  1282. goto out;
  1283. }
  1284. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1285. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1286. if (file)
  1287. fput(file);
  1288. out:
  1289. return retval;
  1290. }
  1291. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1292. struct mmap_arg_struct {
  1293. unsigned long addr;
  1294. unsigned long len;
  1295. unsigned long prot;
  1296. unsigned long flags;
  1297. unsigned long fd;
  1298. unsigned long offset;
  1299. };
  1300. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1301. {
  1302. struct mmap_arg_struct a;
  1303. if (copy_from_user(&a, arg, sizeof(a)))
  1304. return -EFAULT;
  1305. if (a.offset & ~PAGE_MASK)
  1306. return -EINVAL;
  1307. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1308. a.offset >> PAGE_SHIFT);
  1309. }
  1310. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1311. /*
  1312. * split a vma into two pieces at address 'addr', a new vma is allocated either
  1313. * for the first part or the tail.
  1314. */
  1315. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1316. unsigned long addr, int new_below)
  1317. {
  1318. struct vm_area_struct *new;
  1319. struct vm_region *region;
  1320. unsigned long npages;
  1321. kenter("");
  1322. /* we're only permitted to split anonymous regions (these should have
  1323. * only a single usage on the region) */
  1324. if (vma->vm_file)
  1325. return -ENOMEM;
  1326. if (mm->map_count >= sysctl_max_map_count)
  1327. return -ENOMEM;
  1328. region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
  1329. if (!region)
  1330. return -ENOMEM;
  1331. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1332. if (!new) {
  1333. kmem_cache_free(vm_region_jar, region);
  1334. return -ENOMEM;
  1335. }
  1336. /* most fields are the same, copy all, and then fixup */
  1337. *new = *vma;
  1338. *region = *vma->vm_region;
  1339. new->vm_region = region;
  1340. npages = (addr - vma->vm_start) >> PAGE_SHIFT;
  1341. if (new_below) {
  1342. region->vm_top = region->vm_end = new->vm_end = addr;
  1343. } else {
  1344. region->vm_start = new->vm_start = addr;
  1345. region->vm_pgoff = new->vm_pgoff += npages;
  1346. }
  1347. if (new->vm_ops && new->vm_ops->open)
  1348. new->vm_ops->open(new);
  1349. delete_vma_from_mm(vma);
  1350. down_write(&nommu_region_sem);
  1351. delete_nommu_region(vma->vm_region);
  1352. if (new_below) {
  1353. vma->vm_region->vm_start = vma->vm_start = addr;
  1354. vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
  1355. } else {
  1356. vma->vm_region->vm_end = vma->vm_end = addr;
  1357. vma->vm_region->vm_top = addr;
  1358. }
  1359. add_nommu_region(vma->vm_region);
  1360. add_nommu_region(new->vm_region);
  1361. up_write(&nommu_region_sem);
  1362. add_vma_to_mm(mm, vma);
  1363. add_vma_to_mm(mm, new);
  1364. return 0;
  1365. }
  1366. /*
  1367. * shrink a VMA by removing the specified chunk from either the beginning or
  1368. * the end
  1369. */
  1370. static int shrink_vma(struct mm_struct *mm,
  1371. struct vm_area_struct *vma,
  1372. unsigned long from, unsigned long to)
  1373. {
  1374. struct vm_region *region;
  1375. kenter("");
  1376. /* adjust the VMA's pointers, which may reposition it in the MM's tree
  1377. * and list */
  1378. delete_vma_from_mm(vma);
  1379. if (from > vma->vm_start)
  1380. vma->vm_end = from;
  1381. else
  1382. vma->vm_start = to;
  1383. add_vma_to_mm(mm, vma);
  1384. /* cut the backing region down to size */
  1385. region = vma->vm_region;
  1386. BUG_ON(region->vm_usage != 1);
  1387. down_write(&nommu_region_sem);
  1388. delete_nommu_region(region);
  1389. if (from > region->vm_start) {
  1390. to = region->vm_top;
  1391. region->vm_top = region->vm_end = from;
  1392. } else {
  1393. region->vm_start = to;
  1394. }
  1395. add_nommu_region(region);
  1396. up_write(&nommu_region_sem);
  1397. free_page_series(from, to);
  1398. return 0;
  1399. }
  1400. /*
  1401. * release a mapping
  1402. * - under NOMMU conditions the chunk to be unmapped must be backed by a single
  1403. * VMA, though it need not cover the whole VMA
  1404. */
  1405. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1406. {
  1407. struct vm_area_struct *vma;
  1408. unsigned long end;
  1409. int ret;
  1410. kenter(",%lx,%zx", start, len);
  1411. len = PAGE_ALIGN(len);
  1412. if (len == 0)
  1413. return -EINVAL;
  1414. end = start + len;
  1415. /* find the first potentially overlapping VMA */
  1416. vma = find_vma(mm, start);
  1417. if (!vma) {
  1418. static int limit = 0;
  1419. if (limit < 5) {
  1420. printk(KERN_WARNING
  1421. "munmap of memory not mmapped by process %d"
  1422. " (%s): 0x%lx-0x%lx\n",
  1423. current->pid, current->comm,
  1424. start, start + len - 1);
  1425. limit++;
  1426. }
  1427. return -EINVAL;
  1428. }
  1429. /* we're allowed to split an anonymous VMA but not a file-backed one */
  1430. if (vma->vm_file) {
  1431. do {
  1432. if (start > vma->vm_start) {
  1433. kleave(" = -EINVAL [miss]");
  1434. return -EINVAL;
  1435. }
  1436. if (end == vma->vm_end)
  1437. goto erase_whole_vma;
  1438. vma = vma->vm_next;
  1439. } while (vma);
  1440. kleave(" = -EINVAL [split file]");
  1441. return -EINVAL;
  1442. } else {
  1443. /* the chunk must be a subset of the VMA found */
  1444. if (start == vma->vm_start && end == vma->vm_end)
  1445. goto erase_whole_vma;
  1446. if (start < vma->vm_start || end > vma->vm_end) {
  1447. kleave(" = -EINVAL [superset]");
  1448. return -EINVAL;
  1449. }
  1450. if (start & ~PAGE_MASK) {
  1451. kleave(" = -EINVAL [unaligned start]");
  1452. return -EINVAL;
  1453. }
  1454. if (end != vma->vm_end && end & ~PAGE_MASK) {
  1455. kleave(" = -EINVAL [unaligned split]");
  1456. return -EINVAL;
  1457. }
  1458. if (start != vma->vm_start && end != vma->vm_end) {
  1459. ret = split_vma(mm, vma, start, 1);
  1460. if (ret < 0) {
  1461. kleave(" = %d [split]", ret);
  1462. return ret;
  1463. }
  1464. }
  1465. return shrink_vma(mm, vma, start, end);
  1466. }
  1467. erase_whole_vma:
  1468. delete_vma_from_mm(vma);
  1469. delete_vma(mm, vma);
  1470. kleave(" = 0");
  1471. return 0;
  1472. }
  1473. EXPORT_SYMBOL(do_munmap);
  1474. int vm_munmap(unsigned long addr, size_t len)
  1475. {
  1476. struct mm_struct *mm = current->mm;
  1477. int ret;
  1478. down_write(&mm->mmap_sem);
  1479. ret = do_munmap(mm, addr, len);
  1480. up_write(&mm->mmap_sem);
  1481. return ret;
  1482. }
  1483. EXPORT_SYMBOL(vm_munmap);
  1484. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1485. {
  1486. return vm_munmap(addr, len);
  1487. }
  1488. /*
  1489. * release all the mappings made in a process's VM space
  1490. */
  1491. void exit_mmap(struct mm_struct *mm)
  1492. {
  1493. struct vm_area_struct *vma;
  1494. if (!mm)
  1495. return;
  1496. kenter("");
  1497. mm->total_vm = 0;
  1498. while ((vma = mm->mmap)) {
  1499. mm->mmap = vma->vm_next;
  1500. delete_vma_from_mm(vma);
  1501. delete_vma(mm, vma);
  1502. cond_resched();
  1503. }
  1504. kleave("");
  1505. }
  1506. unsigned long vm_brk(unsigned long addr, unsigned long len)
  1507. {
  1508. return -ENOMEM;
  1509. }
  1510. /*
  1511. * expand (or shrink) an existing mapping, potentially moving it at the same
  1512. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  1513. *
  1514. * under NOMMU conditions, we only permit changing a mapping's size, and only
  1515. * as long as it stays within the region allocated by do_mmap_private() and the
  1516. * block is not shareable
  1517. *
  1518. * MREMAP_FIXED is not supported under NOMMU conditions
  1519. */
  1520. static unsigned long do_mremap(unsigned long addr,
  1521. unsigned long old_len, unsigned long new_len,
  1522. unsigned long flags, unsigned long new_addr)
  1523. {
  1524. struct vm_area_struct *vma;
  1525. /* insanity checks first */
  1526. old_len = PAGE_ALIGN(old_len);
  1527. new_len = PAGE_ALIGN(new_len);
  1528. if (old_len == 0 || new_len == 0)
  1529. return (unsigned long) -EINVAL;
  1530. if (addr & ~PAGE_MASK)
  1531. return -EINVAL;
  1532. if (flags & MREMAP_FIXED && new_addr != addr)
  1533. return (unsigned long) -EINVAL;
  1534. vma = find_vma_exact(current->mm, addr, old_len);
  1535. if (!vma)
  1536. return (unsigned long) -EINVAL;
  1537. if (vma->vm_end != vma->vm_start + old_len)
  1538. return (unsigned long) -EFAULT;
  1539. if (vma->vm_flags & VM_MAYSHARE)
  1540. return (unsigned long) -EPERM;
  1541. if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
  1542. return (unsigned long) -ENOMEM;
  1543. /* all checks complete - do it */
  1544. vma->vm_end = vma->vm_start + new_len;
  1545. return vma->vm_start;
  1546. }
  1547. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  1548. unsigned long, new_len, unsigned long, flags,
  1549. unsigned long, new_addr)
  1550. {
  1551. unsigned long ret;
  1552. down_write(&current->mm->mmap_sem);
  1553. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1554. up_write(&current->mm->mmap_sem);
  1555. return ret;
  1556. }
  1557. struct page *follow_page_mask(struct vm_area_struct *vma,
  1558. unsigned long address, unsigned int flags,
  1559. unsigned int *page_mask)
  1560. {
  1561. *page_mask = 0;
  1562. return NULL;
  1563. }
  1564. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1565. unsigned long pfn, unsigned long size, pgprot_t prot)
  1566. {
  1567. if (addr != (pfn << PAGE_SHIFT))
  1568. return -EINVAL;
  1569. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  1570. return 0;
  1571. }
  1572. EXPORT_SYMBOL(remap_pfn_range);
  1573. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
  1574. {
  1575. unsigned long pfn = start >> PAGE_SHIFT;
  1576. unsigned long vm_len = vma->vm_end - vma->vm_start;
  1577. pfn += vma->vm_pgoff;
  1578. return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
  1579. }
  1580. EXPORT_SYMBOL(vm_iomap_memory);
  1581. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1582. unsigned long pgoff)
  1583. {
  1584. unsigned int size = vma->vm_end - vma->vm_start;
  1585. if (!(vma->vm_flags & VM_USERMAP))
  1586. return -EINVAL;
  1587. vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
  1588. vma->vm_end = vma->vm_start + size;
  1589. return 0;
  1590. }
  1591. EXPORT_SYMBOL(remap_vmalloc_range);
  1592. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  1593. unsigned long len, unsigned long pgoff, unsigned long flags)
  1594. {
  1595. return -ENOMEM;
  1596. }
  1597. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1598. {
  1599. }
  1600. void unmap_mapping_range(struct address_space *mapping,
  1601. loff_t const holebegin, loff_t const holelen,
  1602. int even_cows)
  1603. {
  1604. }
  1605. EXPORT_SYMBOL(unmap_mapping_range);
  1606. /*
  1607. * Check that a process has enough memory to allocate a new virtual
  1608. * mapping. 0 means there is enough memory for the allocation to
  1609. * succeed and -ENOMEM implies there is not.
  1610. *
  1611. * We currently support three overcommit policies, which are set via the
  1612. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  1613. *
  1614. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  1615. * Additional code 2002 Jul 20 by Robert Love.
  1616. *
  1617. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1618. *
  1619. * Note this is a helper function intended to be used by LSMs which
  1620. * wish to use this logic.
  1621. */
  1622. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  1623. {
  1624. unsigned long free, allowed, reserve;
  1625. vm_acct_memory(pages);
  1626. /*
  1627. * Sometimes we want to use more memory than we have
  1628. */
  1629. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1630. return 0;
  1631. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1632. free = global_page_state(NR_FREE_PAGES);
  1633. free += global_page_state(NR_FILE_PAGES);
  1634. /*
  1635. * shmem pages shouldn't be counted as free in this
  1636. * case, they can't be purged, only swapped out, and
  1637. * that won't affect the overall amount of available
  1638. * memory in the system.
  1639. */
  1640. free -= global_page_state(NR_SHMEM);
  1641. free += get_nr_swap_pages();
  1642. /*
  1643. * Any slabs which are created with the
  1644. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1645. * which are reclaimable, under pressure. The dentry
  1646. * cache and most inode caches should fall into this
  1647. */
  1648. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1649. /*
  1650. * Leave reserved pages. The pages are not for anonymous pages.
  1651. */
  1652. if (free <= totalreserve_pages)
  1653. goto error;
  1654. else
  1655. free -= totalreserve_pages;
  1656. /*
  1657. * Reserve some for root
  1658. */
  1659. if (!cap_sys_admin)
  1660. free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
  1661. if (free > pages)
  1662. return 0;
  1663. goto error;
  1664. }
  1665. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1666. /*
  1667. * Reserve some 3% for root
  1668. */
  1669. if (!cap_sys_admin)
  1670. allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
  1671. allowed += total_swap_pages;
  1672. /*
  1673. * Don't let a single process grow so big a user can't recover
  1674. */
  1675. if (mm) {
  1676. reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
  1677. allowed -= min(mm->total_vm / 32, reserve);
  1678. }
  1679. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  1680. return 0;
  1681. error:
  1682. vm_unacct_memory(pages);
  1683. return -ENOMEM;
  1684. }
  1685. int in_gate_area_no_mm(unsigned long addr)
  1686. {
  1687. return 0;
  1688. }
  1689. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1690. {
  1691. BUG();
  1692. return 0;
  1693. }
  1694. EXPORT_SYMBOL(filemap_fault);
  1695. int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
  1696. unsigned long size, pgoff_t pgoff)
  1697. {
  1698. BUG();
  1699. return 0;
  1700. }
  1701. EXPORT_SYMBOL(generic_file_remap_pages);
  1702. static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  1703. unsigned long addr, void *buf, int len, int write)
  1704. {
  1705. struct vm_area_struct *vma;
  1706. down_read(&mm->mmap_sem);
  1707. /* the access must start within one of the target process's mappings */
  1708. vma = find_vma(mm, addr);
  1709. if (vma) {
  1710. /* don't overrun this mapping */
  1711. if (addr + len >= vma->vm_end)
  1712. len = vma->vm_end - addr;
  1713. /* only read or write mappings where it is permitted */
  1714. if (write && vma->vm_flags & VM_MAYWRITE)
  1715. copy_to_user_page(vma, NULL, addr,
  1716. (void *) addr, buf, len);
  1717. else if (!write && vma->vm_flags & VM_MAYREAD)
  1718. copy_from_user_page(vma, NULL, addr,
  1719. buf, (void *) addr, len);
  1720. else
  1721. len = 0;
  1722. } else {
  1723. len = 0;
  1724. }
  1725. up_read(&mm->mmap_sem);
  1726. return len;
  1727. }
  1728. /**
  1729. * @access_remote_vm - access another process' address space
  1730. * @mm: the mm_struct of the target address space
  1731. * @addr: start address to access
  1732. * @buf: source or destination buffer
  1733. * @len: number of bytes to transfer
  1734. * @write: whether the access is a write
  1735. *
  1736. * The caller must hold a reference on @mm.
  1737. */
  1738. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1739. void *buf, int len, int write)
  1740. {
  1741. return __access_remote_vm(NULL, mm, addr, buf, len, write);
  1742. }
  1743. /*
  1744. * Access another process' address space.
  1745. * - source/target buffer must be kernel space
  1746. */
  1747. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1748. {
  1749. struct mm_struct *mm;
  1750. if (addr + len < addr)
  1751. return 0;
  1752. mm = get_task_mm(tsk);
  1753. if (!mm)
  1754. return 0;
  1755. len = __access_remote_vm(tsk, mm, addr, buf, len, write);
  1756. mmput(mm);
  1757. return len;
  1758. }
  1759. /**
  1760. * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
  1761. * @inode: The inode to check
  1762. * @size: The current filesize of the inode
  1763. * @newsize: The proposed filesize of the inode
  1764. *
  1765. * Check the shared mappings on an inode on behalf of a shrinking truncate to
  1766. * make sure that that any outstanding VMAs aren't broken and then shrink the
  1767. * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
  1768. * automatically grant mappings that are too large.
  1769. */
  1770. int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
  1771. size_t newsize)
  1772. {
  1773. struct vm_area_struct *vma;
  1774. struct vm_region *region;
  1775. pgoff_t low, high;
  1776. size_t r_size, r_top;
  1777. low = newsize >> PAGE_SHIFT;
  1778. high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1779. down_write(&nommu_region_sem);
  1780. mutex_lock(&inode->i_mapping->i_mmap_mutex);
  1781. /* search for VMAs that fall within the dead zone */
  1782. vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
  1783. /* found one - only interested if it's shared out of the page
  1784. * cache */
  1785. if (vma->vm_flags & VM_SHARED) {
  1786. mutex_unlock(&inode->i_mapping->i_mmap_mutex);
  1787. up_write(&nommu_region_sem);
  1788. return -ETXTBSY; /* not quite true, but near enough */
  1789. }
  1790. }
  1791. /* reduce any regions that overlap the dead zone - if in existence,
  1792. * these will be pointed to by VMAs that don't overlap the dead zone
  1793. *
  1794. * we don't check for any regions that start beyond the EOF as there
  1795. * shouldn't be any
  1796. */
  1797. vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap,
  1798. 0, ULONG_MAX) {
  1799. if (!(vma->vm_flags & VM_SHARED))
  1800. continue;
  1801. region = vma->vm_region;
  1802. r_size = region->vm_top - region->vm_start;
  1803. r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
  1804. if (r_top > newsize) {
  1805. region->vm_top -= r_top - newsize;
  1806. if (region->vm_end > region->vm_top)
  1807. region->vm_end = region->vm_top;
  1808. }
  1809. }
  1810. mutex_unlock(&inode->i_mapping->i_mmap_mutex);
  1811. up_write(&nommu_region_sem);
  1812. return 0;
  1813. }
  1814. /*
  1815. * Initialise sysctl_user_reserve_kbytes.
  1816. *
  1817. * This is intended to prevent a user from starting a single memory hogging
  1818. * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
  1819. * mode.
  1820. *
  1821. * The default value is min(3% of free memory, 128MB)
  1822. * 128MB is enough to recover with sshd/login, bash, and top/kill.
  1823. */
  1824. static int __meminit init_user_reserve(void)
  1825. {
  1826. unsigned long free_kbytes;
  1827. free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  1828. sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
  1829. return 0;
  1830. }
  1831. module_init(init_user_reserve)
  1832. /*
  1833. * Initialise sysctl_admin_reserve_kbytes.
  1834. *
  1835. * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
  1836. * to log in and kill a memory hogging process.
  1837. *
  1838. * Systems with more than 256MB will reserve 8MB, enough to recover
  1839. * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
  1840. * only reserve 3% of free pages by default.
  1841. */
  1842. static int __meminit init_admin_reserve(void)
  1843. {
  1844. unsigned long free_kbytes;
  1845. free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
  1846. sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
  1847. return 0;
  1848. }
  1849. module_init(init_admin_reserve)