linux下用户程序通过mmap映射驱动的内存,使用O_DIRCTE模式创建文件,write到文件时报Bad addreee

qiubo666 2020-06-24 10:45:47
下面为用户层函数代码:

void simple_test(char *filename, int dma_fd)
{
int file_fd
void *user_mem = NULL;
int pagesize = 0;
int rc;

file_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_DIRECT, 0666);
pagesize = getpagesize();
user_mem = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, dma_fd, 0);
if (user_mem == (void*) -1)
{
fprintf(stderr, "mmap: %s\n", strerror(errno));
exit(-1);
}

printf("user_mem %p\n", user_mem);

rc = write(file_fd, user_mem, pagesize);
if (rc < 0)
{
perror("write file");
}
else if(rc != pagesize)
{
printf("Write file fail 0x%lx != 0x%lx.\n", rc, pagesize);
}

rc = munmap(user_mem, pagesize);
if (rc)
{
perror("munmap");
}
}

内核驱动使用dma_alloc_coherent分配的内存,使用dma_mmap_coherent映射到用户空间。
执行测试函数simple_test时报write file: Bad addree

映射的地址和长度都是页大小的整数倍。不使用O_DIRECT时没有错误。
在用户态分配页对其的地址也没有问题
...全文
15819 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiubo666 2020-06-24
  • 打赏
  • 举报
回复
有什么方法可以规避该问题呢?
qiubo666 2020-06-24
  • 打赏
  • 举报
回复
在内核中使用filp_open创建文件,用kernel_write写文件也有相同问题。现在看可能是direct IO不支持内核空间分配的地址。

1,324

社区成员

发帖
与我相关
我的任务
社区描述
主要是开发驱动技术
社区管理员
  • 驱动程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧