diff mbox series

iio: core: make iio_device_claim_direct_mode() __must_check

Message ID 20241002-iio-must-check-claim-direct-v1-1-ab94ce728731@baylibre.com (mailing list archive)
State Changes Requested
Headers show
Series iio: core: make iio_device_claim_direct_mode() __must_check | expand

Commit Message

David Lechner Oct. 2, 2024, 3:34 p.m. UTC
Add __must_check attribute to iio_device_claim_direct_mode().

Since we have now DEFINE_GUARD(iio_claim_direct, ...,
iio_device_claim_direct_mode, ...), it is possible to write:

    guard(iio_claim_direct)(indio_dev);

This would be a bug, since the return value is not checked. Adding
__must_check to iio_device_claim_direct_mode() makes the compiler emit
a warning for the above code.

Suggested-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
This comes from the suggestion at the end of [1]. It seems like a
reasonable thing to do regardless if the other series works out or not,
so submitting it separately.

[1]: https://lore.kernel.org/all/66fcac2dbde60_964f229426@dwillia2-xfh.jf.intel.com.notmuch/
---
 include/linux/iio/iio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


---
base-commit: 431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e
change-id: 20241002-iio-must-check-claim-direct-e6988a195368

Best regards,

Comments

kernel test robot Oct. 3, 2024, 11:26 p.m. UTC | #1
Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Lechner/iio-core-make-iio_device_claim_direct_mode-__must_check/20241002-233644
base:   431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e
patch link:    https://lore.kernel.org/r/20241002-iio-must-check-claim-direct-v1-1-ab94ce728731%40baylibre.com
patch subject: [PATCH] iio: core: make iio_device_claim_direct_mode() __must_check
config: arm-randconfig-001-20241004 (https://download.01.org/0day-ci/archive/20241004/202410040721.upAHwZJm-lkp@intel.com/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project fef3566a25ff0e34fb87339ba5e13eca17cec00f)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241004/202410040721.upAHwZJm-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410040721.upAHwZJm-lkp@intel.com/

All warnings (new ones prefixed by >>):

   In file included from drivers/iio/pressure/bmp280-regmap.c:6:
   In file included from drivers/iio/pressure/bmp280.h:5:
   In file included from include/linux/regulator/consumer.h:35:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:21:
   In file included from include/linux/mm.h:2213:
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   In file included from drivers/iio/pressure/bmp280-regmap.c:6:
   In file included from drivers/iio/pressure/bmp280.h:7:
>> include/linux/iio/iio.h:669:50: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
     669 | DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
         |                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
   include/linux/cleanup.h:291:54: note: expanded from macro 'DEFINE_GUARD'
     291 |         DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
         |                                                             ^~~~~
   include/linux/cleanup.h:248:13: note: expanded from macro 'DEFINE_CLASS'
     248 | { _type t = _init; return t; }
         |             ^~~~~
   2 warnings generated.
--
   In file included from drivers/iio/pressure/ms5611_core.c:14:
>> include/linux/iio/iio.h:669:50: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
     669 | DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
         |                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
   include/linux/cleanup.h:291:54: note: expanded from macro 'DEFINE_GUARD'
     291 |         DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
         |                                                             ^~~~~
   include/linux/cleanup.h:248:13: note: expanded from macro 'DEFINE_CLASS'
     248 | { _type t = _init; return t; }
         |             ^~~~~
   In file included from drivers/iio/pressure/ms5611_core.c:16:
   In file included from include/linux/regulator/consumer.h:35:
   In file included from include/linux/suspend.h:5:
   In file included from include/linux/swap.h:9:
   In file included from include/linux/memcontrol.h:21:
   In file included from include/linux/mm.h:2213:
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   2 warnings generated.
--
   In file included from drivers/iio/pressure/mpl115.c:11:
>> include/linux/iio/iio.h:669:50: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
     669 | DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
         |                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
   include/linux/cleanup.h:291:54: note: expanded from macro 'DEFINE_GUARD'
     291 |         DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
         |                                                             ^~~~~
   include/linux/cleanup.h:248:13: note: expanded from macro 'DEFINE_CLASS'
     248 | { _type t = _init; return t; }
         |             ^~~~~
   1 warning generated.


vim +/warn_unused_result +669 include/linux/iio/iio.h

1dae0cb79ceacb Jonathan Cameron 2024-01-28  664  
1dae0cb79ceacb Jonathan Cameron 2024-01-28  665  /*
1dae0cb79ceacb Jonathan Cameron 2024-01-28  666   * This autocleanup logic is normally used via
1dae0cb79ceacb Jonathan Cameron 2024-01-28  667   * iio_device_claim_direct_scoped().
1dae0cb79ceacb Jonathan Cameron 2024-01-28  668   */
1dae0cb79ceacb Jonathan Cameron 2024-01-28 @669  DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
1dae0cb79ceacb Jonathan Cameron 2024-01-28  670  	     iio_device_release_direct_mode(_T))
1dae0cb79ceacb Jonathan Cameron 2024-01-28  671
Dan Williams Oct. 4, 2024, 5:57 p.m. UTC | #2
kernel test robot wrote:
> Hi David,
> 
> kernel test robot noticed the following build warnings:
> 
> [auto build test WARNING on 431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e]
> 
> url:    https://github.com/intel-lab-lkp/linux/commits/David-Lechner/iio-core-make-iio_device_claim_direct_mode-__must_check/20241002-233644
> base:   431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e
> patch link:    https://lore.kernel.org/r/20241002-iio-must-check-claim-direct-v1-1-ab94ce728731%40baylibre.com
> patch subject: [PATCH] iio: core: make iio_device_claim_direct_mode() __must_check
[..]
> >> include/linux/iio/iio.h:669:50: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
>      669 | DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),

So I think this points to the fact that iio_device_claim_direct_mode()
should not be using DEFINE_GUARD() in the first instance. I think
iio_claim_direct() really wants to be using DEFINE_CLASS() directly.
Skip usage of DEFINE_GUARD() which I now see is unable to interoperate
with a __must_check locking function.

Perhaps the new class can be something like:

    DEFINE_GUARD_EXCL_COND()

...which creates a guard that is exclusively conditional and has no
unconditional flavor. However, maybe that only lives in iio unless and
until another user arrives.
David Lechner Oct. 4, 2024, 7:12 p.m. UTC | #3
On 10/4/24 12:57 PM, Dan Williams wrote:
> kernel test robot wrote:
>> Hi David,
>>
>> kernel test robot noticed the following build warnings:
>>
>> [auto build test WARNING on 431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e]
>>
>> url:    https://github.com/intel-lab-lkp/linux/commits/David-Lechner/iio-core-make-iio_device_claim_direct_mode-__must_check/20241002-233644
>> base:   431c39f6d3edbab14f48dbf37a58ccdc0ac3be1e
>> patch link:    https://lore.kernel.org/r/20241002-iio-must-check-claim-direct-v1-1-ab94ce728731%40baylibre.com
>> patch subject: [PATCH] iio: core: make iio_device_claim_direct_mode() __must_check
> [..]
>>>> include/linux/iio/iio.h:669:50: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
>>      669 | DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
> 
> So I think this points to the fact that iio_device_claim_direct_mode()
> should not be using DEFINE_GUARD() in the first instance. I think
> iio_claim_direct() really wants to be using DEFINE_CLASS() directly.
> Skip usage of DEFINE_GUARD() which I now see is unable to interoperate
> with a __must_check locking function.
> 
> Perhaps the new class can be something like:
> 
>     DEFINE_GUARD_EXCL_COND()
> 
> ...which creates a guard that is exclusively conditional and has no
> unconditional flavor. However, maybe that only lives in iio unless and
> until another user arrives.

Hmm... I see what you mean. All other conditional guards (e.g.
mutex_trylock) have an unconditional counterpart (mutex_lock)
that is used with DEFINE_GUARD.
diff mbox series

Patch

diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index 18779b631e90..2dc6470d20a4 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -659,7 +659,7 @@  void iio_device_unregister(struct iio_dev *indio_dev);
 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
 			       struct module *this_mod);
 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
-int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
+__must_check int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
 void iio_device_release_direct_mode(struct iio_dev *indio_dev);
 
 /*