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>
20 * We do use our own empty page to avoid interference with other users
21 * of ZERO_PAGE(), such as /dev/zero
23 static struct page
*__xip_sparse_page
;
25 static struct page
*xip_sparse_page(void)
27 if (!__xip_sparse_page
) {
28 unsigned long zeroes
= get_zeroed_page(GFP_HIGHUSER
);
30 static DEFINE_SPINLOCK(xip_alloc_lock
);
31 spin_lock(&xip_alloc_lock
);
32 if (!__xip_sparse_page
)
33 __xip_sparse_page
= virt_to_page(zeroes
);
36 spin_unlock(&xip_alloc_lock
);
39 return __xip_sparse_page
;
43 * This is a file read routine for execute in place files, and uses
44 * the mapping->a_ops->get_xip_page() function for the actual low-level
47 * Note the struct file* is not used at all. It may be NULL.
50 do_xip_mapping_read(struct address_space
*mapping
,
51 struct file_ra_state
*_ra
,
54 read_descriptor_t
*desc
,
57 struct inode
*inode
= mapping
->host
;
58 unsigned long index
, end_index
, offset
;
61 BUG_ON(!mapping
->a_ops
->get_xip_page
);
63 index
= *ppos
>> PAGE_CACHE_SHIFT
;
64 offset
= *ppos
& ~PAGE_CACHE_MASK
;
66 isize
= i_size_read(inode
);
70 end_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
73 unsigned long nr
, ret
;
75 /* nr is the maximum number of bytes to copy from this page */
77 if (index
>= end_index
) {
78 if (index
> end_index
)
80 nr
= ((isize
- 1) & ~PAGE_CACHE_MASK
) + 1;
87 page
= mapping
->a_ops
->get_xip_page(mapping
,
88 index
*(PAGE_SIZE
/512), 0);
91 if (unlikely(IS_ERR(page
))) {
92 if (PTR_ERR(page
) == -ENODATA
) {
96 desc
->error
= PTR_ERR(page
);
101 /* If users can be writing to this page using arbitrary
102 * virtual addresses, take care about potential aliasing
103 * before reading the page on the kernel side.
105 if (mapping_writably_mapped(mapping
))
106 flush_dcache_page(page
);
109 * Ok, we have the page, so now we can copy it to user space...
111 * The actor routine returns how many bytes were actually used..
112 * NOTE! This may not be the same as how much of a user buffer
113 * we filled up (we may be padding etc), so we can only update
114 * "pos" here (the actor routine has to update the user buffer
115 * pointers and the remaining count).
117 ret
= actor(desc
, page
, offset
, nr
);
119 index
+= offset
>> PAGE_CACHE_SHIFT
;
120 offset
&= ~PAGE_CACHE_MASK
;
122 if (ret
== nr
&& desc
->count
)
127 /* Did not get the page. Report it */
133 *ppos
= ((loff_t
) index
<< PAGE_CACHE_SHIFT
) + offset
;
139 xip_file_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
141 read_descriptor_t desc
;
143 if (!access_ok(VERIFY_WRITE
, buf
, len
))
151 do_xip_mapping_read(filp
->f_mapping
, &filp
->f_ra
, filp
,
152 ppos
, &desc
, file_read_actor
);
159 EXPORT_SYMBOL_GPL(xip_file_read
);
162 * __xip_unmap is invoked from xip_unmap and
165 * This function walks all vmas of the address_space and unmaps the
166 * __xip_sparse_page when found at pgoff.
169 __xip_unmap (struct address_space
* mapping
,
172 struct vm_area_struct
*vma
;
173 struct mm_struct
*mm
;
174 struct prio_tree_iter iter
;
175 unsigned long address
;
181 page
= __xip_sparse_page
;
185 spin_lock(&mapping
->i_mmap_lock
);
186 vma_prio_tree_foreach(vma
, &iter
, &mapping
->i_mmap
, pgoff
, pgoff
) {
188 address
= vma
->vm_start
+
189 ((pgoff
- vma
->vm_pgoff
) << PAGE_SHIFT
);
190 BUG_ON(address
< vma
->vm_start
|| address
>= vma
->vm_end
);
191 pte
= page_check_address(page
, mm
, address
, &ptl
);
193 /* Nuke the page table entry. */
194 flush_cache_page(vma
, address
, pte_pfn(*pte
));
195 pteval
= ptep_clear_flush(vma
, address
, pte
);
196 page_remove_rmap(page
, vma
);
197 dec_mm_counter(mm
, file_rss
);
198 BUG_ON(pte_dirty(pteval
));
199 pte_unmap_unlock(pte
, ptl
);
200 page_cache_release(page
);
203 spin_unlock(&mapping
->i_mmap_lock
);
207 * xip_fault() is invoked via the vma operations vector for a
208 * mapped memory region to read in file data during a page fault.
210 * This function is derived from filemap_fault, but used for execute in place
212 static int xip_file_fault(struct vm_area_struct
*area
, struct vm_fault
*vmf
)
214 struct file
*file
= area
->vm_file
;
215 struct address_space
*mapping
= file
->f_mapping
;
216 struct inode
*inode
= mapping
->host
;
220 /* XXX: are VM_FAULT_ codes OK? */
222 size
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
223 if (vmf
->pgoff
>= size
)
224 return VM_FAULT_SIGBUS
;
226 page
= mapping
->a_ops
->get_xip_page(mapping
,
227 vmf
->pgoff
*(PAGE_SIZE
/512), 0);
230 if (PTR_ERR(page
) != -ENODATA
)
234 if ((area
->vm_flags
& (VM_WRITE
| VM_MAYWRITE
)) &&
235 (area
->vm_flags
& (VM_SHARED
| VM_MAYSHARE
)) &&
236 (!(mapping
->host
->i_sb
->s_flags
& MS_RDONLY
))) {
237 /* maybe shared writable, allocate new block */
238 page
= mapping
->a_ops
->get_xip_page(mapping
,
239 vmf
->pgoff
*(PAGE_SIZE
/512), 1);
241 return VM_FAULT_SIGBUS
;
242 /* unmap page at pgoff from all other vmas */
243 __xip_unmap(mapping
, vmf
->pgoff
);
245 /* not shared and writable, use xip_sparse_page() */
246 page
= xip_sparse_page();
252 page_cache_get(page
);
257 static struct vm_operations_struct xip_file_vm_ops
= {
258 .fault
= xip_file_fault
,
261 int xip_file_mmap(struct file
* file
, struct vm_area_struct
* vma
)
263 BUG_ON(!file
->f_mapping
->a_ops
->get_xip_page
);
266 vma
->vm_ops
= &xip_file_vm_ops
;
267 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
270 EXPORT_SYMBOL_GPL(xip_file_mmap
);
273 __xip_file_write(struct file
*filp
, const char __user
*buf
,
274 size_t count
, loff_t pos
, loff_t
*ppos
)
276 struct address_space
* mapping
= filp
->f_mapping
;
277 const struct address_space_operations
*a_ops
= mapping
->a_ops
;
278 struct inode
*inode
= mapping
->host
;
284 BUG_ON(!mapping
->a_ops
->get_xip_page
);
288 unsigned long offset
;
292 offset
= (pos
& (PAGE_CACHE_SIZE
-1)); /* Within page */
293 index
= pos
>> PAGE_CACHE_SHIFT
;
294 bytes
= PAGE_CACHE_SIZE
- offset
;
298 page
= a_ops
->get_xip_page(mapping
,
299 index
*(PAGE_SIZE
/512), 0);
300 if (IS_ERR(page
) && (PTR_ERR(page
) == -ENODATA
)) {
301 /* we allocate a new page unmap it */
302 page
= a_ops
->get_xip_page(mapping
,
303 index
*(PAGE_SIZE
/512), 1);
305 /* unmap page at pgoff from all other vmas */
306 __xip_unmap(mapping
, index
);
310 status
= PTR_ERR(page
);
314 fault_in_pages_readable(buf
, bytes
);
315 kaddr
= kmap_atomic(page
, KM_USER0
);
317 __copy_from_user_inatomic_nocache(kaddr
, buf
, bytes
);
318 kunmap_atomic(kaddr
, KM_USER0
);
319 flush_dcache_page(page
);
321 if (likely(copied
> 0)) {
331 if (unlikely(copied
!= bytes
))
339 * No need to use i_size_read() here, the i_size
340 * cannot change under us because we hold i_mutex.
342 if (pos
> inode
->i_size
) {
343 i_size_write(inode
, pos
);
344 mark_inode_dirty(inode
);
347 return written
? written
: status
;
351 xip_file_write(struct file
*filp
, const char __user
*buf
, size_t len
,
354 struct address_space
*mapping
= filp
->f_mapping
;
355 struct inode
*inode
= mapping
->host
;
360 mutex_lock(&inode
->i_mutex
);
362 if (!access_ok(VERIFY_READ
, buf
, len
)) {
370 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
372 /* We can write back this queue in page reclaim */
373 current
->backing_dev_info
= mapping
->backing_dev_info
;
375 ret
= generic_write_checks(filp
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
381 ret
= remove_suid(filp
->f_path
.dentry
);
385 file_update_time(filp
);
387 ret
= __xip_file_write (filp
, buf
, count
, pos
, ppos
);
390 current
->backing_dev_info
= NULL
;
392 mutex_unlock(&inode
->i_mutex
);
395 EXPORT_SYMBOL_GPL(xip_file_write
);
398 * truncate a page used for execute in place
399 * functionality is analog to block_truncate_page but does use get_xip_page
400 * to get the page instead of page cache
403 xip_truncate_page(struct address_space
*mapping
, loff_t from
)
405 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
406 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
411 BUG_ON(!mapping
->a_ops
->get_xip_page
);
413 blocksize
= 1 << mapping
->host
->i_blkbits
;
414 length
= offset
& (blocksize
- 1);
416 /* Block boundary? Nothing to do */
420 length
= blocksize
- length
;
422 page
= mapping
->a_ops
->get_xip_page(mapping
,
423 index
*(PAGE_SIZE
/512), 0);
426 if (unlikely(IS_ERR(page
))) {
427 if (PTR_ERR(page
) == -ENODATA
)
428 /* Hole? No need to truncate */
431 return PTR_ERR(page
);
433 zero_user_page(page
, offset
, length
, KM_USER0
);
436 EXPORT_SYMBOL_GPL(xip_truncate_page
);