filemap_xip.c 10 KB

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