filemap_xip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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/export.h>
  13. #include <linux/uio.h>
  14. #include <linux/rmap.h>
  15. #include <linux/mmu_notifier.h>
  16. #include <linux/sched.h>
  17. #include <linux/seqlock.h>
  18. #include <linux/mutex.h>
  19. #include <linux/gfp.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/io.h>
  22. /*
  23. * We do use our own empty page to avoid interference with other users
  24. * of ZERO_PAGE(), such as /dev/zero
  25. */
  26. static DEFINE_MUTEX(xip_sparse_mutex);
  27. static seqcount_t xip_sparse_seq = SEQCNT_ZERO(xip_sparse_seq);
  28. static struct page *__xip_sparse_page;
  29. /* called under xip_sparse_mutex */
  30. static struct page *xip_sparse_page(void)
  31. {
  32. if (!__xip_sparse_page) {
  33. struct page *page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
  34. if (page)
  35. __xip_sparse_page = page;
  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 - copied)
  84. nr = len - copied;
  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 xip_write
  139. *
  140. * This function walks all vmas of the address_space and unmaps the
  141. * __xip_sparse_page when found at pgoff.
  142. */
  143. static void __xip_unmap(struct address_space * mapping, unsigned long pgoff)
  144. {
  145. struct vm_area_struct *vma;
  146. struct page *page;
  147. unsigned count;
  148. int locked = 0;
  149. count = read_seqcount_begin(&xip_sparse_seq);
  150. page = __xip_sparse_page;
  151. if (!page)
  152. return;
  153. retry:
  154. i_mmap_lock_read(mapping);
  155. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  156. pte_t *pte, pteval;
  157. spinlock_t *ptl;
  158. struct mm_struct *mm = vma->vm_mm;
  159. unsigned long address = vma->vm_start +
  160. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  161. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  162. pte = page_check_address(page, mm, address, &ptl, 1);
  163. if (pte) {
  164. /* Nuke the page table entry. */
  165. flush_cache_page(vma, address, pte_pfn(*pte));
  166. pteval = ptep_clear_flush(vma, address, pte);
  167. page_remove_rmap(page);
  168. dec_mm_counter(mm, MM_FILEPAGES);
  169. BUG_ON(pte_dirty(pteval));
  170. pte_unmap_unlock(pte, ptl);
  171. /* must invalidate_page _before_ freeing the page */
  172. mmu_notifier_invalidate_page(mm, address);
  173. page_cache_release(page);
  174. }
  175. }
  176. i_mmap_unlock_read(mapping);
  177. if (locked) {
  178. mutex_unlock(&xip_sparse_mutex);
  179. } else if (read_seqcount_retry(&xip_sparse_seq, count)) {
  180. mutex_lock(&xip_sparse_mutex);
  181. locked = 1;
  182. goto retry;
  183. }
  184. }
  185. /*
  186. * xip_fault() is invoked via the vma operations vector for a
  187. * mapped memory region to read in file data during a page fault.
  188. *
  189. * This function is derived from filemap_fault, but used for execute in place
  190. */
  191. static int xip_file_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  192. {
  193. struct file *file = vma->vm_file;
  194. struct address_space *mapping = file->f_mapping;
  195. struct inode *inode = mapping->host;
  196. pgoff_t size;
  197. void *xip_mem;
  198. unsigned long xip_pfn;
  199. struct page *page;
  200. int error;
  201. /* XXX: are VM_FAULT_ codes OK? */
  202. again:
  203. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  204. if (vmf->pgoff >= size)
  205. return VM_FAULT_SIGBUS;
  206. error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0,
  207. &xip_mem, &xip_pfn);
  208. if (likely(!error))
  209. goto found;
  210. if (error != -ENODATA)
  211. return VM_FAULT_OOM;
  212. /* sparse block */
  213. if ((vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  214. (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) &&
  215. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  216. int err;
  217. /* maybe shared writable, allocate new block */
  218. mutex_lock(&xip_sparse_mutex);
  219. error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 1,
  220. &xip_mem, &xip_pfn);
  221. mutex_unlock(&xip_sparse_mutex);
  222. if (error)
  223. return VM_FAULT_SIGBUS;
  224. /* unmap sparse mappings at pgoff from all other vmas */
  225. __xip_unmap(mapping, vmf->pgoff);
  226. found:
  227. err = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
  228. xip_pfn);
  229. if (err == -ENOMEM)
  230. return VM_FAULT_OOM;
  231. /*
  232. * err == -EBUSY is fine, we've raced against another thread
  233. * that faulted-in the same page
  234. */
  235. if (err != -EBUSY)
  236. BUG_ON(err);
  237. return VM_FAULT_NOPAGE;
  238. } else {
  239. int err, ret = VM_FAULT_OOM;
  240. mutex_lock(&xip_sparse_mutex);
  241. write_seqcount_begin(&xip_sparse_seq);
  242. error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0,
  243. &xip_mem, &xip_pfn);
  244. if (unlikely(!error)) {
  245. write_seqcount_end(&xip_sparse_seq);
  246. mutex_unlock(&xip_sparse_mutex);
  247. goto again;
  248. }
  249. if (error != -ENODATA)
  250. goto out;
  251. /* not shared and writable, use xip_sparse_page() */
  252. page = xip_sparse_page();
  253. if (!page)
  254. goto out;
  255. err = vm_insert_page(vma, (unsigned long)vmf->virtual_address,
  256. page);
  257. if (err == -ENOMEM)
  258. goto out;
  259. ret = VM_FAULT_NOPAGE;
  260. out:
  261. write_seqcount_end(&xip_sparse_seq);
  262. mutex_unlock(&xip_sparse_mutex);
  263. return ret;
  264. }
  265. }
  266. static const struct vm_operations_struct xip_file_vm_ops = {
  267. .fault = xip_file_fault,
  268. .page_mkwrite = filemap_page_mkwrite,
  269. .remap_pages = generic_file_remap_pages,
  270. };
  271. int xip_file_mmap(struct file * file, struct vm_area_struct * vma)
  272. {
  273. BUG_ON(!file->f_mapping->a_ops->get_xip_mem);
  274. file_accessed(file);
  275. vma->vm_ops = &xip_file_vm_ops;
  276. vma->vm_flags |= VM_MIXEDMAP;
  277. return 0;
  278. }
  279. EXPORT_SYMBOL_GPL(xip_file_mmap);
  280. static ssize_t
  281. __xip_file_write(struct file *filp, const char __user *buf,
  282. size_t count, loff_t pos, loff_t *ppos)
  283. {
  284. struct address_space * mapping = filp->f_mapping;
  285. const struct address_space_operations *a_ops = mapping->a_ops;
  286. struct inode *inode = mapping->host;
  287. long status = 0;
  288. size_t bytes;
  289. ssize_t written = 0;
  290. BUG_ON(!mapping->a_ops->get_xip_mem);
  291. do {
  292. unsigned long index;
  293. unsigned long offset;
  294. size_t copied;
  295. void *xip_mem;
  296. unsigned long xip_pfn;
  297. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  298. index = pos >> PAGE_CACHE_SHIFT;
  299. bytes = PAGE_CACHE_SIZE - offset;
  300. if (bytes > count)
  301. bytes = count;
  302. status = a_ops->get_xip_mem(mapping, index, 0,
  303. &xip_mem, &xip_pfn);
  304. if (status == -ENODATA) {
  305. /* we allocate a new page unmap it */
  306. mutex_lock(&xip_sparse_mutex);
  307. status = a_ops->get_xip_mem(mapping, index, 1,
  308. &xip_mem, &xip_pfn);
  309. mutex_unlock(&xip_sparse_mutex);
  310. if (!status)
  311. /* unmap page at pgoff from all other vmas */
  312. __xip_unmap(mapping, index);
  313. }
  314. if (status)
  315. break;
  316. copied = bytes -
  317. __copy_from_user_nocache(xip_mem + offset, buf, bytes);
  318. if (likely(copied > 0)) {
  319. status = copied;
  320. if (status >= 0) {
  321. written += status;
  322. count -= status;
  323. pos += status;
  324. buf += status;
  325. }
  326. }
  327. if (unlikely(copied != bytes))
  328. if (status >= 0)
  329. status = -EFAULT;
  330. if (status < 0)
  331. break;
  332. } while (count);
  333. *ppos = pos;
  334. /*
  335. * No need to use i_size_read() here, the i_size
  336. * cannot change under us because we hold i_mutex.
  337. */
  338. if (pos > inode->i_size) {
  339. i_size_write(inode, pos);
  340. mark_inode_dirty(inode);
  341. }
  342. return written ? written : status;
  343. }
  344. ssize_t
  345. xip_file_write(struct file *filp, const char __user *buf, size_t len,
  346. loff_t *ppos)
  347. {
  348. struct address_space *mapping = filp->f_mapping;
  349. struct inode *inode = mapping->host;
  350. size_t count;
  351. loff_t pos;
  352. ssize_t ret;
  353. mutex_lock(&inode->i_mutex);
  354. if (!access_ok(VERIFY_READ, buf, len)) {
  355. ret=-EFAULT;
  356. goto out_up;
  357. }
  358. pos = *ppos;
  359. count = len;
  360. /* We can write back this queue in page reclaim */
  361. current->backing_dev_info = mapping->backing_dev_info;
  362. ret = generic_write_checks(filp, &pos, &count, S_ISBLK(inode->i_mode));
  363. if (ret)
  364. goto out_backing;
  365. if (count == 0)
  366. goto out_backing;
  367. ret = file_remove_suid(filp);
  368. if (ret)
  369. goto out_backing;
  370. ret = file_update_time(filp);
  371. if (ret)
  372. goto out_backing;
  373. ret = __xip_file_write (filp, buf, count, pos, ppos);
  374. out_backing:
  375. current->backing_dev_info = NULL;
  376. out_up:
  377. mutex_unlock(&inode->i_mutex);
  378. return ret;
  379. }
  380. EXPORT_SYMBOL_GPL(xip_file_write);
  381. /*
  382. * truncate a page used for execute in place
  383. * functionality is analog to block_truncate_page but does use get_xip_mem
  384. * to get the page instead of page cache
  385. */
  386. int
  387. xip_truncate_page(struct address_space *mapping, loff_t from)
  388. {
  389. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  390. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  391. unsigned blocksize;
  392. unsigned length;
  393. void *xip_mem;
  394. unsigned long xip_pfn;
  395. int err;
  396. BUG_ON(!mapping->a_ops->get_xip_mem);
  397. blocksize = 1 << mapping->host->i_blkbits;
  398. length = offset & (blocksize - 1);
  399. /* Block boundary? Nothing to do */
  400. if (!length)
  401. return 0;
  402. length = blocksize - length;
  403. err = mapping->a_ops->get_xip_mem(mapping, index, 0,
  404. &xip_mem, &xip_pfn);
  405. if (unlikely(err)) {
  406. if (err == -ENODATA)
  407. /* Hole? No need to truncate */
  408. return 0;
  409. else
  410. return err;
  411. }
  412. memset(xip_mem + offset, 0, length);
  413. return 0;
  414. }
  415. EXPORT_SYMBOL_GPL(xip_truncate_page);