* [PATCH] gpio: reduce descriptor validation code size
@ 2017-12-21 0:27 Rasmus Villemoes
2017-12-21 12:53 ` Linus Walleij
2017-12-21 12:55 ` Vladimir Zapolskiy
0 siblings, 2 replies; 3+ messages in thread
From: Rasmus Villemoes @ 2017-12-21 0:27 UTC (permalink / raw)
To: Linus Walleij; +Cc: Rasmus Villemoes, linux-gpio, linux-kernel
While we do need macros to be able to return from the "calling"
function, we can still factor the checks done by the VALIDATE_DESC*
macros into a real helper function. This reduces the backslashtitis,
avoids duplicating the logic in the two macros and saves about 1K of
generated code:
$ scripts/bloat-o-meter drivers/gpio/gpiolib.o.{0,1}
add/remove: 1/0 grow/shrink: 0/15 up/down: 104/-1281 (-1177)
Function old new delta
validate_desc - 104 +104
gpiod_set_value 192 135 -57
gpiod_set_raw_value 125 67 -58
gpiod_direction_output 412 351 -61
gpiod_set_value_cansleep 150 70 -80
gpiod_set_raw_value_cansleep 132 52 -80
gpiod_get_raw_value 139 54 -85
gpiod_set_debounce 226 140 -86
gpiod_direction_output_raw 124 38 -86
gpiod_get_value 161 74 -87
gpiod_cansleep 126 39 -87
gpiod_get_raw_value_cansleep 130 39 -91
gpiod_get_value_cansleep 152 59 -93
gpiod_is_active_low 128 33 -95
gpiod_request 299 184 -115
gpiod_direction_input 386 266 -120
Total: Before=25460, After=24283, chg -4.62%
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
drivers/gpio/gpiolib.c | 57 ++++++++++++++++++++++++--------------------------
1 file changed, 27 insertions(+), 30 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index aad84a6306c4..adbaa19bffe4 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -2165,40 +2165,37 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
* macro to avoid endless duplication. If the desc is NULL it is an
* optional GPIO and calls should just bail out.
*/
+static int validate_desc(const struct gpio_desc *desc, const char *func)
+{
+ if (!desc)
+ return 0;
+ if (IS_ERR(desc)) {
+ pr_warn("%s: invalid GPIO (errorpointer)\n", func);
+ return PTR_ERR(desc);
+ }
+ if (!desc->gdev) {
+ pr_warn("%s: invalid GPIO (no device)\n", func);
+ return -EINVAL;
+ }
+ if (!desc->gdev->chip) {
+ dev_warn(&desc->gdev->dev,
+ "%s: backing chip is gone\n", func);
+ return 0;
+ }
+ return 1;
+}
+
#define VALIDATE_DESC(desc) do { \
- if (!desc) \
- return 0; \
- if (IS_ERR(desc)) { \
- pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
- return PTR_ERR(desc); \
- } \
- if (!desc->gdev) { \
- pr_warn("%s: invalid GPIO (no device)\n", __func__); \
- return -EINVAL; \
- } \
- if ( !desc->gdev->chip ) { \
- dev_warn(&desc->gdev->dev, \
- "%s: backing chip is gone\n", __func__); \
- return 0; \
- } } while (0)
+ int __valid = validate_desc(desc, __func__); \
+ if (__valid <= 0) \
+ return __valid; \
+ } while (0)
#define VALIDATE_DESC_VOID(desc) do { \
- if (!desc) \
- return; \
- if (IS_ERR(desc)) { \
- pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
+ int __valid = validate_desc(desc, __func__); \
+ if (__valid <= 0) \
return; \
- } \
- if (!desc->gdev) { \
- pr_warn("%s: invalid GPIO (no device)\n", __func__); \
- return; \
- } \
- if (!desc->gdev->chip) { \
- dev_warn(&desc->gdev->dev, \
- "%s: backing chip is gone\n", __func__); \
- return; \
- } } while (0)
-
+ } while (0)
int gpiod_request(struct gpio_desc *desc, const char *label)
{
--
2.11.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] gpio: reduce descriptor validation code size
2017-12-21 0:27 [PATCH] gpio: reduce descriptor validation code size Rasmus Villemoes
@ 2017-12-21 12:53 ` Linus Walleij
2017-12-21 12:55 ` Vladimir Zapolskiy
1 sibling, 0 replies; 3+ messages in thread
From: Linus Walleij @ 2017-12-21 12:53 UTC (permalink / raw)
To: Rasmus Villemoes; +Cc: linux-gpio, linux-kernel
On Thu, Dec 21, 2017 at 1:27 AM, Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
> While we do need macros to be able to return from the "calling"
> function, we can still factor the checks done by the VALIDATE_DESC*
> macros into a real helper function. This reduces the backslashtitis,
> avoids duplicating the logic in the two macros and saves about 1K of
> generated code:
>
> $ scripts/bloat-o-meter drivers/gpio/gpiolib.o.{0,1}
> add/remove: 1/0 grow/shrink: 0/15 up/down: 104/-1281 (-1177)
> Function old new delta
> validate_desc - 104 +104
> gpiod_set_value 192 135 -57
> gpiod_set_raw_value 125 67 -58
> gpiod_direction_output 412 351 -61
> gpiod_set_value_cansleep 150 70 -80
> gpiod_set_raw_value_cansleep 132 52 -80
> gpiod_get_raw_value 139 54 -85
> gpiod_set_debounce 226 140 -86
> gpiod_direction_output_raw 124 38 -86
> gpiod_get_value 161 74 -87
> gpiod_cansleep 126 39 -87
> gpiod_get_raw_value_cansleep 130 39 -91
> gpiod_get_value_cansleep 152 59 -93
> gpiod_is_active_low 128 33 -95
> gpiod_request 299 184 -115
> gpiod_direction_input 386 266 -120
> Total: Before=25460, After=24283, chg -4.62%
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Excellent patch! Very happy with this.
Patch applied, naturally :)
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] gpio: reduce descriptor validation code size
2017-12-21 0:27 [PATCH] gpio: reduce descriptor validation code size Rasmus Villemoes
2017-12-21 12:53 ` Linus Walleij
@ 2017-12-21 12:55 ` Vladimir Zapolskiy
1 sibling, 0 replies; 3+ messages in thread
From: Vladimir Zapolskiy @ 2017-12-21 12:55 UTC (permalink / raw)
To: Rasmus Villemoes, Linus Walleij; +Cc: linux-gpio, linux-kernel
Hello Rasmus,
On 12/21/2017 02:27 AM, Rasmus Villemoes wrote:
> While we do need macros to be able to return from the "calling"
> function, we can still factor the checks done by the VALIDATE_DESC*
> macros into a real helper function. This reduces the backslashtitis,
> avoids duplicating the logic in the two macros and saves about 1K of
> generated code:
>
> $ scripts/bloat-o-meter drivers/gpio/gpiolib.o.{0,1}
> add/remove: 1/0 grow/shrink: 0/15 up/down: 104/-1281 (-1177)
> Function old new delta
> validate_desc - 104 +104
> gpiod_set_value 192 135 -57
> gpiod_set_raw_value 125 67 -58
> gpiod_direction_output 412 351 -61
> gpiod_set_value_cansleep 150 70 -80
> gpiod_set_raw_value_cansleep 132 52 -80
> gpiod_get_raw_value 139 54 -85
> gpiod_set_debounce 226 140 -86
> gpiod_direction_output_raw 124 38 -86
> gpiod_get_value 161 74 -87
> gpiod_cansleep 126 39 -87
> gpiod_get_raw_value_cansleep 130 39 -91
> gpiod_get_value_cansleep 152 59 -93
> gpiod_is_active_low 128 33 -95
> gpiod_request 299 184 -115
> gpiod_direction_input 386 266 -120
> Total: Before=25460, After=24283, chg -4.62%
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
The change looks good from my point of view, thank you for the change.
Reviewed-by: Vladimir Zapolskiy <vz@mleia.com>
--
With best wishes,
Vladimir
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-12-21 12:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-21 0:27 [PATCH] gpio: reduce descriptor validation code size Rasmus Villemoes
2017-12-21 12:53 ` Linus Walleij
2017-12-21 12:55 ` Vladimir Zapolskiy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®