Commit e7920310 authored by John Hubbard's avatar John Hubbard Committed by Linus Torvalds

ivtv: convert get_user_pages() --> pin_user_pages()

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/Signed-off-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Cc: Andy Walls <awalls@md.metrocast.net>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Link: http://lkml.kernel.org/r/20200518012157.1178336-3-jhubbard@nvidia.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 91429023
...@@ -92,7 +92,7 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, ...@@ -92,7 +92,7 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
{ {
struct ivtv_dma_page_info user_dma; struct ivtv_dma_page_info user_dma;
struct ivtv_user_dma *dma = &itv->udma; struct ivtv_user_dma *dma = &itv->udma;
int i, err; int err;
IVTV_DEBUG_DMA("ivtv_udma_setup, dst: 0x%08x\n", (unsigned int)ivtv_dest_addr); IVTV_DEBUG_DMA("ivtv_udma_setup, dst: 0x%08x\n", (unsigned int)ivtv_dest_addr);
...@@ -111,16 +111,15 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, ...@@ -111,16 +111,15 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
return -EINVAL; return -EINVAL;
} }
/* Get user pages for DMA Xfer */ /* Pin user pages for DMA Xfer */
err = get_user_pages_unlocked(user_dma.uaddr, user_dma.page_count, err = pin_user_pages_unlocked(user_dma.uaddr, user_dma.page_count,
dma->map, FOLL_FORCE); dma->map, FOLL_FORCE);
if (user_dma.page_count != err) { if (user_dma.page_count != err) {
IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n", IVTV_DEBUG_WARN("failed to map user pages, returned %d instead of %d\n",
err, user_dma.page_count); err, user_dma.page_count);
if (err >= 0) { if (err >= 0) {
for (i = 0; i < err; i++) unpin_user_pages(dma->map, err);
put_page(dma->map[i]);
return -EINVAL; return -EINVAL;
} }
return err; return err;
...@@ -130,9 +129,7 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, ...@@ -130,9 +129,7 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
/* Fill SG List with new values */ /* Fill SG List with new values */
if (ivtv_udma_fill_sg_list(dma, &user_dma, 0) < 0) { if (ivtv_udma_fill_sg_list(dma, &user_dma, 0) < 0) {
for (i = 0; i < dma->page_count; i++) { unpin_user_pages(dma->map, dma->page_count);
put_page(dma->map[i]);
}
dma->page_count = 0; dma->page_count = 0;
return -ENOMEM; return -ENOMEM;
} }
...@@ -153,7 +150,6 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr, ...@@ -153,7 +150,6 @@ int ivtv_udma_setup(struct ivtv *itv, unsigned long ivtv_dest_addr,
void ivtv_udma_unmap(struct ivtv *itv) void ivtv_udma_unmap(struct ivtv *itv)
{ {
struct ivtv_user_dma *dma = &itv->udma; struct ivtv_user_dma *dma = &itv->udma;
int i;
IVTV_DEBUG_INFO("ivtv_unmap_user_dma\n"); IVTV_DEBUG_INFO("ivtv_unmap_user_dma\n");
...@@ -169,10 +165,7 @@ void ivtv_udma_unmap(struct ivtv *itv) ...@@ -169,10 +165,7 @@ void ivtv_udma_unmap(struct ivtv *itv)
/* sync DMA */ /* sync DMA */
ivtv_udma_sync_for_cpu(itv); ivtv_udma_sync_for_cpu(itv);
/* Release User Pages */ unpin_user_pages(dma->map, dma->page_count);
for (i = 0; i < dma->page_count; i++) {
put_page(dma->map[i]);
}
dma->page_count = 0; dma->page_count = 0;
} }
......
...@@ -30,7 +30,6 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, ...@@ -30,7 +30,6 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
struct yuv_playback_info *yi = &itv->yuv_info; struct yuv_playback_info *yi = &itv->yuv_info;
u8 frame = yi->draw_frame; u8 frame = yi->draw_frame;
struct yuv_frame_info *f = &yi->new_frame_info[frame]; struct yuv_frame_info *f = &yi->new_frame_info[frame];
int i;
int y_pages, uv_pages; int y_pages, uv_pages;
unsigned long y_buffer_offset, uv_buffer_offset; unsigned long y_buffer_offset, uv_buffer_offset;
int y_decode_height, uv_decode_height, y_size; int y_decode_height, uv_decode_height, y_size;
...@@ -62,12 +61,12 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, ...@@ -62,12 +61,12 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
ivtv_udma_get_page_info (&y_dma, (unsigned long)args->y_source, 720 * y_decode_height); ivtv_udma_get_page_info (&y_dma, (unsigned long)args->y_source, 720 * y_decode_height);
ivtv_udma_get_page_info (&uv_dma, (unsigned long)args->uv_source, 360 * uv_decode_height); ivtv_udma_get_page_info (&uv_dma, (unsigned long)args->uv_source, 360 * uv_decode_height);
/* Get user pages for DMA Xfer */ /* Pin user pages for DMA Xfer */
y_pages = get_user_pages_unlocked(y_dma.uaddr, y_pages = pin_user_pages_unlocked(y_dma.uaddr,
y_dma.page_count, &dma->map[0], FOLL_FORCE); y_dma.page_count, &dma->map[0], FOLL_FORCE);
uv_pages = 0; /* silence gcc. value is set and consumed only if: */ uv_pages = 0; /* silence gcc. value is set and consumed only if: */
if (y_pages == y_dma.page_count) { if (y_pages == y_dma.page_count) {
uv_pages = get_user_pages_unlocked(uv_dma.uaddr, uv_pages = pin_user_pages_unlocked(uv_dma.uaddr,
uv_dma.page_count, &dma->map[y_pages], uv_dma.page_count, &dma->map[y_pages],
FOLL_FORCE); FOLL_FORCE);
} }
...@@ -81,8 +80,7 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, ...@@ -81,8 +80,7 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
uv_pages, uv_dma.page_count); uv_pages, uv_dma.page_count);
if (uv_pages >= 0) { if (uv_pages >= 0) {
for (i = 0; i < uv_pages; i++) unpin_user_pages(&dma->map[y_pages], uv_pages);
put_page(dma->map[y_pages + i]);
rc = -EFAULT; rc = -EFAULT;
} else { } else {
rc = uv_pages; rc = uv_pages;
...@@ -93,8 +91,7 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, ...@@ -93,8 +91,7 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
y_pages, y_dma.page_count); y_pages, y_dma.page_count);
} }
if (y_pages >= 0) { if (y_pages >= 0) {
for (i = 0; i < y_pages; i++) unpin_user_pages(dma->map, y_pages);
put_page(dma->map[i]);
/* /*
* Inherit the -EFAULT from rc's * Inherit the -EFAULT from rc's
* initialization, but allow it to be * initialization, but allow it to be
...@@ -112,9 +109,7 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma, ...@@ -112,9 +109,7 @@ static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
/* Fill & map SG List */ /* Fill & map SG List */
if (ivtv_udma_fill_sg_list (dma, &uv_dma, ivtv_udma_fill_sg_list (dma, &y_dma, 0)) < 0) { if (ivtv_udma_fill_sg_list (dma, &uv_dma, ivtv_udma_fill_sg_list (dma, &y_dma, 0)) < 0) {
IVTV_DEBUG_WARN("could not allocate bounce buffers for highmem userspace buffers\n"); IVTV_DEBUG_WARN("could not allocate bounce buffers for highmem userspace buffers\n");
for (i = 0; i < dma->page_count; i++) { unpin_user_pages(dma->map, dma->page_count);
put_page(dma->map[i]);
}
dma->page_count = 0; dma->page_count = 0;
return -ENOMEM; return -ENOMEM;
} }
......
...@@ -281,10 +281,10 @@ static int ivtvfb_prep_dec_dma_to_device(struct ivtv *itv, ...@@ -281,10 +281,10 @@ static int ivtvfb_prep_dec_dma_to_device(struct ivtv *itv,
/* Map User DMA */ /* Map User DMA */
if (ivtv_udma_setup(itv, ivtv_dest_addr, userbuf, size_in_bytes) <= 0) { if (ivtv_udma_setup(itv, ivtv_dest_addr, userbuf, size_in_bytes) <= 0) {
mutex_unlock(&itv->udma.lock); mutex_unlock(&itv->udma.lock);
IVTVFB_WARN("ivtvfb_prep_dec_dma_to_device, Error with get_user_pages: %d bytes, %d pages returned\n", IVTVFB_WARN("ivtvfb_prep_dec_dma_to_device, Error with pin_user_pages: %d bytes, %d pages returned\n",
size_in_bytes, itv->udma.page_count); size_in_bytes, itv->udma.page_count);
/* get_user_pages must have failed completely */ /* pin_user_pages must have failed completely */
return -EIO; return -EIO;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment