From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 F1CB418AE2 for ; Sat, 14 Feb 2026 16:28:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771086492; cv=none; b=RBo0GUsJZgQiBgMILlBd556WZbosS/uqCRniw4NZR8+gXBKudRd2v005kGnAmqIrVXtRvELjELxpcGdCtGHIvj2orl9Cl29ob3ogKBA205opos1RGi7MaALBN/KtfwQ8Di2dRT7nSZxS2SglS+UHIPRftA5/Ob/vfDsDmrdFQ04= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771086492; c=relaxed/simple; bh=PmDN7Am92anJ3w+2U6QHfhG7MjAie5xXtL2rRG62c+A=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ZoKPc39ahNTgLxd+6pPXqSawEO2vgbLghuE0NTnvaJbIRFyvDVfxzGENR27+GbSysP6oe1yWplz1XqNAE0gHh8LcGZk6/V22zGZQE2uJRUZOrFwvMgJw35No5DPhkY/BSzbkUPFhlheHUgP1ecYy6La6RcCLeWKKrF/BJKsKx5A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=F+EgUh73; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="F+EgUh73" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-7d4c4cacf78so619096a34.0 for ; Sat, 14 Feb 2026 08:28:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1771086489; x=1771691289; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=T+8pno1sBRzUIHg5L96yyIS/XItd6cwvwphuXPZkTb8=; b=F+EgUh7392wzMqvDX+7AIZf4JrKmuOq7vo/q/USjfC8LYhXQOxraA2GiUgMpdqwWWa w6wAuCOMeeWrEVAH/Iev2mws+jK2q6p+pdCt4I/XDzvomJzdAbTR24V7Cu3uHNqRfynU ZLnzIDBThoxH+c8ht0xJBcTXuCGlHpofFAsqA9nT7Y1HtDGuETbJkNDOZjKNIiHczBG+ 7Tf/0naDS0c2fp4ghTXpCcZShtNC4pLMAriRQjnL+f98fRfUoJWOnSgw2ETDPAOD075Z IF2e2LES2KxSI5BeAwV2450JpEs28tjF0QLjdef1ZqdB69cHrzTHYSFPLeSM3IXHUtV4 /LNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771086489; x=1771691289; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=T+8pno1sBRzUIHg5L96yyIS/XItd6cwvwphuXPZkTb8=; b=pNx5B3qBiXdYS3FLOBIDAe760K3RbJkh2IVefbgcSA1C3gJRkoS0kgzXZ9VzSwAtOG 1Sk8B3FI4v2mUYqtRWfKeuEkvN6vW90RI/8c+KHgqyDzEESPDooPn2ieH7/lsPb7WvsK 7jWil8iyr7QZSIKqxcXIZ+EFYIYCh5VOc7fglAxobSceutFy6YlEc1XdMEIAQht+8t9R nhX/KvjsJokY4+bGA8GGYIjQVZyRwtSXxnlNMvPzCG8k2Ww9Z2itIFpT0A2TsDb/lLEY BgumldKlInFPWQlrbWBNEWn4Do0hInX/ZL1okATOYodG1rUeuDWz+6Vm7d6S30EGmwVg RBTQ== X-Forwarded-Encrypted: i=1; AJvYcCUuwnqYHz3HgoCFrnxbVP+GMl3mmJ358kEqJ2fgROKnpY4/4pCkO69Sr+MiNudCrcamplPKqYZiNKpa8mc=@vger.kernel.org X-Gm-Message-State: AOJu0YwOIlNKR5dGbcuzNceHLfqz8OnOj/mHJU1zKmdU0YarCPQjBk24 v0OJvwowkbv9Yey6SGMAWrlZJHB0whtgZku1atIF6NWrCXHoCdQXZADcreK+isN2CJY= X-Gm-Gg: AZuq6aLorX7ZriHVV1tk0EoUD2fJS9cVWlCCtNUkeQC5+bQ3g9KzaxSWW/2hfK10e88 MU6Pl4CKul4yTPsuIcnOZbbyL5MBWxqtBsf+eDixE5/f7AjiOd4Fhx56vXHu6uANBslW8kNUADp 0PEVO5gtGhTf9quftTlqlImd/IDJkX5dbVoM605qFuw8vaVDgSvdUiAZTVudfNdAv5HxzUE4toW /ufPBLYW2h86RfKT7K4lWCh85tB/VE1vGAc2U7Rp/RRC6GgAof+4Jumoou6TplVTIP82wv43EBA Ncd9Hj/1qI9KU4WLQw9wGs+rfklgsTZv76ZAkprV2+0DhtIiiKCUMrh8HV6l9nPYraOC9J4S0mW cpWhrpM96uvXlBCb9EjcbpMLvKO6qosysnevqvuzHmyB+nlBS0GggvSnNzyYjEiyMfRiJ8AEz4A IxlHC4Mgzdboe3Vyx+RjZ7JZ9RVtni9egzXN3YCMzYB6kkd0RJymCoWuiPalqalWCHR54MHQ== X-Received: by 2002:a05:6830:82bd:b0:7cf:d742:9d79 with SMTP id 46e09a7af769-7d4d0c42385mr1394916a34.30.1771086488880; Sat, 14 Feb 2026 08:28:08 -0800 (PST) Received: from ?IPV6:2600:8803:e7e4:500:109:393c:254e:962e? ([2600:8803:e7e4:500:109:393c:254e:962e]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7d4a75309bcsm8614464a34.1.2026.02.14.08.28.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 14 Feb 2026 08:28:08 -0800 (PST) Message-ID: <39707e67-c184-47ef-9e42-fe322f237081@baylibre.com> Date: Sat, 14 Feb 2026 10:28:07 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 1/3] iio: magnetometer: bmc150_magn: use automated cleanup for mutex To: Neel Bullywon , jic23@kernel.org Cc: nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org References: <20260210233945.40975-1-neelb2403@gmail.com> <20260210233945.40975-2-neelb2403@gmail.com> Content-Language: en-US From: David Lechner In-Reply-To: <20260210233945.40975-2-neelb2403@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2/10/26 5:39 PM, Neel Bullywon wrote: > Use guard() and scoped_guard() to replace manual mutex lock/unlock > calls. This simplifies error handling and ensures RAII-style cleanup. > > scoped_guard() is used in read_raw IIO_CHAN_INFO_RAW case for a > multi-statement mutex-protected block, as well as in remove, > runtime_suspend, and suspend where a short mutex-protected scope > is needed for a single function call. > > guard() is used in write_raw and trig_reen where the mutex scope > extends to the end of the function or case block. Case blocks in > write_raw are wrapped in braces to ensure clear scope for the > cleanup guards. > > The trigger_handler function is left unchanged as mixing guard() > with goto error paths can be fragile. > > Signed-off-by: Neel Bullywon > --- > drivers/iio/magnetometer/bmc150_magn.c | 130 ++++++++++--------------- > 1 file changed, 52 insertions(+), 78 deletions(-) > > diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c > index 6a73f6e2f1f0..5eb710eb020d 100644 > --- a/drivers/iio/magnetometer/bmc150_magn.c > +++ b/drivers/iio/magnetometer/bmc150_magn.c > @@ -11,6 +11,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -456,29 +457,22 @@ static int bmc150_magn_read_raw(struct iio_dev *indio_dev, > case IIO_CHAN_INFO_RAW: > if (iio_buffer_enabled(indio_dev)) > return -EBUSY; > - mutex_lock(&data->mutex); > - > - ret = bmc150_magn_set_power_state(data, true); > - if (ret < 0) { > - mutex_unlock(&data->mutex); > - return ret; > - } > + scoped_guard(mutex, &data->mutex) { I am very wary of scoped_guard(), especially in case statements. There is a hidden for loop in the macro, so if `break;` is used inside of the scoped_guard() {}, it will only break out of the guard scope (for loop) and not the case. It would be better if we could use `guard()` instead (in the style that Andy suggested). > static const struct iio_trigger_ops bmc150_magn_trigger_ops = { > @@ -980,9 +961,8 @@ void bmc150_magn_remove(struct device *dev) > if (data->dready_trig) > iio_trigger_unregister(data->dready_trig); > > - mutex_lock(&data->mutex); > - bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); > - mutex_unlock(&data->mutex); > + scoped_guard(mutex, &data->mutex) > + bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); > > regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); > } > @@ -995,10 +975,9 @@ static int bmc150_magn_runtime_suspend(struct device *dev) > struct bmc150_magn_data *data = iio_priv(indio_dev); > int ret; > > - mutex_lock(&data->mutex); > - ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, > - true); > - mutex_unlock(&data->mutex); > + scoped_guard(mutex, &data->mutex) > + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, > + true); This way of using scoped_guard() is OK since there is no { }. > if (ret < 0) { > dev_err(dev, "powering off device failed\n"); > return ret; > @@ -1026,10 +1005,9 @@ static int bmc150_magn_suspend(struct device *dev) > struct bmc150_magn_data *data = iio_priv(indio_dev); > int ret; > > - mutex_lock(&data->mutex); > - ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, > - true); > - mutex_unlock(&data->mutex); > + scoped_guard(mutex, &data->mutex) > + ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, > + true); This one can be regular guard() and return directly. > > return ret; > }