nommu.c 50 KB

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