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: inkern: 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.

While at it, also use __free(kfree) where allocations are done and drop
obvious comment in iio_channel_read_min().

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

authored by

Nuno Sa and committed by
Jonathan Cameron
3092bde7 714b5b4c

+89 -174
+89 -174
drivers/iio/inkern.c
··· 3 3 * 4 4 * Copyright (c) 2011 Jonathan Cameron 5 5 */ 6 + #include <linux/cleanup.h> 6 7 #include <linux/err.h> 7 8 #include <linux/export.h> 8 9 #include <linux/minmax.h> ··· 44 43 45 44 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps) 46 45 { 47 - int i = 0, ret = 0; 48 46 struct iio_map_internal *mapi; 47 + int i = 0; 48 + int ret; 49 49 50 50 if (!maps) 51 51 return 0; 52 52 53 - mutex_lock(&iio_map_list_lock); 53 + guard(mutex)(&iio_map_list_lock); 54 54 while (maps[i].consumer_dev_name) { 55 55 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL); 56 56 if (!mapi) { ··· 63 61 list_add_tail(&mapi->l, &iio_map_list); 64 62 i++; 65 63 } 66 - error_ret: 67 - if (ret) 68 - iio_map_array_unregister_locked(indio_dev); 69 - mutex_unlock(&iio_map_list_lock); 70 64 65 + return 0; 66 + error_ret: 67 + iio_map_array_unregister_locked(indio_dev); 71 68 return ret; 72 69 } 73 70 EXPORT_SYMBOL_GPL(iio_map_array_register); ··· 76 75 */ 77 76 int iio_map_array_unregister(struct iio_dev *indio_dev) 78 77 { 79 - int ret; 80 - 81 - mutex_lock(&iio_map_list_lock); 82 - ret = iio_map_array_unregister_locked(indio_dev); 83 - mutex_unlock(&iio_map_list_lock); 84 - 85 - return ret; 78 + guard(mutex)(&iio_map_list_lock); 79 + return iio_map_array_unregister_locked(indio_dev); 86 80 } 87 81 EXPORT_SYMBOL_GPL(iio_map_array_unregister); 88 82 ··· 179 183 static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode, 180 184 int index) 181 185 { 182 - struct iio_channel *channel; 183 186 int err; 184 187 185 188 if (index < 0) 186 189 return ERR_PTR(-EINVAL); 187 190 188 - channel = kzalloc(sizeof(*channel), GFP_KERNEL); 191 + struct iio_channel *channel __free(kfree) = 192 + kzalloc(sizeof(*channel), GFP_KERNEL); 189 193 if (!channel) 190 194 return ERR_PTR(-ENOMEM); 191 195 192 196 err = __fwnode_iio_channel_get(channel, fwnode, index); 193 197 if (err) 194 - goto err_free_channel; 198 + return ERR_PTR(err); 195 199 196 - return channel; 197 - 198 - err_free_channel: 199 - kfree(channel); 200 - return ERR_PTR(err); 200 + return_ptr(channel); 201 201 } 202 202 203 203 static struct iio_channel * ··· 283 291 static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev) 284 292 { 285 293 struct fwnode_handle *fwnode = dev_fwnode(dev); 286 - struct iio_channel *chans; 287 294 int i, mapind, nummaps = 0; 288 295 int ret; 289 296 ··· 298 307 return ERR_PTR(-ENODEV); 299 308 300 309 /* NULL terminated array to save passing size */ 301 - chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 310 + struct iio_channel *chans __free(kfree) = 311 + kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 302 312 if (!chans) 303 313 return ERR_PTR(-ENOMEM); 304 314 ··· 309 317 if (ret) 310 318 goto error_free_chans; 311 319 } 312 - return chans; 320 + return_ptr(chans); 313 321 314 322 error_free_chans: 315 323 for (i = 0; i < mapind; i++) 316 324 iio_device_put(chans[i].indio_dev); 317 - kfree(chans); 318 325 return ERR_PTR(ret); 319 326 } 320 327 ··· 321 330 const char *channel_name) 322 331 { 323 332 struct iio_map_internal *c_i = NULL, *c = NULL; 324 - struct iio_channel *channel; 325 333 int err; 326 334 327 335 if (!(name || channel_name)) 328 336 return ERR_PTR(-ENODEV); 329 337 330 338 /* first find matching entry the channel map */ 331 - mutex_lock(&iio_map_list_lock); 332 - list_for_each_entry(c_i, &iio_map_list, l) { 333 - if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || 334 - (channel_name && 335 - strcmp(channel_name, c_i->map->consumer_channel) != 0)) 336 - continue; 337 - c = c_i; 338 - iio_device_get(c->indio_dev); 339 - break; 339 + scoped_guard(mutex, &iio_map_list_lock) { 340 + list_for_each_entry(c_i, &iio_map_list, l) { 341 + if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || 342 + (channel_name && 343 + strcmp(channel_name, c_i->map->consumer_channel) != 0)) 344 + continue; 345 + c = c_i; 346 + iio_device_get(c->indio_dev); 347 + break; 348 + } 340 349 } 341 - mutex_unlock(&iio_map_list_lock); 342 350 if (!c) 343 351 return ERR_PTR(-ENODEV); 344 352 345 - channel = kzalloc(sizeof(*channel), GFP_KERNEL); 353 + struct iio_channel *channel __free(kfree) = 354 + kzalloc(sizeof(*channel), GFP_KERNEL); 346 355 if (!channel) { 347 356 err = -ENOMEM; 348 357 goto error_no_mem; ··· 357 366 358 367 if (!channel->channel) { 359 368 err = -EINVAL; 360 - goto error_no_chan; 369 + goto error_no_mem; 361 370 } 362 371 } 363 372 364 - return channel; 373 + return_ptr(channel); 365 374 366 - error_no_chan: 367 - kfree(channel); 368 375 error_no_mem: 369 376 iio_device_put(c->indio_dev); 370 377 return ERR_PTR(err); ··· 439 450 struct iio_channel *iio_channel_get_all(struct device *dev) 440 451 { 441 452 const char *name; 442 - struct iio_channel *chans; 443 453 struct iio_map_internal *c = NULL; 454 + struct iio_channel *fw_chans; 444 455 int nummaps = 0; 445 456 int mapind = 0; 446 457 int i, ret; ··· 448 459 if (!dev) 449 460 return ERR_PTR(-EINVAL); 450 461 451 - chans = fwnode_iio_channel_get_all(dev); 462 + fw_chans = fwnode_iio_channel_get_all(dev); 452 463 /* 453 464 * We only want to carry on if the error is -ENODEV. Anything else 454 465 * should be reported up the stack. 455 466 */ 456 - if (!IS_ERR(chans) || PTR_ERR(chans) != -ENODEV) 457 - return chans; 467 + if (!IS_ERR(fw_chans) || PTR_ERR(fw_chans) != -ENODEV) 468 + return fw_chans; 458 469 459 470 name = dev_name(dev); 460 471 461 - mutex_lock(&iio_map_list_lock); 472 + guard(mutex)(&iio_map_list_lock); 462 473 /* first count the matching maps */ 463 474 list_for_each_entry(c, &iio_map_list, l) 464 475 if (name && strcmp(name, c->map->consumer_dev_name) != 0) ··· 466 477 else 467 478 nummaps++; 468 479 469 - if (nummaps == 0) { 470 - ret = -ENODEV; 471 - goto error_ret; 472 - } 480 + if (nummaps == 0) 481 + return ERR_PTR(-ENODEV); 473 482 474 483 /* NULL terminated array to save passing size */ 475 - chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 476 - if (!chans) { 477 - ret = -ENOMEM; 478 - goto error_ret; 479 - } 484 + struct iio_channel *chans __free(kfree) = 485 + kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 486 + if (!chans) 487 + return ERR_PTR(-ENOMEM); 480 488 481 489 /* for each map fill in the chans element */ 482 490 list_for_each_entry(c, &iio_map_list, l) { ··· 495 509 ret = -ENODEV; 496 510 goto error_free_chans; 497 511 } 498 - mutex_unlock(&iio_map_list_lock); 499 512 500 - return chans; 513 + return_ptr(chans); 501 514 502 515 error_free_chans: 503 516 for (i = 0; i < nummaps; i++) 504 517 iio_device_put(chans[i].indio_dev); 505 - kfree(chans); 506 - error_ret: 507 - mutex_unlock(&iio_map_list_lock); 508 - 509 518 return ERR_PTR(ret); 510 519 } 511 520 EXPORT_SYMBOL_GPL(iio_channel_get_all); ··· 571 590 int iio_read_channel_raw(struct iio_channel *chan, int *val) 572 591 { 573 592 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 574 - int ret; 575 593 576 - mutex_lock(&iio_dev_opaque->info_exist_lock); 577 - if (!chan->indio_dev->info) { 578 - ret = -ENODEV; 579 - goto err_unlock; 580 - } 594 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 595 + if (!chan->indio_dev->info) 596 + return -ENODEV; 581 597 582 - ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 583 - err_unlock: 584 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 585 - 586 - return ret; 598 + return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 587 599 } 588 600 EXPORT_SYMBOL_GPL(iio_read_channel_raw); 589 601 590 602 int iio_read_channel_average_raw(struct iio_channel *chan, int *val) 591 603 { 592 604 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 593 - int ret; 594 605 595 - mutex_lock(&iio_dev_opaque->info_exist_lock); 596 - if (!chan->indio_dev->info) { 597 - ret = -ENODEV; 598 - goto err_unlock; 599 - } 606 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 607 + if (!chan->indio_dev->info) 608 + return -ENODEV; 600 609 601 - ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); 602 - err_unlock: 603 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 604 - 605 - return ret; 610 + return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); 606 611 } 607 612 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); 608 613 ··· 675 708 int *processed, unsigned int scale) 676 709 { 677 710 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 678 - int ret; 679 711 680 - mutex_lock(&iio_dev_opaque->info_exist_lock); 681 - if (!chan->indio_dev->info) { 682 - ret = -ENODEV; 683 - goto err_unlock; 684 - } 712 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 713 + if (!chan->indio_dev->info) 714 + return -ENODEV; 685 715 686 - ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, 687 - scale); 688 - err_unlock: 689 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 690 - 691 - return ret; 716 + return iio_convert_raw_to_processed_unlocked(chan, raw, processed, 717 + scale); 692 718 } 693 719 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); 694 720 ··· 689 729 enum iio_chan_info_enum attribute) 690 730 { 691 731 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 692 - int ret; 693 732 694 - mutex_lock(&iio_dev_opaque->info_exist_lock); 695 - if (!chan->indio_dev->info) { 696 - ret = -ENODEV; 697 - goto err_unlock; 698 - } 733 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 734 + if (!chan->indio_dev->info) 735 + return -ENODEV; 699 736 700 - ret = iio_channel_read(chan, val, val2, attribute); 701 - err_unlock: 702 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 703 - 704 - return ret; 737 + return iio_channel_read(chan, val, val2, attribute); 705 738 } 706 739 EXPORT_SYMBOL_GPL(iio_read_channel_attribute); 707 740 ··· 710 757 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 711 758 int ret; 712 759 713 - mutex_lock(&iio_dev_opaque->info_exist_lock); 714 - if (!chan->indio_dev->info) { 715 - ret = -ENODEV; 716 - goto err_unlock; 717 - } 760 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 761 + if (!chan->indio_dev->info) 762 + return -ENODEV; 718 763 719 764 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { 720 765 ret = iio_channel_read(chan, val, NULL, 721 766 IIO_CHAN_INFO_PROCESSED); 722 767 if (ret < 0) 723 - goto err_unlock; 768 + return ret; 724 769 *val *= scale; 770 + 771 + return 0; 725 772 } else { 726 773 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 727 774 if (ret < 0) 728 - goto err_unlock; 729 - ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 730 - scale); 775 + return ret; 776 + 777 + return iio_convert_raw_to_processed_unlocked(chan, *val, val, 778 + scale); 731 779 } 732 - 733 - err_unlock: 734 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 735 - 736 - return ret; 737 780 } 738 781 EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale); 739 782 ··· 762 813 enum iio_chan_info_enum attribute) 763 814 { 764 815 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 765 - int ret; 766 816 767 - mutex_lock(&iio_dev_opaque->info_exist_lock); 768 - if (!chan->indio_dev->info) { 769 - ret = -ENODEV; 770 - goto err_unlock; 771 - } 817 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 818 + if (!chan->indio_dev->info) 819 + return -ENODEV; 772 820 773 - ret = iio_channel_read_avail(chan, vals, type, length, attribute); 774 - err_unlock: 775 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 776 - 777 - return ret; 821 + return iio_channel_read_avail(chan, vals, type, length, attribute); 778 822 } 779 823 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute); 780 824 ··· 834 892 int iio_read_max_channel_raw(struct iio_channel *chan, int *val) 835 893 { 836 894 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 837 - int ret; 838 895 int type; 839 896 840 - mutex_lock(&iio_dev_opaque->info_exist_lock); 841 - if (!chan->indio_dev->info) { 842 - ret = -ENODEV; 843 - goto err_unlock; 844 - } 897 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 898 + if (!chan->indio_dev->info) 899 + return -ENODEV; 845 900 846 - ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 847 - err_unlock: 848 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 849 - 850 - return ret; 901 + return iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 851 902 } 852 903 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); 853 904 ··· 890 955 int iio_read_min_channel_raw(struct iio_channel *chan, int *val) 891 956 { 892 957 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 893 - int ret; 894 958 int type; 895 959 896 - mutex_lock(&iio_dev_opaque->info_exist_lock); 897 - if (!chan->indio_dev->info) { 898 - ret = -ENODEV; 899 - goto err_unlock; 900 - } 960 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 961 + if (!chan->indio_dev->info) 962 + return -ENODEV; 901 963 902 - ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 903 - err_unlock: 904 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 905 - 906 - return ret; 964 + return iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 907 965 } 908 966 EXPORT_SYMBOL_GPL(iio_read_min_channel_raw); 909 967 910 968 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) 911 969 { 912 970 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 913 - int ret = 0; 914 - /* Need to verify underlying driver has not gone away */ 915 971 916 - mutex_lock(&iio_dev_opaque->info_exist_lock); 917 - if (!chan->indio_dev->info) { 918 - ret = -ENODEV; 919 - goto err_unlock; 920 - } 972 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 973 + if (!chan->indio_dev->info) 974 + return -ENODEV; 921 975 922 976 *type = chan->channel->type; 923 - err_unlock: 924 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 925 977 926 - return ret; 978 + return 0; 927 979 } 928 980 EXPORT_SYMBOL_GPL(iio_get_channel_type); 929 981 ··· 925 1003 enum iio_chan_info_enum attribute) 926 1004 { 927 1005 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 928 - int ret; 929 1006 930 - mutex_lock(&iio_dev_opaque->info_exist_lock); 931 - if (!chan->indio_dev->info) { 932 - ret = -ENODEV; 933 - goto err_unlock; 934 - } 1007 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 1008 + if (!chan->indio_dev->info) 1009 + return -ENODEV; 935 1010 936 - ret = iio_channel_write(chan, val, val2, attribute); 937 - err_unlock: 938 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 939 - 940 - return ret; 1011 + return iio_channel_write(chan, val, val2, attribute); 941 1012 } 942 1013 EXPORT_SYMBOL_GPL(iio_write_channel_attribute); 943 1014