Back to home page

OSCL-LXR

 
 

    


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