From patchwork Wed Sep 4 04:30:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 13789678 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 786DA4DA13; Wed, 4 Sep 2024 04:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424277; cv=none; b=mJOAoesremU49NX3z+nDNemUWABNBQlNRGJJ596QppEKXDysLamiK2x0W6X1RRtbSYZWiN1sVwpEkpg6Ppne6gCU7mK+dI7cSmvGvYo8XirPr8zwwI7UAhLE6P14cbwOBVNKV5Rk8XIEPEZiQW9sQHZX+wQyqjL5wS+Emu0kV1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424277; c=relaxed/simple; bh=jlqX7lEvPPP7Gwii7g4gUqajw+l5/BJ/1iZbrTUNHio=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G1KhMJGaHEAkJgHaFIiBTQrJRJFq+NeSwp2rDCW4ty4o5rxXjSu9nzs5ctPMKt2EvT+HvS2YYEUfuN4GxN0kEHlrWbi2W19hlPjUiCfWw/LN8hvUQyG8ynm+pqOfOJDJiv2dqQgluhSLfSpdcnDpu7m5Lx9MA8Rwmb+v1XQJMHg= 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=gz/nGyNx; arc=none smtp.client-ip=209.85.214.169 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="gz/nGyNx" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2059204f448so18647235ad.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=1725424275; x=1726029075; 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=LuvbdLqDOw3GR1ip2WHJLn3qAyxYmXThLI8hRYt2OZ4=; b=gz/nGyNxjKf2uUQ61xDE8c/IbdunZ9s1C+v6KVAKqIgwoG9hwoGmXACmZkrOcFY0mD 0RVAJ3ViSEqlOl2KKQOeFqWLpJxoN6bev8GC9HioMCcqvf2BIUUR2wgKWjmN7K/AvFQu S3Y0g1qI7tTZQkTXsX+NFJm+T3/gmen2EIhYB4r0hsTy3nubYzsSbTaEdQfQbIR8+waJ gqbMXMkTbGeJa6oCuF+9kQkIaHZzazq11ibzeast7KW1eSGOaXkpiovlc2PlmWfT2WCP /oKZQpTbeqhtJZ/DeD5Ku4FYsldwgkhhPAnzKqdjxRDPWaNlNd9GPi9nC9UNNg9w/cYt pdNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725424275; x=1726029075; 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=LuvbdLqDOw3GR1ip2WHJLn3qAyxYmXThLI8hRYt2OZ4=; b=AGgqUtdpJ8Q5+B+h6/UEOIdwxxwr21Eil5q0Kiqpe6hnhiOyiovkpU0Xmg50szVY6m hHyZsb24n7I3lth9fu7h247QJazXGbWIJuitx5+b2QsnLvOfxG2Houwoss2YcJuJ+gu2 EjN51ya8LZKg/yYR1Ot01E5YlfXkV4NtKlDZpDnSO5BzYLxkOfa2Z4L2jCaDCK/HWrc9 nqrvHSHnGAxYKMwA4FPGl/KkxJuG/ThAc276aZkypiCPiXL3YunEJNUwQteOiY1iPzbF 4DXKg1KOi3XM5YNhm+wgjlUtaNCx18pdYA9sbo0Ohytkv5KG7qwPf7sgCIshJ/ErMnL0 PEvQ== X-Forwarded-Encrypted: i=1; AJvYcCWgdY0Kkqr0RHDoMT1YECZhiDlLnLtCRDrJP4vGWJW1XjZSYhzX/ASC3EGHZOwIH9p9AsvU+peDMtZUx+8=@vger.kernel.org X-Gm-Message-State: AOJu0YxyZbdJFcy4F748ZSlMDXa3v3ZfmFyZdh/h4sXIP/LcGHFz6KlQ mEr4Iz3MafIO/d0Qz70eU24+x3G1LjGnSXQ4dZmrAqTwUNsrYaEJU3qv/A== X-Google-Smtp-Source: AGHT+IF/fOXrTzItgYgQEqWVhU4cjrRoZX8g/kHy1+pt1MNXw6HrR0jRWbQBtZAPz5aYoQy1olu10w== X-Received: by 2002:a17:903:292:b0:1fd:9d0c:998c with SMTP id d9443c01a7336-205447a43e5mr107822345ad.56.1725424275130; 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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 21:31:14 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Erick Archer , Christophe JAILLET , linux-kernel@vger.kernel.org Subject: [PATCH 1/6] Input: db9 - use guard notation when acquiring mutex Date: Tue, 3 Sep 2024 21:30:58 -0700 Message-ID: <20240904043104.1030257-2-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/db9.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/drivers/input/joystick/db9.c b/drivers/input/joystick/db9.c index 682a29c27832..7ac0cfc3e786 100644 --- a/drivers/input/joystick/db9.c +++ b/drivers/input/joystick/db9.c @@ -505,24 +505,22 @@ static int db9_open(struct input_dev *dev) { struct db9 *db9 = input_get_drvdata(dev); struct parport *port = db9->pd->port; - int err; - err = mutex_lock_interruptible(&db9->mutex); - if (err) - return err; - - if (!db9->used++) { - parport_claim(db9->pd); - parport_write_data(port, 0xff); - if (db9_modes[db9->mode].reverse) { - parport_data_reverse(port); - parport_write_control(port, DB9_NORMAL); + scoped_guard(mutex_intr, &db9->mutex) { + if (!db9->used++) { + parport_claim(db9->pd); + parport_write_data(port, 0xff); + if (db9_modes[db9->mode].reverse) { + parport_data_reverse(port); + parport_write_control(port, DB9_NORMAL); + } + mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME); } - mod_timer(&db9->timer, jiffies + DB9_REFRESH_TIME); + + return 0; } - mutex_unlock(&db9->mutex); - return 0; + return -EINTR; } static void db9_close(struct input_dev *dev) @@ -530,14 +528,14 @@ static void db9_close(struct input_dev *dev) struct db9 *db9 = input_get_drvdata(dev); struct parport *port = db9->pd->port; - mutex_lock(&db9->mutex); + guard(mutex)(&db9->mutex); + if (!--db9->used) { del_timer_sync(&db9->timer); parport_write_control(port, 0x00); parport_data_forward(port); parport_release(db9->pd); } - mutex_unlock(&db9->mutex); } static void db9_attach(struct parport *pp) 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: 13789679 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:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 13789680 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 2FF8A823DE; Wed, 4 Sep 2024 04:31:17 +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=1725424279; cv=none; b=URWGOm2KGbcuS7/QfKF4Z+tJvTPeG2jXGyvr5drwQ96es3JH/NTimupTJTnl8TgUtrnfxNVpEGjyGHfuVa4x5H2i70cd5QNnEHDTv4tq6yrN9+6YAowoV1kRrzgeEAx1thYxnfiAUOH/gBULzQmGllCGSPz88SERjZULpjc9zrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424279; c=relaxed/simple; bh=CFJVsOPx90AxULBy5akrpax+TBZQuboU2SraioZ4Low=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dWU4/5Jdz7O68E77s/vlX0wgmj0s+ZKMl6m30BGEHn72OK8h+tzYHobZ2knYyaeq3M2k1+sJkPhQydzcAoZGaIrWU6kKHcGXHT0K87G8DbhnClYfxkAA+xumqpq95SyOhQpcfihO6gJbeHaUh79U+6QMr9f/xREATHT3tILBDIM= 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=TuhQiexi; 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="TuhQiexi" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7cd9d408040so4540737a12.0; Tue, 03 Sep 2024 21:31:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725424277; x=1726029077; 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=W6XdSiBXsmBN1finzDUHBvynyjPDNiPKK0O7rO2yo3o=; b=TuhQiexi4QMnThZLT74aXwCJVyMcmEWwrVRFzIgQH+ZhUQYMaE72MX5raoI1Qe+GKs J49scu6xy7hyDi5BmW/B5u3QdH78xS8mjiuVwjFpo2VMQof1AJ46ha8C35xmPpJrEiL4 /90jFpYwN7iHBganHvy6tUfd3sxiJ9XxSCzSLr6cHk/XWB/1UB97Nlz2kPFyhSKvub9X yvQfmmbedtKOoojvPq/bvy3XqzUE6kwwUv/hj0Gw8VblmzWC+A0zDXTvfikyDLSGqNli 9b91S1JG37OpVwajWhCiMLVOlBp7feyl7VuHxQs4+CFHAXq52Ve8doXj3V3MMCAPY+mg 71hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725424277; x=1726029077; 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=W6XdSiBXsmBN1finzDUHBvynyjPDNiPKK0O7rO2yo3o=; b=jOlOPHfPJcK/3bR+KSjCHk4M0FTCfzQkveQ5mOzBF05gNfAjj5tS/utL2cchLH4H5E +hSk5eqCVPNtXCdqsYbmux0KnnqivdsYeQBm8/8mIWSbzFswY/vf4cchjXdmu4CRLiP3 zatt9zQRiJpBE3310ZADTHNYYajd2CkkK6e3UpQeBYpHkfeaOxaEUKqT+pkBjEr6nd90 xYMAi13EX9PufYOCueIcPXo/Je1PjJa+w6Q9nEoKmyS2tmO3RfcV4Hx7D3vQP6uiv8kI Jjrg72ps50VdrVfs4c1oyfZsPAGuuF1ptxkon3Do6mnmkplOSiJPMQTgA+RzsSkSBDMQ G5mg== X-Forwarded-Encrypted: i=1; AJvYcCXJSDqSP/i1MQesM8MTF8eoko1eGQdVhLPnEFbymB/7bMMGchGBHdtzR5hNXFh8vkq4GFWEF7utPfbehm8=@vger.kernel.org X-Gm-Message-State: AOJu0YxHQIuTsBtKaoeHiWACCGlXporZ8Hl4uMqVLJqQ/T8l2xmZvcaf UsJdQBi2Kcpjo6PgcRpja+LWhwi+4IVmhVd3bT82//ITxls7Y3Bb7g7k/Q== X-Google-Smtp-Source: AGHT+IGJFu6hewiFTP2KUs5gSTWLveSHSZ+1Qrxh8bE7dgAf5m2RmiKeIeGDwfnSfsPfl4saTTyYFw== X-Received: by 2002:a05:6a20:b40a:b0:1ce:f77a:67e5 with SMTP id adf61e73a8af0-1cef77a69f1mr6406930637.47.1725424277143; 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.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 21:31:16 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Erick Archer , Christophe JAILLET , linux-kernel@vger.kernel.org Subject: [PATCH 3/6] Input: iforce - use guard notation when acquiring mutex and spinlock Date: Tue, 3 Sep 2024 21:31:00 -0700 Message-ID: <20240904043104.1030257-4-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/iforce/iforce-ff.c | 48 +++++++--------- .../input/joystick/iforce/iforce-packets.c | 57 ++++++++----------- drivers/input/joystick/iforce/iforce-serio.c | 36 +++++------- drivers/input/joystick/iforce/iforce-usb.c | 13 ++--- 4 files changed, 68 insertions(+), 86 deletions(-) diff --git a/drivers/input/joystick/iforce/iforce-ff.c b/drivers/input/joystick/iforce/iforce-ff.c index 95c0348843e6..8c78cbe553c8 100644 --- a/drivers/input/joystick/iforce/iforce-ff.c +++ b/drivers/input/joystick/iforce/iforce-ff.c @@ -21,14 +21,13 @@ static int make_magnitude_modifier(struct iforce* iforce, unsigned char data[3]; if (!no_alloc) { - mutex_lock(&iforce->mem_mutex); - if (allocate_resource(&(iforce->device_memory), mod_chunk, 2, - iforce->device_memory.start, iforce->device_memory.end, 2L, - NULL, NULL)) { - mutex_unlock(&iforce->mem_mutex); + guard(mutex)(&iforce->mem_mutex); + + if (allocate_resource(&iforce->device_memory, mod_chunk, 2, + iforce->device_memory.start, + iforce->device_memory.end, + 2L, NULL, NULL)) return -ENOSPC; - } - mutex_unlock(&iforce->mem_mutex); } data[0] = LO(mod_chunk->start); @@ -54,14 +53,13 @@ static int make_period_modifier(struct iforce* iforce, period = TIME_SCALE(period); if (!no_alloc) { - mutex_lock(&iforce->mem_mutex); - if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c, - iforce->device_memory.start, iforce->device_memory.end, 2L, - NULL, NULL)) { - mutex_unlock(&iforce->mem_mutex); + guard(mutex)(&iforce->mem_mutex); + + if (allocate_resource(&iforce->device_memory, mod_chunk, 0x0c, + iforce->device_memory.start, + iforce->device_memory.end, + 2L, NULL, NULL)) return -ENOSPC; - } - mutex_unlock(&iforce->mem_mutex); } data[0] = LO(mod_chunk->start); @@ -94,14 +92,13 @@ static int make_envelope_modifier(struct iforce* iforce, fade_duration = TIME_SCALE(fade_duration); if (!no_alloc) { - mutex_lock(&iforce->mem_mutex); + guard(mutex)(&iforce->mem_mutex); + if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e, - iforce->device_memory.start, iforce->device_memory.end, 2L, - NULL, NULL)) { - mutex_unlock(&iforce->mem_mutex); + iforce->device_memory.start, + iforce->device_memory.end, + 2L, NULL, NULL)) return -ENOSPC; - } - mutex_unlock(&iforce->mem_mutex); } data[0] = LO(mod_chunk->start); @@ -131,14 +128,13 @@ static int make_condition_modifier(struct iforce* iforce, unsigned char data[10]; if (!no_alloc) { - mutex_lock(&iforce->mem_mutex); + guard(mutex)(&iforce->mem_mutex); + if (allocate_resource(&(iforce->device_memory), mod_chunk, 8, - iforce->device_memory.start, iforce->device_memory.end, 2L, - NULL, NULL)) { - mutex_unlock(&iforce->mem_mutex); + iforce->device_memory.start, + iforce->device_memory.end, + 2L, NULL, NULL)) return -ENOSPC; - } - mutex_unlock(&iforce->mem_mutex); } data[0] = LO(mod_chunk->start); diff --git a/drivers/input/joystick/iforce/iforce-packets.c b/drivers/input/joystick/iforce/iforce-packets.c index 763642c8cee9..8c2531e2977c 100644 --- a/drivers/input/joystick/iforce/iforce-packets.c +++ b/drivers/input/joystick/iforce/iforce-packets.c @@ -31,49 +31,42 @@ int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data) int c; int empty; int head, tail; - unsigned long flags; /* * Update head and tail of xmit buffer */ - spin_lock_irqsave(&iforce->xmit_lock, flags); - - head = iforce->xmit.head; - tail = iforce->xmit.tail; - - - if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) { - dev_warn(&iforce->dev->dev, - "not enough space in xmit buffer to send new packet\n"); - spin_unlock_irqrestore(&iforce->xmit_lock, flags); - return -1; - } + scoped_guard(spinlock_irqsave, &iforce->xmit_lock) { + head = iforce->xmit.head; + tail = iforce->xmit.tail; + + if (CIRC_SPACE(head, tail, XMIT_SIZE) < n + 2) { + dev_warn(&iforce->dev->dev, + "not enough space in xmit buffer to send new packet\n"); + return -1; + } - empty = head == tail; - XMIT_INC(iforce->xmit.head, n+2); + empty = head == tail; + XMIT_INC(iforce->xmit.head, n + 2); /* * Store packet in xmit buffer */ - iforce->xmit.buf[head] = HI(cmd); - XMIT_INC(head, 1); - iforce->xmit.buf[head] = LO(cmd); - XMIT_INC(head, 1); - - c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE); - if (n < c) c=n; - - memcpy(&iforce->xmit.buf[head], - data, - c); - if (n != c) { - memcpy(&iforce->xmit.buf[0], - data + c, - n - c); + iforce->xmit.buf[head] = HI(cmd); + XMIT_INC(head, 1); + iforce->xmit.buf[head] = LO(cmd); + XMIT_INC(head, 1); + + c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE); + if (n < c) + c = n; + + memcpy(&iforce->xmit.buf[head], data, c); + if (n != c) + memcpy(&iforce->xmit.buf[0], data + c, n - c); + + XMIT_INC(head, n); } - XMIT_INC(head, n); - spin_unlock_irqrestore(&iforce->xmit_lock, flags); /* * If necessary, start the transmission */ diff --git a/drivers/input/joystick/iforce/iforce-serio.c b/drivers/input/joystick/iforce/iforce-serio.c index 2380546d7978..75b85c46dfa4 100644 --- a/drivers/input/joystick/iforce/iforce-serio.c +++ b/drivers/input/joystick/iforce/iforce-serio.c @@ -28,45 +28,39 @@ static void iforce_serio_xmit(struct iforce *iforce) iforce); unsigned char cs; int i; - unsigned long flags; if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) { set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags); return; } - spin_lock_irqsave(&iforce->xmit_lock, flags); + guard(spinlock_irqsave)(&iforce->xmit_lock); -again: - if (iforce->xmit.head == iforce->xmit.tail) { - iforce_clear_xmit_and_wake(iforce); - spin_unlock_irqrestore(&iforce->xmit_lock, flags); - return; - } + do { + if (iforce->xmit.head == iforce->xmit.tail) + break; - cs = 0x2b; + cs = 0x2b; - serio_write(iforce_serio->serio, 0x2b); + serio_write(iforce_serio->serio, 0x2b); - serio_write(iforce_serio->serio, iforce->xmit.buf[iforce->xmit.tail]); - cs ^= iforce->xmit.buf[iforce->xmit.tail]; - XMIT_INC(iforce->xmit.tail, 1); - - for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) { serio_write(iforce_serio->serio, iforce->xmit.buf[iforce->xmit.tail]); cs ^= iforce->xmit.buf[iforce->xmit.tail]; XMIT_INC(iforce->xmit.tail, 1); - } - serio_write(iforce_serio->serio, cs); + for (i = iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) { + serio_write(iforce_serio->serio, + iforce->xmit.buf[iforce->xmit.tail]); + cs ^= iforce->xmit.buf[iforce->xmit.tail]; + XMIT_INC(iforce->xmit.tail, 1); + } - if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags)) - goto again; + serio_write(iforce_serio->serio, cs); - iforce_clear_xmit_and_wake(iforce); + } while (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags)); - spin_unlock_irqrestore(&iforce->xmit_lock, flags); + iforce_clear_xmit_and_wake(iforce); } static int iforce_serio_get_id(struct iforce *iforce, u8 id, diff --git a/drivers/input/joystick/iforce/iforce-usb.c b/drivers/input/joystick/iforce/iforce-usb.c index cba92bd590a8..1f00f76b0174 100644 --- a/drivers/input/joystick/iforce/iforce-usb.c +++ b/drivers/input/joystick/iforce/iforce-usb.c @@ -25,13 +25,11 @@ static void __iforce_usb_xmit(struct iforce *iforce) struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, iforce); int n, c; - unsigned long flags; - spin_lock_irqsave(&iforce->xmit_lock, flags); + guard(spinlock_irqsave)(&iforce->xmit_lock); if (iforce->xmit.head == iforce->xmit.tail) { iforce_clear_xmit_and_wake(iforce); - spin_unlock_irqrestore(&iforce->xmit_lock, flags); return; } @@ -45,7 +43,8 @@ static void __iforce_usb_xmit(struct iforce *iforce) /* Copy rest of data then */ c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE); - if (n < c) c=n; + if (n < c) + c = n; memcpy(iforce_usb->out->transfer_buffer + 1, &iforce->xmit.buf[iforce->xmit.tail], @@ -53,11 +52,12 @@ static void __iforce_usb_xmit(struct iforce *iforce) if (n != c) { memcpy(iforce_usb->out->transfer_buffer + 1 + c, &iforce->xmit.buf[0], - n-c); + n - c); } XMIT_INC(iforce->xmit.tail, n); - if ( (n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC)) ) { + n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC); + if (n) { dev_warn(&iforce_usb->intf->dev, "usb_submit_urb failed %d\n", n); iforce_clear_xmit_and_wake(iforce); @@ -66,7 +66,6 @@ static void __iforce_usb_xmit(struct iforce *iforce) /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended. * As long as the urb completion handler is not called, the transmiting * is considered to be running */ - spin_unlock_irqrestore(&iforce->xmit_lock, flags); } static void iforce_usb_xmit(struct iforce *iforce) 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: 13789681 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:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 13789682 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 126CE83CD9; Wed, 4 Sep 2024 04:31:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424281; cv=none; b=E55bHg66sc7AkzO3Q11QFirgIFpmRDGrC9caA3RxJOBM2AoIQXvMpFeJLiO3NgVmPr/Mz/9IQWb1mnfdoQatGQoQ+e+98OJJEkvz3Qo/tegVTrL74g45yJu2aT9sUZQlS+53cEe8EjjJmewoOuj2q+aY6R0q3fDMplyJfNYwiC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725424281; c=relaxed/simple; bh=qsJupgvB05zPy8V+FqhBDe8t6X0p52K2TGgaTcj5xC0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iusyCpR0aJ6m738X2KIdqXvjfiUgtydyxfWI4bbbRlbF5YN7Zj084Ese9b5vu/J1GUKsBWwTwspEV3JXCL8KPcRyiQB0BM0TBleAkXEB6qV59ag+wZaA0M2/cHzuTX9TkB4Wj0CGoJv6aSh025kXG7vElQO3EkWZwkhUkfjfV9g= 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=LIIDPQe8; arc=none smtp.client-ip=209.85.215.175 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="LIIDPQe8" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7cd9d408040so4540756a12.0; Tue, 03 Sep 2024 21:31:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725424279; x=1726029079; 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=wO+7gee83CsLsM7sYii7cqnFdXQ9E+GWWcO+DnEDlDQ=; b=LIIDPQe8bSQRwAe1hyCBRJh8umIaeq5v36sk/Ct4zPaCc6ApygzxRJbgqN6j2BkG42 UW0QsBFZS4g2jrlJ3nEK8NZJoP1ZhEnLXbTF4eHuQhyePGWYyaOIxMHi7FnJbz3TP2cR LHqMYtdFr9k4TKKY5F3hoEixdWcIOIGiiWXPcqsFJqtgwdLCRC03aTaslBrWciL+8vUN kStMz7XWLmnC4Z3fj1a5WnI77z29CG85C7AqIF62vU/J83kgDy+rr/bnoCY6gmaIW8vJ J2p7YE9vLC2kxpXM3nvpliTtT90BtGm1LeAGfsvGbUEXbKzKsNSVMiRuHd+iCIDRI0Ig 2mRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725424279; x=1726029079; 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=wO+7gee83CsLsM7sYii7cqnFdXQ9E+GWWcO+DnEDlDQ=; b=d7zr14d1r+T7Ru8CS0CcbZYX9bplIxlt29/C0PL/YZjJuJ17KZeZMhZxV/YCvFvcLs lyC36qWtk4T5iVcfSZ56sMFCF37/4tclDCwcKWvpmSeDbJozPWA3rOjTFkzVEKZRsacT HYf29iNM30ohadpvzYIeiPs98du+dGEA5HAaqYWeHIzYy0W39Tn1OBOSMt8E3iaz/Rmn mE0q6Jq3N024b+PCBBXnP8aTbQ7bc/eOdxeAYEI9w/kpPmeZqxP6m7VUDqKq6uqNT2AM F0fiaYHNUYtDQwsXiZE77HDSelrwkN/DQXJS/x7vHZyUsxk8Be0gLJf/RpxTL9vnIl5Q 7/Yg== X-Forwarded-Encrypted: i=1; AJvYcCWEK/MHRUCV/p9/ofnEQpMmGsS7W2+89zBAgtz4DIdUGkBpL4ssqEPxWVPPZ4LhS08N9Iq71l7ZjpP2uqo=@vger.kernel.org X-Gm-Message-State: AOJu0YwKKFmotlVgRe8Llt7zFVpD9cQgN0c8EWis8HqZmbwd2dcuM6ZS VCy54XpiNmprRLNCDJmdjjPXkg2fOkofu2f0J8OVpX95KFzk6lvsB+v3cw== X-Google-Smtp-Source: AGHT+IE3Z+c0rj00rM2a+Wkqo3oBJbD+lht50s7aTATJbOyllMNnS1GTsuIiCSTHfCiY3R6vKtzbiA== X-Received: by 2002:a05:6a21:3305:b0:1c4:7929:a593 with SMTP id adf61e73a8af0-1cecf5146c9mr17715923637.23.1725424279123; Tue, 03 Sep 2024 21:31:19 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 21:31:18 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Erick Archer , Christophe JAILLET , linux-kernel@vger.kernel.org Subject: [PATCH 5/6] Input: turbografx - use guard notation when acquiring mutex Date: Tue, 3 Sep 2024 21:31:02 -0700 Message-ID: <20240904043104.1030257-6-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/turbografx.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/drivers/input/joystick/turbografx.c b/drivers/input/joystick/turbografx.c index eb8455c34e67..015010a928aa 100644 --- a/drivers/input/joystick/turbografx.c +++ b/drivers/input/joystick/turbografx.c @@ -103,33 +103,31 @@ static void tgfx_timer(struct timer_list *t) static int tgfx_open(struct input_dev *dev) { struct tgfx *tgfx = input_get_drvdata(dev); - int err; - err = mutex_lock_interruptible(&tgfx->sem); - if (err) - return err; + scoped_guard(mutex_intr, &tgfx->sem) { + if (!tgfx->used++) { + parport_claim(tgfx->pd); + parport_write_control(tgfx->pd->port, 0x04); + mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME); + } - if (!tgfx->used++) { - parport_claim(tgfx->pd); - parport_write_control(tgfx->pd->port, 0x04); - mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME); + return 0; } - mutex_unlock(&tgfx->sem); - return 0; + return -EINTR; } static void tgfx_close(struct input_dev *dev) { struct tgfx *tgfx = input_get_drvdata(dev); - mutex_lock(&tgfx->sem); + guard(mutex)(&tgfx->sem); + if (!--tgfx->used) { del_timer_sync(&tgfx->timer); parport_write_control(tgfx->pd->port, 0x00); parport_release(tgfx->pd); } - mutex_unlock(&tgfx->sem); } 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: 13789683 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 = {