filemap_xip.c 11 KB

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