Re: drivers/ata/ahci_st.c:229:34: warning: unused variable 'st_ahci_match'

From: Arnd Bergmann
Date: Thu Oct 13 2022 - 03:33:57 EST


On Thu, Oct 13, 2022, at 8:13 AM, Damien Le Moal wrote:
> On 10/12/22 15:37, kernel test robot wrote:

> I am at a loss with this one... There are plenty of patterns similar to
> drivers/ata/ahci_st.c doing something like:
>
> static const struct of_device_id st_ahci_match[] = {
>
> { .compatible = "st,ahci", },
>
> { /* sentinel */ }
>
> };
>
> MODULE_DEVICE_TABLE(of, st_ahci_match);
>
> For instance, in drivers/pwm/pwm-sti.c, we have:
>
>
> And countless others like this for STI and other arch too.
>
> So if CONFIG_MODULE is not enabled, how come we are not submerged with
> warnings about unused variables ? Is mips arch special in this regard ?
> Or am I missing something ?

It has nothing to do with MIPS, the problem is

.of_match_table = of_match_ptr(st_ahci_match),

The 'of_match_ptr()' sets the pointer to NULL when CONFIG_OF is
disabled, which avoids a build failure when st_ahci_match[]
itself is in an #ifdef.

In this driver, there is no #ifdef around st_ahci_match[], so we
simply want

.of_match_table = st_ahci_match,

The thing with the MODULE_DEVICE_TABLE() is that it would
create another reference if CONFIG_MODULE is enabled, but not
for a built-in driver, so you only get this type of warning
for randconfig builds that have the driver built-in and OF
disabled.

It's a common mistake, and we should probably remove most
of the of_match_ptr() references as you rarely have drivers
that optionally use OF support but benefit from compiling
that support out on kernels without OF.

Arnd