From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 B3F4F2DCC04 for ; Fri, 16 Jan 2026 22:03:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768601043; cv=none; b=TZMPsF+0RtNG26fq4NpPlPIje90lJS+yEc0+UjBdwE69J163ixSLBtHa4tgyjzj96SEEuJ17dBFM09xD294VLpj83QmzcriWpRd+lwP2Y6W0rPy3LRUaIn+ij6u9LzxOdJhtaoRlToAWTstfIRa3eJqOlCe16Tnm3qqU+yTChg4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768601043; c=relaxed/simple; bh=6tyjTajdOQh6J57U/rMOsH8jV3xfmFKJ5YGgVVF1VoE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=BrWy01JtGtzopBrTKLD/lqpSaBPCnidHqUxDIdMJERespg23IOVgTRwd/tsKl+1VWtEwaWmWDKn+VK/03xC6ODuIK8P8QAlF7No4ul15Deee8kCbMyEDjufrjXNskUeeCaq6Nnuonbs0r83m58tlAJNt0f4a8S7JaLptbaGwq0s= 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=M7HQHeGn; arc=none smtp.client-ip=209.85.167.180 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="M7HQHeGn" Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-45c96b175baso1220034b6e.1 for ; Fri, 16 Jan 2026 14:03:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1768601038; x=1769205838; 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=nIt+ZzeCtP7pqDmlgpYUQhxXE3OKLmphYCyoYGAfb3I=; b=M7HQHeGnR/x/f/5HIdCcKrt/y9i/PxRJ3b1TUx6ox7f1ljkWFWRVxoD38motfGT7yz LIXe1HFwkFD1tsU9jgL23NwKH1Y4/uDytK3Yoj4RQpPBQ2++tYQjILz4jFzhBW91ve5q VDOAYvI9Y3DUyAp4sUBaB/qfIOLtwfBisTHTOGn7ddm3Upw5ItNaoJXGWe0R3sbmSMNM w5INf09pxfHP2a0+HS/vqHCkEr7LinXF/JIK/iPr1MqDlTzJ30NYSCs47NWPdKtGz6C4 GyXyZqNU+lAfGiFJAobZDEnwoCF2XOFiZe1ToTnVtJUYXCh0XPFwJgXobTajHTND4P9E qjlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768601038; x=1769205838; 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=nIt+ZzeCtP7pqDmlgpYUQhxXE3OKLmphYCyoYGAfb3I=; b=OagFJ8Pgfm/UzR3WKw4vm/KtdigUNGhAL2ihgs94kAD33LM5aDSwu9rsQpxOhvBaLK ckAX9bVCTsgnJ46C+nsQ5UhQ2UZJgK+G0JtgGo9iW+UimQ1ciX9EVK+AkVymX/E9AQgX 4pdo3f0y9hFsBMv4DvhNdtsnFX7dhh5xF9kAU+4Hst7qm0s8u9CVUyI/eFynxF9A03vj yb00MKgKhiZzP1HhYHHyK16d++iFp6G7M1pZcS2gIeBB4ih62t8v6my1/j1uxQEpfsjW TNGkErAdfytQ5x8mpH0EvVFo8R6UvMcUjYA9z7M9zU3FptntjEXku459plJI0+diR8c5 GVKQ== X-Forwarded-Encrypted: i=1; AJvYcCU1rqZC+qR2lHfSJfFs9EKXu+g+baOPSMNhW2U4EWjJ6W1pdawyNrg8IPDzEfksDENV1MqK0bd+UzaIY28=@vger.kernel.org X-Gm-Message-State: AOJu0YxcszWOW8uMmaf3x/F7TLHxHx9nWQyO/weEcI8UbYoxT3LLoWKJ qgOxmsehgpYs5wej0THj7xf3N5AAg1vbGJSvBRDPiaPvO83p2o/B960blIuuRy0RUbw= X-Gm-Gg: AY/fxX78S9HhFnyhYXHwCvrEbwAkrTcawbNttoVDGu85sAVSPux9Qc6tznSn3EQ2CMZ djsBCAD+IUNV3E113y9NbYUr0Ut7TFrhs8BqEe2iMOdMF52Sp3fcobvIF5KT+urws8BKppc8y7b 74vaJCci3CiCVTvAy6rhws3v5DmI5FvS1KHh4UouWyAG209M8LQRwoz8gbXTc9Tc+/62ZrH1CQ0 IrVDd+Ze1Q5MBbHsPiO3f2EhP6co3bPJccdUppNQYlN06zBVgw1dZIhlQy2+Esnh8AOtotSMrFL uAxmSGzZ0B/lWtdsSZ5kPX3PhYF1juGx/A2zWfdYikazblaortyDYY0jiX38auaFFW/xnI0Ab6P ENJxJDp0eL40RQLLCBFUnro6d5z/WFLt3cxvRllYF1yDGxtHBcBHbAU62yQhA9+KgLdTqdKe359 q0qzj++nAdDQFFzyJs3unC9UYJH++a8CiTjCE6x+uUgpoq8R+TTRTxcIGVRcuZ X-Received: by 2002:a05:6808:4f4e:b0:45c:7eba:576a with SMTP id 5614622812f47-45c9c1a732emr2028739b6e.64.1768601038648; Fri, 16 Jan 2026 14:03:58 -0800 (PST) Received: from ?IPV6:2600:8803:e7e4:500:bd39:740e:f70f:5f7d? ([2600:8803:e7e4:500:bd39:740e:f70f:5f7d]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4044bd14883sm2386316fac.12.2026.01.16.14.03.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Jan 2026 14:03:58 -0800 (PST) Message-ID: <27cf1ec1-545b-4e44-8229-852f8bdae116@baylibre.com> Date: Fri, 16 Jan 2026 16:03:57 -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 v3 4/7] iio: core: Add cleanup.h support for iio_device_claim_*() To: Kurt Borja , Andy Shevchenko , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Benson Leung , Antoniu Miclaus , Gwendal Grignou , Shrikant Raskar , Per-Daniel Olsson Cc: =?UTF-8?Q?Nuno_S=C3=A1?= , Andy Shevchenko , Guenter Roeck , Jonathan Cameron , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev References: <20260106-lock-impr-v3-0-1db909b192c0@gmail.com> <20260106-lock-impr-v3-4-1db909b192c0@gmail.com> Content-Language: en-US From: David Lechner In-Reply-To: <20260106-lock-impr-v3-4-1db909b192c0@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 1/6/26 2:06 AM, Kurt Borja wrote: > Add guard classes for iio_device_claim_*() conditional locks. This will > aid drivers write safer and cleaner code when dealing with some common > patterns. > > These classes are not meant to be used directly by drivers (hence the > __priv__ prefix). Instead, documented wrapper macros are provided to > enforce the use of ACQUIRE() or guard() semantics and avoid the > problematic scoped guard. > > Suggested-by: Andy Shevchenko > Signed-off-by: Kurt Borja > --- > include/linux/iio/iio.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 71 insertions(+) > > diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h > index d8af0456f966..c795f731f2d8 100644 > --- a/include/linux/iio/iio.h > +++ b/include/linux/iio/iio.h > @@ -10,6 +10,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -740,6 +741,76 @@ static inline bool iio_device_try_claim_buffer_mode(struct iio_dev *indio_dev) > */ > #define iio_device_release_buffer_mode(indio_dev) __iio_dev_mode_unlock(indio_dev) > > +/* > + * These classes are not meant to be used directly by drivers (hence the > + * __priv__ prefix). Instead, documented wrapper macros are provided bellow to > + * enforce the use of ACQUIRE() or guard() semantics and avoid the problematic > + * scoped guard variants. > + */ > +DEFINE_GUARD(__priv__iio_dev_mode_lock, struct iio_dev *, > + __iio_dev_mode_lock(_T), __iio_dev_mode_unlock(_T)); > +DEFINE_GUARD_COND(__priv__iio_dev_mode_lock, _try_direct, > + iio_device_claim_direct(_T)); > + > +/** > + * IIO_DEV_ACQUIRE_DIRECT_MODE(_dev, _var) - Tries to acquire the direct mode > + * lock with automatic release I don't think it is usual to put the function parameters in the doc comment like this. They don't match the actual names anyway. > + * @dev: IIO device instance > + * @claim: Variable identifier to store acquire result > + * > + * Tries to acquire the direct mode lock with cleanup ACQUIRE() semantics and > + * automatically releases it at the end of the scope. It most be always paired > + * with IIO_DEV_ACQUIRE_ERR(), for example:: > + * > + * IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim); > + * if (IIO_DEV_ACQUIRE_FAILED(&claim)) > + * return -EBUSY; > + * > + * ...or a more common scenario (notice scope the braces):: > + * > + * switch() { > + * case IIO_CHAN_INFO_RAW: { > + * IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim); > + * if (IIO_DEV_ACQUIRE_FAILED(&claim)) > + * return -EBUSY; > + * > + * ... > + * } > + * case IIO_CHAN_INFO_SCALE: > + * ... > + * ... > + * } > + * > + * Context: Can sleep > + */ > +#define IIO_DEV_ACQUIRE_DIRECT_MODE(dev, claim) \ > + ACQUIRE(__priv__iio_dev_mode_lock_try_direct, claim)(dev) > + > +/** > + * IIO_DEV_ACQUIRE_FAILED() - ACQUIRE_ERR() wrapper > + * @claim_ptr: Pointer to the claim variable passed to IIO_DEV_ACQUIRE_*_MODE() > + * > + * Return: true if acquired the mode failed, otherwise false. > + */ > +#define IIO_DEV_ACQUIRE_FAILED(claim_ptr) \ > + ACQUIRE_ERR(__priv__iio_dev_mode_lock_try_direct, claim_ptr) > + If we always have to add the & at the call site, could we just put that in the macro instead? Then the parameter would just be claim instead of claim_ptr.