Skip to contentSkip to conversation

gpiolib: notify user-space about line state changes triggered by kernel

PATCH Superseded Bartosz Golaszewski 2 participants 3 messages Aug 18, 2023 – Aug 24, 2023 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: notify user-space about line state changes triggered by kernel

From: Bartosz Golaszewski <[email protected]>

We currently only emit CHANGED_CONFIG events when the user-space changes GPIO config. We won't be notified if changes come from in-kernel. Let's call the notifier chain whenever kernel users change direction or any of the active-low, debounce or consumer name settings. We don't notify the user-space about the persistence as the uAPI has no notion of it.

---
 drivers/gpio/gpiolib.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +19 -2
index 76e0c38026c3..77e0d0afa2b5 100644
@@ -2421,6 +2421,9 @@ int gpiod_direction_input(struct gpio_desc *desc)
 	}
 	if (ret == 0) {
 		clear_bit(FLAG_IS_OUT, &desc->flags);
+		blocking_notifier_call_chain(&desc->gdev->notifier,
+					     GPIO_V2_LINE_CHANGED_CONFIG,
+					     desc);
 		ret = gpio_set_bias(desc);
 	}
 
@@ -2466,8 +2469,12 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
 		gc->set(gc, gpio_chip_hwgpio(desc), val);
 	}
 
-	if (!ret)
+	if (!ret) {
 		set_bit(FLAG_IS_OUT, &desc->flags);
+		blocking_notifier_call_chain(&desc->gdev->notifier,
+					     GPIO_V2_LINE_CHANGED_CONFIG,
+					     desc);
+	}
 	trace_gpio_value(desc_to_gpio(desc), 0, val);
 	trace_gpio_direction(desc_to_gpio(desc), 0, ret);
 	return ret;
@@ -2654,9 +2661,15 @@ EXPORT_SYMBOL_GPL(gpiod_set_config);
 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
 {
 	unsigned long config;
+	int ret;
 
 	config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
-	return gpiod_set_config(desc, config);
+	ret = gpiod_set_config(desc, config);
+	if (!ret)
+		blocking_notifier_call_chain(&desc->gdev->notifier,
+					     GPIO_V2_LINE_CHANGED_CONFIG,
+					     desc);
+	return ret;
 }
 EXPORT_SYMBOL_GPL(gpiod_set_debounce);
 
@@ -2705,6 +2718,8 @@ void gpiod_toggle_active_low(struct gpio_desc *desc)
 {
 	VALIDATE_DESC_VOID(desc);
 	change_bit(FLAG_ACTIVE_LOW, &desc->flags);
+	blocking_notifier_call_chain(&desc->gdev->notifier,
+				     GPIO_V2_LINE_CHANGED_CONFIG, desc);
 }
 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
 
@@ -3312,6 +3327,8 @@ int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
 
 	kfree_const(desc->label);
 	desc_set_label(desc, name);
+	blocking_notifier_call_chain(&desc->gdev->notifier,
+				     GPIO_V2_LINE_CHANGED_CONFIG, desc);
 
 	return 0;
 }
Signature · 1 line
2.39.2
#1 lore raw
Quoted 1 line · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Interestingly why you keep submitter and author different...

Quoted 5 lines

We currently only emit CHANGED_CONFIG events when the user-space changes GPIO config. We won't be notified if changes come from in-kernel. Let's call the notifier chain whenever kernel users change direction or any of the active-low, debounce or consumer name settings. We don't notify the user-space about the persistence as the uAPI has no notion of it.

...

drivers/gpio/gpiolib.c static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) · line 2480

Quoted 10 lines
drivers/gpio/gpiolib.c +5 -1
-	if (!ret)
+	if (!ret) {
 		set_bit(FLAG_IS_OUT, &desc->flags);
+		blocking_notifier_call_chain(&desc->gdev->notifier,
+					     GPIO_V2_LINE_CHANGED_CONFIG,
+					     desc);
+	}
 	trace_gpio_value(desc_to_gpio(desc), 0, val);
 	trace_gpio_direction(desc_to_gpio(desc), 0, ret);
 	return ret;

The if (!ret) makes me a bit slower to understand as usual pattern to test for the errors first.

That said, perhaps

	if (ret)
		goto out_trace_event;

	set_bit(FLAG_IS_OUT, &desc->flags);
	blocking_notifier_call_chain(&desc->gdev->notifier,
				     GPIO_V2_LINE_CHANGED_CONFIG, desc);

out_trace_event:

	trace_gpio_value(desc_to_gpio(desc), 0, val);
	trace_gpio_direction(desc_to_gpio(desc), 0, ret);
	return ret;

...

drivers/gpio/gpiolib.c EXPORT_SYMBOL_GPL(gpiod_set_config); · line 2672

Quoted 6 lines
drivers/gpio/gpiolib.c +6 -0
+	ret = gpiod_set_config(desc, config);
+	if (!ret)
+		blocking_notifier_call_chain(&desc->gdev->notifier,
+					     GPIO_V2_LINE_CHANGED_CONFIG,
+					     desc);
+	return ret;

Ditto.

	ret = gpiod_set_config(desc, config);
	if (ret)
		return ret;

	blocking_notifier_call_chain(&desc->gdev->notifier,
				     GPIO_V2_LINE_CHANGED_CONFIG, desc);
	return 0;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 53 lines · Andy Shevchenko
Quoted 1 line · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Interestingly why you keep submitter and author different...

Quoted 5 lines

We currently only emit CHANGED_CONFIG events when the user-space changes GPIO config. We won't be notified if changes come from in-kernel. Let's call the notifier chain whenever kernel users change direction or any of the active-low, debounce or consumer name settings. We don't notify the user-space about the persistence as the uAPI has no notion of it.

...

drivers/gpio/gpiolib.c static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) · line 2480

Quoted 10 lines
drivers/gpio/gpiolib.c +5 -1
-     if (!ret)
+     if (!ret) {
              set_bit(FLAG_IS_OUT, &desc->flags);
+             blocking_notifier_call_chain(&desc->gdev->notifier,
+                                          GPIO_V2_LINE_CHANGED_CONFIG,
+                                          desc);
+     }
      trace_gpio_value(desc_to_gpio(desc), 0, val);
      trace_gpio_direction(desc_to_gpio(desc), 0, ret);
      return ret;

The if (!ret) makes me a bit slower to understand as usual pattern to test for the errors first.

That said, perhaps

        if (ret)
                goto out_trace_event;

        set_bit(FLAG_IS_OUT, &desc->flags);
        blocking_notifier_call_chain(&desc->gdev->notifier,
                                     GPIO_V2_LINE_CHANGED_CONFIG, desc);

out_trace_event:

        trace_gpio_value(desc_to_gpio(desc), 0, val);
        trace_gpio_direction(desc_to_gpio(desc), 0, ret);
        return ret;

...

drivers/gpio/gpiolib.c EXPORT_SYMBOL_GPL(gpiod_set_config); · line 2672

Quoted 6 lines
drivers/gpio/gpiolib.c +6 -0
+     ret = gpiod_set_config(desc, config);
+     if (!ret)
+             blocking_notifier_call_chain(&desc->gdev->notifier,
+                                          GPIO_V2_LINE_CHANGED_CONFIG,
+                                          desc);
+     return ret;

Ditto.

Quoted 8 lines
        ret = gpiod_set_config(desc, config);
        if (ret)
                return ret;

        blocking_notifier_call_chain(&desc->gdev->notifier,
                                     GPIO_V2_LINE_CHANGED_CONFIG, desc);
        return 0;

I agree with the latter but the former introduces an unnecessary goto and looks worse IMO.

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko