nommu.c 46 KB

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