开发者

How to write mmap input memory to O_DIRECT output file?

开发者 https://www.devze.com 2023-03-14 22:22 出处:网络
why doesn\'t following pseudo-code work (O_DIRECT results in EFAULT) in_fd = open(\"/dev/mem\"); in_mmap = mmap(in_fd);

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 开发者_如何学Pythonthe write call?

Best regards,

Friedrich


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.


There are two ways:

  1. use CMA and vm_insert_pages. I have put the details here

  2. use no-map reserved memory + my patch series, I have a sample in this series, so you can learn how to do it easily.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号