filemap_xip.c 10 KB

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