* [PATCH] Input: alps - use guard notation when acquiring mutex
@ 2024-08-25 5:30 Dmitry Torokhov
2024-08-25 20:13 ` Pali Rohár
2024-08-25 20:26 ` Markus Elfring
0 siblings, 2 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2024-08-25 5:30 UTC (permalink / raw)
To: linux-input; +Cc: Pali Rohár, Erick Archer, linux-kernel
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/mouse/alps.c | 48 +++++++++++++++++++++-----------------
1 file changed, 27 insertions(+), 21 deletions(-)
diff --git a/drivers/input/mouse/alps.c b/drivers/input/mouse/alps.c
index d5ef5a112d6f..4e37fc3f1a9e 100644
--- a/drivers/input/mouse/alps.c
+++ b/drivers/input/mouse/alps.c
@@ -1396,24 +1396,16 @@ static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
static DEFINE_MUTEX(alps_mutex);
-static void alps_register_bare_ps2_mouse(struct work_struct *work)
+static int alps_do_register_bare_ps2_mouse(struct alps_data *priv)
{
- struct alps_data *priv =
- container_of(work, struct alps_data, dev3_register_work.work);
struct psmouse *psmouse = priv->psmouse;
struct input_dev *dev3;
- int error = 0;
-
- mutex_lock(&alps_mutex);
-
- if (priv->dev3)
- goto out;
+ int error;
dev3 = input_allocate_device();
if (!dev3) {
psmouse_err(psmouse, "failed to allocate secondary device\n");
- error = -ENOMEM;
- goto out;
+ return -ENOMEM;
}
snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
@@ -1446,21 +1438,35 @@ static void alps_register_bare_ps2_mouse(struct work_struct *work)
psmouse_err(psmouse,
"failed to register secondary device: %d\n",
error);
- input_free_device(dev3);
- goto out;
+ goto err_free_input;
}
priv->dev3 = dev3;
+ return 0;
-out:
- /*
- * Save the error code so that we can detect that we
- * already tried to create the device.
- */
- if (error)
- priv->dev3 = ERR_PTR(error);
+err_free_input:
+ input_free_device(dev3);
+ return error;
+}
- mutex_unlock(&alps_mutex);
+static void alps_register_bare_ps2_mouse(struct work_struct *work)
+{
+ struct alps_data *priv = container_of(work, struct alps_data,
+ dev3_register_work.work);
+ int error;
+
+ guard(mutex)(&alps_mutex);
+
+ if (!priv->dev3) {
+ error = alps_do_register_bare_ps2_mouse(priv);
+ if (error) {
+ /*
+ * Save the error code so that we can detect that we
+ * already tried to create the device.
+ */
+ priv->dev3 = ERR_PTR(error);
+ }
+ }
}
static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
--
2.46.0.295.g3b9ea8a38a-goog
--
Dmitry
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] Input: alps - use guard notation when acquiring mutex
2024-08-25 5:30 [PATCH] Input: alps - use guard notation when acquiring mutex Dmitry Torokhov
@ 2024-08-25 20:13 ` Pali Rohár
2024-08-29 17:57 ` Dmitry Torokhov
2024-08-25 20:26 ` Markus Elfring
1 sibling, 1 reply; 6+ messages in thread
From: Pali Rohár @ 2024-08-25 20:13 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linux-input, Erick Archer, linux-kernel
On Saturday 24 August 2024 22:30:57 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>
> ---
> drivers/input/mouse/alps.c | 48 +++++++++++++++++++++-----------------
> 1 file changed, 27 insertions(+), 21 deletions(-)
>
> diff --git a/drivers/input/mouse/alps.c b/drivers/input/mouse/alps.c
> index d5ef5a112d6f..4e37fc3f1a9e 100644
> --- a/drivers/input/mouse/alps.c
> +++ b/drivers/input/mouse/alps.c
> @@ -1396,24 +1396,16 @@ static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
>
> static DEFINE_MUTEX(alps_mutex);
>
> -static void alps_register_bare_ps2_mouse(struct work_struct *work)
> +static int alps_do_register_bare_ps2_mouse(struct alps_data *priv)
> {
> - struct alps_data *priv =
> - container_of(work, struct alps_data, dev3_register_work.work);
> struct psmouse *psmouse = priv->psmouse;
> struct input_dev *dev3;
> - int error = 0;
> -
> - mutex_lock(&alps_mutex);
> -
> - if (priv->dev3)
> - goto out;
> + int error;
>
> dev3 = input_allocate_device();
> if (!dev3) {
> psmouse_err(psmouse, "failed to allocate secondary device\n");
> - error = -ENOMEM;
> - goto out;
> + return -ENOMEM;
> }
>
> snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
> @@ -1446,21 +1438,35 @@ static void alps_register_bare_ps2_mouse(struct work_struct *work)
> psmouse_err(psmouse,
> "failed to register secondary device: %d\n",
> error);
> - input_free_device(dev3);
> - goto out;
> + goto err_free_input;
> }
>
> priv->dev3 = dev3;
> + return 0;
>
> -out:
> - /*
> - * Save the error code so that we can detect that we
> - * already tried to create the device.
> - */
> - if (error)
> - priv->dev3 = ERR_PTR(error);
> +err_free_input:
> + input_free_device(dev3);
> + return error;
> +}
>
> - mutex_unlock(&alps_mutex);
> +static void alps_register_bare_ps2_mouse(struct work_struct *work)
> +{
> + struct alps_data *priv = container_of(work, struct alps_data,
> + dev3_register_work.work);
> + int error;
> +
> + guard(mutex)(&alps_mutex);
> +
> + if (!priv->dev3) {
> + error = alps_do_register_bare_ps2_mouse(priv);
> + if (error) {
> + /*
> + * Save the error code so that we can detect that we
> + * already tried to create the device.
> + */
> + priv->dev3 = ERR_PTR(error);
> + }
> + }
> }
>
> static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
> --
> 2.46.0.295.g3b9ea8a38a-goog
>
>
> --
> Dmitry
Hello, I'm not familiar with new guards and their usage. But if this is
a preferred way for handling mutexes then go ahead.
I just looked at the code and I do not see any obvious error neither in
old nor in new version.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Input: alps - use guard notation when acquiring mutex
2024-08-25 5:30 [PATCH] Input: alps - use guard notation when acquiring mutex Dmitry Torokhov
2024-08-25 20:13 ` Pali Rohár
@ 2024-08-25 20:26 ` Markus Elfring
1 sibling, 0 replies; 6+ messages in thread
From: Markus Elfring @ 2024-08-25 20:26 UTC (permalink / raw)
To: Dmitry Torokhov, linux-input; +Cc: LKML, Erick Archer, Pali Rohár
> This makes the code …
Would you ever like to improve such a change description with imperative wordings?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.11-rc5#n94
Regards,
Markus
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Input: alps - use guard notation when acquiring mutex
2024-08-25 20:13 ` Pali Rohár
@ 2024-08-29 17:57 ` Dmitry Torokhov
2024-09-05 15:44 ` Pali Rohár
0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Torokhov @ 2024-08-29 17:57 UTC (permalink / raw)
To: Pali Rohár; +Cc: linux-input, Erick Archer, linux-kernel
Hi Pali,
On Sun, Aug 25, 2024 at 10:13:47PM +0200, Pali Rohár wrote:
>
> Hello, I'm not familiar with new guards and their usage. But if this is
> a preferred way for handling mutexes then go ahead.
>
> I just looked at the code and I do not see any obvious error neither in
> old nor in new version.
Is this a Reviewed-by or Acked-by? If neither that is fine too, just
want to make sure.
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Input: alps - use guard notation when acquiring mutex
2024-08-29 17:57 ` Dmitry Torokhov
@ 2024-09-05 15:44 ` Pali Rohár
2024-09-06 5:51 ` Dmitry Torokhov
0 siblings, 1 reply; 6+ messages in thread
From: Pali Rohár @ 2024-09-05 15:44 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linux-input, Erick Archer, linux-kernel
On Thursday 29 August 2024 10:57:41 Dmitry Torokhov wrote:
> Hi Pali,
>
> On Sun, Aug 25, 2024 at 10:13:47PM +0200, Pali Rohár wrote:
> >
> > Hello, I'm not familiar with new guards and their usage. But if this is
> > a preferred way for handling mutexes then go ahead.
> >
> > I just looked at the code and I do not see any obvious error neither in
> > old nor in new version.
>
> Is this a Reviewed-by or Acked-by? If neither that is fine too, just
> want to make sure.
>
> Thanks.
>
> --
> Dmitry
Hello, I have looked at it again, and you can add my:
Acked-by: Pali Rohár <pali@kernel.org>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Input: alps - use guard notation when acquiring mutex
2024-09-05 15:44 ` Pali Rohár
@ 2024-09-06 5:51 ` Dmitry Torokhov
0 siblings, 0 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2024-09-06 5:51 UTC (permalink / raw)
To: Pali Rohár; +Cc: linux-input, Erick Archer, linux-kernel
On Thu, Sep 05, 2024 at 05:44:14PM +0200, Pali Rohár wrote:
> On Thursday 29 August 2024 10:57:41 Dmitry Torokhov wrote:
> > Hi Pali,
> >
> > On Sun, Aug 25, 2024 at 10:13:47PM +0200, Pali Rohár wrote:
> > >
> > > Hello, I'm not familiar with new guards and their usage. But if this is
> > > a preferred way for handling mutexes then go ahead.
> > >
> > > I just looked at the code and I do not see any obvious error neither in
> > > old nor in new version.
> >
> > Is this a Reviewed-by or Acked-by? If neither that is fine too, just
> > want to make sure.
> >
> > Thanks.
> >
> > --
> > Dmitry
>
> Hello, I have looked at it again, and you can add my:
> Acked-by: Pali Rohár <pali@kernel.org>
Thanks Pali.
--
Dmitry
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-09-06 5:52 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-25 5:30 [PATCH] Input: alps - use guard notation when acquiring mutex Dmitry Torokhov
2024-08-25 20:13 ` Pali Rohár
2024-08-29 17:57 ` Dmitry Torokhov
2024-09-05 15:44 ` Pali Rohár
2024-09-06 5:51 ` Dmitry Torokhov
2024-08-25 20:26 ` Markus Elfring
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).