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

iio: core: Move the currentmode entry to the opaque structure

This entry should, under no situation, be modified by device
drivers. Now that we have limited its read access to device drivers
really needing it and did so through a dedicated helper, we can
easily move this variable to the opaque structure in order to
prevent any further modification from non-authorized code (out of the
core, basically).

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Reviewed-by: Alexandru Ardelean <ardeleanalex@gmail.com>
Link: https://lore.kernel.org/r/20220207143840.707510-12-miquel.raynal@bootlin.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Miquel Raynal and committed by
Jonathan Cameron
51570c9d 8c576f87

+18 -14
+6 -6
drivers/iio/industrialio-buffer.c
··· 1065 1065 indio_dev->active_scan_mask = config->scan_mask; 1066 1066 indio_dev->scan_timestamp = config->scan_timestamp; 1067 1067 indio_dev->scan_bytes = config->scan_bytes; 1068 - indio_dev->currentmode = config->mode; 1068 + iio_dev_opaque->currentmode = config->mode; 1069 1069 1070 1070 iio_update_demux(indio_dev); 1071 1071 ··· 1103 1103 } 1104 1104 } 1105 1105 1106 - if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { 1106 + if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { 1107 1107 ret = iio_trigger_attach_poll_func(indio_dev->trig, 1108 1108 indio_dev->pollfunc); 1109 1109 if (ret) ··· 1122 1122 return 0; 1123 1123 1124 1124 err_detach_pollfunc: 1125 - if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { 1125 + if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { 1126 1126 iio_trigger_detach_poll_func(indio_dev->trig, 1127 1127 indio_dev->pollfunc); 1128 1128 } ··· 1135 1135 if (indio_dev->setup_ops->postdisable) 1136 1136 indio_dev->setup_ops->postdisable(indio_dev); 1137 1137 err_undo_config: 1138 - indio_dev->currentmode = INDIO_DIRECT_MODE; 1138 + iio_dev_opaque->currentmode = INDIO_DIRECT_MODE; 1139 1139 indio_dev->active_scan_mask = NULL; 1140 1140 1141 1141 return ret; ··· 1165 1165 ret = ret2; 1166 1166 } 1167 1167 1168 - if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { 1168 + if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { 1169 1169 iio_trigger_detach_poll_func(indio_dev->trig, 1170 1170 indio_dev->pollfunc); 1171 1171 } ··· 1184 1184 1185 1185 iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask); 1186 1186 indio_dev->active_scan_mask = NULL; 1187 - indio_dev->currentmode = INDIO_DIRECT_MODE; 1187 + iio_dev_opaque->currentmode = INDIO_DIRECT_MODE; 1188 1188 1189 1189 return ret; 1190 1190 }
+7 -3
drivers/iio/industrialio-core.c
··· 190 190 */ 191 191 bool iio_buffer_enabled(struct iio_dev *indio_dev) 192 192 { 193 - return indio_dev->currentmode 193 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 194 + 195 + return iio_dev_opaque->currentmode 194 196 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | 195 197 INDIO_BUFFER_SOFTWARE); 196 198 } ··· 2074 2072 2075 2073 /** 2076 2074 * iio_device_get_current_mode() - helper function providing read-only access to 2077 - * the @currentmode variable 2075 + * the opaque @currentmode variable 2078 2076 * @indio_dev: IIO device structure for device 2079 2077 */ 2080 2078 int iio_device_get_current_mode(struct iio_dev *indio_dev) 2081 2079 { 2082 - return indio_dev->currentmode; 2080 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 2081 + 2082 + return iio_dev_opaque->currentmode; 2083 2083 } 2084 2084 EXPORT_SYMBOL_GPL(iio_device_get_current_mode); 2085 2085
+1 -1
drivers/iio/industrialio-trigger.c
··· 444 444 int ret; 445 445 446 446 mutex_lock(&indio_dev->mlock); 447 - if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { 447 + if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { 448 448 mutex_unlock(&indio_dev->mlock); 449 449 return -EBUSY; 450 450 }
+4
include/linux/iio/iio-opaque.h
··· 7 7 * struct iio_dev_opaque - industrial I/O device opaque information 8 8 * @indio_dev: public industrial I/O device information 9 9 * @id: used to identify device internally 10 + * @currentmode: operating mode currently in use, may be eventually 11 + * checked by device drivers but should be considered 12 + * read-only as this is a core internal bit 10 13 * @driver_module: used to make it harder to undercut users 11 14 * @info_exist_lock: lock to prevent use during removal 12 15 * @trig_readonly: mark the current trigger immutable ··· 39 36 */ 40 37 struct iio_dev_opaque { 41 38 struct iio_dev indio_dev; 39 + int currentmode; 42 40 int id; 43 41 struct module *driver_module; 44 42 struct mutex info_exist_lock;
-4
include/linux/iio/iio.h
··· 494 494 * also be filed up by the IIO core, as a result of 495 495 * enabling particular features in the driver 496 496 * (see iio_triggered_event_setup()). 497 - * @currentmode: [INTERN] operating mode currently in use, may be 498 - * eventually checked by device drivers but should be 499 - * considered read-only as this is a core internal bit 500 497 * @dev: [DRIVER] device structure, should be assigned a parent 501 498 * and owner 502 499 * @buffer: [DRIVER] any buffer present ··· 520 523 */ 521 524 struct iio_dev { 522 525 int modes; 523 - int currentmode; 524 526 struct device dev; 525 527 526 528 struct iio_buffer *buffer;