yes, it's a other good way, it's the minimum modify to fix it.

but i think it's not gcc reason maybe. i update my gcc to 14.2.0 version, when lack of linux/usr/include/linux/vhost_types.h file, the "__user redefine" -Werror will disappear。





----

 




主 题:Re: [PATCH] tools: fixed compile tools/virtio error "__user" redefined [-Werror]
日 期:2025-01-13 10:34
发件人:Hao Ge
收件人:Greg KH;王宇锋;

Hi Greg and Yufeng


On 2025/1/9 17:14, Greg KH wrote:
> On Thu, Jan 09, 2025 at 04:43:41PM +0800, Yufeng Wang wrote:
>> we use -Werror now, and warnings break the build so let's fixed it.
>>
>> from virtio_test.c:17:
>> ./linux/../../../include/linux/compiler_types.h:48: error: "__user" redefined [-Werror]
>> 48 | # define __user BTF_TYPE_TAG(user)
>> |
>> In file included from ../../usr/include/linux/stat.h:5,
>> from /usr/include/x86_64-linux-gnu/bits/statx.h:31,
>> from /usr/include/x86_64-linux-gnu/sys/stat.h:465,
>> from virtio_test.c:12:
>> ../include/linux/types.h:56: note: this is the location of the previous definition
>> 56 | #define __user
>>
>> Cc: stable@vger.kernel.org
>>
>> Signed-off-by: Yufeng Wang
>> ---
>> include/linux/compiler_types.h | 1 +
>> 1 file changed, 1 insertion(+)
>>
>> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
>> index 5d6544545658..3316e56140d6 100644
>> --- a/include/linux/compiler_types.h
>> +++ b/include/linux/compiler_types.h
>> @@ -54,6 +54,7 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
>> # ifdef STRUCTLEAK_PLUGIN
>> # define __user __attribute__((user))
>> # else
>> +# undef __user
>> # define __user BTF_TYPE_TAG(user)
>> # endif
>> # define __iomem
>> --
>> 2.34.1
> What commit does this fix? Why is this suddenly showing up now?
>
> thanks,
>
> greg k-h


This may be an issue caused by an upgrade in the GCC version.

Using GCC version 9.3.0, it can happily pass the build process.

However, when using GCC version 12.3.1, issues arise.

The initial build error stack is as follows:

cc -g -O2 -Werror -Wno-maybe-uninitialized -Wall -I. -I../include/ -I
.../../usr/include/ -Wno-pointer-sign -fno-strict-overflow
-fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -include
.../../include/linux/kconfig.h -mfunction-return=thunk
-fcf-protection=none -mindirect-branch-register -pthread   -c -o
virtio_test.o virtio_test.c
In file included from ./linux/compiler.h:5,
                 from ./linux/kernel.h:12,
                 from ./linux/scatterlist.h:4,
                 from ./linux/virtio.h:4,
                 from ./linux/virtio_config.h:5,
                 from ../../usr/include/linux/vhost_types.h:14,
                 from ../../usr/include/linux/vhost.h:14,
                 from virtio_test.c:17:
../linux/../../../include/linux/compiler_types.h:57: error: "__user"
redefined [-Werror]

   57 | #  define __user        BTF_TYPE_TAG(user)
      |
In file included from ../../usr/include/linux/stat.h:5,
                 from /usr/include/bits/statx.h:31,
                 from /usr/include/sys/stat.h:465,
                 from virtio_test.c:12:

.../include/linux/types.h:56: note: this is the location of the previous
definition

   56 | #define __user

It should have first encountered the following #ifndef

https://elixir.bootlin.com/linux/v6.12.5/source/tools/include/linux/types..h#L52

So I think the  modification should be as follows(Adjust the position of
the header file),

What do you think?


diff --git a/tools/virtio/linux/virtio_config.h
b/tools/virtio/linux/virtio_config.h
index 42a564f22f2d..3d32211d2d23 100644
--- a/tools/virtio/linux/virtio_config.h
+++ b/tools/virtio/linux/virtio_config.h
@@ -1,8 +1,8 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef LINUX_VIRTIO_CONFIG_H
 #define LINUX_VIRTIO_CONFIG_H
-#include
 #include
+#include
 #include

 struct virtio_config_ops {
diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c
index 028f54e6854a..8cbe632e98b0 100644
--- a/tools/virtio/virtio_test.c
+++ b/tools/virtio/virtio_test.c
@@ -9,13 +9,13 @@
 #include
 #include
 #include
+#include
 #include
 #include
 #include
 #include
 #include
 #include
-#include
 #include
 #include "../../drivers/vhost/test.h"

Thanks

Best Regards

Hao