From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.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 85067277026 for ; Sun, 18 Jan 2026 19:35:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768764945; cv=none; b=MLqi1gtQ4TUMMfMaI5hEojFzg64U4k7LrNByZuXSO8jtaEWEKPYSWqjt/kOEQUYfvqizr61lFgI5e2Y+7qTAGfr8akpKC+uJQl3gYM8AHob3OGvJDskhdIzbpSN8DrLOR0H0gKD7jR9jsAU3udg3mMDGntDZAW4TjttfucrIdSg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768764945; c=relaxed/simple; bh=ZrkbSSR7SPbk0sXpHbnHk+xR9V/NadYrGwGl0W6n57s=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=emk9lyefh8phuLzcAcJUdkIJVCfli8/lx+KATOyu+I0tr3nuiHLkWmPEeedyaU/jq7HfHqVRswJgiEushn1liqHOYysUbqHsaYQ6eWe5wBzVtc1nGuop2KhBkbgXBj5Ok4TYpe8R2rD/pacTLc/IMk3d9AHwpPty8MNnWoCfKFg= 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=eRtVkqHo; arc=none smtp.client-ip=209.85.167.179 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="eRtVkqHo" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-45c93313721so2071922b6e.2 for ; Sun, 18 Jan 2026 11:35:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1768764941; x=1769369741; 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=Js92nL0F9mZGARPs875T0z11qnkMg6IzGWXmj5+ZqzY=; b=eRtVkqHoknami+JmYHI/j9eDdJwl65pnJTZwys0lKc6PYSVJccNGgVuX3lOf51vU4E ZhbqvgM37INnlpizdVkq7mJxV+SP4osL39WSP4sPL3SWBLItab2j4FajRgX2YRBBgNun ogScFZHCSyoWdNx3WdymEiQkuctNXHI6G4b1Y8vHt6lQdcUEnDR49+cZSFUQhzp71Sem AOmrKN3IqPcN/e/h2r6d2iXLZP7f0l3N9i+uML6zkPonTRLpZ4FPH0PT7934KhOBsbXv bXgjRs/QBb+9q+4wTbO+5+uGiidxAWb7/TiGvUDm8055WA+9IHD0bZDAPRZsDkztmkCC 0VVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768764941; x=1769369741; 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=Js92nL0F9mZGARPs875T0z11qnkMg6IzGWXmj5+ZqzY=; b=pX6p9++8mW1OqfYmeXG41zWBJcPWX5zfKz15UPXp1BZ0XzQUyNz8bzNQR4kOQ+aOLF ORsaNC4NOW9HcLYUtd/N1q3xtCFcyOND1nakzBjVE4vhSNS3H0AfQwHz5Qlxi0wazypW ssWsIzQB3POns5Qw8cbTp/p+/QXeLawqV3QsLiuW4ym1Vlht/wQRiBnM9NlLzwaUo8Hh 6Iki3DF5ynHeX7nJFuzRR0D4DdMh83vg5euxpS65p0Hc6Hf9CvDRExd64yeLrcYf/bp3 KLy0SPhj3/JKB2yhaYcsE3f/xCIpWcdOjm01Ai3muZxkxeim0tOnKyMkUcEX40zJGe1s O3Fg== X-Forwarded-Encrypted: i=1; AJvYcCWJVLGhaE9yYJrsrwCgHdE1bbcy62tcK7uMNoVlFlKwnDuzqlz+3DmfwXjbWT6qyCv/wPEUhzQfBPPWwv0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1MX5YahclxKv3zSkxRoz+JGDGXxYjJZhU6bix6rXPp13d74wj fNzQP4xWZ21VtN+WUqFKkba8SA2qv2BTaRFoiyhBDSsT85v7boiQMb0HCcxO6xNDJqU= X-Gm-Gg: AY/fxX4szr5Ktm1gcgaR75dctKkJ/KToN1AvD6JFbpcP5CuXIkxXsCyQSV2Ps3p7BOv 3dFqGorKETCm4fH3oS7oxw8TZdhmPB6zXJh7+2Ko3NhMvM+mnzldK35sT5xNZxI4jAaJkQy6QKz tDr7NDoVUgfDzYNwbI/v0A40rYEawECionFy0/LL3rCROIpl7Bzzi0RKCRm6vLAl4ScD4hkig3z ZpUUWMQ1w9mN0cwEtVT2QNKMHEAqXLQMrQw8k0TNNLUtyWCFt04FEUy9siC0dvEgWZtfI+aI4aY whGWO7TVgcuCg8ZmPUN92BOMWHmQBH/MlNX2Izq1YdpZblG1szNwUkwQGsNmYnqEY4Jx9fZA92z VgN6C6jyy3+IqLc5HuqaQv3RrdGizYkMzs7WcyfUWRbCrk5HzEvCrrzTWc2yevKMluDYUJ0+Pn0 Tnnnq5ikDBSenBeogdRI2V76FomsQKs5U65iAczNA3LsiVNY2EjGUGJ+mn0CJa X-Received: by 2002:a05:6808:198b:b0:44f:e931:38c2 with SMTP id 5614622812f47-45c9bf514c2mr4148232b6e.15.1768764941468; Sun, 18 Jan 2026 11:35:41 -0800 (PST) Received: from ?IPV6:2600:8803:e7e4:500:d542:af69:64b6:f7df? ([2600:8803:e7e4:500:d542:af69:64b6:f7df]) by smtp.gmail.com with ESMTPSA id 5614622812f47-45c9e0086a4sm4522900b6e.12.2026.01.18.11.35.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 18 Jan 2026 11:35:40 -0800 (PST) Message-ID: Date: Sun, 18 Jan 2026 13:35:37 -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 v4 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: <20260118-lock-impr-v4-0-6c8d0aee8ed2@gmail.com> <20260118-lock-impr-v4-4-6c8d0aee8ed2@gmail.com> Content-Language: en-US From: David Lechner In-Reply-To: <20260118-lock-impr-v4-4-6c8d0aee8ed2@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 1/18/26 9:44 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 > Reviewed-by: David Lechner > Reviewed-by: Nuno Sá > Signed-off-by: Kurt Borja > --- > include/linux/iio/iio.h | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 65 insertions(+) > > diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h > index fa671e120e95..4a0a77fcfa8d 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,70 @@ 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 below 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 Should be just IIO_DEV_ACQUIRE_DIRECT_MODE() > + * @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 (notice the scope 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 This one is correct style. > + * @claim: The claim variable passed to IIO_DEV_ACQUIRE_*_MODE() > + * > + * Return: true if failed to acquire the mode, otherwise false. > + */ > +#define IIO_DEV_ACQUIRE_FAILED(claim) \ > + ACQUIRE_ERR(__priv__iio_dev_mode_lock_try_direct, &(claim)) > + > +/** > + * IIO_DEV_GUARD_CURRENT_MODE - Acquires the mode lock with automatic release Missing () > + * @dev: IIO device instance > + * > + * Acquires the mode lock with cleanup guard() semantics. It is usually paired > + * with iio_buffer_enabled(). > + * > + * This should *not* be used to protect internal driver state and it's use in > + * general is *strongly* discouraged. Use any of the IIO_DEV_ACQUIRE_*_MODE() > + * variants. > + * > + * Context: Can sleep > + */ > +#define IIO_DEV_GUARD_CURRENT_MODE(dev) \ > + guard(__priv__iio_dev_mode_lock)(dev) > + > extern const struct bus_type iio_bus_type; > > /** >