mirror of
https://github.com/torvalds/linux.git
synced 2024-12-28 22:02:28 +00:00
227ae227c9
vb2_get_vma() copy the content of the vma to a new structure but set some of its pointers to NULL. One of this pointer is used by follow_pte() called by follow_pfn() on io memory. This can lead to a NULL pointer derreference. The version of vma that has not been cleared must be used. [ 406.143320] BUG: unable to handle kernel NULL pointer dereference at 0000000000000040 [ 406.143427] IP: [<ffffffff8115204c>] follow_pfn+0x2c/0x70 [ 406.143491] PGD 6c3f0067 PUD 6c3ef067 PMD 0 [ 406.143546] Oops: 0000 [#1] SMP [ 406.143587] Modules linked in: qtec_mem qt5023_video qtec_testgen qtec_xform videobuf2_core gpio_xilinx videobuf2_vmalloc videobuf2_dma_sg qtec_cmosis videobuf2_memops qtec_pcie qtec_white fglrx(PO) qt5023 spi_xilinx spi_bitbang [ 406.143852] CPU: 0 PID: 299 Comm: tracker Tainted: P O 3.13.0-qtec-standard #10 [ 406.143927] Hardware name: QTechnology QT5022/QT5022, BIOS PM_2.1.0.309 X64 04/04/2013 [ 406.144000] task: ffff880085c82d60 ti: ffff880085abe000 task.ti: ffff880085abe000 [ 406.144067] RIP: 0010:[<ffffffff8115204c>] [<ffffffff8115204c>] follow_pfn+0x2c/0x70 [ 406.144145] RSP: 0018:ffff880085abf888 EFLAGS: 00010296 [ 406.144195] RAX: 0000000000000000 RBX: ffff880085abf8e0 RCX: ffff880085abf888 [ 406.144260] RDX: ffff880085abf890 RSI: 00007fc52e173000 RDI: ffff8800863cbe40 [ 406.144325] RBP: ffff880085abf8a8 R08: 0000000000000018 R09: ffff8800863cbf00 [ 406.144388] R10: ffff880086703b80 R11: 00000000000001e0 R12: 0000000000018000 [ 406.144452] R13: 0000000000000000 R14: ffffea0000000000 R15: ffff88015922fea0 [ 406.144517] FS: 00007fc536e7c740(0000) GS:ffff88015ec00000(0000) knlGS:0000000000000000 [ 406.144591] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 406.144644] CR2: 0000000000000040 CR3: 0000000066c9d000 CR4: 00000000000007f0 [ 406.144708] Stack: [ 406.144731] 0000000000018000 00007fc52e18b000 0000000000000000 00007fc52e173000 [ 406.144813] ffff880085abf918 ffffffffa083b2fd ffff880085ab1ba8 0000000000000000 [ 406.144894] 0000000000000000 0000000100000000 ffff880085abf928 ffff880159a20800 [ 406.144976] Call Trace: [ 406.145011] [<ffffffffa083b2fd>] vb2_dma_sg_get_userptr+0x14d/0x310 [videobuf2_dma_sg] [ 406.145089] [<ffffffffa08507df>] __qbuf_userptr+0xbf/0x3e0 [videobuf2_core] [ 406.147229] [<ffffffffa0041454>] ? mc_heap_lock_memory+0x1f4/0x490 [fglrx] [ 406.149234] [<ffffffff813428f3>] ? cpumask_next_and+0x23/0x50 [ 406.151223] [<ffffffff810b2e38>] ? enqueue_task_fair+0x658/0xde0 [ 406.153199] [<ffffffff81061888>] ? native_smp_send_reschedule+0x48/0x60 [ 406.155184] [<ffffffff815836b9>] ? get_ctrl+0xa9/0xd0 [ 406.157161] [<ffffffff8116f4e4>] ? __kmalloc+0x1a4/0x1b0 [ 406.159135] [<ffffffffa0850b9c>] ? __vb2_queue_alloc+0x9c/0x4a0 [videobuf2_core] [ 406.161130] [<ffffffffa0852d08>] __buf_prepare+0x1a8/0x210 [videobuf2_core] [ 406.163171] [<ffffffffa0854c57>] __vb2_qbuf+0x27/0xcc [videobuf2_core] [ 406.165229] [<ffffffffa0851dfd>] vb2_queue_or_prepare_buf+0x1ed/0x270 [videobuf2_core] [ 406.167325] [<ffffffffa0854c30>] ? vb2_ioctl_querybuf+0x30/0x30 [videobuf2_core] [ 406.169419] [<ffffffffa0851e9c>] vb2_qbuf+0x1c/0x20 [videobuf2_core] [ 406.171508] [<ffffffffa0851ef8>] vb2_ioctl_qbuf+0x58/0x70 [videobuf2_core] [ 406.173604] [<ffffffff8157d3a8>] v4l_qbuf+0x48/0x60 [ 406.175681] [<ffffffff8157b29c>] __video_do_ioctl+0x2bc/0x340 [ 406.177779] [<ffffffff8116f43c>] ? __kmalloc+0xfc/0x1b0 [ 406.179883] [<ffffffff8157cd0e>] ? video_usercopy+0x7e/0x470 [ 406.181961] [<ffffffff8157ce81>] video_usercopy+0x1f1/0x470 [ 406.184021] [<ffffffff8157afe0>] ? v4l_printk_ioctl+0xb0/0xb0 [ 406.186085] [<ffffffff810ae1ed>] ? account_system_time+0x8d/0x190 [ 406.188149] [<ffffffff8157d115>] video_ioctl2+0x15/0x20 [ 406.190216] [<ffffffff815781b3>] v4l2_ioctl+0x123/0x160 [ 406.192251] [<ffffffff810ce415>] ? rcu_eqs_enter+0x65/0xa0 [ 406.194256] [<ffffffff81186b28>] do_vfs_ioctl+0x88/0x560 [ 406.196258] [<ffffffff810ae145>] ? account_user_time+0x95/0xb0 [ 406.198262] [<ffffffff810ae6a4>] ? vtime_account_user+0x44/0x70 [ 406.200215] [<ffffffff81187091>] SyS_ioctl+0x91/0xb0 [ 406.202107] [<ffffffff817be109>] tracesys+0xd0/0xd5 [ 406.203946] Code: 66 66 66 90 48 f7 47 50 00 44 00 00 b8 ea ff ff ff 74 52 55 48 89 e5 53 48 89 d3 48 8d 4d e0 48 8d 55 e8 48 83 ec 18 48 8b 47 40 <48> 8b 78 40 e8 8b fe ff ff 85 c0 75 27 48 8b 55 e8 48 b9 00 f0 [ 406.208011] RIP [<ffffffff8115204c>] follow_pfn+0x2c/0x70 [ 406.209908] RSP <ffff880085abf888> [ 406.211760] CR2: 0000000000000040 [ 406.213676] ---[ end trace 996d9f64e6739a04 ]--- Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com> Acked-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
360 lines
8.0 KiB
C
360 lines
8.0 KiB
C
/*
|
|
* videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
|
|
*
|
|
* Copyright (C) 2010 Samsung Electronics
|
|
*
|
|
* Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/scatterlist.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <media/videobuf2-core.h>
|
|
#include <media/videobuf2-memops.h>
|
|
#include <media/videobuf2-dma-sg.h>
|
|
|
|
static int debug;
|
|
module_param(debug, int, 0644);
|
|
|
|
#define dprintk(level, fmt, arg...) \
|
|
do { \
|
|
if (debug >= level) \
|
|
printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
|
|
} while (0)
|
|
|
|
struct vb2_dma_sg_buf {
|
|
void *vaddr;
|
|
struct page **pages;
|
|
int write;
|
|
int offset;
|
|
struct sg_table sg_table;
|
|
size_t size;
|
|
unsigned int num_pages;
|
|
atomic_t refcount;
|
|
struct vb2_vmarea_handler handler;
|
|
struct vm_area_struct *vma;
|
|
};
|
|
|
|
static void vb2_dma_sg_put(void *buf_priv);
|
|
|
|
static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
|
|
gfp_t gfp_flags)
|
|
{
|
|
unsigned int last_page = 0;
|
|
int size = buf->size;
|
|
|
|
while (size > 0) {
|
|
struct page *pages;
|
|
int order;
|
|
int i;
|
|
|
|
order = get_order(size);
|
|
/* Dont over allocate*/
|
|
if ((PAGE_SIZE << order) > size)
|
|
order--;
|
|
|
|
pages = NULL;
|
|
while (!pages) {
|
|
pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
|
|
__GFP_NOWARN | gfp_flags, order);
|
|
if (pages)
|
|
break;
|
|
|
|
if (order == 0) {
|
|
while (last_page--)
|
|
__free_page(buf->pages[last_page]);
|
|
return -ENOMEM;
|
|
}
|
|
order--;
|
|
}
|
|
|
|
split_page(pages, order);
|
|
for (i = 0; i < (1 << order); i++)
|
|
buf->pages[last_page++] = &pages[i];
|
|
|
|
size -= PAGE_SIZE << order;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size, gfp_t gfp_flags)
|
|
{
|
|
struct vb2_dma_sg_buf *buf;
|
|
int ret;
|
|
int num_pages;
|
|
|
|
buf = kzalloc(sizeof *buf, GFP_KERNEL);
|
|
if (!buf)
|
|
return NULL;
|
|
|
|
buf->vaddr = NULL;
|
|
buf->write = 0;
|
|
buf->offset = 0;
|
|
buf->size = size;
|
|
/* size is already page aligned */
|
|
buf->num_pages = size >> PAGE_SHIFT;
|
|
|
|
buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
|
|
GFP_KERNEL);
|
|
if (!buf->pages)
|
|
goto fail_pages_array_alloc;
|
|
|
|
ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
|
|
if (ret)
|
|
goto fail_pages_alloc;
|
|
|
|
ret = sg_alloc_table_from_pages(&buf->sg_table, buf->pages,
|
|
buf->num_pages, 0, size, gfp_flags);
|
|
if (ret)
|
|
goto fail_table_alloc;
|
|
|
|
buf->handler.refcount = &buf->refcount;
|
|
buf->handler.put = vb2_dma_sg_put;
|
|
buf->handler.arg = buf;
|
|
|
|
atomic_inc(&buf->refcount);
|
|
|
|
dprintk(1, "%s: Allocated buffer of %d pages\n",
|
|
__func__, buf->num_pages);
|
|
return buf;
|
|
|
|
fail_table_alloc:
|
|
num_pages = buf->num_pages;
|
|
while (num_pages--)
|
|
__free_page(buf->pages[num_pages]);
|
|
fail_pages_alloc:
|
|
kfree(buf->pages);
|
|
fail_pages_array_alloc:
|
|
kfree(buf);
|
|
return NULL;
|
|
}
|
|
|
|
static void vb2_dma_sg_put(void *buf_priv)
|
|
{
|
|
struct vb2_dma_sg_buf *buf = buf_priv;
|
|
int i = buf->num_pages;
|
|
|
|
if (atomic_dec_and_test(&buf->refcount)) {
|
|
dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
|
|
buf->num_pages);
|
|
if (buf->vaddr)
|
|
vm_unmap_ram(buf->vaddr, buf->num_pages);
|
|
sg_free_table(&buf->sg_table);
|
|
while (--i >= 0)
|
|
__free_page(buf->pages[i]);
|
|
kfree(buf->pages);
|
|
kfree(buf);
|
|
}
|
|
}
|
|
|
|
static inline int vma_is_io(struct vm_area_struct *vma)
|
|
{
|
|
return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
|
|
}
|
|
|
|
static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
|
|
unsigned long size, int write)
|
|
{
|
|
struct vb2_dma_sg_buf *buf;
|
|
unsigned long first, last;
|
|
int num_pages_from_user;
|
|
struct vm_area_struct *vma;
|
|
|
|
buf = kzalloc(sizeof *buf, GFP_KERNEL);
|
|
if (!buf)
|
|
return NULL;
|
|
|
|
buf->vaddr = NULL;
|
|
buf->write = write;
|
|
buf->offset = vaddr & ~PAGE_MASK;
|
|
buf->size = size;
|
|
|
|
first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
|
|
last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
|
|
buf->num_pages = last - first + 1;
|
|
|
|
buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
|
|
GFP_KERNEL);
|
|
if (!buf->pages)
|
|
goto userptr_fail_alloc_pages;
|
|
|
|
vma = find_vma(current->mm, vaddr);
|
|
if (!vma) {
|
|
dprintk(1, "no vma for address %lu\n", vaddr);
|
|
goto userptr_fail_find_vma;
|
|
}
|
|
|
|
if (vma->vm_end < vaddr + size) {
|
|
dprintk(1, "vma at %lu is too small for %lu bytes\n",
|
|
vaddr, size);
|
|
goto userptr_fail_find_vma;
|
|
}
|
|
|
|
buf->vma = vb2_get_vma(vma);
|
|
if (!buf->vma) {
|
|
dprintk(1, "failed to copy vma\n");
|
|
goto userptr_fail_find_vma;
|
|
}
|
|
|
|
if (vma_is_io(buf->vma)) {
|
|
for (num_pages_from_user = 0;
|
|
num_pages_from_user < buf->num_pages;
|
|
++num_pages_from_user, vaddr += PAGE_SIZE) {
|
|
unsigned long pfn;
|
|
|
|
if (follow_pfn(vma, vaddr, &pfn)) {
|
|
dprintk(1, "no page for address %lu\n", vaddr);
|
|
break;
|
|
}
|
|
buf->pages[num_pages_from_user] = pfn_to_page(pfn);
|
|
}
|
|
} else
|
|
num_pages_from_user = get_user_pages(current, current->mm,
|
|
vaddr & PAGE_MASK,
|
|
buf->num_pages,
|
|
write,
|
|
1, /* force */
|
|
buf->pages,
|
|
NULL);
|
|
|
|
if (num_pages_from_user != buf->num_pages)
|
|
goto userptr_fail_get_user_pages;
|
|
|
|
if (sg_alloc_table_from_pages(&buf->sg_table, buf->pages,
|
|
buf->num_pages, buf->offset, size, 0))
|
|
goto userptr_fail_alloc_table_from_pages;
|
|
|
|
return buf;
|
|
|
|
userptr_fail_alloc_table_from_pages:
|
|
userptr_fail_get_user_pages:
|
|
dprintk(1, "get_user_pages requested/got: %d/%d]\n",
|
|
buf->num_pages, num_pages_from_user);
|
|
if (!vma_is_io(buf->vma))
|
|
while (--num_pages_from_user >= 0)
|
|
put_page(buf->pages[num_pages_from_user]);
|
|
vb2_put_vma(buf->vma);
|
|
userptr_fail_find_vma:
|
|
kfree(buf->pages);
|
|
userptr_fail_alloc_pages:
|
|
kfree(buf);
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* @put_userptr: inform the allocator that a USERPTR buffer will no longer
|
|
* be used
|
|
*/
|
|
static void vb2_dma_sg_put_userptr(void *buf_priv)
|
|
{
|
|
struct vb2_dma_sg_buf *buf = buf_priv;
|
|
int i = buf->num_pages;
|
|
|
|
dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
|
|
__func__, buf->num_pages);
|
|
if (buf->vaddr)
|
|
vm_unmap_ram(buf->vaddr, buf->num_pages);
|
|
sg_free_table(&buf->sg_table);
|
|
while (--i >= 0) {
|
|
if (buf->write)
|
|
set_page_dirty_lock(buf->pages[i]);
|
|
if (!vma_is_io(buf->vma))
|
|
put_page(buf->pages[i]);
|
|
}
|
|
kfree(buf->pages);
|
|
vb2_put_vma(buf->vma);
|
|
kfree(buf);
|
|
}
|
|
|
|
static void *vb2_dma_sg_vaddr(void *buf_priv)
|
|
{
|
|
struct vb2_dma_sg_buf *buf = buf_priv;
|
|
|
|
BUG_ON(!buf);
|
|
|
|
if (!buf->vaddr)
|
|
buf->vaddr = vm_map_ram(buf->pages,
|
|
buf->num_pages,
|
|
-1,
|
|
PAGE_KERNEL);
|
|
|
|
/* add offset in case userptr is not page-aligned */
|
|
return buf->vaddr + buf->offset;
|
|
}
|
|
|
|
static unsigned int vb2_dma_sg_num_users(void *buf_priv)
|
|
{
|
|
struct vb2_dma_sg_buf *buf = buf_priv;
|
|
|
|
return atomic_read(&buf->refcount);
|
|
}
|
|
|
|
static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
|
|
{
|
|
struct vb2_dma_sg_buf *buf = buf_priv;
|
|
unsigned long uaddr = vma->vm_start;
|
|
unsigned long usize = vma->vm_end - vma->vm_start;
|
|
int i = 0;
|
|
|
|
if (!buf) {
|
|
printk(KERN_ERR "No memory to map\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
do {
|
|
int ret;
|
|
|
|
ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
|
|
if (ret) {
|
|
printk(KERN_ERR "Remapping memory, error: %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
uaddr += PAGE_SIZE;
|
|
usize -= PAGE_SIZE;
|
|
} while (usize > 0);
|
|
|
|
|
|
/*
|
|
* Use common vm_area operations to track buffer refcount.
|
|
*/
|
|
vma->vm_private_data = &buf->handler;
|
|
vma->vm_ops = &vb2_common_vm_ops;
|
|
|
|
vma->vm_ops->open(vma);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void *vb2_dma_sg_cookie(void *buf_priv)
|
|
{
|
|
struct vb2_dma_sg_buf *buf = buf_priv;
|
|
|
|
return &buf->sg_table;
|
|
}
|
|
|
|
const struct vb2_mem_ops vb2_dma_sg_memops = {
|
|
.alloc = vb2_dma_sg_alloc,
|
|
.put = vb2_dma_sg_put,
|
|
.get_userptr = vb2_dma_sg_get_userptr,
|
|
.put_userptr = vb2_dma_sg_put_userptr,
|
|
.vaddr = vb2_dma_sg_vaddr,
|
|
.mmap = vb2_dma_sg_mmap,
|
|
.num_users = vb2_dma_sg_num_users,
|
|
.cookie = vb2_dma_sg_cookie,
|
|
};
|
|
EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
|
|
|
|
MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
|
|
MODULE_AUTHOR("Andrzej Pietrasiewicz");
|
|
MODULE_LICENSE("GPL");
|