Message ID | ZoLt_qBCQS-tG8Ar@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | iInput: adp5588-keys - use guard notation when acquiring mutexes | expand |
Hello Dmitry, $Subject ~= s/iI/I/ On Mon, Jul 01, 2024 at 10:57:18AM -0700, Dmitry Torokhov wrote: > This makes the code more compact and error handling more robust. > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> > --- > > Sending this out because Utsav is working on the driver so he can rebase > the work on top of this. > > drivers/input/keyboard/adp5588-keys.c | 49 ++++++++++----------------- > 1 file changed, 17 insertions(+), 32 deletions(-) > > diff --git a/drivers/input/keyboard/adp5588-keys.c b/drivers/input/keyboard/adp5588-keys.c > index 1b0279393df4..09bcfc6b9408 100644 > --- a/drivers/input/keyboard/adp5588-keys.c > +++ b/drivers/input/keyboard/adp5588-keys.c > @@ -221,15 +221,13 @@ static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off) > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > int val; > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > if (kpad->dir[bank] & bit) > val = kpad->dat_out[bank]; > else > val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank); > > - mutex_unlock(&kpad->gpio_lock); > - > return !!(val & bit); > } > > @@ -240,7 +238,7 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > if (val) > kpad->dat_out[bank] |= bit; > @@ -248,8 +246,6 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, > kpad->dat_out[bank] &= ~bit; > > adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, kpad->dat_out[bank]); > - > - mutex_unlock(&kpad->gpio_lock); > } > > static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, > @@ -259,7 +255,6 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > bool pull_disable; > - int ret; > > switch (pinconf_to_config_param(config)) { > case PIN_CONFIG_BIAS_PULL_UP: > @@ -272,19 +267,15 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, > return -ENOTSUPP; > } > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > if (pull_disable) > kpad->pull_dis[bank] |= bit; > else > kpad->pull_dis[bank] &= bit; > > - ret = adp5588_write(kpad->client, GPIO_PULL1 + bank, > - kpad->pull_dis[bank]); > - > - mutex_unlock(&kpad->gpio_lock); > - > - return ret; > + return adp5588_write(kpad->client, GPIO_PULL1 + bank, > + kpad->pull_dis[bank]); > } > > static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off) > @@ -292,16 +283,11 @@ static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off > struct adp5588_kpad *kpad = gpiochip_get_data(chip); > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > - int ret; > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > kpad->dir[bank] &= ~bit; > - ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); > - > - mutex_unlock(&kpad->gpio_lock); > - > - return ret; > + return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); > } > > static int adp5588_gpio_direction_output(struct gpio_chip *chip, > @@ -310,9 +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, > struct adp5588_kpad *kpad = gpiochip_get_data(chip); > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > - int ret; > + int error; If you keep ret it's consistent with the other functions in this driver (at least the one you touched above). Otherwise looks fine to me, Uwe
On Mon, Jul 01, 2024 at 10:28:09PM +0200, Uwe Kleine-König wrote: > Hello Dmitry, > > $Subject ~= s/iI/I/ Oops, vim strikes again ;) > > > > static int adp5588_gpio_direction_output(struct gpio_chip *chip, > > @@ -310,9 +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, > > struct adp5588_kpad *kpad = gpiochip_get_data(chip); > > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > > - int ret; > > + int error; > > If you keep ret it's consistent with the other functions in this driver > (at least the one you touched above). This is part of my quest to have variables that hold error codes (i.e. when we do not return them in case of success) to be called "error". "ret" or "retval" is to be used when it is returned in both error and success cases; before the guard notation we needed to use the same variable in both success and failure. > > Otherwise looks fine to me, Thanks for the review!
> -----Original Message----- > From: Dmitry Torokhov <dmitry.torokhov@gmail.com> > Sent: Monday, July 1, 2024 7:57 PM > To: Hennerich, Michael <Michael.Hennerich@analog.com>; Agarwal, Utsav > <Utsav.Agarwal@analog.com>; Nuno Sá <noname.nuno@gmail.com> > Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>; Uwe Kleine-König > <u.kleine-koenig@pengutronix.de>; linux-input@vger.kernel.org; linux- > kernel@vger.kernel.org > Subject: [PATCH] iInput: adp5588-keys - use guard notation when acquiring > mutexes > > > This makes the code more compact and error handling more robust. > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Acked-by: Michael Hennerich <michael.hennerich@analog.com> > --- > > Sending this out because Utsav is working on the driver so he can rebase the > work on top of this. > > drivers/input/keyboard/adp5588-keys.c | 49 ++++++++++----------------- > 1 file changed, 17 insertions(+), 32 deletions(-) > > diff --git a/drivers/input/keyboard/adp5588-keys.c > b/drivers/input/keyboard/adp5588-keys.c > index 1b0279393df4..09bcfc6b9408 100644 > --- a/drivers/input/keyboard/adp5588-keys.c > +++ b/drivers/input/keyboard/adp5588-keys.c > @@ -221,15 +221,13 @@ static int adp5588_gpio_get_value(struct gpio_chip > *chip, unsigned int off) > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > int val; > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > if (kpad->dir[bank] & bit) > val = kpad->dat_out[bank]; > else > val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank); > > - mutex_unlock(&kpad->gpio_lock); > - > return !!(val & bit); > } > > @@ -240,7 +238,7 @@ static void adp5588_gpio_set_value(struct gpio_chip > *chip, > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > if (val) > kpad->dat_out[bank] |= bit; > @@ -248,8 +246,6 @@ static void adp5588_gpio_set_value(struct gpio_chip > *chip, > kpad->dat_out[bank] &= ~bit; > > adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, kpad- > >dat_out[bank]); > - > - mutex_unlock(&kpad->gpio_lock); > } > > static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, > @@ -259,7 +255,6 @@ static int adp5588_gpio_set_config(struct gpio_chip > *chip, unsigned int off, > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > bool pull_disable; > - int ret; > > switch (pinconf_to_config_param(config)) { > case PIN_CONFIG_BIAS_PULL_UP: > @@ -272,19 +267,15 @@ static int adp5588_gpio_set_config(struct gpio_chip > *chip, unsigned int off, > return -ENOTSUPP; > } > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > if (pull_disable) > kpad->pull_dis[bank] |= bit; > else > kpad->pull_dis[bank] &= bit; > > - ret = adp5588_write(kpad->client, GPIO_PULL1 + bank, > - kpad->pull_dis[bank]); > - > - mutex_unlock(&kpad->gpio_lock); > - > - return ret; > + return adp5588_write(kpad->client, GPIO_PULL1 + bank, > + kpad->pull_dis[bank]); > } > > static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int > off) @@ -292,16 +283,11 @@ static int adp5588_gpio_direction_input(struct > gpio_chip *chip, unsigned int off > struct adp5588_kpad *kpad = gpiochip_get_data(chip); > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > - int ret; > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > kpad->dir[bank] &= ~bit; > - ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); > - > - mutex_unlock(&kpad->gpio_lock); > - > - return ret; > + return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad- > >dir[bank]); > } > > static int adp5588_gpio_direction_output(struct gpio_chip *chip, @@ -310,9 > +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, > struct adp5588_kpad *kpad = gpiochip_get_data(chip); > unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); > unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); > - int ret; > + int error; > > - mutex_lock(&kpad->gpio_lock); > + guard(mutex)(&kpad->gpio_lock); > > kpad->dir[bank] |= bit; > > @@ -321,17 +307,16 @@ static int adp5588_gpio_direction_output(struct > gpio_chip *chip, > else > kpad->dat_out[bank] &= ~bit; > > - ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, > - kpad->dat_out[bank]); > - if (ret) > - goto out_unlock; > - > - ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); > + error = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, > + kpad->dat_out[bank]); > + if (error) > + return error; > > -out_unlock: > - mutex_unlock(&kpad->gpio_lock); > + error = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad- > >dir[bank]); > + if (error) > + return error; > > - return ret; > + return 0; > } > > static int adp5588_build_gpiomap(struct adp5588_kpad *kpad) > -- > 2.45.2.803.g4e1b14247a-goog > > > -- > Dmitry
Hello Dmitry, On Mon, Jul 01, 2024 at 03:51:43PM -0700, Dmitry Torokhov wrote: > On Mon, Jul 01, 2024 at 10:28:09PM +0200, Uwe Kleine-König wrote: > > If you keep ret it's consistent with the other functions in this driver > > (at least the one you touched above). > > This is part of my quest to have variables that hold error codes (i.e. > when we do not return them in case of success) to be called "error". > "ret" or "retval" is to be used when it is returned in both error and > success cases; before the guard notation we needed to use the same > variable in both success and failure. That seems to be a bit artificial to me, given that you changed the variable "ret" in adp5588_gpio_direction_output() to "error" and the resulting code can be further simplified using diff --git a/drivers/input/keyboard/adp5588-keys.c b/drivers/input/keyboard/adp5588-keys.c index 09bcfc6b9408..5c8f07bbc452 100644 --- a/drivers/input/keyboard/adp5588-keys.c +++ b/drivers/input/keyboard/adp5588-keys.c @@ -313,10 +313,8 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, return error; error = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); - if (error) - return error; - return 0; + return error; } static int adp5588_build_gpiomap(struct adp5588_kpad *kpad) There are further inconsistencies in this driver, e.g. ret in adp5588_fw_parse() isn't returned in the success path. adp5588_probe() has both, error and ret, neither is used in the success path and a single variable would be enough. Given that, I'd suggest to make two commits: Let the driver use guards without unmentioned and incomplete cleanups in the variable naming and then in an additional changeset unify the error variable naming in the complete driver. Best regards Uwe
> This makes the code more compact and error handling more robust.
I suggest to improve such a change description another bit.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc6#n45
Regards,
Markus
diff --git a/drivers/input/keyboard/adp5588-keys.c b/drivers/input/keyboard/adp5588-keys.c index 1b0279393df4..09bcfc6b9408 100644 --- a/drivers/input/keyboard/adp5588-keys.c +++ b/drivers/input/keyboard/adp5588-keys.c @@ -221,15 +221,13 @@ static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned int off) unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); int val; - mutex_lock(&kpad->gpio_lock); + guard(mutex)(&kpad->gpio_lock); if (kpad->dir[bank] & bit) val = kpad->dat_out[bank]; else val = adp5588_read(kpad->client, GPIO_DAT_STAT1 + bank); - mutex_unlock(&kpad->gpio_lock); - return !!(val & bit); } @@ -240,7 +238,7 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); - mutex_lock(&kpad->gpio_lock); + guard(mutex)(&kpad->gpio_lock); if (val) kpad->dat_out[bank] |= bit; @@ -248,8 +246,6 @@ static void adp5588_gpio_set_value(struct gpio_chip *chip, kpad->dat_out[bank] &= ~bit; adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, kpad->dat_out[bank]); - - mutex_unlock(&kpad->gpio_lock); } static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, @@ -259,7 +255,6 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); bool pull_disable; - int ret; switch (pinconf_to_config_param(config)) { case PIN_CONFIG_BIAS_PULL_UP: @@ -272,19 +267,15 @@ static int adp5588_gpio_set_config(struct gpio_chip *chip, unsigned int off, return -ENOTSUPP; } - mutex_lock(&kpad->gpio_lock); + guard(mutex)(&kpad->gpio_lock); if (pull_disable) kpad->pull_dis[bank] |= bit; else kpad->pull_dis[bank] &= bit; - ret = adp5588_write(kpad->client, GPIO_PULL1 + bank, - kpad->pull_dis[bank]); - - mutex_unlock(&kpad->gpio_lock); - - return ret; + return adp5588_write(kpad->client, GPIO_PULL1 + bank, + kpad->pull_dis[bank]); } static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off) @@ -292,16 +283,11 @@ static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned int off struct adp5588_kpad *kpad = gpiochip_get_data(chip); unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); - int ret; - mutex_lock(&kpad->gpio_lock); + guard(mutex)(&kpad->gpio_lock); kpad->dir[bank] &= ~bit; - ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); - - mutex_unlock(&kpad->gpio_lock); - - return ret; + return adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); } static int adp5588_gpio_direction_output(struct gpio_chip *chip, @@ -310,9 +296,9 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, struct adp5588_kpad *kpad = gpiochip_get_data(chip); unsigned int bank = ADP5588_BANK(kpad->gpiomap[off]); unsigned int bit = ADP5588_BIT(kpad->gpiomap[off]); - int ret; + int error; - mutex_lock(&kpad->gpio_lock); + guard(mutex)(&kpad->gpio_lock); kpad->dir[bank] |= bit; @@ -321,17 +307,16 @@ static int adp5588_gpio_direction_output(struct gpio_chip *chip, else kpad->dat_out[bank] &= ~bit; - ret = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, - kpad->dat_out[bank]); - if (ret) - goto out_unlock; - - ret = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); + error = adp5588_write(kpad->client, GPIO_DAT_OUT1 + bank, + kpad->dat_out[bank]); + if (error) + return error; -out_unlock: - mutex_unlock(&kpad->gpio_lock); + error = adp5588_write(kpad->client, GPIO_DIR1 + bank, kpad->dir[bank]); + if (error) + return error; - return ret; + return 0; } static int adp5588_build_gpiomap(struct adp5588_kpad *kpad)
This makes the code more compact and error handling more robust. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> --- Sending this out because Utsav is working on the driver so he can rebase the work on top of this. drivers/input/keyboard/adp5588-keys.c | 49 ++++++++++----------------- 1 file changed, 17 insertions(+), 32 deletions(-)