linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: {put,get}_user() side effects
       [not found] <1HVGV-1Wl-21@gated-at.bofh.it>
@ 2004-04-06 11:32 ` Andi Kleen
  2004-04-06 11:46   ` Geert Uytterhoeven
                     ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Andi Kleen @ 2004-04-06 11:32 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: linux-kernel

Geert Uytterhoeven <geert@linux-m68k.org> writes:

> On most (all?) architectures {get,put}_user() has side effects:
>
> #define put_user(x,ptr)                                                 \
>   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

Neither typeof not sizeof are supposed to have side effects. If your
compiler generates them that's a compiler bug.

-Andi


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

* Re: {put,get}_user() side effects
  2004-04-06 11:32 ` {put,get}_user() side effects Andi Kleen
@ 2004-04-06 11:46   ` Geert Uytterhoeven
  2004-04-06 20:53     ` Horst von Brand
  2004-04-06 11:50   ` Xavier Bestel
  2004-04-06 14:28   ` Xavier Bestel
  2 siblings, 1 reply; 11+ messages in thread
From: Geert Uytterhoeven @ 2004-04-06 11:46 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Linux Kernel Development

On Tue, 6 Apr 2004, Andi Kleen wrote:
> Geert Uytterhoeven <geert@linux-m68k.org> writes:
> > On most (all?) architectures {get,put}_user() has side effects:
> >
> > #define put_user(x,ptr)                                                 \
> >   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
>
> Neither typeof not sizeof are supposed to have side effects. If your
> compiler generates them that's a compiler bug.

>From a simple compile test, you seem to be right... Weird, since it does expand
to 3 times 'pIndex++', but pIndex is incremented only once.

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
							    -- Linus Torvalds

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

* Re: {put,get}_user() side effects
  2004-04-06 11:32 ` {put,get}_user() side effects Andi Kleen
  2004-04-06 11:46   ` Geert Uytterhoeven
@ 2004-04-06 11:50   ` Xavier Bestel
  2004-04-06 12:17     ` Wichert Akkerman
  2004-04-06 14:28   ` Xavier Bestel
  2 siblings, 1 reply; 11+ messages in thread
From: Xavier Bestel @ 2004-04-06 11:50 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Geert Uytterhoeven, linux-kernel

On Tue, 2004-04-06 at 13:32 +0200, Andi Kleen wrote:
> Geert Uytterhoeven <geert@linux-m68k.org> writes:
> 
> > On most (all?) architectures {get,put}_user() has side effects:
> >
> > #define put_user(x,ptr)                                                 \
> >   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
> 
> Neither typeof not sizeof are supposed to have side effects. If your
> compiler generates them that's a compiler bug.

Using ptr three times in a define has side effects if ptr is an
expression with side effects (e.g. "p++").

	Xav


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

* Re: {put,get}_user() side effects
  2004-04-06 11:50   ` Xavier Bestel
@ 2004-04-06 12:17     ` Wichert Akkerman
  0 siblings, 0 replies; 11+ messages in thread
From: Wichert Akkerman @ 2004-04-06 12:17 UTC (permalink / raw)
  To: linux-kernel

Previously Xavier Bestel wrote:
> Using ptr three times in a define has side effects if ptr is an
> expression with side effects (e.g. "p++").

As I understand it both typeof and sizeof don't evalutate their argument
but only look at its type. Which means using p++ is perfectly safe.

Wichert.

-- 
Wichert Akkerman <wichert@wiggy.net>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.


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

* Re: {put,get}_user() side effects
  2004-04-06 11:32 ` {put,get}_user() side effects Andi Kleen
  2004-04-06 11:46   ` Geert Uytterhoeven
  2004-04-06 11:50   ` Xavier Bestel
@ 2004-04-06 14:28   ` Xavier Bestel
  2004-04-06 15:30     ` Kevin P. Fleming
  2 siblings, 1 reply; 11+ messages in thread
From: Xavier Bestel @ 2004-04-06 14:28 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Geert Uytterhoeven, linux-kernel

> On Tue, 2004-04-06 at 13:32 +0200, Andi Kleen wrote:
> > Geert Uytterhoeven <geert@linux-m68k.org> writes:
> > 
> > > On most (all?) architectures {get,put}_user() has side effects:
> > >
> > > #define put_user(x,ptr)                                                 \
> > >   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
> > 
> > Neither typeof not sizeof are supposed to have side effects. If your
> > compiler generates them that's a compiler bug.
> 
> Using ptr three times in a define has side effects if ptr is an
> expression with side effects (e.g. "p++").

Sorry, I read too fast. I didn't know sizeof could avoid side effects.

	Xav


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

* Re: {put,get}_user() side effects
  2004-04-06 14:28   ` Xavier Bestel
@ 2004-04-06 15:30     ` Kevin P. Fleming
  2004-04-06 15:47       ` Xavier Bestel
  0 siblings, 1 reply; 11+ messages in thread
From: Kevin P. Fleming @ 2004-04-06 15:30 UTC (permalink / raw)
  To: Xavier Bestel; +Cc: Andi Kleen, Geert Uytterhoeven, linux-kernel

Xavier Bestel wrote:

> Sorry, I read too fast. I didn't know sizeof could avoid side effects.
> 

Both typeof and sizeof are compile-time constructs, so there is no 
opportunity for any expression side effects to occur. Presumably one 
could do:

typeof(1/0UL)

without ever causing the obvious side effect either (granted, this is a 
pointless piece of code :-)).

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

* Re: {put,get}_user() side effects
  2004-04-06 15:30     ` Kevin P. Fleming
@ 2004-04-06 15:47       ` Xavier Bestel
  0 siblings, 0 replies; 11+ messages in thread
From: Xavier Bestel @ 2004-04-06 15:47 UTC (permalink / raw)
  To: Kevin P. Fleming; +Cc: Andi Kleen, Geert Uytterhoeven, linux-kernel

On Tue, 2004-04-06 at 08:30 -0700, Kevin P. Fleming wrote:
> Xavier Bestel wrote:
> 
> > Sorry, I read too fast. I didn't know sizeof could avoid side effects.
> > 
> 
> Both typeof and sizeof are compile-time constructs, so there is no 
> opportunity for any expression side effects to occur. Presumably one 
> could do:
> 
> typeof(1/0UL)
> 
> without ever causing the obvious side effect either (granted, this is a 
> pointless piece of code :-)).

Yeah, now that you tell it, it seems obvious when you consider macros
like min/max and friends which rely on typeof to avoid side-effects.



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

* Re: {put,get}_user() side effects
  2004-04-06 11:46   ` Geert Uytterhoeven
@ 2004-04-06 20:53     ` Horst von Brand
  2004-04-06 22:53       ` Andreas Schwab
  0 siblings, 1 reply; 11+ messages in thread
From: Horst von Brand @ 2004-04-06 20:53 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: Linux Kernel Mailing List

Geert Uytterhoeven <geert@linux-m68k.org> said:
> On Tue, 6 Apr 2004, Andi Kleen wrote:
> > Geert Uytterhoeven <geert@linux-m68k.org> writes:
> > > On most (all?) architectures {get,put}_user() has side effects:
> > >
> > > #define put_user(x,ptr)                                                 \
> > >   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
> >
> > Neither typeof not sizeof are supposed to have side effects. If your
> > compiler generates them that's a compiler bug.

> From a simple compile test, you seem to be right... Weird, since it does
> expand to 3 times 'pIndex++', but pIndex is incremented only once.

Better check with a C language lawyer. Maybe gcc gets it wrong, or it is
undefined (in which case next gcc could screw you over, and give you a hard
time finding out how...). An inline should be safe, and unless gcc still
gets them wrong, equally fast/small.
-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

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

* Re: {put,get}_user() side effects
  2004-04-06 20:53     ` Horst von Brand
@ 2004-04-06 22:53       ` Andreas Schwab
  0 siblings, 0 replies; 11+ messages in thread
From: Andreas Schwab @ 2004-04-06 22:53 UTC (permalink / raw)
  To: Horst von Brand; +Cc: Geert Uytterhoeven, Linux Kernel Mailing List

Horst von Brand <vonbrand@inf.utfsm.cl> writes:

> Geert Uytterhoeven <geert@linux-m68k.org> said:
>> On Tue, 6 Apr 2004, Andi Kleen wrote:
>> > Geert Uytterhoeven <geert@linux-m68k.org> writes:
>> > > On most (all?) architectures {get,put}_user() has side effects:
>> > >
>> > > #define put_user(x,ptr)                                                 \
>> > >   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
>> >
>> > Neither typeof not sizeof are supposed to have side effects. If your
>> > compiler generates them that's a compiler bug.
>
>> From a simple compile test, you seem to be right... Weird, since it does
>> expand to 3 times 'pIndex++', but pIndex is incremented only once.
>
> Better check with a C language lawyer. Maybe gcc gets it wrong, or it is
> undefined

It's not undefined, the standard explicitly says that the argument of
sizeof is not evaluated (unless its type is a VLA).  I can't remember gcc
ever getting that wrong.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: {put,get}_user() side effects
  2004-04-06 10:03 Geert Uytterhoeven
@ 2004-04-06 10:10 ` Russell King
  0 siblings, 0 replies; 11+ messages in thread
From: Russell King @ 2004-04-06 10:10 UTC (permalink / raw)
  To: Geert Uytterhoeven; +Cc: Linux Kernel Development

On Tue, Apr 06, 2004 at 12:03:14PM +0200, Geert Uytterhoeven wrote:
> On most (all?) architectures {get,put}_user() has side effects:
> 
> #define put_user(x,ptr)                                                 \
>   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))

I thought this came up before, and it was decided that put_user and
get_user must not have such side effects - I certainly remember fixing
this very thing on ARM.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 PCMCIA      - http://pcmcia.arm.linux.org.uk/
                 2.6 Serial core

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

* {put,get}_user() side effects
@ 2004-04-06 10:03 Geert Uytterhoeven
  2004-04-06 10:10 ` Russell King
  0 siblings, 1 reply; 11+ messages in thread
From: Geert Uytterhoeven @ 2004-04-06 10:03 UTC (permalink / raw)
  To: Linux Kernel Development


On most (all?) architectures {get,put}_user() has side effects:

#define put_user(x,ptr)                                                 \
  __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))


E.g. drivers/char/ip2main.c seems to be completely broken due to constructs
like:

    rc = put_user(i2Input, pIndex++ );

I only noticed these because the compiler gave some warnings due to other
reasons.

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
							    -- Linus Torvalds

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

end of thread, other threads:[~2004-04-06 22:53 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1HVGV-1Wl-21@gated-at.bofh.it>
2004-04-06 11:32 ` {put,get}_user() side effects Andi Kleen
2004-04-06 11:46   ` Geert Uytterhoeven
2004-04-06 20:53     ` Horst von Brand
2004-04-06 22:53       ` Andreas Schwab
2004-04-06 11:50   ` Xavier Bestel
2004-04-06 12:17     ` Wichert Akkerman
2004-04-06 14:28   ` Xavier Bestel
2004-04-06 15:30     ` Kevin P. Fleming
2004-04-06 15:47       ` Xavier Bestel
2004-04-06 10:03 Geert Uytterhoeven
2004-04-06 10:10 ` Russell King

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).