kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* struct nbd_reply not packed => trouble?
@ 2013-01-10 16:09 Sebastian Pipping
  2013-01-10 16:36 ` Yann Droneaud
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Pipping @ 2013-01-10 16:09 UTC (permalink / raw)
  To: kernelnewbies

Hello kernel hackers,


I noticed that in [linux]/include/uapi/linux/nbd.h the structure

   struct nbd_request {
           __be32 magic;
           __be32 type;    /* == READ || == WRITE */
           char handle[8];
           __be64 from;
           __be32 len;
   } __attribute__((packed));

is packed but its reply counter part

   struct nbd_reply {
           __be32 magic;
           __be32 error;    /* 0 = ok, else error */
	  char handle[8];  /* handle you got from request */
   };

is not.  Since Linux seems to read sizeof(nbd_reply) bytes from the 
network (see [1]), the number of bytes read varies with the number of 
bytes of the structure.

So my understanding is that if the size of struct nbd_reply varies from 
platform/compiler to another that means trouble.  I wonder:

  - Is there anything about struct nbd_reply that would keep its size
    equal everywhere or a reason why variance in size is no problem here?

  - Any ideas for a platform where you would expect struct nbd_reply to
    be other than 4 + 4 + 8 = 16 bytes in size?

Best,



Sebastian


[1] http://lxr.linux.no/#linux+v3.7.1/drivers/block/nbd.c#L336

^ permalink raw reply	[flat|nested] 5+ messages in thread

* struct nbd_reply not packed => trouble?
  2013-01-10 16:09 struct nbd_reply not packed => trouble? Sebastian Pipping
@ 2013-01-10 16:36 ` Yann Droneaud
  2013-01-13 16:45   ` Sebastian Pipping
  2013-01-14 23:59   ` 卜弋天
  0 siblings, 2 replies; 5+ messages in thread
From: Yann Droneaud @ 2013-01-10 16:36 UTC (permalink / raw)
  To: kernelnewbies

> Hello kernel hackers,
>
>
> I noticed that in [linux]/include/uapi/linux/nbd.h the structure
>
>    struct nbd_request {
>            __be32 magic;
>            __be32 type;    /* == READ || == WRITE */
>            char handle[8];
>            __be64 from;
>            __be32 len;
>    } __attribute__((packed));
>
> is packed

Given a target where ABI requires types to be aligned on their size (1 =>
1, 2 => 2, 4 => 4, 8 => 8, etc.)


4 + 4 + 8 + 8 + 4 = 28 bytes

To satisfy __be64 alignement (the strictest), it must be aligned on a 8
bytes boundary, but 28 is not multiple of 8. 32 is. Padding is required.

[The padding is required so that in a array of struct nbd_request[],
 all members of all structures are aligned to their requirement alignement]


> but its reply counter part
>
>    struct nbd_reply {
>            __be32 magic;
>            __be32 error;    /* 0 = ok, else error */
> 	  char handle[8];  /* handle you got from request */
>    };
>

4 + 4 + 8 = 16 bytes.
To satisfy __be32 alignement (the strictest), it must be aligned on a 4
bytes boundary and 16 is a multiple of 4: no need for padding.

> is not.  Since Linux seems to read sizeof(nbd_reply) bytes from the
> network (see [1]), the number of bytes read varies with the number of
> bytes of the structure.


It won't.

>
> So my understanding is that if the size of struct nbd_reply varies from
> platform/compiler to another that means trouble.  I wonder:
>
>   - Is there anything about struct nbd_reply that would keep its size
>     equal everywhere or a reason why variance in size is no problem here?
>
>   - Any ideas for a platform where you would expect struct nbd_reply to
>     be other than 4 + 4 + 8 = 16 bytes in size?
>

Look for an ABI that require 'char' with either property:
- size > 1 byte
- alignment > 8 (16 bytes !)


Regards.

-- 
Yann Droneaud
OPTEYA

^ permalink raw reply	[flat|nested] 5+ messages in thread

* struct nbd_reply not packed => trouble?
  2013-01-10 16:36 ` Yann Droneaud
@ 2013-01-13 16:45   ` Sebastian Pipping
  2013-01-14 23:59   ` 卜弋天
  1 sibling, 0 replies; 5+ messages in thread
From: Sebastian Pipping @ 2013-01-13 16:45 UTC (permalink / raw)
  To: kernelnewbies

I see, thank you!

Best,



Sebastian

^ permalink raw reply	[flat|nested] 5+ messages in thread

* struct nbd_reply not packed => trouble?
  2013-01-10 16:36 ` Yann Droneaud
  2013-01-13 16:45   ` Sebastian Pipping
@ 2013-01-14 23:59   ` 卜弋天
  2013-01-15  9:59     ` Yann Droneaud
  1 sibling, 1 reply; 5+ messages in thread
From: 卜弋天 @ 2013-01-14 23:59 UTC (permalink / raw)
  To: kernelnewbies



? 2013-1-11?0:42?"Yann Droneaud" <ydroneaud@opteya.com> ???

>> Hello kernel hackers,
>> 
>> 
>> I noticed that in [linux]/include/uapi/linux/nbd.h the structure
>> 
>>   struct nbd_request {
>>           __be32 magic;
>>           __be32 type;    /* == READ || == WRITE */
>>           char handle[8];
>>           __be64 from;
>>           __be32 len;
>>   } __attribute__((packed));
>> 
>> is packed
> 
> Given a target where ABI requires types to be aligned on their size (1 =>
> 1, 2 => 2, 4 => 4, 8 => 8, etc.)
> 
> 
> 4 + 4 + 8 + 8 + 4 = 28 bytes
> 
> To satisfy __be64 alignement (the strictest), it must be aligned on a 8
> bytes boundary, but 28 is not multiple of 8. 32 is. Padding is required.
> 
> [The padding is required so that in a array of struct nbd_request[],
> all members of all structures are aligned to their requirement alignement]
> 
> 
>> but its reply counter part
>> 
>>   struct nbd_reply {
>>           __be32 magic;
>>           __be32 error;    /* 0 = ok, else error */
>>      char handle[8];  /* handle you got from request */
>>   };
> 
> 4 + 4 + 8 = 16 bytes.
> To satisfy __be32 alignement (the strictest), it must be aligned on a 4
> bytes boundary and 16 is a multiple of 4: no need for padding.
> 
>> is not.  Since Linux seems to read sizeof(nbd_reply) bytes from the
>> network (see [1]), the number of bytes read varies with the number of
>> bytes of the structure.
> 
> 
> It won't.
> 
>> 
>> So my understanding is that if the size of struct nbd_reply varies from
>> platform/compiler to another that means trouble.  I wonder:
>> 
>>  - Is there anything about struct nbd_reply that would keep its size
>>    equal everywhere or a reason why variance in size is no problem here?
>> 
>>  - Any ideas for a platform where you would expect struct nbd_reply to
>>    be other than 4 + 4 + 8 = 16 bytes in size?
> 
> Look for an ABI that require 'char' with either property:
> - size > 1 byte
> - alignment > 8 (16 bytes !)
> 
> 

      Do you mean this structure size will vary on different platform? If yes, why Linux does not packed here? Thanks


> Regards.
> 
> -- 
> Yann Droneaud
> OPTEYA
> 
> 
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* struct nbd_reply not packed => trouble?
  2013-01-14 23:59   ` 卜弋天
@ 2013-01-15  9:59     ` Yann Droneaud
  0 siblings, 0 replies; 5+ messages in thread
From: Yann Droneaud @ 2013-01-15  9:59 UTC (permalink / raw)
  To: kernelnewbies

Hi,

Le mardi 15 janvier 2013 ? 07:59 +0800, ??? a ?crit :
> 
> ? 2013-1-11?0:42?"Yann Droneaud" <ydroneaud@opteya.com> ???

> >>  - Any ideas for a platform where you would expect struct nbd_reply to
> >>    be other than 4 + 4 + 8 = 16 bytes in size?
> > 
> > Look for an ABI that require 'char' with either property:
> > - size > 1 byte
> > - alignment > 8 (16 bytes !)
> > 
> > 
> 
>       Do you mean this structure size will vary on different platform? If yes, why Linux does not packed here? Thanks
> 

There's probably no such architecture for a general purpose processor
with the ABI properties I've mentionned. eg. (sizeof(char) > 1 ||
alignof(char) > 8).

And if one day Linux is going to be ported on such target, the packing
of the nbd_reply struct will be the smallest problem to fix.

Regards

-- 
Yann Droneaud
OPTEYA

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-01-15  9:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-10 16:09 struct nbd_reply not packed => trouble? Sebastian Pipping
2013-01-10 16:36 ` Yann Droneaud
2013-01-13 16:45   ` Sebastian Pipping
2013-01-14 23:59   ` 卜弋天
2013-01-15  9:59     ` Yann Droneaud

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).