Find the answer to your Linux question:
Results 1 to 5 of 5
Hi- can I write: Code: char __user *userAddr; char kernAddr; unsigned offset, n; : copy_to_user(userAddr + offset, kernAddr, n); to copy n bytes from kernAddr[0..n-1] in kernel space into usrAddr[offset..offset+n-1] ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Location
    Reading, UK
    Posts
    5

    copy_to_user() with offset


    Hi- can I write:
    Code:
    char __user *userAddr;
    char kernAddr;
    unsigned offset, n;
    :
    copy_to_user(userAddr + offset, kernAddr, n);
    to copy n bytes from kernAddr[0..n-1] in kernel space into usrAddr[offset..offset+n-1] in user space?

    I don't really understand how the user/kernel memory division works, so apologies if this is a silly question.

    Regards,
    John

  2. #2
    Linux Newbie dilbert's Avatar
    Join Date
    Sep 2006
    Location
    Yorkshire, GB
    Posts
    237
    I also don't understand how it "really" works ...

    But if it is defined as a pointer to a char array, you can use offsets as long as your offset doesn't end up somewhere else.

    BTW: Why don't you use a multiple of the sizeof of that array, but an ordinary unsigned number instead for your offset?
    Bus Error: Passengers dumped. Hech gap yo'q.

  3. #3
    Just Joined!
    Join Date
    Jun 2006
    Location
    Reading, UK
    Posts
    5
    Thanks for your reply dilbert.
    Quote Originally Posted by dilbert View Post
    But if it is defined as a pointer to a char array, you can use offsets as long as your offset doesn't end up somewhere else.
    What I am worried about is whether the user space memory is contiguous when 'seen' from the kernel ie. whether userAddr[0] and userAddr[1] are actually next to each other. Just to confirm, you are saying that they are?
    BTW: Why don't you use a multiple of the sizeof of that array, but an ordinary unsigned number instead for your offset?
    Sorry- it's Monday and I'm not quite with it. Please could you give an example?

    John

  4. #4
    Linux Newbie dilbert's Avatar
    Join Date
    Sep 2006
    Location
    Yorkshire, GB
    Posts
    237
    Surely, the memory is on both sides, kernel and user, contiguous. It would be the hit if it wasn't.

    But I don't know what's the amximum size and other constraints. I don't know if it was necessary or not, but I used a special structure type for that with kernel 2.4. Also, I used a checking function in front of it. I guess, all this changed now with kernel 2.6. But you still will have a return value that you can check for errors.

    So, you don't know how it's working, but al least if it's working.

    Therefore, if you have a larger array or are concerned anyway, you could find out what the constraints are and where they are comfigured.

    For offsets in an array, it is always a bad idea to do your own platform-dependent pointer maths.
    Better do it that way:

    Code:
    copy_to_user(userAddr + (offset * sizeof (userAddr[0])), kernAddr, n);
    This independent from the memory size of an array element.
    Bus Error: Passengers dumped. Hech gap yo'q.

  5. #5
    Just Joined!
    Join Date
    Jun 2006
    Location
    Reading, UK
    Posts
    5

    Thumbs up

    Quote Originally Posted by dilbert View Post
    Surely, the memory is on both sides, kernel and user, contiguous. It would be the hit if it wasn't.


    But I don't know what's the amximum size and other constraints. I don't know if it was necessary or not, but I used a special structure type for that with kernel 2.4. Also, I used a checking function in front of it. I guess, all this changed now with kernel 2.6. But you still will have a return value that you can check for errors.
    Good idea.

    For offsets in an array, it is always a bad idea to do your own platform-dependent pointer maths.
    Better do it that way:

    Code:
    copy_to_user(userAddr + (offset * sizeof (userAddr[0])), kernAddr, n);
    This independent from the memory size of an array element.
    Right, I understand now. In fact userAddr is a generic pointer to different arrays, and the array element size has already been taken into account in the calculation of offset. But it would still be good to use sizeof as you suggest.

    Thanks for your help.

    John
    Last edited by matth1j; 05-14-2007 at 11:27 AM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •