From patchwork Sun Aug 25 05:16:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 13776678 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 25DF3C5321E for ; Sun, 25 Aug 2024 05:20:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=G5PKJ4DF5X1qlJhF7gsux7p32n0IqTVXA5WgHVkgHUQ=; b=Nuu2qCN+uxen+T8MJloJPimNj1 Jr16hHvqxzTyGoBsCV5PfcfAcKuzsu/Br5TamAE4I3MaMOMdZ4TbzoH+tmSJRl+fKGgciMn1HOTOH xQQquSJo1swuSQbGlID6xl6ZsrlIhq1HInkkYA9zn31Ea7ljjm9uHwvmSnwN/jxFGoanNOVeus/I0 uswKYQ/cyO6hTPbnhcXG9PZRnox4y+4OIYXOJSzKTdIahO6ENsDQXPN5EHf6+aMRgXxzBC6U+HnP3 luej7f/Sxtrk2WUY+lJmVLjx8wGOkJXKedPs0wYvTA1DVSn4pXlFGriaE1P0ky+VQdNwMbg7hL66/ Qi2GN40w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1si5g9-00000003f1W-0qOt; Sun, 25 Aug 2024 05:20:41 +0000 Received: from mail-oa1-x34.google.com ([2001:4860:4864:20::34]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1si5cJ-00000003dcS-2eVr for linux-arm-kernel@lists.infradead.org; Sun, 25 Aug 2024 05:16:45 +0000 Received: by mail-oa1-x34.google.com with SMTP id 586e51a60fabf-27012aa4a74so2181346fac.0 for ; Sat, 24 Aug 2024 22:16:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724563003; x=1725167803; darn=lists.infradead.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=G5PKJ4DF5X1qlJhF7gsux7p32n0IqTVXA5WgHVkgHUQ=; b=Dtl370YX7SvjwCgdhM+6PefFzqmTEMINxvGi5zLkDestOKMvJYB5BCe+5KanSKp+KU HWNRAgHr5UVqsetx5u4XF8hi8dUrzcjmBGUP0nYWuIXrnjogUVZANQBYlVHABcStKte4 4Bob73laxvTdGgO16L3IBIT8kFjGj7b4pq8TYoHIJTcj89ck7dqyFDG+Izv8MD2+JbhT 5YopT00qLH0VMozVKBqRDoCMEBaE7iUsblKRqqj2G0HhAQFdsmr1NjpGb2zl67/RCEXX hmKSL3bXgof/XJHN5lSDByvyyoMbnjVZbwWcqL1vjgzJcvV1y6lSFBLDGxn5EL0dCELT U+tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724563003; x=1725167803; 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=G5PKJ4DF5X1qlJhF7gsux7p32n0IqTVXA5WgHVkgHUQ=; b=aiCi+OlsVBmxh0lWn6Z/XB8LnVdEpDM8AegAayQWGkWEkoO97DYDJeOjVmqS3799L3 nyDt27Tt7o2sHqxXrkDeqgQ51+GJnUPXmDw4DzU4OTzztYwVF+6du1ZhFeY6t1ypGSjW /Q9C0FkBUosdPtfmCTBBw6amSOr8XSYTZeq1PI5dfEvRHIYZSlrxupgvqyd58ODYwH7Y 6ZmEc4/J6ik8h+SjLhkEaQRC5/O/DIUvyHIp6k4vuJor/tLpwqoQcF7mOfQtLQOYli6h YNUNuXYlLa98vf8J/x5dOB/2/tOpBtbZiSG6iW3H2IEkFhsElgK19Hdrkar9mSwEc2Ir 3p7g== X-Forwarded-Encrypted: i=1; AJvYcCWKpvvHGP1if/Wm4F3b2JbS5uD3PLSS64wsBp65xfBWOD/H5rOHmHCrAwjItIOLu0YBUH4VdIPWtUDUWlIYp7L9@lists.infradead.org X-Gm-Message-State: AOJu0YxqdvbK9Cg7b3YvG5oUc9/xHzvQY7997joQof6QiWZ92p7bStGb GvtOyliSCouXjw86zq2GkvE8er5Wpa8AnsitXCK29BTMm3pMUG3O X-Google-Smtp-Source: AGHT+IG8p40c6YWUkGfP1Ref/yI6506RwxbxXGHkuvgXNPmHrDm0NWWnw5t8czN5o3U7acylmdbbqg== X-Received: by 2002:a05:6870:e310:b0:261:113c:1507 with SMTP id 586e51a60fabf-273e6472931mr7072687fac.20.1724563002770; Sat, 24 Aug 2024 22:16:42 -0700 (PDT) Received: from dtor-ws.sjc.corp.google.com ([2620:15c:9d:2:66dc:ce07:b7cc:51ea]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ad55a94sm5622442a12.57.2024.08.24.22.16.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Aug 2024 22:16:42 -0700 (PDT) From: Dmitry Torokhov To: linux-input@vger.kernel.org Cc: Michael Hennerich , Shawn Guo , Sascha Hauer , Fabio Estevam , Laxman Dewangan , Thierry Reding , Hans de Goede , Tony Lindgren , Jeff LaBundy , linux-kernel@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-tegra@vger.kernel.org Subject: [PATCH 03/17] Input: atkbd - use guard notation when acquiring mutex Date: Sat, 24 Aug 2024 22:16:07 -0700 Message-ID: <20240825051627.2848495-4-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.46.0.295.g3b9ea8a38a-goog In-Reply-To: <20240825051627.2848495-1-dmitry.torokhov@gmail.com> References: <20240825051627.2848495-1-dmitry.torokhov@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240824_221643_741573_AA89571E X-CRM114-Status: GOOD ( 14.76 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 Reviewed-by: Hans de Goede --- 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)