nommu.c 35 KB

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