nommu.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * linux/mm/nommu.c
  3. *
  4. * Replacement code for mm functions to support CPU's that don't
  5. * have any form of memory management unit (thus no virtual memory).
  6. *
  7. * See Documentation/nommu-mmap.txt
  8. *
  9. * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
  10. * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
  11. * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
  12. * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
  13. * Copyright (c) 2007-2009 Paul Mundt <lethal@linux-sh.org>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/swap.h>
  19. #include <linux/file.h>
  20. #include <linux/highmem.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/mount.h>
  28. #include <linux/personality.h>
  29. #include <linux/security.h>
  30. #include <linux/syscalls.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/tlb.h>
  33. #include <asm/tlbflush.h>
  34. #include "internal.h"
  35. static inline __attribute__((format(printf, 1, 2)))
  36. void no_printk(const char *fmt, ...)
  37. {
  38. }
  39. #if 0
  40. #define kenter(FMT, ...) \
  41. printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  42. #define kleave(FMT, ...) \
  43. printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  44. #define kdebug(FMT, ...) \
  45. printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
  46. #else
  47. #define kenter(FMT, ...) \
  48. no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  49. #define kleave(FMT, ...) \
  50. no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  51. #define kdebug(FMT, ...) \
  52. no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
  53. #endif
  54. #include "internal.h"
  55. void *high_memory;
  56. struct page *mem_map;
  57. unsigned long max_mapnr;
  58. unsigned long num_physpages;
  59. struct percpu_counter vm_committed_as;
  60. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  61. int sysctl_overcommit_ratio = 50; /* default is 50% */
  62. int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
  63. int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
  64. int heap_stack_gap = 0;
  65. /* amount of vm to protect from userspace access */
  66. unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
  67. atomic_long_t mmap_pages_allocated;
  68. EXPORT_SYMBOL(mem_map);
  69. EXPORT_SYMBOL(num_physpages);
  70. /* list of mapped, potentially shareable regions */
  71. static struct kmem_cache *vm_region_jar;
  72. struct rb_root nommu_region_tree = RB_ROOT;
  73. DECLARE_RWSEM(nommu_region_sem);
  74. struct vm_operations_struct generic_file_vm_ops = {
  75. };
  76. /*
  77. * Handle all mappings that got truncated by a "truncate()"
  78. * system call.
  79. *
  80. * NOTE! We have to be ready to update the memory sharing
  81. * between the file and the memory map for a potential last
  82. * incomplete page. Ugly, but necessary.
  83. */
  84. int vmtruncate(struct inode *inode, loff_t offset)
  85. {
  86. struct address_space *mapping = inode->i_mapping;
  87. unsigned long limit;
  88. if (inode->i_size < offset)
  89. goto do_expand;
  90. i_size_write(inode, offset);
  91. truncate_inode_pages(mapping, offset);
  92. goto out_truncate;
  93. do_expand:
  94. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  95. if (limit != RLIM_INFINITY && offset > limit)
  96. goto out_sig;
  97. if (offset > inode->i_sb->s_maxbytes)
  98. goto out;
  99. i_size_write(inode, offset);
  100. out_truncate:
  101. if (inode->i_op->truncate)
  102. inode->i_op->truncate(inode);
  103. return 0;
  104. out_sig:
  105. send_sig(SIGXFSZ, current, 0);
  106. out:
  107. return -EFBIG;
  108. }
  109. EXPORT_SYMBOL(vmtruncate);
  110. /*
  111. * Return the total memory allocated for this pointer, not
  112. * just what the caller asked for.
  113. *
  114. * Doesn't have to be accurate, i.e. may have races.
  115. */
  116. unsigned int kobjsize(const void *objp)
  117. {
  118. struct page *page;
  119. /*
  120. * If the object we have should not have ksize performed on it,
  121. * return size of 0
  122. */
  123. if (!objp || !virt_addr_valid(objp))
  124. return 0;
  125. page = virt_to_head_page(objp);
  126. /*
  127. * If the allocator sets PageSlab, we know the pointer came from
  128. * kmalloc().
  129. */
  130. if (PageSlab(page))
  131. return ksize(objp);
  132. /*
  133. * If it's not a compound page, see if we have a matching VMA
  134. * region. This test is intentionally done in reverse order,
  135. * so if there's no VMA, we still fall through and hand back
  136. * PAGE_SIZE for 0-order pages.
  137. */
  138. if (!PageCompound(page)) {
  139. struct vm_area_struct *vma;
  140. vma = find_vma(current->mm, (unsigned long)objp);
  141. if (vma)
  142. return vma->vm_end - vma->vm_start;
  143. }
  144. /*
  145. * The ksize() function is only guaranteed to work for pointers
  146. * returned by kmalloc(). So handle arbitrary pointers here.
  147. */
  148. return PAGE_SIZE << compound_order(page);
  149. }
  150. int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  151. unsigned long start, int nr_pages, int flags,
  152. struct page **pages, struct vm_area_struct **vmas)
  153. {
  154. struct vm_area_struct *vma;
  155. unsigned long vm_flags;
  156. int i;
  157. int write = !!(flags & GUP_FLAGS_WRITE);
  158. int force = !!(flags & GUP_FLAGS_FORCE);
  159. int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
  160. /* calculate required read or write permissions.
  161. * - if 'force' is set, we only require the "MAY" flags.
  162. */
  163. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  164. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  165. for (i = 0; i < nr_pages; i++) {
  166. vma = find_vma(mm, start);
  167. if (!vma)
  168. goto finish_or_fault;
  169. /* protect what we can, including chardevs */
  170. if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
  171. (!ignore && !(vm_flags & vma->vm_flags)))
  172. goto finish_or_fault;
  173. if (pages) {
  174. pages[i] = virt_to_page(start);
  175. if (pages[i])
  176. page_cache_get(pages[i]);
  177. }
  178. if (vmas)
  179. vmas[i] = vma;
  180. start += PAGE_SIZE;
  181. }
  182. return i;
  183. finish_or_fault:
  184. return i ? : -EFAULT;
  185. }
  186. /*
  187. * get a list of pages in an address range belonging to the specified process
  188. * and indicate the VMA that covers each page
  189. * - this is potentially dodgy as we may end incrementing the page count of a
  190. * slab page or a secondary page from a compound page
  191. * - don't permit access to VMAs that don't support it, such as I/O mappings
  192. */
  193. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  194. unsigned long start, int nr_pages, int write, int force,
  195. struct page **pages, struct vm_area_struct **vmas)
  196. {
  197. int flags = 0;
  198. if (write)
  199. flags |= GUP_FLAGS_WRITE;
  200. if (force)
  201. flags |= GUP_FLAGS_FORCE;
  202. return __get_user_pages(tsk, mm, start, nr_pages, flags, 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. int ret;
  401. ret = percpu_counter_init(&vm_committed_as, 0);
  402. VM_BUG_ON(ret);
  403. vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
  404. }
  405. /*
  406. * validate the region tree
  407. * - the caller must hold the region lock
  408. */
  409. #ifdef CONFIG_DEBUG_NOMMU_REGIONS
  410. static noinline void validate_nommu_regions(void)
  411. {
  412. struct vm_region *region, *last;
  413. struct rb_node *p, *lastp;
  414. lastp = rb_first(&nommu_region_tree);
  415. if (!lastp)
  416. return;
  417. last = rb_entry(lastp, struct vm_region, vm_rb);
  418. BUG_ON(unlikely(last->vm_end <= last->vm_start));
  419. BUG_ON(unlikely(last->vm_top < last->vm_end));
  420. while ((p = rb_next(lastp))) {
  421. region = rb_entry(p, struct vm_region, vm_rb);
  422. last = rb_entry(lastp, struct vm_region, vm_rb);
  423. BUG_ON(unlikely(region->vm_end <= region->vm_start));
  424. BUG_ON(unlikely(region->vm_top < region->vm_end));
  425. BUG_ON(unlikely(region->vm_start < last->vm_top));
  426. lastp = p;
  427. }
  428. }
  429. #else
  430. static void validate_nommu_regions(void)
  431. {
  432. }
  433. #endif
  434. /*
  435. * add a region into the global tree
  436. */
  437. static void add_nommu_region(struct vm_region *region)
  438. {
  439. struct vm_region *pregion;
  440. struct rb_node **p, *parent;
  441. validate_nommu_regions();
  442. parent = NULL;
  443. p = &nommu_region_tree.rb_node;
  444. while (*p) {
  445. parent = *p;
  446. pregion = rb_entry(parent, struct vm_region, vm_rb);
  447. if (region->vm_start < pregion->vm_start)
  448. p = &(*p)->rb_left;
  449. else if (region->vm_start > pregion->vm_start)
  450. p = &(*p)->rb_right;
  451. else if (pregion == region)
  452. return;
  453. else
  454. BUG();
  455. }
  456. rb_link_node(&region->vm_rb, parent, p);
  457. rb_insert_color(&region->vm_rb, &nommu_region_tree);
  458. validate_nommu_regions();
  459. }
  460. /*
  461. * delete a region from the global tree
  462. */
  463. static void delete_nommu_region(struct vm_region *region)
  464. {
  465. BUG_ON(!nommu_region_tree.rb_node);
  466. validate_nommu_regions();
  467. rb_erase(&region->vm_rb, &nommu_region_tree);
  468. validate_nommu_regions();
  469. }
  470. /*
  471. * free a contiguous series of pages
  472. */
  473. static void free_page_series(unsigned long from, unsigned long to)
  474. {
  475. for (; from < to; from += PAGE_SIZE) {
  476. struct page *page = virt_to_page(from);
  477. kdebug("- free %lx", from);
  478. atomic_long_dec(&mmap_pages_allocated);
  479. if (page_count(page) != 1)
  480. kdebug("free page %p: refcount not one: %d",
  481. page, page_count(page));
  482. put_page(page);
  483. }
  484. }
  485. /*
  486. * release a reference to a region
  487. * - the caller must hold the region semaphore for writing, which this releases
  488. * - the region may not have been added to the tree yet, in which case vm_top
  489. * will equal vm_start
  490. */
  491. static void __put_nommu_region(struct vm_region *region)
  492. __releases(nommu_region_sem)
  493. {
  494. kenter("%p{%d}", region, atomic_read(&region->vm_usage));
  495. BUG_ON(!nommu_region_tree.rb_node);
  496. if (atomic_dec_and_test(&region->vm_usage)) {
  497. if (region->vm_top > region->vm_start)
  498. delete_nommu_region(region);
  499. up_write(&nommu_region_sem);
  500. if (region->vm_file)
  501. fput(region->vm_file);
  502. /* IO memory and memory shared directly out of the pagecache
  503. * from ramfs/tmpfs mustn't be released here */
  504. if (region->vm_flags & VM_MAPPED_COPY) {
  505. kdebug("free series");
  506. free_page_series(region->vm_start, region->vm_top);
  507. }
  508. kmem_cache_free(vm_region_jar, region);
  509. } else {
  510. up_write(&nommu_region_sem);
  511. }
  512. }
  513. /*
  514. * release a reference to a region
  515. */
  516. static void put_nommu_region(struct vm_region *region)
  517. {
  518. down_write(&nommu_region_sem);
  519. __put_nommu_region(region);
  520. }
  521. /*
  522. * add a VMA into a process's mm_struct in the appropriate place in the list
  523. * and tree and add to the address space's page tree also if not an anonymous
  524. * page
  525. * - should be called with mm->mmap_sem held writelocked
  526. */
  527. static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
  528. {
  529. struct vm_area_struct *pvma, **pp;
  530. struct address_space *mapping;
  531. struct rb_node **p, *parent;
  532. kenter(",%p", vma);
  533. BUG_ON(!vma->vm_region);
  534. mm->map_count++;
  535. vma->vm_mm = mm;
  536. /* add the VMA to the mapping */
  537. if (vma->vm_file) {
  538. mapping = vma->vm_file->f_mapping;
  539. flush_dcache_mmap_lock(mapping);
  540. vma_prio_tree_insert(vma, &mapping->i_mmap);
  541. flush_dcache_mmap_unlock(mapping);
  542. }
  543. /* add the VMA to the tree */
  544. parent = NULL;
  545. p = &mm->mm_rb.rb_node;
  546. while (*p) {
  547. parent = *p;
  548. pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
  549. /* sort by: start addr, end addr, VMA struct addr in that order
  550. * (the latter is necessary as we may get identical VMAs) */
  551. if (vma->vm_start < pvma->vm_start)
  552. p = &(*p)->rb_left;
  553. else if (vma->vm_start > pvma->vm_start)
  554. p = &(*p)->rb_right;
  555. else if (vma->vm_end < pvma->vm_end)
  556. p = &(*p)->rb_left;
  557. else if (vma->vm_end > pvma->vm_end)
  558. p = &(*p)->rb_right;
  559. else if (vma < pvma)
  560. p = &(*p)->rb_left;
  561. else if (vma > pvma)
  562. p = &(*p)->rb_right;
  563. else
  564. BUG();
  565. }
  566. rb_link_node(&vma->vm_rb, parent, p);
  567. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  568. /* add VMA to the VMA list also */
  569. for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
  570. if (pvma->vm_start > vma->vm_start)
  571. break;
  572. if (pvma->vm_start < vma->vm_start)
  573. continue;
  574. if (pvma->vm_end < vma->vm_end)
  575. break;
  576. }
  577. vma->vm_next = *pp;
  578. *pp = vma;
  579. }
  580. /*
  581. * delete a VMA from its owning mm_struct and address space
  582. */
  583. static void delete_vma_from_mm(struct vm_area_struct *vma)
  584. {
  585. struct vm_area_struct **pp;
  586. struct address_space *mapping;
  587. struct mm_struct *mm = vma->vm_mm;
  588. kenter("%p", vma);
  589. mm->map_count--;
  590. if (mm->mmap_cache == vma)
  591. mm->mmap_cache = NULL;
  592. /* remove the VMA from the mapping */
  593. if (vma->vm_file) {
  594. mapping = vma->vm_file->f_mapping;
  595. flush_dcache_mmap_lock(mapping);
  596. vma_prio_tree_remove(vma, &mapping->i_mmap);
  597. flush_dcache_mmap_unlock(mapping);
  598. }
  599. /* remove from the MM's tree and list */
  600. rb_erase(&vma->vm_rb, &mm->mm_rb);
  601. for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
  602. if (*pp == vma) {
  603. *pp = vma->vm_next;
  604. break;
  605. }
  606. }
  607. vma->vm_mm = NULL;
  608. }
  609. /*
  610. * destroy a VMA record
  611. */
  612. static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  613. {
  614. kenter("%p", vma);
  615. if (vma->vm_ops && vma->vm_ops->close)
  616. vma->vm_ops->close(vma);
  617. if (vma->vm_file) {
  618. fput(vma->vm_file);
  619. if (vma->vm_flags & VM_EXECUTABLE)
  620. removed_exe_file_vma(mm);
  621. }
  622. put_nommu_region(vma->vm_region);
  623. kmem_cache_free(vm_area_cachep, vma);
  624. }
  625. /*
  626. * look up the first VMA in which addr resides, NULL if none
  627. * - should be called with mm->mmap_sem at least held readlocked
  628. */
  629. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  630. {
  631. struct vm_area_struct *vma;
  632. struct rb_node *n = mm->mm_rb.rb_node;
  633. /* check the cache first */
  634. vma = mm->mmap_cache;
  635. if (vma && vma->vm_start <= addr && vma->vm_end > addr)
  636. return vma;
  637. /* trawl the tree (there may be multiple mappings in which addr
  638. * resides) */
  639. for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
  640. vma = rb_entry(n, struct vm_area_struct, vm_rb);
  641. if (vma->vm_start > addr)
  642. return NULL;
  643. if (vma->vm_end > addr) {
  644. mm->mmap_cache = vma;
  645. return vma;
  646. }
  647. }
  648. return NULL;
  649. }
  650. EXPORT_SYMBOL(find_vma);
  651. /*
  652. * find a VMA
  653. * - we don't extend stack VMAs under NOMMU conditions
  654. */
  655. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  656. {
  657. return find_vma(mm, addr);
  658. }
  659. /*
  660. * expand a stack to a given address
  661. * - not supported under NOMMU conditions
  662. */
  663. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  664. {
  665. return -ENOMEM;
  666. }
  667. /*
  668. * look up the first VMA exactly that exactly matches addr
  669. * - should be called with mm->mmap_sem at least held readlocked
  670. */
  671. static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  672. unsigned long addr,
  673. unsigned long len)
  674. {
  675. struct vm_area_struct *vma;
  676. struct rb_node *n = mm->mm_rb.rb_node;
  677. unsigned long end = addr + len;
  678. /* check the cache first */
  679. vma = mm->mmap_cache;
  680. if (vma && vma->vm_start == addr && vma->vm_end == end)
  681. return vma;
  682. /* trawl the tree (there may be multiple mappings in which addr
  683. * resides) */
  684. for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
  685. vma = rb_entry(n, struct vm_area_struct, vm_rb);
  686. if (vma->vm_start < addr)
  687. continue;
  688. if (vma->vm_start > addr)
  689. return NULL;
  690. if (vma->vm_end == end) {
  691. mm->mmap_cache = vma;
  692. return vma;
  693. }
  694. }
  695. return NULL;
  696. }
  697. /*
  698. * determine whether a mapping should be permitted and, if so, what sort of
  699. * mapping we're capable of supporting
  700. */
  701. static int validate_mmap_request(struct file *file,
  702. unsigned long addr,
  703. unsigned long len,
  704. unsigned long prot,
  705. unsigned long flags,
  706. unsigned long pgoff,
  707. unsigned long *_capabilities)
  708. {
  709. unsigned long capabilities, rlen;
  710. unsigned long reqprot = prot;
  711. int ret;
  712. /* do the simple checks first */
  713. if (flags & MAP_FIXED || addr) {
  714. printk(KERN_DEBUG
  715. "%d: Can't do fixed-address/overlay mmap of RAM\n",
  716. current->pid);
  717. return -EINVAL;
  718. }
  719. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  720. (flags & MAP_TYPE) != MAP_SHARED)
  721. return -EINVAL;
  722. if (!len)
  723. return -EINVAL;
  724. /* Careful about overflows.. */
  725. rlen = PAGE_ALIGN(len);
  726. if (!rlen || rlen > TASK_SIZE)
  727. return -ENOMEM;
  728. /* offset overflow? */
  729. if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
  730. return -EOVERFLOW;
  731. if (file) {
  732. /* validate file mapping requests */
  733. struct address_space *mapping;
  734. /* files must support mmap */
  735. if (!file->f_op || !file->f_op->mmap)
  736. return -ENODEV;
  737. /* work out if what we've got could possibly be shared
  738. * - we support chardevs that provide their own "memory"
  739. * - we support files/blockdevs that are memory backed
  740. */
  741. mapping = file->f_mapping;
  742. if (!mapping)
  743. mapping = file->f_path.dentry->d_inode->i_mapping;
  744. capabilities = 0;
  745. if (mapping && mapping->backing_dev_info)
  746. capabilities = mapping->backing_dev_info->capabilities;
  747. if (!capabilities) {
  748. /* no explicit capabilities set, so assume some
  749. * defaults */
  750. switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
  751. case S_IFREG:
  752. case S_IFBLK:
  753. capabilities = BDI_CAP_MAP_COPY;
  754. break;
  755. case S_IFCHR:
  756. capabilities =
  757. BDI_CAP_MAP_DIRECT |
  758. BDI_CAP_READ_MAP |
  759. BDI_CAP_WRITE_MAP;
  760. break;
  761. default:
  762. return -EINVAL;
  763. }
  764. }
  765. /* eliminate any capabilities that we can't support on this
  766. * device */
  767. if (!file->f_op->get_unmapped_area)
  768. capabilities &= ~BDI_CAP_MAP_DIRECT;
  769. if (!file->f_op->read)
  770. capabilities &= ~BDI_CAP_MAP_COPY;
  771. if (flags & MAP_SHARED) {
  772. /* do checks for writing, appending and locking */
  773. if ((prot & PROT_WRITE) &&
  774. !(file->f_mode & FMODE_WRITE))
  775. return -EACCES;
  776. if (IS_APPEND(file->f_path.dentry->d_inode) &&
  777. (file->f_mode & FMODE_WRITE))
  778. return -EACCES;
  779. if (locks_verify_locked(file->f_path.dentry->d_inode))
  780. return -EAGAIN;
  781. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  782. return -ENODEV;
  783. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  784. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  785. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  786. ) {
  787. printk("MAP_SHARED not completely supported on !MMU\n");
  788. return -EINVAL;
  789. }
  790. /* we mustn't privatise shared mappings */
  791. capabilities &= ~BDI_CAP_MAP_COPY;
  792. }
  793. else {
  794. /* we're going to read the file into private memory we
  795. * allocate */
  796. if (!(capabilities & BDI_CAP_MAP_COPY))
  797. return -ENODEV;
  798. /* we don't permit a private writable mapping to be
  799. * shared with the backing device */
  800. if (prot & PROT_WRITE)
  801. capabilities &= ~BDI_CAP_MAP_DIRECT;
  802. }
  803. /* handle executable mappings and implied executable
  804. * mappings */
  805. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  806. if (prot & PROT_EXEC)
  807. return -EPERM;
  808. }
  809. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  810. /* handle implication of PROT_EXEC by PROT_READ */
  811. if (current->personality & READ_IMPLIES_EXEC) {
  812. if (capabilities & BDI_CAP_EXEC_MAP)
  813. prot |= PROT_EXEC;
  814. }
  815. }
  816. else if ((prot & PROT_READ) &&
  817. (prot & PROT_EXEC) &&
  818. !(capabilities & BDI_CAP_EXEC_MAP)
  819. ) {
  820. /* backing file is not executable, try to copy */
  821. capabilities &= ~BDI_CAP_MAP_DIRECT;
  822. }
  823. }
  824. else {
  825. /* anonymous mappings are always memory backed and can be
  826. * privately mapped
  827. */
  828. capabilities = BDI_CAP_MAP_COPY;
  829. /* handle PROT_EXEC implication by PROT_READ */
  830. if ((prot & PROT_READ) &&
  831. (current->personality & READ_IMPLIES_EXEC))
  832. prot |= PROT_EXEC;
  833. }
  834. /* allow the security API to have its say */
  835. ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  836. if (ret < 0)
  837. return ret;
  838. /* looks okay */
  839. *_capabilities = capabilities;
  840. return 0;
  841. }
  842. /*
  843. * we've determined that we can make the mapping, now translate what we
  844. * now know into VMA flags
  845. */
  846. static unsigned long determine_vm_flags(struct file *file,
  847. unsigned long prot,
  848. unsigned long flags,
  849. unsigned long capabilities)
  850. {
  851. unsigned long vm_flags;
  852. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  853. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  854. /* vm_flags |= mm->def_flags; */
  855. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  856. /* attempt to share read-only copies of mapped file chunks */
  857. if (file && !(prot & PROT_WRITE))
  858. vm_flags |= VM_MAYSHARE;
  859. }
  860. else {
  861. /* overlay a shareable mapping on the backing device or inode
  862. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  863. * romfs/cramfs */
  864. if (flags & MAP_SHARED)
  865. vm_flags |= VM_MAYSHARE | VM_SHARED;
  866. else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
  867. vm_flags |= VM_MAYSHARE;
  868. }
  869. /* refuse to let anyone share private mappings with this process if
  870. * it's being traced - otherwise breakpoints set in it may interfere
  871. * with another untraced process
  872. */
  873. if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
  874. vm_flags &= ~VM_MAYSHARE;
  875. return vm_flags;
  876. }
  877. /*
  878. * set up a shared mapping on a file (the driver or filesystem provides and
  879. * pins the storage)
  880. */
  881. static int do_mmap_shared_file(struct vm_area_struct *vma)
  882. {
  883. int ret;
  884. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  885. if (ret == 0) {
  886. vma->vm_region->vm_top = vma->vm_region->vm_end;
  887. return ret;
  888. }
  889. if (ret != -ENOSYS)
  890. return ret;
  891. /* getting an ENOSYS error indicates that direct mmap isn't
  892. * possible (as opposed to tried but failed) so we'll fall
  893. * through to making a private copy of the data and mapping
  894. * that if we can */
  895. return -ENODEV;
  896. }
  897. /*
  898. * set up a private mapping or an anonymous shared mapping
  899. */
  900. static int do_mmap_private(struct vm_area_struct *vma,
  901. struct vm_region *region,
  902. unsigned long len)
  903. {
  904. struct page *pages;
  905. unsigned long total, point, n, rlen;
  906. void *base;
  907. int ret, order;
  908. /* invoke the file's mapping function so that it can keep track of
  909. * shared mappings on devices or memory
  910. * - VM_MAYSHARE will be set if it may attempt to share
  911. */
  912. if (vma->vm_file) {
  913. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  914. if (ret == 0) {
  915. /* shouldn't return success if we're not sharing */
  916. BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
  917. vma->vm_region->vm_top = vma->vm_region->vm_end;
  918. return ret;
  919. }
  920. if (ret != -ENOSYS)
  921. return ret;
  922. /* getting an ENOSYS error indicates that direct mmap isn't
  923. * possible (as opposed to tried but failed) so we'll try to
  924. * make a private copy of the data and map that instead */
  925. }
  926. rlen = PAGE_ALIGN(len);
  927. /* allocate some memory to hold the mapping
  928. * - note that this may not return a page-aligned address if the object
  929. * we're allocating is smaller than a page
  930. */
  931. order = get_order(rlen);
  932. kdebug("alloc order %d for %lx", order, len);
  933. pages = alloc_pages(GFP_KERNEL, order);
  934. if (!pages)
  935. goto enomem;
  936. total = 1 << order;
  937. atomic_long_add(total, &mmap_pages_allocated);
  938. point = rlen >> PAGE_SHIFT;
  939. /* we allocated a power-of-2 sized page set, so we may want to trim off
  940. * the excess */
  941. if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
  942. while (total > point) {
  943. order = ilog2(total - point);
  944. n = 1 << order;
  945. kdebug("shave %lu/%lu @%lu", n, total - point, total);
  946. atomic_long_sub(n, &mmap_pages_allocated);
  947. total -= n;
  948. set_page_refcounted(pages + total);
  949. __free_pages(pages + total, order);
  950. }
  951. }
  952. for (point = 1; point < total; point++)
  953. set_page_refcounted(&pages[point]);
  954. base = page_address(pages);
  955. region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
  956. region->vm_start = (unsigned long) base;
  957. region->vm_end = region->vm_start + rlen;
  958. region->vm_top = region->vm_start + (total << PAGE_SHIFT);
  959. vma->vm_start = region->vm_start;
  960. vma->vm_end = region->vm_start + len;
  961. if (vma->vm_file) {
  962. /* read the contents of a file into the copy */
  963. mm_segment_t old_fs;
  964. loff_t fpos;
  965. fpos = vma->vm_pgoff;
  966. fpos <<= PAGE_SHIFT;
  967. old_fs = get_fs();
  968. set_fs(KERNEL_DS);
  969. ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
  970. set_fs(old_fs);
  971. if (ret < 0)
  972. goto error_free;
  973. /* clear the last little bit */
  974. if (ret < rlen)
  975. memset(base + ret, 0, rlen - ret);
  976. } else {
  977. /* if it's an anonymous mapping, then just clear it */
  978. memset(base, 0, rlen);
  979. }
  980. return 0;
  981. error_free:
  982. free_page_series(region->vm_start, region->vm_end);
  983. region->vm_start = vma->vm_start = 0;
  984. region->vm_end = vma->vm_end = 0;
  985. region->vm_top = 0;
  986. return ret;
  987. enomem:
  988. printk("Allocation of length %lu from process %d (%s) failed\n",
  989. len, current->pid, current->comm);
  990. show_free_areas();
  991. return -ENOMEM;
  992. }
  993. /*
  994. * handle mapping creation for uClinux
  995. */
  996. unsigned long do_mmap_pgoff(struct file *file,
  997. unsigned long addr,
  998. unsigned long len,
  999. unsigned long prot,
  1000. unsigned long flags,
  1001. unsigned long pgoff)
  1002. {
  1003. struct vm_area_struct *vma;
  1004. struct vm_region *region;
  1005. struct rb_node *rb;
  1006. unsigned long capabilities, vm_flags, result;
  1007. int ret;
  1008. kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
  1009. if (!(flags & MAP_FIXED))
  1010. addr = round_hint_to_min(addr);
  1011. /* decide whether we should attempt the mapping, and if so what sort of
  1012. * mapping */
  1013. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  1014. &capabilities);
  1015. if (ret < 0) {
  1016. kleave(" = %d [val]", ret);
  1017. return ret;
  1018. }
  1019. /* we've determined that we can make the mapping, now translate what we
  1020. * now know into VMA flags */
  1021. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  1022. /* we're going to need to record the mapping */
  1023. region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
  1024. if (!region)
  1025. goto error_getting_region;
  1026. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1027. if (!vma)
  1028. goto error_getting_vma;
  1029. atomic_set(&region->vm_usage, 1);
  1030. region->vm_flags = vm_flags;
  1031. region->vm_pgoff = pgoff;
  1032. INIT_LIST_HEAD(&vma->anon_vma_node);
  1033. vma->vm_flags = vm_flags;
  1034. vma->vm_pgoff = pgoff;
  1035. if (file) {
  1036. region->vm_file = file;
  1037. get_file(file);
  1038. vma->vm_file = file;
  1039. get_file(file);
  1040. if (vm_flags & VM_EXECUTABLE) {
  1041. added_exe_file_vma(current->mm);
  1042. vma->vm_mm = current->mm;
  1043. }
  1044. }
  1045. down_write(&nommu_region_sem);
  1046. /* if we want to share, we need to check for regions created by other
  1047. * mmap() calls that overlap with our proposed mapping
  1048. * - we can only share with a superset match on most regular files
  1049. * - shared mappings on character devices and memory backed files are
  1050. * permitted to overlap inexactly as far as we are concerned for in
  1051. * these cases, sharing is handled in the driver or filesystem rather
  1052. * than here
  1053. */
  1054. if (vm_flags & VM_MAYSHARE) {
  1055. struct vm_region *pregion;
  1056. unsigned long pglen, rpglen, pgend, rpgend, start;
  1057. pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1058. pgend = pgoff + pglen;
  1059. for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
  1060. pregion = rb_entry(rb, struct vm_region, vm_rb);
  1061. if (!(pregion->vm_flags & VM_MAYSHARE))
  1062. continue;
  1063. /* search for overlapping mappings on the same file */
  1064. if (pregion->vm_file->f_path.dentry->d_inode !=
  1065. file->f_path.dentry->d_inode)
  1066. continue;
  1067. if (pregion->vm_pgoff >= pgend)
  1068. continue;
  1069. rpglen = pregion->vm_end - pregion->vm_start;
  1070. rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1071. rpgend = pregion->vm_pgoff + rpglen;
  1072. if (pgoff >= rpgend)
  1073. continue;
  1074. /* handle inexactly overlapping matches between
  1075. * mappings */
  1076. if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
  1077. !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
  1078. /* new mapping is not a subset of the region */
  1079. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  1080. goto sharing_violation;
  1081. continue;
  1082. }
  1083. /* we've found a region we can share */
  1084. atomic_inc(&pregion->vm_usage);
  1085. vma->vm_region = pregion;
  1086. start = pregion->vm_start;
  1087. start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
  1088. vma->vm_start = start;
  1089. vma->vm_end = start + len;
  1090. if (pregion->vm_flags & VM_MAPPED_COPY) {
  1091. kdebug("share copy");
  1092. vma->vm_flags |= VM_MAPPED_COPY;
  1093. } else {
  1094. kdebug("share mmap");
  1095. ret = do_mmap_shared_file(vma);
  1096. if (ret < 0) {
  1097. vma->vm_region = NULL;
  1098. vma->vm_start = 0;
  1099. vma->vm_end = 0;
  1100. atomic_dec(&pregion->vm_usage);
  1101. pregion = NULL;
  1102. goto error_just_free;
  1103. }
  1104. }
  1105. fput(region->vm_file);
  1106. kmem_cache_free(vm_region_jar, region);
  1107. region = pregion;
  1108. result = start;
  1109. goto share;
  1110. }
  1111. /* obtain the address at which to make a shared mapping
  1112. * - this is the hook for quasi-memory character devices to
  1113. * tell us the location of a shared mapping
  1114. */
  1115. if (file && file->f_op->get_unmapped_area) {
  1116. addr = file->f_op->get_unmapped_area(file, addr, len,
  1117. pgoff, flags);
  1118. if (IS_ERR((void *) addr)) {
  1119. ret = addr;
  1120. if (ret != (unsigned long) -ENOSYS)
  1121. goto error_just_free;
  1122. /* the driver refused to tell us where to site
  1123. * the mapping so we'll have to attempt to copy
  1124. * it */
  1125. ret = (unsigned long) -ENODEV;
  1126. if (!(capabilities & BDI_CAP_MAP_COPY))
  1127. goto error_just_free;
  1128. capabilities &= ~BDI_CAP_MAP_DIRECT;
  1129. } else {
  1130. vma->vm_start = region->vm_start = addr;
  1131. vma->vm_end = region->vm_end = addr + len;
  1132. }
  1133. }
  1134. }
  1135. vma->vm_region = region;
  1136. /* set up the mapping */
  1137. if (file && vma->vm_flags & VM_SHARED)
  1138. ret = do_mmap_shared_file(vma);
  1139. else
  1140. ret = do_mmap_private(vma, region, len);
  1141. if (ret < 0)
  1142. goto error_put_region;
  1143. add_nommu_region(region);
  1144. /* okay... we have a mapping; now we have to register it */
  1145. result = vma->vm_start;
  1146. current->mm->total_vm += len >> PAGE_SHIFT;
  1147. share:
  1148. add_vma_to_mm(current->mm, vma);
  1149. up_write(&nommu_region_sem);
  1150. if (prot & PROT_EXEC)
  1151. flush_icache_range(result, result + len);
  1152. kleave(" = %lx", result);
  1153. return result;
  1154. error_put_region:
  1155. __put_nommu_region(region);
  1156. if (vma) {
  1157. if (vma->vm_file) {
  1158. fput(vma->vm_file);
  1159. if (vma->vm_flags & VM_EXECUTABLE)
  1160. removed_exe_file_vma(vma->vm_mm);
  1161. }
  1162. kmem_cache_free(vm_area_cachep, vma);
  1163. }
  1164. kleave(" = %d [pr]", ret);
  1165. return ret;
  1166. error_just_free:
  1167. up_write(&nommu_region_sem);
  1168. error:
  1169. fput(region->vm_file);
  1170. kmem_cache_free(vm_region_jar, region);
  1171. fput(vma->vm_file);
  1172. if (vma->vm_flags & VM_EXECUTABLE)
  1173. removed_exe_file_vma(vma->vm_mm);
  1174. kmem_cache_free(vm_area_cachep, vma);
  1175. kleave(" = %d", ret);
  1176. return ret;
  1177. sharing_violation:
  1178. up_write(&nommu_region_sem);
  1179. printk(KERN_WARNING "Attempt to share mismatched mappings\n");
  1180. ret = -EINVAL;
  1181. goto error;
  1182. error_getting_vma:
  1183. kmem_cache_free(vm_region_jar, region);
  1184. printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
  1185. " from process %d failed\n",
  1186. len, current->pid);
  1187. show_free_areas();
  1188. return -ENOMEM;
  1189. error_getting_region:
  1190. printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
  1191. " from process %d failed\n",
  1192. len, current->pid);
  1193. show_free_areas();
  1194. return -ENOMEM;
  1195. }
  1196. EXPORT_SYMBOL(do_mmap_pgoff);
  1197. /*
  1198. * split a vma into two pieces at address 'addr', a new vma is allocated either
  1199. * for the first part or the tail.
  1200. */
  1201. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1202. unsigned long addr, int new_below)
  1203. {
  1204. struct vm_area_struct *new;
  1205. struct vm_region *region;
  1206. unsigned long npages;
  1207. kenter("");
  1208. /* we're only permitted to split anonymous regions that have a single
  1209. * owner */
  1210. if (vma->vm_file ||
  1211. atomic_read(&vma->vm_region->vm_usage) != 1)
  1212. return -ENOMEM;
  1213. if (mm->map_count >= sysctl_max_map_count)
  1214. return -ENOMEM;
  1215. region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
  1216. if (!region)
  1217. return -ENOMEM;
  1218. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1219. if (!new) {
  1220. kmem_cache_free(vm_region_jar, region);
  1221. return -ENOMEM;
  1222. }
  1223. /* most fields are the same, copy all, and then fixup */
  1224. *new = *vma;
  1225. *region = *vma->vm_region;
  1226. new->vm_region = region;
  1227. npages = (addr - vma->vm_start) >> PAGE_SHIFT;
  1228. if (new_below) {
  1229. region->vm_top = region->vm_end = new->vm_end = addr;
  1230. } else {
  1231. region->vm_start = new->vm_start = addr;
  1232. region->vm_pgoff = new->vm_pgoff += npages;
  1233. }
  1234. if (new->vm_ops && new->vm_ops->open)
  1235. new->vm_ops->open(new);
  1236. delete_vma_from_mm(vma);
  1237. down_write(&nommu_region_sem);
  1238. delete_nommu_region(vma->vm_region);
  1239. if (new_below) {
  1240. vma->vm_region->vm_start = vma->vm_start = addr;
  1241. vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
  1242. } else {
  1243. vma->vm_region->vm_end = vma->vm_end = addr;
  1244. vma->vm_region->vm_top = addr;
  1245. }
  1246. add_nommu_region(vma->vm_region);
  1247. add_nommu_region(new->vm_region);
  1248. up_write(&nommu_region_sem);
  1249. add_vma_to_mm(mm, vma);
  1250. add_vma_to_mm(mm, new);
  1251. return 0;
  1252. }
  1253. /*
  1254. * shrink a VMA by removing the specified chunk from either the beginning or
  1255. * the end
  1256. */
  1257. static int shrink_vma(struct mm_struct *mm,
  1258. struct vm_area_struct *vma,
  1259. unsigned long from, unsigned long to)
  1260. {
  1261. struct vm_region *region;
  1262. kenter("");
  1263. /* adjust the VMA's pointers, which may reposition it in the MM's tree
  1264. * and list */
  1265. delete_vma_from_mm(vma);
  1266. if (from > vma->vm_start)
  1267. vma->vm_end = from;
  1268. else
  1269. vma->vm_start = to;
  1270. add_vma_to_mm(mm, vma);
  1271. /* cut the backing region down to size */
  1272. region = vma->vm_region;
  1273. BUG_ON(atomic_read(&region->vm_usage) != 1);
  1274. down_write(&nommu_region_sem);
  1275. delete_nommu_region(region);
  1276. if (from > region->vm_start) {
  1277. to = region->vm_top;
  1278. region->vm_top = region->vm_end = from;
  1279. } else {
  1280. region->vm_start = to;
  1281. }
  1282. add_nommu_region(region);
  1283. up_write(&nommu_region_sem);
  1284. free_page_series(from, to);
  1285. return 0;
  1286. }
  1287. /*
  1288. * release a mapping
  1289. * - under NOMMU conditions the chunk to be unmapped must be backed by a single
  1290. * VMA, though it need not cover the whole VMA
  1291. */
  1292. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1293. {
  1294. struct vm_area_struct *vma;
  1295. struct rb_node *rb;
  1296. unsigned long end = start + len;
  1297. int ret;
  1298. kenter(",%lx,%zx", start, len);
  1299. if (len == 0)
  1300. return -EINVAL;
  1301. /* find the first potentially overlapping VMA */
  1302. vma = find_vma(mm, start);
  1303. if (!vma) {
  1304. static int limit = 0;
  1305. if (limit < 5) {
  1306. printk(KERN_WARNING
  1307. "munmap of memory not mmapped by process %d"
  1308. " (%s): 0x%lx-0x%lx\n",
  1309. current->pid, current->comm,
  1310. start, start + len - 1);
  1311. limit++;
  1312. }
  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. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  1571. return 0;
  1572. error:
  1573. vm_unacct_memory(pages);
  1574. return -ENOMEM;
  1575. }
  1576. int in_gate_area_no_task(unsigned long addr)
  1577. {
  1578. return 0;
  1579. }
  1580. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1581. {
  1582. BUG();
  1583. return 0;
  1584. }
  1585. EXPORT_SYMBOL(filemap_fault);
  1586. /*
  1587. * Access another process' address space.
  1588. * - source/target buffer must be kernel space
  1589. */
  1590. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1591. {
  1592. struct vm_area_struct *vma;
  1593. struct mm_struct *mm;
  1594. if (addr + len < addr)
  1595. return 0;
  1596. mm = get_task_mm(tsk);
  1597. if (!mm)
  1598. return 0;
  1599. down_read(&mm->mmap_sem);
  1600. /* the access must start within one of the target process's mappings */
  1601. vma = find_vma(mm, addr);
  1602. if (vma) {
  1603. /* don't overrun this mapping */
  1604. if (addr + len >= vma->vm_end)
  1605. len = vma->vm_end - addr;
  1606. /* only read or write mappings where it is permitted */
  1607. if (write && vma->vm_flags & VM_MAYWRITE)
  1608. len -= copy_to_user((void *) addr, buf, len);
  1609. else if (!write && vma->vm_flags & VM_MAYREAD)
  1610. len -= copy_from_user(buf, (void *) addr, len);
  1611. else
  1612. len = 0;
  1613. } else {
  1614. len = 0;
  1615. }
  1616. up_read(&mm->mmap_sem);
  1617. mmput(mm);
  1618. return len;
  1619. }