nommu.c 48 KB

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