LCOV - code coverage report
Current view: top level - mm - fremap.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 64 74 86.5 %
Date: 2014-02-18 Functions: 4 4 100.0 %
Branches: 41 64 64.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *   linux/mm/fremap.c
       3                 :            :  * 
       4                 :            :  * Explicit pagetable population and nonlinear (random) mappings support.
       5                 :            :  *
       6                 :            :  * started by Ingo Molnar, Copyright (C) 2002, 2003
       7                 :            :  */
       8                 :            : #include <linux/export.h>
       9                 :            : #include <linux/backing-dev.h>
      10                 :            : #include <linux/mm.h>
      11                 :            : #include <linux/swap.h>
      12                 :            : #include <linux/file.h>
      13                 :            : #include <linux/mman.h>
      14                 :            : #include <linux/pagemap.h>
      15                 :            : #include <linux/swapops.h>
      16                 :            : #include <linux/rmap.h>
      17                 :            : #include <linux/syscalls.h>
      18                 :            : #include <linux/mmu_notifier.h>
      19                 :            : 
      20                 :            : #include <asm/mmu_context.h>
      21                 :            : #include <asm/cacheflush.h>
      22                 :            : #include <asm/tlbflush.h>
      23                 :            : 
      24                 :            : #include "internal.h"
      25                 :            : 
      26                 :          0 : static void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
      27                 :            :                         unsigned long addr, pte_t *ptep)
      28                 :            : {
      29                 :         16 :         pte_t pte = *ptep;
      30                 :            : 
      31         [ +  - ]:         16 :         if (pte_present(pte)) {
      32                 :            :                 struct page *page;
      33                 :            : 
      34                 :         16 :                 flush_cache_page(vma, addr, pte_pfn(pte));
      35                 :         16 :                 pte = ptep_clear_flush(vma, addr, ptep);
      36                 :         16 :                 page = vm_normal_page(vma, addr, pte);
      37         [ +  - ]:         16 :                 if (page) {
      38         [ -  + ]:         16 :                         if (pte_dirty(pte))
      39                 :          0 :                                 set_page_dirty(page);
      40                 :         16 :                         page_remove_rmap(page);
      41                 :         16 :                         page_cache_release(page);
      42                 :            :                         update_hiwater_rss(mm);
      43                 :            :                         dec_mm_counter(mm, MM_FILEPAGES);
      44                 :            :                 }
      45                 :            :         } else {
      46         [ #  # ]:          0 :                 if (!pte_file(pte))
      47                 :          0 :                         free_swap_and_cache(pte_to_swp_entry(pte));
      48                 :            :                 pte_clear_not_present_full(mm, addr, ptep, 0);
      49                 :            :         }
      50                 :         16 : }
      51                 :            : 
      52                 :            : /*
      53                 :            :  * Install a file pte to a given virtual memory address, release any
      54                 :            :  * previously existing mapping.
      55                 :            :  */
      56                 :         32 : static int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
      57                 :            :                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
      58                 :            : {
      59                 :            :         int err = -ENOMEM;
      60                 :            :         pte_t *pte, ptfile;
      61                 :            :         spinlock_t *ptl;
      62                 :            : 
      63                 :            :         pte = get_locked_pte(mm, addr, &ptl);
      64         [ +  - ]:         32 :         if (!pte)
      65                 :            :                 goto out;
      66                 :            : 
      67                 :         32 :         ptfile = pgoff_to_pte(pgoff);
      68                 :            : 
      69         [ +  + ]:         32 :         if (!pte_none(*pte)) {
      70                 :            :                 if (pte_present(*pte) && pte_soft_dirty(*pte))
      71                 :            :                         pte_file_mksoft_dirty(ptfile);
      72                 :         16 :                 zap_pte(mm, vma, addr, pte);
      73                 :            :         }
      74                 :            : 
      75                 :            :         set_pte_at(mm, addr, pte, ptfile);
      76                 :            :         /*
      77                 :            :          * We don't need to run update_mmu_cache() here because the "file pte"
      78                 :            :          * being installed by install_file_pte() is not a real pte - it's a
      79                 :            :          * non-present entry (like a swap entry), noting what file offset should
      80                 :            :          * be mapped there when there's a fault (in a non-linear vma where
      81                 :            :          * that's not obvious).
      82                 :            :          */
      83                 :         32 :         pte_unmap_unlock(pte, ptl);
      84                 :            :         err = 0;
      85                 :            : out:
      86                 :         32 :         return err;
      87                 :            : }
      88                 :            : 
      89                 :          0 : int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
      90                 :            :                              unsigned long size, pgoff_t pgoff)
      91                 :            : {
      92                 :         16 :         struct mm_struct *mm = vma->vm_mm;
      93                 :            :         int err;
      94                 :            : 
      95                 :            :         do {
      96                 :         32 :                 err = install_file_pte(mm, vma, addr, pgoff, vma->vm_page_prot);
      97         [ +  - ]:         32 :                 if (err)
      98                 :            :                         return err;
      99                 :            : 
     100                 :         32 :                 size -= PAGE_SIZE;
     101                 :         32 :                 addr += PAGE_SIZE;
     102                 :         32 :                 pgoff++;
     103         [ +  + ]:         32 :         } while (size);
     104                 :            : 
     105                 :            :         return 0;
     106                 :            : }
     107                 :            : EXPORT_SYMBOL(generic_file_remap_pages);
     108                 :            : 
     109                 :            : /**
     110                 :            :  * sys_remap_file_pages - remap arbitrary pages of an existing VM_SHARED vma
     111                 :            :  * @start: start of the remapped virtual memory range
     112                 :            :  * @size: size of the remapped virtual memory range
     113                 :            :  * @prot: new protection bits of the range (see NOTE)
     114                 :            :  * @pgoff: to-be-mapped page of the backing store file
     115                 :            :  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
     116                 :            :  *
     117                 :            :  * sys_remap_file_pages remaps arbitrary pages of an existing VM_SHARED vma
     118                 :            :  * (shared backing store file).
     119                 :            :  *
     120                 :            :  * This syscall works purely via pagetables, so it's the most efficient
     121                 :            :  * way to map the same (large) file into a given virtual window. Unlike
     122                 :            :  * mmap()/mremap() it does not create any new vmas. The new mappings are
     123                 :            :  * also safe across swapout.
     124                 :            :  *
     125                 :            :  * NOTE: the @prot parameter right now is ignored (but must be zero),
     126                 :            :  * and the vma's default protection is used. Arbitrary protections
     127                 :            :  * might be implemented in the future.
     128                 :            :  */
     129                 :          0 : SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
     130                 :            :                 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
     131                 :            : {
     132                 :         36 :         struct mm_struct *mm = current->mm;
     133                 :          9 :         struct address_space *mapping;
     134                 :         26 :         struct vm_area_struct *vma;
     135                 :            :         int err = -EINVAL;
     136                 :            :         int has_write_lock = 0;
     137                 :            :         vm_flags_t vm_flags = 0;
     138                 :            : 
     139         [ +  + ]:         36 :         if (prot)
     140                 :            :                 return err;
     141                 :            :         /*
     142                 :            :          * Sanitize the syscall parameters:
     143                 :            :          */
     144                 :         35 :         start = start & PAGE_MASK;
     145                 :         35 :         size = size & PAGE_MASK;
     146                 :            : 
     147                 :            :         /* Does the address range wrap, or is the span zero-sized? */
     148         [ +  - ]:         35 :         if (start + size <= start)
     149                 :            :                 return err;
     150                 :            : 
     151                 :            :         /* Does pgoff wrap? */
     152         [ +  - ]:         35 :         if (pgoff + (size >> PAGE_SHIFT) < pgoff)
     153                 :            :                 return err;
     154                 :            : 
     155                 :            :         /* Can we represent this offset inside this architecture's pte's? */
     156                 :            : #if PTE_FILE_MAX_BITS < BITS_PER_LONG
     157         [ +  - ]:         35 :         if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
     158                 :            :                 return err;
     159                 :            : #endif
     160                 :            : 
     161                 :            :         /* We need down_write() to change vma->vm_flags. */
     162                 :         35 :         down_read(&mm->mmap_sem);
     163                 :            :  retry:
     164                 :         44 :         vma = find_vma(mm, start);
     165                 :            : 
     166                 :            :         /*
     167                 :            :          * Make sure the vma is shared, that it supports prefaulting,
     168                 :            :          * and that the remapped range is valid and fully within
     169                 :            :          * the single existing vma.
     170                 :            :          */
     171 [ +  - ][ +  + ]:         44 :         if (!vma || !(vma->vm_flags & VM_SHARED))
     172                 :            :                 goto out;
     173                 :            : 
     174 [ +  - ][ +  - ]:         42 :         if (!vma->vm_ops || !vma->vm_ops->remap_pages)
     175                 :            :                 goto out;
     176                 :            : 
     177 [ +  - ][ +  + ]:         42 :         if (start < vma->vm_start || start + size > vma->vm_end)
     178                 :            :                 goto out;
     179                 :            : 
     180                 :            :         /* Must set VM_NONLINEAR before any pages are populated. */
     181         [ +  + ]:         41 :         if (!(vma->vm_flags & VM_NONLINEAR)) {
     182                 :            :                 /*
     183                 :            :                  * vm_private_data is used as a swapout cursor
     184                 :            :                  * in a VM_NONLINEAR vma.
     185                 :            :                  */
     186         [ +  - ]:         26 :                 if (vma->vm_private_data)
     187                 :            :                         goto out;
     188                 :            : 
     189                 :            :                 /* Don't need a nonlinear mapping, exit success */
     190         [ +  + ]:         26 :                 if (pgoff == linear_page_index(vma, start)) {
     191                 :            :                         err = 0;
     192                 :            :                         goto out;
     193                 :            :                 }
     194                 :            : 
     195         [ +  + ]:         18 :                 if (!has_write_lock) {
     196                 :            : get_write_lock:
     197                 :          9 :                         up_read(&mm->mmap_sem);
     198                 :          9 :                         down_write(&mm->mmap_sem);
     199                 :            :                         has_write_lock = 1;
     200                 :            :                         goto retry;
     201                 :            :                 }
     202                 :          9 :                 mapping = vma->vm_file->f_mapping;
     203                 :            :                 /*
     204                 :            :                  * page_mkclean doesn't work on nonlinear vmas, so if
     205                 :            :                  * dirty pages need to be accounted, emulate with linear
     206                 :            :                  * vmas.
     207                 :            :                  */
     208         [ +  + ]:          9 :                 if (mapping_cap_account_dirty(mapping)) {
     209                 :            :                         unsigned long addr;
     210                 :            :                         struct file *file = get_file(vma->vm_file);
     211                 :            :                         /* mmap_region may free vma; grab the info now */
     212                 :          8 :                         vm_flags = vma->vm_flags;
     213                 :            : 
     214                 :          8 :                         addr = mmap_region(file, start, size, vm_flags, pgoff);
     215                 :          8 :                         fput(file);
     216         [ -  + ]:          8 :                         if (IS_ERR_VALUE(addr)) {
     217                 :          0 :                                 err = addr;
     218                 :            :                         } else {
     219         [ -  + ]:          8 :                                 BUG_ON(addr != start);
     220                 :            :                                 err = 0;
     221                 :            :                         }
     222                 :            :                         goto out_freed;
     223                 :            :                 }
     224                 :          1 :                 mutex_lock(&mapping->i_mmap_mutex);
     225                 :            :                 flush_dcache_mmap_lock(mapping);
     226                 :          1 :                 vma->vm_flags |= VM_NONLINEAR;
     227                 :          1 :                 vma_interval_tree_remove(vma, &mapping->i_mmap);
     228                 :          1 :                 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
     229                 :            :                 flush_dcache_mmap_unlock(mapping);
     230                 :          1 :                 mutex_unlock(&mapping->i_mmap_mutex);
     231                 :            :         }
     232                 :            : 
     233         [ -  + ]:         16 :         if (vma->vm_flags & VM_LOCKED) {
     234                 :            :                 /*
     235                 :            :                  * drop PG_Mlocked flag for over-mapped range
     236                 :            :                  */
     237         [ #  # ]:          0 :                 if (!has_write_lock)
     238                 :            :                         goto get_write_lock;
     239                 :            :                 vm_flags = vma->vm_flags;
     240                 :          0 :                 munlock_vma_pages_range(vma, start, start + size);
     241                 :          0 :                 vma->vm_flags = vm_flags;
     242                 :            :         }
     243                 :            : 
     244                 :            :         mmu_notifier_invalidate_range_start(mm, start, start + size);
     245                 :         16 :         err = vma->vm_ops->remap_pages(vma, start, size, pgoff);
     246                 :            :         mmu_notifier_invalidate_range_end(mm, start, start + size);
     247                 :            : 
     248                 :            :         /*
     249                 :            :          * We can't clear VM_NONLINEAR because we'd have to do
     250                 :            :          * it after ->populate completes, and that would prevent
     251                 :            :          * downgrading the lock.  (Locks can't be upgraded).
     252                 :            :          */
     253                 :            : 
     254                 :            : out:
     255         [ +  - ]:         27 :         if (vma)
     256                 :         27 :                 vm_flags = vma->vm_flags;
     257                 :            : out_freed:
     258         [ +  + ]:         35 :         if (likely(!has_write_lock))
     259                 :         26 :                 up_read(&mm->mmap_sem);
     260                 :            :         else
     261                 :          9 :                 up_write(&mm->mmap_sem);
     262 [ +  + ][ +  - ]:         35 :         if (!err && ((vm_flags & VM_LOCKED) || !(flags & MAP_NONBLOCK)))
                 [ +  - ]
     263                 :            :                 mm_populate(start, size);
     264                 :            : 
     265                 :            :         return err;
     266                 :            : }

Generated by: LCOV version 1.9