Re: Need help about how linux to handle translation fault

From: Clemens Ladisch
Date: Fri Aug 12 2011 - 07:24:56 EST


Pecker Hu wrote:
> ...
> And I send the address of str to kernel space using IOCTL interface.
> ioctl(fd, VM_TEST_GET_VADDR, str);
>
> When I try to use pgd to find the physical address, I don't find it
> .

You must not directly access user space memory from kernel space because
that memory can be moved or swapped out at any time. Use only functions
like copy_from_user() which take appropriate precautions; and don't
forget to check for errors.

Furthermore, the physical address is useless. If you want to access
the memory from a device, you have to use the DMA mapping API because
the CPU's physical address it not necessarily the same address as that
used on the device's bus, and sometimes you have to program an IOMMU
before devices are allowed to access that memory at all.

> So I think string array is located in data cache not in physical
> memory, after I call copy_from_user, kernel will allocate page for it,
> then we can access it, is it right?

As far as any code running on the CPU is concerned, caches are
completely transparent. (This is not always true for devices, but the
DMA mapping API takes care of that.)


Regards,
Clemens
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/