2019-06-03 05:44:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2007-08-23 19:26:14 +00:00
|
|
|
/*
|
2008-04-22 17:42:13 +00:00
|
|
|
* helper functions for SG DMA video4linux capture buffers
|
2007-08-23 19:26:14 +00:00
|
|
|
*
|
2008-07-17 00:27:49 +00:00
|
|
|
* The functions expect the hardware being able to scatter gather
|
2007-08-23 19:26:14 +00:00
|
|
|
* (i.e. the buffers are not linear in physical memory, but fragmented
|
|
|
|
* into PAGE_SIZE chunks). They also assume the driver does not need
|
|
|
|
* to touch the video data.
|
|
|
|
*
|
MAINTAINERS & files: Canonize the e-mails I use at files
From now on, I'll start using my @kernel.org as my development e-mail.
As such, let's remove the entries that point to the old
mchehab@s-opensource.com at MAINTAINERS file.
For the files written with a copyright with mchehab@s-opensource,
let's keep Samsung on their names, using mchehab+samsung@kernel.org,
in order to keep pointing to my employer, with sponsors the work.
For the files written before I join Samsung (on July, 4 2013),
let's just use mchehab@kernel.org.
For bug reports, we can simply point to just kernel.org, as
this will reach my mchehab+samsung inbox anyway.
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Signed-off-by: Brian Warner <brian.warner@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2018-04-25 09:34:48 +00:00
|
|
|
* (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org>
|
2007-08-23 19:26:14 +00:00
|
|
|
*
|
|
|
|
* Highly based on video-buf written originally by:
|
|
|
|
* (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
|
MAINTAINERS & files: Canonize the e-mails I use at files
From now on, I'll start using my @kernel.org as my development e-mail.
As such, let's remove the entries that point to the old
mchehab@s-opensource.com at MAINTAINERS file.
For the files written with a copyright with mchehab@s-opensource,
let's keep Samsung on their names, using mchehab+samsung@kernel.org,
in order to keep pointing to my employer, with sponsors the work.
For the files written before I join Samsung (on July, 4 2013),
let's just use mchehab@kernel.org.
For bug reports, we can simply point to just kernel.org, as
this will reach my mchehab+samsung inbox anyway.
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Signed-off-by: Brian Warner <brian.warner@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2018-04-25 09:34:48 +00:00
|
|
|
* (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org>
|
2007-08-23 19:26:14 +00:00
|
|
|
* (c) 2006 Ted Walther and John Sokol
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
2017-02-03 23:16:44 +00:00
|
|
|
#include <linux/sched/mm.h>
|
2007-08-23 19:26:14 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
#include <linux/dma-mapping.h>
|
2007-08-23 19:26:14 +00:00
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/pagemap.h>
|
2007-10-23 18:42:11 +00:00
|
|
|
#include <linux/scatterlist.h>
|
2007-08-23 19:26:14 +00:00
|
|
|
#include <asm/page.h>
|
2020-06-09 04:32:38 +00:00
|
|
|
#include <linux/pgtable.h>
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
#include <media/videobuf-dma-sg.h>
|
|
|
|
|
|
|
|
#define MAGIC_DMABUF 0x19721112
|
|
|
|
#define MAGIC_SG_MEM 0x17890714
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
#define MAGIC_CHECK(is, should) \
|
|
|
|
if (unlikely((is) != (should))) { \
|
|
|
|
printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
|
|
|
|
is, should); \
|
|
|
|
BUG(); \
|
|
|
|
}
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2008-04-22 17:41:48 +00:00
|
|
|
static int debug;
|
2007-08-23 19:26:14 +00:00
|
|
|
module_param(debug, int, 0644);
|
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
|
MAINTAINERS & files: Canonize the e-mails I use at files
From now on, I'll start using my @kernel.org as my development e-mail.
As such, let's remove the entries that point to the old
mchehab@s-opensource.com at MAINTAINERS file.
For the files written with a copyright with mchehab@s-opensource,
let's keep Samsung on their names, using mchehab+samsung@kernel.org,
in order to keep pointing to my employer, with sponsors the work.
For the files written before I join Samsung (on July, 4 2013),
let's just use mchehab@kernel.org.
For bug reports, we can simply point to just kernel.org, as
this will reach my mchehab+samsung inbox anyway.
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Signed-off-by: Brian Warner <brian.warner@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2018-04-25 09:34:48 +00:00
|
|
|
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
|
2007-08-23 19:26:14 +00:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
#define dprintk(level, fmt, arg...) \
|
|
|
|
if (debug >= level) \
|
|
|
|
printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
2010-05-11 13:36:32 +00:00
|
|
|
/*
|
|
|
|
* Return a scatterlist for some page-aligned vmalloc()'ed memory
|
|
|
|
* block (NULL on errors). Memory for the scatterlist is allocated
|
|
|
|
* using kmalloc. The caller must free the memory.
|
|
|
|
*/
|
|
|
|
static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
|
|
|
|
int nr_pages)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
|
|
|
struct scatterlist *sglist;
|
|
|
|
struct page *pg;
|
|
|
|
int i;
|
|
|
|
|
treewide: Use array_size() in vzalloc()
The vzalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vzalloc(a * b)
with:
vzalloc(array_size(a, b))
as well as handling cases of:
vzalloc(a * b * c)
with:
vzalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vzalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vzalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vzalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vzalloc(C1 * C2 * C3, ...)
|
vzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vzalloc(C1 * C2, ...)
|
vzalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 21:27:37 +00:00
|
|
|
sglist = vzalloc(array_size(nr_pages, sizeof(*sglist)));
|
2007-08-23 19:26:14 +00:00
|
|
|
if (NULL == sglist)
|
|
|
|
return NULL;
|
2007-10-22 19:19:53 +00:00
|
|
|
sg_init_table(sglist, nr_pages);
|
2007-08-23 19:26:14 +00:00
|
|
|
for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
|
|
|
|
pg = vmalloc_to_page(virt);
|
|
|
|
if (NULL == pg)
|
|
|
|
goto err;
|
|
|
|
BUG_ON(PageHighMem(pg));
|
2007-10-24 09:20:47 +00:00
|
|
|
sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
return sglist;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
err:
|
2009-05-11 14:00:20 +00:00
|
|
|
vfree(sglist);
|
2007-08-23 19:26:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:36:32 +00:00
|
|
|
/*
|
|
|
|
* Return a scatterlist for a an array of userpages (NULL on errors).
|
|
|
|
* Memory for the scatterlist is allocated using kmalloc. The caller
|
|
|
|
* must free the memory.
|
|
|
|
*/
|
|
|
|
static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
|
2010-09-07 09:10:45 +00:00
|
|
|
int nr_pages, int offset, size_t size)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
|
|
|
struct scatterlist *sglist;
|
2008-07-04 09:33:22 +00:00
|
|
|
int i;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
if (NULL == pages[0])
|
|
|
|
return NULL;
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 21:27:11 +00:00
|
|
|
sglist = vmalloc(array_size(nr_pages, sizeof(*sglist)));
|
2007-08-23 19:26:14 +00:00
|
|
|
if (NULL == sglist)
|
|
|
|
return NULL;
|
2007-10-22 19:19:53 +00:00
|
|
|
sg_init_table(sglist, nr_pages);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
if (PageHighMem(pages[0]))
|
|
|
|
/* DMA to highmem pages might not work */
|
|
|
|
goto highmem;
|
2011-04-19 14:54:54 +00:00
|
|
|
sg_set_page(&sglist[0], pages[0],
|
|
|
|
min_t(size_t, PAGE_SIZE - offset, size), offset);
|
|
|
|
size -= min_t(size_t, PAGE_SIZE - offset, size);
|
2007-08-23 19:26:14 +00:00
|
|
|
for (i = 1; i < nr_pages; i++) {
|
|
|
|
if (NULL == pages[i])
|
|
|
|
goto nopage;
|
|
|
|
if (PageHighMem(pages[i]))
|
|
|
|
goto highmem;
|
2010-10-07 12:31:33 +00:00
|
|
|
sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0);
|
|
|
|
size -= min_t(size_t, PAGE_SIZE, size);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
return sglist;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
nopage:
|
|
|
|
dprintk(2, "sgl: oops - no page\n");
|
2009-05-11 14:00:20 +00:00
|
|
|
vfree(sglist);
|
2007-08-23 19:26:14 +00:00
|
|
|
return NULL;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
highmem:
|
|
|
|
dprintk(2, "sgl: oops - highmem page\n");
|
2009-05-11 14:00:20 +00:00
|
|
|
vfree(sglist);
|
2007-08-23 19:26:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2008-04-22 17:42:13 +00:00
|
|
|
struct videobuf_dma_sg_memory *mem = buf->priv;
|
|
|
|
BUG_ON(!mem);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
return &mem->dma;
|
|
|
|
}
|
2010-03-17 07:01:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(videobuf_to_dma);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2014-12-23 12:46:27 +00:00
|
|
|
static void videobuf_dma_init(struct videobuf_dmabuf *dma)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2010-03-17 07:01:04 +00:00
|
|
|
memset(dma, 0, sizeof(*dma));
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->magic = MAGIC_DMABUF;
|
|
|
|
}
|
|
|
|
|
2007-09-27 23:34:09 +00:00
|
|
|
static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
|
|
|
|
int direction, unsigned long data, unsigned long size)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2010-03-17 07:01:04 +00:00
|
|
|
unsigned long first, last;
|
2007-08-23 19:26:14 +00:00
|
|
|
int err, rw = 0;
|
2016-10-13 00:20:16 +00:00
|
|
|
unsigned int flags = FOLL_FORCE;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
dma->direction = direction;
|
|
|
|
switch (dma->direction) {
|
2008-04-22 17:42:13 +00:00
|
|
|
case DMA_FROM_DEVICE:
|
|
|
|
rw = READ;
|
|
|
|
break;
|
|
|
|
case DMA_TO_DEVICE:
|
|
|
|
rw = WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
first = (data & PAGE_MASK) >> PAGE_SHIFT;
|
|
|
|
last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
|
2010-09-07 09:10:45 +00:00
|
|
|
dma->offset = data & ~PAGE_MASK;
|
|
|
|
dma->size = size;
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->nr_pages = last-first+1;
|
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:
kmalloc(a * b, gfp)
with:
kmalloc_array(a * b, gfp)
as well as handling cases of:
kmalloc(a * b * c, gfp)
with:
kmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kmalloc
+ kmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kmalloc(sizeof(THING) * C2, ...)
|
kmalloc(sizeof(TYPE) * C2, ...)
|
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 20:55:00 +00:00
|
|
|
dma->pages = kmalloc_array(dma->nr_pages, sizeof(struct page *),
|
|
|
|
GFP_KERNEL);
|
2007-08-23 19:26:14 +00:00
|
|
|
if (NULL == dma->pages)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-10-13 00:20:16 +00:00
|
|
|
if (rw == READ)
|
|
|
|
flags |= FOLL_WRITE;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
|
|
|
|
data, size, dma->nr_pages);
|
|
|
|
|
2020-01-31 06:13:20 +00:00
|
|
|
err = pin_user_pages(data & PAGE_MASK, dma->nr_pages,
|
mm/gup: replace get_user_pages_longterm() with FOLL_LONGTERM
Pach series "Add FOLL_LONGTERM to GUP fast and use it".
HFI1, qib, and mthca, use get_user_pages_fast() due to its performance
advantages. These pages can be held for a significant time. But
get_user_pages_fast() does not protect against mapping FS DAX pages.
Introduce FOLL_LONGTERM and use this flag in get_user_pages_fast() which
retains the performance while also adding the FS DAX checks. XDP has also
shown interest in using this functionality.[1]
In addition we change get_user_pages() to use the new FOLL_LONGTERM flag
and remove the specialized get_user_pages_longterm call.
[1] https://lkml.org/lkml/2019/3/19/939
"longterm" is a relative thing and at this point is probably a misnomer.
This is really flagging a pin which is going to be given to hardware and
can't move. I've thought of a couple of alternative names but I think we
have to settle on if we are going to use FL_LAYOUT or something else to
solve the "longterm" problem. Then I think we can change the flag to a
better name.
Secondly, it depends on how often you are registering memory. I have
spoken with some RDMA users who consider MR in the performance path...
For the overall application performance. I don't have the numbers as the
tests for HFI1 were done a long time ago. But there was a significant
advantage. Some of which is probably due to the fact that you don't have
to hold mmap_sem.
Finally, architecturally I think it would be good for everyone to use
*_fast. There are patches submitted to the RDMA list which would allow
the use of *_fast (they reworking the use of mmap_sem) and as soon as they
are accepted I'll submit a patch to convert the RDMA core as well. Also
to this point others are looking to use *_fast.
As an aside, Jasons pointed out in my previous submission that *_fast and
*_unlocked look very much the same. I agree and I think further cleanup
will be coming. But I'm focused on getting the final solution for DAX at
the moment.
This patch (of 7):
This patch starts a series which aims to support FOLL_LONGTERM in
get_user_pages_fast(). Some callers who would like to do a longterm (user
controlled pin) of pages with the fast variant of GUP for performance
purposes.
Rather than have a separate get_user_pages_longterm() call, introduce
FOLL_LONGTERM and change the longterm callers to use it.
This patch does not change any functionality. In the short term
"longterm" or user controlled pins are unsafe for Filesystems and FS DAX
in particular has been blocked. However, callers of get_user_pages_fast()
were not "protected".
FOLL_LONGTERM can _only_ be supported with get_user_pages[_fast]() as it
requires vmas to determine if DAX is in use.
NOTE: In merging with the CMA changes we opt to change the
get_user_pages() call in check_and_migrate_cma_pages() to a call of
__get_user_pages_locked() on the newly migrated pages. This makes the
code read better in that we are calling __get_user_pages_locked() on the
pages before and after a potential migration.
As a side affect some of the interfaces are cleaned up but this is not the
primary purpose of the series.
In review[1] it was asked:
<quote>
> This I don't get - if you do lock down long term mappings performance
> of the actual get_user_pages call shouldn't matter to start with.
>
> What do I miss?
A couple of points.
First "longterm" is a relative thing and at this point is probably a
misnomer. This is really flagging a pin which is going to be given to
hardware and can't move. I've thought of a couple of alternative names
but I think we have to settle on if we are going to use FL_LAYOUT or
something else to solve the "longterm" problem. Then I think we can
change the flag to a better name.
Second, It depends on how often you are registering memory. I have spoken
with some RDMA users who consider MR in the performance path... For the
overall application performance. I don't have the numbers as the tests
for HFI1 were done a long time ago. But there was a significant
advantage. Some of which is probably due to the fact that you don't have
to hold mmap_sem.
Finally, architecturally I think it would be good for everyone to use
*_fast. There are patches submitted to the RDMA list which would allow
the use of *_fast (they reworking the use of mmap_sem) and as soon as they
are accepted I'll submit a patch to convert the RDMA core as well. Also
to this point others are looking to use *_fast.
As an asside, Jasons pointed out in my previous submission that *_fast and
*_unlocked look very much the same. I agree and I think further cleanup
will be coming. But I'm focused on getting the final solution for DAX at
the moment.
</quote>
[1] https://lore.kernel.org/lkml/20190220180255.GA12020@iweiny-DESK2.sc.intel.com/T/#md6abad2569f3bf6c1f03686c8097ab6563e94965
[ira.weiny@intel.com: v3]
Link: http://lkml.kernel.org/r/20190328084422.29911-2-ira.weiny@intel.com
Link: http://lkml.kernel.org/r/20190328084422.29911-2-ira.weiny@intel.com
Link: http://lkml.kernel.org/r/20190317183438.2057-2-ira.weiny@intel.com
Signed-off-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Rich Felker <dalias@libc.org>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: James Hogan <jhogan@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Mike Marshall <hubcap@omnibond.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-05-14 00:17:03 +00:00
|
|
|
flags | FOLL_LONGTERM, dma->pages, NULL);
|
2007-09-27 23:34:09 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
if (err != dma->nr_pages) {
|
|
|
|
dma->nr_pages = (err >= 0) ? err : 0;
|
2020-01-31 06:13:20 +00:00
|
|
|
dprintk(1, "pin_user_pages: err=%d [%d]\n", err,
|
2017-11-30 00:10:43 +00:00
|
|
|
dma->nr_pages);
|
2007-08-23 19:26:14 +00:00
|
|
|
return err < 0 ? err : -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-23 12:46:27 +00:00
|
|
|
static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
|
2007-09-27 23:34:09 +00:00
|
|
|
unsigned long data, unsigned long size)
|
|
|
|
{
|
|
|
|
int ret;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-09-27 23:34:09 +00:00
|
|
|
down_read(¤t->mm->mmap_sem);
|
|
|
|
ret = videobuf_dma_init_user_locked(dma, direction, data, size);
|
|
|
|
up_read(¤t->mm->mmap_sem);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-23 12:46:27 +00:00
|
|
|
static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
|
2007-08-23 19:26:14 +00:00
|
|
|
int nr_pages)
|
|
|
|
{
|
2014-06-12 09:53:38 +00:00
|
|
|
int i;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "init kernel [%d pages]\n", nr_pages);
|
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->direction = direction;
|
2014-06-12 09:53:38 +00:00
|
|
|
dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!dma->vaddr_pages)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
|
|
|
|
if (!dma->dma_addr) {
|
|
|
|
kfree(dma->vaddr_pages);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nr_pages; i++) {
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
|
|
|
|
&(dma->dma_addr[i]), GFP_KERNEL);
|
|
|
|
if (addr == NULL)
|
|
|
|
goto out_free_pages;
|
|
|
|
|
|
|
|
dma->vaddr_pages[i] = virt_to_page(addr);
|
|
|
|
}
|
|
|
|
dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
|
|
|
|
PAGE_KERNEL);
|
2010-05-11 13:36:34 +00:00
|
|
|
if (NULL == dma->vaddr) {
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
|
2014-06-12 09:53:38 +00:00
|
|
|
goto out_free_pages;
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2018-03-22 19:00:32 +00:00
|
|
|
dprintk(1, "vmalloc is at addr %p, size=%d\n",
|
|
|
|
dma->vaddr, nr_pages << PAGE_SHIFT);
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2010-05-11 13:36:34 +00:00
|
|
|
memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->nr_pages = nr_pages;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
return 0;
|
2014-06-12 09:53:38 +00:00
|
|
|
out_free_pages:
|
|
|
|
while (i > 0) {
|
2014-08-05 08:11:13 +00:00
|
|
|
void *addr;
|
|
|
|
|
2014-06-12 09:53:38 +00:00
|
|
|
i--;
|
2014-08-05 08:11:13 +00:00
|
|
|
addr = page_address(dma->vaddr_pages[i]);
|
|
|
|
dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]);
|
2014-06-12 09:53:38 +00:00
|
|
|
}
|
|
|
|
kfree(dma->dma_addr);
|
|
|
|
dma->dma_addr = NULL;
|
|
|
|
kfree(dma->vaddr_pages);
|
|
|
|
dma->vaddr_pages = NULL;
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 12:46:27 +00:00
|
|
|
static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
|
2007-08-23 19:26:14 +00:00
|
|
|
dma_addr_t addr, int nr_pages)
|
|
|
|
{
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
|
|
|
|
nr_pages, (unsigned long)addr);
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->direction = direction;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
if (0 == addr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
dma->bus_addr = addr;
|
|
|
|
dma->nr_pages = nr_pages;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-23 12:46:27 +00:00
|
|
|
static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2010-03-17 07:01:04 +00:00
|
|
|
MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
|
2007-08-23 19:26:14 +00:00
|
|
|
BUG_ON(0 == dma->nr_pages);
|
|
|
|
|
|
|
|
if (dma->pages) {
|
|
|
|
dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
|
2010-09-07 09:10:45 +00:00
|
|
|
dma->offset, dma->size);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2010-05-11 13:36:34 +00:00
|
|
|
if (dma->vaddr) {
|
|
|
|
dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
|
2010-03-17 07:01:04 +00:00
|
|
|
dma->nr_pages);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
if (dma->bus_addr) {
|
2009-05-11 14:00:20 +00:00
|
|
|
dma->sglist = vmalloc(sizeof(*dma->sglist));
|
2007-08-23 19:26:14 +00:00
|
|
|
if (NULL != dma->sglist) {
|
2010-03-17 07:01:04 +00:00
|
|
|
dma->sglen = 1;
|
|
|
|
sg_dma_address(&dma->sglist[0]) = dma->bus_addr
|
|
|
|
& PAGE_MASK;
|
|
|
|
dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
|
|
|
|
sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NULL == dma->sglist) {
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "scatterlist is NULL\n");
|
2007-08-23 19:26:14 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
if (!dma->bus_addr) {
|
2010-05-11 13:36:30 +00:00
|
|
|
dma->sglen = dma_map_sg(dev, dma->sglist,
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->nr_pages, dma->direction);
|
|
|
|
if (0 == dma->sglen) {
|
|
|
|
printk(KERN_WARNING
|
2010-03-17 07:01:04 +00:00
|
|
|
"%s: videobuf_map_sg failed\n", __func__);
|
2009-05-11 14:00:20 +00:00
|
|
|
vfree(dma->sglist);
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->sglist = NULL;
|
|
|
|
dma->sglen = 0;
|
2009-06-11 02:17:27 +00:00
|
|
|
return -ENOMEM;
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:36:30 +00:00
|
|
|
int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2008-04-22 17:42:13 +00:00
|
|
|
MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
if (!dma->sglen)
|
|
|
|
return 0;
|
|
|
|
|
2018-04-30 16:56:28 +00:00
|
|
|
dma_unmap_sg(dev, dma->sglist, dma->nr_pages, dma->direction);
|
2007-10-08 14:43:49 +00:00
|
|
|
|
2009-05-11 14:00:20 +00:00
|
|
|
vfree(dma->sglist);
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->sglist = NULL;
|
|
|
|
dma->sglen = 0;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-17 07:01:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
int videobuf_dma_free(struct videobuf_dmabuf *dma)
|
|
|
|
{
|
2010-03-17 07:01:04 +00:00
|
|
|
int i;
|
|
|
|
MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
|
2007-08-23 19:26:14 +00:00
|
|
|
BUG_ON(dma->sglen);
|
|
|
|
|
|
|
|
if (dma->pages) {
|
2020-01-31 06:13:35 +00:00
|
|
|
unpin_user_pages_dirty_lock(dma->pages, dma->nr_pages,
|
|
|
|
dma->direction == DMA_FROM_DEVICE);
|
2007-08-23 19:26:14 +00:00
|
|
|
kfree(dma->pages);
|
|
|
|
dma->pages = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-12 09:53:38 +00:00
|
|
|
if (dma->dma_addr) {
|
|
|
|
for (i = 0; i < dma->nr_pages; i++) {
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
addr = page_address(dma->vaddr_pages[i]);
|
|
|
|
dma_free_coherent(dma->dev, PAGE_SIZE, addr,
|
|
|
|
dma->dma_addr[i]);
|
|
|
|
}
|
|
|
|
kfree(dma->dma_addr);
|
|
|
|
dma->dma_addr = NULL;
|
|
|
|
kfree(dma->vaddr_pages);
|
|
|
|
dma->vaddr_pages = NULL;
|
|
|
|
vunmap(dma->vaddr);
|
|
|
|
dma->vaddr = NULL;
|
|
|
|
}
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
if (dma->bus_addr)
|
2007-08-23 19:26:14 +00:00
|
|
|
dma->bus_addr = 0;
|
2008-04-22 17:42:13 +00:00
|
|
|
dma->direction = DMA_NONE;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-17 07:01:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(videobuf_dma_free);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
static void videobuf_vm_open(struct vm_area_struct *vma)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
|
|
|
struct videobuf_mapping *map = vma->vm_private_data;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
|
|
|
|
map->count, vma->vm_start, vma->vm_end);
|
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
map->count++;
|
|
|
|
}
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
static void videobuf_vm_close(struct vm_area_struct *vma)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
|
|
|
struct videobuf_mapping *map = vma->vm_private_data;
|
|
|
|
struct videobuf_queue *q = map->q;
|
2008-04-22 17:42:13 +00:00
|
|
|
struct videobuf_dma_sg_memory *mem;
|
2007-08-23 19:26:14 +00:00
|
|
|
int i;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
|
|
|
|
map->count, vma->vm_start, vma->vm_end);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2014-01-03 11:10:49 +00:00
|
|
|
map->count--;
|
|
|
|
if (0 == map->count) {
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "munmap %p q=%p\n", map, q);
|
2014-01-03 11:10:49 +00:00
|
|
|
videobuf_queue_lock(q);
|
2007-08-23 19:26:14 +00:00
|
|
|
for (i = 0; i < VIDEO_MAX_FRAME; i++) {
|
|
|
|
if (NULL == q->bufs[i])
|
|
|
|
continue;
|
2010-03-17 07:01:04 +00:00
|
|
|
mem = q->bufs[i]->priv;
|
2007-08-23 19:26:14 +00:00
|
|
|
if (!mem)
|
|
|
|
continue;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2007-09-27 21:25:44 +00:00
|
|
|
if (q->bufs[i]->map != map)
|
2007-08-23 19:26:14 +00:00
|
|
|
continue;
|
2007-09-27 21:25:44 +00:00
|
|
|
q->bufs[i]->map = NULL;
|
2007-08-23 19:26:14 +00:00
|
|
|
q->bufs[i]->baddr = 0;
|
2010-03-17 07:01:04 +00:00
|
|
|
q->ops->buf_release(q, q->bufs[i]);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2014-01-03 11:10:49 +00:00
|
|
|
videobuf_queue_unlock(q);
|
2007-08-23 19:26:14 +00:00
|
|
|
kfree(map);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a anonymous page for the mapping. Make sure we can DMA to that
|
|
|
|
* memory location with 32bit PCI devices (i.e. don't use highmem for
|
|
|
|
* now ...). Bounce buffers don't work very well for the data rates
|
|
|
|
* video capture has.
|
|
|
|
*/
|
2018-04-18 18:47:16 +00:00
|
|
|
static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2017-02-24 22:56:41 +00:00
|
|
|
struct vm_area_struct *vma = vmf->vma;
|
2007-08-23 19:26:14 +00:00
|
|
|
struct page *page;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
|
2016-12-14 23:07:01 +00:00
|
|
|
vmf->address, vma->vm_start, vma->vm_end);
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
page = alloc_page(GFP_USER | __GFP_DMA32);
|
|
|
|
if (!page)
|
2007-12-07 20:57:38 +00:00
|
|
|
return VM_FAULT_OOM;
|
2016-12-14 23:07:01 +00:00
|
|
|
clear_user_highpage(page, vmf->address);
|
2007-12-07 20:57:38 +00:00
|
|
|
vmf->page = page;
|
2010-03-17 07:01:04 +00:00
|
|
|
|
2007-12-07 20:57:38 +00:00
|
|
|
return 0;
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
static const struct vm_operations_struct videobuf_vm_ops = {
|
|
|
|
.open = videobuf_vm_open,
|
|
|
|
.close = videobuf_vm_close,
|
|
|
|
.fault = videobuf_vm_fault,
|
2007-08-23 19:26:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------
|
2008-04-22 17:42:13 +00:00
|
|
|
* SG handlers for the generic methods
|
2007-08-23 19:26:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocated area consists on 3 parts:
|
|
|
|
struct video_buffer
|
|
|
|
struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
|
2008-04-22 17:42:13 +00:00
|
|
|
struct videobuf_dma_sg_memory
|
2007-08-23 19:26:14 +00:00
|
|
|
*/
|
|
|
|
|
2010-05-11 13:36:28 +00:00
|
|
|
static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2008-04-22 17:42:13 +00:00
|
|
|
struct videobuf_dma_sg_memory *mem;
|
2007-08-23 19:26:14 +00:00
|
|
|
struct videobuf_buffer *vb;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
|
2010-02-22 16:10:06 +00:00
|
|
|
if (!vb)
|
|
|
|
return vb;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
mem = vb->priv = ((char *)vb) + size;
|
|
|
|
mem->magic = MAGIC_SG_MEM;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
videobuf_dma_init(&mem->dma);
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
|
|
|
|
__func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
|
|
|
|
mem, (long)sizeof(*mem));
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
return vb;
|
|
|
|
}
|
|
|
|
|
2010-03-28 11:18:37 +00:00
|
|
|
static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
|
2008-04-13 18:10:00 +00:00
|
|
|
{
|
|
|
|
struct videobuf_dma_sg_memory *mem = buf->priv;
|
|
|
|
BUG_ON(!mem);
|
|
|
|
|
|
|
|
MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
|
|
|
|
|
2010-05-11 13:36:34 +00:00
|
|
|
return mem->dma.vaddr;
|
2008-04-13 18:10:00 +00:00
|
|
|
}
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
static int __videobuf_iolock(struct videobuf_queue *q,
|
|
|
|
struct videobuf_buffer *vb,
|
|
|
|
struct v4l2_framebuffer *fbuf)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2010-03-17 07:01:04 +00:00
|
|
|
int err, pages;
|
2007-08-23 19:26:14 +00:00
|
|
|
dma_addr_t bus;
|
2008-04-22 17:42:13 +00:00
|
|
|
struct videobuf_dma_sg_memory *mem = vb->priv;
|
2007-08-23 19:26:14 +00:00
|
|
|
BUG_ON(!mem);
|
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2014-06-12 09:53:38 +00:00
|
|
|
if (!mem->dma.dev)
|
|
|
|
mem->dma.dev = q->dev;
|
|
|
|
else
|
|
|
|
WARN_ON(mem->dma.dev != q->dev);
|
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
switch (vb->memory) {
|
|
|
|
case V4L2_MEMORY_MMAP:
|
|
|
|
case V4L2_MEMORY_USERPTR:
|
|
|
|
if (0 == vb->baddr) {
|
|
|
|
/* no userspace addr -- kernel bounce buffer */
|
|
|
|
pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
|
2010-03-17 07:01:04 +00:00
|
|
|
err = videobuf_dma_init_kernel(&mem->dma,
|
|
|
|
DMA_FROM_DEVICE,
|
|
|
|
pages);
|
2007-08-23 19:26:14 +00:00
|
|
|
if (0 != err)
|
|
|
|
return err;
|
2007-09-27 23:34:09 +00:00
|
|
|
} else if (vb->memory == V4L2_MEMORY_USERPTR) {
|
2007-08-23 19:26:14 +00:00
|
|
|
/* dma directly to userspace */
|
2010-03-17 07:01:04 +00:00
|
|
|
err = videobuf_dma_init_user(&mem->dma,
|
|
|
|
DMA_FROM_DEVICE,
|
|
|
|
vb->baddr, vb->bsize);
|
2007-08-23 19:26:14 +00:00
|
|
|
if (0 != err)
|
|
|
|
return err;
|
2007-09-27 23:34:09 +00:00
|
|
|
} else {
|
|
|
|
/* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
|
|
|
|
buffers can only be called from videobuf_qbuf
|
|
|
|
we take current->mm->mmap_sem there, to prevent
|
|
|
|
locking inversion, so don't take it here */
|
|
|
|
|
|
|
|
err = videobuf_dma_init_user_locked(&mem->dma,
|
2008-04-22 17:42:13 +00:00
|
|
|
DMA_FROM_DEVICE,
|
2007-09-27 23:34:09 +00:00
|
|
|
vb->baddr, vb->bsize);
|
|
|
|
if (0 != err)
|
|
|
|
return err;
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case V4L2_MEMORY_OVERLAY:
|
|
|
|
if (NULL == fbuf)
|
|
|
|
return -EINVAL;
|
|
|
|
/* FIXME: need sanity checks for vb->boff */
|
|
|
|
/*
|
|
|
|
* Using a double cast to avoid compiler warnings when
|
|
|
|
* building for PAE. Compiler doesn't like direct casting
|
|
|
|
* of a 32 bit ptr to 64 bit integer.
|
|
|
|
*/
|
|
|
|
bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
|
|
|
|
pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
|
2008-04-22 17:42:13 +00:00
|
|
|
err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
|
2007-08-23 19:26:14 +00:00
|
|
|
bus, pages);
|
|
|
|
if (0 != err)
|
|
|
|
return err;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
2010-05-11 13:36:30 +00:00
|
|
|
err = videobuf_dma_map(q->dev, &mem->dma);
|
2007-08-23 19:26:14 +00:00
|
|
|
if (0 != err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __videobuf_sync(struct videobuf_queue *q,
|
|
|
|
struct videobuf_buffer *buf)
|
|
|
|
{
|
2008-04-22 17:42:13 +00:00
|
|
|
struct videobuf_dma_sg_memory *mem = buf->priv;
|
2010-05-05 19:23:09 +00:00
|
|
|
BUG_ON(!mem || !mem->dma.sglen);
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
|
2010-05-05 19:23:09 +00:00
|
|
|
MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2010-05-05 19:23:09 +00:00
|
|
|
dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
|
2018-04-30 16:56:28 +00:00
|
|
|
mem->dma.nr_pages, mem->dma.direction);
|
2010-05-05 19:23:09 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __videobuf_mmap_mapper(struct videobuf_queue *q,
|
2010-03-28 12:09:05 +00:00
|
|
|
struct videobuf_buffer *buf,
|
|
|
|
struct vm_area_struct *vma)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2010-03-28 12:09:05 +00:00
|
|
|
struct videobuf_dma_sg_memory *mem = buf->priv;
|
2007-08-23 19:26:14 +00:00
|
|
|
struct videobuf_mapping *map;
|
2010-04-25 14:23:52 +00:00
|
|
|
unsigned int first, last, size = 0, i;
|
2007-08-23 19:26:14 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = -EINVAL;
|
|
|
|
|
2010-03-28 12:09:05 +00:00
|
|
|
BUG_ON(!mem);
|
|
|
|
MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
|
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
/* look for first buffer to map */
|
|
|
|
for (first = 0; first < VIDEO_MAX_FRAME; first++) {
|
2010-03-28 12:09:05 +00:00
|
|
|
if (buf == q->bufs[first]) {
|
|
|
|
size = PAGE_ALIGN(q->bufs[first]->bsize);
|
2007-08-23 19:26:14 +00:00
|
|
|
break;
|
2010-03-28 12:09:05 +00:00
|
|
|
}
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2010-03-28 12:09:05 +00:00
|
|
|
|
|
|
|
/* paranoia, should never happen since buf is always valid. */
|
2010-04-25 14:23:52 +00:00
|
|
|
if (!size) {
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
|
2010-03-28 12:09:05 +00:00
|
|
|
(vma->vm_pgoff << PAGE_SHIFT));
|
2007-08-23 19:26:14 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-03-28 12:09:05 +00:00
|
|
|
last = first;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
|
|
|
/* create mapping + update buffer list */
|
|
|
|
retval = -ENOMEM;
|
2010-03-17 07:01:04 +00:00
|
|
|
map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
|
2007-08-23 19:26:14 +00:00
|
|
|
if (NULL == map)
|
|
|
|
goto done;
|
2008-04-02 21:10:59 +00:00
|
|
|
|
|
|
|
size = 0;
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
if (NULL == q->bufs[i])
|
|
|
|
continue;
|
2007-09-27 21:25:44 +00:00
|
|
|
q->bufs[i]->map = map;
|
2007-08-23 19:26:14 +00:00
|
|
|
q->bufs[i]->baddr = vma->vm_start + size;
|
2009-07-23 13:56:25 +00:00
|
|
|
size += PAGE_ALIGN(q->bufs[i]->bsize);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2008-04-02 21:10:59 +00:00
|
|
|
|
2007-08-23 19:26:14 +00:00
|
|
|
map->count = 1;
|
|
|
|
map->q = q;
|
|
|
|
vma->vm_ops = &videobuf_vm_ops;
|
mm: kill vma flag VM_RESERVED and mm->reserved_vm counter
A long time ago, in v2.4, VM_RESERVED kept swapout process off VMA,
currently it lost original meaning but still has some effects:
| effect | alternative flags
-+------------------------+---------------------------------------------
1| account as reserved_vm | VM_IO
2| skip in core dump | VM_IO, VM_DONTDUMP
3| do not merge or expand | VM_IO, VM_DONTEXPAND, VM_HUGETLB, VM_PFNMAP
4| do not mlock | VM_IO, VM_DONTEXPAND, VM_HUGETLB, VM_PFNMAP
This patch removes reserved_vm counter from mm_struct. Seems like nobody
cares about it, it does not exported into userspace directly, it only
reduces total_vm showed in proc.
Thus VM_RESERVED can be replaced with VM_IO or pair VM_DONTEXPAND | VM_DONTDUMP.
remap_pfn_range() and io_remap_pfn_range() set VM_IO|VM_DONTEXPAND|VM_DONTDUMP.
remap_vmalloc_range() set VM_DONTEXPAND | VM_DONTDUMP.
[akpm@linux-foundation.org: drivers/vfio/pci/vfio_pci.c fixup]
Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Carsten Otte <cotte@de.ibm.com>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Eric Paris <eparis@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Morris <james.l.morris@oracle.com>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Kentaro Takeda <takedakn@nttdata.co.jp>
Cc: Matt Helsley <matthltc@us.ibm.com>
Cc: Nick Piggin <npiggin@kernel.dk>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Robert Richter <robert.richter@amd.com>
Cc: Suresh Siddha <suresh.b.siddha@intel.com>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Venkatesh Pallipadi <venki@google.com>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-08 23:29:02 +00:00
|
|
|
vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
|
2007-08-23 19:26:14 +00:00
|
|
|
vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
|
|
|
|
vma->vm_private_data = map;
|
2010-03-17 07:01:04 +00:00
|
|
|
dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
|
|
|
|
map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
|
2007-08-23 19:26:14 +00:00
|
|
|
retval = 0;
|
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
done:
|
2007-08-23 19:26:14 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
static struct videobuf_qtype_ops sg_ops = {
|
2007-08-23 19:26:14 +00:00
|
|
|
.magic = MAGIC_QTYPE_OPS,
|
|
|
|
|
2010-05-11 13:36:28 +00:00
|
|
|
.alloc_vb = __videobuf_alloc_vb,
|
2007-08-23 19:26:14 +00:00
|
|
|
.iolock = __videobuf_iolock,
|
|
|
|
.sync = __videobuf_sync,
|
|
|
|
.mmap_mapper = __videobuf_mmap_mapper,
|
2010-03-28 11:18:37 +00:00
|
|
|
.vaddr = __videobuf_to_vaddr,
|
2007-08-23 19:26:14 +00:00
|
|
|
};
|
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
void *videobuf_sg_alloc(size_t size)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
|
|
|
struct videobuf_queue q;
|
|
|
|
|
|
|
|
/* Required to make generic handler to call __videobuf_alloc */
|
2008-04-22 17:42:13 +00:00
|
|
|
q.int_ops = &sg_ops;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2008-04-22 17:42:13 +00:00
|
|
|
q.msize = size;
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2010-05-11 13:36:28 +00:00
|
|
|
return videobuf_alloc_vb(&q);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2010-03-17 07:01:04 +00:00
|
|
|
EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
|
2007-08-23 19:26:14 +00:00
|
|
|
|
2010-03-17 07:01:04 +00:00
|
|
|
void videobuf_queue_sg_init(struct videobuf_queue *q,
|
2009-11-17 22:43:41 +00:00
|
|
|
const struct videobuf_queue_ops *ops,
|
2008-04-22 17:42:13 +00:00
|
|
|
struct device *dev,
|
2007-08-23 19:26:14 +00:00
|
|
|
spinlock_t *irqlock,
|
|
|
|
enum v4l2_buf_type type,
|
|
|
|
enum v4l2_field field,
|
|
|
|
unsigned int msize,
|
2010-09-20 20:39:46 +00:00
|
|
|
void *priv,
|
|
|
|
struct mutex *ext_lock)
|
2007-08-23 19:26:14 +00:00
|
|
|
{
|
2007-10-08 15:20:02 +00:00
|
|
|
videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
|
2010-09-20 20:39:46 +00:00
|
|
|
priv, &sg_ops, ext_lock);
|
2007-08-23 19:26:14 +00:00
|
|
|
}
|
2008-04-22 17:42:13 +00:00
|
|
|
EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);
|
2007-08-23 19:26:14 +00:00
|
|
|
|