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/backing-dev.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/export.h>
  14. #include <linux/uio.h>
  15. #include <linux/rmap.h>
  16. #include <linux/mmu_notifier.h>
  17. #include <linux/sched.h>
  18. #include <linux/seqlock.h>
  19. #include <linux/mutex.h>
  20. #include <linux/gfp.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/io.h>
  23. /*
  24. * We do use our own empty page to avoid interference with other users
  25. * of ZERO_PAGE(), such as /dev/zero
  26. */
  27. static DEFINE_MUTEX(xip_sparse_mutex);
  28. static seqcount_t xip_sparse_seq = SEQCNT_ZERO(xip_sparse_seq);
  29. static struct page *__xip_sparse_page;
  30. /* called under xip_sparse_mutex */
  31. static struct page *xip_sparse_page(void)
  32. {
  33. if (!__xip_sparse_page) {
  34. struct page *page = alloc_page(GFP_HIGHUSER | __GFP_ZERO);
  35. if (page)
  36. __xip_sparse_page = page;
  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 - copied)
  85. nr = len - copied;
  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 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 __xip_unmap(struct address_space * mapping, unsigned long pgoff)
  145. {
  146. struct vm_area_struct *vma;
  147. struct page *page;
  148. unsigned count;
  149. int locked = 0;
  150. count = read_seqcount_begin(&xip_sparse_seq);
  151. page = __xip_sparse_page;
  152. if (!page)
  153. return;
  154. retry:
  155. i_mmap_lock_read(mapping);
  156. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  157. pte_t *pte, pteval;
  158. spinlock_t *ptl;
  159. struct mm_struct *mm = vma->vm_mm;
  160. unsigned long address = vma->vm_start +
  161. ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  162. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  163. pte = page_check_address(page, mm, address, &ptl, 1);
  164. if (pte) {
  165. /* Nuke the page table entry. */
  166. flush_cache_page(vma, address, pte_pfn(*pte));
  167. pteval = ptep_clear_flush(vma, address, pte);
  168. page_remove_rmap(page);
  169. dec_mm_counter(mm, MM_FILEPAGES);
  170. BUG_ON(pte_dirty(pteval));
  171. pte_unmap_unlock(pte, ptl);
  172. /* must invalidate_page _before_ freeing the page */
  173. mmu_notifier_invalidate_page(mm, address);
  174. page_cache_release(page);
  175. }
  176. }
  177. i_mmap_unlock_read(mapping);
  178. if (locked) {
  179. mutex_unlock(&xip_sparse_mutex);
  180. } else if (read_seqcount_retry(&xip_sparse_seq, count)) {
  181. mutex_lock(&xip_sparse_mutex);
  182. locked = 1;
  183. goto retry;
  184. }
  185. }
  186. /*
  187. * xip_fault() is invoked via the vma operations vector for a
  188. * mapped memory region to read in file data during a page fault.
  189. *
  190. * This function is derived from filemap_fault, but used for execute in place
  191. */
  192. static int xip_file_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  193. {
  194. struct file *file = vma->vm_file;
  195. struct address_space *mapping = file->f_mapping;
  196. struct inode *inode = mapping->host;
  197. pgoff_t size;
  198. void *xip_mem;
  199. unsigned long xip_pfn;
  200. struct page *page;
  201. int error;
  202. /* XXX: are VM_FAULT_ codes OK? */
  203. again:
  204. size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  205. if (vmf->pgoff >= size)
  206. return VM_FAULT_SIGBUS;
  207. error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0,
  208. &xip_mem, &xip_pfn);
  209. if (likely(!error))
  210. goto found;
  211. if (error != -ENODATA)
  212. return VM_FAULT_OOM;
  213. /* sparse block */
  214. if ((vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) &&
  215. (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) &&
  216. (!(mapping->host->i_sb->s_flags & MS_RDONLY))) {
  217. int err;
  218. /* maybe shared writable, allocate new block */
  219. mutex_lock(&xip_sparse_mutex);
  220. error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 1,
  221. &xip_mem, &xip_pfn);
  222. mutex_unlock(&xip_sparse_mutex);
  223. if (error)
  224. return VM_FAULT_SIGBUS;
  225. /* unmap sparse mappings at pgoff from all other vmas */
  226. __xip_unmap(mapping, vmf->pgoff);
  227. found:
  228. err = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
  229. xip_pfn);
  230. if (err == -ENOMEM)
  231. return VM_FAULT_OOM;
  232. /*
  233. * err == -EBUSY is fine, we've raced against another thread
  234. * that faulted-in the same page
  235. */
  236. if (err != -EBUSY)
  237. BUG_ON(err);
  238. return VM_FAULT_NOPAGE;
  239. } else {
  240. int err, ret = VM_FAULT_OOM;
  241. mutex_lock(&xip_sparse_mutex);
  242. write_seqcount_begin(&xip_sparse_seq);
  243. error = mapping->a_ops->get_xip_mem(mapping, vmf->pgoff, 0,
  244. &xip_mem, &xip_pfn);
  245. if (unlikely(!error)) {
  246. write_seqcount_end(&xip_sparse_seq);
  247. mutex_unlock(&xip_sparse_mutex);
  248. goto again;
  249. }
  250. if (error != -ENODATA)
  251. goto out;
  252. /* not shared and writable, use xip_sparse_page() */
  253. page = xip_sparse_page();
  254. if (!page)
  255. goto out;
  256. err = vm_insert_page(vma, (unsigned long)vmf->virtual_address,
  257. page);
  258. if (err == -ENOMEM)
  259. goto out;
  260. ret = VM_FAULT_NOPAGE;
  261. out:
  262. write_seqcount_end(&xip_sparse_seq);
  263. mutex_unlock(&xip_sparse_mutex);
  264. return ret;
  265. }
  266. }
  267. static const struct vm_operations_struct xip_file_vm_ops = {
  268. .fault = xip_file_fault,
  269. .page_mkwrite = filemap_page_mkwrite,
  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 = inode_to_bdi(inode);
  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);