nommu.c 48 KB

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