nommu.c 53 KB

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