【问题标题】:writing data to debugfs --- from a device driver将数据写入 debugfs --- 从设备驱动程序
【发布时间】:2017-03-20 00:13:34
【问题描述】:

使用 proc,我们可以轻松地使用 read & write 系统调用,如本例所示。 write on /proc entry through user space

但我正在使用 debugfs 将信息从驱动程序传递到用户空间。 我能够找到这两个示例代码。这里应用程序可以使用 mmap() 系统调用读取和写入 debugfs 文件。

但假设在我的情况下需要使用 Debugfs 文件与设备驱动程序进行通信:

user-space application    <-------> debugfs file <-------> Device driver
  1. 那么我可以在我的 --->> 设备驱动程序代码 --->> 中使用相同的代码 mmap_simple_kernel.c 并将数据直接从驱动程序传输到 debugfs 吗?但是在这种情况下,我的驱动程序中会有两个 file_operations 结构会导致一些问题吗?这是正确的方法吗?

  2. 或者就像应用程序正在遵循 -- mmap_user.c 中的进程 --- 相同的进程 -- 我在我的设备驱动程序中遵循。并保留 mmap_simple_kernel.c 作为 debugfs 条目的单独模块?

【问题讨论】:

  • 如果需要,您也可以在 debugfs 中的文件上使用 read()write()。例如,如果您需要传递大量数据,mmap() 会派上用场,但并非强制使用它。
  • 在我的一个项目中,我使用 debugfs 文件向用户空间提供一些数据并控制我的内核空间系统,例如,参见this file。在 debugfs 中创建了三个文件("i_addr""func_name""func_i_start"),第一个是可写的,其余的两个是只读的。另请参阅那里如何使用 debugfs_create_file()。我还建议查看其他debugfs_create_* functions,它们可能更适合您的需求。
  • 另外一个使用mmap()的例子,链接是in this comment。根据您要完成的任务,这可能很有用。

标签: linux-kernel linux-device-driver


【解决方案1】:

您可以在 mm/kmemleak.c 中查看 kmemleak 如何使用 debugfs:

static const struct seq_operations kmemleak_seq_ops = {
        .start = kmemleak_seq_start,
        .next  = kmemleak_seq_next,
        .stop  = kmemleak_seq_stop,
        .show  = kmemleak_seq_show,
};

static int kmemleak_open(struct inode *inode, struct file *file)
{
        return seq_open(file, &kmemleak_seq_ops);
}

static int kmemleak_release(struct inode *inode, struct file *file)
{
        return seq_release(inode, file);
}

static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
                              size_t size, loff_t *ppos)
{...}

static const struct file_operations kmemleak_fops = {
        .owner          = THIS_MODULE,
        .open           = kmemleak_open,
        .read           = seq_read,
        .write          = kmemleak_write,
        .llseek         = seq_lseek,
        .release        = kmemleak_release,
};


dentry = debugfs_create_file("kmemleak", S_IRUGO, NULL, NULL,
                             &kmemleak_fops);

【讨论】:

    【解决方案2】:

    此问题是 Google 中 mmap debugfs 的热门搜索结果。我在这里添加一个重要信息。根据这个https://lkml.org/lkml/2016/5/21/73 post debugfs_create_file() 在内核 4.8.0 或更高版本中将忽略 struct file_operations 中的 .mmap 字段

    使用 debugfs_create_file_unsafe() 作为解决方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多