From nobody Mon Nov 25 02:03:51 2024 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 583531CB33B for ; Thu, 31 Oct 2024 20:02:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730404927; cv=none; b=ctjnv0920y+Qqq5M7QqKXCnFH9U2BDBE0mYQuTSg1c6Y9njL1lYHltGtKOPbP2+/aBGQOkRg2xAQD1oVRhuBXyY5hbnFHFglhxMKgJ4GTm86rGvQSmbAy5F40wmbKYD4q22QnNcopG2D8kRNQIYaL60yCEHHC622HNOO+hxNIi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730404927; c=relaxed/simple; bh=lxfcHFlINBm4B273RJGWT9N7EfQtgO45pd+YAFioyAU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=chD+gVKStXQB2m1WncjdOFLPIoTZ0yG3B8cB1JqBQdFI/160wr+bfKwHpqUKdAEcu8uu6/h7j1ZQ70SnwyrEkiv72V7PgKUQUEh805HLz+v0z03uV/uhgT3G19ynLz5g0SRTHKFfa2xi3Ct+koN7FKOuvvjrfG3Ly/U1VRU9oQY= 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=A1vdRIA/; arc=none smtp.client-ip=209.85.128.52 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="A1vdRIA/" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-43167ff0f91so11800945e9.1 for ; Thu, 31 Oct 2024 13:02:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1730404923; x=1731009723; 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=yzrxfr7wPhfTvrfVJLIuoZ9JjJdY5AB7ATojPjTFoR4=; b=A1vdRIA/j6CFHA585KI3akgUqOsi94+PdSdv9J0zO10hiDR2DX0XI4dljLleAgg9Nr X4PGJwG+YZk2aht6+NFEjLFE0poCcIkv2ApIejkVUUwP8P8cp1xyK5viAF7OUHIP0LBm JGayKPkwKHeyy666jM9WwzAOOXO3p7xK/dXk8f/MMTTqV9g43H9tJjnc+TRF9wd2l3+b IuVDgmtM960x98VBn45t3Obg7eFZ0tRACBoYvS1ePGDZvhc3ZkwPCxF8uKbro1EZC7G3 aQPawEFtEqPHUBksukHt5KpbN2QwJHgC6Z8TCHlF5dVduZADBToPmTq5RD9sbrzIt6WM u1NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730404923; x=1731009723; 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=yzrxfr7wPhfTvrfVJLIuoZ9JjJdY5AB7ATojPjTFoR4=; b=iPgxiwkWCXtVKnj0dQ/be6oGIS5QY8GXUuI+FJPE+T5iOLywRXpRiKkD0fHJrwqc5D QtxkDmTj2TajA4xzXr+mjSPzHau6UOCMZd9IZkAxLw9ZBoQmIwiZQQ1nkxuU5E4xUDWY UMHws5cy6wQfR78dB5FZ+IxPkNiWXYBoC4ePV7WgaGQMh1dU1Y6ro5qsBMgSmp/FiMgt X4yjjiGofnvH7Mdv7PfGYYefIp0uScukVoTCSUpS6d/bOB5kvUV7xfOi40tXMfoMi/d0 iTg07Gm+3ku4wxhtzuxnXDgQ773oj73bX/kwMoUMcNzDFDlbpMzkHL5KXtnzx/ctJELt zb5A== X-Forwarded-Encrypted: i=1; AJvYcCWN6RLE21CZG8unNA7G5z/k9IQH75LkT8ZQKsHSEcVsVp1P2RezP3FlZSO7TykcPr7e3A6bThyMzh3/zPU=@vger.kernel.org X-Gm-Message-State: AOJu0YxJmj0T4OBRAjwAwGm1tg93mxSWCKDNYmjvfEROcBvruWpcUe1T xJJMOm7zkDUd/WPuDxsAhq0kWP0cgZUeIELULfnZoU4FcIhSRtWzv/M9r5hkb4w= X-Google-Smtp-Source: AGHT+IGgFm4zjZjWhXC7650nVqWNS1PFsWNFmkOgJQXkZXvXD+FgAP7Ug+L614sjMLXKokJuGBunpw== X-Received: by 2002:a5d:5988:0:b0:37d:30e7:3865 with SMTP id ffacd0b85a97d-381be7d63e0mr3768793f8f.34.1730404923552; Thu, 31 Oct 2024 13:02:03 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:dc:7e00:f39e:5519:d578:740]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-381c1189123sm3003993f8f.115.2024.10.31.13.02.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 13:02:02 -0700 (PDT) From: Bartosz Golaszewski Date: Thu, 31 Oct 2024 21:01:52 +0100 Subject: [PATCH v4 2/5] gpio: sysfs: use cleanup guards for the sysfs_lock 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: <20241031-gpio-notify-sysfs-v4-2-142021c2195c@linaro.org> References: <20241031-gpio-notify-sysfs-v4-0-142021c2195c@linaro.org> In-Reply-To: <20241031-gpio-notify-sysfs-v4-0-142021c2195c@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=2976; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=hQbFwd1QT9qA/6G04ep+yw6mU1491E7zvhbWfpDUDoM=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBnI+I2RfXIbXJmBgVj3RdDRuu1VHrPbMrDmhip1 i4/Nv5KCieJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZyPiNgAKCRARpy6gFHHX cuR0EADJxy3vXb63V7EH/zHNUnjvJN4b/Jnp/CIAd6WwUskF72NdGGNHTRQzMXEMvcgewrEk2hR 14WOrVchK+ryfS85FkruV8qf94ButgWv0rjh7tf8DvkI+kqQD69+tC70yTCZ4pHOte0Z5zEc2JB PNXUjSFL9RhoeBXWPE4ZsprN5UmtRx9XrNjCCKolq1PrDaGeBnZYrF9DYcGf00hrpTwufg1CbRH mSJ5PnXmCOzGbVVP5J/4dZ4M8RgXYDHr67ZRUAkwRWvginLBwNA5ZZCDKl+LRWbXeSxdhO9uK4C F3Vgeu2H1LBBD96tIzyGVu+rwFlnl+rYBk1Z4xIph6tyfa5vgFfaoMaxBLfKcrZVKj+OHdOZpvl /znYWNXA26/7FinsE5rwGOfKlgJ4NFYrtzsds98qre84LJUw8ZDKwDdaayU7jPP+9siATW0hu4Q jPBJuanaliUmNcxiJNeBvpxqHn2VEs3z+44kvOx/qYZoHg/os6TWE7Un1hKvQorNldyIqZqhHpK 0uW6Q8yuREGLdr4ez609UwHl8Z9XY2BK6oafvDoN1aay7an4wYP1ex8WBvHoy9zge+nlppY8E9G ZA/J09WyVgumZ5XgKYXsyczyHTyAbOePwFViM0D0iEHNWmRQieoyiOAV843wD3yLDfyVnN5cvQZ fK96CAgt4iPXWDQ== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 From: Bartosz Golaszewski Shrink the code and remove some goto labels by using guards around the sysfs_lock mutex. Signed-off-by: Bartosz Golaszewski --- drivers/gpio/gpiolib-sysfs.c | 55 ++++++++++++++++++----------------------= ---- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index a0926a1061ae..e6c1e26f302d 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -575,24 +575,24 @@ int gpiod_export(struct gpio_desc *desc, bool directi= on_may_change) =20 gdev =3D desc->gdev; =20 - mutex_lock(&sysfs_lock); + guard(mutex)(&sysfs_lock); =20 /* check if chip is being removed */ if (!gdev->mockdev) { status =3D -ENODEV; - goto err_unlock; + goto err_clear_bit; } =20 if (!test_bit(FLAG_REQUESTED, &desc->flags)) { gpiod_dbg(desc, "%s: unavailable (not requested)\n", __func__); status =3D -EPERM; - goto err_unlock; + goto err_clear_bit; } =20 data =3D kzalloc(sizeof(*data), GFP_KERNEL); if (!data) { status =3D -ENOMEM; - goto err_unlock; + goto err_clear_bit; } =20 data->desc =3D desc; @@ -610,13 +610,11 @@ int gpiod_export(struct gpio_desc *desc, bool directi= on_may_change) goto err_free_data; } =20 - mutex_unlock(&sysfs_lock); return 0; =20 err_free_data: kfree(data); -err_unlock: - mutex_unlock(&sysfs_lock); +err_clear_bit: clear_bit(FLAG_EXPORT, &desc->flags); gpiod_dbg(desc, "%s: status %d\n", __func__, status); return status; @@ -680,36 +678,28 @@ void gpiod_unexport(struct gpio_desc *desc) return; } =20 - mutex_lock(&sysfs_lock); + scoped_guard(mutex, &sysfs_lock) { + if (!test_bit(FLAG_EXPORT, &desc->flags)) + return; =20 - if (!test_bit(FLAG_EXPORT, &desc->flags)) - goto err_unlock; + dev =3D class_find_device(&gpio_class, NULL, desc, match_export); + if (!dev) + return; =20 - dev =3D class_find_device(&gpio_class, NULL, desc, match_export); - if (!dev) - goto err_unlock; + data =3D dev_get_drvdata(dev); + clear_bit(FLAG_EXPORT, &desc->flags); + device_unregister(dev); =20 - data =3D dev_get_drvdata(dev); - - clear_bit(FLAG_EXPORT, &desc->flags); - - device_unregister(dev); - - /* - * Release irq after deregistration to prevent race with edge_store. - */ - if (data->irq_flags) - gpio_sysfs_free_irq(dev); - - mutex_unlock(&sysfs_lock); + /* + * Release irq after deregistration to prevent race with + * edge_store. + */ + if (data->irq_flags) + gpio_sysfs_free_irq(dev); + } =20 put_device(dev); kfree(data); - - return; - -err_unlock: - mutex_unlock(&sysfs_lock); } EXPORT_SYMBOL_GPL(gpiod_unexport); =20 @@ -750,9 +740,8 @@ int gpiochip_sysfs_register(struct gpio_device *gdev) if (IS_ERR(dev)) return PTR_ERR(dev); =20 - mutex_lock(&sysfs_lock); + guard(mutex)(&sysfs_lock); gdev->mockdev =3D dev; - mutex_unlock(&sysfs_lock); =20 return 0; } --=20 2.45.2