nommu.c 51 KB

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