filemap_xip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_fault() 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_fault, but used for execute in place
  188. */
  189. static int xip_file_fault(struct vm_area_struct *area, struct vm_fault *vmf)
  190. {
  191. struct file *file = area->vm_file;
  192. struct address_space *mapping = file->f_mapping;
  193. struct inode *inode = mapping->host;
  194. struct page *page;
  195. pgoff_t size;
  196. /* XXX: are VM_FAULT_ codes OK? */
  197. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  198. if (vmf->pgoff >= size)
  199. return VM_FAULT_SIGBUS;
  200. page = mapping->a_ops->get_xip_page(mapping,
  201. vmf->pgoff*(PAGE_SIZE/512), 0);
  202. if (!IS_ERR(page))
  203. goto out;
  204. if (PTR_ERR(page) != -ENODATA)
  205. return VM_FAULT_OOM;
  206. /* sparse block */
  207. if ((area->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  208. (area->vm_flags & (VM_SHARED| VM_MAYSHARE)) &&
  209. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  210. /* maybe shared writable, allocate new block */
  211. page = mapping->a_ops->get_xip_page(mapping,
  212. vmf->pgoff*(PAGE_SIZE/512), 1);
  213. if (IS_ERR(page))
  214. return VM_FAULT_SIGBUS;
  215. /* unmap page at pgoff from all other vmas */
  216. __xip_unmap(mapping, vmf->pgoff);
  217. } else {
  218. /* not shared and writable, use xip_sparse_page() */
  219. page = xip_sparse_page();
  220. if (!page)
  221. return VM_FAULT_OOM;
  222. }
  223. out:
  224. page_cache_get(page);
  225. vmf->page = page;
  226. return 0;
  227. }
  228. static struct vm_operations_struct xip_file_vm_ops = {
  229. .fault = xip_file_fault,
  230. };
  231. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  232. {
  233. BUG_ON(!file->f_mapping->a_ops->get_xip_page);
  234. file_accessed(file);
  235. vma->vm_ops = &xip_file_vm_ops;
  236. vma->vm_flags |= VM_CAN_NONLINEAR;
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(xip_file_mmap);
  240. static ssize_t
  241. __xip_file_write(struct file *filp, const char __user *buf,
  242. size_t count, loff_t pos, loff_t *ppos)
  243. {
  244. struct address_space * mapping = filp->f_mapping;
  245. const struct address_space_operations *a_ops = mapping->a_ops;
  246. struct inode *inode = mapping->host;
  247. long status = 0;
  248. struct page *page;
  249. size_t bytes;
  250. ssize_t written = 0;
  251. BUG_ON(!mapping->a_ops->get_xip_page);
  252. do {
  253. unsigned long index;
  254. unsigned long offset;
  255. size_t copied;
  256. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  257. index = pos >> PAGE_CACHE_SHIFT;
  258. bytes = PAGE_CACHE_SIZE - offset;
  259. if (bytes > count)
  260. bytes = count;
  261. /*
  262. * Bring in the user page that we will copy from _first_.
  263. * Otherwise there's a nasty deadlock on copying from the
  264. * same page as we're writing to, without it being marked
  265. * up-to-date.
  266. */
  267. fault_in_pages_readable(buf, bytes);
  268. page = a_ops->get_xip_page(mapping,
  269. index*(PAGE_SIZE/512), 0);
  270. if (IS_ERR(page) && (PTR_ERR(page) == -ENODATA)) {
  271. /* we allocate a new page unmap it */
  272. page = a_ops->get_xip_page(mapping,
  273. index*(PAGE_SIZE/512), 1);
  274. if (!IS_ERR(page))
  275. /* unmap page at pgoff from all other vmas */
  276. __xip_unmap(mapping, index);
  277. }
  278. if (IS_ERR(page)) {
  279. status = PTR_ERR(page);
  280. break;
  281. }
  282. copied = filemap_copy_from_user(page, offset, buf, bytes);
  283. flush_dcache_page(page);
  284. if (likely(copied > 0)) {
  285. status = copied;
  286. if (status >= 0) {
  287. written += status;
  288. count -= status;
  289. pos += status;
  290. buf += status;
  291. }
  292. }
  293. if (unlikely(copied != bytes))
  294. if (status >= 0)
  295. status = -EFAULT;
  296. if (status < 0)
  297. break;
  298. } while (count);
  299. *ppos = pos;
  300. /*
  301. * No need to use i_size_read() here, the i_size
  302. * cannot change under us because we hold i_mutex.
  303. */
  304. if (pos > inode->i_size) {
  305. i_size_write(inode, pos);
  306. mark_inode_dirty(inode);
  307. }
  308. return written ? written : status;
  309. }
  310. ssize_t
  311. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  312. loff_t *ppos)
  313. {
  314. struct address_space *mapping = filp->f_mapping;
  315. struct inode *inode = mapping->host;
  316. size_t count;
  317. loff_t pos;
  318. ssize_t ret;
  319. mutex_lock(&inode->i_mutex);
  320. if (!access_ok(VERIFY_READ, buf, len)) {
  321. ret=-EFAULT;
  322. goto out_up;
  323. }
  324. pos = *ppos;
  325. count = len;
  326. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  327. /* We can write back this queue in page reclaim */
  328. current->backing_dev_info = mapping->backing_dev_info;
  329. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  330. if (ret)
  331. goto out_backing;
  332. if (count == 0)
  333. goto out_backing;
  334. ret = remove_suid(filp->f_path.dentry);
  335. if (ret)
  336. goto out_backing;
  337. file_update_time(filp);
  338. ret = __xip_file_write (filp, buf, count, pos, ppos);
  339. out_backing:
  340. current->backing_dev_info = NULL;
  341. out_up:
  342. mutex_unlock(&inode->i_mutex);
  343. return ret;
  344. }
  345. EXPORT_SYMBOL_GPL(xip_file_write);
  346. /*
  347. * truncate a page used for execute in place
  348. * functionality is analog to block_truncate_page but does use get_xip_page
  349. * to get the page instead of page cache
  350. */
  351. int
  352. xip_truncate_page(struct address_space *mapping, loff_t from)
  353. {
  354. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  355. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  356. unsigned blocksize;
  357. unsigned length;
  358. struct page *page;
  359. BUG_ON(!mapping->a_ops->get_xip_page);
  360. blocksize = 1 << mapping->host->i_blkbits;
  361. length = offset & (blocksize - 1);
  362. /* Block boundary? Nothing to do */
  363. if (!length)
  364. return 0;
  365. length = blocksize - length;
  366. page = mapping->a_ops->get_xip_page(mapping,
  367. index*(PAGE_SIZE/512), 0);
  368. if (!page)
  369. return -ENOMEM;
  370. if (unlikely(IS_ERR(page))) {
  371. if (PTR_ERR(page) == -ENODATA)
  372. /* Hole? No need to truncate */
  373. return 0;
  374. else
  375. return PTR_ERR(page);
  376. }
  377. zero_user_page(page, offset, length, KM_USER0);
  378. return 0;
  379. }
  380. EXPORT_SYMBOL_GPL(xip_truncate_page);