linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning
@ 2023-08-10  8:50 Krzysztof Kozlowski
  2023-08-10 15:44 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 5+ messages in thread
From: Krzysztof Kozlowski @ 2023-08-10  8:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team,
	linux-serial, linux-kernel, linux-arm-kernel
  Cc: Krzysztof Kozlowski, Andi Shyti

`devtype` is enum, thus cast of pointer on 64-bit compile test with W=1
causes:

  mxs-auart.c:1598:15: error: cast to smaller integer type 'enum mxs_auart_type' from 'const void *' [-Werror,-Wvoid-pointer-to-enum-cast]

Cc: Andi Shyti <andi.shyti@kernel.org>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
 drivers/tty/serial/mxs-auart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
index 8eeecf8ad359..a9b32722b049 100644
--- a/drivers/tty/serial/mxs-auart.c
+++ b/drivers/tty/serial/mxs-auart.c
@@ -1595,7 +1595,7 @@ static int mxs_auart_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
+	s->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev);
 
 	ret = mxs_get_clks(s, pdev);
 	if (ret)
-- 
2.34.1


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

* Re: [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning
  2023-08-10  8:50 [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning Krzysztof Kozlowski
@ 2023-08-10 15:44 ` Greg Kroah-Hartman
  2023-08-14  6:58   ` Krzysztof Kozlowski
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2023-08-10 15:44 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Jiri Slaby, Shawn Guo, Sascha Hauer, Pengutronix Kernel Team,
	Fabio Estevam, NXP Linux Team, linux-serial, linux-kernel,
	linux-arm-kernel, Andi Shyti

On Thu, Aug 10, 2023 at 10:50:42AM +0200, Krzysztof Kozlowski wrote:
> `devtype` is enum, thus cast of pointer on 64-bit compile test with W=1
> causes:
> 
>   mxs-auart.c:1598:15: error: cast to smaller integer type 'enum mxs_auart_type' from 'const void *' [-Werror,-Wvoid-pointer-to-enum-cast]
> 
> Cc: Andi Shyti <andi.shyti@kernel.org>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
>  drivers/tty/serial/mxs-auart.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
> index 8eeecf8ad359..a9b32722b049 100644
> --- a/drivers/tty/serial/mxs-auart.c
> +++ b/drivers/tty/serial/mxs-auart.c
> @@ -1595,7 +1595,7 @@ static int mxs_auart_probe(struct platform_device *pdev)
>  		return -EINVAL;
>  	}
>  
> -	s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
> +	s->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev);

This feels like a compiler issue as devtype is a enum mxs_auart_type
variable, so the cast shoudl be correct.

And if not, unitptr_t isn't a valid kernel type, so that's not a good
solution either.  Worst case, it's how big a pointer is, which is not
going to be what an enum is if you have a sane compiler :(

thanks,

greg k-h

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

* Re: [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning
  2023-08-10 15:44 ` Greg Kroah-Hartman
@ 2023-08-14  6:58   ` Krzysztof Kozlowski
  2023-08-14 16:04     ` Greg Kroah-Hartman
  2023-08-14 16:04     ` Nathan Chancellor
  0 siblings, 2 replies; 5+ messages in thread
From: Krzysztof Kozlowski @ 2023-08-14  6:58 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Nathan Chancellor
  Cc: Jiri Slaby, Shawn Guo, Sascha Hauer, Pengutronix Kernel Team,
	Fabio Estevam, NXP Linux Team, linux-serial, linux-kernel,
	linux-arm-kernel, Andi Shyti

On 10/08/2023 17:44, Greg Kroah-Hartman wrote:
> On Thu, Aug 10, 2023 at 10:50:42AM +0200, Krzysztof Kozlowski wrote:
>> `devtype` is enum, thus cast of pointer on 64-bit compile test with W=1
>> causes:
>>
>>   mxs-auart.c:1598:15: error: cast to smaller integer type 'enum mxs_auart_type' from 'const void *' [-Werror,-Wvoid-pointer-to-enum-cast]
>>
>> Cc: Andi Shyti <andi.shyti@kernel.org>
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
>> ---
>>  drivers/tty/serial/mxs-auart.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
>> index 8eeecf8ad359..a9b32722b049 100644
>> --- a/drivers/tty/serial/mxs-auart.c
>> +++ b/drivers/tty/serial/mxs-auart.c
>> @@ -1595,7 +1595,7 @@ static int mxs_auart_probe(struct platform_device *pdev)
>>  		return -EINVAL;
>>  	}
>>  
>> -	s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
>> +	s->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev);
> 
> This feels like a compiler issue as devtype is a enum mxs_auart_type
> variable, so the cast shoudl be correct.

While the cast is obviously safe here, the warning in general is
reasonable - people were make too many mistakes by assuming pointers are
integers...

Just for the record (not saying that others doing is proof of correctness):

https://lore.kernel.org/lkml/20230809-cbl-1903-v1-1-df9d66a3ba3e@google.com/T/

But maybe Nathan can share his thoughts whether we should just disable
this warning for kernel?

> 
> And if not, unitptr_t isn't a valid kernel type, so that's not a good

It is in include/linux/types.h, so do you mean that it is not
recommended for in-kernel usage? I can go with kernel_ulong_t - which is
a kernel type - if the cast is agreed.

> solution either.  Worst case, it's how big a pointer is, which is not
> going to be what an enum is if you have a sane compiler :(


Best regards,
Krzysztof


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

* Re: [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning
  2023-08-14  6:58   ` Krzysztof Kozlowski
@ 2023-08-14 16:04     ` Greg Kroah-Hartman
  2023-08-14 16:04     ` Nathan Chancellor
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2023-08-14 16:04 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Nathan Chancellor, Jiri Slaby, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team,
	linux-serial, linux-kernel, linux-arm-kernel, Andi Shyti

On Mon, Aug 14, 2023 at 08:58:34AM +0200, Krzysztof Kozlowski wrote:
> On 10/08/2023 17:44, Greg Kroah-Hartman wrote:
> > On Thu, Aug 10, 2023 at 10:50:42AM +0200, Krzysztof Kozlowski wrote:
> >> `devtype` is enum, thus cast of pointer on 64-bit compile test with W=1
> >> causes:
> >>
> >>   mxs-auart.c:1598:15: error: cast to smaller integer type 'enum mxs_auart_type' from 'const void *' [-Werror,-Wvoid-pointer-to-enum-cast]
> >>
> >> Cc: Andi Shyti <andi.shyti@kernel.org>
> >> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> >> ---
> >>  drivers/tty/serial/mxs-auart.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
> >> index 8eeecf8ad359..a9b32722b049 100644
> >> --- a/drivers/tty/serial/mxs-auart.c
> >> +++ b/drivers/tty/serial/mxs-auart.c
> >> @@ -1595,7 +1595,7 @@ static int mxs_auart_probe(struct platform_device *pdev)
> >>  		return -EINVAL;
> >>  	}
> >>  
> >> -	s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
> >> +	s->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev);
> > 
> > This feels like a compiler issue as devtype is a enum mxs_auart_type
> > variable, so the cast shoudl be correct.
> 
> While the cast is obviously safe here, the warning in general is
> reasonable - people were make too many mistakes by assuming pointers are
> integers...

But this isn't a pointer, it's an enumerated type thrown into a void *,
so cast it properly (void * == unsigned long).

> Just for the record (not saying that others doing is proof of correctness):
> 
> https://lore.kernel.org/lkml/20230809-cbl-1903-v1-1-df9d66a3ba3e@google.com/T/
> 
> But maybe Nathan can share his thoughts whether we should just disable
> this warning for kernel?

I thought that Linus had some objection to the compilers doing something
foolish here as well, but I can't find it in my archives at the moment
(am traveling).

> > And if not, unitptr_t isn't a valid kernel type, so that's not a good
> 
> It is in include/linux/types.h, so do you mean that it is not
> recommended for in-kernel usage? I can go with kernel_ulong_t - which is
> a kernel type - if the cast is agreed.

not recommended for in-kernel usage.

thanks,

greg k-h

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

* Re: [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning
  2023-08-14  6:58   ` Krzysztof Kozlowski
  2023-08-14 16:04     ` Greg Kroah-Hartman
@ 2023-08-14 16:04     ` Nathan Chancellor
  1 sibling, 0 replies; 5+ messages in thread
From: Nathan Chancellor @ 2023-08-14 16:04 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: Greg Kroah-Hartman, Jiri Slaby, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team,
	linux-serial, linux-kernel, linux-arm-kernel, Andi Shyti

On Mon, Aug 14, 2023 at 08:58:34AM +0200, Krzysztof Kozlowski wrote:
> On 10/08/2023 17:44, Greg Kroah-Hartman wrote:
> > On Thu, Aug 10, 2023 at 10:50:42AM +0200, Krzysztof Kozlowski wrote:
> >> `devtype` is enum, thus cast of pointer on 64-bit compile test with W=1
> >> causes:
> >>
> >>   mxs-auart.c:1598:15: error: cast to smaller integer type 'enum mxs_auart_type' from 'const void *' [-Werror,-Wvoid-pointer-to-enum-cast]
> >>
> >> Cc: Andi Shyti <andi.shyti@kernel.org>
> >> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> >> ---
> >>  drivers/tty/serial/mxs-auart.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/tty/serial/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
> >> index 8eeecf8ad359..a9b32722b049 100644
> >> --- a/drivers/tty/serial/mxs-auart.c
> >> +++ b/drivers/tty/serial/mxs-auart.c
> >> @@ -1595,7 +1595,7 @@ static int mxs_auart_probe(struct platform_device *pdev)
> >>  		return -EINVAL;
> >>  	}
> >>  
> >> -	s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
> >> +	s->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev);
> > 
> > This feels like a compiler issue as devtype is a enum mxs_auart_type
> > variable, so the cast shoudl be correct.
> 
> While the cast is obviously safe here, the warning in general is
> reasonable - people were make too many mistakes by assuming pointers are
> integers...

Yeah, this is just a variant of -Wvoid-pointer-to-int-cast, which itself
is a variant of -Wpointer-to-int-cast. When clang implemented that
warning, it chose to warn for both (int) and (enum ...) casts, whereas
GCC just warns for int casts: https://godbolt.org/z/G8xsYqbzP

I brought up that difference to the clang folks and they justified it
well I think: https://reviews.llvm.org/D72231#1881784

> Just for the record (not saying that others doing is proof of correctness):
> 
> https://lore.kernel.org/lkml/20230809-cbl-1903-v1-1-df9d66a3ba3e@google.com/T/
> 
> But maybe Nathan can share his thoughts whether we should just disable
> this warning for kernel?

Technically, it is disabled for a regular kernel build but it shows up
with W=1:

https://git.kernel.org/linus/82f2bc2fcc0160d6f82dd1ac64518ae0a4dd183f

That was not done because the warning was wrong but because we were working
hard to drive the warning count down to zero and did not want to have to
solve the 90+ instances that were around at that time. Now, with the
patches that you have sent and the ones that Justin is working on, we
are much closer:

https://github.com/ClangBuiltLinux/linux/issues/1910#issuecomment-1675563993

> > And if not, unitptr_t isn't a valid kernel type, so that's not a good
> 
> It is in include/linux/types.h, so do you mean that it is not
> recommended for in-kernel usage? I can go with kernel_ulong_t - which is
> a kernel type - if the cast is agreed.

uintptr_t is used quite a bit in the kernel already, so I don't really
get the "not a valid kernel type" argument. I have seen other folks
prefer 'unsigned long' since Linux assumes it has the same size as a
pointer but that is what uintptr_t is supposed to be...

Cheers,
Nathan

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

end of thread, other threads:[~2023-08-14 16:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-10  8:50 [PATCH] serial: mxs-uart: fix Wvoid-pointer-to-enum-cast warning Krzysztof Kozlowski
2023-08-10 15:44 ` Greg Kroah-Hartman
2023-08-14  6:58   ` Krzysztof Kozlowski
2023-08-14 16:04     ` Greg Kroah-Hartman
2023-08-14 16:04     ` Nathan Chancellor

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