Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: Don't silently expect attribute types

The iio_triggered_buffer_setup_ext() and the
devm_iio_kfifo_buffer_setup_ext() were changed by
commit 15097c7a1adc ("iio: buffer: wrap all buffer attributes into iio_dev_attr")
to silently expect that all attributes given in buffer_attrs array are
device-attributes. This expectation was not forced by the API - and some
drivers did register attributes created by IIO_CONST_ATTR().

When using IIO_CONST_ATTRs the added attribute "wrapping" does not copy
the pointer to stored string constant and when the sysfs file is read the
kernel will access to invalid location.

Change the function signatures to expect an array of iio_dev_attrs to
avoid similar errors in the future.

Merge conflict resolved whilst applying due to patch crossing with
two new drivers (kx022a accelerometer and ad4130 ADC).

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Tested-by: Claudiu Beznea <claudiu.beznea@microchip.com>
Link: https://lore.kernel.org/r/63f54787a684eb1232f1c5d275a09c786987fe4a.1664782676.git.mazziesaccount@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Matti Vaittinen and committed by
Jonathan Cameron
0a33755c c1531e3a

+55 -51
+5 -5
drivers/iio/accel/adxl367.c
··· 1191 1191 static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, 1192 1192 adxl367_get_fifo_enabled, NULL, 0); 1193 1193 1194 - static const struct attribute *adxl367_fifo_attributes[] = { 1195 - &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr, 1196 - &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, 1197 - &iio_dev_attr_hwfifo_watermark.dev_attr.attr, 1198 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 1194 + static const struct iio_dev_attr *adxl367_fifo_attributes[] = { 1195 + &iio_dev_attr_hwfifo_watermark_min, 1196 + &iio_dev_attr_hwfifo_watermark_max, 1197 + &iio_dev_attr_hwfifo_watermark, 1198 + &iio_dev_attr_hwfifo_enabled, 1199 1199 NULL, 1200 1200 }; 1201 1201
+5 -5
drivers/iio/accel/adxl372.c
··· 1006 1006 static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, 1007 1007 adxl372_get_fifo_enabled, NULL, 0); 1008 1008 1009 - static const struct attribute *adxl372_fifo_attributes[] = { 1010 - &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr, 1011 - &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, 1012 - &iio_dev_attr_hwfifo_watermark.dev_attr.attr, 1013 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 1009 + static const struct iio_dev_attr *adxl372_fifo_attributes[] = { 1010 + &iio_dev_attr_hwfifo_watermark_min, 1011 + &iio_dev_attr_hwfifo_watermark_max, 1012 + &iio_dev_attr_hwfifo_watermark, 1013 + &iio_dev_attr_hwfifo_enabled, 1014 1014 NULL, 1015 1015 }; 1016 1016
+6 -6
drivers/iio/accel/bmc150-accel-core.c
··· 933 933 static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO, 934 934 bmc150_accel_get_fifo_watermark, NULL, 0); 935 935 936 - static const struct attribute *bmc150_accel_fifo_attributes[] = { 937 - &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr, 938 - &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, 939 - &iio_dev_attr_hwfifo_watermark.dev_attr.attr, 940 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 936 + static const struct iio_dev_attr *bmc150_accel_fifo_attributes[] = { 937 + &iio_dev_attr_hwfifo_watermark_min, 938 + &iio_dev_attr_hwfifo_watermark_max, 939 + &iio_dev_attr_hwfifo_watermark, 940 + &iio_dev_attr_hwfifo_enabled, 941 941 NULL, 942 942 }; 943 943 ··· 1665 1665 enum bmc150_type type, const char *name, 1666 1666 bool block_supported) 1667 1667 { 1668 - const struct attribute **fifo_attrs; 1668 + const struct iio_dev_attr **fifo_attrs; 1669 1669 struct bmc150_accel_data *data; 1670 1670 struct iio_dev *indio_dev; 1671 1671 int ret;
+3 -3
drivers/iio/accel/kionix-kx022a.c
··· 575 575 static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0); 576 576 static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0); 577 577 578 - static const struct attribute *kx022a_fifo_attributes[] = { 579 - &iio_dev_attr_hwfifo_watermark.dev_attr.attr, 580 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 578 + static const struct iio_dev_attr *kx022a_fifo_attributes[] = { 579 + &iio_dev_attr_hwfifo_watermark, 580 + &iio_dev_attr_hwfifo_enabled, 581 581 NULL 582 582 }; 583 583
+5 -5
drivers/iio/adc/ad4130.c
··· 1380 1380 static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0); 1381 1381 static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0); 1382 1382 1383 - static const struct attribute *ad4130_fifo_attributes[] = { 1384 - &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr, 1385 - &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, 1386 - &iio_dev_attr_hwfifo_watermark.dev_attr.attr, 1387 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 1383 + static const struct iio_dev_attr *ad4130_fifo_attributes[] = { 1384 + &iio_dev_attr_hwfifo_watermark_min, 1385 + &iio_dev_attr_hwfifo_watermark_max, 1386 + &iio_dev_attr_hwfifo_watermark, 1387 + &iio_dev_attr_hwfifo_enabled, 1388 1388 NULL 1389 1389 }; 1390 1390
+6 -6
drivers/iio/adc/at91-sama5d2_adc.c
··· 2201 2201 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "2"); 2202 2202 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max, AT91_HWFIFO_MAX_SIZE_STR); 2203 2203 2204 - static const struct attribute *at91_adc_fifo_attributes[] = { 2205 - &iio_dev_attr_hwfifo_watermark_min.dev_attr.attr, 2206 - &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, 2207 - &iio_dev_attr_hwfifo_watermark.dev_attr.attr, 2208 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 2204 + static const struct iio_dev_attr *at91_adc_fifo_attributes[] = { 2205 + &iio_dev_attr_hwfifo_watermark_min, 2206 + &iio_dev_attr_hwfifo_watermark_max, 2207 + &iio_dev_attr_hwfifo_watermark, 2208 + &iio_dev_attr_hwfifo_enabled, 2209 2209 NULL, 2210 2210 }; 2211 2211 ··· 2222 2222 struct iio_dev *indio) 2223 2223 { 2224 2224 struct at91_adc_state *st = iio_priv(indio); 2225 - const struct attribute **fifo_attrs; 2225 + const struct iio_dev_attr **fifo_attrs; 2226 2226 int ret; 2227 2227 2228 2228 if (st->selected_trig->hw_trig)
+2 -2
drivers/iio/buffer/industrialio-buffer-dmaengine.c
··· 142 142 static IIO_DEVICE_ATTR(length_align_bytes, 0444, 143 143 iio_dmaengine_buffer_get_length_align, NULL, 0); 144 144 145 - static const struct attribute *iio_dmaengine_buffer_attrs[] = { 146 - &iio_dev_attr_length_align_bytes.dev_attr.attr, 145 + static const struct iio_dev_attr *iio_dmaengine_buffer_attrs[] = { 146 + &iio_dev_attr_length_align_bytes, 147 147 NULL, 148 148 }; 149 149
+2 -2
drivers/iio/buffer/industrialio-triggered-buffer.c
··· 41 41 irqreturn_t (*thread)(int irq, void *p), 42 42 enum iio_buffer_direction direction, 43 43 const struct iio_buffer_setup_ops *setup_ops, 44 - const struct attribute **buffer_attrs) 44 + const struct iio_dev_attr **buffer_attrs) 45 45 { 46 46 struct iio_buffer *buffer; 47 47 int ret; ··· 110 110 irqreturn_t (*thread)(int irq, void *p), 111 111 enum iio_buffer_direction direction, 112 112 const struct iio_buffer_setup_ops *ops, 113 - const struct attribute **buffer_attrs) 113 + const struct iio_dev_attr **buffer_attrs) 114 114 { 115 115 int ret; 116 116
+1 -1
drivers/iio/buffer/kfifo_buf.c
··· 270 270 int devm_iio_kfifo_buffer_setup_ext(struct device *dev, 271 271 struct iio_dev *indio_dev, 272 272 const struct iio_buffer_setup_ops *setup_ops, 273 - const struct attribute **buffer_attrs) 273 + const struct iio_dev_attr **buffer_attrs) 274 274 { 275 275 struct iio_buffer *buffer; 276 276
+3 -3
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
··· 172 172 173 173 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0); 174 174 175 - static const struct attribute *cros_ec_sensor_fifo_attributes[] = { 176 - &iio_dev_attr_hwfifo_timeout.dev_attr.attr, 177 - &iio_dev_attr_hwfifo_watermark_max.dev_attr.attr, 175 + static const struct iio_dev_attr *cros_ec_sensor_fifo_attributes[] = { 176 + &iio_dev_attr_hwfifo_timeout, 177 + &iio_dev_attr_hwfifo_watermark_max, 178 178 NULL, 179 179 }; 180 180
+4 -4
drivers/iio/common/hid-sensors/hid-sensor-trigger.c
··· 75 75 static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, 76 76 _hid_sensor_get_fifo_state, NULL, 0); 77 77 78 - static const struct attribute *hid_sensor_fifo_attributes[] = { 79 - &iio_dev_attr_hwfifo_timeout.dev_attr.attr, 80 - &iio_dev_attr_hwfifo_enabled.dev_attr.attr, 78 + static const struct iio_dev_attr *hid_sensor_fifo_attributes[] = { 79 + &iio_dev_attr_hwfifo_timeout, 80 + &iio_dev_attr_hwfifo_enabled, 81 81 NULL, 82 82 }; 83 83 ··· 231 231 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 232 232 struct hid_sensor_common *attrb) 233 233 { 234 - const struct attribute **fifo_attrs; 234 + const struct iio_dev_attr **fifo_attrs; 235 235 int ret; 236 236 struct iio_trigger *trig; 237 237
+7 -4
drivers/iio/industrialio-buffer.c
··· 1605 1605 { 1606 1606 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1607 1607 struct iio_dev_attr *p; 1608 + const struct iio_dev_attr *id_attr; 1608 1609 struct attribute **attr; 1609 1610 int ret, i, attrn, scan_el_attrcount, buffer_attrcount; 1610 1611 const struct iio_chan_spec *channels; ··· 1615 1614 while (buffer->attrs[buffer_attrcount] != NULL) 1616 1615 buffer_attrcount++; 1617 1616 } 1617 + buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs); 1618 1618 1619 1619 scan_el_attrcount = 0; 1620 1620 INIT_LIST_HEAD(&buffer->buffer_attr_list); ··· 1658 1656 } 1659 1657 } 1660 1658 1661 - attrn = buffer_attrcount + scan_el_attrcount + ARRAY_SIZE(iio_buffer_attrs); 1659 + attrn = buffer_attrcount + scan_el_attrcount; 1662 1660 attr = kcalloc(attrn + 1, sizeof(*attr), GFP_KERNEL); 1663 1661 if (!attr) { 1664 1662 ret = -ENOMEM; ··· 1673 1671 attr[2] = &dev_attr_watermark_ro.attr; 1674 1672 1675 1673 if (buffer->attrs) 1676 - memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs, 1677 - sizeof(struct attribute *) * buffer_attrcount); 1674 + for (i = 0, id_attr = buffer->attrs[i]; 1675 + (id_attr = buffer->attrs[i]); i++) 1676 + attr[ARRAY_SIZE(iio_buffer_attrs) + i] = 1677 + (struct attribute *)&id_attr->dev_attr.attr; 1678 1678 1679 - buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs); 1680 1679 buffer->buffer_group.attrs = attr; 1681 1680 1682 1681 for (i = 0; i < buffer_attrcount; i++) {
+1 -1
include/linux/iio/buffer_impl.h
··· 123 123 struct attribute_group buffer_group; 124 124 125 125 /* @attrs: Standard attributes of the buffer. */ 126 - const struct attribute **attrs; 126 + const struct iio_dev_attr **attrs; 127 127 128 128 /* @demux_bounce: Buffer for doing gather from incoming scan. */ 129 129 void *demux_bounce;
+2 -1
include/linux/iio/kfifo_buf.h
··· 5 5 struct iio_buffer; 6 6 struct iio_buffer_setup_ops; 7 7 struct iio_dev; 8 + struct iio_dev_attr; 8 9 struct device; 9 10 10 11 struct iio_buffer *iio_kfifo_allocate(void); ··· 14 13 int devm_iio_kfifo_buffer_setup_ext(struct device *dev, 15 14 struct iio_dev *indio_dev, 16 15 const struct iio_buffer_setup_ops *setup_ops, 17 - const struct attribute **buffer_attrs); 16 + const struct iio_dev_attr **buffer_attrs); 18 17 19 18 #define devm_iio_kfifo_buffer_setup(dev, indio_dev, setup_ops) \ 20 19 devm_iio_kfifo_buffer_setup_ext((dev), (indio_dev), (setup_ops), NULL)
+3 -3
include/linux/iio/triggered_buffer.h
··· 5 5 #include <linux/iio/buffer.h> 6 6 #include <linux/interrupt.h> 7 7 8 - struct attribute; 9 8 struct iio_dev; 9 + struct iio_dev_attr; 10 10 struct iio_buffer_setup_ops; 11 11 12 12 int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev, ··· 14 14 irqreturn_t (*thread)(int irq, void *p), 15 15 enum iio_buffer_direction direction, 16 16 const struct iio_buffer_setup_ops *setup_ops, 17 - const struct attribute **buffer_attrs); 17 + const struct iio_dev_attr **buffer_attrs); 18 18 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev); 19 19 20 20 #define iio_triggered_buffer_setup(indio_dev, h, thread, setup_ops) \ ··· 28 28 irqreturn_t (*thread)(int irq, void *p), 29 29 enum iio_buffer_direction direction, 30 30 const struct iio_buffer_setup_ops *ops, 31 - const struct attribute **buffer_attrs); 31 + const struct iio_dev_attr **buffer_attrs); 32 32 33 33 #define devm_iio_triggered_buffer_setup(dev, indio_dev, h, thread, setup_ops) \ 34 34 devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), \