All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nathan Chancellor <nathan@kernel.org>
To: Rob Herring <robh+dt@kernel.org>, Frank Rowand <frowand.list@gmail.com>
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Matt Flax <flatmax@flatmax.com>,
	Nathan Chancellor <nathan@kernel.org>
Subject: [PATCH] of: Define of_match_ptr() with PTR_IF() to avoid unused variable warnings
Date: Thu, 13 Oct 2022 12:51:53 -0700	[thread overview]
Message-ID: <20221013195153.2767632-1-nathan@kernel.org> (raw)

When CONFIG_OF is disabled and a driver is built in, it is possible for
an of_device_id structure to be unused, which results in a build warning
with W=1 due to -Wunused-const-variable.

For example, in sound/soc/codecs/src4xxx-i2c.c:

  static const struct of_device_id src4xxx_of_match[] = {
      { .compatible = "ti,src4392", },
      { }
  };
  MODULE_DEVICE_TABLE(of, src4xxx_of_match);

  static struct i2c_driver src4xxx_i2c_driver = {
      .driver = {
          .name = "src4xxx",
          .of_match_table = of_match_ptr(src4xxx_of_match),
      },
      .probe = src4xxx_i2c_probe,
      .id_table = src4xxx_i2c_ids,
  };
  module_i2c_driver(src4xxx_i2c_driver);

A configuration with CONFIG_OF=n and CONFIG_SND_SOC_SRC4XXX_I2C=y
produces

  sound/soc/codecs/src4xxx-i2c.c:28:34: warning: unused variable 'src4xxx_of_match' [-Wunused-const-variable]
  static const struct of_device_id src4xxx_of_match[] = {
                                   ^

because of_patch_ptr() expands to NULL when CONFIG_OF=n and
MODULE_DEVICE_TABLE() expands to nothing when MODULE is not set (i.e.,
when the driver is built into the kernel).

This is a similar situation to the power management suspend and resume
functions, which may or may not be used depending on whether or not
CONFIG_PM is set. The solution for a long time was to make the functions
as __maybe_unused but commit c06ef740d401 ("PM: core: Redefine pm_ptr()
macro") adopted a new solution involving IS_ENABLED() and PTR_IF(),
which allows the compiler to see the referenced object at least once in
the file to clear up the unused warning, while simultaneously allowing
the compiler to eliminate unused code in the final object file.

Do the same thing with of_match_ptr() so that unused device IDs do not
cause warnings. This would have prevented several added #ifdef's, such
as the one added by commit 527a7f52529f ("perf/smmuv3: Fix unused
variable warning when CONFIG_OF=n").

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 include/linux/of.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/of.h b/include/linux/of.h
index 766d002bddb9..f2a8d411a0f2 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -14,6 +14,7 @@
 #include <linux/types.h>
 #include <linux/bitops.h>
 #include <linux/errno.h>
+#include <linux/kconfig.h>
 #include <linux/kobject.h>
 #include <linux/mod_devicetable.h>
 #include <linux/spinlock.h>
@@ -405,8 +406,6 @@ extern int of_update_property(struct device_node *np, struct property *newprop);
 extern int of_attach_node(struct device_node *);
 extern int of_detach_node(struct device_node *);
 
-#define of_match_ptr(_ptr)	(_ptr)
-
 /*
  * struct property *prop;
  * const __be32 *p;
@@ -843,10 +842,11 @@ static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
 	return PHYS_ADDR_MAX;
 }
 
-#define of_match_ptr(_ptr)	NULL
 #define of_match_node(_matches, _node)	NULL
 #endif /* CONFIG_OF */
 
+#define of_match_ptr(_ptr)	PTR_IF(IS_ENABLED(CONFIG_OF), (_ptr))
+
 /* Default string compare functions, Allow arch asm/prom.h to override */
 #if !defined(of_compat_cmp)
 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))

base-commit: 4fe89d07dcc2804c8b562f6c7896a45643d34b2f
-- 
2.37.3


             reply	other threads:[~2022-10-13 19:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13 19:51 Nathan Chancellor [this message]
2022-10-14  2:27 ` [PATCH] of: Define of_match_ptr() with PTR_IF() to avoid unused variable warnings kernel test robot
2022-10-14  2:27 ` kernel test robot
2022-10-14  2:37 ` kernel test robot
2022-10-14  8:10 ` Arnd Bergmann
2022-10-14 16:47   ` Nathan Chancellor

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=20221013195153.2767632-1-nathan@kernel.org \
    --to=nathan@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=flatmax@flatmax.com \
    --cc=frowand.list@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.