From nobody Sun Feb 8 16:31:00 2026 Received: from mail-vs1-f48.google.com (mail-vs1-f48.google.com [209.85.217.48]) (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 499A131A812 for ; Tue, 6 Jan 2026 08:07:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767686841; cv=none; b=g1Z6xjnsQjTRhEnHcO4dOQjdbBUhyOxL3BGldmdddoqv/562cYRLP7oKCc27F7JqTgdtsUddKlNqJ7nNziQu99iMWZzkJ/IBuEHbxxHv9iHVfOUazdNW8tbZzWsehQC21wMVZI1m6YVq2dvKSq9D3+ccW42jsPrLihUvv0ssQaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767686841; c=relaxed/simple; bh=Pzl04Z/f7G9BuXHblYf8c17cgor4qrSf/5Wv2Bzp4O4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Cl0BtoMB7Q0nva3ieuQWzQbMRUizeWhSakNVp1xa7hivLDnnzE/BYP6BFruSHhdBctxNjaQ6j1YRen7WviEhEgSRF5Ktn/N9TIefhwYk3Dld/WSaTDFGecAHwGyZcsHHPtI5GnprDTDXSd48HUorgVXf1D0gSJRizop3+Yxmr0Q= 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=MH4D2U6j; arc=none smtp.client-ip=209.85.217.48 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="MH4D2U6j" Received: by mail-vs1-f48.google.com with SMTP id ada2fe7eead31-5dd6fbe5091so283293137.1 for ; Tue, 06 Jan 2026 00:07:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767686839; x=1768291639; 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=QROS04KwedxjJOENCLLDXjHHa0xV8qTJ7XV/vfD/RD4=; b=MH4D2U6jMGQiAHe2vszsXYT6BCWdgTcT90xuO/2NV12K/lFZYioNtqwuEXg/iZVuGh H/wNBHnMszxqX/NJOnczuWt9419N3fRuPIRqGjj836awdeXfVz3nBplCMo0YnKwejJZJ 4EDacqQG4wgdwm9xhjC71YIapjvr5cchXCGn3Vf+p8XjcafBejX7hjvy3zQMR+4ZGlGQ RUe+zxY/pLRITYekweXPGnKc6C8GSv2NhvGKtx3MdZKx4SF03XFPrkOS+5yk0Xz7t7OO DGMF+Zq/NiL+mczVYPYiWrJkKMsgFD5lhyLg54jvbIgqtsc1ONxojufV0hVHFcsj9zaa Rp0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767686839; x=1768291639; 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=QROS04KwedxjJOENCLLDXjHHa0xV8qTJ7XV/vfD/RD4=; b=mlRHIB8kl+U9nAuDUaO5f8buhcBt2EkuGQ6acMUPFfkaiD3Oe8Mz84pNQs1jOJM6g9 Ky/VVEOhIkS5UWnHlXWCn6F3WytowgitdslX819wOapNoeTdKJ/kSWTEYM9sfC22nVm1 dbAg88EUEKLl1W6h8eRCWRnY5ybu8CSsSW11ZWaO4SVDdl3nknDJuVGCnmwQ4OeYFF1W lo+pW3od1MyGf7/zKxxti1XV7rWRmX/axYF3wafMAJaLpDFbyU0P0YGjIrOPWiV5hckn sadpOTCfClQ+J7X6YYpxdjWhr4fPO6QrbStimpQUSdZ9WZ3lX4Rbr4iw0w/rPaMLpFMs RNnA== X-Forwarded-Encrypted: i=1; AJvYcCU7P6bLt1czhTv7HABX9eeMZGCb2zJEBgY+MQPqLKq1ZjvGwDthrf/wt3mi2IvnM8Z/2qDoHo3rTEMMHn4=@vger.kernel.org X-Gm-Message-State: AOJu0YzjPmSKypKEdRShlfVglSpW5JHaRonuhwb7SoTRveaW0+mtSkrE eSOGEhrjpfQ8fykJM0QqydfQjhKYgKLDEaK5K7BDVmT4ACRfHgOUhPPe X-Gm-Gg: AY/fxX4bc2XzDW4oERPPqhlRZ7RTMU0tJFLCivOd/6BL+Ho+LzBpxoa2J2TbpdLmISx SPd0fUUQyG0AE+hTdv4+yniUAw53gwjrdLgW+FLH/qyB4Lh+5Fb8JKYEwJWlnTAc8Vrr/4464mc 7bljdBkR4k9j2oLLUIwr+Bp5smLysbcyypnhzL3lNARpSNdaHOUu6CbI/gAB6cscKNsBuadqGbf 64T8Vmey7JxXqlLJIjj3GSCmerK0k0/tjFv6i2imM4jkkvXso9BHiJv809FEDcmHZE9tdRZa72n lXhakSqj326/HnWzo3pJLsdrS4tSTLjfmsO9a1NpUvruoHxps3MfsZAlEN28rw1xY9x+eRDGxyt DsV0kGzCUkb2jdFVId659wfSTdOZiB5glCgE2AnAjWqRQqQ/OX3q/pwNNEY3A3fTGp3WaI0Ej5b HBDJ96TyHuW4rnLm6dkyOJwq8= X-Google-Smtp-Source: AGHT+IHcK8ji32sCIat7H2xKucoKdh58wxd0RlCa758Zauf5a9oF4zVD5I+zYwQ45lypy7m4v6Nizw== X-Received: by 2002:a05:6102:2908:b0:5d5:f6ae:38c4 with SMTP id ada2fe7eead31-5ec74598e62mr521625137.45.1767686839040; Tue, 06 Jan 2026 00:07:19 -0800 (PST) Received: from [192.168.100.253] ([2800:bf0:82:11a2:7ac4:1f2:947b:2b6]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-944124a3386sm457615241.11.2026.01.06.00.07.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jan 2026 00:07:18 -0800 (PST) From: Kurt Borja Date: Tue, 06 Jan 2026 03:06:59 -0500 Subject: [PATCH v3 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: <20260106-lock-impr-v3-4-1db909b192c0@gmail.com> References: <20260106-lock-impr-v3-0-1db909b192c0@gmail.com> In-Reply-To: <20260106-lock-impr-v3-0-1db909b192c0@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=3685; i=kuurtb@gmail.com; h=from:subject:message-id; bh=Pzl04Z/f7G9BuXHblYf8c17cgor4qrSf/5Wv2Bzp4O4=; b=owGbwMvMwCUmluBs8WX+lTTG02pJDJkxh1ZJfdTQqWUKNpvNntYjcOZ4IUuhqvinZ9Ou1nP+e Kort7K5o5SFQYyLQVZMkaU9YdG3R1F5b/0OhN6HmcPKBDKEgYtTACYSW8Dwv2zOqk/z+d/OvOiq bXHn7oXz5lt1+ZaeTBXcxfj9QLje94+MDI3NDoEKG/Mdwzqn/nzc0HVkx+JQg9jgiGc9d5ySCt7 fZgYA 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 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(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 bello= w 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 m= ode + * 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:: + * + * 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_*_M= ODE() + * + * 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) + +/** + * 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