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

thermal core: convert ID allocation to IDA

The thermal core does not use the ability to look up pointers by ID, so
convert it from using an IDR to the more space-efficient IDA.

Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
Signed-off-by: Zhang Rui <rui.zhang@intel.com>

authored by

Matthew Wilcox and committed by
Zhang Rui
b31ef828 0c744ea4

+28 -51
+26 -49
drivers/thermal/thermal_core.c
··· 36 36 MODULE_DESCRIPTION("Generic thermal management sysfs support"); 37 37 MODULE_LICENSE("GPL v2"); 38 38 39 - static DEFINE_IDR(thermal_tz_idr); 40 - static DEFINE_IDR(thermal_cdev_idr); 41 - static DEFINE_MUTEX(thermal_idr_lock); 39 + static DEFINE_IDA(thermal_tz_ida); 40 + static DEFINE_IDA(thermal_cdev_ida); 42 41 43 42 static LIST_HEAD(thermal_tz_list); 44 43 static LIST_HEAD(thermal_cdev_list); ··· 588 589 * - thermal zone devices lifecycle: registration, unregistration, 589 590 * binding, and unbinding. 590 591 */ 591 - static int get_idr(struct idr *idr, struct mutex *lock, int *id) 592 - { 593 - int ret; 594 - 595 - if (lock) 596 - mutex_lock(lock); 597 - ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); 598 - if (lock) 599 - mutex_unlock(lock); 600 - if (unlikely(ret < 0)) 601 - return ret; 602 - *id = ret; 603 - return 0; 604 - } 605 - 606 - static void release_idr(struct idr *idr, struct mutex *lock, int id) 607 - { 608 - if (lock) 609 - mutex_lock(lock); 610 - idr_remove(idr, id); 611 - if (lock) 612 - mutex_unlock(lock); 613 - } 614 592 615 593 /** 616 594 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone ··· 661 685 dev->target = THERMAL_NO_TARGET; 662 686 dev->weight = weight; 663 687 664 - result = get_idr(&tz->idr, &tz->lock, &dev->id); 665 - if (result) 688 + result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL); 689 + if (result < 0) 666 690 goto free_mem; 667 691 692 + dev->id = result; 668 693 sprintf(dev->name, "cdev%d", dev->id); 669 694 result = 670 695 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); 671 696 if (result) 672 - goto release_idr; 697 + goto release_ida; 673 698 674 699 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); 675 700 sysfs_attr_init(&dev->attr.attr); ··· 714 737 device_remove_file(&tz->device, &dev->attr); 715 738 remove_symbol_link: 716 739 sysfs_remove_link(&tz->device.kobj, dev->name); 717 - release_idr: 718 - release_idr(&tz->idr, &tz->lock, dev->id); 740 + release_ida: 741 + ida_simple_remove(&tz->ida, dev->id); 719 742 free_mem: 720 743 kfree(dev); 721 744 return result; ··· 762 785 device_remove_file(&tz->device, &pos->weight_attr); 763 786 device_remove_file(&tz->device, &pos->attr); 764 787 sysfs_remove_link(&tz->device.kobj, pos->name); 765 - release_idr(&tz->idr, &tz->lock, pos->id); 788 + ida_simple_remove(&tz->ida, pos->id); 766 789 kfree(pos); 767 790 return 0; 768 791 } ··· 897 920 if (!cdev) 898 921 return ERR_PTR(-ENOMEM); 899 922 900 - result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); 901 - if (result) { 923 + result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL); 924 + if (result < 0) { 902 925 kfree(cdev); 903 926 return ERR_PTR(result); 904 927 } 905 928 929 + cdev->id = result; 906 930 strlcpy(cdev->type, type ? : "", sizeof(cdev->type)); 907 931 mutex_init(&cdev->lock); 908 932 INIT_LIST_HEAD(&cdev->thermal_instances); ··· 916 938 dev_set_name(&cdev->device, "cooling_device%d", cdev->id); 917 939 result = device_register(&cdev->device); 918 940 if (result) { 919 - release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); 941 + ida_simple_remove(&thermal_cdev_ida, cdev->id); 920 942 kfree(cdev); 921 943 return ERR_PTR(result); 922 944 } ··· 1043 1065 1044 1066 mutex_unlock(&thermal_list_lock); 1045 1067 1046 - release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); 1068 + ida_simple_remove(&thermal_cdev_ida, cdev->id); 1047 1069 device_unregister(&cdev->device); 1048 1070 } 1049 1071 EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); ··· 1145 1167 return ERR_PTR(-ENOMEM); 1146 1168 1147 1169 INIT_LIST_HEAD(&tz->thermal_instances); 1148 - idr_init(&tz->idr); 1170 + ida_init(&tz->ida); 1149 1171 mutex_init(&tz->lock); 1150 - result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); 1151 - if (result) { 1172 + result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL); 1173 + if (result < 0) { 1152 1174 kfree(tz); 1153 1175 return ERR_PTR(result); 1154 1176 } 1155 1177 1178 + tz->id = result; 1156 1179 strlcpy(tz->type, type, sizeof(tz->type)); 1157 1180 tz->ops = ops; 1158 1181 tz->tzp = tzp; ··· 1175 1196 dev_set_name(&tz->device, "thermal_zone%d", tz->id); 1176 1197 result = device_register(&tz->device); 1177 1198 if (result) { 1178 - release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); 1199 + ida_simple_remove(&thermal_tz_ida, tz->id); 1179 1200 kfree(tz); 1180 1201 return ERR_PTR(result); 1181 1202 } ··· 1229 1250 return tz; 1230 1251 1231 1252 unregister: 1232 - release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); 1253 + ida_simple_remove(&thermal_tz_ida, tz->id); 1233 1254 device_unregister(&tz->device); 1234 1255 return ERR_PTR(result); 1235 1256 } ··· 1291 1312 thermal_set_governor(tz, NULL); 1292 1313 1293 1314 thermal_remove_hwmon_sysfs(tz); 1294 - release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); 1295 - idr_destroy(&tz->idr); 1315 + ida_simple_remove(&thermal_tz_ida, tz->id); 1316 + ida_destroy(&tz->ida); 1296 1317 mutex_destroy(&tz->lock); 1297 1318 device_unregister(&tz->device); 1298 1319 kfree(tz->device.groups); ··· 1493 1514 unregister_governors: 1494 1515 thermal_unregister_governors(); 1495 1516 error: 1496 - idr_destroy(&thermal_tz_idr); 1497 - idr_destroy(&thermal_cdev_idr); 1498 - mutex_destroy(&thermal_idr_lock); 1517 + ida_destroy(&thermal_tz_ida); 1518 + ida_destroy(&thermal_cdev_ida); 1499 1519 mutex_destroy(&thermal_list_lock); 1500 1520 mutex_destroy(&thermal_governor_lock); 1501 1521 return result; ··· 1507 1529 genetlink_exit(); 1508 1530 class_unregister(&thermal_class); 1509 1531 thermal_unregister_governors(); 1510 - idr_destroy(&thermal_tz_idr); 1511 - idr_destroy(&thermal_cdev_idr); 1512 - mutex_destroy(&thermal_idr_lock); 1532 + ida_destroy(&thermal_tz_ida); 1533 + ida_destroy(&thermal_cdev_ida); 1513 1534 mutex_destroy(&thermal_list_lock); 1514 1535 mutex_destroy(&thermal_governor_lock); 1515 1536 }
+2 -2
include/linux/thermal.h
··· 194 194 * @governor: pointer to the governor for this thermal zone 195 195 * @governor_data: private pointer for governor data 196 196 * @thermal_instances: list of &struct thermal_instance of this thermal zone 197 - * @idr: &struct idr to generate unique id for this zone's cooling 197 + * @ida: &struct ida to generate unique id for this zone's cooling 198 198 * devices 199 199 * @lock: lock to protect thermal_instances list 200 200 * @node: node in thermal_tz_list (in thermal_core.c) ··· 227 227 struct thermal_governor *governor; 228 228 void *governor_data; 229 229 struct list_head thermal_instances; 230 - struct idr idr; 230 + struct ida ida; 231 231 struct mutex lock; 232 232 struct list_head node; 233 233 struct delayed_work poll_queue;