如何读取/写入&quot/sys/bus/pci/设备/...在Linux内核或内核模块中?
我是Linux内核写作的绿色手。我在QEMU中有两台KVM虚拟机。我为每个添加一个IVSHMEM,他们可以使用open
和mmap
函数来读/写/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_read
和kernel_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论