From: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> To: unlisted-recipients:; (no To-header on input) Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>, Sakari Ailus <sakari.ailus@linux.intel.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, linux-media@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 14/41] media: atomisp: use pin_user_pages() for memory allocation Date: Sat, 30 May 2020 08:55:31 +0200 Message-ID: <2ff6f038d661da0f0e3b75ce1749b8612ac578f3.1590821410.git.mchehab+huawei@kernel.org> (raw) In-Reply-To: <cover.1590821410.git.mchehab+huawei@kernel.org> Instead of using a hacked version of an old copy of get_user_pages(), use pin_user_pages(). Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> --- .../media/atomisp/include/hmm/hmm_bo.h | 2 + .../staging/media/atomisp/pci/hmm/hmm_bo.c | 145 +++--------------- 2 files changed, 24 insertions(+), 123 deletions(-) diff --git a/drivers/staging/media/atomisp/include/hmm/hmm_bo.h b/drivers/staging/media/atomisp/include/hmm/hmm_bo.h index 7fcb93b6c0f5..39aea0cb2d33 100644 --- a/drivers/staging/media/atomisp/include/hmm/hmm_bo.h +++ b/drivers/staging/media/atomisp/include/hmm/hmm_bo.h @@ -130,6 +130,8 @@ struct hmm_buffer_object { struct list_head list; struct kref kref; + struct page **pages; + /* mutex protecting this BO */ struct mutex mutex; enum hmm_bo_type type; diff --git a/drivers/staging/media/atomisp/pci/hmm/hmm_bo.c b/drivers/staging/media/atomisp/pci/hmm/hmm_bo.c index b6dcd246d7af..64bf6b8fc7cc 100644 --- a/drivers/staging/media/atomisp/pci/hmm/hmm_bo.c +++ b/drivers/staging/media/atomisp/pci/hmm/hmm_bo.c @@ -854,109 +854,20 @@ static void free_private_pages(struct hmm_buffer_object *bo, kfree(bo->page_obj); } -/* - * Hacked from kernel function __get_user_pages in mm/memory.c - * - * Handle buffers allocated by other kernel space driver and mmaped into user - * space, function Ignore the VM_PFNMAP and VM_IO flag in VMA structure - * - * Get physical pages from user space virtual address and update into page list - */ -static int __get_pfnmap_pages(struct task_struct *tsk, struct mm_struct *mm, - unsigned long start, int nr_pages, - unsigned int gup_flags, struct page **pages, - struct vm_area_struct **vmas) +static void free_user_pages(struct hmm_buffer_object *bo) { - int i, ret; - unsigned long vm_flags; - - if (nr_pages <= 0) - return 0; - - VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); - - /* - * Require read or write permissions. - * If FOLL_FORCE is set, we only require the "MAY" flags. - */ - vm_flags = (gup_flags & FOLL_WRITE) ? - (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD); - vm_flags &= (gup_flags & FOLL_FORCE) ? - (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE); - i = 0; - - do { - struct vm_area_struct *vma; - - vma = find_vma(mm, start); - if (!vma) { - dev_err(atomisp_dev, "find_vma failed\n"); - return i ? : -EFAULT; - } - - if (is_vm_hugetlb_page(vma)) { - /* - i = follow_hugetlb_page(mm, vma, pages, vmas, - &start, &nr_pages, i, gup_flags); - */ - continue; - } - - do { - struct page *page; - unsigned long pfn; - - /* - * If we have a pending SIGKILL, don't keep faulting - * pages and potentially allocating memory. - */ - if (unlikely(fatal_signal_pending(current))) { - dev_err(atomisp_dev, - "fatal_signal_pending in %s\n", - __func__); - return i ? i : -ERESTARTSYS; - } - - ret = follow_pfn(vma, start, &pfn); - if (ret) { - dev_err(atomisp_dev, "follow_pfn() failed\n"); - return i ? : -EFAULT; - } - - page = pfn_to_page(pfn); - if (IS_ERR(page)) - return i ? i : PTR_ERR(page); - if (pages) { - pages[i] = page; - get_page(page); - flush_anon_page(vma, page, start); - flush_dcache_page(page); - } - if (vmas) - vmas[i] = vma; - i++; - start += PAGE_SIZE; - nr_pages--; - } while (nr_pages && start < vma->vm_end); - } while (nr_pages); - - return i; -} - -static int get_pfnmap_pages(struct task_struct *tsk, struct mm_struct *mm, - unsigned long start, int nr_pages, int write, int force, - struct page **pages, struct vm_area_struct **vmas) -{ - int flags = FOLL_TOUCH; - - if (pages) - flags |= FOLL_GET; - if (write) - flags |= FOLL_WRITE; - if (force) - flags |= FOLL_FORCE; - - return __get_pfnmap_pages(tsk, mm, start, nr_pages, flags, pages, vmas); + int i; + + hmm_mem_stat.usr_size -= bo->pgnr; + + if (bo->mem_type == HMM_BO_MEM_TYPE_PFN) { + unpin_user_pages(bo->pages, bo->pgnr); + } else { + for (i = 0; i < bo->pgnr; i++) + put_page(bo->pages[i]); + } + kfree(bo->pages); + kfree(bo->page_obj); } /* @@ -1000,11 +911,12 @@ static int alloc_user_pages(struct hmm_buffer_object *bo, userptr = untagged_addr(userptr); + bo->pages = pages; + if (vma->vm_flags & (VM_IO | VM_PFNMAP)) { - page_nr = get_pfnmap_pages(current, current->mm, - (unsigned long)userptr, - (int)(bo->pgnr), 1, 0, - pages, NULL); + page_nr = pin_user_pages((unsigned long)userptr, bo->pgnr, + FOLL_LONGTERM | FOLL_WRITE, + pages, NULL); bo->mem_type = HMM_BO_MEM_TYPE_PFN; } else { /*Handle frame buffer allocated in user space*/ @@ -1020,6 +932,8 @@ static int alloc_user_pages(struct hmm_buffer_object *bo, bo->pgnr, bo->mem_type == HMM_BO_MEM_TYPE_USER ? "user" : "pfn", page_nr); + hmm_mem_stat.usr_size += bo->pgnr; + /* can be written by caller, not forced */ if (page_nr != bo->pgnr) { dev_err(atomisp_dev, @@ -1032,31 +946,16 @@ static int alloc_user_pages(struct hmm_buffer_object *bo, bo->page_obj[i].page = pages[i]; bo->page_obj[i].type = HMM_PAGE_TYPE_GENERAL; } - hmm_mem_stat.usr_size += bo->pgnr; - kfree(pages); return 0; out_of_mem: - for (i = 0; i < page_nr; i++) - put_page(pages[i]); - kfree(pages); - kfree(bo->page_obj); + + free_user_pages(bo); return -ENOMEM; } -static void free_user_pages(struct hmm_buffer_object *bo) -{ - int i; - - for (i = 0; i < bo->pgnr; i++) - put_page(bo->page_obj[i].page); - hmm_mem_stat.usr_size -= bo->pgnr; - - kfree(bo->page_obj); -} - /* * allocate/free physical pages for the bo. * -- 2.26.2
next prev parent reply other threads:[~2020-05-30 6:57 UTC|newest] Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-05-30 6:55 [PATCH v2 00/41] More atomisp fixes and cleanups Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 01/41] media: atomisp: simplify hive_isp_css_mm_hrt wrapper Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 02/41] media: atomisp: get rid of the hrt/hive_isp_css_mm_hrt abstraction layer Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 03/41] media: atomisp: reduce abstraction at ia_css_memory_access Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 04/41] media: atomisp: go one step further to drop ia_css_memory_access.c Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 05/41] media: atomisp: get rid of mmgr_load and mmgr_store Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 06/41] media: atomisp: get rid of unused memory_realloc code Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 07/41] media: atomisp: change the type returned by mmgr alloc Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 08/41] media: atomisp: get rid of memory_access.c Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 09/41] media: atomisp: hmm_bo: untag user pointers Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 10/41] media: atomisp: add debug message to help debugging hmm code Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 11/41] media: atomisp: use Yocto Aero default hmm pool sizes Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 12/41] media: atomisp: get rid of a warning message Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 13/41] media: atomisp: fix driver caps Mauro Carvalho Chehab 2020-05-30 6:55 ` Mauro Carvalho Chehab [this message] 2020-05-30 6:55 ` [PATCH v2 15/41] media: atomisp: add debug for hmm alloc Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 16/41] media: atomisp: improve warning for IRQ enable function Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 17/41] media: atomisp: add debug functions for received events Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 18/41] media: atomisp: add more comments about frame allocation Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 19/41] media: atomisp: remove kvmalloc/kvcalloc abstractions Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 20/41] media: atomisp: avoid OOPS due to non-existing ref_frames Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 21/41] media: atomisp: Clean up if block in sh_css_sp_init_stage Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 22/41] media: atomisp: Remove second increment of count in atomisp_subdev_probe Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 23/41] media: atomisp: Remove unnecessary NULL checks in ia_css_pipe_load_extension Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 24/41] media: atomisp: Remove unnecessary NULL check in atomisp_param Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 25/41] media: atomisp: Avoid overflow in compute_blending Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 26/41] media: atomisp: Remove binary_supports_input_format Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 27/41] media: atomisp: avoid an extra memset() when alloc memory Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 28/41] media: atomisp: remove some trivial wrappers from compat css20 Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 29/41] media: atomisp: do another round of coding style cleanup Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 31/41] media: atomisp: get rid of an error abstraction layer Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 32/41] media: atomisp: don't cause a warn if probe failed Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 33/41] media: atomisp: get rid of a bunch of other wrappers Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 34/41] media: atomisp: get rid of system_types.h Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 35/41] media: atomisp: provide more details about the firmware binaries Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 36/41] media: atomisp: print firmware data during load Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 37/41] media: atomisp: allow passing firmware name at modprobe time Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 38/41] media: atomisp: add a debug message at hmm free Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 39/41] media: atomisp: add some debug messages when binaries are used Mauro Carvalho Chehab 2020-05-30 6:55 ` [PATCH v2 40/41] media: atomisp: get rid of set_fs() dirty hacks Mauro Carvalho Chehab
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=2ff6f038d661da0f0e3b75ce1749b8612ac578f3.1590821410.git.mchehab+huawei@kernel.org \ --to=mchehab+huawei@kernel.org \ --cc=devel@driverdev.osuosl.org \ --cc=gregkh@linuxfoundation.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-media@vger.kernel.org \ --cc=sakari.ailus@linux.intel.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Unnamed repository; edit this file 'description' to name the repository. This inbox may be cloned and mirrored by anyone: git clone --mirror http://archive.lwn.net:8080/linux-media/0 linux-media/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 linux-media linux-media/ http://archive.lwn.net:8080/linux-media \ linux-media@vger.kernel.org lwn-linux-media@archive.lwn.net public-inbox-index linux-media Example config snippet for mirrors. Newsgroup available over NNTP: nntp://archive.lwn.net/lwn.kernel.linux-media AGPL code for this site: git clone https://public-inbox.org/public-inbox.git