nommu.c 35 KB

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