nommu.c 35 KB

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