nommu.c 47 KB

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