Skip to content

Commit

Permalink
genwqe: convert get_user_pages() --> pin_user_pages()
Browse files Browse the repository at this point in the history
This code was using get_user_pages*(), in a "Case 2" scenario
(DMA/RDMA), using the categorization from [1]. That means that it's
time to convert the get_user_pages*() + put_page() calls to
pin_user_pages*() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small
part of fixing a long-standing disconnect between pinning pages, and
file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":
    https://lwn.net/Articles/807108/

Cc: Frank Haverkamp <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Signed-off-by: John Hubbard <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
johnhubbard authored and gregkh committed May 19, 2020
1 parent 5459cee commit ddae142
Showing 1 changed file with 9 additions and 33 deletions.
42 changes: 9 additions & 33 deletions drivers/misc/genwqe/card_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,30 +514,6 @@ int genwqe_free_sync_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl)
return rc;
}

/**
* genwqe_free_user_pages() - Give pinned pages back
*
* Documentation of get_user_pages is in mm/gup.c:
*
* If the page is written to, set_page_dirty (or set_page_dirty_lock,
* as appropriate) must be called after the page is finished with, and
* before put_page is called.
*/
static int genwqe_free_user_pages(struct page **page_list,
unsigned int nr_pages, int dirty)
{
unsigned int i;

for (i = 0; i < nr_pages; i++) {
if (page_list[i] != NULL) {
if (dirty)
set_page_dirty_lock(page_list[i]);
put_page(page_list[i]);
}
}
return 0;
}

/**
* genwqe_user_vmap() - Map user-space memory to virtual kernel memory
* @cd: pointer to genwqe device
Expand Down Expand Up @@ -597,18 +573,18 @@ int genwqe_user_vmap(struct genwqe_dev *cd, struct dma_mapping *m, void *uaddr,
m->dma_list = (dma_addr_t *)(m->page_list + m->nr_pages);

/* pin user pages in memory */
rc = get_user_pages_fast(data & PAGE_MASK, /* page aligned addr */
rc = pin_user_pages_fast(data & PAGE_MASK, /* page aligned addr */
m->nr_pages,
m->write ? FOLL_WRITE : 0, /* readable/writable */
m->page_list); /* ptrs to pages */
if (rc < 0)
goto fail_get_user_pages;
goto fail_pin_user_pages;

/* assumption: get_user_pages can be killed by signals. */
/* assumption: pin_user_pages can be killed by signals. */
if (rc < m->nr_pages) {
genwqe_free_user_pages(m->page_list, rc, m->write);
unpin_user_pages_dirty_lock(m->page_list, rc, m->write);
rc = -EFAULT;
goto fail_get_user_pages;
goto fail_pin_user_pages;
}

rc = genwqe_map_pages(cd, m->page_list, m->nr_pages, m->dma_list);
Expand All @@ -618,9 +594,9 @@ int genwqe_user_vmap(struct genwqe_dev *cd, struct dma_mapping *m, void *uaddr,
return 0;

fail_free_user_pages:
genwqe_free_user_pages(m->page_list, m->nr_pages, m->write);
unpin_user_pages_dirty_lock(m->page_list, m->nr_pages, m->write);

fail_get_user_pages:
fail_pin_user_pages:
kfree(m->page_list);
m->page_list = NULL;
m->dma_list = NULL;
Expand Down Expand Up @@ -650,8 +626,8 @@ int genwqe_user_vunmap(struct genwqe_dev *cd, struct dma_mapping *m)
genwqe_unmap_pages(cd, m->dma_list, m->nr_pages);

if (m->page_list) {
genwqe_free_user_pages(m->page_list, m->nr_pages, m->write);

unpin_user_pages_dirty_lock(m->page_list, m->nr_pages,
m->write);
kfree(m->page_list);
m->page_list = NULL;
m->dma_list = NULL;
Expand Down

0 comments on commit ddae142

Please sign in to comment.