nommu.c 46 KB

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