Linux MIPS Architecture development
 help / color / mirror / Atom feed
From: Ralf Baechle <ralf@linux-mips.org>
To: Sam Ravnborg <sam@ravnborg.org>
Cc: linux-mips <linux-mips@linux-mips.org>,
	Wu Zhangjin <wuzhangjin@gmail.com>
Subject: Re: [PATCH 0/6] mips: diverse Makefile updates
Date: Tue, 1 Jun 2010 11:28:22 +0100	[thread overview]
Message-ID: <20100601102822.GA20578@linux-mips.org> (raw)
In-Reply-To: <20100531105550.GA15995@merkur.ravnborg.org>

On Mon, May 31, 2010 at 12:55:50PM +0200, Sam Ravnborg wrote:

> > I played with it for a bit.  The warning is present in all gcc 4.1.0 to
> > 4.1.2 and it is bogus.  When I first looked into this years ago I just
> > gave up on gcc 4.1 as a newer version was already available.
> > 
> > The variable returned by get_user is undefined in case of an error, so
> > what get_user() is doing is entirely legitimate.  This is different from
> > copy_from_user() which in case of an error will clear the remainder of
> > the destination area which couldn't not be copied from userspace.
> 
> What I looked at:
> 
> 1)	u32 word;
> 2)	if (unlikely(get_user(word, header)))
> 3)		word = 0;
> 4)	if (word == magic.cmp)
> 5)		return PAGE_SIZE;
> 
> If gcc does not see an assignment to word in line 2) then
> it complains about the use line 4).
> 
> If we look at the implementation of get_user it is more or less the
> following:
> ({
> 	int err = -EFAULT;
> 	if (access_ok(VERIFY_READ, header))
> 		switch (sizeof(word)) {
> 		case 4:
> 			word = *(u32 *)header;
> 			err = 0;
> 			break;
> 		default:
> 			__get_user_unknown();
> 			break;
> 		}
> 	err;
> })
> 
> Simplified a lot - I know. But it shows my point.
> (And simplifying the code also helped me understand the macros).
> 
> gcc needs to be smart enough to deduce that we always return != 0
> in the cases where word is not assigned - in which case line 3)
> will do the assignment.
> 
> So gcc is indeed wrong when is sas "uninitialized" but considering
> the complexity of these macros I think it is excused.
> 
> The x86 version has the following assignment
> 
>     (val) = (__typeof__(*(addr))) __gu_tmp; 
> 
> unconditionally - so they avoid the " = 0" thing.
> sparc has explicit "= 0" assignments.
> 
> So refactoring the macros may do the trick too.
> But I do not think it is worth the effort.

One reason for the being written as they are is also that this allows a
compiler to generate some useful warnings such as:

	get_user(var, ptr);
	printk("var is %d\n", var);

Where var indeed can be used uninitialzed.  The return value of get_user()
being ignored is really a separate problem which should be attacked as
well.  __must_check would be the obvious way of doing this but it can only
be used as a function attribute.  Will have to experiment to see if it's
possible to use it within get_user / put_user in something like:

static inline int __must_check __gu_must_check(int __gu_err)
{
	return __gu_err
}

#define __get_user_check(x, ptr, size)                                  \
({                                                                      \
        int __gu_err = -EFAULT;                                         \
        const __typeof__(*(ptr)) __user * __gu_ptr = (ptr);             \
                                                                        \
        might_fault();                                                  \
        if (likely(access_ok(VERIFY_READ,  __gu_ptr, size)))            \
                __get_user_common((x), size, __gu_ptr);                 \
                                                                        \
        __gu_must_check(__gu_err);					\
})

  Ralf

  reply	other threads:[~2010-06-01 10:28 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-30 14:19 [PATCH 0/6] mips: diverse Makefile updates Sam Ravnborg
2010-05-30 14:26 ` [PATCH 1/6] mips: introduce arch/mips/Kbuild Sam Ravnborg
2010-05-30 14:26 ` [PATCH 2/6] mips: add -Werror to arch/mips/Kbuild Sam Ravnborg
2010-05-30 14:27 ` [PATCH 3/6] mips: introduce support for Platform definitions Sam Ravnborg
2010-05-30 14:27 ` [PATCH 4/6] mips: refactor arch/mips/boot/Makefile Sam Ravnborg
2010-05-30 14:28 ` [PATCH 5/6] mips: refactor arch/mips/boot/compressed/Makefile Sam Ravnborg
2010-05-30 14:28 ` [PATCH 6/6] mips: clean up arch/mips/Makefile Sam Ravnborg
2010-05-30 15:39 ` [PATCH 0/6] mips: diverse Makefile updates Sam Ravnborg
2010-05-30 23:19   ` Ralf Baechle
2010-05-31 10:29     ` Ralf Baechle
2010-05-31 10:55       ` Sam Ravnborg
2010-06-01 10:28         ` Ralf Baechle [this message]
2010-05-30 18:03 ` [ Sam Ravnborg
2010-05-30 18:06 ` [PATCH] mips: fix uninitialized warning when using get_user() Sam Ravnborg
2010-05-31  8:45 ` [PATCH 0/6] mips: diverse Makefile updates Wu Zhangjin
2010-05-31  9:10   ` Sam Ravnborg
2010-05-31 14:56 ` Ralf Baechle
2010-05-31 15:33 ` Manuel Lauss
2010-05-31 18:03   ` [PATCH] mips: fix build with O= Sam Ravnborg
2010-05-31 18:03     ` Sam Ravnborg
2010-05-31 18:19     ` Manuel Lauss
2010-05-31 19:00       ` Sam Ravnborg
2010-05-31 22:36         ` Ralf Baechle
2010-05-31 22:46     ` Ralf Baechle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100601102822.GA20578@linux-mips.org \
    --to=ralf@linux-mips.org \
    --cc=linux-mips@linux-mips.org \
    --cc=sam@ravnborg.org \
    --cc=wuzhangjin@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox