venus.w
venus.w

Reputation: 2251

The implementation of read: Is copying memory to kernel space firstly better than user space directly?

When a process is blocked after the calling of "read", the kernel reads data from ios and coping it to the buffer, but where is the buffer, in the kernel or in the user space(which is the parameter of the "read" function).It saves coping from kernel space to user space for the later choice, furthermore, there is much more buffer in the user space. But it should change the cr3 every time when coping, which flushes all the TLB data. That is what I know for the two choices, Is there anything else?

Upvotes: 0

Views: 507

Answers (2)

jchiang
jchiang

Reputation: 29

One of the way is to ask device driver to map its kernel buffer to process's user address space via mmap and in turns uses something like remap_pfn_range.

When driver finishes the I/O operation, the copying to its kernel buffer doesn't need cr3 change or shoot down the TLB.

While the user process is waiting for the I/O, it is highly possible that it will be scheduled out for another new process to run, and then the cr3 has to change (+ TLB flush) for a entire I/O operation to be completed.

Upvotes: 1

Omair
Omair

Reputation: 874

One common pattern that I see in the kernel is that a buffer is generally allocated in kernel space - kzalloc(PAGE_SIZE, GFP_KERNEL) (not necessarily PAGE_SIZE), then the read happens to that buffer. Then it is copied to the userspace using simple_read_from_buffer(..) (fs/libfs.c) - which internally uses copy_to_user(). Though this is generally done for simple I/O operations or other (eg. debugfs) reads.

Upvotes: 1

Related Questions