在 Linux 内核中分配用户 space 内存

Allocating user space memory in Linux Kernel

是否允许从内核 space 分配用户 space 内存?我知道 Linux 中的进程使用虚拟内存和虚拟地址。并且有一个保护不允许使用不同进程的内存(它会引发分段错误)。所以,没有办法分配一个缓冲区和 return 指向它的有效指针给用户 space 进程?

is that allowed to allocate user space memory from kernel space? I know that process in Linux uses virtual memory and virtual addresses. And there is a protection which can`t allow using of memory of different process (it raises segmentation fault). So, there is no way to allocate a buffer and return a valid pointer to it to user space process?

内存分配例程通常有一个 return 值,它是内核分配值的指针(在虚拟内存坐标中)。如果用户不要求新内存,在用户 space 中为他分配内存而不告诉分配位置是不正常的....

但你可以做到。我认为了解如何做到这一点的最好方法是研究 mmap(2) 系统调用的工作原理(因为它将分配的内存映射到用户 space,并且 return 是用户 space 指针让用户知道它被分配在哪里),并使用用于分配用户内存的内部内核函数。内核 malloc 例程组需要知道如何将内存页映射(即使对于内核虚拟 space,因为内核也在虚拟地址内存 space 中运行)内存页到虚拟内存地址。不这样做会导致无法访问内存。