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