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

driver core: Constify API device_find_child() and adapt for various usages

Constify the following API:
struct device *device_find_child(struct device *dev, void *data,
int (*match)(struct device *dev, void *data));
To :
struct device *device_find_child(struct device *dev, const void *data,
device_match_t match);
typedef int (*device_match_t)(struct device *dev, const void *data);
with the following reasons:

- Protect caller's match data @*data which is for comparison and lookup
and the API does not actually need to modify @*data.

- Make the API's parameters (@match)() and @data have the same type as
all of other device finding APIs (bus|class|driver)_find_device().

- All kinds of existing device match functions can be directly taken
as the API's argument, they were exported by driver core.

Constify the API and adapt for various existing usages.

BTW, various subsystem changes are squashed into this commit to meet
'git bisect' requirement, and this commit has the minimal and simplest
changes to complement squashing shortcoming, and that may bring extra
code improvement.

Reviewed-by: Alison Schofield <alison.schofield@intel.com>
Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Acked-by: Uwe Kleine-König <ukleinek@kernel.org> # for drivers/pwm
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Link: https://lore.kernel.org/r/20241224-const_dfc_done-v5-4-6623037414d4@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Zijun Hu and committed by
Greg Kroah-Hartman
f1e8bf56 e9451ab9

+66 -62
+3 -3
arch/sparc/kernel/vio.c
··· 419 419 u64 node; 420 420 }; 421 421 422 - static int vio_md_node_match(struct device *dev, void *arg) 422 + static int vio_md_node_match(struct device *dev, const void *arg) 423 423 { 424 424 struct vio_dev *vdev = to_vio_dev(dev); 425 - struct vio_remove_node_data *node_data; 425 + const struct vio_remove_node_data *node_data; 426 426 u64 node; 427 427 428 - node_data = (struct vio_remove_node_data *)arg; 428 + node_data = (const struct vio_remove_node_data *)arg; 429 429 430 430 node = vio_vdev_node(node_data->hp, vdev); 431 431
+3 -3
drivers/base/core.c
··· 4079 4079 * 4080 4080 * NOTE: you will need to drop the reference with put_device() after use. 4081 4081 */ 4082 - struct device *device_find_child(struct device *parent, void *data, 4083 - int (*match)(struct device *dev, void *data)) 4082 + struct device *device_find_child(struct device *parent, const void *data, 4083 + device_match_t match) 4084 4084 { 4085 4085 struct klist_iter i; 4086 4086 struct device *child; ··· 4125 4125 } 4126 4126 EXPORT_SYMBOL_GPL(device_find_child_by_name); 4127 4127 4128 - static int match_any(struct device *dev, void *unused) 4128 + static int match_any(struct device *dev, const void *unused) 4129 4129 { 4130 4130 return 1; 4131 4131 }
+3 -3
drivers/block/sunvdc.c
··· 918 918 char *type; 919 919 }; 920 920 921 - static int vdc_device_probed(struct device *dev, void *arg) 921 + static int vdc_device_probed(struct device *dev, const void *arg) 922 922 { 923 923 struct vio_dev *vdev = to_vio_dev(dev); 924 - struct vdc_check_port_data *port_data; 924 + const struct vdc_check_port_data *port_data; 925 925 926 - port_data = (struct vdc_check_port_data *)arg; 926 + port_data = (const struct vdc_check_port_data *)arg; 927 927 928 928 if ((vdev->dev_no == port_data->dev_no) && 929 929 (!(strcmp((char *)&vdev->type, port_data->type))) &&
+2 -2
drivers/bus/fsl-mc/dprc-driver.c
··· 112 112 } 113 113 EXPORT_SYMBOL_GPL(dprc_remove_devices); 114 114 115 - static int __fsl_mc_device_match(struct device *dev, void *data) 115 + static int __fsl_mc_device_match(struct device *dev, const void *data) 116 116 { 117 - struct fsl_mc_obj_desc *obj_desc = data; 117 + const struct fsl_mc_obj_desc *obj_desc = data; 118 118 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 119 119 120 120 return fsl_mc_device_match(mc_dev, obj_desc);
+2 -2
drivers/cxl/core/pci.c
··· 252 252 } 253 253 254 254 /* require dvsec ranges to be covered by a locked platform window */ 255 - static int dvsec_range_allowed(struct device *dev, void *arg) 255 + static int dvsec_range_allowed(struct device *dev, const void *arg) 256 256 { 257 - struct range *dev_range = arg; 257 + const struct range *dev_range = arg; 258 258 struct cxl_decoder *cxld; 259 259 260 260 if (!is_root_decoder(dev))
+1 -1
drivers/cxl/core/pmem.c
··· 57 57 } 58 58 EXPORT_SYMBOL_NS_GPL(is_cxl_nvdimm_bridge, "CXL"); 59 59 60 - static int match_nvdimm_bridge(struct device *dev, void *data) 60 + static int match_nvdimm_bridge(struct device *dev, const void *data) 61 61 { 62 62 return is_cxl_nvdimm_bridge(dev); 63 63 }
+12 -9
drivers/cxl/core/region.c
··· 792 792 return 0; 793 793 } 794 794 795 - static int match_free_decoder(struct device *dev, void *data) 795 + static int match_free_decoder(struct device *dev, const void *data) 796 796 { 797 797 struct cxl_port *port = to_cxl_port(dev->parent); 798 798 struct cxl_decoder *cxld; ··· 824 824 return 1; 825 825 } 826 826 827 - static int match_auto_decoder(struct device *dev, void *data) 827 + static int match_auto_decoder(struct device *dev, const void *data) 828 828 { 829 - struct cxl_region_params *p = data; 829 + const struct cxl_region_params *p = data; 830 830 struct cxl_decoder *cxld; 831 831 struct range *r; 832 832 ··· 1722 1722 return port->parent_dport->port; 1723 1723 } 1724 1724 1725 - static int match_switch_decoder_by_range(struct device *dev, void *data) 1725 + static int match_switch_decoder_by_range(struct device *dev, 1726 + const void *data) 1726 1727 { 1727 1728 struct cxl_switch_decoder *cxlsd; 1728 - struct range *r1, *r2 = data; 1729 + const struct range *r1, *r2 = data; 1730 + 1729 1731 1730 1732 if (!is_switch_decoder(dev)) 1731 1733 return 0; ··· 3178 3176 return rc; 3179 3177 } 3180 3178 3181 - static int match_root_decoder_by_range(struct device *dev, void *data) 3179 + static int match_root_decoder_by_range(struct device *dev, 3180 + const void *data) 3182 3181 { 3183 - struct range *r1, *r2 = data; 3182 + const struct range *r1, *r2 = data; 3184 3183 struct cxl_root_decoder *cxlrd; 3185 3184 3186 3185 if (!is_root_decoder(dev)) ··· 3192 3189 return range_contains(r1, r2); 3193 3190 } 3194 3191 3195 - static int match_region_by_range(struct device *dev, void *data) 3192 + static int match_region_by_range(struct device *dev, const void *data) 3196 3193 { 3197 3194 struct cxl_region_params *p; 3198 3195 struct cxl_region *cxlr; 3199 - struct range *r = data; 3196 + const struct range *r = data; 3200 3197 int rc = 0; 3201 3198 3202 3199 if (!is_cxl_region(dev))
+2 -2
drivers/firewire/core-device.c
··· 988 988 return 0; 989 989 } 990 990 991 - static int compare_configuration_rom(struct device *dev, void *data) 991 + static int compare_configuration_rom(struct device *dev, const void *data) 992 992 { 993 993 const struct fw_device *old = fw_device(dev); 994 994 const u32 *config_rom = data; ··· 1039 1039 // 1040 1040 // serialize config_rom access. 1041 1041 scoped_guard(rwsem_read, &fw_device_rwsem) { 1042 - found = device_find_child(card->device, (void *)device->config_rom, 1042 + found = device_find_child(card->device, device->config_rom, 1043 1043 compare_configuration_rom); 1044 1044 } 1045 1045 if (found) {
+2 -2
drivers/firmware/arm_scmi/bus.c
··· 238 238 return 0; 239 239 } 240 240 241 - static int scmi_match_by_id_table(struct device *dev, void *data) 241 + static int scmi_match_by_id_table(struct device *dev, const void *data) 242 242 { 243 243 struct scmi_device *sdev = to_scmi_dev(dev); 244 - struct scmi_device_id *id_table = data; 244 + const struct scmi_device_id *id_table = data; 245 245 246 246 return sdev->protocol_id == id_table->protocol_id && 247 247 (id_table->name && !strcmp(sdev->name, id_table->name));
+2 -2
drivers/firmware/efi/dev-path-parser.c
··· 47 47 return 0; 48 48 } 49 49 50 - static int __init match_pci_dev(struct device *dev, void *data) 50 + static int __init match_pci_dev(struct device *dev, const void *data) 51 51 { 52 - unsigned int devfn = *(unsigned int *)data; 52 + unsigned int devfn = *(const unsigned int *)data; 53 53 54 54 return dev_is_pci(dev) && to_pci_dev(dev)->devfn == devfn; 55 55 }
+1 -1
drivers/gpio/gpio-sim.c
··· 413 413 return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip); 414 414 } 415 415 416 - static int gpio_sim_dev_match_fwnode(struct device *dev, void *data) 416 + static int gpio_sim_dev_match_fwnode(struct device *dev, const void *data) 417 417 { 418 418 return device_match_fwnode(dev, data); 419 419 }
+1 -1
drivers/gpu/drm/mediatek/mtk_drm_drv.c
··· 359 359 }; 360 360 MODULE_DEVICE_TABLE(of, mtk_drm_of_ids); 361 361 362 - static int mtk_drm_match(struct device *dev, void *data) 362 + static int mtk_drm_match(struct device *dev, const void *data) 363 363 { 364 364 if (!strncmp(dev_name(dev), "mediatek-drm", sizeof("mediatek-drm") - 1)) 365 365 return true;
+1 -1
drivers/hwmon/hwmon.c
··· 341 341 342 342 static DEFINE_MUTEX(hwmon_pec_mutex); 343 343 344 - static int hwmon_match_device(struct device *dev, void *data) 344 + static int hwmon_match_device(struct device *dev, const void *data) 345 345 { 346 346 return dev->class == &hwmon_class; 347 347 }
+2 -2
drivers/media/pci/mgb4/mgb4_core.c
··· 123 123 }; 124 124 #endif 125 125 126 - static int match_i2c_adap(struct device *dev, void *data) 126 + static int match_i2c_adap(struct device *dev, const void *data) 127 127 { 128 128 return i2c_verify_adapter(dev) ? 1 : 0; 129 129 } ··· 139 139 return dev ? to_i2c_adapter(dev) : NULL; 140 140 } 141 141 142 - static int match_spi_adap(struct device *dev, void *data) 142 + static int match_spi_adap(struct device *dev, const void *data) 143 143 { 144 144 return to_spi_device(dev) ? 1 : 0; 145 145 }
+1 -1
drivers/nvdimm/bus.c
··· 1212 1212 DIMM_IOCTL, 1213 1213 }; 1214 1214 1215 - static int match_dimm(struct device *dev, void *data) 1215 + static int match_dimm(struct device *dev, const void *data) 1216 1216 { 1217 1217 long id = (long) data; 1218 1218
+1 -1
drivers/pwm/core.c
··· 1276 1276 return 0; 1277 1277 } 1278 1278 1279 - static int pwm_unexport_match(struct device *pwm_dev, void *data) 1279 + static int pwm_unexport_match(struct device *pwm_dev, const void *data) 1280 1280 { 1281 1281 return pwm_from_dev(pwm_dev) == data; 1282 1282 }
+2 -2
drivers/rpmsg/rpmsg_core.c
··· 377 377 * this is used to make sure we're not creating rpmsg devices for channels 378 378 * that already exist. 379 379 */ 380 - static int rpmsg_device_match(struct device *dev, void *data) 380 + static int rpmsg_device_match(struct device *dev, const void *data) 381 381 { 382 - struct rpmsg_channel_info *chinfo = data; 382 + const struct rpmsg_channel_info *chinfo = data; 383 383 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 384 384 385 385 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
+2 -1
drivers/scsi/qla4xxx/ql4_os.c
··· 7189 7189 * 1: if flashnode entry is non-persistent 7190 7190 * 0: if flashnode entry is persistent 7191 7191 **/ 7192 - static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data) 7192 + static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, 7193 + const void *data) 7193 7194 { 7194 7195 struct iscsi_bus_flash_session *fnode_sess; 7195 7196
+5 -5
drivers/scsi/scsi_transport_iscsi.c
··· 1324 1324 * 1 on success 1325 1325 * 0 on failure 1326 1326 */ 1327 - static int iscsi_is_flashnode_conn_dev(struct device *dev, void *data) 1327 + static int iscsi_is_flashnode_conn_dev(struct device *dev, const void *data) 1328 1328 { 1329 1329 return dev->bus == &iscsi_flashnode_bus; 1330 1330 } ··· 1335 1335 return 0; 1336 1336 } 1337 1337 1338 - static int flashnode_match_index(struct device *dev, void *data) 1338 + static int flashnode_match_index(struct device *dev, const void *data) 1339 1339 { 1340 1340 struct iscsi_bus_flash_session *fnode_sess = NULL; 1341 1341 int ret = 0; ··· 1344 1344 goto exit_match_index; 1345 1345 1346 1346 fnode_sess = iscsi_dev_to_flash_session(dev); 1347 - ret = (fnode_sess->target_id == *((int *)data)) ? 1 : 0; 1347 + ret = (fnode_sess->target_id == *((const int *)data)) ? 1 : 0; 1348 1348 1349 1349 exit_match_index: 1350 1350 return ret; ··· 1389 1389 * %NULL on failure 1390 1390 */ 1391 1391 struct device * 1392 - iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data, 1393 - int (*fn)(struct device *dev, void *data)) 1392 + iscsi_find_flashnode_sess(struct Scsi_Host *shost, const void *data, 1393 + device_match_t fn) 1394 1394 { 1395 1395 return device_find_child(&shost->shost_gendev, data, fn); 1396 1396 }
+4 -4
drivers/slimbus/core.c
··· 337 337 a->instance == b->instance); 338 338 } 339 339 340 - static int slim_match_dev(struct device *dev, void *data) 340 + static int slim_match_dev(struct device *dev, const void *data) 341 341 { 342 - struct slim_eaddr *e_addr = data; 342 + const struct slim_eaddr *e_addr = data; 343 343 struct slim_device *sbdev = to_slim_device(dev); 344 344 345 345 return slim_eaddr_equal(&sbdev->e_addr, e_addr); ··· 385 385 } 386 386 EXPORT_SYMBOL_GPL(slim_get_device); 387 387 388 - static int of_slim_match_dev(struct device *dev, void *data) 388 + static int of_slim_match_dev(struct device *dev, const void *data) 389 389 { 390 - struct device_node *np = data; 390 + const struct device_node *np = data; 391 391 struct slim_device *sbdev = to_slim_device(dev); 392 392 393 393 return (sbdev->dev.of_node == np);
+1 -1
drivers/thunderbolt/retimer.c
··· 461 461 u8 index; 462 462 }; 463 463 464 - static int retimer_match(struct device *dev, void *data) 464 + static int retimer_match(struct device *dev, const void *data) 465 465 { 466 466 const struct tb_retimer_lookup *lookup = data; 467 467 struct tb_retimer *rt = tb_to_retimer(dev);
+1 -1
drivers/thunderbolt/xdomain.c
··· 1026 1026 return 0; 1027 1027 } 1028 1028 1029 - static int find_service(struct device *dev, void *data) 1029 + static int find_service(struct device *dev, const void *data) 1030 1030 { 1031 1031 const struct tb_property *p = data; 1032 1032 struct tb_service *svc;
+2 -2
drivers/tty/serial/serial_core.c
··· 2365 2365 struct uart_driver *driver; 2366 2366 }; 2367 2367 2368 - static int serial_match_port(struct device *dev, void *data) 2368 + static int serial_match_port(struct device *dev, const void *data) 2369 2369 { 2370 - struct uart_match *match = data; 2370 + const struct uart_match *match = data; 2371 2371 struct tty_driver *tty_drv = match->driver->tty_driver; 2372 2372 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) + 2373 2373 match->port->line;
+4 -4
drivers/usb/typec/class.c
··· 229 229 /* ------------------------------------------------------------------------- */ 230 230 /* Alternate Modes */ 231 231 232 - static int altmode_match(struct device *dev, void *data) 232 + static int altmode_match(struct device *dev, const void *data) 233 233 { 234 234 struct typec_altmode *adev = to_typec_altmode(dev); 235 - struct typec_device_id *id = data; 235 + const struct typec_device_id *id = data; 236 236 237 237 if (!is_typec_altmode(dev)) 238 238 return 0; ··· 1282 1282 .release = typec_cable_release, 1283 1283 }; 1284 1284 1285 - static int cable_match(struct device *dev, void *data) 1285 + static int cable_match(struct device *dev, const void *data) 1286 1286 { 1287 1287 return is_typec_cable(dev); 1288 1288 } ··· 2028 2028 /* --------------------------------------- */ 2029 2029 /* Driver callbacks to report role updates */ 2030 2030 2031 - static int partner_match(struct device *dev, void *data) 2031 + static int partner_match(struct device *dev, const void *data) 2032 2032 { 2033 2033 return is_typec_partner(dev); 2034 2034 }
+2 -2
include/linux/device.h
··· 1081 1081 int device_for_each_child_reverse_from(struct device *parent, 1082 1082 struct device *from, const void *data, 1083 1083 int (*fn)(struct device *, const void *)); 1084 - struct device *device_find_child(struct device *dev, void *data, 1085 - int (*match)(struct device *dev, void *data)); 1084 + struct device *device_find_child(struct device *dev, const void *data, 1085 + device_match_t match); 1086 1086 struct device *device_find_child_by_name(struct device *parent, 1087 1087 const char *name); 1088 1088 struct device *device_find_any_child(struct device *parent);
+2 -2
include/scsi/scsi_transport_iscsi.h
··· 497 497 extern int iscsi_flashnode_bus_match(struct device *dev, 498 498 const struct device_driver *drv); 499 499 extern struct device * 500 - iscsi_find_flashnode_sess(struct Scsi_Host *shost, void *data, 501 - int (*fn)(struct device *dev, void *data)); 500 + iscsi_find_flashnode_sess(struct Scsi_Host *shost, const void *data, 501 + device_match_t fn); 502 502 extern struct device * 503 503 iscsi_find_flashnode_conn(struct iscsi_bus_flash_session *fnode_sess); 504 504
+1 -1
net/dsa/dsa.c
··· 1367 1367 return dsa_switch_parse_ports_of(ds, dn); 1368 1368 } 1369 1369 1370 - static int dev_is_class(struct device *dev, void *class) 1370 + static int dev_is_class(struct device *dev, const void *class) 1371 1371 { 1372 1372 if (dev->class != NULL && !strcmp(dev->class->name, class)) 1373 1373 return 1;
+1 -1
tools/testing/cxl/test/cxl.c
··· 725 725 cxld->reset = mock_decoder_reset; 726 726 } 727 727 728 - static int first_decoder(struct device *dev, void *data) 728 + static int first_decoder(struct device *dev, const void *data) 729 729 { 730 730 struct cxl_decoder *cxld; 731 731