From patchwork Wed Sep 4 04:30:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 825436 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DD1318C36; Wed, 4 Sep 2024 04:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424278; cv=none; b=Om8eZeKyu263+FzBkeWInyzwHleiKLfY6dJdtaa67lccBbtEMyRsdTQaa6Vi3+5EXJSTMpXjALksaxa8Ijm7VPi++/rYPT4xanoMW2gbnNfQSXtDkCbJtRNyhVhZ/e/sKe/Mx+WPu/yw23Uj18kc1ikD6bTULF060XtpcD+Pikc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424278; c=relaxed/simple; bh=ZJAi8VHy7qSDD1/8e5V67cLxULpG+erGpInrDT6ftBM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hqjdLe6x73gAx9A1aR5AUgEq7Kw8+G6iKVJ0IQ40zJssGDUjG0WmT/L9Q/0DRs+n2XjoTNHzfxCstBwulLwhfEAGgvO/1wRxCf17JMgG0do23iEpM+5oTKI2wAF1eSjk+1JVm1WYUgO5NoA2LiRMlElYTwpqv8YL6urm8hEbEjY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FKLPb66H; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FKLPb66H" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1fc47abc040so3068215ad.0; Tue, 03 Sep 2024 21:31:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725424276; x=1726029076; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qLlgku3anJMbbsPu9u0QAuCB6pezMxOtz2uO45yg+go=; b=FKLPb66H3Qjb4iBcf0uJUjbsImrPYpdyYr1nXaH85pkPWrcPcluin07lOXVOBoKBIf BXrgqaeC1pqB60IYpVG6JNWuGaeXWblWZOrmqO+5zo1iAB5XFI7Ry++9Gr7nglx1Yp7a JKyShrEvaNRcpoDfiHUhc0LaNBnx/YWwtaxZvREgiPpaMN8UQ04g9cdDjM8pR9g5CQy4 3rTAOI/UaDNEH4J+0omHEYcd2AAQ4ypG2qDQAFWwsWEtWTqQgwwIx0Q3GZU3DgQrudcY MbvVhwwpW/kTHVAG4zE4LUcE6YWc39UsX2zE92YAeqWnpqgJgDd7HSd24A8FvXgeem3T rj5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725424276; x=1726029076; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qLlgku3anJMbbsPu9u0QAuCB6pezMxOtz2uO45yg+go=; b=m7VFcIrj0v9BYG4ipdxiqe/ZZQcNGS6rbkJWcbSINpbViJyfkSj7TMS2iV6T150SjV Mx4I9nhfmAwrPwOmnkFhkGuv9Rziv3GAk6RKk+rhCFB4AW9ArVTNymR6aiv4G47vpI03 L5ihpAbSPLYF/5SvepXeCag5TOkRtcl/F2Jj597cPRdFO2Z/tFyqS/kEZvrytKN6aIkR j4Oc7a72ORurcjcsRh86QcPI/9jgB/auJlt4khGrQQGPmb6fnGmXe0wVlR20v3uDtNuO vyHS+TFvWR12sQbpHQ2pBHzzcpUIXISflvt5sRJ5AqhVZRxs1pcXZ2l5WKxWyuAwapMq RZzQ== X-Forwarded-Encrypted: i=1; AJvYcCV5FhoymsS/WJRUrIS17fuTg5Nig4Qw8CbZLlj0XEJcK4qr1GeT/2V/HlnIP4maXIHorJ1OkU0LoWSUz6s=@vger.kernel.org X-Gm-Message-State: AOJu0YwparXU2E6UiHoBH1aKyGPihSt5O7AootyMZndelliXGlspMJ1q osWlJf5jDwlCR0YkfLt25++XXpnwzk0N8evm9QKh6Pgp50ZOoWowlp7asQ== X-Google-Smtp-Source: AGHT+IHWVnwJuRlqZZFMv5HEQK4dSHyx01u9Q2WTkQJpZCoausD/W6CmZ4U2SjUpGvpw6XSEDdsAFw== X-Received: by 2002:a17:902:c94f:b0:205:7829:9d83 with SMTP id d9443c01a7336-20578299e50mr57313105ad.38.1725424275938; Tue, 03 Sep 2024 21:31:15 -0700 (PDT) Received: from dtor-ws.sjc.corp.google.com ([2620:15c:9d:2:e682:e3dc:908:eef0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea392e3sm5503555ad.135.2024.09.03.21.31.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 21:31:15 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Erick Archer , Christophe JAILLET , linux-kernel@vger.kernel.org Subject: [PATCH 2/6] Input: gamecon - use guard notation when acquiring mutex Date: Tue, 3 Sep 2024 21:30:59 -0700 Message-ID: <20240904043104.1030257-3-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240904043104.1030257-1-dmitry.torokhov@gmail.com> References: <20240904043104.1030257-1-dmitry.torokhov@gmail.com> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using guard notation 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 --- drivers/input/joystick/gamecon.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/drivers/input/joystick/gamecon.c b/drivers/input/joystick/gamecon.c index c38de3094553..968c0e653f2e 100644 --- a/drivers/input/joystick/gamecon.c +++ b/drivers/input/joystick/gamecon.c @@ -765,33 +765,31 @@ static void gc_timer(struct timer_list *t) static int gc_open(struct input_dev *dev) { struct gc *gc = input_get_drvdata(dev); - int err; - err = mutex_lock_interruptible(&gc->mutex); - if (err) - return err; + scoped_guard(mutex_intr, &gc->mutex) { + if (!gc->used++) { + parport_claim(gc->pd); + parport_write_control(gc->pd->port, 0x04); + mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME); + } - if (!gc->used++) { - parport_claim(gc->pd); - parport_write_control(gc->pd->port, 0x04); - mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME); + return 0; } - mutex_unlock(&gc->mutex); - return 0; + return -EINTR; } static void gc_close(struct input_dev *dev) { struct gc *gc = input_get_drvdata(dev); - mutex_lock(&gc->mutex); + guard(mutex)(&gc->mutex); + if (!--gc->used) { del_timer_sync(&gc->timer); parport_write_control(gc->pd->port, 0x00); parport_release(gc->pd); } - mutex_unlock(&gc->mutex); } static int gc_setup_pad(struct gc *gc, int idx, int pad_type) From patchwork Wed Sep 4 04:31:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 825435 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC0548289A; Wed, 4 Sep 2024 04:31:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424280; cv=none; b=bGWASIg1XlNBO9W5SnC+noAQhjQCwhiuLxwyd+85guSZXL214QQ9Zy4rT3uFUA1HEHl0Ii/43Gg34u/6l3CypezxCal4RGh6u63dYJa0SlCXRd3ThUE43ZGJ0X/lwn32NdLE/isKBSANlRApupRMx3VshEBqSZdgjdiBqYnXoeE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424280; c=relaxed/simple; bh=r0cxrpq12Wc5JLPO0bEEQNp8b1UwZh2OO5LdZdjrSyc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DupWFfUtA8okirmPWXMz9MidWc7CeIanRNSlONPoVJVxEPXEyKxqPrp72LWmY/nLLRZ2yVdVoAUO/JiKM+pt3GFx27hnabZAbh3E9NZOc/7W8DRY2DZERAW8o9GgL2e85F+pwJOVEJ0y1JmGA/fbKlWWHkiN2IKSNR1MDH5kxQE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dUgCi5J5; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dUgCi5J5" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7163489149eso4105794a12.1; Tue, 03 Sep 2024 21:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725424278; x=1726029078; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=j+gNKYsvHuXnjKEvzIZIgUJ9/UmMPbrYSkvOAUKFKPM=; b=dUgCi5J50/Zb+RFh749b3Ny3avbU08tkXDFhlat+VdsNQHAqKAvNkuIuQ4zeT5/HJt 9SsGFaO2prHnQt3e+81IDiQXbAbTJtlY7dRTOitUVkP8jZm1Y1oDv+ud/7L7b5vnbLX2 udSJMWC+RzMWYClSCo/uNR5gseptHXdIDbtcsEjmP0zQM1e06Z/IpeD8CXMj/v8YUXAE /HHLNKXfUU79v7Sk0fJ+59F27NaasBtF20IfHF04DI64m/c5fkrramKVXKbLvKehzTQz VXgDFyTVy1rcbL4/tqC64fwl8HbZou8ZkxplNxWa63/foEwdUupFBrLj5whzx050zWbA 9e9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725424278; x=1726029078; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j+gNKYsvHuXnjKEvzIZIgUJ9/UmMPbrYSkvOAUKFKPM=; b=VTk5o9lVxGZ8hXPb3OK903Ld8X+vuFeHM61rA0LRxXvWpltncUARhDgTES6c6yQhGj /l3wQ528hmRbM04t9R36kUxA3iDQeyMVWI60Np/6AQbGNKKTjQPtclZVOH/UTRfFCHpl zIkJoYkvQB4yRU7kp+UhdzjUME+3l9Q+PHyrwvfrmYTFjGdqKNWajMwywzVNpKbQGSSQ YA+ux4KErSITwH/WpkhLjTXgFy7BoAjZDD5EssqR1fICp6jdbfEdQNuVpLipQHueVO/q sxBvJIH6iQiI5yfn4oEX6qp4lkEB+/4J3+P9QlTxDLI0T3eLpshIHtC7pmx4rH+rJnt4 1yxQ== X-Forwarded-Encrypted: i=1; AJvYcCVSQDUfCVvdnZNdRc3q3R6fp6SI61/no+pu5Y2x+qA31TjLJkZ0NahbZQN94TQTCyQ6Ingx4PVRKb90Wnc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6UhHpETdJJvGRv2Ffky8TYkz0Dx2gAwowjrvAzEquGNiwhP/M MU/0zPWO2seIFen6qbn9lmLheHUFI99Gps3eXquK4k5UIUxZgjMyw4/aqw== X-Google-Smtp-Source: AGHT+IFKizNM26PpIpqCOHP9ptn1AvEhZFbz0Y25ZyMu/uNJhRLNs6CGsrZEbrGHhtJjKSTRyFST6w== X-Received: by 2002:a05:6a20:d04a:b0:1ca:cccd:4a1c with SMTP id adf61e73a8af0-1cecdfe0a64mr13978391637.43.1725424277970; Tue, 03 Sep 2024 21:31:17 -0700 (PDT) Received: from dtor-ws.sjc.corp.google.com ([2620:15c:9d:2:e682:e3dc:908:eef0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea392e3sm5503555ad.135.2024.09.03.21.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 21:31:17 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Erick Archer , Christophe JAILLET , linux-kernel@vger.kernel.org Subject: [PATCH 4/6] Input: n64joy - use guard notation when acquiring mutex Date: Tue, 3 Sep 2024 21:31:01 -0700 Message-ID: <20240904043104.1030257-5-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240904043104.1030257-1-dmitry.torokhov@gmail.com> References: <20240904043104.1030257-1-dmitry.torokhov@gmail.com> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using guard notation 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 --- drivers/input/joystick/n64joy.c | 35 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/drivers/input/joystick/n64joy.c b/drivers/input/joystick/n64joy.c index b0986d2195d6..c344dbc0c493 100644 --- a/drivers/input/joystick/n64joy.c +++ b/drivers/input/joystick/n64joy.c @@ -191,35 +191,32 @@ static void n64joy_poll(struct timer_list *t) static int n64joy_open(struct input_dev *dev) { struct n64joy_priv *priv = input_get_drvdata(dev); - int err; - - err = mutex_lock_interruptible(&priv->n64joy_mutex); - if (err) - return err; - - if (!priv->n64joy_opened) { - /* - * We could use the vblank irq, but it's not important if - * the poll point slightly changes. - */ - timer_setup(&priv->timer, n64joy_poll, 0); - mod_timer(&priv->timer, jiffies + msecs_to_jiffies(16)); - } - priv->n64joy_opened++; + scoped_guard(mutex_intr, &priv->n64joy_mutex) { + if (!priv->n64joy_opened) { + /* + * We could use the vblank irq, but it's not important + * if the poll point slightly changes. + */ + timer_setup(&priv->timer, n64joy_poll, 0); + mod_timer(&priv->timer, jiffies + msecs_to_jiffies(16)); + } - mutex_unlock(&priv->n64joy_mutex); - return err; + priv->n64joy_opened++; + return 0; + } + + return -EINTR; } static void n64joy_close(struct input_dev *dev) { struct n64joy_priv *priv = input_get_drvdata(dev); - mutex_lock(&priv->n64joy_mutex); + guard(mutex)(&priv->n64joy_mutex); + if (!--priv->n64joy_opened) del_timer_sync(&priv->timer); - mutex_unlock(&priv->n64joy_mutex); } static const u64 __initconst scandata[] ____cacheline_aligned = { From patchwork Wed Sep 4 04:31:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 825434 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8230884E04; Wed, 4 Sep 2024 04:31:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424283; cv=none; b=lz6hf88VCBUwqTpmZCnIrfUYP8JVgwnweSCsQTdl5bih6KE6ag7KmFf8quZwtsJTJQ44QUiXwsTWO7NSVln9qBZ9KflCkCBqy659QCqRhxlIAskwtofASISrVHJ6l/HDctUaFEliJ1b+tE3HtqsYVJKZXtZUgn8eQ0cdkMfmRXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424283; c=relaxed/simple; bh=0LOtEDxqgMC7wo/o2yyIbKIOxPtlr40fkEONHzYmrbg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fHHFYOc++IKuTmFQdoiFNckP7Pcp22aJmCsAt6mP6+LXcJvreapUXytHeqYZ6B1aInlJjsIJ3/F39qyHEHDeAZUbKQDgx+gfuo704qqwH2EhMqWoq49wD4a4OHR3HVtMstsixtH5IXozJrG5VFA+QpIo25/3qZ+JgUjT9aeHkTU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CcQLUdSK; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CcQLUdSK" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7d4ed6158bcso1321235a12.1; Tue, 03 Sep 2024 21:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725424280; x=1726029080; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0BhurDw2g3u6eFgUPXTkTmmYE1RtoDZNWEQeOaQogBU=; b=CcQLUdSK0icp+Rpw57nOnBb+LCpl5xUWTozQw3mnrw8ct9Q7oPNLVpbnPKd3deVhFL LV+tguBZa2cAW4kDQhq8wbR/BQIJVea+zTibPp5a9JYtjXBsmy7ve2y6S2NJGp6F3DlC CMkKRxe1TAEsrdq1ddXLSnbfvv9vUsKMr48Kmc9SDpp/yNooq9EAVFLlFdISFvcCEzcq KGrNNmweCE8ijledqxfr71Q3L5Dib1d/2/W/M1lvC0aDMoKlRPpEzKn+2r5YvRn5Hc9z nvjRltYVVlpjUrDL6x9hoSeqHU/ZpWLW5RrXxHMvDHlXalzuRNuSiM1byzNuqmcN4u0I qYbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725424280; x=1726029080; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0BhurDw2g3u6eFgUPXTkTmmYE1RtoDZNWEQeOaQogBU=; b=t5gu74bzd3YHSMJAtR8xLgtTWnopdbwrSyXv4CwUy7DlA4bTtfNHoDbzWpf7bjikUM HGlo6GMrkGwZTaH/6Wo4QAxh3P00mXxIFgOB02cRv5fDK8eptkXtI+Sc0szlJwKKZyg2 iQwIkvgvXcZMYXn8Ai1bPJ45RE/gpHEyuxumBAFEqXkHKxTLGFRQrKzCms2iqoim55Kg Vw2sZ4o5U7ONTrlsGEqJvTjYwlqp2j3q1+TnyfThDsJ9GkEtV68xtdMeh+qSVQJQBkT0 bcs4e+HroNqGux+KTu3OmYkVkukr8r6IBLQZpZrOO8iWECniFhvXo+WTs80IHwyHUZKu Ay1A== X-Forwarded-Encrypted: i=1; AJvYcCXujthEPw3YuYodohTdsgeKF12fR1WyExehMDxbb2ryNQTfr/ncxv6Zhsav9p7s7hQsVCh7pmUsdEuNEjE=@vger.kernel.org X-Gm-Message-State: AOJu0YxIguSHq4V0SCa+E0tj1VGSWxY0gXRFY5uLEU+w/j3MPPTPuma+ yKu4FlCUZkFa8UeWUXmtm6okxPY7Q/kOAwEzjT+M33JCVIcuTEyDCBV4Qg== X-Google-Smtp-Source: AGHT+IEYg1nJX27vH3Xy+K67QrvM81/pMR2nK7XP0mMhMyQCDtH5uVgKMvBxNaUGTQMnciMCx8QiYg== X-Received: by 2002:a05:6a21:9614:b0:1c3:b234:5f54 with SMTP id adf61e73a8af0-1cece5e2846mr11941713637.48.1725424280140; Tue, 03 Sep 2024 21:31:20 -0700 (PDT) Received: from dtor-ws.sjc.corp.google.com ([2620:15c:9d:2:e682:e3dc:908:eef0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206aea392e3sm5503555ad.135.2024.09.03.21.31.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 21:31:19 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Erick Archer , Christophe JAILLET , linux-kernel@vger.kernel.org Subject: [PATCH 6/6] Input: xpad - use guard notation when acquiring mutex and spinlock Date: Tue, 3 Sep 2024 21:31:03 -0700 Message-ID: <20240904043104.1030257-7-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240904043104.1030257-1-dmitry.torokhov@gmail.com> References: <20240904043104.1030257-1-dmitry.torokhov@gmail.com> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using guard notation 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 --- drivers/input/joystick/xpad.c | 99 ++++++++++++----------------------- 1 file changed, 34 insertions(+), 65 deletions(-) diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c index 4eda18f4f46e..3e61df927277 100644 --- a/drivers/input/joystick/xpad.c +++ b/drivers/input/joystick/xpad.c @@ -1289,9 +1289,8 @@ static void xpad_irq_out(struct urb *urb) struct device *dev = &xpad->intf->dev; int status = urb->status; int error; - unsigned long flags; - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); switch (status) { case 0: @@ -1325,8 +1324,6 @@ static void xpad_irq_out(struct urb *urb) xpad->irq_out_active = false; } } - - spin_unlock_irqrestore(&xpad->odata_lock, flags); } static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad, @@ -1391,10 +1388,8 @@ static int xpad_inquiry_pad_presence(struct usb_xpad *xpad) { struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_CMD_IDX]; - unsigned long flags; - int retval; - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); packet->data[0] = 0x08; packet->data[1] = 0x00; @@ -1413,17 +1408,12 @@ static int xpad_inquiry_pad_presence(struct usb_xpad *xpad) /* Reset the sequence so we send out presence first */ xpad->last_out_packet = -1; - retval = xpad_try_sending_next_out_packet(xpad); - - spin_unlock_irqrestore(&xpad->odata_lock, flags); - - return retval; + return xpad_try_sending_next_out_packet(xpad); } static int xpad_start_xbox_one(struct usb_xpad *xpad) { - unsigned long flags; - int retval; + int error; if (usb_ifnum_to_if(xpad->udev, GIP_WIRED_INTF_AUDIO)) { /* @@ -1432,15 +1422,15 @@ static int xpad_start_xbox_one(struct usb_xpad *xpad) * Controller for Series X|S (0x20d6:0x200e) to report the * guide button. */ - retval = usb_set_interface(xpad->udev, - GIP_WIRED_INTF_AUDIO, 0); - if (retval) + error = usb_set_interface(xpad->udev, + GIP_WIRED_INTF_AUDIO, 0); + if (error) dev_warn(&xpad->dev->dev, "unable to disable audio interface: %d\n", - retval); + error); } - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); /* * Begin the init sequence by attempting to send a packet. @@ -1448,16 +1438,11 @@ static int xpad_start_xbox_one(struct usb_xpad *xpad) * sending any packets from the output ring. */ xpad->init_seq = 0; - retval = xpad_try_sending_next_out_packet(xpad); - - spin_unlock_irqrestore(&xpad->odata_lock, flags); - - return retval; + return xpad_try_sending_next_out_packet(xpad); } static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num) { - unsigned long flags; struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_CMD_IDX]; static const u8 mode_report_ack[] = { @@ -1465,7 +1450,7 @@ static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num) 0x00, GIP_CMD_VIRTUAL_KEY, GIP_OPT_INTERNAL, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 }; - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); packet->len = sizeof(mode_report_ack); memcpy(packet->data, mode_report_ack, packet->len); @@ -1475,8 +1460,6 @@ static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num) /* Reset the sequence so we send out the ack now */ xpad->last_out_packet = -1; xpad_try_sending_next_out_packet(xpad); - - spin_unlock_irqrestore(&xpad->odata_lock, flags); } #ifdef CONFIG_JOYSTICK_XPAD_FF @@ -1486,8 +1469,6 @@ static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX]; __u16 strong; __u16 weak; - int retval; - unsigned long flags; if (effect->type != FF_RUMBLE) return 0; @@ -1495,7 +1476,7 @@ static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect strong = effect->u.rumble.strong_magnitude; weak = effect->u.rumble.weak_magnitude; - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); switch (xpad->xtype) { case XTYPE_XBOX: @@ -1561,15 +1542,10 @@ static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect dev_dbg(&xpad->dev->dev, "%s - rumble command sent to unsupported xpad type: %d\n", __func__, xpad->xtype); - retval = -EINVAL; - goto out; + return -EINVAL; } - retval = xpad_try_sending_next_out_packet(xpad); - -out: - spin_unlock_irqrestore(&xpad->odata_lock, flags); - return retval; + return xpad_try_sending_next_out_packet(xpad); } static int xpad_init_ff(struct usb_xpad *xpad) @@ -1622,11 +1598,10 @@ static void xpad_send_led_command(struct usb_xpad *xpad, int command) { struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_LED_IDX]; - unsigned long flags; command %= 16; - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); switch (xpad->xtype) { case XTYPE_XBOX360: @@ -1656,8 +1631,6 @@ static void xpad_send_led_command(struct usb_xpad *xpad, int command) } xpad_try_sending_next_out_packet(xpad); - - spin_unlock_irqrestore(&xpad->odata_lock, flags); } /* @@ -1782,11 +1755,10 @@ static void xpad_stop_input(struct usb_xpad *xpad) static void xpad360w_poweroff_controller(struct usb_xpad *xpad) { - unsigned long flags; struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_CMD_IDX]; - spin_lock_irqsave(&xpad->odata_lock, flags); + guard(spinlock_irqsave)(&xpad->odata_lock); packet->data[0] = 0x00; packet->data[1] = 0x00; @@ -1806,8 +1778,6 @@ static void xpad360w_poweroff_controller(struct usb_xpad *xpad) /* Reset the sequence so we send out poweroff now */ xpad->last_out_packet = -1; xpad_try_sending_next_out_packet(xpad); - - spin_unlock_irqrestore(&xpad->odata_lock, flags); } static int xpad360w_start_input(struct usb_xpad *xpad) @@ -2231,10 +2201,10 @@ static int xpad_suspend(struct usb_interface *intf, pm_message_t message) if (auto_poweroff && xpad->pad_present) xpad360w_poweroff_controller(xpad); } else { - mutex_lock(&input->mutex); + guard(mutex)(&input->mutex); + if (input_device_enabled(input)) xpad_stop_input(xpad); - mutex_unlock(&input->mutex); } xpad_stop_output(xpad); @@ -2246,26 +2216,25 @@ static int xpad_resume(struct usb_interface *intf) { struct usb_xpad *xpad = usb_get_intfdata(intf); struct input_dev *input = xpad->dev; - int retval = 0; - if (xpad->xtype == XTYPE_XBOX360W) { - retval = xpad360w_start_input(xpad); - } else { - mutex_lock(&input->mutex); - if (input_device_enabled(input)) { - retval = xpad_start_input(xpad); - } else if (xpad->xtype == XTYPE_XBOXONE) { - /* - * Even if there are no users, we'll send Xbox One pads - * the startup sequence so they don't sit there and - * blink until somebody opens the input device again. - */ - retval = xpad_start_xbox_one(xpad); - } - mutex_unlock(&input->mutex); + if (xpad->xtype == XTYPE_XBOX360W) + return xpad360w_start_input(xpad); + + guard(mutex)(&input->mutex); + + if (input_device_enabled(input)) + return xpad_start_input(xpad); + + if (xpad->xtype == XTYPE_XBOXONE) { + /* + * Even if there are no users, we'll send Xbox One pads + * the startup sequence so they don't sit there and + * blink until somebody opens the input device again. + */ + return xpad_start_xbox_one(xpad); } - return retval; + return 0; } static struct usb_driver xpad_driver = {