nommu.c 47 KB

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