Skip to contentSkip to conversation

iio: imu: kmx61: Use guard(mutex)() family over manual locking

PATCH Superseded Maxwell Doose 4 participants 7 messages May 2 – May 5 IIO

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: kmx61: Use guard(mutex)() family over manual locking

Include linux/cleanup.h to take advantage of new macros.

Replace manual mutex_lock() and mutex_unlock() calls across the file with guard(mutex)() and scoped_guard() where appropriate. This will help modernize the driver with up-to-date functions/macros.

Remove now redundant gotos and ret variables, as the new RAII macros make them unneeded.

---
 drivers/iio/imu/kmx61.c | 74 ++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 49 deletions(-)
drivers/iio/imu/kmx61.c +25 -49
index 3cd91d8a89ee..2baaca129601 100644
@@ -7,6 +7,7 @@
  * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
  */
 
+#include <linux/cleanup.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/mod_devicetable.h>
@@ -794,25 +795,21 @@ static int kmx61_read_raw(struct iio_dev *indio_dev,
 		default:
 			return -EINVAL;
 		}
-		mutex_lock(&data->lock);
+		guard(mutex)(&data->lock);
 
 		ret = kmx61_set_power_state(data, true, chan->address);
-		if (ret) {
-			mutex_unlock(&data->lock);
+		if (ret)
 			return ret;
-		}
 
 		ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
 		if (ret < 0) {
 			kmx61_set_power_state(data, false, chan->address);
-			mutex_unlock(&data->lock);
 			return ret;
 		}
 		*val = sign_extend32(ret >> chan->scan_type.shift,
 				     chan->scan_type.realbits - 1);
 		ret = kmx61_set_power_state(data, false, chan->address);
 
-		mutex_unlock(&data->lock);
 		if (ret)
 			return ret;
 		return IIO_VAL_INT;
@@ -834,9 +831,8 @@ static int kmx61_read_raw(struct iio_dev *indio_dev,
 		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
 			return -EINVAL;
 
-		mutex_lock(&data->lock);
-		ret = kmx61_get_odr(data, val, val2, chan->address);
-		mutex_unlock(&data->lock);
+		scoped_guard(mutex, &data->lock)
+			ret = kmx61_get_odr(data, val, val2, chan->address);
 		if (ret)
 			return -EINVAL;
 		return IIO_VAL_INT_PLUS_MICRO;
@@ -856,19 +852,15 @@ static int kmx61_write_raw(struct iio_dev *indio_dev,
 		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
 			return -EINVAL;
 
-		mutex_lock(&data->lock);
-		ret = kmx61_set_odr(data, val, val2, chan->address);
-		mutex_unlock(&data->lock);
-		return ret;
+		guard(mutex)(&data->lock);
+		return kmx61_set_odr(data, val, val2, chan->address);
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
 		case IIO_ACCEL:
 			if (val != 0)
 				return -EINVAL;
-			mutex_lock(&data->lock);
-			ret = kmx61_set_scale(data, val2);
-			mutex_unlock(&data->lock);
-			return ret;
+			guard(mutex)(&data->lock);
+			return kmx61_set_scale(data, val2);
 		default:
 			return -EINVAL;
 		}
@@ -945,28 +937,25 @@ static int kmx61_write_event_config(struct iio_dev *indio_dev,
 	if (state && data->ev_enable_state)
 		return 0;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	if (!state && data->motion_trig_on) {
 		data->ev_enable_state = false;
-		goto err_unlock;
+		return ret;
 	}
 
 	ret = kmx61_set_power_state(data, state, KMX61_ACC);
 	if (ret < 0)
-		goto err_unlock;
+		return ret;
 
 	ret = kmx61_setup_any_motion_interrupt(data, state);
 	if (ret < 0) {
 		kmx61_set_power_state(data, false, KMX61_ACC);
-		goto err_unlock;
+		return ret;
 	}
 
 	data->ev_enable_state = state;
 
-err_unlock:
-	mutex_unlock(&data->lock);
-
 	return ret;
 }
 
@@ -1020,11 +1009,11 @@ static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
 	struct kmx61_data *data = kmx61_get_data(indio_dev);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	if (!state && data->ev_enable_state && data->motion_trig_on) {
 		data->motion_trig_on = false;
-		goto err_unlock;
+		return ret;
 	}
 
 	if (data->acc_dready_trig == trig || data->motion_trig == trig)
@@ -1034,7 +1023,7 @@ static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
 
 	ret = kmx61_set_power_state(data, state, device);
 	if (ret < 0)
-		goto err_unlock;
+		return ret;
 
 	if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
 		ret = kmx61_setup_new_data_interrupt(data, state, device);
@@ -1042,7 +1031,7 @@ static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
 		ret = kmx61_setup_any_motion_interrupt(data, state);
 	if (ret < 0) {
 		kmx61_set_power_state(data, false, device);
-		goto err_unlock;
+		return ret;
 	}
 
 	if (data->acc_dready_trig == trig)
@@ -1051,8 +1040,6 @@ static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
 		data->mag_dready_trig_on = state;
 	else
 		data->motion_trig_on = state;
-err_unlock:
-	mutex_unlock(&data->lock);
 
 	return ret;
 }
@@ -1195,19 +1182,17 @@ static irqreturn_t kmx61_trigger_handler(int irq, void *p)
 	else
 		base = KMX61_MAG_XOUT_L;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	iio_for_each_active_channel(indio_dev, bit) {
 		ret = kmx61_read_measurement(data, base, bit);
 		if (ret < 0) {
-			mutex_unlock(&data->lock);
-			goto err;
+			iio_trigger_notify_done(indio_dev->trig);
+			return IRQ_HANDLED;
 		}
 		buffer[i++] = ret;
 	}
-	mutex_unlock(&data->lock);
 
 	iio_push_to_buffers(indio_dev, buffer);
-err:
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;
@@ -1419,22 +1404,17 @@ static void kmx61_remove(struct i2c_client *client)
 		iio_trigger_unregister(data->motion_trig);
 	}
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
-	mutex_unlock(&data->lock);
 }
 
 static int kmx61_suspend(struct device *dev)
 {
-	int ret;
 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
 
-	mutex_lock(&data->lock);
-	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+	guard(mutex)(&data->lock);
+	return kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
 			     false);
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
 
 static int kmx61_resume(struct device *dev)
@@ -1453,13 +1433,9 @@ static int kmx61_resume(struct device *dev)
 static int kmx61_runtime_suspend(struct device *dev)
 {
 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
-	int ret;
-
-	mutex_lock(&data->lock);
-	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
-	mutex_unlock(&data->lock);
 
-	return ret;
+	guard(mutex)(&data->lock);
+	return kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
 }
 
 static int kmx61_runtime_resume(struct device *dev)
Signature · 1 line
2.53.0
#1 lore raw
Quoted 8 lines · Maxwell Doose

Include linux/cleanup.h to take advantage of new macros.

Replace manual mutex_lock() and mutex_unlock() calls across the file with guard(mutex)() and scoped_guard() where appropriate. This will help modernize the driver with up-to-date functions/macros.

Remove now redundant gotos and ret variables, as the new RAII macros make them unneeded.

...

drivers/iio/imu/kmx61.c static int kmx61_read_raw(struct iio_dev *indio_dev, · line 811

Quoted 5 lines
drivers/iio/imu/kmx61.c +0 -0
 		*val = sign_extend32(ret >> chan->scan_type.shift,
 				     chan->scan_type.realbits - 1);
 		ret = kmx61_set_power_state(data, false, chan->address);

Now this blank line becomes a bit confusing as the following conditional is tightly coupled with the above code, remove it.

Quoted 4 lines
drivers/iio/imu/kmx61.c +0 -1
-		mutex_unlock(&data->lock);
 		if (ret)
 			return ret;
 		return IIO_VAL_INT;

...

drivers/iio/imu/kmx61.c static irqreturn_t kmx61_trigger_handler(int irq, void *p) · line 1190

Quoted 9 lines
drivers/iio/imu/kmx61.c +3 -3
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	iio_for_each_active_channel(indio_dev, bit) {
 		ret = kmx61_read_measurement(data, base, bit);
 		if (ret < 0) {
-			mutex_unlock(&data->lock);
-			goto err;
+			iio_trigger_notify_done(indio_dev->trig);
+			return IRQ_HANDLED;

Hmm... Is the HANDLED a right choice?

drivers/iio/imu/kmx61.c static irqreturn_t kmx61_trigger_handler(int irq, void *p) · line 1198

Quoted 10 lines
drivers/iio/imu/kmx61.c +0 -2
 		}
 		buffer[i++] = ret;
 	}
-	mutex_unlock(&data->lock);
 
 	iio_push_to_buffers(indio_dev, buffer);
-err:
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;

If the answer is yes, I'm wondering if we may deduplicate that...

...

drivers/iio/imu/kmx61.c static void kmx61_remove(struct i2c_client *client)

Quoted 11 lines

static int kmx61_suspend(struct device *dev)
{

drivers/iio/imu/kmx61.c +2 -3
-	int ret;
 	struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
 
-	mutex_lock(&data->lock);
-	ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+	guard(mutex)(&data->lock);
 
+	return kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
 			     false);

One line is okay in this case.

Quoted 4 lines
drivers/iio/imu/kmx61.c +0 -3
-	mutex_unlock(&data->lock);
-
-	return ret;
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 14 lines

...

drivers/iio/imu/kmx61.c static irqreturn_t kmx61_trigger_handler(int irq, void *p) · line 1190

Quoted 9 lines
drivers/iio/imu/kmx61.c +3 -3
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	iio_for_each_active_channel(indio_dev, bit) {
 		ret = kmx61_read_measurement(data, base, bit);
 		if (ret < 0) {
-			mutex_unlock(&data->lock);
-			goto err;
+			iio_trigger_notify_done(indio_dev->trig);
+			return IRQ_HANDLED;  

Hmm... Is the HANDLED a right choice?

Ah. Long running discussion point on what right thing to do if you are sure it's your interrupt but your handling failed - in a fashion that doesn't prevent the interrupt clearing. Not obvious :(

Quoted 6 lines
Quoted 6 lines
drivers/iio/imu/kmx61.c +0 -1
 		}
 		buffer[i++] = ret;
 	}
-	mutex_unlock(&data->lock);
Quoted 7 lines

drivers/iio/imu/kmx61.c static irqreturn_t kmx61_trigger_handler(int irq, void *p) · line 1198

Quoted 5 lines
drivers/iio/imu/kmx61.c +0 -1
 	iio_push_to_buffers(indio_dev, buffer);
-err:
 	iio_trigger_notify_done(indio_dev->trig);
 
 	return IRQ_HANDLED;  

If the answer is yes, I'm wondering if we may deduplicate that...

Would require a helper function to avoid gotos + guard() in the same function or some ugly code flow. Might not be worth it.

Jonathan
p.s. I deleted some more stuff from Andy so look at his reply.

#3 lore raw
In reply to Andy Shevchenko

Hi Andy,

Quoted 13 lines · Andy Shevchenko
Quoted 8 lines · Maxwell Doose

Include linux/cleanup.h to take advantage of new macros.

Replace manual mutex_lock() and mutex_unlock() calls across the file with guard(mutex)() and scoped_guard() where appropriate. This will help modernize the driver with up-to-date functions/macros.

Remove now redundant gotos and ret variables, as the new RAII macros make them unneeded.

...

Quoted 9 lines
Quoted 5 lines
              *val = sign_extend32(ret >> chan->scan_type.shift,
                                   chan->scan_type.realbits - 1);
              ret = kmx61_set_power_state(data, false, chan->address);

Now this blank line becomes a bit confusing as the following conditional is tightly coupled with the above code, remove it.

Will do, sounds good.

Quoted 18 lines
Quoted 5 lines
-             mutex_unlock(&data->lock);
              if (ret)
                      return ret;
              return IIO_VAL_INT;

...

drivers/iio/imu/kmx61.c static irqreturn_t kmx61_trigger_handler(int irq, void *p) · line 1190

Quoted 9 lines
drivers/iio/imu/kmx61.c +3 -3
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      iio_for_each_active_channel(indio_dev, bit) {
              ret = kmx61_read_measurement(data, base, bit);
              if (ret < 0) {
-                     mutex_unlock(&data->lock);
-                     goto err;
+                     iio_trigger_notify_done(indio_dev->trig);
+                     return IRQ_HANDLED;
Quoted 2 lines

Hmm... Is the HANDLED a right choice?

This is what was done at the former err goto label, so I thought it was appropriate when we removed it to move it here.

Quoted 7 lines
Quoted 7 lines

              }
              buffer[i++] = ret;
      }

- mutex_unlock(&data->lock);

Quoted 8 lines
Quoted 5 lines
      iio_push_to_buffers(indio_dev, buffer);
-err:
      iio_trigger_notify_done(indio_dev->trig);

      return IRQ_HANDLED;

If the answer is yes, I'm wondering if we may deduplicate that...

Best way I can think of deduplication is:
A. __attribute__((cleanup())) function helper (overkill) B. macro (bad idea)
C. refactor logic so that it will just fall through to the end

If we do go with option C then we should probably just wait until this is merged and then send that patch...

Quoted 8 lines

...

Quoted 5 lines

static int kmx61_suspend(struct device *dev)
{

-     int ret;
      struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
Quoted 9 lines

drivers/iio/imu/kmx61.c static void kmx61_remove(struct i2c_client *client) · line 1417

Quoted 6 lines
drivers/iio/imu/kmx61.c +2 -2
-     mutex_lock(&data->lock);
-     ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
+     guard(mutex)(&data->lock);
 
+     return kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
                           false);

One line is okay in this case.

Also sounds good.

best regards,
max

Quoted 12 lines

drivers/iio/imu/kmx61.c static void kmx61_remove(struct i2c_client *client) · line 1418

Quoted 6 lines
drivers/iio/imu/kmx61.c +0 -3
-     mutex_unlock(&data->lock);
-
-     return ret;
 }

--
With Best Regards,
Andy Shevchenko

#4 lore raw
In reply to Maxwell Doose
kernel test robot noticed the following build errors:

Hi Maxwell,

kernel test robot noticed the following build errors:

[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on linus/master v7.1-rc2 next-20260430] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxwell-Doose/iio-imu-kmx61-Use-guard-mutex-family-over-manual-locking/20260504-212159
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20260502032455.76107-1-m32285159%40gmail.com
patch subject: [PATCH] iio: imu: kmx61: Use guard(mutex)() family over manual locking
config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20260505/[email protected]/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260505/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/iio/imu/kmx61.c:830:2: error: cannot jump from switch statement to this case label
     830 |         case IIO_CHAN_INFO_SAMP_FREQ:
         |         ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
     798 |                 guard(mutex)(&data->lock);
         |                 ^
   include/linux/cleanup.h:419:2: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^
   include/linux/cleanup.h:300:3: note: expanded from macro 'CLASS'
     300 |                 class_##_name##_constructor
         |                 ^
   <scratch space>:9:1: note: expanded from here
       9 | class_mutex_constructor
         | ^
   note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:15:1: note: expanded from here
      15 | __UNIQUE_ID_unlock_468
         | ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
   include/linux/cleanup.h:419:15: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |                      ^
   include/linux/compiler.h:168:2: note: expanded from macro '__UNIQUE_ID'
     168 |         __PASTE(__UNIQUE_ID_,                                   \
         |         ^
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:3:1: note: expanded from here
       3 | __UNIQUE_ID_guard_467
         | ^
   drivers/iio/imu/kmx61.c:816:2: error: cannot jump from switch statement to this case label
     816 |         case IIO_CHAN_INFO_SCALE:
         |         ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
     798 |                 guard(mutex)(&data->lock);
         |                 ^
   include/linux/cleanup.h:419:2: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^
   include/linux/cleanup.h:300:3: note: expanded from macro 'CLASS'
     300 |                 class_##_name##_constructor
         |                 ^
   <scratch space>:9:1: note: expanded from here
       9 | class_mutex_constructor
         | ^
   note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:15:1: note: expanded from here
      15 | __UNIQUE_ID_unlock_468
         | ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
   include/linux/cleanup.h:419:15: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |                      ^
   include/linux/compiler.h:168:2: note: expanded from macro '__UNIQUE_ID'
     168 |         __PASTE(__UNIQUE_ID_,                                   \
         |         ^
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:3:1: note: expanded from here
       3 | __UNIQUE_ID_guard_467
         | ^
   drivers/iio/imu/kmx61.c:847:6: warning: unused variable 'ret' [-Wunused-variable]
     847 |         int ret;
         |             ^~~
   drivers/iio/imu/kmx61.c:864:3: error: cannot jump from switch statement to this case label
     864 |                 default:
         |                 ^
   drivers/iio/imu/kmx61.c:862:4: note: jump bypasses initialization of variable with __attribute__((cleanup))
     862 |                         guard(mutex)(&data->lock);
         |                         ^
   include/linux/cleanup.h:419:2: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^
   include/linux/cleanup.h:300:3: note: expanded from macro 'CLASS'
     300 |                 class_##_name##_constructor
         |                 ^

<scratch space>:67:1: note: expanded from here
67 | class_mutex_constructor
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)

vim +830 drivers/iio/imu/kmx61.c

20ffac278ebd64 Daniel Baluta 2014-12-03  777  
20ffac278ebd64 Daniel Baluta 2014-12-03  778  static int kmx61_read_raw(struct iio_dev *indio_dev,
20ffac278ebd64 Daniel Baluta 2014-12-03  779  			  struct iio_chan_spec const *chan, int *val,
20ffac278ebd64 Daniel Baluta 2014-12-03  780  			  int *val2, long mask)
20ffac278ebd64 Daniel Baluta 2014-12-03  781  {
20ffac278ebd64 Daniel Baluta 2014-12-03  782  	int ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  783  	u8 base_reg;
20ffac278ebd64 Daniel Baluta 2014-12-03  784  	struct kmx61_data *data = kmx61_get_data(indio_dev);
20ffac278ebd64 Daniel Baluta 2014-12-03  785  
20ffac278ebd64 Daniel Baluta 2014-12-03  786  	switch (mask) {
20ffac278ebd64 Daniel Baluta 2014-12-03  787  	case IIO_CHAN_INFO_RAW:
20ffac278ebd64 Daniel Baluta 2014-12-03  788  		switch (chan->type) {
20ffac278ebd64 Daniel Baluta 2014-12-03  789  		case IIO_ACCEL:
20ffac278ebd64 Daniel Baluta 2014-12-03  790  			base_reg = KMX61_ACC_XOUT_L;
20ffac278ebd64 Daniel Baluta 2014-12-03  791  			break;
20ffac278ebd64 Daniel Baluta 2014-12-03  792  		case IIO_MAGN:
20ffac278ebd64 Daniel Baluta 2014-12-03  793  			base_reg = KMX61_MAG_XOUT_L;
20ffac278ebd64 Daniel Baluta 2014-12-03  794  			break;
20ffac278ebd64 Daniel Baluta 2014-12-03  795  		default:
20ffac278ebd64 Daniel Baluta 2014-12-03  796  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  797  		}
84cb5906c7fb44 Maxwell Doose 2026-05-01  798  		guard(mutex)(&data->lock);
20ffac278ebd64 Daniel Baluta 2014-12-03  799  
a3da4fa301ae60 Daniel Baluta 2014-12-23  800  		ret = kmx61_set_power_state(data, true, chan->address);
84cb5906c7fb44 Maxwell Doose 2026-05-01  801  		if (ret)
a3da4fa301ae60 Daniel Baluta 2014-12-23  802  			return ret;
a3da4fa301ae60 Daniel Baluta 2014-12-23  803  
20ffac278ebd64 Daniel Baluta 2014-12-03  804  		ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
20ffac278ebd64 Daniel Baluta 2014-12-03  805  		if (ret < 0) {
aff8609addd00e Daniel Baluta 2014-12-03  806  			kmx61_set_power_state(data, false, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  807  			return ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  808  		}
20ffac278ebd64 Daniel Baluta 2014-12-03  809  		*val = sign_extend32(ret >> chan->scan_type.shift,
20ffac278ebd64 Daniel Baluta 2014-12-03  810  				     chan->scan_type.realbits - 1);
a3da4fa301ae60 Daniel Baluta 2014-12-23  811  		ret = kmx61_set_power_state(data, false, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  812  
a3da4fa301ae60 Daniel Baluta 2014-12-23  813  		if (ret)
a3da4fa301ae60 Daniel Baluta 2014-12-23  814  			return ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  815  		return IIO_VAL_INT;
20ffac278ebd64 Daniel Baluta 2014-12-03  816  	case IIO_CHAN_INFO_SCALE:
20ffac278ebd64 Daniel Baluta 2014-12-03  817  		switch (chan->type) {
20ffac278ebd64 Daniel Baluta 2014-12-03  818  		case IIO_ACCEL:
20ffac278ebd64 Daniel Baluta 2014-12-03  819  			*val = 0;
20ffac278ebd64 Daniel Baluta 2014-12-03  820  			*val2 = kmx61_uscale_table[data->range];
20ffac278ebd64 Daniel Baluta 2014-12-03  821  			return IIO_VAL_INT_PLUS_MICRO;
20ffac278ebd64 Daniel Baluta 2014-12-03  822  		case IIO_MAGN:
20ffac278ebd64 Daniel Baluta 2014-12-03  823  			/* 14 bits res, 1465 microGauss per magn count */
20ffac278ebd64 Daniel Baluta 2014-12-03  824  			*val = 0;
20ffac278ebd64 Daniel Baluta 2014-12-03  825  			*val2 = 1465;
20ffac278ebd64 Daniel Baluta 2014-12-03  826  			return IIO_VAL_INT_PLUS_MICRO;
20ffac278ebd64 Daniel Baluta 2014-12-03  827  		default:
20ffac278ebd64 Daniel Baluta 2014-12-03  828  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  829  		}
20ffac278ebd64 Daniel Baluta 2014-12-03 @830  	case IIO_CHAN_INFO_SAMP_FREQ:
20ffac278ebd64 Daniel Baluta 2014-12-03  831  		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
20ffac278ebd64 Daniel Baluta 2014-12-03  832  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  833  
84cb5906c7fb44 Maxwell Doose 2026-05-01  834  		scoped_guard(mutex, &data->lock)
20ffac278ebd64 Daniel Baluta 2014-12-03  835  			ret = kmx61_get_odr(data, val, val2, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  836  		if (ret)
20ffac278ebd64 Daniel Baluta 2014-12-03  837  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  838  		return IIO_VAL_INT_PLUS_MICRO;
20ffac278ebd64 Daniel Baluta 2014-12-03  839  	}
20ffac278ebd64 Daniel Baluta 2014-12-03  840  	return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  841  }
20ffac278ebd64 Daniel Baluta 2014-12-03  842  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#5 lore raw
Quoted 18 lines · On Tue, 5 May 2026 12:38:07 +0800 kernel test robot <[email protected]> wrote:

Hi Maxwell,

kernel test robot noticed the following build errors:

[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on linus/master v7.1-rc2 next-20260430] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxwell-Doose/iio-imu-kmx61-Use-guard-mutex-family-over-manual-locking/20260504-212159
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20260502032455.76107-1-m32285159%40gmail.com
patch subject: [PATCH] iio: imu: kmx61: Use guard(mutex)() family over manual locking
config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20260505/[email protected]/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260505/[email protected]/reproduce)
Quoted 4 lines

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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

This complicated error is really just pointing out you haven't defined scope for the case statement with guard() in it. Need {}

Quoted 175 lines

All errors (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

drivers/iio/imu/kmx61.c:830:2: error: cannot jump from switch statement to this case label

     830 |         case IIO_CHAN_INFO_SAMP_FREQ:
         |         ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
     798 |                 guard(mutex)(&data->lock);
         |                 ^
   include/linux/cleanup.h:419:2: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^
   include/linux/cleanup.h:300:3: note: expanded from macro 'CLASS'
     300 |                 class_##_name##_constructor
         |                 ^
   <scratch space>:9:1: note: expanded from here
       9 | class_mutex_constructor
         | ^
   note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:15:1: note: expanded from here
      15 | __UNIQUE_ID_unlock_468
         | ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
   include/linux/cleanup.h:419:15: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |                      ^
   include/linux/compiler.h:168:2: note: expanded from macro '__UNIQUE_ID'
     168 |         __PASTE(__UNIQUE_ID_,                                   \
         |         ^
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:3:1: note: expanded from here
       3 | __UNIQUE_ID_guard_467
         | ^
   drivers/iio/imu/kmx61.c:816:2: error: cannot jump from switch statement to this case label
     816 |         case IIO_CHAN_INFO_SCALE:
         |         ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
     798 |                 guard(mutex)(&data->lock);
         |                 ^
   include/linux/cleanup.h:419:2: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^
   include/linux/cleanup.h:300:3: note: expanded from macro 'CLASS'
     300 |                 class_##_name##_constructor
         |                 ^
   <scratch space>:9:1: note: expanded from here
       9 | class_mutex_constructor
         | ^
   note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:15:1: note: expanded from here
      15 | __UNIQUE_ID_unlock_468
         | ^
   drivers/iio/imu/kmx61.c:798:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
   include/linux/cleanup.h:419:15: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |                      ^
   include/linux/compiler.h:168:2: note: expanded from macro '__UNIQUE_ID'
     168 |         __PASTE(__UNIQUE_ID_,                                   \
         |         ^
   include/linux/compiler_types.h:16:23: note: expanded from macro '__PASTE'
      16 | #define __PASTE(a, b) ___PASTE(a, b)
         |                       ^
   include/linux/compiler_types.h:15:24: note: expanded from macro '___PASTE'
      15 | #define ___PASTE(a, b) a##b
         |                        ^
   <scratch space>:3:1: note: expanded from here
       3 | __UNIQUE_ID_guard_467
         | ^
   drivers/iio/imu/kmx61.c:847:6: warning: unused variable 'ret' [-Wunused-variable]
     847 |         int ret;
         |             ^~~
   drivers/iio/imu/kmx61.c:864:3: error: cannot jump from switch statement to this case label
     864 |                 default:
         |                 ^
   drivers/iio/imu/kmx61.c:862:4: note: jump bypasses initialization of variable with __attribute__((cleanup))
     862 |                         guard(mutex)(&data->lock);
         |                         ^
   include/linux/cleanup.h:419:2: note: expanded from macro 'guard'
     419 |         CLASS(_name, __UNIQUE_ID(guard))
         |         ^
   include/linux/cleanup.h:300:3: note: expanded from macro 'CLASS'
     300 |                 class_##_name##_constructor
         |                 ^

<scratch space>:67:1: note: expanded from here
67 | class_mutex_constructor
| ^
note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)

vim +830 drivers/iio/imu/kmx61.c

20ffac278ebd64 Daniel Baluta 2014-12-03  777  
20ffac278ebd64 Daniel Baluta 2014-12-03  778  static int kmx61_read_raw(struct iio_dev *indio_dev,
20ffac278ebd64 Daniel Baluta 2014-12-03  779  			  struct iio_chan_spec const *chan, int *val,
20ffac278ebd64 Daniel Baluta 2014-12-03  780  			  int *val2, long mask)
20ffac278ebd64 Daniel Baluta 2014-12-03  781  {
20ffac278ebd64 Daniel Baluta 2014-12-03  782  	int ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  783  	u8 base_reg;
20ffac278ebd64 Daniel Baluta 2014-12-03  784  	struct kmx61_data *data = kmx61_get_data(indio_dev);
20ffac278ebd64 Daniel Baluta 2014-12-03  785  
20ffac278ebd64 Daniel Baluta 2014-12-03  786  	switch (mask) {
20ffac278ebd64 Daniel Baluta 2014-12-03  787  	case IIO_CHAN_INFO_RAW:
20ffac278ebd64 Daniel Baluta 2014-12-03  788  		switch (chan->type) {
20ffac278ebd64 Daniel Baluta 2014-12-03  789  		case IIO_ACCEL:
20ffac278ebd64 Daniel Baluta 2014-12-03  790  			base_reg = KMX61_ACC_XOUT_L;
20ffac278ebd64 Daniel Baluta 2014-12-03  791  			break;
20ffac278ebd64 Daniel Baluta 2014-12-03  792  		case IIO_MAGN:
20ffac278ebd64 Daniel Baluta 2014-12-03  793  			base_reg = KMX61_MAG_XOUT_L;
20ffac278ebd64 Daniel Baluta 2014-12-03  794  			break;
20ffac278ebd64 Daniel Baluta 2014-12-03  795  		default:
20ffac278ebd64 Daniel Baluta 2014-12-03  796  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  797  		}
84cb5906c7fb44 Maxwell Doose 2026-05-01  798  		guard(mutex)(&data->lock);
20ffac278ebd64 Daniel Baluta 2014-12-03  799  
a3da4fa301ae60 Daniel Baluta 2014-12-23  800  		ret = kmx61_set_power_state(data, true, chan->address);
84cb5906c7fb44 Maxwell Doose 2026-05-01  801  		if (ret)
a3da4fa301ae60 Daniel Baluta 2014-12-23  802  			return ret;
a3da4fa301ae60 Daniel Baluta 2014-12-23  803  
20ffac278ebd64 Daniel Baluta 2014-12-03  804  		ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
20ffac278ebd64 Daniel Baluta 2014-12-03  805  		if (ret < 0) {
aff8609addd00e Daniel Baluta 2014-12-03  806  			kmx61_set_power_state(data, false, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  807  			return ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  808  		}
20ffac278ebd64 Daniel Baluta 2014-12-03  809  		*val = sign_extend32(ret >> chan->scan_type.shift,
20ffac278ebd64 Daniel Baluta 2014-12-03  810  				     chan->scan_type.realbits - 1);
a3da4fa301ae60 Daniel Baluta 2014-12-23  811  		ret = kmx61_set_power_state(data, false, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  812  
a3da4fa301ae60 Daniel Baluta 2014-12-23  813  		if (ret)
a3da4fa301ae60 Daniel Baluta 2014-12-23  814  			return ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  815  		return IIO_VAL_INT;
20ffac278ebd64 Daniel Baluta 2014-12-03  816  	case IIO_CHAN_INFO_SCALE:
20ffac278ebd64 Daniel Baluta 2014-12-03  817  		switch (chan->type) {
20ffac278ebd64 Daniel Baluta 2014-12-03  818  		case IIO_ACCEL:
20ffac278ebd64 Daniel Baluta 2014-12-03  819  			*val = 0;
20ffac278ebd64 Daniel Baluta 2014-12-03  820  			*val2 = kmx61_uscale_table[data->range];
20ffac278ebd64 Daniel Baluta 2014-12-03  821  			return IIO_VAL_INT_PLUS_MICRO;
20ffac278ebd64 Daniel Baluta 2014-12-03  822  		case IIO_MAGN:
20ffac278ebd64 Daniel Baluta 2014-12-03  823  			/* 14 bits res, 1465 microGauss per magn count */
20ffac278ebd64 Daniel Baluta 2014-12-03  824  			*val = 0;
20ffac278ebd64 Daniel Baluta 2014-12-03  825  			*val2 = 1465;
20ffac278ebd64 Daniel Baluta 2014-12-03  826  			return IIO_VAL_INT_PLUS_MICRO;
20ffac278ebd64 Daniel Baluta 2014-12-03  827  		default:
20ffac278ebd64 Daniel Baluta 2014-12-03  828  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  829  		}
20ffac278ebd64 Daniel Baluta 2014-12-03 @830  	case IIO_CHAN_INFO_SAMP_FREQ:
20ffac278ebd64 Daniel Baluta 2014-12-03  831  		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
20ffac278ebd64 Daniel Baluta 2014-12-03  832  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  833  
84cb5906c7fb44 Maxwell Doose 2026-05-01  834  		scoped_guard(mutex, &data->lock)
20ffac278ebd64 Daniel Baluta 2014-12-03  835  			ret = kmx61_get_odr(data, val, val2, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  836  		if (ret)
20ffac278ebd64 Daniel Baluta 2014-12-03  837  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  838  		return IIO_VAL_INT_PLUS_MICRO;
20ffac278ebd64 Daniel Baluta 2014-12-03  839  	}
20ffac278ebd64 Daniel Baluta 2014-12-03  840  	return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  841  }
20ffac278ebd64 Daniel Baluta 2014-12-03  842  
#6 lore raw
In reply to Maxwell Doose
kernel test robot noticed the following build warnings:

Hi Maxwell,

kernel test robot noticed the following build warnings:

[auto build test WARNING on jic23-iio/togreg]
[also build test WARNING on linus/master v7.1-rc2 next-20260504] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxwell-Doose/iio-imu-kmx61-Use-guard-mutex-family-over-manual-locking/20260504-212159
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20260502032455.76107-1-m32285159%40gmail.com
patch subject: [PATCH] iio: imu: kmx61: Use guard(mutex)() family over manual locking
config: i386-buildonly-randconfig-006-20260504 (https://download.01.org/0day-ci/archive/20260505/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260505/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/iio/imu/kmx61.c: In function 'kmx61_write_raw':

>> drivers/iio/imu/kmx61.c:847:13: warning: unused variable 'ret' [-Wunused-variable]
     847 |         int ret;
         |             ^~~

vim +/ret +847 drivers/iio/imu/kmx61.c

20ffac278ebd64 Daniel Baluta 2014-12-03  842  
20ffac278ebd64 Daniel Baluta 2014-12-03  843  static int kmx61_write_raw(struct iio_dev *indio_dev,
20ffac278ebd64 Daniel Baluta 2014-12-03  844  			   struct iio_chan_spec const *chan, int val,
20ffac278ebd64 Daniel Baluta 2014-12-03  845  			   int val2, long mask)
20ffac278ebd64 Daniel Baluta 2014-12-03  846  {
20ffac278ebd64 Daniel Baluta 2014-12-03 @847  	int ret;
20ffac278ebd64 Daniel Baluta 2014-12-03  848  	struct kmx61_data *data = kmx61_get_data(indio_dev);
20ffac278ebd64 Daniel Baluta 2014-12-03  849  
20ffac278ebd64 Daniel Baluta 2014-12-03  850  	switch (mask) {
20ffac278ebd64 Daniel Baluta 2014-12-03  851  	case IIO_CHAN_INFO_SAMP_FREQ:
20ffac278ebd64 Daniel Baluta 2014-12-03  852  		if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
20ffac278ebd64 Daniel Baluta 2014-12-03  853  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  854  
84cb5906c7fb44 Maxwell Doose 2026-05-01  855  		guard(mutex)(&data->lock);
84cb5906c7fb44 Maxwell Doose 2026-05-01  856  		return kmx61_set_odr(data, val, val2, chan->address);
20ffac278ebd64 Daniel Baluta 2014-12-03  857  	case IIO_CHAN_INFO_SCALE:
20ffac278ebd64 Daniel Baluta 2014-12-03  858  		switch (chan->type) {
20ffac278ebd64 Daniel Baluta 2014-12-03  859  		case IIO_ACCEL:
20ffac278ebd64 Daniel Baluta 2014-12-03  860  			if (val != 0)
20ffac278ebd64 Daniel Baluta 2014-12-03  861  				return -EINVAL;
84cb5906c7fb44 Maxwell Doose 2026-05-01  862  			guard(mutex)(&data->lock);
84cb5906c7fb44 Maxwell Doose 2026-05-01  863  			return kmx61_set_scale(data, val2);
20ffac278ebd64 Daniel Baluta 2014-12-03  864  		default:
20ffac278ebd64 Daniel Baluta 2014-12-03  865  			return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  866  		}
20ffac278ebd64 Daniel Baluta 2014-12-03  867  	default:
20ffac278ebd64 Daniel Baluta 2014-12-03  868  		return -EINVAL;
20ffac278ebd64 Daniel Baluta 2014-12-03  869  	}
20ffac278ebd64 Daniel Baluta 2014-12-03  870  }
20ffac278ebd64 Daniel Baluta 2014-12-03  871  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki