如何将 mmap 输入内存写入 O_DIRECT 输出文件?
为什么下面的伪代码不起作用(O_DIRECT 结果为 EFAULT),
in_fd = open("/dev/mem");
in_mmap = mmap(in_fd);
out_fd = open("/tmp/file", O_DIRECT);
write(out_fd, in_mmap, PAGE_SIZE);
而下面的却起作用(没有 O_DIRECT)
in_fd = open("/dev/mem");
in_mmap = mmap(in_fd);
out_fd = open("/tmp/file");
write(out_fd, in_mmap, PAGE_SIZE);
我猜这是虚拟内核页面到虚拟用户页面的问题,无法在写入调用中进行转换?
最好的问候,
弗里德里希
why doesn't following pseudo-code work (O_DIRECT results in EFAULT)
in_fd = open("/dev/mem");
in_mmap = mmap(in_fd);
out_fd = open("/tmp/file", O_DIRECT);
write(out_fd, in_mmap, PAGE_SIZE);
while following does (no O_DIRECT)
in_fd = open("/dev/mem");
in_mmap = mmap(in_fd);
out_fd = open("/tmp/file");
write(out_fd, in_mmap, PAGE_SIZE);
I guess it's something with virtual kernel pages to virtual user pages, which cannot be translated in the write call?
Best regards,
Friedrich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 mmap() 与 O_DIRECT 一起使用是很棘手的。有一些限制。文件的输出应该是块对齐的。例如,如果您将 mmap() 中的偏移量设置为 0,您的代码将起作用。您必须检查文件系统的块大小才能正确设置该值。
Using mmap() with O_DIRECT is tricky. There are some restrictions. The output to the file should be block aligned. For example, if you set offset in mmap() to 0 your code will work. You have to check the block size of your filesystem to set that value properly.
有两种方法:
使用
CMA
和vm_insert_pages
。详细说明可以在我的另一个 Stack Overflow 答案中找到
使用
no-map
保留记忆与我的补丁系列相结合。本系列包含一个示例,演示如何轻松实现此方法。There are two approaches:
Use
CMA
andvm_insert_pages
. Detailed instructions can be found in my another Stack Overflow answerUse
no-map
reserved memory combined with my patch series. This series includes a sample that demonstrates how to implement this approach easily.