nommu.c 50 KB

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