* copy_*_user
@ 2011-10-22 17:06 Xin Tong
2011-10-23 2:50 ` copy_*_user Américo Wang
0 siblings, 1 reply; 5+ messages in thread
From: Xin Tong @ 2011-10-22 17:06 UTC (permalink / raw)
To: linux-kernel
I am investigating copy_from_user and copy_to_user in linux under
i386. These two function both take a pointer with virtual address and
a pointer with physical address.
copy_from_user calls __copy_from_user_ll and copy_to_user calls
__copy_to_user_ll. It make sense to me that __copy_to_user_ll converts
the virtual address to physical address using the current process's
page table.
unsigned long __copy_to_user_ll(void __user *to, const void *from,
unsigned long n)
{
...
retval = get_user_pages(current, current->mm,
(unsigned long)to, 1, 1, 0, &pg, NULL);
if (retval == -ENOMEM && is_global_init(current)) {
up_read(¤t->mm->mmap_sem);
congestion_wait(BLK_RW_ASYNC, HZ/50);
goto survive;
}
if (retval != 1) {
up_read(¤t->mm->mmap_sem);
break;
}
maddr = kmap_atomic(pg, KM_USER0);
memcpy(maddr + offset, from, len);
...
}
But it seems to be that __copy_from_user_ll is not converted the
address at all before attempting to copy. Can someone help explain to
me why ?
Thanks
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copy_*_user
2011-10-22 17:06 copy_*_user Xin Tong
@ 2011-10-23 2:50 ` Américo Wang
2011-10-23 9:23 ` copy_*_user Xin Tong
0 siblings, 1 reply; 5+ messages in thread
From: Américo Wang @ 2011-10-23 2:50 UTC (permalink / raw)
To: Xin Tong; +Cc: linux-kernel
On Sun, Oct 23, 2011 at 1:06 AM, Xin Tong <xerox.time.tech@gmail.com> wrote:
> I am investigating copy_from_user and copy_to_user in linux under
> i386. These two function both take a pointer with virtual address and
> a pointer with physical address.
>
> copy_from_user calls __copy_from_user_ll and copy_to_user calls
> __copy_to_user_ll. It make sense to me that __copy_to_user_ll converts
> the virtual address to physical address using the current process's
> page table.
>
[...]
> But it seems to be that __copy_from_user_ll is not converted the
> address at all before attempting to copy. Can someone help explain to
> me why ?
>
You missed that __copy_to_user_ll() only does that when CONFIG_X86_WP_WORKS_OK
is not defined. And there is a comment right inside __copy_to_user_ll() said:
/*
* CPU does not honor the WP bit when writing
* from supervisory mode, and due to preemption or SMP,
* the page tables can change at any time.
* Do it manually. Manfred <manfred@colorfullife.com>
*/
this is why it uses kmap_atomic()+memcpy() to copy the data.
Also, all the addresses are virtual address.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copy_*_user
2011-10-23 2:50 ` copy_*_user Américo Wang
@ 2011-10-23 9:23 ` Xin Tong
2011-10-23 10:15 ` copy_*_user H. Peter Anvin
0 siblings, 1 reply; 5+ messages in thread
From: Xin Tong @ 2011-10-23 9:23 UTC (permalink / raw)
To: Américo Wang; +Cc: linux-kernel
I found that __copy_from_user_ll(void *to, const void __user *from,
unsigned long n) eventually calls some asm which use mov and rep on
the passed-in to & from pointers. This is in kernel mode and to & from
are virtual addresses ? are not the kernel accessing physical RAM
directly ( without pagetable ) ?
Thanks
Xin
On Sat, Oct 22, 2011 at 10:50 PM, Américo Wang <xiyou.wangcong@gmail.com> wrote:
> On Sun, Oct 23, 2011 at 1:06 AM, Xin Tong <xerox.time.tech@gmail.com> wrote:
>> I am investigating copy_from_user and copy_to_user in linux under
>> i386. These two function both take a pointer with virtual address and
>> a pointer with physical address.
>>
>> copy_from_user calls __copy_from_user_ll and copy_to_user calls
>> __copy_to_user_ll. It make sense to me that __copy_to_user_ll converts
>> the virtual address to physical address using the current process's
>> page table.
>>
> [...]
>> But it seems to be that __copy_from_user_ll is not converted the
>> address at all before attempting to copy. Can someone help explain to
>> me why ?
>>
>
> You missed that __copy_to_user_ll() only does that when CONFIG_X86_WP_WORKS_OK
> is not defined. And there is a comment right inside __copy_to_user_ll() said:
>
>
> /*
> * CPU does not honor the WP bit when writing
> * from supervisory mode, and due to preemption or SMP,
> * the page tables can change at any time.
> * Do it manually. Manfred <manfred@colorfullife.com>
> */
>
> this is why it uses kmap_atomic()+memcpy() to copy the data.
>
> Also, all the addresses are virtual address.
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copy_*_user
2011-10-23 9:23 ` copy_*_user Xin Tong
@ 2011-10-23 10:15 ` H. Peter Anvin
2011-10-23 11:25 ` copy_*_user Xin Tong
0 siblings, 1 reply; 5+ messages in thread
From: H. Peter Anvin @ 2011-10-23 10:15 UTC (permalink / raw)
To: Xin Tong; +Cc: Américo Wang, linux-kernel
On 10/23/2011 11:23 AM, Xin Tong wrote:
> I found that __copy_from_user_ll(void *to, const void __user *from,
> unsigned long n) eventually calls some asm which use mov and rep on
> the passed-in to & from pointers. This is in kernel mode and to & from
> are virtual addresses ? are not the kernel accessing physical RAM
> directly ( without pagetable ) ?
No. What gave you that idea?
-hpa
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: copy_*_user
2011-10-23 10:15 ` copy_*_user H. Peter Anvin
@ 2011-10-23 11:25 ` Xin Tong
0 siblings, 0 replies; 5+ messages in thread
From: Xin Tong @ 2011-10-23 11:25 UTC (permalink / raw)
To: H. Peter Anvin; +Cc: Américo Wang, linux-kernel
I do not remember, probably I was under the impression that kernel
accesses physical memory directly from my os course in college. So ,
the kernel access memory using the page table of the current process
here?
Thanks
Xin
On Sun, Oct 23, 2011 at 6:15 AM, H. Peter Anvin <hpa@zytor.com> wrote:
> On 10/23/2011 11:23 AM, Xin Tong wrote:
>> I found that __copy_from_user_ll(void *to, const void __user *from,
>> unsigned long n) eventually calls some asm which use mov and rep on
>> the passed-in to & from pointers. This is in kernel mode and to & from
>> are virtual addresses ? are not the kernel accessing physical RAM
>> directly ( without pagetable ) ?
>
> No. What gave you that idea?
>
> -hpa
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-10-23 11:25 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-22 17:06 copy_*_user Xin Tong
2011-10-23 2:50 ` copy_*_user Américo Wang
2011-10-23 9:23 ` copy_*_user Xin Tong
2011-10-23 10:15 ` copy_*_user H. Peter Anvin
2011-10-23 11:25 ` copy_*_user Xin Tong
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.