* [PATCH 01/17] Input: adp5589-keys - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 02/17] Input: applespi - use guard notation when acquiring spinlock Dmitry Torokhov
` (15 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/adp5589-keys.c | 39 +++++++++++++--------------
1 file changed, 18 insertions(+), 21 deletions(-)
diff --git a/drivers/input/keyboard/adp5589-keys.c b/drivers/input/keyboard/adp5589-keys.c
index 8996e00cd63a..735d96b056d4 100644
--- a/drivers/input/keyboard/adp5589-keys.c
+++ b/drivers/input/keyboard/adp5589-keys.c
@@ -404,7 +404,7 @@ static void adp5589_gpio_set_value(struct gpio_chip *chip,
unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
- mutex_lock(&kpad->gpio_lock);
+ guard(mutex)(&kpad->gpio_lock);
if (val)
kpad->dat_out[bank] |= bit;
@@ -413,8 +413,6 @@ static void adp5589_gpio_set_value(struct gpio_chip *chip,
adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A) +
bank, kpad->dat_out[bank]);
-
- mutex_unlock(&kpad->gpio_lock);
}
static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
@@ -422,18 +420,13 @@ static int adp5589_gpio_direction_input(struct gpio_chip *chip, unsigned off)
struct adp5589_kpad *kpad = gpiochip_get_data(chip);
unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
- int ret;
- mutex_lock(&kpad->gpio_lock);
+ guard(mutex)(&kpad->gpio_lock);
kpad->dir[bank] &= ~bit;
- ret = adp5589_write(kpad->client,
- kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
- kpad->dir[bank]);
-
- mutex_unlock(&kpad->gpio_lock);
-
- return ret;
+ return adp5589_write(kpad->client,
+ kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
+ kpad->dir[bank]);
}
static int adp5589_gpio_direction_output(struct gpio_chip *chip,
@@ -442,9 +435,9 @@ static int adp5589_gpio_direction_output(struct gpio_chip *chip,
struct adp5589_kpad *kpad = gpiochip_get_data(chip);
unsigned int bank = kpad->var->bank(kpad->gpiomap[off]);
unsigned int bit = kpad->var->bit(kpad->gpiomap[off]);
- int ret;
+ int error;
- mutex_lock(&kpad->gpio_lock);
+ guard(mutex)(&kpad->gpio_lock);
kpad->dir[bank] |= bit;
@@ -453,15 +446,19 @@ static int adp5589_gpio_direction_output(struct gpio_chip *chip,
else
kpad->dat_out[bank] &= ~bit;
- ret = adp5589_write(kpad->client, kpad->var->reg(ADP5589_GPO_DATA_OUT_A)
- + bank, kpad->dat_out[bank]);
- ret |= adp5589_write(kpad->client,
- kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
- kpad->dir[bank]);
+ error = adp5589_write(kpad->client,
+ kpad->var->reg(ADP5589_GPO_DATA_OUT_A) + bank,
+ kpad->dat_out[bank]);
+ if (error)
+ return error;
- mutex_unlock(&kpad->gpio_lock);
+ error = adp5589_write(kpad->client,
+ kpad->var->reg(ADP5589_GPIO_DIRECTION_A) + bank,
+ kpad->dir[bank]);
+ if (error)
+ return error;
- return ret;
+ return 0;
}
static int adp5589_build_gpiomap(struct adp5589_kpad *kpad,
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 02/17] Input: applespi - use guard notation when acquiring spinlock
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 01/17] Input: adp5589-keys - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 03/17] Input: atkbd - use guard notation when acquiring mutex Dmitry Torokhov
` (14 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that locks are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/applespi.c | 72 ++++++++-----------------------
1 file changed, 18 insertions(+), 54 deletions(-)
diff --git a/drivers/input/keyboard/applespi.c b/drivers/input/keyboard/applespi.c
index cf25177b4830..501ce8154786 100644
--- a/drivers/input/keyboard/applespi.c
+++ b/drivers/input/keyboard/applespi.c
@@ -717,9 +717,7 @@ static int applespi_send_cmd_msg(struct applespi_data *applespi);
static void applespi_msg_complete(struct applespi_data *applespi,
bool is_write_msg, bool is_read_compl)
{
- unsigned long flags;
-
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
if (is_read_compl)
applespi->read_active = false;
@@ -733,8 +731,6 @@ static void applespi_msg_complete(struct applespi_data *applespi,
applespi->cmd_msg_queued = 0;
applespi_send_cmd_msg(applespi);
}
-
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static void applespi_async_write_complete(void *context)
@@ -888,33 +884,22 @@ static int applespi_send_cmd_msg(struct applespi_data *applespi)
static void applespi_init(struct applespi_data *applespi, bool is_resume)
{
- unsigned long flags;
-
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
if (is_resume)
applespi->want_mt_init_cmd = true;
else
applespi->want_tp_info_cmd = true;
applespi_send_cmd_msg(applespi);
-
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static int applespi_set_capsl_led(struct applespi_data *applespi,
bool capslock_on)
{
- unsigned long flags;
- int sts;
-
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
applespi->want_cl_led_on = capslock_on;
- sts = applespi_send_cmd_msg(applespi);
-
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
-
- return sts;
+ return applespi_send_cmd_msg(applespi);
}
static void applespi_set_bl_level(struct led_classdev *led_cdev,
@@ -922,9 +907,8 @@ static void applespi_set_bl_level(struct led_classdev *led_cdev,
{
struct applespi_data *applespi =
container_of(led_cdev, struct applespi_data, backlight_info);
- unsigned long flags;
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
if (value == 0) {
applespi->want_bl_level = value;
@@ -940,8 +924,6 @@ static void applespi_set_bl_level(struct led_classdev *led_cdev,
}
applespi_send_cmd_msg(applespi);
-
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static int applespi_event(struct input_dev *dev, unsigned int type,
@@ -1428,9 +1410,7 @@ static void applespi_got_data(struct applespi_data *applespi)
/* process packet header */
if (!applespi_verify_crc(applespi, applespi->rx_buffer,
APPLESPI_PACKET_SIZE)) {
- unsigned long flags;
-
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
if (applespi->drain) {
applespi->read_active = false;
@@ -1439,8 +1419,6 @@ static void applespi_got_data(struct applespi_data *applespi)
wake_up_all(&applespi->drain_complete);
}
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
-
return;
}
@@ -1573,11 +1551,10 @@ static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
{
struct applespi_data *applespi = context;
int sts;
- unsigned long flags;
trace_applespi_irq_received(ET_RD_IRQ, PT_READ);
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
if (!applespi->suspended) {
sts = applespi_async(applespi, &applespi->rd_m,
@@ -1590,8 +1567,6 @@ static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
applespi->read_active = true;
}
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
-
return ACPI_INTERRUPT_HANDLED;
}
@@ -1819,29 +1794,21 @@ static int applespi_probe(struct spi_device *spi)
static void applespi_drain_writes(struct applespi_data *applespi)
{
- unsigned long flags;
-
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
applespi->drain = true;
wait_event_lock_irq(applespi->drain_complete, !applespi->write_active,
applespi->cmd_msg_lock);
-
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static void applespi_drain_reads(struct applespi_data *applespi)
{
- unsigned long flags;
-
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+ guard(spinlock_irqsave)(&applespi->cmd_msg_lock);
wait_event_lock_irq(applespi->drain_complete, !applespi->read_active,
applespi->cmd_msg_lock);
applespi->suspended = true;
-
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}
static void applespi_remove(struct spi_device *spi)
@@ -1908,21 +1875,18 @@ static int applespi_resume(struct device *dev)
struct spi_device *spi = to_spi_device(dev);
struct applespi_data *applespi = spi_get_drvdata(spi);
acpi_status acpi_sts;
- unsigned long flags;
/* ensure our flags and state reflect a newly resumed device */
- spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
-
- applespi->drain = false;
- applespi->have_cl_led_on = false;
- applespi->have_bl_level = 0;
- applespi->cmd_msg_queued = 0;
- applespi->read_active = false;
- applespi->write_active = false;
-
- applespi->suspended = false;
+ scoped_guard(spinlock_irqsave, &applespi->cmd_msg_lock) {
+ applespi->drain = false;
+ applespi->have_cl_led_on = false;
+ applespi->have_bl_level = 0;
+ applespi->cmd_msg_queued = 0;
+ applespi->read_active = false;
+ applespi->write_active = false;
- spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
+ applespi->suspended = false;
+ }
/* switch on the SPI interface */
applespi_enable_spi(applespi);
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 03/17] Input: atkbd - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 01/17] Input: adp5589-keys - use guard notation when acquiring mutex Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 02/17] Input: applespi - use guard notation when acquiring spinlock Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 13:12 ` Hans de Goede
2024-08-25 5:16 ` [PATCH 04/17] Input: ep93xx_keypad " Dmitry Torokhov
` (13 subsequent siblings)
16 siblings, 1 reply; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/atkbd.c | 37 ++++++++++++++--------------------
1 file changed, 15 insertions(+), 22 deletions(-)
diff --git a/drivers/input/keyboard/atkbd.c b/drivers/input/keyboard/atkbd.c
index f4f2078cf501..5855d4fc6e6a 100644
--- a/drivers/input/keyboard/atkbd.c
+++ b/drivers/input/keyboard/atkbd.c
@@ -639,7 +639,7 @@ static void atkbd_event_work(struct work_struct *work)
{
struct atkbd *atkbd = container_of(work, struct atkbd, event_work.work);
- mutex_lock(&atkbd->mutex);
+ guard(mutex)(&atkbd->mutex);
if (!atkbd->enabled) {
/*
@@ -657,8 +657,6 @@ static void atkbd_event_work(struct work_struct *work)
if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask))
atkbd_set_repeat_rate(atkbd);
}
-
- mutex_unlock(&atkbd->mutex);
}
/*
@@ -1361,7 +1359,7 @@ static int atkbd_reconnect(struct serio *serio)
{
struct atkbd *atkbd = atkbd_from_serio(serio);
struct serio_driver *drv = serio->drv;
- int retval = -1;
+ int error;
if (!atkbd || !drv) {
dev_dbg(&serio->dev,
@@ -1369,16 +1367,17 @@ static int atkbd_reconnect(struct serio *serio)
return -1;
}
- mutex_lock(&atkbd->mutex);
+ guard(mutex)(&atkbd->mutex);
atkbd_disable(atkbd);
if (atkbd->write) {
- if (atkbd_probe(atkbd))
- goto out;
+ error = atkbd_probe(atkbd);
+ if (error)
+ return error;
if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra))
- goto out;
+ return -EIO;
/*
* Restore LED state and repeat rate. While input core
@@ -1404,11 +1403,7 @@ static int atkbd_reconnect(struct serio *serio)
if (atkbd->write)
atkbd_activate(atkbd);
- retval = 0;
-
- out:
- mutex_unlock(&atkbd->mutex);
- return retval;
+ return 0;
}
static const struct serio_device_id atkbd_serio_ids[] = {
@@ -1465,17 +1460,15 @@ static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t
struct atkbd *atkbd = atkbd_from_serio(serio);
int retval;
- retval = mutex_lock_interruptible(&atkbd->mutex);
- if (retval)
- return retval;
+ scoped_guard(mutex_intr, &atkbd->mutex) {
+ atkbd_disable(atkbd);
+ retval = handler(atkbd, buf, count);
+ atkbd_enable(atkbd);
- atkbd_disable(atkbd);
- retval = handler(atkbd, buf, count);
- atkbd_enable(atkbd);
-
- mutex_unlock(&atkbd->mutex);
+ return retval;
+ }
- return retval;
+ return -EINTR;
}
static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf)
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH 03/17] Input: atkbd - use guard notation when acquiring mutex
2024-08-25 5:16 ` [PATCH 03/17] Input: atkbd - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 13:12 ` Hans de Goede
0 siblings, 0 replies; 22+ messages in thread
From: Hans de Goede @ 2024-08-25 13:12 UTC (permalink / raw)
To: Dmitry Torokhov, linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Tony Lindgren, Jeff LaBundy,
linux-kernel, imx, linux-arm-kernel, linux-tegra
Hi,
On 8/25/24 7:16 AM, Dmitry Torokhov wrote:
> This makes the code more compact and error handling more robust
> by ensuring that mutexes are released in all code paths when control
> leaves critical section.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
> ---
> drivers/input/keyboard/atkbd.c | 37 ++++++++++++++--------------------
> 1 file changed, 15 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/input/keyboard/atkbd.c b/drivers/input/keyboard/atkbd.c
> index f4f2078cf501..5855d4fc6e6a 100644
> --- a/drivers/input/keyboard/atkbd.c
> +++ b/drivers/input/keyboard/atkbd.c
> @@ -639,7 +639,7 @@ static void atkbd_event_work(struct work_struct *work)
> {
> struct atkbd *atkbd = container_of(work, struct atkbd, event_work.work);
>
> - mutex_lock(&atkbd->mutex);
> + guard(mutex)(&atkbd->mutex);
>
> if (!atkbd->enabled) {
> /*
> @@ -657,8 +657,6 @@ static void atkbd_event_work(struct work_struct *work)
> if (test_and_clear_bit(ATKBD_REP_EVENT_BIT, &atkbd->event_mask))
> atkbd_set_repeat_rate(atkbd);
> }
> -
> - mutex_unlock(&atkbd->mutex);
> }
>
> /*
> @@ -1361,7 +1359,7 @@ static int atkbd_reconnect(struct serio *serio)
> {
> struct atkbd *atkbd = atkbd_from_serio(serio);
> struct serio_driver *drv = serio->drv;
> - int retval = -1;
> + int error;
>
> if (!atkbd || !drv) {
> dev_dbg(&serio->dev,
> @@ -1369,16 +1367,17 @@ static int atkbd_reconnect(struct serio *serio)
> return -1;
> }
>
> - mutex_lock(&atkbd->mutex);
> + guard(mutex)(&atkbd->mutex);
>
> atkbd_disable(atkbd);
>
> if (atkbd->write) {
> - if (atkbd_probe(atkbd))
> - goto out;
> + error = atkbd_probe(atkbd);
> + if (error)
> + return error;
>
> if (atkbd->set != atkbd_select_set(atkbd, atkbd->set, atkbd->extra))
> - goto out;
> + return -EIO;
>
> /*
> * Restore LED state and repeat rate. While input core
> @@ -1404,11 +1403,7 @@ static int atkbd_reconnect(struct serio *serio)
> if (atkbd->write)
> atkbd_activate(atkbd);
>
> - retval = 0;
> -
> - out:
> - mutex_unlock(&atkbd->mutex);
> - return retval;
> + return 0;
> }
>
> static const struct serio_device_id atkbd_serio_ids[] = {
> @@ -1465,17 +1460,15 @@ static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t
> struct atkbd *atkbd = atkbd_from_serio(serio);
> int retval;
>
> - retval = mutex_lock_interruptible(&atkbd->mutex);
> - if (retval)
> - return retval;
> + scoped_guard(mutex_intr, &atkbd->mutex) {
> + atkbd_disable(atkbd);
> + retval = handler(atkbd, buf, count);
> + atkbd_enable(atkbd);
>
> - atkbd_disable(atkbd);
> - retval = handler(atkbd, buf, count);
> - atkbd_enable(atkbd);
> -
> - mutex_unlock(&atkbd->mutex);
> + return retval;
> + }
>
> - return retval;
> + return -EINTR;
> }
>
> static ssize_t atkbd_show_extra(struct atkbd *atkbd, char *buf)
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 04/17] Input: ep93xx_keypad - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (2 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 03/17] Input: atkbd - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 05/17] Input: gpio-keys - switch to using cleanup functions Dmitry Torokhov
` (12 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/ep93xx_keypad.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/drivers/input/keyboard/ep93xx_keypad.c b/drivers/input/keyboard/ep93xx_keypad.c
index 6b811d6bf625..a8df957ef261 100644
--- a/drivers/input/keyboard/ep93xx_keypad.c
+++ b/drivers/input/keyboard/ep93xx_keypad.c
@@ -184,15 +184,13 @@ static int ep93xx_keypad_suspend(struct device *dev)
struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
struct input_dev *input_dev = keypad->input_dev;
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (keypad->enabled) {
clk_disable(keypad->clk);
keypad->enabled = false;
}
- mutex_unlock(&input_dev->mutex);
-
return 0;
}
@@ -202,7 +200,7 @@ static int ep93xx_keypad_resume(struct device *dev)
struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
struct input_dev *input_dev = keypad->input_dev;
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (input_device_enabled(input_dev)) {
if (!keypad->enabled) {
@@ -212,8 +210,6 @@ static int ep93xx_keypad_resume(struct device *dev)
}
}
- mutex_unlock(&input_dev->mutex);
-
return 0;
}
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 05/17] Input: gpio-keys - switch to using cleanup functions
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (3 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 04/17] Input: ep93xx_keypad " Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 13:10 ` Hans de Goede
2024-08-25 5:16 ` [PATCH 06/17] Input: imx_keypad - use guard notation when acquiring mutex Dmitry Torokhov
` (11 subsequent siblings)
16 siblings, 1 reply; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
Start using __free() and guard() primitives to simplify the code
and error handling. This makes the code more compact and error
handling more robust by ensuring that locks are released in all
code paths when control leaves critical section and all allocated
memory is freed.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/gpio_keys.c | 44 ++++++++++++------------------
1 file changed, 17 insertions(+), 27 deletions(-)
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 9fb0bdcfbf9e..380fe8dab3b0 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -245,23 +245,20 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
{
int n_events = get_n_events_by_type(type);
const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
- unsigned long *bits;
ssize_t error;
int i;
- bits = bitmap_alloc(n_events, GFP_KERNEL);
+ unsigned long *bits __free(bitmap) = bitmap_alloc(n_events, GFP_KERNEL);
if (!bits)
return -ENOMEM;
error = bitmap_parselist(buf, bits, n_events);
if (error)
- goto out;
+ return error;
/* First validate */
- if (!bitmap_subset(bits, bitmap, n_events)) {
- error = -EINVAL;
- goto out;
- }
+ if (!bitmap_subset(bits, bitmap, n_events))
+ return -EINVAL;
for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i];
@@ -271,12 +268,11 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
if (test_bit(*bdata->code, bits) &&
!bdata->button->can_disable) {
- error = -EINVAL;
- goto out;
+ return -EINVAL;
}
}
- mutex_lock(&ddata->disable_lock);
+ guard(mutex)(&ddata->disable_lock);
for (i = 0; i < ddata->pdata->nbuttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i];
@@ -290,11 +286,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
gpio_keys_enable_button(bdata);
}
- mutex_unlock(&ddata->disable_lock);
-
-out:
- bitmap_free(bits);
- return error;
+ return 0;
}
#define ATTR_SHOW_FN(name, type, only_disabled) \
@@ -470,11 +462,10 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
{
struct gpio_button_data *bdata = dev_id;
struct input_dev *input = bdata->input;
- unsigned long flags;
BUG_ON(irq != bdata->irq);
- spin_lock_irqsave(&bdata->lock, flags);
+ guard(spinlock_irqsave)(&bdata->lock);
if (!bdata->key_pressed) {
if (bdata->button->wakeup)
@@ -497,7 +488,6 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
ms_to_ktime(bdata->release_delay),
HRTIMER_MODE_REL_HARD);
out:
- spin_unlock_irqrestore(&bdata->lock, flags);
return IRQ_HANDLED;
}
@@ -1062,10 +1052,10 @@ static int gpio_keys_suspend(struct device *dev)
if (error)
return error;
} else {
- mutex_lock(&input->mutex);
+ guard(mutex)(&input->mutex);
+
if (input_device_enabled(input))
gpio_keys_close(input);
- mutex_unlock(&input->mutex);
}
return 0;
@@ -1075,20 +1065,20 @@ static int gpio_keys_resume(struct device *dev)
{
struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
struct input_dev *input = ddata->input;
- int error = 0;
+ int error;
if (device_may_wakeup(dev)) {
gpio_keys_disable_wakeup(ddata);
} else {
- mutex_lock(&input->mutex);
- if (input_device_enabled(input))
+ guard(mutex)(&input->mutex);
+
+ if (input_device_enabled(input)) {
error = gpio_keys_open(input);
- mutex_unlock(&input->mutex);
+ if (error)
+ return error;
+ }
}
- if (error)
- return error;
-
gpio_keys_report_state(ddata);
return 0;
}
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH 05/17] Input: gpio-keys - switch to using cleanup functions
2024-08-25 5:16 ` [PATCH 05/17] Input: gpio-keys - switch to using cleanup functions Dmitry Torokhov
@ 2024-08-25 13:10 ` Hans de Goede
0 siblings, 0 replies; 22+ messages in thread
From: Hans de Goede @ 2024-08-25 13:10 UTC (permalink / raw)
To: Dmitry Torokhov, linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Tony Lindgren, Jeff LaBundy,
linux-kernel, imx, linux-arm-kernel, linux-tegra
Hi,
On 8/25/24 7:16 AM, Dmitry Torokhov wrote:
> Start using __free() and guard() primitives to simplify the code
> and error handling. This makes the code more compact and error
> handling more robust by ensuring that locks are released in all
> code paths when control leaves critical section and all allocated
> memory is freed.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Regards,
Hans
> ---
> drivers/input/keyboard/gpio_keys.c | 44 ++++++++++++------------------
> 1 file changed, 17 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
> index 9fb0bdcfbf9e..380fe8dab3b0 100644
> --- a/drivers/input/keyboard/gpio_keys.c
> +++ b/drivers/input/keyboard/gpio_keys.c
> @@ -245,23 +245,20 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
> {
> int n_events = get_n_events_by_type(type);
> const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
> - unsigned long *bits;
> ssize_t error;
> int i;
>
> - bits = bitmap_alloc(n_events, GFP_KERNEL);
> + unsigned long *bits __free(bitmap) = bitmap_alloc(n_events, GFP_KERNEL);
> if (!bits)
> return -ENOMEM;
>
> error = bitmap_parselist(buf, bits, n_events);
> if (error)
> - goto out;
> + return error;
>
> /* First validate */
> - if (!bitmap_subset(bits, bitmap, n_events)) {
> - error = -EINVAL;
> - goto out;
> - }
> + if (!bitmap_subset(bits, bitmap, n_events))
> + return -EINVAL;
>
> for (i = 0; i < ddata->pdata->nbuttons; i++) {
> struct gpio_button_data *bdata = &ddata->data[i];
> @@ -271,12 +268,11 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
>
> if (test_bit(*bdata->code, bits) &&
> !bdata->button->can_disable) {
> - error = -EINVAL;
> - goto out;
> + return -EINVAL;
> }
> }
>
> - mutex_lock(&ddata->disable_lock);
> + guard(mutex)(&ddata->disable_lock);
>
> for (i = 0; i < ddata->pdata->nbuttons; i++) {
> struct gpio_button_data *bdata = &ddata->data[i];
> @@ -290,11 +286,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
> gpio_keys_enable_button(bdata);
> }
>
> - mutex_unlock(&ddata->disable_lock);
> -
> -out:
> - bitmap_free(bits);
> - return error;
> + return 0;
> }
>
> #define ATTR_SHOW_FN(name, type, only_disabled) \
> @@ -470,11 +462,10 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
> {
> struct gpio_button_data *bdata = dev_id;
> struct input_dev *input = bdata->input;
> - unsigned long flags;
>
> BUG_ON(irq != bdata->irq);
>
> - spin_lock_irqsave(&bdata->lock, flags);
> + guard(spinlock_irqsave)(&bdata->lock);
>
> if (!bdata->key_pressed) {
> if (bdata->button->wakeup)
> @@ -497,7 +488,6 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
> ms_to_ktime(bdata->release_delay),
> HRTIMER_MODE_REL_HARD);
> out:
> - spin_unlock_irqrestore(&bdata->lock, flags);
> return IRQ_HANDLED;
> }
>
> @@ -1062,10 +1052,10 @@ static int gpio_keys_suspend(struct device *dev)
> if (error)
> return error;
> } else {
> - mutex_lock(&input->mutex);
> + guard(mutex)(&input->mutex);
> +
> if (input_device_enabled(input))
> gpio_keys_close(input);
> - mutex_unlock(&input->mutex);
> }
>
> return 0;
> @@ -1075,20 +1065,20 @@ static int gpio_keys_resume(struct device *dev)
> {
> struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
> struct input_dev *input = ddata->input;
> - int error = 0;
> + int error;
>
> if (device_may_wakeup(dev)) {
> gpio_keys_disable_wakeup(ddata);
> } else {
> - mutex_lock(&input->mutex);
> - if (input_device_enabled(input))
> + guard(mutex)(&input->mutex);
> +
> + if (input_device_enabled(input)) {
> error = gpio_keys_open(input);
> - mutex_unlock(&input->mutex);
> + if (error)
> + return error;
> + }
> }
>
> - if (error)
> - return error;
> -
> gpio_keys_report_state(ddata);
> return 0;
> }
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 06/17] Input: imx_keypad - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (4 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 05/17] Input: gpio-keys - switch to using cleanup functions Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 07/17] Input: ipaq-micro-keys - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
` (10 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/imx_keypad.c | 27 +++++++++++----------------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/drivers/input/keyboard/imx_keypad.c b/drivers/input/keyboard/imx_keypad.c
index e15a93619e82..b92268ddfd84 100644
--- a/drivers/input/keyboard/imx_keypad.c
+++ b/drivers/input/keyboard/imx_keypad.c
@@ -521,13 +521,11 @@ static int __maybe_unused imx_kbd_noirq_suspend(struct device *dev)
struct input_dev *input_dev = kbd->input_dev;
unsigned short reg_val = readw(kbd->mmio_base + KPSR);
- /* imx kbd can wake up system even clock is disabled */
- mutex_lock(&input_dev->mutex);
-
- if (input_device_enabled(input_dev))
- clk_disable_unprepare(kbd->clk);
-
- mutex_unlock(&input_dev->mutex);
+ scoped_guard(mutex, &input_dev->mutex) {
+ /* imx kbd can wake up system even clock is disabled */
+ if (input_device_enabled(input_dev))
+ clk_disable_unprepare(kbd->clk);
+ }
if (device_may_wakeup(&pdev->dev)) {
if (reg_val & KBD_STAT_KPKD)
@@ -547,23 +545,20 @@ static int __maybe_unused imx_kbd_noirq_resume(struct device *dev)
struct platform_device *pdev = to_platform_device(dev);
struct imx_keypad *kbd = platform_get_drvdata(pdev);
struct input_dev *input_dev = kbd->input_dev;
- int ret = 0;
+ int error;
if (device_may_wakeup(&pdev->dev))
disable_irq_wake(kbd->irq);
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (input_device_enabled(input_dev)) {
- ret = clk_prepare_enable(kbd->clk);
- if (ret)
- goto err_clk;
+ error = clk_prepare_enable(kbd->clk);
+ if (error)
+ return error;
}
-err_clk:
- mutex_unlock(&input_dev->mutex);
-
- return ret;
+ return 0;
}
static const struct dev_pm_ops imx_kbd_pm_ops = {
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 07/17] Input: ipaq-micro-keys - use guard notation when acquiring mutex and spinlock
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (5 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 06/17] Input: imx_keypad - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 08/17] Input: iqs62x-keys - use cleanup facility for fwnodes Dmitry Torokhov
` (9 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that locks are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/ipaq-micro-keys.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/drivers/input/keyboard/ipaq-micro-keys.c b/drivers/input/keyboard/ipaq-micro-keys.c
index 1d71dd79ffd2..58631bf7ce55 100644
--- a/drivers/input/keyboard/ipaq-micro-keys.c
+++ b/drivers/input/keyboard/ipaq-micro-keys.c
@@ -54,18 +54,18 @@ static void micro_key_receive(void *data, int len, unsigned char *msg)
static void micro_key_start(struct ipaq_micro_keys *keys)
{
- spin_lock(&keys->micro->lock);
+ guard(spinlock)(&keys->micro->lock);
+
keys->micro->key = micro_key_receive;
keys->micro->key_data = keys;
- spin_unlock(&keys->micro->lock);
}
static void micro_key_stop(struct ipaq_micro_keys *keys)
{
- spin_lock(&keys->micro->lock);
+ guard(spinlock)(&keys->micro->lock);
+
keys->micro->key = NULL;
keys->micro->key_data = NULL;
- spin_unlock(&keys->micro->lock);
}
static int micro_key_open(struct input_dev *input)
@@ -141,13 +141,11 @@ static int micro_key_resume(struct device *dev)
struct ipaq_micro_keys *keys = dev_get_drvdata(dev);
struct input_dev *input = keys->input;
- mutex_lock(&input->mutex);
+ guard(mutex)(&input->mutex);
if (input_device_enabled(input))
micro_key_start(keys);
- mutex_unlock(&input->mutex);
-
return 0;
}
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 08/17] Input: iqs62x-keys - use cleanup facility for fwnodes
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (6 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 07/17] Input: ipaq-micro-keys - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-26 22:43 ` Jeff LaBundy
2024-08-25 5:16 ` [PATCH 09/17] Input: lm8323 - use guard notation when acquiring mutexes Dmitry Torokhov
` (8 subsequent siblings)
16 siblings, 1 reply; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
Use __free(fwnode_handle) cleanup facility to ensure that references
to acquired fwnodes are dropped at appropriate times automatically.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/iqs62x-keys.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/drivers/input/keyboard/iqs62x-keys.c b/drivers/input/keyboard/iqs62x-keys.c
index 688d61244b5f..1315b0f0862f 100644
--- a/drivers/input/keyboard/iqs62x-keys.c
+++ b/drivers/input/keyboard/iqs62x-keys.c
@@ -45,7 +45,6 @@ struct iqs62x_keys_private {
static int iqs62x_keys_parse_prop(struct platform_device *pdev,
struct iqs62x_keys_private *iqs62x_keys)
{
- struct fwnode_handle *child;
unsigned int val;
int ret, i;
@@ -68,7 +67,8 @@ static int iqs62x_keys_parse_prop(struct platform_device *pdev,
}
for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
- child = device_get_named_child_node(&pdev->dev,
+ struct fwnode_handle *child __free(fwnode_handle) =
+ device_get_named_child_node(&pdev->dev,
iqs62x_switch_names[i]);
if (!child)
continue;
@@ -77,7 +77,6 @@ static int iqs62x_keys_parse_prop(struct platform_device *pdev,
if (ret) {
dev_err(&pdev->dev, "Failed to read switch code: %d\n",
ret);
- fwnode_handle_put(child);
return ret;
}
iqs62x_keys->switches[i].code = val;
@@ -91,8 +90,6 @@ static int iqs62x_keys_parse_prop(struct platform_device *pdev,
iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
IQS62X_EVENT_HALL_N_T :
IQS62X_EVENT_HALL_S_T);
-
- fwnode_handle_put(child);
}
return 0;
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH 08/17] Input: iqs62x-keys - use cleanup facility for fwnodes
2024-08-25 5:16 ` [PATCH 08/17] Input: iqs62x-keys - use cleanup facility for fwnodes Dmitry Torokhov
@ 2024-08-26 22:43 ` Jeff LaBundy
0 siblings, 0 replies; 22+ messages in thread
From: Jeff LaBundy @ 2024-08-26 22:43 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: linux-input, Michael Hennerich, Shawn Guo, Sascha Hauer,
Fabio Estevam, Laxman Dewangan, Thierry Reding, Hans de Goede,
Tony Lindgren, linux-kernel, imx, linux-arm-kernel, linux-tegra
Hi Dmitry,
On Sat, Aug 24, 2024 at 10:16:12PM -0700, Dmitry Torokhov wrote:
> Use __free(fwnode_handle) cleanup facility to ensure that references
> to acquired fwnodes are dropped at appropriate times automatically.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Reviewed-by: Jeff LaBundy <jeff@labundy.com>
> ---
> drivers/input/keyboard/iqs62x-keys.c | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/input/keyboard/iqs62x-keys.c b/drivers/input/keyboard/iqs62x-keys.c
> index 688d61244b5f..1315b0f0862f 100644
> --- a/drivers/input/keyboard/iqs62x-keys.c
> +++ b/drivers/input/keyboard/iqs62x-keys.c
> @@ -45,7 +45,6 @@ struct iqs62x_keys_private {
> static int iqs62x_keys_parse_prop(struct platform_device *pdev,
> struct iqs62x_keys_private *iqs62x_keys)
> {
> - struct fwnode_handle *child;
> unsigned int val;
> int ret, i;
>
> @@ -68,7 +67,8 @@ static int iqs62x_keys_parse_prop(struct platform_device *pdev,
> }
>
> for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
> - child = device_get_named_child_node(&pdev->dev,
> + struct fwnode_handle *child __free(fwnode_handle) =
> + device_get_named_child_node(&pdev->dev,
> iqs62x_switch_names[i]);
> if (!child)
> continue;
> @@ -77,7 +77,6 @@ static int iqs62x_keys_parse_prop(struct platform_device *pdev,
> if (ret) {
> dev_err(&pdev->dev, "Failed to read switch code: %d\n",
> ret);
> - fwnode_handle_put(child);
> return ret;
> }
> iqs62x_keys->switches[i].code = val;
> @@ -91,8 +90,6 @@ static int iqs62x_keys_parse_prop(struct platform_device *pdev,
> iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
> IQS62X_EVENT_HALL_N_T :
> IQS62X_EVENT_HALL_S_T);
> -
> - fwnode_handle_put(child);
> }
>
> return 0;
> --
> 2.46.0.295.g3b9ea8a38a-goog
>
Kind regards,
Jeff LaBundy
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH 09/17] Input: lm8323 - use guard notation when acquiring mutexes
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (7 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 08/17] Input: iqs62x-keys - use cleanup facility for fwnodes Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 10/17] Input: lpc32xx-keys - use guard notation when acquiring mutex Dmitry Torokhov
` (7 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/lm8323.c | 49 +++++++++++++++------------------
1 file changed, 22 insertions(+), 27 deletions(-)
diff --git a/drivers/input/keyboard/lm8323.c b/drivers/input/keyboard/lm8323.c
index cf67ba13477a..e26bf2956344 100644
--- a/drivers/input/keyboard/lm8323.c
+++ b/drivers/input/keyboard/lm8323.c
@@ -350,11 +350,11 @@ static int lm8323_configure(struct lm8323_chip *lm)
static void pwm_done(struct lm8323_pwm *pwm)
{
- mutex_lock(&pwm->lock);
+ guard(mutex)(&pwm->lock);
+
pwm->running = false;
if (pwm->desired_brightness != pwm->brightness)
schedule_work(&pwm->work);
- mutex_unlock(&pwm->lock);
}
/*
@@ -367,7 +367,7 @@ static irqreturn_t lm8323_irq(int irq, void *_lm)
u8 ints;
int i;
- mutex_lock(&lm->lock);
+ guard(mutex)(&lm->lock);
while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
if (likely(ints & INT_KEYPAD))
@@ -394,8 +394,6 @@ static irqreturn_t lm8323_irq(int irq, void *_lm)
}
}
- mutex_unlock(&lm->lock);
-
return IRQ_HANDLED;
}
@@ -445,7 +443,7 @@ static void lm8323_pwm_work(struct work_struct *work)
u16 pwm_cmds[3];
int num_cmds = 0;
- mutex_lock(&pwm->lock);
+ guard(mutex)(&pwm->lock);
/*
* Do nothing if we're already at the requested level,
@@ -454,7 +452,7 @@ static void lm8323_pwm_work(struct work_struct *work)
* finishes.
*/
if (pwm->running || pwm->desired_brightness == pwm->brightness)
- goto out;
+ return;
kill = (pwm->desired_brightness == 0);
up = (pwm->desired_brightness > pwm->brightness);
@@ -489,9 +487,6 @@ static void lm8323_pwm_work(struct work_struct *work)
lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
pwm->brightness = pwm->desired_brightness;
-
- out:
- mutex_unlock(&pwm->lock);
}
static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
@@ -500,9 +495,9 @@ static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
struct lm8323_chip *lm = pwm->chip;
- mutex_lock(&pwm->lock);
- pwm->desired_brightness = brightness;
- mutex_unlock(&pwm->lock);
+ scoped_guard(mutex, &pwm->lock) {
+ pwm->desired_brightness = brightness;
+ }
if (in_interrupt()) {
schedule_work(&pwm->work);
@@ -510,12 +505,12 @@ static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
/*
* Schedule PWM work as usual unless we are going into suspend
*/
- mutex_lock(&lm->lock);
- if (likely(!lm->pm_suspend))
- schedule_work(&pwm->work);
- else
- lm8323_pwm_work(&pwm->work);
- mutex_unlock(&lm->lock);
+ scoped_guard(mutex, &lm->lock) {
+ if (likely(!lm->pm_suspend))
+ schedule_work(&pwm->work);
+ else
+ lm8323_pwm_work(&pwm->work);
+ }
}
}
@@ -608,9 +603,9 @@ static ssize_t lm8323_set_disable(struct device *dev,
if (ret)
return ret;
- mutex_lock(&lm->lock);
+ guard(mutex)(&lm->lock);
+
lm->kp_enabled = !i;
- mutex_unlock(&lm->lock);
return count;
}
@@ -758,9 +753,9 @@ static int lm8323_suspend(struct device *dev)
irq_set_irq_wake(client->irq, 0);
disable_irq(client->irq);
- mutex_lock(&lm->lock);
- lm->pm_suspend = true;
- mutex_unlock(&lm->lock);
+ scoped_guard(mutex, &lm->lock) {
+ lm->pm_suspend = true;
+ }
for (i = 0; i < 3; i++)
if (lm->pwm[i].enabled)
@@ -775,9 +770,9 @@ static int lm8323_resume(struct device *dev)
struct lm8323_chip *lm = i2c_get_clientdata(client);
int i;
- mutex_lock(&lm->lock);
- lm->pm_suspend = false;
- mutex_unlock(&lm->lock);
+ scoped_guard(mutex, &lm->lock) {
+ lm->pm_suspend = false;
+ }
for (i = 0; i < 3; i++)
if (lm->pwm[i].enabled)
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 10/17] Input: lpc32xx-keys - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (8 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 09/17] Input: lm8323 - use guard notation when acquiring mutexes Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 11/17] Input: matrix_keypad - use guard notation when acquiring spinlock Dmitry Torokhov
` (6 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/lpc32xx-keys.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/drivers/input/keyboard/lpc32xx-keys.c b/drivers/input/keyboard/lpc32xx-keys.c
index 423035be86fb..2392e7ec3b19 100644
--- a/drivers/input/keyboard/lpc32xx-keys.c
+++ b/drivers/input/keyboard/lpc32xx-keys.c
@@ -262,7 +262,7 @@ static int lpc32xx_kscan_suspend(struct device *dev)
struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
struct input_dev *input = kscandat->input;
- mutex_lock(&input->mutex);
+ guard(mutex)(&input->mutex);
if (input_device_enabled(input)) {
/* Clear IRQ and disable clock */
@@ -270,7 +270,6 @@ static int lpc32xx_kscan_suspend(struct device *dev)
clk_disable_unprepare(kscandat->clk);
}
- mutex_unlock(&input->mutex);
return 0;
}
@@ -279,19 +278,20 @@ static int lpc32xx_kscan_resume(struct device *dev)
struct platform_device *pdev = to_platform_device(dev);
struct lpc32xx_kscan_drv *kscandat = platform_get_drvdata(pdev);
struct input_dev *input = kscandat->input;
- int retval = 0;
+ int error;
- mutex_lock(&input->mutex);
+ guard(mutex)(&input->mutex);
if (input_device_enabled(input)) {
/* Enable clock and clear IRQ */
- retval = clk_prepare_enable(kscandat->clk);
- if (retval == 0)
- writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
+ error = clk_prepare_enable(kscandat->clk);
+ if (error)
+ return error;
+
+ writel(1, LPC32XX_KS_IRQ(kscandat->kscan_base));
}
- mutex_unlock(&input->mutex);
- return retval;
+ return 0;
}
static DEFINE_SIMPLE_DEV_PM_OPS(lpc32xx_kscan_pm_ops, lpc32xx_kscan_suspend,
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 11/17] Input: matrix_keypad - use guard notation when acquiring spinlock
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (9 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 10/17] Input: lpc32xx-keys - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 12/17] Input: omap4-keypad - use guard notation when acquiring mutex Dmitry Torokhov
` (5 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that locks are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/matrix_keypad.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c
index 7a56f3d3aacd..bd763d704306 100644
--- a/drivers/input/keyboard/matrix_keypad.c
+++ b/drivers/input/keyboard/matrix_keypad.c
@@ -158,18 +158,17 @@ static void matrix_keypad_scan(struct work_struct *work)
activate_all_cols(pdata, true);
/* Enable IRQs again */
- spin_lock_irq(&keypad->lock);
- keypad->scan_pending = false;
- enable_row_irqs(keypad);
- spin_unlock_irq(&keypad->lock);
+ scoped_guard(spinlock_irq, &keypad->lock) {
+ keypad->scan_pending = false;
+ enable_row_irqs(keypad);
+ }
}
static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
{
struct matrix_keypad *keypad = id;
- unsigned long flags;
- spin_lock_irqsave(&keypad->lock, flags);
+ guard(spinlock_irqsave)(&keypad->lock);
/*
* See if another IRQ beaten us to it and scheduled the
@@ -185,7 +184,6 @@ static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
msecs_to_jiffies(keypad->pdata->debounce_ms));
out:
- spin_unlock_irqrestore(&keypad->lock, flags);
return IRQ_HANDLED;
}
@@ -209,9 +207,9 @@ static void matrix_keypad_stop(struct input_dev *dev)
{
struct matrix_keypad *keypad = input_get_drvdata(dev);
- spin_lock_irq(&keypad->lock);
- keypad->stopped = true;
- spin_unlock_irq(&keypad->lock);
+ scoped_guard(spinlock_irq, &keypad->lock) {
+ keypad->stopped = true;
+ }
flush_delayed_work(&keypad->work);
/*
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 12/17] Input: omap4-keypad - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (10 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 11/17] Input: matrix_keypad - use guard notation when acquiring spinlock Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 13/17] Input: pmic8xxx-keypad " Dmitry Torokhov
` (4 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/omap4-keypad.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/drivers/input/keyboard/omap4-keypad.c b/drivers/input/keyboard/omap4-keypad.c
index 040b340995d8..935c2b27b81c 100644
--- a/drivers/input/keyboard/omap4-keypad.c
+++ b/drivers/input/keyboard/omap4-keypad.c
@@ -144,7 +144,7 @@ static void omap4_keypad_scan_keys(struct omap4_keypad *keypad_data, u64 keys)
{
u64 changed;
- mutex_lock(&keypad_data->lock);
+ guard(mutex)(&keypad_data->lock);
changed = keys ^ keypad_data->keys;
@@ -158,8 +158,6 @@ static void omap4_keypad_scan_keys(struct omap4_keypad *keypad_data, u64 keys)
omap4_keypad_report_keys(keypad_data, changed & keys, true);
keypad_data->keys = keys;
-
- mutex_unlock(&keypad_data->lock);
}
/* Interrupt handlers */
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 13/17] Input: pmic8xxx-keypad - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (11 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 12/17] Input: omap4-keypad - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 14/17] Input: pxa27x_keypad " Dmitry Torokhov
` (3 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/pmic8xxx-keypad.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/drivers/input/keyboard/pmic8xxx-keypad.c b/drivers/input/keyboard/pmic8xxx-keypad.c
index 26a005f9ace3..35d1aa2a22a5 100644
--- a/drivers/input/keyboard/pmic8xxx-keypad.c
+++ b/drivers/input/keyboard/pmic8xxx-keypad.c
@@ -630,12 +630,10 @@ static int pmic8xxx_kp_suspend(struct device *dev)
if (device_may_wakeup(dev)) {
enable_irq_wake(kp->key_sense_irq);
} else {
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (input_device_enabled(input_dev))
pmic8xxx_kp_disable(kp);
-
- mutex_unlock(&input_dev->mutex);
}
return 0;
@@ -650,12 +648,10 @@ static int pmic8xxx_kp_resume(struct device *dev)
if (device_may_wakeup(dev)) {
disable_irq_wake(kp->key_sense_irq);
} else {
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (input_device_enabled(input_dev))
pmic8xxx_kp_enable(kp);
-
- mutex_unlock(&input_dev->mutex);
}
return 0;
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 14/17] Input: pxa27x_keypad - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (12 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 13/17] Input: pmic8xxx-keypad " Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 15/17] Input: spear-keyboard " Dmitry Torokhov
` (2 subsequent siblings)
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/pxa27x_keypad.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/input/keyboard/pxa27x_keypad.c b/drivers/input/keyboard/pxa27x_keypad.c
index 3724363d140e..38ec619aa359 100644
--- a/drivers/input/keyboard/pxa27x_keypad.c
+++ b/drivers/input/keyboard/pxa27x_keypad.c
@@ -682,7 +682,7 @@ static int pxa27x_keypad_resume(struct device *dev)
struct platform_device *pdev = to_platform_device(dev);
struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
struct input_dev *input_dev = keypad->input_dev;
- int ret = 0;
+ int error;
/*
* If the keypad is used as wake up source, the clock is not turned
@@ -691,19 +691,19 @@ static int pxa27x_keypad_resume(struct device *dev)
if (device_may_wakeup(&pdev->dev)) {
disable_irq_wake(keypad->irq);
} else {
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (input_device_enabled(input_dev)) {
/* Enable unit clock */
- ret = clk_prepare_enable(keypad->clk);
- if (!ret)
- pxa27x_keypad_config(keypad);
- }
+ error = clk_prepare_enable(keypad->clk);
+ if (error)
+ return error;
- mutex_unlock(&input_dev->mutex);
+ pxa27x_keypad_config(keypad);
+ }
}
- return ret;
+ return 0;
}
static DEFINE_SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 15/17] Input: spear-keyboard - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (13 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 14/17] Input: pxa27x_keypad " Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 16/17] Input: st-keyscan " Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 17/17] Input: tegra-kbc - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/spear-keyboard.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/drivers/input/keyboard/spear-keyboard.c b/drivers/input/keyboard/spear-keyboard.c
index 1df4feb8ba01..2d3f656e59dc 100644
--- a/drivers/input/keyboard/spear-keyboard.c
+++ b/drivers/input/keyboard/spear-keyboard.c
@@ -274,7 +274,7 @@ static int spear_kbd_suspend(struct device *dev)
struct input_dev *input_dev = kbd->input;
unsigned int rate = 0, mode_ctl_reg, val;
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
/* explicitly enable clock as we may program device */
clk_enable(kbd->clk);
@@ -315,8 +315,6 @@ static int spear_kbd_suspend(struct device *dev)
/* restore previous clk state */
clk_disable(kbd->clk);
- mutex_unlock(&input_dev->mutex);
-
return 0;
}
@@ -326,7 +324,7 @@ static int spear_kbd_resume(struct device *dev)
struct spear_kbd *kbd = platform_get_drvdata(pdev);
struct input_dev *input_dev = kbd->input;
- mutex_lock(&input_dev->mutex);
+ guard(mutex)(&input_dev->mutex);
if (device_may_wakeup(&pdev->dev)) {
if (kbd->irq_wake_enabled) {
@@ -342,8 +340,6 @@ static int spear_kbd_resume(struct device *dev)
if (input_device_enabled(input_dev))
writel_relaxed(kbd->mode_ctl_reg, kbd->io_base + MODE_CTL_REG);
- mutex_unlock(&input_dev->mutex);
-
return 0;
}
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 16/17] Input: st-keyscan - use guard notation when acquiring mutex
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (14 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 15/17] Input: spear-keyboard " Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-25 5:16 ` [PATCH 17/17] Input: tegra-kbc - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
16 siblings, 0 replies; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that mutexes are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/st-keyscan.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/drivers/input/keyboard/st-keyscan.c b/drivers/input/keyboard/st-keyscan.c
index 0d27324af809..e53ef4c670e4 100644
--- a/drivers/input/keyboard/st-keyscan.c
+++ b/drivers/input/keyboard/st-keyscan.c
@@ -216,14 +216,13 @@ static int keyscan_suspend(struct device *dev)
struct st_keyscan *keypad = platform_get_drvdata(pdev);
struct input_dev *input = keypad->input_dev;
- mutex_lock(&input->mutex);
+ guard(mutex)(&input->mutex);
if (device_may_wakeup(dev))
enable_irq_wake(keypad->irq);
else if (input_device_enabled(input))
keyscan_stop(keypad);
- mutex_unlock(&input->mutex);
return 0;
}
@@ -232,17 +231,19 @@ static int keyscan_resume(struct device *dev)
struct platform_device *pdev = to_platform_device(dev);
struct st_keyscan *keypad = platform_get_drvdata(pdev);
struct input_dev *input = keypad->input_dev;
- int retval = 0;
+ int error;
- mutex_lock(&input->mutex);
+ guard(mutex)(&input->mutex);
- if (device_may_wakeup(dev))
+ if (device_may_wakeup(dev)) {
disable_irq_wake(keypad->irq);
- else if (input_device_enabled(input))
- retval = keyscan_start(keypad);
+ } else if (input_device_enabled(input)) {
+ error = keyscan_start(keypad);
+ if (error)
+ return error;
+ }
- mutex_unlock(&input->mutex);
- return retval;
+ return 0;
}
static DEFINE_SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops,
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH 17/17] Input: tegra-kbc - use guard notation when acquiring mutex and spinlock
2024-08-25 5:16 [PATCH 00/17] Convert keyboard drivers to use new cleanup facilities Dmitry Torokhov
` (15 preceding siblings ...)
2024-08-25 5:16 ` [PATCH 16/17] Input: st-keyscan " Dmitry Torokhov
@ 2024-08-25 5:16 ` Dmitry Torokhov
2024-08-27 9:08 ` Thierry Reding
16 siblings, 1 reply; 22+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:16 UTC (permalink / raw)
To: linux-input
Cc: Michael Hennerich, Shawn Guo, Sascha Hauer, Fabio Estevam,
Laxman Dewangan, Thierry Reding, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
This makes the code more compact and error handling more robust
by ensuring that locks are released in all code paths when control
leaves critical section.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
drivers/input/keyboard/tegra-kbc.c | 45 +++++++++++++-----------------
1 file changed, 19 insertions(+), 26 deletions(-)
diff --git a/drivers/input/keyboard/tegra-kbc.c b/drivers/input/keyboard/tegra-kbc.c
index a1765ed8c825..204ba189807e 100644
--- a/drivers/input/keyboard/tegra-kbc.c
+++ b/drivers/input/keyboard/tegra-kbc.c
@@ -241,11 +241,10 @@ static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
static void tegra_kbc_keypress_timer(struct timer_list *t)
{
struct tegra_kbc *kbc = from_timer(kbc, t, timer);
- unsigned long flags;
u32 val;
unsigned int i;
- spin_lock_irqsave(&kbc->lock, flags);
+ guard(spinlock_irqsave)(&kbc->lock);
val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
if (val) {
@@ -270,17 +269,14 @@ static void tegra_kbc_keypress_timer(struct timer_list *t)
/* All keys are released so enable the keypress interrupt */
tegra_kbc_set_fifo_interrupt(kbc, true);
}
-
- spin_unlock_irqrestore(&kbc->lock, flags);
}
static irqreturn_t tegra_kbc_isr(int irq, void *args)
{
struct tegra_kbc *kbc = args;
- unsigned long flags;
u32 val;
- spin_lock_irqsave(&kbc->lock, flags);
+ guard(spinlock_irqsave)(&kbc->lock);
/*
* Quickly bail out & reenable interrupts if the fifo threshold
@@ -301,8 +297,6 @@ static irqreturn_t tegra_kbc_isr(int irq, void *args)
kbc->keypress_caused_wake = true;
}
- spin_unlock_irqrestore(&kbc->lock, flags);
-
return IRQ_HANDLED;
}
@@ -413,14 +407,13 @@ static int tegra_kbc_start(struct tegra_kbc *kbc)
static void tegra_kbc_stop(struct tegra_kbc *kbc)
{
- unsigned long flags;
u32 val;
- spin_lock_irqsave(&kbc->lock, flags);
- val = readl(kbc->mmio + KBC_CONTROL_0);
- val &= ~1;
- writel(val, kbc->mmio + KBC_CONTROL_0);
- spin_unlock_irqrestore(&kbc->lock, flags);
+ scoped_guard(spinlock_irqsave, &kbc->lock) {
+ val = readl(kbc->mmio + KBC_CONTROL_0);
+ val &= ~1;
+ writel(val, kbc->mmio + KBC_CONTROL_0);
+ }
disable_irq(kbc->irq);
del_timer_sync(&kbc->timer);
@@ -724,7 +717,8 @@ static int tegra_kbc_suspend(struct device *dev)
struct platform_device *pdev = to_platform_device(dev);
struct tegra_kbc *kbc = platform_get_drvdata(pdev);
- mutex_lock(&kbc->idev->mutex);
+ guard(mutex)(&kbc->idev->mutex);
+
if (device_may_wakeup(&pdev->dev)) {
disable_irq(kbc->irq);
del_timer_sync(&kbc->timer);
@@ -747,11 +741,9 @@ static int tegra_kbc_suspend(struct device *dev)
tegra_kbc_set_keypress_interrupt(kbc, true);
enable_irq(kbc->irq);
enable_irq_wake(kbc->irq);
- } else {
- if (input_device_enabled(kbc->idev))
- tegra_kbc_stop(kbc);
+ } else if (input_device_enabled(kbc->idev)) {
+ tegra_kbc_stop(kbc);
}
- mutex_unlock(&kbc->idev->mutex);
return 0;
}
@@ -760,9 +752,10 @@ static int tegra_kbc_resume(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct tegra_kbc *kbc = platform_get_drvdata(pdev);
- int err = 0;
+ int err;
+
+ guard(mutex)(&kbc->idev->mutex);
- mutex_lock(&kbc->idev->mutex);
if (device_may_wakeup(&pdev->dev)) {
disable_irq_wake(kbc->irq);
tegra_kbc_setup_wakekeys(kbc, false);
@@ -787,13 +780,13 @@ static int tegra_kbc_resume(struct device *dev)
input_report_key(kbc->idev, kbc->wakeup_key, 0);
input_sync(kbc->idev);
}
- } else {
- if (input_device_enabled(kbc->idev))
- err = tegra_kbc_start(kbc);
+ } else if (input_device_enabled(kbc->idev)) {
+ err = tegra_kbc_start(kbc);
+ if (err)
+ return err;
}
- mutex_unlock(&kbc->idev->mutex);
- return err;
+ return 0;
}
static DEFINE_SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops,
--
2.46.0.295.g3b9ea8a38a-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH 17/17] Input: tegra-kbc - use guard notation when acquiring mutex and spinlock
2024-08-25 5:16 ` [PATCH 17/17] Input: tegra-kbc - use guard notation when acquiring mutex and spinlock Dmitry Torokhov
@ 2024-08-27 9:08 ` Thierry Reding
0 siblings, 0 replies; 22+ messages in thread
From: Thierry Reding @ 2024-08-27 9:08 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: linux-input, Michael Hennerich, Shawn Guo, Sascha Hauer,
Fabio Estevam, Laxman Dewangan, Hans de Goede, Tony Lindgren,
Jeff LaBundy, linux-kernel, imx, linux-arm-kernel, linux-tegra
[-- Attachment #1: Type: text/plain, Size: 480 bytes --]
On Sat, Aug 24, 2024 at 10:16:21PM GMT, Dmitry Torokhov wrote:
> This makes the code more compact and error handling more robust
> by ensuring that locks are released in all code paths when control
> leaves critical section.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
> drivers/input/keyboard/tegra-kbc.c | 45 +++++++++++++-----------------
> 1 file changed, 19 insertions(+), 26 deletions(-)
Acked-by: Thierry Reding <treding@nvidia.com>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 22+ messages in thread