2007-08-30 22:29:33

by Clemens Kolbitsch

[permalink] [raw]
Subject: Copy large memory regions from & to userspace

Hi!
Just a short question: What is the correct method of copying large areas of
memory from userspace into userspace when running in kernel-mode?

According to just about any type of documentation out there something like

unsigned long *from = 0x08000000;
unsigned long *to = 0x09000000;
memcpy(to, from, 0x1000);

should be avoided as copy_from_user, put_user, etc. should be used instead...

Is there a third set of functions for direct from-userspace-to-userspace
copying or is it legitimate to assure that the first bytes in the from & to
memory areas are available and then just do the plain memcpy?

Maybe there is a simple "just use the xxx function-set" answer to that, that i
just have not ran into yet :-)

Thanks!


2007-08-31 13:45:41

by Clemens Kolbitsch

[permalink] [raw]
Subject: Re: Copy large memory regions from & to userspace

On Friday 31 August 2007 15:25:40 you wrote:
> On 8/30/07, Clemens Kolbitsch <[email protected]> wrote:
> > Hi!
> > Just a short question: What is the correct method of copying large areas
> > of memory from userspace into userspace when running in kernel-mode?
>
> relayfs?

no... I'm copying user-memory to user-memory, not kernel-to-user, however
running the code in kernel-mode.

what i wanted to know is how to check the access-rights...
i didn't get any other answers, so for now i'm just using

if (access_ok(VERIFY_READ, from, PAGE_SIZE) &&
access_ok(VERIFY_WRITE, to, PAGE_SIZE))
{
memcpy(to, from, PAGE_SIZE);
}

and hope that this is the *correct* way to do it...

2007-09-01 01:53:56

by Robert Hancock

[permalink] [raw]
Subject: Re: Copy large memory regions from & to userspace

Clemens Kolbitsch wrote:
> On Friday 31 August 2007 15:25:40 you wrote:
>> On 8/30/07, Clemens Kolbitsch <[email protected]> wrote:
>>> Hi!
>>> Just a short question: What is the correct method of copying large areas
>>> of memory from userspace into userspace when running in kernel-mode?
>> relayfs?
>
> no... I'm copying user-memory to user-memory, not kernel-to-user, however
> running the code in kernel-mode.
>
> what i wanted to know is how to check the access-rights...
> i didn't get any other answers, so for now i'm just using
>
> if (access_ok(VERIFY_READ, from, PAGE_SIZE) &&
> access_ok(VERIFY_WRITE, to, PAGE_SIZE))
> {
> memcpy(to, from, PAGE_SIZE);
> }
>
> and hope that this is the *correct* way to do it...

No, it's not. access_ok does not guarantee that the memory region can be
validly read or written. It only allows using __copy_to_user or
__copy_from_user which skips the same checks that access_ok does.

I'm not aware of any code in the kernel that does userspace-to-userspace
copies directly. Likely because there's rarely a need for it?

--
Robert Hancock Saskatoon, SK, Canada
To email, remove "nospam" from [email protected]
Home Page: http://www.roberthancock.com/

2007-09-01 06:11:18

by Jan Engelhardt

[permalink] [raw]
Subject: Re: Copy large memory regions from & to userspace


On Aug 31 2007 19:48, Robert Hancock wrote:
>
> I'm not aware of any code in the kernel that does userspace-to-userspace
> copies directly. Likely because there's rarely a need for it?

splice(), sort of.



Jan
--