Skip to content

Commit d5322d5

Browse files
committed
Input: alps - use guard notation when acquiring mutex
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. Acked-by: Pali Rohár <pali@kernel.org> Link: https://lore.kernel.org/r/ZsrBkWIpyEqzClUG@google.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 8e028d6 commit d5322d5

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

drivers/input/mouse/alps.c

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,24 +1396,16 @@ static bool alps_is_valid_package_ss4_v2(struct psmouse *psmouse)
13961396

13971397
static DEFINE_MUTEX(alps_mutex);
13981398

1399-
static void alps_register_bare_ps2_mouse(struct work_struct *work)
1399+
static int alps_do_register_bare_ps2_mouse(struct alps_data *priv)
14001400
{
1401-
struct alps_data *priv =
1402-
container_of(work, struct alps_data, dev3_register_work.work);
14031401
struct psmouse *psmouse = priv->psmouse;
14041402
struct input_dev *dev3;
1405-
int error = 0;
1406-
1407-
mutex_lock(&alps_mutex);
1408-
1409-
if (priv->dev3)
1410-
goto out;
1403+
int error;
14111404

14121405
dev3 = input_allocate_device();
14131406
if (!dev3) {
14141407
psmouse_err(psmouse, "failed to allocate secondary device\n");
1415-
error = -ENOMEM;
1416-
goto out;
1408+
return -ENOMEM;
14171409
}
14181410

14191411
snprintf(priv->phys3, sizeof(priv->phys3), "%s/%s",
@@ -1446,21 +1438,35 @@ static void alps_register_bare_ps2_mouse(struct work_struct *work)
14461438
psmouse_err(psmouse,
14471439
"failed to register secondary device: %d\n",
14481440
error);
1449-
input_free_device(dev3);
1450-
goto out;
1441+
goto err_free_input;
14511442
}
14521443

14531444
priv->dev3 = dev3;
1445+
return 0;
14541446

1455-
out:
1456-
/*
1457-
* Save the error code so that we can detect that we
1458-
* already tried to create the device.
1459-
*/
1460-
if (error)
1461-
priv->dev3 = ERR_PTR(error);
1447+
err_free_input:
1448+
input_free_device(dev3);
1449+
return error;
1450+
}
14621451

1463-
mutex_unlock(&alps_mutex);
1452+
static void alps_register_bare_ps2_mouse(struct work_struct *work)
1453+
{
1454+
struct alps_data *priv = container_of(work, struct alps_data,
1455+
dev3_register_work.work);
1456+
int error;
1457+
1458+
guard(mutex)(&alps_mutex);
1459+
1460+
if (!priv->dev3) {
1461+
error = alps_do_register_bare_ps2_mouse(priv);
1462+
if (error) {
1463+
/*
1464+
* Save the error code so that we can detect that we
1465+
* already tried to create the device.
1466+
*/
1467+
priv->dev3 = ERR_PTR(error);
1468+
}
1469+
}
14641470
}
14651471

14661472
static void alps_report_bare_ps2_packet(struct psmouse *psmouse,

0 commit comments

Comments
 (0)