From patchwork Sun Aug 25 05:16:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 13776680 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 B8797C5321E for ; Sun, 25 Aug 2024 05:22:21 +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=+IClYBjpK7iTXWqXO0hqSivHGfHTegcYSer/+UixwhE=; b=0Ifi61tbXEMRSpUJA0V2QKOFxQ QDgu3AgTT5Bz+NStvTyADZh+HFuxDfcU8+p6IwLEaGUQTML8VH8TddRoo7LGJhr+LqOUS2p0Ht17i 3tnS74ATwXff0M5dGFBK8abVi7bLvAgTXtBq/cmT9zF08L9fQN2d1l4oVtT1EBF8AtSp/1gyq5Bm7 HaqggB1aulG8XvhjJnh6i7QmhTHEctBWtgusENMJOLGsAADQF/+EP2OOIOHpeY+RnmI6o4Rasxlb+ dEw3tDZDScsqch3jzPkejqrZSSGN243pjW4aqh2GNqo/Tm65IreQKZFjCkVxWl3uPzfaWNMVY7H5X ys0svNHQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1si5hc-00000003fNc-0HNk; Sun, 25 Aug 2024 05:22:12 +0000 Received: from mail-oa1-x2c.google.com ([2001:4860:4864:20::2c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1si5cM-00000003dei-3Bvn for linux-arm-kernel@lists.infradead.org; Sun, 25 Aug 2024 05:16:48 +0000 Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-2689f749702so2273473fac.3 for ; Sat, 24 Aug 2024 22:16:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724563005; x=1725167805; 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=+IClYBjpK7iTXWqXO0hqSivHGfHTegcYSer/+UixwhE=; b=EJ7BUAw1SK8vmPY2G4rdirrj/8tGyfV49jRz/85JLEHoq1hueAg22qpjh3x2TDVrk9 GXu2nK5NdnU82ahdzmlRUe0/7cnwHcycyuWiODtW1Qfh7jaspDcUPe8Kau1VkKs3vCyT SeccGAFcESd6QAl11duswyVgnjY0TeRiYnQZfIrs0ai+kQNHBm6saYf72h3Mti7zFPPs n3zLMJ75Mx6Go2ic5qyw4NYSalODOVhmLp2PcUeVyGI4dPdHiryuYZ0j9ymOBRxcph4H +mOLlaCQUDrq8OM6QEbBLnP4j8xAC95GEejcC4E661/lO7YF4ZkTkLa3izbdU4ft7gmr ACRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724563005; x=1725167805; 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=+IClYBjpK7iTXWqXO0hqSivHGfHTegcYSer/+UixwhE=; b=MNBtBrhQXevGCC9a6F3sBO0A7jl9aLCfifJ2j585PpGd5FHgFJkJnF9GZEeJlEdAyz hN4QZaY/0Zep6x9LYy5pLDvD9bUmNjBmDVc7SgYXR2RsgZK7ali/XoekswaUZwdCkbBW mGCg3s/et5uz8OpBzT+CA6wfTv2ZPChZLavT+WATQZgrCa48gEljk5VzKlPheWpFURVZ FBva1SEGkMwfhfBw0iyq9sPS0n56DKEsQ5LpK6WPiMGGNYtO2V3QBiIt18iMXQKQsGSS nHir/mTVxhCVhFsbHw8Tt0PpzCR/Su33FQVifTUicezLoO8e5/B2SVQVLQFykkzwUTdy RnAg== X-Forwarded-Encrypted: i=1; AJvYcCVqjjf9E/n32euO6zZjuMaNGdauldf9a6tRxrGEgjzv4IB2BQFsnGckZfzmiA76DaRd/bvZvo8HKS7qDtwASyq5@lists.infradead.org X-Gm-Message-State: AOJu0YxW4F6GT493ZVSjvFdI1o6rK62R+heL90E3P22Kb3g9wM9TWY7K 8KFNjhO2ieEk8mE1IPpdXn7dX3m1xducyYu58CitaT3KP9RZtDCY X-Google-Smtp-Source: AGHT+IEotK5Z46zRG2q3RwlYVnZUsrGo/WBEX9X+Oub64iSKDadCyz4xjkfGuQRwO8QaHq4D1a66ZA== X-Received: by 2002:a05:6870:a91a:b0:270:46be:9534 with SMTP id 586e51a60fabf-273e64f5c77mr7810387fac.24.1724563005413; Sat, 24 Aug 2024 22:16:45 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Aug 2024 22:16:45 -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 05/17] Input: gpio-keys - switch to using cleanup functions Date: Sat, 24 Aug 2024 22:16:09 -0700 Message-ID: <20240825051627.2848495-6-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_221646_820844_B43891B2 X-CRM114-Status: GOOD ( 18.18 ) 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 Start using __free() and guard() primitives to simplify the code and error handling. This 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 and all allocated memory is freed. Signed-off-by: Dmitry Torokhov Reviewed-by: Hans de Goede --- drivers/input/keyboard/gpio_keys.c | 44 ++++++++++++------------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c index 9fb0bdcfbf9e..380fe8dab3b0 100644 --- a/drivers/input/keyboard/gpio_keys.c +++ b/drivers/input/keyboard/gpio_keys.c @@ -245,23 +245,20 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, { int n_events = get_n_events_by_type(type); const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type); - unsigned long *bits; ssize_t error; int i; - bits = bitmap_alloc(n_events, GFP_KERNEL); + unsigned long *bits __free(bitmap) = bitmap_alloc(n_events, GFP_KERNEL); if (!bits) return -ENOMEM; error = bitmap_parselist(buf, bits, n_events); if (error) - goto out; + return error; /* First validate */ - if (!bitmap_subset(bits, bitmap, n_events)) { - error = -EINVAL; - goto out; - } + if (!bitmap_subset(bits, bitmap, n_events)) + return -EINVAL; for (i = 0; i < ddata->pdata->nbuttons; i++) { struct gpio_button_data *bdata = &ddata->data[i]; @@ -271,12 +268,11 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, if (test_bit(*bdata->code, bits) && !bdata->button->can_disable) { - error = -EINVAL; - goto out; + return -EINVAL; } } - mutex_lock(&ddata->disable_lock); + guard(mutex)(&ddata->disable_lock); for (i = 0; i < ddata->pdata->nbuttons; i++) { struct gpio_button_data *bdata = &ddata->data[i]; @@ -290,11 +286,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, gpio_keys_enable_button(bdata); } - mutex_unlock(&ddata->disable_lock); - -out: - bitmap_free(bits); - return error; + return 0; } #define ATTR_SHOW_FN(name, type, only_disabled) \ @@ -470,11 +462,10 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id) { struct gpio_button_data *bdata = dev_id; struct input_dev *input = bdata->input; - unsigned long flags; BUG_ON(irq != bdata->irq); - spin_lock_irqsave(&bdata->lock, flags); + guard(spinlock_irqsave)(&bdata->lock); if (!bdata->key_pressed) { if (bdata->button->wakeup) @@ -497,7 +488,6 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id) ms_to_ktime(bdata->release_delay), HRTIMER_MODE_REL_HARD); out: - spin_unlock_irqrestore(&bdata->lock, flags); return IRQ_HANDLED; } @@ -1062,10 +1052,10 @@ static int gpio_keys_suspend(struct device *dev) if (error) return error; } else { - mutex_lock(&input->mutex); + guard(mutex)(&input->mutex); + if (input_device_enabled(input)) gpio_keys_close(input); - mutex_unlock(&input->mutex); } return 0; @@ -1075,20 +1065,20 @@ static int gpio_keys_resume(struct device *dev) { struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev); struct input_dev *input = ddata->input; - int error = 0; + int error; if (device_may_wakeup(dev)) { gpio_keys_disable_wakeup(ddata); } else { - mutex_lock(&input->mutex); - if (input_device_enabled(input)) + guard(mutex)(&input->mutex); + + if (input_device_enabled(input)) { error = gpio_keys_open(input); - mutex_unlock(&input->mutex); + if (error) + return error; + } } - if (error) - return error; - gpio_keys_report_state(ddata); return 0; }