-Wuninitialized warning in drivers/misc/sgi-xp/xpc_partition.c

7 views
Skip to first unread message

Nathan Chancellor

unread,
May 2, 2019, 11:33:44 PM5/2/19
to Cliff Whickman, Robin Holt, Arnd Bergmann, Greg Kroah-Hartman, linux-...@vger.kernel.org, Nick Desaulniers, clang-bu...@googlegroups.com
Hi all,

When building with -Wuninitialized, Clang warns:

drivers/misc/sgi-xp/xpc_partition.c:73:14: warning: variable 'buf' is uninitialized when used within its own initialization [-Wuninitialized]
void *buf = buf;
~~~ ^~~
1 warning generated.

I am not really sure how to properly initialize buf in this instance.
I would assume it would involve xpc_kmalloc_cacheline_aligned like
further down in the function but maybe not, this function isn't entirely
clear. Could we get your input, this is one of the last warnings I see
in a few allyesconfig builds.

Thanks,
Nathan

Nathan Chancellor

unread,
May 22, 2019, 9:56:42 PM5/22/19
to Cliff Whickman, Robin Holt, Arnd Bergmann, Greg Kroah-Hartman, linux-...@vger.kernel.org, Nick Desaulniers, clang-bu...@googlegroups.com
Hi all,

Friendly ping for comments/input. This is one of a few remaining
warnings I see, it'd be nice to get it fixed up so we can turn it on for
the whole kernel.

Cheers,
Nathan

Greg Kroah-Hartman

unread,
May 23, 2019, 1:53:02 AM5/23/19
to Nathan Chancellor, Cliff Whickman, Robin Holt, Arnd Bergmann, linux-...@vger.kernel.org, Nick Desaulniers, clang-bu...@googlegroups.com
Patches are gladly welcome :)

thanks,

greg k-h

Stephen Hines

unread,
May 23, 2019, 2:58:25 AM5/23/19
to Greg Kroah-Hartman, Nathan Chancellor, Cliff Whickman, Robin Holt, Arnd Bergmann, linux-...@vger.kernel.org, Nick Desaulniers, clang-bu...@googlegroups.com
Hi Nathan,

Since this kind of self-initialization is considered undefined
behavior, the simplest fix here is to just initialize to NULL. It's a
reasonable interpretation of what is currently written, and will at
least make the existing code more deterministic.

Thanks,
Steve
> --
> You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to clang-built-li...@googlegroups.com.
> To post to this group, send email to clang-bu...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20190523055258.GC22946%40kroah.com.
> For more options, visit https://groups.google.com/d/optout.

Nathan Chancellor

unread,
May 23, 2019, 12:20:21 PM5/23/19
to Cliff Whickman, Robin Holt, Arnd Bergmann, Greg Kroah-Hartman, linux-...@vger.kernel.org, clang-bu...@googlegroups.com, Nick Desaulniers, Nathan Chancellor, Stephen Hines
Clang warns:

drivers/misc/sgi-xp/xpc_partition.c:73:14: warning: variable 'buf' is
uninitialized when used within its own initialization [-Wuninitialized]
void *buf = buf;
~~~ ^~~
1 warning generated.

Initialize it to NULL, which is more deterministic.

Fixes: 279290294662 ("[IA64-SGI] cleanup the way XPC locates the reserved page")
Link: https://github.com/ClangBuiltLinux/linux/issues/466
Suggested-by: Stephen Hines <srh...@google.com>
Signed-off-by: Nathan Chancellor <natecha...@gmail.com>
---

Thanks Steve for the suggestion, don't know why that never crossed my
mind...

I tried to follow buf all the way down in get_partition_rsvd_page to see
if there would be any dereferences and I didn't see any but I could
have easily missed something.

drivers/misc/sgi-xp/xpc_partition.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/misc/sgi-xp/xpc_partition.c b/drivers/misc/sgi-xp/xpc_partition.c
index 3eba1c420cc0..782ce95d3f17 100644
--- a/drivers/misc/sgi-xp/xpc_partition.c
+++ b/drivers/misc/sgi-xp/xpc_partition.c
@@ -70,7 +70,7 @@ xpc_get_rsvd_page_pa(int nasid)
unsigned long rp_pa = nasid; /* seed with nasid */
size_t len = 0;
size_t buf_len = 0;
- void *buf = buf;
+ void *buf = NULL;
void *buf_base = NULL;
enum xp_retval (*get_partition_rsvd_page_pa)
(void *, u64 *, unsigned long *, size_t *) =
--
2.22.0.rc1

Stephen Hines

unread,
May 23, 2019, 12:46:52 PM5/23/19
to Nathan Chancellor, Cliff Whickman, Robin Holt, Arnd Bergmann, Greg Kroah-Hartman, linux-...@vger.kernel.org, clang-bu...@googlegroups.com, Nick Desaulniers
Looks good to me. :) Since I don't contribute to the Linux kernel
directly, I appreciate you picking this up.

Thanks,
Steve

Nick Desaulniers

unread,
May 23, 2019, 2:05:41 PM5/23/19
to Nathan Chancellor, Cliff Whickman, Robin Holt, Arnd Bergmann, Greg Kroah-Hartman, LKML, clang-built-linux, Stephen Hines, tony...@intel.com, r...@sgi.com, fengh...@intel.com
On Thu, May 23, 2019 at 9:20 AM Nathan Chancellor
<natecha...@gmail.com> wrote:
>
> Clang warns:
>
> drivers/misc/sgi-xp/xpc_partition.c:73:14: warning: variable 'buf' is
> uninitialized when used within its own initialization [-Wuninitialized]
> void *buf = buf;
> ~~~ ^~~
> 1 warning generated.
>
> Initialize it to NULL, which is more deterministic.
>
> Fixes: 279290294662 ("[IA64-SGI] cleanup the way XPC locates the reserved page")
> Link: https://github.com/ClangBuiltLinux/linux/issues/466
> Suggested-by: Stephen Hines <srh...@google.com>
> Signed-off-by: Nathan Chancellor <natecha...@gmail.com>

From https://github.com/ClangBuiltLinux/linux/issues/466#issuecomment-488781917
I tried to follow the rabbit hole, but eventually these void* get
converted to u64's and passed along to function that I have no idea
whether they handle the value `(u64)(void*)0` or not. Either way,
they definitely don't handle uninitialized values/UB.

I was going to cc Robin who's already cc'ed, but looks like this code
was last touched 7-10 years ago. + Tony and Fenghua for ia64 since
sn_partition_reserved_page_pa is defined in
arch/ia64/include/asm/sn/sn_sal.h.

In absence of consensus, I'll prefer NULL to uninitialized.
Reviewed-by: Nick Desaulniers <ndesau...@google.com>
Thanks Nathan for following up on this.

> ---
>
> Thanks Steve for the suggestion, don't know why that never crossed my
> mind...
>
> I tried to follow buf all the way down in get_partition_rsvd_page to see
> if there would be any dereferences and I didn't see any but I could
> have easily missed something.
>
> drivers/misc/sgi-xp/xpc_partition.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/misc/sgi-xp/xpc_partition.c b/drivers/misc/sgi-xp/xpc_partition.c
> index 3eba1c420cc0..782ce95d3f17 100644
> --- a/drivers/misc/sgi-xp/xpc_partition.c
> +++ b/drivers/misc/sgi-xp/xpc_partition.c
> @@ -70,7 +70,7 @@ xpc_get_rsvd_page_pa(int nasid)
> unsigned long rp_pa = nasid; /* seed with nasid */
> size_t len = 0;
> size_t buf_len = 0;
> - void *buf = buf;
> + void *buf = NULL;
> void *buf_base = NULL;
> enum xp_retval (*get_partition_rsvd_page_pa)
> (void *, u64 *, unsigned long *, size_t *) =
> --
> 2.22.0.rc1
>


--
Thanks,
~Nick Desaulniers

Arnd Bergmann

unread,
May 24, 2019, 3:41:05 AM5/24/19
to Nick Desaulniers, Nathan Chancellor, Cliff Whickman, Robin Holt, Greg Kroah-Hartman, LKML, clang-built-linux, Stephen Hines, Tony Luck, r...@sgi.com, Fenghua Yu
I also had to take a look, and I think I understand what's going on,
and interestingly, the code is correct, both before and after your patch.
It's described in this comment:

/*
* Returns the physical address of the partition's reserved page through
* an iterative number of calls.
*
* On first call, 'cookie' and 'len' should be set to 0, and 'addr'
* set to the nasid of the partition whose reserved page's address is
* being sought.
* On subsequent calls, pass the values, that were passed back on the
* previous call.
*
* While the return status equals SALRET_MORE_PASSES, keep calling
* this function after first copying 'len' bytes starting at 'addr'
* into 'buf'. Once the return status equals SALRET_OK, 'addr' will
* be the physical address of the partition's reserved page. If the
* return status equals neither of these, an error as occurred.
*/
static inline s64
sn_partition_reserved_page_pa(u64 buf, u64 *cookie, u64 *addr, u64 *len)

so *len is set to zero on the first call and tells the bios how many bytes
are accessible at 'buf', and it does get updated by the BIOS to tell
us how many bytes it needs, and then we allocate that and try again.

With that explanation added,

Reviewed-by: Arnd Bergmann <ar...@arndb.de>

Greg Kroah-Hartman

unread,
May 24, 2019, 12:00:19 PM5/24/19
to Arnd Bergmann, Nick Desaulniers, Nathan Chancellor, Cliff Whickman, Robin Holt, LKML, clang-built-linux, Stephen Hines, Tony Luck, r...@sgi.com, Fenghua Yu
Nathan, can you add this to the changelog comment and add the reviewed
by lines and resend it so I can queue it up?

thanks,

greg k-h

Nathan Chancellor

unread,
May 24, 2019, 12:15:44 PM5/24/19
to Greg Kroah-Hartman, Cliff Whickman, Robin Holt, linux-...@vger.kernel.org, clang-bu...@googlegroups.com, Nathan Chancellor, Stephen Hines, Arnd Bergmann, Nick Desaulniers
Clang warns:

drivers/misc/sgi-xp/xpc_partition.c:73:14: warning: variable 'buf' is
uninitialized when used within its own initialization [-Wuninitialized]
void *buf = buf;
~~~ ^~~
1 warning generated.

Arnd's explanation during review:

/*
* Returns the physical address of the partition's reserved page through
* an iterative number of calls.
*
* On first call, 'cookie' and 'len' should be set to 0, and 'addr'
* set to the nasid of the partition whose reserved page's address is
* being sought.
* On subsequent calls, pass the values, that were passed back on the
* previous call.
*
* While the return status equals SALRET_MORE_PASSES, keep calling
* this function after first copying 'len' bytes starting at 'addr'
* into 'buf'. Once the return status equals SALRET_OK, 'addr' will
* be the physical address of the partition's reserved page. If the
* return status equals neither of these, an error as occurred.
*/
static inline s64
sn_partition_reserved_page_pa(u64 buf, u64 *cookie, u64 *addr, u64 *len)

so *len is set to zero on the first call and tells the bios how many
bytes are accessible at 'buf', and it does get updated by the BIOS to
tell us how many bytes it needs, and then we allocate that and try again.

Fixes: 279290294662 ("[IA64-SGI] cleanup the way XPC locates the reserved page")
Link: https://github.com/ClangBuiltLinux/linux/issues/466
Suggested-by: Stephen Hines <srh...@google.com>
Reviewed-by: Arnd Bergmann <ar...@arndb.de>
Reviewed-by: Nick Desaulniers <ndesau...@google.com>
Signed-off-by: Nathan Chancellor <natecha...@gmail.com>
---
Reply all
Reply to author
Forward
0 new messages