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

Configure Feed

Select the types of activity you want to include in your feed.

iio: buffer: iio: core: move to the cleanup.h magic

Use the new cleanup magic for handling mutexes in IIO. This allows us to
greatly simplify some code paths.

Signed-off-by: Nuno Sa <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20240229-iio-use-cleanup-magic-v3-3-c3d34889ae3c@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Nuno Sa and committed by
Jonathan Cameron
714b5b4c 095be2d5

+48 -74
+48 -74
drivers/iio/industrialio-buffer.c
··· 10 10 * - Alternative access techniques? 11 11 */ 12 12 #include <linux/anon_inodes.h> 13 + #include <linux/cleanup.h> 13 14 #include <linux/kernel.h> 14 15 #include <linux/export.h> 15 16 #include <linux/device.h> ··· 534 533 ret = kstrtobool(buf, &state); 535 534 if (ret < 0) 536 535 return ret; 537 - mutex_lock(&iio_dev_opaque->mlock); 538 - if (iio_buffer_is_active(buffer)) { 539 - ret = -EBUSY; 540 - goto error_ret; 541 - } 536 + 537 + guard(mutex)(&iio_dev_opaque->mlock); 538 + if (iio_buffer_is_active(buffer)) 539 + return -EBUSY; 540 + 542 541 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); 543 542 if (ret < 0) 544 - goto error_ret; 545 - if (!state && ret) { 546 - ret = iio_scan_mask_clear(buffer, this_attr->address); 547 - if (ret) 548 - goto error_ret; 549 - } else if (state && !ret) { 543 + return ret; 544 + 545 + if (state && ret) 546 + return len; 547 + 548 + if (state) 550 549 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); 551 - if (ret) 552 - goto error_ret; 553 - } 550 + else 551 + ret = iio_scan_mask_clear(buffer, this_attr->address); 552 + if (ret) 553 + return ret; 554 554 555 - error_ret: 556 - mutex_unlock(&iio_dev_opaque->mlock); 557 - 558 - return ret < 0 ? ret : len; 555 + return len; 559 556 } 560 557 561 558 static ssize_t iio_scan_el_ts_show(struct device *dev, ··· 580 581 if (ret < 0) 581 582 return ret; 582 583 583 - mutex_lock(&iio_dev_opaque->mlock); 584 - if (iio_buffer_is_active(buffer)) { 585 - ret = -EBUSY; 586 - goto error_ret; 587 - } 588 - buffer->scan_timestamp = state; 589 - error_ret: 590 - mutex_unlock(&iio_dev_opaque->mlock); 584 + guard(mutex)(&iio_dev_opaque->mlock); 585 + if (iio_buffer_is_active(buffer)) 586 + return -EBUSY; 591 587 592 - return ret ? ret : len; 588 + buffer->scan_timestamp = state; 589 + 590 + return len; 593 591 } 594 592 595 593 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, ··· 670 674 if (val == buffer->length) 671 675 return len; 672 676 673 - mutex_lock(&iio_dev_opaque->mlock); 674 - if (iio_buffer_is_active(buffer)) { 675 - ret = -EBUSY; 676 - } else { 677 - buffer->access->set_length(buffer, val); 678 - ret = 0; 679 - } 680 - if (ret) 681 - goto out; 677 + guard(mutex)(&iio_dev_opaque->mlock); 678 + if (iio_buffer_is_active(buffer)) 679 + return -EBUSY; 680 + 681 + buffer->access->set_length(buffer, val); 682 + 682 683 if (buffer->length && buffer->length < buffer->watermark) 683 684 buffer->watermark = buffer->length; 684 - out: 685 - mutex_unlock(&iio_dev_opaque->mlock); 686 685 687 - return ret ? ret : len; 686 + return len; 688 687 } 689 688 690 689 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, ··· 1259 1268 struct iio_buffer *remove_buffer) 1260 1269 { 1261 1270 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1262 - int ret; 1263 1271 1264 1272 if (insert_buffer == remove_buffer) 1265 1273 return 0; ··· 1267 1277 insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT) 1268 1278 return -EINVAL; 1269 1279 1270 - mutex_lock(&iio_dev_opaque->info_exist_lock); 1271 - mutex_lock(&iio_dev_opaque->mlock); 1280 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 1281 + guard(mutex)(&iio_dev_opaque->mlock); 1272 1282 1273 1283 if (insert_buffer && iio_buffer_is_active(insert_buffer)) 1274 1284 insert_buffer = NULL; ··· 1276 1286 if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 1277 1287 remove_buffer = NULL; 1278 1288 1279 - if (!insert_buffer && !remove_buffer) { 1280 - ret = 0; 1281 - goto out_unlock; 1282 - } 1289 + if (!insert_buffer && !remove_buffer) 1290 + return 0; 1283 1291 1284 - if (!indio_dev->info) { 1285 - ret = -ENODEV; 1286 - goto out_unlock; 1287 - } 1292 + if (!indio_dev->info) 1293 + return -ENODEV; 1288 1294 1289 - ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 1290 - 1291 - out_unlock: 1292 - mutex_unlock(&iio_dev_opaque->mlock); 1293 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 1294 - 1295 - return ret; 1295 + return __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 1296 1296 } 1297 1297 EXPORT_SYMBOL_GPL(iio_update_buffers); 1298 1298 ··· 1306 1326 if (ret < 0) 1307 1327 return ret; 1308 1328 1309 - mutex_lock(&iio_dev_opaque->mlock); 1329 + guard(mutex)(&iio_dev_opaque->mlock); 1310 1330 1311 1331 /* Find out if it is in the list */ 1312 1332 inlist = iio_buffer_is_active(buffer); 1313 1333 /* Already in desired state */ 1314 1334 if (inlist == requested_state) 1315 - goto done; 1335 + return len; 1316 1336 1317 1337 if (requested_state) 1318 1338 ret = __iio_update_buffers(indio_dev, buffer, NULL); 1319 1339 else 1320 1340 ret = __iio_update_buffers(indio_dev, NULL, buffer); 1341 + if (ret) 1342 + return ret; 1321 1343 1322 - done: 1323 - mutex_unlock(&iio_dev_opaque->mlock); 1324 - return (ret < 0) ? ret : len; 1344 + return len; 1325 1345 } 1326 1346 1327 1347 static ssize_t watermark_show(struct device *dev, struct device_attribute *attr, ··· 1348 1368 if (!val) 1349 1369 return -EINVAL; 1350 1370 1351 - mutex_lock(&iio_dev_opaque->mlock); 1371 + guard(mutex)(&iio_dev_opaque->mlock); 1352 1372 1353 - if (val > buffer->length) { 1354 - ret = -EINVAL; 1355 - goto out; 1356 - } 1373 + if (val > buffer->length) 1374 + return -EINVAL; 1357 1375 1358 - if (iio_buffer_is_active(buffer)) { 1359 - ret = -EBUSY; 1360 - goto out; 1361 - } 1376 + if (iio_buffer_is_active(buffer)) 1377 + return -EBUSY; 1362 1378 1363 1379 buffer->watermark = val; 1364 - out: 1365 - mutex_unlock(&iio_dev_opaque->mlock); 1366 1380 1367 - return ret ? ret : len; 1381 + return len; 1368 1382 } 1369 1383 1370 1384 static ssize_t data_available_show(struct device *dev,