filemap_xip.c 11 KB

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