Skip to contentSkip to conversation

gpiolib: move all includes to the top of gpio/consumer.h

PATCH Applied Bartosz Golaszewski 3 participants 7 messages Feb 17, 2025 – Feb 19, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 gpiolib: move all includes to the top of gpio/consumer.h

From: Bartosz Golaszewski <[email protected]>

We have several conditional includes depending on !CONFIG_GPIOLIB. This is supposed to reduce compilation time with CONFIG_GPIOLIB=y but in practice there's no difference on modern machines. It makes adding new stubs that depend on more than just GPIOLIB harder so move them all to the top, unduplicate them and replace asm/ with preferred linux/ alternatives.

---
Changes in v4:
- rebased after fixing conflicts

 include/linux/gpio/consumer.h | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
include/linux/gpio/consumer.h +3 -9
index 5cbd4afd7862..0dc49b5fca5c 100644
@@ -3,7 +3,10 @@
 #define __LINUX_GPIO_CONSUMER_H
 
 #include <linux/bits.h>
+#include <linux/bug.h>
 #include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
 #include <linux/types.h>
 
 struct acpi_device;
@@ -184,11 +187,6 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 
 #else /* CONFIG_GPIOLIB */
 
-#include <linux/err.h>
-#include <linux/kernel.h>
-
-#include <asm/bug.h>
-
 static inline int gpiod_count(struct device *dev, const char *con_id)
 {
 	return 0;
@@ -609,8 +607,6 @@ int devm_acpi_dev_add_driver_gpios(struct device *dev,
 
 #else  /* CONFIG_GPIOLIB && CONFIG_ACPI */
 
-#include <linux/err.h>
-
 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
 			      const struct acpi_gpio_mapping *gpios)
 {
@@ -636,8 +632,6 @@ void gpiod_unexport(struct gpio_desc *desc);
 
 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
 
-#include <asm/errno.h>
-
 static inline int gpiod_export(struct gpio_desc *desc,
 			       bool direction_may_change)
 {
Signature · 1 line
2.45.2
#1 lore raw

PATCH v4 2/2 gpiolib: don't build HTE code with CONFIG_HTE disabled

From: Bartosz Golaszewski <[email protected]>

Hardware timestamping is only used on tegra186 platforms but we include the code and export the symbols everywhere. Shrink the binary a bit by compiling the relevant functions conditionally.

---
Changes in v4:
- rebased after fixing conflicts

 drivers/gpio/gpiolib.c        |  2 ++
 include/linux/gpio/consumer.h | 36 +++++++++++++++++++++--------------
 2 files changed, 24 insertions(+), 14 deletions(-)
drivers/gpio/gpiolib.c +2 -0
index f261f7893f85..65ca749a1078 100644
@@ -2906,6 +2906,7 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
 	return 0;
 }
 
+#if IS_ENABLED(CONFIG_HTE)
 /**
  * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
  *
@@ -2971,6 +2972,7 @@ int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
 	return ret;
 }
 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
+#endif /* CONFIG_HTE */
 
 /**
  * gpiod_set_config - sets @config for a GPIO
include/linux/gpio/consumer.h +22 -14
index 0dc49b5fca5c..0b2b56199c36 100644
@@ -114,8 +114,6 @@ int gpiod_get_direction(struct gpio_desc *desc);
 int gpiod_direction_input(struct gpio_desc *desc);
 int gpiod_direction_output(struct gpio_desc *desc, int value);
 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
-int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
-int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
 
 /* Value get/set from non-sleeping context */
 int gpiod_get_value(const struct gpio_desc *desc);
@@ -347,18 +345,6 @@ static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
 	WARN_ON(desc);
 	return -ENOSYS;
 }
-static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
-					       unsigned long flags)
-{
-	WARN_ON(desc);
-	return -ENOSYS;
-}
-static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
-						unsigned long flags)
-{
-	WARN_ON(desc);
-	return -ENOSYS;
-}
 static inline int gpiod_get_value(const struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
@@ -559,6 +545,28 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 
 #endif /* CONFIG_GPIOLIB */
 
+#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_HTE)
+int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
+int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
+#else
+static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
+					       unsigned long flags)
+{
+	if (!IS_ENABLED(CONFIG_GPIOLIB))
+		WARN_ON(desc);
+
+	return -ENOSYS;
+}
+static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
+						unsigned long flags)
+{
+	if (!IS_ENABLED(CONFIG_GPIOLIB))
+		WARN_ON(desc);
+
+	return -ENOSYS;
+}
+#endif /* CONFIG_GPIOLIB && CONFIG_HTE */
+
 static inline
 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
 					struct fwnode_handle *fwnode,
Signature · 1 line
2.45.2
maintainer Acked-by on v4 2/2
#2 lore raw

Re: [PATCH v4 2/2] gpiolib: don't build HTE code with CONFIG_HTE disabled

Quoted 87 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Hardware timestamping is only used on tegra186 platforms but we include the code and export the symbols everywhere. Shrink the binary a bit by compiling the relevant functions conditionally.

---
Changes in v4:
- rebased after fixing conflicts

 drivers/gpio/gpiolib.c        |  2 ++
 include/linux/gpio/consumer.h | 36 +++++++++++++++++++++--------------
 2 files changed, 24 insertions(+), 14 deletions(-)
drivers/gpio/gpiolib.c +2 -0
index f261f7893f85..65ca749a1078 100644
@@ -2906,6 +2906,7 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
 	return 0;
 }
 
+#if IS_ENABLED(CONFIG_HTE)
 /**
  * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds.
  *
@@ -2971,6 +2972,7 @@ int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags)
 	return ret;
 }
 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns);
+#endif /* CONFIG_HTE */
 
 /**
  * gpiod_set_config - sets @config for a GPIO
include/linux/gpio/consumer.h +15 -14
index 0dc49b5fca5c..0b2b56199c36 100644
@@ -114,8 +114,6 @@ int gpiod_get_direction(struct gpio_desc *desc);
 int gpiod_direction_input(struct gpio_desc *desc);
 int gpiod_direction_output(struct gpio_desc *desc, int value);
 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
-int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
-int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
 
 /* Value get/set from non-sleeping context */
 int gpiod_get_value(const struct gpio_desc *desc);
@@ -347,18 +345,6 @@ static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
 	WARN_ON(desc);
 	return -ENOSYS;
 }
-static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
-					       unsigned long flags)
-{
-	WARN_ON(desc);
-	return -ENOSYS;
-}
-static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
-						unsigned long flags)
-{
-	WARN_ON(desc);
-	return -ENOSYS;
-}
 static inline int gpiod_get_value(const struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
@@ -559,6 +545,28 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
 
 #endif /* CONFIG_GPIOLIB */
 
+#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_HTE)
+int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
+int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
+#else
+static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
+					       unsigned long flags)
+{
+	if (!IS_ENABLED(CONFIG_GPIOLIB))
+		WARN_ON(desc);
+
+	return -ENOSYS;
+}
+static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
+						unsigned long flags)
+{

include/linux/gpio/consumer.h struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, · line 572

Quoted 10 lines
include/linux/gpio/consumer.h +7 -0
+	if (!IS_ENABLED(CONFIG_GPIOLIB))
+		WARN_ON(desc);
+
+	return -ENOSYS;
+}
+#endif /* CONFIG_GPIOLIB && CONFIG_HTE */
+
 static inline
 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
 					struct fwnode_handle *fwnode,
author Applied
#3 lore raw
In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 8 lines · Bartosz Golaszewski

We have several conditional includes depending on !CONFIG_GPIOLIB. This is supposed to reduce compilation time with CONFIG_GPIOLIB=y but in practice there's no difference on modern machines. It makes adding new stubs that depend on more than just GPIOLIB harder so move them all to the top, unduplicate them and replace asm/ with preferred linux/ alternatives.

[...]

Applied, thanks!

[1/2] gpiolib: move all includes to the top of gpio/consumer.h

      commit: dea69f2d1cc8d9ecdc72ba350d10a1e71940ef18
[2/2] gpiolib: don't build HTE code with CONFIG_HTE disabled
      commit: 63cdf6241ac7edd94cb4cd9a8f1ba2c3c61ed219

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#4 lore raw
In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We have several conditional includes depending on !CONFIG_GPIOLIB. This is supposed to reduce compilation time with CONFIG_GPIOLIB=y but in practice there's no difference on modern machines.

It's not about modern machines. If every maintainer will think this way, we end up in the complete and utter dead end with the headers.

I believe you at least had read the cover letter for the infamous Ingo's series about headers and how it speeds up the build (in some cases up to 70% on as you said "modern machines").

Quoted 3 lines

It makes adding new stubs that depend on more than just GPIOLIB harder so move them all to the top, unduplicate them and replace asm/ with preferred linux/ alternatives.

NAK.

This makes dependency hell things much worse and this is a step back on the untangling the current situation along with the slowing down the speed of the build. Please. consider to revert or discard this patch.

...

Quoted 2 lines

#include <linux/bits.h>
+#include <linux/bug.h>

Okay to replace, but not okay to move.

Quoted 2 lines

#include <linux/err.h>
+#include <linux/errno.h>

Please, double check that it uses error codes from it, otherwise err.h includes asm/errno.h with basic codes already.

Quoted 1 line

+#include <linux/kernel.h>

This is definitely no. Please, read what's written in the top of that file and here is just a proxy for should come in the future a kind of might_sleep.h. Do not move this one at all, please.

Quoted 1 line

#include <linux/types.h>

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 34 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We have several conditional includes depending on !CONFIG_GPIOLIB. This is supposed to reduce compilation time with CONFIG_GPIOLIB=y but in practice there's no difference on modern machines.

It's not about modern machines. If every maintainer will think this way, we end up in the complete and utter dead end with the headers.

I believe you at least had read the cover letter for the infamous Ingo's series about headers and how it speeds up the build (in some cases up to 70% on as you said "modern machines").

Quoted 3 lines

It makes adding new stubs that depend on more than just GPIOLIB harder so move them all to the top, unduplicate them and replace asm/ with preferred linux/ alternatives.

NAK.

This makes dependency hell things much worse and this is a step back on the untangling the current situation along with the slowing down the speed of the build. Please. consider to revert or discard this patch.

...

Quoted 2 lines

#include <linux/bits.h>
+#include <linux/bug.h>

Okay to replace, but not okay to move.

Quoted 2 lines

#include <linux/err.h>
+#include <linux/errno.h>

Quoted 11 lines

Please, double check that it uses error codes from it, otherwise err.h includes asm/errno.h with basic codes already.

Quoted 1 line

+#include <linux/kernel.h>

This is definitely no. Please, read what's written in the top of that file and here is just a proxy for should come in the future a kind of might_sleep.h. Do not move this one at all, please.

Quoted 1 line

#include <linux/types.h>

Fair enough. Does this look right to you?

include/linux/gpio/consumer.h +0 -4
index 0b2b56199c36..38e313fd0e9a 100644
@@ -3,10 +3,6 @@
 #define __LINUX_GPIO_CONSUMER_H
 
 #include <linux/bits.h>
-#include <linux/bug.h>
-#include <linux/err.h>
-#include <linux/errno.h>
-#include <linux/kernel.h>
 #include <linux/types.h>
 
 struct acpi_device;
@@ -185,6 +181,10 @@ struct gpio_desc

*devm_fwnode_gpiod_get_index(struct device *dev,

#else /* CONFIG_GPIOLIB */

+#include <linux/bug.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+
static inline int gpiod_count(struct device *dev, const char *con_id)

 {
	return 0;

@@ -549,6 +549,10 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); #else
+
+#include <linux/bug.h>
+#include <linux/err.h>
+
static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
unsigned long flags)
{
@@ -615,6 +619,8 @@ int devm_acpi_dev_add_driver_gpios(struct device *dev,

#else /* CONFIG_GPIOLIB && CONFIG_ACPI */

+#include <linux/err.h>
+
static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
const struct acpi_gpio_mapping *gpios)
{
@@ -640,6 +646,8 @@ void gpiod_unexport(struct gpio_desc *desc);

#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */

+#include <linux/err.h>
+
static inline int gpiod_export(struct gpio_desc *desc,
bool direction_may_change)
{

Bart

#6 lore raw
Quoted 39 lines · Bartosz Golaszewski
Quoted 37 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We have several conditional includes depending on !CONFIG_GPIOLIB. This is supposed to reduce compilation time with CONFIG_GPIOLIB=y but in practice there's no difference on modern machines.

It's not about modern machines. If every maintainer will think this way, we end up in the complete and utter dead end with the headers.

I believe you at least had read the cover letter for the infamous Ingo's series about headers and how it speeds up the build (in some cases up to 70% on as you said "modern machines").

Quoted 3 lines

It makes adding new stubs that depend on more than just GPIOLIB harder so move them all to the top, unduplicate them and replace asm/ with preferred linux/ alternatives.

NAK.

This makes dependency hell things much worse and this is a step back on the untangling the current situation along with the slowing down the speed of the build. Please. consider to revert or discard this patch.

...

Quoted 2 lines

#include <linux/bits.h>
+#include <linux/bug.h>

Okay to replace, but not okay to move.

Quoted 2 lines

#include <linux/err.h>
+#include <linux/errno.h>

Please, double check that it uses error codes from it, otherwise err.h includes asm/errno.h with basic codes already.

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

+#include <linux/kernel.h>

This is definitely no. Please, read what's written in the top of that file and here is just a proxy for should come in the future a kind of might_sleep.h. Do not move this one at all, please.

Quoted 1 line

#include <linux/types.h>

Fair enough. Does this look right to you?

For kernel.h definitely, for err.h you proved your point (but which was unclear to me as the repetitions are already being in a number).

For bug.h looks also good. But I prefer to use asm/bug.h as it's the one that provides the APIs. Note, the reference to the recommended linux/* headers rather than asm/* applies to the C code or custom headers which are not under include/*. The latter should be optimized to what it uses exactly.

So, summarizing the above I would return kernel.h to where it belongs and move back to asm/bug.h.

Signature · 2 lines
With Best Regards,
Andy Shevchenko