nommu.c 35 KB

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