2 * linux/mm/filemap_xip.c
4 * Copyright (C) 2005 IBM Corporation
5 * Author: Carsten Otte <cotte@de.ibm.com>
7 * derived from linux/mm/filemap.c - Copyright (C) Linus Torvalds
12 #include <linux/pagemap.h>
13 #include <linux/module.h>
14 #include <linux/uio.h>
15 #include <linux/rmap.h>
16 #include <linux/sched.h>
17 #include <asm/tlbflush.h>
21 * We do use our own empty page to avoid interference with other users
22 * of ZERO_PAGE(), such as /dev/zero
24 static struct page
*__xip_sparse_page
;
26 static struct page
*xip_sparse_page(void)
28 if (!__xip_sparse_page
) {
29 unsigned long zeroes
= get_zeroed_page(GFP_HIGHUSER
);
31 static DEFINE_SPINLOCK(xip_alloc_lock
);
32 spin_lock(&xip_alloc_lock
);
33 if (!__xip_sparse_page
)
34 __xip_sparse_page
= virt_to_page(zeroes
);
37 spin_unlock(&xip_alloc_lock
);
40 return __xip_sparse_page
;
44 * This is a file read routine for execute in place files, and uses
45 * the mapping->a_ops->get_xip_page() function for the actual low-level
48 * Note the struct file* is not used at all. It may be NULL.
51 do_xip_mapping_read(struct address_space
*mapping
,
52 struct file_ra_state
*_ra
,
55 read_descriptor_t
*desc
,
58 struct inode
*inode
= mapping
->host
;
59 unsigned long index
, end_index
, offset
;
62 BUG_ON(!mapping
->a_ops
->get_xip_page
);
64 index
= *ppos
>> PAGE_CACHE_SHIFT
;
65 offset
= *ppos
& ~PAGE_CACHE_MASK
;
67 isize
= i_size_read(inode
);
71 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
74 unsigned long nr
, ret
;
76 /* nr is the maximum number of bytes to copy from this page */
78 if (index
>= end_index
) {
79 if (index
> end_index
)
81 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
88 page
= mapping
->a_ops
->get_xip_page(mapping
,
89 index
*(PAGE_SIZE
/512), 0);
92 if (unlikely(IS_ERR(page
))) {
93 if (PTR_ERR(page
) == -ENODATA
) {
97 desc
->error
= PTR_ERR(page
);
102 /* If users can be writing to this page using arbitrary
103 * virtual addresses, take care about potential aliasing
104 * before reading the page on the kernel side.
106 if (mapping_writably_mapped(mapping
))
107 flush_dcache_page(page
);
110 * Ok, we have the page, so now we can copy it to user space...
112 * The actor routine returns how many bytes were actually used..
113 * NOTE! This may not be the same as how much of a user buffer
114 * we filled up (we may be padding etc), so we can only update
115 * "pos" here (the actor routine has to update the user buffer
116 * pointers and the remaining count).
118 ret
= actor(desc
, page
, offset
, nr
);
120 index
+= offset
>> PAGE_CACHE_SHIFT
;
121 offset
&= ~PAGE_CACHE_MASK
;
123 if (ret
== nr
&& desc
->count
)
128 /* Did not get the page. Report it */
134 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
140 xip_file_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
142 read_descriptor_t desc
;
144 if (!access_ok(VERIFY_WRITE
, buf
, len
))
152 do_xip_mapping_read(filp
->f_mapping
, &filp
->f_ra
, filp
,
153 ppos
, &desc
, file_read_actor
);
160 EXPORT_SYMBOL_GPL(xip_file_read
);
163 * __xip_unmap is invoked from xip_unmap and
166 * This function walks all vmas of the address_space and unmaps the
167 * __xip_sparse_page when found at pgoff.
170 __xip_unmap (struct address_space
* mapping
,
173 struct vm_area_struct
*vma
;
174 struct mm_struct
*mm
;
175 struct prio_tree_iter iter
;
176 unsigned long address
;
182 page
= __xip_sparse_page
;
186 spin_lock(&mapping
->i_mmap_lock
);
187 vma_prio_tree_foreach(vma
, &iter
, &mapping
->i_mmap
, pgoff
, pgoff
) {
189 address
= vma
->vm_start
+
190 ((pgoff
- vma
->vm_pgoff
) << PAGE_SHIFT
);
191 BUG_ON(address
< vma
->vm_start
|| address
>= vma
->vm_end
);
192 pte
= page_check_address(page
, mm
, address
, &ptl
);
194 /* Nuke the page table entry. */
195 flush_cache_page(vma
, address
, pte_pfn(*pte
));
196 pteval
= ptep_clear_flush(vma
, address
, pte
);
197 page_remove_rmap(page
, vma
);
198 dec_mm_counter(mm
, file_rss
);
199 BUG_ON(pte_dirty(pteval
));
200 pte_unmap_unlock(pte
, ptl
);
201 page_cache_release(page
);
204 spin_unlock(&mapping
->i_mmap_lock
);
208 * xip_nopage() is invoked via the vma operations vector for a
209 * mapped memory region to read in file data during a page fault.
211 * This function is derived from filemap_nopage, but used for execute in place
214 xip_file_nopage(struct vm_area_struct
* area
,
215 unsigned long address
,
218 struct file
*file
= area
->vm_file
;
219 struct address_space
*mapping
= file
->f_mapping
;
220 struct inode
*inode
= mapping
->host
;
222 unsigned long size
, pgoff
, endoff
;
224 pgoff
= ((address
- area
->vm_start
) >> PAGE_CACHE_SHIFT
)
226 endoff
= ((area
->vm_end
- area
->vm_start
) >> PAGE_CACHE_SHIFT
)
229 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
231 return NOPAGE_SIGBUS
;
233 page
= mapping
->a_ops
->get_xip_page(mapping
, pgoff
*(PAGE_SIZE
/512), 0);
236 if (PTR_ERR(page
) != -ENODATA
)
237 return NOPAGE_SIGBUS
;
240 if ((area
->vm_flags
& (VM_WRITE
| VM_MAYWRITE
)) &&
241 (area
->vm_flags
& (VM_SHARED
| VM_MAYSHARE
)) &&
242 (!(mapping
->host
->i_sb
->s_flags
& MS_RDONLY
))) {
243 /* maybe shared writable, allocate new block */
244 page
= mapping
->a_ops
->get_xip_page (mapping
,
245 pgoff
*(PAGE_SIZE
/512), 1);
247 return NOPAGE_SIGBUS
;
248 /* unmap page at pgoff from all other vmas */
249 __xip_unmap(mapping
, pgoff
);
251 /* not shared and writable, use xip_sparse_page() */
252 page
= xip_sparse_page();
258 page_cache_get(page
);
262 static struct vm_operations_struct xip_file_vm_ops
= {
263 .nopage
= xip_file_nopage
,
266 int xip_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
268 BUG_ON(!file
->f_mapping
->a_ops
->get_xip_page
);
271 vma
->vm_ops
= &xip_file_vm_ops
;
274 EXPORT_SYMBOL_GPL(xip_file_mmap
);
277 __xip_file_write(struct file
*filp
, const char __user
*buf
,
278 size_t count
, loff_t pos
, loff_t
*ppos
)
280 struct address_space
* mapping
= filp
->f_mapping
;
281 const struct address_space_operations
*a_ops
= mapping
->a_ops
;
282 struct inode
*inode
= mapping
->host
;
288 BUG_ON(!mapping
->a_ops
->get_xip_page
);
292 unsigned long offset
;
295 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
296 index
= pos
>> PAGE_CACHE_SHIFT
;
297 bytes
= PAGE_CACHE_SIZE
- offset
;
302 * Bring in the user page that we will copy from _first_.
303 * Otherwise there's a nasty deadlock on copying from the
304 * same page as we're writing to, without it being marked
307 fault_in_pages_readable(buf
, bytes
);
309 page
= a_ops
->get_xip_page(mapping
,
310 index
*(PAGE_SIZE
/512), 0);
311 if (IS_ERR(page
) && (PTR_ERR(page
) == -ENODATA
)) {
312 /* we allocate a new page unmap it */
313 page
= a_ops
->get_xip_page(mapping
,
314 index
*(PAGE_SIZE
/512), 1);
316 /* unmap page at pgoff from all other vmas */
317 __xip_unmap(mapping
, index
);
321 status
= PTR_ERR(page
);
325 copied
= filemap_copy_from_user(page
, offset
, buf
, bytes
);
326 flush_dcache_page(page
);
327 if (likely(copied
> 0)) {
337 if (unlikely(copied
!= bytes
))
345 * No need to use i_size_read() here, the i_size
346 * cannot change under us because we hold i_mutex.
348 if (pos
> inode
->i_size
) {
349 i_size_write(inode
, pos
);
350 mark_inode_dirty(inode
);
353 return written
? written
: status
;
357 xip_file_write(struct file
*filp
, const char __user
*buf
, size_t len
,
360 struct address_space
*mapping
= filp
->f_mapping
;
361 struct inode
*inode
= mapping
->host
;
366 mutex_lock(&inode
->i_mutex
);
368 if (!access_ok(VERIFY_READ
, buf
, len
)) {
376 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
378 /* We can write back this queue in page reclaim */
379 current
->backing_dev_info
= mapping
->backing_dev_info
;
381 ret
= generic_write_checks(filp
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
387 ret
= remove_suid(filp
->f_path
.dentry
);
391 file_update_time(filp
);
393 ret
= __xip_file_write (filp
, buf
, count
, pos
, ppos
);
396 current
->backing_dev_info
= NULL
;
398 mutex_unlock(&inode
->i_mutex
);
401 EXPORT_SYMBOL_GPL(xip_file_write
);
404 * truncate a page used for execute in place
405 * functionality is analog to block_truncate_page but does use get_xip_page
406 * to get the page instead of page cache
409 xip_truncate_page(struct address_space
*mapping
, loff_t from
)
411 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
412 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
417 BUG_ON(!mapping
->a_ops
->get_xip_page
);
419 blocksize
= 1 << mapping
->host
->i_blkbits
;
420 length
= offset
& (blocksize
- 1);
422 /* Block boundary? Nothing to do */
426 length
= blocksize
- length
;
428 page
= mapping
->a_ops
->get_xip_page(mapping
,
429 index
*(PAGE_SIZE
/512), 0);
432 if (unlikely(IS_ERR(page
))) {
433 if (PTR_ERR(page
) == -ENODATA
)
434 /* Hole? No need to truncate */
437 return PTR_ERR(page
);
439 zero_user_page(page
, offset
, length
, KM_USER0
);
442 EXPORT_SYMBOL_GPL(xip_truncate_page
);