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

iio: core: move 'mlock' to 'struct iio_dev_opaque'

Now that there are no more users accessing 'mlock' directly, we can move
it to the iio_dev private structure. Hence, it's now explicit that new
driver's should not directly use this lock.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20221012151620.1725215-5-nuno.sa@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Nuno Sá and committed by
Jonathan Cameron
16afe125 6b701cda

+42 -37
-3
drivers/iio/TODO
··· 7 7 - ABI Documentation 8 8 - Audit driviers/iio/staging/Documentation 9 9 10 - - Replace iio_dev->mlock by either a local lock or use 11 - iio_claim_direct.(Requires analysis of the purpose of the lock.) 12 - 13 10 - Converting drivers from device tree centric to more generic 14 11 property handlers. 15 12
+17 -12
drivers/iio/industrialio-buffer.c
··· 507 507 int ret; 508 508 bool state; 509 509 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 510 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 510 511 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 511 512 struct iio_buffer *buffer = this_attr->buffer; 512 513 513 514 ret = kstrtobool(buf, &state); 514 515 if (ret < 0) 515 516 return ret; 516 - mutex_lock(&indio_dev->mlock); 517 + mutex_lock(&iio_dev_opaque->mlock); 517 518 if (iio_buffer_is_active(buffer)) { 518 519 ret = -EBUSY; 519 520 goto error_ret; ··· 533 532 } 534 533 535 534 error_ret: 536 - mutex_unlock(&indio_dev->mlock); 535 + mutex_unlock(&iio_dev_opaque->mlock); 537 536 538 537 return ret < 0 ? ret : len; 539 538 ··· 555 554 { 556 555 int ret; 557 556 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 557 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 558 558 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer; 559 559 bool state; 560 560 ··· 563 561 if (ret < 0) 564 562 return ret; 565 563 566 - mutex_lock(&indio_dev->mlock); 564 + mutex_lock(&iio_dev_opaque->mlock); 567 565 if (iio_buffer_is_active(buffer)) { 568 566 ret = -EBUSY; 569 567 goto error_ret; 570 568 } 571 569 buffer->scan_timestamp = state; 572 570 error_ret: 573 - mutex_unlock(&indio_dev->mlock); 571 + mutex_unlock(&iio_dev_opaque->mlock); 574 572 575 573 return ret ? ret : len; 576 574 } ··· 644 642 const char *buf, size_t len) 645 643 { 646 644 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 645 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 647 646 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer; 648 647 unsigned int val; 649 648 int ret; ··· 656 653 if (val == buffer->length) 657 654 return len; 658 655 659 - mutex_lock(&indio_dev->mlock); 656 + mutex_lock(&iio_dev_opaque->mlock); 660 657 if (iio_buffer_is_active(buffer)) { 661 658 ret = -EBUSY; 662 659 } else { ··· 668 665 if (buffer->length && buffer->length < buffer->watermark) 669 666 buffer->watermark = buffer->length; 670 667 out: 671 - mutex_unlock(&indio_dev->mlock); 668 + mutex_unlock(&iio_dev_opaque->mlock); 672 669 673 670 return ret ? ret : len; 674 671 } ··· 1259 1256 return -EINVAL; 1260 1257 1261 1258 mutex_lock(&iio_dev_opaque->info_exist_lock); 1262 - mutex_lock(&indio_dev->mlock); 1259 + mutex_lock(&iio_dev_opaque->mlock); 1263 1260 1264 1261 if (insert_buffer && iio_buffer_is_active(insert_buffer)) 1265 1262 insert_buffer = NULL; ··· 1280 1277 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 1281 1278 1282 1279 out_unlock: 1283 - mutex_unlock(&indio_dev->mlock); 1280 + mutex_unlock(&iio_dev_opaque->mlock); 1284 1281 mutex_unlock(&iio_dev_opaque->info_exist_lock); 1285 1282 1286 1283 return ret; ··· 1299 1296 int ret; 1300 1297 bool requested_state; 1301 1298 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1299 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1302 1300 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer; 1303 1301 bool inlist; 1304 1302 ··· 1307 1303 if (ret < 0) 1308 1304 return ret; 1309 1305 1310 - mutex_lock(&indio_dev->mlock); 1306 + mutex_lock(&iio_dev_opaque->mlock); 1311 1307 1312 1308 /* Find out if it is in the list */ 1313 1309 inlist = iio_buffer_is_active(buffer); ··· 1321 1317 ret = __iio_update_buffers(indio_dev, NULL, buffer); 1322 1318 1323 1319 done: 1324 - mutex_unlock(&indio_dev->mlock); 1320 + mutex_unlock(&iio_dev_opaque->mlock); 1325 1321 return (ret < 0) ? ret : len; 1326 1322 } 1327 1323 ··· 1338 1334 const char *buf, size_t len) 1339 1335 { 1340 1336 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1337 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1341 1338 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer; 1342 1339 unsigned int val; 1343 1340 int ret; ··· 1349 1344 if (!val) 1350 1345 return -EINVAL; 1351 1346 1352 - mutex_lock(&indio_dev->mlock); 1347 + mutex_lock(&iio_dev_opaque->mlock); 1353 1348 1354 1349 if (val > buffer->length) { 1355 1350 ret = -EINVAL; ··· 1363 1358 1364 1359 buffer->watermark = val; 1365 1360 out: 1366 - mutex_unlock(&indio_dev->mlock); 1361 + mutex_unlock(&iio_dev_opaque->mlock); 1367 1362 1368 1363 return ret ? ret : len; 1369 1364 }
+15 -11
drivers/iio/industrialio-core.c
··· 285 285 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 286 286 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; 287 287 288 - ret = mutex_lock_interruptible(&indio_dev->mlock); 288 + ret = mutex_lock_interruptible(&iio_dev_opaque->mlock); 289 289 if (ret) 290 290 return ret; 291 291 if ((ev_int && iio_event_enabled(ev_int)) || 292 292 iio_buffer_enabled(indio_dev)) { 293 - mutex_unlock(&indio_dev->mlock); 293 + mutex_unlock(&iio_dev_opaque->mlock); 294 294 return -EBUSY; 295 295 } 296 296 iio_dev_opaque->clock_id = clock_id; 297 - mutex_unlock(&indio_dev->mlock); 297 + mutex_unlock(&iio_dev_opaque->mlock); 298 298 299 299 return 0; 300 300 } ··· 1674 1674 indio_dev->dev.type = &iio_device_type; 1675 1675 indio_dev->dev.bus = &iio_bus_type; 1676 1676 device_initialize(&indio_dev->dev); 1677 - mutex_init(&indio_dev->mlock); 1677 + mutex_init(&iio_dev_opaque->mlock); 1678 1678 mutex_init(&iio_dev_opaque->info_exist_lock); 1679 1679 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list); 1680 1680 ··· 1696 1696 INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers); 1697 1697 1698 1698 lockdep_register_key(&iio_dev_opaque->mlock_key); 1699 - lockdep_set_class(&indio_dev->mlock, &iio_dev_opaque->mlock_key); 1699 + lockdep_set_class(&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key); 1700 1700 1701 1701 return indio_dev; 1702 1702 } ··· 2058 2058 */ 2059 2059 int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 2060 2060 { 2061 - mutex_lock(&indio_dev->mlock); 2061 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 2062 + 2063 + mutex_lock(&iio_dev_opaque->mlock); 2062 2064 2063 2065 if (iio_buffer_enabled(indio_dev)) { 2064 - mutex_unlock(&indio_dev->mlock); 2066 + mutex_unlock(&iio_dev_opaque->mlock); 2065 2067 return -EBUSY; 2066 2068 } 2067 2069 return 0; ··· 2081 2079 */ 2082 2080 void iio_device_release_direct_mode(struct iio_dev *indio_dev) 2083 2081 { 2084 - mutex_unlock(&indio_dev->mlock); 2082 + mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock); 2085 2083 } 2086 2084 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 2087 2085 ··· 2098 2096 */ 2099 2097 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev) 2100 2098 { 2101 - mutex_lock(&indio_dev->mlock); 2099 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 2100 + 2101 + mutex_lock(&iio_dev_opaque->mlock); 2102 2102 2103 2103 if (iio_buffer_enabled(indio_dev)) 2104 2104 return 0; 2105 2105 2106 - mutex_unlock(&indio_dev->mlock); 2106 + mutex_unlock(&iio_dev_opaque->mlock); 2107 2107 return -EBUSY; 2108 2108 } 2109 2109 EXPORT_SYMBOL_GPL(iio_device_claim_buffer_mode); ··· 2121 2117 */ 2122 2118 void iio_device_release_buffer_mode(struct iio_dev *indio_dev) 2123 2119 { 2124 - mutex_unlock(&indio_dev->mlock); 2120 + mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock); 2125 2121 } 2126 2122 EXPORT_SYMBOL_GPL(iio_device_release_buffer_mode); 2127 2123
+2 -2
drivers/iio/industrialio-event.c
··· 198 198 if (ev_int == NULL) 199 199 return -ENODEV; 200 200 201 - fd = mutex_lock_interruptible(&indio_dev->mlock); 201 + fd = mutex_lock_interruptible(&iio_dev_opaque->mlock); 202 202 if (fd) 203 203 return fd; 204 204 ··· 219 219 } 220 220 221 221 unlock: 222 - mutex_unlock(&indio_dev->mlock); 222 + mutex_unlock(&iio_dev_opaque->mlock); 223 223 return fd; 224 224 } 225 225
+6 -6
drivers/iio/industrialio-trigger.c
··· 120 120 return -EINVAL; 121 121 122 122 iio_dev_opaque = to_iio_dev_opaque(indio_dev); 123 - mutex_lock(&indio_dev->mlock); 123 + mutex_lock(&iio_dev_opaque->mlock); 124 124 WARN_ON(iio_dev_opaque->trig_readonly); 125 125 126 126 indio_dev->trig = iio_trigger_get(trig); 127 127 iio_dev_opaque->trig_readonly = true; 128 - mutex_unlock(&indio_dev->mlock); 128 + mutex_unlock(&iio_dev_opaque->mlock); 129 129 130 130 return 0; 131 131 } ··· 438 438 struct iio_trigger *trig; 439 439 int ret; 440 440 441 - mutex_lock(&indio_dev->mlock); 441 + mutex_lock(&iio_dev_opaque->mlock); 442 442 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { 443 - mutex_unlock(&indio_dev->mlock); 443 + mutex_unlock(&iio_dev_opaque->mlock); 444 444 return -EBUSY; 445 445 } 446 446 if (iio_dev_opaque->trig_readonly) { 447 - mutex_unlock(&indio_dev->mlock); 447 + mutex_unlock(&iio_dev_opaque->mlock); 448 448 return -EPERM; 449 449 } 450 - mutex_unlock(&indio_dev->mlock); 450 + mutex_unlock(&iio_dev_opaque->mlock); 451 451 452 452 trig = iio_trigger_acquire_by_name(buf); 453 453 if (oldtrig == trig) {
+2
include/linux/iio/iio-opaque.h
··· 11 11 * checked by device drivers but should be considered 12 12 * read-only as this is a core internal bit 13 13 * @driver_module: used to make it harder to undercut users 14 + * @mlock: lock used to prevent simultaneous device state changes 14 15 * @mlock_key: lockdep class for iio_dev lock 15 16 * @info_exist_lock: lock to prevent use during removal 16 17 * @trig_readonly: mark the current trigger immutable ··· 44 43 int currentmode; 45 44 int id; 46 45 struct module *driver_module; 46 + struct mutex mlock; 47 47 struct lock_class_key mlock_key; 48 48 struct mutex info_exist_lock; 49 49 bool trig_readonly;
-3
include/linux/iio/iio.h
··· 548 548 * and owner 549 549 * @buffer: [DRIVER] any buffer present 550 550 * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux 551 - * @mlock: [INTERN] lock used to prevent simultaneous device state 552 - * changes 553 551 * @available_scan_masks: [DRIVER] optional array of allowed bitmasks 554 552 * @masklength: [INTERN] the length of the mask established from 555 553 * channels ··· 572 574 573 575 struct iio_buffer *buffer; 574 576 int scan_bytes; 575 - struct mutex mlock; 576 577 577 578 const unsigned long *available_scan_masks; 578 579 unsigned masklength;