Skip to contentSkip to conversation

Doing a zero-copy move of data from a kernel buffer to hard disk

DISCUSSION Neeraj Kumar 3 participants 5 messages Sep 20, 2010 – Oct 12, 2010

lore
Share & tools
lore
#0 lore raw

I am trying to move data from a buffer in kernel space into the hard disk without having to incur any additional copies from kernel buffer to user buffers or any other kernel buffers. Any ideas/suggestions would be most helpful.

The use case is basically a demux driver which collects data into a demux buffer in kernel space and this buffer has to be emptied periodically by copying the contents into a FUSE-based partition on the disk. As the buffer gets full, a user process is signalled which then determines the sector numbers on the disk the contents need to be copied to.

I was hoping to mmap the above demux kernel buffer into user address space and issue a write system call to the raw partition device. But from what I can see, the this data is being cached by the kernel on its way to the Hard Disk driver. And so I am assuming that involves additional copies by the linux kernel.

At this point I am wondering if there is any other mechansim to do this without involving additional copies by the kernel. I realize this is an unsual usage scenario for non-embedded environments, but I would appreciate any feedback on possible options.

BTW - I have tried using O_DIRECT when opening the raw partition, but the subsequent write call fails if the buffer being passed is the mmapped buffer.

Thanx!

#1 lore raw
Quoted 18 lines · Neeraj Kumar

I am trying to move data from a buffer in kernel space into the hard disk without having to incur any additional copies from kernel buffer to user buffers or any other kernel buffers. Any ideas/suggestions would be most helpful.

The use case is basically a demux driver which collects data into a demux buffer in kernel space and this buffer has to be emptied periodically by copying the contents into a FUSE-based partition on the disk. As the buffer gets full, a user process is signalled which then determines the sector numbers on the disk the contents need to be copied to.

I was hoping to mmap the above demux kernel buffer into user address space and issue a write system call to the raw partition device. But from what I can see, the this data is being cached by the kernel on its way to the Hard Disk driver. And so I am assuming that involves additional copies by the linux kernel.

Quoted 8 lines

At this point I am wondering if there is any other mechansim to do this without involving additional copies by the kernel. I realize this is an unsual usage scenario for non-embedded environments, but I would appreciate any feedback on possible options.

BTW - I have tried using O_DIRECT when opening the raw partition, but the subsequent write call fails if the buffer being passed is the mmapped buffer.

O_DIRECT would be the right way to do it - I think you'd need to figure out why that write is failing. Keep in mind you need to observe the alignment restrictions of O_DIRECT (man 2 open).

#2 lore raw
Quoted 26 lines · Neeraj Kumar
Quoted 8 lines

I am trying to move data from a buffer in kernel space into the hard disk without having to incur any additional copies from kernel buffer to user buffers or any other kernel buffers. Any ideas/suggestions would be most helpful.

The use case is basically a demux driver which collects data into a demux buffer in kernel space and this buffer has to be emptied periodically by copying the contents into a FUSE-based partition on the

disk. As the buffer gets full, a user process is signalled which then

Quoted 14 lines

determines the sector numbers on the disk the contents need to be copied to.

I was hoping to mmap the above demux kernel buffer into user address space and issue a write system call to the raw partition device. But from what I can see, the this data is being cached by the kernel on its way to the Hard Disk driver. And so I am assuming that involves additional copies by the linux kernel.

At this point I am wondering if there is any other mechansim to do this without involving additional copies by the kernel. I realize this is an unsual usage scenario for non-embedded environments, but I would appreciate any feedback on possible options.

Quoted 7 lines
Quoted 3 lines

BTW - I have tried using O_DIRECT when opening the raw partition, but the subsequent write call fails if the buffer being passed is the mmapped buffer.

O_DIRECT would be the right way to do it - I think you'd need to figure out why that write is failing. Keep in mind you need to observe the alignment restrictions of O_DIRECT (man 2 open).

I was able to figure out that O_DIRECT can not work with memory mapped pages. In the kernel, get_user_pages() returns error if user provided pages are mapped kernel pages.

Thanks
Neeraj

#3 lore raw
In reply to Neeraj Kumar
Quoted 12 lines · Neeraj Kumar

I am trying to move data from a buffer in kernel space into the hard disk without having to incur any additional copies from kernel buffer to user buffers or any other kernel buffers. Any ideas/suggestions would be most helpful.

The use case is basically a demux driver which collects data into a demux buffer in kernel space and this buffer has to be emptied periodically by copying the contents into a FUSE-based partition on the disk. As the buffer gets full, a user process is signalled which then determines the sector numbers on the disk the contents need to be copied to.

Quoted 5 lines

I was hoping to mmap the above demux kernel buffer into user address space and issue a write system call to the raw partition device. But from what I can see, the this data is being cached by the kernel on its way to the Hard Disk driver. And so I am assuming that involves additional copies by the linux kernel.

Isn't this functionality already served by relayfs?

Quoted 8 lines

At this point I am wondering if there is any other mechansim to do this without involving additional copies by the kernel. I realize this is an unsual usage scenario for non-embedded environments, but I would appreciate any feedback on possible options.

BTW - I have tried using O_DIRECT when opening the raw partition, but the subsequent write call fails if the buffer being passed is the mmapped buffer.

The other zero-copy mechanism is splice(2).

Thanks,
Miklos

#4 lore raw
Quoted 12 lines · Neeraj Kumar
Quoted 12 lines

I am trying to move data from a buffer in kernel space into the hard disk without having to incur any additional copies from kernel buffer to user buffers or any other kernel buffers. Any ideas/suggestions would be most helpful.

The use case is basically a demux driver which collects data into a demux buffer in kernel space and this buffer has to be emptied periodically by copying the contents into a FUSE-based partition on the disk. As the buffer gets full, a user process is signalled which then determines the sector numbers on the disk the contents need to be copied to.

Quoted 7 lines
Quoted 5 lines

I was hoping to mmap the above demux kernel buffer into user address space and issue a write system call to the raw partition device. But from what I can see, the this data is being cached by the kernel on its way to the Hard Disk driver. And so I am assuming that involves additional copies by the linux kernel.

Isn't this functionality already served by relayfs?

I could not figure out how relayfs could help me.

Quoted 10 lines
Quoted 8 lines

At this point I am wondering if there is any other mechansim to do this without involving additional copies by the kernel. I realize this is an unsual usage scenario for non-embedded environments, but I would appreciate any feedback on possible options.

BTW - I have tried using O_DIRECT when opening the raw partition, but the subsequent write call fails if the buffer being passed is the mmapped buffer.

The other zero-copy mechanism is splice(2).

I looked at the implementation of splice(). It looks promising. I have 2 more questions related to it:
1. SPLICE_F_MOVE which is needed for zero copy movement of pages is defined as no op in the kernel code.
Does splice still tries for zero copy movement?

2. I may need to implement splice_read() and splice_write() for the demux driver. I did not find any other
driver making use of these interfaces in the kernel except file system. Is
there any sample code available
which could help me to understand/implement splice() interfaces in a better way?

Thanks,
Miklos