From nobody Mon Jun 8 23:56:25 2026 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 D6E1D2F28FC for ; Mon, 25 May 2026 11:01:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779706905; cv=none; b=hXC0nssXP8SXVWYKD0baKbi71PUK/ceSPHvLthQLRUlrffjj4ZUkhzmdRxvxujcKieOciGWp/Q0wME3R5SRgNYi6QOgdByi43qxLcR5RA7QxkkguwDzIES/mXYtdj/r/m0kclg4rCLfjBzyf96y6YGPyBZBPHryZJcjXOirEaBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779706905; c=relaxed/simple; bh=Zos+qgIjbJfieklSGDIwVMAHBFpTdFUw3LBl++UHaK0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=EqaAwOGen62PSdkGusEO8JzdND5Rsp3ZIjO8M7gLofqX9XYspUo1xOY4tG5U2l3y2r4u1ykhfBvFR7xjK/v2ZwI4+1u/1/qAOy4ClGRZNK2XURggsU0EGM0y4pBg3MJfvklNZtCs+fXIJiPlOJTbVIKary69H3fFEShKT4DOn0U= 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=Zpmb85D0; arc=none smtp.client-ip=209.85.128.46 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="Zpmb85D0" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-4891c00e7aeso65458535e9.2 for ; Mon, 25 May 2026 04:01:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779706901; x=1780311701; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gwzakygw4EN8seZI+IQRrpcLpC9qbJLKNAylpwQVQ+A=; b=Zpmb85D0FlH8/eppoLD7xgJKheTBBdDNbhCfJSYSQiuerAkOdjX0Oy+4047wSEdObQ miRKJnE4bVWnkX/5Znw2Z8jbWyVzzBGX54KWF1QoZnHscHYop+p6wMN9R/N4XTJWYrGv zI9dwoDbtXd+f3dcXaxbTGw4xzS9WeNSPZlNb0LY+PFNkr0IL3MDybE7ifL/mF5Kf5ip zE5gY5zn7iq7kaLD6BTM2fVsAZ5OCAWiMWwuLTXKCu3Klh/rsunPbTisA0ies/GiQbQG IfUbQVFKhJjwaPjZEAl8m3TCEDL8R4ym10DZyPBO7ID50m4y9MD/tZ+uGvxAEs/g9lCX MlGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779706901; x=1780311701; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gwzakygw4EN8seZI+IQRrpcLpC9qbJLKNAylpwQVQ+A=; b=k9xxE3ryd5s2EmnDobPvRIwgsA4J/dUWlp3q4T/Ppb4hVqpmojmZnbev9gRzUC1tOJ CJ6eHCxuCvKJNnRWXf/Nw1otvmIfkyWz3/tdD6baj1vPAyHakCIVfEHquOepzrRu8tQN Afh6bpWTsC8hMnAdfekcBWVTVrkVqyO6GL8O/5z3fV2QZ6/wrLU2XvJeEbTzjbVA7EXo uRZy8FqExb+IVe4c1L6yyMUpcaOU2opsV/9EztHg+YbdeIy3Inko6sJzFPcE9+bRmVR7 hXk1ItsmtPmDjYPPxZL20EDiokQTfZ/hVRlvmnXg19aLn4Qgp2p4vKmsnVRw3dMDpLMY NCbw== X-Forwarded-Encrypted: i=1; AFNElJ+MK8BIcp3ALuGrskRELLfe571HcPopVRYQxMjfN1ZzmQ01tc+gn3b76fHt8nv7ZCmmpQxU7AjsFylftqA=@vger.kernel.org X-Gm-Message-State: AOJu0YxVSmygQvEQWmbs+u+/NByS8c/s/YHIBbOxt9loOE+smQyG+61Y AtvUixeaGasH5g9Y5GbFjyIRAfJLohvKrih2aLqTWtJOTjpxG5nwwcYg+c7c//uw180= X-Gm-Gg: Acq92OH12VLumUw4FrQQlEISn5ZpRFAIVgBEPnuyEhFC/gFfAMQyUIqrWfV+d7Bn3uV 1UBXct/MBTELA5b7WWFNJGTUdlfk/5HlDmEhoSFb98eDYnZcuDIWA5st8wclCIuWGENCtmc2UY4 HbtlmpT/0Nky+J1FpcZ5cCXF0JzT+KSOV9RHXRsTCbVEtIv4c08M/KUSJc7V8FNc89bhg5JSLzI 3ELtD8HOgrOhGAP2yi6yc3vQEdoFOcjbUu09IRbj2fS39ReffBfeGm5Ywbx+bfnEdQ36SoZRa/W X7am6SrB+VozsImMPg3Vm8ZtB8e6RJO8csGbXiD7xtzYJMqAlHlwGlG7v2+4E9cajAg3dFVb5ND 2HMpuSlRAwigWvyClnC1idjvm3eVJcf/DpiJJHmgZ7bGvcLud5eBslpl1K2PWSq9eGts3Y0LfoQ A4cSnBcRK4G5lwD3HuhXWeSrZbQpgHz1UZ3UJwR5gbKL2435GFsJQr X-Received: by 2002:a05:600d:640f:10b0:490:3ff5:737f with SMTP id 5b1f17b1804b1-490426cefe0mr164507855e9.18.1779706900959; Mon, 25 May 2026 04:01:40 -0700 (PDT) Received: from localhost.localdomain ([2001:8a0:6c9b:a000:e928:104a:57a8:6fa9]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49042aeafa8sm80034525e9.27.2026.05.25.04.01.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2026 04:01:39 -0700 (PDT) From: Gabriel Rondon To: jic23@kernel.org Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, andy@kernel.org, nuno.sa@analog.com, dlechner@baylibre.com, sozdayvek@gmail.com Subject: [PATCH v3] iio: accel: bmc150: use guard(mutex) for mutex handling Date: Mon, 25 May 2026 12:01:27 +0100 Message-Id: <20260525110130.61284-1-grondon@gmail.com> X-Mailer: git-send-email 2.33.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace manual mutex_lock()/mutex_unlock() pairs with guard(mutex) and scoped_guard() from cleanup.h. This simplifies error paths by removing the need for explicit unlock calls before returning. Most converted functions hold the lock for their entire body, so guard(mutex) applies directly. bmc150_accel_trigger_handler() only holds the lock around a single register read, so scoped_guard() is used there to keep the lock scope unchanged. Signed-off-by: Gabriel Rondon Reviewed-by: Stepan Ionichev Reviewed-by: Maxwell Doose --- Changes since v2: - bmc150_accel_get_fifo_state(): normalize fifo_mode to 0/1 in the sysfs output (data->fifo_mode ? 1 : 0). v2 dropped the bool intermediate and printed the raw mode value, which would emit the FIFO mode bit (0x40) as 64 instead of 0/1. The original 0/1 output is preserved. Reported by Jonathan Cameron. - Collected Reviewed-by from Stepan Ionichev. Changes since v1: - Drop the verbose list of converted functions from the commit message. - bmc150_accel_get_temp(): keep the original declaration order; no unrelated line movement. - bmc150_accel_get_axis(): convert to guard(mutex) as well, the only code outside the old lock scope was a trivial error check. - bmc150_accel_trigger_handler(): use scoped_guard() so that no manual mutex_lock()/mutex_unlock() pairs are left behind. - bmc150_accel_get_fifo_watermark() / bmc150_accel_get_fifo_state(): drop the intermediate variable and return directly. drivers/iio/accel/bmc150-accel-core.c | 68 ++++++++------------------- 1 file changed, 20 insertions(+), 48 deletions(-) diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc1= 50-accel-core.c index 2398eb7e12cd..e8c40aa0375d 100644 --- a/drivers/iio/accel/bmc150-accel-core.c +++ b/drivers/iio/accel/bmc150-accel-core.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -600,18 +601,15 @@ static int bmc150_accel_get_temp(struct bmc150_accel_= data *data, int *val) int ret; unsigned int value; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); =20 ret =3D regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value); if (ret < 0) { dev_err(dev, "Error reading reg_temp\n"); - mutex_unlock(&data->mutex); return ret; } *val =3D sign_extend32(value, 7); =20 - mutex_unlock(&data->mutex); - return IIO_VAL_INT; } =20 @@ -624,25 +622,21 @@ static int bmc150_accel_get_axis(struct bmc150_accel_= data *data, int axis =3D chan->scan_index; __le16 raw_val; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); ret =3D bmc150_accel_set_power_state(data, true); - if (ret < 0) { - mutex_unlock(&data->mutex); + if (ret < 0) return ret; - } =20 ret =3D regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis), &raw_val, sizeof(raw_val)); if (ret < 0) { dev_err(dev, "Error reading axis %d\n", axis); bmc150_accel_set_power_state(data, false); - mutex_unlock(&data->mutex); return ret; } *val =3D sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift, chan->scan_type.realbits - 1); ret =3D bmc150_accel_set_power_state(data, false); - mutex_unlock(&data->mutex); if (ret < 0) return ret; =20 @@ -810,17 +804,14 @@ static int bmc150_accel_write_event_config(struct iio= _dev *indio_dev, if (state =3D=3D data->ev_enable_state) return 0; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); =20 ret =3D bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION, state); - if (ret < 0) { - mutex_unlock(&data->mutex); + if (ret < 0) return ret; - } =20 data->ev_enable_state =3D state; - mutex_unlock(&data->mutex); =20 return 0; } @@ -845,13 +836,10 @@ static ssize_t bmc150_accel_get_fifo_watermark(struct= device *dev, { struct iio_dev *indio_dev =3D dev_to_iio_dev(dev); struct bmc150_accel_data *data =3D iio_priv(indio_dev); - int wm; =20 - mutex_lock(&data->mutex); - wm =3D data->watermark; - mutex_unlock(&data->mutex); + guard(mutex)(&data->mutex); =20 - return sysfs_emit(buf, "%d\n", wm); + return sysfs_emit(buf, "%d\n", data->watermark); } =20 static ssize_t bmc150_accel_get_fifo_state(struct device *dev, @@ -860,13 +848,10 @@ static ssize_t bmc150_accel_get_fifo_state(struct dev= ice *dev, { struct iio_dev *indio_dev =3D dev_to_iio_dev(dev); struct bmc150_accel_data *data =3D iio_priv(indio_dev); - bool state; =20 - mutex_lock(&data->mutex); - state =3D data->fifo_mode; - mutex_unlock(&data->mutex); + guard(mutex)(&data->mutex); =20 - return sysfs_emit(buf, "%d\n", state); + return sysfs_emit(buf, "%d\n", data->fifo_mode ? 1 : 0); } =20 static const struct iio_mount_matrix * @@ -906,9 +891,8 @@ static int bmc150_accel_set_watermark(struct iio_dev *i= ndio_dev, unsigned val) if (val > BMC150_ACCEL_FIFO_LENGTH) val =3D BMC150_ACCEL_FIFO_LENGTH; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); data->watermark =3D val; - mutex_unlock(&data->mutex); =20 return 0; } @@ -1021,13 +1005,10 @@ static int __bmc150_accel_fifo_flush(struct iio_dev= *indio_dev, static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned sam= ples) { struct bmc150_accel_data *data =3D iio_priv(indio_dev); - int ret; =20 - mutex_lock(&data->mutex); - ret =3D __bmc150_accel_fifo_flush(indio_dev, samples, false); - mutex_unlock(&data->mutex); + guard(mutex)(&data->mutex); =20 - return ret; + return __bmc150_accel_fifo_flush(indio_dev, samples, false); } =20 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( @@ -1187,10 +1168,9 @@ static irqreturn_t bmc150_accel_trigger_handler(int = irq, void *p) struct bmc150_accel_data *data =3D iio_priv(indio_dev); int ret; =20 - mutex_lock(&data->mutex); - ret =3D regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L, - data->buffer, AXIS_MAX * 2); - mutex_unlock(&data->mutex); + scoped_guard(mutex, &data->mutex) + ret =3D regmap_bulk_read(data->regmap, BMC150_ACCEL_REG_XOUT_L, + data->buffer, AXIS_MAX * 2); if (ret < 0) goto err_read; =20 @@ -1230,31 +1210,23 @@ static int bmc150_accel_trigger_set_state(struct ii= o_trigger *trig, struct bmc150_accel_data *data =3D t->data; int ret; =20 - mutex_lock(&data->mutex); + guard(mutex)(&data->mutex); =20 - if (t->enabled =3D=3D state) { - mutex_unlock(&data->mutex); + if (t->enabled =3D=3D state) return 0; - } =20 if (t->setup) { ret =3D t->setup(t, state); - if (ret < 0) { - mutex_unlock(&data->mutex); + if (ret < 0) return ret; - } } =20 ret =3D bmc150_accel_set_interrupt(data, t->intr, state); - if (ret < 0) { - mutex_unlock(&data->mutex); + if (ret < 0) return ret; - } =20 t->enabled =3D state; =20 - mutex_unlock(&data->mutex); - return ret; } =20 --=20 2.33.0