filemap_xip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * linux/mm/filemap_xip.c
  3. *
  4. * Copyright (C) 2005 IBM Corporation
  5. * Author: Carsten Otte <cotte@de.ibm.com>
  6. *
  7. * derived from linux/mm/filemap.c - Copyright (C) Linus Torvalds
  8. *
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/module.h>
  13. #include <linux/uio.h>
  14. #include <linux/rmap.h>
  15. #include <linux/sched.h>
  16. #include <asm/tlbflush.h>
  17. #include "filemap.h"
  18. /*
  19. * We do use our own empty page to avoid interference with other users
  20. * of ZERO_PAGE(), such as /dev/zero
  21. */
  22. static struct page *__xip_sparse_page;
  23. static struct page *xip_sparse_page(void)
  24. {
  25. if (!__xip_sparse_page) {
  26. unsigned long zeroes = get_zeroed_page(GFP_HIGHUSER);
  27. if (zeroes) {
  28. static DEFINE_SPINLOCK(xip_alloc_lock);
  29. spin_lock(&xip_alloc_lock);
  30. if (!__xip_sparse_page)
  31. __xip_sparse_page = virt_to_page(zeroes);
  32. else
  33. free_page(zeroes);
  34. spin_unlock(&xip_alloc_lock);
  35. }
  36. }
  37. return __xip_sparse_page;
  38. }
  39. /*
  40. * This is a file read routine for execute in place files, and uses
  41. * the mapping->a_ops->get_xip_page() function for the actual low-level
  42. * stuff.
  43. *
  44. * Note the struct file* is not used at all. It may be NULL.
  45. */
  46. static void
  47. do_xip_mapping_read(struct address_space *mapping,
  48. struct file_ra_state *_ra,
  49. struct file *filp,
  50. loff_t *ppos,
  51. read_descriptor_t *desc,
  52. read_actor_t actor)
  53. {
  54. struct inode *inode = mapping->host;
  55. unsigned long index, end_index, offset;
  56. loff_t isize;
  57. BUG_ON(!mapping->a_ops->get_xip_page);
  58. index = *ppos >> PAGE_CACHE_SHIFT;
  59. offset = *ppos & ~PAGE_CACHE_MASK;
  60. isize = i_size_read(inode);
  61. if (!isize)
  62. goto out;
  63. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  64. for (;;) {
  65. struct page *page;
  66. unsigned long nr, ret;
  67. /* nr is the maximum number of bytes to copy from this page */
  68. nr = PAGE_CACHE_SIZE;
  69. if (index >= end_index) {
  70. if (index > end_index)
  71. goto out;
  72. nr = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  73. if (nr <= offset) {
  74. goto out;
  75. }
  76. }
  77. nr = nr - offset;
  78. page = mapping->a_ops->get_xip_page(mapping,
  79. index*(PAGE_SIZE/512), 0);
  80. if (!page)
  81. goto no_xip_page;
  82. if (unlikely(IS_ERR(page))) {
  83. if (PTR_ERR(page) == -ENODATA) {
  84. /* sparse */
  85. page = ZERO_PAGE(0);
  86. } else {
  87. desc->error = PTR_ERR(page);
  88. goto out;
  89. }
  90. }
  91. /* If users can be writing to this page using arbitrary
  92. * virtual addresses, take care about potential aliasing
  93. * before reading the page on the kernel side.
  94. */
  95. if (mapping_writably_mapped(mapping))
  96. flush_dcache_page(page);
  97. /*
  98. * Ok, we have the page, so now we can copy it to user space...
  99. *
  100. * The actor routine returns how many bytes were actually used..
  101. * NOTE! This may not be the same as how much of a user buffer
  102. * we filled up (we may be padding etc), so we can only update
  103. * "pos" here (the actor routine has to update the user buffer
  104. * pointers and the remaining count).
  105. */
  106. ret = actor(desc, page, offset, nr);
  107. offset += ret;
  108. index += offset >> PAGE_CACHE_SHIFT;
  109. offset &= ~PAGE_CACHE_MASK;
  110. if (ret == nr && desc->count)
  111. continue;
  112. goto out;
  113. no_xip_page:
  114. /* Did not get the page. Report it */
  115. desc->error = -EIO;
  116. goto out;
  117. }
  118. out:
  119. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  120. if (filp)
  121. file_accessed(filp);
  122. }
  123. ssize_t
  124. xip_file_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  125. {
  126. read_descriptor_t desc;
  127. if (!access_ok(VERIFY_WRITE, buf, len))
  128. return -EFAULT;
  129. desc.written = 0;
  130. desc.arg.buf = buf;
  131. desc.count = len;
  132. desc.error = 0;
  133. do_xip_mapping_read(filp->f_mapping, &filp->f_ra, filp,
  134. ppos, &desc, file_read_actor);
  135. if (desc.written)
  136. return desc.written;
  137. else
  138. return desc.error;
  139. }
  140. EXPORT_SYMBOL_GPL(xip_file_read);
  141. ssize_t
  142. xip_file_sendfile(struct file *in_file, loff_t *ppos,
  143. size_t count, read_actor_t actor, void *target)
  144. {
  145. read_descriptor_t desc;
  146. if (!count)
  147. return 0;
  148. desc.written = 0;
  149. desc.count = count;
  150. desc.arg.data = target;
  151. desc.error = 0;
  152. do_xip_mapping_read(in_file->f_mapping, &in_file->f_ra, in_file,
  153. ppos, &desc, actor);
  154. if (desc.written)
  155. return desc.written;
  156. return desc.error;
  157. }
  158. EXPORT_SYMBOL_GPL(xip_file_sendfile);
  159. /*
  160. * __xip_unmap is invoked from xip_unmap and
  161. * xip_write
  162. *
  163. * This function walks all vmas of the address_space and unmaps the
  164. * __xip_sparse_page when found at pgoff.
  165. */
  166. static void
  167. __xip_unmap (struct address_space * mapping,
  168. unsigned long pgoff)
  169. {
  170. struct vm_area_struct *vma;
  171. struct mm_struct *mm;
  172. struct prio_tree_iter iter;
  173. unsigned long address;
  174. pte_t *pte;
  175. pte_t pteval;
  176. spinlock_t *ptl;
  177. struct page *page;
  178. page = __xip_sparse_page;
  179. if (!page)
  180. return;
  181. spin_lock(&mapping->i_mmap_lock);
  182. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  183. mm = vma->vm_mm;
  184. address = vma->vm_start +
  185. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  186. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  187. pte = page_check_address(page, mm, address, &ptl);
  188. if (pte) {
  189. /* Nuke the page table entry. */
  190. flush_cache_page(vma, address, pte_pfn(*pte));
  191. pteval = ptep_clear_flush(vma, address, pte);
  192. page_remove_rmap(page, vma);
  193. dec_mm_counter(mm, file_rss);
  194. BUG_ON(pte_dirty(pteval));
  195. pte_unmap_unlock(pte, ptl);
  196. page_cache_release(page);
  197. }
  198. }
  199. spin_unlock(&mapping->i_mmap_lock);
  200. }
  201. /*
  202. * xip_nopage() is invoked via the vma operations vector for a
  203. * mapped memory region to read in file data during a page fault.
  204. *
  205. * This function is derived from filemap_nopage, but used for execute in place
  206. */
  207. static struct page *
  208. xip_file_nopage(struct vm_area_struct * area,
  209. unsigned long address,
  210. int *type)
  211. {
  212. struct file *file = area->vm_file;
  213. struct address_space *mapping = file->f_mapping;
  214. struct inode *inode = mapping->host;
  215. struct page *page;
  216. unsigned long size, pgoff, endoff;
  217. pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
  218. + area->vm_pgoff;
  219. endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
  220. + area->vm_pgoff;
  221. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  222. if (pgoff >= size)
  223. return NOPAGE_SIGBUS;
  224. page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
  225. if (!IS_ERR(page))
  226. goto out;
  227. if (PTR_ERR(page) != -ENODATA)
  228. return NOPAGE_SIGBUS;
  229. /* sparse block */
  230. if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  231. (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
  232. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  233. /* maybe shared writable, allocate new block */
  234. page = mapping->a_ops->get_xip_page (mapping,
  235. pgoff*(PAGE_SIZE/512), 1);
  236. if (IS_ERR(page))
  237. return NOPAGE_SIGBUS;
  238. /* unmap page at pgoff from all other vmas */
  239. __xip_unmap(mapping, pgoff);
  240. } else {
  241. /* not shared and writable, use xip_sparse_page() */
  242. page = xip_sparse_page();
  243. if (!page)
  244. return NOPAGE_OOM;
  245. }
  246. out:
  247. page_cache_get(page);
  248. return page;
  249. }
  250. static struct vm_operations_struct xip_file_vm_ops = {
  251. .nopage = xip_file_nopage,
  252. };
  253. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  254. {
  255. BUG_ON(!file->f_mapping->a_ops->get_xip_page);
  256. file_accessed(file);
  257. vma->vm_ops = &xip_file_vm_ops;
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(xip_file_mmap);
  261. static ssize_t
  262. __xip_file_write(struct file *filp, const char __user *buf,
  263. size_t count, loff_t pos, loff_t *ppos)
  264. {
  265. struct address_space * mapping = filp->f_mapping;
  266. const struct address_space_operations *a_ops = mapping->a_ops;
  267. struct inode *inode = mapping->host;
  268. long status = 0;
  269. struct page *page;
  270. size_t bytes;
  271. ssize_t written = 0;
  272. BUG_ON(!mapping->a_ops->get_xip_page);
  273. do {
  274. unsigned long index;
  275. unsigned long offset;
  276. size_t copied;
  277. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  278. index = pos >> PAGE_CACHE_SHIFT;
  279. bytes = PAGE_CACHE_SIZE - offset;
  280. if (bytes > count)
  281. bytes = count;
  282. /*
  283. * Bring in the user page that we will copy from _first_.
  284. * Otherwise there's a nasty deadlock on copying from the
  285. * same page as we're writing to, without it being marked
  286. * up-to-date.
  287. */
  288. fault_in_pages_readable(buf, bytes);
  289. page = a_ops->get_xip_page(mapping,
  290. index*(PAGE_SIZE/512), 0);
  291. if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
  292. /* we allocate a new page unmap it */
  293. page = a_ops->get_xip_page(mapping,
  294. index*(PAGE_SIZE/512), 1);
  295. if (!IS_ERR(page))
  296. /* unmap page at pgoff from all other vmas */
  297. __xip_unmap(mapping, index);
  298. }
  299. if (IS_ERR(page)) {
  300. status = PTR_ERR(page);
  301. break;
  302. }
  303. copied = filemap_copy_from_user(page, offset, buf, bytes);
  304. flush_dcache_page(page);
  305. if (likely(copied > 0)) {
  306. status = copied;
  307. if (status >= 0) {
  308. written += status;
  309. count -= status;
  310. pos += status;
  311. buf += status;
  312. }
  313. }
  314. if (unlikely(copied != bytes))
  315. if (status >= 0)
  316. status = -EFAULT;
  317. if (status < 0)
  318. break;
  319. } while (count);
  320. *ppos = pos;
  321. /*
  322. * No need to use i_size_read() here, the i_size
  323. * cannot change under us because we hold i_mutex.
  324. */
  325. if (pos > inode->i_size) {
  326. i_size_write(inode, pos);
  327. mark_inode_dirty(inode);
  328. }
  329. return written ? written : status;
  330. }
  331. ssize_t
  332. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  333. loff_t *ppos)
  334. {
  335. struct address_space *mapping = filp->f_mapping;
  336. struct inode *inode = mapping->host;
  337. size_t count;
  338. loff_t pos;
  339. ssize_t ret;
  340. mutex_lock(&inode->i_mutex);
  341. if (!access_ok(VERIFY_READ, buf, len)) {
  342. ret=-EFAULT;
  343. goto out_up;
  344. }
  345. pos = *ppos;
  346. count = len;
  347. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  348. /* We can write back this queue in page reclaim */
  349. current->backing_dev_info = mapping->backing_dev_info;
  350. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  351. if (ret)
  352. goto out_backing;
  353. if (count == 0)
  354. goto out_backing;
  355. ret = remove_suid(filp->f_path.dentry);
  356. if (ret)
  357. goto out_backing;
  358. file_update_time(filp);
  359. ret = __xip_file_write (filp, buf, count, pos, ppos);
  360. out_backing:
  361. current->backing_dev_info = NULL;
  362. out_up:
  363. mutex_unlock(&inode->i_mutex);
  364. return ret;
  365. }
  366. EXPORT_SYMBOL_GPL(xip_file_write);
  367. /*
  368. * truncate a page used for execute in place
  369. * functionality is analog to block_truncate_page but does use get_xip_page
  370. * to get the page instead of page cache
  371. */
  372. int
  373. xip_truncate_page(struct address_space *mapping, loff_t from)
  374. {
  375. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  376. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  377. unsigned blocksize;
  378. unsigned length;
  379. struct page *page;
  380. BUG_ON(!mapping->a_ops->get_xip_page);
  381. blocksize = 1 << mapping->host->i_blkbits;
  382. length = offset & (blocksize - 1);
  383. /* Block boundary? Nothing to do */
  384. if (!length)
  385. return 0;
  386. length = blocksize - length;
  387. page = mapping->a_ops->get_xip_page(mapping,
  388. index*(PAGE_SIZE/512), 0);
  389. if (!page)
  390. return -ENOMEM;
  391. if (unlikely(IS_ERR(page))) {
  392. if (PTR_ERR(page) == -ENODATA)
  393. /* Hole? No need to truncate */
  394. return 0;
  395. else
  396. return PTR_ERR(page);
  397. }
  398. zero_user_page(page, offset, length, KM_USER0);
  399. return 0;
  400. }
  401. EXPORT_SYMBOL_GPL(xip_truncate_page);