From nobody Mon Nov 25 22:30:24 2024 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 11A061C9EAF for ; Thu, 24 Oct 2024 11:32:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729769581; cv=none; b=u9K5LE+jorMEHz3MZ59zyr77zwwSMA03Fu3BFYRkoPxMC7rXX86853ex/2LYJU4x+dDO9GZw5QhUcvCaBOscLuM/qZ5wY9sUw5QpkRwlp+D0Pi5fR+FppBcPYb+NVXtZAwnR9QcdrosHzGsNlgQ413mCoqD1lsW/hFFF12E8CCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729769581; c=relaxed/simple; bh=EtR59e/+9h+je4mbWsdv/8tBtr6ipuedGd537vHWyEo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CcZTj0u/J91KJYc+JeXovcM5kUKeL6tO2LUYt4kk5hVLNjHlociuOqg8dW1C+I8D+WaUvfFunUhnuYVdn7Urc9n7W3sGpRYV7hsTapwaczYabmaos9zhqiSvVsTcad184O9JlIZ/XX2fJSJ73FjvLmoevvNYbIOGf6YPwKhtqu8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=PIQRFXyP; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="PIQRFXyP" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4315baec681so8313035e9.2 for ; Thu, 24 Oct 2024 04:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1729769575; x=1730374375; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=JZ/mirgwgAjZlQMvPm+2FBmAjsoLyei227snBkMmziQ=; b=PIQRFXyPWRXp8mNpc2Rk23+VNS5vUqTK3yJdAHJr1YpQRy6gfetdtY8j1tkXc0UlmU 8LnRr0r+G8/9R69ezYeeelMbwBw9ns9qJXgoN3pzuhOzO7MNvQR2fYSMVrbhyX9NWOMq I8Z5LptX4Fp+jf1hnuK0eJk/7xgOIiZDpdiI64tssm2U86Gt1qn4/Qwgj/XaKH9VihCH O5rLZRDwMeK65KvymsDv6C+mqclMXfwZp7meodSQcRF/8u9pAh+JkDrHIt/a67PnafUs 60am9Yb2lFOL5ubsZu1vG6WJj1jBEgjprhmwPx4gyPskoyNeHFfhSSnUp1HvtEU0Ra8I +kkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729769575; x=1730374375; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JZ/mirgwgAjZlQMvPm+2FBmAjsoLyei227snBkMmziQ=; b=qqOZQDmgnE+NVweo4+8ctni+PW9efc5ErmZ/0Jg2NlssemgXIYLH/3PjnOAxPU9ZIR CfXnIOuxfLNdSutvyW4WZVwAd4dFRvPyqDpoPJlb6uVfD3TTQp3cBy/GAEc18+SAACwi cnZHU16MSjmMbsPPRtOg1fvaNRDCsW56rkcZ4HSaS+glThtnwvBiaDkfzui9nIqC0Uxu Toflc8J4SazDzDrbS1YeYg5vyobmzmso5nK21XBPJGj/NqiImX09/AuQbv07fnRG4NSg Gx3yGMFcPwtRd0A45/aTjsKOJ6PYh6BR640FMdA9MR96zx3JsKAez1UtEPZCIlum4+rW GaOA== X-Forwarded-Encrypted: i=1; AJvYcCUhC8mgY9JCRnlTgD/SHYrK6Uopj+c+5lgbeHSYxNe2PUMYXW5js72/BXUnhAHlQmKZVgAdXHwvlyFBvGs=@vger.kernel.org X-Gm-Message-State: AOJu0YwryFZm8H6BSKUZJ93XYViFWdJpESxBP0BPcajTy/TW4ZtXw+qB wALcbYwwnIWKRIhfnm8T+NZDST7gqngoAOe00dhSEWBeuHY5NQIPyvwd2rBt4sg= X-Google-Smtp-Source: AGHT+IFZeRwqNw1uDsh6gIlNZ1JNVdIwXELyLjzTQeBUZCZZdjd5Qppprx6oSMT7zHC4UJfL+x8U3A== X-Received: by 2002:a05:600c:350b:b0:431:46fe:4cad with SMTP id 5b1f17b1804b1-4318c6e8c1fmr12383515e9.9.1729769575100; Thu, 24 Oct 2024 04:32:55 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:17a2:e679:56a4:a25a]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4318b55f484sm14592705e9.13.2024.10.24.04.32.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 04:32:54 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 24 Oct 2024 13:32:44 +0200 Subject: [PATCH 1/5] gpio: sysfs: use cleanup guards for gpiod_data::mutex Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241024-gpio-notify-sysfs-v1-1-981f2773e785@linaro.org> References: <20241024-gpio-notify-sysfs-v1-0-981f2773e785@linaro.org> In-Reply-To: <20241024-gpio-notify-sysfs-v1-0-981f2773e785@linaro.org> To: Linus Walleij , Bartosz Golaszewski , Kent Gibson Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=3945; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=wbHVTkTm1C0gUmYkzNOsSMYN8WOIj6nu1O2fyjTM1Kw=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnGjBk1alXs/1HXQjNSnjKs1lDzlGar0/GrDJIT a9gq7H7jPSJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZxowZAAKCRARpy6gFHHX cs2hEACKFi/0C62knN5BYjVREi89SqnFlqdsJp8T0Cgk7D/3XvulNT2VQEkMOhaFYFHoJdo4pZF JemLjv1Rk5hWGe9RWktQT2Ye5Tokk95g0yjOB09oJSMira3SfQXkIUwvac5NXgss5Jd17MrQvdm l7bSR6tZHho0iEqBNv1yP2kPhnmPgfJ4XJzoWKwkPYo33dN+4+QIcN7MpnsqkDycDRGPKzN3l5d EGcLcL4e1Xm1eaVqxH7kF0MSMwd+9XNaObseTrAEUZSxFhwSQ/flZLrb33mSxh3tMDc15lXX3eA /Im7VTzY/O926NCka1ytp5cg951uve1cMX6D3DR/ijWETQnHUCDMHS/HqmhBgIYcTN01S94sQ6g 0onoNOeU4xa2DZ9ZrjoeLbaE+z6KntxSsUXCiWvd0JoOOD7uixocy2a46VN72XUE3+s1iqMxS6K qHL7FApV1mK79Kt3tLncRwoPUBbDazy9SVUM+bM9jG+qJympC2q9tCDhNTFI4as/8nRVW3S0yvk UjYP9Mxhq2s60IHwIJRvC8n0L1KXp6WK+JMwjmoJj6TnM4HEOxwhaGScJjJ+QG5Zm3uaP+j0An+ V07WY77DDSDSvONAYhde1oZwUxl2F8eAAAmoLsT8hJ0WYCz9NWp0jd04CUm1chsT1r55ZOfG1Pj tkgi61lFZmckrfg== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski Shrink the code and drop some goto labels by using lock guards around gpiod_data::mutex. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-sysfs.c | 57 +++++++++++++---------------------------= ---- 1 file changed, 17 insertions(+), 40 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 0c713baa7784..3ccb41a93ea7 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -77,12 +77,10 @@ static ssize_t direction_show(struct device *dev, struct gpio_desc *desc =3D data->desc; int value; =20 - mutex_lock(&data->mutex); - - gpiod_get_direction(desc); - value =3D !!test_bit(FLAG_IS_OUT, &desc->flags); - - mutex_unlock(&data->mutex); + scoped_guard(mutex, &data->mutex) { + gpiod_get_direction(desc); + value =3D !!test_bit(FLAG_IS_OUT, &desc->flags); + } =20 return sysfs_emit(buf, "%s\n", value ? "out" : "in"); } @@ -94,7 +92,7 @@ static ssize_t direction_store(struct device *dev, struct gpio_desc *desc =3D data->desc; ssize_t status; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); =20 if (sysfs_streq(buf, "high")) status =3D gpiod_direction_output_raw(desc, 1); @@ -105,8 +103,6 @@ static ssize_t direction_store(struct device *dev, else status =3D -EINVAL; =20 - mutex_unlock(&data->mutex); - return status ? : size; } static DEVICE_ATTR_RW(direction); @@ -118,11 +114,8 @@ static ssize_t value_show(struct device *dev, struct gpio_desc *desc =3D data->desc; ssize_t status; =20 - mutex_lock(&data->mutex); - - status =3D gpiod_get_value_cansleep(desc); - - mutex_unlock(&data->mutex); + scoped_guard(mutex, &data->mutex) + status =3D gpiod_get_value_cansleep(desc); =20 if (status < 0) return status; @@ -140,7 +133,7 @@ static ssize_t value_store(struct device *dev, =20 status =3D kstrtol(buf, 0, &value); =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); =20 if (!test_bit(FLAG_IS_OUT, &desc->flags)) { status =3D -EPERM; @@ -149,8 +142,6 @@ static ssize_t value_store(struct device *dev, status =3D size; } =20 - mutex_unlock(&data->mutex); - return status; } static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_st= ore); @@ -253,11 +244,8 @@ static ssize_t edge_show(struct device *dev, struct gpiod_data *data =3D dev_get_drvdata(dev); int flags; =20 - mutex_lock(&data->mutex); - - flags =3D data->irq_flags; - - mutex_unlock(&data->mutex); + scoped_guard(mutex, &data->mutex) + flags =3D data->irq_flags; =20 if (flags >=3D ARRAY_SIZE(trigger_names)) return 0; @@ -276,12 +264,10 @@ static ssize_t edge_store(struct device *dev, if (flags < 0) return flags; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); =20 - if (flags =3D=3D data->irq_flags) { - status =3D size; - goto out_unlock; - } + if (flags =3D=3D data->irq_flags) + return size; =20 if (data->irq_flags) gpio_sysfs_free_irq(dev); @@ -292,9 +278,6 @@ static ssize_t edge_store(struct device *dev, status =3D size; } =20 -out_unlock: - mutex_unlock(&data->mutex); - return status; } static DEVICE_ATTR_RW(edge); @@ -330,11 +313,8 @@ static ssize_t active_low_show(struct device *dev, struct gpio_desc *desc =3D data->desc; int value; =20 - mutex_lock(&data->mutex); - - value =3D !!test_bit(FLAG_ACTIVE_LOW, &desc->flags); - - mutex_unlock(&data->mutex); + scoped_guard(mutex, &data->mutex) + value =3D !!test_bit(FLAG_ACTIVE_LOW, &desc->flags); =20 return sysfs_emit(buf, "%d\n", value); } @@ -350,11 +330,8 @@ static ssize_t active_low_store(struct device *dev, if (status) return status; =20 - mutex_lock(&data->mutex); - - status =3D gpio_sysfs_set_active_low(dev, value); - - mutex_unlock(&data->mutex); + scoped_guard(mutex, &data->mutex) + status =3D gpio_sysfs_set_active_low(dev, value); =20 return status ? : size; } --=20 2.45.2