nommu.c 46 KB

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