nommu.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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-2005 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 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/ptrace.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. void *high_memory;
  35. struct page *mem_map;
  36. unsigned long max_mapnr;
  37. unsigned long num_physpages;
  38. unsigned long askedalloc, realalloc;
  39. atomic_t vm_committed_space = ATOMIC_INIT(0);
  40. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  41. int sysctl_overcommit_ratio = 50; /* default is 50% */
  42. int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
  43. int heap_stack_gap = 0;
  44. EXPORT_SYMBOL(mem_map);
  45. EXPORT_SYMBOL(num_physpages);
  46. /* list of shareable VMAs */
  47. struct rb_root nommu_vma_tree = RB_ROOT;
  48. DECLARE_RWSEM(nommu_vma_sem);
  49. struct vm_operations_struct generic_file_vm_ops = {
  50. };
  51. /*
  52. * Handle all mappings that got truncated by a "truncate()"
  53. * system call.
  54. *
  55. * NOTE! We have to be ready to update the memory sharing
  56. * between the file and the memory map for a potential last
  57. * incomplete page. Ugly, but necessary.
  58. */
  59. int vmtruncate(struct inode *inode, loff_t offset)
  60. {
  61. struct address_space *mapping = inode->i_mapping;
  62. unsigned long limit;
  63. if (inode->i_size < offset)
  64. goto do_expand;
  65. i_size_write(inode, offset);
  66. truncate_inode_pages(mapping, offset);
  67. goto out_truncate;
  68. do_expand:
  69. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  70. if (limit != RLIM_INFINITY && offset > limit)
  71. goto out_sig;
  72. if (offset > inode->i_sb->s_maxbytes)
  73. goto out;
  74. i_size_write(inode, offset);
  75. out_truncate:
  76. if (inode->i_op && inode->i_op->truncate)
  77. inode->i_op->truncate(inode);
  78. return 0;
  79. out_sig:
  80. send_sig(SIGXFSZ, current, 0);
  81. out:
  82. return -EFBIG;
  83. }
  84. EXPORT_SYMBOL(vmtruncate);
  85. /*
  86. * Return the total memory allocated for this pointer, not
  87. * just what the caller asked for.
  88. *
  89. * Doesn't have to be accurate, i.e. may have races.
  90. */
  91. unsigned int kobjsize(const void *objp)
  92. {
  93. struct page *page;
  94. if (!objp || !((page = virt_to_page(objp))))
  95. return 0;
  96. if (PageSlab(page))
  97. return ksize(objp);
  98. BUG_ON(page->index < 0);
  99. BUG_ON(page->index >= MAX_ORDER);
  100. return (PAGE_SIZE << page->index);
  101. }
  102. /*
  103. * get a list of pages in an address range belonging to the specified process
  104. * and indicate the VMA that covers each page
  105. * - this is potentially dodgy as we may end incrementing the page count of a
  106. * slab page or a secondary page from a compound page
  107. * - don't permit access to VMAs that don't support it, such as I/O mappings
  108. */
  109. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  110. unsigned long start, int len, int write, int force,
  111. struct page **pages, struct vm_area_struct **vmas)
  112. {
  113. struct vm_area_struct *vma;
  114. unsigned long vm_flags;
  115. int i;
  116. /* calculate required read or write permissions.
  117. * - if 'force' is set, we only require the "MAY" flags.
  118. */
  119. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  120. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  121. for (i = 0; i < len; i++) {
  122. vma = find_vma(mm, start);
  123. if (!vma)
  124. goto finish_or_fault;
  125. /* protect what we can, including chardevs */
  126. if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
  127. !(vm_flags & vma->vm_flags))
  128. goto finish_or_fault;
  129. if (pages) {
  130. pages[i] = virt_to_page(start);
  131. if (pages[i])
  132. page_cache_get(pages[i]);
  133. }
  134. if (vmas)
  135. vmas[i] = vma;
  136. start += PAGE_SIZE;
  137. }
  138. return i;
  139. finish_or_fault:
  140. return i ? : -EFAULT;
  141. }
  142. EXPORT_SYMBOL(get_user_pages);
  143. DEFINE_RWLOCK(vmlist_lock);
  144. struct vm_struct *vmlist;
  145. void vfree(const void *addr)
  146. {
  147. kfree(addr);
  148. }
  149. EXPORT_SYMBOL(vfree);
  150. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  151. {
  152. /*
  153. * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
  154. * returns only a logical address.
  155. */
  156. return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
  157. }
  158. EXPORT_SYMBOL(__vmalloc);
  159. void *vmalloc_user(unsigned long size)
  160. {
  161. void *ret;
  162. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  163. PAGE_KERNEL);
  164. if (ret) {
  165. struct vm_area_struct *vma;
  166. down_write(&current->mm->mmap_sem);
  167. vma = find_vma(current->mm, (unsigned long)ret);
  168. if (vma)
  169. vma->vm_flags |= VM_USERMAP;
  170. up_write(&current->mm->mmap_sem);
  171. }
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(vmalloc_user);
  175. struct page *vmalloc_to_page(const void *addr)
  176. {
  177. return virt_to_page(addr);
  178. }
  179. EXPORT_SYMBOL(vmalloc_to_page);
  180. unsigned long vmalloc_to_pfn(const void *addr)
  181. {
  182. return page_to_pfn(virt_to_page(addr));
  183. }
  184. EXPORT_SYMBOL(vmalloc_to_pfn);
  185. long vread(char *buf, char *addr, unsigned long count)
  186. {
  187. memcpy(buf, addr, count);
  188. return count;
  189. }
  190. long vwrite(char *buf, char *addr, unsigned long count)
  191. {
  192. /* Don't allow overflow */
  193. if ((unsigned long) addr + count < count)
  194. count = -(unsigned long) addr;
  195. memcpy(addr, buf, count);
  196. return(count);
  197. }
  198. /*
  199. * vmalloc - allocate virtually continguos memory
  200. *
  201. * @size: allocation size
  202. *
  203. * Allocate enough pages to cover @size from the page level
  204. * allocator and map them into continguos kernel virtual space.
  205. *
  206. * For tight control over page level allocator and protection flags
  207. * use __vmalloc() instead.
  208. */
  209. void *vmalloc(unsigned long size)
  210. {
  211. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  212. }
  213. EXPORT_SYMBOL(vmalloc);
  214. void *vmalloc_node(unsigned long size, int node)
  215. {
  216. return vmalloc(size);
  217. }
  218. EXPORT_SYMBOL(vmalloc_node);
  219. /**
  220. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  221. * @size: allocation size
  222. *
  223. * Allocate enough 32bit PA addressable pages to cover @size from the
  224. * page level allocator and map them into continguos kernel virtual space.
  225. */
  226. void *vmalloc_32(unsigned long size)
  227. {
  228. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  229. }
  230. EXPORT_SYMBOL(vmalloc_32);
  231. /**
  232. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  233. * @size: allocation size
  234. *
  235. * The resulting memory area is 32bit addressable and zeroed so it can be
  236. * mapped to userspace without leaking data.
  237. *
  238. * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
  239. * remap_vmalloc_range() are permissible.
  240. */
  241. void *vmalloc_32_user(unsigned long size)
  242. {
  243. /*
  244. * We'll have to sort out the ZONE_DMA bits for 64-bit,
  245. * but for now this can simply use vmalloc_user() directly.
  246. */
  247. return vmalloc_user(size);
  248. }
  249. EXPORT_SYMBOL(vmalloc_32_user);
  250. void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
  251. {
  252. BUG();
  253. return NULL;
  254. }
  255. EXPORT_SYMBOL(vmap);
  256. void vunmap(const void *addr)
  257. {
  258. BUG();
  259. }
  260. EXPORT_SYMBOL(vunmap);
  261. /*
  262. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  263. * have one.
  264. */
  265. void __attribute__((weak)) vmalloc_sync_all(void)
  266. {
  267. }
  268. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  269. struct page *page)
  270. {
  271. return -EINVAL;
  272. }
  273. EXPORT_SYMBOL(vm_insert_page);
  274. /*
  275. * sys_brk() for the most part doesn't need the global kernel
  276. * lock, except when an application is doing something nasty
  277. * like trying to un-brk an area that has already been mapped
  278. * to a regular file. in this case, the unmapping will need
  279. * to invoke file system routines that need the global lock.
  280. */
  281. asmlinkage unsigned long sys_brk(unsigned long brk)
  282. {
  283. struct mm_struct *mm = current->mm;
  284. if (brk < mm->start_brk || brk > mm->context.end_brk)
  285. return mm->brk;
  286. if (mm->brk == brk)
  287. return mm->brk;
  288. /*
  289. * Always allow shrinking brk
  290. */
  291. if (brk <= mm->brk) {
  292. mm->brk = brk;
  293. return brk;
  294. }
  295. /*
  296. * Ok, looks good - let it rip.
  297. */
  298. return mm->brk = brk;
  299. }
  300. #ifdef DEBUG
  301. static void show_process_blocks(void)
  302. {
  303. struct vm_list_struct *vml;
  304. printk("Process blocks %d:", current->pid);
  305. for (vml = &current->mm->context.vmlist; vml; vml = vml->next) {
  306. printk(" %p: %p", vml, vml->vma);
  307. if (vml->vma)
  308. printk(" (%d @%lx #%d)",
  309. kobjsize((void *) vml->vma->vm_start),
  310. vml->vma->vm_start,
  311. atomic_read(&vml->vma->vm_usage));
  312. printk(vml->next ? " ->" : ".\n");
  313. }
  314. }
  315. #endif /* DEBUG */
  316. /*
  317. * add a VMA into a process's mm_struct in the appropriate place in the list
  318. * - should be called with mm->mmap_sem held writelocked
  319. */
  320. static void add_vma_to_mm(struct mm_struct *mm, struct vm_list_struct *vml)
  321. {
  322. struct vm_list_struct **ppv;
  323. for (ppv = &current->mm->context.vmlist; *ppv; ppv = &(*ppv)->next)
  324. if ((*ppv)->vma->vm_start > vml->vma->vm_start)
  325. break;
  326. vml->next = *ppv;
  327. *ppv = vml;
  328. }
  329. /*
  330. * look up the first VMA in which addr resides, NULL if none
  331. * - should be called with mm->mmap_sem at least held readlocked
  332. */
  333. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  334. {
  335. struct vm_list_struct *loop, *vml;
  336. /* search the vm_start ordered list */
  337. vml = NULL;
  338. for (loop = mm->context.vmlist; loop; loop = loop->next) {
  339. if (loop->vma->vm_start > addr)
  340. break;
  341. vml = loop;
  342. }
  343. if (vml && vml->vma->vm_end > addr)
  344. return vml->vma;
  345. return NULL;
  346. }
  347. EXPORT_SYMBOL(find_vma);
  348. /*
  349. * find a VMA
  350. * - we don't extend stack VMAs under NOMMU conditions
  351. */
  352. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  353. {
  354. return find_vma(mm, addr);
  355. }
  356. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  357. {
  358. return -ENOMEM;
  359. }
  360. /*
  361. * look up the first VMA exactly that exactly matches addr
  362. * - should be called with mm->mmap_sem at least held readlocked
  363. */
  364. static inline struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  365. unsigned long addr)
  366. {
  367. struct vm_list_struct *vml;
  368. /* search the vm_start ordered list */
  369. for (vml = mm->context.vmlist; vml; vml = vml->next) {
  370. if (vml->vma->vm_start == addr)
  371. return vml->vma;
  372. if (vml->vma->vm_start > addr)
  373. break;
  374. }
  375. return NULL;
  376. }
  377. /*
  378. * find a VMA in the global tree
  379. */
  380. static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
  381. {
  382. struct vm_area_struct *vma;
  383. struct rb_node *n = nommu_vma_tree.rb_node;
  384. while (n) {
  385. vma = rb_entry(n, struct vm_area_struct, vm_rb);
  386. if (start < vma->vm_start)
  387. n = n->rb_left;
  388. else if (start > vma->vm_start)
  389. n = n->rb_right;
  390. else
  391. return vma;
  392. }
  393. return NULL;
  394. }
  395. /*
  396. * add a VMA in the global tree
  397. */
  398. static void add_nommu_vma(struct vm_area_struct *vma)
  399. {
  400. struct vm_area_struct *pvma;
  401. struct address_space *mapping;
  402. struct rb_node **p = &nommu_vma_tree.rb_node;
  403. struct rb_node *parent = NULL;
  404. /* add the VMA to the mapping */
  405. if (vma->vm_file) {
  406. mapping = vma->vm_file->f_mapping;
  407. flush_dcache_mmap_lock(mapping);
  408. vma_prio_tree_insert(vma, &mapping->i_mmap);
  409. flush_dcache_mmap_unlock(mapping);
  410. }
  411. /* add the VMA to the master list */
  412. while (*p) {
  413. parent = *p;
  414. pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
  415. if (vma->vm_start < pvma->vm_start) {
  416. p = &(*p)->rb_left;
  417. }
  418. else if (vma->vm_start > pvma->vm_start) {
  419. p = &(*p)->rb_right;
  420. }
  421. else {
  422. /* mappings are at the same address - this can only
  423. * happen for shared-mem chardevs and shared file
  424. * mappings backed by ramfs/tmpfs */
  425. BUG_ON(!(pvma->vm_flags & VM_SHARED));
  426. if (vma < pvma)
  427. p = &(*p)->rb_left;
  428. else if (vma > pvma)
  429. p = &(*p)->rb_right;
  430. else
  431. BUG();
  432. }
  433. }
  434. rb_link_node(&vma->vm_rb, parent, p);
  435. rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
  436. }
  437. /*
  438. * delete a VMA from the global list
  439. */
  440. static void delete_nommu_vma(struct vm_area_struct *vma)
  441. {
  442. struct address_space *mapping;
  443. /* remove the VMA from the mapping */
  444. if (vma->vm_file) {
  445. mapping = vma->vm_file->f_mapping;
  446. flush_dcache_mmap_lock(mapping);
  447. vma_prio_tree_remove(vma, &mapping->i_mmap);
  448. flush_dcache_mmap_unlock(mapping);
  449. }
  450. /* remove from the master list */
  451. rb_erase(&vma->vm_rb, &nommu_vma_tree);
  452. }
  453. /*
  454. * determine whether a mapping should be permitted and, if so, what sort of
  455. * mapping we're capable of supporting
  456. */
  457. static int validate_mmap_request(struct file *file,
  458. unsigned long addr,
  459. unsigned long len,
  460. unsigned long prot,
  461. unsigned long flags,
  462. unsigned long pgoff,
  463. unsigned long *_capabilities)
  464. {
  465. unsigned long capabilities;
  466. unsigned long reqprot = prot;
  467. int ret;
  468. /* do the simple checks first */
  469. if (flags & MAP_FIXED || addr) {
  470. printk(KERN_DEBUG
  471. "%d: Can't do fixed-address/overlay mmap of RAM\n",
  472. current->pid);
  473. return -EINVAL;
  474. }
  475. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  476. (flags & MAP_TYPE) != MAP_SHARED)
  477. return -EINVAL;
  478. if (!len)
  479. return -EINVAL;
  480. /* Careful about overflows.. */
  481. len = PAGE_ALIGN(len);
  482. if (!len || len > TASK_SIZE)
  483. return -ENOMEM;
  484. /* offset overflow? */
  485. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  486. return -EOVERFLOW;
  487. if (file) {
  488. /* validate file mapping requests */
  489. struct address_space *mapping;
  490. /* files must support mmap */
  491. if (!file->f_op || !file->f_op->mmap)
  492. return -ENODEV;
  493. /* work out if what we've got could possibly be shared
  494. * - we support chardevs that provide their own "memory"
  495. * - we support files/blockdevs that are memory backed
  496. */
  497. mapping = file->f_mapping;
  498. if (!mapping)
  499. mapping = file->f_path.dentry->d_inode->i_mapping;
  500. capabilities = 0;
  501. if (mapping && mapping->backing_dev_info)
  502. capabilities = mapping->backing_dev_info->capabilities;
  503. if (!capabilities) {
  504. /* no explicit capabilities set, so assume some
  505. * defaults */
  506. switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
  507. case S_IFREG:
  508. case S_IFBLK:
  509. capabilities = BDI_CAP_MAP_COPY;
  510. break;
  511. case S_IFCHR:
  512. capabilities =
  513. BDI_CAP_MAP_DIRECT |
  514. BDI_CAP_READ_MAP |
  515. BDI_CAP_WRITE_MAP;
  516. break;
  517. default:
  518. return -EINVAL;
  519. }
  520. }
  521. /* eliminate any capabilities that we can't support on this
  522. * device */
  523. if (!file->f_op->get_unmapped_area)
  524. capabilities &= ~BDI_CAP_MAP_DIRECT;
  525. if (!file->f_op->read)
  526. capabilities &= ~BDI_CAP_MAP_COPY;
  527. if (flags & MAP_SHARED) {
  528. /* do checks for writing, appending and locking */
  529. if ((prot & PROT_WRITE) &&
  530. !(file->f_mode & FMODE_WRITE))
  531. return -EACCES;
  532. if (IS_APPEND(file->f_path.dentry->d_inode) &&
  533. (file->f_mode & FMODE_WRITE))
  534. return -EACCES;
  535. if (locks_verify_locked(file->f_path.dentry->d_inode))
  536. return -EAGAIN;
  537. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  538. return -ENODEV;
  539. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  540. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  541. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  542. ) {
  543. printk("MAP_SHARED not completely supported on !MMU\n");
  544. return -EINVAL;
  545. }
  546. /* we mustn't privatise shared mappings */
  547. capabilities &= ~BDI_CAP_MAP_COPY;
  548. }
  549. else {
  550. /* we're going to read the file into private memory we
  551. * allocate */
  552. if (!(capabilities & BDI_CAP_MAP_COPY))
  553. return -ENODEV;
  554. /* we don't permit a private writable mapping to be
  555. * shared with the backing device */
  556. if (prot & PROT_WRITE)
  557. capabilities &= ~BDI_CAP_MAP_DIRECT;
  558. }
  559. /* handle executable mappings and implied executable
  560. * mappings */
  561. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  562. if (prot & PROT_EXEC)
  563. return -EPERM;
  564. }
  565. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  566. /* handle implication of PROT_EXEC by PROT_READ */
  567. if (current->personality & READ_IMPLIES_EXEC) {
  568. if (capabilities & BDI_CAP_EXEC_MAP)
  569. prot |= PROT_EXEC;
  570. }
  571. }
  572. else if ((prot & PROT_READ) &&
  573. (prot & PROT_EXEC) &&
  574. !(capabilities & BDI_CAP_EXEC_MAP)
  575. ) {
  576. /* backing file is not executable, try to copy */
  577. capabilities &= ~BDI_CAP_MAP_DIRECT;
  578. }
  579. }
  580. else {
  581. /* anonymous mappings are always memory backed and can be
  582. * privately mapped
  583. */
  584. capabilities = BDI_CAP_MAP_COPY;
  585. /* handle PROT_EXEC implication by PROT_READ */
  586. if ((prot & PROT_READ) &&
  587. (current->personality & READ_IMPLIES_EXEC))
  588. prot |= PROT_EXEC;
  589. }
  590. /* allow the security API to have its say */
  591. ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  592. if (ret < 0)
  593. return ret;
  594. /* looks okay */
  595. *_capabilities = capabilities;
  596. return 0;
  597. }
  598. /*
  599. * we've determined that we can make the mapping, now translate what we
  600. * now know into VMA flags
  601. */
  602. static unsigned long determine_vm_flags(struct file *file,
  603. unsigned long prot,
  604. unsigned long flags,
  605. unsigned long capabilities)
  606. {
  607. unsigned long vm_flags;
  608. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  609. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  610. /* vm_flags |= mm->def_flags; */
  611. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  612. /* attempt to share read-only copies of mapped file chunks */
  613. if (file && !(prot & PROT_WRITE))
  614. vm_flags |= VM_MAYSHARE;
  615. }
  616. else {
  617. /* overlay a shareable mapping on the backing device or inode
  618. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  619. * romfs/cramfs */
  620. if (flags & MAP_SHARED)
  621. vm_flags |= VM_MAYSHARE | VM_SHARED;
  622. else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
  623. vm_flags |= VM_MAYSHARE;
  624. }
  625. /* refuse to let anyone share private mappings with this process if
  626. * it's being traced - otherwise breakpoints set in it may interfere
  627. * with another untraced process
  628. */
  629. if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
  630. vm_flags &= ~VM_MAYSHARE;
  631. return vm_flags;
  632. }
  633. /*
  634. * set up a shared mapping on a file
  635. */
  636. static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
  637. {
  638. int ret;
  639. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  640. if (ret != -ENOSYS)
  641. return ret;
  642. /* getting an ENOSYS error indicates that direct mmap isn't
  643. * possible (as opposed to tried but failed) so we'll fall
  644. * through to making a private copy of the data and mapping
  645. * that if we can */
  646. return -ENODEV;
  647. }
  648. /*
  649. * set up a private mapping or an anonymous shared mapping
  650. */
  651. static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
  652. {
  653. void *base;
  654. int ret;
  655. /* invoke the file's mapping function so that it can keep track of
  656. * shared mappings on devices or memory
  657. * - VM_MAYSHARE will be set if it may attempt to share
  658. */
  659. if (vma->vm_file) {
  660. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  661. if (ret != -ENOSYS) {
  662. /* shouldn't return success if we're not sharing */
  663. BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
  664. return ret; /* success or a real error */
  665. }
  666. /* getting an ENOSYS error indicates that direct mmap isn't
  667. * possible (as opposed to tried but failed) so we'll try to
  668. * make a private copy of the data and map that instead */
  669. }
  670. /* allocate some memory to hold the mapping
  671. * - note that this may not return a page-aligned address if the object
  672. * we're allocating is smaller than a page
  673. */
  674. base = kmalloc(len, GFP_KERNEL|__GFP_COMP);
  675. if (!base)
  676. goto enomem;
  677. vma->vm_start = (unsigned long) base;
  678. vma->vm_end = vma->vm_start + len;
  679. vma->vm_flags |= VM_MAPPED_COPY;
  680. #ifdef WARN_ON_SLACK
  681. if (len + WARN_ON_SLACK <= kobjsize(result))
  682. printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
  683. len, current->pid, kobjsize(result) - len);
  684. #endif
  685. if (vma->vm_file) {
  686. /* read the contents of a file into the copy */
  687. mm_segment_t old_fs;
  688. loff_t fpos;
  689. fpos = vma->vm_pgoff;
  690. fpos <<= PAGE_SHIFT;
  691. old_fs = get_fs();
  692. set_fs(KERNEL_DS);
  693. ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
  694. set_fs(old_fs);
  695. if (ret < 0)
  696. goto error_free;
  697. /* clear the last little bit */
  698. if (ret < len)
  699. memset(base + ret, 0, len - ret);
  700. } else {
  701. /* if it's an anonymous mapping, then just clear it */
  702. memset(base, 0, len);
  703. }
  704. return 0;
  705. error_free:
  706. kfree(base);
  707. vma->vm_start = 0;
  708. return ret;
  709. enomem:
  710. printk("Allocation of length %lu from process %d failed\n",
  711. len, current->pid);
  712. show_free_areas();
  713. return -ENOMEM;
  714. }
  715. /*
  716. * handle mapping creation for uClinux
  717. */
  718. unsigned long do_mmap_pgoff(struct file *file,
  719. unsigned long addr,
  720. unsigned long len,
  721. unsigned long prot,
  722. unsigned long flags,
  723. unsigned long pgoff)
  724. {
  725. struct vm_list_struct *vml = NULL;
  726. struct vm_area_struct *vma = NULL;
  727. struct rb_node *rb;
  728. unsigned long capabilities, vm_flags;
  729. void *result;
  730. int ret;
  731. if (!(flags & MAP_FIXED))
  732. addr = round_hint_to_min(addr);
  733. /* decide whether we should attempt the mapping, and if so what sort of
  734. * mapping */
  735. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  736. &capabilities);
  737. if (ret < 0)
  738. return ret;
  739. /* we've determined that we can make the mapping, now translate what we
  740. * now know into VMA flags */
  741. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  742. /* we're going to need to record the mapping if it works */
  743. vml = kzalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
  744. if (!vml)
  745. goto error_getting_vml;
  746. down_write(&nommu_vma_sem);
  747. /* if we want to share, we need to check for VMAs created by other
  748. * mmap() calls that overlap with our proposed mapping
  749. * - we can only share with an exact match on most regular files
  750. * - shared mappings on character devices and memory backed files are
  751. * permitted to overlap inexactly as far as we are concerned for in
  752. * these cases, sharing is handled in the driver or filesystem rather
  753. * than here
  754. */
  755. if (vm_flags & VM_MAYSHARE) {
  756. unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  757. unsigned long vmpglen;
  758. /* suppress VMA sharing for shared regions */
  759. if (vm_flags & VM_SHARED &&
  760. capabilities & BDI_CAP_MAP_DIRECT)
  761. goto dont_share_VMAs;
  762. for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
  763. vma = rb_entry(rb, struct vm_area_struct, vm_rb);
  764. if (!(vma->vm_flags & VM_MAYSHARE))
  765. continue;
  766. /* search for overlapping mappings on the same file */
  767. if (vma->vm_file->f_path.dentry->d_inode != file->f_path.dentry->d_inode)
  768. continue;
  769. if (vma->vm_pgoff >= pgoff + pglen)
  770. continue;
  771. vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
  772. vmpglen >>= PAGE_SHIFT;
  773. if (pgoff >= vma->vm_pgoff + vmpglen)
  774. continue;
  775. /* handle inexactly overlapping matches between mappings */
  776. if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
  777. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  778. goto sharing_violation;
  779. continue;
  780. }
  781. /* we've found a VMA we can share */
  782. atomic_inc(&vma->vm_usage);
  783. vml->vma = vma;
  784. result = (void *) vma->vm_start;
  785. goto shared;
  786. }
  787. dont_share_VMAs:
  788. vma = NULL;
  789. /* obtain the address at which to make a shared mapping
  790. * - this is the hook for quasi-memory character devices to
  791. * tell us the location of a shared mapping
  792. */
  793. if (file && file->f_op->get_unmapped_area) {
  794. addr = file->f_op->get_unmapped_area(file, addr, len,
  795. pgoff, flags);
  796. if (IS_ERR((void *) addr)) {
  797. ret = addr;
  798. if (ret != (unsigned long) -ENOSYS)
  799. goto error;
  800. /* the driver refused to tell us where to site
  801. * the mapping so we'll have to attempt to copy
  802. * it */
  803. ret = (unsigned long) -ENODEV;
  804. if (!(capabilities & BDI_CAP_MAP_COPY))
  805. goto error;
  806. capabilities &= ~BDI_CAP_MAP_DIRECT;
  807. }
  808. }
  809. }
  810. /* we're going to need a VMA struct as well */
  811. vma = kzalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  812. if (!vma)
  813. goto error_getting_vma;
  814. INIT_LIST_HEAD(&vma->anon_vma_node);
  815. atomic_set(&vma->vm_usage, 1);
  816. if (file)
  817. get_file(file);
  818. vma->vm_file = file;
  819. vma->vm_flags = vm_flags;
  820. vma->vm_start = addr;
  821. vma->vm_end = addr + len;
  822. vma->vm_pgoff = pgoff;
  823. vml->vma = vma;
  824. /* set up the mapping */
  825. if (file && vma->vm_flags & VM_SHARED)
  826. ret = do_mmap_shared_file(vma, len);
  827. else
  828. ret = do_mmap_private(vma, len);
  829. if (ret < 0)
  830. goto error;
  831. /* okay... we have a mapping; now we have to register it */
  832. result = (void *) vma->vm_start;
  833. if (vma->vm_flags & VM_MAPPED_COPY) {
  834. realalloc += kobjsize(result);
  835. askedalloc += len;
  836. }
  837. realalloc += kobjsize(vma);
  838. askedalloc += sizeof(*vma);
  839. current->mm->total_vm += len >> PAGE_SHIFT;
  840. add_nommu_vma(vma);
  841. shared:
  842. realalloc += kobjsize(vml);
  843. askedalloc += sizeof(*vml);
  844. add_vma_to_mm(current->mm, vml);
  845. up_write(&nommu_vma_sem);
  846. if (prot & PROT_EXEC)
  847. flush_icache_range((unsigned long) result,
  848. (unsigned long) result + len);
  849. #ifdef DEBUG
  850. printk("do_mmap:\n");
  851. show_process_blocks();
  852. #endif
  853. return (unsigned long) result;
  854. error:
  855. up_write(&nommu_vma_sem);
  856. kfree(vml);
  857. if (vma) {
  858. if (vma->vm_file)
  859. fput(vma->vm_file);
  860. kfree(vma);
  861. }
  862. return ret;
  863. sharing_violation:
  864. up_write(&nommu_vma_sem);
  865. printk("Attempt to share mismatched mappings\n");
  866. kfree(vml);
  867. return -EINVAL;
  868. error_getting_vma:
  869. up_write(&nommu_vma_sem);
  870. kfree(vml);
  871. printk("Allocation of vma for %lu byte allocation from process %d failed\n",
  872. len, current->pid);
  873. show_free_areas();
  874. return -ENOMEM;
  875. error_getting_vml:
  876. printk("Allocation of vml for %lu byte allocation from process %d failed\n",
  877. len, current->pid);
  878. show_free_areas();
  879. return -ENOMEM;
  880. }
  881. EXPORT_SYMBOL(do_mmap_pgoff);
  882. /*
  883. * handle mapping disposal for uClinux
  884. */
  885. static void put_vma(struct vm_area_struct *vma)
  886. {
  887. if (vma) {
  888. down_write(&nommu_vma_sem);
  889. if (atomic_dec_and_test(&vma->vm_usage)) {
  890. delete_nommu_vma(vma);
  891. if (vma->vm_ops && vma->vm_ops->close)
  892. vma->vm_ops->close(vma);
  893. /* IO memory and memory shared directly out of the pagecache from
  894. * ramfs/tmpfs mustn't be released here */
  895. if (vma->vm_flags & VM_MAPPED_COPY) {
  896. realalloc -= kobjsize((void *) vma->vm_start);
  897. askedalloc -= vma->vm_end - vma->vm_start;
  898. kfree((void *) vma->vm_start);
  899. }
  900. realalloc -= kobjsize(vma);
  901. askedalloc -= sizeof(*vma);
  902. if (vma->vm_file)
  903. fput(vma->vm_file);
  904. kfree(vma);
  905. }
  906. up_write(&nommu_vma_sem);
  907. }
  908. }
  909. /*
  910. * release a mapping
  911. * - under NOMMU conditions the parameters must match exactly to the mapping to
  912. * be removed
  913. */
  914. int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
  915. {
  916. struct vm_list_struct *vml, **parent;
  917. unsigned long end = addr + len;
  918. #ifdef DEBUG
  919. printk("do_munmap:\n");
  920. #endif
  921. for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next) {
  922. if ((*parent)->vma->vm_start > addr)
  923. break;
  924. if ((*parent)->vma->vm_start == addr &&
  925. ((len == 0) || ((*parent)->vma->vm_end == end)))
  926. goto found;
  927. }
  928. printk("munmap of non-mmaped memory by process %d (%s): %p\n",
  929. current->pid, current->comm, (void *) addr);
  930. return -EINVAL;
  931. found:
  932. vml = *parent;
  933. put_vma(vml->vma);
  934. *parent = vml->next;
  935. realalloc -= kobjsize(vml);
  936. askedalloc -= sizeof(*vml);
  937. kfree(vml);
  938. update_hiwater_vm(mm);
  939. mm->total_vm -= len >> PAGE_SHIFT;
  940. #ifdef DEBUG
  941. show_process_blocks();
  942. #endif
  943. return 0;
  944. }
  945. EXPORT_SYMBOL(do_munmap);
  946. asmlinkage long sys_munmap(unsigned long addr, size_t len)
  947. {
  948. int ret;
  949. struct mm_struct *mm = current->mm;
  950. down_write(&mm->mmap_sem);
  951. ret = do_munmap(mm, addr, len);
  952. up_write(&mm->mmap_sem);
  953. return ret;
  954. }
  955. /*
  956. * Release all mappings
  957. */
  958. void exit_mmap(struct mm_struct * mm)
  959. {
  960. struct vm_list_struct *tmp;
  961. if (mm) {
  962. #ifdef DEBUG
  963. printk("Exit_mmap:\n");
  964. #endif
  965. mm->total_vm = 0;
  966. while ((tmp = mm->context.vmlist)) {
  967. mm->context.vmlist = tmp->next;
  968. put_vma(tmp->vma);
  969. realalloc -= kobjsize(tmp);
  970. askedalloc -= sizeof(*tmp);
  971. kfree(tmp);
  972. }
  973. #ifdef DEBUG
  974. show_process_blocks();
  975. #endif
  976. }
  977. }
  978. unsigned long do_brk(unsigned long addr, unsigned long len)
  979. {
  980. return -ENOMEM;
  981. }
  982. /*
  983. * expand (or shrink) an existing mapping, potentially moving it at the same
  984. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  985. *
  986. * under NOMMU conditions, we only permit changing a mapping's size, and only
  987. * as long as it stays within the hole allocated by the kmalloc() call in
  988. * do_mmap_pgoff() and the block is not shareable
  989. *
  990. * MREMAP_FIXED is not supported under NOMMU conditions
  991. */
  992. unsigned long do_mremap(unsigned long addr,
  993. unsigned long old_len, unsigned long new_len,
  994. unsigned long flags, unsigned long new_addr)
  995. {
  996. struct vm_area_struct *vma;
  997. /* insanity checks first */
  998. if (new_len == 0)
  999. return (unsigned long) -EINVAL;
  1000. if (flags & MREMAP_FIXED && new_addr != addr)
  1001. return (unsigned long) -EINVAL;
  1002. vma = find_vma_exact(current->mm, addr);
  1003. if (!vma)
  1004. return (unsigned long) -EINVAL;
  1005. if (vma->vm_end != vma->vm_start + old_len)
  1006. return (unsigned long) -EFAULT;
  1007. if (vma->vm_flags & VM_MAYSHARE)
  1008. return (unsigned long) -EPERM;
  1009. if (new_len > kobjsize((void *) addr))
  1010. return (unsigned long) -ENOMEM;
  1011. /* all checks complete - do it */
  1012. vma->vm_end = vma->vm_start + new_len;
  1013. askedalloc -= old_len;
  1014. askedalloc += new_len;
  1015. return vma->vm_start;
  1016. }
  1017. EXPORT_SYMBOL(do_mremap);
  1018. asmlinkage unsigned long sys_mremap(unsigned long addr,
  1019. unsigned long old_len, unsigned long new_len,
  1020. unsigned long flags, unsigned long new_addr)
  1021. {
  1022. unsigned long ret;
  1023. down_write(&current->mm->mmap_sem);
  1024. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1025. up_write(&current->mm->mmap_sem);
  1026. return ret;
  1027. }
  1028. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  1029. unsigned int foll_flags)
  1030. {
  1031. return NULL;
  1032. }
  1033. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  1034. unsigned long to, unsigned long size, pgprot_t prot)
  1035. {
  1036. vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL(remap_pfn_range);
  1040. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1041. unsigned long pgoff)
  1042. {
  1043. unsigned int size = vma->vm_end - vma->vm_start;
  1044. if (!(vma->vm_flags & VM_USERMAP))
  1045. return -EINVAL;
  1046. vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
  1047. vma->vm_end = vma->vm_start + size;
  1048. return 0;
  1049. }
  1050. EXPORT_SYMBOL(remap_vmalloc_range);
  1051. void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1052. {
  1053. }
  1054. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  1055. unsigned long len, unsigned long pgoff, unsigned long flags)
  1056. {
  1057. return -ENOMEM;
  1058. }
  1059. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1060. {
  1061. }
  1062. void unmap_mapping_range(struct address_space *mapping,
  1063. loff_t const holebegin, loff_t const holelen,
  1064. int even_cows)
  1065. {
  1066. }
  1067. EXPORT_SYMBOL(unmap_mapping_range);
  1068. /*
  1069. * ask for an unmapped area at which to create a mapping on a file
  1070. */
  1071. unsigned long get_unmapped_area(struct file *file, unsigned long addr,
  1072. unsigned long len, unsigned long pgoff,
  1073. unsigned long flags)
  1074. {
  1075. unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
  1076. unsigned long, unsigned long);
  1077. get_area = current->mm->get_unmapped_area;
  1078. if (file && file->f_op && file->f_op->get_unmapped_area)
  1079. get_area = file->f_op->get_unmapped_area;
  1080. if (!get_area)
  1081. return -ENOSYS;
  1082. return get_area(file, addr, len, pgoff, flags);
  1083. }
  1084. EXPORT_SYMBOL(get_unmapped_area);
  1085. /*
  1086. * Check that a process has enough memory to allocate a new virtual
  1087. * mapping. 0 means there is enough memory for the allocation to
  1088. * succeed and -ENOMEM implies there is not.
  1089. *
  1090. * We currently support three overcommit policies, which are set via the
  1091. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  1092. *
  1093. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  1094. * Additional code 2002 Jul 20 by Robert Love.
  1095. *
  1096. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1097. *
  1098. * Note this is a helper function intended to be used by LSMs which
  1099. * wish to use this logic.
  1100. */
  1101. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  1102. {
  1103. unsigned long free, allowed;
  1104. vm_acct_memory(pages);
  1105. /*
  1106. * Sometimes we want to use more memory than we have
  1107. */
  1108. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1109. return 0;
  1110. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1111. unsigned long n;
  1112. free = global_page_state(NR_FILE_PAGES);
  1113. free += nr_swap_pages;
  1114. /*
  1115. * Any slabs which are created with the
  1116. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1117. * which are reclaimable, under pressure. The dentry
  1118. * cache and most inode caches should fall into this
  1119. */
  1120. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1121. /*
  1122. * Leave the last 3% for root
  1123. */
  1124. if (!cap_sys_admin)
  1125. free -= free / 32;
  1126. if (free > pages)
  1127. return 0;
  1128. /*
  1129. * nr_free_pages() is very expensive on large systems,
  1130. * only call if we're about to fail.
  1131. */
  1132. n = nr_free_pages();
  1133. /*
  1134. * Leave reserved pages. The pages are not for anonymous pages.
  1135. */
  1136. if (n <= totalreserve_pages)
  1137. goto error;
  1138. else
  1139. n -= totalreserve_pages;
  1140. /*
  1141. * Leave the last 3% for root
  1142. */
  1143. if (!cap_sys_admin)
  1144. n -= n / 32;
  1145. free += n;
  1146. if (free > pages)
  1147. return 0;
  1148. goto error;
  1149. }
  1150. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1151. /*
  1152. * Leave the last 3% for root
  1153. */
  1154. if (!cap_sys_admin)
  1155. allowed -= allowed / 32;
  1156. allowed += total_swap_pages;
  1157. /* Don't let a single process grow too big:
  1158. leave 3% of the size of this process for other processes */
  1159. allowed -= current->mm->total_vm / 32;
  1160. /*
  1161. * cast `allowed' as a signed long because vm_committed_space
  1162. * sometimes has a negative value
  1163. */
  1164. if (atomic_read(&vm_committed_space) < (long)allowed)
  1165. return 0;
  1166. error:
  1167. vm_unacct_memory(pages);
  1168. return -ENOMEM;
  1169. }
  1170. int in_gate_area_no_task(unsigned long addr)
  1171. {
  1172. return 0;
  1173. }
  1174. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1175. {
  1176. BUG();
  1177. return 0;
  1178. }
  1179. EXPORT_SYMBOL(filemap_fault);
  1180. /*
  1181. * Access another process' address space.
  1182. * - source/target buffer must be kernel space
  1183. */
  1184. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1185. {
  1186. struct vm_area_struct *vma;
  1187. struct mm_struct *mm;
  1188. if (addr + len < addr)
  1189. return 0;
  1190. mm = get_task_mm(tsk);
  1191. if (!mm)
  1192. return 0;
  1193. down_read(&mm->mmap_sem);
  1194. /* the access must start within one of the target process's mappings */
  1195. vma = find_vma(mm, addr);
  1196. if (vma) {
  1197. /* don't overrun this mapping */
  1198. if (addr + len >= vma->vm_end)
  1199. len = vma->vm_end - addr;
  1200. /* only read or write mappings where it is permitted */
  1201. if (write && vma->vm_flags & VM_MAYWRITE)
  1202. len -= copy_to_user((void *) addr, buf, len);
  1203. else if (!write && vma->vm_flags & VM_MAYREAD)
  1204. len -= copy_from_user(buf, (void *) addr, len);
  1205. else
  1206. len = 0;
  1207. } else {
  1208. len = 0;
  1209. }
  1210. up_read(&mm->mmap_sem);
  1211. mmput(mm);
  1212. return len;
  1213. }