2017-11-07 21:58:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-01-10 04:25:06 +08:00
|
|
|
/*
|
|
|
|
* comedi_buf.c
|
|
|
|
*
|
|
|
|
* COMEDI - Linux Control and Measurement Device Interface
|
|
|
|
* Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
|
2014-09-15 20:46:06 +08:00
|
|
|
* Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
|
2013-01-10 04:25:06 +08:00
|
|
|
*/
|
|
|
|
|
2013-07-25 00:55:39 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2013-11-08 23:03:43 +08:00
|
|
|
#include <linux/slab.h>
|
2021-11-17 20:05:59 +08:00
|
|
|
#include <linux/comedi/comedidev.h>
|
2013-01-10 04:25:06 +08:00
|
|
|
#include "comedi_internal.h"
|
|
|
|
|
2013-01-10 04:26:26 +08:00
|
|
|
#ifdef PAGE_KERNEL_NOCACHE
|
|
|
|
#define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE
|
|
|
|
#else
|
|
|
|
#define COMEDI_PAGE_PROTECTION PAGE_KERNEL
|
|
|
|
#endif
|
|
|
|
|
2013-11-08 23:03:43 +08:00
|
|
|
static void comedi_buf_map_kref_release(struct kref *kref)
|
2013-01-10 04:25:54 +08:00
|
|
|
{
|
2013-11-08 23:03:43 +08:00
|
|
|
struct comedi_buf_map *bm =
|
|
|
|
container_of(kref, struct comedi_buf_map, refcount);
|
2013-01-10 04:25:54 +08:00
|
|
|
struct comedi_buf_page *buf;
|
2013-11-08 23:03:43 +08:00
|
|
|
unsigned int i;
|
2013-01-10 04:25:54 +08:00
|
|
|
|
2013-11-08 23:03:43 +08:00
|
|
|
if (bm->page_list) {
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
if (bm->dma_dir != DMA_NONE) {
|
|
|
|
/*
|
|
|
|
* DMA buffer was allocated as a single block.
|
|
|
|
* Address is in page_list[0].
|
|
|
|
*/
|
|
|
|
buf = &bm->page_list[0];
|
|
|
|
dma_free_coherent(bm->dma_hw_dev,
|
|
|
|
PAGE_SIZE * bm->n_pages,
|
|
|
|
buf->virt_addr, buf->dma_addr);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < bm->n_pages; i++) {
|
|
|
|
buf = &bm->page_list[i];
|
|
|
|
ClearPageReserved(virt_to_page(buf->virt_addr));
|
2013-01-10 04:25:54 +08:00
|
|
|
free_page((unsigned long)buf->virt_addr);
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 23:03:43 +08:00
|
|
|
vfree(bm->page_list);
|
|
|
|
}
|
|
|
|
if (bm->dma_dir != DMA_NONE)
|
|
|
|
put_device(bm->dma_hw_dev);
|
|
|
|
kfree(bm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __comedi_buf_free(struct comedi_device *dev,
|
|
|
|
struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-11 02:41:57 +08:00
|
|
|
struct comedi_buf_map *bm;
|
|
|
|
unsigned long flags;
|
2013-11-08 23:03:43 +08:00
|
|
|
|
|
|
|
if (async->prealloc_buf) {
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
if (s->async_dma_dir == DMA_NONE)
|
|
|
|
vunmap(async->prealloc_buf);
|
2013-11-08 23:03:43 +08:00
|
|
|
async->prealloc_buf = NULL;
|
|
|
|
async->prealloc_bufsz = 0;
|
2013-01-10 04:25:54 +08:00
|
|
|
}
|
2013-11-08 23:03:43 +08:00
|
|
|
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-11 02:41:57 +08:00
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
|
|
|
bm = async->buf_map;
|
2013-11-08 23:03:43 +08:00
|
|
|
async->buf_map = NULL;
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-11 02:41:57 +08:00
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
comedi_buf_map_put(bm);
|
2013-01-10 04:25:54 +08:00
|
|
|
}
|
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
static struct comedi_buf_map *
|
|
|
|
comedi_buf_map_alloc(struct comedi_device *dev, enum dma_data_direction dma_dir,
|
|
|
|
unsigned int n_pages)
|
|
|
|
{
|
|
|
|
struct comedi_buf_map *bm;
|
|
|
|
struct comedi_buf_page *buf;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
bm = kzalloc(sizeof(*bm), GFP_KERNEL);
|
|
|
|
if (!bm)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
kref_init(&bm->refcount);
|
|
|
|
bm->dma_dir = dma_dir;
|
|
|
|
if (bm->dma_dir != DMA_NONE) {
|
|
|
|
/* Need ref to hardware device to free buffer later. */
|
|
|
|
bm->dma_hw_dev = get_device(dev->hw_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
bm->page_list = vzalloc(sizeof(*buf) * n_pages);
|
|
|
|
if (!bm->page_list)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (bm->dma_dir != DMA_NONE) {
|
|
|
|
void *virt_addr;
|
|
|
|
dma_addr_t dma_addr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently, the DMA buffer needs to be allocated as a
|
|
|
|
* single block so that it can be mmap()'ed.
|
|
|
|
*/
|
|
|
|
virt_addr = dma_alloc_coherent(bm->dma_hw_dev,
|
|
|
|
PAGE_SIZE * n_pages, &dma_addr,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!virt_addr)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i = 0; i < n_pages; i++) {
|
|
|
|
buf = &bm->page_list[i];
|
|
|
|
buf->virt_addr = virt_addr + (i << PAGE_SHIFT);
|
|
|
|
buf->dma_addr = dma_addr + (i << PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bm->n_pages = i;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < n_pages; i++) {
|
|
|
|
buf = &bm->page_list[i];
|
|
|
|
buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
|
|
|
|
if (!buf->virt_addr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
SetPageReserved(virt_to_page(buf->virt_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
bm->n_pages = i;
|
|
|
|
if (i < n_pages)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bm;
|
|
|
|
|
|
|
|
err:
|
|
|
|
comedi_buf_map_put(bm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-10 04:26:26 +08:00
|
|
|
static void __comedi_buf_alloc(struct comedi_device *dev,
|
|
|
|
struct comedi_subdevice *s,
|
2016-03-21 08:26:12 +08:00
|
|
|
unsigned int n_pages)
|
2013-01-10 04:26:26 +08:00
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
struct page **pages = NULL;
|
2013-11-08 23:03:43 +08:00
|
|
|
struct comedi_buf_map *bm;
|
2013-01-10 04:26:26 +08:00
|
|
|
struct comedi_buf_page *buf;
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-11 02:41:57 +08:00
|
|
|
unsigned long flags;
|
2016-03-21 08:26:12 +08:00
|
|
|
unsigned int i;
|
2013-01-10 04:26:26 +08:00
|
|
|
|
2013-05-10 21:07:16 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) {
|
|
|
|
dev_err(dev->class_dev,
|
|
|
|
"dma buffer allocation not supported\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
bm = comedi_buf_map_alloc(dev, s->async_dma_dir, n_pages);
|
2013-11-08 23:03:43 +08:00
|
|
|
if (!bm)
|
|
|
|
return;
|
|
|
|
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-11 02:41:57 +08:00
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
|
|
|
async->buf_map = bm;
|
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
2013-11-08 23:03:43 +08:00
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
if (bm->dma_dir != DMA_NONE) {
|
|
|
|
/*
|
|
|
|
* DMA buffer was allocated as a single block.
|
|
|
|
* Address is in page_list[0].
|
|
|
|
*/
|
|
|
|
buf = &bm->page_list[0];
|
|
|
|
async->prealloc_buf = buf->virt_addr;
|
|
|
|
} else {
|
2013-01-10 04:26:26 +08:00
|
|
|
pages = vmalloc(sizeof(struct page *) * n_pages);
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
if (!pages)
|
|
|
|
return;
|
2013-01-10 04:26:26 +08:00
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
for (i = 0; i < n_pages; i++) {
|
|
|
|
buf = &bm->page_list[i];
|
|
|
|
pages[i] = virt_to_page(buf->virt_addr);
|
|
|
|
}
|
2013-01-10 04:26:26 +08:00
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
/* vmap the pages to prealloc_buf */
|
2013-01-10 04:26:26 +08:00
|
|
|
async->prealloc_buf = vmap(pages, n_pages, VM_MAP,
|
|
|
|
COMEDI_PAGE_PROTECTION);
|
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 19:26:59 +08:00
|
|
|
vfree(pages);
|
|
|
|
}
|
2013-01-10 04:26:26 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 23:03:43 +08:00
|
|
|
void comedi_buf_map_get(struct comedi_buf_map *bm)
|
|
|
|
{
|
|
|
|
if (bm)
|
|
|
|
kref_get(&bm->refcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
int comedi_buf_map_put(struct comedi_buf_map *bm)
|
|
|
|
{
|
|
|
|
if (bm)
|
|
|
|
return kref_put(&bm->refcount, comedi_buf_map_kref_release);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-21 02:05:14 +08:00
|
|
|
/* helper for "access" vm operation */
|
|
|
|
int comedi_buf_map_access(struct comedi_buf_map *bm, unsigned long offset,
|
|
|
|
void *buf, int len, int write)
|
|
|
|
{
|
2017-07-04 07:13:34 +08:00
|
|
|
unsigned int pgoff = offset_in_page(offset);
|
2017-04-21 02:05:14 +08:00
|
|
|
unsigned long pg = offset >> PAGE_SHIFT;
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
while (done < len && pg < bm->n_pages) {
|
|
|
|
int l = min_t(int, len - done, PAGE_SIZE - pgoff);
|
|
|
|
void *b = bm->page_list[pg].virt_addr + pgoff;
|
|
|
|
|
|
|
|
if (write)
|
|
|
|
memcpy(b, buf, l);
|
|
|
|
else
|
|
|
|
memcpy(buf, b, l);
|
|
|
|
buf += l;
|
|
|
|
done += l;
|
|
|
|
pg++;
|
|
|
|
pgoff = 0;
|
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-11 02:41:57 +08:00
|
|
|
/* returns s->async->buf_map and increments its kref refcount */
|
|
|
|
struct comedi_buf_map *
|
|
|
|
comedi_buf_map_from_subdev_get(struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
struct comedi_buf_map *bm = NULL;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!async)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
|
|
|
bm = async->buf_map;
|
|
|
|
/* only want it if buffer pages allocated */
|
|
|
|
if (bm && bm->n_pages)
|
|
|
|
comedi_buf_map_get(bm);
|
|
|
|
else
|
|
|
|
bm = NULL;
|
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:12:11 +08:00
|
|
|
bool comedi_buf_is_mmapped(struct comedi_subdevice *s)
|
2013-11-08 23:03:43 +08:00
|
|
|
{
|
2014-05-06 20:12:11 +08:00
|
|
|
struct comedi_buf_map *bm = s->async->buf_map;
|
2013-11-08 23:03:43 +08:00
|
|
|
|
2016-11-15 00:29:48 +08:00
|
|
|
return bm && (kref_read(&bm->refcount) > 1);
|
2013-11-08 23:03:43 +08:00
|
|
|
}
|
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
|
|
|
|
unsigned long new_size)
|
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
|
2019-04-17 22:39:29 +08:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
/* Round up new_size to multiple of PAGE_SIZE */
|
|
|
|
new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
|
|
|
|
|
|
|
|
/* if no change is required, do nothing */
|
|
|
|
if (async->prealloc_buf && async->prealloc_bufsz == new_size)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-10 04:25:54 +08:00
|
|
|
/* deallocate old buffer */
|
2013-11-08 23:03:43 +08:00
|
|
|
__comedi_buf_free(dev, s);
|
2013-01-10 04:25:54 +08:00
|
|
|
|
2013-01-10 04:26:26 +08:00
|
|
|
/* allocate new buffer */
|
2013-01-10 04:25:06 +08:00
|
|
|
if (new_size) {
|
2016-03-21 08:26:12 +08:00
|
|
|
unsigned int n_pages = new_size >> PAGE_SHIFT;
|
2013-01-10 04:26:26 +08:00
|
|
|
|
|
|
|
__comedi_buf_alloc(dev, s, n_pages);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:25:54 +08:00
|
|
|
if (!async->prealloc_buf) {
|
2013-01-10 04:26:26 +08:00
|
|
|
/* allocation failed */
|
2013-11-08 23:03:43 +08:00
|
|
|
__comedi_buf_free(dev, s);
|
2013-01-10 04:25:06 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async->prealloc_bufsz = new_size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:12:10 +08:00
|
|
|
void comedi_buf_reset(struct comedi_subdevice *s)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:10 +08:00
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
async->buf_write_alloc_count = 0;
|
|
|
|
async->buf_write_count = 0;
|
|
|
|
async->buf_read_alloc_count = 0;
|
|
|
|
async->buf_read_count = 0;
|
|
|
|
|
|
|
|
async->buf_write_ptr = 0;
|
|
|
|
async->buf_read_ptr = 0;
|
|
|
|
|
|
|
|
async->cur_chan = 0;
|
2014-11-06 01:20:52 +08:00
|
|
|
async->scans_done = 0;
|
2013-01-10 04:25:06 +08:00
|
|
|
async->scan_progress = 0;
|
|
|
|
async->munge_chan = 0;
|
|
|
|
async->munge_count = 0;
|
|
|
|
async->munge_ptr = 0;
|
|
|
|
|
|
|
|
async->events = 0;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:26:48 +08:00
|
|
|
static unsigned int comedi_buf_write_n_unalloc(struct comedi_subdevice *s)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:14 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2013-01-10 04:27:48 +08:00
|
|
|
unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:27:48 +08:00
|
|
|
return free_end - async->buf_write_alloc_count;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:26:49 +08:00
|
|
|
unsigned int comedi_buf_write_n_available(struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
|
|
|
|
|
|
|
|
return free_end - async->buf_write_count;
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:02:39 +08:00
|
|
|
/**
|
|
|
|
* comedi_buf_write_alloc() - Reserve buffer space for writing
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @nbytes: Maximum space to reserve in bytes.
|
|
|
|
*
|
|
|
|
* Reserve up to @nbytes bytes of space to be written in the COMEDI acquisition
|
|
|
|
* data buffer associated with the subdevice. The amount reserved is limited
|
|
|
|
* by the space available.
|
|
|
|
*
|
|
|
|
* Return: The amount of space reserved in bytes.
|
|
|
|
*/
|
2014-10-23 06:37:18 +08:00
|
|
|
unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s,
|
|
|
|
unsigned int nbytes)
|
2013-01-10 04:27:48 +08:00
|
|
|
{
|
2014-05-06 20:12:13 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2015-10-09 19:26:48 +08:00
|
|
|
unsigned int unalloc = comedi_buf_write_n_unalloc(s);
|
2013-01-10 04:27:48 +08:00
|
|
|
|
2015-10-09 19:26:48 +08:00
|
|
|
if (nbytes > unalloc)
|
|
|
|
nbytes = unalloc;
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:27:48 +08:00
|
|
|
async->buf_write_alloc_count += nbytes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ensure the async buffer 'counts' are read and updated
|
|
|
|
* before we write data to the write-alloc'ed buffer space
|
2013-01-10 04:25:06 +08:00
|
|
|
*/
|
|
|
|
smp_mb();
|
2013-01-10 04:27:48 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
return nbytes;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:29:19 +08:00
|
|
|
/*
|
|
|
|
* munging is applied to data by core as it passes between user
|
|
|
|
* and kernel space
|
|
|
|
*/
|
2014-05-06 20:12:12 +08:00
|
|
|
static unsigned int comedi_buf_munge(struct comedi_subdevice *s,
|
2013-01-10 04:25:06 +08:00
|
|
|
unsigned int num_bytes)
|
|
|
|
{
|
2014-05-06 20:12:12 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2013-01-10 04:25:06 +08:00
|
|
|
unsigned int count = 0;
|
2016-03-21 08:26:12 +08:00
|
|
|
const unsigned int num_sample_bytes = comedi_bytes_per_sample(s);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:29:19 +08:00
|
|
|
if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) {
|
2013-01-10 04:25:06 +08:00
|
|
|
async->munge_count += num_bytes;
|
2021-03-25 21:35:20 +08:00
|
|
|
return num_bytes;
|
|
|
|
}
|
2013-01-10 04:29:19 +08:00
|
|
|
|
2021-03-25 21:35:20 +08:00
|
|
|
/* don't munge partial samples */
|
|
|
|
num_bytes -= num_bytes % num_sample_bytes;
|
|
|
|
while (count < num_bytes) {
|
|
|
|
int block_size = num_bytes - count;
|
|
|
|
unsigned int buf_end;
|
2013-01-10 04:29:19 +08:00
|
|
|
|
2021-03-25 21:35:20 +08:00
|
|
|
buf_end = async->prealloc_bufsz - async->munge_ptr;
|
|
|
|
if (block_size > buf_end)
|
|
|
|
block_size = buf_end;
|
2013-01-10 04:29:19 +08:00
|
|
|
|
2021-03-25 21:35:20 +08:00
|
|
|
s->munge(s->device, s,
|
|
|
|
async->prealloc_buf + async->munge_ptr,
|
|
|
|
block_size, async->munge_chan);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ensure data is munged in buffer before the
|
|
|
|
* async buffer munge_count is incremented
|
|
|
|
*/
|
|
|
|
smp_wmb();
|
|
|
|
|
|
|
|
async->munge_chan += block_size / num_sample_bytes;
|
|
|
|
async->munge_chan %= async->cmd.chanlist_len;
|
|
|
|
async->munge_count += block_size;
|
|
|
|
async->munge_ptr += block_size;
|
|
|
|
async->munge_ptr %= async->prealloc_bufsz;
|
|
|
|
count += block_size;
|
2013-01-10 04:25:06 +08:00
|
|
|
}
|
2013-01-10 04:29:19 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-05-06 20:12:06 +08:00
|
|
|
unsigned int comedi_buf_write_n_allocated(struct comedi_subdevice *s)
|
2013-01-10 04:32:18 +08:00
|
|
|
{
|
2014-05-06 20:12:06 +08:00
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
|
2013-01-10 04:32:18 +08:00
|
|
|
return async->buf_write_alloc_count - async->buf_write_count;
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:02:39 +08:00
|
|
|
/**
|
|
|
|
* comedi_buf_write_free() - Free buffer space after it is written
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @nbytes: Maximum space to free in bytes.
|
|
|
|
*
|
|
|
|
* Free up to @nbytes bytes of space previously reserved for writing in the
|
|
|
|
* COMEDI acquisition data buffer associated with the subdevice. The amount of
|
|
|
|
* space freed is limited to the amount that was reserved. The freed space is
|
|
|
|
* assumed to have been filled with sample data by the writer.
|
|
|
|
*
|
|
|
|
* If the samples in the freed space need to be "munged", do so here. The
|
|
|
|
* freed space becomes available for allocation by the reader.
|
|
|
|
*
|
|
|
|
* Return: The amount of space freed in bytes.
|
|
|
|
*/
|
2014-05-06 20:12:05 +08:00
|
|
|
unsigned int comedi_buf_write_free(struct comedi_subdevice *s,
|
2013-01-10 04:32:56 +08:00
|
|
|
unsigned int nbytes)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:05 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2014-05-06 20:12:06 +08:00
|
|
|
unsigned int allocated = comedi_buf_write_n_allocated(s);
|
2013-01-10 04:29:53 +08:00
|
|
|
|
2013-01-11 01:37:56 +08:00
|
|
|
if (nbytes > allocated)
|
2013-01-10 04:29:53 +08:00
|
|
|
nbytes = allocated;
|
2013-01-11 01:37:56 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
async->buf_write_count += nbytes;
|
|
|
|
async->buf_write_ptr += nbytes;
|
2014-05-06 20:12:12 +08:00
|
|
|
comedi_buf_munge(s, async->buf_write_count - async->munge_count);
|
2013-01-10 04:25:06 +08:00
|
|
|
if (async->buf_write_ptr >= async->prealloc_bufsz)
|
|
|
|
async->buf_write_ptr %= async->prealloc_bufsz;
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_write_free);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2015-09-23 01:02:39 +08:00
|
|
|
/**
|
|
|
|
* comedi_buf_read_n_available() - Determine amount of readable buffer space
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
*
|
|
|
|
* Determine the amount of readable buffer space in the COMEDI acquisition data
|
|
|
|
* buffer associated with the subdevice. The readable buffer space is that
|
|
|
|
* which has been freed by the writer and "munged" to the sample data format
|
|
|
|
* expected by COMEDI if necessary.
|
|
|
|
*
|
|
|
|
* Return: The amount of readable buffer space.
|
|
|
|
*/
|
2014-05-06 20:12:09 +08:00
|
|
|
unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:09 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2016-03-21 08:26:12 +08:00
|
|
|
unsigned int num_bytes;
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:30:22 +08:00
|
|
|
if (!async)
|
2013-01-10 04:25:06 +08:00
|
|
|
return 0;
|
2013-01-10 04:30:22 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
num_bytes = async->munge_count - async->buf_read_count;
|
2013-01-10 04:30:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ensure the async buffer 'counts' are read before we
|
|
|
|
* attempt to read data from the buffer
|
2013-01-10 04:25:06 +08:00
|
|
|
*/
|
|
|
|
smp_rmb();
|
2013-01-10 04:30:22 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
return num_bytes;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2015-09-23 01:02:39 +08:00
|
|
|
/**
|
|
|
|
* comedi_buf_read_alloc() - Reserve buffer space for reading
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @nbytes: Maximum space to reserve in bytes.
|
|
|
|
*
|
|
|
|
* Reserve up to @nbytes bytes of previously written and "munged" buffer space
|
|
|
|
* for reading in the COMEDI acquisition data buffer associated with the
|
|
|
|
* subdevice. The amount reserved is limited to the space available. The
|
|
|
|
* reader can read from the reserved space and then free it. A reader is also
|
|
|
|
* allowed to read from the space before reserving it as long as it determines
|
|
|
|
* the amount of readable data available, but the space needs to be marked as
|
|
|
|
* reserved before it can be freed.
|
|
|
|
*
|
|
|
|
* Return: The amount of space reserved in bytes.
|
|
|
|
*/
|
2014-05-06 20:12:07 +08:00
|
|
|
unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s,
|
2013-01-10 04:32:56 +08:00
|
|
|
unsigned int nbytes)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:07 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2013-01-10 04:30:49 +08:00
|
|
|
unsigned int available;
|
|
|
|
|
|
|
|
available = async->munge_count - async->buf_read_alloc_count;
|
|
|
|
if (nbytes > available)
|
|
|
|
nbytes = available;
|
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
async->buf_read_alloc_count += nbytes;
|
2013-01-10 04:30:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ensure the async buffer 'counts' are read before we
|
|
|
|
* attempt to read data from the read-alloc'ed buffer space
|
|
|
|
*/
|
2013-01-10 04:25:06 +08:00
|
|
|
smp_rmb();
|
2013-01-10 04:30:49 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
return nbytes;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2013-01-10 04:31:46 +08:00
|
|
|
static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
|
|
|
|
{
|
|
|
|
return async->buf_read_alloc_count - async->buf_read_count;
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:02:39 +08:00
|
|
|
/**
|
|
|
|
* comedi_buf_read_free() - Free buffer space after it has been read
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @nbytes: Maximum space to free in bytes.
|
|
|
|
*
|
|
|
|
* Free up to @nbytes bytes of buffer space previously reserved for reading in
|
|
|
|
* the COMEDI acquisition data buffer associated with the subdevice. The
|
|
|
|
* amount of space freed is limited to the amount that was reserved.
|
|
|
|
*
|
|
|
|
* The freed space becomes available for allocation by the writer.
|
|
|
|
*
|
|
|
|
* Return: The amount of space freed in bytes.
|
|
|
|
*/
|
2014-05-06 20:12:08 +08:00
|
|
|
unsigned int comedi_buf_read_free(struct comedi_subdevice *s,
|
2013-01-10 04:32:56 +08:00
|
|
|
unsigned int nbytes)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:08 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2013-01-10 04:31:16 +08:00
|
|
|
unsigned int allocated;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ensure data has been read out of buffer before
|
|
|
|
* the async read count is incremented
|
|
|
|
*/
|
2013-01-10 04:25:06 +08:00
|
|
|
smp_mb();
|
2013-01-10 04:31:16 +08:00
|
|
|
|
|
|
|
allocated = comedi_buf_read_n_allocated(async);
|
2013-01-11 01:38:20 +08:00
|
|
|
if (nbytes > allocated)
|
2013-01-10 04:31:16 +08:00
|
|
|
nbytes = allocated;
|
2013-01-11 01:38:20 +08:00
|
|
|
|
2013-01-10 04:25:06 +08:00
|
|
|
async->buf_read_count += nbytes;
|
|
|
|
async->buf_read_ptr += nbytes;
|
|
|
|
async->buf_read_ptr %= async->prealloc_bufsz;
|
|
|
|
return nbytes;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_read_free);
|
2013-01-10 04:25:06 +08:00
|
|
|
|
2014-10-23 05:36:49 +08:00
|
|
|
static void comedi_buf_memcpy_to(struct comedi_subdevice *s,
|
|
|
|
const void *data, unsigned int num_bytes)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
2014-05-06 20:12:01 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2014-10-23 05:36:50 +08:00
|
|
|
unsigned int write_ptr = async->buf_write_ptr;
|
2013-01-10 04:25:06 +08:00
|
|
|
|
|
|
|
while (num_bytes) {
|
|
|
|
unsigned int block_size;
|
|
|
|
|
|
|
|
if (write_ptr + num_bytes > async->prealloc_bufsz)
|
|
|
|
block_size = async->prealloc_bufsz - write_ptr;
|
|
|
|
else
|
|
|
|
block_size = num_bytes;
|
|
|
|
|
|
|
|
memcpy(async->prealloc_buf + write_ptr, data, block_size);
|
|
|
|
|
|
|
|
data += block_size;
|
|
|
|
num_bytes -= block_size;
|
|
|
|
|
|
|
|
write_ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 05:36:48 +08:00
|
|
|
static void comedi_buf_memcpy_from(struct comedi_subdevice *s,
|
|
|
|
void *dest, unsigned int nbytes)
|
2013-01-10 04:25:06 +08:00
|
|
|
{
|
|
|
|
void *src;
|
2014-05-06 20:12:02 +08:00
|
|
|
struct comedi_async *async = s->async;
|
2014-10-23 05:36:51 +08:00
|
|
|
unsigned int read_ptr = async->buf_read_ptr;
|
2013-01-10 04:25:06 +08:00
|
|
|
|
|
|
|
while (nbytes) {
|
|
|
|
unsigned int block_size;
|
|
|
|
|
|
|
|
src = async->prealloc_buf + read_ptr;
|
|
|
|
|
|
|
|
if (nbytes >= async->prealloc_bufsz - read_ptr)
|
|
|
|
block_size = async->prealloc_bufsz - read_ptr;
|
|
|
|
else
|
|
|
|
block_size = nbytes;
|
|
|
|
|
|
|
|
memcpy(dest, src, block_size);
|
|
|
|
nbytes -= block_size;
|
|
|
|
dest += block_size;
|
|
|
|
read_ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-09-15 20:46:03 +08:00
|
|
|
|
2014-10-23 06:36:24 +08:00
|
|
|
/**
|
2015-09-23 01:02:38 +08:00
|
|
|
* comedi_buf_write_samples() - Write sample data to COMEDI buffer
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @data: Pointer to source samples.
|
|
|
|
* @nsamples: Number of samples to write.
|
2014-10-23 06:36:24 +08:00
|
|
|
*
|
2015-09-23 01:02:38 +08:00
|
|
|
* Write up to @nsamples samples to the COMEDI acquisition data buffer
|
|
|
|
* associated with the subdevice, mark it as written and update the
|
|
|
|
* acquisition scan progress. If there is not enough room for the specified
|
|
|
|
* number of samples, the number of samples written is limited to the number
|
|
|
|
* that will fit and the %COMEDI_CB_OVERFLOW event flag is set to cause the
|
|
|
|
* acquisition to terminate with an overrun error. Set the %COMEDI_CB_BLOCK
|
|
|
|
* event flag if any samples are written to cause waiting tasks to be woken
|
|
|
|
* when the event flags are processed.
|
2014-10-23 06:36:24 +08:00
|
|
|
*
|
2015-09-23 01:02:38 +08:00
|
|
|
* Return: The amount of data written in bytes.
|
2014-10-23 06:36:24 +08:00
|
|
|
*/
|
|
|
|
unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
|
|
|
|
const void *data, unsigned int nsamples)
|
|
|
|
{
|
|
|
|
unsigned int max_samples;
|
|
|
|
unsigned int nbytes;
|
|
|
|
|
2014-10-31 02:10:50 +08:00
|
|
|
/*
|
|
|
|
* Make sure there is enough room in the buffer for all the samples.
|
|
|
|
* If not, clamp the nsamples to the number that will fit, flag the
|
|
|
|
* buffer overrun and add the samples that fit.
|
|
|
|
*/
|
2015-10-09 19:26:48 +08:00
|
|
|
max_samples = comedi_bytes_to_samples(s, comedi_buf_write_n_unalloc(s));
|
2014-10-23 06:36:24 +08:00
|
|
|
if (nsamples > max_samples) {
|
|
|
|
dev_warn(s->device->class_dev, "buffer overrun\n");
|
|
|
|
s->async->events |= COMEDI_CB_OVERFLOW;
|
2014-10-31 02:10:50 +08:00
|
|
|
nsamples = max_samples;
|
2014-10-23 06:36:24 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 06:37:17 +08:00
|
|
|
if (nsamples == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-11-01 03:04:28 +08:00
|
|
|
nbytes = comedi_buf_write_alloc(s,
|
|
|
|
comedi_samples_to_bytes(s, nsamples));
|
2014-10-23 06:37:17 +08:00
|
|
|
comedi_buf_memcpy_to(s, data, nbytes);
|
|
|
|
comedi_buf_write_free(s, nbytes);
|
|
|
|
comedi_inc_scan_progress(s, nbytes);
|
|
|
|
s->async->events |= COMEDI_CB_BLOCK;
|
2014-10-23 06:36:24 +08:00
|
|
|
|
2014-10-23 06:37:17 +08:00
|
|
|
return nbytes;
|
2014-10-23 06:36:24 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_write_samples);
|
|
|
|
|
2014-10-23 05:36:34 +08:00
|
|
|
/**
|
2015-09-23 01:02:38 +08:00
|
|
|
* comedi_buf_read_samples() - Read sample data from COMEDI buffer
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @data: Pointer to destination.
|
|
|
|
* @nsamples: Maximum number of samples to read.
|
2014-10-23 05:36:34 +08:00
|
|
|
*
|
2015-09-23 01:02:38 +08:00
|
|
|
* Read up to @nsamples samples from the COMEDI acquisition data buffer
|
|
|
|
* associated with the subdevice, mark it as read and update the acquisition
|
|
|
|
* scan progress. Limit the number of samples read to the number available.
|
|
|
|
* Set the %COMEDI_CB_BLOCK event flag if any samples are read to cause waiting
|
|
|
|
* tasks to be woken when the event flags are processed.
|
2014-10-23 05:36:34 +08:00
|
|
|
*
|
2015-09-23 01:02:38 +08:00
|
|
|
* Return: The amount of data read in bytes.
|
2014-10-23 05:36:34 +08:00
|
|
|
*/
|
|
|
|
unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
|
|
|
|
void *data, unsigned int nsamples)
|
|
|
|
{
|
|
|
|
unsigned int max_samples;
|
|
|
|
unsigned int nbytes;
|
|
|
|
|
2014-10-23 05:36:42 +08:00
|
|
|
/* clamp nsamples to the number of full samples available */
|
2014-11-01 03:04:28 +08:00
|
|
|
max_samples = comedi_bytes_to_samples(s,
|
|
|
|
comedi_buf_read_n_available(s));
|
2014-10-23 05:36:34 +08:00
|
|
|
if (nsamples > max_samples)
|
|
|
|
nsamples = max_samples;
|
|
|
|
|
2014-10-23 05:36:42 +08:00
|
|
|
if (nsamples == 0)
|
2014-10-23 05:36:41 +08:00
|
|
|
return 0;
|
2014-10-23 05:36:34 +08:00
|
|
|
|
2014-11-01 03:04:28 +08:00
|
|
|
nbytes = comedi_buf_read_alloc(s,
|
|
|
|
comedi_samples_to_bytes(s, nsamples));
|
2014-10-23 05:36:51 +08:00
|
|
|
comedi_buf_memcpy_from(s, data, nbytes);
|
2014-10-23 05:36:41 +08:00
|
|
|
comedi_buf_read_free(s, nbytes);
|
|
|
|
comedi_inc_scan_progress(s, nbytes);
|
|
|
|
s->async->events |= COMEDI_CB_BLOCK;
|
|
|
|
|
|
|
|
return nbytes;
|
2014-10-23 05:36:34 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_buf_read_samples);
|