From nobody Sun Feb 8 11:26:00 2026 Received: from mail-ua1-f53.google.com (mail-ua1-f53.google.com [209.85.222.53]) (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 39CA82D5932 for ; Sun, 18 Jan 2026 15:45:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768751105; cv=none; b=Uw9xgwTcs8mkxqUpiB08Gk/fObs2ShO8o9gKeUctSs0FXrVJ0EY6ntSXdBe/+HXTDsgdLJ5F4BnAOv/nSQAYE1UrTbD4ADxnfKUIJ2WOpttx6BIkeB7M92wa2Uxt0GoDqn+djqc2QynMX6EbrS+A5FSZRXI0yN4/91mqQItb7F0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768751105; c=relaxed/simple; bh=Bt3F/lHb6LI1N+BCX9VGngIeBqcSWQC3l35PbzVvVCs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QxSfavi9r1gqwcLUQ8Aym4dzofN9oXqKrdLDOmTv6i61LhXBRpwknvhhO36cWZdbEp7L+g0AkiJ4a5rPDl1ZNItKRpyxIA2YXwMKA/tC/fDQiWlvBCRWCPd5tZi26juvOx6+wRHwBjpOtb6qibxOSEaIdxRF5EJitrY9iRVxwhs= 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=g6/ZCnhL; arc=none smtp.client-ip=209.85.222.53 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="g6/ZCnhL" Received: by mail-ua1-f53.google.com with SMTP id a1e0cc1a2514c-93f542917eeso1161555241.2 for ; Sun, 18 Jan 2026 07:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768751101; x=1769355901; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=XGdTfbPm6m/cN0XbYhh/ArH4Gt5W4bmHVBdR2RxVxHc=; b=g6/ZCnhL2VuK3YHY1zHho5axxQPApJjnUe+UGvYZ8313wbUsdBgaSviWW2y/taSmEc kfe5q3yMNrJGt8c+4Oj+7F3M6EwWc31jCB56jxT6kEg1wcuAwNPs8YYrIgVLuwGy7UfL vrIliTxYTP/bD6W1lLA8mWdB3QZsU7GFAm5PqmcZUL1Nt27Rozh23P1qC0u/Po6eH3N1 VAu6AixxxUKkG2Fc+iN6DgZ7PWD3yuvfxj3Gq6ujbtdF1Gco5XFmvb1E56lApuq23fBX WbEk1enYlf30/fTo92rdmM+nCauvciY9yuO4C0vn5D/nkmIoaZBvBNLWbo7I9+YbJhou SXKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768751101; x=1769355901; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=XGdTfbPm6m/cN0XbYhh/ArH4Gt5W4bmHVBdR2RxVxHc=; b=RvLiVQaKTJm0DJQY8cyxoBhrbBsldpI/pr8hpsI6KRc6IOwuVf5uqIxaHg10aaDQzf /afk++/wkp/t9WsCcBSFWzFH2J7QfGBZYcVQ4V6Cq0q9iBo9kCiwxBmAoj3oKW5YoO20 L4lCzB86PbUvZFW4oh+HBBdS2V1PFYzRAI1j5kO8xvqrhEh3JUCh3daa/DObGmUQFBeE OaScRYfz9fjX9lk4kdobvYUgp5DAMDrj5sK6G3tAC9q0rAGk3IZISTHm2+JiLHKa84Nt J2gJvVu9eSMuuZCCl1rmCd/qWzAe+BXLzK5qJSNOXFGxC6DW+xhR41s/7ZEEZsbiuTzg wQiQ== X-Forwarded-Encrypted: i=1; AJvYcCUi9jSvYdMUoMP97/nXBEtkrSCE3x4cA5UR5rPI87AhbRA26DjqZ4b3kUY3m1VFI1jBltaGSVXblV+GVDo=@vger.kernel.org X-Gm-Message-State: AOJu0YzcPk3/j6TBCJlzORyvxp0PlCAaPYzJySV32syCJuBIktG0g4tZ at8GyvzP0/fvyI6I23Jcdfn2hzzClgqtue2wRE20fNrL3jhkm9kkg2qP X-Gm-Gg: AY/fxX5I5Q5eEwL+peu39ihPHaWm6NzGYqmMO2FluJiAbu47XbC6bsSIMZh73mdHEY9 gGYJvfQfJRqgpKwbrRw16BZ+4lbRCozFP3lO29GlEV/xuQ6MAyVJwLaw9EyLJPAYByZ5xrucPfH 6fPvw8JGQBTUKvkxNPZAJ1lYsYOcoOXaJCgW1ftQzeARJYI844i4Wp+I7gmgDPDhn5+T9f/AXMQ bYgGnXKc3LgtV6B1knX5vyH0swxOoMO6HFPjucNoTUHCfOf+/g3XNlZI3YQUe4MytPOpXgBpdYM kGVPqd5dNMHBnB1eoUiKG7O58KOz3ZcdGi2Mb58PYaB5xnlLUO/EYZFIdshE/HWKmeaKg2pxiyq fax0ejElZrC59rXm9vMbItzTRSmY3jVjCqXW9MyuaWQFzBIg8wKSGvlwysXuFv+rZbTcaXeyFVj 99xXYZ3PzsA7fz X-Received: by 2002:a05:6102:dcc:b0:5db:f031:84c7 with SMTP id ada2fe7eead31-5f1a717ed4emr2752450137.30.1768751100866; Sun, 18 Jan 2026 07:45:00 -0800 (PST) Received: from [192.168.100.253] ([2800:bf0:82:11a2:7ac4:1f2:947b:2b6]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-5f1a6d3ca2esm2832731137.10.2026.01.18.07.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Jan 2026 07:45:00 -0800 (PST) From: Kurt Borja Date: Sun, 18 Jan 2026 10:44:31 -0500 Subject: [PATCH v4 4/7] iio: core: Add cleanup.h support for iio_device_claim_*() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260118-lock-impr-v4-4-6c8d0aee8ed2@gmail.com> References: <20260118-lock-impr-v4-0-6c8d0aee8ed2@gmail.com> In-Reply-To: <20260118-lock-impr-v4-0-6c8d0aee8ed2@gmail.com> To: Andy Shevchenko , Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , Benson Leung , Antoniu Miclaus , Gwendal Grignou , Shrikant Raskar , Per-Daniel Olsson Cc: David Lechner , =?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, Kurt Borja X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3563; i=kuurtb@gmail.com; h=from:subject:message-id; bh=Bt3F/lHb6LI1N+BCX9VGngIeBqcSWQC3l35PbzVvVCs=; b=owGbwMvMwCUmluBs8WX+lTTG02pJDJk5/z/eZhPNcLLkddP59UGR49r002tzUzdNjdqo2JfJW 3O1JGlhRykLgxgXg6yYIkt7wqJvj6Ly3vodCL0PM4eVCWQIAxenAEzEtomR4cuUHvZdLO4rD//d OF+kpG/y8dPXpjw71f91z99lwi8/bVjKyDCfY9uReJ37ehPnBBxm7NIxvvZq4s0F+97uz85dN8l J9wkzAA== X-Developer-Key: i=kuurtb@gmail.com; a=openpgp; fpr=54D3BE170AEF777983C3C63B57E3B6585920A69A 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=C3=A1 Signed-off-by: Kurt Borja Acked-by: Andy Shevchenko --- 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(st= ruct iio_dev *indio_dev) */ #define iio_device_release_buffer_mode(indio_dev) __iio_dev_mode_unlock(in= dio_dev) =20 +/* + * 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 problem= atic + * 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 + * @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 pa= ired + * 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 + * @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 rele= ase + * @dev: IIO device instance + * + * Acquires the mode lock with cleanup guard() semantics. It is usually pa= ired + * 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_*_MOD= E() + * 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; =20 /** --=20 2.52.0