Linux内核中的循环缓冲区【转】

(转自:https://blog.csdn.net/heanyu/article/details/6291825

循环缓冲区定义在include/linux/kfifo.h中,如下:

struct kfifo { 
    unsigned char *buffer; // buffer指向存放数据的缓冲区 
    unsigned int size;        // size是缓冲区的大小 
    unsigned int in;           // in是写指针下标 
    unsigned int out;        // out是读指针下标 
    spinlock_t *lock;         // lock是加到struct kfifo上的自旋锁 
};  

(上面说的免锁不是免这里的锁,这个锁是必须的),防止多个进程并发访问此数据结构。当in==out时,说明缓冲区为空;当(in-out)==size时,说明缓冲区已满。 
       为kfifo提供的接口可以分为两类: 
       一类是满足上述情况下使用的,以双下划线开头,没有加锁的; 
       另一类是在不满足的条件下,即需要额外加锁的情况下使用的。 
       其实后一类只是在前一类的基础上进行加锁后的包装(也有一处进行了小的改进),实现中所加的锁是spin_lock_irqsave。


清空缓冲区的函数: 
static inline void __kfifo_reset(struct kfifo *fifo); 
static inline void kfifo_reset(struct kfifo *fifo); 
这很简单,直接把读写指针都置为0即可。

向缓冲区里放入数据的接口是: 
static inline unsigned int kfifo_put(struct kfifo *fifo, unsigned char *buffer, unsigned int len); 
unsigned int __kfifo_put(struct kfifo *fifo, unsigned char *buffer, unsigned int len);

后者是在kernel/kfifo.c中定义的。这个接口是经过精心构造的,可以小心地避免一些边界情况。我们有必要一起来看一下它的具体实现。
 

   /**
    * __kfifo_put - puts some data into the FIFO, no locking version
    * @fifo: the fifo to be used.
    * @buffer: the 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.
    *
    * Note that with only one concurrent reader and one concurrent
    * writer, you don't need extra locking to use these functions.
    */

    unsigned int __kfifo_put(struct kfifo *fifo,
               const unsigned char *buffer, unsigned int len)
    {
       unsigned int l;
    
       len = min(len, fifo->size - fifo->in + fifo->out);
    
       /*
        * Ensure that we sample the fifo->out index -before- we
        * start putting bytes into the kfifo.
        */
    
       smp_mb();
    
       /* first put the data starting from fifo->in to buffer end */
       l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
       memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
    
       /* then put the rest (if any) at the beginning of the buffer */
       memcpy(fifo->buffer, buffer + l, len - l);
    
       /*
        * Ensure that we add the bytes to the kfifo -before-
        * we update the fifo->in index.
        */
    
       smp_wmb();
   
       fifo->in += len;
    
       return len;
    }
    EXPORT_SYMBOL(__kfifo_put);
   /**
    * kfifo_put - puts some data into the FIFO
    * @fifo: the fifo to be used.
    * @buffer: the 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 unsigned int kfifo_put(struct kfifo *fifo,
                   const unsigned char *buffer, unsigned int len)
   {
       unsigned long flags;
       unsigned int ret;
    
       spin_lock_irqsave(fifo->lock, flags);
    
       ret = __kfifo_put(fifo, buffer, len);
    
       spin_unlock_irqrestore(fifo->lock, flags);
    
       return ret;
   }

len = min(len, fifo->size - fifo->in + fifo->out); 
      在 len(fifo->size - fifo->in + fifo->out) 之间取一个较小的值赋给len。注意,当 (fifo->in == fifo->out+fifo->size) 时,表示缓冲区已满,此时得到的较小值一定是0,后面实际写入的字节数也全为0。 
      另一种边界情况是当 len 很大时(因为len是无符号的,负数对它来说也是一个很大的正数),这一句也能保证len取到一个较小的值,因为     fifo->in 总是大于等于 fifo->out ,所以后面的那个表达式 l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); 的值不会超过fifo->size的大小。 
      smp_mb();  smp_wmb(); 是加内存屏障,这里不是我们讨论的范围,你可以忽略它。 


      l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));    是把上一步决定的要写入的字节数 len “切开”,这里又使用了一个技巧。注意:实际分配给 fifo->buffer 的字节数 fifo->size ,必须是2的幂,否则这里就会出错。既然 fifo->size 是2的幂,那么 (fifo->size-1) 也就是一个后面几位全为1的数,也就能保证 (fifo->in & (fifo->size - 1)) 总为不超过 (fifo->size - 1) 的那一部分,和 (fifo->in)% (fifo->size - 1) 的效果一样。 
      这样后面的代码就不难理解了,它先向  fifo->in  到缓冲区末端这一块写数据,如果还没写完,在从缓冲区头开始写入剩下的,从而实现了循环缓冲。最后,把写指针后移 len 个字节,并返回len。 
       从上面可以看出,fifo->in的值可以从0变化到超过fifo->size的数值,fifo->out也如此,但它们的差不会超过fifo->size。

从kfifo向外读数据的函数是: 
static inline unsigned int kfifo_get(struct kfifo *fifo, unsigned char *buffer, unsigned int len); 
unsigned int __kfifo_get(struct kfifo *fifo, unsigned char *buffer, unsigned int len);

    /**
    * __kfifo_get - gets some data from the FIFO, no locking version
    * @fifo: the fifo to be used.
    * @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.
    *
    * Note that with only one concurrent reader and one concurrent
    * writer, you don't need extra locking to use these functions.
    */
   unsigned int __kfifo_get(struct kfifo *fifo,
                unsigned char *buffer, unsigned int len)
   {
       unsigned int l;
    
       len = min(len, fifo->in - fifo->out);
    
       /*
        * Ensure that we sample the fifo->in index -before- we
        * start removing bytes from the kfifo.
        */
    
       smp_rmb();
    
       /* first get the data from fifo->out until the end of the buffer */
       l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
       memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
    
       /* then get the rest (if any) from the beginning of the buffer */
       memcpy(buffer + l, fifo->buffer, len - l);
    
       /*
        * Ensure that we remove the bytes from the kfifo -before-
        * we update the fifo->out index.
        */
    
       smp_mb();
    
       fifo->out += len;
    
       return len;
   }
   EXPORT_SYMBOL(__kfifo_get);
    /**
    * kfifo_get - gets some data from the FIFO
    * @fifo: the fifo to be used.
    * @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 unsigned int kfifo_get(struct kfifo *fifo,
                        unsigned char *buffer, unsigned int len)
   {
       unsigned long flags;
       unsigned int ret;
    
       spin_lock_irqsave(fifo->lock, flags);
    
       ret = __kfifo_get(fifo, buffer, len);
    
       /*
        * optimization: if the FIFO is empty, set the indices to 0
        * so we don't wrap the next time
        */
       if (fifo->in == fifo->out)
           fifo->in = fifo->out = 0;
    
       spin_unlock_irqrestore(fifo->lock, flags);
    
       return ret;
   }

和上面的__kfifo_put类似,不难分析。

static inline unsigned int __kfifo_len(struct kfifo *fifo); 
static inline unsigned int kfifo_len(struct kfifo *fifo);
 

    /**
    * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
    * @fifo: the fifo to be used.
    */
   static inline unsigned int __kfifo_len(struct kfifo *fifo)
   {
       return fifo->in - fifo->out;
   }
    
   /**
    * kfifo_len - returns the number of bytes available in the FIFO
    * @fifo: the fifo to be used.
    */
   static inline unsigned int kfifo_len(struct kfifo *fifo)
   {
       unsigned long flags;
       unsigned int ret;
    
       spin_lock_irqsave(fifo->lock, flags);
    
       ret = __kfifo_len(fifo);
    
       spin_unlock_irqrestore(fifo->lock, flags);
    
       return ret;
   }

这两个函数返回缓冲区中实际的字节数,只要用fifo->in减去fifo->out即可。

kernel/kfifo.c中还提供了初始化kfifo,分配和释放kfifo的接口:

struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size, gfp_t gfp_mask, spinlock_t *lock); 
struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock);

void kfifo_free(struct kfifo *fifo);

再一次强调,调用kfifo_init必须保证size是2的幂,而kfifo_alloc不必,它内部会把size向上圆到2的幂。kfifo_alloc和kfifo_free搭配使用,因为这两个函数会为fifo->buffer分配/释放内存空间。而kfifo_init只会接受一个已分配好空间的fifo->buffer,不能和kfifo->free搭配,用kfifo_init分配的kfifo只能用kfree释放。

循环缓冲区在驱动程序中使用较多,尤其是网络适配器。但这种免锁的方式在内核互斥中使用较少,取而代之的是另一种高级的互斥机制──RCU。

参考资料:

1. Linux Device Drivers, 3rd Edition, Jonathan Corbet, Alessandro Rubini and Greg Kroah-Hartman, O'Reilly.

2. Linux Kernel 2.6.19 source code.
 

猜你喜欢

转载自blog.csdn.net/biqioso/article/details/83996536
今日推荐