nommu.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  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. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  788. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  789. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  790. ) {
  791. printk("MAP_SHARED not completely supported on !MMU\n");
  792. return -EINVAL;
  793. }
  794. /* we mustn't privatise shared mappings */
  795. capabilities &= ~BDI_CAP_MAP_COPY;
  796. }
  797. else {
  798. /* we're going to read the file into private memory we
  799. * allocate */
  800. if (!(capabilities & BDI_CAP_MAP_COPY))
  801. return -ENODEV;
  802. /* we don't permit a private writable mapping to be
  803. * shared with the backing device */
  804. if (prot & PROT_WRITE)
  805. capabilities &= ~BDI_CAP_MAP_DIRECT;
  806. }
  807. /* handle executable mappings and implied executable
  808. * mappings */
  809. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  810. if (prot & PROT_EXEC)
  811. return -EPERM;
  812. }
  813. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  814. /* handle implication of PROT_EXEC by PROT_READ */
  815. if (current->personality & READ_IMPLIES_EXEC) {
  816. if (capabilities & BDI_CAP_EXEC_MAP)
  817. prot |= PROT_EXEC;
  818. }
  819. }
  820. else if ((prot & PROT_READ) &&
  821. (prot & PROT_EXEC) &&
  822. !(capabilities & BDI_CAP_EXEC_MAP)
  823. ) {
  824. /* backing file is not executable, try to copy */
  825. capabilities &= ~BDI_CAP_MAP_DIRECT;
  826. }
  827. }
  828. else {
  829. /* anonymous mappings are always memory backed and can be
  830. * privately mapped
  831. */
  832. capabilities = BDI_CAP_MAP_COPY;
  833. /* handle PROT_EXEC implication by PROT_READ */
  834. if ((prot & PROT_READ) &&
  835. (current->personality & READ_IMPLIES_EXEC))
  836. prot |= PROT_EXEC;
  837. }
  838. /* allow the security API to have its say */
  839. ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  840. if (ret < 0)
  841. return ret;
  842. /* looks okay */
  843. *_capabilities = capabilities;
  844. return 0;
  845. }
  846. /*
  847. * we've determined that we can make the mapping, now translate what we
  848. * now know into VMA flags
  849. */
  850. static unsigned long determine_vm_flags(struct file *file,
  851. unsigned long prot,
  852. unsigned long flags,
  853. unsigned long capabilities)
  854. {
  855. unsigned long vm_flags;
  856. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  857. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  858. /* vm_flags |= mm->def_flags; */
  859. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  860. /* attempt to share read-only copies of mapped file chunks */
  861. if (file && !(prot & PROT_WRITE))
  862. vm_flags |= VM_MAYSHARE;
  863. }
  864. else {
  865. /* overlay a shareable mapping on the backing device or inode
  866. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  867. * romfs/cramfs */
  868. if (flags & MAP_SHARED)
  869. vm_flags |= VM_MAYSHARE | VM_SHARED;
  870. else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
  871. vm_flags |= VM_MAYSHARE;
  872. }
  873. /* refuse to let anyone share private mappings with this process if
  874. * it's being traced - otherwise breakpoints set in it may interfere
  875. * with another untraced process
  876. */
  877. if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
  878. vm_flags &= ~VM_MAYSHARE;
  879. return vm_flags;
  880. }
  881. /*
  882. * set up a shared mapping on a file (the driver or filesystem provides and
  883. * pins the storage)
  884. */
  885. static int do_mmap_shared_file(struct vm_area_struct *vma)
  886. {
  887. int ret;
  888. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  889. if (ret == 0) {
  890. vma->vm_region->vm_top = vma->vm_region->vm_end;
  891. return 0;
  892. }
  893. if (ret != -ENOSYS)
  894. return ret;
  895. /* getting -ENOSYS indicates that direct mmap isn't possible (as
  896. * opposed to tried but failed) so we can only give a suitable error as
  897. * it's not possible to make a private copy if MAP_SHARED was given */
  898. return -ENODEV;
  899. }
  900. /*
  901. * set up a private mapping or an anonymous shared mapping
  902. */
  903. static int do_mmap_private(struct vm_area_struct *vma,
  904. struct vm_region *region,
  905. unsigned long len,
  906. unsigned long capabilities)
  907. {
  908. struct page *pages;
  909. unsigned long total, point, n, rlen;
  910. void *base;
  911. int ret, order;
  912. /* invoke the file's mapping function so that it can keep track of
  913. * shared mappings on devices or memory
  914. * - VM_MAYSHARE will be set if it may attempt to share
  915. */
  916. if (capabilities & BDI_CAP_MAP_DIRECT) {
  917. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  918. if (ret == 0) {
  919. /* shouldn't return success if we're not sharing */
  920. BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
  921. vma->vm_region->vm_top = vma->vm_region->vm_end;
  922. return 0;
  923. }
  924. if (ret != -ENOSYS)
  925. return ret;
  926. /* getting an ENOSYS error indicates that direct mmap isn't
  927. * possible (as opposed to tried but failed) so we'll try to
  928. * make a private copy of the data and map that instead */
  929. }
  930. rlen = PAGE_ALIGN(len);
  931. /* allocate some memory to hold the mapping
  932. * - note that this may not return a page-aligned address if the object
  933. * we're allocating is smaller than a page
  934. */
  935. order = get_order(rlen);
  936. kdebug("alloc order %d for %lx", order, len);
  937. pages = alloc_pages(GFP_KERNEL, order);
  938. if (!pages)
  939. goto enomem;
  940. total = 1 << order;
  941. atomic_long_add(total, &mmap_pages_allocated);
  942. point = rlen >> PAGE_SHIFT;
  943. /* we allocated a power-of-2 sized page set, so we may want to trim off
  944. * the excess */
  945. if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
  946. while (total > point) {
  947. order = ilog2(total - point);
  948. n = 1 << order;
  949. kdebug("shave %lu/%lu @%lu", n, total - point, total);
  950. atomic_long_sub(n, &mmap_pages_allocated);
  951. total -= n;
  952. set_page_refcounted(pages + total);
  953. __free_pages(pages + total, order);
  954. }
  955. }
  956. for (point = 1; point < total; point++)
  957. set_page_refcounted(&pages[point]);
  958. base = page_address(pages);
  959. region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
  960. region->vm_start = (unsigned long) base;
  961. region->vm_end = region->vm_start + rlen;
  962. region->vm_top = region->vm_start + (total << PAGE_SHIFT);
  963. vma->vm_start = region->vm_start;
  964. vma->vm_end = region->vm_start + len;
  965. if (vma->vm_file) {
  966. /* read the contents of a file into the copy */
  967. mm_segment_t old_fs;
  968. loff_t fpos;
  969. fpos = vma->vm_pgoff;
  970. fpos <<= PAGE_SHIFT;
  971. old_fs = get_fs();
  972. set_fs(KERNEL_DS);
  973. ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
  974. set_fs(old_fs);
  975. if (ret < 0)
  976. goto error_free;
  977. /* clear the last little bit */
  978. if (ret < rlen)
  979. memset(base + ret, 0, rlen - ret);
  980. }
  981. return 0;
  982. error_free:
  983. free_page_series(region->vm_start, region->vm_end);
  984. region->vm_start = vma->vm_start = 0;
  985. region->vm_end = vma->vm_end = 0;
  986. region->vm_top = 0;
  987. return ret;
  988. enomem:
  989. printk("Allocation of length %lu from process %d (%s) failed\n",
  990. len, current->pid, current->comm);
  991. show_free_areas();
  992. return -ENOMEM;
  993. }
  994. /*
  995. * handle mapping creation for uClinux
  996. */
  997. unsigned long do_mmap_pgoff(struct file *file,
  998. unsigned long addr,
  999. unsigned long len,
  1000. unsigned long prot,
  1001. unsigned long flags,
  1002. unsigned long pgoff)
  1003. {
  1004. struct vm_area_struct *vma;
  1005. struct vm_region *region;
  1006. struct rb_node *rb;
  1007. unsigned long capabilities, vm_flags, result;
  1008. int ret;
  1009. kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
  1010. /* decide whether we should attempt the mapping, and if so what sort of
  1011. * mapping */
  1012. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  1013. &capabilities);
  1014. if (ret < 0) {
  1015. kleave(" = %d [val]", ret);
  1016. return ret;
  1017. }
  1018. /* we ignore the address hint */
  1019. addr = 0;
  1020. /* we've determined that we can make the mapping, now translate what we
  1021. * now know into VMA flags */
  1022. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  1023. /* we're going to need to record the mapping */
  1024. region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
  1025. if (!region)
  1026. goto error_getting_region;
  1027. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1028. if (!vma)
  1029. goto error_getting_vma;
  1030. region->vm_usage = 1;
  1031. region->vm_flags = vm_flags;
  1032. region->vm_pgoff = pgoff;
  1033. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1034. vma->vm_flags = vm_flags;
  1035. vma->vm_pgoff = pgoff;
  1036. if (file) {
  1037. region->vm_file = file;
  1038. get_file(file);
  1039. vma->vm_file = file;
  1040. get_file(file);
  1041. if (vm_flags & VM_EXECUTABLE) {
  1042. added_exe_file_vma(current->mm);
  1043. vma->vm_mm = current->mm;
  1044. }
  1045. }
  1046. down_write(&nommu_region_sem);
  1047. /* if we want to share, we need to check for regions created by other
  1048. * mmap() calls that overlap with our proposed mapping
  1049. * - we can only share with a superset match on most regular files
  1050. * - shared mappings on character devices and memory backed files are
  1051. * permitted to overlap inexactly as far as we are concerned for in
  1052. * these cases, sharing is handled in the driver or filesystem rather
  1053. * than here
  1054. */
  1055. if (vm_flags & VM_MAYSHARE) {
  1056. struct vm_region *pregion;
  1057. unsigned long pglen, rpglen, pgend, rpgend, start;
  1058. pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1059. pgend = pgoff + pglen;
  1060. for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
  1061. pregion = rb_entry(rb, struct vm_region, vm_rb);
  1062. if (!(pregion->vm_flags & VM_MAYSHARE))
  1063. continue;
  1064. /* search for overlapping mappings on the same file */
  1065. if (pregion->vm_file->f_path.dentry->d_inode !=
  1066. file->f_path.dentry->d_inode)
  1067. continue;
  1068. if (pregion->vm_pgoff >= pgend)
  1069. continue;
  1070. rpglen = pregion->vm_end - pregion->vm_start;
  1071. rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1072. rpgend = pregion->vm_pgoff + rpglen;
  1073. if (pgoff >= rpgend)
  1074. continue;
  1075. /* handle inexactly overlapping matches between
  1076. * mappings */
  1077. if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
  1078. !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
  1079. /* new mapping is not a subset of the region */
  1080. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  1081. goto sharing_violation;
  1082. continue;
  1083. }
  1084. /* we've found a region we can share */
  1085. pregion->vm_usage++;
  1086. vma->vm_region = pregion;
  1087. start = pregion->vm_start;
  1088. start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
  1089. vma->vm_start = start;
  1090. vma->vm_end = start + len;
  1091. if (pregion->vm_flags & VM_MAPPED_COPY) {
  1092. kdebug("share copy");
  1093. vma->vm_flags |= VM_MAPPED_COPY;
  1094. } else {
  1095. kdebug("share mmap");
  1096. ret = do_mmap_shared_file(vma);
  1097. if (ret < 0) {
  1098. vma->vm_region = NULL;
  1099. vma->vm_start = 0;
  1100. vma->vm_end = 0;
  1101. pregion->vm_usage--;
  1102. pregion = NULL;
  1103. goto error_just_free;
  1104. }
  1105. }
  1106. fput(region->vm_file);
  1107. kmem_cache_free(vm_region_jar, region);
  1108. region = pregion;
  1109. result = start;
  1110. goto share;
  1111. }
  1112. /* obtain the address at which to make a shared mapping
  1113. * - this is the hook for quasi-memory character devices to
  1114. * tell us the location of a shared mapping
  1115. */
  1116. if (capabilities & BDI_CAP_MAP_DIRECT) {
  1117. addr = file->f_op->get_unmapped_area(file, addr, len,
  1118. pgoff, flags);
  1119. if (IS_ERR((void *) addr)) {
  1120. ret = addr;
  1121. if (ret != (unsigned long) -ENOSYS)
  1122. goto error_just_free;
  1123. /* the driver refused to tell us where to site
  1124. * the mapping so we'll have to attempt to copy
  1125. * it */
  1126. ret = (unsigned long) -ENODEV;
  1127. if (!(capabilities & BDI_CAP_MAP_COPY))
  1128. goto error_just_free;
  1129. capabilities &= ~BDI_CAP_MAP_DIRECT;
  1130. } else {
  1131. vma->vm_start = region->vm_start = addr;
  1132. vma->vm_end = region->vm_end = addr + len;
  1133. }
  1134. }
  1135. }
  1136. vma->vm_region = region;
  1137. /* set up the mapping
  1138. * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
  1139. */
  1140. if (file && vma->vm_flags & VM_SHARED)
  1141. ret = do_mmap_shared_file(vma);
  1142. else
  1143. ret = do_mmap_private(vma, region, len, capabilities);
  1144. if (ret < 0)
  1145. goto error_just_free;
  1146. add_nommu_region(region);
  1147. /* clear anonymous mappings that don't ask for uninitialized data */
  1148. if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
  1149. memset((void *)region->vm_start, 0,
  1150. region->vm_end - region->vm_start);
  1151. /* okay... we have a mapping; now we have to register it */
  1152. result = vma->vm_start;
  1153. current->mm->total_vm += len >> PAGE_SHIFT;
  1154. share:
  1155. add_vma_to_mm(current->mm, vma);
  1156. /* we flush the region from the icache only when the first executable
  1157. * mapping of it is made */
  1158. if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
  1159. flush_icache_range(region->vm_start, region->vm_end);
  1160. region->vm_icache_flushed = true;
  1161. }
  1162. up_write(&nommu_region_sem);
  1163. kleave(" = %lx", result);
  1164. return result;
  1165. error_just_free:
  1166. up_write(&nommu_region_sem);
  1167. error:
  1168. if (region->vm_file)
  1169. fput(region->vm_file);
  1170. kmem_cache_free(vm_region_jar, region);
  1171. if (vma->vm_file)
  1172. fput(vma->vm_file);
  1173. if (vma->vm_flags & VM_EXECUTABLE)
  1174. removed_exe_file_vma(vma->vm_mm);
  1175. kmem_cache_free(vm_area_cachep, vma);
  1176. kleave(" = %d", ret);
  1177. return ret;
  1178. sharing_violation:
  1179. up_write(&nommu_region_sem);
  1180. printk(KERN_WARNING "Attempt to share mismatched mappings\n");
  1181. ret = -EINVAL;
  1182. goto error;
  1183. error_getting_vma:
  1184. kmem_cache_free(vm_region_jar, region);
  1185. printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
  1186. " from process %d failed\n",
  1187. len, current->pid);
  1188. show_free_areas();
  1189. return -ENOMEM;
  1190. error_getting_region:
  1191. printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
  1192. " from process %d failed\n",
  1193. len, current->pid);
  1194. show_free_areas();
  1195. return -ENOMEM;
  1196. }
  1197. EXPORT_SYMBOL(do_mmap_pgoff);
  1198. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1199. unsigned long, prot, unsigned long, flags,
  1200. unsigned long, fd, unsigned long, pgoff)
  1201. {
  1202. struct file *file = NULL;
  1203. unsigned long retval = -EBADF;
  1204. if (!(flags & MAP_ANONYMOUS)) {
  1205. file = fget(fd);
  1206. if (!file)
  1207. goto out;
  1208. }
  1209. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1210. down_write(&current->mm->mmap_sem);
  1211. retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1212. up_write(&current->mm->mmap_sem);
  1213. if (file)
  1214. fput(file);
  1215. out:
  1216. return retval;
  1217. }
  1218. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1219. struct mmap_arg_struct {
  1220. unsigned long addr;
  1221. unsigned long len;
  1222. unsigned long prot;
  1223. unsigned long flags;
  1224. unsigned long fd;
  1225. unsigned long offset;
  1226. };
  1227. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1228. {
  1229. struct mmap_arg_struct a;
  1230. if (copy_from_user(&a, arg, sizeof(a)))
  1231. return -EFAULT;
  1232. if (a.offset & ~PAGE_MASK)
  1233. return -EINVAL;
  1234. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1235. a.offset >> PAGE_SHIFT);
  1236. }
  1237. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1238. /*
  1239. * split a vma into two pieces at address 'addr', a new vma is allocated either
  1240. * for the first part or the tail.
  1241. */
  1242. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1243. unsigned long addr, int new_below)
  1244. {
  1245. struct vm_area_struct *new;
  1246. struct vm_region *region;
  1247. unsigned long npages;
  1248. kenter("");
  1249. /* we're only permitted to split anonymous regions (these should have
  1250. * only a single usage on the region) */
  1251. if (vma->vm_file)
  1252. return -ENOMEM;
  1253. if (mm->map_count >= sysctl_max_map_count)
  1254. return -ENOMEM;
  1255. region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
  1256. if (!region)
  1257. return -ENOMEM;
  1258. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1259. if (!new) {
  1260. kmem_cache_free(vm_region_jar, region);
  1261. return -ENOMEM;
  1262. }
  1263. /* most fields are the same, copy all, and then fixup */
  1264. *new = *vma;
  1265. *region = *vma->vm_region;
  1266. new->vm_region = region;
  1267. npages = (addr - vma->vm_start) >> PAGE_SHIFT;
  1268. if (new_below) {
  1269. region->vm_top = region->vm_end = new->vm_end = addr;
  1270. } else {
  1271. region->vm_start = new->vm_start = addr;
  1272. region->vm_pgoff = new->vm_pgoff += npages;
  1273. }
  1274. if (new->vm_ops && new->vm_ops->open)
  1275. new->vm_ops->open(new);
  1276. delete_vma_from_mm(vma);
  1277. down_write(&nommu_region_sem);
  1278. delete_nommu_region(vma->vm_region);
  1279. if (new_below) {
  1280. vma->vm_region->vm_start = vma->vm_start = addr;
  1281. vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
  1282. } else {
  1283. vma->vm_region->vm_end = vma->vm_end = addr;
  1284. vma->vm_region->vm_top = addr;
  1285. }
  1286. add_nommu_region(vma->vm_region);
  1287. add_nommu_region(new->vm_region);
  1288. up_write(&nommu_region_sem);
  1289. add_vma_to_mm(mm, vma);
  1290. add_vma_to_mm(mm, new);
  1291. return 0;
  1292. }
  1293. /*
  1294. * shrink a VMA by removing the specified chunk from either the beginning or
  1295. * the end
  1296. */
  1297. static int shrink_vma(struct mm_struct *mm,
  1298. struct vm_area_struct *vma,
  1299. unsigned long from, unsigned long to)
  1300. {
  1301. struct vm_region *region;
  1302. kenter("");
  1303. /* adjust the VMA's pointers, which may reposition it in the MM's tree
  1304. * and list */
  1305. delete_vma_from_mm(vma);
  1306. if (from > vma->vm_start)
  1307. vma->vm_end = from;
  1308. else
  1309. vma->vm_start = to;
  1310. add_vma_to_mm(mm, vma);
  1311. /* cut the backing region down to size */
  1312. region = vma->vm_region;
  1313. BUG_ON(region->vm_usage != 1);
  1314. down_write(&nommu_region_sem);
  1315. delete_nommu_region(region);
  1316. if (from > region->vm_start) {
  1317. to = region->vm_top;
  1318. region->vm_top = region->vm_end = from;
  1319. } else {
  1320. region->vm_start = to;
  1321. }
  1322. add_nommu_region(region);
  1323. up_write(&nommu_region_sem);
  1324. free_page_series(from, to);
  1325. return 0;
  1326. }
  1327. /*
  1328. * release a mapping
  1329. * - under NOMMU conditions the chunk to be unmapped must be backed by a single
  1330. * VMA, though it need not cover the whole VMA
  1331. */
  1332. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1333. {
  1334. struct vm_area_struct *vma;
  1335. struct rb_node *rb;
  1336. unsigned long end = start + len;
  1337. int ret;
  1338. kenter(",%lx,%zx", start, len);
  1339. if (len == 0)
  1340. return -EINVAL;
  1341. /* find the first potentially overlapping VMA */
  1342. vma = find_vma(mm, start);
  1343. if (!vma) {
  1344. static int limit = 0;
  1345. if (limit < 5) {
  1346. printk(KERN_WARNING
  1347. "munmap of memory not mmapped by process %d"
  1348. " (%s): 0x%lx-0x%lx\n",
  1349. current->pid, current->comm,
  1350. start, start + len - 1);
  1351. limit++;
  1352. }
  1353. return -EINVAL;
  1354. }
  1355. /* we're allowed to split an anonymous VMA but not a file-backed one */
  1356. if (vma->vm_file) {
  1357. do {
  1358. if (start > vma->vm_start) {
  1359. kleave(" = -EINVAL [miss]");
  1360. return -EINVAL;
  1361. }
  1362. if (end == vma->vm_end)
  1363. goto erase_whole_vma;
  1364. rb = rb_next(&vma->vm_rb);
  1365. vma = rb_entry(rb, struct vm_area_struct, vm_rb);
  1366. } while (rb);
  1367. kleave(" = -EINVAL [split file]");
  1368. return -EINVAL;
  1369. } else {
  1370. /* the chunk must be a subset of the VMA found */
  1371. if (start == vma->vm_start && end == vma->vm_end)
  1372. goto erase_whole_vma;
  1373. if (start < vma->vm_start || end > vma->vm_end) {
  1374. kleave(" = -EINVAL [superset]");
  1375. return -EINVAL;
  1376. }
  1377. if (start & ~PAGE_MASK) {
  1378. kleave(" = -EINVAL [unaligned start]");
  1379. return -EINVAL;
  1380. }
  1381. if (end != vma->vm_end && end & ~PAGE_MASK) {
  1382. kleave(" = -EINVAL [unaligned split]");
  1383. return -EINVAL;
  1384. }
  1385. if (start != vma->vm_start && end != vma->vm_end) {
  1386. ret = split_vma(mm, vma, start, 1);
  1387. if (ret < 0) {
  1388. kleave(" = %d [split]", ret);
  1389. return ret;
  1390. }
  1391. }
  1392. return shrink_vma(mm, vma, start, end);
  1393. }
  1394. erase_whole_vma:
  1395. delete_vma_from_mm(vma);
  1396. delete_vma(mm, vma);
  1397. kleave(" = 0");
  1398. return 0;
  1399. }
  1400. EXPORT_SYMBOL(do_munmap);
  1401. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1402. {
  1403. int ret;
  1404. struct mm_struct *mm = current->mm;
  1405. down_write(&mm->mmap_sem);
  1406. ret = do_munmap(mm, addr, len);
  1407. up_write(&mm->mmap_sem);
  1408. return ret;
  1409. }
  1410. /*
  1411. * release all the mappings made in a process's VM space
  1412. */
  1413. void exit_mmap(struct mm_struct *mm)
  1414. {
  1415. struct vm_area_struct *vma;
  1416. if (!mm)
  1417. return;
  1418. kenter("");
  1419. mm->total_vm = 0;
  1420. while ((vma = mm->mmap)) {
  1421. mm->mmap = vma->vm_next;
  1422. delete_vma_from_mm(vma);
  1423. delete_vma(mm, vma);
  1424. }
  1425. kleave("");
  1426. }
  1427. unsigned long do_brk(unsigned long addr, unsigned long len)
  1428. {
  1429. return -ENOMEM;
  1430. }
  1431. /*
  1432. * expand (or shrink) an existing mapping, potentially moving it at the same
  1433. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  1434. *
  1435. * under NOMMU conditions, we only permit changing a mapping's size, and only
  1436. * as long as it stays within the region allocated by do_mmap_private() and the
  1437. * block is not shareable
  1438. *
  1439. * MREMAP_FIXED is not supported under NOMMU conditions
  1440. */
  1441. unsigned long do_mremap(unsigned long addr,
  1442. unsigned long old_len, unsigned long new_len,
  1443. unsigned long flags, unsigned long new_addr)
  1444. {
  1445. struct vm_area_struct *vma;
  1446. /* insanity checks first */
  1447. if (old_len == 0 || new_len == 0)
  1448. return (unsigned long) -EINVAL;
  1449. if (addr & ~PAGE_MASK)
  1450. return -EINVAL;
  1451. if (flags & MREMAP_FIXED && new_addr != addr)
  1452. return (unsigned long) -EINVAL;
  1453. vma = find_vma_exact(current->mm, addr, old_len);
  1454. if (!vma)
  1455. return (unsigned long) -EINVAL;
  1456. if (vma->vm_end != vma->vm_start + old_len)
  1457. return (unsigned long) -EFAULT;
  1458. if (vma->vm_flags & VM_MAYSHARE)
  1459. return (unsigned long) -EPERM;
  1460. if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
  1461. return (unsigned long) -ENOMEM;
  1462. /* all checks complete - do it */
  1463. vma->vm_end = vma->vm_start + new_len;
  1464. return vma->vm_start;
  1465. }
  1466. EXPORT_SYMBOL(do_mremap);
  1467. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  1468. unsigned long, new_len, unsigned long, flags,
  1469. unsigned long, new_addr)
  1470. {
  1471. unsigned long ret;
  1472. down_write(&current->mm->mmap_sem);
  1473. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1474. up_write(&current->mm->mmap_sem);
  1475. return ret;
  1476. }
  1477. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  1478. unsigned int foll_flags)
  1479. {
  1480. return NULL;
  1481. }
  1482. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  1483. unsigned long to, unsigned long size, pgprot_t prot)
  1484. {
  1485. vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
  1486. return 0;
  1487. }
  1488. EXPORT_SYMBOL(remap_pfn_range);
  1489. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1490. unsigned long pgoff)
  1491. {
  1492. unsigned int size = vma->vm_end - vma->vm_start;
  1493. if (!(vma->vm_flags & VM_USERMAP))
  1494. return -EINVAL;
  1495. vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
  1496. vma->vm_end = vma->vm_start + size;
  1497. return 0;
  1498. }
  1499. EXPORT_SYMBOL(remap_vmalloc_range);
  1500. void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1501. {
  1502. }
  1503. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  1504. unsigned long len, unsigned long pgoff, unsigned long flags)
  1505. {
  1506. return -ENOMEM;
  1507. }
  1508. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1509. {
  1510. }
  1511. void unmap_mapping_range(struct address_space *mapping,
  1512. loff_t const holebegin, loff_t const holelen,
  1513. int even_cows)
  1514. {
  1515. }
  1516. EXPORT_SYMBOL(unmap_mapping_range);
  1517. /*
  1518. * Check that a process has enough memory to allocate a new virtual
  1519. * mapping. 0 means there is enough memory for the allocation to
  1520. * succeed and -ENOMEM implies there is not.
  1521. *
  1522. * We currently support three overcommit policies, which are set via the
  1523. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  1524. *
  1525. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  1526. * Additional code 2002 Jul 20 by Robert Love.
  1527. *
  1528. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1529. *
  1530. * Note this is a helper function intended to be used by LSMs which
  1531. * wish to use this logic.
  1532. */
  1533. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  1534. {
  1535. unsigned long free, allowed;
  1536. vm_acct_memory(pages);
  1537. /*
  1538. * Sometimes we want to use more memory than we have
  1539. */
  1540. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1541. return 0;
  1542. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1543. unsigned long n;
  1544. free = global_page_state(NR_FILE_PAGES);
  1545. free += nr_swap_pages;
  1546. /*
  1547. * Any slabs which are created with the
  1548. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1549. * which are reclaimable, under pressure. The dentry
  1550. * cache and most inode caches should fall into this
  1551. */
  1552. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1553. /*
  1554. * Leave the last 3% for root
  1555. */
  1556. if (!cap_sys_admin)
  1557. free -= free / 32;
  1558. if (free > pages)
  1559. return 0;
  1560. /*
  1561. * nr_free_pages() is very expensive on large systems,
  1562. * only call if we're about to fail.
  1563. */
  1564. n = nr_free_pages();
  1565. /*
  1566. * Leave reserved pages. The pages are not for anonymous pages.
  1567. */
  1568. if (n <= totalreserve_pages)
  1569. goto error;
  1570. else
  1571. n -= totalreserve_pages;
  1572. /*
  1573. * Leave the last 3% for root
  1574. */
  1575. if (!cap_sys_admin)
  1576. n -= n / 32;
  1577. free += n;
  1578. if (free > pages)
  1579. return 0;
  1580. goto error;
  1581. }
  1582. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1583. /*
  1584. * Leave the last 3% for root
  1585. */
  1586. if (!cap_sys_admin)
  1587. allowed -= allowed / 32;
  1588. allowed += total_swap_pages;
  1589. /* Don't let a single process grow too big:
  1590. leave 3% of the size of this process for other processes */
  1591. if (mm)
  1592. allowed -= mm->total_vm / 32;
  1593. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  1594. return 0;
  1595. error:
  1596. vm_unacct_memory(pages);
  1597. return -ENOMEM;
  1598. }
  1599. int in_gate_area_no_task(unsigned long addr)
  1600. {
  1601. return 0;
  1602. }
  1603. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1604. {
  1605. BUG();
  1606. return 0;
  1607. }
  1608. EXPORT_SYMBOL(filemap_fault);
  1609. /*
  1610. * Access another process' address space.
  1611. * - source/target buffer must be kernel space
  1612. */
  1613. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1614. {
  1615. struct vm_area_struct *vma;
  1616. struct mm_struct *mm;
  1617. if (addr + len < addr)
  1618. return 0;
  1619. mm = get_task_mm(tsk);
  1620. if (!mm)
  1621. return 0;
  1622. down_read(&mm->mmap_sem);
  1623. /* the access must start within one of the target process's mappings */
  1624. vma = find_vma(mm, addr);
  1625. if (vma) {
  1626. /* don't overrun this mapping */
  1627. if (addr + len >= vma->vm_end)
  1628. len = vma->vm_end - addr;
  1629. /* only read or write mappings where it is permitted */
  1630. if (write && vma->vm_flags & VM_MAYWRITE)
  1631. copy_to_user_page(vma, NULL, addr,
  1632. (void *) addr, buf, len);
  1633. else if (!write && vma->vm_flags & VM_MAYREAD)
  1634. copy_from_user_page(vma, NULL, addr,
  1635. buf, (void *) addr, len);
  1636. else
  1637. len = 0;
  1638. } else {
  1639. len = 0;
  1640. }
  1641. up_read(&mm->mmap_sem);
  1642. mmput(mm);
  1643. return len;
  1644. }
  1645. /**
  1646. * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
  1647. * @inode: The inode to check
  1648. * @size: The current filesize of the inode
  1649. * @newsize: The proposed filesize of the inode
  1650. *
  1651. * Check the shared mappings on an inode on behalf of a shrinking truncate to
  1652. * make sure that that any outstanding VMAs aren't broken and then shrink the
  1653. * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
  1654. * automatically grant mappings that are too large.
  1655. */
  1656. int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
  1657. size_t newsize)
  1658. {
  1659. struct vm_area_struct *vma;
  1660. struct prio_tree_iter iter;
  1661. struct vm_region *region;
  1662. pgoff_t low, high;
  1663. size_t r_size, r_top;
  1664. low = newsize >> PAGE_SHIFT;
  1665. high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1666. down_write(&nommu_region_sem);
  1667. /* search for VMAs that fall within the dead zone */
  1668. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  1669. low, high) {
  1670. /* found one - only interested if it's shared out of the page
  1671. * cache */
  1672. if (vma->vm_flags & VM_SHARED) {
  1673. up_write(&nommu_region_sem);
  1674. return -ETXTBSY; /* not quite true, but near enough */
  1675. }
  1676. }
  1677. /* reduce any regions that overlap the dead zone - if in existence,
  1678. * these will be pointed to by VMAs that don't overlap the dead zone
  1679. *
  1680. * we don't check for any regions that start beyond the EOF as there
  1681. * shouldn't be any
  1682. */
  1683. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  1684. 0, ULONG_MAX) {
  1685. if (!(vma->vm_flags & VM_SHARED))
  1686. continue;
  1687. region = vma->vm_region;
  1688. r_size = region->vm_top - region->vm_start;
  1689. r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
  1690. if (r_top > newsize) {
  1691. region->vm_top -= r_top - newsize;
  1692. if (region->vm_end > region->vm_top)
  1693. region->vm_end = region->vm_top;
  1694. }
  1695. }
  1696. up_write(&nommu_region_sem);
  1697. return 0;
  1698. }