Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we use virt_to_phys for user space memory in kernel module?

I will allocate memory in user application using malloc and send the malloc returned address to a kernel module through character driver interface.

I will pin the pages for this memory using get_user_pages_fast in kernel module.

Can I use virt_to_phys to get the address returned by malloc. Is it valid? If not then how can I get proper physical address?

My aim is to get physical address of user space allocated memory. I m limiting my transfer size to pagesize (4KB).

like image 532
valmiki Avatar asked Apr 10 '17 13:04

valmiki


People also ask

Can kernel access user space memory?

Whilst a user-space program is not allowed to access kernel memory, it is possible for the kernel to access user memory. However, the kernel must never execute user-space memory and it must also never access user-space memory without explicit expectation to do so.

How do I transfer data from kernel space to user space?

The function copy_to_user is used to copy data from the kernel address space to the address space of the user program. For example, to copy a buffer which has been allocated with kmalloc to the buffer provided by the user.

Which function is used to copy data from kernel space to user space?

The copy_to_user function copies a block of data from the kernel into user space.

How is kernel space and user space interfaced?

The most common way of implementing a user mode separate from kernel mode involves operating system protection rings. Protection rings, in turn, are implemented using CPU modes. Typically, kernel space programs run in kernel mode, also called supervisor mode; normal applications in user space run in user mode.


2 Answers

No you can't, virt_to_phys converts kernel virtual addresses into physical addresses. There exist 3 (or 4) types of addresses in linux:

  • kernel virtual address: which is physical address +/- an offset (PAGE_OFFSET).
  • kernel physical address: actual physical address (obtained by __pa or virt_to_phys functions).
  • user virtual address: the translation to physical address is inside the page table of the process.

Note that page table 'layout' depends on the architecture of the processor, so you need to implement a software page table walk that corresponds to the architecture you work on.

And last word, the 4th kind of addresses that exists is :

  • bus address: which is an address as seen by a device.
like image 168
alexghiti Avatar answered Sep 26 '22 00:09

alexghiti


malloc returns the user virtual address. So I think you can not use the address returned by malloc from inside the driver.

virt_to_phys: The returned physical address is the physical (CPU) mapping for the memory address given. It is only valid to use this function on addresses directly mapped or allocated via kmalloc. It means It is used by the kernel to translate kernel virtual address (not user virtual address) to physical address

like image 20
vinod maverick Avatar answered Sep 25 '22 00:09

vinod maverick