Copy_to_user needs to check the validity of the pointer during each copy, that is, the pointer to the address of the user space is indeed the address of the process itself, instead of pointing to the location that does not belong to it, in addition, data is copied once each time, and memory is frequently accessed. Because the virtual address is continuous, the physical address may not necessarily be consecutive, resulting in frequent cache failures of the CPU, thus reducing the speed.
MMAP only creates a page table for the process when it is used for the first time, that is, maps a physical address to a virtual address, in the future, the legality of its address will not be checked (the legality is determined by the CPU page protection exception). In addition, the MMAP address can be directly operated in the kernel without frequent copying, that is to say, you can directly operate on the address using a pointer in the kernel, instead of opening a buffer in the kernel, and then copy the data in the buffer, MMAP maps a consecutive physical address to a virtual address. Of course, you can also map consecutive physical addresses in each segment to a continuous virtual address, in any case, the physical address is continuous in each segment, so that the CPU cache will not become invalid frequently, thus greatly saving time.