内核实验(七):使用内核KFIFO环形缓冲区机制

简介: 本文通过一个内核模块实验,演示了如何在Linux内核中使用KFIFO环形缓冲区机制,包括定义KFIFO、编写驱动程序以及在Qemu虚拟机中进行编译、部署和测试,展示了KFIFO在无需额外加锁的情况下如何安全地在读者和写者线程间进行数据传输。

一、篇头

继续使用qemu调试内核的实验。

二、系列文章

略……

三、实验环境

  • 编译服务器+NFS:ubuntu 22.04
  • Qemu 虚拟机:Linux version 5.15.102 + Buysbox 1.3.36 + ARM_32bit

Qemu 启动命令:qemu-system-arm -nographic -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -initrd …/busybox/rootfs.ext4.img.gz -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb

四、源码

4.1 KFIFO介绍

Linux内核实现了一个称为KFIFO的环形缓冲区的机制,它可以在一个读者线程和一个写者线程并发执行的场景下,无须使用额外的加锁来保证环形缓冲区的数据安全。同时由此机制来管理了缓冲区的使用情况,省去许多是,特别方便。

KFIFO提供的接口函数定义在include/linux/kfifo.h文件中。

4.1.1 声明及定义:DEFINE_KFIFO(fifo, type, size)
/**
 * DEFINE_KFIFO - macro to define and initialize a fifo
 * @fifo: name of the declared fifo datatype
 * @type: type of the fifo elements
 * @size: the number of elements in the fifo, this must be a power of 2
 *
 * Note: the macro can be used for global and local fifo data type variables.
 */
#define DEFINE_KFIFO(fifo, type, size) \
    DECLARE_KFIFO(fifo, type, size) = \
    (typeof(fifo)) { \
        { \
            { \
            .in    = 0, \
            .out    = 0, \
            .mask    = __is_kfifo_ptr(&(fifo)) ? \
                  0 : \
                  ARRAY_SIZE((fifo).buf) - 1, \
            .esize    = sizeof(*(fifo).buf), \
            .data    = __is_kfifo_ptr(&(fifo)) ? \
                NULL : \
                (fifo).buf, \
            } \
        } \
    }
4.1.2 用户写:kfifo_from_user(fifo, from, len, copied)
/**
 * kfifo_from_user - puts some data from user space into the fifo
 * @fifo: address of the fifo to be used
 * @from: pointer to the data to be added
 * @len: the length of the data to be added
 * @copied: pointer to output variable to store the number of copied bytes
 *
 * This macro copies at most @len bytes from the @from into the
 * fifo, depending of the available space and returns -EFAULT/0.
 *
 * Note that with only one concurrent reader and one concurrent
 * writer, you don't need extra locking to use these macro.
 */
#define    kfifo_from_user(fifo, from, len, copied) \
__kfifo_uint_must_check_helper( \
({ \
    typeof((fifo) + 1) __tmp = (fifo); \
    const void __user *__from = (from); \
    unsigned int __len = (len); \
    unsigned int *__copied = (copied); \
    const size_t __recsize = sizeof(*__tmp->rectype); \
    struct __kfifo *__kfifo = &__tmp->kfifo; \
    (__recsize) ? \
    __kfifo_from_user_r(__kfifo, __from, __len,  __copied, __recsize) : \
    __kfifo_from_user(__kfifo, __from, __len, __copied); \
}) \
)
4.1.3 用户读:kfifo_to_user(fifo, to, len, copied)
/**
 * kfifo_to_user - copies data from the fifo into user space
 * @fifo: address of the fifo to be used
 * @to: where the data must be copied
 * @len: the size of the destination buffer
 * @copied: pointer to output variable to store the number of copied bytes
 *
 * This macro copies at most @len bytes from the fifo into the
 * @to buffer and returns -EFAULT/0.
 *
 * Note that with only one concurrent reader and one concurrent
 * writer, you don't need extra locking to use these macro.
 */
#define    kfifo_to_user(fifo, to, len, copied) \
__kfifo_int_must_check_helper( \
({ \
    typeof((fifo) + 1) __tmp = (fifo); \
    void __user *__to = (to); \
    unsigned int __len = (len); \
    unsigned int *__copied = (copied); \
    const size_t __recsize = sizeof(*__tmp->rectype); \
    struct __kfifo *__kfifo = &__tmp->kfifo; \
    (__recsize) ? \
    __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
    __kfifo_to_user(__kfifo, __to, __len, __copied); \
}) \
)

4.2 驱动源码

  • 文件名:linux-stable\my_kmodules\test_5.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/kfifo.h>


#define MY_DEV_NAME "my_dev"

DEFINE_KFIFO(my_kfifo, char, 128);
static char kbuf[128];


static int test_5_open(struct inode *inode, struct file *file)
{
   
   
    int major = MAJOR(inode->i_rdev);
    int minor = MINOR(inode->i_rdev);

    pr_info("%s: major=%d, minor=%d\n", __func__, major, minor);
    return 0;
}

static int test_5_release(struct inode *inode, struct file *file)
{
   
   
    pr_info("%s \n", __func__);
    return 0;
}

static ssize_t test_5_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
   
   
    int ret;
    unsigned int copied_count=0;
    pr_info("%s \n", __func__);

    ret = kfifo_to_user(&my_kfifo, buf,  count, &copied_count);
    if(ret != 0) {
   
   
        return -EIO;
    }
    pr_info("%s :%s, count=%d, copied_count=%d\n", __func__, my_kfifo.buf,  count, copied_count);

    return copied_count;
}

static ssize_t test_5_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
{
   
   
    int ret;
    unsigned int copied_count=0;
    pr_info("%s \n", __func__);

    ret = kfifo_from_user(&my_kfifo, buf, count, &copied_count);
    if(ret != 0) {
   
   
        return -EIO;
    }
    pr_info("%s , my_kfifo.buf=%s\n", __func__, my_kfifo.buf);
    //注意:使用用户空间的buf,需要先copy_from_user
    ret = copy_from_user(kbuf, buf, copied_count);
    pr_info("%s :%s, count=%d, copied_count=%d, ret=%d\n", __func__, kbuf,  count, copied_count, ret);

    return copied_count;
}

static const struct file_operations test_fops = {
   
   
    .owner = THIS_MODULE,
    .open = test_5_open,
    .release = test_5_release,
    .read = test_5_read,
    .write = test_5_write
};

static struct miscdevice test_5_misc_device ={
   
   

    .minor = MISC_DYNAMIC_MINOR,
    .name = MY_DEV_NAME,
    .fops = &test_fops,
};


static int __init test_5_init(void)
{
   
   
    int ret;
    pr_info("test_5_init\n");

    ret = misc_register(&test_5_misc_device);
    if (ret != 0 ) {
   
   
        pr_err("failed to misc_register");
        return ret;
    }

    pr_err("Minor number = %d\n", test_5_misc_device.minor);
    return 0;
}

static void __exit test_5_exit(void)
{
   
   
    pr_info("test_5_exit\n");
    misc_deregister(&test_5_misc_device);
}

module_init(test_5_init);
module_exit(test_5_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("szhou <66176468@qq.com>");
MODULE_DESCRIPTION("test_5, 使用misc、KFIFO开发设备驱动");

4.3 APP源码

本次实验,直接使用 echo /cat 负责设备的写/读。

4.4 Makefile

  • 文件名:linux-stable\my_kmodules\Makefile
KDIR := /home/szhou/works/qemu_linux/linux-stable

obj-m := test_1.o test_2.o test_3.o test_4.o  test_5.o
all :
    $(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
    rm -f *.ko

五、编译及部署

1)执行驱动KO编译
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ make 
make -C /home/szhou/works/qemu_linux/linux-stable M=/home/szhou/works/qemu_linux/linux-stable/my_kmodules modules
make[1]: Entering directory '/home/szhou/works/qemu_linux/linux-stable'
  CC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_5.o
  MODPOST /home/szhou/works/qemu_linux/linux-stable/my_kmodules/Module.symvers
  LD [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_5.ko
make[1]: Leaving directory '/home/szhou/works/qemu_linux/linux-stable'
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ cp test_5.ko ~/works/nfs_share/
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ 

(2)将KO和APP存放到NFS共享目录
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ cp test_5.ko ~/works/nfs_share/

六、运行及测试

1)启动之前编译组建的QEMU虚拟机
----------------------------------------
Welcome to szhou's tiny Linux
----------------------------------------

Please press Enter to activate this console.2)挂载NFS共享目录
~ #  mount -t nfs -o nolock 192.168.3.67:/home/szhou/works/nfs_share /mnt

(3) 加载ko
~ # cd /mnt/
/mnt # insmod test_5.ko 
test_5: loading out-of-tree module taints kernel.
test_5_init
Minor number = 125
/mnt # 
/mnt # mdev  -s
/mnt # 

(4)写入字符串 
/mnt # echo "hellow kfifo" >  /dev/my_dev 
test_5_open: major=10, minor=125
test_5_write 
test_5_write , my_kfifo.buf=hellow kfifo

test_5_write :hellow kfifo
, count=13, copied_count=13, ret=0
test_5_release 
/mnt # 

(5)读取字符串 
/mnt # cat /dev/my_dev 
test_5_open: major=10, minor=125
test_5_read 
test_5_read :hellow kfifo
, count=4096, copied_count=13
hellow kfifo
test_5_read 
test_5_read :hellow kfifo
, count=4096, copied_count=0
test_5_release 
/mnt #

操作画面如下图:

在这里插入图片描述

相关文章
|
存储 并行计算 Linux
国产之路:复旦微FMQL调试笔记1:PS网口
FPGA,全程现场可编程门阵列,是指一切通过软件手段更改、配置器件内部连接结构和逻辑单元,完成既定设计功能的数字集成电路。换个简单通俗的介绍方式,就好比一个全能的运动员,FPGA就是这么神奇的可以通过设定而实现各种复杂的功能电路。
3134 0
国产之路:复旦微FMQL调试笔记1:PS网口
|
开发工具 git 缓存
Git忽略规则.gitignore不生效
在项目开发过程中个,一般都会添加 .gitignore 文件,规则很简单,但有时会发现,规则不生效。 原因是 .gitignore 只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
63927 5
|
10月前
|
人工智能 安全 Ubuntu
保姆级教程 | 在Ubuntu上部署Claude CodeUI全过程
Claude Code Plan Mode 是 Anthropic 推出的智能编程助手功能,采用只读分析模式,保障代码安全的同时提供AI驱动的项目规划与风险评估。该模式平均每周为开发者节省27小时,显著提升开发效率与项目成功率,是AI编程领域的重要创新。
13689 10
|
定位技术
环形缓冲区RingBuff
环形缓冲区RingBuff
|
NoSQL Linux Android开发
内核实验(三):编写简单Linux内核模块,使用Qemu加载ko做测试
本文介绍了如何在QEMU中挂载虚拟分区、创建和编译简单的Linux内核模块,并在QEMU虚拟机中加载和测试这些内核模块,包括创建虚拟分区、编写内核模块代码、编译、部署以及在QEMU中的加载和测试过程。
877 0
内核实验(三):编写简单Linux内核模块,使用Qemu加载ko做测试
|
Linux TensorFlow 算法框架/工具
在Linux上安装其他版本的cmake 或 升级cmake
在Linux上安装其他版本的cmake 或 升级cmake
3591 2
|
测试技术 Linux Android开发
i2c总线及设备测试工具i2ctools:i2cdetect、i2cdump、i2cget、i2cset
本文介绍了i2ctools工具集的使用,包括i2cdetect、i2cdump、i2cget和i2cset,这些工具有助于I2C设备的开发和调试,通过检测设备、读写寄存器和数据块来提高开发效率。
5312 1
|
数据可视化
【Qt 学习笔记】Qt窗口 | 菜单栏 | QMenuBar的使用及说明
【Qt 学习笔记】Qt窗口 | 菜单栏 | QMenuBar的使用及说明
4831 3
|
Linux 编译器 C语言
Linux EXPORT_SYMBOL宏详解
Linux EXPORT_SYMBOL宏详解
Linux EXPORT_SYMBOL宏详解
|
存储 前端开发 数据可视化
一文教会你 如何在Github中创建仓库?如何将多个项目放到一个仓库中管理?如何将本地项目上传到GitHub中?
这篇文章详细介绍了如何在GitHub上创建新仓库,以及如何将多个项目整合到一个仓库中进行管理。文章还提供了克隆仓库到本地、使用不同文件夹存放不同项目代码、以及将这些项目提交到远程服务器的步骤和方法。
一文教会你 如何在Github中创建仓库?如何将多个项目放到一个仓库中管理?如何将本地项目上传到GitHub中?