How to write mmap input memory to O_DIRECT output file?
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:
use CMA and
vm_insert_pages
. I have put the details hereuse no-map reserved memory + my patch series, I have a sample in this series, so you can learn how to do it easily.
精彩评论