filemap_xip.c 11 KB

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