0

I am using this function to copy some structures to the kernel. But, the problem is that I have to copy three data structures which are part of a bigger data structure. NOTE: the 3 data structures are contiguous in the bigger data structure.

SO, In my copy user function I pass the pointer of the 1st data structure and give the length of all the 3 data structure. But, when I go to user-space and print the 1st element of the 2nd data structure it gives some other value.

SO, what am I doing wrong. As, a solution I made 3 copt_to_user calls and to my surprise it works fine. Its the problem when I make a single copy_to_user call.

Please, let me know what could be the reason.

Hey guys thanks for the answer it was a alignment issue , but, going further, if I want to pad an internal structure how do I do it..? Example-

structure d{

struct b;

struct c; //I want to make this structure a padded one, how to go about it?

struct d;

}

Invictus
  • 2,653
  • 8
  • 31
  • 50
  • 2
    It's hard to tell what you're asking. Show us some code ? – cnicutar Jul 26 '12 at 21:23
  • struct a; struct b; struct c; struct d{struct a;struct b;struct c;} ..... then I call copy_to_user(pointer_in_user, &a, len).. where len=sizeof(struct a)+sizeof(struct b)+sizeof(struct c).... but, only value of a is seen and the value of struct b and struct c are messed up. Hope, you got my point. – Invictus Jul 26 '12 at 22:09
  • Edit the question, don't add as comment. – cnicutar Jul 26 '12 at 22:13
  • 6
    Sounds to me like alignment issues, but without code it's impossible to tell. Let me emphasize that: We need the actual structure definition and how you're copying to be able to tell if it's alignment issues. – Fred Jul 26 '12 at 22:18
  • 1
    @Invictus: As mentioned by Quentin below in his answer, you might have padding problem, quick try I would do: len = sizeof(struct d) instead – sergico Jul 28 '12 at 15:47
  • Hey thanks, it was alingment issue, can you answer the further question, as I have edited my question now. – Invictus Aug 03 '12 at 03:50

1 Answers1

2

As mentioned in the comments, this really seems to be an alignment problem. Gcc will probably add some padding between the structures a, b and c in struct d. Depending on how you instantiated the one in userland, it could be a problem. You can force gcc to not generate padding, using __atribute__ ((packed)) on your structure, but unless this structure maps to hardware registers, it's usually a bad idea as it will lead to worse performance when accessing fields of that structure.

Another possible problem would be if your kernel is 64 bits and your userland program is 32 bits, in this case you need to use fixed size types to be sure to have the same layout.

Quentin Casasnovas
  • 1,079
  • 5
  • 10
  • Hey thanks for the answer, can you please check for the question again, as I have edited it. Thanks – Invictus Aug 03 '12 at 03:49