filemap_xip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /*
  142. * __xip_unmap is invoked from xip_unmap and
  143. * xip_write
  144. *
  145. * This function walks all vmas of the address_space and unmaps the
  146. * __xip_sparse_page when found at pgoff.
  147. */
  148. static void
  149. __xip_unmap (struct address_space * mapping,
  150. unsigned long pgoff)
  151. {
  152. struct vm_area_struct *vma;
  153. struct mm_struct *mm;
  154. struct prio_tree_iter iter;
  155. unsigned long address;
  156. pte_t *pte;
  157. pte_t pteval;
  158. spinlock_t *ptl;
  159. struct page *page;
  160. page = __xip_sparse_page;
  161. if (!page)
  162. return;
  163. spin_lock(&mapping->i_mmap_lock);
  164. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  165. mm = vma->vm_mm;
  166. address = vma->vm_start +
  167. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  168. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  169. pte = page_check_address(page, mm, address, &ptl);
  170. if (pte) {
  171. /* Nuke the page table entry. */
  172. flush_cache_page(vma, address, pte_pfn(*pte));
  173. pteval = ptep_clear_flush(vma, address, pte);
  174. page_remove_rmap(page, vma);
  175. dec_mm_counter(mm, file_rss);
  176. BUG_ON(pte_dirty(pteval));
  177. pte_unmap_unlock(pte, ptl);
  178. page_cache_release(page);
  179. }
  180. }
  181. spin_unlock(&mapping->i_mmap_lock);
  182. }
  183. /*
  184. * xip_nopage() is invoked via the vma operations vector for a
  185. * mapped memory region to read in file data during a page fault.
  186. *
  187. * This function is derived from filemap_nopage, but used for execute in place
  188. */
  189. static struct page *
  190. xip_file_nopage(struct vm_area_struct * area,
  191. unsigned long address,
  192. int *type)
  193. {
  194. struct file *file = area->vm_file;
  195. struct address_space *mapping = file->f_mapping;
  196. struct inode *inode = mapping->host;
  197. struct page *page;
  198. unsigned long size, pgoff, endoff;
  199. pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT)
  200. + area->vm_pgoff;
  201. endoff = ((area->vm_end - area->vm_start) >> PAGE_CACHE_SHIFT)
  202. + area->vm_pgoff;
  203. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  204. if (pgoff >= size)
  205. return NOPAGE_SIGBUS;
  206. page = mapping->a_ops->get_xip_page(mapping, pgoff*(PAGE_SIZE/512), 0);
  207. if (!IS_ERR(page))
  208. goto out;
  209. if (PTR_ERR(page) != -ENODATA)
  210. return NOPAGE_SIGBUS;
  211. /* sparse block */
  212. if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  213. (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
  214. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  215. /* maybe shared writable, allocate new block */
  216. page = mapping->a_ops->get_xip_page (mapping,
  217. pgoff*(PAGE_SIZE/512), 1);
  218. if (IS_ERR(page))
  219. return NOPAGE_SIGBUS;
  220. /* unmap page at pgoff from all other vmas */
  221. __xip_unmap(mapping, pgoff);
  222. } else {
  223. /* not shared and writable, use xip_sparse_page() */
  224. page = xip_sparse_page();
  225. if (!page)
  226. return NOPAGE_OOM;
  227. }
  228. out:
  229. page_cache_get(page);
  230. return page;
  231. }
  232. static struct vm_operations_struct xip_file_vm_ops = {
  233. .nopage = xip_file_nopage,
  234. };
  235. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  236. {
  237. BUG_ON(!file->f_mapping->a_ops->get_xip_page);
  238. file_accessed(file);
  239. vma->vm_ops = &xip_file_vm_ops;
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(xip_file_mmap);
  243. static ssize_t
  244. __xip_file_write(struct file *filp, const char __user *buf,
  245. size_t count, loff_t pos, loff_t *ppos)
  246. {
  247. struct address_space * mapping = filp->f_mapping;
  248. const struct address_space_operations *a_ops = mapping->a_ops;
  249. struct inode *inode = mapping->host;
  250. long status = 0;
  251. struct page *page;
  252. size_t bytes;
  253. ssize_t written = 0;
  254. BUG_ON(!mapping->a_ops->get_xip_page);
  255. do {
  256. unsigned long index;
  257. unsigned long offset;
  258. size_t copied;
  259. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  260. index = pos >> PAGE_CACHE_SHIFT;
  261. bytes = PAGE_CACHE_SIZE - offset;
  262. if (bytes > count)
  263. bytes = count;
  264. /*
  265. * Bring in the user page that we will copy from _first_.
  266. * Otherwise there's a nasty deadlock on copying from the
  267. * same page as we're writing to, without it being marked
  268. * up-to-date.
  269. */
  270. fault_in_pages_readable(buf, bytes);
  271. page = a_ops->get_xip_page(mapping,
  272. index*(PAGE_SIZE/512), 0);
  273. if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
  274. /* we allocate a new page unmap it */
  275. page = a_ops->get_xip_page(mapping,
  276. index*(PAGE_SIZE/512), 1);
  277. if (!IS_ERR(page))
  278. /* unmap page at pgoff from all other vmas */
  279. __xip_unmap(mapping, index);
  280. }
  281. if (IS_ERR(page)) {
  282. status = PTR_ERR(page);
  283. break;
  284. }
  285. copied = filemap_copy_from_user(page, offset, buf, bytes);
  286. flush_dcache_page(page);
  287. if (likely(copied > 0)) {
  288. status = copied;
  289. if (status >= 0) {
  290. written += status;
  291. count -= status;
  292. pos += status;
  293. buf += status;
  294. }
  295. }
  296. if (unlikely(copied != bytes))
  297. if (status >= 0)
  298. status = -EFAULT;
  299. if (status < 0)
  300. break;
  301. } while (count);
  302. *ppos = pos;
  303. /*
  304. * No need to use i_size_read() here, the i_size
  305. * cannot change under us because we hold i_mutex.
  306. */
  307. if (pos > inode->i_size) {
  308. i_size_write(inode, pos);
  309. mark_inode_dirty(inode);
  310. }
  311. return written ? written : status;
  312. }
  313. ssize_t
  314. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  315. loff_t *ppos)
  316. {
  317. struct address_space *mapping = filp->f_mapping;
  318. struct inode *inode = mapping->host;
  319. size_t count;
  320. loff_t pos;
  321. ssize_t ret;
  322. mutex_lock(&inode->i_mutex);
  323. if (!access_ok(VERIFY_READ, buf, len)) {
  324. ret=-EFAULT;
  325. goto out_up;
  326. }
  327. pos = *ppos;
  328. count = len;
  329. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  330. /* We can write back this queue in page reclaim */
  331. current->backing_dev_info = mapping->backing_dev_info;
  332. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  333. if (ret)
  334. goto out_backing;
  335. if (count == 0)
  336. goto out_backing;
  337. ret = remove_suid(filp->f_path.dentry);
  338. if (ret)
  339. goto out_backing;
  340. file_update_time(filp);
  341. ret = __xip_file_write (filp, buf, count, pos, ppos);
  342. out_backing:
  343. current->backing_dev_info = NULL;
  344. out_up:
  345. mutex_unlock(&inode->i_mutex);
  346. return ret;
  347. }
  348. EXPORT_SYMBOL_GPL(xip_file_write);
  349. /*
  350. * truncate a page used for execute in place
  351. * functionality is analog to block_truncate_page but does use get_xip_page
  352. * to get the page instead of page cache
  353. */
  354. int
  355. xip_truncate_page(struct address_space *mapping, loff_t from)
  356. {
  357. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  358. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  359. unsigned blocksize;
  360. unsigned length;
  361. struct page *page;
  362. BUG_ON(!mapping->a_ops->get_xip_page);
  363. blocksize = 1 << mapping->host->i_blkbits;
  364. length = offset & (blocksize - 1);
  365. /* Block boundary? Nothing to do */
  366. if (!length)
  367. return 0;
  368. length = blocksize - length;
  369. page = mapping->a_ops->get_xip_page(mapping,
  370. index*(PAGE_SIZE/512), 0);
  371. if (!page)
  372. return -ENOMEM;
  373. if (unlikely(IS_ERR(page))) {
  374. if (PTR_ERR(page) == -ENODATA)
  375. /* Hole? No need to truncate */
  376. return 0;
  377. else
  378. return PTR_ERR(page);
  379. }
  380. zero_user_page(page, offset, length, KM_USER0);
  381. return 0;
  382. }
  383. EXPORT_SYMBOL_GPL(xip_truncate_page);