From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.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 C34EA2472A2 for ; Mon, 16 Feb 2026 00:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771203220; cv=none; b=feyJ+fMU+mMbC0RtJfVCV9K/AM8DOIQ5zp4BVyNDyOvaQiYTncoW5Ilxb/NsVAR1YGHBzJbTySDzh+er08MyH3Fz7un15LZwCLBm1/ANTkqM1Wrw6lB/YZ/UV0HPjZD1lwOUhx9knxKspiTzpSPG/JtXO74FedO/wkmoPHPQ0pk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771203220; c=relaxed/simple; bh=TBLSrqBeZomDV2gMZy7BCzI9rhkp7P+o3Dt33E5ZQvQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aCiG3s1Y691JId3J6g5AB83VHuhlwd5PXltdOqjdQrD28iPaO0YJ1uWkxfRRn7Ib6Inp185MTGjuUe4ttxoLUF+x5IxprD9yXU4Q1kedRK0itn+70grzSi4vRw8uHpAbYSUs2P5qdLaAOGApvvCBODKmMq6IGWGvTWI0IIW0c7A= 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=QFPO2+pV; arc=none smtp.client-ip=209.85.128.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="QFPO2+pV" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-7927261a3acso22661327b3.0 for ; Sun, 15 Feb 2026 16:53:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771203218; x=1771808018; 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=TEPsTH3PriBiWEFGViHWLoH9Qxt8oTMiRXWW0uvcBQY=; b=QFPO2+pVWLu2MiHeMqPwcZsvQc81mH6xZsqgW5OwGR+cgYo89ZZMxomYjOXIQnv5/9 fsMt5y5GB8YDqcmY7O7u37geQcPmLNYUHttscQjSsEtJI7GZc5MME85EfBIQLL+aHy2g jhEwELhfF6vI17O5xU1KZdV9428C7BK+bqKiEH6eNOhkkZW16FwDp+vr5nd4n/dIsHdu N7Ki2RKqtHAcGgKtthFlWBEJhNWQjXrt/2+G75iAyYax5NMLE5lduCQ5CbHZcPxU3Le1 Nnkz4431c3205OG0lxz8UJBFFK9owCrPZYTZrWP8DJ1WCD5FHMZWH1f9jQa7tiGM3tji rbbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771203218; x=1771808018; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=TEPsTH3PriBiWEFGViHWLoH9Qxt8oTMiRXWW0uvcBQY=; b=lt1DMntgA+WKxjlB0eyGbFyor8CvNXU134w7niDaCYJDoR4KDMjSDi5QJ+dCPY8wCd CicWeePDCcJYGKuNzONvg6peypo6vbBR3hnQieSN0NriebX/73U7ekNP4YNK79wDVpCR 4Cu/40Byauu2jKdJrI2RhGK3H+T25X9OKaOqGeXWDrbqriotIeASw26qhfBZrIP8Ikb2 XWn1yH5MUrugzrFYdTpBNGHZDjW2txkpbOQMIoElyPdd+dXyAag13sv+p00WDZQaeoN2 B+R3ufEUYwGRAUN+j3HbjECNl0Dnau9FOgZmN08ZrCAWPq+SCgvJtyuXmr9aKd91Oand uCKg== X-Forwarded-Encrypted: i=1; AJvYcCVryaJRBlFKPw28AZ0XlwkGawaaxDmcFpdsVajfoBKXwZNKWSEDH38jnjp/NX4zHylfV76x+U1d2OFODOQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/UWFt7WVR+m1Na3dZS1RfTF9E+VAVPyB2/UWGZ1s6070QzFZn SiGS9UCRW6+zY4WkL7pz370/xepAVM8+t2ivvWq6bBUUVKLLIjDIInHT X-Gm-Gg: AZuq6aLOMhhrUsma5EXjaWo/mXS2cJLI9ISw5eDon8jE96HvxZdWdYiE1kR9cesRxK+ B0ehlkkiuX7l5jUzrXI8KE9grxCfagXP4aCAuIVo+ZIAA+tNXoMQXmt3uNPJxO2iI0Qjvfa/kFx C6ilnqyxBgx+TG4UszZvVO25kW6Mmib1DkUaEzYjprRggb6K8Fu6IHO6LtLBoyK9WvnisboNGE/ dI7G0iiJkRuqhhXGnhdF2ScUNRXxp9nwk7PyK0meolMz/1C6FqMeO1ZOGihq2jkwJxYe4AdZPNT N3kXq6dplBa7TF41C7tqU5nWSmkIL3fEjXehpRAgCETf86gDtopWqaTO5ZzhIUYc3aynuRMdSHI IstGvO4EiZ0FFo2MkGVdoaGbECTldSmyfgPsrY/qgmGZ4cPZF3fV7xlsca/mShGJtxgu4qtDQ7s FJ7rP+lvMvawa3Wy8QPgN+98qKIceVzMCuWrF4vayZjYZBXDro7K7gw0fWguqgQUHLCBHTd94BH 0Licbxt4U4wPJYcWWyB/woca6kFQK04dQ7lWaxpjAc= X-Received: by 2002:a05:690c:1a:b0:796:3c5d:70bf with SMTP id 00721157ae682-797ac5570c3mr48531827b3.29.1771203217683; Sun, 15 Feb 2026 16:53:37 -0800 (PST) Received: from tux ([2601:7c0:c37c:4c00:e3a8:26f7:7e08:88e1]) by smtp.gmail.com with ESMTPSA id 00721157ae682-7966c257ed5sm94797637b3.45.2026.02.15.16.53.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Feb 2026 16:53:37 -0800 (PST) From: Ethan Tidmore To: jic23@kernel.org Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Ethan Tidmore Subject: [PATCH v3 1/2] iio: light: gp2ap020a00f: simplify locking with guard() Date: Sun, 15 Feb 2026 18:53:16 -0600 Message-ID: <20260216005317.11763-2-ethantidmore06@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260216005317.11763-1-ethantidmore06@gmail.com> References: <20260216005317.11763-1-ethantidmore06@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Use the guard() cleanup handler to manage the device lock. This simplifies the code by removing the need for manual unlocking and goto error handling paths. Suggested-by: Jonathan Cameron Signed-off-by: Ethan Tidmore --- v3: - Use guard() instead of manual locking and unlocking. drivers/iio/light/gp2ap020a00f.c | 64 ++++++++++---------------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/drivers/iio/light/gp2ap020a00f.c b/drivers/iio/light/gp2ap020a00f.c index c7df4b258e2c..d1a511ac7690 100644 --- a/drivers/iio/light/gp2ap020a00f.c +++ b/drivers/iio/light/gp2ap020a00f.c @@ -31,6 +31,7 @@ * the other one. */ +#include #include #include #include @@ -1024,17 +1025,14 @@ static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev, bool event_en = false; u8 thresh_val_id; u8 thresh_reg_l; - int err = 0; - mutex_lock(&data->lock); + guard(mutex)(&data->lock); thresh_reg_l = gp2ap020a00f_get_thresh_reg(chan, dir); thresh_val_id = GP2AP020A00F_THRESH_VAL_ID(thresh_reg_l); - if (thresh_val_id > GP2AP020A00F_THRESH_PH) { - err = -EINVAL; - goto error_unlock; - } + if (thresh_val_id > GP2AP020A00F_THRESH_PH) + return -EINVAL; switch (thresh_reg_l) { case GP2AP020A00F_TH_L_REG: @@ -1046,30 +1044,23 @@ static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev, &data->flags); break; case GP2AP020A00F_PH_L_REG: - if (val == 0) { - err = -EINVAL; - goto error_unlock; - } + if (val == 0) + return -EINVAL; + event_en = test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV, &data->flags); break; case GP2AP020A00F_PL_L_REG: - if (val == 0) { - err = -EINVAL; - goto error_unlock; - } + if (val == 0) + return -EINVAL; + event_en = test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV, &data->flags); break; } data->thresh_val[thresh_val_id] = val; - err = gp2ap020a00f_write_event_threshold(data, thresh_val_id, - event_en); -error_unlock: - mutex_unlock(&data->lock); - - return err; + return gp2ap020a00f_write_event_threshold(data, thresh_val_id, event_en); } static int gp2ap020a00f_read_event_val(struct iio_dev *indio_dev, @@ -1081,23 +1072,17 @@ static int gp2ap020a00f_read_event_val(struct iio_dev *indio_dev, { struct gp2ap020a00f_data *data = iio_priv(indio_dev); u8 thresh_reg_l; - int err = IIO_VAL_INT; - mutex_lock(&data->lock); + guard(mutex)(&data->lock); thresh_reg_l = gp2ap020a00f_get_thresh_reg(chan, dir); - if (thresh_reg_l > GP2AP020A00F_PH_L_REG) { - err = -EINVAL; - goto error_unlock; - } + if (thresh_reg_l > GP2AP020A00F_PH_L_REG) + return -EINVAL; *val = data->thresh_val[GP2AP020A00F_THRESH_VAL_ID(thresh_reg_l)]; -error_unlock: - mutex_unlock(&data->lock); - - return err; + return IIO_VAL_INT; } static int gp2ap020a00f_write_prox_event_config(struct iio_dev *indio_dev, @@ -1165,7 +1150,7 @@ static int gp2ap020a00f_write_event_config(struct iio_dev *indio_dev, enum gp2ap020a00f_cmd cmd; int err; - mutex_lock(&data->lock); + guard(mutex)(&data->lock); switch (chan->type) { case IIO_PROXIMITY: @@ -1186,8 +1171,6 @@ static int gp2ap020a00f_write_event_config(struct iio_dev *indio_dev, err = -EINVAL; } - mutex_unlock(&data->lock); - return err; } @@ -1199,7 +1182,7 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev, struct gp2ap020a00f_data *data = iio_priv(indio_dev); int event_en = 0; - mutex_lock(&data->lock); + guard(mutex)(&data->lock); switch (chan->type) { case IIO_PROXIMITY: @@ -1223,8 +1206,6 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev, break; } - mutex_unlock(&data->lock); - return event_en; } @@ -1385,7 +1366,7 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) struct gp2ap020a00f_data *data = iio_priv(indio_dev); int i, err = 0; - mutex_lock(&data->lock); + guard(mutex)(&data->lock); /* * Enable triggers according to the scan_mask. Enabling either @@ -1413,15 +1394,12 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) } if (err < 0) - goto error_unlock; + return err; data->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); if (!data->buffer) err = -ENOMEM; -error_unlock: - mutex_unlock(&data->lock); - return err; } @@ -1430,7 +1408,7 @@ static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev) struct gp2ap020a00f_data *data = iio_priv(indio_dev); int i, err = 0; - mutex_lock(&data->lock); + guard(mutex)(&data->lock); iio_for_each_active_channel(indio_dev, i) { switch (i) { @@ -1452,8 +1430,6 @@ static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev) if (err == 0) kfree(data->buffer); - mutex_unlock(&data->lock); - return err; } -- 2.53.0