如何读取/写入&quot/sys/bus/pci/设备/...在Linux内核或内核模块中?

发布于 2025-01-29 14:38:30 字数 2865 浏览 3 评论 0原文

我是Linux内核写作的绿色手。我在QEMU中有两台KVM虚拟机。我为每个添加一个IVSHMEM,他们可以使用openmmap函数来读/写/sys/bus/pci/pci/devices/0000:00:00:05.0 /resource2同步内存。这是一个参考: https:// https:// liujunming.top/2021/11/11/qemu-tutorial-inter-vm-shared-memory-device/

# QEMU parameters:
    -object memory-backend-file,size=4M,share=on,mem-path=/dev/shm/shm1,id=shm1 \
    -device ivshmem-plain,memdev=shm1,addr=0x05,master=on

但是,我需要在内核中同步内存,而不是用户空间。我使用Linux内核5.10.20。有两个功能kernel_readkernel_write。我可以使用两个函数来读取/编写一个普通文件,例如/home/xxx/a.txt,但我无法读取/写/sys/bus/bus/pci/pci/devices/0000 :00:05.0/resource2

这是我的代码读取第一个字符:

char buf_2[1];

void read_file(const char *filename)
{
    ssize_t i;
    struct file *f = NULL;
    loff_t f_pos = 0;

    f = filp_open(filename, O_RDWR, 0);
    if (IS_ERR(f)) {
        pr_err("open file error!\n");
    }

    memset(buf_2, 0, sizeof(buf_2));
    i = kernel_read(f, buf_2, 1, &f_pos);
    pr_info("f: %p, f_pos: %lld, buf_2: %d, i: %ld", f, f_pos, buf_2[0], i);

    filp_close(f, NULL);
}

当我通过/home/xxx/a.txt/sys/bus/pci/devices/0000:00:00:05.0.0.0.0/resource2 到read_file,它打印以下:

[   40.154424] f: 00000000cad673bc, f_pos: 1, buf_2: 115, i: 1
[   40.154457] f: 0000000047ddc318, f_pos: 0, buf_2: 0, i: -5

第一个结果是相关的。但是第二个不是。 kernel_read返回错误代码-5

如何在linux内核中读/写/sys/bus/bus/pci/devices/0000:00:00:00:05.0/resource2

$ ls /sys/bus/pci/devices/0000:00:05.0/
ari_enabled               driver_override  msi_bus    resource2_wc
broken_parity_status      enable           numa_node  revision
class                     firmware_node    power      subsystem
config                    irq              remove     subsystem_device
consistent_dma_mask_bits  link             rescan     subsystem_vendor
d3cold_allowed            local_cpulist    resource   uevent
device                    local_cpus       resource0  vendor
dma_mask_bits             modalias         resource2

$ cat /sys/bus/pci/devices/0000:00:05.0/resource
0x00000000c1031000 0x00000000c10310ff 0x0000000000040200
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000001000000000 0x00000010003fffff 0x000000000014220c
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000

I am a green hand in linux kernel writing. I have two kvm virtual machines in qemu. I add a ivshmem for each, and they can use open and mmap function to read/write /sys/bus/pci/devices/0000:00:05.0/resource2 to sync memory. Here is a reference: https://liujunming.top/2021/11/30/QEMU-tutorial-Inter-VM-Shared-Memory-device/

# QEMU parameters:
    -object memory-backend-file,size=4M,share=on,mem-path=/dev/shm/shm1,id=shm1 \
    -device ivshmem-plain,memdev=shm1,addr=0x05,master=on

However, I need to sync memory in kernel but not user space. I use linux kernel 5.10.20. There are two functions kernel_read and kernel_write. I can use the two functions to read/write a normal file such as /home/xxx/a.txt, but I cannot read/write /sys/bus/pci/devices/0000:00:05.0/resource2.

Here is my code to read the first character:

char buf_2[1];

void read_file(const char *filename)
{
    ssize_t i;
    struct file *f = NULL;
    loff_t f_pos = 0;

    f = filp_open(filename, O_RDWR, 0);
    if (IS_ERR(f)) {
        pr_err("open file error!\n");
    }

    memset(buf_2, 0, sizeof(buf_2));
    i = kernel_read(f, buf_2, 1, &f_pos);
    pr_info("f: %p, f_pos: %lld, buf_2: %d, i: %ld", f, f_pos, buf_2[0], i);

    filp_close(f, NULL);
}

When I pass /home/xxx/a.txt and /sys/bus/pci/devices/0000:00:05.0/resource2 to the read_file, it prints this:

[   40.154424] f: 00000000cad673bc, f_pos: 1, buf_2: 115, i: 1
[   40.154457] f: 0000000047ddc318, f_pos: 0, buf_2: 0, i: -5

The first result is corrent. But the second is not. And kernel_read returns a error code -5.

How can I read/write /sys/bus/pci/devices/0000:00:05.0/resource2 in linux kernel?

$ ls /sys/bus/pci/devices/0000:00:05.0/
ari_enabled               driver_override  msi_bus    resource2_wc
broken_parity_status      enable           numa_node  revision
class                     firmware_node    power      subsystem
config                    irq              remove     subsystem_device
consistent_dma_mask_bits  link             rescan     subsystem_vendor
d3cold_allowed            local_cpulist    resource   uevent
device                    local_cpus       resource0  vendor
dma_mask_bits             modalias         resource2

$ cat /sys/bus/pci/devices/0000:00:05.0/resource
0x00000000c1031000 0x00000000c10310ff 0x0000000000040200
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000001000000000 0x00000010003fffff 0x000000000014220c
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文