filemap_xip.c 10 KB

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