nommu.c 46 KB

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