Re: [PATCH v6 1/3] iio: magnetometer: bmc150_magn: use automated cleanup for mutex
From: Jonathan Cameron
Date: Sat Feb 14 2026 - 11:34:28 EST
On Sat, 14 Feb 2026 07:31:03 +0800
kernel test robot <lkp@xxxxxxxxx> wrote:
> Hi Neel,
>
> kernel test robot noticed the following build errors:
Just replying to this to highlight what I the whole scope thing is about.
>
> [auto build test ERROR on e7aa57247700733e52a8e2e4dee6a52c2a76de02]
>
> url: https://github.com/intel-lab-lkp/linux/commits/Neel-Bullywon/iio-magnetometer-bmc150_magn-use-automated-cleanup-for-mutex/20260211-074324
> base: e7aa57247700733e52a8e2e4dee6a52c2a76de02
> patch link: https://lore.kernel.org/r/20260210233945.40975-2-neelb2403%40gmail.com
> patch subject: [PATCH v6 1/3] iio: magnetometer: bmc150_magn: use automated cleanup for mutex
> config: loongarch-allmodconfig (https://download.01.org/0day-ci/archive/20260214/202602140708.3KGC8ZJJ-lkp@xxxxxxxxx/config)
> compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260214/202602140708.3KGC8ZJJ-lkp@xxxxxxxxx/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@xxxxxxxxx>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202602140708.3KGC8ZJJ-lkp@xxxxxxxxx/
>
> All errors (new ones prefixed by >>):
>
> >> drivers/iio/magnetometer/bmc150_magn.c:561:2: error: cannot jump from switch statement to this case label
> 561 | default:
> | ^
> drivers/iio/magnetometer/bmc150_magn.c:527:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
> 527 | guard(mutex)(&data->mutex);
> | ^
> include/linux/cleanup.h:414:15: note: expanded from macro 'guard'
> 414 | 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>:122:1: note: expanded from here
> 122 | __UNIQUE_ID_guard_677
> | ^
> drivers/iio/magnetometer/bmc150_magn.c:529:2: error: cannot jump from switch statement to this case label
> 529 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> | ^
> drivers/iio/magnetometer/bmc150_magn.c:527:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
> 527 | guard(mutex)(&data->mutex);
> | ^
> include/linux/cleanup.h:414:15: note: expanded from macro 'guard'
> 414 | 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>:122:1: note: expanded from here
> 122 | __UNIQUE_ID_guard_677
> | ^
> 2 errors generated.
>
>
> vim +561 drivers/iio/magnetometer/bmc150_magn.c
>
> c91746a2361d75 Irina Tirdea 2015-04-29 515
> c91746a2361d75 Irina Tirdea 2015-04-29 516 static int bmc150_magn_write_raw(struct iio_dev *indio_dev,
> c91746a2361d75 Irina Tirdea 2015-04-29 517 struct iio_chan_spec const *chan,
> c91746a2361d75 Irina Tirdea 2015-04-29 518 int val, int val2, long mask)
> c91746a2361d75 Irina Tirdea 2015-04-29 519 {
> c91746a2361d75 Irina Tirdea 2015-04-29 520 struct bmc150_magn_data *data = iio_priv(indio_dev);
> c91746a2361d75 Irina Tirdea 2015-04-29 521 int ret;
> c91746a2361d75 Irina Tirdea 2015-04-29 522
> c91746a2361d75 Irina Tirdea 2015-04-29 523 switch (mask) {
> c91746a2361d75 Irina Tirdea 2015-04-29 524 case IIO_CHAN_INFO_SAMP_FREQ:
> 5990dc9703679a Irina Tirdea 2015-04-29 525 if (val > data->max_odr)
> 5990dc9703679a Irina Tirdea 2015-04-29 526 return -EINVAL;
> d65a37daae6aa8 Neel Bullywon 2026-02-10 527 guard(mutex)(&data->mutex);
It's hard to spot from the error message, but here is the bug. This is in the same
scope as default label below that is hightlighted.
Thus wonderfully the __free() that hides under the guard() magic is
initialized but because we don't go down this path to reach that
default, we can the __free() (ultimately mutex_unlock())
on an unassigned pointer.
Hence scope for these really matters!
Jonathan
> d65a37daae6aa8 Neel Bullywon 2026-02-10 528 return bmc150_magn_set_odr(data, val);
> 5990dc9703679a Irina Tirdea 2015-04-29 529 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> 5990dc9703679a Irina Tirdea 2015-04-29 530 switch (chan->channel2) {
> 5990dc9703679a Irina Tirdea 2015-04-29 531 case IIO_MOD_X:
> 5990dc9703679a Irina Tirdea 2015-04-29 532 case IIO_MOD_Y:
> 5990dc9703679a Irina Tirdea 2015-04-29 533 if (val < 1 || val > 511)
> 5990dc9703679a Irina Tirdea 2015-04-29 534 return -EINVAL;
> d65a37daae6aa8 Neel Bullywon 2026-02-10 535 {
> d65a37daae6aa8 Neel Bullywon 2026-02-10 536 guard(mutex)(&data->mutex);
> 5990dc9703679a Irina Tirdea 2015-04-29 537 ret = bmc150_magn_set_max_odr(data, val, 0, 0);
> d65a37daae6aa8 Neel Bullywon 2026-02-10 538 if (ret < 0)
> 5990dc9703679a Irina Tirdea 2015-04-29 539 return ret;
> d65a37daae6aa8 Neel Bullywon 2026-02-10 540 return regmap_update_bits(data->regmap,
> 5990dc9703679a Irina Tirdea 2015-04-29 541 BMC150_MAGN_REG_REP_XY,
> 1506f3cd0b8169 Hartmut Knaack 2015-07-17 542 BMC150_MAGN_REG_REP_DATAMASK,
> d65a37daae6aa8 Neel Bullywon 2026-02-10 543 BMC150_MAGN_REPXY_TO_REGVAL(val));
> d65a37daae6aa8 Neel Bullywon 2026-02-10 544 }
> 5990dc9703679a Irina Tirdea 2015-04-29 545 case IIO_MOD_Z:
> 5990dc9703679a Irina Tirdea 2015-04-29 546 if (val < 1 || val > 256)
> 5990dc9703679a Irina Tirdea 2015-04-29 547 return -EINVAL;
> d65a37daae6aa8 Neel Bullywon 2026-02-10 548 {
> d65a37daae6aa8 Neel Bullywon 2026-02-10 549 guard(mutex)(&data->mutex);
> 5990dc9703679a Irina Tirdea 2015-04-29 550 ret = bmc150_magn_set_max_odr(data, 0, val, 0);
> d65a37daae6aa8 Neel Bullywon 2026-02-10 551 if (ret < 0)
> 5990dc9703679a Irina Tirdea 2015-04-29 552 return ret;
> d65a37daae6aa8 Neel Bullywon 2026-02-10 553 return regmap_update_bits(data->regmap,
> 5990dc9703679a Irina Tirdea 2015-04-29 554 BMC150_MAGN_REG_REP_Z,
> 1506f3cd0b8169 Hartmut Knaack 2015-07-17 555 BMC150_MAGN_REG_REP_DATAMASK,
> d65a37daae6aa8 Neel Bullywon 2026-02-10 556 BMC150_MAGN_REPZ_TO_REGVAL(val));
> d65a37daae6aa8 Neel Bullywon 2026-02-10 557 }
> 5990dc9703679a Irina Tirdea 2015-04-29 558 default:
> 5990dc9703679a Irina Tirdea 2015-04-29 559 return -EINVAL;
> 5990dc9703679a Irina Tirdea 2015-04-29 560 }
> c91746a2361d75 Irina Tirdea 2015-04-29 @561 default:
> c91746a2361d75 Irina Tirdea 2015-04-29 562 return -EINVAL;
> c91746a2361d75 Irina Tirdea 2015-04-29 563 }
> c91746a2361d75 Irina Tirdea 2015-04-29 564 }
> c91746a2361d75 Irina Tirdea 2015-04-29 565
>