Skip to contentSkip to conversation

2.5 fast poll on ppc64

PATCH Mikael Pettersson 4 participants 4 messages Dec 26, 2002 – Dec 27, 2002

lore
Share & tools
lore
#0 lore raw
Quoted 8 lines · Manfred Spraul
Quoted 6 lines · Mikael Pettersson
Quoted 5 lines · Anton Blanchard

I was unable to boot 2.5-BK on ppc64 and narrowed it down to the fast poll patch. I found:

offsetof(struct poll_list, entries) == 12 but
sizeof(struct poll_list) == 16

...

Quoted 17 lines
Quoted 9 lines

To me (I'm a compiler writer) it looks like your compiler did NOT mess up. Assuming struct pollfd has 32-bit alignment, the compiler is doing the right thing by starting entries[] at offset 12. The 16-byte size for struct poll_list is because the 'next' field has 64-bit alignment, which forces the compiler to pad struct poll_lists's size to a multiple of 8 bytes, i.e. 16 bytes in this case. So the compiler is not broken.

I would agree, but there is a special restriction on offsetof() and sizeof() of structures with flexible array members: section 6.7.2.1, clause 16:

First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.

The simplest test case I've found is

Quoted 7 lines

struct a1 { int a; char b; short c[];};
struct a2 { int a; char b; short c[1];};

   sizeof(struct a{1,2}) is 8.
   offsetof(struct a{1,2}, c) is 6.

--> sizeof(struct a1) != offsetof(struct a2, c)

Oh dear. I checked my C9x draft copy and you seem to be right. The standard states that sizeof(struct a1) == offsetof(struct a1, c), but both gcc (2.95.3 and 3.2) and Intel's icc (7.0) get it wrong on x86:
they make sizeof(struct a1) == 8 but offsetof(struct a1, c) == 6.

Technically speaking, the kernel code which uses 'entries[0]' is non-compliant since the proper syntax is 'entries[]', but the empty array size syntax isn't implemented in gcc 2.95.3.

Quoted 2 lines
Quoted 2 lines

But the old code which assumed pp+1 == pp->entries is so horribly broken I can't find words for it. s/pp+1/pp->entries/ is the correct fix.

My mistake. The old code is Ok for C99, but broken for ANSI-C.

Quoted 7 lines

I agree. Should we fix the kmalloc allocations, too?

(quoted diff) +1 -1
-	pp = kmalloc(sizeof(struct poll_list)+
+	pp = kmalloc(offsetof(struct poll_list,entries)+
			sizeof(struct pollfd)*
			(i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
				GFP_KERNEL);

Yes, this should be changed as you suggest. The old code only works in C99-compliant implementations, but we now know that both gcc and icc get this wrong, so it seems prudent to revert to the classical formulation, using the 'entries[0]' declaration syntax and offsetof() instead of sizeof().

/Mikael

#1 lore raw
Quoted 4 lines · Mikael Pettersson

Technically speaking, the kernel code which uses 'entries[0]' is non-compliant since the proper syntax is 'entries[]', but the empty array size syntax isn't implemented in gcc 2.95.3.

The two things have two totally different semantics:

- array[0] is a zero-sized array, and is a long-time gcc extension that has nothing to do with the modern "flexible array". The kernel uses zero-sized arrays, because flexible arrays simply aren't historically supported by gcc at all.

- array[] is the standard "flexible array" thing, and has different rules than array[0]. For example, sizeof() is undefined on flexible arrays, but is well-defined on zero-sized ones (at zero). Also, the alignment constraints are potentially quite different.

The gcc people want to make the two behave fairly similarly to ease implementation issues, but they are definitely not the same.

Quoted 1 line

My mistake. The old code is Ok for C99, but broken for ANSI-C.

The old code is ugly and arguably always broken, I agree 100% with making the usage code use "pp->entries". Whether the allocators use "sizeof" or "offsetof" is secondary, at worst it ends up being conservative.

Linus

#2 lore raw
In reply to Mikael Pettersson
Quoted 6 lines · Mikael Pettersson
Quoted 6 lines

struct a1 { int a; char b; short c[];};
struct a2 { int a; char b; short c[1];};

sizeof(struct a{1,2}) is 8.
offsetof(struct a{1,2}, c) is 6.

Quoted 10 lines
Quoted 3 lines

--> sizeof(struct a1) != offsetof(struct a2, c)

Oh dear. I checked my C9x draft copy and you seem to be right. The standard states that sizeof(struct a1) == offsetof(struct a1, c), but both gcc (2.95.3 and 3.2) and Intel's icc (7.0) get it wrong on x86:
they make sizeof(struct a1) == 8 but offsetof(struct a1, c) == 6.

I've filed a gcc bug, no 9058: As the reply, I got a mail from Joseph Myers with links to TC to the C99 standard:
http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n983.htm
http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n987.htm

Quoted 4 lines
Quoted 4 lines

I agree. Should we fix the kmalloc allocations, too?

Quoted 15 lines
Quoted 7 lines
(quoted diff) +1 -1
-	pp = kmalloc(sizeof(struct poll_list)+
+	pp = kmalloc(offsetof(struct poll_list,entries)+
			sizeof(struct pollfd)*
			(i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
				GFP_KERNEL);

Yes, this should be changed as you suggest. The old code only works in C99-compliant implementations, but we now know that both gcc and icc get this wrong, so it seems prudent to revert to the classical formulation, using the 'entries[0]' declaration syntax and offsetof() instead of sizeof().

I found an even simpler formula:

   offsetof(struct poll_list,entries[i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i];

I'll ask the gcc guys if that formula is a portable solution.

--
Manfred

#3 lore raw
In reply to Mikael Pettersson
Quoted 7 lines · Mikael Pettersson
Quoted 7 lines

I would agree, but there is a special restriction on offsetof() and sizeof() of structures with flexible array members: section 6.7.2.1, clause 16:

First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.

[...]

Quoted 4 lines

Oh dear. I checked my C9x draft copy and you seem to be right. The standard states that sizeof(struct a1) == offsetof(struct a1, c), but both gcc (2.95.3 and 3.2) and Intel's icc (7.0) get it wrong on x86:
they make sizeof(struct a1) == 8 but offsetof(struct a1, c) == 6.

Indeed, *every* compiler that supports flexible array members does it this way. On the gcc lists we've conjectured that this will wind up being a Defect Report, and so have elected not to change anything for the nonce.

That said, you should also note that "struct S foo[0]" is *not* a flexible array member; "struct S foo[]" is. The former is gcc's zero-length array extension. There are subtle differences between the two features. See the gcc docs for details.

r~