Back to home page

LXR

 
 

    


0001 #include <linux/kernel.h>
0002 #include <linux/errno.h>
0003 #include <linux/err.h>
0004 #include <linux/mm.h>
0005 #include <linux/slab.h>
0006 #include <linux/vmalloc.h>
0007 #include <linux/pagemap.h>
0008 #include <linux/sched.h>
0009 
0010 /**
0011  * get_vaddr_frames() - map virtual addresses to pfns
0012  * @start:  starting user address
0013  * @nr_frames:  number of pages / pfns from start to map
0014  * @gup_flags:  flags modifying lookup behaviour
0015  * @vec:    structure which receives pages / pfns of the addresses mapped.
0016  *      It should have space for at least nr_frames entries.
0017  *
0018  * This function maps virtual addresses from @start and fills @vec structure
0019  * with page frame numbers or page pointers to corresponding pages (choice
0020  * depends on the type of the vma underlying the virtual address). If @start
0021  * belongs to a normal vma, the function grabs reference to each of the pages
0022  * to pin them in memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't
0023  * touch page structures and the caller must make sure pfns aren't reused for
0024  * anything else while he is using them.
0025  *
0026  * The function returns number of pages mapped which may be less than
0027  * @nr_frames. In particular we stop mapping if there are more vmas of
0028  * different type underlying the specified range of virtual addresses.
0029  * When the function isn't able to map a single page, it returns error.
0030  *
0031  * This function takes care of grabbing mmap_sem as necessary.
0032  */
0033 int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
0034              unsigned int gup_flags, struct frame_vector *vec)
0035 {
0036     struct mm_struct *mm = current->mm;
0037     struct vm_area_struct *vma;
0038     int ret = 0;
0039     int err;
0040     int locked;
0041 
0042     if (nr_frames == 0)
0043         return 0;
0044 
0045     if (WARN_ON_ONCE(nr_frames > vec->nr_allocated))
0046         nr_frames = vec->nr_allocated;
0047 
0048     down_read(&mm->mmap_sem);
0049     locked = 1;
0050     vma = find_vma_intersection(mm, start, start + 1);
0051     if (!vma) {
0052         ret = -EFAULT;
0053         goto out;
0054     }
0055     if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
0056         vec->got_ref = true;
0057         vec->is_pfns = false;
0058         ret = get_user_pages_locked(start, nr_frames,
0059             gup_flags, (struct page **)(vec->ptrs), &locked);
0060         goto out;
0061     }
0062 
0063     vec->got_ref = false;
0064     vec->is_pfns = true;
0065     do {
0066         unsigned long *nums = frame_vector_pfns(vec);
0067 
0068         while (ret < nr_frames && start + PAGE_SIZE <= vma->vm_end) {
0069             err = follow_pfn(vma, start, &nums[ret]);
0070             if (err) {
0071                 if (ret == 0)
0072                     ret = err;
0073                 goto out;
0074             }
0075             start += PAGE_SIZE;
0076             ret++;
0077         }
0078         /*
0079          * We stop if we have enough pages or if VMA doesn't completely
0080          * cover the tail page.
0081          */
0082         if (ret >= nr_frames || start < vma->vm_end)
0083             break;
0084         vma = find_vma_intersection(mm, start, start + 1);
0085     } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
0086 out:
0087     if (locked)
0088         up_read(&mm->mmap_sem);
0089     if (!ret)
0090         ret = -EFAULT;
0091     if (ret > 0)
0092         vec->nr_frames = ret;
0093     return ret;
0094 }
0095 EXPORT_SYMBOL(get_vaddr_frames);
0096 
0097 /**
0098  * put_vaddr_frames() - drop references to pages if get_vaddr_frames() acquired
0099  *          them
0100  * @vec:    frame vector to put
0101  *
0102  * Drop references to pages if get_vaddr_frames() acquired them. We also
0103  * invalidate the frame vector so that it is prepared for the next call into
0104  * get_vaddr_frames().
0105  */
0106 void put_vaddr_frames(struct frame_vector *vec)
0107 {
0108     int i;
0109     struct page **pages;
0110 
0111     if (!vec->got_ref)
0112         goto out;
0113     pages = frame_vector_pages(vec);
0114     /*
0115      * frame_vector_pages() might needed to do a conversion when
0116      * get_vaddr_frames() got pages but vec was later converted to pfns.
0117      * But it shouldn't really fail to convert pfns back...
0118      */
0119     if (WARN_ON(IS_ERR(pages)))
0120         goto out;
0121     for (i = 0; i < vec->nr_frames; i++)
0122         put_page(pages[i]);
0123     vec->got_ref = false;
0124 out:
0125     vec->nr_frames = 0;
0126 }
0127 EXPORT_SYMBOL(put_vaddr_frames);
0128 
0129 /**
0130  * frame_vector_to_pages - convert frame vector to contain page pointers
0131  * @vec:    frame vector to convert
0132  *
0133  * Convert @vec to contain array of page pointers.  If the conversion is
0134  * successful, return 0. Otherwise return an error. Note that we do not grab
0135  * page references for the page structures.
0136  */
0137 int frame_vector_to_pages(struct frame_vector *vec)
0138 {
0139     int i;
0140     unsigned long *nums;
0141     struct page **pages;
0142 
0143     if (!vec->is_pfns)
0144         return 0;
0145     nums = frame_vector_pfns(vec);
0146     for (i = 0; i < vec->nr_frames; i++)
0147         if (!pfn_valid(nums[i]))
0148             return -EINVAL;
0149     pages = (struct page **)nums;
0150     for (i = 0; i < vec->nr_frames; i++)
0151         pages[i] = pfn_to_page(nums[i]);
0152     vec->is_pfns = false;
0153     return 0;
0154 }
0155 EXPORT_SYMBOL(frame_vector_to_pages);
0156 
0157 /**
0158  * frame_vector_to_pfns - convert frame vector to contain pfns
0159  * @vec:    frame vector to convert
0160  *
0161  * Convert @vec to contain array of pfns.
0162  */
0163 void frame_vector_to_pfns(struct frame_vector *vec)
0164 {
0165     int i;
0166     unsigned long *nums;
0167     struct page **pages;
0168 
0169     if (vec->is_pfns)
0170         return;
0171     pages = (struct page **)(vec->ptrs);
0172     nums = (unsigned long *)pages;
0173     for (i = 0; i < vec->nr_frames; i++)
0174         nums[i] = page_to_pfn(pages[i]);
0175     vec->is_pfns = true;
0176 }
0177 EXPORT_SYMBOL(frame_vector_to_pfns);
0178 
0179 /**
0180  * frame_vector_create() - allocate & initialize structure for pinned pfns
0181  * @nr_frames:  number of pfns slots we should reserve
0182  *
0183  * Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns
0184  * pfns.
0185  */
0186 struct frame_vector *frame_vector_create(unsigned int nr_frames)
0187 {
0188     struct frame_vector *vec;
0189     int size = sizeof(struct frame_vector) + sizeof(void *) * nr_frames;
0190 
0191     if (WARN_ON_ONCE(nr_frames == 0))
0192         return NULL;
0193     /*
0194      * This is absurdly high. It's here just to avoid strange effects when
0195      * arithmetics overflows.
0196      */
0197     if (WARN_ON_ONCE(nr_frames > INT_MAX / sizeof(void *) / 2))
0198         return NULL;
0199     /*
0200      * Avoid higher order allocations, use vmalloc instead. It should
0201      * be rare anyway.
0202      */
0203     if (size <= PAGE_SIZE)
0204         vec = kmalloc(size, GFP_KERNEL);
0205     else
0206         vec = vmalloc(size);
0207     if (!vec)
0208         return NULL;
0209     vec->nr_allocated = nr_frames;
0210     vec->nr_frames = 0;
0211     return vec;
0212 }
0213 EXPORT_SYMBOL(frame_vector_create);
0214 
0215 /**
0216  * frame_vector_destroy() - free memory allocated to carry frame vector
0217  * @vec:    Frame vector to free
0218  *
0219  * Free structure allocated by frame_vector_create() to carry frames.
0220  */
0221 void frame_vector_destroy(struct frame_vector *vec)
0222 {
0223     /* Make sure put_vaddr_frames() got called properly... */
0224     VM_BUG_ON(vec->nr_frames > 0);
0225     kvfree(vec);
0226 }
0227 EXPORT_SYMBOL(frame_vector_destroy);