filemap_xip.c 11 KB

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