support for user-space buffers in kfifo
RFC PATCH 1/1 support for user-space buffers in kfifo
Hi.
I just added support for user space buffers in kfifo. I found useful __kfifo_get_user to copy data to a user buffer in a read call. I didn't like the idea of having an extra buffer.
* Is it ok to add this support?
* Am I missing something?
I expect to fill the fifo using __kfifo_put in interrupt context. If I understand correctly, I won't need extra locking if I have a single interrupt handler filling this buffer and all the readers use a semaphore.
This is how I'm using it now:
static ssize_t device_read(struct file *filep, char __user *buffer,
size_t length, loff_t * offset)
{
struct sarpic_dev *dev = filep->private_data;
int ret_val;
if (down_interruptible (&dev->sem))
return -ERESTARTSYS;
while (__kfifo_len(dev->fifo) == 0) {
up(&dev->sem);
if (filep->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(dev->readq, (__kfifo_len(dev->fifo) != 0)))
return -ERESTARTSYS;
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
}
ret_val = __kfifo_get_user(dev->fifo, buffer, length);
up(&dev->sem);
return ret_val;
}static ssize_t
device_write(struct file *filep,
const char __user *buffer, size_t length, loff_t *offset)
{
struct sarpic_dev *dev = filep->private_data;
int ret_val; if (down_interruptible (&dev->sem))
return -ERESTARTSYS;
ret_val = __kfifo_put_user(dev->fifo, buffer, length); up(&dev->sem); wake_up_interruptible(&dev->readq);
return ret_val;
}
Regards,
Nelson.-
---
include/linux/kfifo.h +59 -0
@@ -41,8 +41,13 @@ extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,extern void kfifo_free (struct kfifo *fifo);extern unsigned int __kfifo_put (struct kfifo *fifo,unsigned char *buffer,unsigned int len); +extern int __kfifo_put_user (struct kfifo *fifo, +const unsigned char __user *buffer, +unsigned int len);extern unsigned int __kfifo_get (struct kfifo *fifo,unsigned char *buffer,unsigned int len); +extern int __kfifo_get_user (struct kfifo *fifo,unsigned char __user *buffer, +unsigned int len);/** * __kfifo_reset - removes the entire FIFO contents, no locking version
@@ -94,6 +99,32 @@ static inline unsigned int kfifo_put(struct kfifo *fifo, }/** +* kfifo_put_user - puts some user data into the FIFO +* @fifo: the fifo to be used. +* @buffer: the user-space data to be added. +* @len: the length of the data to be added. +* +* This function copies at most @len bytes from the @buffer into +* the FIFO depending on the free space, and returns the number of +* bytes copied. +*/ +static inline int kfifo_put_user (struct kfifo *fifo, +const unsigned char __user *buffer, +unsigned int len) +{ +unsigned long flags; +int ret; + +spin_lock_irqsave (fifo->lock, flags); + + ret =__kfifo_put_user (fifo, buffer, len); + +spin_unlock_irqrestore (fifo->lock, flags); + +return ret; +} + +/** * kfifo_get - gets some data from the FIFO * @fifo: the fifo to be used. * @buffer: where the data must be copied.
@@ -125,6 +156,34 @@ static inline unsigned int kfifo_get(struct kfifo *fifo, }/** +* kfifo_get_user - gets some data from the FIFO an user buffer +* @fifo: the fifo to be used. +* @buffer: user buffer where the data must be copied +* @len: the size of the destination buffer. +* +* This function copies at most @len bytes from the FIFO into the +* @buffer and returns the number of copied bytes. +*/ + +static inline int kfifo_get_user (struct kfifo *fifo, +unsigned char __user *buffer,unsigned int len) +{ +unsigned long flags; +int ret; + +spin_lock_irqsave (fifo->lock, flags); + + ret =__kfifo_get_user (fifo, buffer, len); + +if (fifo->in == fifo->out) + fifo->in = fifo->out =0 ; + +spin_unlock_irqrestore (fifo->lock, flags); + +return ret; +} + +/** * __kfifo_len - returns the number of bytes available in the FIFO, no locking version * @fifo: the fifo to be used. */
kernel/kfifo.c +108 -0
124 lines not on this page · raw message
Signature · 2 lines
http://arhuaco.org http://emQbit.com
Quoted 7 lines · Nelson Castillo
Hi.
I just added support for user space buffers in kfifo. I found useful
__kfifo_get_userto copy data to a user buffer in a read call. I didn't like the idea of having an extra buffer.* Is it ok to add this support?
I suppose it is, however:
Quoted 5 lines
include/linux/kfifo.h +5
-0+spin_lock_irqsave (fifo->lock, flags); + + ret =__kfifo_put_user (fifo, buffer, len); + +spin_unlock_irqrestore (fifo->lock, flags);
[...]
Quoted 2 lines
kernel/kfifo.c +2
-0+int __kfifo_put_user (struct kfifo *fifo,const unsigned char __user *buffer, +unsigned int len)
[...]
kernel/kfifo.c unsigned int __kfifo_put(struct kfifo *fifo, · line 186
Quoted 3 lines
kernel/kfifo.c +3
-0+if (copy_from_user (fifo->buffer + (fifo->in & (fifo->size -1 )), + buffer, l)) +return -EFAULT ;
[...]
accessing userspace memory with a spinlock taken (moreover an irqsave() one) is bad bad bad.
Stelian.
Signature · 1 line
Stelian Pop <[email protected]>
Quoted 22 lines · Stelian Pop
Quoted 7 lines · Nelson Castillo
Hi.
I just added support for user space buffers in kfifo. I found useful
__kfifo_get_userto copy data to a user buffer in a read call. I didn't like the idea of having an extra buffer.* Is it ok to add this support?
I suppose it is, however:
Quoted 5 lines
include/linux/kfifo.h +5
-0+spin_lock_irqsave (fifo->lock, flags); + + ret =__kfifo_put_user (fifo, buffer, len); + +spin_unlock_irqrestore (fifo->lock, flags);[...]
Quoted 2 lines
kernel/kfifo.c +2
-0+int __kfifo_put_user (struct kfifo *fifo,const unsigned char __user *buffer, +unsigned int len)[...]
Quoted 7 lines
kernel/kfifo.c unsigned int __kfifo_put(struct kfifo *fifo, · line 186
Quoted 3 lines
kernel/kfifo.c +3
-0+if (copy_from_user (fifo->buffer + (fifo->in & (fifo->size -1 )), + buffer, l)) +return -EFAULT ;[...]
accessing userspace memory with a spinlock taken (moreover an
irqsave()one) is bad bad bad.
Yes.
Perhaps then only the functions __kfifo_put_user and __kfifo_get_user should be allowed. Those are the ones I'm using now , anyway.
I added the others for completeness but adding them is probably a mistake.
Regards,
Nelson.-
Signature · 2 lines
http://arhuaco.org http://emQbit.com