nommu.c 52 KB

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