nommu.c 46 KB

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