nommu.c 53 KB

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